Skip to content

Commit 2e0bd97

Browse files
fix(server): Force stop no longer tears down a queued follow-up that started during cleanup (#31038)
1 parent 7556047 commit 2e0bd97

5 files changed

Lines changed: 432 additions & 46 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,73 @@ it.effect("ProviderSessionManagerV2 gives up on a provider that never confirms a
16261626
}),
16271627
);
16281628

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+
16291696
it.effect(
16301697
"ProviderSessionManagerV2 keeps a shared session a sibling is busy on when releasing",
16311698
() =>
@@ -1692,6 +1759,7 @@ it.effect(
16921759
manager.releaseUnlessBusy({
16931760
providerSessionId,
16941761
ownProviderThreadIds: [providerThreadOf(own).id],
1762+
throughRunOrdinal: 1,
16951763
reason: "manual_shutdown",
16961764
});
16971765

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

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ export const PROVIDER_INTERRUPT_TIMEOUT_MS = 15 * 1000;
7171
const busyTurnPrefix = (providerThreadId: ProviderThreadId) => `${providerThreadId}#`;
7272
const busyTurnKey = (providerThreadId: ProviderThreadId, runOrdinal: number) =>
7373
`${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+
});
7485
const UNLOAD_THREAD_TIMEOUT_MS = 10 * 1000;
7586
// Providers can announce a retry and never resume. Longer than any adapter's own stall handling.
7687
const PROVIDER_RETRY_STALL_TIMEOUT = Duration.minutes(15);
@@ -243,13 +254,16 @@ export interface ProviderSessionManagerV2Shape {
243254
readonly revokeMcpCredential?: boolean;
244255
}) => Effect.Effect<void, ProviderSessionManagerV2Error>;
245256
/**
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.
249260
*/
250261
readonly releaseUnlessBusy: (input: {
251262
readonly providerSessionId: ProviderSessionId;
252263
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>;
253267
readonly reason: ProviderSessionReleaseReason;
254268
readonly detail?: string;
255269
}) => Effect.Effect<boolean, ProviderSessionManagerV2Error>;
@@ -1125,48 +1139,56 @@ export const layerWithOptions = (
11251139
function* (input: {
11261140
readonly providerSessionId: ProviderSessionId;
11271141
readonly onlyIfIdleGeneration?: number;
1128-
readonly keepIfBusyOtherThan?: ReadonlyArray<ProviderThreadId>;
1142+
readonly keepIfBusyOtherThan?: OwnBusyTurns;
11291143
readonly keepIf?: Effect.Effect<boolean>;
11301144
}): Effect.fn.Return<readonly [Option.Option<LiveSessionEntry>, DateTime.Utc]> {
11311145
const key = sessionKey(input.providerSessionId);
11321146
const candidate = (yield* Ref.get(sessions)).get(key);
11331147
if (candidate === undefined) {
11341148
return [Option.none<LiveSessionEntry>(), yield* DateTime.now] as const;
11351149
}
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),
11631191
);
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));
11701192
if (outcome === "changed") return yield* removeLiveEntry(input);
11711193
return [
11721194
outcome === "removed" ? Option.some(candidate) : Option.none<LiveSessionEntry>(),
@@ -1218,7 +1240,7 @@ export const layerWithOptions = (
12181240
readonly detachedThreadId?: ThreadId;
12191241
readonly cancelIdleFiber?: boolean;
12201242
readonly onlyIfIdleGeneration?: number;
1221-
readonly keepIfBusyOtherThan?: ReadonlyArray<ProviderThreadId>;
1243+
readonly keepIfBusyOtherThan?: OwnBusyTurns;
12221244
readonly keepIf?: Effect.Effect<boolean>;
12231245
readonly gracefulSubscribers?: boolean;
12241246
readonly eventStreamCause?: Cause.Cause<ProviderAdapter.ProviderAdapterV2Error>;
@@ -3076,10 +3098,19 @@ export const layerWithOptions = (
30763098
providerSessionId: input.providerSessionId,
30773099
reason: input.reason,
30783100
...(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 }),
30803106
});
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;
30833114
}),
30843115
otherWorkingThreads: Effect.fn("ProviderSessionManager.otherWorkingThreads")(
30853116
function* (input) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,28 @@ const make = Effect.gen(function* () {
13031303
const ownProviderThreadIds = records.providerThreads.flatMap((providerThread) =>
13041304
providerThread.appThreadId === input.threadId ? [providerThread.id] : [],
13051305
);
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+
);
13061328
const detach = (providerSessionId: ProviderSessionId) =>
13071329
sessions
13081330
.detach({ providerSessionId, threadId: input.threadId, detail: FORCE_STOP_REASON })
@@ -1340,6 +1362,9 @@ const make = Effect.gen(function* () {
13401362
.releaseUnlessBusy({
13411363
providerSessionId,
13421364
ownProviderThreadIds,
1365+
throughRunOrdinal,
1366+
// A promoted follow-up may not have marked its turn busy yet.
1367+
keepIf: laterRunActive(providerSessionId),
13431368
reason: "manual_shutdown",
13441369
detail: FORCE_STOP_REASON,
13451370
})
@@ -1354,7 +1379,7 @@ const make = Effect.gen(function* () {
13541379
);
13551380
if (Option.isNone(released)) {
13561381
releasing = true;
1357-
} else if (!released.value) {
1382+
} else if (!released.value && !(yield* laterRunActive(release.providerSessionId))) {
13581383
shared = true;
13591384
yield* detach(release.providerSessionId);
13601385
}

0 commit comments

Comments
 (0)