| 1 | import assert from "node:assert/strict"; |
| 2 | import React, { act } from "react"; |
| 3 | import { createRoot } from "react-dom/client"; |
| 4 | import { installRemoteSurfaceDom } from "./helpers/remoteSurfaceDom"; |
| 5 | import { installDesktopHostStub } from "./desktopHostStub"; |
| 6 | import type { FollowRequest, TranscriptFollowResponse } from "../generated/desktopContract.generated"; |
| 7 | |
| 8 | const dom = installRemoteSurfaceDom(); |
| 9 | let reads = 0, revision = 1; |
| 10 | let failNext = false; |
| 11 | const pending: Array<(response: TranscriptFollowResponse) => void> = []; |
| 12 | const status = { running: false, label: "Model", plan: false, toolApprovalMode: "ask", goal: "", takenOver: false }; |
| 13 | const stub = installDesktopHostStub({ |
| 14 | RemoteTabHydrationState: async () => ({ state: "ready", hydrationRevision: revision }), |
| 15 | RemoteTabStatus: async () => status, |
| 16 | RemoteTabMetadata: async () => ({ status }), |
| 17 | RemoteTabSnapshot: async () => ({ status, history: [] }), |
| 18 | ForkTargetsRemoteTab: async () => ({ targets: [], verifiable: false }), |
| 19 | RemoteTranscriptFollowForTab: async (_id: string, request: FollowRequest): Promise<TranscriptFollowResponse> => { |
| 20 | if (request.close) return { protocolVersion: 2, subscription: request.subscription ?? "", changes: [], resetRequired: false }; |
| 21 | if (request.subscription) return new Promise(() => {}); |
| 22 | reads++; |
| 23 | if (failNext) { failNext = false; throw new Error("synthetic baseline failure"); } |
| 24 | return new Promise(resolve => pending.push(resolve)); |
| 25 | }, |
| 26 | }); |
| 27 | const { useRemoteSession } = await import("../lib/useRemoteSession"); |
| 28 | const { setTranscriptBindingIdentity } = await import("../lib/canonicalTranscriptBackend"); |
| 29 | setTranscriptBindingIdentity(() => "remote"); |
| 30 | let probe!: ReturnType<typeof useRemoteSession>; |
| 31 | function Probe() { probe = useRemoteSession("remote-a", "ready", "session-a"); return null; } |
| 32 | const root = createRoot(document.getElementById("root")!); |
| 33 | async function drain() { await act(async () => { for (let i = 0; i < 40; i++) await new Promise(resolve => setImmediate(resolve)); }); } |
| 34 | const ready = (value: number | undefined) => stub.emit("remote-tab:remote-a:state", { state: "ready", hydrationRevision: value }); |
| 35 | const response = (id: string): TranscriptFollowResponse => ({ protocolVersion: 2, subscription: id, changes: [], resetRequired: false, |
| 36 | snapshot: { protocolVersion: 1, snapshotId: id, identity: { sessionId: id, runtimeEpoch: id, rewriteEpoch: 0, headId: "" }, |
| 37 | projectionRevision: 1, coveredThroughSeq: 1, durableSeq: 1, records: [], activeRecords: [], activeAttempts: [], |
| 38 | runtime: { status: "completed", pendingEvents: [], samplingCount: 0, toolCount: 0 }, before: 0, hasOlder: false, |
| 39 | totalRecords: 0, totalTurns: 0, stale: false }, |
| 40 | history: { status: "ready", snapshotSequence: 1, coverageSequence: 1, generation: id, totalTurns: 0, |
| 41 | hasOlder: false, hasNewer: false, messages: [] } }); |
| 42 | try { |
| 43 | await act(async () => root.render(<Probe />)); |
| 44 | await drain(); |
| 45 | assert.equal(reads, 1); |
| 46 | await act(async () => { ready(1); ready(1); }); |
| 47 | await drain(); |
| 48 | assert.equal(reads, 1, "mount and duplicate ready notifications share one pending baseline"); |
| 49 | await act(async () => pending.shift()!(response("first"))); |
| 50 | await drain(); |
| 51 | assert.equal(probe.hydrated, true); |
| 52 | await act(async () => ready(1)); |
| 53 | await drain(); |
| 54 | assert.equal(reads, 1, "late duplicate does not replace an installed subscription"); |
| 55 | await act(async () => { revision = 2; ready(2); }); |
| 56 | await drain(); |
| 57 | assert.equal(reads, 2, "a real ready-to-ready reset starts a new baseline"); |
| 58 | const stale = pending.shift()!; |
| 59 | await act(async () => { revision = 3; ready(3); ready(2); ready(3); }); |
| 60 | await drain(); |
| 61 | assert.equal(reads, 3, "a newer reset supersedes the old request without accepting replayed barriers"); |
| 62 | await act(async () => stale(response("stale"))); |
| 63 | await drain(); |
| 64 | await act(async () => pending.shift()!(response("latest"))); |
| 65 | await drain(); |
| 66 | assert.equal(probe.transcript.transcriptSessionId, "latest"); |
| 67 | await act(async () => ready(undefined)); |
| 68 | await drain(); |
| 69 | assert.equal(reads, 4, "older hosts without a revision retain explicit ready reset semantics"); |
| 70 | await act(async () => pending.shift()!(response("legacy"))); |
| 71 | await drain(); |
| 72 | await act(async () => { failNext = true; ready(4); }); |
| 73 | await drain(); |
| 74 | assert.equal(reads, 5); |
| 75 | assert.match(probe.error, /synthetic baseline failure/); |
| 76 | await act(async () => ready(4)); |
| 77 | await drain(); |
| 78 | assert.equal(reads, 6, "failure releases ownership so the same barrier can retry"); |
| 79 | await act(async () => pending.shift()!(response("recovered"))); |
| 80 | await drain(); |
| 81 | assert.equal(probe.error, ""); |
| 82 | await act(async () => ready(5)); |
| 83 | await drain(); |
| 84 | const disconnected = pending.shift()!; |
| 85 | await act(async () => stub.emit("remote-tab:remote-a:state", { state: "disconnected", hydrationRevision: 6 })); |
| 86 | await act(async () => disconnected(response("obsolete"))); |
| 87 | await drain(); |
| 88 | assert.equal(probe.state, "disconnected", "late success cannot resurrect a disconnected surface"); |
| 89 | assert.equal(probe.transcript.transcriptSessionId, "recovered"); |
| 90 | await act(async () => { ready(5); ready(7); }); |
| 91 | await drain(); |
| 92 | assert.equal(reads, 8); |
| 93 | await act(async () => pending.shift()!(response("reconnected"))); |
| 94 | await drain(); |
| 95 | assert.equal(probe.transcript.transcriptSessionId, "reconnected"); |
| 96 | await act(async () => stub.emitServiceState({ phase: "stopping", generation: "test-service" })); |
| 97 | await act(async () => { |
| 98 | revision = 1; |
| 99 | stub.emitServiceState({ phase: "ready", generation: "replacement-service" }); |
| 100 | stub.emit("desktop:resync", {}); |
| 101 | ready(1); |
| 102 | }); |
| 103 | await drain(); |
| 104 | assert.equal(reads, 9, "a surviving renderer accepts revision 1 from the replacement service"); |
| 105 | await act(async () => pending.shift()!(response("restarted-service"))); |
| 106 | await drain(); |
| 107 | assert.equal(probe.transcript.transcriptSessionId, "restarted-service"); |
| 108 | await act(async () => { |
| 109 | stub.emitServiceState({ phase: "ready", generation: "third-service" }); |
| 110 | stub.emit("desktop:resync", {}); |
| 111 | }); |
| 112 | await drain(); |
| 113 | assert.equal(reads, 10, "a changed service generation also fences a missed shutdown event"); |
| 114 | await act(async () => pending.shift()!(response("third-service"))); |
| 115 | await drain(); |
| 116 | await act(async () => { stub.emit("desktop:resync", {}); ready(1); }); |
| 117 | await drain(); |
| 118 | assert.equal(reads, 10, "repeated resync within the same service preserves its installed baseline"); |
| 119 | let oldState!: (value: { state: string; hydrationRevision: number }) => void; |
| 120 | let stateReads = 0; |
| 121 | stub.commands.RemoteTabHydrationState = async () => ++stateReads === 1 |
| 122 | ? new Promise(resolve => { oldState = resolve; }) : { state: "ready", hydrationRevision: 1 }; |
| 123 | await act(async () => stub.emit("desktop:resync", {})); |
| 124 | await drain(); |
| 125 | await act(async () => { |
| 126 | stub.emitServiceState({ phase: "ready", generation: "fourth-service" }); |
| 127 | stub.emit("desktop:resync", {}); |
| 128 | }); |
| 129 | await drain(); |
| 130 | await act(async () => oldState({ state: "ready", hydrationRevision: 99 })); |
| 131 | await drain(); |
| 132 | assert.equal(reads, 11, "a stale state RPC cannot install the retired service's higher revision"); |
| 133 | await act(async () => pending.shift()!(response("fourth-service"))); |
| 134 | await drain(); |
| 135 | assert.equal(probe.transcript.transcriptSessionId, "fourth-service"); |
| 136 | console.log("PASS remote hydration owns one baseline per authoritative ready revision"); |
| 137 | } finally { |
| 138 | await act(async () => root.unmount()); |
| 139 | dom.window.close(); |
| 140 | } |
| 141 |