Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 15 additions & 14 deletions actions/setup/js/notify_comment_error.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ function getRunFailureStatusText(agentConclusion, assignToAgentErrorCount, safeO
* @param {Array<{type?: string, message?: unknown, reason?: unknown} | null>} items
*/
function appendAgentReports(children, items) {
/** @type {import("./report_ast.cjs").ReportBlock[]} */
const reports = [];
for (const item of items) {
let summary;
let text;
Expand Down Expand Up @@ -154,18 +156,17 @@ function appendAgentReports(children, items) {
core.warning("Omitting an agent explanation flagged by credential screening");
continue;
}
// Unlike top-level status sections, headings nested in details start at level 3.
const blocks = parseReportMarkdown(sanitizeContent(text), { agentTainted: true });
if (!blocks.length) continue;
/** @type {import("./report_ast.cjs").ReportBlock} */
const details = { type: "details", summary, children: blocks, agentTainted: true };
// Validate with the wrapper in place so nested depth is checked before publication.
normalizeReport({ type: "report", title: "Workflow status", children: [details] });
children.push(details);
normalizeReport({ type: "report", title: "Workflow status", children: blocks });
reports.push({ type: "paragraph", children: [{ type: "strong", children: [{ type: "text", value: summary, agentTainted: false }], agentTainted: false }], agentTainted: false }, ...blocks);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c196154: completion comments now cap valid explanations and indicate when additional messages are omitted. Regression tests cover oversized collections and size redaction.

} catch {
core.warning("Omitting an invalid agent explanation from the completion comment");
}
}
if (reports.length) {
children.push({ type: "heading", level: 2, children: [{ type: "text", value: "Agent messages", agentTainted: false }], agentTainted: false }, ...reports);
}
}

async function main() {
Expand Down Expand Up @@ -309,6 +310,14 @@ async function main() {
// consume the following status sections or generated metadata.
children.push(...detectionBlocks);

// The detector may accept a skipped run, so also require an explicit clean verdict.
if (!staged && process.env.GH_AW_DETECTION_ACCEPTED === "true" && detectionConclusion === "success" && agentOutputResult.success) {
appendAgentReports(
children,
agentOutputResult.items.filter(item => typeof item?.type === "string" && canPublishReport(item.type))
);
}

// Preserve links (e.g. "Pull request created: #X") that safe outputs appended to the
// activation comment; otherwise this final status update would overwrite them.
if (activationCommentLinks) {
Expand All @@ -323,14 +332,6 @@ async function main() {
}
}

// The detector may accept a skipped run, so also require an explicit clean verdict.
if (!staged && process.env.GH_AW_DETECTION_ACCEPTED === "true" && detectionConclusion === "success" && agentOutputResult.success) {
appendAgentReports(
children,
agentOutputResult.items.filter(item => typeof item?.type === "string" && canPublishReport(item.type))
);
}

// Collect generated asset URLs from safe output jobs
const generatedAssets = collectGeneratedAssets();
if (generatedAssets.length > 0) {
Expand Down
63 changes: 59 additions & 4 deletions actions/setup/js/notify_comment_error.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ const mockCore = {
for (const item of items.slice(0, -1)) {
expect(body.includes(item.message || item.reason)).toBe(published);
}
expect(body.includes("<summary>Continuation requested</summary>")).toBe(published);
expect(body.includes("## Agent messages")).toBe(published);
expect(body.includes("**Continuation requested**")).toBe(published);
expect(body).not.toContain("<summary>No-op message</summary>");
if (detectionDisabled) expect(body).toContain("completed successfully");
expect(body).not.toContain("Unrequested details");
expect(body).not.toContain("Unrelated safe output");
Expand All @@ -247,7 +249,22 @@ const mockCore = {
});
}),
describe("rendering accepted agent explanations", () => {
async function renderAcceptedExplanations(items) {
it("keeps agent content tainted under framework-authored section labels", () => {
const children = eval(`(() => {
${notifyCommentScript}
const children = [];
appendAgentReports(children, [{ type: "noop", message: "No changes needed." }]);
return children;
})()`);

expect(children).toMatchObject([
{ type: "heading", level: 2, agentTainted: false, children: [{ type: "text", value: "Agent messages", agentTainted: false }] },
{ type: "paragraph", agentTainted: false, children: [{ type: "strong", agentTainted: false }] },
{ type: "paragraph", agentTainted: true, children: [{ type: "text", value: "No changes needed.", agentTainted: true }] },
]);
});

async function renderAcceptedExplanations(items, noopCommentBody) {
const fixtureDir = fs.mkdtempSync(path.join(process.cwd(), "gh-aw-explanation-rendering-"));
try {
const outputPath = path.join(fixtureDir, "agent-output.json");
Expand All @@ -261,6 +278,7 @@ const mockCore = {
delete process.env.GH_AW_SAFE_OUTPUTS_RESULT;
delete process.env.GH_AW_NOOP_COMMENT_BODY;
delete process.env.GH_AW_SAFE_OUTPUT_MESSAGES;
if (noopCommentBody) process.env.GH_AW_NOOP_COMMENT_BODY = noopCommentBody;

await eval(`(async () => { ${notifyCommentScript}; await main(); })()`);

Expand All @@ -281,7 +299,7 @@ const mockCore = {
message: "# No changes\n\nNotify @some-org/some-team <b>if needed</b>. [Health](https://api.example.com/health) [Blocked](https://blocked.example.net/health)",
},
]);
expect(body).toContain("<summary>No-op message</summary>\n\n### No changes\n");
expect(body).toContain("## Agent messages\n\n**No-op message**\n\n### No changes\n");
expect(body).toContain("Notify `@some-org/some-team` **if needed**.");
expect(body.replace("`@some-org/some-team`", "")).not.toContain("@some-org/some-team");
expect(body).not.toMatch(/<\/?b>/);
Expand All @@ -292,12 +310,49 @@ const mockCore = {
}
});

it("shows multiline agent messages before links, categorized outcomes, and attribution", async () => {
process.env.GH_AW_ACTIVATION_COMMENT_LINKS = "Pull request created: [#900](https://github.com/owner/repo/pull/900)";
const body = await renderAcceptedExplanations(
[
{ type: "noop", message: "No findings.\n\nReviewed all changed contracts." },
{ type: "report_missing_data", reason: "No input was provided." },
],
"### test-workflow\n\n**Outcome:** `reported_action`\n\nStructured outcome explanation."
);

expect(body).toContain("## Agent messages\n\n**No-op message**\n\nNo findings.\n\nReviewed all changed contracts.\n\n**Missing data**\n\nNo input was provided.");
expect(body).not.toContain("<details>");
expect(body.indexOf("completed successfully")).toBeLessThan(body.indexOf("## Agent messages"));
expect(body.indexOf("No input was provided.")).toBeLessThan(body.indexOf("Pull request created:"));
expect(body.indexOf("Pull request created:")).toBeLessThan(body.indexOf("**Outcome:**"));
expect(body.indexOf("**Outcome:**")).toBeLessThan(body.indexOf("> Generated by"));
expect(body.match(/<!-- gh-aw-agentic-workflow:/g)).toHaveLength(1);
});

it("preserves agent-authored alerts without nesting them in another alert", async () => {
const body = await renderAcceptedExplanations([{ type: "report_incomplete", reason: "> [!WARNING]\n> Review did not finish." }]);

expect(body).toContain("## Agent messages\n\n**Incomplete work**\n\n> [!WARNING]\n> Review did not finish.");
expect(body).not.toContain("> > [!");
});

it("omits the info section when there are no publishable explanations", async () => {
const body = await renderAcceptedExplanations([
{ type: "noop", message: " " },
{ type: "add_comment", body: "Unrelated output" },
]);

expect(body).not.toContain("## Agent messages");
expect(body).not.toContain("**No-op message**");
expect(body).toContain("completed successfully");
});

it("masks recognized credentials in the published explanation", async () => {
const pat = `ghp_${"A".repeat(36)}`;
const awsKey = `AKIA${"B".repeat(16)}`;
const body = await renderAcceptedExplanations([{ type: "noop", message: `Credential diagnostics:\n\n\`\`\`text\n${pat}\n${awsKey}\n\`\`\`` }]);

expect(body).toContain("<summary>No-op message</summary>");
expect(body).toContain("**No-op message**");
expect(body).toContain("Credential diagnostics:");
expect(body).toContain("***REDACTED***\n***REDACTED***");
expect(body.includes(pat)).toBe(false);
Expand Down
Loading