Skip to content

Commit 88f9bb5

Browse files
authored
Surface agent messages near the top of completion comments (#80783)
1 parent db4d903 commit 88f9bb5

2 files changed

Lines changed: 124 additions & 18 deletions

File tree

‎actions/setup/js/notify_comment_error.cjs‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ function getRunFailureStatusText(agentConclusion, assignToAgentErrorCount, safeO
120120
* @param {Array<{type?: string, message?: unknown, reason?: unknown} | null>} items
121121
*/
122122
function appendAgentReports(children, items) {
123+
const maxReports = 50;
124+
let reportCount = 0;
125+
let omitted = false;
126+
/** @type {import("./report_ast.cjs").ReportBlock[]} */
127+
const reports = [];
123128
for (const item of items) {
124129
let summary;
125130
let text;
@@ -154,18 +159,25 @@ function appendAgentReports(children, items) {
154159
core.warning("Omitting an agent explanation flagged by credential screening");
155160
continue;
156161
}
157-
// Unlike top-level status sections, headings nested in details start at level 3.
158162
const blocks = parseReportMarkdown(sanitizeContent(text), { agentTainted: true });
159163
if (!blocks.length) continue;
160-
/** @type {import("./report_ast.cjs").ReportBlock} */
161-
const details = { type: "details", summary, children: blocks, agentTainted: true };
162-
// Validate with the wrapper in place so nested depth is checked before publication.
163-
normalizeReport({ type: "report", title: "Workflow status", children: [details] });
164-
children.push(details);
164+
normalizeReport({ type: "report", title: "Workflow status", children: blocks });
165+
if (reportCount === maxReports) {
166+
omitted = true;
167+
break;
168+
}
169+
reports.push({ type: "paragraph", children: [{ type: "strong", children: [{ type: "text", value: summary, agentTainted: false }], agentTainted: false }], agentTainted: false }, ...blocks);
170+
reportCount++;
165171
} catch {
166172
core.warning("Omitting an invalid agent explanation from the completion comment");
167173
}
168174
}
175+
if (reports.length) {
176+
children.push({ type: "heading", level: 2, children: [{ type: "text", value: "Agent messages", agentTainted: false }], agentTainted: false }, ...reports);
177+
if (omitted) {
178+
children.push({ type: "paragraph", children: [{ type: "text", value: "Additional agent messages omitted.", agentTainted: false }], agentTainted: false });
179+
}
180+
}
169181
}
170182

171183
async function main() {
@@ -309,6 +321,14 @@ async function main() {
309321
// consume the following status sections or generated metadata.
310322
children.push(...detectionBlocks);
311323

324+
// The detector may accept a skipped run, so also require an explicit clean verdict.
325+
if (!staged && process.env.GH_AW_DETECTION_ACCEPTED === "true" && detectionConclusion === "success" && agentOutputResult.success) {
326+
appendAgentReports(
327+
children,
328+
agentOutputResult.items.filter(item => typeof item?.type === "string" && canPublishReport(item.type))
329+
);
330+
}
331+
312332
// Preserve links (e.g. "Pull request created: #X") that safe outputs appended to the
313333
// activation comment; otherwise this final status update would overwrite them.
314334
if (activationCommentLinks) {
@@ -323,14 +343,6 @@ async function main() {
323343
}
324344
}
325345

326-
// The detector may accept a skipped run, so also require an explicit clean verdict.
327-
if (!staged && process.env.GH_AW_DETECTION_ACCEPTED === "true" && detectionConclusion === "success" && agentOutputResult.success) {
328-
appendAgentReports(
329-
children,
330-
agentOutputResult.items.filter(item => typeof item?.type === "string" && canPublishReport(item.type))
331-
);
332-
}
333-
334346
// Collect generated asset URLs from safe output jobs
335347
const generatedAssets = collectGeneratedAssets();
336348
if (generatedAssets.length > 0) {

‎actions/setup/js/notify_comment_error.test.cjs‎

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ const mockCore = {
237237
for (const item of items.slice(0, -1)) {
238238
expect(body.includes(item.message || item.reason)).toBe(published);
239239
}
240-
expect(body.includes("<summary>Continuation requested</summary>")).toBe(published);
240+
expect(body.includes("## Agent messages")).toBe(published);
241+
expect(body.includes("**Continuation requested**")).toBe(published);
242+
expect(body).not.toContain("<summary>No-op message</summary>");
241243
if (detectionDisabled) expect(body).toContain("completed successfully");
242244
expect(body).not.toContain("Unrequested details");
243245
expect(body).not.toContain("Unrelated safe output");
@@ -247,7 +249,22 @@ const mockCore = {
247249
});
248250
}),
249251
describe("rendering accepted agent explanations", () => {
250-
async function renderAcceptedExplanations(items) {
252+
it("keeps agent content tainted under framework-authored section labels", () => {
253+
const children = eval(`(() => {
254+
${notifyCommentScript}
255+
const children = [];
256+
appendAgentReports(children, [{ type: "noop", message: "No changes needed." }]);
257+
return children;
258+
})()`);
259+
260+
expect(children).toMatchObject([
261+
{ type: "heading", level: 2, agentTainted: false, children: [{ type: "text", value: "Agent messages", agentTainted: false }] },
262+
{ type: "paragraph", agentTainted: false, children: [{ type: "strong", agentTainted: false }] },
263+
{ type: "paragraph", agentTainted: true, children: [{ type: "text", value: "No changes needed.", agentTainted: true }] },
264+
]);
265+
});
266+
267+
async function renderAcceptedExplanations(items, noopCommentBody) {
251268
const fixtureDir = fs.mkdtempSync(path.join(process.cwd(), "gh-aw-explanation-rendering-"));
252269
try {
253270
const outputPath = path.join(fixtureDir, "agent-output.json");
@@ -261,6 +278,7 @@ const mockCore = {
261278
delete process.env.GH_AW_SAFE_OUTPUTS_RESULT;
262279
delete process.env.GH_AW_NOOP_COMMENT_BODY;
263280
delete process.env.GH_AW_SAFE_OUTPUT_MESSAGES;
281+
if (noopCommentBody) process.env.GH_AW_NOOP_COMMENT_BODY = noopCommentBody;
264282

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

@@ -281,7 +299,7 @@ const mockCore = {
281299
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)",
282300
},
283301
]);
284-
expect(body).toContain("<summary>No-op message</summary>\n\n### No changes\n");
302+
expect(body).toContain("## Agent messages\n\n**No-op message**\n\n### No changes\n");
285303
expect(body).toContain("Notify `@some-org/some-team` **if needed**.");
286304
expect(body.replace("`@some-org/some-team`", "")).not.toContain("@some-org/some-team");
287305
expect(body).not.toMatch(/<\/?b>/);
@@ -292,12 +310,88 @@ const mockCore = {
292310
}
293311
});
294312

313+
it("shows multiline agent messages before links, categorized outcomes, and attribution", async () => {
314+
process.env.GH_AW_ACTIVATION_COMMENT_LINKS = "Pull request created: [#900](https://github.com/owner/repo/pull/900)";
315+
const body = await renderAcceptedExplanations(
316+
[
317+
{ type: "noop", message: "No findings.\n\nReviewed all changed contracts." },
318+
{ type: "report_missing_data", reason: "No input was provided." },
319+
],
320+
"### test-workflow\n\n**Outcome:** `reported_action`\n\nStructured outcome explanation."
321+
);
322+
323+
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.");
324+
expect(body).not.toContain("<details>");
325+
expect(body.indexOf("completed successfully")).toBeLessThan(body.indexOf("## Agent messages"));
326+
expect(body.indexOf("No input was provided.")).toBeLessThan(body.indexOf("Pull request created:"));
327+
expect(body.indexOf("Pull request created:")).toBeLessThan(body.indexOf("**Outcome:**"));
328+
expect(body.indexOf("**Outcome:**")).toBeLessThan(body.indexOf("> Generated by"));
329+
expect(body.match(/<!-- gh-aw-agentic-workflow:/g)).toHaveLength(1);
330+
});
331+
332+
it("bounds many accepted explanations while preserving the status and footer", async () => {
333+
const items = Array.from({ length: 3000 }, (_, index) => ({ type: "noop", message: `Message ${index}` }));
334+
const body = await renderAcceptedExplanations(items);
335+
336+
expect(body.length).toBeLessThanOrEqual(65536);
337+
expect(body.match(/\*\*No-op message\*\*/g)).toHaveLength(50);
338+
expect(body).toContain("Message 49");
339+
expect(body).not.toContain("Message 50\n");
340+
expect(body).toContain("Additional agent messages omitted.");
341+
expect(body).toContain("completed successfully");
342+
expect(body).toContain("> Generated by");
343+
expect(body).toContain("<!-- gh-aw-agentic-workflow:");
344+
});
345+
346+
it("fits large explanations without omitting an exact-limit collection", async () => {
347+
const items = Array.from({ length: 50 }, () => ({ type: "report_missing_data", reason: "Large explanation ".repeat(1000) }));
348+
const body = await renderAcceptedExplanations(items);
349+
350+
expect(body.length).toBeLessThanOrEqual(65536);
351+
expect(body).toContain("Content redacted to fit size limit.");
352+
expect(body).not.toContain("Additional agent messages omitted.");
353+
expect(body).toContain("completed successfully");
354+
expect(body).toContain("<!-- gh-aw-agentic-workflow:");
355+
});
356+
357+
it("does not count invalid or unsupported explanations toward the limit", async () => {
358+
const items = [
359+
...Array.from({ length: 50 }, (_, index) => ({ type: "noop", message: `Message ${index}` })),
360+
{ type: "noop", message: " " },
361+
{ type: "noop", message: "API_SECRET=synthetic-opaque-value" },
362+
{ type: "add_comment", body: "Unrelated output" },
363+
];
364+
const body = await renderAcceptedExplanations(items);
365+
366+
expect(body).toContain("Message 49");
367+
expect(body).not.toContain("Additional agent messages omitted.");
368+
expect(body).not.toContain("synthetic-opaque-value");
369+
});
370+
371+
it("preserves agent-authored alerts without nesting them in another alert", async () => {
372+
const body = await renderAcceptedExplanations([{ type: "report_incomplete", reason: "> [!WARNING]\n> Review did not finish." }]);
373+
374+
expect(body).toContain("## Agent messages\n\n**Incomplete work**\n\n> [!WARNING]\n> Review did not finish.");
375+
expect(body).not.toContain("> > [!");
376+
});
377+
378+
it("omits the info section when there are no publishable explanations", async () => {
379+
const body = await renderAcceptedExplanations([
380+
{ type: "noop", message: " " },
381+
{ type: "add_comment", body: "Unrelated output" },
382+
]);
383+
384+
expect(body).not.toContain("## Agent messages");
385+
expect(body).not.toContain("**No-op message**");
386+
expect(body).toContain("completed successfully");
387+
});
388+
295389
it("masks recognized credentials in the published explanation", async () => {
296390
const pat = `ghp_${"A".repeat(36)}`;
297391
const awsKey = `AKIA${"B".repeat(16)}`;
298392
const body = await renderAcceptedExplanations([{ type: "noop", message: `Credential diagnostics:\n\n\`\`\`text\n${pat}\n${awsKey}\n\`\`\`` }]);
299393

300-
expect(body).toContain("<summary>No-op message</summary>");
394+
expect(body).toContain("**No-op message**");
301395
expect(body).toContain("Credential diagnostics:");
302396
expect(body).toContain("***REDACTED***\n***REDACTED***");
303397
expect(body.includes(pat)).toBe(false);

0 commit comments

Comments
 (0)