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, 21:00 UTC
fix(server): fresh-session schedules start while an old session has background work (#34811)
Julius Marminge committed
Nov 11, 2026 at 23:14 UTC
2ed6f109f4bba917d12b052988dc581af337c7b0
2 files changed
+101
-2
apps/server/src/orchestration-v2/Orchestrator.ts
+6
-2
@@ -7298,7 +7298,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7298
isProviderSwitch &&
7299
activeProviderThread.nativeThreadRef !== null &&
7300
providerSwitchPlan?.transition.type === "restart_and_resume";
7301
- const sessionsToDetach = uniqueSessions([
7301
+ const releasableSessions = uniqueSessions([
7302
...projection.providerSessions.filter(
7303
(session) =>
7304
providerSwitchPlan?.releaseProviderSessionIds.includes(session.id) &&
@@ -7307,8 +7307,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7307
),
7308
...(freshContext ? sessionsReleasedByFreshContext(projection, activeProviderThread) : []),
7309
]);
7310
- for (const detaching of sessionsToDetach) {
7310
+ const sessionsToDetach: Array<OrchestrationV2ProviderSession> = [];
7311
+ for (const detaching of releasableSessions) {
7312
if (yield* sessionHasBackgroundWork(detaching, projection.providerThreads)) {
7313
+ // A schedule has nobody to wait or press Stop; like a queued fresh start, leave it running.
7314
+ if (freshContext && command.scheduledTaskId !== undefined) continue;
7315
return yield* new OrchestratorDispatchError({
7316
commandId: command.commandId,
7317
commandType: command.type,
@@ -7317,6 +7320,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7320
: "The previous provider is still running background agents or commands, and switching would end them. Wait for them to finish, or press Stop, then send the message again.",
7321
});
7322
}
7323
+ sessionsToDetach.push(detaching);
7324
}
7325
7326
if (
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+95
@@ -18,6 +18,7 @@ import {
18
ProviderSessionId,
19
ProviderThreadId,
20
ProviderTurnId,
21
+ ScheduledTaskId,
22
ThreadId,
23
TurnItemId,
24
ProviderDriverKind,
@@ -2973,6 +2974,100 @@ describe("orchestration v2 provider switching", () => {
2974
),
2975
);
2976
2977
+ it.live(
2978
+ "starts a scheduled fresh run while the previous run's session still has background work",
2979
+ () =>
2980
+ Effect.scoped(
2981
+ Effect.gen(function* () {
2982
+ const cwd = yield* checkpointWorkspace("scheduled-fresh-background");
2983
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
2984
+ const closedSessions = yield* Ref.make<ReadonlyArray<string>>([]);
2985
+ const backgroundWork = yield* Ref.make(false);
2986
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
2987
+ makeTestAdapter({
2988
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
2989
+ driver: CLAUDE_DRIVER,
2990
+ capabilities: ClaudeProviderCapabilitiesV2,
2991
+ modelSelection: CLAUDE_MODEL_SELECTION,
2992
+ responseByRunOrdinal: {},
2993
+ capturedTurns,
2994
+ closedSessions,
2995
+ backgroundWork,
2996
+ nativeThreadGeneration: yield* Ref.make(0),
2997
+ }),
2998
+ ]);
2999
+ const threadId = ThreadId.make("thread:scheduled-fresh-background");
3000
+ yield* Effect.gen(function* () {
3001
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
3002
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3003
+ // The command ScheduledTaskService sends for a fresh-session schedule.
3004
+ const fire = (name: string) =>
3005
+ orchestrator.dispatch({
3006
+ type: "message.dispatch",
3007
+ createdBy: "user",
3008
+ creationSource: "web",
3009
+ commandId: CommandId.make(`scheduled-task:scheduled-fresh-background:${name}`),
3010
+ threadId,
3011
+ messageId: MessageId.make(`message:scheduled-fresh-background:${name}`),
3012
+ scheduledTaskId: ScheduledTaskId.make("scheduled-fresh-background"),
3013
+ text: `Scheduled ${name}`,
3014
+ attachments: [],
3015
+ modelSelection: CLAUDE_MODEL_SELECTION,
3016
+ freshContext: true,
3017
+ dispatchMode: { type: "queue_after_active" },
3018
+ });
3019
+ yield* orchestrator.dispatch({
3020
+ type: "thread.create",
3021
+ createdBy: "user",
3022
+ creationSource: "web",
3023
+ commandId: CommandId.make("command:scheduled-fresh-background:create"),
3024
+ threadId,
3025
+ projectId: ProjectId.make("project:scheduled-fresh-background"),
3026
+ title: "Scheduled fresh session",
3027
+ modelSelection: CLAUDE_MODEL_SELECTION,
3028
+ runtimeMode: "full-access",
3029
+ interactionMode: "default",
3030
+ branch: null,
3031
+ worktreePath: cwd,
3032
+ });
3033
+ yield* fire("first");
3034
+ yield* worker.drain();
3035
+ yield* waitForIdle(threadId);
3036
+ // The first run left a background command running after its turn ended.
3037
+ yield* Ref.set(backgroundWork, true);
3038
+ yield* fire("second");
3039
+ yield* worker.drain();
3040
+ yield* waitForIdle(threadId);
3041
+ const turns = yield* Ref.get(capturedTurns);
3042
+ const turnFor = (name: string) =>
3043
+ turns.find((turn) => turn.text.endsWith(`Scheduled ${name}`));
3044
+ assert.isDefined(turnFor("second"));
3045
+ assert.notEqual(turnFor("second")!.nativeThreadId, turnFor("first")!.nativeThreadId);
3046
+ // Closing the busy session would end its background work.
3047
+ assert.deepEqual(yield* Ref.get(closedSessions), []);
3048
+ }).pipe(
3049
+ Effect.provide(
3050
+ ProviderReplayHarness.layerWithRegistry(
3051
+ {
3052
+ name: "scheduled-fresh-background",
3053
+ runtimePolicyOverride: {
3054
+ cwd,
3055
+ approvalPolicy: "never",
3056
+ sandboxPolicy: {
3057
+ type: "readOnly",
3058
+ access: { type: "fullAccess" },
3059
+ networkAccess: false,
3060
+ },
3061
+ },
3062
+ },
3063
+ layerRegistry,
3064
+ ),
3065
+ ),
3066
+ );
3067
+ }),
3068
+ ),
3069
+ );
3070
+
3071
it.live("keeps the thread a queued fresh run reserved for that run", () =>
3072
Effect.scoped(
3073
Effect.gen(function* () {