diff --git a/.github/AGENTS.md b/.github/AGENTS.md index 31ed2ab84..93a1e621e 100644 --- a/.github/AGENTS.md +++ b/.github/AGENTS.md @@ -272,6 +272,7 @@ Runs on every opencode.yml run, regardless of success/failure/cancelled. - Vague triage text (`needs more info from reporter` / `needs the following` / `Suggested next: needs-info`) -> `agent/needs-info`. A bare `## Triage` heading alone does NOT match. - `Investigation Report` heading -> `agent/investigated` (verb-override) - `Created PR #N` or `Opened PR #N` comment -> `agent/done` + - Preflight failure (action release endpoint) -> `agent/blocked` (a failed preflight produced no report this run; report branches above still win) - Clear triage (`## Triage` without vague literals or refusal) -> apply NO label; remove stale `agent/needs-info` and `agent/failed` if present (re-triage/recovery unstick) 3. Reports above win even on `failure`/`cancelled` runs (a triage run that dies at finalization after posting still delivered); with no recognized report, `failure`/`cancelled` -> `agent/failed`. No heading anywhere -> existing label respected, PR context -> `agent/done`, else default `agent/done`. There is deliberately no kind-based default: `investigated` requires the report heading, `duplicate` requires a `duplicate-of-#N` link. 4. If no heading and the context is a PR -> `agent/done`. diff --git a/.github/scripts/opencode-post-step.js b/.github/scripts/opencode-post-step.js index 9a7e98dc4..fb0fc4a30 100644 --- a/.github/scripts/opencode-post-step.js +++ b/.github/scripts/opencode-post-step.js @@ -3,7 +3,8 @@ * the issue if the work product is the comment (not a PR). * * Triggered by: .github/workflows/opencode.yml (the "Apply agent/* label" step) - * Reads: context.payload.issue, context.payload.comment, PREV_CONCLUSION env var + * Reads: context.payload.issue, context.payload.comment, PREV_CONCLUSION env var, + * PREFLIGHT_OUTCOME env var (action release endpoint preflight) * Writes: agent/* label, removes agent/in-progress, optionally closes the issue * * Idempotent: respects an existing agent/* label; safe to re-run. @@ -57,7 +58,9 @@ function findLatestAgentComment(comments) { return ''; } -function decideAgentLabel({ existing, conclusion, latestComment, labels, isPR }) { +function decideAgentLabel({ existing, conclusion, latestComment, labels, isPR, preflightOutcome }) { + const _pf = preflightOutcome; + const _pfFailed = !!_pf && /^(failure|failed|cancelled|canceled)$/i.test(String(_pf).trim()); // Structured reports are deliberate work products: honor them even when the // run later died (triage runs often fail at action finalization after the // comment already landed). Failure maps to failed only with no report. @@ -76,6 +79,9 @@ function decideAgentLabel({ existing, conclusion, latestComment, labels, isPR }) if (isPRCreationComment(latestComment)) { return { label: 'agent/done', reason: 'PR creation comment detected' }; } + if (_pfFailed) { + return { label: 'agent/blocked', reason: 'preflight: action release endpoint failed' }; + } if (isTriageClearComment(latestComment)) { return { label: null, clearNeedsInfo: true, clearFailed: true, reason: 'clear triage, no blocking label' }; } @@ -101,11 +107,13 @@ function shouldClose({ isPR, latestComment, labels, agentLabel }) { return CLOSE_KINDS.some(k => labels.includes(k)); } -module.exports = async ({ github, context, core }) => { +const _runPostStep = async ({ github, context, core }) => { const number = context.payload.issue.number; const isPR = isPRContext(context); const { owner, repo } = context.repo; const conclusion = process.env.PREV_CONCLUSION || 'success'; + const preflightOutcome = process.env.PREFLIGHT_OUTCOME || ''; + core.info('Preflight outcome: ' + (preflightOutcome || '(none)')); core.info('Post-step for #' + number + ' (isPR=' + isPR + ', conclusion=' + conclusion + ')'); @@ -139,6 +147,7 @@ module.exports = async ({ github, context, core }) => { latestComment, labels, isPR, + preflightOutcome, }); core.info('Decision: ' + decision.label + ' (' + decision.reason + ')'); @@ -211,3 +220,5 @@ module.exports = async ({ github, context, core }) => { core.info('Not closing #' + number); } }; + +module.exports = _runPostStep; diff --git a/.github/scripts/tests/opencode-post-step.test.js b/.github/scripts/tests/opencode-post-step.test.js index 918677bb4..f2eb277b0 100644 --- a/.github/scripts/tests/opencode-post-step.test.js +++ b/.github/scripts/tests/opencode-post-step.test.js @@ -256,4 +256,40 @@ test('opencode-post-step.js test suite', async (t) => { await runPostStep({ github, context, core }); assert.ok(calls.addLabels.includes('agent/failed')); }); + + await t.test('Preflight failure applies agent/blocked, not agent/failed', async () => { + const { core, github, context, calls, setIssueData, setComments } = createMocks(); + process.env.PREV_CONCLUSION = 'skipped'; + process.env.PREFLIGHT_OUTCOME = 'failure'; + setIssueData({ labels: [{ name: 'kind/bug' }], state: 'open' }); + setComments([{ body: 'Just a normal comment' }]); + + await runPostStep({ github, context, core }); + assert.ok(calls.addLabels.includes('agent/blocked')); + assert.ok(!calls.addLabels.includes('agent/failed')); + }); + + await t.test('Preflight success keeps genuine agent failure as agent/failed', async () => { + const { core, github, context, calls, setIssueData, setComments } = createMocks(); + process.env.PREV_CONCLUSION = 'failure'; + process.env.PREFLIGHT_OUTCOME = 'success'; + setIssueData({ labels: [{ name: 'kind/bug' }], state: 'open' }); + setComments([{ body: 'Just a normal comment' }]); + + await runPostStep({ github, context, core }); + assert.ok(calls.addLabels.includes('agent/failed')); + assert.ok(!calls.addLabels.includes('agent/blocked')); + }); + + await t.test('Preflight failure with clear triage applies agent/blocked', async () => { + const { core, github, context, calls, setIssueData, setComments } = createMocks(); + process.env.PREV_CONCLUSION = 'skipped'; + process.env.PREFLIGHT_OUTCOME = 'failure'; + setIssueData({ labels: [{ name: 'kind/bug' }], state: 'open' }); + setComments([{ body: '## Triage\n\n- [x] **Suggested next:** fix' }]); + + await runPostStep({ github, context, core }); + assert.ok(calls.addLabels.includes('agent/blocked')); + assert.ok(!calls.addLabels.includes('agent/failed')); + }); }); diff --git a/.github/scripts/tests/workflow-opencode-pin.test.js b/.github/scripts/tests/workflow-opencode-pin.test.js index eb9f09895..c7208ab92 100644 --- a/.github/scripts/tests/workflow-opencode-pin.test.js +++ b/.github/scripts/tests/workflow-opencode-pin.test.js @@ -91,3 +91,37 @@ test("run_agent step timeout is below its job timeout and above a legitimate run const opencodeTimeout = Number(opencodeStep.text.match(/^\s*timeout-minutes:\s*(\d+)\s*$/m)[1]); assert.ok(opencodeTimeout >= 69, "opencode step timeout must exceed the 68m26s legitimate run"); }); + +test("preflight step exists in all three workflows", () => { + for (const name of WORKFLOWS) { + const text = readWorkflow(name); + const step = stepContaining(text, "Preflight: action release endpoint"); + assert.match(step.text, /curl -sS --max-time 60 https:\/\/api\.github\.com\/rate_limit/, name + ": preflight must query anonymous rate limit"); + assert.match(step.text, /resources\.core\.remaining/, name + ": preflight must read resources.core.remaining"); + assert.match(step.text, /^\s*timeout-minutes:\s*2\s*$/m, name + ": preflight timeout must be 2"); + assert.match(step.text, /^\s*id:\s*preflight\s*$/m, name + ": preflight id must be preflight"); + } +}); + +test("preflight step does not set GH_TOKEN", () => { + for (const name of WORKFLOWS) { + const text = readWorkflow(name); + const step = stepContaining(text, "Preflight: action release endpoint"); + assert.doesNotMatch(step.text, /GH_TOKEN/, name + ": preflight must not set GH_TOKEN"); + } +}); + +test("post-step receives PREFLIGHT_OUTCOME from preflight", () => { + for (const name of ["opencode.yml", "triage.yml"]) { + const text = readWorkflow(name); + assert.match(text, /PREFLIGHT_OUTCOME:\s*\$\{\{\s*steps\.preflight\.outcome\s*\}\}/, name + ": post-step must receive PREFLIGHT_OUTCOME"); + } +}); + +test("run_agent step is gated on preflight success", () => { + for (const name of ["opencode.yml", "triage.yml"]) { + const text = readWorkflow(name); + const step = stepContaining(text, ACTION_USES); + assert.match(step.text, /steps\.preflight\.outcome == 'success'/, name + ": run_agent must be gated on preflight success"); + } +}); diff --git a/.github/workflows/opencode.yml b/.github/workflows/opencode.yml index 7ca805de7..f16e8b228 100644 --- a/.github/workflows/opencode.yml +++ b/.github/workflows/opencode.yml @@ -84,6 +84,22 @@ jobs: script: | const { opencodeDedupe } = require('./.github/scripts/opencode-dedupe.js'); await opencodeDedupe({ github, context, core }); + - name: 'Preflight: action release endpoint' + if: steps.dedupe.outputs.should_run == 'true' + id: preflight + timeout-minutes: 2 + run: | + remaining=$(curl -sS --max-time 60 https://api.github.com/rate_limit | jq -r '.resources.core.remaining') + echo "anonymous GitHub API rate limit remaining: $remaining" + if [ -z "$remaining" ] || [ "$remaining" = "null" ]; then + echo "::error::could not read anonymous rate limit" + exit 1 + fi + if [ "$remaining" -le 1 ]; then + echo "::error::anonymous GitHub API rate limit exhausted (remaining=$remaining, threshold <= 1)" + exit 1 + fi + - name: Select model and effort for this run if: steps.dedupe.outputs.should_run == 'true' # Role -> model + effort variant, from .github/scripts/model-config.js. @@ -155,7 +171,7 @@ jobs: if: steps.dedupe.outputs.should_run == 'true' && steps.cache-iso.outputs.cache-hit != 'true' run: sh build.sh cd-x86-lite - name: Run opencode - if: steps.dedupe.outputs.should_run == 'true' + if: steps.dedupe.outputs.should_run == 'true' && steps.preflight.outcome == 'success' id: run_agent timeout-minutes: 85 uses: anomalyco/opencode/github@v1.19.6 @@ -172,6 +188,7 @@ jobs: if: (success() || failure() || cancelled()) && steps.dedupe.outputs.should_run == 'true' env: PREV_CONCLUSION: ${{ steps.run_agent.outcome }} + PREFLIGHT_OUTCOME: ${{ steps.preflight.outcome }} uses: actions/github-script@v7 with: github-token: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/triage.yml b/.github/workflows/triage.yml index 603dd8d8b..975b52b42 100644 --- a/.github/workflows/triage.yml +++ b/.github/workflows/triage.yml @@ -67,7 +67,23 @@ jobs: core.info('Model selection: role=triage model=' + sel.model + ' variant=' + sel.variant); core.exportVariable('SELECTED_MODEL', sel.model); core.exportVariable('SELECTED_VARIANT', sel.variant); + - name: 'Preflight: action release endpoint' + id: preflight + timeout-minutes: 2 + run: | + remaining=$(curl -sS --max-time 60 https://api.github.com/rate_limit | jq -r '.resources.core.remaining') + echo "anonymous GitHub API rate limit remaining: $remaining" + if [ -z "$remaining" ] || [ "$remaining" = "null" ]; then + echo "::error::could not read anonymous rate limit" + exit 1 + fi + if [ "$remaining" -le 1 ]; then + echo "::error::anonymous GitHub API rate limit exhausted (remaining=$remaining, threshold <= 1)" + exit 1 + fi + - name: Run opencode triage + if: steps.preflight.outcome == 'success' id: run_agent timeout-minutes: 15 uses: anomalyco/opencode/github@v1.19.6 @@ -85,6 +101,7 @@ jobs: if: success() || failure() || cancelled() env: PREV_CONCLUSION: ${{ steps.run_agent.outcome }} + PREFLIGHT_OUTCOME: ${{ steps.preflight.outcome }} uses: actions/github-script@v7 with: github-token: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/wiki-update.yml b/.github/workflows/wiki-update.yml index 5f1d22fa1..7fbd4cc66 100644 --- a/.github/workflows/wiki-update.yml +++ b/.github/workflows/wiki-update.yml @@ -75,8 +75,24 @@ jobs: echo "skip=false" >> "$GITHUB_OUTPUT" fi - - name: Run opencode for wiki analysis + - name: 'Preflight: action release endpoint' if: steps.commits.outputs.skip != 'true' + id: preflight + timeout-minutes: 2 + run: | + remaining=$(curl -sS --max-time 60 https://api.github.com/rate_limit | jq -r '.resources.core.remaining') + echo "anonymous GitHub API rate limit remaining: $remaining" + if [ -z "$remaining" ] || [ "$remaining" = "null" ]; then + echo "::error::could not read anonymous rate limit" + exit 1 + fi + if [ "$remaining" -le 1 ]; then + echo "::error::anonymous GitHub API rate limit exhausted (remaining=$remaining, threshold <= 1)" + exit 1 + fi + + - name: Run opencode for wiki analysis + if: steps.commits.outputs.skip != 'true' && steps.preflight.outcome == 'success' id: run_agent timeout-minutes: 45 uses: anomalyco/opencode/github@v1.19.6