From 774cacfce574a47edd2dedc5b2c28ec84b0fefd0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 03:44:40 +0200 Subject: [PATCH] fix(orchestrator): handle fresh review edge cases Co-authored-by: codex --- .../Adapters/OpenCodeAdapterV2.test.ts | 3 + .../Adapters/OpenCodeAdapterV2.ts | 15 ++-- .../RuntimeRequestService.test.ts | 79 +++++++++++++++++++ .../orchestration-v2/RuntimeRequestService.ts | 7 ++ .../chat/ThreadRelationshipsControl.tsx | 15 ++-- .../src/state/threadWorkflows.test.ts | 11 +++ 6 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 apps/server/src/orchestration-v2/RuntimeRequestService.test.ts diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.test.ts index bac5c53780..a6d0d327dc 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.test.ts @@ -119,6 +119,8 @@ describe("OpenCodeAdapterV2", () => { assert.equal(openCodePermissionRequestKind("external_directory", "edit"), "file-change"); assert.equal(openCodePermissionRequestKind("edit"), "file-change"); assert.equal(openCodePermissionRequestKind("apply_patch"), "file-change"); + assert.equal(openCodePermissionRequestKind("todowrite"), "command"); + assert.equal(openCodePermissionRequestKind("custom", "todowrite"), "command"); }); it("maps OpenCode tools to semantic turn-item families", () => { @@ -128,6 +130,7 @@ describe("OpenCodeAdapterV2", () => { assert.equal(openCodeToolProjectionKind("lsp"), "file_search"); assert.equal(openCodeToolProjectionKind("websearch"), "web_search"); assert.equal(openCodeToolProjectionKind("codesearch"), "web_search"); + assert.equal(openCodeToolProjectionKind("todowrite"), "dynamic_tool"); assert.equal(openCodeToolProjectionKind("custom_tool"), "dynamic_tool"); }); diff --git a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts index 59ca6efb35..e0834bec85 100644 --- a/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/OpenCodeAdapterV2.ts @@ -425,11 +425,13 @@ export function openCodePermissionRequestKind( const normalizedTool = toolName?.toLowerCase() ?? ""; if ( normalized === "edit" || - normalized.includes("write") || - normalized.includes("patch") || - normalizedTool.includes("edit") || - normalizedTool.includes("write") || - normalizedTool.includes("patch") + normalized === "write" || + normalized === "patch" || + normalized === "apply_patch" || + normalizedTool === "edit" || + normalizedTool === "write" || + normalizedTool === "patch" || + normalizedTool === "apply_patch" ) { return "file-change"; } @@ -453,6 +455,9 @@ export function openCodeToolProjectionKind( toolName: string, ): "command_execution" | "file_change" | "file_search" | "web_search" | "dynamic_tool" { const normalized = toolName.toLowerCase(); + if (normalized === "todowrite") { + return "dynamic_tool"; + } if (normalized.includes("bash") || normalized.includes("shell")) { return "command_execution"; } diff --git a/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts b/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts new file mode 100644 index 0000000000..df8096bbcb --- /dev/null +++ b/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts @@ -0,0 +1,79 @@ +import { assert, it, vi } from "@effect/vitest"; +import { + NodeId, + ProviderSessionId, + RuntimeRequestId, + ThreadId, + type OrchestrationV2ThreadProjection, +} from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; + +import { ProjectionStoreV2 } from "./ProjectionStore.ts"; +import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts"; +import { + layer as runtimeRequestServiceLayer, + RuntimeRequestServiceV2, +} from "./RuntimeRequestService.ts"; + +it.effect("rejects resolved runtime requests before invoking the live adapter", () => { + const threadId = ThreadId.make("thread-runtime-request-resolved"); + const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-resolved"); + const requestId = RuntimeRequestId.make("request-resolved"); + const respondToRuntimeRequest = vi.fn(() => Effect.void); + const getSession = vi.fn(() => + Effect.succeed( + Option.some({ + respondToRuntimeRequest, + } as never), + ), + ); + const projection = { + runtimeRequests: [ + { + id: requestId, + nodeId: NodeId.make("node-runtime-request-resolved"), + providerTurnId: null, + nativeRequestRef: null, + kind: "command", + status: "resolved", + responseCapability: { + type: "live", + providerSessionId, + }, + createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"), + resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"), + }, + ], + } as unknown as OrchestrationV2ThreadProjection; + const testLayer = runtimeRequestServiceLayer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.mock(ProjectionStoreV2)({ + getThreadProjection: () => Effect.succeed(projection), + }), + Layer.mock(ProviderSessionManagerV2)({ + get: getSession, + }), + ), + ), + ); + + return Effect.gen(function* () { + const service = yield* RuntimeRequestServiceV2; + const error = yield* service + .respond({ + threadId, + providerSessionId, + requestId, + decision: "accept", + }) + .pipe(Effect.flip); + + assert.equal(error.cause, "The runtime request is no longer pending."); + assert.equal(getSession.mock.calls.length, 0); + assert.equal(respondToRuntimeRequest.mock.calls.length, 0); + }).pipe(Effect.provide(testLayer)); +}); diff --git a/apps/server/src/orchestration-v2/RuntimeRequestService.ts b/apps/server/src/orchestration-v2/RuntimeRequestService.ts index 00bcb47d3d..e92fb7a332 100644 --- a/apps/server/src/orchestration-v2/RuntimeRequestService.ts +++ b/apps/server/src/orchestration-v2/RuntimeRequestService.ts @@ -62,6 +62,13 @@ export const layer: Layer.Layer< cause: "The runtime request no longer exists.", }); } + if (request.status !== "pending") { + return yield* new RuntimeRequestResponseExecutionError({ + threadId: input.threadId, + requestId: input.requestId, + cause: "The runtime request is no longer pending.", + }); + } if ( request.responseCapability.type !== "live" || request.responseCapability.providerSessionId !== input.providerSessionId diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx index 2685280112..7427d06f4f 100644 --- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx +++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx @@ -5,7 +5,10 @@ import { resolveMergeBackTargetThreadId, type ThreadRelationshipEdge, } from "@t3tools/client-runtime/state/thread-relationships"; -import { canDetachThreadProviderSession } from "@t3tools/client-runtime/state/thread-workflows"; +import { + canDetachThreadProviderSession, + resolveLatestMergeBackRun, +} from "@t3tools/client-runtime/state/thread-workflows"; import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts"; import { useNavigate } from "@tanstack/react-router"; import { @@ -102,7 +105,7 @@ export function ThreadRelationshipsPanel(props: { const mergeBack = useAtomCommand(threadEnvironment.mergeBack); const stopSession = useAtomCommand(threadEnvironment.stopSession); const [busyAction, setBusyAction] = useState<"merge" | "detach" | null>(null); - const latestCompletedRun = projection?.runs.findLast((run) => run.status === "completed") ?? null; + const latestMergeBackRun = projection === null ? null : resolveLatestMergeBackRun(projection); const mergeTargetThreadId = resolveMergeBackTargetThreadId(projection); const relationshipRows = immediateThreadRelationships(graph, props.threadId).toSorted( (left, right) => @@ -119,7 +122,7 @@ export function ThreadRelationshipsPanel(props: { mergeTargetThreadId, }), ); - const canMerge = mergeTargetThreadId !== null && latestCompletedRun !== null; + const canMerge = mergeTargetThreadId !== null && latestMergeBackRun !== null; const canDetach = projection ? canDetachThreadProviderSession(projection) : false; if (relationshipRows.length === 0) { @@ -134,14 +137,14 @@ export function ThreadRelationshipsPanel(props: { }; const merge = async () => { - if (!latestCompletedRun || mergeTargetThreadId === null || busyAction !== null) return; + if (!latestMergeBackRun || mergeTargetThreadId === null || busyAction !== null) return; setBusyAction("merge"); const result = await mergeBack({ environmentId: props.environmentId, input: { sourceThreadId: props.threadId, targetThreadId: mergeTargetThreadId, - runId: latestCompletedRun.id, + runId: latestMergeBackRun.id, }, }); setBusyAction(null); @@ -292,7 +295,7 @@ export function ThreadRelationshipsPanel(props: { } /> - {latestCompletedRun === null + {latestMergeBackRun === null ? "Complete a run in this fork before merging it back" : parentTitle ? `Merge this conversation back into ${parentTitle}` diff --git a/packages/client-runtime/src/state/threadWorkflows.test.ts b/packages/client-runtime/src/state/threadWorkflows.test.ts index 71d5ab75bf..1e0d806ca1 100644 --- a/packages/client-runtime/src/state/threadWorkflows.test.ts +++ b/packages/client-runtime/src/state/threadWorkflows.test.ts @@ -234,6 +234,17 @@ describe("thread workflows", () => { expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished"); }); + it("does not let a stale completed run later in storage order hide the waiting checkpoint", () => { + const projection = { + runs: [ + { id: "newest-finished", status: "waiting", ordinal: 2 }, + { id: "older-completed", status: "completed", ordinal: 1 }, + ], + } as never; + + expect(resolveLatestMergeBackRun(projection)?.id).toBe("newest-finished"); + }); + it.each(["preparing", "starting", "running"] as const)( "does not merge older history while a newer run is %s", (status) => {