@skyhook-io/radar-app 1.13.1 → 1.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/package.json +2 -2
  2. package/src/App.tsx +19 -1
  3. package/src/RadarApp.tsx +2 -2
  4. package/src/api/diagnose.test.ts +268 -0
  5. package/src/api/diagnose.ts +72 -9
  6. package/src/components/diagnose/AISettings.tsx +1 -1
  7. package/src/components/diagnose/AgentSetupNotice.tsx +5 -5
  8. package/src/components/diagnose/ApplyDialog.test.tsx +72 -0
  9. package/src/components/diagnose/DiagnoseContext.tsx +12 -17
  10. package/src/components/diagnose/DiagnoseSurface.test.tsx +211 -16
  11. package/src/components/diagnose/DiagnoseSurface.tsx +464 -133
  12. package/src/components/diagnose/Home.test.tsx +293 -0
  13. package/src/components/diagnose/Home.tsx +289 -119
  14. package/src/components/diagnose/InvestigationEvidencePane.test.tsx +2170 -0
  15. package/src/components/diagnose/InvestigationEvidencePane.tsx +2253 -0
  16. package/src/components/diagnose/InvestigationResourceEvidence.test.tsx +257 -0
  17. package/src/components/diagnose/InvestigationResourceEvidence.tsx +214 -0
  18. package/src/components/diagnose/InvestigationView.test.ts +17 -0
  19. package/src/components/diagnose/InvestigationView.tsx +1900 -393
  20. package/src/components/diagnose/LocalDiagnoseAction.tsx +42 -25
  21. package/src/components/diagnose/agentCatalog.ts +1 -1
  22. package/src/components/diagnose/diagnoseEvidenceTypes.ts +151 -0
  23. package/src/components/diagnose/investigationEvidence.test.ts +3109 -0
  24. package/src/components/diagnose/investigationEvidence.ts +3492 -0
  25. package/src/components/diagnose/investigationEvidencePresentation.test.ts +447 -0
  26. package/src/components/diagnose/investigationEvidencePresentation.ts +167 -0
  27. package/src/components/diagnose/investigationExplanation.test.ts +63 -0
  28. package/src/components/diagnose/investigationExplanation.ts +22 -0
  29. package/src/components/diagnose/investigationResourceEvidenceModel.ts +322 -0
  30. package/src/components/diagnose/investigationSourceFocus.test.ts +143 -0
  31. package/src/components/diagnose/investigationSourceFocus.ts +98 -0
  32. package/src/components/diagnose/investigationState.test.ts +695 -0
  33. package/src/components/diagnose/investigationState.ts +451 -0
  34. package/src/components/diagnose/parts.test.tsx +864 -3
  35. package/src/components/diagnose/parts.tsx +1337 -541
  36. package/src/components/diagnose/target.test.ts +39 -0
  37. package/src/components/diagnose/target.ts +36 -0
  38. package/src/components/diagnose/useDisclosureReveal.ts +117 -0
  39. package/src/components/home/MCPSetupDialog.tsx +2 -2
  40. package/src/components/home/mcpToolCatalog.test.ts +22 -0
  41. package/src/components/home/mcpToolCatalog.ts +3 -2
  42. package/src/components/issues/IssuesPane.tsx +5 -1
  43. package/src/components/settings/SettingsDialog.tsx +11 -13
  44. package/src/components/workload/WorkloadView.tsx +1 -1
  45. package/src/context/DiagnoseCustomization.tsx +11 -8
  46. package/src/index.css +63 -79
  47. package/src/index.ts +1 -1
@@ -1,16 +1,64 @@
1
+ import type { ReactNode } from "react";
1
2
  // A view over one durable, server-side investigation run. It SUBSCRIBES to the
2
3
  // run's event stream (replay + live) and reconstructs the transcript; it does not
3
4
  // own the run's lifetime — the server does. So closing the panel or navigating
4
5
  // away just unsubscribes; the run keeps going and re-subscribing replays it.
5
6
  import {
7
+ investigationDisclosureSettleDelay,
8
+ prefersReducedMotion,
9
+ useDisclosureReveal,
10
+ } from "./useDisclosureReveal";
11
+ import { investigationExplanation } from "./investigationExplanation";
12
+ import {
13
+ initialInvestigationPane,
14
+ investigationEvidenceShouldMarkUnread,
15
+ investigationEvidenceAnnouncement,
16
+ investigationIsReadOnly,
17
+ investigationInteractionsBlocked,
18
+ canOfferInvestigationApply,
19
+ investigationApplyAttemptVerified,
20
+ investigationAssessmentNeedsCurrentStateVerification,
21
+ investigationApplyRejectionIsDefinitive,
22
+ investigationApplyCompletionEffects,
23
+ investigationTurnWithTerminalEvent,
24
+ investigationApplyTerminalNeedsClusterRefresh,
25
+ investigationClosedEventIsLive,
26
+ investigationClosedRunIsUnavailable,
27
+ investigationEvidenceInputsEqual,
28
+ investigationEvidenceCoverageLimited,
29
+ investigationEvidenceConflictsWithHealthy,
30
+ investigationEndedBeforeConclusion,
31
+ type InvestigationHistoryUnavailableState,
32
+ investigationHistoryUnavailablePresentation,
33
+ investigationPaneCenteredScrollTop,
34
+ canStopInvestigation,
35
+ canContinueInvestigation,
36
+ canInvestigateFurther,
37
+ } from "./investigationState";
38
+ import type { AssessmentExplanation } from "./parts";
39
+ import {
40
+ Fragment,
6
41
  useCallback,
7
42
  useEffect,
43
+ useId,
8
44
  useLayoutEffect,
45
+ useMemo,
9
46
  useRef,
10
47
  useState,
48
+ type KeyboardEvent,
11
49
  } from "react";
12
50
  import { useQueryClient } from "@tanstack/react-query";
13
- import { Send, AlertTriangle, ArrowDown } from "lucide-react";
51
+ import { Badge, Collapse, CollapseChevron } from "@skyhook-io/k8s-ui";
52
+ import {
53
+ Send,
54
+ AlertTriangle,
55
+ ArrowDown,
56
+ ArrowRight,
57
+ Activity,
58
+ CheckCircle2,
59
+ Files,
60
+ Loader2,
61
+ } from "lucide-react";
14
62
  import {
15
63
  subscribeRun,
16
64
  addTurn,
@@ -24,52 +72,76 @@ import { useDiagnose } from "./DiagnoseContext";
24
72
  import {
25
73
  TurnView,
26
74
  ResultCard,
75
+ AssessmentSources,
27
76
  ApplyDialog,
28
- RunContextCard,
29
77
  appendThinking,
30
78
  upsertTool,
31
79
  type Turn,
32
80
  } from "./parts";
81
+ import {
82
+ investigationActivitySourceDomId,
83
+ investigationEvidenceStepIdsByTurn,
84
+ investigationEvidenceSourceDomId,
85
+ projectInvestigationEvidence,
86
+ resolveInvestigationRootCauseEvidence,
87
+ } from "./investigationEvidence";
88
+ import {
89
+ InvestigationEvidencePane,
90
+ partitionInvestigationEvidence,
91
+ } from "./InvestigationEvidencePane";
92
+ import type { DiagnosisResourceRef } from "./diagnoseEvidenceTypes";
93
+ import { formatInvestigationTarget } from "./target";
94
+ import { parseContextName } from "../../utils/context-name";
95
+ import type { InvestigationSourceExcerpt } from "./investigationSourceFocus";
33
96
 
34
97
  const RECHECK_QUESTION =
35
98
  "Did the fix resolve the issue? Re-check the resource's current status and health now, and say whether it's healthy.";
36
99
 
37
- export function canStopInvestigation(
38
- run: RunSummary,
39
- busy: boolean,
40
- gone: boolean,
41
- latestTurnStatus?: Turn["status"],
42
- ): boolean {
43
- // The transcript is fresher than the polled run summary. Once it has a
44
- // terminal frame, a lagging/failed summary refresh must not resurrect Stop.
45
- const transcriptTerminal =
46
- latestTurnStatus === "done" || latestTurnStatus === "error";
100
+ export function InvestigationStartErrorAlert({
101
+ error,
102
+ onDismiss,
103
+ }: {
104
+ error: string;
105
+ onDismiss: () => void;
106
+ }) {
47
107
  return (
48
- run.trigger !== "background" &&
49
- run.status !== "stale" &&
50
- run.status !== "stopping" &&
51
- !gone &&
52
- !transcriptTerminal &&
53
- (busy || run.status === "running")
108
+ <div
109
+ role="alert"
110
+ className="flex items-start gap-2 border-b border-red-500/30 bg-red-500/10 px-3 py-2.5 text-sm text-theme-text-primary"
111
+ >
112
+ <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-red-400" />
113
+ <div className="min-w-0 flex-1">
114
+ <div className="font-medium">
115
+ Couldn&apos;t start a new investigation
116
+ </div>
117
+ <div className="text-theme-text-secondary">{error}</div>
118
+ </div>
119
+ <button
120
+ type="button"
121
+ onClick={onDismiss}
122
+ className="shrink-0 rounded px-1.5 py-0.5 text-xs text-theme-text-tertiary hover:bg-theme-hover hover:text-theme-text-primary"
123
+ >
124
+ Dismiss
125
+ </button>
126
+ </div>
54
127
  );
55
128
  }
56
129
 
57
- export function canContinueInvestigation(
58
- run: RunSummary,
59
- latestTurnStatus?: Turn["status"],
60
- gone = false,
61
- ): boolean {
62
- const transcriptTerminal =
63
- latestTurnStatus === "done" || latestTurnStatus === "error";
64
- const summaryIsLaggingTerminalTranscript =
65
- run.status === "running" &&
66
- run.trigger !== "background" &&
67
- transcriptTerminal;
68
- return (
69
- !gone &&
70
- run.status !== "stale" &&
71
- run.status !== "stopping" &&
72
- (run.canContinue !== false || summaryIsLaggingTerminalTranscript)
130
+ function captureEvidenceCardLayout(container: HTMLElement) {
131
+ const containerTop = container.getBoundingClientRect().top;
132
+ return new Map(
133
+ Array.from(container.querySelectorAll<HTMLElement>("[data-evidence-card]"))
134
+ .filter((card) => !!card.id)
135
+ .map((card) => {
136
+ const rect = card.getBoundingClientRect();
137
+ return [
138
+ card.id,
139
+ {
140
+ top: rect.top - containerTop + container.scrollTop,
141
+ height: rect.height,
142
+ },
143
+ ] as const;
144
+ }),
73
145
  );
74
146
  }
75
147
 
@@ -77,29 +149,33 @@ export function InvestigationView({
77
149
  run,
78
150
  agentLabel,
79
151
  maximized,
152
+ onOpenResource,
80
153
  }: {
81
154
  run: RunSummary;
82
155
  agentLabel: string;
83
156
  maximized: boolean;
157
+ /** Opens an unambiguous evidence subject in Radar's native resource views. */
158
+ onOpenResource?: (ref: DiagnosisResourceRef) => void;
84
159
  }) {
85
160
  const { kind, namespace, name } = run;
86
161
  // Apply is off for hosted agents (read-only server-side). Keyed on the selected
87
162
  // agent, which matches run.agent unless a deployment mixes hosted + local agents.
88
163
  const { refreshRuns, openInvestigation, startError, dismissError, hosted } =
89
164
  useDiagnose();
90
- // Re-run means look again, so it asks for a new session explicitly and only
165
+ // Investigate again means look again, so it asks for a new session explicitly and only
91
166
  // carries the issue forward — being handed the previous answer is the one
92
167
  // thing someone clicking this doesn't want.
93
168
  const retryDiagnosis = useCallback(
94
169
  () =>
95
170
  openInvestigation({
96
171
  kind,
172
+ group: run.group,
97
173
  namespace,
98
174
  name,
99
175
  issueId: run.issueId,
100
176
  fresh: true,
101
177
  }),
102
- [openInvestigation, kind, namespace, name, run.issueId],
178
+ [openInvestigation, kind, namespace, name, run.group, run.issueId],
103
179
  );
104
180
  const queryClient = useQueryClient();
105
181
  const [turns, setTurns] = useState<Turn[]>([]);
@@ -108,14 +184,96 @@ export function InvestigationView({
108
184
  // show a silent blank panel; instead we render a "no longer available" state.
109
185
  const [gone, setGone] = useState(false);
110
186
  const [busy, setBusy] = useState(false);
187
+ const [requestPending, setRequestPending] = useState(false);
188
+ const explanationRequestSerial = useRef(0);
189
+ const [explanationRequest, setExplanationRequest] = useState<{
190
+ sequence: number;
191
+ status: "running" | "error";
192
+ error?: string;
193
+ } | null>(null);
194
+ const [explanationReveal, setExplanationReveal] = useState<{
195
+ sequence: number;
196
+ request: number;
197
+ currentAssessmentIndex: number;
198
+ } | null>(null);
199
+ const [streamReady, setStreamReady] = useState(false);
200
+ const [historyUnavailable, setHistoryUnavailable] =
201
+ useState<InvestigationHistoryUnavailableState | null>(null);
111
202
  const [input, setInput] = useState("");
112
203
  const [actionError, setActionError] = useState<string | null>(null);
204
+ const [verificationError, setVerificationError] = useState<string | null>(
205
+ null,
206
+ );
207
+ // Retire the assessment as soon as the operator confirms Apply, before the
208
+ // HTTP request crosses the network. A lost response is ambiguous: the server
209
+ // may have accepted and completed the write even though fetch rejected. Only
210
+ // a later structured verification assessment makes that action eligible again.
211
+ const [localApplyAttemptAssessmentIdx, setLocalApplyAttemptAssessmentIdx] =
212
+ useState(-1);
213
+ const [applyOutcomeUncertain, setApplyOutcomeUncertain] = useState<
214
+ string | null
215
+ >(null);
216
+ // Covers the short event-stream hand-off after an apply succeeds and before
217
+ // the server-owned verification turn arrives. This is presentation state only:
218
+ // the durable server job, never the browser, schedules verification.
219
+ const [verificationPending, setVerificationPending] = useState(false);
220
+ // The panes are simultaneous above the workspace breakpoint and tabs below it.
221
+ // Successful/stale history opens on its outcome; running and ended-early runs
222
+ // open on Activity, where the user can immediately see what happened.
223
+ const [narrowPane, setNarrowPane] = useState<"activity" | "evidence">(() =>
224
+ initialInvestigationPane(run.status),
225
+ );
226
+ const [unreadEvidence, setUnreadEvidence] = useState(false);
227
+ const [evidenceUpdateAvailable, setEvidenceUpdateAvailable] = useState(false);
228
+ const [evidenceRevealRequest, setEvidenceRevealRequest] = useState<{
229
+ sourceId: string;
230
+ requestId: number;
231
+ }>();
232
+ const [activityRevealRequest, setActivityRevealRequest] = useState<{
233
+ sourceId: string;
234
+ requestId: number;
235
+ excerpt?: InvestigationSourceExcerpt;
236
+ }>();
113
237
  const scrollRef = useRef<HTMLDivElement>(null);
238
+ const evidenceScrollRef = useRef<HTMLDivElement>(null);
239
+ // display:none reports scrollTop=0 even though the browser retains the pane's
240
+ // position. Keep the visible position for evidence arriving behind Activity.
241
+ const evidenceScrollTopRef = useRef(0);
242
+ const evidenceContentRef = useRef<HTMLDivElement>(null);
243
+ const nextStepsRef = useRef<HTMLElement>(null);
244
+ const evidenceCardLayoutRef = useRef(
245
+ new Map<string, { top: number; height: number }>(),
246
+ );
247
+ const latestEvidenceUpdateSourceIdRef = useRef<string | undefined>(undefined);
248
+ const evidenceProjectionTurnsRef = useRef<readonly Turn[]>([]);
249
+ // Replay is accumulated off-screen and committed once at its boundary. This
250
+ // avoids painting a saved transcript turn-by-turn on initial load or reconnect.
251
+ const turnsRef = useRef<Turn[]>([]);
252
+ const replayTurnsRef = useRef<Turn[]>([]);
253
+ // Every subscription starts with replay, including reconnects to a running run.
254
+ // Motion only resumes after the server's explicit replay_complete boundary.
255
+ const suppressEvidenceMotionRef = useRef(true);
256
+ const seenEvidenceGroupRevisionsRef = useRef<Map<string, number>>(new Map());
257
+ const seenEvidenceSourceIdsRef = useRef<Set<string>>(new Set());
258
+ const replayCompleteRef = useRef(false);
259
+ const streamInFlightRef = useRef(false);
114
260
  const pendingApplyRef = useRef(false);
115
- // Set when THIS view initiates an apply, consumed once on that apply's done event
116
- // to auto-run the health re-check (the verification). Ref, not derived from the
117
- // stream, so replaying a past apply on reopen never re-fires the re-check.
118
- const autoRecheckRef = useRef(false);
261
+ // A replayed historical apply marker must reconstruct the transcript without
262
+ // invalidating queries for the cluster connected today. Remember whether the
263
+ // pending marker belongs to activity this view actually watched (including a
264
+ // locally accepted request whose marker arrived during reconnect replay).
265
+ const pendingApplyStartedLiveRef = useRef(false);
266
+ // Covers the interval after the local Apply confirmation but before its SSE
267
+ // turn marker arrives. pendingApplyRef takes over once the stream confirms it.
268
+ const localApplyRequestRef = useRef(false);
269
+ const paneSelectionTouchedRef = useRef(false);
270
+ const evidenceRevealRequestIdRef = useRef(0);
271
+ const activityRevealRequestIdRef = useRef(0);
272
+ const workspaceId = useId();
273
+ const activityTabId = `${workspaceId}-activity-tab`;
274
+ const activityPaneId = `${workspaceId}-activity-pane`;
275
+ const findingsTabId = `${workspaceId}-findings-tab`;
276
+ const findingsPaneId = `${workspaceId}-findings-pane`;
119
277
  // Stick-to-bottom: follow streaming output while the user is at/near the bottom,
120
278
  // detach the moment they scroll up to read history, re-attach when they return.
121
279
  // Tracked from scroll events (the user's intent) — NOT post-render geometry, which
@@ -124,31 +282,6 @@ export function InvestigationView({
124
282
  const [showJump, setShowJump] = useState(false);
125
283
  const STICK_THRESHOLD = 64; // px from bottom counted as "at the bottom"
126
284
 
127
- // Staged synthesis beats: when a real diagnosis is ready, hold it for a beat and
128
- // narrate the closing reasoning ("Formulating the root cause…" → "Weighing
129
- // remediation options…") before revealing the verdict. These ARE the phases the
130
- // model just ran; pacing their presentation makes the payoff feel earned instead
131
- // of dumped. Apply outcomes and plain follow-ups skip it (no root cause to build).
132
- const [synth, setSynth] = useState<string | null>(null);
133
- // Controls how much of a freshly-revealed diagnosis the card shows, so the verdict
134
- // unfolds in beats: "rca" = root cause only (+ a "weighing remediation" beat),
135
- // "full" = everything. null/"full" for replayed turns (no choreography on rebuild).
136
- const [reveal, setReveal] = useState<"rca" | "full" | null>(null);
137
- const latestTurnStatus = turns[turns.length - 1]?.status;
138
- const canContinue = canContinueInvestigation(run, latestTurnStatus, gone);
139
- // Continuation needs a resumable session, but stopping does not. A brand-new
140
- // hosted turn can be running before the SDK has reported its session id, so
141
- // keep Stop available for human investigations without advertising a
142
- // follow-up composer that the server would reject. Automatic runs remain
143
- // immutable even while their stream marks this view busy.
144
- const canStop = canStopInvestigation(run, busy, gone, latestTurnStatus);
145
- const synthTimers = useRef<ReturnType<typeof setTimeout>[]>([]);
146
- const clearSynth = () => {
147
- synthTimers.current.forEach(clearTimeout);
148
- synthTimers.current = [];
149
- setSynth(null);
150
- };
151
-
152
285
  // After a successful apply, refresh the cluster-state views so the fix shows in
153
286
  // the surrounding UI (Issues, the resource, topology, …), not just the transcript.
154
287
  const refreshClusterState = useCallback(() => {
@@ -166,8 +299,19 @@ export function InvestigationView({
166
299
  }
167
300
  }, [queryClient, kind, namespace, name]);
168
301
 
302
+ const updateTurns = (fn: (prev: Turn[]) => Turn[]) => {
303
+ if (!replayCompleteRef.current) {
304
+ replayTurnsRef.current = fn(replayTurnsRef.current);
305
+ return;
306
+ }
307
+ const next = fn(turnsRef.current);
308
+ turnsRef.current = next;
309
+ setTurns(next);
310
+ };
169
311
  const updateLast = (fn: (t: Turn) => Turn) =>
170
- setTurns((prev) => prev.map((t, i) => (i === prev.length - 1 ? fn(t) : t)));
312
+ updateTurns((prev) =>
313
+ prev.map((t, i) => (i === prev.length - 1 ? fn(t) : t)),
314
+ );
171
315
 
172
316
  // Progressive reasoning reveal: the agent hands us each thinking block whole, but
173
317
  // dumping a paragraph at once reads as a jarring pop. Instead we buffer it and
@@ -184,12 +328,15 @@ export function InvestigationView({
184
328
  revealTimerRef.current = null;
185
329
  }
186
330
  };
187
- const flushReveal = () => {
331
+ const flushReveal = (animate = replayCompleteRef.current) => {
188
332
  stopReveal();
189
333
  const rest = revealBufRef.current;
190
334
  revealBufRef.current = "";
191
335
  if (rest)
192
- updateLast((t) => ({ ...t, timeline: appendThinking(t.timeline, rest) }));
336
+ updateLast((t) => ({
337
+ ...t,
338
+ timeline: appendThinking(t.timeline, rest, animate),
339
+ }));
193
340
  };
194
341
  // Next reveal unit: a whole line, but cap a long unwrapped line at a sentence
195
342
  // boundary so prose paragraphs (no hard breaks) still reveal in pieces.
@@ -226,7 +373,7 @@ export function InvestigationView({
226
373
  if (take)
227
374
  updateLast((t) => ({
228
375
  ...t,
229
- timeline: appendThinking(t.timeline, take),
376
+ timeline: appendThinking(t.timeline, take, true),
230
377
  }));
231
378
  }, 150);
232
379
  };
@@ -235,183 +382,286 @@ export function InvestigationView({
235
382
  // (re)subscribe — the server replays everything, so a fresh tab reconstructs the
236
383
  // whole conversation.
237
384
  useEffect(() => {
385
+ turnsRef.current = [];
386
+ replayTurnsRef.current = [];
238
387
  setTurns([]);
239
388
  setGone(false);
240
389
  setBusy(false);
390
+ setRequestPending(false);
391
+ setStreamReady(false);
392
+ setExplanationRequest(null);
393
+ explanationRequestSerial.current++;
394
+ setExplanationReveal(null);
395
+ setHistoryUnavailable(null);
241
396
  setActionError(null);
397
+ setVerificationError(null);
398
+ setVerificationPending(false);
399
+ setLocalApplyAttemptAssessmentIdx(-1);
400
+ setApplyOutcomeUncertain(null);
242
401
  pendingApplyRef.current = false;
402
+ pendingApplyStartedLiveRef.current = false;
403
+ localApplyRequestRef.current = false;
404
+ replayCompleteRef.current = false;
405
+ streamInFlightRef.current = false;
406
+ suppressEvidenceMotionRef.current = true;
407
+ seenEvidenceGroupRevisionsRef.current.clear();
408
+ seenEvidenceSourceIdsRef.current.clear();
243
409
  revealBufRef.current = "";
244
410
  stopReveal();
245
- clearSynth();
246
- setReveal(null);
247
- // Was the run ALREADY finished when we opened it? Then this subscribe is a
248
- // replay of history — show every verdict immediately, no staged-reveal beats.
249
- // The choreography is only for a verdict we watch land live (status running at
250
- // open). Captured here, not read live, so a follow-up later doesn't re-trigger
251
- // it for the replayed turns.
252
- const replaying = run.status !== "running";
411
+ paneSelectionTouchedRef.current = false;
412
+ setNarrowPane(initialInvestigationPane(run.status));
413
+ setUnreadEvidence(false);
414
+ setEvidenceUpdateAvailable(false);
415
+ evidenceScrollTopRef.current = 0;
416
+ latestEvidenceUpdateSourceIdRef.current = undefined;
417
+ setEvidenceRevealRequest(undefined);
418
+ evidenceRevealRequestIdRef.current = 0;
419
+ setActivityRevealRequest(undefined);
420
+ activityRevealRequestIdRef.current = 0;
421
+ evidenceCardLayoutRef.current.clear();
422
+ evidenceProjectionTurnsRef.current = [];
253
423
  const cancel = subscribeRun(run.id, {
254
- onEvent: (ev: DiagnoseStreamEvent) => {
424
+ onEvent: (ev: DiagnoseStreamEvent, sequence?: number) => {
425
+ const live = replayCompleteRef.current;
255
426
  switch (ev.type) {
256
427
  case "turn":
257
- flushReveal(); // close out the prior turn's reasoning before the new one
258
- clearSynth();
259
- setReveal(null);
260
- if (ev.apply) pendingApplyRef.current = true;
261
- setBusy(true);
262
- setTurns((prev) => [
428
+ flushReveal(live); // close out the prior turn's reasoning before the new one
429
+ setRequestPending(false);
430
+ if (ev.explainAssessment) setExplanationRequest(null);
431
+ if (ev.apply) {
432
+ pendingApplyStartedLiveRef.current =
433
+ pendingApplyStartedLiveRef.current ||
434
+ live ||
435
+ localApplyRequestRef.current;
436
+ pendingApplyRef.current = true;
437
+ localApplyRequestRef.current = false;
438
+ // The stream now owns the exact outcome. Keep the assessment
439
+ // retired, but replace the transport-uncertainty banner with the
440
+ // streamed apply result / error when it arrives.
441
+ setApplyOutcomeUncertain(null);
442
+ }
443
+ if (ev.verify) {
444
+ setVerificationPending(false);
445
+ setVerificationError(null);
446
+ }
447
+ streamInFlightRef.current = true;
448
+ if (live) setBusy(true);
449
+ updateTurns((prev) => [
263
450
  ...prev,
264
451
  {
265
452
  question: ev.question,
266
453
  actor: ev.actor,
454
+ explainAssessment: ev.explainAssessment,
267
455
  timeline: [],
268
456
  diagnosis: null,
269
457
  error: null,
270
458
  status: "running",
271
459
  apply: ev.apply,
460
+ verify: ev.verify,
272
461
  },
273
462
  ]);
274
463
  break;
275
464
  case "thinking":
276
465
  if (ev.token) {
277
- revealBufRef.current += ev.token;
278
- pumpReveal();
466
+ if (live) {
467
+ revealBufRef.current += ev.token;
468
+ pumpReveal();
469
+ } else {
470
+ updateLast((t) => ({
471
+ ...t,
472
+ timeline: appendThinking(t.timeline, ev.token!, false),
473
+ }));
474
+ }
279
475
  }
280
476
  break;
281
477
  case "step":
282
- flushReveal(); // reasoning fully precedes the tool it led to
478
+ flushReveal(live); // reasoning fully precedes the tool it led to
283
479
  if (ev.step)
284
480
  updateLast((t) => ({
285
481
  ...t,
286
- timeline: upsertTool(t.timeline, ev.step!),
482
+ timeline: upsertTool(t.timeline, ev.step!, live),
287
483
  }));
288
484
  break;
289
485
  case "done": {
290
- flushReveal(); // the result can't wait on a reveal animation
291
- const dx = (ev.diagnosis ?? null) as Diagnosis | null;
486
+ flushReveal(live); // the result can't wait on a reveal animation
292
487
  const isApply = pendingApplyRef.current;
293
- const finalize = () => {
294
- setBusy(false);
295
- if (isApply) {
296
- pendingApplyRef.current = false;
297
- refreshClusterState();
298
- // Verify the write automatically: re-check health as a follow-up
299
- // turn. Guarded by autoRecheckRef so replaying a past apply on
300
- // reopen never re-fires it.
301
- if (autoRecheckRef.current && run.status !== "stale") {
302
- autoRecheckRef.current = false;
303
- setTimeout(() => {
304
- addTurn(run.id, { question: RECHECK_QUESTION }).catch(
305
- () => {},
306
- );
307
- }, 900);
308
- }
488
+ const applyStartedLive = pendingApplyStartedLiveRef.current;
489
+ streamInFlightRef.current = false;
490
+ updateLast((t) => ({
491
+ ...investigationTurnWithTerminalEvent(t, ev, live),
492
+ resultSequence: sequence,
493
+ }));
494
+ if (live) setBusy(false);
495
+ if (isApply) {
496
+ pendingApplyRef.current = false;
497
+ const effects = investigationApplyCompletionEffects({
498
+ live,
499
+ applyStartedLive,
500
+ stale: run.status === "stale",
501
+ });
502
+ pendingApplyStartedLiveRef.current = false;
503
+ if (effects.refreshClusterState) refreshClusterState();
504
+ // A successful apply is one compound server-owned job. Its next
505
+ // durable event is the automatic read-only verification turn; hold
506
+ // the controls through that adjacent event so there is no idle flash.
507
+ if (effects.verificationPending) setVerificationPending(true);
508
+ }
509
+ if (live || (isApply && applyStartedLive)) refreshRuns();
510
+ break;
511
+ }
512
+ case "error": {
513
+ flushReveal(live);
514
+ streamInFlightRef.current = false;
515
+ const verificationScheduled =
516
+ live && ev.verificationScheduled === true;
517
+ const applyMayHaveMutated =
518
+ investigationApplyTerminalNeedsClusterRefresh({
519
+ localApplyRequestPending: localApplyRequestRef.current,
520
+ streamedApplyPending: pendingApplyRef.current,
521
+ streamedApplyStartedLive: pendingApplyStartedLiveRef.current,
522
+ terminalEventIsLive: live,
523
+ });
524
+ {
525
+ const activeTurns = replayCompleteRef.current
526
+ ? turnsRef.current
527
+ : replayTurnsRef.current;
528
+ if (live && activeTurns.at(-1)?.verify) {
529
+ setVerificationError(
530
+ ev.error || "The verification could not be completed.",
531
+ );
309
532
  }
310
- refreshRuns();
311
- };
312
- const showCard = (stage: "rca" | "full") => {
313
- setReveal(stage);
314
- updateLast((t) => ({ ...t, diagnosis: dx, status: "done" }));
315
- };
316
- // Only a real, structured diagnosis earns the staged reveal — and only
317
- // when watched live. On replay (the run was already finished when we
318
- // opened it) the beats would just stall showing a verdict that's
319
- // already known, so we skip straight to the full card.
320
- const hasRC = !!dx?.rootCause;
321
- const hasRem = (dx?.remediation?.length ?? 0) > 0;
322
- const allClear = !!dx?.healthy && !hasRC;
323
- const inconclusive = !!dx?.inconclusive && !hasRC;
324
- const structured =
325
- !!dx && (allClear || inconclusive || hasRC || hasRem);
326
- if (!isApply && structured && !replaying) {
327
- const STEP = 2000;
328
- // Beat 1 (in the timeline): formulating, before any card is shown.
329
- setReveal(null);
330
- setSynth(
331
- allClear
332
- ? "Confirming health"
333
- : inconclusive
334
- ? "Weighing the evidence"
335
- : hasRC
336
- ? "Formulating the root cause"
337
- : "Analyzing the findings",
338
- );
339
- synthTimers.current.push(
340
- setTimeout(() => {
341
- setSynth(null);
342
- // Reveal the root cause. If remediation follows, the card shows a
343
- // "weighing remediation options" beat where the steps will land.
344
- showCard(hasRC && hasRem ? "rca" : "full");
345
- if (hasRC && hasRem) {
346
- synthTimers.current.push(
347
- setTimeout(() => {
348
- setReveal("full");
349
- finalize();
350
- }, STEP),
351
- );
352
- } else {
353
- finalize();
354
- }
355
- }, STEP),
356
- );
357
- } else {
358
- setReveal("full");
359
- updateLast((t) => ({ ...t, diagnosis: dx, status: "done" }));
360
- finalize();
361
533
  }
534
+ updateLast((t) => investigationTurnWithTerminalEvent(t, ev, live));
535
+ if (live && !verificationScheduled) setBusy(false);
536
+ setRequestPending(false);
537
+ pendingApplyRef.current = false;
538
+ pendingApplyStartedLiveRef.current = false;
539
+ localApplyRequestRef.current = false;
540
+ setVerificationPending(verificationScheduled);
541
+ if (applyMayHaveMutated) refreshClusterState();
542
+ if (live) refreshRuns();
362
543
  break;
363
544
  }
364
- case "error":
365
- flushReveal();
366
- updateLast((t) => ({
367
- ...t,
368
- error: ev.error || "The investigation failed.",
369
- status: "error",
370
- }));
545
+ case "history_unavailable":
546
+ setHistoryUnavailable({
547
+ error: ev.error || "Radar could not read the saved history.",
548
+ retryable: ev.retryable === true,
549
+ });
550
+ setStreamReady(false);
371
551
  setBusy(false);
372
- pendingApplyRef.current = false;
373
- refreshRuns();
552
+ setRequestPending(false);
374
553
  break;
554
+ case "replay_complete":
555
+ turnsRef.current = replayTurnsRef.current;
556
+ setTurns(replayTurnsRef.current);
557
+ if (!paneSelectionTouchedRef.current) {
558
+ const latest = replayTurnsRef.current.at(-1);
559
+ if (latest?.explainAssessment) {
560
+ setNarrowPane("evidence");
561
+ } else if (
562
+ latest?.status === "done" &&
563
+ latest.question &&
564
+ !latest.verify &&
565
+ !latest.apply
566
+ ) {
567
+ setNarrowPane("activity");
568
+ }
569
+ }
570
+ replayCompleteRef.current = true;
571
+ suppressEvidenceMotionRef.current = false;
572
+ setHistoryUnavailable(null);
573
+ setStreamReady(true);
574
+ setBusy(streamInFlightRef.current);
575
+ break;
576
+ }
577
+ },
578
+ onReplayStart: () => {
579
+ // `open` fires for reconnects too. Transitioning from live seeds the replay
580
+ // staging buffer from the committed transcript. If an initial replay itself
581
+ // reconnects, preserve its uncommitted prefix: Last-Event-ID only sends the
582
+ // suffix, so resetting here would silently drop already-received history.
583
+ const wasLive = replayCompleteRef.current;
584
+ if (wasLive) {
585
+ flushReveal(true);
586
+ replayTurnsRef.current = turnsRef.current;
375
587
  }
588
+ replayCompleteRef.current = false;
589
+ suppressEvidenceMotionRef.current = true;
590
+ setStreamReady(false);
376
591
  },
377
592
  // The run can no longer produce events (evicted / gone). Stale runs emit their
378
593
  // own error event + banner before closing, so this only bites the case where a
379
594
  // run vanishes while we still think it's running — clear the spinner and mark
380
595
  // the open turn terminal so it can't shimmer forever.
381
- onClosed: () => {
596
+ onClosed: (reason) => {
382
597
  stopReveal();
383
- clearSynth();
598
+ const applyMayHaveMutated =
599
+ investigationApplyTerminalNeedsClusterRefresh({
600
+ localApplyRequestPending: localApplyRequestRef.current,
601
+ streamedApplyPending: pendingApplyRef.current,
602
+ streamedApplyStartedLive: pendingApplyStartedLiveRef.current,
603
+ terminalEventIsLive: investigationClosedEventIsLive({
604
+ reason,
605
+ subscribedRunStatus: run.status,
606
+ replayComplete: replayCompleteRef.current,
607
+ }),
608
+ });
609
+ pendingApplyRef.current = false;
610
+ pendingApplyStartedLiveRef.current = false;
611
+ localApplyRequestRef.current = false;
612
+ if (applyMayHaveMutated) refreshClusterState();
613
+ if (!replayCompleteRef.current) {
614
+ turnsRef.current = replayTurnsRef.current;
615
+ setTurns(replayTurnsRef.current);
616
+ }
617
+ replayCompleteRef.current = false;
618
+ streamInFlightRef.current = false;
384
619
  setBusy(false);
385
- setGone(true); // render decides: empty → gone state; mid-run → terminal error
386
- updateLast((t) =>
387
- t.status === "running"
388
- ? {
389
- ...t,
390
- status: "error",
391
- error:
392
- "This investigation is no longer available. Re-run Diagnose to analyze the current cluster.",
393
- }
394
- : t,
395
- );
620
+ setRequestPending(false);
621
+ setVerificationPending(false);
622
+ setHistoryUnavailable(null);
623
+ setStreamReady(true);
624
+ // A durable close is expected for stale history. A 404/eviction is gone;
625
+ // don't relabel a successfully reconstructed stale transcript as missing.
626
+ const unavailable = investigationClosedRunIsUnavailable({
627
+ reason,
628
+ subscribedRunStatus: run.status,
629
+ });
630
+ setGone(unavailable);
631
+ if (unavailable) {
632
+ const current = turnsRef.current;
633
+ const next = current.map((t, i) =>
634
+ i === current.length - 1 && t.status === "running"
635
+ ? {
636
+ ...t,
637
+ status: "error" as const,
638
+ error: applyMayHaveMutated
639
+ ? "This investigation is no longer available. The requested change may have completed; Radar refreshed cluster state, but you should start a new investigation to verify it before applying anything again."
640
+ : "This investigation is no longer available. Start a new investigation to analyze the current cluster.",
641
+ }
642
+ : t,
643
+ );
644
+ turnsRef.current = next;
645
+ setTurns(next);
646
+ }
396
647
  },
397
648
  });
398
649
  return () => {
399
650
  stopReveal();
400
- clearSynth();
401
651
  cancel();
402
652
  };
403
653
  // eslint-disable-next-line react-hooks/exhaustive-deps
404
654
  }, [run.id]);
405
655
 
406
656
  // Follow the bottom IFF still pinned, on anything that changes rendered height:
407
- // new transcript content (turns), the staged verdict reveal (reveal: rca→full
408
- // adds the remediation card), and synthesis beats (synth). useLayoutEffect runs
657
+ // new transcript content (turns), or Activity becoming visible after live work
658
+ // arrived behind the Findings tab. useLayoutEffect runs
409
659
  // before paint, so the jump is invisible and it overrides browser scroll-anchoring
410
660
  // (which would otherwise nudge us off the bottom when the remediation card lands).
411
661
  useLayoutEffect(() => {
412
662
  const el = scrollRef.current;
413
663
  if (el && pinnedRef.current) el.scrollTop = el.scrollHeight;
414
- }, [turns, reveal, synth]);
664
+ }, [turns, narrowPane]);
415
665
 
416
666
  // User scroll updates the pin state: scrolling up past the threshold detaches;
417
667
  // scrolling back within it re-attaches. Programmatic scroll-to-bottom lands at
@@ -429,32 +679,108 @@ export function InvestigationView({
429
679
  if (!el) return;
430
680
  pinnedRef.current = true;
431
681
  setShowJump(false);
432
- el.scrollTo({ top: el.scrollHeight, behavior: "smooth" });
682
+ el.scrollTo({
683
+ top: el.scrollHeight,
684
+ behavior: prefersReducedMotion() ? "auto" : "smooth",
685
+ });
433
686
  };
434
687
 
435
688
  const stale = run.status === "stale";
689
+ const lastTurn = turns.at(-1);
690
+ const unavailable = investigationIsReadOnly(run.status, gone);
691
+ const canContinue = canContinueInvestigation(run, lastTurn?.status, gone);
692
+ const canStop = canStopInvestigation(run, busy, gone, lastTurn?.status);
693
+ const readOnly = unavailable || !canContinue;
694
+ const endedEarly = investigationEndedBeforeConclusion(run.status, lastTurn);
695
+ const rebuildingReplay = !streamReady && turns.length === 0;
696
+ const historyUnavailablePresentation = historyUnavailable
697
+ ? investigationHistoryUnavailablePresentation(historyUnavailable)
698
+ : null;
699
+ const interactionsBlocked = investigationInteractionsBlocked({
700
+ streamReady,
701
+ busy,
702
+ requestPending,
703
+ readOnly,
704
+ verificationPending,
705
+ });
436
706
 
437
707
  const submitFollowup = () => {
438
708
  const q = input.trim();
439
- if (!q || busy || !canContinue) return;
709
+ if (!q || interactionsBlocked) return;
440
710
  setInput("");
441
711
  setActionError(null);
712
+ setNarrowPane("activity");
713
+ suppressEvidenceMotionRef.current = false;
442
714
  pinnedRef.current = true; // a user-initiated turn always follows to the bottom
443
- addTurn(run.id, { question: q }).catch((e) =>
444
- setActionError(e instanceof DiagnoseError ? e.message : "Couldn't send."),
445
- );
715
+ setRequestPending(true);
716
+ addTurn(run.id, { question: q }).catch((e) => {
717
+ setRequestPending(false);
718
+ setActionError(e instanceof DiagnoseError ? e.message : "Couldn't send.");
719
+ });
446
720
  };
447
721
  const stop = () => stopRun(run.id);
448
722
 
449
- // Ask a canned follow-up (e.g. "explain simply") — a one-tap path that turns the
450
- // prompt's plain-language instruction into something the user controls.
451
- const askFollowup = (q: string) => {
452
- if (busy || !canContinue) return;
723
+ const askExplanation = (sequence: number) => {
724
+ if (interactionsBlocked) return;
453
725
  setActionError(null);
454
- pinnedRef.current = true;
455
- addTurn(run.id, { question: q }).catch((e) =>
456
- setActionError(e instanceof DiagnoseError ? e.message : "Couldn't send."),
457
- );
726
+ const serial = ++explanationRequestSerial.current;
727
+ const previousTurns = turnsRef.current.length;
728
+ setExplanationRequest({ sequence, status: "running" });
729
+ setRequestPending(true);
730
+ addTurn(run.id, { explainAssessment: sequence }).catch((e) => {
731
+ if (serial !== explanationRequestSerial.current) return;
732
+ // If the stream already accepted the turn, it owns progress and failure.
733
+ if (
734
+ turnsRef.current
735
+ .slice(previousTurns)
736
+ .some((turn) => turn.explainAssessment === sequence)
737
+ )
738
+ return;
739
+ setRequestPending(false);
740
+ setExplanationRequest({
741
+ sequence,
742
+ status: "error",
743
+ error:
744
+ e instanceof DiagnoseError
745
+ ? e.message
746
+ : "Couldn't request an explanation.",
747
+ });
748
+ });
749
+ };
750
+
751
+ const explanationFor = (
752
+ assessment: Turn,
753
+ ): AssessmentExplanation | undefined => {
754
+ const sequence = assessment.resultSequence;
755
+ if (!sequence || !assessment.diagnosis?.rootCause) return undefined;
756
+ const saved = investigationExplanation(turns, sequence);
757
+ // This intent is implemented by the local run manager; hosted continuation
758
+ // alone does not imply support for assessment-bound explanation turns.
759
+ if ((readOnly || hosted) && saved.status === "idle") return undefined;
760
+ const state =
761
+ explanationRequest?.sequence === sequence ? explanationRequest : saved;
762
+ return {
763
+ ...state,
764
+ onGenerate:
765
+ !hosted && !interactionsBlocked
766
+ ? () => askExplanation(sequence)
767
+ : undefined,
768
+ openRequest:
769
+ explanationReveal?.sequence === sequence &&
770
+ explanationReveal.currentAssessmentIndex === currentAssessmentIdx
771
+ ? explanationReveal.request
772
+ : undefined,
773
+ };
774
+ };
775
+
776
+ const viewExplanation = (sequence: number) => {
777
+ paneSelectionTouchedRef.current = true;
778
+ setNarrowPane("evidence");
779
+ setExplanationReveal((previous) => ({
780
+ sequence,
781
+ request: (previous?.request ?? 0) + 1,
782
+ currentAssessmentIndex: currentAssessmentIdx,
783
+ }));
458
784
  };
459
785
 
460
786
  // Apply: a user-confirmed remediation turn. Any step is applyable; the chosen
@@ -462,40 +788,90 @@ export function InvestigationView({
462
788
  const [confirmApply, setConfirmApply] = useState(false);
463
789
  const [pendingFix, setPendingFix] = useState("");
464
790
  const requestApply = (fix: string) => {
791
+ if (interactionsBlocked) return;
465
792
  setPendingFix(fix);
466
793
  setConfirmApply(true);
467
794
  };
468
795
  const runApply = () => {
469
796
  setConfirmApply(false);
797
+ if (interactionsBlocked) return;
470
798
  setActionError(null);
471
- autoRecheckRef.current = true; // verify the write automatically once it lands
799
+ setVerificationError(null);
800
+ setApplyOutcomeUncertain(null);
801
+ setNarrowPane("activity");
802
+ suppressEvidenceMotionRef.current = false;
803
+ pinnedRef.current = true;
804
+ // Pessimistic by design: once the operator confirms a write, a transport
805
+ // failure cannot prove that it did not run. Retire this assessment before
806
+ // fetch and require a later verification before Apply can return.
807
+ setLocalApplyAttemptAssessmentIdx((previous) =>
808
+ Math.max(previous, currentAssessmentIdx),
809
+ );
810
+ localApplyRequestRef.current = true;
811
+ setRequestPending(true);
472
812
  addTurn(run.id, { apply: true, fix: pendingFix }).catch((e) => {
473
- autoRecheckRef.current = false; // the apply never started — don't auto-recheck
474
- setActionError(
475
- e instanceof DiagnoseError ? e.message : "Couldn't apply.",
813
+ setRequestPending(false);
814
+ if (investigationApplyRejectionIsDefinitive(e)) {
815
+ localApplyRequestRef.current = false;
816
+ setLocalApplyAttemptAssessmentIdx(-1);
817
+ setApplyOutcomeUncertain(null);
818
+ setActionError(e.message.trim() || "Couldn't apply.");
819
+ return;
820
+ }
821
+ refreshClusterState();
822
+ const detail = e instanceof DiagnoseError ? e.message.trim() : "";
823
+ setApplyOutcomeUncertain(
824
+ detail
825
+ ? `${detail} Radar has not verified the current state; check it before applying again.`
826
+ : "Radar couldn't confirm whether the apply request completed. Cluster state was refreshed; check current status before applying again.",
827
+ );
828
+ });
829
+ };
830
+ const checkStatus = () => {
831
+ if (interactionsBlocked) return Promise.resolve();
832
+ setActionError(null);
833
+ setVerificationError(null);
834
+ setNarrowPane("activity");
835
+ suppressEvidenceMotionRef.current = false;
836
+ pinnedRef.current = true;
837
+ setRequestPending(true);
838
+ return addTurn(run.id, {
839
+ question: RECHECK_QUESTION,
840
+ verify: true,
841
+ }).catch((error) => {
842
+ setRequestPending(false);
843
+ setVerificationError(
844
+ error instanceof DiagnoseError
845
+ ? error.message
846
+ : "Couldn't check status.",
476
847
  );
477
848
  });
478
849
  };
479
- const checkStatus = () =>
480
- addTurn(run.id, { question: RECHECK_QUESTION }).catch(() => {});
481
850
 
482
851
  // Apply tracks the latest turn that produced remediation (so follow-ups don't
483
- // strip it) and is blocked on a stale (context-switched) run.
852
+ // strip it). Any accepted apply attempt, including a stopped or failed one,
853
+ // retires that assessment until a later verification produces a new one.
484
854
  let lastRemediationIdx = -1;
855
+ let lastApplyAttemptIdx = -1;
856
+ let lastApplyOutcome: Turn["applyOutcome"];
485
857
  turns.forEach((t, i) => {
486
858
  if (
487
859
  t.status === "done" &&
488
860
  !t.apply &&
861
+ !t.explainAssessment &&
862
+ (!t.question || t.verify) &&
489
863
  (t.diagnosis?.remediation?.length ?? 0) > 0
490
864
  )
491
865
  lastRemediationIdx = i;
866
+ if (t.apply) {
867
+ lastApplyAttemptIdx = i;
868
+ lastApplyOutcome = t.applyOutcome;
869
+ }
492
870
  });
493
871
 
494
- // The "primary verdict" — the latest initial-style structured diagnosis (root
495
- // cause / remediation / healthy / inconclusive), excluding apply outcomes and
496
- // conversational follow-ups. In the maximized workspace this pins to a side rail
497
- // so it (and Apply) stay in view while the transcript scrolls as evidence.
498
- let pinnedIdx = -1;
872
+ // Initial and explicit verification turns update the Evidence-pane assessment.
873
+ // Ordinary questions remain conversational answers in Activity.
874
+ const assessmentIndexes: number[] = [];
499
875
  turns.forEach((t, i) => {
500
876
  const dx = t.diagnosis;
501
877
  const structured =
@@ -504,221 +880,1352 @@ export function InvestigationView({
504
880
  (dx.remediation?.length ?? 0) > 0 ||
505
881
  dx.healthy ||
506
882
  dx.inconclusive);
507
- if (t.status === "done" && !t.apply && !t.question && structured)
508
- pinnedIdx = i;
883
+ if (
884
+ t.status === "done" &&
885
+ !t.apply &&
886
+ !t.explainAssessment &&
887
+ (!t.question || t.verify) &&
888
+ structured
889
+ )
890
+ assessmentIndexes.push(i);
509
891
  });
510
- const pinned = maximized && pinnedIdx >= 0;
892
+ const currentAssessmentIdx = assessmentIndexes.at(-1) ?? -1;
893
+ const initialAssessmentIdx = assessmentIndexes[0] ?? -1;
894
+ const hasMultipleAssessments = assessmentIndexes.length > 1;
895
+ const currentAssessment =
896
+ currentAssessmentIdx >= 0 ? turns[currentAssessmentIdx] : undefined;
511
897
 
512
- return (
513
- <div className="relative flex min-h-0 flex-1 flex-col">
514
- <div className="flex min-h-0 flex-1">
515
- <div
516
- ref={scrollRef}
517
- onScroll={onScroll}
518
- className="flex-1 overflow-y-auto overflow-x-hidden px-4 py-3 [scrollbar-gutter:stable]"
898
+ const laterVerificationRecorded = investigationApplyAttemptVerified({
899
+ localApplyAttemptAssessmentIdx,
900
+ currentAssessmentIdx,
901
+ currentAssessmentIsVerification:
902
+ turns[currentAssessmentIdx]?.verify === true,
903
+ });
904
+ useEffect(() => {
905
+ if (!laterVerificationRecorded) return;
906
+ setLocalApplyAttemptAssessmentIdx(-1);
907
+ setApplyOutcomeUncertain(null);
908
+ localApplyRequestRef.current = false;
909
+ }, [laterVerificationRecorded]);
910
+
911
+ if (
912
+ !investigationEvidenceInputsEqual(evidenceProjectionTurnsRef.current, turns)
913
+ ) {
914
+ evidenceProjectionTurnsRef.current = turns;
915
+ }
916
+ const evidenceProjectionTurns = evidenceProjectionTurnsRef.current;
917
+ const projection = useMemo(
918
+ () =>
919
+ projectInvestigationEvidence(evidenceProjectionTurns, {
920
+ kind,
921
+ group: run.group,
922
+ namespace,
923
+ name,
924
+ }),
925
+ [evidenceProjectionTurns, kind, namespace, name, run.group],
926
+ );
927
+ const currentAssessmentProjection = useMemo(
928
+ () =>
929
+ projectInvestigationEvidence(
930
+ currentAssessmentIdx >= 0
931
+ ? [evidenceProjectionTurns[currentAssessmentIdx]]
932
+ : [],
933
+ { kind, group: run.group, namespace, name },
934
+ ),
935
+ [
936
+ evidenceProjectionTurns,
937
+ currentAssessmentIdx,
938
+ kind,
939
+ namespace,
940
+ name,
941
+ run.group,
942
+ ],
943
+ );
944
+ const rootCauseEvidenceResolution = useMemo(
945
+ () =>
946
+ currentAssessment?.diagnosis?.rootCause
947
+ ? resolveInvestigationRootCauseEvidence(
948
+ projection,
949
+ currentAssessment.diagnosis.rootCauseEvidence,
950
+ currentAssessmentIdx,
951
+ )
952
+ : undefined,
953
+ [currentAssessment, currentAssessmentIdx, projection],
954
+ );
955
+ const visibleEvidenceGroupIds = useMemo(
956
+ () =>
957
+ new Set(
958
+ partitionInvestigationEvidence(
959
+ projection.groups,
960
+ rootCauseEvidenceResolution,
961
+ ).collectionByGroup.keys(),
962
+ ),
963
+ [projection.groups, rootCauseEvidenceResolution],
964
+ );
965
+ const evidenceStepIdsByTurn = useMemo(
966
+ () =>
967
+ investigationEvidenceStepIdsByTurn(projection, visibleEvidenceGroupIds),
968
+ [projection, visibleEvidenceGroupIds],
969
+ );
970
+
971
+ const animateEvidenceGroupIds = useMemo(() => {
972
+ if (suppressEvidenceMotionRef.current) return new Set<string>();
973
+ return new Set(
974
+ projection.groups
975
+ .filter((group) => {
976
+ if (!visibleEvidenceGroupIds.has(group.id)) return false;
977
+ const seen = seenEvidenceGroupRevisionsRef.current.get(group.id) ?? 0;
978
+ return (
979
+ group.observations.length > seen &&
980
+ group.observations
981
+ .slice(seen)
982
+ .some(
983
+ (observation) =>
984
+ turns[observation.source.turnIndex]?.timeline[
985
+ observation.source.timelineIndex
986
+ ]?.animate === true,
987
+ )
988
+ );
989
+ })
990
+ .map((group) => group.id),
991
+ );
992
+ }, [projection.groups, turns, visibleEvidenceGroupIds]);
993
+ useEffect(() => {
994
+ for (const group of projection.groups) {
995
+ seenEvidenceGroupRevisionsRef.current.set(
996
+ group.id,
997
+ group.observations.length,
998
+ );
999
+ }
1000
+ }, [projection.groups]);
1001
+
1002
+ // A repeated check can revise an existing card without changing the group
1003
+ // count, so new live sources—not card count—drive the inactive-tab pulse.
1004
+ // Replayed sources are marked seen without pulsing the tab.
1005
+ useEffect(() => {
1006
+ const newLiveSources = projection.sources.filter((source) => {
1007
+ if (!evidenceStepIdsByTurn.get(source.turnIndex)?.has(source.stepId))
1008
+ return false;
1009
+ if (seenEvidenceSourceIdsRef.current.has(source.id)) return false;
1010
+ return (
1011
+ turns[source.turnIndex]?.timeline[source.timelineIndex]?.animate ===
1012
+ true
1013
+ );
1014
+ });
1015
+ const hasNewLiveSource = newLiveSources.length > 0;
1016
+ if (
1017
+ investigationEvidenceShouldMarkUnread({
1018
+ hasNewLiveSource,
1019
+ selectedPane: narrowPane,
1020
+ evidencePaneVisible:
1021
+ evidenceScrollRef.current !== null &&
1022
+ evidenceScrollRef.current.offsetParent !== null,
1023
+ })
1024
+ ) {
1025
+ setUnreadEvidence(true);
1026
+ }
1027
+ const scrollTop = evidenceScrollRef.current?.offsetParent
1028
+ ? evidenceScrollRef.current.scrollTop
1029
+ : evidenceScrollTopRef.current;
1030
+ if (hasNewLiveSource && scrollTop > 80) {
1031
+ latestEvidenceUpdateSourceIdRef.current = newLiveSources.at(-1)?.id;
1032
+ setEvidenceUpdateAvailable(true);
1033
+ }
1034
+ for (const source of projection.sources) {
1035
+ seenEvidenceSourceIdsRef.current.add(source.id);
1036
+ }
1037
+ }, [projection.sources, turns, narrowPane, evidenceStepIdsByTurn]);
1038
+
1039
+ // The projection can fold a fresh observation into an existing evidence
1040
+ // source. Keep the scrolled-away cue reliable for that in-place revision too.
1041
+ useEffect(() => {
1042
+ const scrollTop = evidenceScrollRef.current?.offsetParent
1043
+ ? evidenceScrollRef.current.scrollTop
1044
+ : evidenceScrollTopRef.current;
1045
+ if (animateEvidenceGroupIds.size > 0 && scrollTop > 80) {
1046
+ const latestChangedSource = projection.groups
1047
+ .filter((group) => animateEvidenceGroupIds.has(group.id))
1048
+ .map((group) => group.chronologicalLatest.source)
1049
+ .filter((source) =>
1050
+ evidenceStepIdsByTurn.get(source.turnIndex)?.has(source.stepId),
1051
+ )
1052
+ .sort((left, right) => left.order - right.order)
1053
+ .at(-1);
1054
+ if (latestChangedSource) {
1055
+ latestEvidenceUpdateSourceIdRef.current = latestChangedSource.id;
1056
+ setEvidenceUpdateAvailable(true);
1057
+ }
1058
+ }
1059
+ }, [animateEvidenceGroupIds, projection.groups, evidenceStepIdsByTurn]);
1060
+
1061
+ // Evidence is inserted into semantic tiers rather than blindly appended. Keep
1062
+ // the first card a user is reading fixed in place when a live result lands above
1063
+ // it. Native scroll anchoring varies across nested grids, so this pane owns the
1064
+ // policy explicitly (and leaves the top of the story free to update when the
1065
+ // reader has not scrolled away from it).
1066
+ const evidenceLayoutRevision = `${projection.groups
1067
+ .filter((group) => visibleEvidenceGroupIds.has(group.id))
1068
+ .map(
1069
+ (group) =>
1070
+ `${group.id}:${group.observations.length}:${group.latest.tier}:${group.historical ? 1 : 0}`,
1071
+ )
1072
+ .join("|")}|limitations:${projection.limitations
1073
+ .map(
1074
+ (limitation) =>
1075
+ `${limitation.kind}:${limitation.source}:${limitation.sources.length}:${limitation.message}`,
1076
+ )
1077
+ .join(",")}`;
1078
+ useLayoutEffect(() => {
1079
+ const container = evidenceScrollRef.current;
1080
+ if (!container || container.offsetParent === null) return;
1081
+ const cards = Array.from(
1082
+ container.querySelectorAll<HTMLElement>("[data-evidence-card]"),
1083
+ );
1084
+ const current = captureEvidenceCardLayout(container);
1085
+
1086
+ const previous = evidenceCardLayoutRef.current;
1087
+ if (
1088
+ !suppressEvidenceMotionRef.current &&
1089
+ previous.size > 0 &&
1090
+ container.scrollTop > 8
1091
+ ) {
1092
+ const anchor = cards
1093
+ .map((card) => ({ card, layout: previous.get(card.id) }))
1094
+ .filter(
1095
+ (
1096
+ entry,
1097
+ ): entry is {
1098
+ card: HTMLElement;
1099
+ layout: { top: number; height: number };
1100
+ } => !!entry.layout,
1101
+ )
1102
+ .sort((a, b) => a.layout.top - b.layout.top)
1103
+ .find(
1104
+ ({ layout }) => layout.top + layout.height >= container.scrollTop - 1,
1105
+ );
1106
+ if (anchor) {
1107
+ const nextTop = current.get(anchor.card.id)?.top;
1108
+ if (nextTop != null) {
1109
+ const delta = nextTop - anchor.layout.top;
1110
+ if (Math.abs(delta) > 1) container.scrollTop += delta;
1111
+ }
1112
+ }
1113
+ }
1114
+
1115
+ // Record positions after any scroll correction so the next insertion compares
1116
+ // against what the user actually saw.
1117
+ evidenceCardLayoutRef.current = captureEvidenceCardLayout(container);
1118
+ }, [evidenceLayoutRevision, currentAssessmentIdx, narrowPane]);
1119
+
1120
+ // Disclosure animations and responsive reflow can move cards without changing
1121
+ // the evidence projection. Continuously refresh the baseline after those layout
1122
+ // changes; the layout effect above remains the only place that adjusts scroll.
1123
+ useEffect(() => {
1124
+ const container = evidenceScrollRef.current;
1125
+ const content = evidenceContentRef.current;
1126
+ if (!container || !content || typeof ResizeObserver === "undefined") {
1127
+ return;
1128
+ }
1129
+ let frame: number | undefined;
1130
+ const refreshLayoutBaseline = () => {
1131
+ if (frame !== undefined) cancelAnimationFrame(frame);
1132
+ frame = requestAnimationFrame(() => {
1133
+ frame = undefined;
1134
+ if (container.offsetParent !== null) {
1135
+ // A responsive transition can reveal Findings without changing the
1136
+ // selected narrow-pane tab (for example, maximizing into split view).
1137
+ // Once the evidence is onscreen it is no longer unread.
1138
+ setUnreadEvidence(false);
1139
+ evidenceCardLayoutRef.current = captureEvidenceCardLayout(container);
1140
+ }
1141
+ });
1142
+ };
1143
+ const observer = new ResizeObserver(refreshLayoutBaseline);
1144
+ // Observe the pane itself so crossing the responsive visibility boundary is
1145
+ // detected even when the projected evidence content has not changed size.
1146
+ observer.observe(container);
1147
+ observer.observe(content);
1148
+ for (const card of container.querySelectorAll<HTMLElement>(
1149
+ "[data-evidence-card]",
1150
+ )) {
1151
+ observer.observe(card);
1152
+ }
1153
+ refreshLayoutBaseline();
1154
+ return () => {
1155
+ observer.disconnect();
1156
+ if (frame !== undefined) cancelAnimationFrame(frame);
1157
+ };
1158
+ }, [evidenceLayoutRevision, narrowPane, maximized]);
1159
+
1160
+ const focusAfterPaneSwitch = useCallback(
1161
+ (domId: string, evidence: boolean) => {
1162
+ requestAnimationFrame(() => {
1163
+ requestAnimationFrame(() => {
1164
+ const marker = document.getElementById(domId);
1165
+ const target = evidence
1166
+ ? ((marker?.closest(
1167
+ "[data-evidence-card], [data-evidence-source-container]",
1168
+ ) as HTMLElement | null) ?? marker)
1169
+ : marker;
1170
+ const container = evidence
1171
+ ? evidenceScrollRef.current
1172
+ : scrollRef.current;
1173
+ if (target && container) {
1174
+ const containerRect = container.getBoundingClientRect();
1175
+ const targetRect = target.getBoundingClientRect();
1176
+ container.scrollTo({
1177
+ top: investigationPaneCenteredScrollTop({
1178
+ scrollTop: container.scrollTop,
1179
+ viewportHeight: container.clientHeight,
1180
+ contentHeight: container.scrollHeight,
1181
+ targetTop: targetRect.top - containerRect.top,
1182
+ targetHeight: targetRect.height,
1183
+ }),
1184
+ behavior: prefersReducedMotion() ? "auto" : "smooth",
1185
+ });
1186
+ }
1187
+ target?.focus({ preventScroll: true });
1188
+ });
1189
+ });
1190
+ },
1191
+ [],
1192
+ );
1193
+ const viewEvidenceSource = useCallback((sourceId: string) => {
1194
+ paneSelectionTouchedRef.current = true;
1195
+ setNarrowPane("evidence");
1196
+ setUnreadEvidence(false);
1197
+ // This path reveals the exact changed source, so the broader scrolled-away
1198
+ // cue has served its purpose even when the centered card remains below 80px.
1199
+ setEvidenceUpdateAvailable(false);
1200
+ latestEvidenceUpdateSourceIdRef.current = undefined;
1201
+ evidenceRevealRequestIdRef.current += 1;
1202
+ setEvidenceRevealRequest({
1203
+ sourceId,
1204
+ requestId: evidenceRevealRequestIdRef.current,
1205
+ });
1206
+ }, []);
1207
+ const revealEvidenceSource = useCallback(
1208
+ (sourceId: string) => {
1209
+ focusAfterPaneSwitch(investigationEvidenceSourceDomId(sourceId), true);
1210
+ },
1211
+ [focusAfterPaneSwitch],
1212
+ );
1213
+ const viewActivitySource = useCallback(
1214
+ (sourceId: string, excerpt?: InvestigationSourceExcerpt) => {
1215
+ paneSelectionTouchedRef.current = true;
1216
+ activityRevealRequestIdRef.current += 1;
1217
+ setActivityRevealRequest({
1218
+ sourceId,
1219
+ excerpt,
1220
+ requestId: activityRevealRequestIdRef.current,
1221
+ });
1222
+ setNarrowPane("activity");
1223
+ window.setTimeout(
1224
+ () =>
1225
+ focusAfterPaneSwitch(
1226
+ investigationActivitySourceDomId(sourceId),
1227
+ false,
1228
+ ),
1229
+ investigationDisclosureSettleDelay(prefersReducedMotion()),
1230
+ );
1231
+ },
1232
+ [focusAfterPaneSwitch],
1233
+ );
1234
+
1235
+ const verificationRunning = turns.some(
1236
+ (turn) => turn.verify && turn.status === "running",
1237
+ );
1238
+ const toolCallCount = turns.reduce(
1239
+ (count, turn) =>
1240
+ count + turn.timeline.filter((item) => item.kind === "tool").length,
1241
+ 0,
1242
+ );
1243
+ const latestVerification = [...turns].reverse().find((turn) => turn.verify);
1244
+ const displayedVerificationError =
1245
+ latestVerification?.status === "error"
1246
+ ? latestVerification.error || "The verification could not be completed."
1247
+ : verificationError;
1248
+ const displayedStatusCheckError =
1249
+ displayedVerificationError || applyOutcomeUncertain;
1250
+ const currentKeyFindingCount = projection.groups.filter(
1251
+ (group) => !group.historical && group.latest.tier === "key",
1252
+ ).length;
1253
+ const findingsTabAccessibleLabel =
1254
+ "Findings: current assessment, Radar evidence, and next steps";
1255
+ const currentAssessmentCoverageLimited = investigationEvidenceCoverageLimited(
1256
+ currentAssessmentProjection,
1257
+ );
1258
+ const currentAssessmentEvidenceConflict =
1259
+ currentAssessment?.diagnosis?.healthy === true &&
1260
+ investigationEvidenceConflictsWithHealthy(projection);
1261
+ const hasEvidenceCollectedAfterAssessment =
1262
+ currentAssessmentIdx >= 0 &&
1263
+ projection.sources.some(
1264
+ (source) => source.turnIndex > currentAssessmentIdx,
1265
+ );
1266
+ const assessmentNeedsCurrentStateVerification =
1267
+ investigationAssessmentNeedsCurrentStateVerification({
1268
+ currentAssessmentIdx,
1269
+ lastApplyAttemptIdx,
1270
+ lastApplyOutcome,
1271
+ localApplyAttemptAssessmentIdx,
1272
+ });
1273
+ const hasNextSteps = Boolean(
1274
+ currentAssessment?.diagnosis &&
1275
+ (currentAssessment.diagnosis.remediation?.length ?? 0) > 0,
1276
+ );
1277
+ const earlierPlan =
1278
+ assessmentNeedsCurrentStateVerification ||
1279
+ hasEvidenceCollectedAfterAssessment;
1280
+ const showSplitWorkspace = maximized;
1281
+ const splitGridClass = showSplitWorkspace
1282
+ ? "@min-[1000px]/investigation:grid-cols-[minmax(360px,520px)_minmax(0,1fr)]"
1283
+ : "";
1284
+ const splitTabClass = showSplitWorkspace
1285
+ ? "@min-[1000px]/investigation:hidden"
1286
+ : "";
1287
+ const splitPaneClass = showSplitWorkspace
1288
+ ? "@min-[1000px]/investigation:flex"
1289
+ : "";
1290
+ const splitActivityBorderClass = showSplitWorkspace
1291
+ ? "@min-[1000px]/investigation:border-r @min-[1000px]/investigation:border-theme-border"
1292
+ : "";
1293
+
1294
+ const selectPane = (pane: "activity" | "evidence") => {
1295
+ paneSelectionTouchedRef.current = true;
1296
+ setNarrowPane(pane);
1297
+ if (pane === "evidence") {
1298
+ setUnreadEvidence(false);
1299
+ }
1300
+ };
1301
+ const viewActivity = () => {
1302
+ paneSelectionTouchedRef.current = true;
1303
+ setNarrowPane("activity");
1304
+ requestAnimationFrame(() => {
1305
+ scrollRef.current?.scrollTo({ top: 0 });
1306
+ document.getElementById(activityPaneId)?.focus({ preventScroll: true });
1307
+ });
1308
+ };
1309
+ const onTabKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {
1310
+ let pane: "activity" | "evidence" | undefined;
1311
+ if (event.key === "ArrowLeft" || event.key === "Home") pane = "activity";
1312
+ if (event.key === "ArrowRight" || event.key === "End") pane = "evidence";
1313
+ if (!pane) return;
1314
+ event.preventDefault();
1315
+ selectPane(pane);
1316
+ document
1317
+ .getElementById(pane === "activity" ? activityTabId : findingsTabId)
1318
+ ?.focus();
1319
+ };
1320
+ const revealLatestEvidenceUpdate = () => {
1321
+ const sourceId = latestEvidenceUpdateSourceIdRef.current;
1322
+ const source = projection.sources.find((item) => item.id === sourceId);
1323
+ if (
1324
+ source &&
1325
+ evidenceStepIdsByTurn.get(source.turnIndex)?.has(source.stepId)
1326
+ ) {
1327
+ viewEvidenceSource(source.id);
1328
+ return;
1329
+ }
1330
+ evidenceScrollRef.current?.scrollTo({
1331
+ top: 0,
1332
+ behavior: prefersReducedMotion() ? "auto" : "smooth",
1333
+ });
1334
+ setEvidenceUpdateAvailable(false);
1335
+ };
1336
+
1337
+ const composer = !unavailable ? (
1338
+ <div className="shrink-0 border-t border-theme-border px-3 py-2.5">
1339
+ {canStop ? (
1340
+ <button
1341
+ type="button"
1342
+ onClick={stop}
1343
+ className="w-full rounded-lg border border-theme-border py-1.5 text-sm text-theme-text-secondary hover:bg-theme-hover"
519
1344
  >
520
- <div className={maximized ? "mx-auto max-w-3xl" : ""}>
521
- <div className="space-y-4">
522
- {stale && (
523
- <div className="rounded-lg border border-amber-500/40 bg-amber-500/10 p-3 text-xs text-theme-text-secondary">
524
- <div className="flex items-start gap-2">
525
- <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-amber-500" />
526
- <span>
527
- This investigation ran against{" "}
528
- <span className="font-medium text-theme-text-primary">
529
- {run.context || "a different cluster"}
530
- </span>
531
- . The cluster context has changed — it's read-only now.
532
- </span>
533
- </div>
534
- <button
535
- onClick={retryDiagnosis}
536
- className="mt-2 inline-flex items-center gap-1.5 rounded-md border border-amber-500/50 px-2.5 py-1 font-medium text-amber-600 hover:bg-amber-500/10 dark:text-amber-400"
537
- >
538
- <Send className="h-3 w-3" />
539
- Re-run on current cluster
540
- </button>
541
- </div>
542
- )}
543
- {gone && turns.length === 0 && (
544
- <div className="rounded-lg border border-theme-border bg-theme-elevated p-3 text-sm text-theme-text-secondary">
545
- <div className="flex items-start gap-2">
546
- <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-amber-500" />
547
- <span>
548
- This investigation is unavailable. It may be private, your
549
- access may have changed, or its history may have been
550
- cleared. Check your account and organization, or ask the
551
- creator for access.
552
- </span>
553
- </div>
1345
+ Stop agent
1346
+ </button>
1347
+ ) : run.status === "stopping" ? (
1348
+ <div className="px-3 py-2 text-xs text-theme-text-secondary">
1349
+ Stopping investigation…
1350
+ </div>
1351
+ ) : !canContinue ? (
1352
+ <div className="px-3 py-2 text-xs text-theme-text-secondary">
1353
+ {run.trigger === "background" ? (
1354
+ <>
1355
+ <p>Automatic investigation · read-only.</p>
1356
+ {canInvestigateFurther(run, gone) ? (
1357
+ <div className="mt-2 flex flex-wrap items-center gap-2">
554
1358
  <button
555
- onClick={retryDiagnosis}
556
- className="mt-2 inline-flex items-center gap-1.5 rounded-md border border-theme-border px-2.5 py-1 font-medium text-theme-text-primary hover:bg-theme-hover"
1359
+ className="btn-brand px-3 py-2 text-xs"
1360
+ onClick={() =>
1361
+ openInvestigation({
1362
+ kind,
1363
+ group: run.group,
1364
+ namespace,
1365
+ name,
1366
+ issueId: run.issueId,
1367
+ })
1368
+ }
557
1369
  >
558
- <Send className="h-3 w-3" />
559
- Re-run Diagnose
1370
+ Investigate further
560
1371
  </button>
1372
+ <span>
1373
+ Re-checks current state, including recent automatic findings
1374
+ when available.
1375
+ </span>
561
1376
  </div>
1377
+ ) : (
1378
+ <p>
1379
+ Start a new investigation on this resource to continue
1380
+ digging.
1381
+ </p>
562
1382
  )}
563
- <RunContextCard run={run} />
564
- {turns.map((t, i) => {
565
- const isLast = i === turns.length - 1;
566
- // Hosted runners are read-only — the server refuses apply turns.
567
- const canApply = i === lastRemediationIdx && !stale && !hosted;
568
- const canCheck = isLast && t.status === "done" && !!t.apply;
569
- return (
570
- <TurnView
571
- key={i}
572
- turn={t}
573
- synthLabel={isLast ? synth : null}
574
- reveal={isLast ? (reveal ?? "full") : "full"}
575
- onApply={canApply ? requestApply : undefined}
576
- onAsk={
577
- isLast && !busy && canContinue ? askFollowup : undefined
578
- }
579
- onCheckStatus={canCheck ? checkStatus : undefined}
580
- onRetryDiagnosis={
581
- isLast &&
582
- t.status === "error" &&
583
- !t.question &&
584
- !t.apply &&
585
- !stale
586
- ? retryDiagnosis
587
- : undefined
588
- }
589
- hideVerdict={pinned && i === pinnedIdx}
590
- />
591
- );
592
- })}
593
- {actionError && (
594
- <div className="flex items-start gap-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-theme-text-primary">
595
- <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-red-400" />
596
- <span>{actionError}</span>
597
- </div>
598
- )}
599
- {/* A start that failed belongs to the investigation that never began,
600
- not to the one on screen. Unlabelled at the foot of a finished
601
- transcript — verdict directly above — it reads as "this
602
- investigation failed". It also outlives the click that caused it,
603
- and this is the only place it surfaces while a run is focused, so
604
- it needs a way out. */}
605
- {startError && (
606
- <div
607
- role="alert"
608
- className="flex items-start gap-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-theme-text-primary"
609
- >
610
- <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-red-400" />
611
- <div className="min-w-0 flex-1">
612
- <div className="font-medium">
613
- Couldn&apos;t start a new investigation
1383
+ </>
1384
+ ) : (
1385
+ "This investigation is read-only."
1386
+ )}
1387
+ </div>
1388
+ ) : (
1389
+ <div className="flex items-end gap-2">
1390
+ <textarea
1391
+ value={input}
1392
+ onChange={(event) => setInput(event.target.value)}
1393
+ onKeyDown={(event) => {
1394
+ if (event.key === "Enter" && !event.shiftKey) {
1395
+ event.preventDefault();
1396
+ submitFollowup();
1397
+ }
1398
+ }}
1399
+ rows={1}
1400
+ disabled={
1401
+ !streamReady ||
1402
+ readOnly ||
1403
+ busy ||
1404
+ requestPending ||
1405
+ verificationPending
1406
+ }
1407
+ placeholder={
1408
+ !streamReady
1409
+ ? historyUnavailablePresentation?.loading
1410
+ ? "Retrying investigation history…"
1411
+ : historyUnavailablePresentation
1412
+ ? "Investigation history unavailable"
1413
+ : "Loading investigation history…"
1414
+ : verificationPending
1415
+ ? "Waiting to verify the applied change…"
1416
+ : requestPending
1417
+ ? "Agent is working…"
1418
+ : "Ask a follow-up or refine…"
1419
+ }
1420
+ className="max-h-32 min-h-[38px] flex-1 resize-none rounded-lg border border-theme-border bg-theme-base px-3 py-2 text-sm text-theme-text-primary placeholder:text-theme-text-tertiary focus:border-accent focus:outline-none disabled:opacity-50"
1421
+ />
1422
+ <button
1423
+ type="button"
1424
+ onClick={submitFollowup}
1425
+ disabled={!input.trim() || interactionsBlocked}
1426
+ className="shrink-0 rounded-lg btn-brand p-2 disabled:opacity-40"
1427
+ aria-label="Send follow-up"
1428
+ >
1429
+ <Send className="h-4 w-4" />
1430
+ </button>
1431
+ </div>
1432
+ )}
1433
+ </div>
1434
+ ) : null;
1435
+
1436
+ return (
1437
+ <div
1438
+ data-investigation-workspace
1439
+ className={`@container/investigation relative flex min-h-0 flex-1 flex-col bg-theme-surface ${maximized ? "investigation-split-enabled" : ""}`}
1440
+ >
1441
+ {stale ? (
1442
+ <div className="flex items-center gap-2 border-b border-amber-500/35 bg-amber-500/10 px-3 py-2 text-xs text-theme-text-secondary">
1443
+ <AlertTriangle className="h-4 w-4 shrink-0 text-amber-500" />
1444
+ <span className="min-w-0 flex-1">
1445
+ This investigation ran on{" "}
1446
+ <span className="font-medium text-theme-text-primary">
1447
+ {parseContextName(run.context).clusterName}
1448
+ </span>
1449
+ . It is read-only because its agent session was closed after a
1450
+ cluster switch.
1451
+ </span>
1452
+ </div>
1453
+ ) : null}
1454
+ {!stale && gone ? (
1455
+ <div className="flex items-center gap-2 border-b border-theme-border bg-theme-elevated px-3 py-2 text-xs text-theme-text-secondary">
1456
+ <AlertTriangle className="h-4 w-4 shrink-0 text-amber-500" />
1457
+ <span className="min-w-0 flex-1">
1458
+ This investigation is closed and read-only.{" "}
1459
+ {turns.length > 0
1460
+ ? "Evidence already loaded in this view is preserved, but Radar can no longer continue the run."
1461
+ : "It may be private, your access may have changed, or its history may have been cleared. Check your account and organization, or ask the creator for access."}
1462
+ </span>
1463
+ <button
1464
+ type="button"
1465
+ onClick={retryDiagnosis}
1466
+ className="shrink-0 rounded-md border border-theme-border px-2 py-1 font-medium text-theme-text-primary hover:bg-theme-hover"
1467
+ >
1468
+ Investigate again
1469
+ </button>
1470
+ </div>
1471
+ ) : null}
1472
+ {!stale && !gone && endedEarly ? (
1473
+ <div
1474
+ role="status"
1475
+ className="flex items-center gap-2 border-b border-theme-border bg-theme-elevated px-3 py-2 text-xs text-theme-text-secondary"
1476
+ >
1477
+ <AlertTriangle className="h-4 w-4 shrink-0 text-amber-500" />
1478
+ <span className="min-w-0 flex-1">
1479
+ {run.status === "stopped"
1480
+ ? "This investigation was stopped before it reached a conclusion."
1481
+ : "This investigation ended before it reached a complete conclusion."}
1482
+ <span className="ml-1">
1483
+ Evidence collected so far is preserved. See Activity for the final
1484
+ error or stopped state.
1485
+ </span>
1486
+ </span>
1487
+ <button
1488
+ type="button"
1489
+ onClick={retryDiagnosis}
1490
+ className="shrink-0 rounded-md border border-theme-border px-2 py-1 font-medium text-theme-text-primary hover:bg-theme-hover"
1491
+ >
1492
+ Investigate again
1493
+ </button>
1494
+ </div>
1495
+ ) : null}
1496
+
1497
+ {startError ? (
1498
+ <InvestigationStartErrorAlert
1499
+ error={startError}
1500
+ onDismiss={dismissError}
1501
+ />
1502
+ ) : null}
1503
+
1504
+ {!gone && historyUnavailablePresentation ? (
1505
+ <div
1506
+ role={historyUnavailablePresentation.loading ? "status" : "alert"}
1507
+ className={`flex items-start gap-2 border-b px-3 py-2.5 text-xs text-theme-text-secondary ${
1508
+ historyUnavailablePresentation.loading
1509
+ ? "border-amber-500/35 bg-amber-500/10"
1510
+ : "border-red-500/30 bg-red-500/10"
1511
+ }`}
1512
+ >
1513
+ {historyUnavailablePresentation.loading ? (
1514
+ <Loader2 className="mt-0.5 h-4 w-4 shrink-0 animate-spin text-amber-500" />
1515
+ ) : (
1516
+ <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-red-400" />
1517
+ )}
1518
+ <span className="min-w-0">
1519
+ <span className="block font-medium text-theme-text-primary">
1520
+ {historyUnavailablePresentation.title}
1521
+ </span>
1522
+ <span>{historyUnavailablePresentation.detail}</span>
1523
+ </span>
1524
+ </div>
1525
+ ) : null}
1526
+
1527
+ <div
1528
+ role="group"
1529
+ aria-label="Investigation workspace"
1530
+ className={`grid grid-cols-2 border-b border-theme-border bg-theme-base/40 p-1 ${splitTabClass}`}
1531
+ >
1532
+ <button
1533
+ type="button"
1534
+ id={activityTabId}
1535
+ aria-controls={activityPaneId}
1536
+ aria-pressed={narrowPane === "activity"}
1537
+ aria-label="Activity: agent reasoning and tool calls"
1538
+ onKeyDown={onTabKeyDown}
1539
+ onClick={() => selectPane("activity")}
1540
+ className={`flex items-center justify-center gap-1.5 rounded-md px-2 py-1.5 text-xs font-medium ${
1541
+ narrowPane === "activity"
1542
+ ? "selection-strong selection-text selection-ring"
1543
+ : "text-theme-text-secondary hover:bg-theme-hover"
1544
+ }`}
1545
+ >
1546
+ <Activity className="h-3.5 w-3.5" />
1547
+ Activity
1548
+ </button>
1549
+ <button
1550
+ type="button"
1551
+ id={findingsTabId}
1552
+ aria-controls={findingsPaneId}
1553
+ aria-pressed={narrowPane === "evidence"}
1554
+ aria-label={findingsTabAccessibleLabel}
1555
+ onKeyDown={onTabKeyDown}
1556
+ onClick={() => selectPane("evidence")}
1557
+ className={`relative flex items-center justify-center gap-1.5 rounded-md px-2 py-1.5 text-xs font-medium ${
1558
+ narrowPane === "evidence"
1559
+ ? "selection-strong selection-text selection-ring"
1560
+ : "text-theme-text-secondary hover:bg-theme-hover"
1561
+ }`}
1562
+ >
1563
+ <Files className="h-3.5 w-3.5" />
1564
+ Findings
1565
+ {unreadEvidence && narrowPane !== "evidence" ? (
1566
+ <span className="h-1.5 w-1.5 rounded-full bg-accent" aria-hidden />
1567
+ ) : null}
1568
+ </button>
1569
+ <span className="sr-only" role="status" aria-live="polite">
1570
+ {investigationEvidenceAnnouncement({
1571
+ unreadEvidence,
1572
+ evidenceUpdateAvailable,
1573
+ })}
1574
+ </span>
1575
+ </div>
1576
+
1577
+ <div className={`grid min-h-0 flex-1 ${splitGridClass}`}>
1578
+ <section
1579
+ id={activityPaneId}
1580
+ tabIndex={-1}
1581
+ aria-label="Activity: agent reasoning and tool calls"
1582
+ aria-busy={busy || requestPending || rebuildingReplay}
1583
+ className={`${
1584
+ narrowPane === "activity" ? "flex" : "hidden"
1585
+ } relative min-h-0 min-w-0 flex-col outline-none ${splitPaneClass} ${splitActivityBorderClass}`}
1586
+ >
1587
+ <div
1588
+ className={`hidden items-center justify-between border-b border-theme-border/60 px-3 py-2 ${splitPaneClass}`}
1589
+ >
1590
+ <div className="flex min-w-0 items-center gap-2">
1591
+ <Activity className="h-4 w-4 shrink-0 text-theme-text-tertiary" />
1592
+ <div className="min-w-0">
1593
+ <h2 className="truncate text-sm font-semibold text-theme-text-primary">
1594
+ Activity
1595
+ </h2>
1596
+ <p className="truncate text-[11px] text-theme-text-tertiary">
1597
+ Agent reasoning and tool calls
1598
+ </p>
1599
+ </div>
1600
+ </div>
1601
+ <span className="inline-flex items-center gap-1.5 text-[11px] text-theme-text-tertiary">
1602
+ {toolCallCount > 0
1603
+ ? `${toolCallCount} ${toolCallCount === 1 ? "tool call" : "tool calls"}`
1604
+ : turns.length > 0
1605
+ ? `${turns.length} ${turns.length === 1 ? "turn" : "turns"}`
1606
+ : null}
1607
+ </span>
1608
+ </div>
1609
+ <div className="relative flex min-h-0 flex-1 flex-col">
1610
+ <div
1611
+ ref={scrollRef}
1612
+ data-investigation-activity-scroll
1613
+ onScroll={onScroll}
1614
+ className="min-h-0 flex-1 overflow-y-auto overflow-x-hidden px-3 py-3 [scrollbar-gutter:stable]"
1615
+ >
1616
+ <div>
1617
+ <div className="space-y-4">
1618
+ {turns.length === 0 && !gone ? (
1619
+ <div className="flex min-h-36 flex-col items-center justify-center rounded-lg border border-dashed border-theme-border px-4 text-center">
1620
+ {historyUnavailablePresentation &&
1621
+ !historyUnavailablePresentation.loading ? (
1622
+ <AlertTriangle className="h-5 w-5 text-red-400" />
1623
+ ) : !streamReady || busy ? (
1624
+ <Loader2 className="h-5 w-5 animate-spin text-accent" />
1625
+ ) : (
1626
+ <Activity className="h-5 w-5 text-theme-text-tertiary" />
1627
+ )}
1628
+ <p className="mt-2 text-sm font-medium text-theme-text-secondary">
1629
+ {!streamReady
1630
+ ? historyUnavailablePresentation?.loading
1631
+ ? "Retrying saved activity"
1632
+ : historyUnavailablePresentation
1633
+ ? "Saved activity unavailable"
1634
+ : "Loading saved activity"
1635
+ : busy
1636
+ ? "Starting the investigation"
1637
+ : "No activity recorded"}
1638
+ </p>
1639
+ <p className="mt-1 text-xs text-theme-text-tertiary">
1640
+ {!streamReady
1641
+ ? historyUnavailablePresentation?.loading
1642
+ ? "Radar will continue when saved history is available."
1643
+ : historyUnavailablePresentation
1644
+ ? "Radar could not restore this run from saved history."
1645
+ : "Restoring this run from saved history."
1646
+ : busy
1647
+ ? "Reasoning and tool activity will appear here."
1648
+ : "No saved activity is available for this run."}
1649
+ </p>
614
1650
  </div>
615
- <div className="text-theme-text-secondary">
616
- {startError}
1651
+ ) : null}
1652
+ {turns.map((turn, index) => {
1653
+ const isLast = index === turns.length - 1;
1654
+ const verifiedHealthy =
1655
+ turn.verify &&
1656
+ turn.diagnosis?.healthy === true &&
1657
+ !currentAssessmentCoverageLimited &&
1658
+ !currentAssessmentEvidenceConflict;
1659
+ const canCheck =
1660
+ isLast &&
1661
+ (turn.status === "done" || turn.status === "error") &&
1662
+ !!turn.apply &&
1663
+ !readOnly;
1664
+ return (
1665
+ <Fragment key={index}>
1666
+ <TurnView
1667
+ turn={turn}
1668
+ turnIndex={index}
1669
+ evidenceStepIds={evidenceStepIdsByTurn.get(index)}
1670
+ onViewEvidence={viewEvidenceSource}
1671
+ sourceRevealRequest={activityRevealRequest}
1672
+ onViewExplanation={
1673
+ turn.explainAssessment
1674
+ ? () => viewExplanation(turn.explainAssessment!)
1675
+ : undefined
1676
+ }
1677
+ onCheckStatus={
1678
+ canCheck && !interactionsBlocked
1679
+ ? checkStatus
1680
+ : undefined
1681
+ }
1682
+ onRetryDiagnosis={
1683
+ isLast &&
1684
+ turn.status === "error" &&
1685
+ !turn.question &&
1686
+ !turn.apply &&
1687
+ !stale
1688
+ ? retryDiagnosis
1689
+ : undefined
1690
+ }
1691
+ hideConclusion={assessmentIndexes.includes(index)}
1692
+ />
1693
+ {showSplitWorkspace &&
1694
+ index === currentAssessmentIdx &&
1695
+ turn.timeline.length === 0 ? (
1696
+ <p className="hidden text-sm text-theme-text-tertiary @min-[1000px]/investigation:block">
1697
+ No reasoning or tool activity was recorded for this
1698
+ assessment. See Findings.
1699
+ </p>
1700
+ ) : null}
1701
+ {index === currentAssessmentIdx ? (
1702
+ <button
1703
+ type="button"
1704
+ onClick={() => selectPane("evidence")}
1705
+ className={`group flex w-full items-center gap-2.5 rounded-lg border px-3 py-2.5 text-left transition-colors ${
1706
+ verifiedHealthy
1707
+ ? "border-emerald-500/30 bg-emerald-500/5 hover:bg-emerald-500/10"
1708
+ : "border-accent/30 bg-accent/5 hover:bg-accent/10"
1709
+ } ${splitTabClass}`}
1710
+ >
1711
+ <span
1712
+ className={`flex h-7 w-7 shrink-0 items-center justify-center rounded-full ${
1713
+ verifiedHealthy
1714
+ ? "bg-emerald-500/15 text-emerald-500"
1715
+ : "bg-accent/10 text-accent-text"
1716
+ }`}
1717
+ >
1718
+ {verifiedHealthy ? (
1719
+ <CheckCircle2 className="h-4 w-4" />
1720
+ ) : (
1721
+ <Files className="h-4 w-4" />
1722
+ )}
1723
+ </span>
1724
+ <span className="min-w-0 flex-1">
1725
+ <span className="block text-xs font-semibold text-theme-text-primary">
1726
+ {turn.verify
1727
+ ? "Verification complete"
1728
+ : "Assessment ready"}
1729
+ </span>
1730
+ <span className="block text-[11px] text-theme-text-tertiary">
1731
+ {currentKeyFindingCount > 0
1732
+ ? `${currentKeyFindingCount} ${currentKeyFindingCount === 1 ? "key finding" : "key findings"} ready to review`
1733
+ : "Findings compiled from Radar results"}
1734
+ </span>
1735
+ </span>
1736
+ <span className="inline-flex shrink-0 items-center gap-1 text-xs font-medium text-accent-text">
1737
+ View Findings
1738
+ <ArrowRight className="h-3.5 w-3.5 transition-transform group-hover:translate-x-0.5" />
1739
+ </span>
1740
+ </button>
1741
+ ) : null}
1742
+ </Fragment>
1743
+ );
1744
+ })}
1745
+ {(actionError || displayedStatusCheckError) && (
1746
+ <div className="flex items-start gap-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-theme-text-primary">
1747
+ <AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-red-400" />
1748
+ <div className="min-w-0 flex-1">
1749
+ <span>{displayedStatusCheckError || actionError}</span>
1750
+ {displayedStatusCheckError ? (
1751
+ <button
1752
+ type="button"
1753
+ onClick={checkStatus}
1754
+ disabled={interactionsBlocked}
1755
+ className="mt-2 block rounded-md border border-red-500/30 px-2 py-1 text-xs font-medium text-theme-text-primary hover:bg-red-500/10 disabled:opacity-50"
1756
+ >
1757
+ {applyOutcomeUncertain &&
1758
+ !displayedVerificationError
1759
+ ? "Check current status"
1760
+ : "Check current status again"}
1761
+ </button>
1762
+ ) : null}
1763
+ </div>
617
1764
  </div>
618
- </div>
619
- <button
620
- onClick={dismissError}
621
- className="shrink-0 rounded px-1.5 py-0.5 text-xs text-theme-text-tertiary hover:bg-theme-hover hover:text-theme-text-primary"
622
- >
623
- Dismiss
624
- </button>
1765
+ )}
625
1766
  </div>
626
- )}
1767
+ </div>
627
1768
  </div>
1769
+ {showJump ? (
1770
+ <button
1771
+ type="button"
1772
+ onClick={jumpToBottom}
1773
+ className="absolute bottom-3 left-1/2 z-10 flex -translate-x-1/2 items-center gap-1.5 rounded-full border border-theme-border bg-theme-elevated px-3 py-1.5 text-xs font-medium text-theme-text-secondary shadow-theme-md hover:bg-theme-hover hover:text-theme-text-primary"
1774
+ >
1775
+ <ArrowDown className="h-3.5 w-3.5" />
1776
+ {busy ? "Jump to latest" : "Scroll to bottom"}
1777
+ </button>
1778
+ ) : null}
628
1779
  </div>
629
- </div>
630
- {pinned && (
631
- <aside
632
- className={`w-[400px] shrink-0 overflow-y-auto border-l border-theme-border px-4 py-3 ${busy ? "opacity-70" : ""}`}
1780
+ {composer}
1781
+ </section>
1782
+ <section
1783
+ id={findingsPaneId}
1784
+ aria-label="Findings: current assessment, Radar evidence, and next steps"
1785
+ aria-busy={
1786
+ busy || requestPending || verificationPending || rebuildingReplay
1787
+ }
1788
+ className={`${
1789
+ narrowPane === "evidence" ? "flex" : "hidden"
1790
+ } relative min-h-0 min-w-0 flex-col ${splitPaneClass}`}
1791
+ >
1792
+ <div
1793
+ className={`hidden items-center justify-between gap-3 border-b border-theme-border/60 px-3 py-2 ${splitPaneClass}`}
633
1794
  >
634
- <div className="mb-1 text-[11px] font-medium uppercase tracking-wide text-theme-text-tertiary">
635
- {busy ? "Verdict · revising…" : "Verdict"}
1795
+ <div className="flex min-w-0 items-center gap-2">
1796
+ <Files className="h-4 w-4 shrink-0 text-theme-text-tertiary" />
1797
+ <div className="min-w-0">
1798
+ <h2 className="truncate text-sm font-semibold text-theme-text-primary">
1799
+ Findings
1800
+ </h2>
1801
+ <p className="truncate text-[11px] text-theme-text-tertiary">
1802
+ Current assessment, evidence, and next steps
1803
+ </p>
1804
+ </div>
636
1805
  </div>
637
- <ResultCard
638
- diagnosis={turns[pinnedIdx].diagnosis!}
639
- onApply={
640
- pinnedIdx === lastRemediationIdx && !stale && !hosted
641
- ? requestApply
642
- : undefined
1806
+ <div className="flex shrink-0 items-center gap-2">
1807
+ {evidenceUpdateAvailable ? (
1808
+ <button
1809
+ type="button"
1810
+ onClick={revealLatestEvidenceUpdate}
1811
+ className="inline-flex items-center gap-1 rounded-full border border-accent/30 bg-accent/5 px-2 py-1 text-[11px] font-medium text-accent-text hover:bg-accent/10"
1812
+ >
1813
+ <Files className="h-3 w-3" />
1814
+ See new evidence
1815
+ </button>
1816
+ ) : null}
1817
+ </div>
1818
+ </div>
1819
+ {evidenceUpdateAvailable ? (
1820
+ <div className={`absolute right-4 top-2 z-20 ${splitTabClass}`}>
1821
+ <button
1822
+ type="button"
1823
+ onClick={revealLatestEvidenceUpdate}
1824
+ className="inline-flex items-center gap-1 rounded-full border border-accent/30 bg-theme-elevated px-2 py-1 text-[11px] font-medium text-accent-text shadow-theme-sm hover:bg-theme-hover"
1825
+ >
1826
+ <Files className="h-3 w-3" />
1827
+ See new evidence
1828
+ </button>
1829
+ </div>
1830
+ ) : null}
1831
+ <div
1832
+ ref={evidenceScrollRef}
1833
+ data-investigation-findings-scroll
1834
+ onScroll={(event) => {
1835
+ if (event.currentTarget.offsetParent === null) return;
1836
+ evidenceScrollTopRef.current = event.currentTarget.scrollTop;
1837
+ if (event.currentTarget.scrollTop <= 40) {
1838
+ setEvidenceUpdateAvailable(false);
1839
+ latestEvidenceUpdateSourceIdRef.current = undefined;
643
1840
  }
644
- onAsk={!busy && canContinue ? askFollowup : undefined}
645
- reveal="full"
646
- />
647
- </aside>
648
- )}
649
- </div>
1841
+ }}
1842
+ className="min-h-0 flex-1 overflow-y-auto overflow-x-hidden px-3 py-3 [overflow-anchor:none] [scrollbar-gutter:stable]"
1843
+ >
1844
+ <div ref={evidenceContentRef} className="min-w-0 space-y-6">
1845
+ {rebuildingReplay ? (
1846
+ <div className="flex min-h-28 flex-col items-center justify-center rounded-lg border border-dashed border-theme-border px-4 text-center">
1847
+ {historyUnavailablePresentation &&
1848
+ !historyUnavailablePresentation.loading ? (
1849
+ <AlertTriangle className="h-5 w-5 text-red-400" />
1850
+ ) : (
1851
+ <Loader2 className="h-5 w-5 animate-spin text-accent" />
1852
+ )}
1853
+ <p className="mt-2 text-sm font-medium text-theme-text-secondary">
1854
+ {historyUnavailablePresentation?.loading
1855
+ ? "Retrying saved evidence"
1856
+ : historyUnavailablePresentation
1857
+ ? "Saved evidence unavailable"
1858
+ : "Loading saved evidence"}
1859
+ </p>
1860
+ <p className="mt-1 text-xs text-theme-text-tertiary">
1861
+ {historyUnavailablePresentation?.loading
1862
+ ? "Radar will continue when saved history is available."
1863
+ : historyUnavailablePresentation
1864
+ ? "Radar could not restore evidence from saved history."
1865
+ : "Restoring the assessment and evidence from saved history."}
1866
+ </p>
1867
+ </div>
1868
+ ) : (
1869
+ <>
1870
+ <section
1871
+ aria-labelledby={`${workspaceId}-assessment-heading`}
1872
+ className="investigation-assessment rounded-xl border p-4"
1873
+ >
1874
+ <div className="flex flex-wrap items-center gap-1.5">
1875
+ <h2
1876
+ id={`${workspaceId}-assessment-heading`}
1877
+ className="text-lg font-semibold text-theme-text-primary"
1878
+ >
1879
+ {assessmentNeedsCurrentStateVerification
1880
+ ? "Assessment before apply"
1881
+ : hasEvidenceCollectedAfterAssessment
1882
+ ? "Earlier assessment"
1883
+ : !currentAssessment
1884
+ ? "Assessment"
1885
+ : currentAssessment.verify
1886
+ ? "Verification result"
1887
+ : currentAssessmentIdx ===
1888
+ initialAssessmentIdx &&
1889
+ hasMultipleAssessments
1890
+ ? "Initial assessment"
1891
+ : "Assessment"}
1892
+ </h2>
1893
+ {assessmentNeedsCurrentStateVerification ? (
1894
+ <Badge severity="warning" size="sm">
1895
+ Current state unverified
1896
+ </Badge>
1897
+ ) : null}
1898
+ {hasEvidenceCollectedAfterAssessment &&
1899
+ !assessmentNeedsCurrentStateVerification ? (
1900
+ <Badge severity="info" size="sm">
1901
+ Newer evidence below
1902
+ </Badge>
1903
+ ) : null}
1904
+ {verificationRunning || verificationPending ? (
1905
+ <Badge severity="info" size="sm">
1906
+ Verifying…
1907
+ </Badge>
1908
+ ) : null}
1909
+ </div>
1910
+ {assessmentNeedsCurrentStateVerification ? (
1911
+ <p className="mt-0.5 text-xs text-theme-text-tertiary">
1912
+ {verificationRunning || verificationPending
1913
+ ? "This assessment predates the apply attempt. Radar is checking the current state now."
1914
+ : "This assessment predates the apply attempt; cluster state after it has not been verified."}
1915
+ </p>
1916
+ ) : hasEvidenceCollectedAfterAssessment ? (
1917
+ <p className="mt-0.5 text-xs text-theme-text-tertiary">
1918
+ Some evidence below was collected after this assessment.
1919
+ Validate the conclusion against it before acting.
1920
+ </p>
1921
+ ) : null}
1922
+ {currentAssessment?.diagnosis ? (
1923
+ <ResultCard
1924
+ key={
1925
+ currentAssessment.resultSequence ??
1926
+ currentAssessmentIdx
1927
+ }
1928
+ diagnosis={currentAssessment.diagnosis}
1929
+ assessmentSources={
1930
+ rootCauseEvidenceResolution?.links.length ? (
1931
+ <AssessmentSources
1932
+ resolution={rootCauseEvidenceResolution}
1933
+ onViewSource={viewActivitySource}
1934
+ />
1935
+ ) : undefined
1936
+ }
1937
+ assessmentAction={
1938
+ hasNextSteps ? (
1939
+ <button
1940
+ type="button"
1941
+ onClick={() => {
1942
+ const section = nextStepsRef.current;
1943
+ const scroller = evidenceScrollRef.current;
1944
+ if (!section || !scroller) return;
1945
+ section.focus({ preventScroll: true });
1946
+ scroller.scrollTo({
1947
+ top:
1948
+ scroller.scrollTop +
1949
+ section.getBoundingClientRect().top -
1950
+ scroller.getBoundingClientRect().top -
1951
+ 12,
1952
+ behavior: prefersReducedMotion()
1953
+ ? "auto"
1954
+ : "smooth",
1955
+ });
1956
+ }}
1957
+ className="ml-auto rounded-md px-2 py-1 text-xs font-medium text-accent-text hover:bg-theme-hover"
1958
+ >
1959
+ {earlierPlan
1960
+ ? "Earlier proposed steps ↓"
1961
+ : "Next steps ↓"}
1962
+ </button>
1963
+ ) : null
1964
+ }
1965
+ explanation={explanationFor(currentAssessment)}
1966
+ section="conclusion"
1967
+ animate={currentAssessment.animateResult !== false}
1968
+ showDisclaimer={false}
1969
+ coverageLimited={currentAssessmentCoverageLimited}
1970
+ evidenceConflict={currentAssessmentEvidenceConflict}
1971
+ />
1972
+ ) : (
1973
+ <div className="mt-2 flex items-center gap-2 rounded-md bg-theme-surface/60 px-2.5 py-2 text-xs text-theme-text-tertiary">
1974
+ {busy || requestPending ? (
1975
+ <span
1976
+ className="h-1.5 w-1.5 shrink-0 animate-pulse rounded-full bg-accent"
1977
+ aria-hidden
1978
+ />
1979
+ ) : (
1980
+ <Activity
1981
+ className="h-3.5 w-3.5 shrink-0"
1982
+ aria-hidden
1983
+ />
1984
+ )}
1985
+ <span>
1986
+ {busy || requestPending
1987
+ ? "Forming an assessment as evidence arrives…"
1988
+ : "The agent did not provide a final assessment."}
1989
+ </span>
1990
+ </div>
1991
+ )}
1992
+ {displayedStatusCheckError ? (
1993
+ <div className="mt-2 flex items-center gap-2 rounded-lg border border-red-500/30 bg-red-500/5 px-3 py-2 text-xs text-theme-text-secondary">
1994
+ <AlertTriangle className="h-3.5 w-3.5 shrink-0 text-red-400" />
1995
+ <span className="min-w-0 flex-1">
1996
+ {applyOutcomeUncertain && !displayedVerificationError
1997
+ ? displayedStatusCheckError
1998
+ : `Verification did not complete: ${displayedStatusCheckError}`}
1999
+ </span>
2000
+ <button
2001
+ type="button"
2002
+ onClick={checkStatus}
2003
+ disabled={interactionsBlocked}
2004
+ className="shrink-0 rounded-md border border-theme-border px-2 py-1 font-medium text-theme-text-primary hover:bg-theme-hover disabled:opacity-50"
2005
+ >
2006
+ {applyOutcomeUncertain && !displayedVerificationError
2007
+ ? "Check current status"
2008
+ : "Check current status again"}
2009
+ </button>
2010
+ </div>
2011
+ ) : null}
2012
+ </section>
650
2013
 
651
- {showJump && (
652
- <button
653
- onClick={jumpToBottom}
654
- className="absolute bottom-20 left-1/2 z-10 flex -translate-x-1/2 items-center gap-1.5 rounded-full border border-theme-border bg-theme-elevated px-3 py-1.5 text-xs font-medium text-theme-text-secondary shadow-theme-md transition hover:bg-theme-hover hover:text-theme-text-primary"
655
- >
656
- <ArrowDown className="h-3.5 w-3.5" />
657
- {busy ? "Jump to latest" : "Scroll to bottom"}
658
- </button>
659
- )}
2014
+ <AssessmentHistory
2015
+ assessments={assessmentIndexes
2016
+ .filter(
2017
+ (index) =>
2018
+ index !== currentAssessmentIdx &&
2019
+ (index === initialAssessmentIdx ||
2020
+ (turns[index].diagnosis?.remediation?.length ?? 0) >
2021
+ 0 ||
2022
+ turns.some(
2023
+ (turn) =>
2024
+ turn.explainAssessment ===
2025
+ turns[index].resultSequence,
2026
+ )),
2027
+ )
2028
+ .map((index) => ({
2029
+ sequence: turns[index].resultSequence ?? index,
2030
+ initial: index === initialAssessmentIdx,
2031
+ diagnosis: turns[index].diagnosis!,
2032
+ explanation: explanationFor(turns[index]),
2033
+ sources: (() => {
2034
+ const resolution =
2035
+ resolveInvestigationRootCauseEvidence(
2036
+ projection,
2037
+ turns[index].diagnosis!.rootCauseEvidence,
2038
+ index,
2039
+ );
2040
+ return resolution.links.length ? (
2041
+ <AssessmentSources
2042
+ resolution={resolution}
2043
+ onViewSource={viewActivitySource}
2044
+ />
2045
+ ) : undefined;
2046
+ })(),
2047
+ }))}
2048
+ />
2049
+
2050
+ <InvestigationEvidencePane
2051
+ projection={projection}
2052
+ rootCauseEvidence={rootCauseEvidenceResolution}
2053
+ collecting={
2054
+ explanationRequest?.status !== "running" &&
2055
+ (requestPending ||
2056
+ (busy &&
2057
+ (!lastTurn?.explainAssessment ||
2058
+ lastTurn.timeline.some(
2059
+ (item) => item.kind === "tool",
2060
+ ))))
2061
+ }
2062
+ animateGroupIds={animateEvidenceGroupIds}
2063
+ onViewSource={viewActivitySource}
2064
+ onViewActivity={viewActivity}
2065
+ onOpenResource={stale ? undefined : onOpenResource}
2066
+ revealRequest={evidenceRevealRequest}
2067
+ onRevealReady={revealEvidenceSource}
2068
+ afterEvidence={
2069
+ hasNextSteps && currentAssessment?.diagnosis ? (
2070
+ <section
2071
+ ref={nextStepsRef}
2072
+ tabIndex={-1}
2073
+ aria-labelledby={`${workspaceId}-next-steps`}
2074
+ className="investigation-next-steps rounded-xl border p-4 outline-none focus-visible:ring-2 focus-visible:ring-accent/50"
2075
+ >
2076
+ <h2
2077
+ id={`${workspaceId}-next-steps`}
2078
+ className="text-lg font-semibold text-theme-text-primary"
2079
+ >
2080
+ {earlierPlan
2081
+ ? "Earlier proposed steps"
2082
+ : "Next steps"}
2083
+ </h2>
2084
+ <ResultCard
2085
+ diagnosis={currentAssessment.diagnosis}
2086
+ section="actions"
2087
+ compactActions
2088
+ actionNotice={
2089
+ earlierPlan
2090
+ ? assessmentNeedsCurrentStateVerification
2091
+ ? "Proposed before the apply attempt. Current state has not been verified."
2092
+ : "Proposed before the latest evidence. Reassess before applying."
2093
+ : undefined
2094
+ }
2095
+ onApply={
2096
+ canOfferInvestigationApply({
2097
+ currentAssessmentIdx,
2098
+ lastRemediationIdx,
2099
+ lastApplyAttemptIdx,
2100
+ localApplyAttemptAssessmentIdx,
2101
+ interactionsBlocked,
2102
+ hosted,
2103
+ hasNewerEvidence:
2104
+ hasEvidenceCollectedAfterAssessment,
2105
+ })
2106
+ ? requestApply
2107
+ : undefined
2108
+ }
2109
+ animate={currentAssessment.animateResult !== false}
2110
+ showDisclaimer={false}
2111
+ />
2112
+ </section>
2113
+ ) : undefined
2114
+ }
2115
+ />
2116
+ </>
2117
+ )}
2118
+ </div>
2119
+ </div>
2120
+ </section>
2121
+ </div>
660
2122
 
661
2123
  <ApplyDialog
662
2124
  open={confirmApply}
663
2125
  onClose={() => setConfirmApply(false)}
664
2126
  onConfirm={runApply}
665
2127
  agentLabel={agentLabel}
666
- resourceLabel={`${kind} ${namespace ? `${namespace}/` : ""}${name}`}
2128
+ resourceLabel={formatInvestigationTarget(run)}
2129
+ context={run.context}
667
2130
  fix={pendingFix}
668
2131
  managedBy={run.managedBy}
669
2132
  confidence={turns[lastRemediationIdx]?.diagnosis?.confidence}
670
2133
  />
2134
+ </div>
2135
+ );
2136
+ }
671
2137
 
672
- <div
673
- className={`border-t border-theme-border px-3 py-2.5 ${maximized ? "[&>*]:mx-auto [&>*]:max-w-3xl" : ""}`}
2138
+ function AssessmentHistory({
2139
+ assessments,
2140
+ }: {
2141
+ assessments: {
2142
+ sequence: number;
2143
+ diagnosis: Diagnosis;
2144
+ explanation?: AssessmentExplanation;
2145
+ initial: boolean;
2146
+ sources?: ReactNode;
2147
+ }[];
2148
+ }) {
2149
+ const reveal = useDisclosureReveal<HTMLDivElement>();
2150
+ const { revealAfterToggle } = reveal;
2151
+ const [open, setOpen] = useState(false);
2152
+ const regionId = useId();
2153
+ const openRequest = assessments.find((item) => item.explanation?.openRequest)
2154
+ ?.explanation?.openRequest;
2155
+ useEffect(() => {
2156
+ if (openRequest) {
2157
+ setOpen(true);
2158
+ revealAfterToggle(true);
2159
+ }
2160
+ }, [openRequest, revealAfterToggle]);
2161
+ if (assessments.length === 0) return null;
2162
+ return (
2163
+ <div data-investigation-assessment-history className="overflow-hidden">
2164
+ <button
2165
+ type="button"
2166
+ aria-expanded={open}
2167
+ aria-controls={regionId}
2168
+ onClick={() => {
2169
+ setOpen(!open);
2170
+ reveal.revealAfterToggle(!open);
2171
+ }}
2172
+ className="flex items-center gap-2 rounded-md px-2 py-1 text-left text-xs text-theme-text-tertiary hover:bg-theme-hover hover:text-theme-text-secondary"
674
2173
  >
675
- {canStop ? (
676
- <button
677
- onClick={stop}
678
- className="w-full rounded-lg border border-theme-border py-1.5 text-sm text-theme-text-secondary hover:bg-theme-hover"
679
- >
680
- Stop
681
- </button>
682
- ) : run.status === "stopping" ? (
683
- <div className="rounded-lg border border-theme-border bg-theme-base px-3 py-2 text-xs text-theme-text-secondary">
684
- Stopping investigation…
685
- </div>
686
- ) : !canContinue ? (
687
- <div className="rounded-lg border border-theme-border bg-theme-base px-3 py-2 text-xs text-theme-text-secondary">
688
- {run.trigger === "background"
689
- ? "Automatic investigation · read-only. Start a new investigation on this resource to continue digging."
690
- : "This investigation is read-only."}
691
- </div>
692
- ) : (
693
- <div className="flex items-end gap-2">
694
- <textarea
695
- value={input}
696
- onChange={(e) => setInput(e.target.value)}
697
- onKeyDown={(e) => {
698
- if (e.key === "Enter" && !e.shiftKey) {
699
- e.preventDefault();
700
- submitFollowup();
701
- }
702
- }}
703
- rows={1}
704
- disabled={!canContinue || busy}
705
- placeholder={
706
- stale
707
- ? "Cluster changed — re-run Diagnose"
708
- : "Ask a follow-up or refine…"
709
- }
710
- className="max-h-32 min-h-[38px] flex-1 resize-none rounded-lg border border-theme-border bg-theme-base px-3 py-2 text-sm text-theme-text-primary placeholder:text-theme-text-tertiary focus:border-accent focus:outline-none disabled:opacity-50"
711
- />
712
- <button
713
- onClick={submitFollowup}
714
- disabled={!input.trim() || !canContinue || busy}
715
- className="shrink-0 rounded-lg btn-brand p-2 disabled:opacity-40"
716
- aria-label="Send follow-up"
717
- >
718
- <Send className="h-4 w-4" />
719
- </button>
2174
+ <CollapseChevron open={open} className="h-3.5 w-3.5" />
2175
+ <span className="font-medium">
2176
+ Previous assessments · {assessments.length}
2177
+ </span>
2178
+ </button>
2179
+ <div id={regionId} ref={reveal.elementRef}>
2180
+ <Collapse open={open}>
2181
+ <div className="mt-2 space-y-4 border-l border-theme-border pl-3 pb-2">
2182
+ {assessments.map(
2183
+ (
2184
+ { sequence, diagnosis, explanation, initial, sources },
2185
+ index,
2186
+ ) => (
2187
+ <section
2188
+ key={sequence}
2189
+ aria-label={
2190
+ initial
2191
+ ? "Initial assessment"
2192
+ : `Earlier assessment ${index + 1}`
2193
+ }
2194
+ >
2195
+ <h3 className="text-sm font-medium text-theme-text-secondary">
2196
+ {initial
2197
+ ? "Initial assessment"
2198
+ : `Earlier assessment ${index + 1}`}
2199
+ </h3>
2200
+ <ResultCard
2201
+ diagnosis={diagnosis}
2202
+ explanation={explanation}
2203
+ assessmentSources={sources}
2204
+ section="conclusion"
2205
+ showDisclaimer={false}
2206
+ />
2207
+ {(diagnosis.remediation?.length ?? 0) > 0 && (
2208
+ <section
2209
+ aria-label="Earlier proposed steps"
2210
+ className="mt-3 border-t border-theme-border/60 pt-3"
2211
+ >
2212
+ <h3 className="text-sm font-medium text-theme-text-primary">
2213
+ Earlier proposed steps
2214
+ </h3>
2215
+ <ResultCard
2216
+ diagnosis={diagnosis}
2217
+ section="actions"
2218
+ compactActions
2219
+ actionNotice="From an earlier assessment, not the current recommendation."
2220
+ showDisclaimer={false}
2221
+ />
2222
+ </section>
2223
+ )}
2224
+ </section>
2225
+ ),
2226
+ )}
720
2227
  </div>
721
- )}
2228
+ </Collapse>
722
2229
  </div>
723
2230
  </div>
724
2231
  );