Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 20 additions & 3 deletions .github/workflows/code-review.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,9 +1633,13 @@ jobs:
}
checkpoint
failed=0
dispatch_err=$(mktemp)
# Code Monkey caps a run at max_pull_requests, named ones
# included, so each dispatch lifts the cap (0). One pull
# request per dispatch keeps a failure to that pull request.
# inherit_run_parameters has Code Monkey take the round's
# model and switches from its own footer on the pull
# request's latest round; none of this run's inputs pass.
for pull in $PULLS; do
review=$(jq -r --arg pull "$pull" \
'first(.[] | select(.pull == $pull) | .review) // empty' \
Expand Down Expand Up @@ -1664,13 +1668,26 @@ jobs:
:
elif gh workflow run code-monkey-cron.yaml --repo "$REPOSITORY" \
-f dry_run=false -f mode=pull-requests -f max_pull_requests=0 \
-f pull_requests="$pull"; then
-f inherit_run_parameters=true \
-f pull_requests="$pull" 2> "$dispatch_err"; then
state=dispatched
else
# A refusal (a 422 for an input Code Monkey does not
# declare) is recorded once and never retried here; the
# failed job's re-run is the retry.
state=failed
reason='the dispatch failed'
failed=$((failed + 1))
echo "::warning::$pull: the dispatch failed"
cat "$dispatch_err" >&2
err=$(<"$dispatch_err")
status='no HTTP status'
if [[ "$err" =~ HTTP\ ([1-5][0-9][0-9]) ]]; then
status="HTTP ${BASH_REMATCH[1]}"
fi
first=${err%%$'\n'*}
first=${first//[$'\001'-$'\037'\`]/}
first=${first:0:300}
reason="the dispatch failed: $status; ${first:-no error output}"
echo "::warning::$pull: $reason"
fi
deliveries=$(jq -c --arg pull "$pull" --argjson review "$review" \
--arg state "$state" --arg reason "$reason" \
Expand Down
16 changes: 16 additions & 0 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,22 @@ when a maintainer takes over.
costs that pull request alone. It never runs in a dry run or the
approve mode. The token needs Actions: write on that repository and
nothing else.
- **Whose parameters.** Each dispatch also passes
`inherit_run_parameters=true`, so the revision round runs on the
parameters of the Code Monkey run that published the pull request's
latest round (its model, runtime, round cap and switches), not on
the form's defaults. Code Monkey decides those from its own trusted
round footer; this workflow neither knows nor carries them, and
none of its own inputs (`model`, `reasoning_effort`,
`max_ai_credits`) passes to Code Monkey. Code Monkey refuses the
input on anything but a live `pull-requests` dispatch with
`max_pull_requests=0`, which is what this job sends. The job records
a dispatch Code Monkey refuses, such as an HTTP 422 for an input its
`code-monkey-cron.yaml` does not declare, as `failed` with the
reason `the dispatch failed: HTTP <status>; <first line of the
CLI's error output>` (or `no HTTP status` and `no error output` when
it gives none), in the delivery record and the step summary. The job
never retries a refusal itself; it fails, and a re-run is the retry.
- **The delivery record.** The `handback` job writes
`delivery/handback.json`, `{"schema": 1, "deliveries": [...]}`, with
each pull request, its review id, and `dispatched`, `skipped` (with
Expand Down
51 changes: 48 additions & 3 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,14 +1887,16 @@ def test_the_token_reaches_the_dispatch_step_alone(self) -> None:
self.assertEqual(step["env"]["GH_TOKEN"], "${{ secrets.handback_token }}")

def test_dispatch_target_and_list_are_fixed_and_checked(self) -> None:
"""One workflow file, live, revision mode, no count cap, and a validated
list dispatched whole, one pull request per dispatch.
"""One workflow file, live, revision mode, no count cap, the parameters
of the run that published the round, and a validated list dispatched
whole, one pull request per dispatch.
"""
step = self.step("handback", "Dispatch the revision round")
run = flatten(step["run"])
self.assertIn(
'gh workflow run code-monkey-cron.yaml --repo "$REPOSITORY"'
" -f dry_run=false -f mode=pull-requests -f max_pull_requests=0"
" -f inherit_run_parameters=true"
' -f pull_requests="$pull"',
run,
)
Expand Down Expand Up @@ -1999,7 +2001,7 @@ def test_the_record_uploads_whatever_the_dispatch_did(self) -> None:
case "$arg" in pull_requests=*) pull="${arg#pull_requests=}" ;; esac
done
case " $GH_ABORT " in *" $pull "*) kill -KILL "$PPID"; exit 1 ;; esac
case " $GH_FAIL " in *" $pull "*) exit 1 ;; esac
case " $GH_FAIL " in *" $pull "*) printf '%s' "$GH_FAIL_OUTPUT" >&2; exit 1 ;; esac
"""

# Stands for jq, and kills the step part-way through writing the Nth
Expand Down Expand Up @@ -2064,6 +2066,7 @@ def dispatch(
fail: str = "",
abort: str = "",
jq_abort_at: int = 0,
fail_output: str = "",
) -> tuple[int, list[str], dict[str, Any] | None, str]:
"""Run once: the exit code, the pull requests sent, the record, and
the step summary.
Expand All @@ -2087,6 +2090,7 @@ def dispatch(
"JQ_COUNT": str(self.tmp / "jq.count"),
"JQ_ABORT_AT": str(jq_abort_at),
"GH_FAIL": fail,
"GH_FAIL_OUTPUT": fail_output,
"GH_ABORT": abort,
"REPOSITORY": "org/github-code-monkey-bot",
"PULLS": pulls,
Expand Down Expand Up @@ -2172,6 +2176,47 @@ def test_a_failed_dispatch_fails_the_job_and_a_re_run_sends_it_alone(
self.assertEqual(sent, ["org/repo#2"])
self.assertEqual(set(self.states(record).values()), {"dispatched"})

def test_a_refused_dispatch_records_its_status_and_reason(self) -> None:
"""A refused input reads as one in the record and the summary, and the
refusal is sent once, never retried.
"""
refusal = (
"could not create workflow dispatch event: HTTP 422: Unexpected"
' inputs provided: ["inherit_run_parameters"]\nsecond line\n'
)
code, sent, record, summary = self.dispatch(
self.pending(1), fail="org/repo#1", fail_output=refusal
)
self.assertEqual(code, 1)
self.assertEqual(sent, ["org/repo#1"])
assert record is not None
reason = (
"the dispatch failed: HTTP 422; could not create workflow dispatch"
" event: HTTP 422: Unexpected inputs provided:"
' ["inherit_run_parameters"]'
)
self.assertEqual(
record["deliveries"],
[
{
"pull": "org/repo#1",
"review": 501,
"state": "failed",
"reason": reason,
}
],
)
self.assertIn(f"- `org/repo#1` (review 501): failed; {reason}", summary)

def test_a_silent_refusal_says_so(self) -> None:
"""A failure with no output or status still records a reason."""
_, _, record, _ = self.dispatch(self.pending(1), fail="org/repo#1")
assert record is not None
self.assertEqual(
record["deliveries"][0]["reason"],
"the dispatch failed: no HTTP status; no error output",
)

def test_a_re_run_after_a_late_failure_sends_nothing(self) -> None:
"""Every dispatch went before the job failed: the re-run repeats none."""
records = self.pending(1, 2)
Expand Down
Loading