智能体
dev.to ·
@Quinn Zhu
第一次回滚:撤销你无法解释的 Agent PR
文章主张在合并 AI Agent 生成的 PR 之前就应准备好回滚方案,因为 Agent 常产出体量大且看似自信的 diff,而开发者初期缺乏仓库历史。作者将首次回滚定义为撤销自己控制的 Agent 分支,不触碰他人提交,并给出具体步骤:先冻结合并、停止 Agent 与流水线、取消该分支的 CI、将 PR 标记为 draft 并通知评审者,再行诊断。
AI推荐理由 文章针对 AI Agent 生成代码 PR 的工程实践,提出在合并前就应规划回滚方案,并给出冻结合并、取消 CI、标记 draft 等具体操作步骤,对使用 Agent 编码的开发者有实际参考价值。内容为经验性建议而非实证研究,来源为个人 DEV 博客,缺少案例数据支撑,因此质量分中等;但主题切中 Agent 落地中的真实风险,兴趣分较高。
62
正文
Your first AI pull request will often need rollback.
Plan that rollback before you merge anything.
You lack repo history on day one.
Agents still produce large and confident diffs today.
A rollback plan keeps that blast radius tiny.
What first rollback actually means
First rollback means undoing your own agent PR.
It does not mean rewriting team history casually.
You revert a branch you still control.
You leave other people's commits completely untouched.
Why juniors stall on revert
You treat revert as an admission of failure.
It is a safety move, not a performance review.
You also wait for perfect understanding of every hunk.
That wait lets CI keep building a bad branch.
Step 1: Freeze merge before you diagnose
Do not keep generating patches during a bad merge.
Stop the agent, the pipeline, and extra pushes now.
Mark the PR as draft if GitHub still allows it.
Cancel running CI on that branch right now.
Tell your reviewer the branch is under rollback.
gh pr ready --undo
gh run cancel --branch "$BRANCH"
git status -sb
Enter fullscreen mode
Exit fullscreen mode
Those three commands buy you quiet thinking time.
You need that quiet before you touch git history.
Step 2: Inventory every file the agent touched
You cannot revert a change you cannot list.
Ask git, not the chat log, for the truth.
BASE="${BASE_SHA:-origin/main}"
HEAD="${HEAD_SHA:-HEAD}"
git fetch origin
git diff --name-status "$BASE"..."$HEAD"
git diff --stat "$BASE"..."$HEAD"
git log --oneline "$BASE".."$HEAD"
Enter fullscreen mode
Exit fullscreen mode
Save that inventory into a local file immediately.
mkdir -p .onboarding
git diff --name-status "$BASE"..."$HEAD" \
> .onboarding/first-pr-files.txt
git diff "$BASE"..."$HEAD" \
> .onboarding/first-pr.patch
Enter fullscreen mode
Exit fullscreen mode
Read the name-status list before any revert.
Look for migrations, lockfiles, and generated assets.
Read the patch in three passes
You do not read the patch from top to bottom.
You read it in three passes with git only.
Read names first, then stats, then one scary file.
Skip vendor folders until the end of the review.
Stop when you cannot explain a hunk out loud.
git diff --name-only "$BASE"...HEAD | wc -l
git diff --stat "$BASE"...HEAD | tail -n 1
Enter fullscreen mode
Exit fullscreen mode
If the file count exceeds the ticket's expected paths, revert.
A junior should not debug a thirty-file agent surprise.
Step 3: Choose revert or fix-forward
Use a hard table. Do not improvise under pressure.
Signal
Prefer revert
Prefer fix-forward
Files outside the ticket
Yes
No
Tests red on your branch
Yes
Only if one test fails
Schema or migration changed
Yes
No
Reviewer already approved
Ask first
Maybe
You cannot explain one hunk
Yes
No
If two or more Yes cells fire, revert.
Fix-forward is for a single, obvious typo only.
Write the decision into the same onboarding folder.
cat > .onboarding/rollback-decision.md "$OUT/branch.txt"
git diff --name-status "$BASE"...HEAD > "$OUT/name-status.txt"
git diff --stat "$BASE"...HEAD > "$OUT/stat.txt"
git log --oneline "$BASE"..HEAD > "$OUT/commits.txt"
echo "Risky paths:" > "$OUT/risks.txt"
if command -v rg >/dev/null 2>&1; then
rg -n -i "migration|schema|lock|generated|dist/" \
"$OUT/name-status.txt" >> "$OUT/risks.txt" || true
else
grep -Ei "migration|schema|lock|generated|dist/" \
"$OUT/name-status.txt" >> "$OUT/risks.txt" || true
fi
echo "Inventory written to $OUT"
Enter fullscreen mode
Exit fullscreen mode
Label this as a proposed local helper, not team policy.
Your new team may already have a revert runbook.
Commit the script only if the team wants it.
Do not sneak tooling into the rollback PR itself.
Limitations
This workflow assumes you still own the branch.
It assumes main is protected and you can open drafts.
It does not cover broken production traffic today.
It does not cover signed commits you cannot reproduce.
Revert can miss submodule pointers and LFS files.
It can also miss squashed commits on a rewritten branch.
The decision table is a heuristic, not a proof.
Two yes cells do not replace a staff engineer's call.
Free model access will not know your production topology.
A free server will not hold your private incident facts.
Do not paste secrets, tokens, or customer data into prompts.
Who should not use this approach
Skip this if you are not the PR author.
Skip this if the change already shipped to customers.
Skip this if the repo uses a required merge queue.
Talk to the maintainer before any revert there.
Skip this if you cannot run the README test command.
A green revert you cannot test is still a guess.
Juniors on regulated codebases should wait for a buddy.
Pair on the revert and do not solo a schema undo.
Close the loop on hour one
Write three notes before you log off.
What the agent changed outside the ticket.
Which table signal made you choose revert.
Which test command proved the tree was clean.
Those notes become your second-day review checklist.
They also keep the next agent session much smaller.
Practice this workflow on a throwaway clone first.
A free explain-only session can walk the inventory.
Keep every git command in your own terminal.