diff --git a/.env.example b/.env.example index 6dd4fdfd..2b66d641 100644 --- a/.env.example +++ b/.env.example @@ -272,3 +272,7 @@ DEBUG=false # LOG_LEVEL=INFO # SENIOR_DEVELOPER_REPO_DELAY_MS=1000 # PORT=8080 + +# Review waits for every check (required or not) of the head SHA; stuck checks stop blocking after this many minutes since the head commit +CHECKS_WAIT_ENABLED=true +CHECKS_WAIT_MAX_MINUTES=45 diff --git a/README.md b/README.md index 0f990652..8b10b4ed 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ Everything is read from environment variables (see `.env.example` and | `AGENT_CONTRACT_REFRESH`, `REPO_STANDARDS_MAX_PRS` | Runtime refresh of skills + global AGENTS.md (on in the image); standardization PRs per run | | `TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHAT_ID` | Notifications | | `CONFLICT_OPENCODE_ENABLED`, `PIPELINE_FIX_ENABLED` | Opt-in opencode conflict / CI fixing | +| `CHECKS_WAIT_ENABLED`, `CHECKS_WAIT_MAX_MINUTES` | PR Assistant waits for all checks of the head SHA to finish before reviewing; no checks (or billing-blocked Actions) never wait, stuck ones stop blocking after the timeout | | `REVIEW_WAIVE_AFTER_MINUTES`, `REVIEW_WAIVE_MAX_LINES`, `REVIEW_WAIVE_MAX_PER_RUN` | When a model outage may let a PR merge without model review | | `MERGE_LOCAL_CHECKS_ENABLED`, `PR_ASSISTANT_RESOLVE_CONFLICTS`, `GITHUB_BOT_LOGIN` | Substitute CI for billing/no-CI PRs, inline conflicts, bot identity | | `AUTOMATION_MODE`, `SIMULATION_MODE` | `observe` vs `autonomous`; dry-run all writes | diff --git a/ts/src/agents/pr_assistant/agent.ts b/ts/src/agents/pr_assistant/agent.ts index d6555c2a..8a97d07f 100644 --- a/ts/src/agents/pr_assistant/agent.ts +++ b/ts/src/agents/pr_assistant/agent.ts @@ -19,6 +19,7 @@ import { attemptPipelineFix } from "./pipeline-fix-flow.ts"; import { enabled as flagEnabled } from "./review-flags.ts"; import { botAuthActive, botLogin } from "../../github/bot-auth.ts"; import { reviewerLogins } from "./bot-identity.ts"; +import { mustWaitForChecks } from "./checks-wait.ts"; import { clearManualConflictLabel, handleConflict, type Resolver } from "./conflict-flow.ts"; import { hasLabel, KEEP_DRAFT_LABEL } from "../core/pr-options.ts"; import { resolveConflictsAutonomously } from "./conflict-resolver.ts"; @@ -125,6 +126,11 @@ export class PRAssistantAgent extends BaseAgent { const [status, comments] = await Promise.all([checkPipelineStatus(this.githubClient, pr), this.githubClient.getIssueComments(repo, pr.number)]); const cmds = ownerCommands(comments, this.targetOwner); if (cmds.hold) { this.recordSkip(results, pr.number, pr.title, repo, "owner_hold"); return; } + // Owner `/review` or `/merge` skips the wait; otherwise no model work while any check of this head is still running. + if (cmds.reviewAt === null && cmds.mergeAt === null && status.state === "pending" && mustWaitForChecks(status, await headCommitAt(this.githubClient, pr))) { + this.log(`PR #${pr.number}: waiting for ${status.pending_checks.length} pending check(s)`); + this.recordSkip(results, pr.number, pr.title, repo, "checks_pending"); return; + } // Builder PRs: reviewed like any other, but never marked ready nor merged by the bot (not even on `/merge`). if (hasLabel(pr, KEEP_DRAFT_LABEL)) { if (!this.simulationMode) await this.reviews.runOpencodeReview(pr, comments, { force: this.reviewForced(cmds, comments) }); diff --git a/ts/src/agents/pr_assistant/checks-wait.ts b/ts/src/agents/pr_assistant/checks-wait.ts new file mode 100644 index 00000000..88052e63 --- /dev/null +++ b/ts/src/agents/pr_assistant/checks-wait.ts @@ -0,0 +1,13 @@ +/** + * Review starts only after every check of the head SHA finished (required or not). Absent checks never wait (no CI, + * billing-blocked Actions); jobs stuck pending past CHECKS_WAIT_MAX_MINUTES since the head commit stop blocking. + */ +const int = (v: string | undefined, d: number) => { const n = parseInt(v ?? "", 10); return Number.isNaN(n) ? d : Math.max(0, n); }; +export const checksWaitEnabled = (v = process.env.CHECKS_WAIT_ENABLED) => !["0", "false", "no", "off"].includes((v ?? "").trim().toLowerCase()); +export const checksWaitMaxMinutes = () => int(process.env.CHECKS_WAIT_MAX_MINUTES, 45); + +/** True while the PR must keep waiting. `headAt` = head commit time (ms); unknown time waits (next run re-checks). */ +export function mustWaitForChecks(status: Record, headAt: number | null, now = Date.now()): boolean { + if (!checksWaitEnabled() || status.state !== "pending" || !(status.pending_checks?.length)) return false; + return headAt === null || now - headAt < checksWaitMaxMinutes() * 60_000; +} diff --git a/ts/src/notifications/telegram-format.ts b/ts/src/notifications/telegram-format.ts index 709a50d5..80c5ab75 100644 --- a/ts/src/notifications/telegram-format.ts +++ b/ts/src/notifications/telegram-format.ts @@ -4,7 +4,7 @@ import { TelegramNotifier } from "./telegram.ts"; export const esc = TelegramNotifier.escapeHtml; export const SEP = "──────────────────────"; const REASON_LABELS: Record = { - pr_too_young: "PR recente (aguardando idade mínima)", "auto-merge-skip": "label auto-merge-skip", untrusted_author: "autor não confiável", + pr_too_young: "PR recente (aguardando idade mínima)", checks_pending: "checks ainda rodando (aguardando)", "auto-merge-skip": "label auto-merge-skip", untrusted_author: "autor não confiável", has_conflicts: "conflito de merge", mergeable_unknown: "GitHub ainda calculando mergeabilidade", pipeline_failure: "pipeline falhou", pipeline_fix_attempted: "correção de pipeline enviada", pipeline_pending: "pipeline em andamento", pipeline_queued: "pipeline na fila", review_blocked: "revisão encontrou problema crítico", review_pending: "revisão ainda não executada (tenta na próxima rodada)", diff --git a/ts/tests/checks-wait.test.ts b/ts/tests/checks-wait.test.ts new file mode 100644 index 00000000..5f371080 --- /dev/null +++ b/ts/tests/checks-wait.test.ts @@ -0,0 +1,15 @@ +import { expect, test } from "bun:test"; +import { mustWaitForChecks } from "../src/agents/pr_assistant/checks-wait.ts"; + +const now = Date.parse("2026-01-01T12:00:00Z"), min = (n: number) => now - n * 60_000; +const pending = { state: "pending", pending_checks: ["lint", "e2e"] }; + +test("waits while any check is pending on a fresh head", () => expect(mustWaitForChecks(pending, min(5), now)).toBe(true)); +test("stops waiting once stuck checks pass the timeout (billing/queue never finishing)", () => expect(mustWaitForChecks(pending, min(46), now)).toBe(false)); +test("no checks at all never waits (no CI)", () => expect(mustWaitForChecks({ state: "success", pending_checks: [], no_ci: true }, min(1), now)).toBe(false)); +test("finished checks do not wait", () => expect(mustWaitForChecks({ state: "success", pending_checks: [] }, min(1), now)).toBe(false)); +test("unknown head time keeps waiting until next run", () => expect(mustWaitForChecks(pending, null, now)).toBe(true)); +test("can be disabled", () => { + process.env.CHECKS_WAIT_ENABLED = "false"; + try { expect(mustWaitForChecks(pending, min(1), now)).toBe(false); } finally { delete process.env.CHECKS_WAIT_ENABLED; } +});