Skip to content

Commit f35ccb9

Browse files
t3dotggclaude
andcommitted
fix(web): pin the setup card to its dispatch environment and finish review nits
- The card's subscription and cancel now target the environment that received the dispatch, so changing the draft's machine mid-setup does not lose the card or cancel the wrong server. - The "Work locally" resend flag stays set until onSend gets past its own guards, so a reconnect or loading thread in between does not drop it. - The tracker forgets a thread's sequence counter when retention removes its snapshot. - Card layout: long stage details ellipsize, and the output tail no longer clips its newest line. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 79fad28 commit f35ccb9

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

‎apps/server/src/project/WorktreeSetupTracker.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ export const make = Effect.gen(function* () {
142142
const next = new Map(current);
143143
next.delete(threadId);
144144
return next;
145-
}).pipe(Effect.andThen(publish(threadId, null)));
145+
}).pipe(
146+
Effect.andThen(publish(threadId, null)),
147+
// A subscriber that outlives retention sees `null` here and accepts any
148+
// sequence after it, so the counter can start over for this thread.
149+
Effect.tap(() => Effect.sync(() => lastSequenceByThread.delete(threadId))),
150+
);
146151

147152
const begin: WorktreeSetupTracker["Service"]["begin"] = (input) =>
148153
Effect.gen(function* () {
@@ -291,7 +296,7 @@ export const make = Effect.gen(function* () {
291296
Effect.sync(() => {
292297
if (change.threadId !== threadId) return;
293298
if (change.snapshot !== null && change.snapshot.sequence <= lastSequence) return;
294-
lastSequence = change.snapshot?.sequence ?? lastSequence;
299+
lastSequence = change.snapshot?.sequence ?? -1;
295300
Queue.offerUnsafe(mailbox, change.snapshot);
296301
}),
297302
),

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

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,7 @@ export default function ChatView(props: ChatViewProps) {
16391639
// worktree send starts and cleared once the turn starts or the next send
16401640
// begins, so a failed or cancelled card stays until the user acts.
16411641
const [worktreeSetupRef, setWorktreeSetupRef] = useState<{
1642+
environmentId: EnvironmentId;
16421643
threadId: ThreadId;
16431644
ownerKey: string;
16441645
} | null>(null);
@@ -3340,10 +3341,13 @@ export default function ChatView(props: ChatViewProps) {
33403341
const worktreeSetupOwnerKey = draftId ?? routeThreadKey;
33413342
const worktreeSetupActive =
33423343
worktreeSetupRef !== null && worktreeSetupRef.ownerKey === worktreeSetupOwnerKey;
3344+
// The setup runs on the environment that received the dispatch, so both
3345+
// the subscription and cancel target that one even if the draft's machine
3346+
// picker changes underneath.
33433347
const worktreeSetupQuery = useEnvironmentQuery(
33443348
worktreeSetupActive
33453349
? vcsEnvironment.worktreeSetup({
3346-
environmentId,
3350+
environmentId: worktreeSetupRef.environmentId,
33473351
input: { threadId: worktreeSetupRef.threadId },
33483352
})
33493353
: null,
@@ -3371,9 +3375,12 @@ export default function ChatView(props: ChatViewProps) {
33713375
reportFailure: false,
33723376
});
33733377
const onCancelWorktreeSetup = useCallback(() => {
3374-
if (!worktreeSetup || worktreeSetup.phase !== "running") return;
3375-
void cancelWorktreeSetup({ environmentId, input: { threadId: worktreeSetup.threadId } });
3376-
}, [cancelWorktreeSetup, environmentId, worktreeSetup]);
3378+
if (!worktreeSetup || !worktreeSetupRef || worktreeSetup.phase !== "running") return;
3379+
void cancelWorktreeSetup({
3380+
environmentId: worktreeSetupRef.environmentId,
3381+
input: { threadId: worktreeSetup.threadId },
3382+
});
3383+
}, [cancelWorktreeSetup, worktreeSetup, worktreeSetupRef]);
33773384
// The setup terminal belongs to the thread that was set up. A failed
33783385
// bootstrap deletes that thread and closes its terminals, so only offer the
33793386
// terminal while the setup thread is still the active one.
@@ -7228,7 +7235,9 @@ export default function ChatView(props: ChatViewProps) {
72287235
submissionIntent: resolvedSubmissionIntent,
72297236
});
72307237
setWorktreeSetupRef(
7231-
baseBranchForWorktree ? { threadId: threadIdForSend, ownerKey: worktreeSetupOwnerKey } : null,
7238+
baseBranchForWorktree
7239+
? { environmentId, threadId: threadIdForSend, ownerKey: worktreeSetupOwnerKey }
7240+
: null,
72327241
);
72337242

72347243
const messageIdForSend = newMessageId();
@@ -8278,23 +8287,42 @@ export default function ChatView(props: ChatViewProps) {
82788287
// the bootstrap. The cancelled dispatch fails and puts the message back in
82798288
// the composer; the effect below resends it once that has settled.
82808289
const onWorktreeSetupWorkLocally = useCallback(() => {
8281-
if (!worktreeSetup || worktreeSetup.phase !== "running" || !isLocalDraftThread) return;
8282-
const threadId = worktreeSetup.threadId;
8290+
if (
8291+
!worktreeSetup ||
8292+
!worktreeSetupRef ||
8293+
worktreeSetup.phase !== "running" ||
8294+
!isLocalDraftThread
8295+
) {
8296+
return;
8297+
}
8298+
const target = {
8299+
environmentId: worktreeSetupRef.environmentId,
8300+
input: { threadId: worktreeSetup.threadId },
8301+
};
82838302
void (async () => {
8284-
const result = await cancelWorktreeSetup({ environmentId, input: { threadId } });
8303+
const result = await cancelWorktreeSetup(target);
82858304
if (result._tag !== "Success" || !result.value.cancelled) return;
82868305
onEnvModeChange("local");
82878306
setWorkLocallyResendPending(true);
82888307
})();
8289-
}, [cancelWorktreeSetup, environmentId, isLocalDraftThread, onEnvModeChange, worktreeSetup]);
8308+
}, [cancelWorktreeSetup, isLocalDraftThread, onEnvModeChange, worktreeSetup, worktreeSetupRef]);
82908309
const onSendRef = useRef(onSend);
82918310
onSendRef.current = onSend;
8311+
// Resend once the cancelled dispatch has settled and the composer is free.
8312+
// The flag stays set until `onSend` gets past its own guards, so a reconnect
8313+
// or a loading thread in between does not lose the message.
8314+
const workLocallyResendReady =
8315+
workLocallyResendPending &&
8316+
!isSendBusy &&
8317+
!isConnecting &&
8318+
!threadDetailLoading &&
8319+
clientSettingsHydrated &&
8320+
sendEnvMode === "local";
82928321
useEffect(() => {
8293-
if (!workLocallyResendPending || isSendBusy || sendInFlightRef.current) return;
8294-
if (sendEnvMode !== "local") return;
8322+
if (!workLocallyResendReady || sendInFlightRef.current) return;
82958323
setWorkLocallyResendPending(false);
82968324
void onSendRef.current();
8297-
}, [isSendBusy, sendEnvMode, workLocallyResendPending]);
8325+
}, [workLocallyResendReady]);
82988326

82998327
const onStartFromOriginChange = (nextStartFromOrigin: boolean) => {
83008328
if (canOverrideServerThreadEnvMode && activeThread) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function StageRow({
121121
<StageIcon status={stage.status} />
122122
</span>
123123
<span className="min-w-0 flex-1 truncate">{label}</span>
124-
<span className="flex shrink-0 items-center gap-2 text-xs text-muted-foreground tabular-nums">
124+
<span className="flex min-w-0 items-center gap-2 text-xs text-muted-foreground tabular-nums">
125125
{showBar ? (
126126
<>
127127
<span className="h-1 w-18 overflow-hidden rounded-full bg-input">
@@ -147,7 +147,7 @@ function OutputTail({ lines, failed }: { lines: ReadonlyArray<string>; failed: b
147147
return (
148148
<pre
149149
className={cn(
150-
"mb-1 ml-8 max-h-20 overflow-hidden rounded-md border px-2.5 py-1.5 font-mono text-[11px] leading-relaxed whitespace-pre-wrap break-all select-text",
150+
"mb-1 ml-8 rounded-md border px-2.5 py-1.5 font-mono text-[11px] leading-relaxed whitespace-pre-wrap break-all select-text",
151151
failed
152152
? "border-destructive/20 bg-error-surface text-destructive-foreground"
153153
: "border-border bg-code text-muted-foreground",

0 commit comments

Comments
 (0)