Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Command/AgentDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command;

use function getenv;
use function is_string;
use function trim;

final class AgentDetector
{

public static function isAgentDetected(): bool
{
$aiAgent = getenv('AI_AGENT');
if (is_string($aiAgent) && trim($aiAgent) !== '') {
return true;
}

return getenv('CURSOR_TRACE_ID') !== false
|| getenv('CURSOR_AGENT') !== false
|| getenv('GEMINI_CLI') !== false
|| getenv('CODEX_SANDBOX') !== false
|| getenv('AUGMENT_AGENT') !== false
|| getenv('OPENCODE_CLIENT') !== false
|| getenv('OPENCODE') !== false
|| getenv('CLAUDECODE') !== false
|| getenv('CLAUDE_CODE') !== false
|| getenv('REPLIT_AGENT') !== false;
}

}
7 changes: 6 additions & 1 deletion src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$errorFormat = $inceptionResult->getContainer()->getParameter('errorFormat');
}

$container = $inceptionResult->getContainer();

if ($errorFormat === null && AgentDetector::isAgentDetected()) {
$errorFormat = 'raw';
}

if ($errorFormat === null) {
$errorFormat = 'table';
}

$container = $inceptionResult->getContainer();
$errorFormatterServiceName = sprintf('errorFormatter.%s', $errorFormat);
if (!$container->hasService($errorFormatterServiceName)) {
$errorOutput->writeLineFormatted(sprintf(
Expand Down
8 changes: 5 additions & 3 deletions src/Command/ErrorsConsoleStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ final class ErrorsConsoleStyle extends SymfonyStyle
public function __construct(InputInterface $input, OutputInterface $output)
{
parent::__construct($input, $output);
$this->showProgress = $input->hasOption(self::OPTION_NO_PROGRESS) && !(bool) $input->getOption(self::OPTION_NO_PROGRESS);
$showProgress = $input->hasOption(self::OPTION_NO_PROGRESS) && !(bool) $input->getOption(self::OPTION_NO_PROGRESS);
$this->showProgress = $showProgress && !AgentDetector::isAgentDetected();
}

private function isCiDetected(): bool
Expand Down Expand Up @@ -95,9 +96,10 @@ public function createProgressBar(int $max = 0): ProgressBar
}

$ci = $this->isCiDetected();
$this->progressBar->setOverwrite(!$ci);
$agent = AgentDetector::isAgentDetected();
$this->progressBar->setOverwrite(!$ci && !$agent);

if ($ci) {
if ($ci || $agent) {
$this->progressBar->minSecondsBetweenRedraws(15);
$this->progressBar->maxSecondsBetweenRedraws(30);
} elseif (DIRECTORY_SEPARATOR === '\\') {
Expand Down
77 changes: 77 additions & 0 deletions tests/PHPStan/Command/AgentDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types = 1);

namespace PHPStan\Command;

use Override;
use PHPUnit\Framework\TestCase;
use function putenv;

class AgentDetectorTest extends TestCase
{

private const ENV_VARS = [
'AI_AGENT',
'CURSOR_TRACE_ID',
'CURSOR_AGENT',
'GEMINI_CLI',
'CODEX_SANDBOX',
'AUGMENT_AGENT',
'OPENCODE_CLIENT',
'OPENCODE',
'CLAUDECODE',
'CLAUDE_CODE',
'REPLIT_AGENT',
];

#[Override]
protected function setUp(): void
{
foreach (self::ENV_VARS as $var) {
putenv($var);
}
}

#[Override]
protected function tearDown(): void
{
foreach (self::ENV_VARS as $var) {
putenv($var);
}
}

public function testReturnsFalseWithNoEnvVars(): void
{
$this->assertFalse(AgentDetector::isAgentDetected());
}

public function testReturnsTrueWithAiAgent(): void
{
putenv('AI_AGENT=test');
$this->assertTrue(AgentDetector::isAgentDetected());
}

public function testReturnsFalseWithEmptyAiAgent(): void
{
putenv('AI_AGENT=');
$this->assertFalse(AgentDetector::isAgentDetected());
}

public function testReturnsTrueWithClaudeCode(): void
{
putenv('CLAUDE_CODE=1');
$this->assertTrue(AgentDetector::isAgentDetected());
}

public function testReturnsTrueWithCursorTraceId(): void
{
putenv('CURSOR_TRACE_ID=abc');
$this->assertTrue(AgentDetector::isAgentDetected());
}

public function testReturnsTrueWithReplitAgent(): void
{
putenv('REPLIT_AGENT=1');
$this->assertTrue(AgentDetector::isAgentDetected());
}

}
Loading