Skip to content

Commit b832332

Browse files
fix(client): a mounted thread keeps asking after an early 404 until it exists
A thread detail load could get thread_not_found before the thread was created. The miss marked the thread deleted and parked the subscription, so the open thread stayed blank until the user navigated away and back. A miss now asks HTTP again with the existing capped backoff while the thread stays mounted; a delete event still parks it for good. AI-assisted; verified with the tests and tools listed above.
1 parent 3996498 commit b832332

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

‎packages/client-runtime/src/state/threads-sync.test.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
118118
readonly latestLocalTurnOrdinal?: number | null;
119119
};
120120
readonly httpSnapshot?: ThreadSnapshotLoadResult;
121+
/** Answers per load call; the last one repeats. Overrides `httpSnapshot`. */
122+
readonly httpSnapshots?: readonly ThreadSnapshotLoadResult[];
121123
/** The test sends the completion marker itself instead of the fake server sending it first. */
122124
readonly completionMarker?: boolean;
123125
readonly resumeCache?: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]>;
@@ -193,11 +195,12 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
193195
);
194196
const snapshotLoader = ThreadSnapshotLoader.ThreadSnapshotLoader.of({
195197
load: (_prepared, threadId) =>
196-
Ref.update(loaderCalls, (count) => count + 1).pipe(
197-
Effect.as(
198+
Ref.updateAndGet(loaderCalls, (count) => count + 1).pipe(
199+
Effect.map((count) =>
198200
threadId === THREAD_ID
199-
? (options?.httpSnapshot ??
200-
({ _tag: "unavailable" } satisfies ThreadSnapshotLoadResult))
201+
? (options?.httpSnapshots?.[Math.min(count, options.httpSnapshots.length) - 1] ??
202+
options?.httpSnapshot ??
203+
({ _tag: "unavailable" } satisfies ThreadSnapshotLoadResult))
201204
: ({ _tag: "unavailable" } satisfies ThreadSnapshotLoadResult),
202205
),
203206
),
@@ -798,6 +801,27 @@ describe("EnvironmentThreads", () => {
798801
}),
799802
);
800803

804+
it.effect("keeps asking a mounted thread over HTTP after a miss until it exists", () =>
805+
Effect.gen(function* () {
806+
const harness = yield* makeHarness({
807+
httpSnapshots: [
808+
{ _tag: "missing" },
809+
{ _tag: "missing" },
810+
{
811+
_tag: "present",
812+
snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection: BASE_PROJECTION },
813+
},
814+
],
815+
});
816+
yield* awaitThreadState(harness.observed, (value) => value.status === "deleted");
817+
yield* TestClock.adjust("250 millis");
818+
yield* TestClock.adjust("500 millis");
819+
const state = yield* awaitThreadState(harness.observed, (value) => value.status === "live");
820+
expect(Option.getOrThrow(state.data)).toEqual(BASE_PROJECTION);
821+
expect(yield* Ref.get(harness.loaderCalls)).toBe(3);
822+
}),
823+
);
824+
801825
it.effect.each([
802826
// The HTTP retry decides, so a missing thread never flashes an error.
803827
{

‎packages/client-runtime/src/state/threads.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
977977
});
978978

979979
yield* markSynchronizing;
980+
// A 404 can arrive before the thread exists, so a miss asks HTTP again while mounted.
981+
let httpMisses = 0;
980982
let defectRetries = 0;
981983
const backOffAfterDefect = Effect.suspend(() => {
982984
const retryDelay = subscriptionRetryDelay(THREAD_SUBSCRIPTION_RETRY_DELAY, defectRetries);
@@ -994,10 +996,8 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
994996
ORCHESTRATION_V2_WS_METHODS.subscribeThread,
995997
Effect.fn("EnvironmentThreadState.makeSubscribeInput")(function* (session) {
996998
let current = yield* SubscriptionRef.get(state);
997-
// A prior definitive miss (or delete event) already cleared this thread.
998-
// Park the subscription attempt without opening the socket so we do not
999-
// retry forever against a known-missing id.
1000-
if (current.status === "deleted") {
999+
// A delete event already cleared this thread. Park without opening the socket.
1000+
if (current.status === "deleted" && httpMisses === 0) {
10011001
return yield* Effect.never;
10021002
}
10031003

@@ -1034,6 +1034,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10341034
yield* snapshotLoader.load(prepared, threadId);
10351035
switch (httpResult._tag) {
10361036
case "present": {
1037+
httpMisses = 0;
10371038
if (canLoadHistory && httpResult.history !== undefined) {
10381039
yield* Ref.set(acceptsBoundedSocketSnapshots, true);
10391040
}
@@ -1062,9 +1063,13 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10621063
break;
10631064
}
10641065
case "missing": {
1065-
yield* setDeleted();
1066+
if (current.status !== "deleted") yield* setDeleted();
10661067
// A 404 also means "not created yet". Only a delete event may outlive this mount.
10671068
if (resumeCache?.owner === owner) resumeCache.snapshot = undefined;
1069+
yield* Effect.sleep(
1070+
subscriptionRetryDelay(THREAD_SUBSCRIPTION_RETRY_DELAY, httpMisses++),
1071+
);
1072+
yield* Queue.offer(resubscribes, undefined);
10681073
return yield* Effect.never;
10691074
}
10701075
case "unavailable": {

0 commit comments

Comments
 (0)