Skip to content
Merged
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
43 changes: 43 additions & 0 deletions src/AgentDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace TicketSwap\PHPStanErrorFormatter;

// Copied from https://github.com/shipfastlabs/agent-detector/blob/main/src/AgentDetector.php
final class AgentDetector
{
public static function isAgent() : bool
{
$aiAgent = getenv('AI_AGENT');

if (is_string($aiAgent) && trim($aiAgent) !== '') {
return true;
}

$agentsWithEnvVars = [
'cursor' => ['CURSOR_AGENT'],
'gemini' => ['GEMINI_CLI'],
'codex' => ['CODEX_SANDBOX', 'CODEX_THREAD_ID'],
'augment-cli' => ['AUGMENT_AGENT'],
'opencode' => ['OPENCODE_CLIENT', 'OPENCODE'],
'amp' => ['AMP_CURRENT_THREAD_ID'],
'claude' => ['CLAUDECODE', 'CLAUDE_CODE'],
'replit' => ['REPL_ID'],
];

foreach ($agentsWithEnvVars as $envVars) {
foreach ($envVars as $envVar) {
if (getenv($envVar) !== false) {
return true;
}
}
}

if (file_exists('/opt/.devin')) {
return true;
}

return false;
}
}
2 changes: 1 addition & 1 deletion src/TicketSwapErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static function link(
$editorUrl,
),
'{relativePath}' => $relativePath,
'{shortPath}' => self::trimPath($relativePath),
'{shortPath}' => AgentDetector::isAgent() ? $relativePath : self::trimPath($relativePath),
':{line}' => $line === 0 ? '' : ':' . $line,
'{line}' => $line === 0 ? '' : (string) $line,
],
Expand Down
25 changes: 25 additions & 0 deletions tests/TicketSwapErrorFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,31 @@ public function getWrites() : array
};
}

public function testLinkDoesNotTrimPathWhenAgent() : void
{
putenv('AI_AGENT=1');

try {
$result = TicketSwapErrorFormatter::link(
TicketSwapErrorFormatter::LINK_FORMAT_DEFAULT,
20,
self::isWindows() ? 'c:\www\project\src\Core\Admin\Controller\Dashboard\User\AddUserController.php' : '/www/project/src/Core/Admin/Controller/Dashboard/User/AddUserController.php',
self::isWindows() ? 'src\Core\Admin\Controller\Dashboard\User\AddUserController.php' : 'src/Core/Admin/Controller/Dashboard/User/AddUserController.php',
self::PHPSTORM_EDITOR_URL,
true,
);

$expectedRelativePath = self::isWindows()
? 'src\Core\Admin\Controller\Dashboard\User\AddUserController.php'
: 'src/Core/Admin/Controller/Dashboard/User/AddUserController.php';

self::assertStringContainsString($expectedRelativePath, $result);
self::assertStringNotContainsString('...', $result);
} finally {
putenv('AI_AGENT');
}
}

public function testFormatErrorsNoErrorsWritesNoErrorsAndReturnsZero() : void
{
$analysisResult = new AnalysisResult(
Expand Down
Loading