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
87 changes: 77 additions & 10 deletions .github/workflows/claude-code-focus-on-demand.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ jobs:
# review workflow's `trigger_phrase` comment has the measurement.
trigger_phrase: "@claude /focus"
include_fix_links: false
# THE `Focus areas:` LINE IS A CONTRACT with the verify step below.
# Reword it in both places in the same commit, or every run fails.
# THE `Focus areas:` AND `Focus status: complete` LINES ARE A
# CONTRACT with the verify step below. Reword either in both places
# in the same commit, or every run fails. The completion line is
# plain text because the action's `update_claude_comment` tool
# strips HTML comments from the body.
prompt: |
REPO: ${{ github.repository }}
PR NUMBER: ${{ steps.prep.outputs.pr }}
Expand Down Expand Up @@ -245,6 +248,17 @@ jobs:
quickly. If you posted fewer than requested, or skipped an area
because an existing thread already covers it, say so here.

FINISH that top-level comment with EXACTLY this line, on its own,
as the very last line of the comment:

Focus status: complete

Write it only in your final update, once every inline comment is
posted and every item in your progress checklist is checked. A
workflow step reads it as proof the pass finished: a comment whose
last line is anything else, or that still shows an unchecked
`- [ ]` item or an "in progress" heading, fails the run.

TOOLS. Read the diff with `gh pr diff`. `git` IS NOT AVAILABLE and
every `git` call you make is refused and wasted; do not report its
absence as a limitation. The substitutions are exact:
Expand All @@ -271,7 +285,9 @@ jobs:
# workflow's `Verify the review actually ran` step records the incidents
# behind every choice here: why the verdict is the posted comment and not
# `num_turns`, why bodies go through `@json`, why the run id is anchored
# on its right, and why only bot comments count.
# on its right, why only bot comments count, and why the marker line
# alone is not enough (taskless/taskless #619: a run that stopped early
# passed on a mode line written into its checklist).
#
# This is INLINE rather than a shared script or a local composite action
# on purpose. On issue_comment the workflow file comes from the default
Expand All @@ -293,11 +309,61 @@ jobs:
run: |
set -uo pipefail

# Same checker as the review workflow's verify step; its comments
# explain each test. Exits 0 only for a comment from this run that
# has the `Focus areas:` line, ends on `Focus status: complete`, and
# shows no unchecked item or "in progress" heading outside fences.
comments="${RUNNER_TEMP:-/tmp}/bot-comments.jsonl"
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" \
--paginate --jq '.[] | select(.user.type == "Bot") | .body | @json' \
> "$comments" 2>/dev/null || true

posted=false
if gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" \
--paginate --jq '.[] | select(.user.type == "Bot") | .body | @json' 2>/dev/null \
| grep -E "runs/${GITHUB_RUN_ID}([^0-9]|$)" \
| grep -qF "Focus areas:"; then
if python3 - "$comments" "$GITHUB_RUN_ID" "Focus areas:" "Focus status: complete" <<'PYEOF'
import json, re, sys

path, run_id, mode_line, done_line = sys.argv[1:5]
this_run = re.compile(r"runs/" + re.escape(run_id) + r"(?![0-9])")
fenced = re.compile(r"^[ \t]*(`{3,}|~{3,}).*?^[ \t]*\1[`~]*[ \t]*$", re.M | re.S)
unchecked = re.compile(r"^[ \t]*[-*+][ \t]+\[ \]", re.M)
in_progress = re.compile(r"^[ \t]*#{1,6}[ \t].*\bin progress\b", re.M | re.I)

found, reasons = 0, set()
with open(path) as fh:
for record in fh:
try:
body = json.loads(record)
except json.JSONDecodeError:
continue
if not isinstance(body, str) or not this_run.search(body):
continue
found += 1
problems = []
if mode_line not in body:
problems.append("no mode line")
lines = [l for l in body.splitlines() if l.strip()]
last = lines[-1].strip().strip("*_").strip() if lines else ""
if last != done_line:
problems.append("last line is not the completion line")
prose = fenced.sub("", body)
if unchecked.search(prose):
problems.append("unchecked checklist item")
if in_progress.search(prose):
problems.append("'in progress' heading")
if not problems:
sys.exit(0)
reasons.update(problems)

if found:
print(
f"::warning::{found} bot comment(s) cite this run, but none is"
f" a completed focus summary: {', '.join(sorted(reasons))}."
)
else:
print("::warning::No bot comment cites this run.")
sys.exit(1)
PYEOF
then
posted=true
fi
export FOCUS_POSTED="$posted"
Expand Down Expand Up @@ -375,9 +441,10 @@ jobs:
if not posted:
print(
"::error::No completed focus summary from this run is on the"
" pull request. Expected a comment citing this run and"
" beginning with 'Focus areas:'. Treating this as a failure"
" so it is not mistaken for a clean pass."
" pull request. Expected a comment citing this run, with a"
" 'Focus areas:' line and ending with 'Focus status:"
" complete'. Treating this as a failure so it is not"
" mistaken for a clean pass."
)
failed = True

Expand Down
118 changes: 105 additions & 13 deletions .github/workflows/claude-code-review-on-demand.yml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,24 @@ jobs:
as the REVIEW MODE section above instructs. A reader must never have
to guess whether "no findings" means "nothing new since last time"
or "I read everything and it is clean".

FINISH that top-level comment with EXACTLY this line, on its own,
as the very last line of the comment:

Review status: complete

Write it only in your final update, once every inline comment is
posted and every item in your progress checklist is checked. A
workflow step reads it as proof the review finished: a comment
whose last line is anything else, or that still shows an unchecked
`- [ ]` item or a "Review in progress" heading, fails the run as an
unfinished review.
# THE `Review status: complete` LINE IS A CONTRACT with the verify
# step below, as `Review mode:` is. Reword either in both places in
# the same commit, or every run fails. It is plain text, not an HTML
# comment, because the action's `update_claude_comment` tool runs
# the body through `sanitizeContent`, which strips `<!-- -->`.
#
# In agent mode (comment-triggered) Claude only posts if it has these
# tools. All read-only or comment-posting — no local build/test, no CI
# reads. `Read` is read-only file access, and is safe ONLY alongside
Expand Down Expand Up @@ -532,9 +550,22 @@ jobs:
# characters reading "I'll analyze this and get back to you." and carries
# no such marker; #196, #208 and #209 all carry it.
#
# THAT MARKER IS A CONTRACT WITH THE PROMPT. If the review-mode
# instruction is ever reworded or dropped, change it here in the same
# commit, or this step starts failing every run.
# `Review mode:` PROVES A REVIEW STARTED, NOT THAT IT FINISHED. Measured
# on taskless/taskless PR #619 (run 47226766621): the model wrote the
# mode line into its progress checklist early, stopped after 11 turns
# and 2 Bash denials with no inline comments and no summary, and the
# action still rewrote the header to "Claude finished ... [View job]
# (.../runs/<id>)" when the SDK returned. The comment ended on
# "### Review in progress" and unchecked `- [ ]` items, and this step
# passed it. So the prompt also requires `Review status: complete` as
# the LAST line, written in the final update, and a comment counts only
# if it ends on that line and shows no unchecked item and no
# "in progress" heading. The retry (run 47227799385) ended
# "### Review complete" with every item checked, which passes.
#
# THESE MARKERS ARE A CONTRACT WITH THE PROMPT. If the review-mode or
# completion instruction is ever reworded or dropped, change it here in
# the same commit, or this step starts failing every run.
#
# A COPY OF THIS STEP lives in claude-code-focus-on-demand.yml, differing
# in its marker (`Focus areas:`). A fix to the detection logic here
Expand All @@ -557,9 +588,12 @@ jobs:
# `@json` is load-bearing: it emits each comment body as ONE line
# with newlines escaped. Piping raw bodies instead splits them across
# lines, so `runs/<id>` and `Review mode:` land on different lines and
# the second grep never matches — measured against PR #208, where the
# raw form finds 0 and this form finds 1. That version of this check
# would have failed every review, including the good ones.
# a line-based match never pairs them — measured against PR #208,
# where the raw form finds 0 and this form finds 1. That version of
# this check would have failed every review, including the good ones.
# The checker below reads one JSON string per line and decodes it,
# so each body is judged whole, line structure included, which the
# last-line and checklist tests need.
#
# The run id is anchored on its right, because a bare substring match
# also accepts any id this one is a numeric PREFIX of: searching for
Expand All @@ -580,11 +614,68 @@ jobs:
# would otherwise fail every review closed, and the threat here is a
# human comment, which this excludes. A DIFFERENT bot would still
# need to reproduce this run's id and the marker to matter.
#
# A COMPLETED review, not a started one: see the header above for
# #619. The checker exits 0 only for a comment from this run that
# states the mode, ENDS on the completion line (bold or italics
# around it are tolerated), and has no unchecked `- [ ]` item and no
# "in progress" heading outside fenced code. Fences are skipped
# because a finished review can legitimately quote a task list from
# the diff. The heading test is anchored to `#` because the
# second-set-of-eyes prompt itself says "the review in progress" in
# prose. It prints reason codes, never comment text. Any crash exits
# non-zero, which reads as not posted: this fails closed.
comments="${RUNNER_TEMP:-/tmp}/bot-comments.jsonl"
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" \
--paginate --jq '.[] | select(.user.type == "Bot") | .body | @json' \
> "$comments" 2>/dev/null || true

posted=false
if gh api "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" \
--paginate --jq '.[] | select(.user.type == "Bot") | .body | @json' 2>/dev/null \
| grep -E "runs/${GITHUB_RUN_ID}([^0-9]|$)" \
| grep -qF "Review mode:"; then
if python3 - "$comments" "$GITHUB_RUN_ID" "Review mode:" "Review status: complete" <<'PYEOF'
import json, re, sys

path, run_id, mode_line, done_line = sys.argv[1:5]
this_run = re.compile(r"runs/" + re.escape(run_id) + r"(?![0-9])")
fenced = re.compile(r"^[ \t]*(`{3,}|~{3,}).*?^[ \t]*\1[`~]*[ \t]*$", re.M | re.S)
unchecked = re.compile(r"^[ \t]*[-*+][ \t]+\[ \]", re.M)
in_progress = re.compile(r"^[ \t]*#{1,6}[ \t].*\bin progress\b", re.M | re.I)

found, reasons = 0, set()
with open(path) as fh:
for record in fh:
try:
body = json.loads(record)
except json.JSONDecodeError:
continue
if not isinstance(body, str) or not this_run.search(body):
continue
found += 1
problems = []
if mode_line not in body:
problems.append("no mode line")
lines = [l for l in body.splitlines() if l.strip()]
last = lines[-1].strip().strip("*_").strip() if lines else ""
if last != done_line:
problems.append("last line is not the completion line")
prose = fenced.sub("", body)
if unchecked.search(prose):
problems.append("unchecked checklist item")
if in_progress.search(prose):
problems.append("'in progress' heading")
if not problems:
sys.exit(0)
reasons.update(problems)

if found:
print(
f"::warning::{found} bot comment(s) cite this run, but none is"
f" a completed review: {', '.join(sorted(reasons))}."
)
else:
print("::warning::No bot comment cites this run.")
sys.exit(1)
PYEOF
then
posted=true
fi
export REVIEW_POSTED="$posted"
Expand Down Expand Up @@ -701,9 +792,10 @@ jobs:
if not posted:
print(
"::error::No completed review from this run is on the pull"
" request. Expected a comment citing this run and stating the"
" review mode. Treating this as a failure so it is not"
" mistaken for a clean pass."
" request. Expected a comment citing this run, stating the"
" review mode and ending with 'Review status: complete'."
" Treating this as a failure so it is not mistaken for a"
" clean pass."
)
failed = True

Expand Down
Loading