fix: isolate detectAiAgent test from ambient environment - #1421
Draft
l2ysho wants to merge 1 commit into
Draft
Conversation
The "returns undefined when no agent env vars are set" case relied on the ambient environment being clean. It failed deterministically for anyone running the suite from inside Claude Code, Cursor, or a similar tool, since those set CLAUDECODE and friends. CI is green because it sets none of them. Stub all nine agent env vars to empty in beforeEach and restore them with vi.unstubAllEnvs. The assertion is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
The
detectAiAgent > returns undefined when no agent env vars are settest fails for anyone runningpnpm run test:localfrom inside an AI coding tool. It never cleared the ambient environment, soCLAUDECODEleaked in. Now all nine agent env vars are stubbed to empty per test.Problem
The test asserted no agent is detected, but relied on the ambient environment being clean. Inside Claude Code, Cursor, and similar tools,
CLAUDECODE(or a sibling var) is set, so the test failed withAssertionError: expected 'claude_code' to be undefined.Deterministic, not a flake — 6/6 failures on both master and feature branches. CI sets none of these variables, so it stayed green there.
Change
beforeEachstubs all nine vars fromAI_AGENT_ENV_VARSto''withvi.stubEnv. Empty string is falsy, so the detector sees none set.afterEachcallsvi.unstubAllEnvs(), which restores the real ambient values, replacing the manual delete loop.vi.stubEnvtoo, so their values are restored as well.The assertion is unchanged.
Verification
Node 22.23.2, run from inside Claude Code:
CLAUDECODE=1set: 7/7 passCLAUDECODEandCLAUDE_CODE_ENTRYPOINTunset: 7/7 passCURSOR_AGENT=1 CODEX_SANDBOX=1 OPENCODE=1: 7/7 passpnpm run lint,pnpm run format,pnpm run test:local: clean, 557 passed / 4 skippedFollow-up
The var list in the test duplicates
AI_AGENT_ENV_VARSinsrc/lib/hooks/telemetry/detectEnvironment.ts. A new agent var added to the source but not the test brings the leak back. Exporting the table and importing it in the test would close that gap — left out to keep this diff small.🤖 Generated with Claude Code