Add AI agent detection to auto-switch to raw output#4942
Open
pushpak1300 wants to merge 3 commits intophpstan:2.1.xfrom
Open
Add AI agent detection to auto-switch to raw output#4942pushpak1300 wants to merge 3 commits intophpstan:2.1.xfrom
pushpak1300 wants to merge 3 commits intophpstan:2.1.xfrom
Conversation
8536d19 to
ae99977
Compare
Switch from JSON to raw error format for AI agents. The raw format outputs one error per line as file:line:message, which saves ~49% tokens compared to JSON for typical error output (and 100% for zero-error runs. When an agent is detected, progress bar output is fully suppressed (equivalent to --no-progress), eliminating redraw noise that wastes tokens in agent context windows. Token comparison (3 errors across 2 files): - JSON: ~185 tokens (678 chars) with structural overhead per error - Raw: ~95 tokens (295 chars) with just file:line:message per line Zero errors: - JSON: ~18 tokens for empty structure - Raw: 0 tokens (no output) Explicit --error-format=table or config errorFormat: table always takes priority over agent detection.
ae99977 to
8724e6b
Compare
- Extract duplicated detection logic from AgentDetectedErrorFormatter and ErrorsConsoleStyle into AgentDetector with static method - Drop unnecessary ErrorFormatter implementation and @api tag - Replace REPL_ID with more specific REPLIT_AGENT env var - Fix test tearDown to clean all env vars
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.
This pull request adds AI agent detection that automatically switches PHPStan to raw error format and suppresses progress bar output when executed within Claude Code, Cursor, Gemini CLI, Codex, OpenCode, Augment, Replit
Why:
The default PHPStan output is designed for humans - colorful tables, progress bars with animations, and formatted summaries. AI agents need something different: minimal output that saves context window and reduces token cost, unambiguous error details with file paths and line numbers, and no progress bar animations that waste tokens on redraw noise.
Why raw format over JSON?
The raw format (
file:line:message, one error per line) is significantly more token-efficient than JSON:How it works:
CLAUDECODE/CLAUDE_CODEenvironment variableCURSOR_TRACE_ID/CURSOR_AGENTGEMINI_CLI,CODEX_SANDBOX,AI_AGENT, etc.)file:line:message)--no-progress)Detection is inlined (simple
getenv()calls) to avoid any external dependency and maintain compatibility with all PHP versions PHPStan supports.Output example:
Explicit
--error-format=tableor configerrorFormat: tablealways takes priority over agent detection.Related to #4939