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 22, 18:15 UTC
fix(client-runtime): Stop clears commands already finished on the server (#23814)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 23, 2026 at 20:15 UTC
d31269e46b32f359b58b4f040ed7d36863fedad3
8 files changed
+745
-10
knip.jsonc
+1
-1
@@ -23,7 +23,7 @@
23
// electron-builder loads the macOS signer by path.
24
"entry": [
25
// These fixture scripts are injected into a browser page by path.
26
- "fixtures/issue-16534/*.js",
26
+ "fixtures/*/*.js",
27
"knip-schemas.ts",
28
"mobile-native-client.ts",
29
"setup-worktree.ts",
packages/client-runtime/src/state/threadCommands.test.ts
+76
-2
@@ -3,6 +3,7 @@ import {
3
CommandId,
4
RuntimeRequestId,
5
EnvironmentId,
6
+ RunId,
7
ORCHESTRATION_V2_WS_METHODS,
8
ProjectId,
9
ProviderInstanceId,
@@ -14,9 +15,12 @@ import * as DateTime from "effect/DateTime";
15
import * as Crypto from "effect/Crypto";
16
import * as Deferred from "effect/Deferred";
17
import * as Effect from "effect/Effect";
18
+import * as Exit from "effect/Exit";
19
+import * as Fiber from "effect/Fiber";
20
import * as Layer from "effect/Layer";
21
import * as Option from "effect/Option";
22
import * as Queue from "effect/Queue";
23
+import * as Scope from "effect/Scope";
24
import * as SubscriptionRef from "effect/SubscriptionRef";
25
import { Atom, AtomRegistry } from "effect/reactivity";
26
@@ -24,6 +28,7 @@ import * as EnvironmentRegistry from "../connection/registry.ts";
28
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
29
import type { RpcSession } from "../rpc/session.ts";
30
import { createThreadEnvironmentAtoms } from "./threadCommands.ts";
31
+import * as ThreadHistoryController from "./threadHistoryController.ts";
32
33
const ENVIRONMENT_ID = EnvironmentId.make("remote");
34
const THREAD_ID = ThreadId.make("thread");
@@ -80,11 +85,14 @@ const SNAPSHOT: OrchestrationV2ShellSnapshot = {
85
],
86
};
87
83
-const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* () {
88
+const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* (options?: {
89
+ readonly reconcilePendingWork?: (threadId: ThreadId) => Effect.Effect<void>;
90
+}) {
91
const requests = yield* Queue.unbounded<{
92
command: OrchestrationV2Command;
93
reply: Deferred.Deferred<{ sequence: number }, Error>;
94
}>();
95
+ const reconciled = yield* Queue.unbounded<ThreadId>();
96
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
97
target: { environmentId: ENVIRONMENT_ID },
98
session: yield* SubscriptionRef.make(
@@ -113,6 +121,16 @@ const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* () {
121
digest: (_algorithm, data) => Effect.succeed(data),
122
}),
123
),
124
+ Layer.succeed(
125
+ ThreadHistoryController.ThreadHistoryController,
126
+ ThreadHistoryController.ThreadHistoryController.of({
127
+ register: () => Effect.die("unused"),
128
+ unregister: () => Effect.void,
129
+ loadEarlier: () => Effect.die("unused"),
130
+ reconcilePendingWork: (_environmentId, threadId) =>
131
+ options?.reconcilePendingWork?.(threadId) ?? Queue.offer(reconciled, threadId),
132
+ }),
133
+ ),
134
),
135
);
136
const snapshotAtom = Atom.family((_environmentId: EnvironmentId) => Atom.make(SNAPSHOT));
@@ -121,7 +139,63 @@ const makeHarness = Effect.fn("TestThreadCommands.makeHarness")(function* () {
139
yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
140
const visibleAtom = commands.snapshotAtom(ENVIRONMENT_ID);
141
registry.mount(visibleAtom);
124
- return { registry, commands, snapshotAtom, visibleAtom, requests };
142
+ return { registry, commands, snapshotAtom, visibleAtom, requests, reconciled };
143
+});
144
+
145
+describe("interrupt turn", () => {
146
+ // A stale cached item has nothing left to interrupt, so the server can reject Stop (#18188).
147
+ it.effect("reconciles pending work when the server rejects the interrupt", () =>
148
+ Effect.gen(function* () {
149
+ const h = yield* makeHarness();
150
+ const result = h.commands.interruptTurn.run(h.registry, {
151
+ environmentId: ENVIRONMENT_ID,
152
+ input: { threadId: THREAD_ID, runId: RunId.make("finished-run") },
153
+ });
154
+ const request = yield* Queue.take(h.requests);
155
+ expect(request.command.type).toBe("run.interrupt");
156
+ yield* Deferred.fail(request.reply, new Error("Run is not interruptible"));
157
+ expect((yield* Effect.promise(() => result))._tag).toBe("Failure");
158
+ expect(yield* Queue.take(h.reconciled)).toBe(THREAD_ID);
159
+ }),
160
+ );
161
+
162
+ // Mirrors the thread state's handler: the lookup runs in the thread's scope and is joined.
163
+ const stopWithBlockedLookup = Effect.fn("stopWithBlockedLookup")(function* () {
164
+ const threadScope = yield* Scope.make();
165
+ const asked = yield* Deferred.make<void>();
166
+ const h = yield* makeHarness({
167
+ reconcilePendingWork: () =>
168
+ Deferred.succeed(asked, undefined).pipe(
169
+ Effect.andThen(Effect.never),
170
+ Effect.forkIn(threadScope),
171
+ Effect.flatMap(Fiber.join),
172
+ ),
173
+ });
174
+ const result = h.commands.interruptTurn.run(h.registry, {
175
+ environmentId: ENVIRONMENT_ID,
176
+ input: { threadId: THREAD_ID, runId: RunId.make("running-run") },
177
+ });
178
+ const request = yield* Queue.take(h.requests);
179
+ yield* Deferred.succeed(request.reply, { sequence: 1 });
180
+ yield* Deferred.await(asked);
181
+ return { threadScope, result };
182
+ });
183
+
184
+ it.effect("resolves Stop without waiting for the pending work lookup", () =>
185
+ Effect.gen(function* () {
186
+ const { threadScope, result } = yield* stopWithBlockedLookup();
187
+ expect((yield* Effect.promise(() => result))._tag).toBe("Success");
188
+ yield* Scope.close(threadScope, Exit.void);
189
+ }),
190
+ );
191
+
192
+ it.effect("keeps an accepted Stop when the thread closes during the lookup", () =>
193
+ Effect.gen(function* () {
194
+ const { threadScope, result } = yield* stopWithBlockedLookup();
195
+ yield* Scope.close(threadScope, Exit.void);
196
+ expect((yield* Effect.promise(() => result))._tag).toBe("Success");
197
+ }),
198
+ );
199
});
200
201
describe("remote thread lifecycle commands", () => {
packages/client-runtime/src/state/threadCommands.ts
+23
-1
@@ -166,6 +166,28 @@ export function createThreadEnvironmentAtoms<R, E>(
166
);
167
});
168
169
+ // Runs whether or not the server accepted the interrupt, so a stale cached item still
170
+ // settles when Stop had nothing to interrupt; the interrupt's own failure still surfaces.
171
+ // The repair is detached: its lookups must not hold the thread's serial command key, and
172
+ // closing the thread cancels the repair, not the Stop that triggered it.
173
+ const interruptTurn = Effect.fn("ThreadCommands.interruptTurn")(function* (
174
+ input: InterruptThreadTurnInput,
175
+ ) {
176
+ const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
177
+ const controller = yield* Effect.serviceOption(ThreadHistoryController.ThreadHistoryController);
178
+ return yield* interruptThreadTurn(input).pipe(
179
+ Effect.ensuring(
180
+ Option.match(controller, {
181
+ onNone: () => Effect.void,
182
+ onSome: (value) =>
183
+ value
184
+ .reconcilePendingWork(supervisor.target.environmentId, input.threadId)
185
+ .pipe(Effect.forkDetach, Effect.asVoid),
186
+ }),
187
+ ),
188
+ );
189
+ });
190
+
191
const commands = {
192
create: createEnvironmentCommand(runtime, {
193
label: "environment-data:commands:thread:create",
@@ -301,7 +323,7 @@ export function createThreadEnvironmentAtoms<R, E>(
323
}),
324
interruptTurn: createEnvironmentCommand(runtime, {
325
label: "environment-data:commands:thread:interrupt-turn",
304
- execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
326
+ execute: interruptTurn,
327
scheduler,
328
concurrency,
329
}),
packages/client-runtime/src/state/threadHistoryController.test.ts
+1
@@ -24,6 +24,7 @@ function handler(
24
_tag: "loaded",
25
} satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult);
26
},
27
+ reconcilePendingWork: () => Effect.void,
28
};
29
}
30
packages/client-runtime/src/state/threadHistoryController.ts
+13
@@ -14,6 +14,7 @@ export type ThreadHistoryLoadEarlierResult =
14
15
export type ThreadHistoryHandler = {
16
readonly loadEarlier: (throughEntryId?: string) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
17
+ readonly reconcilePendingWork: () => Effect.Effect<void>;
18
};
19
20
export type ThreadHistoryRegistration = {
@@ -36,6 +37,10 @@ export class ThreadHistoryController extends Context.Service<
37
threadId: ThreadId,
38
throughEntryId?: string,
39
) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
40
+ readonly reconcilePendingWork: (
41
+ environmentId: EnvironmentId,
42
+ threadId: ThreadId,
43
+ ) => Effect.Effect<void>;
44
}
45
>()("@t3tools/client-runtime/state/threadHistoryController") {}
46
@@ -81,6 +86,14 @@ export const layer: Layer.Layer<ThreadHistoryController> = Layer.effect(
86
return handler.loadEarlier(throughEntryId);
87
}),
88
),
89
+ reconcilePendingWork: (environmentId, threadId) =>
90
+ Ref.get(handlers).pipe(
91
+ Effect.flatMap(
92
+ (current) =>
93
+ current.get(threadKey({ environmentId, threadId }))?.reconcilePendingWork() ??
94
+ Effect.void,
95
+ ),
96
+ ),
97
});
98
}),
99
);
packages/client-runtime/src/state/threads-sync.test.ts
+452
@@ -24,6 +24,7 @@ import * as Stream from "effect/Stream";
24
import * as SubscriptionRef from "effect/SubscriptionRef";
25
import * as TestClock from "effect/testing/TestClock";
26
import { HttpClient, HttpClientResponse } from "effect/http";
27
+import { pendingBackgroundTurnItems } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
28
29
import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
30
import {
@@ -37,6 +38,7 @@ import * as EnvironmentSupervisor from "../connection/supervisor.ts";
38
import * as Persistence from "../platform/persistence.ts";
39
import * as RpcSession from "../rpc/session.ts";
40
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
41
+import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
42
import * as ThreadHistoryController from "./threadHistoryController.ts";
43
import {
44
EMPTY_ENVIRONMENT_THREAD_STATE,
@@ -112,6 +114,9 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
114
readonly saveThread?: Persistence.EnvironmentCacheStore["Service"]["saveThread"];
115
readonly historyPaging?: "enabled" | "no-http" | "no-controller";
116
readonly historyHttpClient?: HttpClient.HttpClient;
117
+ readonly getTurnItem?: (input: {
118
+ readonly itemId: TurnItemId;
119
+ }) => Effect.Effect<{ readonly item: OrchestrationV2TurnItem | null }>;
120
}) {
121
const inputs = yield* Queue.unbounded<TestThreadInput>();
122
const observed = yield* Queue.unbounded<EnvironmentThreadState>();
@@ -153,6 +158,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
158
Effect.as(streamFrom(inputs)),
159
),
160
),
161
+ [ORCHESTRATION_V2_WS_METHODS.getTurnItem]: (input: { readonly itemId: TurnItemId }) =>
162
+ options?.getTurnItem?.(input) ?? Effect.die("Unexpected getTurnItem request"),
163
} as unknown as WsRpcProtocolClient;
164
const supervisorSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
165
Option.some(testSession(client, options)),
@@ -256,6 +263,7 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
263
threadState,
264
loadEarlier: (throughEntryId?: string) =>
265
historyController.loadEarlier(TARGET.environmentId, THREAD_ID, throughEntryId),
266
+ reconcilePendingWork: historyController.reconcilePendingWork(TARGET.environmentId, THREAD_ID),
267
inputs,
268
observed,
269
latest,
@@ -578,6 +586,450 @@ describe("EnvironmentThreads", () => {
586
}),
587
);
588
589
+ describe("Stop on cached work the server already finished (#18188)", () => {
590
+ const at = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
591
+ const parentThreadId = ThreadId.make("thread:parent");
592
+ const command = (
593
+ id: string,
594
+ status: "running" | "completed",
595
+ exitCode?: number,
596
+ ordinal = id === "older-command" ? 1 : 2,
597
+ ): OrchestrationV2TurnItem => ({
598
+ id: TurnItemId.make(id),
599
+ threadId: THREAD_ID,
600
+ runId: null,
601
+ nodeId: null,
602
+ providerThreadId: null,
603
+ providerTurnId: null,
604
+ nativeItemRef: null,
605
+ parentItemId: null,
606
+ ordinal,
607
+ status,
608
+ title: null,
609
+ startedAt: at,
610
+ completedAt: status === "completed" ? at : null,
611
+ updatedAt: at,
612
+ type: "command_execution",
613
+ input: `run ${id}`,
614
+ output: "",
615
+ ...(exitCode === undefined ? {} : { exitCode }),
616
+ });
617
+ const localRow = (item: OrchestrationV2TurnItem, position: number) => ({
618
+ position,
619
+ visibility: "local" as const,
620
+ sourceThreadId: THREAD_ID,
621
+ sourceItemId: item.id,
622
+ item,
623
+ });
624
+ // A fork's parent row keeps its own source; the child's Stop must not touch it.
625
+ const inheritedRow = {
626
+ position: 0,
627
+ visibility: "inherited" as const,
628
+ sourceThreadId: parentThreadId,
629
+ sourceItemId: TurnItemId.make("cached-command"),
630
+ item: { ...command("cached-command", "running"), threadId: parentThreadId },
631
+ };
632
+ const cachedProjection = (items: ReadonlyArray<OrchestrationV2TurnItem>) => ({
633
+ ...BASE_PROJECTION,
634
+ turnItems: items,
635
+ visibleTurnItems: [inheritedRow, ...items.map((item, index) => localRow(item, index + 1))],
636
+ });
637
+ // Loaded older history the bounded window would drop, next to the stale item.
638
+ const cached: OrchestrationV2ThreadProjection = cachedProjection([
639
+ command("older-command", "completed", 0),
640
+ command("cached-command", "running"),
641
+ ]);
642
+ const cachedHistory = { historyCursor: "older", hasMoreHistory: true };
643
+ const projectionStatuses = (projection: OrchestrationV2ThreadProjection) => ({
644
+ items: projection.turnItems.map((item) => [String(item.id), item.status]),
645
+ rows: projection.visibleTurnItems.map((row) => [
646
+ row.visibility,
647
+ String(row.sourceItemId),
648
+ row.item.status,
649
+ ]),
650
+ pending: pendingBackgroundTurnItems(projection).map((item) => String(item.id)),
651
+ });
652
+ const statuses = (state: EnvironmentThreadState) =>
653
+ projectionStatuses(Option.getOrThrow(state.data));
654
+ const settledStatuses = {
655
+ items: [
656
+ ["older-command", "completed"],
657
+ ["cached-command", "completed"],
658
+ ],
659
+ rows: [
660
+ ["inherited", "cached-command", "running"],
661
+ ["local", "older-command", "completed"],
662
+ ["local", "cached-command", "completed"],
663
+ ],
664
+ pending: [],
665
+ };
666
+ const serverItems = (items: Record<string, OrchestrationV2TurnItem | null>) => ({
667
+ getTurnItem: ({ itemId }: { readonly itemId: TurnItemId }) =>
668
+ Effect.succeed({ item: items[itemId] ?? null }),
669
+ });
670
+
671
+ it.effect("settles an item the server finished without any new event, keeping history", () =>
672
+ Effect.gen(function* () {
673
+ const written = yield* Deferred.make<OrchestrationV2ThreadDetailSnapshot>();
674
+ const harness = yield* makeHarness({
675
+ cached,
676
+ cachedHistory,
677
+ saveThread: (_environmentId, thread) => Deferred.succeed(written, thread),
678
+ ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
679
+ });
680
+ const before = yield* SubscriptionRef.get(harness.threadState);
681
+
682
+ yield* harness.reconcilePendingWork;
683
+
684
+ const after = yield* SubscriptionRef.get(harness.threadState);
685
+ expect(statuses(after)).toEqual(settledStatuses);
686
+ const settled = Option.getOrThrow(after.data);
687
+ expect(settled.visibleTurnItems[0]).toBe(inheritedRow);
688
+ expect(settled.visibleTurnItems[2]!.item).toBe(settled.turnItems[1]);
689
+ expect(after.history).toEqual(before.history);
690
+ yield* TestClock.adjust("500 millis");
691
+ const saved = yield* Deferred.await(written);
692
+ expect(projectionStatuses(saved.projection)).toEqual(settledStatuses);
693
+ expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
694
+ }),
695
+ );
696
+
697
+ it.effect("saves the repair after older history was loaded, without the expansion", () =>
698
+ Effect.gen(function* () {
699
+ const evenOlder = command("even-older-command", "completed", 0, 0);
700
+ const scope = yield* Scope.make();
701
+ const harness = yield* makeHarness({
702
+ cached,
703
+ cachedHistory,
704
+ historyHttpClient: HttpClient.make((request) =>
705
+ Effect.succeed(
706
+ HttpClientResponse.fromWeb(
707
+ request,
708
+ Response.json({
709
+ snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
710
+ items: [localRow(evenOlder, 0)],
711
+ nextCursor: null,
712
+ hasMoreHistory: false,
713
+ }),
714
+ ),
715
+ ),
716
+ ),
717
+ ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
718
+ }).pipe(Scope.provide(scope));
719
+ expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
720
+ expect((yield* SubscriptionRef.get(harness.threadState)).history.expanded).toBe(true);
721
+
722
+ yield* harness.reconcilePendingWork;
723
+ const after = yield* SubscriptionRef.get(harness.threadState);
724
+ expect(after.history).toMatchObject({ expanded: true, historyCursor: null });
725
+ expect(statuses(after).pending).toEqual([]);
726
+ yield* TestClock.adjust("500 millis");
727
+ yield* Scope.close(scope, Exit.void);
728
+
729
+ const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
730
+ expect(saved).toMatchObject({
731
+ snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
732
+ historyCursor: "older",
733
+ hasMoreHistory: true,
734
+ });
735
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
736
+
737
+ const reloaded = yield* makeHarness({
738
+ cached: saved!.projection,
739
+ cachedHistory: { historyCursor: saved!.historyCursor!, hasMoreHistory: true },
740
+ });
741
+ expect(statuses(yield* SubscriptionRef.get(reloaded.threadState))).toEqual(settledStatuses);
742
+ }),
743
+ );
744
+
745
+ const expandedHarness = Effect.fn("expandedHarness")(function* (
746
+ scope: Scope.Closeable,
747
+ options?: Pick<NonNullable<Parameters<typeof makeHarness>[0]>, "getTurnItem">,
748
+ ) {
749
+ const harness = yield* makeHarness({
750
+ cached,
751
+ cachedHistory,
752
+ historyHttpClient: HttpClient.make((request) =>
753
+ Effect.succeed(
754
+ HttpClientResponse.fromWeb(
755
+ request,
756
+ Response.json({
757
+ snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
758
+ items: [localRow(command("even-older-command", "completed", 0, 0), 0)],
759
+ nextCursor: null,
760
+ hasMoreHistory: false,
761
+ }),
762
+ ),
763
+ ),
764
+ ),
765
+ ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
766
+ ...options,
767
+ }).pipe(Scope.provide(scope));
768
+ return harness;
769
+ });
770
+ const expand = (harness: Effect.Success<ReturnType<typeof expandedHarness>>) =>
771
+ Effect.gen(function* () {
772
+ expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
773
+ expect((yield* SubscriptionRef.get(harness.threadState)).history.expanded).toBe(true);
774
+ });
775
+ const lastSaved = (harness: Effect.Success<ReturnType<typeof expandedHarness>>) =>
776
+ Ref.get(harness.savedThreads).pipe(Effect.map((saved) => saved.at(-1)));
777
+
778
+ it.effect("saves an expanded thread's repair when its scope closes before the save runs", () =>
779
+ Effect.gen(function* () {
780
+ const scope = yield* Scope.make();
781
+ const harness = yield* expandedHarness(scope);
782
+ yield* expand(harness);
783
+
784
+ yield* harness.reconcilePendingWork;
785
+ yield* Scope.close(scope, Exit.void);
786
+
787
+ const saved = yield* lastSaved(harness);
788
+ expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
789
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
790
+ }),
791
+ );
792
+
793
+ it.effect(
794
+ "saves an expanded thread's repair when its scope closes during the save cooldown",
795
+ () =>
796
+ Effect.gen(function* () {
797
+ const scope = yield* Scope.make();
798
+ const harness = yield* expandedHarness(scope);
799
+ yield* Queue.offer(harness.inputs, titleUpdated("Renamed", CACHED_SNAPSHOT_SEQUENCE + 1));
800
+ yield* awaitThreadState(
801
+ harness.observed,
802
+ (value) => Option.getOrThrow(value.data).thread.title === "Renamed",
803
+ );
804
+ yield* TestClock.adjust("500 millis");
805
+ expect((yield* lastSaved(harness))?.projection.thread.title).toBe("Renamed");
806
+ yield* expand(harness);
807
+
808
+ yield* harness.reconcilePendingWork;
809
+ yield* Scope.close(scope, Exit.void);
810
+
811
+ const saved = yield* lastSaved(harness);
812
+ expect(saved).toMatchObject({
813
+ snapshotSequence: CACHED_SNAPSHOT_SEQUENCE + 1,
814
+ historyCursor: "older",
815
+ hasMoreHistory: true,
816
+ });
817
+ expect(saved!.projection.thread.title).toBe("Renamed");
818
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
819
+ }),
820
+ );
821
+
822
+ it.effect("saves a completion event that lands while the server is asked", () =>
823
+ Effect.gen(function* () {
824
+ const asked = yield* Deferred.make<void>();
825
+ const answer = yield* Deferred.make<OrchestrationV2TurnItem>();
826
+ const scope = yield* Scope.make();
827
+ const harness = yield* expandedHarness(scope, {
828
+ getTurnItem: () =>
829
+ Deferred.succeed(asked, undefined).pipe(
830
+ Effect.andThen(Deferred.await(answer)),
831
+ Effect.map((item) => ({ item })),
832
+ ),
833
+ });
834
+ yield* expand(harness);
835
+ const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
836
+ yield* Deferred.await(asked);
837
+ const fromEvent = command("cached-command", "completed", 1);
838
+ yield* Queue.offer(harness.inputs, {
839
+ kind: "event",
840
+ sequence: CACHED_SNAPSHOT_SEQUENCE + 1,
841
+ event: {
842
+ id: EventId.make("event-cached-command-completed"),
843
+ type: "turn-item.updated",
844
+ threadId: THREAD_ID,
845
+ occurredAt: at,
846
+ payload: fromEvent,
847
+ },
848
+ });
849
+ yield* awaitThreadState(harness.observed, (value) =>
850
+ Option.getOrThrow(value.data).turnItems.some(
851
+ (item) => item.id === fromEvent.id && item.status === "completed",
852
+ ),
853
+ );
854
+
855
+ yield* Deferred.succeed(answer, command("cached-command", "completed", 0));
856
+ yield* Fiber.join(reconciling);
857
+ yield* Scope.close(scope, Exit.void);
858
+
859
+ const live = Option.getOrThrow((yield* SubscriptionRef.get(harness.threadState)).data);
860
+ expect(live.turnItems.find((item) => item.id === fromEvent.id)).toEqual(fromEvent);
861
+ const saved = yield* lastSaved(harness);
862
+ expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
863
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
864
+ expect(saved!.projection.turnItems.find((item) => item.id === fromEvent.id)).toEqual(
865
+ fromEvent,
866
+ );
867
+ }),
868
+ );
869
+
870
+ // The event is newer than any reply, so the saved copy follows it however the lookup ends.
871
+ it.effect.each(["running", "missing", "timed out"] as const)(
872
+ "saves a completion event that overtakes a %s lookup reply",
873
+ (reply) =>
874
+ Effect.gen(function* () {
875
+ const asked = yield* Deferred.make<void>();
876
+ const answer = yield* Deferred.make<void>();
877
+ const scope = yield* Scope.make();
878
+ const harness = yield* expandedHarness(scope, {
879
+ getTurnItem: () =>
880
+ Deferred.succeed(asked, undefined).pipe(
881
+ Effect.andThen(reply === "timed out" ? Effect.never : Deferred.await(answer)),
882
+ Effect.as({
883
+ item: reply === "running" ? command("cached-command", "running") : null,
884
+ }),
885
+ ),
886
+ });
887
+ yield* expand(harness);
888
+ const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
889
+ yield* Deferred.await(asked);
890
+ const fromEvent = command("cached-command", "completed", 1);
891
+ yield* Queue.offer(harness.inputs, {
892
+ kind: "event",
893
+ sequence: CACHED_SNAPSHOT_SEQUENCE + 1,
894
+ event: {
895
+ id: EventId.make("event-cached-command-completed"),
896
+ type: "turn-item.updated",
897
+ threadId: THREAD_ID,
898
+ occurredAt: at,
899
+ payload: fromEvent,
900
+ },
901
+ });
902
+ yield* awaitThreadState(harness.observed, (value) =>
903
+ Option.getOrThrow(value.data).turnItems.some(
904
+ (item) => item.id === fromEvent.id && item.status === "completed",
905
+ ),
906
+ );
907
+
908
+ if (reply === "timed out") yield* TestClock.adjust("10 seconds");
909
+ else yield* Deferred.succeed(answer, undefined);
910
+ yield* Fiber.join(reconciling);
911
+ yield* Scope.close(scope, Exit.void);
912
+
913
+ const saved = yield* lastSaved(harness);
914
+ expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
915
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
916
+ expect(saved!.projection.turnItems.find((item) => item.id === fromEvent.id)).toEqual(
917
+ fromEvent,
918
+ );
919
+ }),
920
+ );
921
+
922
+ it.effect("saves the repair for an expanded thread resumed in the same session", () =>
923
+ Effect.gen(function* () {
924
+ const resumeCache: NonNullable<Parameters<typeof makeEnvironmentThreadState>[1]> = {
925
+ snapshot: {
926
+ state: {
927
+ data: Option.some(cached),
928
+ status: "cached",
929
+ error: Option.none(),
930
+ history: { ...EMPTY_THREAD_HISTORY_META, historyCursor: null, expanded: true },
931
+ },
932
+ sequence: CACHED_SNAPSHOT_SEQUENCE,
933
+ persisted: true,
934
+ },
935
+ owner: undefined,
936
+ };
937
+ const scope = yield* Scope.make();
938
+ const harness = yield* makeHarness({
939
+ cached,
940
+ cachedHistory,
941
+ resumeCache,
942
+ ...serverItems({ "cached-command": command("cached-command", "completed", 0) }),
943
+ }).pipe(Scope.provide(scope));
944
+
945
+ yield* harness.reconcilePendingWork;
946
+ yield* TestClock.adjust("500 millis");
947
+ yield* Scope.close(scope, Exit.void);
948
+
949
+ const saved = (yield* Ref.get(harness.savedThreads)).at(-1);
950
+ expect(saved).toMatchObject({ historyCursor: "older", hasMoreHistory: true });
951
+ expect(projectionStatuses(saved!.projection)).toEqual(settledStatuses);
952
+ }),
953
+ );
954
+
955
+ it.effect("gives up on a lookup the server never answers, settling the rest", () =>
956
+ Effect.gen(function* () {
957
+ const askedCached = yield* Deferred.make<void>();
958
+ const askedSilent = yield* Deferred.make<void>();
959
+ const harness = yield* makeHarness({
960
+ cached: cachedProjection([...cached.turnItems, command("silent-command", "running")]),
961
+ cachedHistory,
962
+ getTurnItem: ({ itemId }) =>
963
+ itemId === "silent-command"
964
+ ? Deferred.succeed(askedSilent, undefined).pipe(Effect.andThen(Effect.never))
965
+ : Deferred.succeed(askedCached, undefined).pipe(
966
+ Effect.as({ item: command("cached-command", "completed", 0) }),
967
+ ),
968
+ });
969
+ const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
970
+ yield* Deferred.await(askedCached);
971
+ yield* Deferred.await(askedSilent);
972
+
973
+ yield* TestClock.adjust("10 seconds");
974
+
975
+ expect(reconciling.pollUnsafe()).toEqual(Exit.void);
976
+ expect(statuses(yield* SubscriptionRef.get(harness.threadState)).items).toEqual([
977
+ ["older-command", "completed"],
978
+ ["cached-command", "completed"],
979
+ ["silent-command", "running"],
980
+ ]);
981
+ }),
982
+ );
983
+
984
+ it.effect("keeps items the server still runs or does not return", () =>
985
+ Effect.gen(function* () {
986
+ const harness = yield* makeHarness({
987
+ cached: cachedProjection([...cached.turnItems, command("missing-command", "running")]),
988
+ cachedHistory,
989
+ ...serverItems({ "cached-command": command("cached-command", "running") }),
990
+ });
991
+
992
+ yield* harness.reconcilePendingWork;
993
+
994
+ expect(statuses(yield* SubscriptionRef.get(harness.threadState)).items).toEqual([
995
+ ["older-command", "completed"],
996
+ ["cached-command", "running"],
997
+ ["missing-command", "running"],
998
+ ]);
999
+ }),
1000
+ );
1001
+
1002
+ it.effect("keeps events that land while the server is asked", () =>
1003
+ Effect.gen(function* () {
1004
+ const asked = yield* Deferred.make<void>();
1005
+ const answer = yield* Deferred.make<OrchestrationV2TurnItem>();
1006
+ const harness = yield* makeHarness({
1007
+ cached,
1008
+ cachedHistory,
1009
+ getTurnItem: () =>
1010
+ Deferred.succeed(asked, undefined).pipe(
1011
+ Effect.andThen(Deferred.await(answer)),
1012
+ Effect.map((item) => ({ item })),
1013
+ ),
1014
+ });
1015
+ const reconciling = yield* harness.reconcilePendingWork.pipe(Effect.forkChild);
1016
+ yield* Deferred.await(asked);
1017
+ yield* Queue.offer(harness.inputs, titleUpdated("Renamed", CACHED_SNAPSHOT_SEQUENCE + 1));
1018
+ yield* awaitThreadState(
1019
+ harness.observed,
1020
+ (value) => Option.getOrThrow(value.data).thread.title === "Renamed",
1021
+ );
1022
+
1023
+ yield* Deferred.succeed(answer, command("cached-command", "completed", 0));
1024
+ yield* Fiber.join(reconciling);
1025
+
1026
+ const after = yield* SubscriptionRef.get(harness.threadState);
1027
+ expect(Option.getOrThrow(after.data).thread.title).toBe("Renamed");
1028
+ expect(statuses(after)).toEqual(settledStatuses);
1029
+ }),
1030
+ );
1031
+ });
1032
+
1033
it.effect("keeps progressive history metadata from a bounded socket fallback", () =>
1034
Effect.gen(function* () {
1035
const olderItem = {
packages/client-runtime/src/state/threads.ts
+111
-6
@@ -1,12 +1,15 @@
1
import {
2
+ isOrchestrationV2WorkActive,
3
ORCHESTRATION_V2_WS_METHODS,
4
type EnvironmentId as EnvironmentIdType,
5
type OrchestrationV2ThreadDetailSnapshot,
6
type OrchestrationV2ThreadProjection,
7
type OrchestrationV2ThreadStreamItem,
8
+ type OrchestrationV2TurnItem,
9
type ThreadId as ThreadIdType,
10
} from "@t3tools/contracts";
11
import { boundedSnapshotProjection } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
12
+import { pendingBackgroundTurnItems } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
13
import * as Cause from "effect/Cause";
14
import * as Effect from "effect/Effect";
15
import * as Fiber from "effect/Fiber";
@@ -29,7 +32,7 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
32
import * as Persistence from "../platform/persistence.ts";
33
import { runCachePersistence } from "./cachePersistence.ts";
34
import * as ManagedRelay from "../relay/managedRelay.ts";
32
-import { subscribeDynamic } from "../rpc/client.ts";
35
+import { request, subscribeDynamic } from "../rpc/client.ts";
36
import { parseThreadKey, threadKey } from "./entities.ts";
37
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
38
import { THREAD_SNAPSHOT_IDLE_TTL_MS } from "./threadRetention.ts";
@@ -120,6 +123,29 @@ function shouldPersistThread(
123
);
124
}
125
126
+// Only still-active local copies take the server's item: events applied meanwhile win,
127
+// and inherited rows belong to another thread.
128
+function settleTurnItems(
129
+ threadId: ThreadIdType,
130
+ projection: OrchestrationV2ThreadProjection,
131
+ settled: ReadonlyMap<string, OrchestrationV2TurnItem>,
132
+): OrchestrationV2ThreadProjection {
133
+ let changed = false;
134
+ const settle = (item: OrchestrationV2TurnItem, id: string) => {
135
+ const server = settled.get(id);
136
+ if (server === undefined || !isOrchestrationV2WorkActive(item.status)) return item;
137
+ changed = true;
138
+ return server;
139
+ };
140
+ const turnItems = projection.turnItems.map((item) => settle(item, item.id));
141
+ const visibleTurnItems = projection.visibleTurnItems.map((row) => {
142
+ if (row.sourceThreadId !== threadId) return row;
143
+ const item = settle(row.item, row.sourceItemId);
144
+ return item === row.item ? row : { ...row, item };
145
+ });
146
+ return changed ? { ...projection, turnItems, visibleTurnItems } : projection;
147
+}
148
+
149
interface ThreadResumeSnapshot {
150
readonly state: EnvironmentThreadState;
151
readonly sequence: number;
@@ -242,6 +268,13 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
268
if (resumeCache?.owner === owner) resumeCache.snapshot = committed;
269
});
270
const persistence = yield* Queue.sliding<OrchestrationV2ThreadDetailSnapshot>(1);
271
+ let queuedSnapshot = Option.getOrUndefined(cached);
272
+ let savedSnapshot = queuedSnapshot;
273
+ const queuePersist = (snapshot: OrchestrationV2ThreadDetailSnapshot) =>
274
+ Effect.suspend(() => {
275
+ queuedSnapshot = snapshot;
276
+ return Queue.offer(persistence, snapshot);
277
+ });
278
279
const persist = Effect.fn("EnvironmentThreadState.persist")(function* (
280
snapshot: OrchestrationV2ThreadDetailSnapshot,
@@ -261,6 +294,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
294
yield* cache.saveThread(environmentId, snapshot).pipe(
295
Effect.tap(() =>
296
Effect.sync(() => {
297
+ savedSnapshot = snapshot;
298
if (
299
!matchesThreadSnapshot(
300
committed,
@@ -301,7 +335,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
335
committed.acceptsBoundedSnapshots === true,
336
),
337
)
304
- : Effect.void,
338
+ : // The consumer may still be waiting out its delay; an expanded thread queues
339
+ // only cache repairs, which must survive leaving the thread.
340
+ queuedSnapshot !== undefined && queuedSnapshot !== savedSnapshot
341
+ ? persist(queuedSnapshot)
342
+ : Effect.void,
343
});
344
}),
345
);
@@ -380,8 +418,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
418
// streaming path. Progressive meta rides along when the window is incomplete.
419
if (shouldPersistThread(thread, next.history)) {
420
const snapshotSequence = yield* SubscriptionRef.get(lastSequence);
383
- yield* Queue.offer(
384
- persistence,
421
+ yield* queuePersist(
422
snapshotToPersist(
423
snapshotSequence,
424
thread,
@@ -535,8 +572,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
572
}
573
if (result._tag === "applied" && shouldPersistThread(result.projection, result.history)) {
574
const snapshotSequence = yield* SubscriptionRef.get(lastSequence);
538
- yield* Queue.offer(
539
- persistence,
575
+ yield* queuePersist(
576
snapshotToPersist(
577
snapshotSequence,
578
result.projection,
@@ -721,11 +757,80 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
757
);
758
});
759
760
+ // A cached item can stay running after the server finished it, and no event will ever
761
+ // name it again: it can sit outside every bounded window, and a repeated Stop records
762
+ // nothing new (#18188). Stop asks the server about each pending item instead of
763
+ // inferring status from what a snapshot omits, and settles only those items.
764
+ const reconcilePendingWork = Effect.fn("EnvironmentThreadState.reconcilePendingWork")(
765
+ function* () {
766
+ const current = yield* SubscriptionRef.get(state);
767
+ if (current.status === "deleted" || Option.isNone(current.data)) return;
768
+ const pending = pendingBackgroundTurnItems(current.data.value);
769
+ if (pending.length === 0) return;
770
+ const queried = new Set<string>(pending.map((item) => item.id));
771
+ const fetched = yield* Effect.forEach(
772
+ pending,
773
+ (item) =>
774
+ request(ORCHESTRATION_V2_WS_METHODS.getTurnItem, { threadId, itemId: item.id }).pipe(
775
+ Effect.timeout("10 seconds"),
776
+ Effect.map((result) => result.item),
777
+ Effect.orElseSucceed(() => null),
778
+ ),
779
+ { concurrency: 4 },
780
+ );
781
+ const settled = new Map(
782
+ fetched.flatMap((item) =>
783
+ item === null || isOrchestrationV2WorkActive(item.status) ? [] : [[item.id, item]],
784
+ ),
785
+ );
786
+ yield* applyLock.withPermits(1)(
787
+ Effect.gen(function* () {
788
+ const latest = yield* SubscriptionRef.get(state);
789
+ if (latest.status === "deleted" || Option.isNone(latest.data)) return;
790
+ const thread = settleTurnItems(threadId, latest.data.value, settled);
791
+ if (thread !== latest.data.value) {
792
+ yield* setThread(thread);
793
+ yield* remember;
794
+ }
795
+ // An expanded timeline is never saved, so reload would bring the stale row back.
796
+ // Repair the snapshot last handed to the cache instead, keeping its window and cursor.
797
+ // A resumed state skipped the cache load, so read the saved snapshot then.
798
+ if (shouldPersistThread(thread, latest.history)) return;
799
+ // A terminal event that landed during the lookup is newer than any reply, and the
800
+ // expanded live state never saves it, whatever the lookup returned.
801
+ const latestItems = new Map(settled);
802
+ for (const item of thread.turnItems) {
803
+ if (queried.has(item.id) && !isOrchestrationV2WorkActive(item.status)) {
804
+ latestItems.set(item.id, item);
805
+ }
806
+ }
807
+ if (latestItems.size === 0) return;
808
+ const saved =
809
+ queuedSnapshot ??
810
+ Option.getOrUndefined(
811
+ yield* cache
812
+ .loadThread(environmentId, threadId)
813
+ .pipe(Effect.orElseSucceed(() => Option.none())),
814
+ );
815
+ if (saved === undefined) return;
816
+ const projection = settleTurnItems(threadId, saved.projection, latestItems);
817
+ if (projection !== saved.projection) yield* queuePersist({ ...saved, projection });
818
+ }),
819
+ );
820
+ },
821
+ );
822
+
823
if (Option.isSome(historyController)) {
824
const scope = yield* Scope.Scope;
825
const registration = yield* historyController.value.register(environmentId, threadId, {
826
loadEarlier: (throughEntryId) =>
827
loadEarlier(throughEntryId).pipe(Effect.forkIn(scope), Effect.flatMap(Fiber.join)),
828
+ reconcilePendingWork: () =>
829
+ reconcilePendingWork().pipe(
830
+ Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
831
+ Effect.forkIn(scope),
832
+ Effect.flatMap(Fiber.join),
833
+ ),
834
});
835
yield* Effect.addFinalizer(() => historyController.value.unregister(registration));
836
}
scripts/fixtures/issue-18188/seed-stale-thread-command.js
new
+68
@@ -0,0 +1,68 @@
1
+/**
2
+ * Run in the isolated dev client's browser with preview_evaluate.
3
+ * Seed only IndexedDB; authoritative server state remains unchanged.
4
+ * Await completion, then reload before exercising Stop.
5
+ */
6
+globalThis.seedStaleThreadCommand = async function ({ environmentId, threadId, itemId }) {
7
+ const response = await fetch(`/api/orchestration/threads/${threadId}`, {
8
+ headers: { "x-t3-orchestration-protocol": "2" },
9
+ });
10
+ if (!response.ok) throw new Error(`Thread fetch failed: ${response.status}`);
11
+ const full = await response.json();
12
+ const authoritative = full.projection.turnItems.find((item) => item.id === itemId);
13
+ const visible = full.projection.visibleTurnItems.find((row) => row.item.id === itemId);
14
+ if (
15
+ authoritative?.type !== "command_execution" ||
16
+ authoritative.status !== "completed" ||
17
+ !visible
18
+ ) {
19
+ throw new Error("Choose an authoritative completed command with a visible row");
20
+ }
21
+ return new Promise((resolve, reject) => {
22
+ const request = indexedDB.open("t3code:connection-runtime", 4);
23
+ request.addEventListener("error", () => reject(request.error));
24
+ request.addEventListener("success", () => {
25
+ const db = request.result;
26
+ const transaction = db.transaction("thread", "readwrite");
27
+ const store = transaction.objectStore("thread");
28
+ const key = `${environmentId}:${threadId}`;
29
+ const read = store.get(key);
30
+ let assertion;
31
+ transaction.addEventListener("error", () => reject(transaction.error));
32
+ transaction.addEventListener("abort", () =>
33
+ reject(transaction.error ?? new Error("Seed aborted")),
34
+ );
35
+ transaction.addEventListener("complete", () => {
36
+ db.close();
37
+ resolve(assertion);
38
+ });
39
+ read.addEventListener("success", () => {
40
+ if (!read.result) {
41
+ transaction.abort();
42
+ return;
43
+ }
44
+ const saved = JSON.parse(read.result);
45
+ const snapshot = saved.snapshot;
46
+ const projection = snapshot.projection;
47
+ const stale = { ...authoritative, status: "running", completedAt: null };
48
+ assertion = {
49
+ authoritativeStatus: authoritative.status,
50
+ omittedBeforeSeed: !projection.turnItems.some((item) => item.id === itemId),
51
+ originalCount: projection.turnItems.length,
52
+ snapshotSequence: snapshot.snapshotSequence,
53
+ historyCursor: snapshot.historyCursor,
54
+ hasMoreHistory: snapshot.hasMoreHistory,
55
+ };
56
+ projection.turnItems = [
57
+ stale,
58
+ ...projection.turnItems.filter((item) => item.id !== itemId),
59
+ ];
60
+ projection.visibleTurnItems = [
61
+ { ...visible, item: stale },
62
+ ...projection.visibleTurnItems.filter((row) => row.item.id !== itemId),
63
+ ];
64
+ store.put(JSON.stringify(saved), key);
65
+ });
66
+ });
67
+ });
68
+};