diff --git a/DESIGN.md b/DESIGN.md index 2c58ebd44f8..66afaae09a0 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -161,8 +161,13 @@ current-activity slot names the run's startup stage. The claim continues `Preparing workspace`, `Restoring session`, `Connecting tools` or `Starting agent` (`Resuming session` when the provider resumed a saved session). The header keeps `Working`. A stage shimmers like `Thinking`, because -work is in progress. Show each stage at once, and keep a shown stage for at -least 500 ms; a change during that time shows the latest stage when it ends. +work is in progress. Show a new stage only after it stays current for 300 ms, so +a step too short to name never shows. Keep a shown stage for its reading time: +50 ms per character, and at least 833 ms. When both times pass, show the +latest stage; skip stages that came and went. When a task opens during startup, +show its current stage at once: the stage can already be old, and `Thinking` +would be wrong. The end of startup has no delay, but the last stage still stays +for its reading time. `Thinking` begins when the agent's turn starts, and agent activity replaces it. A run that waits for one of its agent's concurrency slots is a hold too: show `Waiting for · N slots max · P of Q in line · Configure` (the shared diff --git a/ui/src/components/TaskChatThread.test.tsx b/ui/src/components/TaskChatThread.test.tsx index 3b39dc54399..9a1dad60d4b 100644 --- a/ui/src/components/TaskChatThread.test.tsx +++ b/ui/src/components/TaskChatThread.test.tsx @@ -14,7 +14,7 @@ import { TooltipProvider } from "@/components/ui/tooltip"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { ConnectivityProvider, createConnectivityStore } from "@/lib/connectivity"; import { INITIAL_TRANSCRIPT_REVEAL_BUDGET_MS, TaskChatThread } from "./TaskChatThread"; -import { STARTUP_LABEL_MIN_DISPLAY_MS } from "./task-chat/run-startup-stage"; +import { STARTUP_LABEL_SHOW_DELAY_MS, startupLabelHoldMs } from "./task-chat/run-startup-stage"; import { IssueChatThread } from "./IssueChatThread"; import { TaskChatAgentIdentity, TaskTranscriptAgents } from "./task-chat/TaskChatBubble"; import type { @@ -1834,8 +1834,11 @@ describe("TaskChatThread runtime transcript selection", () => { // The stage is the activity line; the header keeps its usual Working status. expect(container.querySelector('[data-testid="task-chat-turn-status-header"]')?.textContent).toContain("Working"); - await act(async () => { await new Promise(resolve => setTimeout(resolve, STARTUP_LABEL_MIN_DISPLAY_MS + 50)); }); + // A shown stage stays up long enough to read; the next one shows once it has stayed current for the show delay. + await act(async () => { await new Promise(resolve => setTimeout(resolve, startupLabelHoldMs("Connecting tools"))); }); renderThread({ ...props, activeRun: { ...run, startupStage: "starting_agent", startupSessionResumed: true } }); + expect(activity()?.textContent).toBe("Connecting tools"); + await act(async () => { await new Promise(resolve => setTimeout(resolve, STARTUP_LABEL_SHOW_DELAY_MS + 50)); }); expect(activity()?.textContent).toBe("Resuming session"); nativeTranscriptState.transcriptByRun.set(run.id, [ diff --git a/ui/src/components/task-chat/run-startup-stage.test.ts b/ui/src/components/task-chat/run-startup-stage.test.ts index 340364e5ed6..8d29f877943 100644 --- a/ui/src/components/task-chat/run-startup-stage.test.ts +++ b/ui/src/components/task-chat/run-startup-stage.test.ts @@ -6,7 +6,9 @@ import { RUN_STARTUP_STAGES } from "@paperclipai/shared"; import { RUN_STARTUP_STAGE_LABELS, runStartupStageLabel, - STARTUP_LABEL_MIN_DISPLAY_MS, + STARTUP_LABEL_MIN_HOLD_MS, + STARTUP_LABEL_SHOW_DELAY_MS, + startupLabelHoldMs, useHeldLabel, } from "./run-startup-stage"; @@ -41,41 +43,98 @@ it("names every startup stage and the resumed session", () => { expect(runStartupStageLabel(null)).toBeNull(); }); -it("shows a changed label at once after the minimum time", () => { - vi.useFakeTimers(); - const { result, rerender } = renderHeldLabel("Getting ready"); - act(() => { vi.advanceTimersByTime(STARTUP_LABEL_MIN_DISPLAY_MS); }); - rerender("Waking up"); - expect(result.current).toBe("Waking up"); +const advance = (ms: number) => act(() => { vi.advanceTimersByTime(ms); }); + +it("holds each label for its reading time, at least five-sixths of a second", () => { + expect(startupLabelHoldMs("Waking up")).toBe(STARTUP_LABEL_MIN_HOLD_MS); + expect(startupLabelHoldMs("Starting agent")).toBe(STARTUP_LABEL_MIN_HOLD_MS); + // 19 characters at 20 a second. + expect(startupLabelHoldMs("Preparing workspace")).toBe(950); + expect(startupLabelHoldMs(null)).toBe(0); }); -it("holds a fresh label, then skips to the latest one", () => { +it("shows the current stage at once when the line first appears", () => { vi.useFakeTimers(); - const { result, rerender } = renderHeldLabel("Getting ready"); - act(() => { vi.advanceTimersByTime(100); }); - rerender("Waking up"); - rerender("Preparing workspace"); - expect(result.current).toBe("Getting ready"); - act(() => { vi.advanceTimersByTime(STARTUP_LABEL_MIN_DISPLAY_MS - 101); }); - expect(result.current).toBe("Getting ready"); - act(() => { vi.advanceTimersByTime(1); }); + const { result } = renderHeldLabel("Preparing workspace"); expect(result.current).toBe("Preparing workspace"); }); -it("holds the last stage before returning to the default", () => { +it("shows a new stage once it has stayed current for the show delay", () => { vi.useFakeTimers(); - const { result, rerender } = renderHeldLabel("Starting agent"); - rerender(null); - expect(result.current).toBe("Starting agent"); - act(() => { vi.advanceTimersByTime(STARTUP_LABEL_MIN_DISPLAY_MS); }); - expect(result.current).toBeNull(); + const { result, rerender } = renderHeldLabel("Getting ready"); + advance(startupLabelHoldMs("Getting ready")); + rerender("Waking up"); + expect(result.current).toBe("Getting ready"); + advance(STARTUP_LABEL_SHOW_DELAY_MS - 1); + expect(result.current).toBe("Getting ready"); + advance(1); + expect(result.current).toBe("Waking up"); }); -it("keeps the shown label when a change is undone within the hold", () => { +it("never shows a stage that ends within the show delay", () => { + vi.useFakeTimers(); + const { result, rerender } = renderHeldLabel("Starting agent"); + advance(startupLabelHoldMs("Starting agent")); + // A step a few milliseconds long, inside a longer one. + rerender("Connecting tools"); + advance(5); + rerender("Starting agent"); + advance(STARTUP_LABEL_SHOW_DELAY_MS * 4); + expect(result.current).toBe("Starting agent"); + // A short step followed by another stage: only the stage that stays current shows. + rerender("Restoring session"); + advance(STARTUP_LABEL_SHOW_DELAY_MS - 1); + rerender("Preparing workspace"); + advance(STARTUP_LABEL_SHOW_DELAY_MS - 1); + expect(result.current).toBe("Starting agent"); + advance(1); + expect(result.current).toBe("Preparing workspace"); +}); + +it("holds a fresh label for its reading time, then skips to the latest one", () => { + vi.useFakeTimers(); + const { result, rerender } = renderHeldLabel("Getting ready"); + advance(100); + rerender("Waking up"); + rerender("Preparing workspace"); + // The show delay has passed, but the shown label has not been up long enough to read. + advance(STARTUP_LABEL_MIN_HOLD_MS - 101); + expect(result.current).toBe("Getting ready"); + advance(1); + expect(result.current).toBe("Preparing workspace"); +}); + +it("holds a longer label for longer", () => { + vi.useFakeTimers(); + const { result, rerender } = renderHeldLabel("Preparing workspace"); + rerender("Starting agent"); + advance(startupLabelHoldMs("Preparing workspace") - 1); + expect(result.current).toBe("Preparing workspace"); + advance(1); + expect(result.current).toBe("Starting agent"); +}); + +it("ends startup without the show delay, once the last label has been up long enough to read", () => { + vi.useFakeTimers(); + const settled = renderHeldLabel("Starting agent"); + advance(startupLabelHoldMs("Starting agent")); + settled.rerender(null); + expect(settled.result.current).toBeNull(); + + const fresh = renderHeldLabel("Starting agent"); + fresh.rerender(null); + expect(fresh.result.current).toBe("Starting agent"); + advance(startupLabelHoldMs("Starting agent") - 1); + expect(fresh.result.current).toBe("Starting agent"); + advance(1); + expect(fresh.result.current).toBeNull(); +}); + +it("keeps the shown label when a change is undone", () => { vi.useFakeTimers(); const { result, rerender } = renderHeldLabel("Getting ready"); rerender("Waking up"); rerender("Getting ready"); - act(() => { vi.advanceTimersByTime(STARTUP_LABEL_MIN_DISPLAY_MS * 2); }); + advance(STARTUP_LABEL_MIN_HOLD_MS * 2); expect(result.current).toBe("Getting ready"); }); diff --git a/ui/src/components/task-chat/run-startup-stage.ts b/ui/src/components/task-chat/run-startup-stage.ts index ace54fa3cd2..d1d68a181b6 100644 --- a/ui/src/components/task-chat/run-startup-stage.ts +++ b/ui/src/components/task-chat/run-startup-stage.ts @@ -21,35 +21,62 @@ export function runStartupStageLabel( } /** - * How long a shown label stays before the next one replaces it. Startup steps - * often take a few milliseconds, so a shorter hold would flicker. + * How long a new stage must stay current before its label replaces the shown + * one. Most startup steps take a few milliseconds; a label for one of them + * would only stay up, held for reading, after its step had ended. This is the + * delay loading indicators commonly use before they appear. */ -export const STARTUP_LABEL_MIN_DISPLAY_MS = 500; +export const STARTUP_LABEL_SHOW_DELAY_MS = 300; + +/** The shortest time a shown label stays: five-sixths of a second, the usual subtitle minimum. */ +export const STARTUP_LABEL_MIN_HOLD_MS = 833; + +/** Reading time per character, at 20 characters a second, the usual adult subtitle reading speed. */ +export const STARTUP_LABEL_MS_PER_CHARACTER = 50; + +/** How long a shown label stays before the next one replaces it, long enough to read it comfortably. */ +export function startupLabelHoldMs(label: string | null): number { + return label === null ? 0 : Math.max(STARTUP_LABEL_MIN_HOLD_MS, label.length * STARTUP_LABEL_MS_PER_CHARACTER); +} + +/** How long `label` must still wait before it replaces `shown`; zero or less when it can show now. */ +function heldLabelWaitMs(label: string | null, since: number, shown: string | null, shownAt: number, now: number): number { + const delay = label === null ? 0 : STARTUP_LABEL_SHOW_DELAY_MS - (now - since); + return Math.max(delay, startupLabelHoldMs(shown) - (now - shownAt)); +} /** - * Shows a changed label at once when the current one has been visible for the - * minimum time. Otherwise it waits out the rest of that time and then shows - * the latest label, skipping any that came and went in between. Nothing is - * delayed by a prediction of how long a step will last. + * The label to show for a label that changes as startup moves on. A new label + * shows once it has stayed current for the show delay, so a step too short to + * name never appears, and once the shown label has been up long enough to + * read. Labels that came and went in between are skipped. The end of startup + * (null) needs no delay, because it is not a guess about a step's length, but + * the last label is still held so that it can be read. */ -export function useHeldLabel(label: string | null, minDisplayMs = STARTUP_LABEL_MIN_DISPLAY_MS): string | null { +export function useHeldLabel(label: string | null): string | null { + // The first label shows at once: a task opened during startup may show a stage that has been current for a while, + // and the default in its place, `Thinking`, would be wrong. const [shown, setShown] = useState(label); const shownAtRef = useRef(Date.now()); - // Once the hold has passed, show the change in this render, not a later one. - const holdPassed = label !== shown && Date.now() - shownAtRef.current >= minDisplayMs; + // When the current label value first arrived. + const currentRef = useRef({ label, since: Date.now() }); + if (currentRef.current.label !== label) currentRef.current = { label, since: Date.now() }; + // Once the delay and the hold have passed, show the change in this render, not a later one. + const ready = label !== shown && + heldLabelWaitMs(label, currentRef.current.since, shown, shownAtRef.current, Date.now()) <= 0; useEffect(() => { if (label === shown) return; - const remainingMs = minDisplayMs - (Date.now() - shownAtRef.current); const show = () => { shownAtRef.current = Date.now(); setShown(label); }; + const remainingMs = heldLabelWaitMs(label, currentRef.current.since, shown, shownAtRef.current, Date.now()); if (remainingMs <= 0) { show(); return; } const timer = setTimeout(show, remainingMs); return () => clearTimeout(timer); - }, [label, shown, minDisplayMs]); - return holdPassed ? label : shown; + }, [label, shown]); + return ready ? label : shown; }