diff --git a/apps/web/src/components/chat/ThreadMilestonesView.tsx b/apps/web/src/components/chat/ThreadMilestonesView.tsx
index b2f4e41fb89e5..f395efa838758 100644
--- a/apps/web/src/components/chat/ThreadMilestonesView.tsx
+++ b/apps/web/src/components/chat/ThreadMilestonesView.tsx
@@ -1,23 +1,24 @@
-import { secondTick } from "@t3tools/client-runtime/clock";
-import { isActiveSubagentStatus } from "@t3tools/client-runtime/state/subagentRuntime";
+import { scopedThreadKey } from "@t3tools/client-runtime/environment";
+import { formatSubagentDisplayTitle } from "@t3tools/client-runtime/state/subagent-display";
import type { ThreadStructureTurnItem } from "@t3tools/client-runtime/state/models";
-import {
- deriveMilestoneBoards,
- MILESTONE_BOARD_ROOT,
- type MilestoneBoardItem,
- parseMilestoneComment,
-} from "@t3tools/client-runtime/state/thread-milestone-board";
+import { deriveMilestoneBoards } from "@t3tools/client-runtime/state/thread-milestone-board";
import {
deriveCoordinatorActivity,
- deriveThreadMilestoneGraph,
type MilestoneCoordinator,
- type MilestoneGraphNode,
- type MilestoneGraphTurn,
- milestoneStripWeights,
} from "@t3tools/client-runtime/state/thread-milestone-graph";
-import { resolveActiveThreadRun } from "@t3tools/client-runtime/state/thread-workflows";
+import {
+ deriveThreadTrace,
+ formatTraceDuration,
+ threadTraceDomain,
+ type ThreadTrace,
+ type TraceAgent,
+ type TraceCounts,
+ type TraceDomain,
+ type TraceGroup,
+ type TraceRow,
+ traceRowDomain,
+} from "@t3tools/client-runtime/state/thread-milestone-trace";
import type {
- EnvironmentId,
OrchestrationV2Milestone,
OrchestrationV2Subagent,
OrchestrationV2ThreadProjection,
@@ -25,23 +26,17 @@ import type {
ThreadId,
} from "@t3tools/contracts";
import { formatModelSlugName } from "@t3tools/shared/model";
-import {
- CheckIcon,
- ChevronRightIcon,
- CornerDownLeftIcon,
- UserRoundIcon,
- XIcon,
-} from "lucide-react";
import * as DateTime from "effect/DateTime";
-import { type ReactNode, useState } from "react";
-import { flushSync } from "react-dom";
+import { ArrowLeftIcon, ChevronLeftIcon, ChevronRightIcon, TriangleAlertIcon } from "lucide-react";
+import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react";
-import { useThreadViewModeStore } from "../../threadViewModeStore";
import { cn } from "~/lib/utils";
+import { type MilestoneZoom, useThreadViewModeStore } from "../../threadViewModeStore";
import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
+import { Kbd } from "../ui/kbd";
+import { Tooltip, TooltipTrigger } from "../ui/tooltip";
import { AgentElapsed } from "./AgentElapsed";
-import { AgentStack, AgentTray, agentTraySummary } from "./MilestoneAgents";
import {
boardCounts,
CommentButton,
@@ -49,373 +44,222 @@ import {
CoordinatorAskCard,
MilestoneBoard,
} from "./MilestoneBoard";
-import { EnvironmentSubagentAvatar } from "./V2LifecycleRow";
+import {
+ afterNowStyle,
+ atNowStyle,
+ atStyle,
+ MilestoneStatusIcon,
+ spanStyle,
+ TraceBar,
+ TraceChart,
+ traceCoverClass,
+ TraceLabel,
+ TraceLegendSwatch,
+ TracePin,
+ TracePlot,
+ traceRowClass,
+ TraceStrand,
+ TraceText,
+ useAgentsWaitingOnYou,
+} from "./MilestoneTrace";
+import {
+ EnvironmentSubagentAvatar,
+ SubagentHoverCard,
+ subagentDetailLine,
+ useSubagentLink,
+} from "./V2LifecycleRow";
type Projection = Pick<
OrchestrationV2ThreadProjection,
"runs" | "subagents" | "milestones" | "messages"
>;
-type Run = Projection["runs"][number];
-type Line = "solid" | "dashed" | "none";
type Tone = "info" | "success" | "error" | "warning" | "outline";
-const iso = (value: DateTime.Utc | null) => (value === null ? null : DateTime.formatIso(value));
+const iso = (ms: number | null) =>
+ ms === null ? null : DateTime.formatIso(DateTime.makeUnsafe(ms));
+const plural = (count: number, word: string) => `${count} ${count === 1 ? word : `${word}s`}`;
-function timesWord(count: number) {
- return count === 1 ? "once" : count === 2 ? "twice" : `${count} times`;
+/** Zooming in, from the view or the Plan panel, takes focus so Esc and the arrows reach the view. */
+function focusZoomedPanel(node: HTMLDivElement | null) {
+ const active = document.activeElement;
+ if (
+ node === null ||
+ (active instanceof HTMLElement &&
+ (active.isContentEditable || active.closest("input, textarea, select") !== null))
+ )
+ return;
+ node.focus({ preventScroll: true });
}
-/** The first line of the user's message for a run, when that message is loaded. */
-function turnText(projection: Projection, turn: MilestoneGraphTurn): string | null {
- const message = projection.messages.find(({ id }) => id === turn.run.userMessageId);
- const line = message?.text.trim().split("\n")[0]?.trim();
- return line ? line : null;
-}
+const linkProps = (
+ threadRef: ScopedThreadRef,
+ agent: OrchestrationV2Subagent,
+ onOpenThread: (threadId: ThreadId) => void,
+) => ({
+ parentRef: threadRef,
+ subagentId: agent.id,
+ providerInstanceId: agent.providerInstanceId,
+ origin: agent.origin,
+ status: agent.status,
+ driver: agent.driver,
+ title: formatSubagentDisplayTitle(agent.title ?? "Subagent"),
+ result: agent.result,
+ progress: agent.progress,
+ startedAt: agent.startedAt,
+ completedAt: agent.completedAt,
+ threadId: agent.childThreadId,
+ remote: agent.remoteChild,
+ onOpenThread,
+});
-function turnLabel(turn: MilestoneGraphTurn): string {
- const at = DateTime.toDate(turn.run.requestedAt).toLocaleTimeString([], {
- hour: "numeric",
- minute: "2-digit",
- });
- return `Turn ${turn.run.ordinal} · ${at}`;
+function coordinatorPhrase(
+ coordinator: MilestoneCoordinator,
+ awaitingUser: boolean,
+ activity: string | null,
+): string {
+ if (awaitingUser) return "Waiting for you";
+ switch (coordinator.status) {
+ case "waiting":
+ return activity ?? `Waiting on ${plural(coordinator.liveAgents, "agent")}`;
+ case "working":
+ return activity ?? "Working";
+ case "failed":
+ return "Failed";
+ case "interrupted":
+ return "Stopped";
+ case "idle":
+ return "Idle";
+ }
}
-const lineClass = (line: Line) =>
- line === "solid" ? "w-px bg-muted-foreground/35" : "border-l border-dashed";
-
-const focusRing =
- "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset focus-visible:outline-none";
-
-const scrollBehavior = (): ScrollBehavior =>
- window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth";
-
-const scrollToSelector = (selector: string) =>
- document.querySelector(selector)?.scrollIntoView({ block: "center", behavior: scrollBehavior() });
-
-/**
- * One stop on the spine: a node in the left gutter, the line arriving at it (`lineAbove`) and the
- * line leaving it toward the next stop (`line`).
- */
-function SpineItem(props: {
- readonly node: ReactNode;
- readonly line: Line;
- readonly lineAbove?: Line;
- readonly first?: boolean;
- readonly last?: boolean;
- readonly enterDelay?: number | null;
- readonly children: ReactNode;
- readonly milestoneId?: string;
-}) {
- const above = props.lineAbove ?? props.line;
- return (
-
- {props.first || above === "none" ? null : (
-
- )}
- {props.last || props.line === "none" ? null : (
-
- )}
-
- {props.node}
-
- {props.children}
-
- );
+function overallBadge(
+ coordinator: MilestoneCoordinator,
+ awaitingUser: boolean,
+ allDone: boolean,
+): { readonly label: string; readonly tone: Tone } {
+ if (awaitingUser) return { label: "Needs you", tone: "warning" };
+ switch (coordinator.status) {
+ case "working":
+ case "waiting":
+ return { label: "Working", tone: "info" };
+ case "failed":
+ return { label: "Failed", tone: "error" };
+ case "interrupted":
+ return { label: "Stopped", tone: "warning" };
+ case "idle":
+ return allDone ? { label: "Done", tone: "success" } : { label: "Idle", tone: "outline" };
+ }
}
-function MilestoneNode(props: {
- readonly status: OrchestrationV2Milestone["status"];
- readonly live: boolean;
-}) {
- switch (props.status) {
- case "done":
- return (
-
-
-
- );
+function rowBadge(status: TraceRow["status"]): { readonly label: string; readonly tone: Tone } {
+ switch (status) {
case "active":
- return (
-
-
-
- );
+ return { label: "In progress", tone: "info" };
+ case "done":
+ return { label: "Done", tone: "success" };
+ case "failed":
+ return { label: "Failed", tone: "error" };
+ case "stopped":
+ return { label: "Stopped", tone: "outline" };
case "dropped":
- return (
-
-
-
- );
+ return { label: "Dropped", tone: "outline" };
case "planned":
- return
;
+ return { label: "Planned", tone: "outline" };
}
}
-/** A user message on the spine. */
-function YouNode() {
- return (
-
-
-
- );
-}
-
-function JumpHint() {
- return (
-
-
- Show in chat
-
- );
-}
+const AGENT_BADGE: Record
= {
+ running: { label: "Running", tone: "info" },
+ "needs-you": { label: "Needs you", tone: "warning" },
+ done: { label: "Done", tone: "success" },
+ failed: { label: "Failed", tone: "error" },
+ queued: { label: "Queued", tone: "outline" },
+ stopped: { label: "Stopped", tone: "outline" },
+};
-const headClass = cn(
- "group/jump -ml-2.5 flex min-h-8 w-full min-w-0 items-center gap-2.5 rounded-lg px-2.5 py-1 text-left hover:bg-accent",
- focusRing,
-);
-
-/** Your message as it reads in Chat; selecting it opens Chat there. */
-function YouMessage(props: { readonly text: string; readonly onJump: () => void }) {
+function StatusBadge(props: {
+ readonly label: string;
+ readonly tone: Tone;
+ readonly dot?: boolean;
+}) {
return (
-
+ {props.label}
+
);
}
-function Elapsed(props: {
- readonly running: boolean;
- readonly startedAt: DateTime.Utc | null;
- readonly completedAt: DateTime.Utc | null;
-}) {
- return (
-
-
-
- );
-}
+const live = (counts: TraceCounts) => counts.running + counts.needsYou;
-function SectionHead(props: { readonly title: string; readonly detail: string }) {
+/** "1 of 3 done · 1 failed · needs you" for a group sub-row. */
+function groupSummary(group: TraceGroup): ReactNode {
+ const { counts } = group;
return (
-
- {props.title}
- {props.detail}
-
+ <>
+ {counts.done} of {group.agents.length} done
+ {counts.failed > 0 ? (
+ · {counts.failed} failed
+ ) : null}
+ {counts.needsYou > 0 ? · needs you : null}
+ >
);
}
-/**
- * The plan as one strip, each milestone sized by the time it took. The active segment grows on
- * the shared second tick through the DOM, so the view doesn't re-render every second.
- */
-function PlanStrip(props: {
- readonly milestones: ReadonlyArray;
- readonly ticking: boolean;
- readonly onSelect: (milestone: OrchestrationV2Milestone) => void;
-}) {
- const { milestones, ticking } = props;
- const labelled = milestones.length <= 5;
- const current =
- milestones.find(({ status }) => status === "active") ??
- milestones.findLast(({ status }) => status === "done");
- const sizeSegments = (node: HTMLDivElement | null) => {
- if (!node) return;
- const write = () => {
- for (const [index, weight] of milestoneStripWeights(milestones, Date.now()).entries()) {
- const segment = node.children[index];
- if (segment instanceof HTMLElement) segment.style.flexGrow = String(weight);
- }
- };
- write();
- return ticking ? secondTick.subscribe(write) : undefined;
- };
+/** "1 running · 1/3 done · 1 failed" for a group header in a zoomed milestone. */
+function groupDetail(group: TraceGroup): ReactNode {
+ const { counts } = group;
return (
-
-
- {milestones.map((milestone) => (
-
- ))}
-
- {labelled || current === undefined ? null : (
-
{current.title}
- )}
-
+ <>
+ {live(counts) > 0 ? `${live(counts)} running · ` : ""}
+ {counts.done}/{group.agents.length} done
+ {counts.failed > 0 ? (
+ · {counts.failed} failed
+ ) : null}
+ {counts.needsYou > 0 ? (
+ · {counts.needsYou} needs you
+ ) : null}
+ >
);
}
-function coordinatorPhrase(coordinator: MilestoneCoordinator, awaitingUser: boolean): string {
- if (awaitingUser) return "is waiting for you";
- switch (coordinator.status) {
- case "waiting":
- return `is waiting on ${coordinator.liveAgents} ${coordinator.liveAgents === 1 ? "agent" : "agents"}`;
- case "working":
- return "is working";
- case "failed":
- return "failed";
- case "interrupted":
- return "was interrupted";
- case "idle":
- return "is idle";
- }
-}
-
-/** The coordinator's own stop: what it is doing now and for how long. */
-function CoordinatorCard(props: {
- readonly environmentId: EnvironmentId;
- readonly coordinator: MilestoneCoordinator;
- readonly run: Run | null;
- readonly activity: string | null;
- readonly awaitingUser: boolean;
- /** Absent when the question already shows on a milestone. */
- readonly onAnswer?: (() => void) | undefined;
- /** A secret or link card to answer here, when no milestone holds it. */
- readonly askCard?: ReactNode;
- readonly lineAbove: Line;
- readonly line: Line;
- readonly last: boolean;
-}) {
- const { status } = props.coordinator;
- const live = status === "working" || status === "waiting";
- const model = props.run ? formatModelSlugName(props.run.modelSelection.model) : null;
- return (
-
- }
- >
-
- {props.run === null ? null : (
-
- )}
-
-
- Coordinator{" "}
- {model ? `${model} · ` : ""}
- {coordinatorPhrase(props.coordinator, props.awaitingUser)}
-
- {live && !props.awaitingUser && props.activity !== null ? (
-
{props.activity}
- ) : null}
-
- {props.awaitingUser && props.onAnswer !== undefined ? (
-
- ) : live && props.run?.startedAt ? (
-
-
-
- ) : null}
-
- {props.askCard ? {props.askCard}
: null}
-
+function RowDuration(props: { readonly row: TraceRow }) {
+ const { span } = props.row;
+ if (span === null) return null;
+ return span.endMs === null ? (
+
+ ) : (
+ {formatTraceDuration(span.endMs - span.startMs)}
);
}
-function focusBadge(status: OrchestrationV2Milestone["status"]): {
- readonly label: string;
- readonly tone: Tone;
-} {
- switch (status) {
- case "active":
- return { label: "In progress", tone: "info" };
- case "done":
- return { label: "Done", tone: "success" };
- case "dropped":
- return { label: "Dropped", tone: "outline" };
- case "planned":
- return { label: "Planned", tone: "outline" };
+function rowMeta(row: TraceRow, trace: ThreadTrace, posts: number): ReactNode {
+ if (row.status === "planned") return trace.kind === "turns" ? "Queued" : "Planned";
+ const parts: Array = [
+ row.status === "dropped" ? "Dropped" : ,
+ ];
+ if (row.agentCount > 0) {
+ parts.push(plural(row.agentCount, "agent"));
+ if (live(row.counts) > 1) {
+ parts.push(
+
+ {live(row.counts)} in parallel
+ ,
+ );
+ } else if (row.counts.failed > 0) {
+ parts.push(
+
+ {row.counts.failed} failed
+ ,
+ );
+ } else if (row.peakParallel > 1) {
+ parts.push(`${row.peakParallel} in parallel`);
+ }
}
+ if (posts > 0 && parts.length < 3) parts.push(`${posts} on the board`);
+ return parts.flatMap((part, index) => (index === 0 ? [part] : [" · ", part]));
}
function Revision(props: { readonly milestone: OrchestrationV2Milestone }) {
@@ -423,6 +267,7 @@ function Revision(props: { readonly milestone: OrchestrationV2Milestone }) {
if (milestone.revision !== undefined) {
return (
+ Changed from{" "}
{milestone.revision.previousTitle}
{milestone.revision.reason}
@@ -430,74 +275,172 @@ function Revision(props: { readonly milestone: OrchestrationV2Milestone }) {
);
}
return milestone.note === undefined ? null : (
-
{milestone.note}
+ {milestone.note}
);
}
-/** What a folded milestone holds, as chips; selecting them unfolds it. */
-function FoldedSummary(props: {
+function AnswerAgentButton(props: {
readonly threadRef: ScopedThreadRef;
- readonly agents: ReadonlyArray;
- readonly items: ReadonlyArray;
- readonly open: boolean;
- readonly onToggle: () => void;
+ readonly agent: OrchestrationV2Subagent;
+ readonly onOpenThread: (threadId: ThreadId) => void;
}) {
- const failed = props.agents.filter(({ status }) => status === "failed").length;
- const chip =
- "inline-flex h-7 items-center gap-1.5 rounded-lg border bg-card px-2 text-xs text-foreground/80 group-hover/fold:border-foreground/20";
+ const link = useSubagentLink(linkProps(props.threadRef, props.agent, props.onOpenThread));
+ if (link.open === null) return null;
return (
-