Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 134 additions & 1 deletion apps/mobile/src/connection/environment-cache-store.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
ORCHESTRATION_CACHE_SCHEMA_VERSION,
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
StoredOrchestrationThreadSnapshot,
} from "@t3tools/client-runtime/platform";
import {
CommandId,
EnvironmentId,
ProjectId,
ProviderInstanceId,
ThreadId,
TurnItemId,
type OrchestrationV2ShellSnapshot,
type OrchestrationV2ThreadDetailSnapshot,
type ThreadPullRequestLink,
Expand All @@ -17,6 +20,7 @@ import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";

import * as MobileDatabase from "../persistence/mobile-database";
import { make } from "./environment-cache-store";
Expand All @@ -26,6 +30,9 @@ const PROJECT_ID = ProjectId.make("project-1");
const THREAD_ID = ThreadId.make("thread-1");
const PROVIDER_INSTANCE_ID = ProviderInstanceId.make("codex");
const NOW = DateTime.makeUnsafe("2026-07-29T12:00:00.000Z");
const encodeLegacyThreadSnapshot = Schema.encodeEffect(
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
);
const SHELL_SNAPSHOT: OrchestrationV2ShellSnapshot = {
schemaVersion: 1,
snapshotSequence: 3,
Expand Down Expand Up @@ -152,6 +159,59 @@ function cacheId(
return `${environmentId}:${kind}:${cacheKey}`;
}

function threadSnapshotWithItems(): OrchestrationV2ThreadDetailSnapshot {
const local = {
id: TurnItemId.make("local-notice"),
threadId: THREAD_ID,
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 10,
status: "completed" as const,
title: null,
startedAt: NOW,
completedAt: NOW,
updatedAt: NOW,
type: "system_notice" as const,
message: "A settled local notice.",
};
const hidden = { ...local, id: TurnItemId.make("hidden-notice"), ordinal: 11 };
const inherited = {
...local,
id: TurnItemId.make("inherited-notice"),
threadId: ThreadId.make("parent-thread"),
};
return {
...THREAD_SNAPSHOT,
historyCursor: "older-page",
hasMoreHistory: true,
latestLocalTurnOrdinal: 11,
projection: {
...THREAD_SNAPSHOT.projection,
turnItems: [local, hidden],
visibleTurnItems: [
{
position: 0,
visibility: "inherited",
sourceThreadId: inherited.threadId,
sourceItemId: inherited.id,
item: inherited,
},
{
position: 1,
visibility: "local",
sourceThreadId: THREAD_ID,
sourceItemId: local.id,
item: local,
},
],
},
};
}

function makeDatabase() {
const values = new Map<string, string>();
const schemaVersions = new Map<string, number>();
Expand Down Expand Up @@ -198,6 +258,79 @@ function makeDatabase() {
}

describe("mobile SQLite environment cache store", () => {
it.effect("loads a V4 record and migrates to V5 on the next save without losing items", () =>
Effect.gen(function* () {
const memory = makeDatabase();
const store = yield* make().pipe(
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
);
const snapshot = threadSnapshotWithItems();
const id = cacheId(ENVIRONMENT_ID, "thread", THREAD_ID);
const raw = yield* encodeLegacyThreadSnapshot({
schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
environmentId: ENVIRONMENT_ID,
threadId: THREAD_ID,
snapshot,
});
memory.values.set(id, raw);
const loaded = Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID));
expect(loaded).toEqual(snapshot);
expect(memory.values.get(id)).toBe(raw);
yield* store.saveThread(ENVIRONMENT_ID, loaded);
expect(memory.schemaVersions.get(id)).toBe(COMPACT_THREAD_CACHE_SCHEMA_VERSION);
expect(Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID))).toEqual(
snapshot,
);
expect(memory.removed).toEqual([]);
}),
);

it.effect(
"discards obsolete and corrupt thread records and rejects another environment's cache",
() =>
Effect.gen(function* () {
const memory = makeDatabase();
const store = yield* make().pipe(
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
);
const id = cacheId(ENVIRONMENT_ID, "thread", THREAD_ID);
yield* store.saveThread(ENVIRONMENT_ID, threadSnapshotWithItems());
const raw = memory.values.get(id)!;
for (const invalid of ["{broken", raw.replace('"schemaVersion":5', '"schemaVersion":3')]) {
memory.values.set(id, invalid);
expect(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID)).toEqual(Option.none());
}
expect(memory.removed).toEqual([id, id]);
memory.values.set(
id,
raw.replace('"environmentId":"environment-1"', '"environmentId":"other"'),
);
expect(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID)).toEqual(Option.none());
memory.values.set(id, raw.replace('"threadId":"thread-1"', '"threadId":"other"'));
expect(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID)).toEqual(Option.none());
}),
);

it.effect(
"persists each local timeline payload once and restores shared items before returning",
() =>
Effect.gen(function* () {
const memory = makeDatabase();
const store = yield* make().pipe(
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
);
const snapshot = threadSnapshotWithItems();
yield* store.saveThread(ENVIRONMENT_ID, snapshot);
const raw = memory.values.get(cacheId(ENVIRONMENT_ID, "thread", THREAD_ID))!;
expect(raw.match(/"id":"local-notice"/g)).toHaveLength(1);

const loaded = Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID));
expect(loaded).toEqual(snapshot);
expect(loaded.projection.turnItems[0]).toBe(loaded.projection.visibleTurnItems[1]!.item);
expect(snapshot.projection.turnItems).toHaveLength(2);
}),
);

it.effect("round-trips V2 shell and thread DateTime fields with the shared cache schema", () =>
Effect.gen(function* () {
const memory = makeDatabase();
Expand Down Expand Up @@ -229,7 +362,7 @@ describe("mobile SQLite environment cache store", () => {
ORCHESTRATION_CACHE_SCHEMA_VERSION,
);
expect(memory.schemaVersions.get(cacheId(ENVIRONMENT_ID, "thread", THREAD_ID))).toBe(
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
);
}),
);
Expand Down
20 changes: 4 additions & 16 deletions apps/mobile/src/connection/environment-cache-store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
ORCHESTRATION_CACHE_SCHEMA_VERSION,
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
StoredOrchestrationThreadSnapshot,
COMPACT_THREAD_CACHE_SCHEMA_VERSION,
decodeStoredThreadSnapshot,
encodeStoredThreadSnapshot,
Persistence,
makeStoredShellSnapshotEncoder,
} from "@t3tools/client-runtime/platform";
Expand Down Expand Up @@ -33,12 +34,6 @@ const StoredVcsRefs = Schema.Struct({
refs: VcsListRefsResult,
});

const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
);
const encodeStoredThreadSnapshot = Schema.encodeEffect(
Schema.fromJsonString(StoredOrchestrationThreadSnapshot),
);
const decodeStoredServerConfig = Schema.decodeUnknownEffect(
Schema.fromJsonString(StoredServerConfig),
);
Expand Down Expand Up @@ -147,19 +142,12 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
saveThread: Effect.fn("MobileEnvironmentCache.saveThread")(function* (environmentId, snapshot) {
const threadId = snapshot.projection.thread.id;
const payload = yield* encodeStoredThreadSnapshot({
schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
environmentId,
threadId,
snapshot,
}).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
yield* database
.saveCache(
environmentId,
"thread",
threadId,
ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
payload,
)
.saveCache(environmentId, "thread", threadId, COMPACT_THREAD_CACHE_SCHEMA_VERSION, payload)
.pipe(Effect.mapError(mapDatabaseError("save-thread")));
}),
removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) =>
Expand Down
1 change: 1 addition & 0 deletions packages/client-runtime/src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export * as Persistence from "./persistence.ts";
export { ConnectionPersistenceError } from "./persistence.ts";
export * from "./orchestrationCache.ts";
export * from "./shellCacheEncoding.ts";
export * from "./threadCacheEncoding.ts";
export * as PlatformConnectionSource from "./source.ts";
export * from "./storageDocument.ts";
Loading
Loading