Files
docs-review/.gitea/workflows/review.yml
T
jaredandClaude Opus 5.5 1ef68f12fb
tests / unittest (push) Failing after 28s
feat: local drafter (pi against an OpenAI-compatible server on your own hardware)
drafter: local runs the pi agent against the model already loaded on a self-hosted server,
as an unprivileged user, in the same sandbox as the Claude drafter. It fails soft when the
server is unreachable or has no model loaded. Both drafters now refuse to continue if
git metadata changed during drafting, and strip project pi configuration from the sandbox.
Tested live against a self-hosted server: one run, 12 turns, docs PR opened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 17:12:42 -05:00

263 lines
11 KiB
YAML

# The reusable docs-review workflow. A repository calls it from its own workflow with
# `uses: Civilytics/docs-review/.gitea/workflows/review.yml@v1`; README.md has the
# caller and the inputs.
#
# Secrets reach only the steps that need them. The bot token is in the clone step and
# the two steps that run the tool. The Claude credentials and the local model key are in
# the drafting step only: the Claude ones only when `drafter` normalizes to `claude`, the
# local key only when it normalizes to `local`. Every `if:` and secret expression reads
# the normalized drafter, never the raw input.
name: docs-review
on:
workflow_call:
inputs:
drafter:
description: "claude, local or none; anything else behaves as none"
type: string
required: false
default: none
docs:
description: "Newline-separated paths or globs of the living documents"
type: string
required: true
generated:
description: "Newline-separated paths of documents that are rebuilt, never hand-edited"
type: string
required: false
default: ""
top_level:
description: "The subset of docs the monthly pass re-reads in full"
type: string
required: false
default: README.md
review_only:
description: "Newline-separated paths or globs within docs that the drafter may read but not edit"
type: string
required: false
default: ""
source_dirs:
description: "Newline-separated directories whose new files count toward uncovered"
type: string
required: false
default: "R\nscripts"
workstream:
description: "Scope in draft commit messages, docs(<workstream>): ..."
type: string
required: false
default: docs
tracking_issue:
description: "Issue number the monthly pass's commits cite"
type: string
required: false
default: ""
doc_guide:
description: "Path to the document naming each listed document's reader and purpose"
type: string
required: false
default: ""
local_url:
description: "OpenAI-compatible base URL of the model server for drafter: local, e.g. http://model-host:8888/v1"
type: string
required: false
default: ""
model:
description: "Model for drafter: claude"
type: string
required: false
default: claude-sonnet-5-5
# Gitea 28.3 does not tell a called workflow its own ref (github.workflow_ref and
# job_workflow_sha are empty, and GITHUB_WORKFLOW_SHA is the caller's commit), so
# the caller names the tool version twice.
tool_ref:
description: "Branch or tag of docs-review to run; keep it equal to the @ref in uses:"
type: string
required: false
default: v1
secrets:
DOCS_REVIEW_BOT_TOKEN:
required: false
CLAUDE_CODE_OAUTH_TOKEN:
required: false
ANTHROPIC_API_KEY:
required: false
LOCAL_MODEL_API_KEY:
required: false
defaults:
run:
shell: bash
jobs:
review:
runs-on: ubuntu-latest
# node:22 carries python3 (3.11) and git, and npm for the Claude Code install.
container: node:22
# The drafter's own timeout is 25 minutes; the debounce adds 3.
timeout-minutes: 40
env:
TOOL_REPO: Civilytics/docs-review
TOOL_REF: ${{ inputs.tool_ref }}
EVENT: ${{ github.event_name }}
ACTION: ${{ github.event.action }}
PR: ${{ github.event.pull_request.number }}
BASE: ${{ github.event.pull_request.base.ref }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
INPUT_DRAFTER: ${{ inputs.drafter }}
INPUT_DOCS: ${{ inputs.docs }}
INPUT_GENERATED: ${{ inputs.generated }}
INPUT_TOP_LEVEL: ${{ inputs.top_level }}
INPUT_REVIEW_ONLY: ${{ inputs.review_only }}
INPUT_SOURCE_DIRS: ${{ inputs.source_dirs }}
INPUT_WORKSTREAM: ${{ inputs.workstream }}
INPUT_TRACKING_ISSUE: ${{ inputs.tracking_issue }}
INPUT_DOC_GUIDE: ${{ inputs.doc_guide }}
INPUT_MODEL: ${{ inputs.model }}
INPUT_LOCAL_URL: ${{ inputs.local_url }}
# The calling repository's files are untrusted. Every python3 call takes -P, so
# Python imports nothing from the working directory and a docs_review/ or os.py in
# a pull request cannot replace the tool.
steps:
- name: Versions
run: |
python3 --version
git --version
node --version
- name: Fetch docs-review
run: |
set -eu
tool="$RUNNER_TEMP/docs-review"
git clone -q --depth 1 --branch "$TOOL_REF" -- \
"$GITHUB_SERVER_URL/$TOOL_REPO.git" "$tool"
echo "docs-review $TOOL_REF at $(git -C "$tool" rev-parse --short HEAD)"
echo "PYTHONPATH=$tool" >> "$GITHUB_ENV"
- name: Normalize the drafter
id: drafter
run: |
set -eu
drafter="$(python3 -P -c 'import os; from docs_review.inputs import normalize_drafter; print(normalize_drafter(os.environ.get("INPUT_DRAFTER")))')"
echo "drafter: ${INPUT_DRAFTER:-<unset>} -> $drafter"
echo "drafter=$drafter" >> "$GITHUB_OUTPUT"
# Full history, so merge bases and the docs branch's ancestry resolve. The token
# travels as an http.<server>/.extraHeader in this command's environment, so it is
# sent only to the Gitea server and never written to .git/config or a remote URL.
- name: Clone the calling repository
if: github.event_name != 'pull_request' || github.event.action != 'closed'
env:
DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }}
run: |
set -eu
if [ -z "$DOCS_REVIEW_BOT_TOKEN" ]; then
echo "::error::the DOCS_REVIEW_BOT_TOKEN secret is not set"
exit 1
fi
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \
GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN"
src="$(mktemp -d)"
if ! git clone -q -- "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" "$src"; then
echo "::error::the bot token could not clone $GITHUB_REPOSITORY; it needs the" \
"write:repository scope and write access to the repository"
exit 1
fi
cd "$src"
if [ "$EVENT" = "pull_request" ]; then
git fetch -q origin "refs/pull/$PR/head"
git fetch -q origin "+refs/heads/$BASE:refs/remotes/origin/$BASE"
git checkout -q --detach "$HEAD_SHA"
fi
git log --oneline -1
cd - >/dev/null
cp -a "$src/." . && rm -rf "$src"
- name: Close the docs PR of a closed pull request
if: github.event_name == 'pull_request' && github.event.action == 'closed'
env:
DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }}
run: python3 -P -m docs_review closed --number "$PR"
# Stage 1 runs on every PR event. With drafter none it is the whole run and posts
# the comment; otherwise it only checks that the flags compute, and the drafting
# step after the debounce posts.
- name: Stage 1
if: >-
(github.event_name == 'pull_request' && github.event.action != 'closed')
|| ((github.event_name == 'schedule' || github.event_name == 'workflow_dispatch')
&& steps.drafter.outputs.drafter == 'none')
env:
DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }}
DOCS_REVIEW_STAGE: "1"
run: |
set -eu
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \
GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN"
if [ "$EVENT" = "pull_request" ]; then
python3 -P -m docs_review pr --number "$PR" --base "$BASE" \
--head-ref "$HEAD_REF" --head-sha "$HEAD_SHA"
else
base="$(git rev-parse --abbrev-ref origin/HEAD)"
python3 -P -m docs_review monthly --month "$(date -u +%Y-%m)" --base "${base#origin/}"
fi
# A re-push inside this wait cancels the run (the caller's concurrency group)
# before anything is spent.
- name: Debounce
if: >-
github.event_name == 'pull_request' && github.event.action != 'closed'
&& steps.drafter.outputs.drafter != 'none'
run: sleep 180
# `claude` gets the Claude credentials and the Claude Code install; `local` gets the
# local model key, the pi install and an unprivileged user to run pi as. pi's tool
# allowlist limits tool types, not paths, so the user is what keeps it out of the
# checkout: the checkout is root-owned and not writable by that user.
- name: Draft
if: >-
steps.drafter.outputs.drafter != 'none'
&& (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
|| (github.event_name == 'pull_request' && github.event.action != 'closed'))
env:
DRAFTER: ${{ steps.drafter.outputs.drafter }}
DOCS_REVIEW_BOT_TOKEN: ${{ secrets.DOCS_REVIEW_BOT_TOKEN }}
CLAUDE_CODE_OAUTH_TOKEN: ${{ steps.drafter.outputs.drafter == 'claude' && secrets.CLAUDE_CODE_OAUTH_TOKEN || '' }}
ANTHROPIC_API_KEY: ${{ steps.drafter.outputs.drafter == 'claude' && secrets.ANTHROPIC_API_KEY || '' }}
LOCAL_MODEL_API_KEY: ${{ steps.drafter.outputs.drafter == 'local' && secrets.LOCAL_MODEL_API_KEY || '' }}
DOCS_REVIEW_DRAFTER_USER: ${{ steps.drafter.outputs.drafter == 'local' && 'docs-review-drafter' || '' }}
CLAUDE_CODE_VERSION: 2.1.322
PI_VERSION: 1.5.2
DISABLE_UPDATES: "1"
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1"
run: |
set -eu
# A fresh HOME, so no settings, credentials or history from the image reach
# Claude Code.
export HOME="$(mktemp -d "$RUNNER_TEMP/home.XXXXXX")"
# With no credential the tool posts the flags with a note and fails, so the
# install would be wasted.
if [ "$DRAFTER" = "claude" ] && [ -n "$CLAUDE_CODE_OAUTH_TOKEN$ANTHROPIC_API_KEY" ]; then
npm install -g --no-fund --no-audit "@anthropic-ai/claude-code@$CLAUDE_CODE_VERSION"
claude --version
fi
# Without the key the tool posts the flags with a note and fails before pi runs.
if [ "$DRAFTER" = "local" ] && [ -n "$LOCAL_MODEL_API_KEY" ]; then
npm install -g --no-fund --no-audit "@earendil-works/pi-coding-agent@$PI_VERSION"
pi --version < /dev/null
id -u "$DOCS_REVIEW_DRAFTER_USER" >/dev/null 2>&1 \
|| useradd --create-home --shell /usr/sbin/nologin "$DOCS_REVIEW_DRAFTER_USER"
# The user's sandbox and HOME are created under RUNNER_TEMP; it needs to
# traverse it, not list it.
chmod o+x "$RUNNER_TEMP"
fi
export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="http.${GITHUB_SERVER_URL}/.extraHeader" \
GIT_CONFIG_VALUE_0="Authorization: token $DOCS_REVIEW_BOT_TOKEN"
if [ "$EVENT" = "pull_request" ]; then
python3 -P -m docs_review pr --number "$PR" --base "$BASE" \
--head-ref "$HEAD_REF" --head-sha "$HEAD_SHA"
else
base="$(git rev-parse --abbrev-ref origin/HEAD)"
python3 -P -m docs_review monthly --month "$(date -u +%Y-%m)" --base "${base#origin/}"
fi