perf(clients): saving the thread list cache no longer freezes the UI (#13767)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-09-26 01:35:22 -07:00
1 parent 1d6f23b519
commit 9151ea407a
5 files changed
+122 -13

No files matched your search

@@ -0,0 +1,55 @@
import {
OrchestrationProjectShell,
OrchestrationShellSnapshot,
OrchestrationThreadShell,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Arr from "effect/Array";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as Arbitrary from "effect/unstable/arbitrary/Arbitrary";
import { encodeShellSnapshotForCache } from "./persistence.ts";
// Generated values can hold untrimmed strings, which a decoded value never
// has. One encode and decode gives a value a client can hold; values that
// fail are dropped. Size 30 makes the generator fill optional fields.
const sampleDecoded = <S extends Schema.Constraint>(schema: S) =>
Effect.gen(function* () {
const encode = Schema.encodeEffect(schema);
const decode = Schema.decodeEffect(schema);
const generated = yield* Arbitrary.sampleEffect(Arbitrary.schema(schema), {
count: 1000,
size: 30,
});
const decoded = yield* Effect.forEach(generated, (value) =>
encode(value).pipe(Effect.flatMap(decode), Effect.option),
);
return Arr.getSomes(decoded);
});
const encodeSnapshot = Schema.encodeEffect(OrchestrationShellSnapshot);
describe("encodeShellSnapshotForCache", () => {
it.effect("matches the Schema encoding of a generated snapshot", () =>
Effect.gen(function* () {
const threads = yield* sampleDecoded(OrchestrationThreadShell);
const projects = yield* sampleDecoded(OrchestrationProjectShell);
const snapshot: OrchestrationShellSnapshot = {
snapshotSequence: 1,
// The generator rarely makes monogram icons, and they are the one
// project field whose encoding differs from the decoded value.
projects: projects.map((project, index) =>
index % 2 === 0
? { ...project, projectIcon: { kind: "monogram", text: "T3", color: "blue" } }
: project,
),
threads,
updatedAt: "2026-09-25T00:00:00.000Z",
};
expect(threads.length).toBeGreaterThan(0);
expect(projects.length).toBeGreaterThan(0);
expect(yield* encodeShellSnapshotForCache(snapshot)).toEqual(yield* encodeSnapshot(snapshot));
}),
);
});
@@ -1,5 +1,6 @@
import {
type EnvironmentId,
OrchestrationProjectShell,
type OrchestrationShellSnapshot,
type OrchestrationThreadDetailSnapshot,
type ServerConfig,
@@ -130,6 +131,21 @@ export class EnvironmentCacheStore extends Context.Service<
}
>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
const encodeProjectShells = Schema.encodeEffect(Schema.Array(OrchestrationProjectShell));
/**
* Encodes a shell snapshot for `EnvironmentCacheStore.saveShell`. The result
* equals `Schema.encode(OrchestrationShellSnapshot)`, so the cache format does
* not change. Walking thousands of threads through Schema blocks the UI
* thread, and a decoded thread shell is already in its encoded form, so only
* the projects go through Schema: their icon has a real encode transform.
*/
export const encodeShellSnapshotForCache = (snapshot: OrchestrationShellSnapshot) =>
Effect.map(
encodeProjectShells(snapshot.projects),
(projects) => ({ ...snapshot, projects }) satisfies typeof OrchestrationShellSnapshot.Encoded,
);
export class EnvironmentOwnedDataCleanup extends Context.Reference<{
readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {