构建一个代理,当你的文档出现偏差时自动打开一个 PR
Build an agent that opens a PR whenever your docs drift
导读
Tessl 博客发布教程,讲解如何构建一个定时检查文档漂移的智能体:它每周一自动比对 tally CLI 的文档与代码,一旦发现不一致就自行提交修复并创建拉取请求。该循环以代码为准,只修改 README 并先运行 npm test;教程还指导读者人为制造漂移、手动触发一次运行并审查结果。
正在把原文翻译成中文,长文需要一两分钟;先显示原文。
ARTICLE

TL;DR: In Part 1, you built a check: a test and a Tessl verifier that run on every change to a CLI's docs. In this post, you'll write the loop itself: a skill that compares the docs with the code, scheduled to run every week and open a pull request when they've drifted. Then you'll plant some drift, run the loop by hand, and check its work before you trust it.
This post picks up where Part 1 left off, with the tally repo, its test and its verifier on your machine. If you haven't built those yet, start with Part 1.
The check looks at each change as it comes in, and that's its blind spot: it can't see drift that's already sitting in your repo, or stop a change that never goes through it. The loop covers both. It sweeps the whole repo on a schedule and fixes whatever it finds.
Zoom
In the diagram from Part 1, this post covers the sweep: boxes 3 to 5. Every Monday, the loop checks main, and when it finds drift, it opens a fix pull request.
Here's where you're headed. By Step 4, the loop spots a command nobody documented and opens this pull request on its own:
Zoom
Prerequisites
On top of what you needed for Part 1, you'll need:
- The GitHub CLI, logged in. Run
gh auth statusto check, orgh auth loginto sign in. - Admin access to a Tessl organization. You need it to connect GitHub. The free plan is enough.
If anything below doesn't behave, the Schedules overview has the full list of prerequisites and more details to help you along.
Step 1: Put it on GitHub and link it to Tessl
Your loop is going to run in Tessl's cloud against your GitHub repo, so the two need a proper introduction.
First, push the repo up to GitHub:
1gh repo create tally --private --source . --push
Next, connect GitHub to your Tessl organization. In the Tessl web app, open Settings, then Integrations, and select Add next to GitHub. Give it access to the tally repo, and wait until it shows Connected.
Then create the repo's project link:
1# Link the repo to a Tessl project
2tessl project create
3
4# Commit and push the change
5git add -A && git commit -m "Link Tessl project" && git push
tessl project create links the repo to a Tessl project, which is how Tessl knows where your schedule belongs. If you belong to more than one Tessl organization, it stops and asks you to pick one, so add --org your-org. It's worth a quick check that the name in tessl.json now looks like your-org/tally. If it just says tally, the link didn't happen, and your schedule won't sync later.
Step 2: Write the loop's job
Before you can schedule a loop, you need to tell the agent what to do on each run. In Tessl, you write that down as a skill: a SKILL.md file of plain instructions that an agent follows, packaged in a plugin. You'll keep this one right in the repo, next to the code it works on.
Scaffold the plugin, replacing your-org with your Tessl workspace name:
1# Create a plugin for the loop's skill, inside the repo
2tessl plugin new \
3 --name your-org/docs-sync \
4 --workspace your-org \
5 --path ./plugins/docs-sync \
6 --skill-name docs-sync \
7 --skill-description "Compare the tally CLI's commands with README.md, and open a pull request that fixes any drift."
This creates plugins/docs-sync/, with a manifest in .tessl-plugin/plugin.json and a starter skill in skills/docs-sync/SKILL.md. The starter skill has your name and description at the top, and a placeholder underneath. Replace the whole file with the loop's instructions:
1---
2name: docs-sync
3description: Compare the tally CLI's commands with README.md, and open a pull request that fixes any drift.
4---
5
6# Keep the README in sync with the CLI
7
8## Find the drift
91. Run `node bin/tally.js --help` and note every command and its description.
102. Read the Commands section of README.md.
113. List every difference: commands the README is missing, commands it lists that no longer exist, and descriptions that no longer say what the command does.
12
13## Fix it
14- The code is the source of truth. Change README.md to match the CLI, and never change code to match the docs.
15- Only edit README.md.
16- Run `npm test`, and only carry on if it passes.
17
18## Report
19- If you found no drift, stop there. Don't open a pull request.
20- Otherwise, commit the fix to a branch called `docs/reconcile-drift`, and open one pull request titled `docs: reconcile drift`.
21- Start its description with "Drift fixed:", then add one bullet per fix: the file, what you changed, and the CLI output it now matches. Mention any commands that already matched.
The important bits:
- Find the drift - the loop runs the CLI rather than reading the source, for the same reason the test does: it checks the docs against what users actually see.
- Fix it - the loop treats the code as the source of truth, and only touches the README. It also runs the test before it opens anything, so it checks its own work.
- Report - a run that finds nothing does nothing, so you only hear from the loop when there's something to review. When it does find something, the same title, branch and description format every time makes its pull requests easy to spot and quick to check.
Keep a loop's job this narrow. A loop with one clear job does it well, while a loop told to "improve the docs" tries to fix everything and does none of it properly. If you'd rather start from something ready-made, the tessl/docs-audit plugin does the same job for bigger repos. Writing your own shows you what's inside a loop, though, and it's the same shape for any chore you want off your plate.
Check the plugin is valid, then commit it:
1# Check the plugin is valid
2tessl plugin lint plugins/docs-sync
3
4# Commit the plugin
5git add -A && git commit -m "Add docs-sync loop skill"
Step 3: Schedule the loop
To run on a schedule, the loop needs two things: somewhere to run, and permission to open pull requests. That's what an environment is for. It works like an environment in GitHub Actions: a named place for runs, holding the secrets and permissions they're allowed to use. Each run checks out your repo in Tessl's cloud, and gets only what its environment grants.
Create an environment for the loop to run in:
1# Create an environment the loop can run in, with permission to open PRs
2tessl env create tally-loops --github-access write
The environment needs write access so the loop can open pull requests. Without it, a run can happily read your repo, but can't change a thing. It's automatically tied to the project this folder is linked to, so there's nothing else to wire up.
Now add a schedules block to tessl.json, leaving everything else in the file as it is:
1"schedules": {
2 "docs-sync": {
3 "action": {
4 "skill": "file:plugins/docs-sync",
5 "environment": "tally-loops"
6 },
7 "schedule": "weekly on monday at 08:00",
8 "description": "Check README.md against the CLI and open a PR when they drift."
9 }
10}
The important bits:
skill- the plugin the loop runs.file:points at the one you just wrote, relative totessl.json. A plugin from the Tessl Registry works here too.environment- where the loop runs and what it's allowed to do. This is the one you just created.schedule- when it runs. Tessl understands plain shorthand like this, or a regular cron expression.
Validate the schedule, then push it to main:
1# Check the schedule, plugin, environment and project all line up
2tessl schedule validate
3
4# Pushing to main is what creates the schedule
5git add -A && git commit -m "Add weekly docs sync" && git push
Pushing to your default branch is what actually creates the schedule. Because you're signed in and the project is linked, validate also checks that the plugin, environment and project all line up before you push, which saves you a frustrating round trip. Give it a minute, and tessl schedule list will show your shiny new docs-sync loop. Make a note of its ID, because you'll need it in a moment.
Notice where the schedule lives: in your repo, right next to the code it runs against. Anyone on the team can read it, and changing it goes through a pull request like any other change, so there's no mystery settings page to go hunting for.
Step 4: Plant drift and watch the loop fix it
Your loop is scheduled, but your check isn't wired into anything yet. It only runs when someone remembers to run it on their laptop, and even then it only warns. So drift can still sneak onto main, which is exactly what happens on real teams. Let's simulate it: add an undo command to bin/tally.js without documenting it, and push it straight to main.
In the commands object, add a line after reset:
1 undo: 'Remove the last number added',
Then push it:
1git commit -am "Add undo command" && git push
node bin/tally.js --help now proudly lists undo, and npm test fails because the README has never heard of it. Resist the urge to fix it yourself. That's the loop's job now.
Before you trust the schedule, run the loop once by hand and see what it does. You don't need to wait until Monday: kick off a run now, then check on it until its status is Completed:
1# Start a run now instead of waiting for Monday
2tessl schedule trigger <id>
3
4# Check its status until it says Completed
5tessl schedule runs <id>
Once it finishes, gh pr list shows a pull request titled docs: reconcile drift. It adds tally undo to the README, and its description lists each drift it fixed alongside the CLI output it now matches, so checking its homework only takes a minute.
Zoom
Check the loop's work
Don't merge it on trust, though. The loop already ran npm test before it opened the pull request, because your skill told it to, so the mechanical check is done. Your job is the part that takes a person: read the diff, and decide whether the fix is right. The description lists each drift next to the CLI output it now matches, so for a change this small, that takes a minute on GitHub.
If you'd like to see the test pass for yourself, check out the branch:
1# Check out the loop's pull request locally
2gh pr checkout <PR number>
3
4# All three tests should pass again
5npm test
In a real project, your CI would run the test on the pull request for you. The verifier would report "not applicable" rather than a pass, and that's correct: this pull request only changes the README, and the verifier's rule is about changes to commands.
Remember the rule you wrote into the skill: the code is the source of truth, so the loop never changes code to match the docs. If that undo command had been a mistake, the loop would have cheerfully documented the mistake. That's why a person still reviews what the loop opens.
That's also why the first few runs deserve a close read. Tessl's own maintenance loops file issues rather than opening pull requests, and only get more autonomy once the team trusts what they find. Docs fixes like this one are small and mechanical, which is why your loop goes straight to a pull request, but the same rule applies: a loop earns trust one run at a time.
Once you're happy with it, merge it with gh pr merge <PR number> --squash, then switch back with git checkout main && git pull.
What's next
Kudos! 🎉 You've written and scheduled your first loop. Every Monday, it compares the docs with the code, and when they've drifted, it opens a pull request that's already passed the test. You've also checked its work before trusting it, which is how every loop should start.
There's one question left, and it's the one that turns a scheduled job into a loop that gets better: why did the undo drift reach main in the first place? In Part 3, you'll answer it, and use the answer to turn the check into a gate.
COPY & SHARE

Chandler Mayo
Chandler Mayo is a Developer Advocate at Tessl. He works with developer communities to make Tessl easier to understand, adopt, and build with, and brings what he hears from developers back to the product team.
YOUR NEXT READ
The best loop finds nothing to fix
TL;DR: In Part 1, you built a check that runs on every change to a CLI's docs. In Part 2, you wrote a loop that sweeps for drift every week and opens a pull request to fix it. In this post, you'll...
Chandler Mayo
·26 Nov 2026·12 min read
More articles by Chandler Mayo
出处
- S 官方Tessl Blog · RSS阅读原文 ↗
Build an agent that opens a PR whenever your docs drift
生成时使用了正文节选。
Stop your docs drifting from your code: catch it on every change TL;DR: A loop is an agent process that does one job, with a defined input and output, in an environment you control. A docs-sync loop, for example, compares a CLI's README with its code and opens a... Chandler Mayo ·19 Nov 2026