Skip to content

Commit 3ea12f2

Browse files
radroidcoil-sync-bot
authored andcommitted
fix(web): steering-path leftovers from the queue transition (#40 A1-A4)
Partial for #40 — section A items 1 through 4, in the order the issue's own triage ranked them. A5/A6 and all of section B are deliberately left open. A4 (the only wrong-outcome bug). `canSteerActiveThread` was keyed on the composer's `selectedProvider`, which resolves through `deriveLockedProvider` and then `resolveSelectableProvider` — whose fallback is *the first enabled provider*. A thread whose provider instance was disabled or removed, or whose session names a driver kind this build does not recognise, could therefore read as `claudeAgent` in the composer while the server still routed it to Codex: the exact case the allowlist exists to prevent. It now keys on the thread's persisted `session.providerName` via a new pure `steerProviderBinding`, so an absent or unrecognised binding misses the allowlist and queues. That is what makes b9d94d9's "fail closed" promise actually hold. `providerSupportsSteering` takes an open string now, because the session binding is typed as a plain non-empty string and narrowing it to the brand first would have to invent a meaning for unrecognised values that the allowlist already answers. A1 + A2 (one fix). The drain shipped the enqueue-time `createdAt` on all three outgoing commands. The snapshot sorts `ORDER BY thread_id, created_at ASC, message_id`, so a reordered queue delivered rows in the new order and rebuilt the transcript in the old one — invisible live, permanent from the first reload. The same stale stamp put `hasQueuedTurnStart` outside its 2-minute `QUEUED_TURN_START_GRACE_MS` window for any message that waited longer than that (the normal case), so `canSettle` read the thread as settleable with the anti-double-send gate already expired. Now stamped at dispatch, which is what upstream's immediate-send path does. The queue's own `createdAt` is untouched and still orders the queue list. A3. `logComposerDispatch` fired at the top of `onSend`, above every guard, so Enter on an empty composer logged `composer dispatch: steer` for a submit that never happened — and per outboxDiagnostics.ts that line is the only client-side evidence of whether a message steered or queued. It now fires at each path's real dispatch point (immediately before the enqueue, and immediately before `sendInFlightRef.current = true`). The record also gained a `steerProvider` field beside `provider`, so the picker-vs-binding divergence that caused A4 is visible in one line rather than inferable. C2. `SEAMS.md`'s "Parallel paths" table had one row and did not include the steering path, which is the more dangerous of the two — a second submit path into a turn that is already running, gated by a hand-asserted allowlist that no upstream change can conflict with. Registered, with the re-check instruction. The `ChatView.tsx` row moves +186 -> +210 (deletions stay at 1, so the additive-seam invariant holds) and the header totals are regenerated with the ledger's own recipe: 37 files, +1994/-912. That run also surfaced a pre-existing 13-line drift in the header, noted inline. Verified: `vp test run apps/web/src/outbox apps/web/src/components/ChatView.logic.test.ts` 79 passed; `vp lint` clean; `tsgo --noEmit -p apps/web` 0 errors.
1 parent d695f9d commit 3ea12f2

6 files changed

Lines changed: 187 additions & 29 deletions

File tree

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +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";
187+
import { canSteerActiveThread, steerProviderBinding } from "~/outbox/composerSteering.logic";
188188
import { logComposerDispatch } from "~/outbox/outboxDiagnostics";
189189
import { ThreadOutboxQueueList } from "./chat/ThreadOutboxQueueList";
190190
import { getProviderModelCapabilities, resolveSelectableProvider } from "../providerModels";
@@ -2330,11 +2330,16 @@ function ChatViewContent(props: ChatViewProps) {
23302330
const composerActiveThreadBusy = phase === "running" || isSendBusy || isRevertingCheckpoint;
23312331
// A running turn is not on its own a reason to queue: steer-capable drivers
23322332
// fold a mid-turn send into the work in progress.
2333+
//
2334+
// Keyed on the thread's routing binding, not `selectedProvider` — the picker
2335+
// falls back to the first enabled provider, so it can classify a Codex thread
2336+
// as steerable (radroid/t3code#40 A4). See `steerProviderBinding`.
2337+
const composerSteerProvider = steerProviderBinding(activeThread);
23332338
const composerCanSteerActiveThread = canSteerActiveThread({
23342339
phase,
23352340
isSendBusy,
23362341
isRevertingCheckpoint,
2337-
provider: selectedProvider,
2342+
provider: composerSteerProvider,
23382343
});
23392344
const composerSendLabel = resolveComposerSendLabel({
23402345
connectionState: activeEnvironmentConnectionPhase,
@@ -4935,6 +4940,34 @@ function ChatViewContent(props: ChatViewProps) {
49354940
],
49364941
);
49374942

4943+
// One breadcrumb per submit that actually dispatches: whether it queued, sent,
4944+
// or steered into a running turn, plus every input to that decision. A refused
4945+
// steer is invisible on the client, so this is the only client-side evidence —
4946+
// which is why it is fired at each path's real dispatch point rather than at
4947+
// the top of `onSend`, where it also logged submits the guards had already
4948+
// rejected (Enter on an empty composer logged `steer`; radroid/t3code#40 A3).
4949+
const logComposerDispatchNow = useCallback(() => {
4950+
logComposerDispatch({
4951+
queued: composerQueueMode,
4952+
steerable: composerCanSteerActiveThread,
4953+
phase,
4954+
queueCount: outboxQueue.length,
4955+
connection: activeEnvironmentConnectionPhase,
4956+
provider: selectedProvider,
4957+
steerProvider: composerSteerProvider,
4958+
threadId: activeThread?.id ?? null,
4959+
});
4960+
}, [
4961+
activeEnvironmentConnectionPhase,
4962+
activeThread?.id,
4963+
composerCanSteerActiveThread,
4964+
composerQueueMode,
4965+
composerSteerProvider,
4966+
outboxQueue.length,
4967+
phase,
4968+
selectedProvider,
4969+
]);
4970+
49384971
// Enqueue the current composer content into the thread outbox. Text-only:
49394972
// terminal/element/preview/review contexts are folded into the prompt like a
49404973
// normal send, but image attachments cannot survive a reload through
@@ -5007,6 +5040,7 @@ function ChatViewContent(props: ChatViewProps) {
50075040
text: messageTextForSend,
50085041
});
50095042

5043+
logComposerDispatchNow();
50105044
try {
50115045
await enqueueThreadOutboxMessage({
50125046
environmentId: activeThread.environmentId,
@@ -5051,6 +5085,7 @@ function ChatViewContent(props: ChatViewProps) {
50515085
composerDraftTarget,
50525086
composerRef,
50535087
interactionMode,
5088+
logComposerDispatchNow,
50545089
promptRef,
50555090
runtimeMode,
50565091
setThreadError,
@@ -5074,18 +5109,6 @@ function ChatViewContent(props: ChatViewProps) {
50745109
}),
50755110
);
50765111
};
5077-
// One breadcrumb per submit: whether it queued, sent, or steered into a
5078-
// running turn, plus every input to that decision. A refused steer is
5079-
// invisible on the client, so this is the only client-side evidence.
5080-
logComposerDispatch({
5081-
queued: composerQueueMode,
5082-
steerable: composerCanSteerActiveThread,
5083-
phase,
5084-
queueCount: outboxQueue.length,
5085-
connection: activeEnvironmentConnectionPhase,
5086-
provider: selectedProvider,
5087-
threadId: activeThread?.id ?? null,
5088-
});
50895112
// Queue mode routes the submit through the thread outbox instead of the
50905113
// immediate-send path (which is gated on an idle, connected thread).
50915114
//
@@ -5296,6 +5319,7 @@ function ChatViewContent(props: ChatViewProps) {
52965319
return;
52975320
}
52985321

5322+
logComposerDispatchNow();
52995323
sendInFlightRef.current = true;
53005324
if (isDraftHeroState && activeThreadKey) {
53015325
let resolveDockStarted: (() => void) | undefined;

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { ProviderDriverKind } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import { canSteerActiveThread, providerSupportsSteering } from "./composerSteering.logic";
4+
import {
5+
canSteerActiveThread,
6+
providerSupportsSteering,
7+
steerProviderBinding,
8+
} from "./composerSteering.logic";
59

610
describe("providerSupportsSteering", () => {
711
it("allows the drivers whose adapters fold a mid-turn send into the running turn", () => {
@@ -58,3 +62,55 @@ describe("canSteerActiveThread", () => {
5862
);
5963
});
6064
});
65+
66+
// radroid/t3code#40 A4. The steer decision used to key on the composer's
67+
// `selectedProvider`, which resolves through `deriveLockedProvider` and then
68+
// `resolveSelectableProvider` — whose fallback is the first ENABLED provider.
69+
describe("steerProviderBinding", () => {
70+
it("reads the thread's persisted routing binding", () => {
71+
expect(steerProviderBinding({ session: { providerName: "codex" } })).toBe("codex");
72+
});
73+
74+
it("fails closed when the thread has no session binding yet", () => {
75+
expect(steerProviderBinding({ session: null })).toBeNull();
76+
expect(steerProviderBinding({})).toBeNull();
77+
expect(steerProviderBinding(null)).toBeNull();
78+
expect(steerProviderBinding(undefined)).toBeNull();
79+
});
80+
81+
// The whole point of A4: a Codex thread must not be classified steer-capable
82+
// because the picker fell back to a Claude instance.
83+
it("keeps a Codex thread unsteerable regardless of what the picker shows", () => {
84+
const thread = { session: { providerName: "codex" } };
85+
expect(
86+
canSteerActiveThread({
87+
phase: "running",
88+
isSendBusy: false,
89+
isRevertingCheckpoint: false,
90+
provider: steerProviderBinding(thread),
91+
}),
92+
).toBe(false);
93+
// ...and the old behaviour, for contrast: the picker's value alone said yes.
94+
expect(
95+
canSteerActiveThread({
96+
phase: "running",
97+
isSendBusy: false,
98+
isRevertingCheckpoint: false,
99+
provider: ProviderDriverKind.make("claudeAgent"),
100+
}),
101+
).toBe(true);
102+
});
103+
104+
// An instance that was removed leaves a session naming a driver this build may
105+
// not recognise. That must queue, not steer.
106+
it("queues a thread whose binding is not on the allowlist", () => {
107+
expect(
108+
canSteerActiveThread({
109+
phase: "running",
110+
isSendBusy: false,
111+
isRevertingCheckpoint: false,
112+
provider: steerProviderBinding({ session: { providerName: "some-removed-driver" } }),
113+
}),
114+
).toBe(false);
115+
});
116+
});

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { ProviderDriverKind } from "@t3tools/contracts";
2-
31
/**
42
* Which provider drivers can take a message *during* a running turn.
53
*
@@ -47,10 +45,40 @@ const STEERABLE_PROVIDER_DRIVER_KINDS: ReadonlySet<string> = new Set([
4745
"opencode",
4846
]);
4947

50-
export function providerSupportsSteering(driver: ProviderDriverKind | null | undefined): boolean {
48+
// Takes an open string rather than a branded `ProviderDriverKind` on purpose:
49+
// the value that decides this is the session's `providerName` (see
50+
// `steerProviderBinding`), which the contracts type as a plain non-empty
51+
// string. Narrowing it to the brand first would have to decide what an
52+
// unrecognised value means, and the allowlist already answers that — it misses,
53+
// so it queues.
54+
export function providerSupportsSteering(driver: string | null | undefined): boolean {
5155
return driver !== null && driver !== undefined && STEERABLE_PROVIDER_DRIVER_KINDS.has(driver);
5256
}
5357

58+
/**
59+
* The driver the SERVER will route this submit to.
60+
*
61+
* A steer decision has to key on the thread's persisted binding, not on the
62+
* composer's picker. `selectedProvider` resolves through `deriveLockedProvider`
63+
* and then `resolveSelectableProvider`, whose fallback is *the first enabled
64+
* provider* — so a thread whose provider instance was disabled or removed, or
65+
* whose session carries a driver kind this build does not recognise, can read
66+
* as `claudeAgent` in the composer while the server still routes it to Codex.
67+
* That is exactly the case the allowlist exists to prevent, and keying off the
68+
* picker made the "fail closed" promise unenforceable (radroid/t3code#40 A4).
69+
*
70+
* Returned raw: an absent or unrecognised binding simply misses the allowlist
71+
* and the submit queues, which is the safe direction.
72+
*/
73+
export function steerProviderBinding(
74+
thread:
75+
| { readonly session?: { readonly providerName?: string | null } | null }
76+
| null
77+
| undefined,
78+
): string | null {
79+
return thread?.session?.providerName ?? null;
80+
}
81+
5482
/**
5583
* Whether the active thread is busy in a way the provider can absorb right now,
5684
* so a submit should go out immediately instead of into the outbox.
@@ -67,7 +95,8 @@ export function canSteerActiveThread(input: {
6795
readonly phase: string;
6896
readonly isSendBusy: boolean;
6997
readonly isRevertingCheckpoint: boolean;
70-
readonly provider: ProviderDriverKind | null | undefined;
98+
/** The thread's routing binding — see `steerProviderBinding`, NOT the picker. */
99+
readonly provider: string | null | undefined;
71100
}): boolean {
72101
return (
73102
input.phase === "running" &&

‎apps/web/src/outbox/outboxDiagnostics.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,29 @@ export interface ComposerDispatchDiagnostics {
2626
readonly phase: string;
2727
readonly queueCount: number;
2828
readonly connection: EnvironmentConnectionPhase;
29+
/** What the composer's picker resolved to — display state, not routing. */
2930
readonly provider: ProviderDriverKind | null;
31+
/**
32+
* The thread's persisted routing binding, which is what the steer allowlist
33+
* actually consults. Logged separately from `provider` because the two can
34+
* disagree — a disabled or unrecognised instance makes the picker fall back
35+
* to the first enabled provider — and that divergence was itself the bug in
36+
* radroid/t3code#40 A4. Seeing both makes it visible in one line.
37+
*/
38+
readonly steerProvider: string | null;
3039
readonly threadId: ThreadId | null;
3140
}
3241

3342
/**
34-
* One line per composer submit, recording which way it went and every input to
35-
* that decision. `steer` is the case worth grepping for: it is the only one
36-
* that hands a message to a turn that is already running.
43+
* One line per composer submit **that actually dispatched**, recording which
44+
* way it went and every input to that decision. `steer` is the case worth
45+
* grepping for: it is the only one that hands a message to a turn that is
46+
* already running.
47+
*
48+
* Call this at each path's real dispatch point, never at the top of the submit
49+
* handler: a breadcrumb that also fires for submits a guard rejected (empty
50+
* composer, thread still loading, send already in flight) is worse than none,
51+
* because it is the only client-side evidence anyone has during triage.
3752
*/
3853
export function logComposerDispatch(input: ComposerDispatchDiagnostics): void {
3954
const decision = input.queued ? "queue" : input.steerable ? "steer" : "send";
@@ -42,6 +57,7 @@ export function logComposerDispatch(input: ComposerDispatchDiagnostics): void {
4257
phase: input.phase,
4358
connection: input.connection,
4459
provider: input.provider,
60+
steerProvider: input.steerProvider,
4561
queueCount: input.queueCount,
4662
threadId: input.threadId,
4763
});

‎apps/web/src/outbox/useThreadOutboxDrain.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ export function useThreadOutboxDrain(): void {
159159
async (queuedMessage: QueuedThreadMessage, thread: EnvironmentThreadShell) => {
160160
const settings = resolveQueuedThreadSettings(queuedMessage, thread);
161161
const { reportFailure, completeDelivery } = makeDeliveryHelpers(queuedMessage);
162+
// Stamped at DISPATCH, not carried over from enqueue (radroid/t3code#40 A1+A2).
163+
// This is what upstream's immediate-send path does, and shipping the enqueue
164+
// time instead broke two things at once:
165+
//
166+
// * the snapshot sorts `ORDER BY thread_id, created_at ASC, message_id`, so a
167+
// reordered queue delivered rows in the new order and rebuilt the transcript
168+
// in the old one. Invisible live (the reducer appends in arrival order) and
169+
// permanent from the first reload, reconnect resync, or thread reopen.
170+
// * `hasQueuedTurnStart` only recognises an unadopted turn start within
171+
// `QUEUED_TURN_START_GRACE_MS` (2 min) of the message time. A message that
172+
// waited longer than that — the normal case for a queued one — arrived with
173+
// the anti-double-send gate already expired, so `canSettle` read the thread
174+
// as settleable and a second `turn/start` could go out. On Codex that
175+
// orphans the first turn's events.
176+
//
177+
// The queue's own `queuedMessage.createdAt` is untouched; it still orders the
178+
// queue list (`threadOutbox.logic.ts` `sortKey ?? createdAt`).
179+
const dispatchedAt = new Date().toISOString();
162180

163181
if (!modelSelectionsEqual(settings.modelSelection, thread.modelSelection)) {
164182
const updateResult = await updateThreadMetadata({
@@ -182,7 +200,7 @@ export function useThreadOutboxDrain(): void {
182200
commandId: settingsCommandId(queuedMessage, "runtime-mode"),
183201
threadId: queuedMessage.threadId,
184202
runtimeMode: settings.runtimeMode,
185-
createdAt: queuedMessage.createdAt,
203+
createdAt: dispatchedAt,
186204
},
187205
});
188206
if (AsyncResult.isFailure(runtimeResult)) {
@@ -198,7 +216,7 @@ export function useThreadOutboxDrain(): void {
198216
commandId: settingsCommandId(queuedMessage, "interaction-mode"),
199217
threadId: queuedMessage.threadId,
200218
interactionMode: settings.interactionMode,
201-
createdAt: queuedMessage.createdAt,
219+
createdAt: dispatchedAt,
202220
},
203221
});
204222
if (AsyncResult.isFailure(interactionResult)) {
@@ -221,7 +239,7 @@ export function useThreadOutboxDrain(): void {
221239
modelSelection: settings.modelSelection,
222240
runtimeMode: settings.runtimeMode,
223241
interactionMode: settings.interactionMode,
224-
createdAt: queuedMessage.createdAt,
242+
createdAt: dispatchedAt,
225243
},
226244
});
227245
return completeDelivery(deliveryResult);

0 commit comments

Comments
 (0)