Skip to content

Commit 0b5c0f1

Browse files
fix(server): Force stop keeps only the session a newer run uses
A queued follow-up can switch provider instance on promotion. Force stop kept every session of the thread whenever any newer run was active, so the stopped run's session on the old instance stayed alive for the idle timeout while the reply said released. The newer-run check is now per session and only counts a run on that session's provider instance. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d8ac38 commit 0b5c0f1

2 files changed

Lines changed: 69 additions & 24 deletions

File tree

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,14 +1304,30 @@ const make = Effect.gen(function* () {
13041304
providerThread.appThreadId === input.threadId ? [providerThread.id] : [],
13051305
);
13061306
// Settling the target can promote a queued follow-up onto the same session; that run is
1307-
// not Force stop's to end. An unreadable thread counts as one, so its session is kept.
1307+
// not Force stop's to end. Sessions are per provider instance and a follow-up may switch
1308+
// instance, so only a session of the newer run's instance is kept. An unreadable thread
1309+
// counts as a newer run, so its sessions are kept.
13081310
const throughRunOrdinal = target?.ordinal ?? 0;
1309-
const laterRunActive = orchestrator.getThreadRecords(input.threadId, ["runs"]).pipe(
1310-
Effect.map(({ runs }) =>
1311-
runs.some((run) => run.ordinal > throughRunOrdinal && isActiveRun(run)),
1312-
),
1313-
Effect.orElseSucceed(() => true),
1314-
);
1311+
const laterRunActive = (providerSessionId: ProviderSessionId) => {
1312+
const instanceIds = new Set(
1313+
records.providerThreads.flatMap((providerThread) =>
1314+
providerThread.providerSessionId === providerSessionId
1315+
? [providerThread.providerInstanceId]
1316+
: [],
1317+
),
1318+
);
1319+
return orchestrator.getThreadRecords(input.threadId, ["runs"]).pipe(
1320+
Effect.map(({ runs }) =>
1321+
runs.some(
1322+
(run) =>
1323+
run.ordinal > throughRunOrdinal &&
1324+
isActiveRun(run) &&
1325+
instanceIds.has(run.providerInstanceId),
1326+
),
1327+
),
1328+
Effect.orElseSucceed(() => true),
1329+
);
1330+
};
13151331
const detach = (providerSessionId: ProviderSessionId) =>
13161332
sessions
13171333
.detach({ providerSessionId, threadId: input.threadId, detail: FORCE_STOP_REASON })
@@ -1351,7 +1367,7 @@ const make = Effect.gen(function* () {
13511367
ownProviderThreadIds,
13521368
throughRunOrdinal,
13531369
// A promoted follow-up may not have marked its turn busy yet.
1354-
keepIf: laterRunActive,
1370+
keepIf: laterRunActive(providerSessionId),
13551371
reason: "manual_shutdown",
13561372
detail: FORCE_STOP_REASON,
13571373
})
@@ -1366,7 +1382,7 @@ const make = Effect.gen(function* () {
13661382
);
13671383
if (Option.isNone(released)) {
13681384
releasing = true;
1369-
} else if (!released.value && !(yield* laterRunActive)) {
1385+
} else if (!released.value && !(yield* laterRunActive(release.providerSessionId))) {
13701386
shared = true;
13711387
yield* detach(release.providerSessionId);
13721388
}

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,25 +1840,34 @@ it.effect.each(["Stop", "Force stop"] as const)(
18401840
);
18411841

18421842
it.effect.each([
1843-
{ followUp: "started its turn", startedBeforeRelease: true },
1844-
{ followUp: "was promoted", startedBeforeRelease: false },
1843+
{ followUp: "started its turn", startedBeforeRelease: true, switchesInstance: false },
1844+
{ followUp: "was promoted", startedBeforeRelease: false, switchesInstance: false },
1845+
{ followUp: "switched provider instance", startedBeforeRelease: true, switchesInstance: true },
18451846
])(
1846-
"Force stop keeps the session of a queued follow-up that $followUp",
1847-
({ startedBeforeRelease }) =>
1847+
"Force stop keeps only the session of a queued follow-up that $followUp",
1848+
({ startedBeforeRelease, switchesInstance }) =>
18481849
Effect.scoped(
18491850
Effect.gen(function* () {
18501851
const cwd = yield* checkpointWorkspace("thread-stop-force-stop-follow-up");
18511852
const driver = adapter.driver;
1852-
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
1853+
const otherInstanceId = ProviderInstanceId.make("codex-other");
1854+
const eventQueues = new Map([
1855+
[instanceId, yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>()],
1856+
[otherInstanceId, yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>()],
1857+
]);
18531858
const started: Array<ProviderAdapter.ProviderAdapterV2TurnInput> = [];
18541859
const interrupted: Array<ProviderAdapter.ProviderAdapterV2InterruptInput> = [];
18551860
let opened = 0;
1856-
const liveAdapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
1861+
const liveAdapter = (
1862+
instanceId: ProviderInstanceId,
1863+
): ProviderAdapter.ProviderAdapterV2["Service"] => ({
18571864
...adapter,
1865+
instanceId,
18581866
openSession: (input) =>
18591867
Effect.gen(function* () {
18601868
opened += 1;
18611869
const now = yield* DateTime.now;
1870+
const events = eventQueues.get(instanceId)!;
18621871
return {
18631872
instanceId,
18641873
driver,
@@ -1878,7 +1887,7 @@ it.effect.each([
18781887
events: Stream.fromQueue(events),
18791888
ensureThread: ({ threadId }) =>
18801889
Effect.succeed({
1881-
id: ProviderThreadId.make(`provider-thread:codex:${threadId}`),
1890+
id: ProviderThreadId.make(`provider-thread:${instanceId}:${threadId}`),
18821891
driver,
18831892
providerInstanceId: instanceId,
18841893
providerSessionId: input.providerSessionId,
@@ -1926,7 +1935,7 @@ it.effect.each([
19261935
forkThread: () => Effect.die("unused"),
19271936
};
19281937
}),
1929-
};
1938+
});
19301939
// The settlement promotes the follow-up; optionally its turn starts before Force stop releases.
19311940
const layerSessionsStartingFollowUpFirst = Layer.effect(
19321941
ProviderSessionManager.ProviderSessionManagerV2,
@@ -1952,7 +1961,10 @@ it.effect.each([
19521961
EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
19531962
ProviderReplayHarness.layerWithRegistry(
19541963
{ name: "thread-stop-force-stop-follow-up" },
1955-
ProviderAdapterRegistry.layerFromAdapters([liveAdapter]),
1964+
ProviderAdapterRegistry.layerFromAdapters([
1965+
liveAdapter(instanceId),
1966+
liveAdapter(otherInstanceId),
1967+
]),
19561968
{ databaseLayer: layerDatabase, runEffectWorker: false },
19571969
),
19581970
),
@@ -1994,7 +2006,20 @@ it.effect.each([
19942006
yield* send(threadId, "first", "start_immediately");
19952007
yield* worker.drain();
19962008
yield* Fiber.join(firstRunning);
1997-
yield* send(threadId, "follow-up", "queue_after_active");
2009+
yield* orchestrator.dispatch({
2010+
type: "message.dispatch",
2011+
commandId: CommandId.make("send:force-stop-follow-up"),
2012+
threadId,
2013+
messageId: MessageId.make("message:force-stop-follow-up"),
2014+
text: "follow-up",
2015+
attachments: [],
2016+
dispatchMode: { type: "queue_after_active" },
2017+
...(switchesInstance
2018+
? { modelSelection: { ...modelSelection, instanceId: otherInstanceId } }
2019+
: {}),
2020+
createdBy: "user",
2021+
creationSource: "web",
2022+
});
19982023

19992024
const result = yield* threads.forceStopThread({ threadId });
20002025
yield* worker.drain();
@@ -2006,12 +2031,16 @@ it.effect.each([
20062031
);
20072032
assert.equal(
20082033
opened,
2009-
1,
2034+
switchesInstance ? 2 : 1,
20102035
"Force stop tore down the session the follow-up was promoted onto",
20112036
);
2012-
const followUp = started[1]!;
2013-
const providerSessionId = (yield* orchestrator.getThreadProjection(threadId))
2014-
.providerThreads[0]!.providerSessionId!;
2037+
const [stopped, followUp] = [started[0]!, started[1]!];
2038+
const providerSessionId = followUp.providerThread.providerSessionId!;
2039+
assert.equal(
2040+
yield* sessions.isLive(stopped.providerThread.providerSessionId!),
2041+
!switchesInstance,
2042+
"Force stop kept the stopped run's session for a follow-up on another instance",
2043+
);
20152044
assert.isFalse(
20162045
interrupted.some((interrupt) =>
20172046
String(interrupt.providerTurnId).includes(followUp.attemptId),
@@ -2029,7 +2058,7 @@ it.effect.each([
20292058
);
20302059

20312060
// The first run's provider never confirms the stop; the follow-up's turn ends normally.
2032-
yield* Queue.offer(events, {
2061+
yield* Queue.offer(eventQueues.get(followUp.providerThread.providerInstanceId)!, {
20332062
type: "turn.terminal",
20342063
driver,
20352064
providerThreadId: followUp.providerThread.id,

0 commit comments

Comments
 (0)