fix(ui): stabilize task loading and live feeds (#13095)

Coordinate initial conversation reveal, preserve message identity and reading anchors during live updates, and bound transcript reads with recoverable retries. Cover desktop/mobile navigation and rich task loading with actual-route browser tests.

Verified all Linux CI gates, 5,575 local UI tests, eight layout browser scenarios, and recorded native Codex walkthroughs. Greptile: 5/5; all review findings resolved.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip authored and GitHub committed 2026-09-09 10:00:37 -05:00
1 parent 35fdc0c66b
commit cd4c4ed205
32 files changed
+1286 -179

No files matched your search

+104 -12
View File
@@ -1,4 +1,6 @@
import { requiresExecutionReconciliation } from "@paperclipai/shared";
import { TaskChatExpansionState } from "@/components/task-chat/expansion-state";
import { TaskChatScrollReady } from "@/components/task-chat/scroll-navigation";
import {
useCallback,
useEffect,
@@ -84,10 +86,11 @@ import {
useRunnerGoalControl,
} from "@/components/task-chat/RunnerGoalWidget";
import { TaskChatQueuedMessages } from "@/components/task-chat/TaskChatQueuedMessages";
import { useWindowAutoFollow } from "@/components/task-chat/useWindowAutoFollow";
import { TaskChatWindowScroll } from "@/components/task-chat/useWindowAutoFollow";
import { useSidebar } from "@/context/SidebarContext";
import { useStreamlinedUiEnabled } from "@/hooks/useStreamlinedUiEnabled";
import { cn } from "@/lib/utils";
import { Skeleton } from "@/components/ui/skeleton";
import { Button } from "@/components/ui/button";
import { useIssuePlanDocument } from "@/hooks/useIssuePlanDocument";
import { latestSameRunHandoffTimestamp } from "@/lib/issue-chat-messages";
@@ -389,7 +392,11 @@ function resolvedWithoutUserFacingResponse(value: unknown): boolean {
);
}
export type TaskChatThreadProps = ComponentProps<typeof IssueChatThread>;
export type TaskChatThreadProps = ComponentProps<typeof IssueChatThread> & {
initialHistoryPending?: boolean;
initialHistoryError?: boolean;
onRetryInitialHistory?: () => void;
};
type PendingComposerInput =
| {
@@ -456,6 +463,9 @@ function durableInputLabel(
export function TaskChatThread(props: TaskChatThreadProps) {
const { enabled: streamlinedUiEnabled } = useStreamlinedUiEnabled();
const {
initialHistoryPending = false,
initialHistoryError = false,
onRetryInitialHistory,
comments,
interactions,
documents = [],
@@ -815,6 +825,9 @@ export function TaskChatThread(props: TaskChatThreadProps) {
const {
transcriptByRun: logTranscriptByRun,
isInitialHydrating: logsAreInitiallyHydrating,
hydratedRunIds: hydratedLogRunIds,
errorsByRun: logErrorsByRun,
retry: retryLogs,
} = useLiveRunTranscripts({
// Native events are authoritative, but the persisted/live log remains a
// compatibility source when an upgraded server has no event history or
@@ -825,20 +838,34 @@ export function TaskChatThread(props: TaskChatThreadProps) {
const {
transcriptByRun: nativeTranscriptByRun,
errorsByRun: nativeTranscriptErrorsByRun,
isInitialHydrating: nativeEventsAreInitiallyHydrating,
hydratedRunIds: hydratedNativeRunIds,
retry: retryNativeEvents,
} = useNativeRunTranscripts(nativeRuns);
const fallbackByRunRef = useRef(new Map<string, NonNullable<ReturnType<typeof logTranscriptByRun.get>>>());
const transcriptByRun = useMemo(() => {
const next = new Map(logTranscriptByRun);
for (const run of nativeRuns) {
const logTranscript = logTranscriptByRun.get(run.id) ?? [];
const nativeTranscript = nativeTranscriptByRun.get(run.id) ?? [];
const nativeEventsUnavailable = nativeTranscriptErrorsByRun.has(run.id);
if (
nativeTranscript.length > 0 &&
(!nativeEventsUnavailable || logTranscript.length === 0)
) {
const logTranscript = logTranscriptByRun.get(run.id) ?? [];
if (nativeTranscriptErrorsByRun.has(run.id) && logTranscript.length > 0) {
fallbackByRunRef.current.set(run.id, logTranscript);
}
const fallback = fallbackByRunRef.current.get(run.id);
const lastTimestamp = (entries: typeof nativeTranscript) =>
entries.reduce((latest, entry) => Math.max(latest, toMs(entry.ts)), 0);
// Keep a newer fallback visible through native transport recovery. An
// empty successful poll must not rewind a response the reader just saw.
if (fallback && (nativeTranscriptErrorsByRun.has(run.id) || lastTimestamp(nativeTranscript) < lastTimestamp(fallback))) {
next.set(run.id, fallback);
} else if (nativeTranscript.length > 0) {
fallbackByRunRef.current.delete(run.id);
next.set(run.id, nativeTranscript);
}
}
for (const id of fallbackByRunRef.current.keys()) {
if (!nativeRuns.some((run) => run.id === id)) fallbackByRunRef.current.delete(id);
}
return next;
}, [
logTranscriptByRun,
@@ -958,7 +985,7 @@ export function TaskChatThread(props: TaskChatThreadProps) {
return map;
}, [comments]);
const { data: planDocument } = useIssuePlanDocument(issueId);
const { data: planDocument, isLoading: planLoading, isError: planError, refetch: retryPlan } = useIssuePlanDocument(issueId);
// A native-runner Plan is part of the turn that wrote its current revision.
// Legacy adapters do not expose the semantic write_document boundary needed
@@ -2426,6 +2453,7 @@ export function TaskChatThread(props: TaskChatThreadProps) {
: runtimeRequestReplacesComposerSkip(selectedPendingInput.item),
}
: null;
const expansionState = useRef(new Map<string, boolean>());
const autoFollowContentKey = `${threadContentKey}:${composerTakeover?.id ?? "composer"}`;
// Mobile (PAP-360): the app shell scrolls the DOCUMENT (Layout's main is
@@ -2435,9 +2463,53 @@ export function TaskChatThread(props: TaskChatThreadProps) {
// and track auto-follow against window scroll. Both paths include takeover
// state so opening or closing composer input preserves bottom pinning.
const { isMobile } = useSidebar();
useWindowAutoFollow(isMobile ? autoFollowContentKey : 0, isMobile);
const initialCommentWindow = useRef<{ oldestAt: number; ids: Set<string> } | null>(null);
if (!initialCommentWindow.current && comments.length > 0) {
initialCommentWindow.current = {
oldestAt: Math.min(...comments.map((comment) => toMs(comment.createdAt))),
ids: new Set(comments.map((comment) => comment.id)),
};
}
const initialCommentRunIds = new Set(comments
.filter((comment) => initialCommentWindow.current?.ids.has(comment.id))
.flatMap((comment) => [comment.runId, comment.createdByRunId, comment.derivedCreatedByRunId]));
const initialRuns = runs.filter((run) => {
if (!initialCommentWindow.current || !isTerminalRunStatus(run.status) || initialCommentRunIds.has(run.id)) return true;
const metadata = linkedRunMetaById.get(run.id);
return !metadata || toMs(metadata.finishedAt ?? metadata.startedAt ?? metadata.createdAt) >= initialCommentWindow.current.oldestAt;
});
const historyPending = initialHistoryPending || planLoading ||
initialRuns.some((run) => {
if (run.runtimeMode === "native" && (hydratedNativeRunIds ? !hydratedNativeRunIds.has(run.id) : nativeEventsAreInitiallyHydrating)) return true;
if (run.runtimeMode === "native" && (nativeTranscriptByRun.get(run.id)?.length ?? 0) > 0) return false;
return run.status !== "queued" && hydratedLogRunIds ? !hydratedLogRunIds.has(run.id) : logsAreInitiallyHydrating;
});
const historyError = initialHistoryError || planError || runs.some((run) =>
run.runtimeMode === "native"
? nativeTranscriptErrorsByRun.has(run.id) && (logTranscriptByRun.get(run.id)?.length ?? 0) === 0
: Boolean(logErrorsByRun?.has(run.id)),
);
const [revealedIssue, setRevealedIssue] = useState<string | null | undefined>(() => historyPending ? undefined : issueId);
const historyRevealed = revealedIssue === issueId;
// Mount and measure the real thread while concealed, then reveal in one
// commit. A frame also lets ancestor navigation scroll restoration finish.
// Readiness is latched per issue: refetches never hide existing conversation.
useEffect(() => {
if (historyRevealed || historyPending) return;
const frame = requestAnimationFrame(() => setRevealedIssue(issueId));
return () => cancelAnimationFrame(frame);
}, [historyPending, historyRevealed, issueId]);
const retryHistory = () => {
onRetryInitialHistory?.();
retryLogs?.();
retryNativeEvents?.();
void retryPlan();
};
return (
<TaskChatExpansionState.Provider value={expansionState.current}>
<TaskChatScrollReady.Provider value={!historyPending}>
<TaskChatWindowScroll contentKey={isMobile ? autoFollowContentKey : 0} enabled={isMobile && historyRevealed} />
<TaskChatPresentationProvider
mode={streamlinedUiEnabled ? "streamlined" : "production"}
>
@@ -2448,7 +2520,24 @@ export function TaskChatThread(props: TaskChatThreadProps) {
)}
data-testid="task-chat-thread"
>
<div className={cn("flex flex-col", !isMobile && "min-h-0 flex-1")}>
<div className={cn("relative flex flex-col", !isMobile && "min-h-0 flex-1")} aria-busy={!historyRevealed}>
{historyError ? (
<div role="status" className="absolute inset-x-0 top-0 z-20 mx-auto flex w-full max-w-(--tc-shell-max-w) items-center gap-2 border border-border bg-background px-4 py-2 text-sm text-muted-foreground">
Some task history could not be loaded.
<Button variant="ghost" size="sm" onClick={retryHistory}>Retry</Button>
</div>
) : null}
{!historyRevealed ? (
<div className="absolute inset-0 z-10 overflow-hidden bg-background" data-testid="task-chat-history-loading" role="status" aria-label="Loading conversation">
<div className="mx-auto flex w-full max-w-(--tc-shell-max-w) flex-col gap-4 px-4 py-3">
{threadHeader}
<Skeleton className="h-16 w-3/4 animate-none" />
<Skeleton className="h-24 w-4/5 self-end animate-none" />
<Skeleton className="h-16 w-3/4 animate-none" />
</div>
</div>
) : null}
<div className={cn("flex flex-col", !isMobile && "min-h-0 flex-1", !historyRevealed && "invisible")} inert={!historyRevealed}>
{items.length === 0 && !tailRunId ? (
<div
className={
@@ -2558,11 +2647,12 @@ export function TaskChatThread(props: TaskChatThreadProps) {
</>
) : null
}
contentKey={autoFollowContentKey}
contentKey={`${autoFollowContentKey}:${historyRevealed}`}
className={isMobile ? undefined : "pt-3"}
scroll={!isMobile}
/>
)}
</div>
</div>
{assignedAgentForNotice?.status === "paused" ? (
<div className="mx-auto w-full max-w-(--tc-shell-max-w) px-4 pt-2">
@@ -2686,5 +2776,7 @@ export function TaskChatThread(props: TaskChatThreadProps) {
) : null}
</div>
</TaskChatPresentationProvider>
</TaskChatScrollReady.Provider>
</TaskChatExpansionState.Provider>
);
}