Files
coder/.github/scripts/doc-check-previous-trace.test.sh
Nick VigilanteandBen Potter dc55633b47 ci(.github): start a new doc-check chat per run and stop it when the run ends (#32185)
## Summary

doc-check reused one chat for every push to a PR, and a review kept
running in Coder after its job stopped waiting for it.
This PR starts a new chat for each run, waits up to 5 hours for it, and
stops the review and its workspace when the run ends for any reason.

On dogfood at 2026-11-13 17:28 UTC, 58 doc-check review chats and 45 of
their sub-agents had status `running`, while 5 workflow runs were in
progress and 1 was queued.
34 of the 58 chats were created before that day, and the oldest on
2026-09-29.
The count covers only chats shared with me, so it is a lower bound.

This combines the workflow half of #32061 with the stop script from
#31720, and changes no skill file.

## Changes

- **New chat per run.**
`force-new-chat` replaces the one chat per PR, so a re-run no longer
carries every earlier turn on the PR.
The prompt gets a `## Previous run` section with the `doc-check-trace`
of the latest finished review.
- **One workspace per PR.**
The job finds `doc-check-pr-<N>`, wakes and starts it, and passes its ID
to the chat.
  On the first run the chat creates the workspace under that name.
- **A started review finishes; pushes are debounced.**
The review job's concurrency group is keyed on the PR URL with
`cancel-in-progress: false`. GitHub keeps one pending run per group and
replaces it on each push, so the next review covers the latest head.
A `debounce` job sleeps 5 minutes on `synchronize`, in its own group
with `cancel-in-progress: true`, so a burst of pushes starts one review.
Other triggers do not wait.
Merging the PR still cancels a running review (the `delete-workspace`
job keeps `cancel-in-progress: true`), then deletes the workspace.
- **Every run stops its review.**
`.github/scripts/doc-check-stop-review.sh` runs before the review and in
a final `always()` step.
It drops queued messages, interrupts the chat and its sub-agents, and
stops the workspace.
- **5-hour wait.**
`wait-timeout-seconds` goes from 2400 to 18000, and the job timeout from
50 to 330 minutes.
The job now stays in its concurrency group for the whole review, so the
next run for the PR waits for it.
- **Previous trace lookup.**
`.github/scripts/doc-check-previous-trace.sh` reads the 5 newest review
chats for the PR and prints the first trace with an `outcome:` line.
It reads more than one chat because a cancelled run's chat has no trace.
  A failed lookup costs only that context and never fails the job.
- The `GITHUB_OUTPUT` delimiter for the prompt is random, because the
trace is model output.

### Behavior changes to check

| Before | After | Why |
|--------|-------|-----|
| A review past the wait ended the job with a green notice and kept
running (#32097) | The last step stops the review, and the job fails |
Nothing watches a review after the job ends |
| A review kept running after its PR merged | The merge cancels the run
| The result has no reader |
| The `doc-check` label forced one review, on the `labeled` event | The
label forces a review on every push until it is removed | The push run
would otherwise skip on paths with no docs surface |
| Runs for one PR queued | A started review finishes; pushes during it
collapse into one pending run for the latest head, after a 5-minute
debounce | Cancelling threw away prefill and starved busy PRs of a
verdict |

## Testing

Static checks, all passing: `actionlint`, `zizmor`, `shellcheck
--external-sources`, and `shfmt -d`.

Script tests, run locally:

- `doc-check-stop-review.test.sh`: 48 cases against a fake API.
- `doc-check-previous-trace.test.sh`: 22 cases, including CRLF in the
trace text and in the PR URL.
- Mutation check on the trace script: 25 one-line breaks, and the suite
failed on 24.
  The survivor was a redundant trim, which I removed.
- The trace extraction ran against 12 real finished doc-check chats on
dogfood and returned a trace for all 12 (750 to 3,500 characters).

End to end on dogfood, on this PR, 2026-11-13 UTC:

| Run | Trigger | Result |
|-----|---------|--------|
| [1](https://github.com/coder/coder/actions/runs/43474029515) |
dispatch, 17:33 | No workspace and no earlier chat. New chat created the
workspace `doc-check-pr-32185` and had 2 running sub-agents at 17:48:23.
|
| [2](https://github.com/coder/coder/actions/runs/43476429715) |
dispatch, 17:48:25 | Cancelled run 1 at 17:48:27. Run 1's last step
interrupted the chat and both sub-agents and stopped the workspace by
17:48:51. Run 2 then started the same workspace, opened a second chat in
it, finished clean at 17:52:54, and stopped the workspace. |
| [3](https://github.com/coder/coder/actions/runs/43477511866) |
dispatch, 17:55:08 | Cancelled by run 4 while the workspace was
starting. Its last step stopped the workspace mid-start. No chat was
created. |
| [4](https://github.com/coder/coder/actions/runs/43477572348) |
`ready_for_review`, 17:55:31 | Waited for run 3 to end, then opened a
third chat in the same workspace. That chat's first message carries run
2's trace under `## Previous run`. |

Not covered:

- The 5-hour timeout path has not run live.
The fake-API tests cover the script, and the summary text is a few lines
of shell.
- The merge path (`delete-workspace` cancelling a review) has not run
live.
- No CI job runs the two `.test.sh` suites.
The workflow runs on `ubuntu-latest` only, and the scripts were not run
on macOS or Windows.

## Follow-ups, not in this PR

- Skip a run when the head SHA or the PR diff matches the last reviewed
one.
- Set a status on the head SHA from the trace.
- Cancel a review when its PR closes without merging.

Supersedes #31720 and the workflow half of #32061.

Linear: DOCS-1745

<details>
<summary>Plan and decision log</summary>

This is PR 1 of a 5-part stack from the design doc "Design: reliable,
low-cost doc-check on Solstice".

1. Workflow base: new chat per run, previous trace, workspace ID, 5-hour
wait, cancel-in-progress, and the stop script. **This PR.**
2. Skip in Actions: same head SHA, same PR diff, and a delta since the
last reviewed SHA with no docs surface.
3. Visible outcome: a status on the head SHA from the trace, for clean,
findings, error, and skipped.
4. Packet and lint in Actions: PR data, diffs, open threads, and
deterministic check results, written once.
5. Skill: scope to wrong, missing, and broken, tiers, a sub-agent cap,
and one long wait. Gated by an eval.

Decisions in this PR:

- **Why this first.**
A re-run's first prompt was 1.01 times the previous run's last prompt,
so each push re-sent the whole history of the PR.
A new chat per run removes that, and stopping stale reviews removes load
that no job is watching.
- **5 hours, not 25 or 40 minutes.**
Reviews on the local model took a median of 61 minutes when clean and
247 minutes with findings (2026-11-04 to 11-11).
  A shorter wait would stop most reviews with findings before they post.
With `cancel-in-progress`, a new push still ends the review of the old
head at once.
- **A timeout fails the job.**
  This reverses #32097, where a timeout was a notice.
  A green job with no review reads as a clean result.
- **The stop runs twice.**
The first step catches a review left by a run whose last step did not
finish, for example a runner that died.
- **The trace lookup is a script with tests, not inline YAML.**
It parses model output, and it has to handle chats with no trace, a
trace with no outcome, and CRLF.
- **The trace is passed as a record, not as instructions.**
  The prompt says so, and asks for a review of the whole PR.
A delta review needs the skip logic and the ledger from later PRs in the
stack.
- **No skill change.**
  The skill changes are gated by an eval that does not exist yet.

</details>

Follow-up commit `64a09655a1` (finish-then-rerun and debounce) generated
by Coder Agents on behalf of @bpmct.

_Generated by Coder Agents on behalf of @nickvigilante._

---------

Co-authored-by: Ben Potter <me@bpmct.net>
2026-11-13 21:14:35 +00:00

315 lines
9.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Tests for doc-check-previous-trace.sh against a fake curl on PATH. No CI
# job runs this file.
#
# Run: bash .github/scripts/doc-check-previous-trace.test.sh
set -euo pipefail
SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/doc-check-previous-trace.sh"
WORK=$(mktemp -d)
trap 'rm -rf "${WORK}"' EXIT
# The fake API. State lives in ${STATE}:
# list.json the answer to the label lookup
# messages/<id> the answer to a read of that chat's messages; a chat
# with no file answers with an HTTP error
# fail-list when present, the label lookup answers with an HTTP error
# calls one line per request
mkdir -p "${WORK}/bin"
cat >"${WORK}/bin/curl" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
url="" query=() max_time=""
while (($#)); do
case "$1" in
-H) shift 2 ;;
--max-time) max_time="$2"; shift 2 ;;
--data-urlencode) query+=("$2"); shift 2 ;;
*) [[ "$1" == http* ]] && url="$1"; shift ;;
esac
done
path="${url#"${CODER_URL%/}"}"
flag=" max=${max_time}"
[[ -z "${max_time}" ]] && flag=" NO-MAX-TIME"
echo "GET ${path}${query[*]:+ ${query[*]}}${flag}" >>"${STATE}/calls"
fail() {
echo '{"message":"simulated failure"}'
exit 22
}
case "${path}" in
/api/v2/chats)
[[ -e "${STATE}/fail-list" ]] && fail
cat "${STATE}/list.json"
;;
/api/v2/chats/*/messages)
id="${path#/api/v2/chats/}"
id="${id%/messages}"
[[ -e "${STATE}/messages/${id}" ]] || fail
cat "${STATE}/messages/${id}"
;;
*)
echo "fake curl: unhandled ${path}" >&2
exit 1
;;
esac
EOF
chmod +x "${WORK}/bin/curl"
failures=0
case_name=""
# Starts a case with an empty chat list.
begin() {
case_name="$1"
STATE="${WORK}/state-${RANDOM}-${RANDOM}"
mkdir -p "${STATE}/messages"
echo '[]' >"${STATE}/list.json"
: >"${STATE}/calls"
}
# add_chat <id> <created_at> [parent id]: adds a chat to the label lookup.
add_chat() {
jq --arg id "$1" --arg at "$2" --arg parent "${3:-}" \
'. + [{id: $id, created_at: $at} + (if $parent == "" then {} else {parent_chat_id: $parent} end)]' \
"${STATE}/list.json" >"${STATE}/list.json.tmp"
mv "${STATE}/list.json.tmp" "${STATE}/list.json"
}
# add_messages <id> <role:text>...: sets a chat's messages, newest first.
add_messages() {
local id="$1" entry
shift
local messages='[]'
for entry in "$@"; do
messages=$(jq --arg role "${entry%%:*}" --arg text "${entry#*:}" \
'. + [{role: $role, content: [{type: "reasoning", text: "doc-check-trace:\n outcome: reasoning"}, {type: "text", text: $text}]}]' \
<<<"${messages}")
done
jq '{messages: ., queued_messages: []}' <<<"${messages}" >"${STATE}/messages/${id}"
}
# A message that ends with a trace in a code fence, as output.md asks.
trace_message() {
# The backticks are a literal code fence.
# shellcheck disable=SC2016
printf 'No findings.\n\n```yaml\ndoc-check-trace:\n pr: 7\n head_sha: %s\n outcome: clean\n```\n' "$1"
}
# Runs the script. A variable that a case does not set reaches the script as
# an empty string, the way GitHub Actions passes an unset value.
run() {
STATUS=0
OUT=$(STATE="${STATE}" PATH="${WORK}/bin:${PATH}" \
CODER_URL="https://coder.example.com/" CODER_TOKEN="token" \
PR_URL="${PR_URL-https://github.com/coder/coder/issues/7}" \
WORKFLOW_NAME="AI Documentation Check" \
TRACE_MAX_CHATS="${TRACE_MAX_CHATS-}" TRACE_MAX_CHARS="${TRACE_MAX_CHARS-}" \
bash "${SCRIPT}" 2>"${STATE}/stderr") || STATUS=$?
}
fail_case() {
failures=$((failures + 1))
echo "FAIL: ${case_name}: $*"
sed 's/^/ stderr: /' "${STATE}/stderr"
sed 's/^/ call: /' "${STATE}/calls"
}
expect_out() {
if [[ "${OUT}" != "$1" ]]; then
fail_case "stdout was $(printf '%q' "${OUT}"), want $(printf '%q' "$1")"
fi
if [[ "${STATUS}" != "0" ]]; then
fail_case "exit status ${STATUS}, want 0"
fi
}
expect_stderr() {
grep -qF -- "$1" "${STATE}/stderr" || fail_case "stderr lacks: $1"
}
expect_calls() {
local got
got=$(grep -c "$1" "${STATE}/calls" || true)
if [[ "${got}" != "$2" ]]; then
fail_case "${got} calls match '$1', want $2"
fi
}
readonly TRACE_A=$'doc-check-trace:\n pr: 7\n head_sha: aaa\n outcome: clean'
readonly TRACE_B=$'doc-check-trace:\n pr: 7\n head_sha: bbb\n outcome: clean'
# Optional variables are unset unless a case sets them for one run.
unset TRACE_MAX_CHATS TRACE_MAX_CHARS PR_URL
begin "no earlier chat prints nothing"
run
expect_out ""
expect_stderr "No earlier review chat for coder/coder#7."
expect_calls "messages" 0
begin "the lookup uses the action's three labels and a request limit"
run
expect_calls "GET /api/v2/chats label=coder-agents-chat-action:true label=gh-target:coder/coder#7 label=coder-agents-chat-action-workflow:AI Documentation Check max=30" 1
begin "the newest chat's trace is printed without its code fence"
add_chat old 2026-11-01T00:00:00Z
add_chat new 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
add_messages new "assistant:$(trace_message aaa)"
run
expect_out "${TRACE_A}"
expect_stderr "Found the trace of review chat new."
expect_calls "/api/v2/chats/old/messages" 0
expect_calls "/api/v2/chats/new/messages limit=50 max=30" 1
begin "newest means created last, not listed first"
add_chat new 2026-11-02T00:00:00Z
add_chat old 2026-11-01T00:00:00Z
add_chat newest 2026-11-03T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
add_messages new "assistant:$(trace_message bbb)"
add_messages newest "assistant:$(trace_message aaa)"
run
expect_out "${TRACE_A}"
begin "a cancelled run's chat has no trace, so the one before it is used"
add_chat old 2026-11-01T00:00:00Z
add_chat cancelled 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
add_messages cancelled "assistant:Reading the diff." "user:Use the doc-check skill."
run
expect_out "${TRACE_B}"
expect_stderr "Review chat cancelled ended with no trace."
begin "a sub-agent chat is not a review chat"
add_chat root 2026-11-01T00:00:00Z
add_chat child 2026-11-02T00:00:00Z root
add_messages root "assistant:$(trace_message aaa)"
add_messages child "assistant:$(trace_message bbb)"
run
expect_out "${TRACE_A}"
expect_calls "/api/v2/chats/child/messages" 0
begin "the trace in the prompt of the next run is not that run's trace"
add_chat old 2026-11-01T00:00:00Z
add_chat new 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
add_messages new "assistant:Starting." "user:Previous run: $(trace_message bbb)"
run
expect_out "${TRACE_B}"
expect_calls "/api/v2/chats/old/messages" 1
begin "the newest message with a trace wins inside one chat"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa)" "user:Review again." "assistant:$(trace_message bbb)"
run
expect_out "${TRACE_A}"
begin "the last trace block in one message wins"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message bbb)$(trace_message aaa)"
run
expect_out "${TRACE_A}"
begin "text after the closing fence is not part of the trace"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa)"$'\nThat is the end of the review.'
run
expect_out "${TRACE_A}"
begin "a trace with no code fence runs to the end of the message"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:Done."$'\n\n'"${TRACE_A}"$'\n\n'
run
expect_out "${TRACE_A}"
begin "a mention of the block with no outcome line is not a trace"
add_chat old 2026-11-01T00:00:00Z
add_chat new 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
add_messages new "assistant:I will end with the doc-check-trace: block once the personas report."
run
expect_out "${TRACE_B}"
begin "a trace written with CRLF line endings loses its carriage returns"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa | sed 's/$/\r/')"
run
expect_out "${TRACE_A}"
begin "a long trace is cut at TRACE_MAX_CHARS"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa)"
TRACE_MAX_CHARS=30 run
expect_out "${TRACE_A:0:30}"
begin "only the TRACE_MAX_CHATS newest chats are read"
add_chat c1 2026-11-01T00:00:00Z
add_chat c2 2026-11-02T00:00:00Z
add_chat c3 2026-11-03T00:00:00Z
add_messages c1 "assistant:$(trace_message aaa)"
add_messages c2 "assistant:Stopped."
add_messages c3 "assistant:Stopped."
TRACE_MAX_CHATS=2 run
expect_out ""
expect_stderr "None of the 2 newest review chats for coder/coder#7 ended with a trace."
expect_calls "/api/v2/chats/c1/messages" 0
begin "five chats are read by default"
for n in 1 2 3 4 5 6; do
add_chat "c${n}" "2026-11-0${n}T00:00:00Z"
add_messages "c${n}" "assistant:Stopped."
done
run
expect_out ""
expect_calls "messages" 5
expect_calls "/api/v2/chats/c1/messages" 0
begin "a failed label lookup prints nothing and exits 0"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa)"
touch "${STATE}/fail-list"
run
expect_out ""
expect_stderr "Could not look up the review chats for coder/coder#7."
begin "a chat whose messages cannot be read is skipped"
add_chat old 2026-11-01T00:00:00Z
add_chat unreadable 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
run
expect_out "${TRACE_B}"
expect_stderr "Could not read review chat unreadable."
begin "a messages answer that is not JSON is skipped"
add_chat old 2026-11-01T00:00:00Z
add_chat broken 2026-11-02T00:00:00Z
add_messages old "assistant:$(trace_message bbb)"
echo "<html>502</html>" >"${STATE}/messages/broken"
run
expect_out "${TRACE_B}"
begin "a pull request URL in its /pull/ form with a trailing CR works"
add_chat chat 2026-11-01T00:00:00Z
add_messages chat "assistant:$(trace_message aaa)"
PR_URL=$'https://github.com/coder/coder/pull/7\r' run
expect_out "${TRACE_A}"
expect_calls "label=gh-target:coder/coder#7 " 1
begin "a URL that is not a pull request makes no request"
PR_URL="https://github.com/coder/coder" run
expect_out ""
expect_calls "GET" 0
begin "an empty pull request URL makes no request"
PR_URL="" run
expect_out ""
expect_calls "GET" 0
if ((failures > 0)); then
echo "${failures} doc-check-previous-trace.sh check(s) failed."
exit 1
fi
echo "All doc-check-previous-trace.sh cases pass."