Skip to content

Commit 9b3a608

Browse files
committed
sync: klaatcode v2.2.3 — 2026-07-18
Changes since the last sync: - feat(oss): add advisory PR review bot workflow, powered by Klaatu (11fbbf60) Klaatcode-Source-Commit: 11fbbf605ba1230ad367afc6ff8c215c57ac18e4
1 parent e6ec655 commit 9b3a608

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: KlaatAI Review Bot
2+
3+
# Advisory-only PR review, powered by the KlaatAI/Klaatu API. Posts a single
4+
# comment with findings (issue-match, test coverage, concerns) — never blocks
5+
# merge, never approves/requests-changes. A human always decides.
6+
7+
on:
8+
pull_request:
9+
types: [opened, synchronize, reopened]
10+
11+
permissions:
12+
contents: read
13+
pull-requests: write
14+
15+
jobs:
16+
review:
17+
runs-on: ubuntu-latest
18+
if: ${{ github.event.pull_request.draft == false }}
19+
steps:
20+
- name: Check for API key
21+
id: guard
22+
run: |
23+
if [ -z "${{ secrets.KLAATU_API_KEY }}" ]; then
24+
echo "KLAATU_API_KEY not set — skipping review bot (not a failure)."
25+
echo "skip=true" >> "$GITHUB_OUTPUT"
26+
else
27+
echo "skip=false" >> "$GITHUB_OUTPUT"
28+
fi
29+
30+
- name: Gather PR + issue context
31+
if: steps.guard.outputs.skip == 'false'
32+
id: ctx
33+
env:
34+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
REPO: ${{ github.repository }}
36+
PR_NUMBER: ${{ github.event.pull_request.number }}
37+
PR_TITLE: ${{ github.event.pull_request.title }}
38+
PR_BODY: ${{ github.event.pull_request.body }}
39+
run: |
40+
set -euo pipefail
41+
42+
# Find a linked issue: "#123", "fixes #123", "closes #123", "fix #123" — case-insensitive.
43+
ISSUE_NUM=$(printf '%s\n%s' "$PR_TITLE" "$PR_BODY" \
44+
| grep -ioE '(fix(e[sd])?|close[sd]?|resolve[sd]?)?[[:space:]]*#[0-9]+' \
45+
| grep -oE '[0-9]+' | head -1 || true)
46+
47+
ISSUE_JSON='null'
48+
if [ -n "${ISSUE_NUM:-}" ]; then
49+
ISSUE_JSON=$(gh issue view "$ISSUE_NUM" --repo "$REPO" --json title,body 2>/dev/null || echo 'null')
50+
fi
51+
echo "issue_num=${ISSUE_NUM:-none}" >> "$GITHUB_OUTPUT"
52+
53+
# PR diff, capped to keep the request payload sane.
54+
DIFF=$(gh pr diff "$PR_NUMBER" --repo "$REPO" 2>/dev/null | head -c 60000)
55+
DIFF_TRUNCATED="false"
56+
FULL_LEN=$(gh pr diff "$PR_NUMBER" --repo "$REPO" 2>/dev/null | wc -c)
57+
if [ "$FULL_LEN" -gt 60000 ]; then DIFF_TRUNCATED="true"; fi
58+
59+
# Changed files + whether any touch a *.test.* / *_test.* / test/ path.
60+
FILES=$(gh pr view "$PR_NUMBER" --repo "$REPO" --json files --jq '.files[].path')
61+
HAS_TEST_FILE="false"
62+
if printf '%s\n' "$FILES" | grep -qiE '(\.test\.|_test\.|/tests?/)'; then
63+
HAS_TEST_FILE="true"
64+
fi
65+
66+
{
67+
echo "issue_json<<'EOF_ISSUE'"
68+
echo "$ISSUE_JSON"
69+
echo "EOF_ISSUE"
70+
echo "diff<<'EOF_DIFF'"
71+
echo "$DIFF"
72+
echo "EOF_DIFF"
73+
echo "diff_truncated=$DIFF_TRUNCATED"
74+
echo "files<<'EOF_FILES'"
75+
echo "$FILES"
76+
echo "EOF_FILES"
77+
echo "has_test_file=$HAS_TEST_FILE"
78+
} >> "$GITHUB_OUTPUT"
79+
80+
- name: Ask Klaatu for a review
81+
if: steps.guard.outputs.skip == 'false'
82+
id: review
83+
env:
84+
KLAATU_API_KEY: ${{ secrets.KLAATU_API_KEY }}
85+
PR_TITLE: ${{ github.event.pull_request.title }}
86+
PR_BODY: ${{ github.event.pull_request.body }}
87+
ISSUE_NUM: ${{ steps.ctx.outputs.issue_num }}
88+
ISSUE_JSON: ${{ steps.ctx.outputs.issue_json }}
89+
DIFF: ${{ steps.ctx.outputs.diff }}
90+
DIFF_TRUNCATED: ${{ steps.ctx.outputs.diff_truncated }}
91+
FILES: ${{ steps.ctx.outputs.files }}
92+
HAS_TEST_FILE: ${{ steps.ctx.outputs.has_test_file }}
93+
run: |
94+
set -euo pipefail
95+
96+
SYSTEM_PROMPT='You are an advisory code reviewer for the klaatcode open-source CLI repo. You never approve, reject, or block a PR — a human maintainer always makes that call. Given a linked GitHub issue (if any), the PR diff, and the changed-file list, produce a SHORT, specific review covering exactly these four things, in this order, as markdown:
97+
98+
1. **Issue match** — does the diff actually address what the linked issue asked for? Partial credit if it does part of it. If there is no linked issue, say so plainly and note that reviewers should confirm intent manually.
99+
2. **Test coverage** — were tests added or updated for this change? Name the specific behavior that is or is not covered. Do not just say "yes/no tests present" — say what a test would need to check that is missing, if anything.
100+
3. **Correctness concerns** — specific edge cases, error paths, or interactions with existing code that look unhandled or risky, referencing exact lines/functions where you can. If nothing stands out, say so briefly rather than inventing a concern.
101+
4. **Verdict** — one line: ready to merge as-is / needs a specific fix first / needs human judgment call on X. Never phrase this as your own approval — phrase it as what you would flag to a human reviewer.
102+
103+
Keep the whole review under 300 words. Be concrete — cite file names and behavior, not generic advice.'
104+
105+
USER_PROMPT=$(cat <<EOF
106+
PR title: ${PR_TITLE}
107+
108+
PR body:
109+
${PR_BODY}
110+
111+
Linked issue #${ISSUE_NUM}:
112+
${ISSUE_JSON}
113+
114+
Changed files:
115+
${FILES}
116+
117+
Has a test-looking file changed: ${HAS_TEST_FILE}
118+
119+
Diff (truncated to first 60000 chars: ${DIFF_TRUNCATED}):
120+
${DIFF}
121+
EOF
122+
)
123+
124+
PAYLOAD=$(jq -n --arg sys "$SYSTEM_PROMPT" --arg usr "$USER_PROMPT" '{
125+
model: "klaatu-reason",
126+
messages: [
127+
{role: "system", content: $sys},
128+
{role: "user", content: $usr}
129+
],
130+
stream: false
131+
}')
132+
133+
RESPONSE=$(curl -sS -X POST "https://api.klaatai.com/v1/chat/completions" \
134+
-H "Authorization: Bearer ${KLAATU_API_KEY}" \
135+
-H "Content-Type: application/json" \
136+
-d "$PAYLOAD")
137+
138+
REVIEW_TEXT=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // empty')
139+
140+
if [ -z "$REVIEW_TEXT" ]; then
141+
echo "Klaatu API did not return review content — response was:"
142+
echo "$RESPONSE"
143+
echo "review_text=" >> "$GITHUB_OUTPUT"
144+
else
145+
{
146+
echo "review_text<<'EOF_REVIEW'"
147+
echo "$REVIEW_TEXT"
148+
echo "EOF_REVIEW"
149+
} >> "$GITHUB_OUTPUT"
150+
fi
151+
152+
- name: Post review comment
153+
if: steps.guard.outputs.skip == 'false' && steps.review.outputs.review_text != ''
154+
env:
155+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
156+
REPO: ${{ github.repository }}
157+
PR_NUMBER: ${{ github.event.pull_request.number }}
158+
REVIEW_TEXT: ${{ steps.review.outputs.review_text }}
159+
run: |
160+
set -euo pipefail
161+
BODY="🤖 **KlaatAI Review Bot** (powered by Klaatu, advisory only — a maintainer makes the real call)
162+
163+
${REVIEW_TEXT}
164+
165+
<sub>This is an automated review to help triage faster, not a gate. Nothing here blocks merging.</sub>"
166+
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$BODY"

0 commit comments

Comments
 (0)