|
5 | 5 | resolveBackgroundDraftWorkspaceOptions, |
6 | 6 | resolveDraftHeroState, |
7 | 7 | shouldDockDraftHeroForSubmission, |
8 | | - resolveVisibleWorktreeSetup, |
9 | | - resolveWorktreeSetupProgress, |
10 | 8 | } from "./ChatView.logic"; |
11 | 9 | import * as DateTime from "effect/DateTime"; |
12 | 10 | import { restorePlanFollowUpComposer } from "./ChatView.logic"; |
@@ -37,6 +35,7 @@ import type { UsageLimitSourceSnapshots } from "@t3tools/contracts"; |
37 | 35 | import { isUsageLimitsCommand } from "@t3tools/shared/usageLimits"; |
38 | 36 | import { feedbackBannerItem } from "./chat/ComposerFeedback"; |
39 | 37 | import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel"; |
| 38 | +import { useWorktreeSetup } from "./chat/useWorktreeSetup"; |
40 | 39 | import { getTerminalLabel } from "@t3tools/shared/terminalLabels"; |
41 | 40 | import * as Schema from "effect/Schema"; |
42 | 41 | import { |
@@ -71,7 +70,6 @@ import { |
71 | 70 | resolveEnvironmentMachineKind, |
72 | 71 | resolveProjectProviderInstanceEnabled, |
73 | 72 | RuntimeMode, |
74 | | - type WorktreeSetupSnapshot, |
75 | 73 | } from "@t3tools/contracts"; |
76 | 74 | import { type EnvironmentConnectionPresentation } from "@t3tools/client-runtime/connection"; |
77 | 75 | import { deriveThreadTitleSeed } from "@t3tools/client-runtime/operations"; |
@@ -389,7 +387,6 @@ import { |
389 | 387 | serverEnvironment, |
390 | 388 | } from "../state/server"; |
391 | 389 | import { threadEnvironment } from "../state/threads"; |
392 | | -import { workspacePreparationRetryRunIds } from "@t3tools/client-runtime/state/turn-item-presentation"; |
393 | 390 | import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills"; |
394 | 391 | import { vcsEnvironment } from "../state/vcs"; |
395 | 392 | import { sourceControlEnvironment } from "../state/sourceControl"; |
@@ -421,7 +418,7 @@ import type { AssistantCitationRequest } from "./chat/AssistantCitationSource"; |
421 | 418 | import { MessagesTimeline, type MessagesTimelineHistoryControls } from "./chat/MessagesTimeline"; |
422 | 419 | import { ProviderSubagentBar } from "./chat/ProviderSubagentBar"; |
423 | 420 | import { getTriggerDisplayModelName } from "./chat/providerIconUtils"; |
424 | | -import { resolveTimelineIsAtEnd, worktreeSetupAgentStarted } from "./chat/MessagesTimeline.logic"; |
| 421 | +import { resolveTimelineIsAtEnd } from "./chat/MessagesTimeline.logic"; |
425 | 422 | import { |
426 | 423 | overlayComposerIsResting, |
427 | 424 | resolveComposerTimelineInset, |
@@ -901,8 +898,12 @@ function releaseChatTimelineAnchor<T extends { readonly messageId: MessageId | n |
901 | 898 | return current.messageId === null ? current : { ...current, messageId: null }; |
902 | 899 | } |
903 | 900 |
|
904 | | -const retryingWorkspacePreparationRunIds = new Set<RunId>(); |
| 901 | +// The factories below build functions ChatView calls during render, or hands |
| 902 | +// to ChatComposer, which can call them after an await. They live outside the |
| 903 | +// component so each closure keeps only its inputs, not a whole render scope |
| 904 | +// (see useStableCallback), while still acting on the render that made them. |
905 | 905 |
|
| 906 | +/** Records a thread error in local state, keyed by the route the making render saw. */ |
906 | 907 | function makeThreadErrorSetter(input: { |
907 | 908 | readonly routeThreadRef: ScopedThreadRef; |
908 | 909 | readonly routeThreadKey: string; |
@@ -1235,15 +1236,10 @@ export default function ChatView(props: ChatViewProps) { |
1235 | 1236 | return () => revokeBlobPreviewUrl(src); |
1236 | 1237 | }, [expandedImage]); |
1237 | 1238 | const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]); |
1238 | | - // Last live snapshot from the setup stream. The server drops a finished |
1239 | | - // snapshot after a grace period and emits null; holding it here bridges the |
1240 | | - // gap until the settled activity arrives on the thread projection. |
1241 | | - const [worktreeSetupRef, setWorktreeSetupRef] = useState<{ |
1242 | | - environmentId: EnvironmentId; |
1243 | | - threadId: ThreadId; |
1244 | | - ownerKey: string; |
1245 | | - } | null>(null); |
1246 | | - const [heldWorktreeSetup, setHeldWorktreeSetup] = useState<WorktreeSetupSnapshot | null>(null); |
| 1239 | + // Set by "Work locally": the draft whose restored message should be resent |
| 1240 | + // once the cancelled dispatch has settled and the draft is in local mode. |
| 1241 | + // Keyed by draft id so a bootstrap rotating the thread id keeps it, while |
| 1242 | + // moving to another draft drops it without an effect. |
1247 | 1243 | const [workLocallyResendDraftId, setWorkLocallyResendDraftId] = useState<DraftId | null>(null); |
1248 | 1244 | useEffect(() => { |
1249 | 1245 | if (workLocallyResendDraftId !== null && workLocallyResendDraftId !== draftId) { |
@@ -3028,98 +3024,29 @@ export default function ChatView(props: ChatViewProps) { |
3028 | 3024 | activeThreadKey, |
3029 | 3025 | ); |
3030 | 3026 | const displayedThreadRef = parseScopedThreadKey(displayedTimelineKey); |
3031 | | - const worktreeSetupOwnerKey = draftId ?? routeThreadKey; |
3032 | | - const worktreeSetupActive = |
3033 | | - worktreeSetupRef !== null && worktreeSetupRef.ownerKey === worktreeSetupOwnerKey; |
3034 | | - const setupTarget = worktreeSetupActive ? worktreeSetupRef : routeThreadRef; |
3035 | | - const worktreeSetupQuery = useEnvironmentQuery( |
3036 | | - worktreeSetupActive || |
3037 | | - (activeThread?.id === routeThreadRef.threadId && |
3038 | | - (isLocallyPreparingWorktree || activeRunPreparing || activeThread.worktreePath !== null)) |
3039 | | - ? vcsEnvironment.worktreeSetup({ |
3040 | | - environmentId: setupTarget.environmentId, |
3041 | | - input: { threadId: setupTarget.threadId }, |
3042 | | - }) |
3043 | | - : null, |
3044 | | - ); |
3045 | | - const latestWorktreeSetup = worktreeSetupQuery.data; |
3046 | | - useEffect(() => { |
3047 | | - if (!latestWorktreeSetup) return; |
3048 | | - setHeldWorktreeSetup((current) => |
3049 | | - current?.threadId === latestWorktreeSetup.threadId && |
3050 | | - current.sequence > latestWorktreeSetup.sequence |
3051 | | - ? current |
3052 | | - : latestWorktreeSetup, |
3053 | | - ); |
3054 | | - }, [latestWorktreeSetup]); |
3055 | | - useEffect(() => { |
3056 | | - setHeldWorktreeSetup(null); |
3057 | | - }, [routeThreadKey]); |
3058 | | - const { snapshot: liveWorktreeSetup, isPreparingWorktree } = resolveWorktreeSetupProgress({ |
3059 | | - threadId: setupTarget.threadId, |
| 3027 | + const { |
| 3028 | + worktreeSetup, |
| 3029 | + isPreparingWorktree, |
| 3030 | + blocksSend: worktreeSetupBlocksSend, |
| 3031 | + retryableRunIds: retryableWorkspacePreparationRunIds, |
| 3032 | + begin: beginWorktreeSetup, |
| 3033 | + cancel: cancelWorktreeSetup, |
| 3034 | + retryPreparation: onRetryWorkspacePreparationStable, |
| 3035 | + openSetupTerminal: onOpenWorktreeSetupTerminal, |
| 3036 | + } = useWorktreeSetup({ |
| 3037 | + routeThreadRef, |
| 3038 | + ownerKey: draftId ?? routeThreadKey, |
| 3039 | + activeThread, |
| 3040 | + threadRef: activeThreadRef, |
| 3041 | + serverProjection, |
| 3042 | + activityRun: activeActivityRun, |
3060 | 3043 | localPreparing: isLocallyPreparingWorktree, |
3061 | | - runStatus: activeActivityRun?.status, |
3062 | | - latest: latestWorktreeSetup, |
3063 | | - held: heldWorktreeSetup, |
3064 | | - }); |
3065 | | - const worktreeSetup = resolveVisibleWorktreeSetup({ |
3066 | | - live: liveWorktreeSetup, |
3067 | | - recorded: null, |
3068 | | - turnStarted: activeActivityRun?.startedAt != null, |
| 3044 | + sendQueuesBehindSetup, |
| 3045 | + // Counts the optimistic send too, so the row retires the moment the |
| 3046 | + // follow-up is on screen rather than when the server echoes it back. |
3069 | 3047 | followUpSent: timelineMessages.filter((message) => message.role === "user").length > 1, |
3070 | 3048 | }); |
3071 | | - // Sends wait for the agent handoff, not for the setup script: an async script |
3072 | | - // keeps the snapshot running while the agent already works, and a follow-up |
3073 | | - // must not be held behind a slow install. |
3074 | | - const worktreeSetupBlocksSend = |
3075 | | - !sendQueuesBehindSetup && |
3076 | | - (worktreeSetup !== null |
3077 | | - ? worktreeSetup.phase === "running" && !worktreeSetupAgentStarted(worktreeSetup) |
3078 | | - : isPreparingWorktree); |
3079 | | - const cancelWorktreeSetup = useAtomCommand(vcsEnvironment.cancelWorktreeSetup, { |
3080 | | - reportFailure: false, |
3081 | | - }); |
3082 | | - const retryWorkspacePreparation = useAtomCommand(threadEnvironment.retryWorkspacePreparation); |
3083 | | - const retryableRunIdsKey = useMemo( |
3084 | | - () => |
3085 | | - [ |
3086 | | - ...workspacePreparationRetryRunIds( |
3087 | | - serverProjection?.runs ?? [], |
3088 | | - serverProjection?.turnItems ?? [], |
3089 | | - ), |
3090 | | - ].join("\n"), |
3091 | | - [serverProjection?.runs, serverProjection?.turnItems], |
3092 | | - ); |
3093 | | - const retryableWorkspacePreparationRunIds = useMemo( |
3094 | | - () => new Set(retryableRunIdsKey === "" ? [] : (retryableRunIdsKey.split("\n") as RunId[])), |
3095 | | - [retryableRunIdsKey], |
3096 | | - ); |
3097 | | - const onRetryWorkspacePreparation = (runId: RunId) => { |
3098 | | - if (!activeThreadRef || retryingWorkspacePreparationRunIds.has(runId)) return; |
3099 | | - retryingWorkspacePreparationRunIds.add(runId); |
3100 | | - void retryWorkspacePreparation({ |
3101 | | - environmentId: activeThreadRef.environmentId, |
3102 | | - input: { threadId: activeThreadRef.threadId, runId }, |
3103 | | - }).finally(() => retryingWorkspacePreparationRunIds.delete(runId)); |
3104 | | - }; |
3105 | | - const onRetryWorkspacePreparationStable = useStableCallback(onRetryWorkspacePreparation); |
3106 | | - const onCancelWorktreeSetup = () => { |
3107 | | - if (!worktreeSetup || worktreeSetup.phase !== "running") return; |
3108 | | - void cancelWorktreeSetup({ |
3109 | | - environmentId: setupTarget.environmentId, |
3110 | | - input: { threadId: worktreeSetup.threadId }, |
3111 | | - }); |
3112 | | - }; |
3113 | | - const onCancelWorktreeSetupStable = useStableCallback(onCancelWorktreeSetup); |
3114 | | - const onOpenWorktreeSetupTerminal = useMemo(() => { |
3115 | | - if (!worktreeSetup || !activeThreadRef || worktreeSetup.threadId !== activeThreadRef.threadId) { |
3116 | | - return null; |
3117 | | - } |
3118 | | - const setupThreadRef = activeThreadRef; |
3119 | | - return (terminalId: string) => { |
3120 | | - storeEnsureTerminal(setupThreadRef, terminalId, { open: true, active: true }); |
3121 | | - }; |
3122 | | - }, [activeThreadRef, storeEnsureTerminal, worktreeSetup]); |
| 3049 | + const onCancelWorktreeSetupStable = useStableCallback(() => void cancelWorktreeSetup()); |
3123 | 3050 | const [dockedDraftHeroThreadKey, setDockedDraftHeroThreadKey] = useState<string | null>(null); |
3124 | 3051 | const draftHeroDockRequested = |
3125 | 3052 | activeThreadKey !== null && dockedDraftHeroThreadKey === activeThreadKey; |
@@ -7353,13 +7280,9 @@ export default function ChatView(props: ChatViewProps) { |
7353 | 7280 | ? "foreground" |
7354 | 7281 | : submissionIntent, |
7355 | 7282 | }); |
7356 | | - setWorktreeSetupRef( |
| 7283 | + beginWorktreeSetup( |
7357 | 7284 | multipleModelSelections === null && baseBranchForWorktree |
7358 | | - ? { |
7359 | | - environmentId: activeThread.environmentId, |
7360 | | - threadId: threadIdForSend, |
7361 | | - ownerKey: worktreeSetupOwnerKey, |
7362 | | - } |
| 7285 | + ? { environmentId: activeThread.environmentId, threadId: threadIdForSend } |
7363 | 7286 | : null, |
7364 | 7287 | ); |
7365 | 7288 | } |
@@ -8432,15 +8355,9 @@ export default function ChatView(props: ChatViewProps) { |
8432 | 8355 | if (!worktreeSetup || worktreeSetup.phase !== "running" || !draftId) { |
8433 | 8356 | return; |
8434 | 8357 | } |
8435 | | - const target = { |
8436 | | - environmentId: setupTarget.environmentId, |
8437 | | - input: { threadId: worktreeSetup.threadId }, |
8438 | | - }; |
8439 | | - void (async () => { |
8440 | | - const result = await cancelWorktreeSetup(target); |
8441 | | - if (result._tag !== "Success" || !result.value.cancelled) return; |
8442 | | - setWorkLocallyResendDraftId(draftId); |
8443 | | - })(); |
| 8358 | + void cancelWorktreeSetup().then((cancelled) => { |
| 8359 | + if (cancelled) setWorkLocallyResendDraftId(draftId); |
| 8360 | + }); |
8444 | 8361 | }; |
8445 | 8362 | const onWorktreeSetupWorkLocallyStable = useStableCallback(onWorktreeSetupWorkLocally); |
8446 | 8363 | const onSendRef = useRef(onSend); |
|