fix(orchestrator): handle fresh review edge cases

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-29 03:44:40 +02:00
1 parent dbc836d591
commit 774cacfce5
6 files changed
+119 -11

No files matched your search

@@ -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");
});
@@ -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";
}
@@ -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));
});
@@ -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
@@ -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: {
}
/>
<TooltipPopup side="left">
{latestCompletedRun === null
{latestMergeBackRun === null
? "Complete a run in this fork before merging it back"
: parentTitle
? `Merge this conversation back into ${parentTitle}`
@@ -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) => {