Skip to content

fix: isolate detectAiAgent test from ambient environment - #1421

Draft
l2ysho wants to merge 1 commit into
masterfrom
claude/magical-haibt-8b2ff6
Draft

fix: isolate detectAiAgent test from ambient environment#1421
l2ysho wants to merge 1 commit into
masterfrom
claude/magical-haibt-8b2ff6

Conversation

@l2ysho

@l2ysho l2ysho commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Note

The detectAiAgent > returns undefined when no agent env vars are set test fails for anyone running pnpm run test:local from inside an AI coding tool. It never cleared the ambient environment, so CLAUDECODE leaked 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 with AssertionError: 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

  • beforeEach stubs all nine vars from AI_AGENT_ENV_VARS to '' with vi.stubEnv. Empty string is falsy, so the detector sees none set.
  • afterEach calls vi.unstubAllEnvs(), which restores the real ambient values, replacing the manual delete loop.
  • The two positive cases use vi.stubEnv too, so their values are restored as well.

The assertion is unchanged.

Verification

Node 22.23.2, run from inside Claude Code:

  • CLAUDECODE=1 set: 7/7 pass
  • CLAUDECODE and CLAUDE_CODE_ENTRYPOINT unset: 7/7 pass
  • CURSOR_AGENT=1 CODEX_SANDBOX=1 OPENCODE=1: 7/7 pass
  • pnpm run lint, pnpm run format, pnpm run test:local: clean, 557 passed / 4 skipped

Follow-up

The var list in the test duplicates AI_AGENT_ENV_VARS in src/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

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>
@github-actions github-actions Bot added this to the 149th sprint - Tooling team milestone Sep 10, 2026
@github-actions github-actions Bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants