@devries-cameron20 / t3code Mirror · read-only / commits / f87ecf0cc3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 10:15 UTC

fix(client): keep warm thread resumes live instead of flashing sync (#9913)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>

Julius Marminge committed Sep 5, 2026 at 00:15 UTC f87ecf0cc307f974745da8eea856fab5711023f3
2 files changed +110 -5
packages/client-runtime/src/state/threads-atoms.test.ts
+89 -2
@@ -27,6 +27,7 @@ import {
27 PrimaryConnectionTarget,
28 type NetworkStatus,
29 type PreparedConnection,
30 + type SupervisorConnectionState,
31 } from "../connection/model.ts";
32 import { EnvironmentSupervisor } from "../connection/supervisor.ts";
33 import { EnvironmentCacheStore } from "../platform/persistence.ts";
@@ -73,8 +74,18 @@ const THREAD: OrchestrationThread = {
74 };
75 const SNAPSHOT: OrchestrationThreadDetailSnapshot = { snapshotSequence: 7, thread: THREAD };
76
77 +const CONNECTED_STATE: SupervisorConnectionState = {
78 + ...AVAILABLE_CONNECTION_STATE,
79 + desired: true,
80 + network: "online",
81 + phase: "connected",
82 + attempt: 1,
83 + generation: 1,
84 +};
85 +
86 const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?: {
87 readonly snapshot?: OrchestrationThreadDetailSnapshot;
88 + readonly connected?: boolean;
89 }) {
90 const subscriptions = yield* Queue.unbounded<{
91 readonly afterSequence: number | undefined;
@@ -123,10 +134,14 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
134 probe: Effect.void,
135 closed: Effect.never,
136 };
137 + const connectionState = yield* SubscriptionRef.make(
138 + options?.connected ? CONNECTED_STATE : AVAILABLE_CONNECTION_STATE,
139 + );
140 + const sessionRef = yield* SubscriptionRef.make(Option.some(session));
141 const supervisor = EnvironmentSupervisor.of({
142 target: TARGET,
128 - state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
129 - session: yield* SubscriptionRef.make(Option.some(session)),
143 + state: connectionState,
144 + session: sessionRef,
145 prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(
146 Option.some({
147 environmentId: TARGET.environmentId,
@@ -228,6 +243,9 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
243 ref,
244 subscriptions,
245 olderLoads,
246 + connectionState,
247 + session,
248 + sessionRef,
249 counts: () => ({ httpLoads, diskLoads, opened, active }),
250 };
251 });
@@ -319,6 +337,75 @@ describe("createEnvironmentThreadStateAtoms", () => {
337 }),
338 );
339
340 + it.effect.each([
341 + { replayed: false, statuses: ["live"] },
342 + { replayed: true, statuses: ["live", "synchronizing", "live"] },
343 + ])(
344 + "keeps a warm resume live until it replays events (replayed: $replayed)",
345 + ({ replayed, statuses }) =>
346 + Effect.gen(function* () {
347 + const h = yield* makeHarness({ connected: true });
348 + const unmount = h.registry.mount(h.stateAtom);
349 + const first = yield* Queue.take(h.subscriptions);
350 + yield* Queue.offer(first.events, { kind: "synchronized" });
351 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live");
352 + unmount();
353 + yield* Deferred.await(first.closed);
354 +
355 + const observed: Array<EnvironmentThreadState["status"]> = [];
356 + const stop = h.registry.subscribe(h.stateAtom, (state) => observed.push(state.status), {
357 + immediate: true,
358 + });
359 + const remount = h.registry.mount(h.stateAtom);
360 + const next = yield* Queue.take(h.subscriptions);
361 + expect(next.afterSequence).toBe(7);
362 + if (replayed) {
363 + yield* Queue.offer(next.events, {
364 + kind: "snapshot",
365 + snapshot: { snapshotSequence: 9, thread: { ...THREAD, title: "Replayed" } },
366 + });
367 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "synchronizing");
368 + }
369 + yield* Queue.offer(next.events, { kind: "synchronized" });
370 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live");
371 + expect(observed.filter((status, index) => observed[index - 1] !== status)).toEqual(
372 + statuses,
373 + );
374 + stop();
375 + remount();
376 + yield* Deferred.await(next.closed);
377 + }),
378 + );
379 +
380 + it.effect("downgrades a warm resume when the connection dropped while away", () =>
381 + Effect.gen(function* () {
382 + const h = yield* makeHarness({ connected: true });
383 + const unmount = h.registry.mount(h.stateAtom);
384 + const first = yield* Queue.take(h.subscriptions);
385 + yield* Queue.offer(first.events, { kind: "synchronized" });
386 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live");
387 + unmount();
388 + yield* Deferred.await(first.closed);
389 +
390 + yield* SubscriptionRef.set(h.sessionRef, Option.none());
391 + yield* SubscriptionRef.set(h.connectionState, AVAILABLE_CONNECTION_STATE);
392 + const remount = h.registry.mount(h.stateAtom);
393 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "cached");
394 + expect(currentThread(h.registry, h.stateAtom)).toBe(THREAD);
395 + expect(h.counts().opened).toBe(1);
396 +
397 + yield* SubscriptionRef.set(h.connectionState, CONNECTED_STATE);
398 + yield* SubscriptionRef.set(h.sessionRef, Option.some(h.session));
399 + const next = yield* Queue.take(h.subscriptions);
400 + expect(next.afterSequence).toBe(7);
401 + expect(h.registry.get(h.stateAtom).status).toBe("synchronizing");
402 + yield* Queue.offer(next.events, { kind: "synchronized" });
403 + yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live");
404 + remount();
405 + yield* Deferred.await(next.closed);
406 + }),
407 + );
408 +
409 it.effect("keeps warm data when the raw atom family's weak entry is collected", () =>
410 Effect.gen(function* () {
411 const h = yield* makeHarness();
packages/client-runtime/src/state/threads.ts
+21 -3
@@ -158,10 +158,18 @@ function matchesThreadSnapshot(
158 currentPage.hasMore === page.hasMore;
159 }
160
161 +// A retained "live" state stays live: the cursor resume that follows only
162 +// replays what the thread missed, and on servers that send the completion
163 +// marker the first replayed event moves the status to "synchronizing" on its
164 +// own. Downgrading here would flash a sync label on every return to a
165 +// recently viewed thread.
166 function cachedThreadState(value: EnvironmentThreadState): EnvironmentThreadState {
167 return {
168 ...value,
164 - status: value.status === "deleted" ? "deleted" : statusWithoutLiveData(value.data),
169 + status:
170 + value.status === "deleted" || (value.status === "live" && Option.isSome(value.data))
171 + ? value.status
172 + : statusWithoutLiveData(value.data),
173 error: Option.none(),
174 page: Option.map(value.page, (page) => ({ ...page, loadingOlder: false })),
175 };
@@ -647,7 +655,16 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
655 service.changes.pipe(Stream.filter(ConnectionWakeups.shouldResubscribeAfterWakeup)),
656 });
657
650 - yield* setSynchronizing;
658 + // Only the first subscription after a warm live resume keeps the retained
659 + // status. A replacement session or foreground resubscribe on the same scope
660 + // may have missed events, so those show sync progress until confirmed.
661 + const resumingLive = yield* Ref.make(initialState.status === "live");
662 + const markSynchronizing = Effect.gen(function* () {
663 + if (yield* Ref.get(resumingLive)) return;
664 + yield* setSynchronizing;
665 + });
666 +
667 + yield* markSynchronizing;
668 yield* Effect.forkScoped(
669 subscribeDynamic(
670 ORCHESTRATION_WS_METHODS.subscribeThread,
@@ -668,7 +685,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
685 const supportsPagination = config.threadSnapshotPagination === true;
686 yield* Ref.set(paginationSupported, supportsPagination);
687 yield* Ref.set(awaitingCompletion, supportsCompletionMarker);
671 - yield* setSynchronizing;
688 + yield* markSynchronizing;
689 + yield* Ref.set(resumingLive, false);
690
691 let current = yield* SubscriptionRef.get(state);
692 // A windowed cache resuming against a server without pagination is a