Skip to content

Commit 1edcd81

Browse files
author
PR Batch Tester
committed
fix(server): serialize forks with source thread deletion
1 parent ebe2236 commit 1edcd81

3 files changed

Lines changed: 187 additions & 5 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,7 +4048,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
40484048
}),
40494049
),
40504050
);
4051-
// The source is unlocked; validate the modes copied here, not an earlier read.
4051+
if (sourceProjection.thread.deletedAt !== null) {
4052+
return yield* new OrchestratorDispatchError({
4053+
commandId: command.commandId,
4054+
commandType: command.type,
4055+
cause: `Thread ${command.sourceThreadId} is deleted.`,
4056+
});
4057+
}
4058+
// Source settings can change outside the deletion lock; validate the modes copied here.
40524059
yield* refuseAboveDispatchModeLimit(command, command.sourceThreadId, sourceProjection.thread);
40534060

40544061
const sourceRun = runForSourcePoint(sourceProjection, command.sourcePoint);
@@ -12364,7 +12371,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1236412371
const dispatchWithReceipt = Effect.fnUntraced(function* (command: OrchestrationV2ServerCommand) {
1236512372
const dispatch = threadDispatch.withLock(
1236612373
commandThreadId(command),
12367-
command.type === "thread.delete"
12374+
command.type === "thread.delete" || command.type === "thread.fork"
1236812375
? threadDispatch.withDeletionLock(dispatchWithReceiptEffect(command))
1236912376
: dispatchWithReceiptEffect(command),
1237012377
);

‎apps/server/src/orchestration-v2/ThreadCommandExecutor.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ export class ThreadCommandExecutor extends Context.Service<
99
ThreadCommandExecutor,
1010
KeyedLock.KeyedLock<ThreadId> & {
1111
/**
12-
* Serializes thread deletion planning through its commit, since deleting one thread
13-
* reads whether other threads still show its pages. Take it inside the thread lock and
14-
* never take a thread lock while holding it.
12+
* Serializes fork and deletion planning through commit because forks retain source pages.
13+
* Take it inside the thread lock; never take a thread lock while holding it.
1514
*/
1615
readonly withDeletionLock: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
1716
}

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import * as Stream from "effect/Stream";
4242
import * as TestClock from "effect/testing/TestClock";
4343
import * as SqlClient from "effect/sql/SqlClient";
4444

45+
import { createAttachmentId } from "../attachmentStore.ts";
4546
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
4647
import * as GitWorkflow from "../git/GitWorkflowService.ts";
4748
import * as ServerConfig from "../config.ts";
@@ -716,6 +717,181 @@ it.layer(layerProjectDeletionTest)("project deletion during thread commands", (i
716717
);
717718
}),
718719
);
720+
721+
it.effect.each(["fork", "delete"] as const)(
722+
"preserves page ownership when %s commits before a concurrent fork or deletion",
723+
(first) =>
724+
Effect.gen(function* () {
725+
const orchestrator = yield* Orchestrator.OrchestratorV2;
726+
const eventSink = yield* EventSink.EventSinkV2;
727+
const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor;
728+
const now = yield* DateTime.now;
729+
const sourceThreadId = ThreadId.make(`fork-delete-source:${first}`);
730+
const targetThreadId = ThreadId.make(`fork-delete-target:${first}`);
731+
const runId = RunId.make(`fork-delete-run:${first}`);
732+
const forkCommandId = CommandId.make(`fork-delete-fork:${first}`);
733+
const deleteCommandId = CommandId.make(`fork-delete-delete:${first}`);
734+
const attachmentId = createAttachmentId(sourceThreadId, "html")!;
735+
yield* orchestrator.dispatch({
736+
type: "thread.create",
737+
commandId: CommandId.make(`fork-delete-create:${first}`),
738+
threadId: sourceThreadId,
739+
projectId: ProjectId.make(`fork-delete-project:${first}`),
740+
title: "Page source",
741+
modelSelection,
742+
runtimeMode: "full-access",
743+
interactionMode: "default",
744+
branch: null,
745+
worktreePath: null,
746+
createdBy: "user",
747+
creationSource: "web",
748+
});
749+
yield* eventSink.write({
750+
events: [
751+
{
752+
id: EventId.make(`fork-delete-run:${first}`),
753+
type: "run.created",
754+
threadId: sourceThreadId,
755+
runId,
756+
occurredAt: now,
757+
payload: {
758+
id: runId,
759+
threadId: sourceThreadId,
760+
ordinal: 1,
761+
providerInstanceId: modelSelection.instanceId,
762+
modelSelection,
763+
providerThreadId: null,
764+
userMessageId: MessageId.make(`fork-delete-message:${first}`),
765+
rootNodeId: null,
766+
activeAttemptId: null,
767+
status: "completed",
768+
requestedAt: now,
769+
startedAt: now,
770+
completedAt: now,
771+
checkpointId: null,
772+
contextHandoffId: null,
773+
},
774+
},
775+
{
776+
id: EventId.make(`fork-delete-page:${first}`),
777+
type: "turn-item.updated",
778+
threadId: sourceThreadId,
779+
runId,
780+
occurredAt: now,
781+
payload: {
782+
id: TurnItemId.make(`fork-delete-page:${first}`),
783+
threadId: sourceThreadId,
784+
runId,
785+
nodeId: null,
786+
providerThreadId: null,
787+
providerTurnId: null,
788+
nativeItemRef: null,
789+
parentItemId: null,
790+
ordinal: 1,
791+
status: "completed",
792+
title: "Shared page",
793+
startedAt: now,
794+
completedAt: now,
795+
updatedAt: now,
796+
type: "dynamic_tool",
797+
toolName: "mcp__t3-code__html_render",
798+
input: {},
799+
output: { htmlRender: { attachmentId, title: "Shared page", height: 300 } },
800+
},
801+
},
802+
],
803+
});
804+
const firstReady = yield* Deferred.make<void>();
805+
const releaseFirst = yield* Deferred.make<void>();
806+
const secondQueued = yield* Deferred.make<void>();
807+
const cleanupIds: Array<string> = [];
808+
const commitCommand = eventSink.commitCommand;
809+
const commitSpy = vi.spyOn(eventSink, "commitCommand").mockImplementation((input) => {
810+
if (input.commandId === deleteCommandId) {
811+
for (const effect of input.effects ?? []) {
812+
if (effect.request.type === "attachment.cleanup")
813+
cleanupIds.push(...effect.request.attachmentIds);
814+
}
815+
}
816+
return input.commandId === (first === "fork" ? forkCommandId : deleteCommandId)
817+
? Deferred.succeed(firstReady, undefined).pipe(
818+
Effect.andThen(Deferred.await(releaseFirst)),
819+
Effect.andThen(commitCommand(input)),
820+
)
821+
: commitCommand(input);
822+
});
823+
const withDeletionLock = executor.withDeletionLock;
824+
let lockRequests = 0;
825+
const observeLock: ThreadCommandExecutor.ThreadCommandExecutor["Service"]["withDeletionLock"] =
826+
(effect) =>
827+
++lockRequests === 2
828+
? Deferred.succeed(secondQueued, undefined).pipe(
829+
Effect.andThen(withDeletionLock(effect)),
830+
)
831+
: withDeletionLock(effect);
832+
const lockSpy = vi.spyOn(executor, "withDeletionLock").mockImplementation(observeLock);
833+
yield* Effect.gen(function* () {
834+
const fork = orchestrator
835+
.dispatch({
836+
type: "thread.fork",
837+
commandId: forkCommandId,
838+
sourceThreadId,
839+
targetThreadId,
840+
sourcePoint: { type: "run", runId },
841+
createdBy: "user",
842+
creationSource: "web",
843+
})
844+
.pipe(Effect.result);
845+
const deletion = orchestrator
846+
.dispatch({
847+
type: "thread.delete",
848+
commandId: deleteCommandId,
849+
threadId: sourceThreadId,
850+
})
851+
.pipe(Effect.result);
852+
const firstFiber = yield* (first === "fork" ? fork : deletion).pipe(
853+
Effect.forkChild({ startImmediately: true }),
854+
);
855+
yield* Effect.raceFirst(
856+
Deferred.await(firstReady),
857+
Fiber.join(firstFiber).pipe(Effect.andThen(Effect.die("Missing commit barrier"))),
858+
);
859+
const secondFiber = yield* (first === "fork" ? deletion : fork).pipe(
860+
Effect.forkChild({ startImmediately: true }),
861+
);
862+
yield* Effect.raceFirst(Deferred.await(secondQueued), Fiber.await(secondFiber));
863+
yield* Deferred.succeed(releaseFirst, undefined);
864+
const firstResult = yield* Fiber.join(firstFiber);
865+
const secondResult = yield* Fiber.join(secondFiber);
866+
assert.equal(firstResult._tag, "Success");
867+
if (first === "fork") {
868+
assert.equal(secondResult._tag, "Success");
869+
assert.deepEqual(cleanupIds, []);
870+
const forkProjection = yield* orchestrator.getThreadProjection(targetThreadId);
871+
assert.isNull(forkProjection.thread.deletedAt);
872+
assert.isTrue(
873+
forkProjection.visibleTurnItems.some(
874+
(row) => row.item.id === TurnItemId.make(`fork-delete-page:${first}`),
875+
),
876+
);
877+
} else {
878+
assert.equal(secondResult._tag, "Failure");
879+
assert.deepEqual(cleanupIds, [attachmentId]);
880+
const target = yield* orchestrator
881+
.getThreadProjection(targetThreadId)
882+
.pipe(Effect.result);
883+
assert.equal(target._tag, "Failure");
884+
}
885+
}).pipe(
886+
Effect.ensuring(
887+
Effect.sync(() => {
888+
commitSpy.mockRestore();
889+
lockSpy.mockRestore();
890+
}),
891+
),
892+
);
893+
}),
894+
);
719895
});
720896

721897
const layerSharedApplicationDataPlaneTest = Layer.mergeAll(

0 commit comments

Comments
 (0)