Skip to content

Commit 0045120

Browse files
committed
fix(server): gate runtime-mode replacement on the live provider session
A blocked Claude replacement stamped the requested runtime mode onto the orchestration session before startSession applied it. The next turn then treated that record as current and sent on the previous permission callback while background work was still live. Compare the provider session mode, keep that mode until replacement succeeds, and reject instead of reusing the process.
1 parent 64f067e commit 0045120

2 files changed

Lines changed: 233 additions & 14 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,6 +3511,201 @@ describe("ProviderCommandReactor", () => {
35113511
expect(recoveredThread?.session?.runtimeMode).toBe("approval-required");
35123512
});
35133513

3514+
it("does not send a turn when a blocked runtime-mode replacement leaves the live session in place", async () => {
3515+
let blockRuntimeModeReplacement = false;
3516+
const harness = await createHarness({
3517+
startSessionEffect: (session) => {
3518+
if (blockRuntimeModeReplacement && session.runtimeMode === "approval-required") {
3519+
return Effect.fail(
3520+
new ProviderAdapterRequestError({
3521+
provider: session.provider,
3522+
method: "startSession",
3523+
detail:
3524+
"Claude session replacement is blocked: 1 live task(s) are still running. Retry after background work finishes.",
3525+
}),
3526+
);
3527+
}
3528+
return Effect.succeed(session);
3529+
},
3530+
});
3531+
const now = "2026-01-01T00:00:00.000Z";
3532+
const threadId = ThreadId.make("thread-1");
3533+
3534+
await harness.runEffect(
3535+
harness.engine.dispatch({
3536+
type: "thread.runtime-mode.set",
3537+
commandId: CommandId.make("cmd-runtime-mode-set-blocked-full-access"),
3538+
threadId,
3539+
runtimeMode: "full-access",
3540+
createdAt: now,
3541+
}),
3542+
);
3543+
3544+
await harness.runEffect(
3545+
harness.engine.dispatch({
3546+
type: "thread.turn.start",
3547+
commandId: CommandId.make("cmd-turn-start-runtime-mode-blocked-1"),
3548+
threadId,
3549+
message: {
3550+
messageId: asMessageId("user-message-runtime-mode-blocked-1"),
3551+
role: "user",
3552+
text: "first",
3553+
attachments: [],
3554+
},
3555+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
3556+
runtimeMode: "full-access",
3557+
createdAt: now,
3558+
}),
3559+
);
3560+
3561+
await waitFor(() => harness.startSession.mock.calls.length === 1);
3562+
await waitFor(() => harness.sendTurn.mock.calls.length === 1);
3563+
expect(harness.runtimeSessions[0]?.runtimeMode).toBe("full-access");
3564+
3565+
blockRuntimeModeReplacement = true;
3566+
3567+
await harness.runEffect(
3568+
harness.engine.dispatch({
3569+
type: "thread.runtime-mode.set",
3570+
commandId: CommandId.make("cmd-runtime-mode-set-blocked-approval-required"),
3571+
threadId,
3572+
runtimeMode: "approval-required",
3573+
createdAt: now,
3574+
}),
3575+
);
3576+
await waitFor(() => harness.startSession.mock.calls.length === 2);
3577+
await harness.drain();
3578+
3579+
expect(harness.sendTurn.mock.calls.length).toBe(1);
3580+
expect(harness.runtimeSessions).toHaveLength(1);
3581+
expect(harness.runtimeSessions[0]?.runtimeMode).toBe("full-access");
3582+
3583+
await harness.runEffect(
3584+
harness.engine.dispatch({
3585+
type: "thread.turn.start",
3586+
commandId: CommandId.make("cmd-turn-start-runtime-mode-blocked-2"),
3587+
threadId,
3588+
message: {
3589+
messageId: asMessageId("user-message-runtime-mode-blocked-2"),
3590+
role: "user",
3591+
text: "second",
3592+
attachments: [],
3593+
},
3594+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
3595+
runtimeMode: "approval-required",
3596+
createdAt: now,
3597+
}),
3598+
);
3599+
3600+
await waitFor(async () => {
3601+
const readModel = await harness.readModel();
3602+
const thread = readModel.threads.find((entry) => entry.id === threadId);
3603+
return (
3604+
thread?.activities.some((activity) => activity.kind === "provider.turn.start.failed") ??
3605+
false
3606+
);
3607+
});
3608+
3609+
expect(harness.startSession.mock.calls.length).toBe(3);
3610+
expect(harness.sendTurn.mock.calls.length).toBe(1);
3611+
expect(harness.runtimeSessions[0]?.runtimeMode).toBe("full-access");
3612+
3613+
const poisoned = await harness.readModel();
3614+
const poisonedThread = poisoned.threads.find((entry) => entry.id === threadId);
3615+
expect(poisonedThread?.runtimeMode).toBe("approval-required");
3616+
expect(poisonedThread?.session?.runtimeMode).toBe("full-access");
3617+
3618+
harness.threadBackgroundLiveness.recordTaskLiveness({
3619+
threadId,
3620+
taskId: "task-live",
3621+
taskType: "local_agent",
3622+
status: "in_progress",
3623+
kind: "started",
3624+
});
3625+
3626+
await harness.runEffect(
3627+
harness.engine.dispatch({
3628+
type: "thread.turn.start",
3629+
commandId: CommandId.make("cmd-turn-start-runtime-mode-blocked-3"),
3630+
threadId,
3631+
message: {
3632+
messageId: asMessageId("user-message-runtime-mode-blocked-3"),
3633+
role: "user",
3634+
text: "third",
3635+
attachments: [],
3636+
},
3637+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
3638+
runtimeMode: "approval-required",
3639+
createdAt: now,
3640+
}),
3641+
);
3642+
3643+
await waitFor(async () => {
3644+
const readModel = await harness.readModel();
3645+
const thread = readModel.threads.find((entry) => entry.id === threadId);
3646+
return (
3647+
thread?.activities.filter((activity) => activity.kind === "provider.turn.start.failed")
3648+
.length === 2
3649+
);
3650+
});
3651+
3652+
expect(harness.startSession.mock.calls.length).toBe(3);
3653+
expect(harness.sendTurn.mock.calls.length).toBe(1);
3654+
expect(harness.stopSession.mock.calls.length).toBe(0);
3655+
3656+
const blocked = await harness.readModel();
3657+
const blockedThread = blocked.threads.find((entry) => entry.id === threadId);
3658+
expect(blockedThread?.activities).toEqual(
3659+
expect.arrayContaining([
3660+
expect.objectContaining({
3661+
kind: "provider.turn.start.failed",
3662+
payload: expect.objectContaining({
3663+
detail: expect.stringContaining("cannot apply runtime mode 'approval-required'"),
3664+
}),
3665+
}),
3666+
]),
3667+
);
3668+
expect(blockedThread?.session?.runtimeMode).toBe("full-access");
3669+
expect(harness.runtimeSessions[0]?.runtimeMode).toBe("full-access");
3670+
3671+
blockRuntimeModeReplacement = false;
3672+
harness.threadBackgroundLiveness.recordTaskLiveness({
3673+
threadId,
3674+
taskId: "task-live",
3675+
taskType: "local_agent",
3676+
status: "completed",
3677+
kind: "completed",
3678+
});
3679+
3680+
await harness.runEffect(
3681+
harness.engine.dispatch({
3682+
type: "thread.turn.start",
3683+
commandId: CommandId.make("cmd-turn-start-runtime-mode-blocked-4"),
3684+
threadId,
3685+
message: {
3686+
messageId: asMessageId("user-message-runtime-mode-blocked-4"),
3687+
role: "user",
3688+
text: "fourth",
3689+
attachments: [],
3690+
},
3691+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
3692+
runtimeMode: "approval-required",
3693+
createdAt: now,
3694+
}),
3695+
);
3696+
3697+
await waitFor(() => harness.startSession.mock.calls.length === 4);
3698+
await waitFor(() => harness.sendTurn.mock.calls.length === 2);
3699+
expect(harness.startSession.mock.calls[3]?.[1]).toMatchObject({
3700+
threadId,
3701+
runtimeMode: "approval-required",
3702+
});
3703+
3704+
const recovered = await harness.readModel();
3705+
const recoveredThread = recovered.threads.find((entry) => entry.id === threadId);
3706+
expect(recoveredThread?.session?.runtimeMode).toBe("approval-required");
3707+
});
3708+
35143709
it("reuses a live claude session when provider options change during background work", async () => {
35153710
const harness = await createHarness({
35163711
threadModelSelection: {

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ export function providerErrorLabelFromInstanceHint(input: {
138138

139139
type ThreadBackgroundLivenessState = "working" | "monitoring" | null | undefined;
140140

141+
/**
142+
* Mode the live provider process was started with.
143+
* Pending turn startup rewrites the orchestration session to the requested mode
144+
* before startSession applies it, so that record cannot gate permissions.
145+
*/
146+
function appliedLiveRuntimeMode(input: {
147+
readonly providerRuntimeMode: RuntimeMode | undefined;
148+
readonly sessionRuntimeMode: RuntimeMode | undefined;
149+
}): RuntimeMode | undefined {
150+
return input.providerRuntimeMode ?? input.sessionRuntimeMode;
151+
}
152+
141153
/** Reject a runtime-mode replacement while projected background work is still live. */
142154
function shouldRejectRuntimeModeReplacementWhileLive(input: {
143155
readonly activeRuntimeMode: RuntimeMode | undefined;
@@ -695,19 +707,17 @@ const make = Effect.gen(function* () {
695707
});
696708
}
697709
const preferredProvider: ProviderDriverKind = desiredDriverKind;
698-
if (
699-
shouldRejectRuntimeModeReplacementWhileLive({
700-
activeRuntimeMode: activeThreadSession?.runtimeMode,
701-
desiredRuntimeMode,
702-
backgroundLiveness: thread.backgroundLiveness,
703-
})
704-
) {
710+
const appliedRuntimeMode = appliedLiveRuntimeMode({
711+
providerRuntimeMode: activeSession?.runtimeMode,
712+
sessionRuntimeMode: activeThreadSession?.runtimeMode,
713+
});
714+
const rejectRuntimeModeReplacementWhileLive = Effect.fnUntraced(function* () {
705715
yield* Effect.logWarning(
706716
"provider command reactor rejecting runtime-mode replacement while background work is live",
707717
{
708718
threadId,
709719
backgroundLiveness: thread.backgroundLiveness,
710-
currentRuntimeMode: activeThreadSession?.runtimeMode,
720+
currentRuntimeMode: appliedRuntimeMode,
711721
desiredRuntimeMode,
712722
},
713723
);
@@ -716,6 +726,15 @@ const make = Effect.gen(function* () {
716726
method: "thread.turn.start",
717727
detail: `Thread '${threadId}' cannot apply runtime mode '${desiredRuntimeMode}' while background work is live. Wait for background tasks to finish, then retry.`,
718728
});
729+
});
730+
if (
731+
shouldRejectRuntimeModeReplacementWhileLive({
732+
activeRuntimeMode: appliedRuntimeMode,
733+
desiredRuntimeMode,
734+
backgroundLiveness: thread.backgroundLiveness,
735+
})
736+
) {
737+
return yield* rejectRuntimeModeReplacementWhileLive();
719738
}
720739
if (options?.pendingTurnStart === true && thread.session?.status !== "running") {
721740
yield* setThreadSession({
@@ -725,7 +744,9 @@ const make = Effect.gen(function* () {
725744
status: "starting",
726745
providerName: activeSession?.provider ?? preferredProvider,
727746
providerInstanceId: activeSession?.providerInstanceId ?? desiredInstanceId,
728-
runtimeMode: desiredRuntimeMode,
747+
// Keep the mode the live process actually has. Writing desiredRuntimeMode
748+
// here makes the next turn look unchanged after a blocked replacement.
749+
runtimeMode: activeSession?.runtimeMode ?? desiredRuntimeMode,
729750
activeTurnId: null,
730751
lastError: null,
731752
updatedAt: createdAt,
@@ -830,7 +851,7 @@ const make = Effect.gen(function* () {
830851
const existingSessionThreadId =
831852
thread.session && thread.session.status !== "stopped" && activeSession ? thread.id : null;
832853
if (existingSessionThreadId) {
833-
const runtimeModeChanged = thread.runtimeMode !== thread.session?.runtimeMode;
854+
const runtimeModeChanged = appliedRuntimeMode !== desiredRuntimeMode;
834855
const cwdChanged = effectiveCwd !== activeSession?.cwd;
835856
const sessionModelSwitch = (yield* providerService.getCapabilities(desiredInstanceId))
836857
.sessionModelSwitch;
@@ -864,10 +885,13 @@ const make = Effect.gen(function* () {
864885
return { threadId: existingSessionThreadId, appliedRequestedSelection: true };
865886
}
866887

867-
// Runtime-mode changes are rejected above: sendTurn keeps the
868-
// startSession permission callback, so a stale bypassPermissions
869-
// session must not be treated as ready.
888+
// sendTurn keeps the startSession permission callback. Never hand back
889+
// the live process when the requested runtime mode is not the one it
890+
// was started with, even if the orchestration session record matches.
870891
if (shouldDeferProviderSessionRestartWhileLive(thread.backgroundLiveness)) {
892+
if (runtimeModeChanged) {
893+
return yield* rejectRuntimeModeReplacementWhileLive();
894+
}
871895
yield* Effect.logWarning(
872896
"provider command reactor deferring provider session restart while background work is live",
873897
{
@@ -894,7 +918,7 @@ const make = Effect.gen(function* () {
894918
currentInstanceId,
895919
desiredInstanceId,
896920
desiredProvider: desiredModelSelection.instanceId,
897-
currentRuntimeMode: thread.session?.runtimeMode,
921+
currentRuntimeMode: appliedRuntimeMode,
898922
desiredRuntimeMode: thread.runtimeMode,
899923
runtimeModeChanged,
900924
previousCwd: activeSession?.cwd,

0 commit comments

Comments
 (0)