Skip to content

Commit fff4d8c

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 5b35402 commit fff4d8c

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,
@@ -43,7 +44,7 @@ import {
4344
decodeOrDiscardOrchestrationCache,
4445
} from "../platform/orchestrationCache.ts";
4546
import * as RpcSession from "../rpc/session.ts";
46-
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
47+
import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
4748
import { EMPTY_THREAD_HISTORY_META } from "./threadHistoryMerge.ts";
4849
import * as ThreadHistoryController from "./threadHistoryController.ts";
4950
import {
@@ -1724,6 +1725,52 @@ describe("EnvironmentThreads", () => {
17241725
}),
17251726
);
17261727

1728+
// regression: #24192 reopening a running thread on mobile waited on the server with a blank view
1729+
it.effect("caches a running thread once when its view closes, not on each update", () =>
1730+
Effect.gen(function* () {
1731+
const scope = yield* Scope.make();
1732+
const harness = yield* makeHarness().pipe(Effect.provideService(Scope.Scope, scope));
1733+
const running: OrchestrationV2ThreadProjection = {
1734+
...BASE_PROJECTION,
1735+
runs: [
1736+
{
1737+
id: RunId.make("run-running"),
1738+
threadId: THREAD_ID,
1739+
ordinal: 1,
1740+
providerInstanceId: BASE_PROJECTION.thread.providerInstanceId,
1741+
modelSelection: BASE_PROJECTION.thread.modelSelection,
1742+
providerThreadId: null,
1743+
userMessageId: MessageId.make("message-running"),
1744+
rootNodeId: null,
1745+
activeAttemptId: null,
1746+
status: "running",
1747+
requestedAt: v2Now,
1748+
startedAt: v2Now,
1749+
completedAt: null,
1750+
checkpointId: null,
1751+
contextHandoffId: null,
1752+
},
1753+
],
1754+
};
1755+
yield* Queue.offer(harness.inputs, snapshot(running, 4));
1756+
yield* Queue.offer(harness.inputs, titleUpdated("Streaming update", 5));
1757+
yield* awaitThreadState(
1758+
harness.observed,
1759+
(value) =>
1760+
Option.isSome(value.data) && value.data.value.thread.title === "Streaming update",
1761+
);
1762+
yield* TestClock.adjust("10 seconds");
1763+
expect(yield* Ref.get(harness.savedThreads)).toHaveLength(0);
1764+
1765+
yield* Scope.close(scope, Exit.void);
1766+
const saved = yield* Ref.get(harness.savedThreads);
1767+
expect(saved).toHaveLength(1);
1768+
expect(saved[0]?.snapshotSequence).toBe(5);
1769+
expect(saved[0]?.projection.thread.title).toBe("Streaming update");
1770+
expect(saved[0]?.projection.runs[0]?.status).toBe("running");
1771+
}),
1772+
);
1773+
17271774
it.effect("preserves data after a domain failure and resumes on a replacement session", () =>
17281775
Effect.gen(function* () {
17291776
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
@@ -320,13 +320,15 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
320320
);
321321
});
322322

323+
// Unlike the streaming path, also write an active thread here, once, so
324+
// reopening it later shows this copy instead of waiting on the server.
323325
yield* Effect.addFinalizer(() =>
324326
Effect.suspend(() => {
325327
const { state: current, sequence: snapshotSequence } = committed;
326328
return Option.match(current.data, {
327329
onNone: () => Effect.void,
328330
onSome: (projection) =>
329-
shouldPersistThread(projection, current.history)
331+
!current.history.expanded
330332
? persist(
331333
snapshotToPersist(
332334
snapshotSequence,

0 commit comments

Comments
 (0)