← sources

My agent ran tests on a copy of its own repo and still hit the live database. How much of the environment do you pass to commands your agent runs?

source: reddit (reddit:AI_Agents)published 2026-10-09 08:01ingested 2026-10-10 02:34

retained text

Two of us are building a desktop agent that runs on a local model. Something happened a few days ago, and there's a part we still haven't decided on, so I'm asking here.

We had the agent fix a bug in its own code. We didn't want it touching the install that was running, so we copied the whole repo to another folder and told it to work only in that copy.

It found the cause, fixed it, and ran pytest in the copy. Right then, the task it was working on flipped to "failed" on screen.

**Here's what happened:**

\- On startup, the agent puts the path of its own data folder into an environment variable.

\- When it ran a command, it handed its whole environment to that command.

\- The tests in the copy started a temporary instance. That instance read the variable and attached to the live database instead of the copy's.

\- It then picked up the task that was still in progress there and marked it as failed.

The failure message came from a test-only safety guard, which is the only reason we could trace it. We didn't lose any data and the task went back to normal, but honestly I think we just got lucky.

While digging, we also noticed that the token that controls the agent was sitting in the environment the same way. So every command the model ran was getting our secret too.

**What we changed:**

\- Commands the agent runs no longer get any variable that starts with our prefix (a blocklist).

\- The test setup clears the same variables on start, so there are two layers.

\- We added a test that fails on the old code and passes on the new one.

We also checked the Codex CLI source. As far as we could tell, its default is to pass everything through, and "only pass what's needed" is something you opt into.

There are still parts we haven't figured out 。°(°.◜ᯅ◝°)°。  


>1. Which is right: "pass everything except our own stuff" or "pass only what's needed"? The second is safer, but it can drop variables the user's project depends on, and then you get "it works when I run it, but not when the agent does."

>2. Running in a container would be cleaner, but the whole point for us is running on the user's real machine, so we can't just do that. If you don't use containers, how do you handle this?

>3. Do you treat "the agent is editing its own code" as a special case? We only found out by getting burned.

This only happened once, so I'm not claiming anything general. If you've run into something similar or solved it differently, I'd love to hear it.

document fetched 2026-10-10 00:19 · canonical https://reddit.com/r/AI_Agents/comments/1x1fhie/my_agent_ran_tests_on_a_copy_of_its_own_repo_and