From af880a274015e795bf03c7f95afc8bf79c270b86 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Wed, 15 Jul 2026 21:58:52 -0700 Subject: [PATCH] Address PR #4026 review findings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - settleThread inherits archive's running-turn guard on web and mobile: settling can never interrupt an active turn - unsettleThread no-ops on auto-settled (unarchived) threads instead of sending an unarchive the server would reject - Mobile v2 partition now receives per-row PR state (rows report up via onChangeRequestState, mirroring web) so merged/closed PRs auto-settle - Mobile empty state accounts for archived-only workspaces when v2 is on - Optimistic settled hold on web + mobile: the shell being settled is kept visible (marked archived) between the live stream dropping it and the archived snapshot returning it — no flicker, and holds are created only at settle time so deletes are never resurrected - Hover-only settle buttons in web SidebarV2 get focus-visible:opacity-100 - v2 full swipe commits the advertised primary action (Settle/Un-settle), with the stretch animation following; delete remains a tap target only. v1 keeps full-swipe delete via the new fullSwipeAction prop default - Android long-press on v2 cards works: PressableScaleCard is the menu's direct child and forwards the injected onLongPress to its Pressable Co-Authored-By: Claude Fable 5 --- apps/mobile/src/features/home/HomeScreen.tsx | 100 +++++++++++++++--- .../features/home/thread-swipe-actions.tsx | 20 +++- .../src/features/home/useThreadListActions.ts | 18 ++++ .../features/threads/thread-list-v2-items.tsx | 51 +++++++-- .../src/features/threads/threadListV2.ts | 8 +- apps/web/src/components/SidebarV2.tsx | 83 ++++++++++++--- apps/web/src/hooks/useThreadActions.ts | 32 +++++- 7 files changed, 262 insertions(+), 50 deletions(-) diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx index c06d42f1ce..e0b5699941 100644 --- a/apps/mobile/src/features/home/HomeScreen.tsx +++ b/apps/mobile/src/features/home/HomeScreen.tsx @@ -14,7 +14,7 @@ import type { } from "@t3tools/contracts"; import { useAtomSet, useAtomValue } from "@effect/atom-react"; import { AsyncResult } from "effect/unstable/reactivity"; -import { useCallback, useMemo, useRef, useState } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ActivityIndicator, Platform, View } from "react-native"; import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable"; import { useSafeAreaInsets } from "react-native-safe-area-context"; @@ -294,22 +294,85 @@ export function HomeScreen(props: HomeScreenProps) { [props.environments, threadListV2Enabled], ); const { snapshots: archivedSnapshots } = useArchivedThreadSnapshots(archivedEnvironmentIds); + // PR states stream in per-row (rows own the VCS subscriptions); a merged or + // closed PR auto-settles its thread on the next partition (mirrors web). + const [changeRequestStateByKey, setChangeRequestStateByKey] = useState< + ReadonlyMap + >(() => new Map()); + const handleChangeRequestState = useCallback( + (threadKey: string, state: "open" | "closed" | "merged" | null) => { + setChangeRequestStateByKey((current) => { + if ((current.get(threadKey) ?? null) === state) return current; + const next = new Map(current); + if (state === null) { + next.delete(threadKey); + } else { + next.set(threadKey, state); + } + return next; + }); + }, + [], + ); + // Bridge the gap between the live stream dropping a just-settled thread + // and the archived snapshot returning it: hold the shell we settled, + // marked archived, until the snapshot carries it. Held explicitly at + // settle time so deleted threads are never resurrected. + const [settledHolds, setSettledHolds] = useState>( + () => new Map(), + ); + const handleSettleThread = useCallback( + (thread: EnvironmentThreadShell) => { + setSettledHolds((current) => + new Map(current).set(scopedThreadKey(thread.environmentId, thread.id), { + ...thread, + archivedAt: thread.archivedAt ?? new Date().toISOString(), + }), + ); + props.onSettleThread(thread); + }, + [props.onSettleThread], + ); + useEffect(() => { + if (settledHolds.size === 0) return; + const covered = new Set(); + for (const { environmentId, snapshot } of archivedSnapshots) { + for (const thread of snapshot.threads) { + covered.add(scopedThreadKey(environmentId, thread.id)); + } + } + if ([...settledHolds.keys()].some((threadKey) => covered.has(threadKey))) { + setSettledHolds((current) => { + const next = new Map(current); + for (const threadKey of covered) next.delete(threadKey); + return next; + }); + } + }, [archivedSnapshots, settledHolds]); const threadListV2Items = useMemo(() => { if (!threadListV2Enabled) return []; - const liveKeys = new Set( - props.threads.map((thread) => scopedThreadKey(thread.environmentId, thread.id)), - ); - const archived = archivedSnapshots.flatMap(({ environmentId, snapshot }) => - snapshot.threads - .map((thread) => ({ ...thread, environmentId })) - .filter((thread) => !liveKeys.has(scopedThreadKey(thread.environmentId, thread.id))), - ); + const merged = new Map(); + for (const { environmentId, snapshot } of archivedSnapshots) { + for (const thread of snapshot.threads) { + merged.set(scopedThreadKey(environmentId, thread.id), { ...thread, environmentId }); + } + } + for (const thread of props.threads) { + merged.set(scopedThreadKey(thread.environmentId, thread.id), thread); + } + for (const [threadKey, shell] of settledHolds) { + if (merged.has(threadKey)) continue; + merged.set(threadKey, shell); + } return buildThreadListV2Items({ - threads: [...props.threads, ...archived], + threads: [...merged.values()], environmentId: props.selectedEnvironmentId, searchQuery: props.searchQuery, + changeRequestStateByKey, }); }, [ + changeRequestStateByKey, + settledHolds, archivedSnapshots, props.searchQuery, props.selectedEnvironmentId, @@ -330,20 +393,27 @@ export function HomeScreen(props: HomeScreenProps) { onSelectThread={props.onSelectThread} onArchiveThread={props.onArchiveThread} onDeleteThread={props.onDeleteThread} - onSettleThread={props.onSettleThread} + onSettleThread={handleSettleThread} onUnsettleThread={props.onUnsettleThread} + onChangeRequestState={handleChangeRequestState} + projectCwd={ + projectCwdByKey.get(scopedProjectKey(item.thread.environmentId, item.thread.projectId)) ?? + null + } onSwipeableClose={handleSwipeableClose} onSwipeableWillOpen={handleSwipeableWillOpen} /> ), [ + handleChangeRequestState, + handleSettleThread, handleSwipeableClose, handleSwipeableWillOpen, projectByKey, + projectCwdByKey, props.onArchiveThread, props.onDeleteThread, props.onSelectThread, - props.onSettleThread, props.onUnsettleThread, ], ); @@ -445,8 +515,12 @@ export function HomeScreen(props: HomeScreenProps) { const keyExtractor = useCallback((item: HomeListItem) => item.key, []); /* Empty states */ + // v2 shows archived threads as its settled tail, so an archived-only + // workspace still has a list to render there. const hasAnyThreads = - props.threads.some((thread) => thread.archivedAt === null) || props.pendingTasks.length > 0; + props.threads.some((thread) => thread.archivedAt === null) || + props.pendingTasks.length > 0 || + (threadListV2Enabled && threadListV2Items.length > 0); const hasResults = projectGroups.length > 0; const selectedEnvironmentLabel = props.selectedEnvironmentId === null diff --git a/apps/mobile/src/features/home/thread-swipe-actions.tsx b/apps/mobile/src/features/home/thread-swipe-actions.tsx index 84a9f32ee5..666169f303 100644 --- a/apps/mobile/src/features/home/thread-swipe-actions.tsx +++ b/apps/mobile/src/features/home/thread-swipe-actions.tsx @@ -167,6 +167,13 @@ export function ThreadSwipeable(props: { /** Disables NEW swipe activations (e.g. while the list scrolls). */ readonly enabled?: boolean; readonly enableTrackpadSwipe?: boolean; + /** + * What a full swipe commits: "delete" (default, v1 behavior — the Delete + * button stretches) or "primary" — the advertised primary action fires and + * its button stretches instead. A full swipe must always match the action + * the stretching button advertises. + */ + readonly fullSwipeAction?: "delete" | "primary"; readonly fullSwipeWidth: number; readonly onDelete: () => void; readonly onSwipeableClose?: (methods: SwipeableMethods) => void; @@ -239,7 +246,11 @@ export function ThreadSwipeable(props: { if (fullSwipeArmedRef.current) { fullSwipeArmedRef.current = false; methods.close(); - props.onDelete(); + if (props.fullSwipeAction === "primary") { + props.primaryAction.onPress(); + } else { + props.onDelete(); + } } }} overshootFriction={1} @@ -247,6 +258,7 @@ export function ThreadSwipeable(props: { renderRightActions={(_progress, translation, methods) => ( void; readonly onFullSwipeArmedChange: (armed: boolean) => void; @@ -430,6 +443,7 @@ export function ThreadSwipeActions(props: { readonly threadTitle: string; readonly translation: SharedValue; }) { + const fullSwipeIsPrimary = props.fullSwipeAction === "primary"; useAnimatedReaction( () => -props.translation.value >= props.fullSwipeThreshold, (armed, previous) => { @@ -457,7 +471,7 @@ export function ThreadSwipeActions(props: { icon={props.primaryAction.icon} label={props.primaryAction.label} onPress={props.primaryAction.onPress} - stretchesOnFullSwipe={false} + stretchesOnFullSwipe={fullSwipeIsPrimary} translation={props.translation} /> diff --git a/apps/mobile/src/features/home/useThreadListActions.ts b/apps/mobile/src/features/home/useThreadListActions.ts index 29f54d4f4a..2034144154 100644 --- a/apps/mobile/src/features/home/useThreadListActions.ts +++ b/apps/mobile/src/features/home/useThreadListActions.ts @@ -62,6 +62,24 @@ function useThreadActionExecutor( inFlightThreadKeys.current.add(key); selectionHaptic(); try { + // Settle rides archive, so it inherits archive's guard: never + // interrupt a thread mid-turn. + if ( + (action === "settle" || action === "archive") && + thread.session?.status === "running" && + thread.session.activeTurnId != null + ) { + Alert.alert( + actionFailureTitle(action), + "This thread is working. Interrupt it first, then try again.", + ); + return; + } + // Auto-settled rows (inactivity / merged PR) are not archived; + // unarchiving them would be rejected. Nothing to undo — no-op. + if (action === "unsettle" && thread.archivedAt === null) { + return; + } const mutation = action === "settle" ? settleMutation diff --git a/apps/mobile/src/features/threads/thread-list-v2-items.tsx b/apps/mobile/src/features/threads/thread-list-v2-items.tsx index a7496630d2..d7dc900c4e 100644 --- a/apps/mobile/src/features/threads/thread-list-v2-items.tsx +++ b/apps/mobile/src/features/threads/thread-list-v2-items.tsx @@ -15,8 +15,10 @@ import { ProjectFavicon } from "../../components/ProjectFavicon"; import { cn } from "../../lib/cn"; import { relativeTime } from "../../lib/time"; import { useThemeColor } from "../../lib/useThemeColor"; +import { useThreadPr } from "../../state/use-thread-pr"; import { ThreadSwipeable } from "../home/thread-swipe-actions"; import { resolveThreadListV2Status, type ThreadListV2Status } from "./threadListV2"; +import { useEffect } from "react"; /** * Thread List v2 rows. The design language is the web sidebar v2 (status @@ -71,11 +73,17 @@ const PRESS_SPRING = { damping: 30, stiffness: 400 } as const; /** * Pressable that springs down to 97% while touched — the "this is a real * object under your finger" signal every native list app ships. + * + * Accepts an injected `onLongPress` and forwards it to the inner Pressable: + * on Android, ControlPillMenu opens its menu by cloning its immediate child + * with an onLongPress prop, so this component must be that child and must + * route the handler to something that actually handles presses. */ function PressableScaleCard(props: { readonly accessibilityHint: string; readonly accessibilityLabel: string; readonly onPress: () => void; + readonly onLongPress?: () => void; readonly children: ReactNode; }) { const scale = useSharedValue(1); @@ -88,6 +96,7 @@ function PressableScaleCard(props: { accessibilityLabel={props.accessibilityLabel} accessibilityRole="button" onPress={props.onPress} + {...(props.onLongPress ? { onLongPress: props.onLongPress } : {})} onPressIn={() => { scale.value = withSpring(0.97, PRESS_SPRING); }} @@ -128,6 +137,13 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { readonly onUnsettleThread: (thread: EnvironmentThreadShell) => void; readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void; readonly onSwipeableClose: (methods: SwipeableMethods) => void; + /** Reports this row's live PR state up so the partition can auto-settle + merged/closed work (mirrors web's onChangeRequestState). */ + readonly onChangeRequestState?: ( + threadKey: string, + state: "open" | "closed" | "merged" | null, + ) => void; + readonly projectCwd?: string | null; readonly simultaneousSwipeGesture?: ComponentProps< typeof ThreadSwipeable >["simultaneousWithExternalGesture"]; @@ -141,8 +157,16 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { onDeleteThread, onSettleThread, onUnsettleThread, + onChangeRequestState, } = props; + const pr = useThreadPr(thread, props.projectCwd ?? props.project?.workspaceRoot ?? null); + const prState = pr?.state ?? null; + const threadKey = `${thread.environmentId}:${thread.id}`; + useEffect(() => { + onChangeRequestState?.(threadKey, prState); + }, [onChangeRequestState, prState, threadKey]); + const iconSubtleColor = useThemeColor("--color-icon-subtle"); const screenColor = useThemeColor("--color-screen"); @@ -187,15 +211,17 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: { const rowContent = (close: () => void) => variant === "card" ? ( - - { - close(); - onSelectThread(thread); - }} - > + // PressableScaleCard must be the ROOT here: ControlPillMenu injects + // its Android long-press by cloning this element. + { + close(); + onSelectThread(thread); + }} + > + {/* Solid raised card: bg-card with no border reads as an object, not a notification banner. */} - - + + ) : ( ; readonly environmentId: EnvironmentId | null; readonly searchQuery: string; + /** Per-row PR state reported up by visible rows ("env:threadId" keys). */ + readonly changeRequestStateByKey?: ReadonlyMap; readonly autoSettleAfterDays?: number; /** Injectable for tests; defaults to now. */ readonly now?: string; @@ -77,9 +79,9 @@ export function buildThreadListV2Items(input: { // archive IS settle, so they render as the settled tail. if (input.environmentId !== null && thread.environmentId !== input.environmentId) continue; if (query.length > 0 && !thread.title.toLocaleLowerCase().includes(query)) continue; - // PR state feeds the web partition per-row; mobile shells don't watch PRs - // from the list, so the partition here is archive/session/inactivity. - if (effectiveSettled(thread, { now, autoSettleAfterDays, changeRequestState: null })) { + const changeRequestState = + input.changeRequestStateByKey?.get(`${thread.environmentId}:${thread.id}`) ?? null; + if (effectiveSettled(thread, { now, autoSettleAfterDays, changeRequestState })) { settled.push(thread); } else { active.push(thread); diff --git a/apps/web/src/components/SidebarV2.tsx b/apps/web/src/components/SidebarV2.tsx index a97912ebf0..e5be113e31 100644 --- a/apps/web/src/components/SidebarV2.tsx +++ b/apps/web/src/components/SidebarV2.tsx @@ -415,7 +415,7 @@ const SidebarV2Row = memo(function SidebarV2Row(props: { type="button" aria-label="Un-settle thread" onClick={handleUnsettleClick} - className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[11px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground group-hover/v2-row:opacity-100" + className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[11px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground focus-visible:opacity-100 group-hover/v2-row:opacity-100" > @@ -424,7 +424,7 @@ const SidebarV2Row = memo(function SidebarV2Row(props: { type="button" aria-label="Settle thread" onClick={handleSettleClick} - className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[11px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground group-hover/v2-row:opacity-100" + className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[11px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground focus-visible:opacity-100 group-hover/v2-row:opacity-100" > @@ -504,7 +504,7 @@ const SidebarV2Row = memo(function SidebarV2Row(props: { type="button" aria-label="Settle thread" onClick={handleSettleClick} - className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[10px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground group-hover/v2-row:opacity-100" + className="absolute inset-y-0 right-0 inline-flex items-center gap-1 border border-border bg-background px-2 text-[10px] text-muted-foreground opacity-0 transition-opacity hover:text-foreground focus-visible:opacity-100 group-hover/v2-row:opacity-100" > Settle @@ -715,20 +715,53 @@ export default function SidebarV2() { [environments], ); const { snapshots: archivedSnapshots } = useArchivedThreadSnapshots(archivedEnvironmentIds); + // Bridge the gap between the live stream dropping a just-settled thread + // (thread.archived → thread-removed) and the archived snapshot returning + // it: hold the shell we settled, marked archived, until either source + // carries the thread again. Held explicitly at settle time — not inferred + // from disappearance — so deleted threads are never resurrected. + const [settledHolds, setSettledHolds] = useState>( + () => new Map(), + ); const allThreads = useMemo(() => { - const liveKeys = new Set( - threads.map((thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id))), - ); - const archived = archivedSnapshots.flatMap(({ environmentId, snapshot }) => - snapshot.threads - .map((thread) => ({ ...thread, environmentId })) - .filter( - (thread) => - !liveKeys.has(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id))), - ), - ); - return [...threads, ...archived]; - }, [archivedSnapshots, threads]); + const merged = new Map(); + for (const { environmentId, snapshot } of archivedSnapshots) { + for (const thread of snapshot.threads) { + merged.set(scopedThreadKey(scopeThreadRef(environmentId, thread.id)), { + ...thread, + environmentId, + }); + } + } + // Live shells win over snapshot copies. + for (const thread of threads) { + merged.set(scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)), thread); + } + for (const [threadKey, shell] of settledHolds) { + if (merged.has(threadKey)) continue; + merged.set(threadKey, shell); + } + return [...merged.values()]; + }, [archivedSnapshots, settledHolds, threads]); + // Drop a hold once the archived snapshot carries the thread — the steady + // state. (Not the live stream: the thread is still live when the hold is + // created, and holds are inert while a real source has the thread anyway.) + useEffect(() => { + if (settledHolds.size === 0) return; + const covered = new Set(); + for (const { environmentId, snapshot } of archivedSnapshots) { + for (const thread of snapshot.threads) { + covered.add(scopedThreadKey(scopeThreadRef(environmentId, thread.id))); + } + } + if ([...settledHolds.keys()].some((threadKey) => covered.has(threadKey))) { + setSettledHolds((current) => { + const next = new Map(current); + for (const threadKey of covered) next.delete(threadKey); + return next; + }); + } + }, [archivedSnapshots, settledHolds]); const { activeThreads, settledThreads } = useMemo(() => { const now = `${nowMinute}:00.000Z`; @@ -894,8 +927,26 @@ export default function SidebarV2() { const attemptSettle = useCallback( (threadRef: ScopedThreadRef) => { void (async () => { + // Hold the shell before dispatching: the live stream drops the + // thread on archive, and the settled tail must not flicker while + // the archived snapshot refresh is in flight. + const threadKey = scopedThreadKey(threadRef); + const shell = threadByKeyRef.current.get(threadKey); + if (shell) { + setSettledHolds((current) => + new Map(current).set(threadKey, { + ...shell, + archivedAt: shell.archivedAt ?? new Date().toISOString(), + }), + ); + } const result = await settleThread(threadRef); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + setSettledHolds((current) => { + const next = new Map(current); + next.delete(threadKey); + return next; + }); const error = squashAtomCommandFailure(result); toastManager.add( stackedThreadToast({ diff --git a/apps/web/src/hooks/useThreadActions.ts b/apps/web/src/hooks/useThreadActions.ts index 47c7767bf6..1934cf44e1 100644 --- a/apps/web/src/hooks/useThreadActions.ts +++ b/apps/web/src/hooks/useThreadActions.ts @@ -360,6 +360,22 @@ export function useThreadActions() { const settleThread = useCallback( async (target: ScopedThreadRef) => { const resolved = resolveThreadTarget(target); + // Settle rides archive, so it inherits archive's guard: never + // interrupt a thread mid-turn. + if ( + resolved && + resolved.thread.session?.status === "running" && + resolved.thread.session.activeTurnId != null + ) { + return AsyncResult.failure( + Cause.fail( + new ThreadArchiveBlockedError({ + environmentId: resolved.threadRef.environmentId, + threadId: resolved.threadRef.threadId, + }), + ), + ); + } const settleResult = await settleThreadMutation({ environmentId: target.environmentId, input: { threadId: target.threadId }, @@ -484,12 +500,20 @@ export function useThreadActions() { ); const unsettleThread = useCallback( - (target: ScopedThreadRef) => - unsettleThreadMutation({ + async (target: ScopedThreadRef) => { + // Auto-settled rows (inactivity / merged PR) are not archived; sending + // unarchive for them would be rejected by the server. There is nothing + // to undo client-side, so succeed as a no-op. + const resolved = resolveThreadTarget(target); + if (resolved && resolved.thread.archivedAt === null) { + return AsyncResult.success(undefined); + } + return unsettleThreadMutation({ environmentId: target.environmentId, input: { threadId: target.threadId }, - }), - [unsettleThreadMutation], + }); + }, + [resolveThreadTarget, unsettleThreadMutation], ); const confirmAndDeleteThread = useCallback(