Skip to content

Commit 077fd62

Browse files
fix(client): finishing a long background thread no longer freezes the app (#33503)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7078dcb commit 077fd62

7 files changed

Lines changed: 461 additions & 51 deletions

File tree

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ORCHESTRATION_CACHE_SCHEMA_VERSION,
33
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
44
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
5+
type ConnectionPersistenceError,
56
StoredOrchestrationThreadSnapshot,
67
} from "@t3tools/client-runtime/platform";
78
import {
@@ -19,6 +20,7 @@ import {
1920
import { describe, expect, it } from "@effect/vitest";
2021
import * as DateTime from "effect/DateTime";
2122
import * as Effect from "effect/Effect";
23+
import * as Fiber from "effect/Fiber";
2224
import * as Logger from "effect/Logger";
2325
import * as Option from "effect/Option";
2426
import * as Schema from "effect/Schema";
@@ -213,6 +215,35 @@ function threadSnapshotWithItems(): OrchestrationV2ThreadDetailSnapshot {
213215
};
214216
}
215217

218+
// More rows than one encoding chunk, so a cold save yields to the host mid-encode.
219+
function threadSnapshotWithManyItems(
220+
snapshotSequence: number,
221+
message = "A settled local notice.",
222+
): OrchestrationV2ThreadDetailSnapshot {
223+
const base = threadSnapshotWithItems();
224+
const turnItems = Array.from({ length: 40 }, (_, index) => ({
225+
...base.projection.turnItems[0]!,
226+
id: TurnItemId.make(`notice-${snapshotSequence}-${index}`),
227+
ordinal: index,
228+
message,
229+
}));
230+
return {
231+
...THREAD_SNAPSHOT,
232+
snapshotSequence,
233+
projection: {
234+
...THREAD_SNAPSHOT.projection,
235+
turnItems,
236+
visibleTurnItems: turnItems.map((item, position) => ({
237+
position,
238+
visibility: "local" as const,
239+
sourceThreadId: THREAD_ID,
240+
sourceItemId: item.id,
241+
item,
242+
})),
243+
},
244+
};
245+
}
246+
216247
function makeDatabase() {
217248
const values = new Map<string, string>();
218249
const schemaVersions = new Map<string, number>();
@@ -608,6 +639,53 @@ describe("mobile SQLite environment cache store", () => {
608639
}).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
609640
});
610641

642+
it.effect("does not write a save that a removal, clear, or newer save overtook", () =>
643+
Effect.gen(function* () {
644+
const id = cacheId(ENVIRONMENT_ID, "thread", THREAD_ID);
645+
const overtake = Effect.fn("overtake")(function* (
646+
later: (
647+
store: Effect.Success<ReturnType<typeof make>>,
648+
) => Effect.Effect<void, ConnectionPersistenceError>,
649+
) {
650+
const memory = makeDatabase();
651+
const store = yield* make().pipe(
652+
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
653+
);
654+
const older = yield* store
655+
.saveThread(ENVIRONMENT_ID, threadSnapshotWithManyItems(1))
656+
.pipe(Effect.forkChild({ startImmediately: true }));
657+
yield* later(store);
658+
yield* Fiber.join(older);
659+
return { memory, store };
660+
});
661+
662+
const removed = yield* overtake((store) => store.removeThread(ENVIRONMENT_ID, THREAD_ID));
663+
expect(removed.memory.values.has(id)).toBe(false);
664+
const cleared = yield* overtake((store) => store.clear(ENVIRONMENT_ID));
665+
expect(cleared.memory.values.has(id)).toBe(false);
666+
const replaced = yield* overtake((store) =>
667+
store.saveThread(ENVIRONMENT_ID, { ...THREAD_SNAPSHOT, snapshotSequence: 2 }),
668+
);
669+
const kept = yield* replaced.store.loadThread(ENVIRONMENT_ID, THREAD_ID);
670+
expect(Option.getOrThrow(kept).snapshotSequence).toBe(2);
671+
}),
672+
);
673+
674+
it.effect("removes the older copy of a thread that grew past the cache limit", () =>
675+
Effect.gen(function* () {
676+
const memory = makeDatabase();
677+
const store = yield* make().pipe(
678+
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
679+
);
680+
const id = cacheId(ENVIRONMENT_ID, "thread", THREAD_ID);
681+
yield* store.saveThread(ENVIRONMENT_ID, threadSnapshotWithItems());
682+
expect(memory.values.has(id)).toBe(true);
683+
yield* store.saveThread(ENVIRONMENT_ID, threadSnapshotWithManyItems(2, "x".repeat(200_000)));
684+
expect(memory.values.has(id)).toBe(false);
685+
expect(memory.removed).toEqual([id]);
686+
}),
687+
);
688+
611689
it.effect("deletes a corrupt cache record and treats it as a miss", () =>
612690
Effect.gen(function* () {
613691
const memory = makeDatabase();

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import {
22
ORCHESTRATION_CACHE_SCHEMA_VERSION,
33
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
44
decodeStoredThreadSnapshot,
5-
encodeStoredThreadSnapshot,
65
Persistence,
76
makeStoredShellSnapshotEncoder,
7+
makeStoredThreadSnapshotEncoder,
8+
makeThreadCacheWriteOrder,
89
} from "@t3tools/client-runtime/platform";
910
import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
1011
import * as Effect from "effect/Effect";
@@ -96,6 +97,8 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
9697
const database = yield* MobileDatabase.MobileDatabase;
9798
attachProjectFaviconDatabase(database);
9899
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder();
100+
const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({ format: "compact" });
101+
const threadWriteOrder = makeThreadCacheWriteOrder();
99102
return Persistence.EnvironmentCacheStore.of({
100103
loadShell: Effect.fn("MobileEnvironmentCache.loadShell")((environmentId) =>
101104
loadDecodedCache({
@@ -156,20 +159,32 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
156159
),
157160
saveThread: Effect.fn("MobileEnvironmentCache.saveThread")(function* (environmentId, snapshot) {
158161
const threadId = snapshot.projection.thread.id;
162+
const isLatest = threadWriteOrder.beginSave(environmentId, threadId);
159163
const payload = yield* encodeStoredThreadSnapshot({
160164
environmentId,
161165
threadId,
162166
snapshot,
163167
}).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")));
168+
if (!isLatest()) return;
169+
// An older copy of a thread now too large to keep would paint stale on reopen.
170+
yield* Option.match(payload, {
171+
onNone: () => database.removeCache(environmentId, "thread", threadId),
172+
onSome: (encoded) =>
173+
database.saveCache(
174+
environmentId,
175+
"thread",
176+
threadId,
177+
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
178+
encoded,
179+
),
180+
}).pipe(Effect.mapError(mapDatabaseError("save-thread")));
167181
}),
168-
removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) =>
169-
database
182+
removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) => {
183+
threadWriteOrder.removeThread(environmentId, threadId);
184+
return database
170185
.removeCache(environmentId, "thread", threadId)
171-
.pipe(Effect.mapError(mapDatabaseError("remove-thread"))),
172-
),
186+
.pipe(Effect.mapError(mapDatabaseError("remove-thread")));
187+
}),
173188
loadServerConfig: Effect.fn("MobileEnvironmentCache.loadServerConfig")((environmentId) =>
174189
loadDecodedCache({
175190
database,
@@ -237,12 +252,13 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
237252
.clearCacheKind(environmentId, "vcs-refs")
238253
.pipe(Effect.mapError(mapDatabaseError("clear-vcs-refs"))),
239254
),
240-
clear: Effect.fn("MobileEnvironmentCache.clear")((environmentId) =>
241-
Effect.promise(() => projectFaviconDatabaseCache.clearEnvironment(environmentId)).pipe(
255+
clear: Effect.fn("MobileEnvironmentCache.clear")((environmentId) => {
256+
threadWriteOrder.clearEnvironment(environmentId);
257+
return Effect.promise(() => projectFaviconDatabaseCache.clearEnvironment(environmentId)).pipe(
242258
Effect.andThen(database.clearEnvironmentCache(environmentId)),
243259
Effect.mapError(mapDatabaseError("clear-environment")),
244-
),
245-
),
260+
);
261+
}),
246262
});
247263
});
248264

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

Lines changed: 17 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,8 @@ import {
1615
replaceCatalogValue,
1716
Persistence,
1817
makeStoredShellSnapshotEncoder,
18+
makeStoredThreadSnapshotEncoder,
19+
makeThreadCacheWriteOrder,
1920
} from "@t3tools/client-runtime/platform";
2021
import { TokenStore } from "@t3tools/client-runtime/authorization";
2122
import {
@@ -87,7 +88,6 @@ const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCat
8788
const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
8889
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
8990
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
90-
const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
9191
const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
9292
const encodeStoredServerConfig = Schema.encodeEffect(StoredServerConfigJson);
9393
const decodeStoredVcsRefs = Schema.decodeUnknownEffect(StoredVcsRefsJson);
@@ -723,6 +723,11 @@ export const layer = Layer.effectContext(
723723
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
724724
yieldToHost: yieldToBrowserTask,
725725
});
726+
const encodeStoredThreadSnapshot = makeStoredThreadSnapshotEncoder({
727+
format: "full",
728+
yieldToHost: yieldToBrowserTask,
729+
});
730+
const threadWriteOrder = makeThreadCacheWriteOrder();
726731

727732
const targetStore = Persistence.ConnectionTargetStore.of({
728733
list: catalog.read.pipe(
@@ -948,18 +953,19 @@ export const layer = Layer.effectContext(
948953
),
949954
saveThread: Effect.fn("storage.saveThread")(
950955
function* (environmentId, snapshot) {
956+
const key = threadCacheKey(environmentId, snapshot.projection.thread.id);
957+
const isLatest = threadWriteOrder.beginSave(environmentId, snapshot.projection.thread.id);
951958
const encoded = yield* encodeStoredThreadSnapshot({
952-
schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
953959
environmentId,
954960
threadId: snapshot.projection.thread.id,
955961
snapshot,
956962
}).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-
);
963+
if (!isLatest()) return;
964+
// An older copy of a thread now too large to keep would paint stale on reopen.
965+
yield* Option.match(encoded, {
966+
onNone: () => removeDatabaseValue(database, THREAD_STORE_NAME, key),
967+
onSome: (payload) => writeDatabaseValue(database, THREAD_STORE_NAME, key, payload),
968+
});
963969
},
964970
Effect.mapError((cause) =>
965971
cause._tag === "ConnectionPersistenceError"
@@ -1036,6 +1042,7 @@ export const layer = Layer.effectContext(
10361042
),
10371043
removeThread: Effect.fn("Persistence.EnvironmentCacheStore.removeThread")(
10381044
function (environmentId, threadId) {
1045+
threadWriteOrder.removeThread(environmentId, threadId);
10391046
return removeDatabaseValue(
10401047
database,
10411048
THREAD_STORE_NAME,
@@ -1044,6 +1051,7 @@ export const layer = Layer.effectContext(
10441051
},
10451052
),
10461053
clear: Effect.fn("Persistence.EnvironmentCacheStore.clear")(function (environmentId) {
1054+
threadWriteOrder.clearEnvironment(environmentId);
10471055
return Effect.all(
10481056
[
10491057
Effect.promise(() => projectFaviconCache.clearEnvironment(environmentId)),
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(

0 commit comments

Comments
 (0)