The best way to control Claude Code, Codex and more from any computer or phone
fix(server): agent messages queued behind /compact keep send order (#39417)
Julius Marminge committed
Nov 19, 2026 at 15:28 UTC
0a159c9fba55f2699b6f36d19f7318046063e3f9
2 files changed
+145
apps/server/src/orchestration-v2/Orchestrator.ts
+22
@@ -7439,6 +7439,28 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
7439
) {
7440
dispatchMode = { type: "queue_after_active" };
7441
}
7442
+ // An agent's automatic send must not overtake an earlier agent message still queued
7443
+ // (e.g. behind /compact), or the agent receives them out of send order.
7444
+ if (
7445
+ command.deliveryIntent === "auto" &&
7446
+ command.createdBy === "agent" &&
7447
+ (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active")
7448
+ ) {
7449
+ const queuedRuns = projection.runs.filter(
7450
+ (run) => run.status === "queued" && run.queueHeld !== true,
7451
+ );
7452
+ if (queuedRuns.length > 0) {
7453
+ const queuedMessages = yield* projectionStore
7454
+ .getMessagesById(
7455
+ command.threadId,
7456
+ queuedRuns.map((run) => run.userMessageId),
7457
+ )
7458
+ .pipe(mapDispatchError(command));
7459
+ if ([...queuedMessages.values()].some((message) => message.createdBy === "agent")) {
7460
+ dispatchMode = { type: "queue_after_active" };
7461
+ }
7462
+ }
7463
+ }
7464
// An explicit restart that Stop reached has only its start left, so it
7465
// queues behind the stopping run like any other start.
7466
if (
apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+123
@@ -1717,6 +1717,129 @@ it.effect("queues a steer until the turn ends when the provider cannot steer it"
1717
}),
1718
);
1719
1720
+// regression: #39404 a later agent message steers ahead of one queued behind /compact
1721
+it.effect("delivers agent messages queued behind /compact before a later one steers", () =>
1722
+ Effect.gen(function* () {
1723
+ const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
1724
+ "agent-order-after-compact",
1725
+ );
1726
+ yield* Effect.gen(function* () {
1727
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1728
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1729
+ const threadId = yield* startFirstTurn;
1730
+ const nextRunning = Effect.fn("nextRunning")(function* () {
1731
+ return yield* orchestrator.streamDomainEvents.pipe(
1732
+ Stream.filter(
1733
+ (event) => event.type === "provider-turn.updated" && event.payload.status === "running",
1734
+ ),
1735
+ Stream.take(1),
1736
+ Stream.runDrain,
1737
+ Effect.forkScoped,
1738
+ );
1739
+ });
1740
+ const lock = (yield* ThreadCommandExecutor.ThreadCommandExecutor).withLock(
1741
+ threadId,
1742
+ Effect.void,
1743
+ );
1744
+ const finishTurn = Effect.fn("finishTurn")(function* (index: number) {
1745
+ const turnInput = started[index]!;
1746
+ const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns.find(
1747
+ (candidate) => candidate.runAttemptId === turnInput.attemptId,
1748
+ )!;
1749
+ const settled = yield* orchestrator.streamDomainEvents.pipe(
1750
+ Stream.filter(
1751
+ (event) =>
1752
+ event.type === "run.updated" &&
1753
+ event.payload.id === turnInput.runId &&
1754
+ (event.payload.status === "waiting" || event.payload.status === "completed"),
1755
+ ),
1756
+ Stream.take(1),
1757
+ Stream.runDrain,
1758
+ Effect.forkScoped,
1759
+ );
1760
+ yield* Queue.offer(events, {
1761
+ type: "provider_turn.updated",
1762
+ driver,
1763
+ providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },
1764
+ });
1765
+ yield* Queue.offer(events, {
1766
+ type: "turn.terminal",
1767
+ driver,
1768
+ providerThreadId: turn.providerThreadId,
1769
+ providerTurnId: turn.id,
1770
+ runOrdinal: turnInput.runOrdinal,
1771
+ status: "completed",
1772
+ failure: null,
1773
+ threadDisposition: "reusable",
1774
+ });
1775
+ yield* Fiber.join(settled);
1776
+ yield* worker.drain();
1777
+ yield* orchestrator.resumeQueuedRuns;
1778
+ yield* worker.drain();
1779
+ });
1780
+ yield* finishTurn(0);
1781
+ const compactRunning = yield* nextRunning();
1782
+ yield* orchestrator.dispatch({
1783
+ type: "message.dispatch",
1784
+ commandId: CommandId.make("compact"),
1785
+ threadId,
1786
+ messageId: MessageId.make("message:compact"),
1787
+ text: "/compact",
1788
+ attachments: [],
1789
+ dispatchMode: { type: "start_immediately" },
1790
+ createdBy: "user",
1791
+ creationSource: "web",
1792
+ });
1793
+ yield* worker.drain();
1794
+ yield* Fiber.join(compactRunning);
1795
+ yield* lock;
1796
+ const sendFromAgent = (codeword: string) =>
1797
+ Effect.gen(function* () {
1798
+ const active = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
1799
+ (run) => run.status === "running",
1800
+ )!;
1801
+ yield* orchestrator.dispatch({
1802
+ type: "message.dispatch",
1803
+ commandId: CommandId.make(`send-${codeword}`),
1804
+ threadId,
1805
+ messageId: MessageId.make(`message:${codeword}`),
1806
+ text: codeword,
1807
+ attachments: [],
1808
+ dispatchMode: { type: "steer_active", targetRunId: active.id },
1809
+ deliveryIntent: "auto",
1810
+ createdBy: "agent",
1811
+ creationSource: "mcp",
1812
+ senderThreadId: ThreadId.make("thread:orchestrator"),
1813
+ });
1814
+ yield* worker.drain();
1815
+ });
1816
+ yield* sendFromAgent("ALPHA");
1817
+ yield* sendFromAgent("BRAVO");
1818
+ const queued = (yield* orchestrator.getThreadProjection(threadId)).runs.filter(
1819
+ (run) => run.status === "queued",
1820
+ );
1821
+ assert.lengthOf(queued, 2);
1822
+ assert.deepEqual(steered, []);
1823
+
1824
+ const alphaRunning = yield* nextRunning();
1825
+ yield* finishTurn(1);
1826
+ yield* Fiber.join(alphaRunning);
1827
+ yield* lock;
1828
+ assert.deepEqual(
1829
+ started.map((turn) => turn.message.text.split("\n").at(-1)),
1830
+ ["first", "/compact", "ALPHA"],
1831
+ );
1832
+
1833
+ yield* sendFromAgent("CHARLIE");
1834
+ const delivered = [...steered, ...started.slice(3).map((turn) => turn.message.text)]
1835
+ .map((text) => text.split("\n").at(-1))
1836
+ .filter((text) => text === "BRAVO" || text === "CHARLIE");
1837
+ // Whatever reaches the agent so far does so in send order: CHARLIE never before BRAVO.
1838
+ assert.deepEqual(delivered, ["BRAVO", "CHARLIE"].slice(0, delivered.length));
1839
+ }).pipe(Effect.provide(layer));
1840
+ }),
1841
+);
1842
+
1843
// regression: #33454 a steer that restarts a completion wake run left its cohort claimed forever
1844
const restartCompletionWake = Effect.fn("restartCompletionWake")(function* (
1845
harness: Pick<