Skip to content
TypeScript

fix(server): Stop ends a run whose provider turn already finished (#28520)

Julius Marminge5f2c27e706de
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 1fed13e08ad..2dc0a5a66d5 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -10448,7 +10448,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
rootNode === undefined ||
run.activeAttemptId !== attempt.id ||
run.status !== "running" ||
- attempt.status !== "running"
+ attempt.status === "pending"
)
return;
const emitEvent = emit(input.events, input.command);
@@ -10470,15 +10470,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
},
});
}
- yield* emitEvent({
- ...base,
- type: "run-attempt.updated",
- payload: {
- ...attempt,
- status: "interrupted",
- completedAt: input.now,
- },
- });
+ // An attempt that already ended keeps its status; only its run was left running.
+ if (attempt.status === "running") {
+ yield* emitEvent({
+ ...base,
+ type: "run-attempt.updated",
+ payload: {
+ ...attempt,
+ status: "interrupted",
+ completedAt: input.now,
+ },
+ });
+ }
const providerThread = input.projection.providerThreads.find(
(candidate) => candidate.id === input.providerTurn.providerThreadId,
);
diff --git a/apps/server/src/orchestration-v2/ThreadStop.test.ts b/apps/server/src/orchestration-v2/ThreadStop.test.ts
index 16dcedaf8ac..6b3ea08a864 100644
--- a/apps/server/src/orchestration-v2/ThreadStop.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadStop.test.ts
@@ -1664,3 +1664,81 @@ it.effect("an unanswered Stop is flagged until Force stop ends the run with its
assert.lengthOf(late.storedEvents, 0);
}).pipe(Effect.provide(layerTest)),
);
+
+it.effect.each(["Stop", "Force stop"] as const)(
+ "%s ends a running run whose provider turn already completed",
+ (action) =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
+ const threadId = ThreadId.make("thread:stop-completed-turn");
+ yield* createWatchingThread(threadId, 7);
+ yield* send(threadId, "go", "start_immediately");
+ const now = yield* DateTime.now;
+ const started = yield* orchestrator.getThreadProjection(threadId);
+ const run = started.runs[0]!;
+ const attempt = started.attempts[0]!;
+ const providerThreadId = run.providerThreadId!;
+ const providerTurnId = ProviderTurnId.make("provider-turn:stop-completed-turn");
+ const apply = (event: Omit<OrchestrationV2DomainEvent, "id" | "threadId">) =>
+ projections.apply({
+ ...event,
+ id: EventId.make(`event:stop-completed-turn:${event.type}`),
+ threadId,
+ } as OrchestrationV2DomainEvent);
+ yield* apply({
+ type: "run.updated",
+ runId: run.id,
+ occurredAt: now,
+ payload: { ...run, status: "running", startedAt: now, activeAttemptId: attempt.id },
+ });
+ yield* apply({
+ type: "run-attempt.updated",
+ occurredAt: now,
+ payload: { ...attempt, status: "completed", startedAt: now, completedAt: now },
+ });
+ yield* apply({
+ type: "provider-turn.updated",
+ occurredAt: now,
+ payload: {
+ id: providerTurnId,
+ providerThreadId,
+ nodeId: run.rootNodeId!,
+ runAttemptId: attempt.id,
+ nativeTurnRef: null,
+ ordinal: 1,
+ status: "completed",
+ startedAt: now,
+ completedAt: now,
+ },
+ });
+ // The provider session was already released.
+ yield* apply({
+ type: "provider-thread.updated",
+ occurredAt: now,
+ payload: {
+ ...started.providerThreads.find((thread) => thread.id === providerThreadId)!,
+ providerSessionId: ProviderSessionId.make("session:gone"),
+ },
+ });
+ yield* send(threadId, "follow", "queue_after_active");
+
+ if (action === "Stop") {
+ yield* orchestrator.dispatch({
+ type: "thread.stop",
+ commandId: CommandId.make("stop-completed-turn"),
+ threadId,
+ });
+ } else {
+ const threads = yield* ThreadManagementService.ThreadManagementService;
+ yield* threads.forceStopThread({ threadId });
+ }
+ const stopped = yield* orchestrator.getThreadProjection(threadId);
+ assert.notEqual(stopped.runs[0]?.status, "running", "the stopped run is still running");
+ assert.deepInclude(stopped.attempts[0], { status: "completed", completedAt: now });
+ assert.isTrue(
+ stopped.turnItems.some((item) => item.type === "run_interrupt_result"),
+ "the stopped run has no interrupt result",
+ );
+ }).pipe(Effect.provide(layerTest)),
+);