Skip to content

Commit dfb8edf

Browse files
fix(server): a session Force stop keeps still idles out
AI-assisted; verified with the tests and tools listed above.
1 parent 553bd61 commit dfb8edf

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,73 @@ it.effect("ProviderSessionManagerV2 gives up on a provider that never confirms a
15241524
}),
15251525
);
15261526

1527+
it.effect("ProviderSessionManagerV2 still idles out a session releaseUnlessBusy kept", () =>
1528+
Effect.gen(function* () {
1529+
const state = yield* Ref.make(emptyState);
1530+
yield* Effect.gen(function* () {
1531+
const eventSink = yield* EventSink.EventSinkV2;
1532+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1533+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
1534+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1535+
const now = yield* DateTime.now;
1536+
const threadId = ThreadId.make("thread-provider-session-manager-kept-idles");
1537+
const providerSessionId = idAllocator.derive.providerSession({
1538+
providerInstanceId: modelSelection.instanceId,
1539+
});
1540+
yield* eventSink.write({
1541+
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
1542+
});
1543+
const runtime = yield* manager.open({
1544+
threadId,
1545+
providerSessionId,
1546+
modelSelection,
1547+
runtimePolicy,
1548+
});
1549+
const providerThread = makeProviderThread({
1550+
idAllocator,
1551+
threadId,
1552+
providerSessionId,
1553+
now,
1554+
nativeThreadId: `native-${threadId}`,
1555+
});
1556+
const runId = idAllocator.derive.run({ threadId, ordinal: 1 });
1557+
yield* runtime.startTurn({
1558+
appThread: (yield* projectionStore.getThreadProjection(threadId)).thread,
1559+
threadId,
1560+
runId,
1561+
runOrdinal: 1,
1562+
providerTurnOrdinal: 1,
1563+
attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }),
1564+
rootNodeId: idAllocator.derive.rootNode({ runId }),
1565+
providerThread,
1566+
message: {
1567+
createdBy: "user",
1568+
creationSource: "web",
1569+
messageId: yield* idAllocator.allocate.message({ threadId, ordinal: 1 }),
1570+
text: "turn",
1571+
attachments: [],
1572+
},
1573+
modelSelection,
1574+
runtimePolicy,
1575+
});
1576+
1577+
assert.isFalse(
1578+
yield* manager.releaseUnlessBusy({
1579+
providerSessionId,
1580+
ownProviderThreadIds: [providerThread.id],
1581+
throughRunOrdinal: 1,
1582+
keepIf: Effect.succeed(true),
1583+
reason: "manual_shutdown",
1584+
}),
1585+
);
1586+
assert.isTrue(yield* manager.isLive(providerSessionId));
1587+
// The stopped run 1 never reports its end.
1588+
yield* TestClock.adjust("2 seconds");
1589+
assert.isFalse(yield* manager.isLive(providerSessionId));
1590+
}).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1_000 })));
1591+
}),
1592+
);
1593+
15271594
it.effect(
15281595
"ProviderSessionManagerV2 keeps a shared session a sibling is busy on when releasing",
15291596
() =>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,9 +2676,11 @@ export const layerWithOptions = (
26762676
});
26772677
// Scope, not presence: open can install a replacement under the same id meanwhile, and
26782678
// a kept entry is rewritten with the same scope.
2679-
return (
2680-
before !== undefined && (yield* Ref.get(sessions)).get(key)?.scope !== before.scope
2681-
);
2679+
const released =
2680+
before !== undefined && (yield* Ref.get(sessions)).get(key)?.scope !== before.scope;
2681+
// The stopped turn's markBusy cancelled the idle timer and its markIdle may never come.
2682+
if (!released) yield* scheduleIdleReleaseInternal(input.providerSessionId);
2683+
return released;
26822684
}),
26832685
otherWorkingThreads: Effect.fn("ProviderSessionManager.otherWorkingThreads")(
26842686
function* (input) {

0 commit comments

Comments
 (0)