From 6b64cffdc0648640c7f6a93095b4af1ab24867dc Mon Sep 17 00:00:00 2001 From: jieyuexing Date: Wed, 18 Nov 2026 10:13:10 +0800 Subject: [PATCH] fix(client): a new thread shows its first reply as soon as it is sent A draft's mounted thread state probes /bounded before the thread exists. Since #25919 each 404 schedules another probe with backoff that grows to 30 s, and nothing wakes it when the first send creates the thread. A user who spends a minute writing the first message then sees "Send a message to start the conversation." for up to 30 s after sending. startTurn now tells the thread's mounted state when a send created the thread, and a state that is backing off after a miss resubscribes at once. States that never missed are left alone, so sends to existing threads do not resync. Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Opus 5.5 --- .../src/state/threadCommands.test.ts | 1 + .../src/state/threadCommands.ts | 16 +++++++++- .../src/state/threadHistoryController.test.ts | 1 + .../src/state/threadHistoryController.ts | 15 ++++++++++ .../src/state/threads-sync.test.ts | 30 +++++++++++++++++++ packages/client-runtime/src/state/threads.ts | 14 ++++++--- 6 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/client-runtime/src/state/threadCommands.test.ts b/packages/client-runtime/src/state/threadCommands.test.ts index 031ef68a14502..54324fea2569a 100644 --- a/packages/client-runtime/src/state/threadCommands.test.ts +++ b/packages/client-runtime/src/state/threadCommands.test.ts @@ -151,6 +151,7 @@ const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* (optio loadEarlier: () => options?.loadEarlier?.() ?? Effect.die("unused"), reconcilePendingWork: (_environmentId, threadId) => options?.reconcilePendingWork?.(threadId) ?? Queue.offer(reconciled, threadId), + threadCreated: () => Effect.void, }), ), ), diff --git a/packages/client-runtime/src/state/threadCommands.ts b/packages/client-runtime/src/state/threadCommands.ts index 41b74e9f2ac78..677d408792f9e 100644 --- a/packages/client-runtime/src/state/threadCommands.ts +++ b/packages/client-runtime/src/state/threadCommands.ts @@ -23,6 +23,7 @@ import { import { waitForTitleRegenerationOutcome } from "./threadTitleRegeneration.ts"; import { type InterruptThreadTurnInput, + type StartThreadTurnInput, type UpdateThreadMetadataInput, archiveThread, closeGuestShare, @@ -126,6 +127,19 @@ export function createThreadEnvironmentAtoms( ); }); + // A draft's thread appears only when its first send commits; tell its mounted state so a + // long "not found" backoff ends now. + const startTurn = Effect.fn("ThreadCommands.startTurn")(function* (input: StartThreadTurnInput) { + const result = yield* startThreadTurn(input); + if (input.bootstrap?.createThread === undefined) return result; + const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor; + const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController); + if (Option.isSome(controller)) { + yield* controller.value.threadCreated(supervisor.target.environmentId, input.threadId); + } + return result; + }); + const armTitleRegeneration = Effect.fn("ThreadCommands.armTitleRegeneration")(function* (input: { readonly threadId: ThreadId; }) { @@ -321,7 +335,7 @@ export function createThreadEnvironmentAtoms( }), startTurn: createEnvironmentCommand(runtime, { label: "environment-data:commands:thread:start-turn", - execute: startThreadTurn, + execute: startTurn, scheduler, concurrency, }), diff --git a/packages/client-runtime/src/state/threadHistoryController.test.ts b/packages/client-runtime/src/state/threadHistoryController.test.ts index 3d6505b02dd37..fd6dab38af3cf 100644 --- a/packages/client-runtime/src/state/threadHistoryController.test.ts +++ b/packages/client-runtime/src/state/threadHistoryController.test.ts @@ -25,6 +25,7 @@ function handler( } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult); }, reconcilePendingWork: () => Effect.void, + threadCreated: () => Effect.void, }; } diff --git a/packages/client-runtime/src/state/threadHistoryController.ts b/packages/client-runtime/src/state/threadHistoryController.ts index 1c8818fd54db1..6b63ff198b77a 100644 --- a/packages/client-runtime/src/state/threadHistoryController.ts +++ b/packages/client-runtime/src/state/threadHistoryController.ts @@ -18,6 +18,7 @@ export type ThreadHistoryHandler = { workAnchorId?: string, ) => Effect.Effect; readonly reconcilePendingWork: () => Effect.Effect; + readonly threadCreated: () => Effect.Effect; }; export type ThreadHistoryRegistration = { @@ -45,6 +46,10 @@ export class ThreadHistoryController extends Context.Service< environmentId: EnvironmentId, threadId: ThreadId, ) => Effect.Effect; + readonly threadCreated: ( + environmentId: EnvironmentId, + threadId: ThreadId, + ) => Effect.Effect; } >()("@t3tools/client-runtime/state/threadHistoryController") {} @@ -108,6 +113,16 @@ export const layer: Layer.Layer = Layer.effect( ); }, ), + threadCreated: Effect.fn("ThreadHistoryController.threadCreated")( + function (environmentId, threadId) { + return Ref.get(handlers).pipe( + Effect.flatMap( + (current) => + current.get(threadKey({ environmentId, threadId }))?.threadCreated() ?? Effect.void, + ), + ); + }, + ), }); }), ); diff --git a/packages/client-runtime/src/state/threads-sync.test.ts b/packages/client-runtime/src/state/threads-sync.test.ts index 5a771646f03fe..6591ce18beefa 100644 --- a/packages/client-runtime/src/state/threads-sync.test.ts +++ b/packages/client-runtime/src/state/threads-sync.test.ts @@ -294,6 +294,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o loadEarlier: (throughEntryId?: string) => historyController.loadEarlier(TARGET.environmentId, THREAD_ID, throughEntryId), reconcilePendingWork: historyController.reconcilePendingWork(TARGET.environmentId, THREAD_ID), + threadCreated: historyController.threadCreated(TARGET.environmentId, THREAD_ID), inputs, observed, latest, @@ -823,6 +824,35 @@ describe("EnvironmentThreads", () => { }), ); + it.effect("subscribes at once when the thread is created while a long miss backoff sleeps", () => + Effect.gen(function* () { + const harness = yield* makeHarness({ + httpSnapshots: [ + ...Array.from({ length: 5 }, () => ({ _tag: "missing" }) as const), + { + _tag: "present", + snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection: BASE_PROJECTION }, + }, + ], + }); + yield* awaitThreadState(harness.observed, (value) => value.status === "deleted"); + for (const delay of ["250 millis", "500 millis", "1 second", "2 seconds"] as const) { + yield* TestClock.adjust(delay); + } + expect(yield* Ref.get(harness.loaderCalls)).toBe(5); + + yield* harness.threadCreated; + const state = yield* awaitThreadState(harness.observed, (value) => value.status === "live"); + expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION); + expect(yield* Ref.get(harness.loaderCalls)).toBe(6); + + // The replaced backoff must not probe or resubscribe again. + yield* TestClock.adjust("60 seconds"); + expect(yield* Ref.get(harness.loaderCalls)).toBe(6); + expect(yield* SubscriptionRef.get(harness.subscriptionCount)).toBe(1); + }), + ); + it.effect.each([ // The HTTP retry decides, so a missing thread never flashes an error. { diff --git a/packages/client-runtime/src/state/threads.ts b/packages/client-runtime/src/state/threads.ts index b21e688162cd3..6064f3ac62533 100644 --- a/packages/client-runtime/src/state/threads.ts +++ b/packages/client-runtime/src/state/threads.ts @@ -922,6 +922,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make }, ); + // A stream defect parks the subscription until something resubscribes it. + const resubscribes = yield* Queue.unbounded(); + // A 404 can arrive before the thread exists, so a miss asks HTTP again while mounted. + let httpMisses = 0; + if (Option.isSome(historyController)) { const scope = yield* Scope.Scope; const registration = yield* historyController.value.register(environmentId, threadId, { @@ -936,6 +941,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make Effect.forkIn(scope), Effect.flatMap(Fiber.join), ), + // A send created the thread: end the miss backoff now instead of at its next probe. + threadCreated: () => + Effect.suspend(() => + httpMisses > 0 ? Queue.offer(resubscribes, undefined) : Effect.void, + ).pipe(Effect.asVoid), }); yield* Effect.addFinalizer(() => historyController.value.unregister(registration)); } @@ -974,8 +984,6 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make (yield* Ref.get(socketOnlyOpen)) && Option.isNone((yield* SubscriptionRef.get(state)).data) ); }); - // A stream defect parks the subscription until something resubscribes it. - const resubscribes = yield* Queue.unbounded(); const markSynchronizing = Effect.gen(function* () { if (yield* Ref.get(resumingLive)) return; yield* SubscriptionRef.update(state, (current) => @@ -986,8 +994,6 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make }); yield* markSynchronizing; - // A 404 can arrive before the thread exists, so a miss asks HTTP again while mounted. - let httpMisses = 0; let defectRetries = 0; const backOffAfterDefect = Effect.suspend(() => { const retryDelay = subscriptionRetryDelay(THREAD_SUBSCRIPTION_RETRY_DELAY, defectRetries);