Skip to content

Commit 15ae88b

Browse files
committed
fix(server): auto thread sends queue while a turn is stopping
1 parent 3da48c1 commit 15ae88b

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ const make = Effect.gen(function* () {
863863
...(input.modelSelection === undefined ? {} : { modelSelection: input.modelSelection }),
864864
...(input.freshContext ? { freshContext: true } : {}),
865865
dispatchMode,
866+
...(mode === "auto" ? { deliveryIntent: "auto" } : {}),
866867
createdBy: input.createdBy,
867868
creationSource: input.creationSource,
868869
});

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,112 @@ it.effect("a message the user sends after Stop releases the held queue; an agent
796796
}).pipe(Effect.provide(layerTest)),
797797
);
798798

799+
it.effect.each(["auto", "steer"] as const)(
800+
"an MCP %s send preserves its intent while the target run is stopping",
801+
(mode) =>
802+
Effect.gen(function* () {
803+
const orchestrator = yield* Orchestrator.OrchestratorV2;
804+
const projections = yield* ProjectionStore.ProjectionStoreV2;
805+
const threads = yield* ThreadManagementService.ThreadManagementService;
806+
const threadId = ThreadId.make(`thread:stop-mcp-send:${mode}`);
807+
yield* createWatchingThread(threadId, 6);
808+
yield* send(threadId, "active", "start_immediately");
809+
const initial = yield* orchestrator.getThreadProjection(threadId);
810+
const run = initial.runs[0]!;
811+
const providerSessionId = ProviderSessionId.make("session:stop-mcp-send");
812+
const now = yield* DateTime.now;
813+
yield* projections.apply({
814+
id: EventId.make("event:stop-mcp-send:session"),
815+
type: "provider-session.updated",
816+
threadId,
817+
occurredAt: now,
818+
payload: {
819+
id: providerSessionId,
820+
driver: adapter.driver,
821+
providerInstanceId: instanceId,
822+
status: "ready",
823+
cwd: "/repo",
824+
model: modelSelection.model,
825+
capabilities: CodexProviderCapabilitiesV2,
826+
createdAt: now,
827+
updatedAt: now,
828+
lastError: null,
829+
},
830+
});
831+
yield* projections.apply({
832+
id: EventId.make("event:stop-mcp-send:provider-thread"),
833+
type: "provider-thread.updated",
834+
threadId,
835+
occurredAt: now,
836+
payload: { ...initial.providerThreads[0]!, providerSessionId },
837+
});
838+
yield* projections.apply({
839+
id: EventId.make("event:stop-mcp-send:running"),
840+
type: "run.updated",
841+
threadId,
842+
runId: run.id,
843+
occurredAt: now,
844+
payload: { ...run, status: "running", startedAt: now },
845+
});
846+
yield* projections.apply({
847+
id: EventId.make("event:stop-mcp-send:turn"),
848+
type: "provider-turn.updated",
849+
threadId,
850+
occurredAt: now,
851+
payload: {
852+
id: ProviderTurnId.make("provider-turn:stop-mcp-send"),
853+
providerThreadId: run.providerThreadId!,
854+
nodeId: run.rootNodeId!,
855+
runAttemptId: run.activeAttemptId!,
856+
nativeTurnRef: null,
857+
ordinal: 1,
858+
status: "running",
859+
startedAt: now,
860+
completedAt: null,
861+
},
862+
});
863+
yield* send(threadId, "queued before the stop", "queue_after_active");
864+
yield* orchestrator.dispatch({
865+
type: "thread.stop",
866+
commandId: CommandId.make("stop-mcp-send"),
867+
threadId,
868+
});
869+
assert.deepEqual((yield* threadState(threadId)).runs, ["running", "queued:held"]);
870+
871+
const input = {
872+
projectId: ProjectId.make("project:thread-stop"),
873+
commandId: CommandId.make("command:stop-mcp-send"),
874+
threadId,
875+
messageId: MessageId.make("message:stop-mcp-send"),
876+
text: "Follow up after Stop",
877+
attachments: [],
878+
mode,
879+
createdBy: "agent",
880+
creationSource: "mcp",
881+
} as const;
882+
if (mode === "steer") {
883+
const error = yield* threads.sendToThread(input).pipe(Effect.flip);
884+
assert.equal(error._tag, "OrchestratorDispatchError");
885+
assert.deepEqual((yield* threadState(threadId)).runs, ["running", "queued:held"]);
886+
return;
887+
}
888+
889+
const result = yield* threads.sendToThread(input);
890+
assert.equal(result.delivery, "queued");
891+
assert.equal(result.run.status, "queued");
892+
assert.isTrue(result.run.queueHeld);
893+
assert.deepEqual((yield* threadState(threadId)).runs, [
894+
"running",
895+
"queued:held",
896+
"queued:held",
897+
]);
898+
const replay = yield* threads.sendToThread(input);
899+
assert.equal(replay.run.id, result.run.id);
900+
assert.equal(replay.delivery, "queued");
901+
assert.lengthOf((yield* threads.getThreadProjection(threadId)).messages, 3);
902+
}).pipe(Effect.provide(layerTest)),
903+
);
904+
799905
it.effect(
800906
"thread.stop ends an idle thread's watches and accepts a thread with nothing to stop",
801907
() =>

0 commit comments

Comments
 (0)