Skip to content

Commit b1c31ee

Browse files
fix(server): a stopped turn start undoes only its own busy mark
The previous fix undid the busy mark on any error from the whole start, including a Stop that landed while re-attaching, before the mark was made. On a session shared by several threads that took another thread's mark, so its running turn could be idle-released. The mark and its undo are now paired with acquireUseRelease. In-flight session scopes close in parallel at shutdown, so one that hangs no longer keeps the others from closing within the time box. The device hub is also stopped if a start is interrupted after the spawn but before the hub is recorded as running. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5beedc5 commit b1c31ee

3 files changed

Lines changed: 133 additions & 14 deletions

File tree

‎apps/server/src/device/LocalDeviceHost.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -595,16 +595,20 @@ export const make = Effect.fn("LocalDeviceHost.make")(function* () {
595595
);
596596
yield* onPhase("starting");
597597
const hub = yield* spawnHub(hubTool, nodePath);
598-
yield* pruneLocalDeviceTools(config.baseDir, nodePath, "hub").pipe(
599-
Effect.provideService(Path.Path, path),
600-
Effect.provideService(ProcessRunner.ProcessRunner, runner),
601-
Effect.ignore,
602-
);
598+
// Until the hub is in runningRef, nothing else stops it.
599+
const [axExists, cliExists] = yield* Effect.gen(function* () {
600+
yield* pruneLocalDeviceTools(config.baseDir, nodePath, "hub").pipe(
601+
Effect.provideService(Path.Path, path),
602+
Effect.provideService(ProcessRunner.ProcessRunner, runner),
603+
Effect.ignore,
604+
);
605+
const candidate = helperPaths(hubTool);
606+
return yield* Effect.all([
607+
fs.exists(candidate.serveSimAxSettings).pipe(Effect.orElseSucceed(() => false)),
608+
fs.exists(candidate.serveSimCli).pipe(Effect.orElseSucceed(() => false)),
609+
]);
610+
}).pipe(Effect.onError(() => stopHub(hub)));
603611
const candidate = helperPaths(hubTool);
604-
const [axExists, cliExists] = yield* Effect.all([
605-
fs.exists(candidate.serveSimAxSettings).pipe(Effect.orElseSucceed(() => false)),
606-
fs.exists(candidate.serveSimCli).pipe(Effect.orElseSucceed(() => false)),
607-
]);
608612
const next: RunningHost = {
609613
hub,
610614
agentDevice: null,

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,110 @@ it.effect("ProviderSessionManagerV2 releases an idle session whose turn start wa
11511151
}),
11521152
);
11531153

1154+
it.effect(
1155+
"ProviderSessionManagerV2 keeps a shared session busy when another thread's start is stopped early",
1156+
() =>
1157+
Effect.gen(function* () {
1158+
const state = yield* Ref.make(emptyState);
1159+
const projectId = ProjectId.make("project-provider-session-manager-shared-stopped-start");
1160+
// Blocks the next project read, so a re-attach can be stopped before its
1161+
// start marks the session busy.
1162+
const holdProjectRead = yield* Ref.make(false);
1163+
const projectReadHeld = yield* Deferred.make<void>();
1164+
const projectServiceLayer = Layer.mock(ProjectService.ProjectService)({
1165+
getById: (requestedProjectId) =>
1166+
Ref.get(holdProjectRead).pipe(
1167+
Effect.flatMap((hold) =>
1168+
hold
1169+
? Deferred.succeed(projectReadHeld, undefined).pipe(Effect.andThen(Effect.never))
1170+
: Effect.succeed(Option.some(makeBrowserAccessProject(requestedProjectId))),
1171+
),
1172+
),
1173+
});
1174+
yield* Effect.gen(function* () {
1175+
const eventSink = yield* EventSink.EventSinkV2;
1176+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1177+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
1178+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1179+
const now = yield* DateTime.now;
1180+
const threadA = ThreadId.make("thread-provider-session-manager-shared-stopped-a");
1181+
const threadB = ThreadId.make("thread-provider-session-manager-shared-stopped-b");
1182+
const providerSessionId = idAllocator.derive.providerSession({
1183+
providerInstanceId: modelSelection.instanceId,
1184+
});
1185+
yield* eventSink.write({
1186+
events: [
1187+
yield* makeThreadCreatedEvent({ idAllocator, threadId: threadA, now, projectId }),
1188+
yield* makeThreadCreatedEvent({ idAllocator, threadId: threadB, now, projectId }),
1189+
],
1190+
});
1191+
const runtime = yield* manager.open({
1192+
threadId: threadA,
1193+
providerSessionId,
1194+
modelSelection,
1195+
runtimePolicy,
1196+
});
1197+
yield* manager.open({
1198+
threadId: threadB,
1199+
providerSessionId,
1200+
modelSelection,
1201+
runtimePolicy,
1202+
});
1203+
const startTurn = (threadId: ThreadId) =>
1204+
Effect.gen(function* () {
1205+
const runId = idAllocator.derive.run({ threadId, ordinal: 1 });
1206+
return yield* runtime.startTurn({
1207+
appThread: (yield* projectionStore.getThreadProjection(threadId)).thread,
1208+
threadId,
1209+
runId,
1210+
runOrdinal: 1,
1211+
providerTurnOrdinal: 1,
1212+
attemptId: idAllocator.derive.runAttempt({ runId, attemptOrdinal: 1 }),
1213+
rootNodeId: idAllocator.derive.rootNode({ runId }),
1214+
providerThread: makeProviderThread({ idAllocator, threadId, providerSessionId, now }),
1215+
message: {
1216+
createdBy: "user",
1217+
creationSource: "web",
1218+
messageId: yield* idAllocator.allocate.message({ threadId, ordinal: 1 }),
1219+
text: "turn",
1220+
attachments: [],
1221+
},
1222+
modelSelection,
1223+
runtimePolicy,
1224+
});
1225+
});
1226+
1227+
// B's turn is accepted and still running.
1228+
yield* startTurn(threadB);
1229+
1230+
// A detaches, and its next start is stopped while re-attaching, before
1231+
// it marks the session busy.
1232+
yield* manager.detach({ providerSessionId, threadId: threadA });
1233+
yield* Ref.set(holdProjectRead, true);
1234+
const startingA = yield* startTurn(threadA).pipe(Effect.forkChild);
1235+
yield* Deferred.await(projectReadHeld);
1236+
yield* Fiber.interrupt(startingA);
1237+
1238+
// B's running turn keeps the session busy past the idle timeout.
1239+
yield* TestClock.adjust("2 seconds");
1240+
yield* Effect.yieldNow;
1241+
assert.isTrue(Option.isSome(yield* manager.get(providerSessionId)));
1242+
assert.equal((yield* Ref.get(state)).closeCount, 0);
1243+
}).pipe(
1244+
Effect.provide(
1245+
makeTestLayer({
1246+
state,
1247+
idleTimeoutMs: 1000,
1248+
projectServiceLayer,
1249+
serverSettingsLayer: ServerSettings.layerTest({
1250+
projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: true } },
1251+
}),
1252+
}),
1253+
),
1254+
);
1255+
}),
1256+
);
1257+
11541258
it.effect("ProviderSessionManagerV2 stops a session still opening when its layer shuts down", () =>
11551259
Effect.gen(function* () {
11561260
const state = yield* Ref.make(emptyState);

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,11 +1518,20 @@ export const layerWithOptions = (
15181518
providerInstanceId: runtime.instanceId,
15191519
}),
15201520
).pipe(
1521-
Effect.andThen(observeActivity(providerSessionId, markBusy(providerSessionId))),
1522-
Effect.andThen(runtime.startTurn(input)),
15231521
// A start that fails or is stopped never emits turn.terminal, so
1524-
// the busy mark is undone here or the session never goes idle.
1525-
Effect.onError(() => observeActivity(providerSessionId, markIdle(providerSessionId))),
1522+
// it undoes its own busy mark or the session never goes idle.
1523+
// Only a mark it made: on a session shared by several threads,
1524+
// undoing another thread's would idle-release its running turn.
1525+
Effect.andThen(
1526+
Effect.acquireUseRelease(
1527+
observeActivity(providerSessionId, markBusy(providerSessionId)),
1528+
() => runtime.startTurn(input),
1529+
(_, exit) =>
1530+
Exit.isFailure(exit)
1531+
? observeActivity(providerSessionId, markIdle(providerSessionId))
1532+
: Effect.void,
1533+
),
1534+
),
15261535
),
15271536
steerTurn: (input) =>
15281537
observeActivity(providerSessionId, touchActivity(providerSessionId)).pipe(
@@ -1672,7 +1681,9 @@ export const layerWithOptions = (
16721681
// Parent of every session scope. On layer close, shutdown releases the
16731682
// live sessions first, then closes any session whose open is still in
16741683
// flight, time-boxed so a stuck adapter cannot hold up server shutdown.
1675-
const sessionScopes = yield* Scope.make();
1684+
// Parallel, so one session whose close hangs does not stop the rest from
1685+
// closing within the time box.
1686+
const sessionScopes = yield* Scope.make("parallel");
16761687
const shutdown = Effect.gen(function* () {
16771688
const activeSessions = [...(yield* Ref.get(sessions)).values()];
16781689
yield* Effect.forEach(

0 commit comments

Comments
 (0)