Skip to content

Commit 5f2c27e

Browse files
fix(server): Stop ends a run whose provider turn already finished (pingdotgg#28520)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b12ab30 commit 5f2c27e

2 files changed

Lines changed: 91 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10448,7 +10448,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1044810448
rootNode === undefined ||
1044910449
run.activeAttemptId !== attempt.id ||
1045010450
run.status !== "running" ||
10451-
attempt.status !== "running"
10451+
attempt.status === "pending"
1045210452
)
1045310453
return;
1045410454
const emitEvent = emit(input.events, input.command);
@@ -10470,15 +10470,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1047010470
},
1047110471
});
1047210472
}
10473-
yield* emitEvent({
10474-
...base,
10475-
type: "run-attempt.updated",
10476-
payload: {
10477-
...attempt,
10478-
status: "interrupted",
10479-
completedAt: input.now,
10480-
},
10481-
});
10473+
// An attempt that already ended keeps its status; only its run was left running.
10474+
if (attempt.status === "running") {
10475+
yield* emitEvent({
10476+
...base,
10477+
type: "run-attempt.updated",
10478+
payload: {
10479+
...attempt,
10480+
status: "interrupted",
10481+
completedAt: input.now,
10482+
},
10483+
});
10484+
}
1048210485
const providerThread = input.projection.providerThreads.find(
1048310486
(candidate) => candidate.id === input.providerTurn.providerThreadId,
1048410487
);

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,3 +1664,81 @@ it.effect("an unanswered Stop is flagged until Force stop ends the run with its
16641664
assert.lengthOf(late.storedEvents, 0);
16651665
}).pipe(Effect.provide(layerTest)),
16661666
);
1667+
1668+
it.effect.each(["Stop", "Force stop"] as const)(
1669+
"%s ends a running run whose provider turn already completed",
1670+
(action) =>
1671+
Effect.gen(function* () {
1672+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1673+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1674+
const threadId = ThreadId.make("thread:stop-completed-turn");
1675+
yield* createWatchingThread(threadId, 7);
1676+
yield* send(threadId, "go", "start_immediately");
1677+
const now = yield* DateTime.now;
1678+
const started = yield* orchestrator.getThreadProjection(threadId);
1679+
const run = started.runs[0]!;
1680+
const attempt = started.attempts[0]!;
1681+
const providerThreadId = run.providerThreadId!;
1682+
const providerTurnId = ProviderTurnId.make("provider-turn:stop-completed-turn");
1683+
const apply = (event: Omit<OrchestrationV2DomainEvent, "id" | "threadId">) =>
1684+
projections.apply({
1685+
...event,
1686+
id: EventId.make(`event:stop-completed-turn:${event.type}`),
1687+
threadId,
1688+
} as OrchestrationV2DomainEvent);
1689+
yield* apply({
1690+
type: "run.updated",
1691+
runId: run.id,
1692+
occurredAt: now,
1693+
payload: { ...run, status: "running", startedAt: now, activeAttemptId: attempt.id },
1694+
});
1695+
yield* apply({
1696+
type: "run-attempt.updated",
1697+
occurredAt: now,
1698+
payload: { ...attempt, status: "completed", startedAt: now, completedAt: now },
1699+
});
1700+
yield* apply({
1701+
type: "provider-turn.updated",
1702+
occurredAt: now,
1703+
payload: {
1704+
id: providerTurnId,
1705+
providerThreadId,
1706+
nodeId: run.rootNodeId!,
1707+
runAttemptId: attempt.id,
1708+
nativeTurnRef: null,
1709+
ordinal: 1,
1710+
status: "completed",
1711+
startedAt: now,
1712+
completedAt: now,
1713+
},
1714+
});
1715+
// The provider session was already released.
1716+
yield* apply({
1717+
type: "provider-thread.updated",
1718+
occurredAt: now,
1719+
payload: {
1720+
...started.providerThreads.find((thread) => thread.id === providerThreadId)!,
1721+
providerSessionId: ProviderSessionId.make("session:gone"),
1722+
},
1723+
});
1724+
yield* send(threadId, "follow", "queue_after_active");
1725+
1726+
if (action === "Stop") {
1727+
yield* orchestrator.dispatch({
1728+
type: "thread.stop",
1729+
commandId: CommandId.make("stop-completed-turn"),
1730+
threadId,
1731+
});
1732+
} else {
1733+
const threads = yield* ThreadManagementService.ThreadManagementService;
1734+
yield* threads.forceStopThread({ threadId });
1735+
}
1736+
const stopped = yield* orchestrator.getThreadProjection(threadId);
1737+
assert.notEqual(stopped.runs[0]?.status, "running", "the stopped run is still running");
1738+
assert.deepInclude(stopped.attempts[0], { status: "completed", completedAt: now });
1739+
assert.isTrue(
1740+
stopped.turnItems.some((item) => item.type === "run_interrupt_result"),
1741+
"the stopped run has no interrupt result",
1742+
);
1743+
}).pipe(Effect.provide(layerTest)),
1744+
);

0 commit comments

Comments
 (0)