diff --git a/apps/mobile/src/connection/environment-cache-store.test.ts b/apps/mobile/src/connection/environment-cache-store.test.ts index ecb706f972..a3e7686c7e 100644 --- a/apps/mobile/src/connection/environment-cache-store.test.ts +++ b/apps/mobile/src/connection/environment-cache-store.test.ts @@ -1,5 +1,15 @@ -import { EnvironmentId, type VcsListRefsResult } from "@t3tools/contracts"; +import { ORCHESTRATION_CACHE_SCHEMA_VERSION } from "@t3tools/client-runtime/platform"; +import { + EnvironmentId, + ProjectId, + ProviderInstanceId, + ThreadId, + type OrchestrationV2ShellSnapshot, + type OrchestrationV2ThreadDetailSnapshot, + type VcsListRefsResult, +} from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; +import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; @@ -7,6 +17,118 @@ import { type ClientCacheKind, MobileDatabase } from "../persistence/mobile-data import { make } from "./environment-cache-store"; const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); +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 SHELL_SNAPSHOT: OrchestrationV2ShellSnapshot = { + schemaVersion: 1, + snapshotSequence: 3, + projects: [ + { + id: PROJECT_ID, + title: "Project", + workspaceRoot: "/repo", + repositoryIdentity: null, + defaultModelSelection: null, + scripts: [], + createdAt: "2026-07-29T12:00:00.000Z", + updatedAt: "2026-07-29T12:00:00.000Z", + }, + ], + threads: [ + { + id: THREAD_ID, + projectId: PROJECT_ID, + title: "Thread", + providerInstanceId: PROVIDER_INSTANCE_ID, + modelSelection: { + instanceId: PROVIDER_INSTANCE_ID, + model: "gpt-5.4", + }, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { + rootThreadId: THREAD_ID, + parentThreadId: null, + relationshipToParent: null, + }, + forkedFrom: null, + createdBy: "user", + creationSource: "mobile", + latestRunId: null, + activeRunId: null, + status: "idle", + pendingRuntimeRequest: null, + latestVisibleMessage: null, + latestUserMessageAt: NOW, + hasActionableProposedPlan: false, + itemCount: 0, + visibleItemCount: 0, + createdAt: NOW, + updatedAt: NOW, + archivedAt: null, + settledOverride: null, + settledAt: NOW, + deletedAt: null, + }, + ], + archivedThreads: [], +}; +const THREAD_SNAPSHOT: OrchestrationV2ThreadDetailSnapshot = { + snapshotSequence: 4, + projection: { + thread: { + id: THREAD_ID, + projectId: PROJECT_ID, + title: "Thread", + providerInstanceId: PROVIDER_INSTANCE_ID, + modelSelection: { + instanceId: PROVIDER_INSTANCE_ID, + model: "gpt-5.4", + }, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { + rootThreadId: THREAD_ID, + parentThreadId: null, + relationshipToParent: null, + }, + forkedFrom: null, + createdBy: "user", + creationSource: "mobile", + createdAt: NOW, + updatedAt: NOW, + archivedAt: null, + settledOverride: null, + settledAt: NOW, + deletedAt: null, + }, + runs: [], + attempts: [], + nodes: [], + subagents: [], + providerSessions: [], + providerThreads: [], + providerTurns: [], + runtimeRequests: [], + messages: [], + plans: [], + turnItems: [], + checkpointScopes: [], + checkpoints: [], + contextHandoffs: [], + contextTransfers: [], + visibleTurnItems: [], + updatedAt: NOW, + }, +}; const REFS: VcsListRefsResult = { refs: [ { @@ -28,13 +150,16 @@ function cacheId(environmentId: EnvironmentId, kind: ClientCacheKind, cacheKey: function makeDatabase() { const values = new Map(); + const schemaVersions = new Map(); const removed: Array = []; const database = MobileDatabase.of({ loadCache: (environmentId, kind, cacheKey) => Effect.succeed(Option.fromUndefinedOr(values.get(cacheId(environmentId, kind, cacheKey)))), - saveCache: (environmentId, kind, cacheKey, _schemaVersion, payload) => + saveCache: (environmentId, kind, cacheKey, schemaVersion, payload) => Effect.sync(() => { - values.set(cacheId(environmentId, kind, cacheKey), payload); + const id = cacheId(environmentId, kind, cacheKey); + values.set(id, payload); + schemaVersions.set(id, schemaVersion); }), removeCache: (environmentId, kind, cacheKey) => Effect.sync(() => { @@ -59,10 +184,37 @@ function makeDatabase() { loadPreferencesJson: Effect.succeed(Option.none()), savePreferencesJson: () => Effect.void, }); - return { database, removed, values }; + return { database, removed, schemaVersions, values }; } describe("mobile SQLite environment cache store", () => { + it.effect("round-trips V2 shell and thread DateTime fields with the shared cache schema", () => + Effect.gen(function* () { + const memory = makeDatabase(); + const store = yield* make().pipe(Effect.provideService(MobileDatabase, memory.database)); + + yield* store.saveShell(ENVIRONMENT_ID, SHELL_SNAPSHOT); + yield* store.saveThread(ENVIRONMENT_ID, THREAD_SNAPSHOT); + + const shell = Option.getOrThrow(yield* store.loadShell(ENVIRONMENT_ID)); + const thread = Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID)); + + expect(DateTime.formatIso(shell.threads[0]!.updatedAt)).toBe("2026-07-29T12:00:00.000Z"); + expect(DateTime.formatIso(shell.threads[0]!.latestUserMessageAt!)).toBe( + "2026-07-29T12:00:00.000Z", + ); + expect(DateTime.formatIso(thread.projection.thread.updatedAt)).toBe( + "2026-07-29T12:00:00.000Z", + ); + expect(memory.schemaVersions.get(cacheId(ENVIRONMENT_ID, "shell", "snapshot"))).toBe( + ORCHESTRATION_CACHE_SCHEMA_VERSION, + ); + expect(memory.schemaVersions.get(cacheId(ENVIRONMENT_ID, "thread", THREAD_ID))).toBe( + ORCHESTRATION_CACHE_SCHEMA_VERSION, + ); + }), + ); + it.effect("round-trips schema-validated VCS refs", () => Effect.gen(function* () { const memory = makeDatabase(); diff --git a/apps/mobile/src/connection/environment-cache-store.ts b/apps/mobile/src/connection/environment-cache-store.ts index 21f08ffe26..1942cb7cc3 100644 --- a/apps/mobile/src/connection/environment-cache-store.ts +++ b/apps/mobile/src/connection/environment-cache-store.ts @@ -1,14 +1,11 @@ import { ConnectionPersistenceError, EnvironmentCacheStore, + ORCHESTRATION_CACHE_SCHEMA_VERSION, + StoredOrchestrationShellSnapshot, + StoredOrchestrationThreadSnapshot, } from "@t3tools/client-runtime/platform"; -import { - type EnvironmentId, - OrchestrationV2ShellSnapshot, - OrchestrationV2ThreadDetailSnapshot, - ServerConfig, - VcsListRefsResult, -} from "@t3tools/contracts"; +import { type EnvironmentId, ServerConfig, VcsListRefsResult } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; @@ -16,22 +13,9 @@ import * as Schema from "effect/Schema"; import * as MobileDatabase from "../persistence/mobile-database"; -const SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION = 1; -const THREAD_SNAPSHOT_CACHE_SCHEMA_VERSION = 2; const SERVER_CONFIG_CACHE_SCHEMA_VERSION = 1; const VCS_REFS_CACHE_SCHEMA_VERSION = 1; -const StoredShellSnapshot = Schema.Struct({ - schemaVersion: Schema.Literal(SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION), - environmentId: Schema.String, - snapshot: OrchestrationV2ShellSnapshot, -}); -const StoredThreadSnapshot = Schema.Struct({ - schemaVersion: Schema.Literal(THREAD_SNAPSHOT_CACHE_SCHEMA_VERSION), - environmentId: Schema.String, - threadId: Schema.String, - snapshot: OrchestrationV2ThreadDetailSnapshot, -}); const StoredServerConfig = Schema.Struct({ schemaVersion: Schema.Literal(SERVER_CONFIG_CACHE_SCHEMA_VERSION), environmentId: Schema.String, @@ -45,13 +29,17 @@ const StoredVcsRefs = Schema.Struct({ }); const decodeStoredShellSnapshot = Schema.decodeUnknownEffect( - Schema.fromJsonString(StoredShellSnapshot), + Schema.fromJsonString(StoredOrchestrationShellSnapshot), +); +const encodeStoredShellSnapshot = Schema.encodeEffect( + Schema.fromJsonString(StoredOrchestrationShellSnapshot), ); -const encodeStoredShellSnapshot = Schema.encodeEffect(Schema.fromJsonString(StoredShellSnapshot)); const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect( - Schema.fromJsonString(StoredThreadSnapshot), + Schema.fromJsonString(StoredOrchestrationThreadSnapshot), +); +const encodeStoredThreadSnapshot = Schema.encodeEffect( + Schema.fromJsonString(StoredOrchestrationThreadSnapshot), ); -const encodeStoredThreadSnapshot = Schema.encodeEffect(Schema.fromJsonString(StoredThreadSnapshot)); const decodeStoredServerConfig = Schema.decodeUnknownEffect( Schema.fromJsonString(StoredServerConfig), ); @@ -127,12 +115,12 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () { ), saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) { const payload = yield* encodeStoredShellSnapshot({ - schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION, + schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION, environmentId, snapshot, }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause))); yield* database - .saveCache(environmentId, "shell", "snapshot", SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION, payload) + .saveCache(environmentId, "shell", "snapshot", ORCHESTRATION_CACHE_SCHEMA_VERSION, payload) .pipe(Effect.mapError(mapDatabaseError("save-shell"))); }), loadThread: Effect.fn("MobileEnvironmentCache.loadThread")((environmentId, threadId) => @@ -152,13 +140,13 @@ 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: THREAD_SNAPSHOT_CACHE_SCHEMA_VERSION, + schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION, environmentId, threadId, snapshot, }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause))); yield* database - .saveCache(environmentId, "thread", threadId, THREAD_SNAPSHOT_CACHE_SCHEMA_VERSION, payload) + .saveCache(environmentId, "thread", threadId, ORCHESTRATION_CACHE_SCHEMA_VERSION, payload) .pipe(Effect.mapError(mapDatabaseError("save-thread"))); }), removeThread: Effect.fn("MobileEnvironmentCache.removeThread")((environmentId, threadId) => diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.test.ts new file mode 100644 index 0000000000..0e819bcfb9 --- /dev/null +++ b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.test.ts @@ -0,0 +1,39 @@ +import { assert, describe, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; + +import { OPENCODE_PROVIDER } from "./OpenCodeAdapterV2.ts"; +import { + OPENCODE_SDK_REPLAY_PROTOCOL, + OpenCodeReplayController, +} from "./OpenCodeAdapterV2.testkit.ts"; + +describe("OpenCodeAdapterV2 replay testkit", () => { + it.effect("stops an event stream when abort races with listener registration", () => + Effect.gen(function* () { + let aborted = false; + const signal = { + get aborted() { + return aborted; + }, + addEventListener: () => { + aborted = true; + }, + removeEventListener: () => {}, + } as unknown as AbortSignal; + const controller = new OpenCodeReplayController({ + provider: OPENCODE_PROVIDER, + protocol: OPENCODE_SDK_REPLAY_PROTOCOL, + version: "test", + scenario: "abort-during-listener-registration", + entries: [], + }); + const iterator = controller.events(signal)[Symbol.asyncIterator](); + + const result = yield* Effect.promise(() => iterator.next()).pipe( + Effect.timeout("100 millis"), + ); + + assert.isTrue(result.done); + }), + ); +}); diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.ts index 14cd39d5a4..a5fa86ccba 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.testkit.ts @@ -120,7 +120,7 @@ function frameRecord(frame: unknown): Record | null { return typeof frame === "object" && frame !== null ? (frame as Record) : null; } -class OpenCodeReplayController { +export class OpenCodeReplayController { private cursor = 0; private readonly waiters = new Set<() => void>(); private failure: unknown = null; @@ -243,15 +243,18 @@ class OpenCodeReplayController { } private changed(signal?: AbortSignal): Promise { - if (signal?.aborted === true) return Promise.resolve(); return new Promise((resolve) => { + let settled = false; const done = () => { + if (settled) return; + settled = true; signal?.removeEventListener("abort", done); this.waiters.delete(done); resolve(); }; this.waiters.add(done); signal?.addEventListener("abort", done, { once: true }); + if (signal?.aborted === true) done(); }); } } diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/claude_local_bash_task/output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/claude_local_bash_task/output.ts index c51a428148..b705ea3610 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/claude_local_bash_task/output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/claude_local_bash_task/output.ts @@ -17,8 +17,8 @@ export function assertClaudeLocalBashTaskOutput( ) { assertBaseProjection({ result, transcript, runCount: 1, runStatuses: ["completed"] }); - assert.lengthOf( - result.projections, + assert.equal( + result.projections.size, 1, "Claude local_bash task lifecycle events must not create child app threads", ); diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/todo_list/grok_output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/todo_list/grok_output.ts index d9e778e28d..3c18c1fa24 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/todo_list/grok_output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/todo_list/grok_output.ts @@ -35,8 +35,9 @@ export function assertTodoListGrokOutput( const planEvents = result.domainEvents.filter((event) => event.type === "plan.updated"); assert.lengthOf(planEvents, 2); - assert.lengthOf( - new Set(planEvents.map((event) => (event.type === "plan.updated" ? event.payload.id : null))), + assert.equal( + new Set(planEvents.map((event) => (event.type === "plan.updated" ? event.payload.id : null))) + .size, 1, "ACP plan updates must preserve one plan identity", );