Skip to content

Commit 9304400

Browse files
Silthusclaude
andcommitted
fix(web): drop a balanced no-project move whose target is no longer a candidate
A balanced move that waits for a no-project folder now checks, when it settles, that its target is still a load-balancing candidate. Referencing a thread on another machine while the folder opens no longer lands the draft where that thread cannot be read; balancing picks again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ba0a8a8 commit 9304400

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ vi.mock("../../composerDraftStore", () => {
3434
};
3535
});
3636
vi.mock("../../hooks/useLoadBalancedEnvironment", () => ({
37-
useLoadBalancedEnvironment: () => ({
37+
useLoadBalancedEnvironment: (candidates: readonly string[]) => ({
3838
refresh: () => {},
3939
pending: false,
40-
environmentId: "box",
40+
environmentId: candidates.includes("box") ? "box" : (candidates[0] ?? null),
4141
failed: false,
4242
}),
4343
}));
@@ -87,6 +87,7 @@ type Routing = ReturnType<typeof useDraftEnvironmentRouting>;
8787

8888
function ScratchDraft(props: {
8989
readonly eligible?: boolean;
90+
readonly referencedEnvironmentId?: EnvironmentId;
9091
readonly onRouting?: (routing: Routing) => void;
9192
}) {
9293
const draftThread = useComposerDraftStore((store) => store.getDraftSession(draftId));
@@ -114,7 +115,9 @@ function ScratchDraft(props: {
114115
[box, [provider]],
115116
]) as never,
116117
loadBalancingSettings: { loadBalancingWeights: {} },
117-
referencedThreadContexts: [],
118+
referencedThreadContexts: props.referencedEnvironmentId
119+
? [{ environmentId: props.referencedEnvironmentId } as never]
120+
: [],
118121
activeProviderInstanceId: null,
119122
selectedProvider: codex,
120123
composerHasAttachments: false,
@@ -166,18 +169,21 @@ describe("useDraftEnvironmentRouting", () => {
166169
expect(mocks.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
167170
});
168171

169-
it("drops a balanced move whose draft stopped being eligible while the folder opened", async () => {
172+
it.each([
173+
{ change: "a file is attached", props: { eligible: false } },
174+
{ change: "a thread on this machine is referenced", props: { referencedEnvironmentId: mac } },
175+
])("drops a balanced move when $change while the folder opens", async ({ props }) => {
170176
setup(boxScratch);
171177
const opens = deferOpens();
172178

173179
// The harness only runs the hook and renders nothing routed.
174180
await using screen = await render(<ScratchDraft />, { router: false });
175181
await vi.waitFor(() => expect(opens).toHaveLength(1));
176-
await screen.rerender(<ScratchDraft eligible={false} />);
182+
await screen.rerender(<ScratchDraft {...props} />);
177183
await act(() => opens[0]?.(boxScratch));
178184

179-
expect(mocks.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
180185
expect(mocks.draft.environmentId).toBe(mac);
186+
expect(mocks.draft.loadBalancedEnvironmentId).not.toBe(box);
181187
});
182188

183189
it("lets Auto balance win over a manual switch that is still opening", async () => {

‎apps/web/src/components/chat/useDraftEnvironmentRouting.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
128128
readonly scope: string;
129129
} | null>(null);
130130
const environmentChangeScopeRef = useRef("");
131-
const automaticEnvironmentRef = useRef(automaticEnvironment);
132-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mirrors the current change scope and routing mode into refs that async environment switches compare against
131+
const balancingCandidatesRef = useRef(loadBalancingCandidates);
132+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mirrors the current change scope and balancing candidates into refs that async environment switches compare against
133133
useLayoutEffect(() => {
134134
environmentChangeScopeRef.current = environmentChangeScope;
135-
automaticEnvironmentRef.current = automaticEnvironment;
135+
balancingCandidatesRef.current = loadBalancingCandidates;
136136
});
137137
// Leaving the scope, or unmounting, abandons a change still in flight.
138138
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- abandons an in-flight environment change when the draft or project scope changes or unmounts
@@ -198,7 +198,7 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
198198
return (
199199
environmentChangeRef.current === request &&
200200
environmentChangeScopeRef.current === request.scope &&
201-
(!automatic || automaticEnvironmentRef.current) &&
201+
(!automatic || balancingCandidatesRef.current.includes(target.environmentId)) &&
202202
!sendInFlightRef.current &&
203203
currentDraft !== null &&
204204
!currentDraft.promotedTo &&

0 commit comments

Comments
 (0)