Skip to content

Commit 2f32429

Browse files
committed
Add strict mode to prevent unrelated code changes
1 parent 8180ac0 commit 2f32429

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

.github/workflows/claude-code.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
3333
3434
- name: Process with Claude Code
35-
uses: fractureinc/claude-code-github-action@v0.2.0
35+
uses: fractureinc/claude-code-github-action@v0.2.2
3636
with:
3737
mode: 'review'
3838
pr-number: ${{ steps.pr.outputs.number }}
@@ -64,7 +64,7 @@ jobs:
6464
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
6565
6666
- name: Process with Claude Code Suggestions
67-
uses: fractureinc/claude-code-github-action@v0.2.0
67+
uses: fractureinc/claude-code-github-action@v0.2.2
6868
with:
6969
mode: 'suggest'
7070
pr-number: ${{ steps.pr.outputs.number }}
@@ -104,7 +104,7 @@ jobs:
104104
echo "line=$LINE" >> $GITHUB_OUTPUT
105105
106106
- name: Process with Claude Code for code review comment
107-
uses: fractureinc/claude-code-github-action@v0.2.0
107+
uses: fractureinc/claude-code-github-action@v0.2.2
108108
with:
109109
mode: 'review'
110110
pr-number: ${{ steps.details.outputs.number }}
@@ -143,13 +143,14 @@ jobs:
143143
echo "line=$LINE" >> $GITHUB_OUTPUT
144144
145145
- name: Process with Claude Code Suggestions for code review
146-
uses: fractureinc/claude-code-github-action@v0.2.1
146+
uses: fractureinc/claude-code-github-action@v0.2.2
147147
with:
148148
mode: 'suggest-review'
149149
pr-number: ${{ steps.details.outputs.number }}
150150
feedback: ${{ steps.details.outputs.feedback }}
151151
file-path: ${{ steps.details.outputs.file_path }}
152152
line-number: ${{ steps.details.outputs.line }}
153153
comment-id: ${{ steps.details.outputs.comment_id }}
154+
strict-mode: 'true'
154155
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
155156
github-token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
5050
5151
- name: Process with Claude Code
52-
uses: fractureinc/claude-code-github-action@v0.2.1
52+
uses: fractureinc/claude-code-github-action@v0.2.2
5353
with:
5454
mode: 'review'
5555
pr-number: ${{ steps.pr.outputs.number }}
@@ -81,7 +81,7 @@ jobs:
8181
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
8282
8383
- name: Process with Claude Code Suggestions
84-
uses: fractureinc/claude-code-github-action@v0.2.1
84+
uses: fractureinc/claude-code-github-action@v0.2.2
8585
with:
8686
mode: 'suggest'
8787
pr-number: ${{ steps.pr.outputs.number }}
@@ -94,18 +94,23 @@ jobs:
9494
9595
| Input | Description | Required | Default |
9696
|-------|-------------|----------|---------|
97-
| `mode` | Operation mode (review, suggest, direct) | Yes | `review` |
97+
| `mode` | Operation mode (review, suggest, suggest-review, direct) | Yes | `review` |
9898
| `pr-number` | Pull request number | Yes* | |
9999
| `feedback` | User query text | Yes | |
100+
| `file-path` | Path to the file being reviewed (for suggest-review mode) | No** | |
101+
| `line-number` | Line number in the file (for suggest-review mode) | No** | |
102+
| `comment-id` | GitHub comment ID to reply to (for suggest-review mode) | No** | |
103+
| `strict-mode` | Whether to strictly follow user requests without adding unrelated improvements | No | `true` |
100104
| `anthropic-api-key` | Anthropic API key | Yes | |
101105
| `github-token` | GitHub token | Yes | |
102106
| `output-file` | Output file path (for direct mode) | No | `claude-code-output` |
103107

104-
\* Required when mode is 'review' or 'suggest'
108+
\* Required when mode is 'review' or 'suggest'
109+
\** Required when mode is 'suggest-review'
105110

106111
## Enhanced Context for Claude
107112

108-
With version 0.2.1, Claude now receives complete context for your PRs, including:
113+
With version 0.2.2, Claude now receives complete context for your PRs, including:
109114

110115
- PR metadata (title, description, branch info)
111116
- List of all files changed
@@ -127,6 +132,8 @@ Creates suggested changes in a PR comment that outline potential code improvemen
127132

128133
Creates true GitHub-compatible suggestions that can be applied with one click directly from the code review interface. These are attached to specific lines of code.
129134

135+
By default, this mode uses "strict mode" which ensures Claude only makes changes specifically related to what was requested, without adding unrelated improvements. You can disable strict mode by setting `strict-mode: 'false'` to allow Claude to suggest additional improvements.
136+
130137
### Direct Mode (`mode: 'direct'`)
131138

132139
Sends a query directly to Claude and saves the response to a file without PR context.

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ inputs:
2424
comment-id:
2525
description: 'GitHub comment ID to reply to (for suggest-review mode)'
2626
required: false
27+
strict-mode:
28+
description: 'Whether to strictly follow user requests without adding unrelated improvements'
29+
required: false
30+
default: 'true'
2731
anthropic-api-key:
2832
description: 'Anthropic API key for Claude access'
2933
required: true
@@ -79,4 +83,4 @@ runs:
7983
shell: bash
8084
run: |
8185
chmod +x ${{ github.action_path }}/scripts/suggest-review-mode.sh
82-
${{ github.action_path }}/scripts/suggest-review-mode.sh "${{ inputs.pr-number }}" "${{ inputs.feedback }}" "${{ inputs.file-path }}" "${{ inputs.line-number }}" "${{ inputs.comment-id }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.github-token }}"
86+
${{ github.action_path }}/scripts/suggest-review-mode.sh "${{ inputs.pr-number }}" "${{ inputs.feedback }}" "${{ inputs.file-path }}" "${{ inputs.line-number }}" "${{ inputs.comment-id }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.github-token }}" "${{ inputs.strict-mode }}"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "claude-code-github-action",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "GitHub action for Claude Code Integration in PR comments, reviews and inline code suggestions",
55
"main": "index.js",
66
"scripts": {

scripts/suggest-review-mode.sh

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ LINE_NUMBER=$4
99
COMMENT_ID=$5
1010
ANTHROPIC_API_KEY=$6
1111
GITHUB_TOKEN=$7
12+
STRICT_MODE=$8
13+
14+
# Default to strict mode if not provided
15+
if [ -z "$STRICT_MODE" ]; then
16+
STRICT_MODE="true"
17+
fi
1218

1319
# Set up authentication
1420
echo "$GITHUB_TOKEN" | gh auth login --with-token
1521
export ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY"
1622

17-
# Create a temp file for Claude's response
23+
# Create a temp files for Claude's responses
1824
RESPONSE_FILE=$(mktemp)
25+
ADDITIONAL_SUGGESTIONS_FILE=$(mktemp)
1926

2027
# Get PR details using GitHub CLI
2128
echo "Fetching PR details for PR #$PR_NUMBER"
@@ -50,6 +57,21 @@ REPO_DESC=$(echo "$REPO_INFO" | jq -r '.description')
5057
REPO_DEFAULT_BRANCH=$(echo "$REPO_INFO" | jq -r '.defaultBranchRef.name')
5158
REPO_LANGUAGES=$(echo "$REPO_INFO" | jq -r '.languages[].name' | tr '\n' ', ' | sed 's/,$//')
5259

60+
# Add strict mode instructions if enabled
61+
STRICT_INSTRUCTIONS=""
62+
if [ "$STRICT_MODE" = "true" ]; then
63+
STRICT_INSTRUCTIONS=$(cat <<EOF
64+
65+
IMPORTANT - STRICT MODE IS ENABLED:
66+
1. You MUST address ONLY what the user explicitly asked for in their query
67+
2. Do NOT make any unrelated improvements to the code, even if they would be beneficial
68+
3. If the user asks to "add X", focus exclusively on adding X, not refactoring existing code
69+
4. If you identify other issues in the code, DO NOT address them in your suggestion
70+
5. Stay hyper-focused on the specific request, even if other improvements seem obvious
71+
EOF
72+
)
73+
fi
74+
5375
# Build the prompt for Claude to generate a suggested change
5476
PROMPT=$(cat <<EOF
5577
This is a GitHub code review. Create a suggestion for the specific code at line $LINE_NUMBER in file '$FILE_PATH'.
@@ -60,6 +82,7 @@ You MUST format your response as a SINGLE suggestion using the exact GitHub sugg
6082
\`\`\`suggestion
6183
[Your improved code here]
6284
\`\`\`
85+
$STRICT_INSTRUCTIONS
6386
6487
Guidelines:
6588
1. Focus ONLY on line $LINE_NUMBER and immediately surrounding lines
@@ -100,7 +123,49 @@ echo "Posting Claude's suggested change as a reply to the comment"
100123
gh api --method POST "/repos/:owner/:repo/pulls/$PR_NUMBER/comments/$COMMENT_ID/replies" \
101124
-F body="@$RESPONSE_FILE"
102125

126+
# If in non-strict mode, check for additional improvement suggestions
127+
if [ "$STRICT_MODE" = "false" ]; then
128+
ADDITIONAL_PROMPT=$(cat <<EOF
129+
You've already provided a suggestion that directly addresses the user's query for line $LINE_NUMBER in file '$FILE_PATH'.
130+
131+
Now, identify any additional code improvements that would be beneficial beyond what was specifically requested.
132+
These should be improvements that weren't part of the original request but would enhance code quality,
133+
readability, performance, or maintainability.
134+
135+
Format your response as:
136+
1. A brief explanation of why these additional improvements would be valuable
137+
2. Clearly labeled additional suggestions (not using the GitHub suggestion format)
138+
139+
File being reviewed: $FILE_PATH
140+
Line number context: $LINE_NUMBER
141+
142+
Context (code around line $LINE_NUMBER):
143+
\`\`\`
144+
$CONTEXT_CONTENT
145+
\`\`\`
146+
147+
Complete file content:
148+
\`\`\`
149+
$FILE_CONTENT
150+
\`\`\`
151+
152+
If you don't have any additional suggestions beyond what was directly requested, respond with "No additional improvements suggested."
153+
EOF
154+
)
155+
156+
echo "Checking for additional improvement suggestions..."
157+
echo "$ADDITIONAL_PROMPT" | claude -p - > "$ADDITIONAL_SUGGESTIONS_FILE"
158+
159+
# Only post additional suggestions if they exist
160+
if ! grep -q "No additional improvements suggested" "$ADDITIONAL_SUGGESTIONS_FILE"; then
161+
echo "Posting additional improvement suggestions..."
162+
ADDITIONAL_CONTENT="## Additional Suggestions\n\nWhile addressing your specific request, I noticed some other potential improvements:\n\n$(cat "$ADDITIONAL_SUGGESTIONS_FILE")\n\n*These are optional suggestions beyond what you specifically requested.*"
163+
gh api --method POST "/repos/:owner/:repo/pulls/$PR_NUMBER/comments/$COMMENT_ID/replies" \
164+
-F body="$ADDITIONAL_CONTENT"
165+
fi
166+
fi
167+
103168
# Clean up
104-
rm -f "$RESPONSE_FILE"
169+
rm -f "$RESPONSE_FILE" "$ADDITIONAL_SUGGESTIONS_FILE"
105170

106-
echo "Claude's in-line suggestion posted successfully!"
171+
echo "Claude's suggestions posted successfully!"

0 commit comments

Comments
 (0)