diff --git a/apps/server/src/orchestration-v2/CommandPolicy.test.ts b/apps/server/src/orchestration-v2/CommandPolicy.test.ts index 8ed4de3e9dea0..c13005c069ae8 100644 --- a/apps/server/src/orchestration-v2/CommandPolicy.test.ts +++ b/apps/server/src/orchestration-v2/CommandPolicy.test.ts @@ -63,25 +63,30 @@ function dispatchProjection( } as unknown as OrchestrationV2ThreadProjection; } +function resolveDispatch( + projection: OrchestrationV2ThreadProjection, + dispatchMode: Parameters[1]["dispatchMode"], + deliveryIntent?: "auto" | "steer" | "restart", + text = "next", +) { + return CommandPolicy.resolveMessageDispatchIntent(projection, { + dispatchMode, + ...(deliveryIntent === undefined ? {} : { deliveryIntent }), + text, + attachments: [], + }); +} + it("resolves automatic message delivery from authoritative provider capabilities", () => { + assert.deepEqual(resolveDispatch(dispatchProjection(), { type: "start_immediately" }, "auto"), { + type: "start_immediately", + }); assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - dispatchProjection(), - { type: "start_immediately" }, - "auto", - ), - { type: "start_immediately" }, - ); - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - dispatchProjection(baseCapabilities), - { type: "start_immediately" }, - "auto", - ), + resolveDispatch(dispatchProjection(baseCapabilities), { type: "start_immediately" }, "auto"), { type: "steer_active", targetRunId: activeRunId }, ); assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( + resolveDispatch( dispatchProjection( capabilities((current) => ({ ...current, @@ -99,7 +104,7 @@ it("resolves automatic message delivery from authoritative provider capabilities { type: "queue_after_active" }, ); assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( + resolveDispatch( dispatchProjection( capabilities((current) => ({ ...current, @@ -123,7 +128,7 @@ it.each(["preparing", "starting"] as const)( (status) => { const projection = dispatchProjection(baseCapabilities); assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( + resolveDispatch( { ...projection, runs: projection.runs.map((run) => ({ ...run, status })) }, { type: "start_immediately" }, "auto", @@ -146,22 +151,17 @@ it.each([ ...dispatchProjection(baseCapabilities), providerTurns, } as unknown as OrchestrationV2ThreadProjection; - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"), - { type: "queue_after_active" }, - ); - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"), - { type: "steer_active", targetRunId: activeRunId }, - ); - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - projection, - { type: "start_immediately" }, - "restart", - ), - { type: "restart_active", targetRunId: activeRunId }, - ); + assert.deepEqual(resolveDispatch(projection, { type: "start_immediately" }, "auto"), { + type: "queue_after_active", + }); + assert.deepEqual(resolveDispatch(projection, { type: "start_immediately" }, "steer"), { + type: "steer_active", + targetRunId: activeRunId, + }); + assert.deepEqual(resolveDispatch(projection, { type: "start_immediately" }, "restart"), { + type: "restart_active", + targetRunId: activeRunId, + }); }); // regression: #26695 an interrupt-restart steer cancels Copilot's background agents @@ -178,32 +178,23 @@ it("queues a steer for a provider that cannot steer either way", () => { })), ); for (const deliveryIntent of ["auto", "steer"] as const) { - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - projection, - { type: "start_immediately" }, - deliveryIntent, - ), - { type: "queue_after_active" }, - ); + assert.deepEqual(resolveDispatch(projection, { type: "start_immediately" }, deliveryIntent), { + type: "queue_after_active", + }); } assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent(projection, { + resolveDispatch(projection, { type: "steer_active", targetRunId: activeRunId, }), { type: "queue_after_active" }, ); + assert.deepEqual(resolveDispatch(projection, { type: "start_immediately" }, "restart"), { + type: "restart_active", + targetRunId: activeRunId, + }); assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - projection, - { type: "start_immediately" }, - "restart", - ), - { type: "restart_active", targetRunId: activeRunId }, - ); - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent(projection, { + resolveDispatch(projection, { type: "steer_active", targetRunId: RunId.make("command-policy-missing-run"), }), @@ -211,6 +202,24 @@ it("queues a steer for a provider that cannot steer either way", () => { ); }); +// regression: #32606 a slash command steered into a running turn replaced the task +it.each([ + ["/context", { type: "queue_after_active" }], + [" /context all\n", { type: "queue_after_active" }], + ["/plugin:review-pr 12", { type: "queue_after_active" }], + ["/home/me/x.ts fails", { type: "steer_active", targetRunId: activeRunId }], + ["/ context", { type: "steer_active", targetRunId: activeRunId }], + ["$skill /context", { type: "steer_active", targetRunId: activeRunId }], +] as const)("steers %j only when it is not a slash command", (text, expected) => { + const projection = dispatchProjection(baseCapabilities); + for (const deliveryIntent of ["auto", "steer"] as const) { + assert.deepEqual( + resolveDispatch(projection, { type: "start_immediately" }, deliveryIntent, text), + expected, + ); + } +}); + it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => { const projection = dispatchProjection(baseCapabilities); const maintenance = { @@ -218,30 +227,21 @@ it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) = messages: [{ id: userMessageId, text, attachments: [] }], } as unknown as OrchestrationV2ThreadProjection; for (const deliveryIntent of ["auto", "steer"] as const) { - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - maintenance, - { type: "start_immediately" }, - deliveryIntent, - ), - { type: "queue_after_active" }, - ); + assert.deepEqual(resolveDispatch(maintenance, { type: "start_immediately" }, deliveryIntent), { + type: "queue_after_active", + }); } assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent(maintenance, { + resolveDispatch(maintenance, { type: "steer_active", targetRunId: activeRunId, }), { type: "queue_after_active" }, ); - assert.deepEqual( - CommandPolicy.resolveMessageDispatchIntent( - maintenance, - { type: "start_immediately" }, - "restart", - ), - { type: "restart_active", targetRunId: activeRunId }, - ); + assert.deepEqual(resolveDispatch(maintenance, { type: "start_immediately" }, "restart"), { + type: "restart_active", + targetRunId: activeRunId, + }); }); const layer = it.layer(CommandPolicy.layer); diff --git a/apps/server/src/orchestration-v2/CommandPolicy.ts b/apps/server/src/orchestration-v2/CommandPolicy.ts index 02f329e536feb..a3007c0d7a165 100644 --- a/apps/server/src/orchestration-v2/CommandPolicy.ts +++ b/apps/server/src/orchestration-v2/CommandPolicy.ts @@ -69,6 +69,14 @@ export function isNativeMaintenanceCommand(message: { ); } +/** A message that only invokes a slash command, such as `/context all`, not a path like `/src/a.ts`. */ +export function isSlashCommand(message: { + readonly text: string; + readonly attachments: ReadonlyArray; +}): boolean { + return message.attachments.length === 0 && /^\/[\w:-]+(?:\s|$)/u.test(message.text.trim()); +} + /** * A goal command sent into active work stops that work when it starts or * replaces goal work, or ends a goal that is driving the run. @@ -90,6 +98,25 @@ export function goalCommandStopsActiveRun( } export function resolveMessageDispatchIntent( + projection: OrchestrationV2ThreadProjection, + message: Pick< + Extract, + "dispatchMode" | "deliveryIntent" | "text" | "attachments" + >, +): MessageDispatchMode { + const mode = resolveActiveRunDispatchIntent( + projection, + message.dispatchMode, + message.deliveryIntent, + ); + // A provider runs a steered command in place of the turn's task, so commands wait. + return mode.type === "steer_active" && isSlashCommand(message) + ? { type: "queue_after_active" } + : mode; +} + +/** How a message joins active work when it is free to steer. */ +export function resolveActiveRunDispatchIntent( projection: OrchestrationV2ThreadProjection, requestedMode: MessageDispatchMode, deliveryIntent?: "auto" | "steer" | "restart", diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index eab5cda62027e..69da72a3a98e5 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -109,6 +109,8 @@ import { goalCommandStopsActiveRun, isActiveRun, isNativeMaintenanceCommand, + isSlashCommand, + resolveActiveRunDispatchIntent, resolveMessageDispatchIntent, } from "./CommandPolicy.ts"; import { CommandReceiptStoreV2, type CommandReceiptV2 } from "./CommandReceiptStore.ts"; @@ -5156,6 +5158,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio "Goal commands must run as a separate turn. Queue it or wait for the active turn to finish.", }); } + // An explicit restart already gives up the turn, so only steering holds commands back. + if (!input.forceRestart && isSlashCommand(input)) { + return yield* new OrchestratorDispatchError({ + commandId: input.command.commandId, + commandType: input.command.type, + cause: + "Commands must run as a separate turn. Queue it or wait for the active turn to finish.", + }); + } const targetMessage = input.projection.messages.find( (message) => message.id === targetRun.userMessageId, ); @@ -6062,11 +6073,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio projection = applyToProjection(projection, event); } let modelSelection = command.modelSelection ?? projection.thread.modelSelection; - let dispatchMode = resolveMessageDispatchIntent( - projection, - command.dispatchMode, - command.deliveryIntent, - ); + // Native goal commands must not steer an existing turn. + const parsedGoalCommand = parseGoalCommand(command); + const goalCommand = + parsedGoalCommand === null || + (yield* goalDriverFor(command, modelSelection.instanceId)) === undefined + ? null + : parsedGoalCommand; + // A goal command keeps the run it was sent at; the goal handling below queues it. + let dispatchMode = + goalCommand === null + ? resolveMessageDispatchIntent(projection, command) + : resolveActiveRunDispatchIntent(projection, command.dispatchMode, command.deliveryIntent); if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") { const targetRunId = dispatchMode.targetRunId; const target = projection.runs.find((run) => run.id === targetRunId); @@ -6116,13 +6134,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ) { dispatchMode = { type: "queue_after_active" }; } - // Native goal commands must not steer an existing turn. - const parsedGoalCommand = parseGoalCommand(command); - const goalCommand = - parsedGoalCommand === null || - (yield* goalDriverFor(command, modelSelection.instanceId)) === undefined - ? null - : parsedGoalCommand; let goalStopRunId: RunId | undefined; if (goalCommand !== null) { // A goal command never joins a running turn. One that starts or ends goal @@ -10117,8 +10128,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio cause: "A message that starts a fresh session can't steer the current turn.", }); } - if (goalCommand !== null) { - yield* ensureGoalCommandSupported(command, queuedMessage, queuedRun.providerInstanceId); + // A steered command replaces the turn's task, so promoting one only moves it first. + if (goalCommand !== null || isSlashCommand(queuedMessage)) { + if (goalCommand !== null) { + yield* ensureGoalCommandSupported(command, queuedMessage, queuedRun.providerInstanceId); + } const goal = projection.providerThreads.find( (candidate) => candidate.id === target?.providerThreadId, )?.goal; @@ -10132,6 +10146,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio payload: { ...queuedRun, queuePosition: 1 }, }); const stops = + goalCommand !== null && target !== undefined && isLiveRun(target) && goalCommandStopsActiveRun(goalCommand, goal?.status ?? null) && @@ -12306,7 +12321,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ), ); - /** Puts a queued goal command first and, given a target, stops the run in its way. */ + /** Puts a queued command first and, given a target, stops the run in its way. */ const stopRunForQueuedCommand = Effect.fn("Orchestrator.stopRunForQueuedCommand")( function* (input: { readonly command: Extract< diff --git a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts index bc8fc9a20e3d3..60ffda9bfa8ef 100644 --- a/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts +++ b/apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts @@ -2124,15 +2124,12 @@ it.effect.each([undefined, false, true])( const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; const threadId = yield* startFirstTurn; yield* send("queued", "queued", "queue"); - const sending = send("goal", "/goal all tests pass", "auto"); - if (nativeGoals === false) { - yield* Effect.flip(sending); - } else yield* sending; + yield* send("goal", "/goal all tests pass", "auto"); yield* worker.drain(); assert.deepEqual(steered, []); assert.deepEqual(yield* read(threadId), { - queue: ["queued"], + queue: nativeGoals === false ? ["queued", "/goal all tests pass"] : ["queued"], started: nativeGoals === false ? ["first"] : ["first", "/goal all tests pass"], stopRequested: nativeGoals !== false, }); @@ -2203,6 +2200,74 @@ it.effect("queues a goal clear without stopping work when no goal drives it", () ), ); +it.effect("promoting a queued /context moves it first and does not steer", () => + Effect.scoped( + Effect.gen(function* () { + const { started, steered, layer, startFirstTurn, send, read } = + yield* goalHarness("slash-promote"); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = yield* startFirstTurn; + yield* send("queued", "queued", "queue"); + yield* send("context", "/context", "queue"); + const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find( + (run) => run.userMessageId === MessageId.make("message:context"), + )!; + yield* orchestrator.dispatch({ + type: "queued-message.promote-to-steer", + commandId: CommandId.make("promote"), + threadId, + queuedRunId: queuedRun.id, + targetRunId: started[0]!.runId, + }); + yield* worker.drain(); + + assert.deepEqual(steered, []); + assert.deepEqual(yield* read(threadId), { + queue: ["/context", "queued"], + started: ["first"], + stopRequested: false, + }); + }).pipe(Effect.provide(layer)); + }), + ), +); + +it.effect("restarts the turn for a slash command sent as an explicit restart", () => + Effect.scoped( + Effect.gen(function* () { + const { started, steered, interrupted, layer, startFirstTurn } = + yield* nextTurnSelectionHarness("slash-restart", true); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = yield* startFirstTurn; + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("restart-context"), + threadId, + messageId: MessageId.make("message:restart-context"), + text: "/context", + attachments: [], + modelSelection: runSelection, + dispatchMode: { type: "restart_active", targetRunId: started[0]!.runId }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + + assert.deepEqual(steered, []); + assert.lengthOf(interrupted, 1); + assert.deepEqual( + started.map((turn) => turn.message.text), + ["first", "/context"], + ); + }).pipe(Effect.provide(layer)); + }), + ), +); + it.effect("promotes a queued goal by stopping the running turn", () => Effect.scoped( Effect.gen(function* () { @@ -2237,6 +2302,41 @@ it.effect("promotes a queued goal by stopping the running turn", () => ), ); +it.effect("stops the running turn for a goal an agent steers into it", () => + Effect.scoped( + Effect.gen(function* () { + const { started, steered, layer, startFirstTurn, read } = + yield* goalHarness("goal-raw-steer"); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = yield* startFirstTurn; + // Agent sends carry a raw steer target and no delivery intent. + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("goal"), + threadId, + messageId: MessageId.make("message:goal"), + text: "/goal ship it", + attachments: [], + modelSelection: runSelection, + dispatchMode: { type: "steer_active", targetRunId: started[0]!.runId }, + createdBy: "agent", + creationSource: "server", + }); + yield* worker.drain(); + + assert.deepEqual(steered, []); + assert.deepEqual(yield* read(threadId), { + queue: [], + started: ["first", "/goal ship it"], + stopRequested: true, + }); + }).pipe(Effect.provide(layer)); + }), + ), +); + // A fresh message skips compaction as its own turn, which steering a running turn would not. it.effect.each(["next", "/goal ship it", "fresh next"])( "promoting %s into a compaction stops it and sends it next", diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts index bfa50b9793490..43c1a8e54025d 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts @@ -719,7 +719,7 @@ it.effect("keeps native maintenance commands out of steering and restart message attachments: [], dispatchMode: { type: scenario.mode, targetRunId: targetRun.id }, }); - if (scenario.name.startsWith("steer-")) { + if (scenario.mode === "steer_active") { yield* dispatch; const queued = (yield* threads.getThreadProjection(launched.threadId)).runs[1]; assert.equal(queued?.status, "queued"); diff --git a/docs/user/composer.md b/docs/user/composer.md index df880e49c6ca5..04eb1f10bafad 100644 --- a/docs/user/composer.md +++ b/docs/user/composer.md @@ -69,6 +69,9 @@ are saved on the server and can be edited or removed before they send. When Enter sends, `Cmd+Enter` on macOS or `Ctrl+Enter` on Windows and Linux uses the opposite action: it steers when your default is Queue and queues when your default is Steer. +A slash command such as `/context` always waits for the current turn to finish, +because a command steered into a turn would replace the agent's task. Sending a +queued command now moves it to the front of the queue. If your **Send shortcut** requires `Cmd+Enter` or `Ctrl+Enter`, that shortcut uses your default follow-up behavior; add **Shift** to use the opposite action. This applies only to multiline messages when you choose the multiline send option.