From f03337a438891b30439b11da2de553d2bf478d43 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 05:06:49 +0200 Subject: [PATCH] fix(orchestrator): validate replay edge cases Co-authored-by: codex --- .../Adapters/CodexAdapterV2.test.ts | 31 +++++++++ .../Adapters/CodexAdapterV2.ts | 2 +- .../CheckpointRollbackService.test.ts | 69 +++++++++++++++++++ .../CheckpointRollbackService.ts | 3 +- .../037_OrchestrationV2Foundation.test.ts | 64 +++++++++++++++++ .../037_OrchestrationV2Foundation.ts | 2 +- 6 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts create mode 100644 apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index f4b8519979..00021d8e04 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -229,6 +229,37 @@ describe("CodexAdapterV2 assistant message streaming", () => { ]); }), ); + + it.effect("treats explicit empty final text as authoritative over buffered deltas", () => + Effect.gen(function* () { + const updates = yield* Ref.make>([]); + const coalescer = yield* makeCodexAgentMessageDeltaCoalescer({ + flushIntervalMs: 50, + emit: (update) => Ref.update(updates, (current) => [...current, update]), + }); + + yield* coalescer.append({ + turnId: "turn-1", + itemId: "message-1", + delta: "stale buffered text", + }); + const completedText = yield* coalescer.complete({ + turnId: "turn-1", + itemId: "message-1", + finalText: "", + }); + + assert.equal(completedText, ""); + assert.deepEqual(yield* Ref.get(updates), [ + { + turnId: "turn-1", + itemId: "message-1", + text: "", + completed: true, + }, + ]); + }), + ); }); describe("CodexAdapterV2 runtime policy", () => { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 0ffd423211..bb735cc074 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1022,7 +1022,7 @@ export const makeCodexAgentMessageDeltaCoalescer = Effect.fn( Effect.gen(function* () { const key = codexAgentMessageBufferKey(turnId, itemId); const existing = (yield* Ref.get(buffered)).get(key); - const text = finalText && finalText.length > 0 ? finalText : (existing?.text ?? ""); + const text = finalText !== undefined ? finalText : (existing?.text ?? ""); if (emitEmpty || text.length > 0) { yield* input.emit({ turnId, itemId, text, completed: true }); } diff --git a/apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts b/apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts new file mode 100644 index 0000000000..20254e0b4c --- /dev/null +++ b/apps/server/src/orchestration-v2/CheckpointRollbackService.test.ts @@ -0,0 +1,69 @@ +import { assert, it, vi } from "@effect/vitest"; +import { + CheckpointId, + CheckpointScopeId, + type OrchestrationV2ThreadProjection, + ProviderSessionId, + ProviderThreadId, + ThreadId, +} from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; + +import { CheckpointServiceV2 } from "./CheckpointService.ts"; +import { + CheckpointRollbackServiceV2, + layer as checkpointRollbackServiceLayer, +} from "./CheckpointRollbackService.ts"; +import { EventSinkV2 } from "./EventSink.ts"; +import { layer as idAllocatorLayer } from "./IdAllocator.ts"; +import { ProjectionStoreV2 } from "./ProjectionStore.ts"; +import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts"; +import { RuntimePolicyV2 } from "./RuntimePolicy.ts"; + +it.effect("rejects a non-ready checkpoint before opening a session or restoring files", () => { + const threadId = ThreadId.make("thread:rollback-non-ready"); + const providerThreadId = ProviderThreadId.make("provider-thread:rollback-non-ready"); + const providerSessionId = ProviderSessionId.make("provider-session:rollback-non-ready"); + const checkpointId = CheckpointId.make("checkpoint:rollback-non-ready"); + const scopeId = CheckpointScopeId.make("checkpoint-scope:rollback-non-ready"); + const restore = vi.fn(() => Effect.die("checkpoint restore must not run")); + const open = vi.fn(() => Effect.die("provider session open must not run")); + const resolveRuntimePolicy = vi.fn(() => Effect.die("runtime policy resolution must not run")); + const projection = { + providerThreads: [{ id: providerThreadId, providerSessionId }], + checkpoints: [{ id: checkpointId, scopeId, status: "stale" }], + checkpointScopes: [{ id: scopeId }], + } as unknown as OrchestrationV2ThreadProjection; + const testLayer = checkpointRollbackServiceLayer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.mock(CheckpointServiceV2)({ restore }), + Layer.mock(EventSinkV2)({}), + idAllocatorLayer, + Layer.mock(ProjectionStoreV2)({ + getThreadProjection: () => Effect.succeed(projection), + }), + Layer.mock(ProviderSessionManagerV2)({ open }), + Layer.mock(RuntimePolicyV2)({ resolve: resolveRuntimePolicy }), + ), + ), + ); + + return Effect.gen(function* () { + const service = yield* CheckpointRollbackServiceV2; + const error = yield* service + .execute({ + threadId, + providerThreadId, + checkpointId, + scopeId, + }) + .pipe(Effect.flip); + + assert.equal(error.cause, "The persisted rollback target is incomplete or no longer valid."); + assert.equal(resolveRuntimePolicy.mock.calls.length, 0); + assert.equal(open.mock.calls.length, 0); + assert.equal(restore.mock.calls.length, 0); + }).pipe(Effect.provide(testLayer)); +}); diff --git a/apps/server/src/orchestration-v2/CheckpointRollbackService.ts b/apps/server/src/orchestration-v2/CheckpointRollbackService.ts index f8732c22ae..ecd8fb9a92 100644 --- a/apps/server/src/orchestration-v2/CheckpointRollbackService.ts +++ b/apps/server/src/orchestration-v2/CheckpointRollbackService.ts @@ -81,7 +81,8 @@ export const layer: Layer.Layer< providerThread.providerSessionId === null || checkpoint === undefined || scope === undefined || - checkpoint.scopeId !== scope.id + checkpoint.scopeId !== scope.id || + checkpoint.status !== "ready" ) { return yield* new CheckpointRollbackExecutionError({ threadId: input.threadId, diff --git a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts b/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts new file mode 100644 index 0000000000..dca30a2f08 --- /dev/null +++ b/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.test.ts @@ -0,0 +1,64 @@ +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/unstable/sql/SqlClient"; + +import { runMigrations } from "../Migrations.ts"; +import * as NodeSqliteClient from "../NodeSqliteClient.ts"; + +it.effect("backfills the V2 event driver from payload before the legacy provider column", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations({ toMigrationInclusive: 36 }); + + yield* sql` + INSERT INTO orchestration_v2_events ( + event_id, + thread_id, + provider, + event_type, + occurred_at, + payload_json + ) VALUES + ( + 'event:explicit-driver', + 'thread:explicit-driver', + 'codex-work', + 'thread.created', + '2026-07-29T00:00:00.000Z', + '{"driver":"codex","providerInstanceId":"codex-work"}' + ), + ( + 'event:legacy-provider', + 'thread:legacy-provider', + 'claude-agent', + 'thread.created', + '2026-07-29T00:00:00.000Z', + '{}' + ) + `; + + yield* runMigrations({ toMigrationInclusive: 37 }); + + const events = yield* sql<{ + readonly event_id: string; + readonly driver: string; + readonly provider_instance_id: string; + }>` + SELECT event_id, driver, provider_instance_id + FROM orchestration_v2_events + ORDER BY event_id + `; + assert.deepStrictEqual(events, [ + { + event_id: "event:explicit-driver", + driver: "codex", + provider_instance_id: "codex-work", + }, + { + event_id: "event:legacy-provider", + driver: "claude-agent", + provider_instance_id: "claude-agent", + }, + ]); + }).pipe(Effect.provide(NodeSqliteClient.layerMemory())), +); diff --git a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts b/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts index 8c333707af..c11dce2a71 100644 --- a/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts +++ b/apps/server/src/persistence/Migrations/037_OrchestrationV2Foundation.ts @@ -18,7 +18,7 @@ export default Effect.gen(function* () { yield* sql` UPDATE orchestration_v2_events SET - driver = provider, + driver = COALESCE(json_extract(payload_json, '$.driver'), provider), provider_instance_id = COALESCE( json_extract(payload_json, '$.providerInstanceId'), provider