Skip to content

Commit 2ed6f10

Browse files
fix(server): fresh-session schedules start while an old session has background work (pingdotgg#34811)
1 parent c833a60 commit 2ed6f10

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7298,7 +7298,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
72987298
isProviderSwitch &&
72997299
activeProviderThread.nativeThreadRef !== null &&
73007300
providerSwitchPlan?.transition.type === "restart_and_resume";
7301-
const sessionsToDetach = uniqueSessions([
7301+
const releasableSessions = uniqueSessions([
73027302
...projection.providerSessions.filter(
73037303
(session) =>
73047304
providerSwitchPlan?.releaseProviderSessionIds.includes(session.id) &&
@@ -7307,8 +7307,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
73077307
),
73087308
...(freshContext ? sessionsReleasedByFreshContext(projection, activeProviderThread) : []),
73097309
]);
7310-
for (const detaching of sessionsToDetach) {
7310+
const sessionsToDetach: Array<OrchestrationV2ProviderSession> = [];
7311+
for (const detaching of releasableSessions) {
73117312
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;
73127315
return yield* new OrchestratorDispatchError({
73137316
commandId: command.commandId,
73147317
commandType: command.type,
@@ -7317,6 +7320,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
73177320
: "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.",
73187321
});
73197322
}
7323+
sessionsToDetach.push(detaching);
73207324
}
73217325

73227326
if (

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ProviderSessionId,
1919
ProviderThreadId,
2020
ProviderTurnId,
21+
ScheduledTaskId,
2122
ThreadId,
2223
TurnItemId,
2324
ProviderDriverKind,
@@ -2973,6 +2974,100 @@ describe("orchestration v2 provider switching", () => {
29732974
),
29742975
);
29752976

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+
29763071
it.live("keeps the thread a queued fresh run reserved for that run", () =>
29773072
Effect.scoped(
29783073
Effect.gen(function* () {

0 commit comments

Comments
 (0)