Skip to content

Commit da89192

Browse files
committed
fix(web): saving the thread list cache no longer re-encodes every thread on the UI thread
1 parent 339179f commit da89192

6 files changed

Lines changed: 153 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
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
StoredOrchestrationShellSnapshot,
5+
makeStoredShellSnapshotEncoder,
56
} from "@t3tools/client-runtime/platform";
67
import {
78
CommandId,
@@ -26,7 +27,6 @@ import * as Option from "effect/Option";
2627

2728
import * as MobileDatabase from "../persistence/mobile-database";
2829
import { make } from "./environment-cache-store";
29-
import { makeStoredShellSnapshotEncoder } from "./shell-cache-encoding";
3030

3131
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
3232
const PROJECT_ID = ProjectId.make("project-1");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
44
StoredOrchestrationThreadSnapshot,
55
Persistence,
6+
makeStoredShellSnapshotEncoder,
67
} from "@t3tools/client-runtime/platform";
78
import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts";
89
import * as Effect from "effect/Effect";
@@ -11,7 +12,6 @@ import * as Option from "effect/Option";
1112
import * as Schema from "effect/Schema";
1213

1314
import * as MobileDatabase from "../persistence/mobile-database";
14-
import { makeStoredShellSnapshotEncoder } from "./shell-cache-encoding";
1515
import {
1616
attachProjectFaviconDatabase,
1717
projectFaviconDatabaseCache,

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
setRoutesInCatalog,
1616
replaceCatalogValue,
1717
Persistence,
18+
makeStoredShellSnapshotEncoder,
1819
} from "@t3tools/client-runtime/platform";
1920
import { TokenStore } from "@t3tools/client-runtime/authorization";
2021
import {
@@ -68,7 +69,6 @@ const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDoc
6869
const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocumentJson);
6970
const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
7071
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
71-
const encodeStoredShellSnapshot = Schema.encodeEffect(StoredShellSnapshotJson);
7272
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
7373
const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
7474
const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
@@ -601,6 +601,24 @@ export function makeBrowserGitHubRoutingPermissions(
601601
});
602602
}
603603

604+
// A message task lets input and rendering run between encoding chunks. Unlike a zero-delay
605+
// timer it is neither clamped to 4 ms after a few chained yields nor stretched to a second
606+
// or more per chunk in a background tab.
607+
const yieldToBrowserTask = Effect.callback<void>((resume) => {
608+
const channel = new MessageChannel();
609+
const onMessage = () => {
610+
channel.port1.close();
611+
resume(Effect.void);
612+
};
613+
channel.port1.addEventListener("message", onMessage, { once: true });
614+
channel.port1.start();
615+
channel.port2.postMessage(null);
616+
return Effect.sync(() => {
617+
channel.port1.removeEventListener("message", onMessage);
618+
channel.port1.close();
619+
});
620+
});
621+
604622
export const layer = Layer.effectContext(
605623
Effect.gen(function* () {
606624
const { handle: database } = yield* Effect.acquireRelease(
@@ -609,6 +627,10 @@ export const layer = Layer.effectContext(
609627
);
610628
const catalog = yield* makeCatalogStore(makeCatalogBackend(database));
611629
const githubRoutingPermissions = makeBrowserGitHubRoutingPermissions();
630+
// Remembers each thread row's encoding, so a save only encodes rows that changed.
631+
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
632+
yieldToHost: yieldToBrowserTask,
633+
});
612634

613635
const targetStore = Persistence.ConnectionTargetStore.of({
614636
list: catalog.read.pipe(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * as Persistence from "./persistence.ts";
33
// Flat so consumers' inferred types can name it.
44
export { ConnectionPersistenceError } from "./persistence.ts";
55
export * from "./orchestrationCache.ts";
6+
export * from "./shellCacheEncoding.ts";
67
export * as PlatformConnectionSource from "./source.ts";
78
export * from "./storageDocument.ts";
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import {
3+
EnvironmentId,
4+
ProjectId,
5+
ProviderInstanceId,
6+
ThreadId,
7+
type OrchestrationV2ThreadShell,
8+
} from "@t3tools/contracts";
9+
import * as DateTime from "effect/DateTime";
10+
import * as Effect from "effect/Effect";
11+
import * as Schema from "effect/Schema";
12+
13+
import {
14+
ORCHESTRATION_CACHE_SCHEMA_VERSION,
15+
StoredOrchestrationShellSnapshot,
16+
} from "./orchestrationCache.ts";
17+
import { makeStoredShellSnapshotEncoder } from "./shellCacheEncoding.ts";
18+
19+
const NOW = DateTime.makeUnsafe("2026-07-29T12:00:00.000Z");
20+
const PROJECT_ID = ProjectId.make("project-1");
21+
const PROVIDER_INSTANCE_ID = ProviderInstanceId.make("codex");
22+
23+
const makeThread = (index: number): OrchestrationV2ThreadShell => {
24+
const id = ThreadId.make(`thread-${index}`);
25+
return {
26+
id,
27+
projectId: PROJECT_ID,
28+
title: `Thread ${index}`,
29+
providerInstanceId: PROVIDER_INSTANCE_ID,
30+
modelSelection: { instanceId: PROVIDER_INSTANCE_ID, model: "gpt-5.4" },
31+
runtimeMode: "full-access",
32+
interactionMode: "default",
33+
branch: null,
34+
worktreePath: null,
35+
activeProviderThreadId: null,
36+
lineage: { rootThreadId: id, parentThreadId: null, relationshipToParent: null },
37+
forkedFrom: null,
38+
createdBy: "user",
39+
creationSource: "web",
40+
latestRunId: null,
41+
activeRunId: null,
42+
status: "idle",
43+
pendingRuntimeRequest: null,
44+
latestVisibleMessage: null,
45+
latestUserMessageAt: NOW,
46+
activityRunStartedAt: null,
47+
activityRunStatus: null,
48+
hasActionableProposedPlan: false,
49+
itemCount: 0,
50+
visibleItemCount: 0,
51+
createdAt: NOW,
52+
updatedAt: NOW,
53+
archivedAt: null,
54+
settledOverride: null,
55+
settledAt: NOW,
56+
unsettledAt: null,
57+
lastVisitedAt: null,
58+
deletedAt: null,
59+
};
60+
};
61+
62+
const makeStored = (threads: ReadonlyArray<OrchestrationV2ThreadShell>) => ({
63+
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
64+
environmentId: EnvironmentId.make("environment-1"),
65+
snapshot: {
66+
schemaVersion: 1 as const,
67+
snapshotSequence: 3,
68+
projects: [],
69+
threads,
70+
archivedThreads: [],
71+
},
72+
});
73+
74+
describe("shell cache encoder with a caller-supplied yield", () => {
75+
const encodeOriginal = Schema.encodeEffect(
76+
Schema.fromJsonString(StoredOrchestrationShellSnapshot),
77+
);
78+
79+
it.effect("keeps output identical to the full encode when only one row changes", () =>
80+
Effect.gen(function* () {
81+
let yields = 0;
82+
const encode = makeStoredShellSnapshotEncoder({
83+
yieldToHost: Effect.sync(() => {
84+
yields += 1;
85+
}),
86+
});
87+
const threads = Array.from({ length: 70 }, (_, index) => makeThread(index));
88+
const first = makeStored(threads);
89+
expect(yield* encode(first)).toBe(yield* encodeOriginal(first));
90+
// 70 rows are three 32-row chunks, so the first save yields between them.
91+
expect(yields).toBeGreaterThanOrEqual(2);
92+
93+
const second = makeStored([{ ...threads[0]!, title: "Renamed" }, ...threads.slice(1)]);
94+
expect(yield* encode(second)).toBe(yield* encodeOriginal(second));
95+
}),
96+
);
97+
98+
it.effect("does not yield again when every row was already encoded", () =>
99+
Effect.gen(function* () {
100+
let yields = 0;
101+
const encode = makeStoredShellSnapshotEncoder({
102+
yieldToHost: Effect.sync(() => {
103+
yields += 1;
104+
}),
105+
});
106+
const stored = makeStored(Array.from({ length: 70 }, (_, index) => makeThread(index)));
107+
yield* encode(stored);
108+
const afterFirst = yields;
109+
yield* encode(makeStored(stored.snapshot.threads));
110+
expect(yields).toBe(afterFirst);
111+
}),
112+
);
113+
});

apps/mobile/src/connection/shell-cache-encoding.ts renamed to packages/client-runtime/src/platform/shellCacheEncoding.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { StoredOrchestrationShellSnapshot } from "@t3tools/client-runtime/platform";
1+
// @effect-diagnostics globalTimers:off globalTimersInEffect:off -- A host timer, not Effect.sleep, lets input and rendering run between row chunks.
22
import { OrchestrationV2ThreadShellJson } from "@t3tools/contracts";
33
import * as Effect from "effect/Effect";
44
import * as Schema from "effect/Schema";
55

6+
import { StoredOrchestrationShellSnapshot } from "./orchestrationCache.ts";
7+
68
const ROWS_PER_CHUNK = 32;
79

8-
// Effect's cooperative yield resumes through microtasks on React Native. A host
9-
// timer lets input and rendering run between chunks of shell rows instead.
10-
const yieldToHost = Effect.callback<void>((resume) => {
10+
// Effect's cooperative yield resumes through microtasks, which run before the next
11+
// frame on React Native and before input in a browser. A host timer lets input and
12+
// rendering run between chunks of shell rows instead.
13+
const yieldToHostTimer = Effect.callback<void>((resume) => {
1114
const timer = setTimeout(() => resume(Effect.void), 0);
1215
return Effect.sync(() => clearTimeout(timer));
1316
});
@@ -38,8 +41,14 @@ const encodeEnvelope = Schema.encodeEffect(
3841
* Shell rows are immutable and the shell reducer keeps unchanged rows by
3942
* reference, so each encoder remembers the canonical encoding of rows it has
4043
* already encoded successfully and only runs the row codec for new rows.
44+
*
45+
* `yieldToHost` defaults to a zero-delay timer. A browser should pass a yield
46+
* that background-tab timer throttling cannot stretch to a second per chunk.
4147
*/
42-
export function makeStoredShellSnapshotEncoder() {
48+
export function makeStoredShellSnapshotEncoder(
49+
options: { readonly yieldToHost?: Effect.Effect<void> } = {},
50+
) {
51+
const yieldToHost = options.yieldToHost ?? yieldToHostTimer;
4352
const encodedRows = new WeakMap<OrchestrationV2ThreadShellJson, unknown>();
4453

4554
return Effect.fnUntraced(function* (stored: typeof StoredOrchestrationShellSnapshot.Type) {

0 commit comments

Comments
 (0)