Skip to content

Commit c66dfd0

Browse files
fix(web): stop the worktree setup card from flashing and shifting
Promoting a draft to its server thread swapped route components, which unmounted ChatView and painted an empty timeline for a frame. The setup card also changed shape at every phase: its own header gave way to the working header, the stage list was replaced by a lone script row once the agent started, the output tail grew and wrapped with each line, and a script that outlived the reply trailed the assistant's message. Both thread routes now render one ThreadRouteView from the _chat layout, keyed by the thread id the draft already reserved, so promotion is a prop change on a mounted element. The working header is the only header for the setup's whole life and swaps its text in place. The stage list keeps its footprint until the agent takes over, then leaves; a still-running script is surfaced as a chip in the working header that opens the stages and live tail in a popover. The tail is a fixed four-line box that never wraps, and the server splits script output on bare carriage returns so progress redraws stay short lines. A clean finish leaves no trace; a failed script keeps a collapsed row under the send while the turn runs. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7a368fe commit c66dfd0

12 files changed

Lines changed: 528 additions & 364 deletions

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ describe("ProjectSetupScriptRunner", () => {
307307
// control sequences are stripped, and the echoed wrapper is hidden.
308308
yield* emit(`( bun install\r\n> ); printf '\\n${sentinel}%s\\n' "$?"\r\n`);
309309
yield* emit("\u001b[32mResolving");
310-
yield* emit(" deps\u001b[0m\r\nDone in 2s\r\n");
310+
yield* emit(" deps\u001b[0m\r\n");
311+
// Progress redraws separated by bare carriage returns are their own lines.
312+
yield* emit("Progress: 1/3\rProgress: 2/3\rProgress: 3/3\r\nDone in 2s\r\n");
311313
// A spoofed sentinel from the script itself must not settle completion.
312314
yield* emit("__T3_SETUP_DONE__:0\r\n");
313315
yield* emit(`__T3_SETUP_DONE___${"0".repeat(32)}:0\r\n`);
@@ -317,6 +319,9 @@ describe("ProjectSetupScriptRunner", () => {
317319
expect(completion.exitCode).toBe(3);
318320
expect(seen).toEqual([
319321
"Resolving deps",
322+
"Progress: 1/3",
323+
"Progress: 2/3",
324+
"Progress: 3/3",
320325
"Done in 2s",
321326
"__T3_SETUP_DONE__:0",
322327
`__T3_SETUP_DONE___${"0".repeat(32)}:0`,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ export const make = Effect.gen(function* () {
258258
}
259259
if (event.type === "output") {
260260
lineBuffer += event.data;
261-
const lines = lineBuffer.split(/\r?\n/);
261+
// A bare carriage return is how installers redraw a progress line in
262+
// place. Treating it as a line break keeps each redraw a short line
263+
// of its own instead of gluing every update into one long one.
264+
const lines = lineBuffer.split(/\r\n|\r|\n/);
262265
lineBuffer = lines.pop() ?? "";
263266
// A script that never prints a newline must not grow this forever.
264267
// The sentinel is always on its own line, so keeping the tail is safe.

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type ServerProvider,
1111
ThreadId,
1212
TurnId,
13+
type WorktreeSetupSnapshot,
1314
} from "@t3tools/contracts";
1415
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
1516
import { Atom, AsyncResult } from "effect/unstable/reactivity";
@@ -2533,7 +2534,12 @@ describe("worktree setup visibility", () => {
25332534
expect(findRecordedWorktreeSetup(activities, ThreadId.make("other"))).toBeNull();
25342535
});
25352536

2536-
it("shows a running setup and hides a clean one once the turn started", () => {
2537+
it("shows a running setup and drops a clean one once the turn started", () => {
2538+
const visible = (
2539+
snapshot: WorktreeSetupSnapshot | null,
2540+
turnStarted: boolean,
2541+
isWorking = true,
2542+
) => resolveVisibleWorktreeSetup({ live: null, recorded: snapshot, turnStarted, isWorking });
25372543
expect(
25382544
resolveVisibleWorktreeSetup({
25392545
live: base,
@@ -2542,80 +2548,37 @@ describe("worktree setup visibility", () => {
25422548
isWorking: true,
25432549
}),
25442550
).toEqual(base);
2545-
expect(
2546-
resolveVisibleWorktreeSetup({
2547-
live: null,
2548-
recorded: settledDone,
2549-
turnStarted: false,
2550-
isWorking: true,
2551-
}),
2552-
).toEqual(settledDone);
2553-
expect(
2554-
resolveVisibleWorktreeSetup({
2555-
live: null,
2556-
recorded: settledDone,
2557-
turnStarted: true,
2558-
isWorking: true,
2559-
}),
2560-
).toBeNull();
2551+
expect(visible(settledDone, false)).toEqual(settledDone);
2552+
expect(visible(settledDone, true)).toBeNull();
2553+
expect(visible(settledDone, true, false)).toBeNull();
2554+
expect(visible(null, true)).toBeNull();
25612555
});
25622556

25632557
it("keeps a failed script visible for the running turn and a failed setup always", () => {
25642558
const scriptFailed = {
25652559
...settledDone,
25662560
stages: [stage("checkout", "done"), stage("setup-script", "failed"), stage("agent", "done")],
25672561
};
2568-
expect(
2569-
resolveVisibleWorktreeSetup({
2570-
live: null,
2571-
recorded: scriptFailed,
2572-
turnStarted: true,
2573-
isWorking: true,
2574-
}),
2575-
).toEqual(scriptFailed);
2576-
expect(
2577-
resolveVisibleWorktreeSetup({
2578-
live: null,
2579-
recorded: scriptFailed,
2580-
turnStarted: true,
2581-
isWorking: false,
2582-
}),
2583-
).toBeNull();
2562+
const visible = (snapshot: WorktreeSetupSnapshot, isWorking: boolean) =>
2563+
resolveVisibleWorktreeSetup({ live: null, recorded: snapshot, turnStarted: true, isWorking });
2564+
expect(visible(scriptFailed, true)).toEqual(scriptFailed);
2565+
expect(visible(scriptFailed, false)).toBeNull();
25842566
const failed = { ...settledDone, phase: "failed" as const, error: "git exploded" };
2585-
expect(
2586-
resolveVisibleWorktreeSetup({
2587-
live: null,
2588-
recorded: failed,
2589-
turnStarted: true,
2590-
isWorking: false,
2591-
}),
2592-
).toEqual(failed);
2567+
expect(visible(failed, false)).toEqual(failed);
2568+
const cancelled = { ...settledDone, phase: "cancelled" as const };
2569+
expect(visible(cancelled, false)).toEqual(cancelled);
25932570
});
25942571

25952572
it("prefers whichever snapshot is newer by sequence", () => {
2596-
expect(
2597-
resolveVisibleWorktreeSetup({
2598-
live: { ...base, sequence: 3 },
2599-
recorded: { ...settledDone, sequence: 7 },
2600-
turnStarted: false,
2601-
isWorking: false,
2602-
}),
2603-
).toEqual({ ...settledDone, sequence: 7 });
2604-
expect(
2605-
resolveVisibleWorktreeSetup({
2606-
live: { ...settledDone, sequence: 9 },
2607-
recorded: { ...base, sequence: 1 },
2608-
turnStarted: false,
2609-
isWorking: false,
2610-
}),
2611-
).toEqual({ ...settledDone, sequence: 9 });
2612-
expect(
2613-
resolveVisibleWorktreeSetup({
2614-
live: base,
2615-
recorded: null,
2616-
turnStarted: false,
2617-
isWorking: false,
2618-
}),
2619-
).toEqual(base);
2573+
const pick = (live: WorktreeSetupSnapshot | null, recorded: WorktreeSetupSnapshot | null) =>
2574+
resolveVisibleWorktreeSetup({ live, recorded, turnStarted: false, isWorking: false });
2575+
expect(pick({ ...base, sequence: 3 }, { ...settledDone, sequence: 7 })).toEqual({
2576+
...settledDone,
2577+
sequence: 7,
2578+
});
2579+
expect(pick({ ...settledDone, sequence: 9 }, { ...base, sequence: 1 })).toEqual({
2580+
...settledDone,
2581+
sequence: 9,
2582+
});
26202583
});
26212584
});

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ export function findRecordedWorktreeSetup(
278278
/**
279279
* Which setup snapshot the timeline shows, if any. The live stream wins while
280280
* it has a newer sequence; the recorded activity covers everything else. A
281-
* running setup always shows. Once settled, the card stays only while it
282-
* still says something the turn does not: the turn has not started yet, or a
283-
* stage failed and the turn is still running so the exit code stays reachable.
281+
* running, failed, or cancelled setup always shows. A clean finish leaves no
282+
* trace once the turn is live: the setup is a means to the reply, not part of
283+
* the conversation. Before the turn is live it stays so nothing collapses in
284+
* the handoff gap. A failed script stays while the turn still runs so its
285+
* exit code and terminal remain reachable.
284286
*/
285287
export function resolveVisibleWorktreeSetup(input: {
286288
live: WorktreeSetupSnapshot | null;
@@ -293,7 +295,6 @@ export function resolveVisibleWorktreeSetup(input: {
293295
? input.live
294296
: input.recorded;
295297
if (!snapshot) return null;
296-
if (snapshot.phase === "running") return snapshot;
297298
if (snapshot.phase !== "done") return snapshot;
298299
if (!input.turnStarted) return snapshot;
299300
const stageFailed = snapshot.stages.some((stage) => stage.status === "failed");
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2+
import type { ScopedThreadRef } from "@t3tools/contracts";
3+
import { useNavigate } from "@tanstack/react-router";
4+
import { useEffect } from "react";
5+
6+
import ChatView from "./ChatView";
7+
import { resolveDraftPromotionNavigationTarget, threadHasStarted } from "./ChatView.logic";
8+
import { waitForDraftHeroTransition } from "./chat/draftHeroTransition";
9+
import { SidebarInset } from "./ui/sidebar";
10+
import {
11+
finalizePromotedDraftThreadByRef,
12+
markPromotedDraftThreadByRef,
13+
useBackgroundDraftSubmissionPending,
14+
useComposerDraftStore,
15+
} from "../composerDraftStore";
16+
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
17+
import {
18+
useEnvironmentThreadRefs,
19+
useThread,
20+
useThreadDetail,
21+
useThreadRefs,
22+
useThreadShell,
23+
useThreadStatus,
24+
} from "../state/entities";
25+
import { useEnvironmentQuery } from "../state/query";
26+
import { environmentShell } from "../state/shell";
27+
import {
28+
buildThreadRouteParams,
29+
resolveThreadRouteRenderState,
30+
type ThreadRouteTarget,
31+
} from "../threadRoutes";
32+
import { resolveThreadSyncPhase } from "../threadSync";
33+
34+
/**
35+
* The single chat surface behind both `/draft/$draftId` and
36+
* `/$environmentId/$threadId`. A draft reserves its thread id up front, so
37+
* one ChatView element keyed by that id carries the draft through its
38+
* promotion to a server thread: the route swap only changes props, nothing
39+
* unmounts, and the timeline never paints an empty frame in between.
40+
*
41+
* Rendered by the `_chat` layout rather than by the two leaf routes, since
42+
* a keyed element only survives when the same parent renders it.
43+
*/
44+
export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
45+
const navigate = useNavigate();
46+
const draftId = target.kind === "draft" ? target.draftId : null;
47+
const draftSession = useComposerDraftStore((store) =>
48+
draftId === null ? null : store.getDraftSession(draftId),
49+
);
50+
const threadRefs = useThreadRefs();
51+
// The server thread this view is about: the route's own ref, or the draft's
52+
// reserved ref once the server knows it.
53+
const inferredThreadRef = draftSession
54+
? (threadRefs.find(
55+
(ref) =>
56+
ref.environmentId === draftSession.environmentId &&
57+
ref.threadId === draftSession.threadId,
58+
) ?? null)
59+
: null;
60+
const serverThreadRef: ScopedThreadRef | null =
61+
target.kind === "server" ? target.threadRef : (draftSession?.promotedTo ?? inferredThreadRef);
62+
const serverThread = useThread(serverThreadRef);
63+
const backgroundSubmissionPending = useBackgroundDraftSubmissionPending(
64+
target.kind === "draft" ? serverThreadRef : null,
65+
);
66+
const canonicalThreadRef =
67+
target.kind === "draft"
68+
? resolveDraftPromotionNavigationTarget({
69+
serverThreadRef,
70+
serverThread,
71+
backgroundSubmissionPending,
72+
})
73+
: null;
74+
75+
const shell = useEnvironmentQuery(
76+
serverThreadRef === null ? null : environmentShell.stateAtom(serverThreadRef.environmentId),
77+
);
78+
const serverThreadShell = useThreadShell(serverThreadRef);
79+
const serverThreadDetail = useThreadDetail(serverThreadRef);
80+
const serverThreadStatus = useThreadStatus(serverThreadRef);
81+
const environmentThreadRefs = useEnvironmentThreadRefs(serverThreadRef?.environmentId ?? null);
82+
const bootstrapComplete = shell.data?.snapshot._tag === "Some";
83+
const draftThread = useComposerDraftStore((store) =>
84+
serverThreadRef ? store.getDraftThreadByRef(serverThreadRef) : null,
85+
);
86+
const environmentHasDraftThreads = useComposerDraftStore((store) =>
87+
serverThreadRef ? store.hasDraftThreadsInEnvironment(serverThreadRef.environmentId) : false,
88+
);
89+
const renderState = resolveThreadRouteRenderState({
90+
bootstrapComplete,
91+
serverThreadShellExists: serverThreadShell !== null,
92+
serverThreadDetailExists: serverThreadDetail !== null,
93+
serverThreadDetailDeleted: serverThreadStatus === "deleted",
94+
draftThreadExists: draftThread !== null,
95+
});
96+
const threadSyncPhase = resolveThreadSyncPhase({
97+
detailExists: serverThreadDetail !== null,
98+
shellExists: serverThreadShell !== null,
99+
status: serverThreadStatus,
100+
});
101+
const serverThreadStarted = threadHasStarted(serverThreadDetail);
102+
const environmentHasAnyThreads = environmentThreadRefs.length > 0 || environmentHasDraftThreads;
103+
104+
useEffect(() => {
105+
if (!inferredThreadRef || draftSession?.promotedTo) {
106+
return;
107+
}
108+
markPromotedDraftThreadByRef(inferredThreadRef);
109+
}, [draftSession?.promotedTo, inferredThreadRef]);
110+
111+
useEffect(() => {
112+
if (!canonicalThreadRef) {
113+
return;
114+
}
115+
let cancelled = false;
116+
void waitForDraftHeroTransition().then(() => {
117+
if (cancelled) {
118+
return;
119+
}
120+
void navigate({
121+
to: "/$environmentId/$threadId",
122+
params: buildThreadRouteParams(canonicalThreadRef),
123+
replace: true,
124+
});
125+
});
126+
return () => {
127+
cancelled = true;
128+
};
129+
}, [canonicalThreadRef, navigate]);
130+
131+
useEffect(() => {
132+
if (target.kind !== "draft" || draftSession || canonicalThreadRef) {
133+
return;
134+
}
135+
void navigate({ to: "/", replace: true });
136+
}, [canonicalThreadRef, draftSession, navigate, target.kind]);
137+
138+
useEffect(() => {
139+
if (target.kind !== "server" || !bootstrapComplete) {
140+
return;
141+
}
142+
// Navigation already resolved onto this path, so a drop aimed here
143+
// passed its landing check; once the thread reads as missing it can
144+
// never be attached, release it even when there is nowhere to redirect.
145+
if (renderState === "missing") {
146+
const { clearPendingFileDropsForThread } = useSidebarPendingFileDropStore.getState();
147+
clearPendingFileDropsForThread(target.threadRef);
148+
if (environmentHasAnyThreads) {
149+
void navigate({ to: "/", replace: true });
150+
}
151+
}
152+
}, [bootstrapComplete, environmentHasAnyThreads, navigate, renderState, target]);
153+
154+
useEffect(() => {
155+
if (target.kind !== "server" || !serverThreadStarted || !draftThread) {
156+
return;
157+
}
158+
finalizePromotedDraftThreadByRef(target.threadRef);
159+
}, [draftThread, serverThreadStarted, target]);
160+
161+
let view: React.ReactNode = null;
162+
if (target.kind === "draft") {
163+
if (draftSession) {
164+
view = (
165+
<ChatView
166+
key={scopedThreadKey({
167+
environmentId: draftSession.environmentId,
168+
threadId: draftSession.threadId,
169+
})}
170+
draftId={target.draftId}
171+
environmentId={draftSession.environmentId}
172+
threadId={draftSession.threadId}
173+
routeKind="draft"
174+
forceExpandedMobileComposer
175+
/>
176+
);
177+
}
178+
} else if (renderState === "ready" || (renderState === "loading" && serverThreadShell !== null)) {
179+
view = (
180+
<ChatView
181+
key={scopedThreadKey(target.threadRef)}
182+
environmentId={target.threadRef.environmentId}
183+
threadId={target.threadRef.threadId}
184+
routeKind="server"
185+
threadSyncPhase={threadSyncPhase}
186+
/>
187+
);
188+
}
189+
190+
return (
191+
<SidebarInset className="h-svh min-h-0 overflow-hidden overscroll-y-none bg-background text-foreground md:h-dvh">
192+
{view}
193+
</SidebarInset>
194+
);
195+
}

0 commit comments

Comments
 (0)