关于 bail
面向 Claude 编码工作的退出流程技能,要求先记录尝试和经验,再进行清理。它指导分支与 worktree 处理、可选的 GitHub 议题或 PR 更新,以及将经验保存到 `MEMORY.md`。
主要功能
- 01识别当前工作步骤
- 02在 `.branch-context.md` 记录经验
- 03将记录汇总到 `MEMORY.md`
- 04指导更新议题和 PR
- 05删除前要求确认
适用场景
- 01记录放弃工作的原因
- 02为后续会话保留经验
- 03清理已放弃的分支或 worktree
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ camacho/ ai-skills --skill bail安装后在 Claude Code 中输入/bail确认已加载。
详细介绍
紧急退出协议:先记录,再清理。所有命令都使用绝对路径和 git -C。
输入
WORKTREE:当前工作树的绝对路径(如果没有工作树,则为主工作树)- 可选的原因字符串(如果未提供,请询问)
步骤
-
通过检查现有内容来判断当前步骤:
现有内容 估计步骤 只有一个问题,没有分支 步骤 0(记录) 有 .branch-context.md,没有工作树 步骤 1(梳理) 工作树存在,没有代码更改 步骤 2(隔离) 分支上有计划文件 步骤 3-4(设计/审查) 代码更改已提交 步骤 5-7(构建/验证/归档) GitHub 上有已打开的 PR 步骤 8(交付) -
如果未提供原因,请询问。
-
将经验记录写入工作树中的 .branch-context.md:
# Write bail context — use absolute path cat >> "$WORKTREE/.branch-context.md" <<EOF ## Bail - Reason: <reason> - What was attempted: <summary> - What was learned: <learnings> EOF git -C "$WORKTREE" add .branch-context.md git -C "$WORKTREE" commit -m "docs: capture bail context" -
通过临时克隆将经验汇总到 MEMORY.md(绝不要在工作树中检出 main):
BAIL_DIR="${CLAUDE_SESSION_DIR:-$TMPDIR}/bail-reflect" [ -d "$BAIL_DIR" ] && rm -rf "$BAIL_DIR" git clone --depth 50 "$(git -C "$WORKTREE" remote get-url origin)" "$BAIL_DIR"读取
$BAIL_DIR/ai-workspace/MEMORY.md,追加.branch-context.md中相关的经验,然后:git -C "$BAIL_DIR" add ai-workspace/MEMORY.md git -C "$BAIL_DIR" commit -m "docs: consolidate learnings from abandoned <branch> Co-Authored-By: Claude <model>" git -C "$BAIL_DIR" push如果不是快进更新:执行
git -C "$BAIL_DIR" pull --rebase,然后推送。最多重试 3 次。 清理:rm -rf "$BAIL_DIR" -
更新 GitHub Issue(如果可以访问):
GH_REPO=<owner/repo> gh issue comment <number> --body "Bailing: <reason>. Learnings captured in MEMORY.md." GH_REPO=<owner/repo> gh issue edit <number> --add-label "deferred" --remove-label "triage" -
如果 PR 仍处于打开状态,则关闭:
PR_NUM=$(GH_REPO=<owner/repo> gh pr list --head "<branch>" --json number -q '.[0].number') if [ -n "$PR_NUM" ]; then GH_REPO=<owner/repo> gh pr close "$PR_NUM" fi -
询问是否保留分支:
默认:保留分支(用户之后可以继续)。
如果用户明确要求删除,请先说明将要删除的内容以供确认:
This will delete: - Branch: <name> - Worktree: <path> - Remote branch (if pushed) Type 'delete' to confirm.只有在用户输入确认后才执行:
PRIMARY="$(git -C "$WORKTREE" worktree list --porcelain | grep -m1 '^worktree ' | sed 's/^worktree //')" # Verify branch is merged before deleting (-d fails if unmerged, -D would force) git -C "$PRIMARY" worktree remove "$WORKTREE" git -C "$PRIMARY" branch -d <branch>如果
branch -d失败(存在未合并的工作),请警告用户并停止。不要使用branch -D。 -
**完成。**不要检出 main;保持在当前会话所在的 CWD。
边界情况
- 不存在工作树(在步骤 0-1 退出)→ 跳过工作树清理,仅更新 Issue
- 无法访问 GitHub → 跳过 Issue 更新,仍执行本地清理和经验汇总
- PR 已合并 → 不要关闭,只在输出中注明
- 不存在 .branch-context.md → 创建该文件,仅写入退出原因,然后再进行汇总
- 未设置
CLAUDE_SESSION_DIR→ 使用$TMPDIR作为克隆目录
输出:总结清理了哪些内容,以及经验保存的位置。
评论
支持 Markdown