feat(run): Diagnose with Agent on a failed test (#4744)

A failed test's context menu in the test tree gets Diagnose with Agent while the agents feature is active. It appends the failure (test name, source location reference, expected/actual when the framework names them, first 60 output lines) to the agent message typed last and opens it for review.
This commit is contained in:
l0ng-ai
2026-11-20 16:54:20 +08:00
committed by GitHub
parent f325e8c9d6
commit e20e7b04bc
8 changed files with 209 additions and 9 deletions
+17
View File
@@ -28,6 +28,7 @@ import { AgentsStatus } from "./AgentsStatus";
import { activateSettingsPages } from "./settingsPages";
import { HOME_KIND, MCP_PAGE, REGISTRY_PAGE, SESSION_KIND, TASK_DRAG, TOOL_WINDOW, providerIcon } from "./model";
import { sendPaths, sendSelection, trackEditorCopies } from "./references";
import { diagnoseTestFailure, isTestFailure } from "./testFailure";
import { focusedSession, inputSession } from "./sessionFocus";
import { cycleFocusedEffort } from "./effortFocus";
import { clearAgentTabFontZoom } from "./fontZoom";
@@ -307,6 +308,22 @@ export function activate(): () => void {
else sendSelection();
},
},
{
id: "agents.diagnoseTestFailure",
title: "Diagnose with Agent",
description:
"Adds a failed test's name, source location, expected and actual values and output to the agent message typed last, to review and send.",
category: "Agents",
icon: "toolwindow-agents",
when: "projectOpen",
keywords: ["agent", "test", "failure", "diagnose", "explain", "fix"],
// Run from the test tree, which passes the failure.
hidden: true,
enabled: ({ arg }) => isTestFailure(arg),
run: ({ arg }) => {
if (isTestFailure(arg)) diagnoseTestFailure(arg);
},
},
{
id: "agents.generateCommitMessage",
title: "Generate Commit Message with Agent",
+20 -3
View File
@@ -1600,6 +1600,18 @@ export interface ReferenceTarget {
* how many were added; a path outside every project shows a notice.
*/
export function addReferences(targets: readonly ReferenceTarget[]): number {
return appendToComposer((mentions) => mentions.join(" "), targets);
}
/**
* Appends text to the agent message typed last (a task's or New Task's) and opens it; nothing is
* sent. `compose` gets the `@` mentions of `targets` (those inside this window's projects, which
* also become chips). Returns how many targets were referenced.
*/
export function appendToComposer(
compose: (mentions: readonly string[]) => string,
targets: readonly ReferenceTarget[] = [],
): number {
const s = useAgents.getState();
const key = activeComposerKey();
const task = key === HOME ? null : s.tasks.get(key)!;
@@ -1623,9 +1635,14 @@ export function addReferences(targets: readonly ReferenceTarget[]): number {
}
if (files.length < targets.length)
notifications().notify({ level: "info", title: tx("Only files of this window's projects can be referenced") });
if (!files.length) return 0;
const joined = mentions.join(" ");
const text = current.text && !/\s$/.test(current.text) ? `${current.text} ${joined} ` : `${current.text}${joined} `;
const joined = compose(mentions);
if (!joined) return 0;
// A block of several lines (a test failure) goes in a paragraph of its own.
const text = joined.includes("\n")
? `${current.text.trimEnd() ? `${current.text.trimEnd()}\n\n` : ""}${joined}\n`
: current.text && !/\s$/.test(current.text)
? `${current.text} ${joined} `
: `${current.text}${joined} `;
updateComposer(key, { text, files: [...current.files, ...files] });
if (task) openTask(task.id);
else openHome();
+42
View File
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { EXCERPT_LINES, failureDraft, isTestFailure } from "./testFailure";
describe("Diagnose with Agent draft (#4716)", () => {
it("names the test, its source reference and the compared values", () => {
const draft = failureDraft(
{
name: "OrderControllerTest > creates()",
expected: "201",
actual: "403",
output: ["", "Status expected:<201> but was:<403>", ""],
},
"@src/test/java/OrderControllerTest.java:42",
);
expect(draft).toBe(
[
"The test OrderControllerTest > creates() failed. Help me find out why.",
"Test source: @src/test/java/OrderControllerTest.java:42",
"Expected: 201\nActual: 403",
"```\nStatus expected:<201> but was:<403>\n```",
].join("\n\n"),
);
});
it("shortens a long excerpt and says how much it left out", () => {
const output = Array.from({ length: EXCERPT_LINES + 5 }, (_, i) => `at frame${i}`);
const draft = failureDraft({ name: "t", output }, undefined);
expect(draft).toContain(`at frame${EXCERPT_LINES - 1}\n… 5 more lines\n\`\`\``);
expect(draft).not.toContain(`at frame${EXCERPT_LINES}\n`);
expect(draft).not.toContain("Test source");
});
it("fences output that holds backticks with a longer fence", () => {
expect(failureDraft({ name: "t", output: ["```js", "x"] }, undefined)).toContain("````\n```js\nx\n````");
});
it("accepts only a failure argument", () => {
expect(isTestFailure({ name: "t", output: [] })).toBe(true);
expect(isTestFailure(undefined)).toBe(false);
expect(isTestFailure({ name: "t" })).toBe(false);
});
});
+53
View File
@@ -0,0 +1,53 @@
/** "Diagnose with Agent" on a failed test: its failure as an editable draft for the agent. */
import { tx } from "@/platform/i18n";
import { appendToComposer } from "./store";
/** The `arg` of `agents.diagnoseTestFailure`, from the run's test tree. */
export interface TestFailure {
name: string;
/** The test's source file (absolute), when the run could resolve it. */
path?: string | null;
/** 1-based. */
line?: number;
expected?: string;
actual?: string;
/** What the run captured for this test: its output and failure details. */
output: readonly string[];
}
/** Excerpt lines kept in the draft; the rest is counted. */
export const EXCERPT_LINES = 60;
export function isTestFailure(arg: unknown): arg is TestFailure {
const a = arg as Partial<TestFailure> | null;
return !!a && typeof a.name === "string" && Array.isArray(a.output);
}
/** The draft text; `location` is the source reference (`@path:line`), if any. */
export function failureDraft(failure: TestFailure, location: string | undefined): string {
const parts = [tx`The test ${failure.name} failed. Help me find out why.`];
if (location) parts.push(tx`Test source: ${location}`);
if (failure.expected !== undefined && failure.actual !== undefined)
parts.push(`${tx("Expected:")} ${failure.expected}\n${tx("Actual:")} ${failure.actual}`);
const lines = failure.output.map((l) => l.trimEnd());
while (lines.length && !lines[0]) lines.shift();
while (lines.length && !lines.at(-1)) lines.pop();
if (lines.length) {
const shown = lines.slice(0, EXCERPT_LINES);
const more = lines.length - shown.length;
if (more > 0) shown.push(tx`… ${more} more lines`);
// A fence longer than any backtick run inside keeps the excerpt in one block.
const longest = Math.max(0, ...shown.map((l) => l.match(/`+/g)?.reduce((m, r) => Math.max(m, r.length), 0) ?? 0));
const fence = "`".repeat(Math.max(3, longest + 1));
parts.push(`${fence}\n${shown.join("\n")}\n${fence}`);
}
return parts.join("\n\n");
}
/** Appends the failure to the agent message typed last and opens it; the user reviews it and sends it. */
export function diagnoseTestFailure(failure: TestFailure): void {
const target = failure.path
? [{ path: failure.path, lines: failure.line ? { from: failure.line, to: failure.line } : undefined }]
: [];
appendToComposer((mentions) => failureDraft(failure, mentions[0]), target);
}
+29 -1
View File
@@ -8,7 +8,10 @@ import { TestResults } from "./testing";
import { TestTreeView } from "./TestTreeView";
import { testTree } from "./testTree";
const nav = vi.hoisted(() => ({ openTarget: vi.fn(async () => {}) }));
const nav = vi.hoisted(() => ({
openTarget: vi.fn(async () => {}),
resolveTarget: vi.fn(async () => "/p/src/test/java/MathTest.java"),
}));
vi.mock("./navigate", () => nav);
const actions = vi.hoisted(() => ({ executeAction: vi.fn(async () => {}) }));
vi.mock("@/platform/actions", async (orig) => ({ ...(await orig<object>()), executeAction: actions.executeAction }));
@@ -97,6 +100,31 @@ describe("TestTreeView", () => {
);
});
it("context menu: Diagnose with Agent passes the selected failure to the agents action (#4716)", async () => {
// Offered only while the agents feature provides the action.
render(<Host />);
fireEvent.contextMenu(screen.getByText("subs()"));
expect(screen.queryByText("Diagnose with Agent")).toBeNull();
fireEvent.keyDown(document.activeElement ?? document.body, { key: "Escape" });
onTestFinished(
registerActions([{ id: "agents.diagnoseTestFailure", title: "Diagnose with Agent", run: () => {} }]),
);
// A suite is not one test.
fireEvent.contextMenu(screen.getByText("MathTest"));
expect(screen.queryByText("Diagnose with Agent")).toBeNull();
fireEvent.keyDown(document.activeElement ?? document.body, { key: "Escape" });
fireEvent.contextMenu(screen.getByText("subs()"));
await act(async () => fireEvent.click(screen.getByText("Diagnose with Agent")));
expect(actions.executeAction).toHaveBeenCalledWith("agents.diagnoseTestFailure", {
name: "subs()",
path: "/p/src/test/java/MathTest.java",
line: 9,
expected: "1",
actual: "2",
output: ["org.opentest4j.AssertionFailedError: expected: <1> but was: <2>"],
});
});
it.each([true, false])(
"enables Run and Debug only when the async selector returns support (%s)",
async (supported) => {
+32 -3
View File
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useRef, useState, type MouseEvent } from "react";
import clsx from "clsx";
import { executeAction } from "@/platform/actions";
import { executeAction, getAction } from "@/platform/actions";
import {
findOccurrence,
Icon,
@@ -15,14 +15,24 @@ import {
type TreeHandle,
} from "@/ui";
import { useOccurrenceNavigator } from "@/workbench/contributions";
import { openTarget } from "./navigate";
import { formatDuration, nodeDuration, nodeStatus, type TestNode, type TestResults, type TestStatus } from "./testing";
import { openTarget, resolveTarget } from "./navigate";
import {
formatDuration,
nodeDuration,
nodeOutput,
nodeStatus,
type TestNode,
type TestResults,
type TestStatus,
} from "./testing";
import { parseComparison } from "./testing/comparison";
import { TEST_TREE_FOCUS, testTree } from "./testTree";
import { ExportTestResultsDialog } from "./ExportTestResultsDialog";
import styles from "./Run.module.css";
import { tx } from "@/platform/i18n";
const DIAGNOSE = "agents.diagnoseTestFailure";
/** Test tree filters and sorting, kept across runs and restarts (IDEA hides passed tests by default). */
const FILTERS_KEY = "openidea.run.testFilters";
@@ -215,6 +225,23 @@ export function TestTreeView({
if (target) void openTarget(target, cwd, true);
};
// The agents feature's action: the failure becomes a draft the user reviews and sends.
const diagnose = async (n: TestNode, name: string) => {
const located = n.location ?? firstLocated(n)?.location;
const path = located ? await resolveTarget(located, cwd).catch(() => null) : null;
// Rust's `left` / `right` don't say which side is expected; the output excerpt shows them as written.
const parsed = parseComparison(n.output);
const comparison = parsed?.labels ? null : parsed;
await executeAction(DIAGNOSE, {
name,
path,
line: path ? located?.line : undefined,
expected: comparison?.expected,
actual: comparison?.actual,
output: nodeOutput(n),
});
};
const contextMenu = (node: TestNode | null, sel: Set<string>, e: MouseEvent) => {
if (!node) return;
const ids = sel.has(node.id) ? [...sel] : [node.id];
@@ -245,6 +272,8 @@ export function TestTreeView({
}
if (ids.length === 1 && nodeStatus(node) === "failed" && parseComparison(node.output))
items.push({ label: "Show Diff", icon: "diff", run: () => void showTestDiff(node) });
if (ids.length === 1 && !node.children.length && nodeStatus(node) === "failed" && getAction(DIAGNOSE))
items.push({ label: "Diagnose with Agent", icon: "toolwindow-agents", run: () => void diagnose(node, name) });
items.push({
label: "Jump to Source",
icon: "external-link",
+8 -1
View File
@@ -2776,5 +2776,12 @@
"rename it back to {name}": "rename it back to {name}",
"restore {name} and check out the previous branch again": "restore {name} and check out the previous branch again",
"Unset upstream of {to} ({root})": "Unset upstream of {to} ({root})",
"Group by Repository": "Group by Repository"
"Group by Repository": "Group by Repository",
"Diagnose with Agent": "Diagnose with Agent",
"Adds a failed test's name, source location, expected and actual values and output to the agent message typed last, to review and send.": "Adds a failed test's name, source location, expected and actual values and output to the agent message typed last, to review and send.",
"The test {name} failed. Help me find out why.": "The test {name} failed. Help me find out why.",
"Test source: {location}": "Test source: {location}",
"Expected:": "Expected:",
"Actual:": "Actual:",
"… {more} more lines": "… {more} more lines"
}
+8 -1
View File
@@ -9139,5 +9139,12 @@
"{name} changed on disk": "{name} 已在磁盘上更改",
"The file was changed outside the IDE while it has unsaved changes in the editor. Saving keeps the editor's version and overwrites the file.": "该文件在 IDE 外被修改,而编辑器中还有未保存的更改。保存会保留编辑器中的版本并覆盖该文件。",
"Load Changes from Disk": "从磁盘加载更改",
"Keep Editor Changes": "保留编辑器中的更改"
"Keep Editor Changes": "保留编辑器中的更改",
"Diagnose with Agent": "用智能体诊断",
"Adds a failed test's name, source location, expected and actual values and output to the agent message typed last, to review and send.": "把失败测试的名称、源码位置、期望值与实际值和输出添加到最近编辑的智能体消息中,检查后再发送。",
"The test {name} failed. Help me find out why.": "测试 {name} 失败了,帮我找出原因。",
"Test source: {location}": "测试源码:{location}",
"Expected:": "期望:",
"Actual:": "实际:",
"… {more} more lines": "… 另有 {more} 行"
}