fix(server): queued messages run in the modes they were sent with #31322
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/Orchestrator.ts line 13760OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -13611,0 +13740,21 @@13740+ : records.runs.find((candidate) => candidate.id === command.runId);13741+ if (run !== undefined) {13742+ // Edit and start refuse automatic deliveries, the only runs that follow the current modes.13743+ const followsCurrent = followsCurrentConversation(13744+ records.messages.find((message) => message.id === run.userMessageId),13745+ );13746+ yield* refuseAboveDispatchModeLimit(13747+ command,13748+ threadId,13749+ queuedRunModes(shell, run, followsCurrent, runtimeModeCeiling?.maxRuntimeMode),13750+ );13751+ }13752+ }13753+ // These hand the caller's input to the active run, which can run above the thread's modes.13754+ if (13755+ shell !== null &&13756+ shell.activeRunId !== null &&13757+ ((command.type === "message.dispatch" &&13758+ (command.dispatchMode.type === "steer_active" ||13759+ command.dispatchMode.type === "restart_active" ||13760+ command.deliveryIntent !== undefined)) ||apps/server/src/orchestration-v2/Orchestrator.ts lines 13738 to 13751OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -13611,0 +13731,21 @@13731+ ["runs", "messages"],13732+ command.type === "queue.resume"13733+ ? { runStatuses: ["queued"], messageRoles: ["user"] }13734+ : { runIds: [command.runId], messageIds: [] },13735+ )13736+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));13737+ const run =13738+ command.type === "queue.resume"13739+ ? queuedRunsInDeliveryOrder(records)[0]13740+ : records.runs.find((candidate) => candidate.id === command.runId);13741+ if (run !== undefined) {13742+ // Edit and start refuse automatic deliveries, the only runs that follow the current modes.13743+ const followsCurrent = followsCurrentConversation(13744+ records.messages.find((message) => message.id === run.userMessageId),13745+ );13746+ yield* refuseAboveDispatchModeLimit(13747+ command,13748+ threadId,13749+ queuedRunModes(shell, run, followsCurrent, runtimeModeCeiling?.maxRuntimeMode),13750+ );13751+ }
- apps/server/src/orchestration-v2/Orchestrator.ts lines 13742 to 13751OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -13611,0 +13731,21 @@13731+ // A queued run starts in the modes it was sent with, which can be above the thread's now.13732+ if (13733+ shell !== null &&13734+ (command.type === "queued-run.edit" ||13735+ command.type === "queued-run.start" ||13736+ command.type === "queue.resume")13737+ ) {13738+ const records = yield* projectionStore13739+ .getThreadRecords(13740+ threadId,13741+ ["runs", "messages"],13742+ command.type === "queue.resume"13743+ ? { runStatuses: ["queued"], messageRoles: ["user"] }13744+ : { runIds: [command.runId], messageIds: [] },13745+ )13746+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));13747+ // Resuming releases every queued run, not only the next one.13748+ const runs =13749+ command.type === "queue.resume"13750+ ? queuedRunsInDeliveryOrder(records)13751+ : records.runs.filter((candidate) => candidate.id === command.runId);
- apps/server/src/orchestration-v2/Orchestrator.ts line 13747OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -13609,3 +13728,20 @@1360913728 .getThreadShell(threadId)1361013729 .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));1361113730 if (shell !== null) yield* refuseAboveDispatchModeLimit(command, threadId, shell);13731+ // A queued run starts in the modes it was sent with, which can be above the thread's now.13732+ if (13733+ shell !== null &&13734+ (command.type === "queued-run.edit" ||13735+ command.type === "queued-run.start" ||13736+ command.type === "queue.resume")13737+ ) {13738+ const records = yield* projectionStore13739+ .getThreadRecords(13740+ threadId,13741+ ["runs", "messages"],13742+ command.type === "queue.resume"13743+ ? { runStatuses: ["queued"], messageRoles: ["user"] }13744+ : { runIds: [command.runId], messageIds: [] },13745+ )13746+ .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));13747+ // Resuming releases every queued run, not only the next one.
- apps/server/src/mcp/threadAccess.ts line 109OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -103,3 +104,6 @@103104 message: "The calling thread was not found.",104105 });105106 }107+ // A queued run can execute in modes other than the thread's; the caller gets the stricter.108+ const activeRun =109+ caller.activeRunId === null
juliusmarminge added labelsbugfix
juliusmarminge added labelspr:needs-julius
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
- @@ -7216,3 +7306,6 @@72167306 }72177307 }72187308 if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {7309+ const targetRunId = dispatchMode.targetRunId;7310+ // The input reaches the active run, which can run above the thread's modes.7311+ yield* refuseAboveDispatchModeLimit(@@ -15160,3 +15252,19 @@1516015252 .getThreadShell(threadId)1516115253 .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));1516215254 if (shell !== null) yield* refuseAboveDispatchModeLimit(command, threadId, shell);15255+ // A queued run starts in the modes it was sent with, which can be above the thread's now.15256+ if (15257+ shell !== null &&15258+ (command.type === "queued-run.edit" ||15259+ command.type === "queued-run.start" ||15260+ command.type === "queue.resume")15261+ ) {15262+ // Starting one queued run or resuming the queue releases every queued run.15263+ const releasesQueue = command.type !== "queued-run.edit";15264+ const records = yield* projectionStore15265+ .getThreadRecords(15266+ threadId,15267+ ["runs", "messages"],15268+ releasesQueue15269+ ? { runStatuses: ["queued"], messageRoles: ["user"] }15270+ : { runIds: [command.runId], messageIds: [] },
- @@ -107,3 +108,10 @@107108 message: "The calling thread was not found.",108109 });109110 }111+ // A queued run can execute in modes other than the thread's; the caller gets the stricter.112+ const activeRun =113+ caller.activeRunId === null114+ ? undefined115+ : (yield* threads116+ .getThreadRecords(caller.id, ["runs"], { runIds: [caller.activeRunId] })117+ .pipe(Effect.mapError(unavailable))).runs[0];@@ -175,3 +175,4 @@175175 Layer.provide(NodeCrypto.layer),176176 Layer.provide(177177 Layer.mock(ThreadManagement.ThreadManagementService)({178+ getThreadRecords: McpToolAccessTestkit.runsWithoutModes,@@ -7185,3 +7275,14 @@71857275 payload: { ...sourcePlan, status: "completed" },71867276 });718772777278+ if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {7279+ const targetRunId = dispatchMode.targetRunId;7280+ // The input reaches the active run, now or once it is ready, and that run can be above the thread's modes.7281+ yield* refuseAboveDispatchModeLimit(7282+ command,7283+ command.threadId,7284+ runModes(7285+ projection.thread,7286+ projection.runs.find((run) => run.id === targetRunId),7287+ ),7288+ );@@ -3316,0 +3517,21 @@3517+ Effect.forkScoped,3518+ );3519+ yield* Queue.offer(events, {3520+ type: "provider_turn.updated",3521+ driver,3522+ providerTurn: { ...turn, status: "completed", completedAt: yield* DateTime.now },3523+ });3524+ yield* Queue.offer(events, {3525+ type: "turn.terminal",3526+ driver,3527+ providerThreadId: turn.providerThreadId,3528+ providerTurnId: turn.id,3529+ runOrdinal: first.runOrdinal,3530+ status: "completed",3531+ failure: null,3532+ threadDisposition: "reusable",3533+ });3534+ yield* Fiber.join(settled);3535+ yield* worker.drain();3536+ yield* orchestrator.resumeQueuedRuns;3537+ yield* worker.drain();@@ -964,4 +964,6 @@964964 typeof query === "string" &&965965 /\bSELECT\s+payload_json\s+FROM orchestration_v2_projection_(?:runs|context_transfers)\b/i.test(966966 query,967- )967+ ) &&968+ // The caller's own active run, read by id for its modes, is not history.969+ !/\brun_id IN\b/i.test(query)
juliusmarminge added labelstakeover:needs-julius
juliusmarminge removed labelstakeover:in-progress
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed