@skyhook-io/radar-app 1.18.2 → 1.19.0

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 (78) hide show
  1. package/package.json +4 -4
  2. package/src/App.tsx +0 -1
  3. package/src/api/diagnose.test.ts +33 -2
  4. package/src/api/diagnose.ts +93 -3
  5. package/src/api/radarFeatures.ts +1 -0
  6. package/src/components/diagnose/AIMarkdown.tsx +1 -1
  7. package/src/components/diagnose/ActivityTurn.tsx +34 -14
  8. package/src/components/diagnose/AgentControls.tsx +53 -6
  9. package/src/components/diagnose/AgentSetupNotice.tsx +1 -1
  10. package/src/components/diagnose/AgentSigninNotice.test.tsx +98 -0
  11. package/src/components/diagnose/AgentSigninNotice.tsx +61 -0
  12. package/src/components/diagnose/AnalysisStory.test.tsx +22 -0
  13. package/src/components/diagnose/AnalysisStory.tsx +17 -4
  14. package/src/components/diagnose/AssessmentCard.tsx +39 -8
  15. package/src/components/diagnose/DiagnoseContext.capacity.test.tsx +113 -0
  16. package/src/components/diagnose/DiagnoseContext.question.test.tsx +245 -0
  17. package/src/components/diagnose/DiagnoseContext.tsx +205 -9
  18. package/src/components/diagnose/DiagnoseSurface.test.tsx +54 -0
  19. package/src/components/diagnose/DiagnoseSurface.tsx +93 -21
  20. package/src/components/diagnose/EvidenceCard.tsx +67 -14
  21. package/src/components/diagnose/Home.test.tsx +182 -5
  22. package/src/components/diagnose/Home.tsx +168 -36
  23. package/src/components/diagnose/InvestigationCapacityNotice.test.tsx +246 -0
  24. package/src/components/diagnose/InvestigationCapacityNotice.tsx +241 -0
  25. package/src/components/diagnose/InvestigationEvidencePane.story.test.tsx +97 -6
  26. package/src/components/diagnose/InvestigationEvidencePane.test.tsx +57 -0
  27. package/src/components/diagnose/InvestigationEvidencePane.tsx +59 -11
  28. package/src/components/diagnose/InvestigationEvidenceSections.tsx +5 -1
  29. package/src/components/diagnose/InvestigationView.tsx +156 -29
  30. package/src/components/diagnose/LocalDiagnoseAction.tsx +23 -16
  31. package/src/components/diagnose/QuestionComposer.test.tsx +553 -0
  32. package/src/components/diagnose/QuestionComposer.tsx +487 -0
  33. package/src/components/diagnose/QuestionFocus.test.tsx +38 -0
  34. package/src/components/diagnose/QuestionFocus.tsx +123 -0
  35. package/src/components/diagnose/ResourcePicker.tsx +206 -0
  36. package/src/components/diagnose/StoryExcerpt.tsx +12 -3
  37. package/src/components/diagnose/assessmentCopy.ts +5 -0
  38. package/src/components/diagnose/investigationCase.runs.test.tsx +328 -0
  39. package/src/components/diagnose/investigationCase.ts +33 -1
  40. package/src/components/diagnose/investigationEvidence/adapters/coverage.test.tsx +46 -0
  41. package/src/components/diagnose/investigationEvidence/adapters/diagnose.ts +8 -0
  42. package/src/components/diagnose/investigationEvidence/adapters/posture.ts +29 -1
  43. package/src/components/diagnose/investigationEvidence/adapters/prometheus.test.ts +40 -0
  44. package/src/components/diagnose/investigationEvidence/adapters/prometheus.ts +27 -1
  45. package/src/components/diagnose/investigationEvidence/bodies/metrics.test.tsx +111 -0
  46. package/src/components/diagnose/investigationEvidence/bodies/metrics.tsx +67 -16
  47. package/src/components/diagnose/investigationEvidence/excerpt.test.ts +18 -5
  48. package/src/components/diagnose/investigationEvidence/excerpt.ts +22 -11
  49. package/src/components/diagnose/investigationEvidence/index.ts +1 -1
  50. package/src/components/diagnose/investigationEvidence/navigation.tsx +2 -0
  51. package/src/components/diagnose/investigationEvidence/types.ts +2 -0
  52. package/src/components/diagnose/investigationEvidencePartition.ts +6 -1
  53. package/src/components/diagnose/investigationFocus.test.ts +271 -0
  54. package/src/components/diagnose/investigationFocus.ts +132 -0
  55. package/src/components/diagnose/parts.test.tsx +20 -0
  56. package/src/components/diagnose/questionSuggestions.test.ts +111 -0
  57. package/src/components/diagnose/questionSuggestions.ts +150 -0
  58. package/src/components/diagnose/storyParts.test.tsx +13 -0
  59. package/src/components/diagnose/target.test.ts +143 -2
  60. package/src/components/diagnose/target.ts +109 -7
  61. package/src/components/diagnose/testdata/story-runs.json +474 -0
  62. package/src/components/diagnose/verdictText.test.tsx +87 -0
  63. package/src/components/diagnose/verdictText.ts +68 -0
  64. package/src/components/home/ClusterHealthCard.tsx +13 -39
  65. package/src/components/home/HomeAgentBlock.test.tsx +113 -0
  66. package/src/components/home/HomeAgentBlock.tsx +94 -0
  67. package/src/components/home/HomeView.tsx +5 -6
  68. package/src/components/home/RadarVersionLine.test.tsx +0 -14
  69. package/src/components/home/RadarVersionLine.tsx +3 -28
  70. package/src/components/ui/Omnibar.investigate.test.tsx +105 -0
  71. package/src/components/ui/Omnibar.tsx +47 -3
  72. package/src/components/ui/RadarOmnibar.test.tsx +1 -0
  73. package/src/components/ui/RadarOmnibar.tsx +11 -0
  74. package/src/components/whats-new/WhatsNewDialog.test.tsx +3 -3
  75. package/src/components/whats-new/releaseNotes.ts +60 -1
  76. package/src/hooks/useAgentSignin.test.tsx +190 -0
  77. package/src/hooks/useAgentSignin.ts +160 -0
  78. package/src/hooks/useApproveAfterSignin.test.tsx +108 -0
@@ -0,0 +1,271 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { focusFromCase } from "./investigationFocus";
4
+ import type {
5
+ InvestigationCaseItem,
6
+ InvestigationCaseResolution,
7
+ } from "./investigationCase";
8
+
9
+ function item(
10
+ role: InvestigationCaseItem["role"],
11
+ tool: string,
12
+ args: Record<string, unknown>,
13
+ patch: Partial<InvestigationCaseItem["source"]> = {},
14
+ ): InvestigationCaseItem {
15
+ return {
16
+ index: 0,
17
+ role,
18
+ claim: "",
19
+ placement: "card",
20
+ source: {
21
+ id: `${tool}-${JSON.stringify(args)}`,
22
+ turnIndex: 0,
23
+ timelineIndex: 0,
24
+ stepId: "s",
25
+ tool,
26
+ args: JSON.stringify(args),
27
+ order: 0,
28
+ phase: "initial",
29
+ confirmedSuccess: true,
30
+ ...patch,
31
+ },
32
+ };
33
+ }
34
+
35
+ const resolution = (
36
+ items: InvestigationCaseItem[],
37
+ ): InvestigationCaseResolution => ({ items, ruledOut: [] });
38
+
39
+ describe("focusFromCase", () => {
40
+ it("names resources by the arguments Radar recorded, causes first", () => {
41
+ const focus = focusFromCase(
42
+ resolution([
43
+ item("symptom", "get_pod_logs", { namespace: "shop", name: "api-0" }),
44
+ item("cause", "get_resource", {
45
+ kind: "configmaps",
46
+ namespace: "shop",
47
+ name: "api-config",
48
+ }),
49
+ ]),
50
+ );
51
+ expect(focus).toEqual([
52
+ { kind: "ConfigMap", group: "", namespace: "shop", name: "api-config" },
53
+ { kind: "Pod", group: "", namespace: "shop", name: "api-0" },
54
+ ]);
55
+ });
56
+
57
+ it("names the cited issue's subject, not the resource the read was about", () => {
58
+ const cause = item("cause", "diagnose", {
59
+ kind: "Deployment",
60
+ namespace: "gl-netpol",
61
+ name: "web",
62
+ });
63
+ cause.observation = {
64
+ data: {
65
+ type: "issue",
66
+ relevance: "producer-related",
67
+ issue: {
68
+ kind: "NetworkPolicy",
69
+ group: "networking.k8s.io",
70
+ namespace: "gl-netpol",
71
+ name: "deny-all-ingress",
72
+ },
73
+ },
74
+ } as unknown as InvestigationCaseItem["observation"];
75
+ expect(focusFromCase(resolution([cause]))).toEqual([
76
+ {
77
+ kind: "NetworkPolicy",
78
+ group: "networking.k8s.io",
79
+ namespace: "gl-netpol",
80
+ name: "deny-all-ingress",
81
+ },
82
+ ]);
83
+ });
84
+
85
+ it("names a bound resource by its apiVersion group", () => {
86
+ const cause = item("cause", "list_resources", { kind: "configmaps" });
87
+ cause.observation = {
88
+ data: {
89
+ type: "resource",
90
+ resource: {
91
+ apiVersion: "apps/v1",
92
+ kind: "Deployment",
93
+ metadata: { namespace: "shop", name: "api" },
94
+ },
95
+ warnings: [],
96
+ },
97
+ } as unknown as InvestigationCaseItem["observation"];
98
+ expect(focusFromCase(resolution([cause]))).toEqual([
99
+ { kind: "Deployment", group: "apps", namespace: "shop", name: "api" },
100
+ ]);
101
+ });
102
+
103
+ it("lists a resource once when a read omitted the group the bound object has", () => {
104
+ const read = item("cause", "diagnose", {
105
+ kind: "deployment",
106
+ namespace: "shop",
107
+ name: "api",
108
+ });
109
+ const bound = item("cause", "get_resource", { kind: "deployments" });
110
+ bound.observation = {
111
+ data: {
112
+ type: "resource",
113
+ resource: {
114
+ apiVersion: "apps/v1",
115
+ kind: "Deployment",
116
+ metadata: { namespace: "shop", name: "api" },
117
+ },
118
+ warnings: [],
119
+ },
120
+ } as unknown as InvestigationCaseItem["observation"];
121
+ expect(focusFromCase(resolution([read, bound]))).toEqual([
122
+ { kind: "Deployment", group: "apps", namespace: "shop", name: "api" },
123
+ ]);
124
+ });
125
+
126
+ it("keeps same-named resources of different exact groups apart", () => {
127
+ const core = item("cause", "get_resource", {});
128
+ core.observation = {
129
+ data: {
130
+ type: "resource",
131
+ resource: {
132
+ apiVersion: "v1",
133
+ kind: "Service",
134
+ metadata: { namespace: "shop", name: "api" },
135
+ },
136
+ warnings: [],
137
+ },
138
+ } as unknown as InvestigationCaseItem["observation"];
139
+ const knative = item("symptom", "get_resource", {});
140
+ knative.observation = {
141
+ data: {
142
+ type: "resource",
143
+ resource: {
144
+ apiVersion: "serving.knative.dev/v1",
145
+ kind: "Service",
146
+ metadata: { namespace: "shop", name: "api" },
147
+ },
148
+ warnings: [],
149
+ },
150
+ } as unknown as InvestigationCaseItem["observation"];
151
+ expect(focusFromCase(resolution([core, knative]))).toHaveLength(2);
152
+ });
153
+
154
+ it("does not guess a resource from a read that searched every namespace", () => {
155
+ expect(
156
+ focusFromCase(
157
+ resolution([
158
+ item("cause", "get_events", { kind: "Deployment", name: "api" }),
159
+ ]),
160
+ ),
161
+ ).toEqual([]);
162
+ });
163
+
164
+ it("reads workload logs as the Deployment the tool defaults to", () => {
165
+ expect(
166
+ focusFromCase(
167
+ resolution([
168
+ item("symptom", "get_workload_logs", {
169
+ namespace: "shop",
170
+ name: "api",
171
+ }),
172
+ ]),
173
+ ),
174
+ ).toEqual([
175
+ { kind: "Deployment", group: "", namespace: "shop", name: "api" },
176
+ ]);
177
+ });
178
+
179
+ it("keeps the exact kind Radar recorded", () => {
180
+ const cause = item("cause", "get_resource", { kind: "podmetrics" });
181
+ cause.observation = {
182
+ data: {
183
+ type: "resource",
184
+ resource: {
185
+ apiVersion: "metrics.k8s.io/v1beta1",
186
+ kind: "PodMetrics",
187
+ metadata: { namespace: "shop", name: "api-0" },
188
+ },
189
+ warnings: [],
190
+ },
191
+ } as unknown as InvestigationCaseItem["observation"];
192
+ expect(focusFromCase(resolution([cause]))).toEqual([
193
+ {
194
+ kind: "PodMetrics",
195
+ group: "metrics.k8s.io",
196
+ namespace: "shop",
197
+ name: "api-0",
198
+ },
199
+ ]);
200
+ });
201
+
202
+ it("names the pod a cited log stream came from", () => {
203
+ const symptom = item("symptom", "diagnose", {
204
+ kind: "Deployment",
205
+ namespace: "shop",
206
+ name: "api",
207
+ });
208
+ symptom.observation = {
209
+ data: {
210
+ type: "logs",
211
+ pod: "api-123",
212
+ namespace: "shop",
213
+ previous: false,
214
+ },
215
+ } as unknown as InvestigationCaseItem["observation"];
216
+ expect(focusFromCase(resolution([symptom]))).toEqual([
217
+ { kind: "Pod", group: "", namespace: "shop", name: "api-123" },
218
+ ]);
219
+ });
220
+
221
+ it("spells an agent's lowercase kind the Kubernetes way", () => {
222
+ expect(
223
+ focusFromCase(
224
+ resolution([
225
+ item("cause", "diagnose", {
226
+ kind: "deployment",
227
+ namespace: "gl-missing-cm",
228
+ name: "checkout",
229
+ }),
230
+ ]),
231
+ ),
232
+ ).toEqual([
233
+ {
234
+ kind: "Deployment",
235
+ group: "",
236
+ namespace: "gl-missing-cm",
237
+ name: "checkout",
238
+ },
239
+ ]);
240
+ });
241
+
242
+ it("ignores broad reads, context, failed reads and repeats", () => {
243
+ const focus = focusFromCase(
244
+ resolution([
245
+ item("cause", "search", { query: "checkout" }),
246
+ item("context", "get_resource", { kind: "Pod", name: "x" }),
247
+ item(
248
+ "cause",
249
+ "get_resource",
250
+ { kind: "Pod", namespace: "shop", name: "denied" },
251
+ { confirmedSuccess: false },
252
+ ),
253
+ item("cause", "diagnose", {
254
+ kind: "Deployment",
255
+ group: "apps",
256
+ namespace: "shop",
257
+ name: "api",
258
+ }),
259
+ item("symptom", "get_events", {
260
+ kind: "Deployment",
261
+ group: "apps",
262
+ namespace: "shop",
263
+ name: "api",
264
+ }),
265
+ ]),
266
+ );
267
+ expect(focus).toEqual([
268
+ { kind: "Deployment", group: "apps", namespace: "shop", name: "api" },
269
+ ]);
270
+ });
271
+ });
@@ -0,0 +1,132 @@
1
+ import type {
2
+ InvestigationCaseItem,
3
+ InvestigationCaseResolution,
4
+ } from "./investigationCase";
5
+ import {
6
+ investigationEvidenceSubjectRef,
7
+ investigationSourceArgs,
8
+ } from "./investigationEvidence/identity";
9
+ import {
10
+ canonicalKind,
11
+ runTargetKey,
12
+ type InvestigationTargetIdentity,
13
+ } from "./target";
14
+
15
+ // Reads that are about exactly the resource their arguments name. A broad
16
+ // read (a search, a namespace listing) proves nothing about which resource
17
+ // mattered, however its rows are cited.
18
+ const SINGLE_RESOURCE_TOOLS = new Set([
19
+ "diagnose",
20
+ "get_resource",
21
+ "get_events",
22
+ "get_neighborhood",
23
+ "get_workload_logs",
24
+ "get_pod_logs",
25
+ ]);
26
+
27
+ const NAMESPACED_ONLY_TOOLS = new Set([
28
+ "get_events",
29
+ "get_pod_logs",
30
+ "get_workload_logs",
31
+ ]);
32
+
33
+ // The resources a question-only assessment came to rest on, causes first.
34
+ // Each is named by what Radar produced, never by what the agent wrote: the
35
+ // object the citation bound to, or else the resource a single-resource read
36
+ // was asked about.
37
+ export function focusFromCase(
38
+ investigationCase: InvestigationCaseResolution | undefined,
39
+ max = 3,
40
+ ): InvestigationTargetIdentity[] {
41
+ if (!investigationCase) return [];
42
+ const items = investigationCase.items
43
+ .filter((item) => item.role === "cause" || item.role === "symptom")
44
+ .sort((a, b) => Number(b.role === "cause") - Number(a.role === "cause"));
45
+ const focus: (InvestigationTargetIdentity & { exact: boolean })[] = [];
46
+ for (const item of items) {
47
+ const bound = boundResource(item);
48
+ const resource = bound ?? readResource(item);
49
+ if (!resource) continue;
50
+ // A read's arguments often leave out a built-in group the bound object
51
+ // carries (deployment vs apps/Deployment): the same resource either way,
52
+ // named by the exact identity whichever arrives first.
53
+ const same = focus.findIndex(
54
+ (seen) =>
55
+ sameResource(seen, resource) &&
56
+ (seen.group === resource.group ||
57
+ (!seen.exact && !seen.group) ||
58
+ (!bound && !resource.group)),
59
+ );
60
+ if (same >= 0) {
61
+ if (bound && !focus[same].exact) focus[same] = { ...bound, exact: true };
62
+ continue;
63
+ }
64
+ if (focus.length >= max) continue;
65
+ focus.push({ ...resource, exact: !!bound });
66
+ }
67
+ return focus.map(({ kind, group, namespace, name }) => ({
68
+ kind,
69
+ group,
70
+ namespace,
71
+ name,
72
+ }));
73
+ }
74
+
75
+ function sameResource(
76
+ a: InvestigationTargetIdentity,
77
+ b: InvestigationTargetIdentity,
78
+ ): boolean {
79
+ return (
80
+ a.namespace === b.namespace &&
81
+ a.name === b.name &&
82
+ runTargetKey(a.kind, "", "", "") === runTargetKey(b.kind, "", "", "")
83
+ );
84
+ }
85
+
86
+ // What the citation bound to, exactly as Radar recorded it.
87
+ function boundResource(
88
+ item: InvestigationCaseItem,
89
+ ): InvestigationTargetIdentity | undefined {
90
+ const data = item.observation?.data;
91
+ const ref = data ? investigationEvidenceSubjectRef(data) : undefined;
92
+ if (!ref?.kind || !ref.name) return undefined;
93
+ return {
94
+ kind: ref.kind,
95
+ group: ref.group ?? "",
96
+ namespace: ref.namespace ?? "",
97
+ name: ref.name,
98
+ };
99
+ }
100
+
101
+ function readResource(
102
+ item: InvestigationCaseItem,
103
+ ): InvestigationTargetIdentity | undefined {
104
+ const { source } = item;
105
+ if (!source.confirmedSuccess || !SINGLE_RESOURCE_TOOLS.has(source.tool)) {
106
+ return undefined;
107
+ }
108
+ const args = investigationSourceArgs(source);
109
+ const name = typeof args?.name === "string" ? args.name : "";
110
+ const kind =
111
+ source.tool === "get_pod_logs"
112
+ ? "Pod"
113
+ : typeof args?.kind === "string" && args.kind
114
+ ? args.kind
115
+ : source.tool === "get_workload_logs"
116
+ ? "Deployment"
117
+ : "";
118
+ const namespace = typeof args?.namespace === "string" ? args.namespace : "";
119
+ if (!name || !kind) return undefined;
120
+ // These reads search every namespace when given none, so their arguments
121
+ // alone don't say which resource they were about.
122
+ if (!namespace && NAMESPACED_ONLY_TOOLS.has(source.tool)) return undefined;
123
+ const group = typeof args?.group === "string" ? args.group : "";
124
+ // Agents spell kinds every way kubectl accepts: deployment, deployments,
125
+ // Deployment. The server canonicalizes the group when a run starts.
126
+ return {
127
+ kind: canonicalKind(kind, group),
128
+ group,
129
+ namespace,
130
+ name,
131
+ };
132
+ }
@@ -1741,3 +1741,23 @@ describe("assessment provenance disclosure", () => {
1741
1741
  ).toBe("");
1742
1742
  });
1743
1743
  });
1744
+
1745
+ describe("TurnView opening question", () => {
1746
+ const opening: Turn = {
1747
+ timeline: [],
1748
+ diagnosis: null,
1749
+ error: null,
1750
+ status: "running",
1751
+ };
1752
+
1753
+ it("shows the question a run opened with as the engineer's message", () => {
1754
+ const html = renderToStaticMarkup(
1755
+ <TurnView
1756
+ turn={opening}
1757
+ openingQuestion={"checkout 502s\nsince lunch"}
1758
+ />,
1759
+ );
1760
+ expect(html).toContain("checkout 502s\nsince lunch");
1761
+ expect(html).toContain("whitespace-pre-wrap");
1762
+ });
1763
+ });
@@ -0,0 +1,111 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { Issue } from "@skyhook-io/k8s-ui";
3
+
4
+ import { suggestionsFromIssues } from "./questionSuggestions";
5
+
6
+ function issue(patch: Partial<Issue>): Issue {
7
+ return {
8
+ id: patch.name ?? "x",
9
+ severity: "warning",
10
+ source: "problem",
11
+ category: "crashloop",
12
+ category_group: "runtime",
13
+ grouping_scope: "workload",
14
+ kind: "Deployment",
15
+ group: "apps",
16
+ namespace: "shop",
17
+ name: "checkout",
18
+ reason: "CrashLoopBackOff",
19
+ ...patch,
20
+ } as Issue;
21
+ }
22
+
23
+ describe("suggestionsFromIssues", () => {
24
+ it("offers nothing when Radar sees nothing", () => {
25
+ expect(suggestionsFromIssues([])).toEqual([]);
26
+ });
27
+
28
+ it("pins a problem on one resource and scopes a shared one to its namespace", () => {
29
+ const suggestions = suggestionsFromIssues([
30
+ issue({ name: "checkout" }),
31
+ issue({ category: "image_pull_failed", namespace: "web", name: "a" }),
32
+ issue({ category: "image_pull_failed", namespace: "web", name: "b" }),
33
+ ]);
34
+ const one = suggestions.find((s) => s.pin);
35
+ expect(one?.pin).toEqual({
36
+ kind: "Deployment",
37
+ group: "apps",
38
+ namespace: "shop",
39
+ name: "checkout",
40
+ });
41
+ expect(one?.title).toBe("Crash loop");
42
+ expect(one?.where).toBe("Deployment shop/checkout");
43
+ expect(one?.question).toBe("What's causing “Crash loop”?");
44
+ const shared = suggestions.find((s) => !s.pin);
45
+ expect(shared?.namespaces).toEqual(["web"]);
46
+ expect(shared?.question).toContain("on 2 resources in web");
47
+ expect(shared?.where).toBe("2 resources · web");
48
+ });
49
+
50
+ it("counts resources, not issues, and keeps different problems apart", () => {
51
+ const suggestions = suggestionsFromIssues([
52
+ issue({
53
+ id: "a",
54
+ name: "pg",
55
+ kind: "Cluster",
56
+ group: "postgresql.cnpg.io",
57
+ reason: "WALArchivingFailing",
58
+ }),
59
+ issue({
60
+ id: "b",
61
+ name: "pg",
62
+ kind: "Cluster",
63
+ group: "postgresql.cnpg.io",
64
+ reason: "WALArchivingFailing",
65
+ }),
66
+ issue({
67
+ id: "c",
68
+ name: "pg",
69
+ kind: "Cluster",
70
+ group: "postgresql.cnpg.io",
71
+ reason: "BackupFailed",
72
+ }),
73
+ ]);
74
+ expect(suggestions.length).toBeGreaterThan(0);
75
+ for (const suggestion of suggestions) {
76
+ expect(suggestion.pin).toMatchObject({ kind: "Cluster", name: "pg" });
77
+ expect(suggestion.where).not.toMatch(/resources/);
78
+ }
79
+ });
80
+
81
+ it("offers different kinds of problem before more of the same", () => {
82
+ const suggestions = suggestionsFromIssues(
83
+ [
84
+ issue({ category: "crashloop", namespace: "a", name: "a" }),
85
+ issue({ category: "crashloop", namespace: "b", name: "b" }),
86
+ issue({ category: "image_pull_failed", namespace: "c", name: "c" }),
87
+ ],
88
+ 2,
89
+ );
90
+ expect(suggestions.map((s) => s.pin?.name)).toEqual(["a", "c"]);
91
+ });
92
+
93
+ it("puts the most severe problem first and stops at the cap", () => {
94
+ const suggestions = suggestionsFromIssues(
95
+ [
96
+ issue({ category: "a", name: "a" }),
97
+ issue({ category: "b", name: "b", severity: "critical" }),
98
+ issue({ category: "c", name: "c" }),
99
+ issue({ category: "d", name: "d" }),
100
+ ],
101
+ 3,
102
+ );
103
+ expect(suggestions).toHaveLength(3);
104
+ expect(suggestions[0].pin?.name).toBe("b");
105
+ expect(suggestions.map((s) => s.severity)).toEqual([
106
+ "critical",
107
+ "warning",
108
+ "warning",
109
+ ]);
110
+ });
111
+ });
@@ -0,0 +1,150 @@
1
+ import {
2
+ compareIssues,
3
+ issueTitle,
4
+ type Issue,
5
+ type IssueSeverity,
6
+ } from "@skyhook-io/k8s-ui";
7
+
8
+ import { formatInvestigationTarget } from "./target";
9
+
10
+ export interface QuestionPin {
11
+ kind: string;
12
+ group: string;
13
+ namespace: string;
14
+ name: string;
15
+ }
16
+
17
+ export interface QuestionSuggestion {
18
+ key: string;
19
+ /** The problem as the Issues page names it, so the row is recognizable. */
20
+ title: string;
21
+ /** Where it is: one resource, or how many resources in which namespace. */
22
+ where: string;
23
+ severity: IssueSeverity;
24
+ /** What goes in the box. A pinned resource is named by its chip instead. */
25
+ question: string;
26
+ namespaces: string[];
27
+ /** Set when the suggestion is about one resource, so the run can start
28
+ * there instead of searching for it. */
29
+ pin?: QuestionPin;
30
+ }
31
+
32
+ // Questions an engineer would plausibly ask about what Radar sees right now,
33
+ // most severe first: one per problem per namespace. A problem on one
34
+ // resource becomes a question about that resource; the same problem on
35
+ // several becomes a question about the namespace. A resource with two issues
36
+ // of the same name is still one resource. A cluster with no issues
37
+ // gets no suggestions — a stock prompt would only be noise.
38
+ export function suggestionsFromIssues(
39
+ issues: readonly Issue[],
40
+ max = 3,
41
+ ): QuestionSuggestion[] {
42
+ const groups = new Map<string, Issue[]>();
43
+ for (const issue of [...issues].sort(compareIssues)) {
44
+ const key = `${issueTitle(issue)}\u0000${issue.namespace ?? ""}`;
45
+ const members = groups.get(key);
46
+ if (members) members.push(issue);
47
+ else groups.set(key, [issue]);
48
+ }
49
+ // One kind of problem per suggestion first, so three namespaces with the
50
+ // same symptom don't crowd out a different one; then fill by severity.
51
+ const seen = new Set<string>();
52
+ const firstOfKind: [string, Issue[]][] = [];
53
+ const rest: [string, Issue[]][] = [];
54
+ for (const entry of groups) {
55
+ const category = entry[1][0].category;
56
+ if (seen.has(category)) rest.push(entry);
57
+ else {
58
+ seen.add(category);
59
+ firstOfKind.push(entry);
60
+ }
61
+ }
62
+ const suggestions: QuestionSuggestion[] = [];
63
+ for (const [key, members] of [...firstOfKind, ...rest]) {
64
+ if (suggestions.length >= max) break;
65
+ const first = members[0];
66
+ const title = issueTitle(first);
67
+ const namespaces = first.namespace ? [first.namespace] : [];
68
+ const resources = new Set(
69
+ members.map((m) => `${m.kind}\u0000${m.group ?? ""}\u0000${m.name}`),
70
+ ).size;
71
+ if (resources === 1) {
72
+ const pin = {
73
+ kind: first.kind,
74
+ group: first.group ?? "",
75
+ namespace: first.namespace ?? "",
76
+ name: first.name,
77
+ };
78
+ suggestions.push({
79
+ key,
80
+ title,
81
+ where: formatInvestigationTarget(pin),
82
+ severity: severityOf(first),
83
+ question: `What's causing “${title}”?`,
84
+ namespaces,
85
+ pin,
86
+ });
87
+ continue;
88
+ }
89
+ const scope = first.namespace
90
+ ? `in ${first.namespace}`
91
+ : "across the cluster";
92
+ suggestions.push({
93
+ key,
94
+ title,
95
+ where: first.namespace
96
+ ? `${resources} resources · ${first.namespace}`
97
+ : `${resources} resources across the cluster`,
98
+ severity: severityOf(first),
99
+ question: `What's causing “${title}” on ${resources} resources ${scope}?`,
100
+ namespaces,
101
+ });
102
+ }
103
+ return suggestions;
104
+ }
105
+
106
+ // Questions about a pinned resource, from the issues Radar reports for it
107
+ // (its own, and ones on related objects that name it). The resource stays
108
+ // pinned; the question names the problem, and the object it is on when that
109
+ // is not the pinned resource.
110
+ export function suggestionsForPin(
111
+ issues: readonly Issue[],
112
+ pin: QuestionPin,
113
+ max = 3,
114
+ ): QuestionSuggestion[] {
115
+ const pinned = formatInvestigationTarget(pin);
116
+ const seen = new Set<string>();
117
+ const suggestions: QuestionSuggestion[] = [];
118
+ for (const issue of [...issues].sort(compareIssues)) {
119
+ if (suggestions.length >= max) break;
120
+ const title = issueTitle(issue);
121
+ const on = formatInvestigationTarget({
122
+ kind: issue.kind,
123
+ group: issue.group ?? "",
124
+ namespace: issue.namespace ?? "",
125
+ name: issue.name,
126
+ });
127
+ const key = `${title}\u0000${on}`;
128
+ if (seen.has(key)) continue;
129
+ seen.add(key);
130
+ const question =
131
+ on === pinned
132
+ ? `What's causing “${title}”?`
133
+ : `What's causing “${title}” on ${on}?`;
134
+ suggestions.push({
135
+ key,
136
+ title,
137
+ where: on,
138
+ severity: severityOf(issue),
139
+ question,
140
+ namespaces: [],
141
+ pin,
142
+ });
143
+ }
144
+ return suggestions;
145
+ }
146
+
147
+ // The Issues page coerces an unknown severity to warning; so does this list.
148
+ function severityOf(issue: Issue): IssueSeverity {
149
+ return issue.severity === "critical" ? "critical" : "warning";
150
+ }