My coding agents can flip feature flags now, but only for themselves
My coding agents now test both the on and off paths whenever they build behind a feature flag. Each runs on its own test environment without asking me. None can change a flag for anyone else, even if it tried.
All it took was one small LaunchDarkly role. Before that, my agents were faking it.
Most of what we ship at work goes behind a flag. You merge something that isn’t ready. You turn it on later, one customer at a time. Every change has at least two versions to check. The one off by default is the one people forget. Months later someone flips it on, often in a hurry, assuming the code underneath was tested.
My agents couldn’t change flags. When a change needed one on, the verifying agent found a workaround. It swapped in a fake flag client. It patched the default in a throwaway commit. Or it rewrote flag responses on their way into the browser. Then it reported the check as passed, with a note saying the flag had been emulated.
I found one of those notes in a pull request description after the fact. The real flag sat one line away in LaunchDarkly. I could have made that edit in a minute if I’d known.
Another time an agent stubbed a flag in the browser. Everything looked right until we redid it with the real flag. Once the server saw it too, a second behavior showed up on the backend. The stub had hidden it completely.
An emulated flag only tests the emulation.
The agents needed the real thing. Giving an agent a LaunchDarkly token with write access is how you flip a flag in production on a Tuesday afternoon. I wanted something much narrower.
The narrow version was already sitting in our setup.
Each of our test environments is a private copy of the platform. Each creates its own test user with a unique ID. LaunchDarkly can target a single user: serve this variation to this one user key, whatever the rules say for everyone else. A target on one environment’s test user changes that environment and nothing else.
With a colleague’s help getting admin access, I made a role that does one thing: add or remove a per-user target. It only applies to the two LaunchDarkly environments our test environments read from. The role can’t touch rules, defaults or whether a flag is on at all. It can’t see staging or production. If an agent tries anything else, LaunchDarkly itself refuses. I trust that more than any instruction in a prompt.
Around that role is a small command-line tool and instructions the agent reads before using it:
- Add and remove only. Every write adds or removes one user key. The tool never replaces a flag’s whole target list. That would wipe out everyone else’s targets.
- Targets expire. By default a target is gone after 7 days. Any forgotten one cleans itself up.
- An off flag stays off. A switched-off flag ignores targets completely. Switching it on affects every environment. The tool won’t look for a way around it. It stops and says a person has to do it.
- Prove the effect. After adding a target, the agent loads the page or calls the route on its own environment. Then it checks a second environment as a control.
That control mattered because of a trap I’d already walked into myself. Our console - the part in the browser - reads its flags from one LaunchDarkly environment. The backend reads from another. Once I turned a flag off in one environment to preview production. I kept seeing the new UI anyway. The browser was reading from the other one.
Most features use a pair of flags: one for the UI, one for the backend. They only line up when both halves are set in both places. That’s where the combinations come in. A permissions screen might have four flags. The interesting bugs are in the mixes: UI on and backend off, one on and one off or the default for everyone with one user opted out. Now an agent sets up each combination on its own environment. It checks one after the next in minutes, without me in the loop.
At first the role was only mine. A colleague who tests a lot of the same features asked about it. I had it assigned to every engineer. We tried it together before I shared it more widely.
A week later it changed how we ran a bug bash. The new permissions UI was off by default in those test environments, with targets for the people building it. Anyone invited to a test environment for the bug bash got the old behavior. Two of the bugs filed that day weren’t bugs at all. The testers just had the flags off. So I flipped it around: on by default in the test environments. Anyone who needs the off path adds a target for their own environment’s test user. Production and staging didn’t change.
Once turning a flag off for yourself is easy, the default can be whatever is most useful.
The last step was the shared agent my whole team can start, which I wrote about here. It got the same role this week. When it builds behind a flag, both paths get checked on a real environment. Its pull request comes back quoting what each LaunchDarkly environment serves.
If you want the same thing, the pieces are probably already there:
- Something unique per test environment. A test user with its own ID, or any context key only that environment sends.
- A custom role that can only target. Add and remove targets in non-production environments. Nothing else.
- Rules your agent reads first. Add and remove only. Targets expire. An off flag is a person’s call. Prove it on the page.
The agents never needed the keys to LaunchDarkly, just one switch with their name on it.