The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(server): Force stop no longer tears down a queued follow-up that started during cleanup (#31038)
Julius Marminge committed
Nov 12, 2026 at 19:54 UTC
2e0bd97e86c700a59c9b5c35ea1b8bb85414161b
5 files changed
+432
-46
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+68
@@ -1626,6 +1626,73 @@ it.effect("ProviderSessionManagerV2 gives up on a provider that never confirms a
1626
}),
1627
);
1628
1629
+it.effect("ProviderSessionManagerV2 still idles out a session releaseUnlessBusy kept", () =>
1630
+ Effect.gen(function* () {
1631
+ const state = yield* Ref.make(emptyState);
1632
+ yield* Effect.gen(function* () {
1633
+ const eventSink = yield* EventSink.EventSinkV2;
1634
+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
1635
+ const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
1636
+ const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1637
+ const now = yield* DateTime.now;
1638
+ const threadId = ThreadId.make("thread-provider-session-manager-kept-idles");
1639
+ const providerSessionId = idAllocator.derive.providerSession({
1640
+ providerInstanceId: modelSelection.instanceId,
1641
+ });
1642
+ yield* eventSink.write({
1643
+ events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1644
+ });
1645
+ const runtime = yield* manager.open({
1646
+ threadId,
1647
+ providerSessionId,
1648
+ modelSelection,
1649
+ runtimePolicy,
1650
+ });
1651
+ const providerThread = makeProviderThread({
1652
+ idAllocator,
1653
+ threadId,
1654
+ providerSessionId,
1655
+ now,
1656
+ nativeThreadId: `native-${threadId}`,
1657
+ });
1658
+ const runId = idAllocator.derive.run({ threadId, ordinal: 1 });
1659
+ yield* runtime.startTurn({
1660
+ appThread: (yield* projectionStore.getThreadProjection(threadId)).thread,
1661
+ threadId,
1662
+ runId,
1663
+ runOrdinal: 1,
1664
+ providerTurnOrdinal: 1,
1665
+ attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }),
1666
+ rootNodeId: idAllocator.derive.rootNode({ runId }),
1667
+ providerThread,
1668
+ message: {
1669
+ createdBy: "user",
1670
+ creationSource: "web",
1671
+ messageId: yield* idAllocator.allocate.message({ threadId, ordinal: 1 }),
1672
+ text: "turn",
1673
+ attachments: [],
1674
+ },
1675
+ modelSelection,
1676
+ runtimePolicy,
1677
+ });
1678
+
1679
+ assert.isFalse(
1680
+ yield* manager.releaseUnlessBusy({
1681
+ providerSessionId,
1682
+ ownProviderThreadIds: [providerThread.id],
1683
+ throughRunOrdinal: 1,
1684
+ keepIf: Effect.succeed(true),
1685
+ reason: "manual_shutdown",
1686
+ }),
1687
+ );
1688
+ assert.isTrue(yield* manager.isLive(providerSessionId));
1689
+ // The stopped run 1 never reports its end.
1690
+ yield* TestClock.adjust("2 seconds");
1691
+ assert.isFalse(yield* manager.isLive(providerSessionId));
1692
+ }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1_000 })));
1693
+ }),
1694
+);
1695
+
1696
it.effect(
1697
"ProviderSessionManagerV2 keeps a shared session a sibling is busy on when releasing",
1698
() =>
@@ -1692,6 +1759,7 @@ it.effect(
1759
manager.releaseUnlessBusy({
1760
providerSessionId,
1761
ownProviderThreadIds: [providerThreadOf(own).id],
1762
+ throughRunOrdinal: 1,
1763
reason: "manual_shutdown",
1764
});
1765
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+72
-41
@@ -71,6 +71,17 @@ export const PROVIDER_INTERRUPT_TIMEOUT_MS = 15 * 1000;
71
const busyTurnPrefix = (providerThreadId: ProviderThreadId) => `${providerThreadId}#`;
72
const busyTurnKey = (providerThreadId: ProviderThreadId, runOrdinal: number) =>
73
`${busyTurnPrefix(providerThreadId)}${runOrdinal}`;
74
+interface OwnBusyTurns {
75
+ readonly providerThreadIds: ReadonlyArray<ProviderThreadId>;
76
+ readonly throughRunOrdinal: number;
77
+}
78
+const isOwnBusyTurn = (turnKey: string, own: OwnBusyTurns) =>
79
+ own.providerThreadIds.some((id) => {
80
+ const prefix = busyTurnPrefix(id);
81
+ return (
82
+ turnKey.startsWith(prefix) && Number(turnKey.slice(prefix.length)) <= own.throughRunOrdinal
83
+ );
84
+ });
85
const UNLOAD_THREAD_TIMEOUT_MS = 10 * 1000;
86
// Providers can announce a retry and never resume. Longer than any adapter's own stall handling.
87
const PROVIDER_RETRY_STALL_TIMEOUT = Duration.minutes(15);
@@ -243,13 +254,16 @@ export interface ProviderSessionManagerV2Shape {
254
readonly revokeMcpCredential?: boolean;
255
}) => Effect.Effect<void, ProviderSessionManagerV2Error>;
256
/**
246
- * Releases the session unless, at the moment of removal, a turn of a provider thread outside
247
- * `ownProviderThreadIds` is busy on it. Checked atomically with turn starts. Succeeds with
248
- * whether it was released.
257
+ * Releases the session unless, at the moment of removal, a turn is busy on it other than one
258
+ * of `ownProviderThreadIds` from a run up to `throughRunOrdinal`. Checked atomically with turn
259
+ * starts. Succeeds with whether it was released.
260
*/
261
readonly releaseUnlessBusy: (input: {
262
readonly providerSessionId: ProviderSessionId;
263
readonly ownProviderThreadIds: ReadonlyArray<ProviderThreadId>;
264
+ readonly throughRunOrdinal: number;
265
+ /** Keeps the session when it succeeds with true, checked atomically with the removal. */
266
+ readonly keepIf?: Effect.Effect<boolean>;
267
readonly reason: ProviderSessionReleaseReason;
268
readonly detail?: string;
269
}) => Effect.Effect<boolean, ProviderSessionManagerV2Error>;
@@ -1125,7 +1139,7 @@ export const layerWithOptions = (
1139
function* (input: {
1140
readonly providerSessionId: ProviderSessionId;
1141
readonly onlyIfIdleGeneration?: number;
1128
- readonly keepIfBusyOtherThan?: ReadonlyArray<ProviderThreadId>;
1142
+ readonly keepIfBusyOtherThan?: OwnBusyTurns;
1143
readonly keepIf?: Effect.Effect<boolean>;
1144
}): Effect.fn.Return<readonly [Option.Option<LiveSessionEntry>, DateTime.Utc]> {
1145
const key = sessionKey(input.providerSessionId);
@@ -1133,40 +1147,48 @@ export const layerWithOptions = (
1147
if (candidate === undefined) {
1148
return [Option.none<LiveSessionEntry>(), yield* DateTime.now] as const;
1149
}
1136
- const remove = Effect.zip(
1137
- Ref.modify(sessions, (current) => {
1138
- const existing = current.get(key);
1139
- if (existing !== candidate) {
1140
- return [existing === undefined ? "gone" : "changed", current] as const;
1141
- }
1142
- if (
1143
- input.onlyIfIdleGeneration !== undefined &&
1144
- (existing.busyTurns.size > 0 ||
1145
- existing.idleGeneration !== input.onlyIfIdleGeneration)
1146
- ) {
1147
- return ["kept", current] as const;
1148
- }
1149
- const own = input.keepIfBusyOtherThan;
1150
- if (
1151
- own !== undefined &&
1152
- [...existing.busyTurns].some(
1153
- (turnKey) => !own.some((id) => turnKey.startsWith(busyTurnPrefix(id))),
1154
- )
1155
- ) {
1156
- return ["kept", current] as const;
1157
- }
1158
- const updated = new Map(current);
1159
- updated.delete(key);
1160
- return ["removed", updated] as const;
1161
- }),
1162
- DateTime.now,
1150
+ const remove = (keepRequested: boolean) =>
1151
+ Effect.zip(
1152
+ Ref.modify(sessions, (current) => {
1153
+ const existing = current.get(key);
1154
+ if (existing !== candidate) {
1155
+ return [existing === undefined ? "gone" : "changed", current] as const;
1156
+ }
1157
+ if (
1158
+ input.onlyIfIdleGeneration !== undefined &&
1159
+ (existing.busyTurns.size > 0 ||
1160
+ existing.idleGeneration !== input.onlyIfIdleGeneration)
1161
+ ) {
1162
+ return ["kept", current] as const;
1163
+ }
1164
+ const own = input.keepIfBusyOtherThan;
1165
+ if (
1166
+ keepRequested ||
1167
+ (own !== undefined &&
1168
+ [...existing.busyTurns].some((turnKey) => !isOwnBusyTurn(turnKey, own)))
1169
+ ) {
1170
+ if (own === undefined) return ["kept", current] as const;
1171
+ // The caller settled its own turns; one whose end never arrives must not keep
1172
+ // the session from going idle once the turn it was kept for ends.
1173
+ const kept = new Map(current);
1174
+ kept.set(key, {
1175
+ ...existing,
1176
+ busyTurns: new Set(
1177
+ [...existing.busyTurns].filter((turnKey) => !isOwnBusyTurn(turnKey, own)),
1178
+ ),
1179
+ });
1180
+ return ["kept", kept] as const;
1181
+ }
1182
+ const updated = new Map(current);
1183
+ updated.delete(key);
1184
+ return ["removed", updated] as const;
1185
+ }),
1186
+ DateTime.now,
1187
+ );
1188
+ const [outcome, releasedAt] = yield* (input.keepIf ?? Effect.succeed(false)).pipe(
1189
+ Effect.flatMap(remove),
1190
+ candidate.requestEventPermit.withPermits(1),
1191
);
1164
- const [outcome, releasedAt] = yield* Effect.gen(function* () {
1165
- if (input.keepIf !== undefined && (yield* input.keepIf)) {
1166
- return ["kept" as const, yield* DateTime.now] as const;
1167
- }
1168
- return yield* remove;
1169
- }).pipe(candidate.requestEventPermit.withPermits(1));
1192
if (outcome === "changed") return yield* removeLiveEntry(input);
1193
return [
1194
outcome === "removed" ? Option.some(candidate) : Option.none<LiveSessionEntry>(),
@@ -1218,7 +1240,7 @@ export const layerWithOptions = (
1240
readonly detachedThreadId?: ThreadId;
1241
readonly cancelIdleFiber?: boolean;
1242
readonly onlyIfIdleGeneration?: number;
1221
- readonly keepIfBusyOtherThan?: ReadonlyArray<ProviderThreadId>;
1243
+ readonly keepIfBusyOtherThan?: OwnBusyTurns;
1244
readonly keepIf?: Effect.Effect<boolean>;
1245
readonly gracefulSubscribers?: boolean;
1246
readonly eventStreamCause?: Cause.Cause<ProviderAdapter.ProviderAdapterV2Error>;
@@ -3076,10 +3098,19 @@ export const layerWithOptions = (
3098
providerSessionId: input.providerSessionId,
3099
reason: input.reason,
3100
...(input.detail === undefined ? {} : { detail: input.detail }),
3079
- keepIfBusyOtherThan: input.ownProviderThreadIds,
3101
+ keepIfBusyOtherThan: {
3102
+ providerThreadIds: input.ownProviderThreadIds,
3103
+ throughRunOrdinal: input.throughRunOrdinal,
3104
+ },
3105
+ ...(input.keepIf === undefined ? {} : { keepIf: input.keepIf }),
3106
});
3081
- // Identity, not presence: open can install a replacement under the same id meanwhile.
3082
- return before !== undefined && (yield* Ref.get(sessions)).get(key) !== before;
3107
+ // Scope, not presence: open can install a replacement under the same id meanwhile, and
3108
+ // a kept entry is rewritten with the same scope.
3109
+ const released =
3110
+ before !== undefined && (yield* Ref.get(sessions)).get(key)?.scope !== before.scope;
3111
+ // The stopped turn's markBusy cancelled the idle timer and its markIdle may never come.
3112
+ if (!released) yield* scheduleIdleReleaseInternal(input.providerSessionId);
3113
+ return released;
3114
}),
3115
otherWorkingThreads: Effect.fn("ProviderSessionManager.otherWorkingThreads")(
3116
function* (input) {
apps/server/src/orchestration-v2/ThreadManagementService.ts
+26
-1
@@ -1303,6 +1303,28 @@ const make = Effect.gen(function* () {
1303
const ownProviderThreadIds = records.providerThreads.flatMap((providerThread) =>
1304
providerThread.appThreadId === input.threadId ? [providerThread.id] : [],
1305
);
1306
+ // Settling the target can promote a queued follow-up onto the same session; that run is
1307
+ // not Force stop's to end. Only a newer run bound to a provider thread on this session
1308
+ // counts. An unreadable thread counts as one, so its sessions are kept.
1309
+ const throughRunOrdinal = target?.ordinal ?? 0;
1310
+ const laterRunActive = (providerSessionId: ProviderSessionId) =>
1311
+ orchestrator.getThreadRecords(input.threadId, ["runs", "providerThreads"]).pipe(
1312
+ Effect.map(({ runs, providerThreads }) => {
1313
+ const onSession = new Set(
1314
+ providerThreads.flatMap((providerThread) =>
1315
+ providerThread.providerSessionId === providerSessionId ? [providerThread.id] : [],
1316
+ ),
1317
+ );
1318
+ return runs.some(
1319
+ (run) =>
1320
+ run.ordinal > throughRunOrdinal &&
1321
+ isActiveRun(run) &&
1322
+ run.providerThreadId !== null &&
1323
+ onSession.has(run.providerThreadId),
1324
+ );
1325
+ }),
1326
+ Effect.orElseSucceed(() => true),
1327
+ );
1328
const detach = (providerSessionId: ProviderSessionId) =>
1329
sessions
1330
.detach({ providerSessionId, threadId: input.threadId, detail: FORCE_STOP_REASON })
@@ -1340,6 +1362,9 @@ const make = Effect.gen(function* () {
1362
.releaseUnlessBusy({
1363
providerSessionId,
1364
ownProviderThreadIds,
1365
+ throughRunOrdinal,
1366
+ // A promoted follow-up may not have marked its turn busy yet.
1367
+ keepIf: laterRunActive(providerSessionId),
1368
reason: "manual_shutdown",
1369
detail: FORCE_STOP_REASON,
1370
})
@@ -1354,7 +1379,7 @@ const make = Effect.gen(function* () {
1379
);
1380
if (Option.isNone(released)) {
1381
releasing = true;
1357
- } else if (!released.value) {
1382
+ } else if (!released.value && !(yield* laterRunActive(release.providerSessionId))) {
1383
shared = true;
1384
yield* detach(release.providerSessionId);
1385
}
apps/server/src/orchestration-v2/ThreadStop.test.ts
+262
@@ -20,7 +20,9 @@ import {
20
import * as DateTime from "effect/DateTime";
21
import * as Effect from "effect/Effect";
22
import * as Exit from "effect/Exit";
23
+import * as Fiber from "effect/Fiber";
24
import * as Layer from "effect/Layer";
25
+import * as Queue from "effect/Queue";
26
import * as Schema from "effect/Schema";
27
import * as SqlClient from "effect/sql/SqlClient";
28
import * as Stream from "effect/Stream";
@@ -38,8 +40,10 @@ import * as EffectWorker from "./EffectWorker.ts";
40
import * as EffectOutbox from "./EffectOutbox.ts";
41
import * as ProjectionStore from "./ProjectionStore.ts";
42
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
43
+import * as ProviderSessionManager from "./ProviderSessionManager.ts";
44
import * as ThreadManagementService from "./ThreadManagementService.ts";
45
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
46
+import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
47
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
48
49
const instanceId = ProviderInstanceId.make("codex");
@@ -1834,3 +1838,261 @@ it.effect.each(["Stop", "Force stop"] as const)(
1838
);
1839
}).pipe(Effect.provide(layerTest)),
1840
);
1841
+
1842
+it.effect.each([
1843
+ { followUp: "started its turn", startedBeforeRelease: true },
1844
+ { followUp: "was promoted", startedBeforeRelease: false },
1845
+ { followUp: "switched provider instance", startedBeforeRelease: true, switchesInstance: true },
1846
+ { followUp: "has fresh context", startedBeforeRelease: true, freshContext: true },
1847
+])(
1848
+ "Force stop keeps only the session of a queued follow-up that $followUp",
1849
+ ({ startedBeforeRelease, switchesInstance = false, freshContext = false }) =>
1850
+ Effect.scoped(
1851
+ Effect.gen(function* () {
1852
+ const cwd = yield* checkpointWorkspace("thread-stop-force-stop-follow-up");
1853
+ const driver = adapter.driver;
1854
+ const otherInstanceId = ProviderInstanceId.make("codex-other");
1855
+ const eventQueues = new Map<
1856
+ ProviderSessionId,
1857
+ Queue.Queue<ProviderAdapter.ProviderAdapterV2Event>
1858
+ >();
1859
+ const started: Array<ProviderAdapter.ProviderAdapterV2TurnInput> = [];
1860
+ const interrupted: Array<ProviderAdapter.ProviderAdapterV2InterruptInput> = [];
1861
+ let opened = 0;
1862
+ // Fresh context on a one-thread-per-session provider puts the follow-up on its own session.
1863
+ // The session itself reports shared threads so fresh-context cleanup leaves the stopped
1864
+ // run's session to Force stop, as pending background work would.
1865
+ const adapterCapabilities = freshContext
1866
+ ? {
1867
+ ...CodexProviderCapabilitiesV2,
1868
+ sessions: {
1869
+ ...CodexProviderCapabilitiesV2.sessions,
1870
+ supportsMultipleProviderThreadsPerSession: false,
1871
+ },
1872
+ }
1873
+ : CodexProviderCapabilitiesV2;
1874
+ const liveAdapter = (
1875
+ instanceId: ProviderInstanceId,
1876
+ ): ProviderAdapter.ProviderAdapterV2["Service"] => ({
1877
+ ...adapter,
1878
+ instanceId,
1879
+ getCapabilities: () => Effect.succeed(adapterCapabilities),
1880
+ openSession: (input) =>
1881
+ Effect.gen(function* () {
1882
+ opened += 1;
1883
+ const now = yield* DateTime.now;
1884
+ const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
1885
+ eventQueues.set(input.providerSessionId, events);
1886
+ return {
1887
+ instanceId,
1888
+ driver,
1889
+ providerSessionId: input.providerSessionId,
1890
+ providerSession: {
1891
+ id: input.providerSessionId,
1892
+ driver,
1893
+ providerInstanceId: instanceId,
1894
+ status: "ready",
1895
+ cwd,
1896
+ model: modelSelection.model,
1897
+ capabilities: CodexProviderCapabilitiesV2,
1898
+ createdAt: now,
1899
+ updatedAt: now,
1900
+ lastError: null,
1901
+ },
1902
+ events: Stream.fromQueue(events),
1903
+ ensureThread: ({ threadId }) =>
1904
+ Effect.succeed({
1905
+ id: ProviderThreadId.make(
1906
+ `provider-thread:${input.providerSessionId}:${threadId}`,
1907
+ ),
1908
+ driver,
1909
+ providerInstanceId: instanceId,
1910
+ providerSessionId: input.providerSessionId,
1911
+ appThreadId: threadId,
1912
+ ownerNodeId: null,
1913
+ nativeThreadRef: { driver, nativeId: "native-thread", strength: "strong" },
1914
+ nativeConversationHeadRef: null,
1915
+ status: "idle",
1916
+ firstRunOrdinal: null,
1917
+ lastRunOrdinal: null,
1918
+ handoffIds: [],
1919
+ forkedFrom: null,
1920
+ createdAt: now,
1921
+ updatedAt: now,
1922
+ }),
1923
+ resumeThread: ({ providerThread }) => Effect.succeed(providerThread),
1924
+ startTurn: (turn) =>
1925
+ Effect.gen(function* () {
1926
+ started.push(turn);
1927
+ yield* Queue.offer(events, {
1928
+ type: "provider_turn.updated",
1929
+ driver,
1930
+ providerTurn: {
1931
+ id: ProviderTurnId.make(`provider-turn:${turn.attemptId}`),
1932
+ providerThreadId: turn.providerThread.id,
1933
+ nodeId: turn.rootNodeId,
1934
+ runAttemptId: turn.attemptId,
1935
+ nativeTurnRef: {
1936
+ driver,
1937
+ nativeId: `native:${turn.attemptId}`,
1938
+ strength: "strong",
1939
+ },
1940
+ ordinal: turn.providerTurnOrdinal,
1941
+ status: "running",
1942
+ startedAt: now,
1943
+ completedAt: null,
1944
+ },
1945
+ });
1946
+ }),
1947
+ steerTurn: () => Effect.die("unused"),
1948
+ interruptTurn: (interrupt) => Effect.sync(() => interrupted.push(interrupt)),
1949
+ respondToRuntimeRequest: () => Effect.die("unused"),
1950
+ readThreadSnapshot: () => Effect.die("unused"),
1951
+ rollbackThread: () => Effect.die("unused"),
1952
+ forkThread: () => Effect.die("unused"),
1953
+ };
1954
+ }),
1955
+ });
1956
+ // The settlement promotes the follow-up; optionally its turn starts before Force stop releases.
1957
+ const layerSessionsStartingFollowUpFirst = Layer.effect(
1958
+ ProviderSessionManager.ProviderSessionManagerV2,
1959
+ Effect.gen(function* () {
1960
+ const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
1961
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1962
+ return ProviderSessionManager.ProviderSessionManagerV2.of({
1963
+ ...sessions,
1964
+ releaseUnlessBusy: (input) =>
1965
+ (startedBeforeRelease ? worker.drain().pipe(Effect.orDie) : Effect.void).pipe(
1966
+ Effect.andThen(sessions.releaseUnlessBusy(input)),
1967
+ ),
1968
+ });
1969
+ }),
1970
+ );
1971
+ const layerLive = ThreadManagementService.layer.pipe(
1972
+ Layer.provide(layerSessionsStartingFollowUpFirst),
1973
+ Layer.provideMerge(
1974
+ Layer.mergeAll(
1975
+ layerDatabase,
1976
+ ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),
1977
+ EventStore.layer.pipe(Layer.provide(layerDatabase)),
1978
+ EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),
1979
+ ProviderReplayHarness.layerWithRegistry(
1980
+ { name: "thread-stop-force-stop-follow-up" },
1981
+ ProviderAdapterRegistry.layerFromAdapters([
1982
+ liveAdapter(instanceId),
1983
+ liveAdapter(otherInstanceId),
1984
+ ]),
1985
+ { databaseLayer: layerDatabase, runEffectWorker: false },
1986
+ ),
1987
+ ),
1988
+ ),
1989
+ Layer.provideMerge(ServerSettings.layerTest()),
1990
+ Layer.provideMerge(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))),
1991
+ );
1992
+
1993
+ yield* Effect.gen(function* () {
1994
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1995
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1996
+ const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
1997
+ const threads = yield* ThreadManagementService.ThreadManagementService;
1998
+ const threadId = ThreadId.make("thread:force-stop-follow-up");
1999
+ const runningTurn = orchestrator.streamDomainEvents.pipe(
2000
+ Stream.filter(
2001
+ (event) =>
2002
+ event.type === "provider-turn.updated" && event.payload.status === "running",
2003
+ ),
2004
+ Stream.take(1),
2005
+ Stream.runDrain,
2006
+ Effect.forkScoped,
2007
+ );
2008
+ yield* orchestrator.dispatch({
2009
+ type: "thread.create",
2010
+ commandId: CommandId.make("create:force-stop-follow-up"),
2011
+ threadId,
2012
+ projectId: ProjectId.make("project:thread-stop"),
2013
+ title: "Force stop follow-up",
2014
+ modelSelection,
2015
+ runtimeMode: "full-access",
2016
+ interactionMode: "default",
2017
+ branch: null,
2018
+ worktreePath: cwd,
2019
+ createdBy: "user",
2020
+ creationSource: "web",
2021
+ });
2022
+ const firstRunning = yield* runningTurn;
2023
+ yield* send(threadId, "first", "start_immediately");
2024
+ yield* worker.drain();
2025
+ yield* Fiber.join(firstRunning);
2026
+ yield* orchestrator.dispatch({
2027
+ type: "message.dispatch",
2028
+ commandId: CommandId.make("send:force-stop-follow-up"),
2029
+ threadId,
2030
+ messageId: MessageId.make("message:force-stop-follow-up"),
2031
+ text: "follow-up",
2032
+ attachments: [],
2033
+ dispatchMode: { type: "queue_after_active" },
2034
+ ...(switchesInstance
2035
+ ? { modelSelection: { ...modelSelection, instanceId: otherInstanceId } }
2036
+ : {}),
2037
+ ...(freshContext ? { freshContext } : {}),
2038
+ createdBy: "user",
2039
+ creationSource: "web",
2040
+ });
2041
+
2042
+ const result = yield* threads.forceStopThread({ threadId });
2043
+ yield* worker.drain();
2044
+
2045
+ assert.deepEqual(
2046
+ started.map((turn) => turn.runOrdinal),
2047
+ [1, 2],
2048
+ "the queued follow-up did not start its turn",
2049
+ );
2050
+ assert.equal(
2051
+ opened,
2052
+ switchesInstance || freshContext ? 2 : 1,
2053
+ "Force stop tore down the session the follow-up was promoted onto",
2054
+ );
2055
+ const [stopped, followUp] = [started[0]!, started[1]!];
2056
+ const providerSessionId = followUp.providerThread.providerSessionId!;
2057
+ assert.equal(
2058
+ yield* sessions.isLive(stopped.providerThread.providerSessionId!),
2059
+ !switchesInstance && !freshContext,
2060
+ "Force stop kept the stopped run's session for a follow-up on another session",
2061
+ );
2062
+ assert.isFalse(
2063
+ interrupted.some((interrupt) =>
2064
+ String(interrupt.providerTurnId).includes(followUp.attemptId),
2065
+ ),
2066
+ "Force stop of the first run interrupted the follow-up's turn",
2067
+ );
2068
+ assert.isTrue(
2069
+ yield* sessions.isLive(providerSessionId),
2070
+ "Force stop of the first run released the session the follow-up's turn is running on",
2071
+ );
2072
+ assert.deepEqual(
2073
+ result,
2074
+ { outcome: "released", sharedWithThreadIds: [] },
2075
+ "Force stop detached the thread the follow-up runs on as if its session were shared",
2076
+ );
2077
+
2078
+ // The first run's provider never confirms the stop; the follow-up's turn ends normally.
2079
+ yield* Queue.offer(eventQueues.get(providerSessionId)!, {
2080
+ type: "turn.terminal",
2081
+ driver,
2082
+ providerThreadId: followUp.providerThread.id,
2083
+ providerTurnId: ProviderTurnId.make(`provider-turn:${followUp.attemptId}`),
2084
+ runOrdinal: followUp.runOrdinal,
2085
+ status: "completed",
2086
+ failure: null,
2087
+ threadDisposition: "reusable",
2088
+ });
2089
+ yield* worker.drain();
2090
+ yield* TestClock.adjust("31 minutes");
2091
+ assert.isFalse(
2092
+ yield* sessions.isLive(providerSessionId),
2093
+ "the stopped run's unconfirmed turn kept the session from going idle",
2094
+ );
2095
+ }).pipe(Effect.provide(layerLive));
2096
+ }),
2097
+ ),
2098
+);
packages/contracts/src/orchestrationV2.ts
+4
-4
@@ -4180,10 +4180,10 @@ export type OrchestrationV2ForceStopThreadInput = typeof OrchestrationV2ForceSto
4180
4181
export const OrchestrationV2ForceStopThreadResult = Schema.Struct({
4182
/**
4183
- * `killed`: the thread's own agent processes were killed. `released`: its provider session
4184
- * was torn down, which kills the agent's process group. `releasing`: that teardown has not
4185
- * finished yet and continues on the server. `shared`: the agent process also works for
4186
- * `sharedWithThreadIds`, so only this thread's turn was ended.
4183
+ * `killed`: its own agent processes were killed. `released`: its provider session was torn
4184
+ * down (killing the agent's process group) or handed to its newer run. `releasing`: teardown
4185
+ * continues on the server. `shared`: the agent process also works for `sharedWithThreadIds`,
4186
+ * so only this thread's turn was ended.
4187
*/
4188
outcome: Schema.Literals(["killed", "released", "releasing", "shared"]),
4189
sharedWithThreadIds: Schema.Array(ThreadId),