如何为 Skill 构建自我提升循环
分享在实际项目中构建 Agent 自我提升循环的方案:通过执行循环与反馈环的结合,实现 Skill 质量的持续自主优化。
最近因为 Google Gemini 工程师的文章,出现了很多利用“循环”来优化工作流的讨论。
听到“循环工程“这个概念,可能很多人会觉得很遥远,但我回过头想了想,我早就将这种“循环“思路应用在了自己的工作中。
我结合了 skill 实现了一种非常使用的循环模式:Skill 自我提升循环。
简单来说,它的核心想法是:让 Agent 能够根据日常运行中收集到的反馈,自动优化自身 Skill 的质量。
下面以一个简单的 Git commit 信息自动生成 skill 来举例。
我将这个 Skill 的职责定义为读取当前暂存的代码 diff,自动生成符合 Commits 规范的提交信息。
即使每个人对 Commit 信息的字数限制、模块命名、Emoji 前缀等都有不同的偏好,但这套自我提升闭环可以让你无需手动调试 Prompt,而是让 Prompt 自动适配你的个人书写习惯。
这个 Commit 生成 Skill 的初版大概长这样:
SKILL.md
---
name: write-commit
description: Generate a concise conventional commit message based on the staged git diff. Use when preparing a git commit.
version: 1
---
# Write Commit Message
You are generating a git commit message for the staged changes. You will be given the output of `git diff --cached`.
## Skill version
This skill is at version 1. This version is bumped automatically by the improve-commit-skill agent when it revises this skill.
## Steps
1. Analyze the git diff and generate a commit message. Use conventional commit format (e.g., feat: ..., fix: ...).
要实现让这个 Skill 自动优化,我们需要两个相互配合的循环:
- 执行循环:我配置了本地的 Git hook(如
prepare-commit-msg),每次我运行git commit时就会自动触发运行该 Skill 并填充初始信息。同时,它会把 Agent 原本推荐的文字和我的暂存日志记录在本地的缓存文件中。 - 提升循环:它的工作是定期拉取内层循环产生的修改记录。其核心任务是:对比 Agent 推荐的 Commit 消息与我最终保存提交的 Commit 消息。如果我做出了修改,外层 Agent 就会分析我的修改意图,并对
SKILL.md进行修改。
搭建执行循环
我在本地仓库的 .git/hooks/prepare-commit-msg 中配置了如下钩子脚本,每次提交时自动触发 Agent:
#!/bin/sh
# .git/hooks/prepare-commit-msg
# 获取当前暂存的 diff
staged_diff=$(git diff --cached)
if [ -n "$staged_diff" ]; then
# 运行 write-commit 产生推荐的提交信息
suggested_msg=$(run-agent-skill --skill "write-commit" --input "$staged_diff")
# 记录 Agent 初始建议,便于外层循环比对
echo "$suggested_msg" > .git/last_suggested_commit.txt
# 将建议写入 commit 编辑器中
echo "$suggested_msg" > "$1"
fi
这个脚本会在本地唤醒 Agent,拉取 diff 内容并自动生成第一版 Commit Message 填充到编辑器中。
每当我在终端运行 git commit,执行循环就会自动运行并为我填好如 feat(api): add user login endpoint 的建议信息。
搭建自我提升循环
在实际提交时,如果我觉得 Agent 写的分类或细节不满意,比如我更习惯将 feat(api) 写成 feat(auth),或者我习惯给涉及文档的提交加上 📝 Emoji 前缀,我就会在编辑器中直接把信息修改为 feat(auth): 📝 add OAuth support 并保存。
这时候,我的提升 Agent 可以通过 post-commit 钩子在每次提交完成后运行(或者每天定时运行)。它会对比 .git/last_suggested_commit.txt 和我最终录入 Git 历史中的真实 Commit 记录。当它检测到我进行了改动,就会触发学习机制。
负责自我提升的外层 Agent Skill 定义如下:
---
name: improve-commit-skill
description: Observe how the developer modified the generated commit messages and propose improvements to the write-commit skill. Evolve the skill based on the developer's editing habits.
---
# Improve Commit Skill
You are the self-improvement half of a commit message loop. Your job is to study the differences between the commit messages proposed by the write-commit skill (`.agents/skills/write-commit/SKILL.md`) and the final messages saved by the developer, extract their writing preferences, and propose edits to the skill.
Work in the repository this skill lives in.
## Step 1: Read commit message history
Compare the proposed messages with the final committed messages in the last 14 days.
这个外层 Agent 会分析我的修改细节(例如把 api 改为了 auth,增加了 📝 标记),理解我的个人偏好,并直接对我的 write-commit Skill 提交一个优化 PR:
一旦这个 commit 被合并,修改后的规则就会被应用到正式执行中。下一次我运行 git commit 时,Agent 生成的初始 Commit 信息就会自动带上我偏好的风格和模块分类。

最后
我知道很多 Agent 工具早就自带了 AI commit 功能,但这篇文章重点不在 skill 内容,而在分享 skill 自我提升的方法。
自从在我的日常开发中引入了这套闭环后,我几乎再也没手动调整过我个人 Skill 的内容。
比如我现在每天都会使用的文章优化 skill、markdown 文章转 HTML 的 skill、第三方库的个人使用经验 skill 等等。这种模式会在我和它的一次次交互中,越来越默契地演进成最适合我的样子。
希望这个小例子能对你有所启发!