Featured image of post The bot that cloned the estate every hour

The bot that cloned the estate every hour

My support bot packed up every repository I own and unpacked it again, once an hour, for a couple of days. Fixing it took seven repositories.

While I was busy blaming my agents for eating my GitLab rate limit back in October (they weren’t, it was me opening merge requests, and that’s a story of its own), my own support bot was failing to refresh 69 of the 77 repositories it reads. Once an hour, for a couple of days.

phpbotscout sits in Discord and answers questions from the estate’s docs and code, with citations, so a stale index means stale answers. And the way it kept that index fresh turned out to be about as rate-limit-friendly as I could have designed it. On the hour it cloned all of them from scratch, over HTTPS, one after another, and when GitLab started answering 429 Too Many Requests it just… carried on to the next one.

Packed for a day trip

Oddly enough, the cloning was deliberate, and the reasoning sat right above it in the doc comment:

// CloneFetcher gets repository content by shallow-cloning in memory.
//
// In memory rather than to disk deliberately. The corpus predicate is a
// security boundary, and content that never touches the filesystem cannot be
// left behind by a crash between the clone and the check that should have
// rejected it.

The corpus predicate is the rule for what the bot is allowed to answer from (I wrote about why I treat it as a security boundary when the issue tracker came up). Clone into memory, read what qualifies, drop the rest, and if the process falls over half way through, nothing’s left lying about on disk to be found later.

When I threw that part of the bot together, I had automation in mind: something that runs as a job, does its indexing and goes away again. For that, travelling light makes a lot of sense. You pack what you need, carry it with you, and leave nothing behind when you go home.

But phpbotscout turned into something else: a daemon that lives on my server permanently, and a resident who packs the whole house into a suitcase on the hour and unpacks it again is a strange sort of resident. It’s slow, it clones each repository in full whether anything changed or not, and over HTTPS each of those clones spends the same web rate limit I was already tripping over from my browser.

Moving in

The fix I wanted was to clone each repository once, keep it, and fetch whatever had changed. The cautious version would have kept those clones in memory for the life of the process, so the old security reasoning could stand, and I wasn’t keen. Seventy-odd repositories held in memory balloon quite a bit, and they don’t survive a restart, so each deploy would have been the old clone-everything all over again. For a resident daemon it made far more sense to turn it round and keep them on disk.

That went into spec 0033 before any code, and the new doc comment shows how the thinking moved: the clones are now “a cache of public content”, the corpus predicate runs before a source is fetched, and a prune step removes anything that stops qualifying. In practice the clones live on disk and get fast-forwarded two at a time, a source the forge reports as untouched since the last run is skipped, and the first 429 stops the run, leaving the rest for next time with its index intact.

It all goes over SSH too, which keeps the git traffic off GitLab’s web rate limit altogether. That needed a key, and the easy one to hand was mine. I gave the bot its own instead, on its own service account, with gitlab.com’s host key pinned in a known_hosts file of its own rather than trusted the first time it’s seen. A bot running its git traffic as me was exactly the kind of mess I’d spent that week sorting out.

Then the prune bit back. As it first shipped, in v0.14.0, it deleted everything in the clones directory that wasn’t a qualifying source. Point the clones directory at the same folder as the bot’s database (a perfectly reasonable-looking bit of config) and the first completed refresh would have tidied away phpbotscout.db… and its -wal and -shm files with it. The new resident’s first clear-out, binning the furniture along with the junk! My estate session, the agent that reviews merges for me, spotted it, and v0.14.1 removes only a git repository named for a source path and leaves anything else where it is.

An address that isn’t a URL

To fetch over SSH, the bot needed each repository’s SSH address from the forge, and that’s where the fix left the bot. My rule is that a gap in one of my modules gets fixed in the module, not worked around in whichever app happened to find it. There were a couple.

go/repo could clone, but it couldn’t fetch into a clone it already had or pull one forward. It gained both (pulling by fast-forward only, in v0.7.0), and then a way to check SSH host keys against a known_hosts file the caller names. go/forge, my layer over GitLab, GitHub, Gitea and Bitbucket, was already getting each repository’s SSH address back from all four APIs and throwing it away. So it gained an SSHCloneURL in discovery (v0.34.0), and each of the four adapters a release to fill it in.

It also needed a way to check that the address pointed somewhere sensible, and that’s where it got interesting. go/forge already had a host check for HTTPS URLs, but an SSH address like [email protected]:phpboyscout/phpbotscout.git isn’t a URL. It’s git’s older scp-style shorthand, user@host:path, and if you hand it to Go’s URL parser you get:

first path segment in URL cannot contain colon

So go/forge grew a parser of its own, and a parser for addresses that end up on git’s and ssh’s command lines is a small security story in itself. Each refusal is written up in spec 0030 with the git or ssh behaviour it guards against:

  • ssh reads anything starting with a dash as an option, so a leading - on the address, the user or the host is refused.
  • A one-character host is refused, since git reads C:foo as a Windows drive and not a server.
  • A host with a non-ASCII character is refused, because Unicode case folding can make a lookalike match a trusted name.
  • The user is split off at the last @ in the scp form, so an extra @ in the user part can’t move where the host starts.

Seven repositories for one bot

Counting it up: go/repo, go/forge, four forge adapters and the bot itself. Seven repositories, for a bot that cloned a bit too often.

Is that the toolbox working as intended, or the price of having one? Both, I think. I’ve always known the way the estate is put together comes with a large blast radius. A gap one app finds gets fixed where it belongs, the apps built on that module pick the fix up, and the work lands across several repositories at once. For what I’m building the estate to be, that’s the nature of the beast, and I’m fine with it.

The first refresh after the change went through without a single 429. It lives there properly now. It keeps its things on disk, only fetches what’s changed, and when it has a clear-out, it only throws away its own stuff.

Built with Hugo · Theme Stack designed by Jimmy