Skip to content

Commit e2b929f

Browse files
bompusjuliusmarmingeclaude
authored
perf(web): saving the thread list cache no longer re-encodes every thread (pingdotgg#19915)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2dd87a4 commit e2b929f

6 files changed

Lines changed: 388 additions & 268 deletions

File tree

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

Lines changed: 0 additions & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
ORCHESTRATION_CACHE_SCHEMA_VERSION,
33
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
4-
StoredOrchestrationShellSnapshot,
54
} from "@t3tools/client-runtime/platform";
65
import {
76
CommandId,
@@ -17,16 +16,11 @@ import {
1716
import { describe, expect, it } from "@effect/vitest";
1817
import * as DateTime from "effect/DateTime";
1918
import * as Effect from "effect/Effect";
20-
import * as Fiber from "effect/Fiber";
2119
import * as Logger from "effect/Logger";
22-
import * as Schema from "effect/Schema";
23-
import { vi } from "vite-plus/test";
24-
import * as Deferred from "effect/Deferred";
2520
import * as Option from "effect/Option";
2621

2722
import * as MobileDatabase from "../persistence/mobile-database";
2823
import { make } from "./environment-cache-store";
29-
import { makeStoredShellSnapshotEncoder } from "./shell-cache-encoding";
3024

3125
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
3226
const PROJECT_ID = ProjectId.make("project-1");
@@ -408,258 +402,3 @@ describe("mobile SQLite environment cache store", () => {
408402
}),
409403
);
410404
});
411-
412-
describe("cooperative shell cache encoding", () => {
413-
// A fresh encoder per call keeps these cases cold; the cache has its own cases below.
414-
const encodeStoredShellSnapshot = (stored: typeof StoredOrchestrationShellSnapshot.Type) =>
415-
makeStoredShellSnapshotEncoder()(stored);
416-
const encodeOriginal = Schema.encodeEffect(
417-
Schema.fromJsonString(StoredOrchestrationShellSnapshot),
418-
);
419-
const stored = {
420-
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
421-
environmentId: ENVIRONMENT_ID,
422-
snapshot: {
423-
...SHELL_SNAPSHOT,
424-
projects: SHELL_SNAPSHOT.projects.map((project) => ({
425-
...project,
426-
title: " Project ",
427-
unexpected: "drop this field",
428-
})),
429-
threads: Array.from({ length: 64 }, (_, index) => ({
430-
...SHELL_SNAPSHOT.threads[0]!,
431-
id: ThreadId.make(`thread-${index}`),
432-
branch: " main ",
433-
activityRunStartedAt: index % 2 === 0 ? NOW : null,
434-
unsettledAt: index % 2 === 0 ? NOW : null,
435-
modelSelection: {
436-
instanceId: PROVIDER_INSTANCE_ID,
437-
model: " gpt-5.4 ",
438-
unexpected: "drop this field",
439-
},
440-
unexpected: "drop this field",
441-
})),
442-
archivedThreads: [{ ...SHELL_SNAPSHOT.threads[0]!, archivedAt: NOW }],
443-
unexpected: "drop this field",
444-
},
445-
unexpected: "drop this field",
446-
};
447-
448-
it.effect("preserves exact JSON, transforms, nulls, dates, and unknown-field stripping", () =>
449-
Effect.gen(function* () {
450-
const expected = yield* encodeOriginal(stored);
451-
const actual = yield* encodeStoredShellSnapshot(stored);
452-
expect(actual).toBe(expected);
453-
const parsed = JSON.parse(actual);
454-
expect(parsed).not.toHaveProperty("unexpected");
455-
expect(parsed.snapshot).not.toHaveProperty("unexpected");
456-
expect(parsed.snapshot.projects[0]).not.toHaveProperty("unexpected");
457-
expect(parsed.snapshot.projects[0].title).toBe("Project");
458-
expect(parsed.snapshot.threads[0]).not.toHaveProperty("unexpected");
459-
expect(parsed.snapshot.threads[0].branch).toBe("main");
460-
expect(parsed.snapshot.threads[0].modelSelection).toEqual({
461-
instanceId: "codex",
462-
model: "gpt-5.4",
463-
});
464-
expect(parsed.snapshot.threads[0].activityRunStartedAt).toBe(DateTime.formatIso(NOW));
465-
expect(parsed.snapshot.threads[1].activityRunStartedAt).toBeNull();
466-
expect(parsed.snapshot.archivedThreads[0].archivedAt).toBe(DateTime.formatIso(NOW));
467-
}),
468-
);
469-
470-
it.effect("preserves optional fields and dates across chunk boundaries and archived rows", () =>
471-
Effect.gen(function* () {
472-
const base = SHELL_SNAPSHOT.threads[0]!;
473-
const { titleRegeneration: _, unsettledAt: __, ...withoutOptional } = base;
474-
const row = (index: number) =>
475-
index % 3 === 0
476-
? { ...withoutOptional, id: ThreadId.make(`row-${index}`) }
477-
: {
478-
...base,
479-
id: ThreadId.make(`row-${index}`),
480-
snoozedUntil: DateTime.add(NOW, { minutes: index }),
481-
pinnedAt: index % 2 === 0 ? null : NOW,
482-
};
483-
const mixed = {
484-
...stored,
485-
snapshot: {
486-
...stored.snapshot,
487-
threads: Array.from({ length: 65 }, (_, index) => row(index)),
488-
archivedThreads: Array.from({ length: 33 }, (_, index) => ({
489-
...row(index + 100),
490-
archivedAt: DateTime.add(NOW, { hours: index }),
491-
})),
492-
},
493-
};
494-
const actual = yield* encodeStoredShellSnapshot(mixed);
495-
expect(actual).toBe(yield* encodeOriginal(mixed));
496-
const parsed = JSON.parse(actual);
497-
expect(parsed.snapshot.threads).toHaveLength(65);
498-
expect(parsed.snapshot.threads[63]).not.toHaveProperty("titleRegeneration");
499-
expect(parsed.snapshot.threads[63]).not.toHaveProperty("unsettledAt");
500-
expect(parsed.snapshot.threads[64].snoozedUntil).toBe(
501-
DateTime.formatIso(DateTime.add(NOW, { minutes: 64 })),
502-
);
503-
expect(parsed.snapshot.archivedThreads).toHaveLength(33);
504-
expect(parsed.snapshot.archivedThreads[32].archivedAt).toBe(
505-
DateTime.formatIso(DateTime.add(NOW, { hours: 32 })),
506-
);
507-
}),
508-
);
509-
510-
it.effect("yields to a host timer between every 32-row chunk", () =>
511-
Effect.gen(function* () {
512-
const setTimer = vi.spyOn(globalThis, "setTimeout");
513-
try {
514-
yield* encodeStoredShellSnapshot({
515-
...stored,
516-
snapshot: {
517-
...stored.snapshot,
518-
threads: Array.from({ length: 65 }, () => ({ ...stored.snapshot.threads[0]! })),
519-
archivedThreads: Array.from({ length: 33 }, () => ({
520-
...stored.snapshot.archivedThreads[0]!,
521-
})),
522-
},
523-
});
524-
// Five row chunks (3 active + 2 archived): four yields between them, one before the envelope.
525-
expect(setTimer).toHaveBeenCalledTimes(5);
526-
} finally {
527-
vi.restoreAllMocks();
528-
}
529-
}),
530-
);
531-
532-
it.effect("still rejects invalid active and archived rows after an encoding chunk yields", () =>
533-
Effect.gen(function* () {
534-
const invalidRow = { ...stored.snapshot.threads[0]!, itemCount: -1 };
535-
const invalidActive = {
536-
...stored,
537-
snapshot: { ...stored.snapshot, threads: [...stored.snapshot.threads, invalidRow] },
538-
};
539-
const invalidArchived = {
540-
...stored,
541-
snapshot: { ...stored.snapshot, archivedThreads: [invalidRow] },
542-
};
543-
const invalidEnvelope = { ...stored, snapshot: { ...stored.snapshot, snapshotSequence: -1 } };
544-
expect(yield* Effect.isFailure(encodeStoredShellSnapshot(invalidActive))).toBe(true);
545-
expect(yield* Effect.isFailure(encodeStoredShellSnapshot(invalidArchived))).toBe(true);
546-
expect(yield* Effect.isFailure(encodeOriginal(invalidEnvelope))).toBe(true);
547-
expect(yield* Effect.isFailure(encodeStoredShellSnapshot(invalidEnvelope))).toBe(true);
548-
}),
549-
);
550-
551-
it.effect("keeps concurrent saves independent", () =>
552-
Effect.gen(function* () {
553-
const other = {
554-
...stored,
555-
environmentId: EnvironmentId.make("environment-2"),
556-
snapshot: {
557-
...stored.snapshot,
558-
threads: stored.snapshot.threads.slice(0, 40).map((thread) => ({
559-
...thread,
560-
title: `Other ${thread.id}`,
561-
})),
562-
archivedThreads: [],
563-
},
564-
};
565-
const [first, second] = yield* Effect.all(
566-
[encodeStoredShellSnapshot(stored), encodeStoredShellSnapshot(other)],
567-
{ concurrency: "unbounded" },
568-
);
569-
expect(first).toBe(yield* encodeOriginal(stored));
570-
expect(second).toBe(yield* encodeOriginal(other));
571-
}),
572-
);
573-
574-
it.effect("cancels the pending host timer when encoding is interrupted", () =>
575-
Effect.gen(function* () {
576-
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
577-
const fakeSetTimeout = globalThis.setTimeout;
578-
const scheduled = yield* Deferred.make<ReturnType<typeof setTimeout>>();
579-
vi.spyOn(globalThis, "setTimeout").mockImplementation((...args) => {
580-
const timer = fakeSetTimeout(...args);
581-
Deferred.doneUnsafe(scheduled, Effect.succeed(timer));
582-
return timer;
583-
});
584-
const clearTimer = vi.spyOn(globalThis, "clearTimeout");
585-
const fiber = yield* Effect.forkScoped(
586-
Effect.suspend(() => encodeStoredShellSnapshot(stored)),
587-
);
588-
try {
589-
const pendingTimer = yield* Deferred.await(scheduled);
590-
expect(vi.getTimerCount()).toBe(1);
591-
yield* Fiber.interrupt(fiber);
592-
expect(clearTimer).toHaveBeenCalledWith(pendingTimer);
593-
expect(vi.getTimerCount()).toBe(0);
594-
} finally {
595-
yield* Fiber.interrupt(fiber);
596-
vi.restoreAllMocks();
597-
vi.useRealTimers();
598-
}
599-
}),
600-
);
601-
602-
it.effect("reuses encoded rows across saves without changing the payload", () =>
603-
Effect.gen(function* () {
604-
const encode = makeStoredShellSnapshotEncoder();
605-
expect(yield* encode(stored)).toBe(yield* encodeOriginal(stored));
606-
607-
const setTimer = vi.spyOn(globalThis, "setTimeout");
608-
try {
609-
// Same row references, new envelope: no row work, so no host yields.
610-
const resaved = {
611-
...stored,
612-
snapshot: { ...stored.snapshot, snapshotSequence: 4 },
613-
};
614-
expect(yield* encode(resaved)).toBe(yield* encodeOriginal(resaved));
615-
expect(setTimer).not.toHaveBeenCalled();
616-
} finally {
617-
vi.restoreAllMocks();
618-
}
619-
}),
620-
);
621-
622-
it.effect("encodes replaced, added, and moved-to-archive rows on a warm encoder", () =>
623-
Effect.gen(function* () {
624-
const encode = makeStoredShellSnapshotEncoder();
625-
yield* encode(stored);
626-
const [first, second, ...rest] = stored.snapshot.threads;
627-
const next = {
628-
...stored,
629-
snapshot: {
630-
...stored.snapshot,
631-
threads: [
632-
{ ...first!, title: "Renamed", itemCount: 9 },
633-
...rest,
634-
{ ...first!, id: ThreadId.make("thread-new"), branch: " feature " },
635-
],
636-
archivedThreads: [
637-
...stored.snapshot.archivedThreads,
638-
{ ...second!, archivedAt: DateTime.add(NOW, { hours: 1 }) },
639-
],
640-
},
641-
};
642-
const actual = yield* encode(next);
643-
expect(actual).toBe(yield* encodeOriginal(next));
644-
const parsed = JSON.parse(actual);
645-
expect(parsed.snapshot.threads[0].title).toBe("Renamed");
646-
expect(parsed.snapshot.threads.at(-1).branch).toBe("feature");
647-
expect(parsed.snapshot.archivedThreads).toHaveLength(2);
648-
}),
649-
);
650-
651-
it.effect("does not cache rows from a failed encode", () =>
652-
Effect.gen(function* () {
653-
const encode = makeStoredShellSnapshotEncoder();
654-
const invalidRow = { ...stored.snapshot.threads[0]!, itemCount: -1 };
655-
const invalid = {
656-
...stored,
657-
snapshot: { ...stored.snapshot, threads: [...stored.snapshot.threads, invalidRow] },
658-
};
659-
expect(yield* Effect.isFailure(encode(invalid))).toBe(true);
660-
expect(yield* Effect.isFailure(encode(invalid))).toBe(true);
661-
// Valid rows from the failed save still encode correctly afterwards.
662-
expect(yield* encode(stored)).toBe(yield* encodeOriginal(stored));
663-
}),
664-
);
665-
});

‎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: 24 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,25 @@ export function makeBrowserGitHubRoutingPermissions(
601601
});
602602
}
603603

604+
// A posted message resumes in a fresh task, so input and rendering get a turn between
605+
// encoding chunks. A zero-delay timer would not do: browsers clamp nested timers to
606+
// 4 ms and throttle them to once a second or less in a background tab.
607+
const yieldToBrowserTask = Effect.callback<void>((resume) => {
608+
const { port1, port2 } = new MessageChannel();
609+
port1.addEventListener(
610+
"message",
611+
() => {
612+
port1.close();
613+
resume(Effect.void);
614+
},
615+
{ once: true },
616+
);
617+
port1.start();
618+
port2.postMessage(null);
619+
// A closed port delivers nothing, so interruption needs no listener bookkeeping.
620+
return Effect.sync(() => port1.close());
621+
});
622+
604623
export const layer = Layer.effectContext(
605624
Effect.gen(function* () {
606625
const { handle: database } = yield* Effect.acquireRelease(
@@ -609,6 +628,10 @@ export const layer = Layer.effectContext(
609628
);
610629
const catalog = yield* makeCatalogStore(makeCatalogBackend(database));
611630
const githubRoutingPermissions = makeBrowserGitHubRoutingPermissions();
631+
// Remembers each thread row's encoding, so a save only encodes rows that changed.
632+
const encodeStoredShellSnapshot = makeStoredShellSnapshotEncoder({
633+
yieldToHost: yieldToBrowserTask,
634+
});
612635

613636
const targetStore = Persistence.ConnectionTargetStore.of({
614637
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";

0 commit comments

Comments
 (0)