Skip to content

Commit 3369733

Browse files
PR Batch Testerclaude
andcommitted
fix(client-runtime): reopening a running thread shows its cached messages right away
The thread cache skipped any thread with an active run, so a thread you checked while its agent worked was never saved on the device. Reopening it after a relaunch showed a blank view until the server answered. Save it once when the thread view closes; streaming updates still skip the write. Refs #24192 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 171f8d8 commit 3369733

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
EventId,
44
MessageId,
55
ORCHESTRATION_V2_WS_METHODS,
6+
RunId,
67
ThreadId,
78
TurnItemId,
89
type OrchestrationV2ThreadDetailSnapshot,
@@ -36,7 +37,7 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
3637
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
3738
import * as Persistence from "../platform/persistence.ts";
3839
import * as RpcSession from "../rpc/session.ts";
39-
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
40+
import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
4041
import * as ThreadHistoryController from "./threadHistoryController.ts";
4142
import {
4243
EMPTY_ENVIRONMENT_THREAD_STATE,
@@ -1210,6 +1211,52 @@ describe("EnvironmentThreads", () => {
12101211
}),
12111212
);
12121213

1214+
// regression: #24192 reopening a running thread on mobile waited on the server with a blank view
1215+
it.effect("caches a running thread once when its view closes, not on each update", () =>
1216+
Effect.gen(function* () {
1217+
const scope = yield* Scope.make();
1218+
const harness = yield* makeHarness().pipe(Effect.provideService(Scope.Scope, scope));
1219+
const running: OrchestrationV2ThreadProjection = {
1220+
...BASE_PROJECTION,
1221+
runs: [
1222+
{
1223+
id: RunId.make("run-running"),
1224+
threadId: THREAD_ID,
1225+
ordinal: 1,
1226+
providerInstanceId: BASE_PROJECTION.thread.providerInstanceId,
1227+
modelSelection: BASE_PROJECTION.thread.modelSelection,
1228+
providerThreadId: null,
1229+
userMessageId: MessageId.make("message-running"),
1230+
rootNodeId: null,
1231+
activeAttemptId: null,
1232+
status: "running",
1233+
requestedAt: v2Now,
1234+
startedAt: v2Now,
1235+
completedAt: null,
1236+
checkpointId: null,
1237+
contextHandoffId: null,
1238+
},
1239+
],
1240+
};
1241+
yield* Queue.offer(harness.inputs, snapshot(running, 4));
1242+
yield* Queue.offer(harness.inputs, titleUpdated("Streaming update", 5));
1243+
yield* awaitThreadState(
1244+
harness.observed,
1245+
(value) =>
1246+
Option.isSome(value.data) && value.data.value.thread.title === "Streaming update",
1247+
);
1248+
yield* TestClock.adjust("10 seconds");
1249+
expect(yield* Ref.get(harness.savedThreads)).toHaveLength(0);
1250+
1251+
yield* Scope.close(scope, Exit.void);
1252+
const saved = yield* Ref.get(harness.savedThreads);
1253+
expect(saved).toHaveLength(1);
1254+
expect(saved[0]?.snapshotSequence).toBe(5);
1255+
expect(saved[0]?.projection.thread.title).toBe("Streaming update");
1256+
expect(saved[0]?.projection.runs[0]?.status).toBe("running");
1257+
}),
1258+
);
1259+
12131260
it.effect("preserves data after a domain failure and resumes on a replacement session", () =>
12141261
Effect.gen(function* () {
12151262
const harness = yield* makeHarness({ cached: BASE_PROJECTION });

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,15 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
286286
);
287287
});
288288

289+
// Unlike the streaming path, also write an active thread here, once, so
290+
// reopening it later shows this copy instead of waiting on the server.
289291
yield* Effect.addFinalizer(() =>
290292
Effect.suspend(() => {
291293
const { state: current, sequence: snapshotSequence } = committed;
292294
return Option.match(current.data, {
293295
onNone: () => Effect.void,
294296
onSome: (projection) =>
295-
shouldPersistThread(projection, current.history)
297+
!current.history.expanded
296298
? persist(
297299
snapshotToPersist(
298300
snapshotSequence,

0 commit comments

Comments
 (0)