/** * Formats the comment that `post-report.ts` and the agent evals workflow share * on a pull request. */ import { formatMarkdown, statusOf } from "@rnx-kit/agent-evals/report"; import type { EvalResult } from "@rnx-kit/agent-evals/report"; export const MARKER = /^(?:\r?\n|$)/; /** Login of the agent evals workflow, which posts with `GITHUB_TOKEN` */ const WORKFLOW = "github-actions[bot]"; export type Comment = { id: number; body: string; login: string; }; /** * Returns the report to update: the one by `author`, the author of the pull * request, or else the one by the workflow. There are both if the author could * not edit the workflow's report, e.g. on a pull request from a fork. Reports * by anyone else are ignored; they could suppress the audit with a fake one. */ export function findReport( comments: Comment[], author: string ): Comment | undefined { let fallback: Comment | undefined; for (const comment of comments) { if (!MARKER.test(comment.body)) { continue; } if (comment.login === author) { return comment; } if (comment.login === WORKFLOW) { fallback ??= comment; } } return fallback; } type AuditResult = EvalResult & { response?: { output?: unknown } }; /** * Returns whether the audit found no changed files. The provider's output is * an `AgentRun`, which promptfoo may have serialized. */ export function auditedNoChanges(results: AuditResult[]): boolean { const result = results.find((result) => result.response?.output); if (!result) { return false; } const output = result.response?.output; const run = typeof output === "string" ? JSON.parse(output) : output; return Array.isArray(run?.files) && run.files.length === 0; } export function formatComment( results: AuditResult[] | undefined, head: string, audited: string | undefined, hasLog: boolean, number: number | string ): string { const lines = [ ``, "### Agent evals", "", results ? formatMarkdown(results) : "The audit did not produce a report.", ]; const statuses = results?.map(statusOf) ?? []; if ( !results || statuses.includes("skipped") || statuses.includes("unchecked") ) { lines.push( "", "Skipped rules need the session log, and unchecked rules could not be evaluated, e.g. because the grader needs Copilot access. To audit them, run the following in `evals/`:", "", `- Copilot cloud agent: \`yarn eval:report ${number}\``, "- Copilot CLI: `yarn eval:report` on the branch, on the machine that ran the session" ); } if (results && auditedNoChanges(results)) { lines.push( "", "> [!WARNING]", "> This audited no changes, so the rules had nothing to check. If the session made changes, check out its branch and run `yarn eval:report` again." ); } const subject = audited ?? "uncommitted changes"; if (results && audited !== head) { lines.push( "", "> [!WARNING]", `> This audited ${subject}, not the head of the pull request (${head}). Push the changes, or audit the session that made them, and run \`yarn eval:report\` again.` ); } lines.push( "", `Audited ${results ? subject : head}. This check only reports.` ); return lines.join("\n"); } const FAILED_NOTE = /\n> \[!CAUTION\]\n> The audit of \w+ failed[^\n]*\n/; /** * Adds a note to a report that the audit of `head` failed, replacing an * earlier one. The marker is kept, so the workflow audits `head` again. */ export function markFailed(body: string, head: string): string { const note = `\n> [!CAUTION]\n> The audit of ${head} failed; the report below is from an earlier run.\n`; return body .replace(FAILED_NOTE, "") .replace(/^### Agent evals\n/m, (heading) => heading + note); }