Skip to content

fix(tracking): Detect more coding agents; Improve typing around Ai Agents.#10104

Open
richieforeman wants to merge 4 commits intomainfrom
rf/agent-detection
Open

fix(tracking): Detect more coding agents; Improve typing around Ai Agents.#10104
richieforeman wants to merge 4 commits intomainfrom
rf/agent-detection

Conversation

@richieforeman
Copy link
Contributor

@richieforeman richieforeman commented Mar 15, 2026

This PR refactors the AI agent detection logic to improve type safety and maintainability. While here, I added detection of a few new coding agents or made the detection for some more robust.

Description

Refactored the environment detection logic in src/env.ts to use standardized constants and a union type. This ensures consistency across the codebase, particularly for Google Analytics tracking and API request headers.

Key changes include:

  • Constants Definition: Added CONSTANT_CASE constants for all supported AI agents (e.g., ANTIGRAVITY, CURSOR, GOOGLE_AI_STUDIO) in src/env.ts.
  • Type Safety: Introduced the AiAgents union type to replace ad-hoc string literals.
  • Improved Detection: Refactored detectAIAgent() to return the new union type and incorporated detection for GOOGLE_AI_STUDIO via the APPLET_DIR environment variable.
  • Standardized Headers: Updated standardHeaders in src/apiv2.ts to use the UNKNOWN constant.
  • Comprehensive Testing: Created src/env.spec.ts with 14 unit tests, utilizing Sinon stubs for process.env and module reloading to ensure reliable state isolation.

Scenarios Tested

  • Agent Detection: Verified that detectAIAgent() correctly identifies all 11+ agents (and UNKNOWN status) based on their respective environment variables.
  • State Management: Verified isFirebaseMcp() correctly reflects values set via setFirebaseMcp().
  • Environment Detection: Verified isFirebaseStudio() identifies both Monospace environments and the presence of the /google/idx folder.
  • Test Reliability: Confirmed that module reloading in the test suite correctly resets internal caches (e.g., for filesystem checks), preventing cross-test contamination.

Sample Commands

N/A (This internal refactoring does not change the user-facing CLI command structure or flags).

@richieforeman richieforeman requested a review from joehan March 15, 2026 13:04
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Firebase CLI's ability to identify and track various AI coding agents. By standardizing agent identifiers with constants and introducing a type-safe union, the changes improve code maintainability and robustness. The updated detection logic covers a broader range of AI environments, ensuring more accurate telemetry and better integration with different development tools.

Highlights

  • Standardized AI Agent Constants: Introduced CONSTANT_CASE constants for all supported AI agents, replacing magic strings for improved consistency and readability.
  • Enhanced Type Safety: Implemented an AiAgents union type to ensure type-safe handling of AI agent identifiers across the codebase, reducing potential errors.
  • Improved Agent Detection Logic: Refactored detectAIAgent() to return the new union type and expanded its capabilities to detect additional agents like GOOGLE_AI_STUDIO, REPLIT, and COPILOT, as well as more robust detection for CURSOR and OPEN_CODE.
  • Updated API Headers: Modified standardHeaders in src/apiv2.ts to utilize the new UNKNOWN constant for consistent agent tracking.
  • Comprehensive Unit Testing: Added a new test file (src/env.spec.ts) with extensive unit tests for AI agent detection, Firebase MCP, and Firebase Studio environment, ensuring reliability and preventing test contamination through module reloading.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/apiv2.ts
    • Imported the new UNKNOWN constant from src/env.ts.
    • Updated the standardHeaders function to use the UNKNOWN constant instead of the string literal "unknown" when an AI agent is not detected.
  • src/env.spec.ts
    • Added a new test file to provide comprehensive unit test coverage for the env module.
    • Included tests for detectAIAgent() to verify correct identification of various AI agents and the UNKNOWN state.
    • Added tests for isFirebaseMcp() to ensure it accurately reflects the value set by setFirebaseMcp().
    • Implemented tests for isFirebaseStudio() to confirm detection of Monospace environments and the /google/idx folder.
    • Utilized sinon for stubbing process.env and fsutils.dirExistsSync to isolate tests.
    • Incorporated module reloading in beforeEach to prevent cross-test contamination.
  • src/env.ts
    • Defined new export const variables for each AI agent identifier (e.g., ANTIGRAVITY, CLAUDE_CODE, UNKNOWN) to replace hardcoded string literals.
    • Introduced the AiAgents union type, derived from these new constants, to provide type safety for AI agent identification.
    • Refactored the detectAIAgent() function:
    • Updated its return type to AiAgents.
    • Modified the detection logic to use the newly defined constants.
    • Expanded detection for CLAUDE_CODE to differentiate between CLAUDE_CODE and CLAUDE_CODE_COWORK.
    • Enhanced CURSOR detection by checking additional environment variables (CURSOR_TRACE_ID, CODEX_THREAD_ID, CODEX_SANDBOX_NETWORK_DISABLED).
    • Improved OPEN_CODE detection to include OPENCODE_CLIENT.
    • Added new detection logic for REPLIT (using REPLIT_USER or REPL_ID), COPILOT (using COPILOT_MODEL), and GOOGLE_AI_STUDIO (using APPLET_DIR).
Activity
  • No specific activity (comments, reviews, progress updates) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively refactors the AI agent detection logic, enhancing type safety and maintainability by introducing constants and a union type. The addition of unit tests is a great step towards ensuring the reliability of this logic. I have a couple of suggestions: one to improve the test coverage to be more comprehensive, and another to simplify a conditional block for better readability, in line with the repository's style guide.

### Description
Refactors the AI agent detection logic to improve type safety and maintainability.

Key changes:
- Defined CONSTANT_CASE constants for all supported AI agents in src/env.ts.
- Introduced an AiAgents union type for better type safety.
- Refactored detectAIAgent() to use these constants and return the new type.
- Incorporated detection for GOOGLE_AI_STUDIO via the APPLET_DIR environment variable.
- Updated apiv2.ts to use the normalized UNKNOWN constant.
- Added 18 unit tests in src/env.spec.ts with Sinon stubs and module reloading.

### Scenarios Tested
- Unit Testing (src/env.spec.ts): Verified detection of all AI agents (Antigravity, Claude, Cline, Codex, Cursor, Gemini, OpenCode, Replit, Copilot, Google AI Studio) and alternative environment variables.
- Verified isFirebaseStudio() and isFirebaseMcp() logic.

### Sample Commands
N/A
if (
process.env["CURSOR_AGENT"] ||
process.env["CURSOR_TRACE_ID"] ||
process.env["CODEX_THREAD_ID"] ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these CODEX env vars really map to cursor? Smells fishy to me, but i have seen weirder.

@joehan
Copy link
Member

joehan commented Mar 16, 2026

Quick sanity check, but LGTM otherwise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants