ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
"use client";
import * as React from "react";
import {
ArrowUpIcon,
BrainIcon,
ChevronDownIcon,
CircleCheckIcon,
CircleSlashIcon,
CircleXIcon,
ClockIcon,
HistoryIcon,
Loader2Icon,
PaperclipIcon,
PauseIcon,
PlusIcon,
RadioIcon,
RotateCwIcon,
ShieldAlertIcon,
SquareIcon,
Trash2Icon,
UserIcon,
WifiOffIcon,
XIcon,
ZapOffIcon,
} from "lucide-react";
import { toast } from "sonner";
import { ApprovalExecutionFocus, useApprovalFocus, useApprovalHistory } from "@/components/approval-execution-focus";
import { MentionTextarea } from "@/components/mention-textarea";
import { SideQuestionButton, SideQuestionWorkspace } from "@/components/side-question-workspace";
import { TodoPopover } from "@/components/todo-popover";
import { Transcript } from "@/components/transcript";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { InputGroup, InputGroupAddon, InputGroupButton } from "@/components/ui/input-group";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Spinner } from "@/components/ui/spinner";
import { Textarea } from "@/components/ui/textarea";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { useSideQuestions } from "@/hooks/use-side-questions";
import { api, sseUrl } from "@/lib/api";
import { shouldSubmitOnKey, useChatSendMode } from "@/lib/chat-send-mode";
import { MOCK } from "@/lib/mock/enabled";
import { isBtwCommand } from "@/lib/side-questions";
import { taskAssetSourceLabel, taskAssetTypeLabel } from "@/lib/task-assets";
import type {
Activity,
ChatAttachment,
IntentAsset,
InterceptApprovalRow,
Session,
SessionStatus,
SessionTokenUsage,
TaskLLMResolution,
TaskLLMResolutions,
TaskNode,
TokenTotal,
} from "@/lib/types";
import { cn } from "@/lib/utils";
import { setVisibleInterval } from "@/lib/polling";
import { intlLocale, t } from "@/i18n";
// resolutionLabel names the LLM a session runs on. The badge shows this alone and
// keeps the model id in its tooltip. Env-backed configs can arrive without a name,
// so fall back to the model id rather than rendering an empty badge.
function resolutionLabel(r: TaskLLMResolution): string {
return r.name || r.model || t("Unnamed config");
}
// fmtBytes renders a human file size for attachment chips (mirrors transcript.tsx).
function fmtBytes(n: number): string {
if (n >= 1 << 20) return `${(n / (1 << 20)).toFixed(1)} MB`;
if (n >= 1 << 10) return `${(n / (1 << 10)).toFixed(1)} KB`;
return `${n} B`;
}
// ── Reliability model (see docs/task-session-history-sse-remediation.md) ──────────
// The task's activity is NO LONGER one unbounded `allActivity` array replayed from
// SSE since=0. Instead:
// • Each UI session (main | plan | intent:<id>) has its own lazily-loaded, reverse-
// paginated cache (SessionState below). Opening a session loads only its latest
// page; scrolling up pages older history in.
// • A single task-level SSE (opened at since=snapshot_cursor from the first history
// page) tails ALL agents' new activity; frames are dispatched by session_key.
// • History + SSE meet gap-free at snapshot_cursor and are merged by seq (dedup),
// so refresh / tab-switch / sleep / reconnect never drop the newest records.
const PAGE = 200; // history page size
const SYSTEM_SCAN_PAGE = 500; // generic activity pages scanned to recover sparse system audit events
const MAX_KEEP = 4000; // per-session in-memory cap; older pages re-fetched on scroll-up
const STREAM_WINDOW_MS = 5000; // "live" = activity seen within this window
const MAX_WORKER_MESSAGE_CHARS = 4000;
function newWorkerMessageRequestID(): string {
return (
globalThis.crypto?.randomUUID?.() ??
`worker-message-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`
);
}
// boldMarkup renders a translated sentence whose **marked** spans are bold, so the
// emphasis can move with Turkish word order instead of splitting the sentence.
function boldMarkup(text: string): React.ReactNode {
return text.split(/\*\*(.+?)\*\*/g).map((part, index) =>
// biome-ignore lint/suspicious/noArrayIndexKey: static split of a fixed string
index % 2 === 1 ? <strong key={index}>{part}</strong> : <React.Fragment key={index}>{part}</React.Fragment>,
);
}
function workerMessageCharCount(value: string): number {
return Array.from(value).length;
}
// One session's lazily-loaded, reverse-paginated cache. `lastTs`/`unread` are kept
// live even for sessions that were never opened, so the list shows liveness + unread
// without holding their full history.
type SessionState = {
items: Activity[];
loaded: boolean;
loading: boolean;
loadingMore: boolean;
hasMore: boolean; // older history remains above the loaded window
earliestSeq: number; // earliest loaded id — reverse-pagination anchor
unread: number;
lastTs: string; // most-recent activity time (drives live badge; updated even when unloaded)
error?: string;
};
type SessionStore = Record<string, SessionState>;
function emptyState(): SessionState {
return {
items: [],
loaded: false,
loading: false,
loadingMore: false,
hasMore: false,
earliestSeq: 0,
unread: 0,
lastTs: "",
};
}
// sessionKeyOf routes an activity to its stable session key. worker="planner" covers
// BOTH the Goal Agent's round-0 decomposition and the Planner (single Plan session).
function sessionKeyOf(a: Activity): string {
if (a.worker === "system" || a.kind === "llm_switch" || a.kind === "llm_failover") return "system";
if (a.worker === "mainagent") return `main:${a.main_seg ?? 0}`; // one key per conversation segment
if (a.worker === "planner") return "plan";
if (a.intent_id) return `intent:${a.intent_id}`;
return "unknown";
}
// mergeBySeq unions two activity lists by seq (dedup) in ascending seq order. Every
// data source — latest page, older page, SSE compensation, live tail — goes through
// this, so history responses can never clobber live records received meanwhile.
function mergeBySeq(current: Activity[], incoming: Activity[]): Activity[] {
if (!incoming.length) return current;
const bySeq = new Map<number, Activity>();
for (const a of current) bySeq.set(a.seq, a);
for (const a of incoming) bySeq.set(a.seq, a);
return [...bySeq.values()].sort((p, q) => p.seq - q.seq);
}
// statusIcon maps a session status to its icon. Worker terminal states are
// distinct & color-coded: done (green check circle) / stopped (amber slash circle) / error (red X circle) /
// steps exhausted (violet). running = blue spinner, pending (open) = gray clock.
function statusIcon(status: SessionStatus) {
switch (status) {
case "running": // executing
return <Loader2Icon className="size-3.5 animate-spin text-blue-500" />;
case "paused":
return <PauseIcon className="size-3.5 text-amber-500" />;
case "pending": // open to claim (open intent)
return <ClockIcon className="size-3.5 text-muted-foreground" />;
case "done": // completed
return <CircleCheckIcon className="size-3.5 text-emerald-500" />;
case "stopped": // cancelled/stopped (terminated by the planner)
return <CircleSlashIcon className="size-3.5 text-amber-500" />;
case "blocked": // error
return <CircleXIcon className="size-3.5 text-red-500" />;
case "exhausted": // steps exhausted (hit max_turns)
return <ZapOffIcon className="size-3.5 text-violet-500" />;
case "deleted": // soft-deleted by the user
return <CircleSlashIcon className="size-3.5 text-muted-foreground" />;
}
}
// fmtTokens renders a compact token count (1234 → 1.2k, 2_000_000 → 2M).
function fmtTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
if (n >= 1000) return `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`;
return String(n);
}
const TokenMetrics = React.forwardRef<
HTMLSpanElement,
React.ComponentPropsWithoutRef<"span"> & {
input: number;
cache: number;
output: number;
labels?: "short" | "long";
}
>(({ input, cache, output, labels = "short", className, ...props }, ref) => {
const names = labels === "short" ? [t("In"), t("Cache"), t("Out")] : [t("input"), t("cache"), t("output")];
const values = [input, cache, output];
return (
<span
ref={ref}
className={cn("inline-flex min-w-0 flex-wrap items-center gap-1 text-muted-foreground", className)}
{...props}
>
{values.map((value, index) => (
<React.Fragment key={names[index]}>
<span>{names[index]}</span>
<Badge variant="secondary" className="h-5 px-1.5 font-mono tabular-nums">
{fmtTokens(value)}
</Badge>
</React.Fragment>
))}
</span>
);
});
TokenMetrics.displayName = "TokenMetrics";
// fmtDuration renders an elapsed milliseconds span compactly (90s → 1m30s).
function fmtDuration(ms: number): string {
const s = Math.floor(ms / 1000);
if (s < 60) return `${s}s`;
const m = Math.floor(s / 60);
if (m < 60) return `${m}m${String(s % 60).padStart(2, "0")}s`;
const h = Math.floor(m / 60);
return `${h}h${String(m % 60).padStart(2, "0")}m`;
}
const roleMeta = {
mainagent: { label: t("Main Agent"), icon: UserIcon },
planner: { label: t("Planner"), icon: BrainIcon },
worker: { label: t("Workers"), icon: RadioIcon },
system: { label: t("System audit"), icon: HistoryIcon },
} as const;
// The main-agent session is the interactive entry point of this tab and has no
// dedicated backend "sessions" endpoint — it is a fixed UI affordance whose
// transcript is the main-agent activity stream (worker="mainagent") for the task.
// A main-agent session is one resettable conversation segment. Segment 0 is the
// original session; "New session" creates further segments (seq 1,2,…) so the agent
// starts on a clean transcript while the task's graph/assets/goal stay shared. Each
// segment is a switchable UI session; only the current (highest) one is writable.
const mainSessionId = (seg: number) => `s-main-${seg}`;
const mainSessionKey = (seg: number) => `main:${seg}`;
const mainSessionTitle = (seg: number) => t("Main Agent · Session #{n}", { n: seg + 1 });
const MAIN_ID = mainSessionId(0);
const MAIN_SESSION: Session = {
id: MAIN_ID,
role: "mainagent",
title: mainSessionTitle(0),
status: "running",
live: true,
last_activity: "",
seg: 0,
};
// The planner session is, like the main-agent session, a fixed UI affordance with
// no dedicated backend "sessions" endpoint — its transcript is every activity step
// the planner emits (worker === "planner", which also carries the Goal Agent's
// round-0 decomposition; the planner carries no intent_id since it generates intents).
const PLANNER_ID = "s-planner";
const PLANNER_SESSION: Session = {
id: PLANNER_ID,
role: "planner",
title: t("Planner · situational analysis"),
status: "running",
live: true,
last_activity: "",
};
// LLM provider switches are task-level audit events rather than agent output. The
// history endpoint has no "system" filter, so this fixed session is populated by
// scanning the generic incremental activity endpoint and then tailed by task SSE.
const SYSTEM_ID = "s-system";
const SYSTEM_SESSION: Session = {
id: SYSTEM_ID,
role: "system",
title: t("System events · LLM failover"),
status: "done",
live: false,
last_activity: "",
};
// keyForSession maps a UI Session → its stable store key (main:<seg> | plan | intent:<id>).
function keyForSession(s: Session): string {
if (s.role === "mainagent") return `main:${s.seg ?? 0}`;
if (s.role === "planner") return "plan";
if (s.role === "system") return "system";
return `intent:${s.intent_id}`;
}
// Map an exploration intent (TaskNode) state → a session status the UI renders.
function intentStatus(state: string): SessionStatus {
switch (state) {
case "done":
return "done";
case "blocked":
return "blocked";
case "exhausted":
return "exhausted";
case "stopped":
return "stopped";
case "paused":
return "paused";
case "open": // open to claim, distinct from executing
return "pending";
case "deleted": // soft-deleted by the user
return "deleted";
default: // running
return "running";
}
}
// Derive worker sessions from running/open intents — there is no backend
// sessions endpoint, so intents (≈ worker units) are the closest real source.
function intentToSession(n: TaskNode): Session {
const label = (n.payload ?? "").trim();
const state = intentStatus(n.state);
return {
id: n.id,
role: "worker",
title: label || t("Intent {id}", { id: n.id }),
status: state,
live: !n.inherited && state === "running",
last_activity: n.ts,
intent_id: n.id,
source_task_id: n.source_task_id,
inherited: n.inherited,
};
}
function SessionItem({
s,
active,
displayTitle,
hasPending,
unread,
onClick,
onCancel,
controlling,
deleted,
}: {
s: Session;
active: boolean;
displayTitle: string;
hasPending?: boolean;
unread?: number;
onClick: () => void;
onCancel?: () => void;
controlling?: boolean;
deleted?: boolean;
}) {
const icon = deleted ? (
<Trash2Icon className="size-3.5 text-destructive" />
) : s.role === "worker" ? (
statusIcon(s.status)
) : s.live ? (
<Loader2Icon className="size-3.5 animate-spin text-blue-500" />
) : null;
const cancellable =
s.role === "worker" &&
!s.inherited &&
!deleted &&
// pending = open (unclaimed) intent; running and paused ones are also deletable.
(s.status === "running" || s.status === "paused" || s.status === "pending");
return (
<div
className={cn(
"group/session flex w-full items-center rounded-md transition-colors",
active ? "bg-accent text-accent-foreground" : "hover:bg-accent/50",
)}
>
<button
type="button"
onClick={onClick}
className="flex min-w-0 flex-1 items-center gap-1 px-2 py-1.5 text-left text-sm"
>
{icon ?? <span className="size-3.5 shrink-0" />}
{s.intent_id && (
<span className="shrink-0 rounded bg-muted px-1 py-0.5 font-mono text-[10px] tabular-nums text-muted-foreground">
#{s.intent_id}
</span>
)}
{s.inherited && s.source_task_id && (
<Badge variant="outline" className="shrink-0">
{t("Source #{id}", { id: s.source_task_id })}
</Badge>
)}
<span
className={cn("min-w-0 flex-1 truncate text-sm font-medium", deleted && "text-muted-foreground line-through")}
>
{displayTitle}
</span>
{deleted && (
<Badge variant="outline" className="shrink-0 border-destructive/40 text-destructive">
{t("Deleted")}
</Badge>
)}
{hasPending && <ShieldAlertIcon className="size-3.5 shrink-0 text-amber-500" />}
{!active && unread ? (
<span className="inline-flex min-w-4 items-center justify-center rounded-full bg-blue-500/15 px-1 text-[10px] font-medium tabular-nums text-blue-600 dark:text-blue-400">
{unread > 99 ? "99+" : unread}
</span>
) : null}
{s.live && (
<span className="inline-flex items-center gap-1 rounded bg-blue-500/15 px-1.5 py-0.5 text-[10px] font-medium text-blue-600 dark:text-blue-400">
<span className="size-1 animate-pulse rounded-full bg-blue-500" />
{t("Live")}
</span>
)}
</button>
{cancellable && (
<div className="flex shrink-0 items-center gap-0.5 pr-1 opacity-100 sm:opacity-0 sm:transition-opacity sm:group-hover/session:opacity-100 sm:group-focus-within/session:opacity-100">
<Button
type="button"
variant="ghost"
size="icon-xs"
onClick={onCancel}
disabled={controlling}
title={t("Delete this intent (reason required; soft or hard delete)")}
aria-label={t("Delete this intent (reason required; soft or hard delete)")}
className="text-destructive hover:text-destructive"
>
<Trash2Icon />
</Button>
</div>
)}
</div>
);
}
function truncateWorkerAssetLabel(value: string, maxChars = 30): string {
const chars = Array.from(value.trim());
if (chars.length <= maxChars) return chars.join("");
return `${chars.slice(0, Math.max(0, maxChars - 1)).join("")}…`;
}
function WorkerAssetBadge({ assets }: { assets: IntentAsset[] }) {
const displayAssets = assets.filter(
(asset) => asset.type === "root_domain" || asset.type === "subdomain" || asset.type === "ip",
);
if (displayAssets.length === 0) return null;
const first = displayAssets[0];
const firstRawLabel = first.label.trim() || `#${first.asset_id}`;
const firstLabel = truncateWorkerAssetLabel(firstRawLabel);
return (
<Tooltip>
<TooltipTrigger asChild>
<Badge variant="outline" className="max-w-60 shrink-0 font-normal" title={firstRawLabel}>
<span className="truncate">{t("Current assets: {label}", { label: firstLabel })}</span>
{displayAssets.length > 1 && <span className="shrink-0 tabular-nums">+{displayAssets.length - 1}</span>}
</Badge>
</TooltipTrigger>
<TooltipContent side="right" align="start" className="max-w-sm">
<div className="flex flex-col gap-2">
{displayAssets.map((asset) => (
<div key={`${asset.intent_id}-${asset.asset_id}`} className="min-w-0">
<div className="break-all font-mono text-xs">{asset.label.trim() || `#${asset.asset_id}`}</div>
<div className="mt-0.5 text-xs text-muted-foreground">
{taskAssetTypeLabel(asset.type)} · {taskAssetSourceLabel(asset.source)}
{asset.inherited ? ` · ${t("from source task #{id}", { id: asset.source_task_id ?? "" })}` : ""}
</div>
<div className="mt-0.5 [overflow-wrap:anywhere] text-xs">{asset.source_summary}</div>
</div>
))}
</div>
</TooltipContent>
</Tooltip>
);
}
export function SessionsTab({ taskId, taskPaused = false }: { taskId: string; taskPaused?: boolean }) {
const approvalFocus = useApprovalFocus({ taskId });
const [selectedSessionId, setActiveId] = React.useState(MAIN_ID);
const focusSession = React.useMemo<Session | undefined>(() => {
const source = approvalFocus.state?.source;
if (!source) return undefined;
if (source.session.startsWith("main:")) {
const seg = Number(source.session.slice(5));
return { ...MAIN_SESSION, id: mainSessionId(seg), seg, title: mainSessionTitle(seg), live: false };
}
if (source.session === "plan") return { ...PLANNER_SESSION, live: false };
if (source.session.startsWith("intent:")) {
const id = source.session.slice(7);
return {
id,
role: "worker",
intent_id: id,
title: t("Worker #{id}", { id }),
status: "done",
live: false,
last_activity: source.items[0]?.ts ?? "",
};
}
return undefined;
}, [approvalFocus.state?.source]);
// Keep a located archived/older session selectable after leaving focus mode.
const [locatedSession, setLocatedSession] = React.useState<{ taskId: string; session: Session }>();
React.useEffect(() => {
if (focusSession) setLocatedSession({ taskId, session: focusSession });
}, [taskId, focusSession]);
const retainedSession = locatedSession?.taskId === taskId ? locatedSession.session : undefined;
const activeId = focusSession?.id ?? selectedSessionId;
// Main-agent conversation segments (newest-first); currentSeg is the writable one.
const [mainSegs, setMainSegs] = React.useState<{ seq: number; created_at: string }[]>([{ seq: 0, created_at: "" }]);
const [currentSeg, setCurrentSeg] = React.useState(0);
const [creatingMain, setCreatingMain] = React.useState(false);
const [confirmNewMain, setConfirmNewMain] = React.useState(false);
// On mobile (<lg) the session list is collapsed by default: screen height is already tight, and a fixed 10~15rem list
// would squeeze the transcript below down to just the header and input. Collapsed, the transcript gets nearly all the height;
// tapping the title bar expands it to pick a session, collapsing again after choosing. Desktop is unaffected (always expanded from lg up).
const [listOpen, setListOpen] = React.useState(false);
// Per-session lazily-loaded caches, keyed by session_key (main | plan | intent:<id>).
const [store, setStore] = React.useState<SessionStore>({});
// Worker sessions derived from exploration intents (paged past the old 300 cap).
const [intents, setIntents] = React.useState<TaskNode[]>([]);
const [intentAssets, setIntentAssets] = React.useState<IntentAsset[]>([]);
const [olderIntents, setOlderIntents] = React.useState<TaskNode[]>([]);
const [firstIntentsHasMore, setFirstIntentsHasMore] = React.useState(false);
const [olderIntentsHasMore, setOlderIntentsHasMore] = React.useState(false);
const [hasLoadedOlderIntentsPage, setHasLoadedOlderIntentsPage] = React.useState(false);
const [loadingOlderIntents, setLoadingOlderIntents] = React.useState(false);
const [input, setInput] = React.useState("");
const [sending, setSending] = React.useState(false);
const [stopping, setStopping] = React.useState(false);
const [mainChatRunning, setMainChatRunning] = React.useState<boolean | null>(null);
const [controllingIntent, setControllingIntent] = React.useState<string | null>(null);
const [cancelIntent, setCancelIntent] = React.useState<Session | null>(null);
const [cancelReason, setCancelReason] = React.useState("");
// Delete mode: soft = soft delete (default, sets deleted + records the reason, keeps data) | hard = hard delete (cascade-removes exclusive descendants).
const [deleteMode, setDeleteMode] = React.useState<"soft" | "hard">("soft");
const [workerMessage, setWorkerMessage] = React.useState("");
const [workerMessageRequestId, setWorkerMessageRequestId] = React.useState("");
const [workerMessageSending, setWorkerMessageSending] = React.useState(false);
// Method 1, file upload: chosen attachments (already landed in the task work dir uploads/) are sent with the next message.
const [attachments, setAttachments] = React.useState<ChatAttachment[]>([]);
const [uploading, setUploading] = React.useState(false);
const fileInputRef = React.useRef<HTMLInputElement>(null);
async function pickFiles(files: FileList | null) {
if (!files || files.length === 0) return;
setUploading(true);
try {
const r = await api.chatUpload("task", taskId, Array.from(files));
setAttachments((prev) => [...prev, ...r.attachments]);
} catch (e) {
toast.error(t("Upload failed: {error}", { error: (e as Error).message }));
} finally {
setUploading(false);
if (fileInputRef.current) fileInputRef.current.value = "";
}
}
// Start a fresh main-agent session: only the segment counter advances — the task's
// graph/assets/goal are untouched, so the agent continues over the same task with a
// clean context. The old segment stays as read-only history you can switch back to.
async function createMainSession() {
if (creatingMain) return;
setCreatingMain(true);
try {
const r = await api.newMainSession(taskId);
setMainSegs((prev) => [{ seq: r.seq, created_at: r.created_at }, ...prev.filter((m) => m.seq !== r.seq)]);
setCurrentSeg(r.current ?? r.seq);
// Seed an empty, loaded state so the new (empty) session renders immediately.
setStore((prev) => ({ ...prev, [mainSessionKey(r.seq)]: { ...emptyState(), loaded: true } }));
setActiveId(mainSessionId(r.seq));
setListOpen(false);
setInput("");
} catch (e) {
toast.error(t("Failed to create session: {error}", { error: (e as Error).message }));
} finally {
setCreatingMain(false);
setConfirmNewMain(false);
}
}
const patchIntentState = React.useCallback((intentId: string, state?: string) => {
const patch = (rows: TaskNode[]) =>
state
? rows.map((row) => (row.id === intentId ? { ...row, state } : row))
: rows.filter((row) => row.id !== intentId);
setIntents(patch);
setOlderIntents(patch);
}, []);
const controlWorker = React.useCallback(
async (session: Session, action: "pause" | "resume" | "cancel", reason?: string, mode?: "soft" | "hard") => {
if (!session.intent_id || session.inherited || controllingIntent) return;
if (action === "cancel" && !reason?.trim()) {
toast.error(t("Please provide a deletion reason"));
return;
}
setControllingIntent(session.intent_id);
try {
const res = await api.controlIntent(taskId, session.intent_id, action, reason, mode);
if (action === "pause") {
patchIntentState(session.intent_id, "paused");
toast.success(t("Worker #{id} paused", { id: session.intent_id }));
} else if (action === "resume") {
patchIntentState(session.intent_id, "open");
toast.success(t("Worker #{id} resumed, waiting to be re-claimed", { id: session.intent_id }));
} else if (mode === "hard") {
// Hard delete: the intent and its exclusive downstream are physically removed; drop the row from the list.
patchIntentState(session.intent_id);
const d = res.deleted;
toast.success(
d
? t(
"Worker #{id} and its exclusive downstream were permanently deleted (including {intents} intents / {facts} facts / {findings} findings)",
{ id: session.intent_id, intents: d.intents, facts: d.facts, findings: d.findings },
)
: t("Worker #{id} and its exclusive downstream were permanently deleted", { id: session.intent_id }),
);
setCancelReason("");
} else {
// Soft delete: the intent is set to deleted with the reason recorded; nodes and output are kept.
patchIntentState(session.intent_id, "deleted");
toast.success(t("Worker #{id} deleted (reason recorded; the planner will re-plan accordingly)", { id: session.intent_id }));
setCancelReason("");
}
} catch (error) {
toast.error(t("Worker operation failed: {error}", { error: (error as Error).message }));
} finally {
setControllingIntent(null);
setCancelIntent(null);
}
},
[controllingIntent, patchIntentState, taskId],
);
// SSE connection state — surfaced so a dropped realtime link is visible, never
// silently shown as "no messages".
const [sseLive, setSseLive] = React.useState(false);
// Whole-task token total (all agents), polled from the backend aggregate.
const [taskTokens, setTaskTokens] = React.useState<TokenTotal | null>(null);
const [sessionTokens, setSessionTokens] = React.useState<Record<string, SessionTokenUsage>>({});
const [llmResolutions, setLLMResolutions] = React.useState<TaskLLMResolutions | null>(null);
// Pending intercept requests for this task — used to show warning icons on sessions.
const [pendingIntercepts, setPendingIntercepts] = React.useState<InterceptApprovalRow[]>([]);
// Refs backing SSE/loading without re-render churn.
const snapshotRef = React.useRef(0); // task-level snapshot cursor → SSE since=
const esRef = React.useRef<EventSource | null>(null);
const activeKeyRef = React.useRef(mainSessionKey(0)); // current session key (for SSE dispatch/unread)
const atBottomRef = React.useRef(true); // transcript pinned to bottom?
const llmToastSeqRef = React.useRef<Set<number>>(new Set());
const chatStatusRequestRef = React.useRef(0);
const firstIntentsRef = React.useRef<TaskNode[]>([]);
// Per-key request token: a stale response for a key is ignored (guards fast
// latest/older interleaving). Writes are ALWAYS keyed, so a late response can only
// touch its own session cache — never the currently-viewed one (see §7.5).
const reqTokenRef = React.useRef<Record<string, number>>({});
// Keys with a latest-page load in flight — dedups the double trigger where the
// first-load effect and the active-session effect both want "main" on mount (the
// former also opens the SSE, so it must not be pre-empted).
const loadingKeysRef = React.useRef<Set<string>>(new Set());
// ── store helpers ──────────────────────────────────────────────────────────────
const patchStore = React.useCallback((key: string, fn: (s: SessionState) => SessionState) => {
setStore((prev) => ({ ...prev, [key]: fn(prev[key] ?? emptyState()) }));
}, []);
// Load a session's LATEST page (before=0) on first open. Keyed + request-token
// guarded so a switch away can't corrupt the view.
const loadSession = React.useCallback(
(key: string) => {
if (MOCK) return; // MOCK preloads everything up front
if (loadingKeysRef.current.has(key)) return; // already in flight (e.g. main on mount)
loadingKeysRef.current.add(key);
const token = (reqTokenRef.current[key] ?? 0) + 1;
reqTokenRef.current[key] = token;
patchStore(key, (s) => ({ ...s, loading: true, error: undefined }));
if (key === "system") {
void (async () => {
let since = 0;
let systemItems: Activity[] = [];
for (;;) {
const page = await api.activity(taskId, { since, limit: SYSTEM_SCAN_PAGE });
if (reqTokenRef.current[key] !== token) return;
systemItems = mergeBySeq(
systemItems,
page.items.filter(
(item) => item.worker === "system" || item.kind === "llm_switch" || item.kind === "llm_failover",
),
);
if (page.items.length < SYSTEM_SCAN_PAGE || page.cursor <= since) break;
since = page.cursor;
}
patchStore(key, (s) => {
const items = mergeBySeq(systemItems, s.items);
return {
...s,
items,
loaded: true,
loading: false,
hasMore: false,
earliestSeq: items.length ? items[0].seq : 0,
unread: 0,
lastTs: items.length ? items[items.length - 1].ts : s.lastTs,
error: undefined,
};
});
})()
.catch((error) => {
if (reqTokenRef.current[key] !== token) return;
patchStore(key, (s) => ({
...s,
loading: false,
error: (error as Error).message || t("Failed to load"),
}));
})
.finally(() => loadingKeysRef.current.delete(key));
return;
}
api
.activityHistory(taskId, key, 0, PAGE)
.then((r) => {
if (reqTokenRef.current[key] !== token) return; // superseded
if (r.snapshotCursor > snapshotRef.current) snapshotRef.current = r.snapshotCursor;
patchStore(key, (s) => {
const items = mergeBySeq(r.items, s.items); // keep any live frames arrived meanwhile
return {
...s,
items,
loaded: true,
loading: false,
hasMore: r.hasMore,
earliestSeq: items.length ? items[0].seq : 0,
unread: 0,
error: undefined,
};
});
})
.catch((e) => {
if (reqTokenRef.current[key] !== token) return;
patchStore(key, (s) => ({ ...s, loading: false, error: (e as Error).message || t("Failed to load") }));
})
.finally(() => loadingKeysRef.current.delete(key));
},
[taskId, patchStore],
);
// Load one older page (scroll-up) for a session, preserving scroll position.
const loadEarlier = React.useCallback(
(key: string, viewport: () => HTMLElement | null) => {
const st = store[key];
if (!st || st.loadingMore || !st.hasMore || !st.earliestSeq) return;
const vp = viewport();
const prevH = vp?.scrollHeight ?? 0;
const prevTop = vp?.scrollTop ?? 0;
patchStore(key, (s) => ({ ...s, loadingMore: true }));
api
.activityHistory(taskId, key, st.earliestSeq, PAGE)
.then((r) => {
patchStore(key, (s) => {
const items = mergeBySeq(r.items, s.items);
return {
...s,
items,
loadingMore: false,
hasMore: r.hasMore,
earliestSeq: items.length ? items[0].seq : s.earliestSeq,
};
});
requestAnimationFrame(() => {
const v = viewport();
if (v) v.scrollTop = prevTop + (v.scrollHeight - prevH);
});
})
.catch(() => {
patchStore(key, (s) => ({ ...s, loadingMore: false }));
});
},
[taskId, store, patchStore],
);
// ── task token total (whole task, all agents) ───────────────────────────────────
React.useEffect(() => {
let alive = true;
const load = () =>
api
.tokenStats(taskId)
.then((r) => {
if (!alive) return;
setTaskTokens(r.total);
setSessionTokens(Object.fromEntries(r.sessions.map((item) => [item.session, item])));
})
.catch(() => {
// Polling is best-effort; the next interval retries automatically.
});
void load();
const stopPolling = setVisibleInterval(load, 5000);
return () => {
alive = false;
stopPolling();
};
}, [taskId]);
React.useEffect(() => {
let alive = true;
setMainChatRunning(null);
const load = () => {
const request = ++chatStatusRequestRef.current;
return api
.chatStatus(taskId)
.then(({ running }) => {
if (alive && chatStatusRequestRef.current === request) setMainChatRunning(running);
})
.catch(() => {
// Keep the last authoritative value. Before the first success, recent
// activity remains a conservative fallback.
});
};
void load();
const stopPolling = setVisibleInterval(load, 2000);
return () => {
alive = false;
stopPolling();
};
}, [taskId]);
React.useEffect(() => {
let alive = true;
const load = () =>
api
.taskLLMResolution(taskId)
.then((value) => {
if (alive) setLLMResolutions(value);
})
.catch(() => {
// Polling is best-effort; the next interval retries automatically.
});
void load();
const stopPolling = setVisibleInterval(load, 10_000);
return () => {
alive = false;
stopPolling();
};
}, [taskId]);
React.useEffect(() => {
let alive = true;
const load = () =>
api
.interceptTask(taskId)
.then((rows) => {
if (alive) setPendingIntercepts(rows.filter((r) => r.status === "pending"));
})
.catch(() => {
// Polling is best-effort; the next interval retries automatically.
});
void load();
const stopPolling = setVisibleInterval(load, 5000);
return () => {
alive = false;
stopPolling();
};
}, [taskId]);
// Re-render on a timer so "streaming" liveness recomputes as activity goes stale.
const [, setTick] = React.useState(0);
React.useEffect(() => {
const id = setInterval(() => setTick((t) => t + 1), 1500);
return () => clearInterval(id);
}, []);
// ── first load + single task SSE ────────────────────────────────────────────────
// On task open: load Main's latest page, take the task-level snapshot cursor from
// it, THEN open ONE task SSE at since=snapshot_cursor. History covers id≤cursor and
// the SSE covers id>cursor with no gap. The SSE tails ALL agents; frames are routed
// by session_key. EventSource auto-reconnects and (via our `id:` lines → Last-Event-
// ID) resumes from the DB, so a dropped realtime link self-heals; seq-merge dedups.
React.useEffect(() => {
setStore({});
setIntents([]);
setIntentAssets([]);
setOlderIntents([]);
setFirstIntentsHasMore(false);
setOlderIntentsHasMore(false);
setHasLoadedOlderIntentsPage(false);
setTaskTokens(null);
setSessionTokens({});
setLLMResolutions(null);
setSseLive(false);
setActiveId(MAIN_ID); // a stale worker id from the previous task must not leak in
setMainSegs([{ seq: 0, created_at: "" }]);
setCurrentSeg(0);
snapshotRef.current = 0;
llmToastSeqRef.current = new Set();
reqTokenRef.current = {};
// Reserve the initial main key so the active-session effect (which fires for main on
// mount) won't double-load it and pre-empt the SSE opened here.
loadingKeysRef.current = new Set([mainSessionKey(0)]);
let alive = true;
// MOCK demo: no SSE backend — pull one activity snapshot and bucket by session.
if (MOCK) {
api
.activity(taskId)
.then((r) => {
if (!alive) return;
const buckets: SessionStore = {};
for (const a of r.items) {
const k = sessionKeyOf(a);
if (!buckets[k]) buckets[k] = emptyState();
buckets[k].items.push(a);
}
for (const k of Object.keys(buckets)) {
const st = buckets[k];
st.items.sort((p, q) => p.seq - q.seq);
st.loaded = true;
st.hasMore = false;
st.earliestSeq = st.items.length ? st.items[0].seq : 0;
st.lastTs = st.items.length ? st.items[st.items.length - 1].ts : "";
}
buckets[mainSessionKey(0)] ??= { ...emptyState(), loaded: true };
buckets.system ??= { ...emptyState(), loaded: true };
setStore(buckets);
})
.catch(() =>
setStore({
[mainSessionKey(0)]: { ...emptyState(), loaded: true },
system: { ...emptyState(), loaded: true },
}),
);
return () => {
alive = false;
};
}
const token = (reqTokenRef.current.mainboot ?? 0) + 1;
reqTokenRef.current.mainboot = token;
let bootKey = mainSessionKey(0);
// Resolve the main-agent segments first, then load the CURRENT segment's history and
// open the SSE from its snapshot cursor. The SSE tails all segments and routes each
// frame by session_key (main:<seg>), so switching segments needs no new stream.
api
.mainSessions(taskId)
.then((ms) => {
if (!alive || reqTokenRef.current.mainboot !== token) throw new Error("superseded");
const segs = ms.sessions.length ? ms.sessions : [{ seq: 0, created_at: "" }];
setMainSegs(segs);
setCurrentSeg(ms.current);
bootKey = mainSessionKey(ms.current);
if (bootKey !== mainSessionKey(0)) loadingKeysRef.current.delete(mainSessionKey(0));
loadingKeysRef.current.add(bootKey);
setActiveId(mainSessionId(ms.current));
patchStore(bootKey, (s) => ({ ...s, loading: true }));
return api.activityHistory(taskId, bootKey, 0, PAGE);
})
.then((r) => {
if (!alive || reqTokenRef.current.mainboot !== token) return;
snapshotRef.current = r.snapshotCursor;
patchStore(bootKey, (s) => {
const items = mergeBySeq(r.items, s.items);
return {
...s,
items,
loaded: true,
loading: false,
hasMore: r.hasMore,
earliestSeq: items.length ? items[0].seq : 0,
unread: 0,
};
});
// Open the single task SSE from the snapshot cursor.
const es = new EventSource(
sseUrl(`/api/exploration/activity/stream?task=${encodeURIComponent(taskId)}&since=${snapshotRef.current}`),
);
esRef.current = es;
es.onopen = () => setSseLive(true);
es.onerror = () => setSseLive(false); // EventSource auto-reconnects; DB compensates the gap
es.onmessage = (e) => {
let a: Activity;
try {
a = JSON.parse(e.data) as Activity;
} catch {
return; // ignore malformed frame
}
if ((a.kind === "llm_switch" || a.kind === "llm_failover") && !llmToastSeqRef.current.has(a.seq)) {
llmToastSeqRef.current.add(a.seq);
const transition = a.metadata?.llm_transition;
if (transition?.mode === "exhausted" || a.is_error) {
toast.error(a.summary, { id: `task-${taskId}-llm-${a.seq}` });
} else if (transition?.mode === "automatic") {
toast.success(a.summary, { id: `task-${taskId}-llm-${a.seq}` });
} else {
toast.info(a.summary, { id: `task-${taskId}-llm-${a.seq}` });
}
void api
.taskLLMResolution(taskId)
.then((value) => {
if (alive) setLLMResolutions(value);
})
.catch(() => {
// The periodic resolver poll will retry if this event-triggered refresh fails.
});
}
const k = sessionKeyOf(a);
setStore((prev) => {
const cur = prev[k] ?? emptyState();
const activeK = activeKeyRef.current;
// Merge into sessions that are loaded, actively loading, or the current
// view (so returning is instant + a frame that lands mid-load isn't lost).
// A cold, inactive session also keeps a tiny accounting tail so the
// sidebar can add the latest unfinished usage without loading history.
if (!cur.loaded && !cur.loading && k !== activeK) {
const accountingItems =
a.kind === "usage" || a.kind === "result" ? mergeBySeq(cur.items, [a]).slice(-4) : cur.items;
return {
...prev,
[k]: { ...cur, items: accountingItems, lastTs: a.ts, unread: cur.unread + 1 },
};
}
let items = mergeBySeq(cur.items, [a]);
// Memory bound: trim oldest when over cap (older re-fetched on scroll-up),
// but never while the user is reading this session's history (scrolled up).
let hasMore = cur.hasMore;
let earliestSeq = cur.earliestSeq;
const trimmable = k !== activeK || atBottomRef.current;
if (trimmable && items.length > MAX_KEEP) {
items = items.slice(items.length - MAX_KEEP);
hasMore = true;
earliestSeq = items[0].seq;
}
const unread = k === activeK ? 0 : cur.unread + 1;
return { ...prev, [k]: { ...cur, items, lastTs: a.ts, unread, hasMore, earliestSeq } };
});
};
})
.catch((err) => {
if (!alive || reqTokenRef.current.mainboot !== token) return;
if ((err as Error).message === "superseded") return;
patchStore(bootKey, (s) => ({ ...s, loading: false, error: (err as Error).message || t("Failed to load") }));
})
.finally(() => loadingKeysRef.current.delete(bootKey));
return () => {
alive = false;
esRef.current?.close();
esRef.current = null;
};
}, [taskId, patchStore]);
React.useEffect(() => {
let active = true;
const load = () =>
api
.taskIntentAssets(taskId)
.then((assets) => {
if (active) setIntentAssets(assets);
})
.catch(() => {
// The next poll retries; Worker controls and transcripts remain available.
});
void load();
const stopPolling = setVisibleInterval(load, 5000);
return () => {
active = false;
stopPolling();
};
}, [taskId]);
// ── worker (intent) session list — paged, poll first page lightly ───────────────
React.useEffect(() => {
let active = true;
firstIntentsRef.current = [];
setIntents([]);
setOlderIntents([]);
setFirstIntentsHasMore(false);
setOlderIntentsHasMore(false);
const load = () =>
api
.intentsPage(taskId, 0, 300)
.then((r) => {
if (!active) return;
const freshIds = new Set(r.items.map((item) => item.id));
const oldestFreshId = r.items.reduce((minimum, item) => Math.min(minimum, Number(item.id)), Infinity);
const displaced = r.hasMore
? firstIntentsRef.current.filter((item) => !freshIds.has(item.id) && Number(item.id) < oldestFreshId)
: [];
if (displaced.length > 0) {
setOlderIntents((previous) => {
const byId = new Map(previous.map((item) => [item.id, item]));
for (const item of displaced) byId.set(item.id, item);
return [...byId.values()];
});
}
firstIntentsRef.current = r.items;
setIntents(r.items);
setFirstIntentsHasMore(r.hasMore);
})
.catch(() => {
// Polling is best-effort; the next interval retries automatically.
});
void load();
const stopPolling = setVisibleInterval(load, 5000);
return () => {
active = false;
stopPolling();
};
}, [taskId]);
const loadOlderIntents = React.useCallback(() => {
if (loadingOlderIntents) return;
const all = [...intents, ...olderIntents];
const minId = all.reduce((m, n) => Math.min(m, Number(n.id)), Infinity);
if (!Number.isFinite(minId)) return;
setLoadingOlderIntents(true);
api
.intentsPage(taskId, minId, 300)
.then((r) => {
setOlderIntents((prev) => {
const seen = new Set([...intents, ...prev].map((n) => n.id));
return [...prev, ...r.items.filter((n) => !seen.has(n.id))];
});
setOlderIntentsHasMore(r.hasMore);
setHasLoadedOlderIntentsPage(true);
})
.catch(() => {
// A later manual retry can fetch this page again.
})
.finally(() => setLoadingOlderIntents(false));
}, [taskId, intents, olderIntents, loadingOlderIntents]);
// Combined, de-duplicated worker list (newest first page + older loaded pages).
const allIntents = React.useMemo(() => {
const byId = new Map<string, TaskNode>();
for (const n of olderIntents) byId.set(n.id, n);
for (const n of intents) byId.set(n.id, n); // fresh poll wins over older snapshot
return [...byId.values()].sort((a, b) => Number(b.id) - Number(a.id));
}, [intents, olderIntents]);
const intentAssetsByID = React.useMemo(() => {
const grouped = new Map<string, IntentAsset[]>();
for (const asset of intentAssets) {
const key = String(asset.intent_id);
const current = grouped.get(key);
if (current) current.push(asset);
else grouped.set(key, [asset]);
}
return grouped;
}, [intentAssets]);
const workerSessions = React.useMemo(() => allIntents.map(intentToSession), [allIntents]);
const intentsHasMore = hasLoadedOlderIntentsPage ? olderIntentsHasMore : firstIntentsHasMore;
// For each worker session (intent), derive the display title from the intent
// payload summary. Also store the full TaskNode for the hover-JSON tooltip.
const sessionMeta = React.useMemo(() => {
const map = new Map<string, { title: string; json: unknown; deleted: boolean; deleteReason: string }>();
for (const node of allIntents) {
let title = t("Intent {id}", { id: node.id });
let parsedPayload: unknown = node.payload;
// Soft delete: the intent's state='deleted'; the deletion reason lives on the separate delete_reason field.
const deleted = node.state === "deleted";
const deleteReason = node.delete_reason ?? "";
if (node.payload) {
try {
const p = JSON.parse(node.payload);
parsedPayload = p;
if (p?.summary) title = String(p.summary);
} catch {
title = node.payload.trim() || title;
}
}
const json = { ...node, payload: parsedPayload };
map.set(node.id, { title, json, deleted, deleteReason });
}
return map;
}, [allIntents]);
// Returns true if any pending intercept belongs to this session.
// Worker agent_name format: "work#N · #intentID". Main/planner match by role key.
const hasPendingForSession = React.useCallback(
(s: Session): boolean => {
if (s.inherited) return false;
if (!pendingIntercepts.length) return false;
if (s.role === "mainagent") return pendingIntercepts.some((r) => r.agent_name === "mainagent");
if (s.role === "planner") return pendingIntercepts.some((r) => r.agent_name === "planner");
// Worker: extract intent ID from "work#N · #<intentID>"
return pendingIntercepts.some((r) => {
const m = r.agent_name.match(/·\s*#(\d+)$/);
return m ? m[1] === s.intent_id : false;
});
},
[pendingIntercepts],
);
// Liveness from each session's last-seen activity time (kept fresh by the 1.5s tick).
const recentLive = React.useCallback(
(key: string) => {
const ts = store[key]?.lastTs;
if (!ts) return false;
const t = Date.parse(ts);
return t > 0 && Date.now() - t < STREAM_WINDOW_MS;
},
[store],
);
const currentMainKey = mainSessionKey(currentSeg);
const plannerLive = recentLive("plan");
// Which main segment is streaming RIGHT NOW. A main turn is serialized per task, so at
// most one segment is live. Gate on the real running flag (sending / mainChatRunning)
// rather than "recent activity", and drop it the moment the turn's terminal record
// (kind='result', or an error) lands — otherwise the badge lingers for STREAM_WINDOW_MS
// after the agent already finished. null = nothing running.
const liveMainSeg = React.useMemo<number | null>(() => {
if (!(sending || mainChatRunning)) return null;
// the streaming segment is the one with the freshest activity (incl. the just-sent turn)
let seg = currentSeg;
let bestTs = -1;
for (const m of mainSegs) {
const raw = store[mainSessionKey(m.seq)]?.lastTs;
const ts = raw ? Date.parse(raw) : -1;
if (ts > bestTs) {
bestTs = ts;
seg = m.seq;
}
}
const items = store[mainSessionKey(seg)]?.items ?? [];
const last = items[items.length - 1];
if (last && (last.kind === "result" || (last.kind === "text" && last.is_error))) return null;
return seg;
}, [sending, mainChatRunning, mainSegs, store, currentSeg]);
// Every main-agent segment is an independent, interactive session (like the top-level
// chat conversations) — you can talk in any of them, newest-first.
const mainSessions = React.useMemo<Session[]>(
() =>
mainSegs.map((m) => ({
id: mainSessionId(m.seq),
role: "mainagent",
title: mainSessionTitle(m.seq),
status: "running",
live: m.seq === liveMainSeg,
last_activity: m.created_at,
seg: m.seq,
})),
[mainSegs, liveMainSeg],
);
const sessions = React.useMemo(() => {
const items = [...mainSessions, { ...PLANNER_SESSION, live: plannerLive }, ...workerSessions, SYSTEM_SESSION];
const located = focusSession ?? retainedSession;
if (located && !items.some((s) => s.id === located.id)) items.push(located);
return items;
}, [mainSessions, workerSessions, plannerLive, focusSession, retainedSession]);
const grouped = {
mainagent: sessions.filter((s) => s.role === "mainagent"),
planner: sessions.filter((s) => s.role === "planner"),
worker: sessions.filter((s) => s.role === "worker"),
system: sessions.filter((s) => s.role === "system"),
};
const active = sessions.find((s) => s.id === activeId) ?? MAIN_SESSION;
const side = useSideQuestions(
active.role === "mainagent"
? `/api/tasks/${taskId}/chat`
: active.role === "worker" && !active.inherited && active.intent_id
? `/api/tasks/${taskId}/intents/${active.intent_id}`
: null,
);
const isMain = active.role === "mainagent";
const isPlanner = active.role === "planner";
const isSystem = active.role === "system";
const activeKey = keyForSession(active);
const activeState = store[activeKey];
// A main turn is serialized per task (chat lock), so "busy" is task-wide: while any
// main segment is mid-turn the active composer is disabled. Clear it the moment the
// active session's terminal record (kind='result', or an error) lands, so the input
// re-enables immediately instead of waiting for the next chat-status poll.
const activeItems = activeState?.items ?? [];
const activeLast = activeItems[activeItems.length - 1];
const activeSettled =
!!activeLast && (activeLast.kind === "result" || (activeLast.kind === "text" && activeLast.is_error));
const mainBusy = isMain && (sending || (!activeSettled && (mainChatRunning ?? recentLive(activeKey))));
// In the collapsed state (mobile) the title bar replaces the whole list: it shows the current session name + the total unread of other sessions,
// otherwise once collapsed you neither know which session you are viewing nor see new messages elsewhere.
const activeDisplayTitle = (active.role === "worker" ? sessionMeta.get(active.id)?.title : "") || active.title;
const hiddenUnread = React.useMemo(
() => Object.entries(store).reduce((sum, [key, s]) => (key === activeKey ? sum : sum + s.unread), 0),
[store, activeKey],
);
// Keep the SSE dispatcher's notion of the active session current, and lazily load
// + clear unread whenever the active session changes.
// biome-ignore lint/correctness/useExhaustiveDependencies: cache updates must not reactivate the current session.
React.useEffect(() => {
activeKeyRef.current = activeKey;
const st = store[activeKey];
if (!st || (!st.loaded && !st.loading)) {
loadSession(activeKey);
} else if (st.unread) {
patchStore(activeKey, (s) => ({ ...s, unread: 0 }));
}
}, [activeKey]);
const focusKey = approvalFocus.state?.source?.session;
const loadFocusPage = React.useCallback(
(before: number) => api.activityHistory(taskId, focusKey ?? "main", before, PAGE),
[taskId, focusKey],
);
const mergeFocusPage = React.useCallback(
(page: { items: Activity[]; hasMore: boolean }) => {
if (!focusKey) return;
patchStore(focusKey, (s) => {
const items = mergeBySeq(page.items, s.items);
return { ...s, items, hasMore: page.hasMore, earliestSeq: items[0]?.seq ?? s.earliestSeq };
});
},
[focusKey, patchStore],
);
const focusHistory = useApprovalHistory(
approvalFocus.state?.source,
!!focusKey && !!store[focusKey]?.loaded,
focusKey ? (store[focusKey]?.items ?? []) : [],
loadFocusPage,
mergeFocusPage,
);
React.useEffect(() => {
if (approvalFocus.state) atBottomRef.current = false;
}, [approvalFocus.state]);
// Main agent is the human↔orchestrator CONSOLE: only the conversation (user msgs +
// the main agent's own replies/steps). Planner session shows planner steps; a
// worker session shows only its intent's activity, led by the intent objective.
const activity = React.useMemo(() => {
const items = activeState?.items ?? [];
// The main-agent console renders purely from server data: the human turn is
// persisted+broadcast by the backend BEFORE it returns, so it arrives over the
// same SSE stream (worker="mainagent") as every agent step — no client-side
// optimistic echo, hence no fabricated seq that could collide with real DB ids.
if (isMain) return items;
if (isPlanner || isSystem) return items;
// Worker session: the intent leads the transcript as a right-aligned "user"-style
// message (the task handed to this worker), followed by its execution steps.
const intentTitle = sessionMeta.get(active.id)?.title ?? active.title;
const intentMsg: Activity = {
seq: -1, // sorts/leads before any real step (real seq ≥ 0)
worker: items[0]?.worker ?? active.id, // reuse the lane so no worker chips appear
ts: active.last_activity || "",
kind: "intent", // LLM-generated objective — rendered as a distinct (non-human) bubble
summary: intentTitle,
source_task_id: active.source_task_id,
inherited: active.inherited,
};
return [intentMsg, ...items];
}, [
isMain,
isPlanner,
isSystem,
activeState,
active.id,
active.title,
active.last_activity,
active.source_task_id,
active.inherited,
sessionMeta,
]);
// seq of this session's most-recent TodoWrite call — for the Todo popover.
const latestTodoSeq = React.useMemo(() => {
for (let i = activity.length - 1; i >= 0; i--) {
const a = activity[i];
if (a.kind === "tool_use" && a.tool === "TodoWrite") return a.seq;
}
return null;
}, [activity]);
// Persisted result totals come from the backend's full activity history. Only
// the latest unfinished usage frame is added client-side, so partial history
// pages cannot undercount completed runs or double-count the active one.
const tokenForSession = React.useCallback(
(key: string): TokenTotal => {
const saved = sessionTokens[key];
const total: TokenTotal = {
input_tokens: saved?.input_tokens ?? 0,
output_tokens: saved?.output_tokens ?? 0,
cache_read_tokens: saved?.cache_read_tokens ?? 0,
cache_write_tokens: saved?.cache_write_tokens ?? 0,
};
const items = store[key]?.items ?? [];
for (let index = items.length - 1; index >= 0; index--) {
const item = items[index];
if (item.kind === "result") break;
if (item.kind !== "usage") continue;
total.input_tokens += item.input_tokens ?? 0;
total.output_tokens += item.output_tokens ?? 0;
total.cache_read_tokens += item.cache_read_tokens ?? 0;
total.cache_write_tokens += item.cache_write_tokens ?? 0;
break;
}
return total;
},
[sessionTokens, store],
);
const activeTokens = tokenForSession(activeKey);
const tokenTotal = {
i: activeTokens.input_tokens,
o: activeTokens.output_tokens,
cr: activeTokens.cache_read_tokens,
any:
activeTokens.input_tokens +
activeTokens.output_tokens +
activeTokens.cache_read_tokens +
activeTokens.cache_write_tokens >
0,
};
// Run duration = span from this session's first step to its last (for a live
// session, "now" so it ticks up — the 1.5s setTick above re-renders it).
// While the TASK is paused nothing time-derived may advance (#84): sessions
// keep state='running' in the store during a pause, so live alone would keep
// ticking and misrepresent the frozen budget. Freeze at the last step instead.
const runDuration = React.useMemo(() => {
let min = Infinity,
max = 0;
for (const a of activity) {
const t = Date.parse(a.ts);
if (!Number.isFinite(t)) continue;
if (t < min) min = t;
if (t > max) max = t;
}
if (!Number.isFinite(min) || max === 0) return null;
const end = active.live && !taskPaused ? Date.now() : max;
return Math.max(0, end - min);
}, [activity, active.live, taskPaused]);
// ---- transcript auto-scroll (open → bottom; stick to bottom unless scrolled up) ----
const contentRef = React.useRef<HTMLDivElement | null>(null);
const viewport = React.useCallback(
() => (contentRef.current?.closest('[data-slot="scroll-area-viewport"]') as HTMLElement | null) ?? null,
[],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: activeId intentionally rebinds the scroll listener.
React.useEffect(() => {
const vp = viewport();
if (!vp) return;
const onScroll = () => {
if (approvalFocus.state && !focusHistory.ready) return;
atBottomRef.current = vp.scrollTop + vp.clientHeight >= vp.scrollHeight - 60;
if (vp.scrollTop <= 80) loadEarlier(activeKeyRef.current, viewport); // near top → older page
};
vp.addEventListener("scroll", onScroll, { passive: true });
return () => vp.removeEventListener("scroll", onScroll);
}, [viewport, activeId, loadEarlier, approvalFocus.state, focusHistory.ready]);
// open/switch a session → jump to the latest (bottom)
// biome-ignore lint/correctness/useExhaustiveDependencies: activeId intentionally scrolls a newly selected session.
React.useLayoutEffect(() => {
const vp = viewport();
if (vp && !approvalFocus.state) {
vp.scrollTop = vp.scrollHeight;
atBottomRef.current = true;
}
}, [activeId, viewport, approvalFocus.state]);
// new activity → stick to bottom only if the user is already pinned there
// biome-ignore lint/correctness/useExhaustiveDependencies: activity growth intentionally drives live-edge scrolling.
React.useLayoutEffect(() => {
if (approvalFocus.state || !atBottomRef.current) return;
const vp = viewport();
if (vp) vp.scrollTop = vp.scrollHeight;
}, [activity, viewport, approvalFocus.state]);
// Lazy detail loads (AnswerBlock / ToolBlock / Markdown) grow the content AFTER the
// activity array settles, WITHOUT changing its reference — so the layout effects
// above never re-fire and a freshly opened session would leave its last message
// scrolled partly off-screen (the final answer expands from a one-line summary to
// full markdown below the fold). A ResizeObserver re-pins to the bottom on any
// height change while the user is still at the bottom, so opening the main agent
// lands on the last message fully shown. contentRef's div is always mounted, so the
// observer catches the transcript mounting + each detail expanding.
// biome-ignore lint/correctness/useExhaustiveDependencies: activeId intentionally rebinds the observer to the new session's content.
React.useEffect(() => {
const el = contentRef.current;
if (!el || typeof ResizeObserver === "undefined") return;
const ro = new ResizeObserver(() => {
if (approvalFocus.state || !atBottomRef.current) return;
const vp = viewport();
if (vp) vp.scrollTop = vp.scrollHeight;
});
ro.observe(el);
return () => ro.disconnect();
}, [activeId, viewport, approvalFocus.state]);
function stop() {
if (stopping) return;
setStopping(true);
void api.stopChat(taskId).finally(() => setStopping(false));
}
function send() {
const text = input.trim();
const atts = attachments;
if (side.handleCommand(text, () => setInput(""))) return;
if ((!text && atts.length === 0) || sending || mainBusy) return;
// No optimistic echo: the backend persists+broadcasts the human turn before it
// returns, so it streams back over SSE (worker="mainagent") with its real DB
// seq — the transcript renders it from server data like every other step. Clear
// the composer eagerly for responsiveness; restore it if the send fails.
setInput("");
setAttachments([]);
setSending(true);
chatStatusRequestRef.current++;
api
.chat(text, taskId, atts.length > 0 ? atts : undefined, active.seg ?? 0)
.then(({ mode }) => {
chatStatusRequestRef.current++;
setMainChatRunning(mode === "llm");
})
.catch((e) => {
setInput(text); // restore so the user doesn't lose their text / attachments
setAttachments(atts);
toast.error(t("Send failed: {error}", { error: (e as Error).message || t("please retry later") }));
})
.finally(() => setSending(false));
}
function sendWorkerChat() {
const intentId = active.intent_id;
const message = workerMessage.trim();
if (side.handleCommand(message, () => setWorkerMessage(""))) return;
if (!intentId || active.inherited || active.status !== "paused" || workerMessageSending || !message) return;
if (workerMessageCharCount(message) > MAX_WORKER_MESSAGE_CHARS) {
toast.error(t("Messages cannot exceed {count} characters", { count: MAX_WORKER_MESSAGE_CHARS }));
return;
}
const requestId = workerMessageRequestId || newWorkerMessageRequestID();
if (!workerMessageRequestId) setWorkerMessageRequestId(requestId);
setWorkerMessageSending(true);
api
.sendWorkerMessage(taskId, intentId, message, requestId)
.then((result) => {
// The server records the user turn and streams the run over SSE, so there is
// no optimistic insert: the message and the continuation arrive live.
patchIntentState(intentId, result.state);
setWorkerMessage("");
setWorkerMessageRequestId("");
toast.success(t("Message sent to Worker #{id}; execution resumed immediately", { id: intentId }));
})
.catch((error) => {
toast.error(t("Send failed: {error}", { error: (error as Error).message || t("please retry later") }));
})
.finally(() => setWorkerMessageSending(false));
}
const mainLoaded = !!store[currentMainKey]?.loaded;
// The send key is decided by system settings (localStorage); Enter sends by default.
const sendMode = useChatSendMode();
// What the transcript pane should show for the active session.
const showLoader = !activeState || (activeState.loading && !activeState.loaded);
const resolutionForSession = (session: Session): TaskLLMResolution | undefined => {
if (!llmResolutions || session.inherited || session.role === "system") return undefined;
if (session.role === "mainagent") return llmResolutions.mainagent;
if (session.role === "planner") return llmResolutions.planner;
return llmResolutions.worker;
};
const activeResolution = resolutionForSession(active);
const activeAssets =
active.role === "worker" && active.intent_id ? intentAssetsByID.get(active.intent_id) : undefined;
return (
<TooltipProvider delayDuration={300}>
{/* Height reserve: page header (title row + goal + Tabs ≈ 7.5rem) + content padding. Mobile p-4,
desktop lg:p-6, and desktop also needs scroll headroom, so the two tiers reserve 10rem / 13rem respectively — mobile
reusing 13rem would waste 3rem of transcript height. */}
<div
className={cn(
"grid h-[calc(100svh-10rem)] min-h-0 grid-cols-1 gap-4",
listOpen ? "grid-rows-[minmax(0,40svh)_minmax(0,1fr)]" : "grid-rows-[auto_minmax(0,1fr)]",
"lg:h-[calc(100svh-13rem)] lg:grid-cols-[18rem_1fr] lg:grid-rows-[minmax(0,1fr)]",
)}
>
{/* Left: session list */}
<div className="flex min-h-0 flex-col overflow-hidden rounded-lg border bg-card">
<div className="border-b px-3 py-2">
<div className="flex items-center justify-between gap-2">
<button
type="button"
onClick={() => setListOpen((open) => !open)}
aria-expanded={listOpen}
aria-controls="session-list-panel"
className="flex min-w-0 flex-1 items-center gap-1 text-left text-xs font-medium text-muted-foreground lg:pointer-events-none"
>
<ChevronDownIcon
className={cn("size-3.5 shrink-0 transition-transform lg:hidden", !listOpen && "-rotate-90")}
/>
<span className="shrink-0">{t("Sessions")}</span>
{!listOpen && (
<>
<span className="min-w-0 truncate text-foreground lg:hidden" title={activeDisplayTitle}>
· {activeDisplayTitle}
</span>
{hiddenUnread > 0 && (
<span className="inline-flex min-w-4 shrink-0 items-center justify-center rounded-full bg-blue-500/15 px-1 text-[10px] font-medium tabular-nums text-blue-600 lg:hidden dark:text-blue-400">
{hiddenUnread > 99 ? "99+" : hiddenUnread}
</span>
)}
</>
)}
</button>
{!MOCK && (
<span
className={cn(
"inline-flex items-center gap-1 text-[10px]",
sseLive ? "text-emerald-500" : "text-amber-500",
)}
title={
sseLive
? t("Live connection healthy")
: t("Live connection lost, reconnecting automatically (history still visible)")
}
>
{sseLive ? (
<>
<span className="size-1 animate-pulse rounded-full bg-emerald-500" />
{t("Live")}
</>
) : (
<>
<WifiOffIcon className="size-3" />
{t("Reconnecting")}
</>
)}
</span>
)}
</div>
{taskTokens && (
<Tooltip>
<TooltipTrigger asChild>
<div className="mt-2 flex min-w-0 flex-wrap items-center gap-1 text-xs text-muted-foreground">
<span>{t("Task total")}</span>
<span>·</span>
<TokenMetrics
input={taskTokens.input_tokens}
cache={taskTokens.cache_read_tokens}
output={taskTokens.output_tokens}
/>
</div>
</TooltipTrigger>
<TooltipContent>
{t("Input {input} · Output {output} · Cache read {cacheRead} · Cache write {cacheWrite}", {
input: taskTokens.input_tokens.toLocaleString(intlLocale),
output: taskTokens.output_tokens.toLocaleString(intlLocale),
cacheRead: taskTokens.cache_read_tokens.toLocaleString(intlLocale),
cacheWrite: taskTokens.cache_write_tokens.toLocaleString(intlLocale),
})}
</TooltipContent>
</Tooltip>
)}
</div>
<ScrollArea
id="session-list-panel"
type="auto"
className={cn(
"min-h-0 flex-1 [&_[data-slot=scroll-area-viewport]>div]:block!",
!listOpen && "max-lg:hidden",
)}
>
<div className="flex w-full flex-col gap-3 p-2">
{(["mainagent", "planner", "system", "worker"] as const).map((role) => {
const items = grouped[role];
if (!items.length) return null;
const Meta = roleMeta[role];
return (
<div key={role} className="flex flex-col gap-0.5">
<div className="flex items-center gap-1.5 px-2 py-1 text-xs font-medium text-muted-foreground">
<Meta.icon className="size-3.5" />
{Meta.label}
{role === "mainagent" && (
<button
type="button"
onClick={() => setConfirmNewMain(true)}
disabled={creatingMain}
title={t("New main agent session (clears context, task state preserved)")}
aria-label={t("New main agent session")}
className="ml-auto flex items-center gap-1 rounded px-1.5 py-0.5 text-[11px] text-muted-foreground hover:bg-accent/60 hover:text-foreground disabled:opacity-50"
>
{creatingMain ? (
<Loader2Icon className="size-3.5 animate-spin" />
) : (
<PlusIcon className="size-3.5" />
)}
{t("New")}
</button>
)}
</div>
{items.map((s) => {
const meta = s.role === "worker" ? sessionMeta.get(s.id) : undefined;
return (
<SessionItem
key={s.id}
s={s}
active={s.id === activeId}
displayTitle={meta?.title ?? s.title}
hasPending={hasPendingForSession(s)}
unread={store[keyForSession(s)]?.unread}
onClick={() => {
approvalFocus.close();
setActiveId(s.id);
setListOpen(false); // on mobile, collapse after picking to give the height back to the transcript
setWorkerMessage("");
setWorkerMessageRequestId("");
}}
controlling={controllingIntent === s.intent_id}
deleted={meta?.deleted}
onCancel={() => {
setCancelIntent(s);
}}
/>
);
})}
{role === "worker" && intentsHasMore && (
<button
type="button"
onClick={loadOlderIntents}
disabled={loadingOlderIntents}
className="mt-0.5 flex items-center justify-center gap-1 rounded-md px-2 py-1 text-xs text-muted-foreground hover:bg-accent/50"
>
{loadingOlderIntents ? (
<Loader2Icon className="size-3.5 animate-spin" />
) : (
<RotateCwIcon className="size-3.5" />
)}
{t("Load earlier workers")}
</button>
)}
</div>
);
})}
{mainLoaded && !workerSessions.length && (
<div className="px-2 py-1 text-xs text-muted-foreground">{t("No running worker sessions.")}</div>
)}
</div>
</ScrollArea>
</div>
{/* Right: transcript */}
<SideQuestionWorkspace
side={side}
label={
active.role === "worker"
? `${t("Worker #{id}", { id: active.intent_id ?? "" })} · ${activeDisplayTitle}`
: activeDisplayTitle
}
>
<div className="flex min-h-0 flex-1 min-w-0 flex-col overflow-hidden rounded-lg border bg-card">
<div className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1 border-b px-3 py-2 sm:px-4 sm:py-2.5">
{(() => {
const isWorker = active.role === "worker";
const meta = isWorker ? sessionMeta.get(active.id) : undefined;
// Worker: the intent moved into the transcript as a message, so the
// header shows a stable generic label (intent JSON stays on hover).
const title = isWorker ? t("Worker execution session") : active.title;
const titleEl = <span className="min-w-0 truncate text-sm font-medium">{title}</span>;
return meta?.json ? (
<Tooltip>
<TooltipTrigger asChild>{titleEl}</TooltipTrigger>
<TooltipContent side="bottom" align="start" className="max-h-80 max-w-sm overflow-auto p-0">
<pre className="p-2 text-[10px] leading-relaxed">{JSON.stringify(meta.json, null, 2)}</pre>
</TooltipContent>
</Tooltip>
) : (
titleEl
);
})()}
<SideQuestionButton side={side} />
{activeResolution && (
<Tooltip>
<TooltipTrigger asChild>
<Badge
variant="outline"
className="max-w-28 shrink-0 font-normal"
aria-label={
activeResolution.available
? t("Current config: {name}", { name: resolutionLabel(activeResolution) })
: t("Model unavailable")
}
>
<span className="truncate">
{activeResolution.available ? resolutionLabel(activeResolution) : t("Model unavailable")}
</span>
</Badge>
</TooltipTrigger>
<TooltipContent side="bottom" className="max-w-xs [overflow-wrap:anywhere]">
{activeResolution.available
? [resolutionLabel(activeResolution), activeResolution.model].filter(Boolean).join(" / ")
: activeResolution.reason || t("No available LLM config")}
</TooltipContent>
</Tooltip>
)}
{activeAssets && activeAssets.length > 0 && <WorkerAssetBadge assets={activeAssets} />}
{active.inherited && active.source_task_id && (
<Badge variant="outline">
{t("Source task #{id} · read-only history", { id: active.source_task_id })}
</Badge>
)}
{active.live && (
<span className="inline-flex items-center gap-1 rounded bg-blue-500/15 px-1.5 py-0.5 text-[10px] font-medium text-blue-600 dark:text-blue-400">
<span className="size-1 animate-pulse rounded-full bg-blue-500" />
{t("Live")}
</span>
)}
{activeState?.hasMore && (
<span className="text-[10px] text-muted-foreground" title={t("Scroll up to load earlier history")}>
↑ {t("Earlier history")}
</span>
)}
<div className="ml-auto flex min-w-0 max-w-full items-center justify-end gap-x-3 gap-y-1 text-xs text-muted-foreground max-sm:w-full max-sm:flex-wrap">
{tokenTotal.any && (
<Tooltip>
{/* Mobile uses short labels (In/Cache/Out): long labels wrap this row onto two lines, further squeezing the transcript. */}
<TooltipTrigger asChild>
<span className="inline-flex min-w-0 items-center">
<TokenMetrics
input={tokenTotal.i}
cache={tokenTotal.cr}
output={tokenTotal.o}
labels="short"
className="sm:hidden"
/>
<TokenMetrics
input={tokenTotal.i}
cache={tokenTotal.cr}
output={tokenTotal.o}
labels="long"
className="max-sm:hidden"
/>
</span>
</TooltipTrigger>
<TooltipContent>
{t("Input {input} · Output {output} · Cache read {cacheRead} · Cache write {cacheWrite}", {
input: activeTokens.input_tokens.toLocaleString(intlLocale),
output: activeTokens.output_tokens.toLocaleString(intlLocale),
cacheRead: activeTokens.cache_read_tokens.toLocaleString(intlLocale),
cacheWrite: activeTokens.cache_write_tokens.toLocaleString(intlLocale),
})}
</TooltipContent>
</Tooltip>
)}
{runDuration != null && (
<span className="inline-flex items-center gap-1" title={t("Run duration (first step → last step)")}>
<ClockIcon className="size-3" />
{fmtDuration(runDuration)}
</span>
)}
{isMain && <span>{t("Interactive")}</span>}
</div>
</div>
{(() => {
const dm = active.role === "worker" ? sessionMeta.get(active.id) : undefined;
if (!dm?.deleted) return null;
return (
<div className="flex items-start gap-2 border-b border-destructive/30 bg-destructive/5 px-4 py-2.5 text-xs">
<Trash2Icon className="mt-0.5 size-3.5 shrink-0 text-destructive" />
<div className="min-w-0">
<span className="font-medium text-destructive">{t("This intent was deleted by the user")}</span>
<span className="text-muted-foreground">
{t(
"(execution stopped and the planner notified; the intent and its output are kept — view the history below)",
)}
</span>
{dm.deleteReason && (
<p className="mt-1 break-words text-foreground">
<span className="text-muted-foreground">{t("Deletion reason:")} </span>
{dm.deleteReason}
</p>
)}
</div>
</div>
);
})()}
<ApprovalExecutionFocus
focus={{
...approvalFocus,
close: () => {
setActiveId(activeId);
approvalFocus.close();
},
}}
history={focusHistory}
/>
{/* Force Radix's internal viewport wrapper (display:table, sizes to content)
to block so wide/unbreakable steps (long commands, code, URLs) can't blow
out the width and defeat the truncation below — the transcript wraps to
the panel instead of overflowing horizontally. */}
<ScrollArea type="auto" className="min-h-0 min-w-0 flex-1 [&_[data-slot=scroll-area-viewport]>div]:block!">
<div className="min-w-0 max-w-full p-4" ref={contentRef}>
{activeState?.loadingMore && (
<div className="flex items-center justify-center gap-2 pb-2 text-xs text-muted-foreground">
<Loader2Icon className="size-3.5 animate-spin" />
{t("Loading earlier history…")}
</div>
)}
{showLoader ? (
<div className="flex items-center gap-2 pl-9 text-xs text-muted-foreground">
<Loader2Icon className="size-3.5 animate-spin" />
{t("Loading activity stream…")}
</div>
) : activeState?.error ? (
<div className="flex items-center gap-2 pl-9 text-xs text-red-500">
<CircleXIcon className="size-3.5" />
{t("Failed to load: {error}", { error: activeState.error })}
<Button
size="sm"
variant="ghost"
className="h-6 px-2 text-xs"
onClick={() => loadSession(activeKey)}
>
{t("Retry")}
</Button>
</div>
) : activity.length ? (
<Transcript
activity={activity}
live={active.live}
taskId={taskId}
chat={isMain}
focusedSeq={focusHistory.ready ? approvalFocus.state?.source?.seq : undefined}
/>
) : (
<div className="pl-9 text-xs text-muted-foreground">
{isMain
? t("No conversation yet. Message the main agent below to steer exploration or step into the flow.")
: t("No activity records.")}
</div>
)}
</div>
</ScrollArea>
{isMain ? (
<div className="border-t p-3">
{attachments.length > 0 && (
<div className="mb-2 flex flex-wrap gap-1.5">
{attachments.map((a) => (
<div
key={a.path}
className="flex items-center gap-1.5 rounded-md border bg-muted/50 px-2 py-1 text-xs"
title={a.path}
>
<PaperclipIcon className="size-3 shrink-0 text-primary" />
<span className="max-w-[160px] truncate">{a.name}</span>
<span className="text-muted-foreground">{fmtBytes(a.size)}</span>
<button
type="button"
className="ml-0.5 text-muted-foreground hover:text-foreground"
onClick={() => setAttachments((p) => p.filter((x) => x.path !== a.path))}
title={t("Remove")}
>
<XIcon className="size-3" />
</button>
</div>
))}
</div>
)}
<input
ref={fileInputRef}
type="file"
multiple
className="hidden"
onChange={(e) => void pickFiles(e.target.files)}
/>
<InputGroup className="min-h-9 has-disabled:opacity-100">
<MentionTextarea
inputGroup
rows={1}
aria-label={t("Message the main agent")}
placeholder={
mainBusy
? t("The main agent is running; type /btw to ask a side question…")
: t("Message the main agent; @ to reference findings, assets, and more…")
}
value={input}
disabled={sending}
onValueChange={setInput}
onKeyDown={(e) => {
if (!shouldSubmitOnKey(e, sendMode)) return;
e.preventDefault();
send();
}}
className="max-h-36 min-h-9 overflow-y-auto"
/>
<InputGroupAddon align="block-end">
<InputGroupButton
size="icon-xs"
variant="ghost"
onClick={() => fileInputRef.current?.click()}
disabled={mainBusy || uploading}
title={t("Upload file")}
aria-label={t("Upload file")}
>
{uploading ? <Loader2Icon className="animate-spin" /> : <PaperclipIcon />}
</InputGroupButton>
{mainBusy && isBtwCommand(input) && (
<InputGroupButton size="icon-xs" onClick={send} aria-label={t("Send side question")}>
<ArrowUpIcon />
</InputGroupButton>
)}
{mainBusy ? (
<InputGroupButton
className="ml-auto"
size="icon-xs"
variant="destructive"
onClick={stop}
disabled={stopping}
title={t("Stop current execution")}
aria-label={t("Stop current execution")}
>
{stopping ? <Loader2Icon className="animate-spin" /> : <SquareIcon />}
</InputGroupButton>
) : (
<InputGroupButton
className="ml-auto"
size="icon-xs"
variant="default"
onClick={send}
disabled={(!input.trim() && attachments.length === 0) || sending}
title={t("Send message")}
aria-label={t("Send message")}
>
{sending ? <Loader2Icon className="animate-spin" /> : <ArrowUpIcon />}
</InputGroupButton>
)}
</InputGroupAddon>
</InputGroup>
</div>
) : active.role === "worker" &&
!active.inherited &&
(active.status === "running" || active.status === "paused") ? (
<div className="border-t p-3">
<InputGroup className="min-h-9 has-disabled:opacity-100">
<MentionTextarea
inputGroup
rows={1}
aria-label={t("Message Worker #{id}", { id: active.intent_id ?? "" })}
placeholder={t("Message Worker #{id}; @ to reference records and adjust execution direction…", {
id: active.intent_id ?? "",
})}
value={workerMessage}
onValueChange={(value) => {
setWorkerMessage(value);
setWorkerMessageRequestId("");
}}
onKeyDown={(event) => {
if (!shouldSubmitOnKey(event, sendMode)) return;
event.preventDefault();
sendWorkerChat();
}}
disabled={workerMessageSending}
aria-invalid={workerMessageCharCount(workerMessage) > MAX_WORKER_MESSAGE_CHARS}
className="max-h-36 min-h-9 overflow-y-auto"
/>
<InputGroupAddon align="block-end">
<span
className={cn(
"px-1 text-[10px] tabular-nums text-muted-foreground",
workerMessageCharCount(workerMessage) > MAX_WORKER_MESSAGE_CHARS && "text-destructive",
)}
>
{workerMessageCharCount(workerMessage)}/{MAX_WORKER_MESSAGE_CHARS}
</span>
{active.status === "running" && isBtwCommand(workerMessage) && (
<InputGroupButton size="icon-xs" onClick={sendWorkerChat} aria-label={t("Send side question")}>
<ArrowUpIcon />
</InputGroupButton>
)}
{active.status === "running" ? (
<InputGroupButton
className="ml-auto"
size="icon-xs"
variant="destructive"
onClick={() => void controlWorker(active, "pause")}
disabled={controllingIntent === active.intent_id}
title={t("Pause current worker")}
aria-label={t("Pause current worker")}
>
{controllingIntent === active.intent_id ? (
<Loader2Icon className="animate-spin" />
) : (
<SquareIcon />
)}
</InputGroupButton>
) : (
<>
<InputGroupButton
className="ml-auto"
size="xs"
variant="ghost"
onClick={() => void controlWorker(active, "resume")}
disabled={controllingIntent === active.intent_id || workerMessageSending}
title={t("Continue execution directly without sending a message")}
aria-label={t("Continue execution directly")}
>
{controllingIntent === active.intent_id ? (
<Loader2Icon className="animate-spin" />
) : (
t("Continue directly")
)}
</InputGroupButton>
<InputGroupButton
size="icon-xs"
variant="default"
onClick={sendWorkerChat}
disabled={
workerMessageSending ||
!workerMessage.trim() ||
workerMessageCharCount(workerMessage) > MAX_WORKER_MESSAGE_CHARS
}
title={t("Send message")}
aria-label={t("Send message")}
>
{workerMessageSending ? <Spinner /> : <ArrowUpIcon />}
</InputGroupButton>
</>
)}
</InputGroupAddon>
</InputGroup>
</div>
) : (
<div className="flex items-center border-t px-4 py-2">
<TodoPopover
seq={latestTodoSeq}
fetchDetail={(seq) => api.activityDetail(seq, taskId).then((r) => r.detail ?? "")}
/>
</div>
)}
</div>
</SideQuestionWorkspace>
<AlertDialog
open={cancelIntent !== null}
onOpenChange={(open) => {
if (!open) {
setCancelIntent(null);
setCancelReason("");
setDeleteMode("soft");
}
}}
>
<AlertDialogContent className="max-w-[min(32rem,calc(100vw-2rem))]">
<AlertDialogHeader>
<AlertDialogTitle>{t("Delete Worker #{id}?", { id: cancelIntent?.intent_id ?? "" })}</AlertDialogTitle>
<AlertDialogDescription className="break-words whitespace-normal">
{deleteMode === "hard" ? (
boldMarkup(
t(
"**Hard delete** physically removes this intent and the downstream nodes **supported solely by it** (cascading to the leaves to avoid orphaned data); shared nodes, goals, and the task's root facts are kept. **This action is irreversible.** The planner is notified of the deletion and re-plans accordingly.",
),
)
) : (
boldMarkup(
t(
'**Soft delete** marks the intent as "deleted" and records the reason; the intent node, execution records, registered facts, and findings **are all kept**. The planner receives "intent deleted by user + reason" and re-plans accordingly.',
),
)
)}
</AlertDialogDescription>
</AlertDialogHeader>
<div className="grid gap-3 py-1">
<div className="grid grid-cols-2 gap-2">
<button
type="button"
onClick={() => setDeleteMode("soft")}
className={cn(
"rounded-md border px-3 py-2 text-left text-sm transition-colors",
deleteMode === "soft" ? "border-primary bg-primary/5" : "hover:bg-accent",
)}
>
<div className="font-medium">{t("Soft delete")}</div>
<div className="text-xs text-muted-foreground">{t("Data kept, traceable")}</div>
</button>
<button
type="button"
onClick={() => setDeleteMode("hard")}
className={cn(
"rounded-md border px-3 py-2 text-left text-sm transition-colors",
deleteMode === "hard" ? "border-destructive bg-destructive/5" : "hover:bg-accent",
)}
>
<div className="font-medium">{t("Hard delete")}</div>
<div className="text-xs text-muted-foreground">{t("Cascade removal, irreversible")}</div>
</button>
</div>
<div className="grid gap-2">
<label htmlFor="cancel-reason" className="text-sm font-medium">
{t("Deletion reason (required)")}
</label>
<Textarea
id="cancel-reason"
value={cancelReason}
onChange={(e) => setCancelReason(e.target.value)}
placeholder={t(
"Explain why this intent is being deleted, e.g. wrong direction / goal obsolete / duplicate of another intent…",
)}
rows={3}
autoFocus
/>
</div>
</div>
<AlertDialogFooter>
<AlertDialogCancel>{t("Back")}</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
disabled={!cancelIntent || controllingIntent !== null || !cancelReason.trim()}
onClick={() => cancelIntent && void controlWorker(cancelIntent, "cancel", cancelReason, deleteMode)}
>
{controllingIntent ? <Loader2Icon className="animate-spin" /> : <Trash2Icon />}
{deleteMode === "hard" ? t("Delete permanently") : t("Confirm delete")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog open={confirmNewMain} onOpenChange={(open) => !open && setConfirmNewMain(false)}>
<AlertDialogContent className="max-w-[min(32rem,calc(100vw-2rem))]">
<AlertDialogHeader>
<AlertDialogTitle>{t("Start a new session?")}</AlertDialogTitle>
<AlertDialogDescription className="break-words whitespace-normal">
{t(
"The current session will be archived (you can switch back anytime) and the main agent continues with a clean context. The task's graph, assets, and goals are unaffected.",
)}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={creatingMain}>{t("Cancel")}</AlertDialogCancel>
<AlertDialogAction disabled={creatingMain} onClick={() => void createMainSession()}>
{creatingMain ? t("Starting…") : t("Start new session")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</TooltipProvider>
);
}