Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/cli/src/commands/issue/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "../../lib/list-command.js";
import type { IssueArgument } from "../../lib/issue-arguments.js";
import { logger } from "../../lib/logger.js";
import { extractRootCauses } from "../../types/seer.js";
import { extractRootCauses, isSingleRun } from "../../types/seer.js";
import { type IssuesArguments, issuesPositional } from "./positional.js";
import {
ensureRootCauseAnalysis,
Expand Down Expand Up @@ -71,7 +71,13 @@ async function analyzeIssue(
);
}

return { issue: issueArg.text, org, issueId, rootCauses };
return {
issue: issueArg.text,
org,
issueId,
rootCauses,
singleRun: isSingleRun(state),
};
} catch (error) {
if (error instanceof ApiError) {
throw handleSeerApiError(
Expand Down Expand Up @@ -194,7 +200,7 @@ export const explainCommand = buildCommand({
return isBatch
? undefined
: {
hint: `To create a plan, run: ${sentry`issue plan ${primaryArg}`}`,
hint: `${results[0]?.singleRun ? "To see the fix" : "To create a plan"}, run: ${sentry`issue plan ${primaryArg}`}`,
};
},
});
112 changes: 112 additions & 0 deletions packages/cli/src/commands/issue/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
*/

import { isatty } from "node:tty";
import { text } from "@sentry/toolkit-core/markdown";
import type { SentryContext } from "../../context.js";
import { triggerSolutionPlanning } from "../../lib/api-client.js";
import { sentry } from "../../lib/app-identity.js";
import { issueSelectorHelp } from "../../lib/arg-parsing.js";
import { buildCommand } from "../../lib/command.js";
import { ApiError, CliError, SeerError } from "../../lib/errors.js";
import { renderMarkdown } from "../../lib/formatters/markdown.js";
import { CommandOutput } from "../../lib/formatters/output.js";
import {
formatSolution,
Expand All @@ -26,11 +28,19 @@ import { logger } from "../../lib/logger.js";
import { prompt } from "../../lib/prompt.js";
import {
type AutofixState,
type CodeChange,
extractCodeChanges,
extractExaminedFiles,
extractNoSolutionReason,
extractPullRequests,
extractRootCauses,
extractSingleRunStop,
extractSolution,
isSingleRun,
isTerminalStatus,
requireAutofixRunId,
type PullRequestLink,
type SingleRunStop,
type SolutionArtifact,
} from "../../types/seer.js";
import {
Expand Down Expand Up @@ -69,15 +79,60 @@ type PlanData = {
solution: SolutionArtifact["data"] | null;
/** Context about why no solution was produced. Only present when solution is null. */
no_solution_context?: NoSolutionContext;
code_changes?: CodeChange[];
pull_requests?: PullRequestLink[];
stop?: SingleRunStop;
};

function formatSingleRunFix(data: PlanData, changes: CodeChange[]): string {
const lines: string[] = [];
if (data.no_solution_context?.root_cause) {
lines.push(
`**Root cause:** ${text(data.no_solution_context.root_cause)}`,
"",
);
}

if (changes.length > 0) {
lines.push("## Code changes", "");
for (const repo of new Set(changes.map((change) => change.repo))) {
lines.push(`**${text(repo)}**`, "");
for (const change of changes.filter((c) => c.repo === repo)) {
lines.push(
`- ${text(change.path)} (+${change.added} −${change.removed})`,
);
}
lines.push("");
}
} else if (data.stop) {
lines.push(`**Seer stopped:** ${text(data.stop.reason)}`, "");
lines.push(text(data.stop.suggestion), "");
} else {
lines.push(
"Seer stopped after the root cause without changing code, for example because of the project's settings. Open the issue in Sentry for details.",
);
}

if (data.pull_requests?.length) {
lines.push("## Pull requests", "");
for (const pr of data.pull_requests) {
lines.push(`- ${text(pr.url)}`);
}
}

return renderMarkdown(lines.join("\n"));
}

/**
* Format solution plan data for human-readable terminal output.
*
* Returns the formatted solution, or a contextual message explaining
* why no solution was produced.
*/
function formatPlanOutput(data: PlanData): string {
if (data.code_changes) {
return formatSingleRunFix(data, data.code_changes);
}
if (data.solution) {
return formatSolution({ key: "solution", data: data.solution });
}
Expand Down Expand Up @@ -165,6 +220,51 @@ function buildPlanData(state: AutofixState): PlanData {
return data;
}

async function waitForSingleRun(
state: AutofixState,
org: string,
issueId: string,
json: boolean,
issueText: string,
): Promise<PlanData> {
const finalState = isTerminalStatus(state.status)
? state
: await pollAutofixState({
orgSlug: org,
issueId,
json,
timeoutMessage:
"Seer is still working on the fix after 6 minutes. Try again or check the issue in Sentry web UI.",
timeoutHint:
"The fix may still be in progress.\n" +
` Or retry: ${sentry`issue plan ${issueText}`}`,
});

if (finalState.status === "ERROR") {
if (finalState.failure_reason === "out_of_credits") {
throw new SeerError("out_of_credits", org);
}
throw new Error("Autofix failed. Check the Sentry web UI for details.");
}
if (finalState.status === "CANCELLED") {
throw new Error("Autofix was cancelled.");
}

const data: PlanData = {
...buildPlanData(finalState),
code_changes: extractCodeChanges(finalState),
};
const pullRequests = extractPullRequests(finalState);
if (pullRequests.length > 0) {
data.pull_requests = pullRequests;
}
const stop = extractSingleRunStop(finalState);
if (stop) {
data.stop = stop;
}
return data;
}

/**
* Root cause analysis paused waiting for the user to confirm the identified root
* cause before solution planning. When the session is interactive (a TTY and not
Expand Down Expand Up @@ -284,6 +384,18 @@ export const planCommand = buildCommand({
json: flags.json,
});

if (isSingleRun(state)) {
return yield new CommandOutput(
await waitForSingleRun(
state,
org,
numericId,
flags.json,
issueArg.text,
),
);
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

// Root cause analysis can pause waiting for the user to confirm the
// root cause before planning. When running interactively, confirm in the
// terminal and continue; otherwise point them at the Sentry UI.
Expand Down
14 changes: 13 additions & 1 deletion packages/cli/src/commands/issue/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ import { buildIssueUrl } from "../../lib/sentry-web-urls.js";
import { setOrgProjectContext } from "../../lib/telemetry.js";
import { isAllDigits, shellQuote } from "../../lib/utils.js";
import type { SentryIssue } from "../../types/index.js";
import { type AutofixState, isTerminalStatus } from "../../types/seer.js";
import {
type AutofixState,
extractRootCauses,
isSingleRun,
isTerminalStatus,
} from "../../types/seer.js";

const log = logger.withTag("issue.utils");

Expand Down Expand Up @@ -1152,6 +1157,13 @@ function shouldStopPolling(
if (isTerminalStatus(state.status)) {
return true;
}
if (
stopOnWaitingForUser &&
isSingleRun(state) &&
extractRootCauses(state).length > 0
) {
return true;
}
if (
stopOnWaitingForUser &&
(state.status === "WAITING_FOR_USER_RESPONSE" ||
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/formatters/seer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export type IssueExplainResult = {
issueId: string;
/** Root causes returned by Seer. */
rootCauses: RootCause[];
singleRun?: boolean;
};

/** Aggregate root-cause output for single- and multi-issue invocations. */
Expand Down
86 changes: 83 additions & 3 deletions packages/cli/src/types/seer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const STOPPING_POINTS = [

export type StoppingPoint = (typeof STOPPING_POINTS)[number];

const SINGLE_RUN_FEATURE_ID = "autofix_single_run";

// Progress Message

export const ProgressMessageSchema = object({
Expand Down Expand Up @@ -189,6 +191,7 @@ export const AutofixStateSchema = looseObject({
run_id: optional(number()),
/** Null for legacy runs predating this field. */
sentry_run_id: optional(nullable(string())),
feature_id: optional(string()),
status: string(),
/**
* Why a failed run stopped, e.g. `out_of_credits`. Null when the run didn't
Expand Down Expand Up @@ -264,6 +267,10 @@ export function isTerminalStatus(status: string): boolean {
return TERMINAL_STATUSES.includes(status as AutofixStatus);
}

export function isSingleRun(state: AutofixState): boolean {
return state.feature_id === SINGLE_RUN_FEATURE_ID;
}

// At least one of sentry_run_id/run_id is always present on a non-null
// autofix state, so a missing run ID means a malformed response.
export function requireAutofixRunId(state: AutofixState): string | number {
Expand Down Expand Up @@ -309,6 +316,7 @@ type AgentRootCauseData = {
five_whys?: string[];
reproduction_steps?: string[];
relevant_repo?: string | null;
relevant_repos?: string[];
};

/**
Expand Down Expand Up @@ -337,9 +345,9 @@ function searchBlocksForAgentRootCause(
id: 0,
description,
cause: agentData.cause,
relevant_repos: agentData.relevant_repo
? [agentData.relevant_repo]
: undefined,
relevant_repos:
agentData.relevant_repos ??
(agentData.relevant_repo ? [agentData.relevant_repo] : undefined),
};
return [cause];
}
Expand Down Expand Up @@ -637,6 +645,78 @@ function findNoSolutionReasonInContainers(
return searchContainersForNoSolutionReason(containers);
}

export type CodeChange = {
repo: string;
path: string;
type: string;
added: number;
removed: number;
};

export type SingleRunStop = { reason: string; suggestion: string };

type MergedFilePatch = {
repo_name: string;
patch: { path: string; type: string; added: number; removed: number };
};

type SingleRunBlock = WithArtifacts & {
merged_file_patches?: MergedFilePatch[];
};

function singleRunBlocks(state: AutofixState): SingleRunBlock[] {
return (state as { blocks?: SingleRunBlock[] }).blocks ?? [];
}

export function extractCodeChanges(state: AutofixState): CodeChange[] {
const byFile = new Map<string, CodeChange>();
for (const block of singleRunBlocks(state)) {
for (const { repo_name, patch } of block.merged_file_patches ?? []) {
byFile.set(`${repo_name}:${patch.path}`, {
repo: repo_name,
path: patch.path,
type: patch.type,
added: patch.added,
removed: patch.removed,
});
}
}
return [...byFile.values()];
}

export type PullRequestLink = {
repo: string | undefined;
url: string;
number: number | undefined;
};

export function extractPullRequests(state: AutofixState): PullRequestLink[] {
const prStates =
(state as { repo_pr_states?: Record<string, unknown> }).repo_pr_states ??
{};
return Object.values(prStates).flatMap((prState) => {
const result = safeParse(PullRequestInfoSchema, prState);
if (!(result.success && result.output.pr_url)) {
return [];
}
const { repo_name, pr_url, pr_number } = result.output;
return [{ repo: repo_name, url: pr_url, number: pr_number }];
});
}

export function extractSingleRunStop(
state: AutofixState,
): SingleRunStop | undefined {
const stops = singleRunBlocks(state)
.flatMap((block) => block.artifacts ?? [])
.filter((artifact) => artifact.key === "stop");
const data = stops.at(-1)?.data as Partial<SingleRunStop> | undefined;
if (typeof data?.reason === "string" && typeof data.suggestion === "string") {
return { reason: data.reason, suggestion: data.suggestion };
}
return;
}

/**
* Extract file paths examined during root cause analysis.
*
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/test/commands/issue/explain.func.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ describe("issue explain multiple IDs", () => {
});
});

test("points single runs at the fix in the closing hint", async () => {
resolveSpy.mockResolvedValue({ org: "test-org", issueId: "1" });
analyzeSpy.mockResolvedValue({
...sampleState(sampleCause("Single cause")),
feature_id: "autofix_single_run",
});

const { context, stdoutWrite } = createMockContext();
const func = await loadCommand(explainCommand);
await func.call(context, { ...EXPLAIN_FLAGS, json: false }, "IOS-1");

const output = stdoutWrite.mock.calls.map((call) => call[0]).join("");
expect(output).toContain("To see the fix, run: sentry issue plan IOS-1");
});

test("fails to parse when no issue ID is provided", async () => {
const func = await loadCommand(explainCommand);
await expect(
Expand Down
Loading
Loading