Skip to content

Commit 4ec957c

Browse files
fix(client): finishing a long background thread no longer freezes the app
When a thread stopped running in the background, its live state was torn down and the full projection was schema-encoded, stringified, and written to the thread cache in one main-thread task. A desktop trace showed two 1.6-1.7s freezes this way while typing, with the heap growing ~300 MB per save. Thread cache saves now go through an incremental encoder, like the shell cache: it remembers each turn item's and timeline row's encoding, encodes new rows in chunks that yield to the host, and stops once the rows exceed 4M JSON characters. A thread over the cap is not cached and its older copy is removed, so reopening it loads the server's bounded window. Web keeps the full format and mobile the compact one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dc72705 commit 4ec957c

6 files changed

Lines changed: 338 additions & 44 deletions

File tree

‎apps/mobile/src/connection/environment-cache-store.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import {
22
ORCHESTRATION_CACHE_SCHEMA_VERSION,
33
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
44
decodeStoredThreadSnapshot,
5-
encodeStoredThreadSnapshot,
65
Persistence,
76
makeStoredShellSnapshotEncoder,
7+
makeStoredThreadSnapshotEncoder,
88
} from "@t3tools/client-runtime/platform";
99
import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
1010
import * as Effect from "effect/Effect";
@@ -96,6 +96,7 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
9696
const database = yield* MobileDatabase.MobileDatabase;
9797
attachProjectFaviconDatabase(database);
9898
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder();
99+
const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({ format: "compact" });
99100
return Persistence.EnvironmentCacheStore.of({
100101
loadShell: Effect.fn("MobileEnvironmentCache.loadShell")((environmentId) =>
101102
loadDecodedCache({
@@ -161,9 +162,18 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
161162
threadId,
162163
snapshot,
163164
}).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
164-
yield* database
165-
.saveCache(environmentId, "thread", threadId, COMPACT_THREAD_CACHE_SCHEMA_VERSION, payload)
166-
.pipe(Effect.mapError(mapDatabaseError("save-thread")));
165+
// An older copy of a thread now too large to keep would paint stale on reopen.
166+
yield* Option.match(payload, {
167+
onNone: () => database.removeCache(environmentId, "thread", threadId),
168+
onSome: (encoded) =>
169+
database.saveCache(
170+
environmentId,
171+
"thread",
172+
threadId,
173+
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
174+
encoded,
175+
),
176+
}).pipe(Effect.mapError(mapDatabaseError("save-thread")));
167177
}),
168178
removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) =>
169179
database

‎apps/web/src/connection/storage.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
type ConnectionCatalogDocument as ConnectionCatalogDocumentType,
44
EMPTY_CONNECTION_CATALOG_DOCUMENT,
55
ORCHESTRATION_CACHE_SCHEMA_VERSION,
6-
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
76
StoredOrchestrationShellSnapshot,
87
StoredOrchestrationThreadSnapshot,
98
decodeOrDiscardOrchestrationCache,
@@ -16,6 +15,7 @@ import {
1615
replaceCatalogValue,
1716
Persistence,
1817
makeStoredShellSnapshotEncoder,
18+
makeStoredThreadSnapshotEncoder,
1919
} from "@t3tools/client-runtime/platform";
2020
import { TokenStore } from "@t3tools/client-runtime/authorization";
2121
import {
@@ -87,7 +87,6 @@ const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCat
8787
const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
8888
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
8989
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
90-
const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
9190
const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
9291
const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);
9392
const decodeStoredVcsRefs = Schema.decodeUnknownEffect(StoredVcsRefsJson);
@@ -723,6 +722,10 @@ export const layer = Layer.effectContext(
723722
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
724723
yieldToHost: yieldToBrowserTask,
725724
});
725+
const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({
726+
format: "full",
727+
yieldToHost: yieldToBrowserTask,
728+
});
726729

727730
const targetStore = Persistence.ConnectionTargetStore.of({
728731
list: catalog.read.pipe(
@@ -948,18 +951,17 @@ export const layer = Layer.effectContext(
948951
),
949952
saveThread: Effect.fn("storage.saveThread")(
950953
function* (environmentId, snapshot) {
954+
const key = threadCacheKey(environmentId, snapshot.projection.thread.id);
951955
const encoded = yield* encodeStoredThreadSnapshot({
952-
schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
953956
environmentId,
954957
threadId: snapshot.projection.thread.id,
955958
snapshot,
956959
}).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
957-
yield* writeDatabaseValue(
958-
database,
959-
THREAD_STORE_NAME,
960-
threadCacheKey(environmentId, snapshot.projection.thread.id),
961-
encoded,
962-
);
960+
// An older copy of a thread now too large to keep would paint stale on reopen.
961+
yield* Option.match(encoded, {
962+
onNone: () => removeDatabaseValue(database, THREAD_STORE_NAME, key),
963+
onSome: (payload) => writeDatabaseValue(database, THREAD_STORE_NAME, key, payload),
964+
});
963965
},
964966
Effect.mapError((cause) =>
965967
cause._tag === "ConnectionPersistenceError"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Effect from "effect/Effect";
2+
3+
// Effect's own yield resumes through `setImmediate` where the host defines it and a
4+
// zero-delay timer otherwise. React Native's `setImmediate` is a microtask shim, so
5+
// nothing from the host runs before the fiber resumes. A timer yields on every host;
6+
// a browser can pass a yield that its timer clamping and throttling do not slow.
7+
export const yieldToHostTimer = Effect.callback<void>((resume) => {
8+
// @effect-diagnostics-next-line globalTimersInEffect:off - A host timer, not Effect.sleep, is the point.
9+
const timer = setTimeout(() => resume(Effect.void), 0);
10+
return Effect.sync(() => clearTimeout(timer));
11+
});

‎packages/client-runtime/src/platform/shellCacheEncoding.ts‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@ import { OrchestrationV2ThreadShellJson } from "@t3tools/contracts";
22
import * as Effect from "effect/Effect";
33
import * as Schema from "effect/Schema";
44

5+
import { yieldToHostTimer } from "./hostYield.ts";
56
import { StoredOrchestrationShellSnapshot } from "./orchestrationCache.ts";
67

78
const ROWS_PER_CHUNK = 32;
89

9-
// Effect's own yield resumes through `setImmediate` where the host defines it and a
10-
// zero-delay timer otherwise. React Native's `setImmediate` is a microtask shim, so
11-
// nothing from the host runs before the fiber resumes. A timer yields on every host;
12-
// a browser can pass a yield that its timer clamping and throttling do not slow.
13-
const yieldToHostTimer = Effect.callback<void>((resume) => {
14-
// @effect-diagnostics-next-line globalTimersInEffect:off - A host timer, not Effect.sleep, is the point.
15-
const timer = setTimeout(() => resume(Effect.void), 0);
16-
return Effect.sync(() => clearTimeout(timer));
17-
});
18-
1910
const encodeThreadChunk = Schema.encodeEffect(Schema.Array(OrchestrationV2ThreadShellJson));
2011
const EncodedRows = Schema.Array(Schema.Unknown);
2112
const encodeEnvelope = Schema.encodeEffect(
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import {
3+
EnvironmentId,
4+
ThreadId,
5+
TurnItemId,
6+
type OrchestrationV2ThreadProjection,
7+
} from "@t3tools/contracts";
8+
import * as DateTime from "effect/DateTime";
9+
import * as Effect from "effect/Effect";
10+
import * as Option from "effect/Option";
11+
import * as Schema from "effect/Schema";
12+
13+
import { v2Now, v2Projection, v2ThreadId } from "../state/orchestrationV2TestFixtures.ts";
14+
import {
15+
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
16+
StoredOrchestrationThreadSnapshot,
17+
} from "./orchestrationCache.ts";
18+
import {
19+
decodeStoredThreadSnapshot,
20+
makeStoredThreadSnapshotEncoder,
21+
} from "./threadCacheEncoding.ts";
22+
23+
const environmentId = EnvironmentId.make("environment-thread-cache");
24+
const otherThreadId = ThreadId.make("thread-parent");
25+
const encodeOriginal = Schema.encodeEffect(
26+
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
27+
);
28+
29+
const notice = (index: number, threadId = v2ThreadId) => ({
30+
id: TurnItemId.make(`notice-${threadId}-${index}`),
31+
threadId,
32+
runId: null,
33+
nodeId: null,
34+
providerThreadId: null,
35+
providerTurnId: null,
36+
nativeItemRef: null,
37+
parentItemId: null,
38+
ordinal: index,
39+
status: "completed" as const,
40+
title: index % 2 === 0 ? null : ` Notice ${index} `,
41+
startedAt: DateTime.add(v2Now, { seconds: index }),
42+
completedAt: index % 3 === 0 ? null : v2Now,
43+
updatedAt: v2Now,
44+
type: "system_notice" as const,
45+
message: `Notice ${index}`,
46+
});
47+
48+
// Inherited rows come first, as in a fork, then local rows whose items alias turnItems.
49+
function projectionWith(count: number): OrchestrationV2ThreadProjection {
50+
const inherited = Array.from({ length: 5 }, (_, index) => notice(index, otherThreadId));
51+
const turnItems = Array.from({ length: count }, (_, index) => notice(index));
52+
return {
53+
...v2Projection,
54+
turnItems,
55+
visibleTurnItems: [
56+
...inherited.map((item) => ({
57+
position: 0,
58+
visibility: "inherited" as const,
59+
sourceThreadId: otherThreadId,
60+
sourceItemId: item.id,
61+
item,
62+
})),
63+
...turnItems.map((item) => ({
64+
position: 0,
65+
visibility: "local" as const,
66+
sourceThreadId: v2ThreadId,
67+
sourceItemId: item.id,
68+
item,
69+
})),
70+
].map((row, position) => ({ ...row, position })),
71+
};
72+
}
73+
74+
const stored = (projection: OrchestrationV2ThreadProjection) => ({
75+
environmentId,
76+
threadId: v2ThreadId,
77+
snapshot: { snapshotSequence: 7, projection, historyCursor: null, hasMoreHistory: false },
78+
});
79+
80+
describe("cooperative thread cache encoding", () => {
81+
it.effect("matches the plain encoding across chunks, cold and after rows change", () =>
82+
Effect.gen(function* () {
83+
const encode = makeStoredThreadSnapshotEncoder({ format: "full" });
84+
const original = (projection: OrchestrationV2ThreadProjection) =>
85+
encodeOriginal({
86+
...stored(projection),
87+
schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
88+
});
89+
const cold = projectionWith(70);
90+
expect(yield* encode(stored(cold))).toEqual(Option.some(yield* original(cold)));
91+
92+
const [first, ...rest] = cold.turnItems;
93+
const replaced = { ...first!, message: "Edited" };
94+
const added = notice(70);
95+
const warm: OrchestrationV2ThreadProjection = {
96+
...cold,
97+
turnItems: [replaced, ...rest, added],
98+
visibleTurnItems: [
99+
...cold.visibleTurnItems.map((row) =>
100+
row.item === first ? { ...row, item: replaced } : row,
101+
),
102+
{
103+
position: cold.visibleTurnItems.length,
104+
visibility: "local",
105+
sourceThreadId: v2ThreadId,
106+
sourceItemId: added.id,
107+
item: added,
108+
},
109+
],
110+
};
111+
expect(yield* encode(stored(warm))).toEqual(Option.some(yield* original(warm)));
112+
}),
113+
);
114+
115+
it.effect("round-trips the compact format", () =>
116+
Effect.gen(function* () {
117+
const projection = projectionWith(40);
118+
const encoded = yield* makeStoredThreadSnapshotEncoder({ format: "compact" })(
119+
stored(projection),
120+
);
121+
const decoded = yield* decodeStoredThreadSnapshot(Option.getOrThrow(encoded));
122+
expect(decoded.snapshot.projection).toEqual(projection);
123+
}),
124+
);
125+
126+
it.effect("encodes nothing once the rows exceed the budget, warm or cold", () =>
127+
Effect.gen(function* () {
128+
const small = stored(projectionWith(4));
129+
const large = stored(projectionWith(200));
130+
const limit = Option.getOrThrow(
131+
yield* makeStoredThreadSnapshotEncoder({ format: "full" })(small),
132+
).length;
133+
for (const format of ["full", "compact"] as const) {
134+
const encode = makeStoredThreadSnapshotEncoder({ format, maxJsonLength: limit });
135+
expect(Option.isSome(yield* encode(small))).toBe(true);
136+
expect(Option.isNone(yield* encode(large))).toBe(true);
137+
expect(Option.isNone(yield* encode(large))).toBe(true);
138+
}
139+
}),
140+
);
141+
});

0 commit comments

Comments
 (0)