-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathseer.ts
More file actions
611 lines (527 loc) · 16.9 KB
/
seer.ts
File metadata and controls
611 lines (527 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
* Seer API Types
*
* Zod schemas and TypeScript types for Sentry's Seer Autofix API.
*/
import { z } from "zod";
// Status Constants
/** Possible autofix run statuses */
export const AUTOFIX_STATUSES = [
"PROCESSING",
"COMPLETED",
"ERROR",
"CANCELLED",
"NEED_MORE_INFORMATION",
"WAITING_FOR_USER_RESPONSE",
] as const;
export type AutofixStatus = (typeof AUTOFIX_STATUSES)[number];
/** Terminal statuses that indicate the run has finished */
export const TERMINAL_STATUSES: AutofixStatus[] = [
"COMPLETED",
"ERROR",
"CANCELLED",
];
/** Stopping point values for autofix runs */
export const STOPPING_POINTS = [
"root_cause",
"solution",
"code_changes",
"open_pr",
] as const;
export type StoppingPoint = (typeof STOPPING_POINTS)[number];
// Progress Message
export const ProgressMessageSchema = z.object({
message: z.string(),
timestamp: z.string(),
type: z.string().optional(),
});
export type ProgressMessage = z.infer<typeof ProgressMessageSchema>;
// Relevant Code File
export const RelevantCodeFileSchema = z.object({
file_path: z.string(),
repo_name: z.string(),
});
export type RelevantCodeFile = z.infer<typeof RelevantCodeFileSchema>;
// Reproduction Step
export const ReproductionStepSchema = z.object({
title: z.string(),
code_snippet_and_analysis: z.string(),
is_most_important_event: z.boolean().optional(),
relevant_code_file: RelevantCodeFileSchema.optional(),
timeline_item_type: z.string().optional(),
});
export type ReproductionStep = z.infer<typeof ReproductionStepSchema>;
// Root Cause
export const RootCauseSchema = z.object({
id: z.number(),
description: z.string(),
relevant_repos: z.array(z.string()).optional(),
reproduction_urls: z.array(z.string()).optional(),
root_cause_reproduction: z.array(ReproductionStepSchema).optional(),
});
export type RootCause = z.infer<typeof RootCauseSchema>;
// Root Cause Selection
export const RootCauseSelectionSchema = z.object({
cause_id: z.number(),
instruction: z.string().nullable().optional(),
});
export type RootCauseSelection = z.infer<typeof RootCauseSelectionSchema>;
// Autofix Step
export const AutofixStepSchema = z
.object({
id: z.string(),
key: z.string(),
status: z.string(),
title: z.string(),
progress: z.array(ProgressMessageSchema).optional(),
causes: z.array(RootCauseSchema).optional(),
selection: RootCauseSelectionSchema.optional(),
})
.passthrough(); // Allow additional fields like artifacts
export type AutofixStep = z.infer<typeof AutofixStepSchema>;
// Repository Info
export const RepositoryInfoSchema = z.object({
integration_id: z.number().optional(),
url: z.string().optional(),
external_id: z.string(),
name: z.string(),
provider: z.string().optional(),
default_branch: z.string().optional(),
is_readable: z.boolean().optional(),
is_writeable: z.boolean().optional(),
});
export type RepositoryInfo = z.infer<typeof RepositoryInfoSchema>;
// Codebase Info
export const CodebaseInfoSchema = z.object({
repo_external_id: z.string(),
file_changes: z.array(z.unknown()).optional(),
is_readable: z.boolean().optional(),
is_writeable: z.boolean().optional(),
});
export type CodebaseInfo = z.infer<typeof CodebaseInfoSchema>;
// PR Info (from completed fix)
export const PullRequestInfoSchema = z.object({
pr_number: z.number().optional(),
pr_url: z.string().optional(),
repo_name: z.string().optional(),
});
export type PullRequestInfo = z.infer<typeof PullRequestInfoSchema>;
// Solution Artifact (from plan command)
/** A single step in the solution plan */
export const SolutionStepSchema = z.object({
title: z.string(),
description: z.string(),
});
export type SolutionStep = z.infer<typeof SolutionStepSchema>;
/** Solution data containing the plan to fix the issue */
export const SolutionDataSchema = z.object({
one_line_summary: z.string(),
steps: z.array(SolutionStepSchema),
});
export type SolutionData = z.infer<typeof SolutionDataSchema>;
/** Solution artifact from the autofix response */
export const SolutionArtifactSchema = z.object({
key: z.literal("solution"),
data: SolutionDataSchema,
reason: z.string().optional(),
});
export type SolutionArtifact = z.infer<typeof SolutionArtifactSchema>;
// Autofix State
export const AutofixStateSchema = z
.object({
run_id: z.number(),
status: z.string(),
updated_at: z.string().optional(),
request: z
.object({
organization_id: z.number().optional(),
project_id: z.number().optional(),
repos: z.array(z.unknown()).optional(),
})
.optional(),
codebases: z.record(z.string(), CodebaseInfoSchema).optional(),
steps: z.array(AutofixStepSchema).optional(),
repositories: z.array(RepositoryInfoSchema).optional(),
coding_agents: z.record(z.string(), z.unknown()).optional(),
created_at: z.string().optional(),
completed_at: z.string().optional(),
})
.passthrough(); // Allow additional fields like blocks
export type AutofixState = z.infer<typeof AutofixStateSchema>;
// Autofix Response (GET /issues/{id}/autofix/)
export const AutofixResponseSchema = z.object({
autofix: AutofixStateSchema.nullable(),
});
export type AutofixResponse = z.infer<typeof AutofixResponseSchema>;
// Update Payloads
export const SelectRootCausePayloadSchema = z.object({
type: z.literal("select_root_cause"),
cause_id: z.number(),
stopping_point: z.enum(["solution", "code_changes", "open_pr"]).optional(),
});
export type SelectRootCausePayload = z.infer<
typeof SelectRootCausePayloadSchema
>;
export const SelectSolutionPayloadSchema = z.object({
type: z.literal("select_solution"),
});
export type SelectSolutionPayload = z.infer<typeof SelectSolutionPayloadSchema>;
export const CreatePrPayloadSchema = z.object({
type: z.literal("create_pr"),
});
export type CreatePrPayload = z.infer<typeof CreatePrPayloadSchema>;
export type AutofixUpdatePayload =
| SelectRootCausePayload
| SelectSolutionPayload
| CreatePrPayload;
// Helper Functions
/**
* Check if an autofix status is terminal (no more updates expected).
*
* @param status - The status string to check
* @returns True if the status indicates completion (COMPLETED, ERROR, CANCELLED)
*/
export function isTerminalStatus(status: string): boolean {
return TERMINAL_STATUSES.includes(status as AutofixStatus);
}
/** Container that may hold root cause analysis data (legacy format) */
type WithCauses = { key: string; causes?: RootCause[] };
/**
* Search an array of containers (blocks or steps) for root causes
* in the legacy format where causes are stored directly on the container.
*/
function searchContainersForRootCauses(
containers: WithCauses[]
): RootCause[] | null {
for (const container of containers) {
if (container.key === "root_cause_analysis" && container.causes) {
return container.causes;
}
}
return null;
}
/** Agent root cause artifact data from the explorer endpoint */
type AgentRootCauseData = {
one_line_description: string;
five_whys?: string[];
reproduction_steps?: string[];
relevant_repo?: string | null;
};
/**
* Search blocks for root cause artifacts in the agent format.
*
* The agent endpoint stores root causes as artifacts with `key: "root_cause"`
* and data `{ one_line_description, five_whys, reproduction_steps, relevant_repo }`.
* Maps to the existing {@link RootCause} shape for downstream compatibility.
*/
function searchBlocksForAgentRootCause(
blocks: WithArtifacts[]
): RootCause[] | null {
for (const block of blocks) {
if (!block.artifacts) {
continue;
}
for (const artifact of block.artifacts) {
if (artifact.key === "root_cause" && artifact.data) {
const agentData = artifact.data as AgentRootCauseData;
const cause: RootCause = {
id: 0,
description: agentData.one_line_description,
relevant_repos: agentData.relevant_repo
? [agentData.relevant_repo]
: undefined,
};
return [cause];
}
}
}
return null;
}
/**
* Extract root causes from autofix state.
*
* Searches through blocks and steps for root cause data in multiple formats:
* 1. Legacy step/block format: containers with `key: "root_cause_analysis"` and `causes[]`
* 2. Agent artifact format: blocks with artifacts `key: "root_cause"` containing
* `{ one_line_description, five_whys, reproduction_steps, relevant_repo }`
*
* @param state - The autofix state containing analysis data
* @returns Array of root causes, or empty array if none found
*/
export function extractRootCauses(state: AutofixState): RootCause[] {
const stateWithExtras = state as AutofixState & {
blocks?: (WithCauses & WithArtifacts)[];
steps?: WithCauses[];
};
if (stateWithExtras.blocks) {
// Try legacy format first (containers with causes[])
const causes = searchContainersForRootCauses(stateWithExtras.blocks);
if (causes) {
return causes;
}
// Try agent artifact format (artifacts with key: "root_cause")
const agentCauses = searchBlocksForAgentRootCause(stateWithExtras.blocks);
if (agentCauses) {
return agentCauses;
}
}
if (stateWithExtras.steps) {
const causes = searchContainersForRootCauses(stateWithExtras.steps);
if (causes) {
return causes;
}
}
return [];
}
/** Artifact structure used in blocks and steps */
type ArtifactEntry = { key: string; data: unknown; reason?: string };
/** Structure that may contain artifacts */
type WithArtifacts = { artifacts?: ArtifactEntry[] };
/**
* Search artifacts array for a solution artifact.
*/
function findSolutionInArtifacts(
artifacts: ArtifactEntry[]
): SolutionArtifact | null {
for (const artifact of artifacts) {
if (artifact.key === "solution") {
const result = SolutionArtifactSchema.safeParse(artifact);
if (result.success) {
return result.data;
}
}
}
return null;
}
/**
* Search artifacts for a solution-keyed entry and return its reason string.
*
* When Seer completes but cannot produce a code fix, the API may return
* `{ key: "solution", data: null, reason: "..." }`. The full artifact
* fails `SolutionArtifactSchema` validation (data is required), but the
* `reason` field still carries useful context for the user.
*/
function findNoSolutionReason(artifacts: ArtifactEntry[]): string | undefined {
for (const artifact of artifacts) {
if (artifact.key === "solution" && artifact.reason) {
return artifact.reason;
}
}
return;
}
/**
* Search containers (blocks or steps) for a no-solution reason in artifacts.
*/
function searchContainersForNoSolutionReason(
containers: WithArtifacts[]
): string | undefined {
for (const container of containers) {
if (container.artifacts) {
const reason = findNoSolutionReason(container.artifacts);
if (reason) {
return reason;
}
}
}
return;
}
/**
* Search containers for step-level no-solution reason.
*
* The Seer API returns solution data directly on steps with `key === "solution"`.
* When no solution is produced, the step has an empty/missing `solution` array
* but its `description` field carries the reason why no fix was found.
*/
function searchContainersForStepLevelNoSolutionReason(
containers: StepWithSolution[]
): string | undefined {
for (const container of containers) {
if (
container.key === "solution" &&
(!container.solution || container.solution.length === 0) &&
container.description
) {
return container.description;
}
}
return;
}
/**
* Search an array of containers (blocks or steps) for a solution artifact.
*/
function searchContainersForSolution(
containers: WithArtifacts[]
): SolutionArtifact | null {
for (const container of containers) {
if (container.artifacts) {
const solution = findSolutionInArtifacts(container.artifacts);
if (solution) {
return solution;
}
}
}
return null;
}
/** Step-level solution item returned by the Seer API */
type StepSolutionItem = {
title: string;
code_snippet_and_analysis?: string;
relevant_code_file?: { file_path: string | null; repo_name: string };
};
/** Step shape with passthrough fields for solution data */
type StepWithSolution = {
key: string;
description?: string;
solution?: StepSolutionItem[];
};
/**
* Search containers for step-level solution data.
*
* The Seer API returns solution data directly on steps with `key === "solution"`
* rather than inside the `artifacts` array. This function finds such steps and
* maps the data to the existing {@link SolutionArtifact} shape so downstream
* formatters and commands don't need changes.
*/
function searchContainersForStepLevelSolution(
containers: StepWithSolution[]
): SolutionArtifact | null {
for (const container of containers) {
if (
container.key === "solution" &&
container.solution &&
container.solution.length > 0
) {
return {
key: "solution",
data: {
one_line_summary: container.description ?? "",
steps: container.solution.map((item) => ({
title: item.title,
description: item.code_snippet_and_analysis ?? "",
})),
},
};
}
}
return null;
}
/**
* Extract solution artifact from autofix state.
*
* Searches through blocks and steps for solution data. The Seer API may
* return solution data in two formats:
* 1. Step-level: `step.solution[]` array with `step.description` (current API)
* 2. Artifact-level: `step.artifacts[]` with `key === "solution"` (legacy/fallback)
*
* Step-level data is checked first since it matches the current API response shape.
*
* @param state - Autofix state (may contain blocks or steps with solution data)
* @returns SolutionArtifact if found, null otherwise
*/
export function extractSolution(state: AutofixState): SolutionArtifact | null {
// Access blocks and steps from passthrough fields
const stateWithExtras = state as AutofixState & {
blocks?: (WithArtifacts & StepWithSolution)[];
steps?: (WithArtifacts & StepWithSolution)[];
};
// Search blocks first (explorer mode / newer API)
if (stateWithExtras.blocks) {
const stepLevel = searchContainersForStepLevelSolution(
stateWithExtras.blocks
);
if (stepLevel) {
return stepLevel;
}
const artifactLevel = searchContainersForSolution(stateWithExtras.blocks);
if (artifactLevel) {
return artifactLevel;
}
}
// Search steps (regular autofix API)
if (stateWithExtras.steps) {
const stepLevel = searchContainersForStepLevelSolution(
stateWithExtras.steps
);
if (stepLevel) {
return stepLevel;
}
const artifactLevel = searchContainersForSolution(stateWithExtras.steps);
if (artifactLevel) {
return artifactLevel;
}
}
return null;
}
/**
* Extract the reason why no solution was produced.
*
* Searches blocks and steps for a no-solution reason in two places:
* 1. Step-level: `step.key === "solution"` with empty `solution[]` and a
* `description` explaining why (current API format).
* 2. Artifact-level: `artifact.key === "solution"` with a `reason` field
* (legacy format, kept as fallback).
*
* @param state - Autofix state (may contain blocks or steps with solution data)
* @returns Reason string if found, undefined otherwise
*/
export function extractNoSolutionReason(
state: AutofixState
): string | undefined {
const stateWithExtras = state as AutofixState & {
blocks?: (WithArtifacts & StepWithSolution)[];
steps?: (WithArtifacts & StepWithSolution)[];
};
// Search blocks first (explorer mode / newer API)
if (stateWithExtras.blocks) {
const stepLevel = searchContainersForStepLevelNoSolutionReason(
stateWithExtras.blocks
);
if (stepLevel) {
return stepLevel;
}
const artifactLevel = searchContainersForNoSolutionReason(
stateWithExtras.blocks
);
if (artifactLevel) {
return artifactLevel;
}
}
// Search steps (regular autofix API)
if (stateWithExtras.steps) {
const stepLevel = searchContainersForStepLevelNoSolutionReason(
stateWithExtras.steps
);
if (stepLevel) {
return stepLevel;
}
const artifactLevel = searchContainersForNoSolutionReason(
stateWithExtras.steps
);
if (artifactLevel) {
return artifactLevel;
}
}
return;
}
/**
* Extract file paths examined during root cause analysis.
*
* Collects file paths from reproduction steps across all root causes.
*
* @param causes - Array of root causes from the autofix state
* @returns Deduplicated array of file paths, or empty array if none found
*/
export function extractExaminedFiles(causes: RootCause[]): string[] {
const files = new Set<string>();
for (const cause of causes) {
if (cause.root_cause_reproduction) {
for (const step of cause.root_cause_reproduction) {
const path = step.relevant_code_file?.file_path;
if (path && path !== "N/A") {
files.add(path);
}
}
}
}
return [...files];
}