Skip to content

Commit 7634e08

Browse files
committed
fix(web): link checked-out PRs with in-flight sends and late thread shells
Three checkout-linking races left a checked-out pull request unlinked, or put a handoff task on the wrong draft: a send read the draft from its render snapshot, so a checkout that recorded its pull request while the send awaited was never linked; the send now reads the live draft session just before dispatch. A checkout landing on a sent draft waited a fixed five seconds for the thread shell and gave up; the link now waits for the shell to arrive (untilAtomValue / untilThreadShell). PR task handoffs wrote the task to the first opened draft, which is not the checkout draft once the user typed into it while the checkout ran; the task now goes on the checkout draft in both handoff handlers. Model: Swift 1.5 (Qwen3.8-27B fine-tune, running locally) through OpenCode
1 parent 1db5576 commit 7634e08

7 files changed

Lines changed: 147 additions & 10 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,12 @@ export function useComposerSend(input: ComposerSendInput) {
12841284
submissionIntent === "background" && isLocalDraftThread
12851285
? scopeThreadRef(environmentId, threadIdForSend)
12861286
: null;
1287-
// Read before sending: once the thread exists the draft holding it is finalized away.
1287+
// Read the live session just before dispatch: a checkout can record its pull request
1288+
// while this send awaits, and the draft is finalized away once the thread exists.
12881289
const checkoutPullRequestUrl = isLocalDraftThread
1289-
? checkoutPullRequestUrlToLink(draftThread)
1290+
? checkoutPullRequestUrlToLink(
1291+
draftId === null ? null : useComposerDraftStore.getState().getDraftSession(draftId),
1292+
)
12901293
: null;
12911294
// Recorded together with the read above, so a checkout finishing on either side of it is
12921295
// linked exactly once: by this send, or by the checkout after it.

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
22
import type { UnifiedSettings } from "@t3tools/contracts";
33
import { useStableCallback } from "../../hooks/useStableCallback";
4-
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
4+
import {
5+
scopeProjectRef,
6+
scopeThreadRef,
7+
scopedThreadKey,
8+
} from "@t3tools/client-runtime/environment";
59
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
10+
import { useEffect, useState } from "react";
611
import { useNavigate } from "@tanstack/react-router";
712
import { DEFAULT_INTERACTION_MODE } from "../../types";
813
import { newDraftId, newThreadId } from "~/lib/utils";
@@ -18,7 +23,7 @@ import {
1823
DraftId,
1924
} from "../../composerDraftStore";
2025
import { openPullRequestThreadDialog } from "./ChatOverlayHosts";
21-
import { readThreadShell, waitForThreadShell } from "../../state/entities";
26+
import { readThreadShell, untilThreadShell } from "../../state/entities";
2227
import { checkoutLandedOnSentDraft } from "../ChatView.logic";
2328
import { type EnvironmentId, type ScopedThreadRef, type ThreadId } from "@t3tools/contracts";
2429
interface PullRequestDraftThreadInput {
@@ -72,6 +77,34 @@ export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
7277
};
7378
const openPullRequestDialogStable = useStableCallback(openPullRequestDialog);
7479

80+
// The shell can arrive long after preparation (remote environments), so a checkout that
81+
// lands on a sent draft links once the shell exists, without a timeout.
82+
const [pendingShellLinks, setPendingShellLinks] = useState<
83+
ReadonlyMap<string, { readonly threadRef: ScopedThreadRef; readonly url: string }>
84+
>(new Map());
85+
const linkPendingShellStable = useStableCallback(
86+
(pending: { readonly threadRef: ScopedThreadRef; readonly url: string }) => {
87+
linkCheckoutPullRequest(pending.threadRef, pending.url);
88+
},
89+
);
90+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the thread-shell atom so each pending checkout link fires the moment the shell arrives
91+
useEffect(() => {
92+
if (pendingShellLinks.size === 0) return;
93+
const stops = Array.from(pendingShellLinks.entries()).map(([threadKey, pending]) =>
94+
untilThreadShell(pending.threadRef, () => {
95+
linkPendingShellStable(pending);
96+
setPendingShellLinks((current) => {
97+
const next = new Map(current);
98+
next.delete(threadKey);
99+
return next;
100+
});
101+
}),
102+
);
103+
return () => {
104+
for (const stop of stops) stop();
105+
};
106+
}, [pendingShellLinks, linkPendingShellStable]);
107+
75108
const openOrReuseProjectDraftThread = async (input: {
76109
branch: string;
77110
worktreePath: string | null;
@@ -163,9 +196,11 @@ export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
163196
}
164197
// A send that fails never creates the thread; the draft keeps the pull request and its
165198
// next send links it.
166-
if (await waitForThreadShell(threadRef)) {
167-
linkCheckoutPullRequest(threadRef, input.pullRequestUrl);
168-
}
199+
setPendingShellLinks((current) => {
200+
const next = new Map(current);
201+
next.set(scopedThreadKey(threadRef), { threadRef, url: input.pullRequestUrl });
202+
return next;
203+
});
169204
};
170205
const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
171206
return {

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,9 @@ export function PullRequestDetailPanel({
958958
);
959959
return;
960960
}
961-
await openThreadWithTask(projectRef, task, opened);
961+
// The task goes on the checkout draft: if the user typed into `opened` while the
962+
// checkout ran, that is no longer the draft the user is looking at.
963+
await openThreadWithTask(projectRef, task, pointed);
962964
toastManager.update(
963965
toastId,
964966
prepared.value.isOnPullRequestHead

‎apps/web/src/components/pullRequest/usePullRequestActions.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ export function usePullRequestHandoffs({
400400
);
401401
return;
402402
}
403-
await openThreadWithTask(projectRef, task, opened);
403+
// The task goes on the checkout draft: if the user typed into `opened` while the
404+
// checkout ran, that is no longer the draft the user is looking at.
405+
await openThreadWithTask(projectRef, task, pointed);
404406
toastManager.update(
405407
toastId,
406408
prepared.value.isOnPullRequestHead

‎apps/web/src/state/entities.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
allEnvironmentShellsBootstrappedAtom,
3737
} from "./shell";
3838
import { environmentThreadDetails, environmentThreadShells } from "./threads";
39-
import { waitForAtomValue } from "./waitForAtomValue";
39+
import { untilAtomValue, waitForAtomValue } from "./waitForAtomValue";
4040

4141
const EMPTY_THREAD_REFS: ReadonlyArray<ScopedThreadRef> = Object.freeze([]);
4242
const EMPTY_VISIBLE_TURN_ITEMS: ReadonlyArray<OrchestrationV2ProjectedTurnItem> = Object.freeze([]);
@@ -321,6 +321,21 @@ export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Pro
321321
});
322322
}
323323

324+
/** Reports once the thread shell exists, or immediately if it already does. */
325+
export function untilThreadShell(
326+
ref: ScopedThreadRef,
327+
onArrival: (shell: EnvironmentThreadShell) => void,
328+
): () => void {
329+
return untilAtomValue({
330+
registry: appAtomRegistry,
331+
atom: environmentThreadShells.threadShellAtom(ref),
332+
predicate: (shell) => shell !== null,
333+
onArrival: (shell) => {
334+
if (shell !== null) onArrival(shell);
335+
},
336+
});
337+
}
338+
324339
export function useEnvironmentSupportsServerBrowser(environmentId: EnvironmentId | null): boolean {
325340
const configs = useServerConfigs();
326341
return (
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Atom, AtomRegistry } from "effect/reactivity";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { untilAtomValue } from "./waitForAtomValue";
5+
6+
describe("untilAtomValue", () => {
7+
it("reports immediately when the current value already qualifies", () => {
8+
const registry = AtomRegistry.make();
9+
const atom = Atom.make<number>(10);
10+
const seen: number[] = [];
11+
const stop = untilAtomValue({
12+
registry,
13+
atom,
14+
predicate: (value) => value >= 5,
15+
onArrival: (value) => seen.push(value),
16+
});
17+
expect(seen).toEqual([10]);
18+
stop();
19+
registry.set(atom, 20);
20+
expect(seen).toEqual([10]);
21+
});
22+
23+
it("reports the first qualifying value and ignores the rest", () => {
24+
const registry = AtomRegistry.make();
25+
const atom = Atom.make<number>(1);
26+
const seen: number[] = [];
27+
const stop = untilAtomValue({
28+
registry,
29+
atom,
30+
predicate: (value) => value >= 5,
31+
onArrival: (value) => seen.push(value),
32+
});
33+
registry.set(atom, 2);
34+
registry.set(atom, 5);
35+
registry.set(atom, 7);
36+
expect(seen).toEqual([5]);
37+
stop();
38+
});
39+
40+
it("stops reporting once the caller unsubscribes", () => {
41+
const registry = AtomRegistry.make();
42+
const atom = Atom.make<number>(1);
43+
const seen: number[] = [];
44+
const stop = untilAtomValue({
45+
registry,
46+
atom,
47+
predicate: (value) => value >= 5,
48+
onArrival: (value) => seen.push(value),
49+
});
50+
stop();
51+
registry.set(atom, 5);
52+
expect(seen).toEqual([]);
53+
});
54+
});

‎apps/web/src/state/waitForAtomValue.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,29 @@ export async function waitForAtomValue<A>(input: {
5454
}, input.timeoutMs);
5555
});
5656
}
57+
58+
/**
59+
* Reports the first value satisfying `predicate`, then unsubscribes; an already-satisfying
60+
* current value reports immediately. Returns the subscription's unsubscribe, a no-op once a
61+
* value has arrived.
62+
*/
63+
export function untilAtomValue<A>(input: {
64+
readonly registry: AtomRegistry.AtomRegistry;
65+
readonly atom: Atom.Atom<A>;
66+
readonly predicate: (value: A) => boolean;
67+
readonly onArrival: (value: A) => void;
68+
}): () => void {
69+
const current = input.registry.get(input.atom);
70+
if (input.predicate(current)) {
71+
input.onArrival(current);
72+
return () => {};
73+
}
74+
let settled = false;
75+
const unsubscribe = input.registry.subscribe(input.atom, (value) => {
76+
if (settled || !input.predicate(value)) return;
77+
settled = true;
78+
unsubscribe();
79+
input.onArrival(value);
80+
});
81+
return unsubscribe;
82+
}

0 commit comments

Comments
 (0)