Skip to content

Commit ba7d4ae

Browse files
committed
fix(server): Stop reaches background work behind queued messages
1 parent e13cf9e commit ba7d4ae

2 files changed

Lines changed: 84 additions & 9 deletions

File tree

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

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const modelSelection = { instanceId, model: "test-model" };
4141
// Codex turns leave commands running, then the thread moves to another
4242
// provider thread (a provider switch). Stop on the newer, settled run must
4343
// reach both provider threads and end all of the Codex work.
44-
const stopEarlierBackgroundWork = (failedStart: boolean) =>
44+
const stopEarlierBackgroundWork = (
45+
failedStart: boolean,
46+
stopWithQueue?: "thread.stop" | "run.interrupt",
47+
) =>
4548
Effect.scoped(
4649
Effect.gen(function* () {
4750
const cwd = yield* checkpointWorkspace("background-work-stop");
@@ -440,12 +443,73 @@ const stopEarlierBackgroundWork = (failedStart: boolean) =>
440443
});
441444
}
442445

443-
yield* orchestrator.dispatch({
444-
type: "run.interrupt",
445-
commandId: CommandId.make("stop-background-work"),
446-
threadId,
447-
runId: failedStart ? failedRun.runId : latestRun.runId,
448-
});
446+
if (stopWithQueue !== undefined) {
447+
const owner = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
448+
(run) => run.id === latestRun.runId,
449+
)!;
450+
// A message queues while checkpointing finishes, then the user holds the queue.
451+
yield* sink.write({
452+
events: [
453+
{
454+
id: EventId.make("latest-run-waiting"),
455+
type: "run.updated",
456+
threadId,
457+
runId: owner.id,
458+
occurredAt: now,
459+
payload: { ...owner, status: "waiting", completedAt: null },
460+
},
461+
],
462+
});
463+
yield* orchestrator.dispatch({
464+
type: "message.dispatch",
465+
commandId: CommandId.make("queue-follow-up"),
466+
threadId,
467+
messageId: MessageId.make("message:queue-follow-up"),
468+
text: "Follow up after the background work",
469+
attachments: [],
470+
dispatchMode: { type: "queue_after_active" },
471+
createdBy: "user",
472+
creationSource: "web",
473+
});
474+
const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)!;
475+
assert.equal(queued.status, "queued");
476+
yield* sink.write({
477+
events: [
478+
{
479+
id: EventId.make("queue-held"),
480+
type: "run.updated",
481+
threadId,
482+
runId: queued.id,
483+
occurredAt: now,
484+
payload: { ...queued, queueHeld: true },
485+
},
486+
{
487+
id: EventId.make("latest-run-settled"),
488+
type: "run.updated",
489+
threadId,
490+
runId: owner.id,
491+
occurredAt: now,
492+
payload: owner,
493+
},
494+
],
495+
});
496+
}
497+
498+
yield* orchestrator.dispatch(
499+
stopWithQueue === "thread.stop"
500+
? {
501+
type: "thread.stop",
502+
commandId: CommandId.make("stop-background-work"),
503+
threadId,
504+
}
505+
: {
506+
type: "run.interrupt",
507+
commandId: CommandId.make("stop-background-work"),
508+
threadId,
509+
runId: failedStart ? failedRun.runId : latestRun.runId,
510+
...(stopWithQueue === undefined ? {} : { holdQueue: true }),
511+
},
512+
);
449513
yield* worker.drain();
450514

451515
// Stop reaches both provider threads. The Codex one is interrupted at
@@ -459,6 +523,11 @@ const stopEarlierBackgroundWork = (failedStart: boolean) =>
459523
],
460524
);
461525
const after = yield* orchestrator.getThreadProjection(threadId);
526+
if (stopWithQueue !== undefined) {
527+
assert.equal(after.runs.at(-1)?.status, "queued");
528+
assert.equal(after.runs.at(-1)?.queueHeld, true);
529+
assert.lengthOf(started, 1);
530+
}
462531
assert.deepEqual(
463532
[devServerId, watcherId, reviewerId].map(
464533
(id) => after.turnItems.find((item) => item.id === id)?.status,
@@ -485,3 +554,8 @@ it.effect(
485554
"Stop reaches earlier background work after the newest run fails before provider start",
486555
() => stopEarlierBackgroundWork(true),
487556
);
557+
558+
it.effect.each(["thread.stop", "run.interrupt"] as const)(
559+
"%s stops background work when a later message is queued",
560+
(stopType) => stopEarlierBackgroundWork(false, stopType),
561+
);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8227,7 +8227,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
82278227
? undefined
82288228
: projection.providerThreads.find((candidate) => candidate.id === run?.providerThreadId);
82298229
const hasBackgroundWork =
8230-
run?.id === projection.runs.at(-1)?.id &&
8230+
run?.id === projection.runs.findLast((candidate) => candidate.status !== "queued")?.id &&
82318231
derivePendingBackgroundWork({
82328232
latestRun: run,
82338233
providerThreads: projection.providerThreads,
@@ -8608,7 +8608,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
86088608
),
86098609
);
86108610
if (projection.thread.deletedAt !== null) return undefined;
8611-
const latestRun = projection.runs.at(-1);
8611+
// Queued messages never started and cannot own background work.
8612+
const latestRun = projection.runs.findLast((run) => run.status !== "queued");
86128613
const target =
86138614
projection.runs.findLast(
86148615
(run) =>

0 commit comments

Comments
 (0)