Skip to content

Commit 5a3daf0

Browse files
t3dotggclaude
andcommitted
fix(clients): keep the shell cache format while skipping thread encoding
Plain JSON.stringify saved monogram project icons in their decoded shape, which builds from before #11993 cannot read. Projects now still go through Schema (about 0.3 ms for 60 projects), and threads, which encode to themselves, are stringified directly. The saved value equals the full Schema encoding again. The helper lives in client-runtime so web and mobile share it. A property test compares it with the full Schema encoding for generated snapshots, so a future encode transform on any shell field fails the test. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8388e6c commit 5a3daf0

5 files changed

Lines changed: 79 additions & 142 deletions

File tree

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

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import {
2-
CommandId,
32
EnvironmentId,
43
ThreadId,
54
ProjectId,
65
ProviderInstanceId,
76
MessageId,
8-
TurnId,
9-
type OrchestrationShellSnapshot,
107
type OrchestrationThreadDetailSnapshot,
118
type VcsListRefsResult,
129
} from "@t3tools/contracts";
@@ -154,129 +151,6 @@ describe("mobile SQLite environment cache store", () => {
154151
}),
155152
);
156153

157-
// saveShell writes plain JSON, not the Schema encoding. This checks that
158-
// loadShell decodes what it wrote. The monogram project icon is where the
159-
// two encodings differ today.
160-
it.effect("reads back the shell snapshot it saves", () =>
161-
Effect.gen(function* () {
162-
const memory = makeDatabase();
163-
const store = yield* make().pipe(Effect.provideService(MobileDatabase, memory.database));
164-
const now = "2026-09-04T00:00:00.000Z";
165-
const projectId = ProjectId.make("project-1");
166-
const threadId = ThreadId.make("thread-1");
167-
const turnId = TurnId.make("turn-1");
168-
const modelSelection = {
169-
instanceId: ProviderInstanceId.make("codex"),
170-
model: "gpt-5.4",
171-
options: [{ id: "effort", value: "high" }],
172-
};
173-
const snapshot: OrchestrationShellSnapshot = {
174-
snapshotSequence: 7,
175-
projects: [
176-
{
177-
id: projectId,
178-
title: "T3 Code",
179-
workspaceRoot: "/repo",
180-
repositoryIdentity: {
181-
canonicalKey: "github.com/pingdotgg/t3code",
182-
locator: {
183-
source: "git-remote",
184-
remoteName: "origin",
185-
remoteUrl: "https://github.com/pingdotgg/t3code.git",
186-
},
187-
owner: "pingdotgg",
188-
name: "t3code",
189-
},
190-
defaultModelSelection: modelSelection,
191-
projectIcon: { kind: "monogram", text: "T3", color: "blue" },
192-
scripts: [
193-
{
194-
id: "test",
195-
name: "Test",
196-
command: "vp test",
197-
icon: "test",
198-
runOnWorktreeCreate: false,
199-
},
200-
],
201-
createdAt: now,
202-
updatedAt: now,
203-
},
204-
],
205-
threads: [
206-
{
207-
id: threadId,
208-
projectId,
209-
title: "Thread",
210-
modelSelection,
211-
runtimeMode: "full-access",
212-
interactionMode: "default",
213-
branch: "t3code/feature",
214-
worktreePath: "/repo/.worktrees/feature",
215-
pullRequests: [
216-
{
217-
host: "github.com",
218-
repository: "pingdotgg/t3code",
219-
number: 1,
220-
url: "https://github.com/pingdotgg/t3code/pull/1",
221-
source: "created",
222-
linkedAt: now,
223-
snapshot: {
224-
state: "open",
225-
title: "Feature",
226-
headBranch: "t3code/feature",
227-
baseBranch: "main",
228-
isDraft: false,
229-
updatedAt: now,
230-
syncedAt: now,
231-
},
232-
stack: null,
233-
},
234-
],
235-
latestTurn: {
236-
turnId,
237-
state: "running",
238-
requestedAt: now,
239-
startedAt: now,
240-
completedAt: null,
241-
assistantMessageId: MessageId.make("message-1"),
242-
},
243-
createdAt: now,
244-
updatedAt: now,
245-
archivedAt: null,
246-
settledOverride: null,
247-
settledAt: null,
248-
pinnedAt: now,
249-
titleState: {
250-
source: "generated",
251-
version: CommandId.make("command-1"),
252-
needsRefinement: false,
253-
},
254-
session: {
255-
threadId,
256-
status: "running",
257-
providerName: "codex",
258-
providerInstanceId: ProviderInstanceId.make("codex"),
259-
runtimeMode: "full-access",
260-
activeTurnId: turnId,
261-
lastError: null,
262-
updatedAt: now,
263-
},
264-
latestUserMessageAt: now,
265-
hasPendingApprovals: false,
266-
hasPendingUserInput: true,
267-
hasActionableProposedPlan: false,
268-
planProgress: { step: "Run tests", completedSteps: 1, totalSteps: 3 },
269-
},
270-
],
271-
updatedAt: now,
272-
};
273-
274-
yield* store.saveShell(ENVIRONMENT_ID, snapshot);
275-
276-
expect(yield* store.loadShell(ENVIRONMENT_ID)).toEqual(Option.some(snapshot));
277-
}),
278-
);
279-
280154
it.effect("round-trips schema-validated VCS refs", () =>
281155
Effect.gen(function* () {
282156
const memory = makeDatabase();

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ConnectionPersistenceError,
33
EnvironmentCacheStore,
4+
encodeShellSnapshotForCache,
45
} from "@t3tools/client-runtime/platform";
56
import {
67
type EnvironmentId,
@@ -135,18 +136,15 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
135136
}).pipe(Effect.tap(() => Effect.promise(() => projectFaviconDatabaseCache.hydrate()))),
136137
),
137138
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) {
138-
// Plain JSON: Schema encoding a snapshot with thousands of threads
139-
// blocks the JS thread. This skips encode transforms, so a monogram
140-
// project icon keeps its decoded shape. loadShell decodes through the
141-
// schema, which also accepts the decoded shape. The round-trip test
142-
// covers this. Builds from before monogram icons cannot read that
143-
// shape, so they drop the cache and load the server snapshot.
139+
const encodedSnapshot = yield* encodeShellSnapshotForCache(snapshot).pipe(
140+
Effect.mapError((cause) => persistenceError("save-shell", cause)),
141+
);
144142
const payload = yield* Effect.try({
145143
try: () =>
146144
JSON.stringify({
147145
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
148146
environmentId,
149-
snapshot,
147+
snapshot: encodedSnapshot,
150148
} satisfies typeof StoredShellSnapshot.Encoded),
151149
catch: (cause) => persistenceError("save-shell", cause),
152150
});

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ConnectionTargetStore,
77
EMPTY_CONNECTION_CATALOG_DOCUMENT,
88
EnvironmentCacheStore,
9+
encodeShellSnapshotForCache,
910
putRemoteDpopTokenInCatalog,
1011
registerConnectionInCatalog,
1112
removeCatalogValue,
@@ -607,20 +608,14 @@ export const connectionStorageLayer = Layer.effectContext(
607608
),
608609
saveShell: (environmentId, snapshot) =>
609610
Effect.gen(function* () {
610-
// Plain JSON: Schema encoding a snapshot with thousands of threads
611-
// blocks the main thread. This skips encode transforms, so a
612-
// monogram project icon keeps its decoded shape. loadShell decodes
613-
// through the schema, which also accepts the decoded shape. The
614-
// mobile cache store round-trip test covers this. Builds from
615-
// before monogram icons cannot read that shape, so they drop the
616-
// cache and load the server snapshot.
611+
const encodedSnapshot = yield* encodeShellSnapshotForCache(snapshot);
617612
const encoded = yield* Effect.try({
618613
try: () =>
619-
// @effect-diagnostics-next-line preferSchemaOverJson:off - see the comment above.
614+
// @effect-diagnostics-next-line preferSchemaOverJson:off - the snapshot is already encoded.
620615
JSON.stringify({
621616
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
622617
environmentId,
623-
snapshot,
618+
snapshot: encodedSnapshot,
624619
} satisfies typeof StoredShellSnapshot.Encoded),
625620
catch: (cause) => persistenceError("save-shell", cause),
626621
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
OrchestrationProjectShell,
3+
OrchestrationShellSnapshot,
4+
OrchestrationThreadShell,
5+
} from "@t3tools/contracts";
6+
import { describe, expect, it } from "@effect/vitest";
7+
import * as Arr from "effect/Array";
8+
import * as Effect from "effect/Effect";
9+
import * as Schema from "effect/Schema";
10+
import * as Arbitrary from "effect/unstable/arbitrary/Arbitrary";
11+
12+
import { encodeShellSnapshotForCache } from "./persistence.ts";
13+
14+
// Generated values can hold untrimmed strings, which a decoded value never
15+
// has. One encode and decode gives a value a client can hold; values that
16+
// fail are dropped. Size 30 makes the generator fill optional fields.
17+
const sampleDecoded = <S extends Schema.Constraint>(schema: S) =>
18+
Effect.gen(function* () {
19+
const encode = Schema.encodeEffect(schema);
20+
const decode = Schema.decodeEffect(schema);
21+
const generated = yield* Arbitrary.sampleEffect(Arbitrary.schema(schema), {
22+
count: 1000,
23+
size: 30,
24+
});
25+
const decoded = yield* Effect.forEach(generated, (value) =>
26+
encode(value).pipe(Effect.flatMap(decode), Effect.option),
27+
);
28+
return Arr.getSomes(decoded);
29+
});
30+
const encodeSnapshot = Schema.encodeEffect(OrchestrationShellSnapshot);
31+
32+
describe("encodeShellSnapshotForCache", () => {
33+
it.effect("matches the Schema encoding of a generated snapshot", () =>
34+
Effect.gen(function* () {
35+
const threads = yield* sampleDecoded(OrchestrationThreadShell);
36+
const projects = yield* sampleDecoded(OrchestrationProjectShell);
37+
const snapshot: OrchestrationShellSnapshot = {
38+
snapshotSequence: 1,
39+
// The generator rarely makes monogram icons, and they are the one
40+
// project field whose encoding differs from the decoded value.
41+
projects: projects.map((project, index) =>
42+
index % 2 === 0
43+
? { ...project, projectIcon: { kind: "monogram", text: "T3", color: "blue" } }
44+
: project,
45+
),
46+
threads,
47+
updatedAt: "2026-09-25T00:00:00.000Z",
48+
};
49+
50+
expect(threads.length).toBeGreaterThan(0);
51+
expect(yield* encodeShellSnapshotForCache(snapshot)).toEqual(yield* encodeSnapshot(snapshot));
52+
}),
53+
);
54+
});

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type EnvironmentId,
3+
OrchestrationProjectShell,
34
type OrchestrationShellSnapshot,
45
type OrchestrationThreadDetailSnapshot,
56
type ServerConfig,
@@ -130,6 +131,21 @@ export class EnvironmentCacheStore extends Context.Service<
130131
}
131132
>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
132133

134+
const encodeProjectShells = Schema.encodeEffect(Schema.Array(OrchestrationProjectShell));
135+
136+
/**
137+
* Encodes a shell snapshot for `EnvironmentCacheStore.saveShell`. The result
138+
* equals `Schema.encode(OrchestrationShellSnapshot)`, so the cache format does
139+
* not change. Walking thousands of threads through Schema blocks the UI
140+
* thread, and a decoded thread shell is already in its encoded form, so only
141+
* the projects go through Schema: their icon has a real encode transform.
142+
*/
143+
export const encodeShellSnapshotForCache = (snapshot: OrchestrationShellSnapshot) =>
144+
Effect.map(
145+
encodeProjectShells(snapshot.projects),
146+
(projects) => ({ ...snapshot, projects }) satisfies typeof OrchestrationShellSnapshot.Encoded,
147+
);
148+
133149
export class EnvironmentOwnedDataCleanup extends Context.Reference<{
134150
readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
135151
}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {

0 commit comments

Comments
 (0)