Skip to content

Commit fb4ed6c

Browse files
radroidclaude
andcommitted
feat(web): steer a running turn instead of queuing behind it (#35)
Typing while the agent works should reach the agent, the way it does in the Claude Code CLI. It already could: upstream's `onSend` has no `phase === "running"` guard, the decider has no running-turn invariant, and every adapter but Codex folds a mid-turn `sendTurn` into the live turn rather than opening a competing one — ClaudeAdapter.ts:3729 offers the message straight into the one long-lived `query()` whose prompt is a stream, which is the same stdin the CLI uses. Upstream even has a test for it (ClaudeAdapter.test.ts:976) and client-side handling to acknowledge a steering message (ChatView.logic.ts:537). The fork's outbox took that away. `resolveComposerSendLabel` returned "Queue" whenever the thread was busy, so `onSend`'s queue branch ran before upstream's immediate-send path and a message typed mid-turn waited out the entire turn — and, because `canSettle` also refuses while an approval is pending, sometimes much longer. Queue now means "cannot go out now" rather than "the thread is busy": - Not connected — still queues; nothing can be dispatched. - Outbox already has messages — still queues, so a new message cannot jump ahead of ones written before it. - Running turn on a steer-capable driver — sends, and the provider folds it into the work in progress. Steering support is an allowlist, not a denylist, because there is no capability flag anywhere in the contracts: `ProviderAdapterCapabilities` has no `supportsSteering`, so it is asserted per driver against each adapter's `sendTurn`. Claude, Cursor, Grok and OpenCode steer, three of them with tests. Codex does not — CodexSessionRuntime.ts:1305 issues `turn/start` unconditionally and never calls the protocol's `turn/steer` RPC — so it keeps queuing, as does any driver added later. Failing closed matters here because a refused steer is invisible: `providerService.sendTurn` is `Effect.forkScoped` (ProviderCommandReactor.ts:1110), so the command has already succeeded and the only trace is a `provider.turn.start.failed` activity. Failing closed costs a queued message; failing open loses one. The running-turn footer now pairs Stop with whichever primary action applies — Queue when the message will be held, the ordinary Send when it will be steered. That is not only discoverability: Enter does not submit on a mobile viewport, so the button is the only mid-turn send affordance on a phone. Seam impact: `ChatView.tsx` +158/-1 → +168/-1 (risk 9540 → 10140) and `ComposerPrimaryActions.tsx` +71/-13 → +130/-64 (336 → 776, the bulk being upstream's send button hoisted to a variable so the running branch can reuse it); rows and header totals updated. The steer predicate itself lives in a new fork-owned file rather than inline, per the ledger's own guidance. Also records the allowlist as a logic mirror: nothing fails if upstream changes an adapter to stop steering, so those four `sendTurn` implementations need re-checking at every sync. Verified: 80 tests green, typecheck and lint clean. The steer-vs-queue decision and the allowlist are unit-tested; the driver slugs are checked against ClaudeDriver.ts:60 and ClaudeAdapter.ts:96. Not verified end-to-end against a live Claude turn — the sandbox environment has no Claude provider instance and its Codex OAuth is stale. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 6669b78 commit fb4ed6c

7 files changed

Lines changed: 281 additions & 58 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ import { useBrowserHistoryStore } from "~/browserHistoryStore";
184184
import { registerFaviconProjectForThread } from "~/browserFaviconStore";
185185
import { resolveComposerSendLabel } from "~/outbox/composerSendLabel.logic";
186186
import { enqueueThreadOutboxMessage, useThreadOutboxQueue } from "~/outbox/threadOutbox";
187+
import { canSteerActiveThread } from "~/outbox/composerSteering.logic";
187188
import { ThreadOutboxQueueList } from "./chat/ThreadOutboxQueueList";
188189
import { getProviderModelCapabilities, resolveSelectableProvider } from "../providerModels";
189190
import { NO_PROVIDER_MODEL_SELECTION } from "../providerInstances";
@@ -2326,10 +2327,19 @@ function ChatViewContent(props: ChatViewProps) {
23262327
// enqueues instead of erroring, and the drain sends the head once settled.
23272328
const outboxQueue = useThreadOutboxQueue(activeThread?.environmentId ?? null, activeThreadId);
23282329
const composerActiveThreadBusy = phase === "running" || isSendBusy || isRevertingCheckpoint;
2330+
// A running turn is not on its own a reason to queue: steer-capable drivers
2331+
// fold a mid-turn send into the work in progress.
2332+
const composerCanSteerActiveThread = canSteerActiveThread({
2333+
phase,
2334+
isSendBusy,
2335+
isRevertingCheckpoint,
2336+
provider: selectedProvider,
2337+
});
23292338
const composerSendLabel = resolveComposerSendLabel({
23302339
connectionState: activeEnvironmentConnectionPhase,
23312340
activeThreadBusy: composerActiveThreadBusy,
23322341
queueCount: outboxQueue.length,
2342+
canSteerActiveThread: composerCanSteerActiveThread,
23332343
});
23342344
const composerQueueMode =
23352345
isServerThread &&

‎apps/web/src/components/chat/ComposerPrimaryActions.tsx‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
259259
);
260260
}
261261

262+
// Upstream hoisted this button to a variable itself in #4781, so the fork no longer
263+
// carries its own copy of the styling -- the stale-hoist hazard this seam used to
264+
// have (silently reverting an upstream restyle instead of conflicting) is gone.
265+
// The only fork addition is the aria-label branch for a running turn: on a
266+
// steer-capable driver this submit folds into the work in progress rather than
267+
// starting a new turn, and that is invisible otherwise.
262268
const sendButton = (
263269
<button
264270
type="submit"
@@ -287,7 +293,9 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
287293
? "Preparing worktree"
288294
: isSendBusy
289295
? "Sending"
290-
: "Send message"
296+
: isRunning
297+
? "Send message to the running turn"
298+
: "Send message"
291299
}
292300
>
293301
{stageBackdropVariant ? (
@@ -320,15 +328,17 @@ export const ComposerPrimaryActions = memo(function ComposerPrimaryActions({
320328
return sendButton;
321329
}
322330

323-
// While the thread is busy, queuing (when available) becomes the primary action and
324-
// Stop moves beside it as a secondary control. This also satisfies upstream's
325-
// showSendWhileRunning intent (#4781) -- the running turn stays reachable on mobile,
326-
// and Queue *is* the send action here, so it stands in for the send button.
331+
// While the thread is busy the primary action moves beside Stop, which drops to a
332+
// secondary control. Which action it is depends on whether the message can go out
333+
// now: "Queue" when it will be held, or the ordinary Send when the provider will
334+
// steer it into the running turn. Rendering it matters beyond discoverability --
335+
// Enter does not submit on a mobile viewport, so this button is the only mid-turn
336+
// send affordance there, which is also upstream's showSendWhileRunning intent (#4781).
327337
if (canQueue) {
328338
return (
329339
<div className={cn("flex items-center justify-end", compact ? "gap-1.5" : "gap-2")}>
330340
{secondaryStopButton}
331-
{queueButton}
341+
{sendLabel === "Queue" ? queueButton : sendButton}
332342
</div>
333343
);
334344
}

‎apps/web/src/outbox/composerSendLabel.logic.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,56 @@ describe("resolveComposerSendLabel", () => {
1313
).toBe("Send");
1414
});
1515

16-
it("labels Queue while the active thread is busy", () => {
16+
it("labels Queue while the active thread is busy and cannot be steered", () => {
1717
expect(
1818
resolveComposerSendLabel({
1919
connectionState: "connected",
2020
activeThreadBusy: true,
2121
queueCount: 0,
2222
}),
2323
).toBe("Queue");
24+
expect(
25+
resolveComposerSendLabel({
26+
connectionState: "connected",
27+
activeThreadBusy: true,
28+
queueCount: 0,
29+
canSteerActiveThread: false,
30+
}),
31+
).toBe("Queue");
32+
});
33+
34+
it("labels Send into a running turn the provider can steer", () => {
35+
expect(
36+
resolveComposerSendLabel({
37+
connectionState: "connected",
38+
activeThreadBusy: true,
39+
queueCount: 0,
40+
canSteerActiveThread: true,
41+
}),
42+
).toBe("Send");
43+
});
44+
45+
it("still queues a steerable thread when the outbox already holds messages", () => {
46+
// Steering would deliver this message ahead of ones queued before it.
47+
expect(
48+
resolveComposerSendLabel({
49+
connectionState: "connected",
50+
activeThreadBusy: true,
51+
queueCount: 1,
52+
canSteerActiveThread: true,
53+
}),
54+
).toBe("Queue");
55+
});
56+
57+
it("never steers while disconnected, however capable the provider is", () => {
58+
expect(
59+
resolveComposerSendLabel({
60+
connectionState: "reconnecting",
61+
activeThreadBusy: true,
62+
queueCount: 0,
63+
canSteerActiveThread: true,
64+
}),
65+
).toBe("Queue");
2466
});
2567

2668
it("labels Queue when messages are already queued to send later", () => {

‎apps/web/src/outbox/composerSendLabel.logic.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
22

33
/**
4-
* Ported from apps/mobile/src/features/threads/composerSendLabel.ts.
4+
* Ported from apps/mobile/src/features/threads/composerSendLabel.ts, and since
5+
* diverged: web steers where mobile still queues (see `canSteerActiveThread`).
56
*
6-
* The composer's send affordance doubles as a queue affordance: every "send"
7-
* enqueues to the thread outbox, which sends-when-idle and holds-when-busy.
8-
* The button label mirrors that decision so the user knows whether pressing it
9-
* (or hitting Return) will dispatch now or queue for later.
7+
* The composer's send affordance doubles as a queue affordance. The button
8+
* label mirrors which one a submit will do, so the user knows whether pressing
9+
* it (or hitting Return) dispatches now or holds for later.
1010
*
11-
* Returns "Queue" when the message cannot be delivered immediately — the
12-
* environment is not connected, the active thread is still working, or the
13-
* outbox already holds messages — and "Send" only when it will go out now.
11+
* Returns "Queue" only when the message genuinely cannot go out now, and "Send"
12+
* whenever it will — including *into* a running turn, which the provider folds
13+
* into the work in progress.
1414
*/
1515
export function resolveComposerSendLabel(input: {
1616
readonly connectionState: EnvironmentConnectionPhase;
1717
readonly activeThreadBusy: boolean;
1818
readonly queueCount: number;
19+
/**
20+
* Whether the thread is busy in a way the provider can absorb right now —
21+
* i.e. a running turn on a steer-capable driver. A busy thread is not on its
22+
* own a reason to queue: the adapter folds a mid-turn send into the running
23+
* turn rather than opening a competing one. Omitted (or false) keeps the
24+
* original hold-while-busy behaviour.
25+
*/
26+
readonly canSteerActiveThread?: boolean;
1927
}): "Send" | "Queue" {
20-
if (input.connectionState !== "connected" || input.activeThreadBusy || input.queueCount > 0) {
28+
// Nothing can be dispatched without a connection.
29+
if (input.connectionState !== "connected") {
30+
return "Queue";
31+
}
32+
// Something is already waiting: sending now would jump the queue and deliver
33+
// this message ahead of ones the user wrote first.
34+
if (input.queueCount > 0) {
35+
return "Queue";
36+
}
37+
if (input.activeThreadBusy && input.canSteerActiveThread !== true) {
2138
return "Queue";
2239
}
2340
return "Send";
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ProviderDriverKind } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { canSteerActiveThread, providerSupportsSteering } from "./composerSteering.logic";
5+
6+
describe("providerSupportsSteering", () => {
7+
it("allows the drivers whose adapters fold a mid-turn send into the running turn", () => {
8+
for (const driver of ["claudeAgent", "cursor", "grok", "opencode"]) {
9+
expect(providerSupportsSteering(ProviderDriverKind.make(driver))).toBe(true);
10+
}
11+
});
12+
13+
it("refuses codex, which always opens a turn rather than steering", () => {
14+
// CodexSessionRuntime issues `turn/start` unconditionally and overwrites
15+
// activeTurnId; the protocol's `turn/steer` RPC is never called.
16+
expect(providerSupportsSteering(ProviderDriverKind.make("codex"))).toBe(false);
17+
});
18+
19+
it("fails closed for an unknown or absent driver", () => {
20+
// An allowlist, not a denylist: a refused steer is invisible to the user
21+
// (the send command has already succeeded by the time the adapter refuses),
22+
// so an unrecognised driver must queue rather than risk losing a message.
23+
expect(providerSupportsSteering(ProviderDriverKind.make("some-future-driver"))).toBe(false);
24+
expect(providerSupportsSteering(null)).toBe(false);
25+
expect(providerSupportsSteering(undefined)).toBe(false);
26+
});
27+
});
28+
29+
describe("canSteerActiveThread", () => {
30+
const steerable = {
31+
phase: "running",
32+
isSendBusy: false,
33+
isRevertingCheckpoint: false,
34+
provider: ProviderDriverKind.make("claudeAgent"),
35+
};
36+
37+
it("steers a running turn on a capable provider", () => {
38+
expect(canSteerActiveThread(steerable)).toBe(true);
39+
});
40+
41+
it("does not steer a thread that is not running", () => {
42+
expect(canSteerActiveThread({ ...steerable, phase: "idle" })).toBe(false);
43+
});
44+
45+
it("does not steer while a local dispatch is still in flight", () => {
46+
// The immediate-send path bails on isSendBusy, so a submit here would be
47+
// dropped rather than sent; queuing keeps the message.
48+
expect(canSteerActiveThread({ ...steerable, isSendBusy: true })).toBe(false);
49+
});
50+
51+
it("does not steer while a checkpoint revert is in progress", () => {
52+
expect(canSteerActiveThread({ ...steerable, isRevertingCheckpoint: true })).toBe(false);
53+
});
54+
55+
it("does not steer a provider that opens a new turn instead", () => {
56+
expect(canSteerActiveThread({ ...steerable, provider: ProviderDriverKind.make("codex") })).toBe(
57+
false,
58+
);
59+
});
60+
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)