Skip to content

Commit 759f67a

Browse files
maria-rcksshivamhwp
andcommitted
fix(client-runtime): cache running threads when their view closes
Co-authored-by: shivam <[email protected]>
1 parent 3d78e91 commit 759f67a

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
NodeId,
66
ORCHESTRATION_V2_WS_METHODS,
77
RuntimeRequestId,
8+
RunId,
89
ThreadId,
910
TurnItemId,
1011
type OrchestrationV2ThreadDetailSnapshot,
@@ -2150,6 +2151,51 @@ describe("EnvironmentThreads", () => {
21502151
}),
21512152
);
21522153

2154+
it.effect("caches a running thread once when its view closes, not on each update", () =>
2155+
Effect.gen(function* () {
2156+
const scope = yield* Scope.make();
2157+
const harness = yield* makeHarness().pipe(Effect.provideService(Scope.Scope, scope));
2158+
const running: OrchestrationV2ThreadProjection = {
2159+
...BASE_PROJECTION,
2160+
runs: [
2161+
{
2162+
id: RunId.make("run-running"),
2163+
threadId: THREAD_ID,
2164+
ordinal: 1,
2165+
providerInstanceId: BASE_PROJECTION.thread.providerInstanceId,
2166+
modelSelection: BASE_PROJECTION.thread.modelSelection,
2167+
providerThreadId: null,
2168+
userMessageId: MessageId.make("message-running"),
2169+
rootNodeId: null,
2170+
activeAttemptId: null,
2171+
status: "running",
2172+
requestedAt: v2Now,
2173+
startedAt: v2Now,
2174+
completedAt: null,
2175+
checkpointId: null,
2176+
contextHandoffId: null,
2177+
},
2178+
],
2179+
};
2180+
yield* Queue.offer(harness.inputs, snapshot(running, 4));
2181+
yield* Queue.offer(harness.inputs, titleUpdated("Streaming update", 5));
2182+
yield* awaitThreadState(
2183+
harness.observed,
2184+
(value) =>
2185+
Option.isSome(value.data) && value.data.value.thread.title === "Streaming update",
2186+
);
2187+
yield* TestClock.adjust("10 seconds");
2188+
expect(yield* Ref.get(harness.savedThreads)).toHaveLength(0);
2189+
2190+
yield* Scope.close(scope, Exit.void);
2191+
const saved = yield* Ref.get(harness.savedThreads);
2192+
expect(saved).toHaveLength(1);
2193+
expect(saved[0]?.snapshotSequence).toBe(5);
2194+
expect(saved[0]?.projection.thread.title).toBe("Streaming update");
2195+
expect(saved[0]?.projection.runs[0]?.status).toBe("running");
2196+
}),
2197+
);
2198+
21532199
it.effect("preserves data after a domain failure and resumes on a replacement session", () =>
21542200
Effect.gen(function* () {
21552201
const harness = yield* makeHarness({ cached: BASE_PROJECTION });

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
361361
return Option.match(current.data, {
362362
onNone: () => Effect.void,
363363
onSome: (projection) =>
364-
shouldPersistThread(projection, current.history)
364+
!current.history.expanded
365365
? persist(
366366
snapshotToPersist(
367367
snapshotSequence,

0 commit comments

Comments
 (0)