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, 13:30 UTC
fix(server): queued messages no longer wait behind every later delegate completion (#34620)
Co-authored-by: Apex-ICP <jesse@apexicp.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Jesse-pink-lab committed
Nov 14, 2026 at 14:10 UTC
4c8cb0445da6c84fa799edee057af355d1e992aa
2 files changed
+67
-34
apps/server/src/orchestration-v2/QueuedRunOrder.test.ts
+38
-24
@@ -1,51 +1,65 @@
1
+import * as DateTime from "effect/DateTime";
2
import { describe, expect, it } from "vite-plus/test";
3
4
import { queuedRunsInDeliveryOrder } from "./QueuedRunOrder.ts";
5
6
+const at = (time: string) => DateTime.makeUnsafe(`2026-01-01T${time}Z`);
7
+const delegatedCompletion = {
8
+ generation: 1,
9
+ parentRunId: "run:parent",
10
+ taskIds: ["task:child"],
11
+};
12
+
13
describe("queued run delivery order", () => {
6
- it("keeps automatic completion delivery ahead of visible queued messages", () => {
14
+ it("delivers completions and queued messages in request order, keeping manual queue order", () => {
15
const projection = {
16
messages: [
9
- { id: "message:visible-first" },
10
- {
11
- id: "message:automatic",
12
- delegatedCompletion: {
13
- generation: 1,
14
- parentRunId: "run:parent",
15
- taskIds: ["task:child"],
16
- },
17
- },
18
- { id: "message:visible-second" },
17
+ { id: "message:old" },
18
+ { id: "message:moved" },
19
+ { id: "message:earlier-completion", delegatedCompletion },
20
+ { id: "message:later-completion", delegatedCompletion },
21
],
22
runs: [
23
{
22
- id: "run:visible-first",
23
- ordinal: 2,
24
- queuePosition: 1,
24
+ id: "run:old",
25
+ ordinal: 3,
26
+ queuePosition: 2,
27
+ requestedAt: at("10:00:00"),
28
status: "queued",
26
- userMessageId: "message:visible-first",
29
+ userMessageId: "message:old",
30
},
31
{
29
- id: "run:automatic",
32
+ id: "run:moved",
33
ordinal: 4,
34
+ queuePosition: 1,
35
+ requestedAt: at("10:02:00"),
36
+ status: "queued",
37
+ userMessageId: "message:moved",
38
+ },
39
+ {
40
+ id: "run:earlier-completion",
41
+ ordinal: 2,
42
queuePosition: 3,
43
+ requestedAt: at("09:59:00"),
44
status: "queued",
33
- userMessageId: "message:automatic",
45
+ userMessageId: "message:earlier-completion",
46
},
47
{
36
- id: "run:visible-second",
37
- ordinal: 3,
38
- queuePosition: 2,
48
+ id: "run:later-completion",
49
+ ordinal: 5,
50
+ queuePosition: 4,
51
+ requestedAt: at("10:03:00"),
52
status: "queued",
40
- userMessageId: "message:visible-second",
53
+ userMessageId: "message:later-completion",
54
},
55
],
56
} as never;
57
58
expect(queuedRunsInDeliveryOrder(projection).map((run) => run.id)).toEqual([
46
- "run:automatic",
47
- "run:visible-first",
48
- "run:visible-second",
59
+ "run:earlier-completion",
60
+ "run:moved",
61
+ "run:old",
62
+ "run:later-completion",
63
]);
64
});
65
});
apps/server/src/orchestration-v2/QueuedRunOrder.ts
+29
-10
@@ -1,4 +1,5 @@
1
import type { OrchestrationV2Run, OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2
+import * as DateTime from "effect/DateTime";
3
4
export function isAutomaticCompletionRun(
5
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "messages">,
@@ -17,16 +18,34 @@ export function queuedRunsInDeliveryOrder(
18
.filter((message) => message.delegatedCompletion !== undefined)
19
.map((message) => message.id),
20
);
20
- return projection.runs
21
+ const queued = projection.runs
22
.filter((run) => run.status === "queued")
22
- .toSorted((left, right) => {
23
- const deliveryPriority =
24
- Number(automaticCompletionMessageIds.has(right.userMessageId)) -
25
- Number(automaticCompletionMessageIds.has(left.userMessageId));
26
- return (
27
- deliveryPriority ||
23
+ .toSorted(
24
+ (left, right) =>
25
(left.queuePosition ?? left.ordinal) - (right.queuePosition ?? right.ordinal) ||
29
- left.ordinal - right.ordinal
30
- );
31
- });
26
+ left.ordinal - right.ordinal,
27
+ );
28
+ const completions = queued.filter((run) => automaticCompletionMessageIds.has(run.userMessageId));
29
+ const messages = queued.filter((run) => !automaticCompletionMessageIds.has(run.userMessageId));
30
+ // Completions and messages each keep their queue order and interleave by request time,
31
+ // so neither kind can hold the other back while more of it keeps arriving.
32
+ const ordered: Array<OrchestrationV2Run> = [];
33
+ let completionIndex = 0;
34
+ let messageIndex = 0;
35
+ for (;;) {
36
+ const completion = completions[completionIndex];
37
+ const message = messages[messageIndex];
38
+ if (completion === undefined || message === undefined) {
39
+ return [...ordered, ...completions.slice(completionIndex), ...messages.slice(messageIndex)];
40
+ }
41
+ if (
42
+ DateTime.toEpochMillis(completion.requestedAt) <= DateTime.toEpochMillis(message.requestedAt)
43
+ ) {
44
+ ordered.push(completion);
45
+ completionIndex += 1;
46
+ } else {
47
+ ordered.push(message);
48
+ messageIndex += 1;
49
+ }
50
+ }
51
}