diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.test.ts index 57a2524134..79617036c0 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.test.ts @@ -8,6 +8,7 @@ import { cursorRuntimeAgentPolicy, cursorSdkModelSelection, makeCursorAgentOptions, + nestedToolCallFromEnvelope, } from "./CursorAdapterV2.ts"; import { isCursorCancellationError, loggedCursorAgentOptions } from "./CursorAgentSdk.ts"; @@ -168,4 +169,27 @@ describe("CursorAdapterV2", () => { assert.isFalse(isCursorCancellationError(new Error("request failed"))); assert.isFalse(isCursorCancellationError(null)); }); + + it("preserves failed nested read calls when Cursor omits their path", () => { + assert.deepEqual( + nestedToolCallFromEnvelope({ + toolCallId: "tool:failed-read", + readToolCall: { + args: {}, + result: { error: "File path was not provided." }, + }, + }), + { + callId: "tool:failed-read", + toolCall: { + type: "read", + args: { path: "" }, + result: { + status: "error", + error: "File path was not provided.", + }, + }, + }, + ); + }); }); diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts index 30b7a36c4d..b60b559122 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts @@ -573,7 +573,7 @@ function nestedGrepWorkspaceResults(success: Record): Record, ): { readonly callId: string; readonly toolCall: ToolCall } | undefined { const callId = typeof envelope.toolCallId === "string" ? envelope.toolCallId : undefined; @@ -598,10 +598,7 @@ function nestedToolCallFromEnvelope( ? args.path : typeof success?.path === "string" ? success.path - : undefined; - if (path === undefined) { - return undefined; - } + : ""; return { callId: nestedCallId, toolCall: { diff --git a/apps/server/src/persistence/Migrations/034_035_OrchestrationV2.test.ts b/apps/server/src/persistence/Migrations/034_035_OrchestrationV2.test.ts index 4eea2904ea..35dc01d903 100644 --- a/apps/server/src/persistence/Migrations/034_035_OrchestrationV2.test.ts +++ b/apps/server/src/persistence/Migrations/034_035_OrchestrationV2.test.ts @@ -105,6 +105,66 @@ layer("035_036_OrchestrationV2", (it) => { ]); }), ); + + it.effect("preserves turn items with colliding ordinals in migration 037", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* runMigrations({ toMigrationInclusive: 36 }); + yield* sql` + INSERT INTO orchestration_v2_projection_runs ( + run_id, + thread_id, + ordinal, + provider, + status, + requested_at, + payload_json + ) VALUES ( + 'run:one', + 'thread:one', + 1, + 'codex', + 'completed', + '2026-01-01T00:00:00.000Z', + '{}' + ) + `; + yield* sql` + INSERT INTO orchestration_v2_projection_turn_items ( + turn_item_id, + thread_id, + run_id, + ordinal, + type, + status, + updated_at, + payload_json + ) VALUES + ('turn-item:b', 'thread:one', 'run:one', 7, 'assistant_message', 'completed', '2026-01-01T00:00:00.000Z', '{}'), + ('turn-item:a', 'thread:one', 'run:one', 7, 'assistant_message', 'completed', '2026-01-01T00:00:00.000Z', '{}'), + ('turn-item:c', 'thread:one', 'run:one', 9, 'assistant_message', 'completed', '2026-01-01T00:00:00.000Z', '{}'), + ('turn-item:d', 'thread:two', NULL, 42, 'assistant_message', 'completed', '2026-01-01T00:00:00.000Z', '{}') + `; + + yield* runMigrations({ toMigrationInclusive: 37 }); + + const positions = yield* sql<{ + readonly thread_id: string; + readonly turn_item_id: string; + readonly ordinal: number; + }>` + SELECT thread_id, turn_item_id, ordinal + FROM orchestration_v2_turn_item_positions + ORDER BY thread_id, ordinal + `; + assert.deepStrictEqual(positions, [ + { thread_id: "thread:one", turn_item_id: "turn-item:a", ordinal: 1_000_001 }, + { thread_id: "thread:one", turn_item_id: "turn-item:b", ordinal: 1_000_002 }, + { thread_id: "thread:one", turn_item_id: "turn-item:c", ordinal: 1_000_003 }, + { thread_id: "thread:two", turn_item_id: "turn-item:d", ordinal: 1 }, + ]); + }).pipe(Effect.provide(NodeSqliteClient.layerMemory())), + ); }); it.effect("upgrades a database already at released main migration 034", () => diff --git a/apps/server/src/persistence/Migrations/036_OrchestrationV2Foundation.ts b/apps/server/src/persistence/Migrations/036_OrchestrationV2Foundation.ts index c11dce2a71..e05c691e9c 100644 --- a/apps/server/src/persistence/Migrations/036_OrchestrationV2Foundation.ts +++ b/apps/server/src/persistence/Migrations/036_OrchestrationV2Foundation.ts @@ -137,14 +137,22 @@ export default Effect.gen(function* () { ) `; yield* sql` - INSERT OR IGNORE INTO orchestration_v2_turn_item_positions ( + INSERT INTO orchestration_v2_turn_item_positions ( thread_id, turn_item_id, ordinal ) - SELECT thread_id, turn_item_id, ordinal - FROM orchestration_v2_projection_turn_items - ORDER BY thread_id, ordinal, turn_item_id + SELECT + item.thread_id, + item.turn_item_id, + COALESCE(run.ordinal, 0) * 1000000 + ROW_NUMBER() OVER ( + PARTITION BY item.thread_id, COALESCE(run.ordinal, 0) + ORDER BY item.ordinal, item.turn_item_id + ) + FROM orchestration_v2_projection_turn_items AS item + LEFT JOIN orchestration_v2_projection_runs AS run + ON run.thread_id = item.thread_id + AND run.run_id = item.run_id `; yield* sql`