fix(server): a message sent during a rollback no longer undoes it (#17079)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-07 22:20:16 -07:00
1 parent 0bfd9dd19d
commit c28cd90557
5 files changed
+95 -61

No files matched your search

@@ -17,6 +17,7 @@ import * as Option from "effect/Option";
import * as Path from "effect/Path";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
import * as CheckpointRollbackService from "./CheckpointRollbackService.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import { CheckpointServiceV2 } from "./CheckpointService.ts";
import { EventSinkV2 } from "./EventSink.ts";
import * as IdAllocator from "./IdAllocator.ts";
@@ -102,6 +103,7 @@ it.effect.each([
Layer.provide(
Layer.mergeAll(
NodeServices.layer,
ThreadCommandExecutor.layer,
IdAllocator.layer,
Layer.mock(ProjectStore.ProjectStoreV2)({
get: () => Effect.succeed(Option.some({ workspaceRoot: parent } as never)),
@@ -20,6 +20,8 @@ import { resolveCodexRollbackTurnCount } from "./Adapters/CodexAdapterV2.ts";
import { isCheckpointRestoreIsolated } from "./CheckpointRestoreSafety.ts";
import * as CheckpointService from "./CheckpointService.ts";
import * as CheckpointRollbackService from "./CheckpointRollbackService.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import * as KeyedLock from "@t3tools/shared/KeyedLock";
import * as EventSink from "./EventSink.ts";
import * as IdAllocator from "./IdAllocator.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
@@ -64,6 +66,7 @@ it.effect("rejects a non-ready checkpoint before opening a session or restoring
const layerTest = layerCheckpointRollbackService.pipe(
Layer.provide(
Layer.mergeAll(
ThreadCommandExecutor.layer,
Layer.mock(CheckpointService.CheckpointServiceV2)({ restore }),
Layer.mock(EventSink.EventSinkV2)({}),
IdAllocator.layer,
@@ -141,6 +144,7 @@ it.effect("rejects a rollback when another provider thread became active", () =>
const layerTest = layerCheckpointRollbackService.pipe(
Layer.provide(
Layer.mergeAll(
ThreadCommandExecutor.layer,
Layer.mock(CheckpointService.CheckpointServiceV2)({ restore }),
Layer.mock(EventSink.EventSinkV2)({}),
IdAllocator.layer,
@@ -221,6 +225,7 @@ it.effect("rejects a rollback when provider selection changed before execution",
const layerTest = layerCheckpointRollbackService.pipe(
Layer.provide(
Layer.mergeAll(
ThreadCommandExecutor.layer,
Layer.mock(CheckpointService.CheckpointServiceV2)({ restore }),
Layer.mock(EventSink.EventSinkV2)({}),
IdAllocator.layer,
@@ -292,6 +297,7 @@ it.effect("reports a missing provider turn as a structured rollback failure", ()
const layerTest = layerCheckpointRollbackService.pipe(
Layer.provide(
Layer.mergeAll(
ThreadCommandExecutor.layer,
Layer.mock(CheckpointService.CheckpointServiceV2)({ restore }),
Layer.mock(EventSink.EventSinkV2)({}),
IdAllocator.layer,
@@ -352,6 +358,15 @@ it.effect.each([
const checkpointId = CheckpointId.make("rewind-start");
const scopeId = CheckpointScopeId.make("rewind-scope");
const calls: string[] = [];
let lockedThreads = Effect.succeed<ReadonlyArray<ThreadId>>([]);
const layerThreadCommands = Layer.effect(
ThreadCommandExecutor.ThreadCommandExecutor,
Effect.tap(KeyedLock.make<ThreadId>(), (lock) =>
Effect.sync(() => {
lockedThreads = lock.activeKeys;
}),
),
);
const providerThread = {
id: providerThreadId,
providerSessionId,
@@ -390,6 +405,7 @@ it.effect.each([
const layerTest = layerCheckpointRollbackService.pipe(
Layer.provide(
Layer.mergeAll(
layerThreadCommands,
Layer.mock(CheckpointService.CheckpointServiceV2)({
restore: () =>
Effect.sync(() => {
@@ -398,7 +414,10 @@ it.effect.each([
}),
Layer.mock(EventSink.EventSinkV2)({
write: ({ events }) =>
Effect.sync(() => {
Effect.map(lockedThreads, (locked) => {
// A thread command that planned against the old provider thread
// must not commit after this and undo the rollback.
assert.include(locked, threadId);
assert.ok(
events.some(
(event) => event.type === "run.updated" && event.payload.status === "rolled_back",
@@ -26,6 +26,7 @@ import * as ProjectStore from "./ProjectStore.ts";
import type { ProviderAdapterV2RollbackTarget } from "./ProviderAdapter.ts";
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
import { RuntimePolicyV2 } from "./RuntimePolicy.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
export const ROLLBACK_FAILED_MESSAGE =
"The provider could not roll back this conversation. Try again; if it keeps failing, check the provider and server logs.";
@@ -91,6 +92,7 @@ export const layer: Layer.Layer<
| FileSystem.FileSystem
| Path.Path
| ProjectStore.ProjectStoreV2
| ThreadCommandExecutor.ThreadCommandExecutor
> = Layer.effect(
CheckpointRollbackServiceV2,
Effect.gen(function* () {
@@ -103,6 +105,7 @@ export const layer: Layer.Layer<
const fileSystem = yield* FileSystem.FileSystem;
const projects = yield* ProjectStore.ProjectStoreV2;
const path = yield* Path.Path;
const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor;
const execute = Effect.fn("orchestrationV2.checkpointRollback.execute")(function* (input: {
readonly threadId: ThreadId;
@@ -274,72 +277,80 @@ export const layer: Layer.Layer<
yield* checkpoints.deleteStaleRefs({ scope, checkpoints: staleCheckpoints });
}
const now = yield* DateTime.now;
const makeEvent = <Event extends OrchestrationV2DomainEvent>(event: Omit<Event, "id">) =>
Effect.map(
ids.allocate.event({ threadId: event.threadId }),
(id) =>
({
...event,
id,
}) as Event,
);
const events: Array<OrchestrationV2DomainEvent> = [];
events.push(
yield* makeEvent({
type: "provider-thread.updated",
threadId: input.threadId,
driver: providerThread.driver,
providerInstanceId: providerThread.providerInstanceId,
occurredAt: now,
payload: {
...snapshot.providerThread,
lastRunOrdinal: targetOrdinal === 0 ? null : targetOrdinal,
updatedAt: now,
},
}),
);
for (const staleCheckpoint of staleCheckpoints) {
events.push(
yield* makeEvent({
type: "checkpoint.captured",
threadId: input.threadId,
...(staleCheckpoint.runId === null ? {} : { runId: staleCheckpoint.runId }),
nodeId: staleCheckpoint.nodeId,
providerInstanceId: providerThread.providerInstanceId,
occurredAt: now,
payload: { ...staleCheckpoint, status: "stale" },
}),
);
}
for (const run of runsToRollback) {
const rootNode = projection.nodes.find((candidate) => candidate.id === run.rootNodeId);
events.push(
yield* makeEvent({
type: "run.updated",
threadId: input.threadId,
runId: run.id,
...(rootNode === undefined ? {} : { nodeId: rootNode.id }),
providerInstanceId: run.providerInstanceId,
occurredAt: now,
payload: { ...run, status: "rolled_back", completedAt: now },
}),
);
if (rootNode !== undefined) {
// Thread commands write full provider thread rows under this lock. Without
// it, a message sent during the rollback can plan against the old native
// session and commit after this, undoing the provider's fork.
yield* threadCommands.withLock(
input.threadId,
Effect.gen(function* () {
const now = yield* DateTime.now;
const makeEvent = <Event extends OrchestrationV2DomainEvent>(event: Omit<Event, "id">) =>
Effect.map(
ids.allocate.event({ threadId: event.threadId }),
(id) =>
({
...event,
id,
}) as Event,
);
const events: Array<OrchestrationV2DomainEvent> = [];
events.push(
yield* makeEvent({
type: "node.updated",
type: "provider-thread.updated",
threadId: input.threadId,
runId: run.id,
nodeId: rootNode.id,
providerInstanceId: run.providerInstanceId,
driver: providerThread.driver,
providerInstanceId: providerThread.providerInstanceId,
occurredAt: now,
payload: { ...rootNode, status: "rolled_back", completedAt: now },
payload: {
...snapshot.providerThread,
lastRunOrdinal: targetOrdinal === 0 ? null : targetOrdinal,
updatedAt: now,
},
}),
);
}
}
yield* eventSink.write({ events });
for (const staleCheckpoint of staleCheckpoints) {
events.push(
yield* makeEvent({
type: "checkpoint.captured",
threadId: input.threadId,
...(staleCheckpoint.runId === null ? {} : { runId: staleCheckpoint.runId }),
nodeId: staleCheckpoint.nodeId,
providerInstanceId: providerThread.providerInstanceId,
occurredAt: now,
payload: { ...staleCheckpoint, status: "stale" },
}),
);
}
for (const run of runsToRollback) {
const rootNode = projection.nodes.find((candidate) => candidate.id === run.rootNodeId);
events.push(
yield* makeEvent({
type: "run.updated",
threadId: input.threadId,
runId: run.id,
...(rootNode === undefined ? {} : { nodeId: rootNode.id }),
providerInstanceId: run.providerInstanceId,
occurredAt: now,
payload: { ...run, status: "rolled_back", completedAt: now },
}),
);
if (rootNode !== undefined) {
events.push(
yield* makeEvent({
type: "node.updated",
threadId: input.threadId,
runId: run.id,
nodeId: rootNode.id,
providerInstanceId: run.providerInstanceId,
occurredAt: now,
payload: { ...rootNode, status: "rolled_back", completedAt: now },
}),
);
}
}
yield* eventSink.write({ events });
}),
);
});
return CheckpointRollbackServiceV2.of({
@@ -182,6 +182,7 @@ const layerCheckpointRollbackServiceProvided = CheckpointRollbackService.layer.p
IdAllocator.layer,
ProjectionStore.layer,
layerProviderSessionManagerProvided,
ThreadCommandExecutor.layer,
layerRuntimePolicyProvided,
),
),
@@ -395,6 +395,7 @@ export function layerWithRegistry<Error>(
IdAllocator.layer,
layerStores,
layerProviderSessionManagerProvided,
ThreadCommandExecutor.layer,
layerRuntime,
),
),