Skip to content

Commit 3d0d88e

Browse files
authored
fix: constrain AI changes to issue-named files (#5)
1 parent 65454df commit 3d0d88e

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

‎.github/workflows/ai-factory.yml‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,34 @@ jobs:
7373
git checkout -b "$branch"
7474
jq -r '"Issue: " + .title + "\n\n" + (.body // "")' <<< "$issue" > /tmp/ai-task.txt
7575
echo 'Implement the issue described below. Keep changes minimal, preserve existing APIs, add or update tests, and do not modify workflows, secrets, or access policies.' >> /tmp/ai-task.txt
76-
CI=1 aider --yes --no-check-update --no-show-release-notes --no-auto-commits --no-pretty --no-stream --chat-history-file /tmp/aider-chat-history.md --input-history-file /tmp/aider-input-history --model gpt-4.1 --message-file /tmp/ai-task.txt
77-
git add -A -- . ':!.aider*' ':!*.log'
78-
if git diff --cached --quiet; then echo 'Agent produced no substantive changes'; exit 1; fi
79-
if git diff --cached --name-only | grep -E '(^|/)(\.github/workflows/|\.env|AGENTS\.md|renovate\.json)' ; then
76+
# Only grant the agent write access to existing files explicitly named in the issue.
77+
# This prevents metadata-only PRs and unexpected repository changes.
78+
python - <<'PY'
79+
import json, pathlib, re, subprocess
80+
issue = json.loads(subprocess.check_output(
81+
["gh", "issue", "view", __import__("os").environ["ISSUE_NUMBER"],
82+
"--repo", __import__("os").environ["GITHUB_REPOSITORY"], "--json", "title,body"]))
83+
description = issue["title"] + "\n" + (issue.get("body") or "")
84+
tracked = subprocess.check_output(["git", "ls-files", "-z"]).decode().split("\0")
85+
selected = [p for p in tracked if p and re.search(r"(?<![\w./-])" + re.escape(p) + r"(?![\w./-])", description)]
86+
selected = [p for p in selected if not p.startswith(".github/") and not p.startswith(".aider") and p not in (".gitignore", "package.json", "package-lock.json", "renovate.json", "AGENTS.md") and not p.startswith(".env")]
87+
pathlib.Path("/tmp/ai-targets.txt").write_text("\n".join(selected[:12]))
88+
if not selected:
89+
raise SystemExit("Issue must name an existing editable repository file")
90+
PY
91+
mapfile -t targets < /tmp/ai-targets.txt
92+
CI=1 aider --yes --no-check-update --no-show-release-notes --no-gitignore --no-auto-commits --no-pretty --no-stream --chat-history-file /tmp/aider-chat-history.md --input-history-file /tmp/aider-input-history --model gpt-4.1 --message-file /tmp/ai-task.txt "${targets[@]}"
93+
git add -- "${targets[@]}"
94+
unexpected=$(git status --porcelain | awk '{print substr($0,4)}' | grep -v -F -x -f /tmp/ai-targets.txt || true)
95+
if [ -n "$unexpected" ]; then
96+
echo "Agent modified unexpected files: $unexpected" >&2
97+
exit 1
98+
fi
99+
if git diff --cached --quiet; then
100+
echo 'Agent produced no substantive tracked changes' >> "$GITHUB_STEP_SUMMARY"
101+
exit 1
102+
fi
103+
if git diff --cached --name-only | grep -E '(^|/)(\.github/|\.env|AGENTS\.md|renovate\.json|package-lock\.json|package\.json)' ; then
80104
echo 'Sensitive configuration changes require a separate manual process'; exit 1
81105
fi
82106
if [ -f package.json ]; then

0 commit comments

Comments
 (0)