|
| 1 | +import type { ProviderDriverKind } from "@t3tools/contracts"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Which provider drivers can take a message *during* a running turn. |
| 5 | + * |
| 6 | + * A `sendTurn` that arrives while a turn is already running is a "steer": the |
| 7 | + * adapter folds the message into the live agent loop and the work continues as |
| 8 | + * the same turn, which is what typing into the Claude Code CLI while it works |
| 9 | + * does. Nothing above the adapter blocks it — upstream's immediate-send path |
| 10 | + * has no `phase === "running"` guard, the decider has no running-turn |
| 11 | + * invariant, and `ProviderService.sendTurn` has no busy check. |
| 12 | + * |
| 13 | + * The catch is that there is no steering capability flag anywhere in the |
| 14 | + * contracts, so support is implicit per adapter and has to be asserted here. |
| 15 | + * This list is therefore an allowlist, not a denylist: a driver only steers if |
| 16 | + * it is named below, and anything unrecognised falls back to the outbox. That |
| 17 | + * matters because a refused steer is invisible — `providerService.sendTurn` is |
| 18 | + * `Effect.forkScoped` in ProviderCommandReactor, so the client's command has |
| 19 | + * already succeeded by then; the message is persisted, the composer is |
| 20 | + * cleared, and the only trace is a `provider.turn.start.failed` activity. |
| 21 | + * Failing closed costs a queued message; failing open loses one. |
| 22 | + * |
| 23 | + * Verified against each adapter's `sendTurn`, all of which reuse the running |
| 24 | + * turn's id and emit no second `turn.started`: |
| 25 | + * |
| 26 | + * - `claudeAgent` — ClaudeAdapter.ts:3729, covered by ClaudeAdapter.test.ts:976 |
| 27 | + * ("steers a running turn instead of opening a new one on mid-turn |
| 28 | + * sendTurn"). One long-lived `query()` per thread whose prompt is a stream |
| 29 | + * fed from a queue, so the message reaches the CLI's stdin immediately. |
| 30 | + * - `cursor` — CursorAdapter.ts:916, covered by CursorAdapter.test.ts:253. |
| 31 | + * - `grok` — GrokAdapter.ts:921. |
| 32 | + * - `opencode` — OpenCodeAdapter.ts:1417, covered by OpenCodeAdapter.test.ts:733. |
| 33 | + * |
| 34 | + * Deliberately absent: |
| 35 | + * |
| 36 | + * - `codex` — CodexSessionRuntime.ts:1305 issues `turn/start` unconditionally |
| 37 | + * and overwrites `activeTurnId` with the response's turn id. The Codex |
| 38 | + * app-server protocol does expose a real `turn/steer` RPC, but t3code never |
| 39 | + * calls it, so any folding is the app-server's own decision rather than |
| 40 | + * something this repo implements. Its `activeTurnNotSteerable` error (raised |
| 41 | + * during `/review` or a manual `/compact`) is not handled either. |
| 42 | + */ |
| 43 | +const STEERABLE_PROVIDER_DRIVER_KINDS: ReadonlySet<string> = new Set([ |
| 44 | + "claudeAgent", |
| 45 | + "cursor", |
| 46 | + "grok", |
| 47 | + "opencode", |
| 48 | +]); |
| 49 | + |
| 50 | +export function providerSupportsSteering(driver: ProviderDriverKind | null | undefined): boolean { |
| 51 | + return driver !== null && driver !== undefined && STEERABLE_PROVIDER_DRIVER_KINDS.has(driver); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Whether the active thread is busy in a way the provider can absorb right now, |
| 56 | + * so a submit should go out immediately instead of into the outbox. |
| 57 | + * |
| 58 | + * Only a running turn qualifies: |
| 59 | + * |
| 60 | + * - `isSendBusy` is the optimistic local-dispatch window. Upstream's |
| 61 | + * immediate-send path bails on it, so a submit made here would be silently |
| 62 | + * dropped rather than sent — queuing keeps the message. |
| 63 | + * - `isRevertingCheckpoint` is not a provider turn at all; there is nothing to |
| 64 | + * steer into. |
| 65 | + */ |
| 66 | +export function canSteerActiveThread(input: { |
| 67 | + readonly phase: string; |
| 68 | + readonly isSendBusy: boolean; |
| 69 | + readonly isRevertingCheckpoint: boolean; |
| 70 | + readonly provider: ProviderDriverKind | null | undefined; |
| 71 | +}): boolean { |
| 72 | + return ( |
| 73 | + input.phase === "running" && |
| 74 | + !input.isSendBusy && |
| 75 | + !input.isRevertingCheckpoint && |
| 76 | + providerSupportsSteering(input.provider) |
| 77 | + ); |
| 78 | +} |
0 commit comments