Skip to content

Commit 7aeaada

Browse files
authored
Do not trim paths when an AI agent is detected (#36)
Agents need full file paths to correctly locate and edit files.
1 parent a03887a commit 7aeaada

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/AgentDetector.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TicketSwap\PHPStanErrorFormatter;
6+
7+
// Copied from https://github.com/shipfastlabs/agent-detector/blob/main/src/AgentDetector.php
8+
final class AgentDetector
9+
{
10+
public static function isAgent() : bool
11+
{
12+
$aiAgent = getenv('AI_AGENT');
13+
14+
if (is_string($aiAgent) && trim($aiAgent) !== '') {
15+
return true;
16+
}
17+
18+
$agentsWithEnvVars = [
19+
'cursor' => ['CURSOR_AGENT'],
20+
'gemini' => ['GEMINI_CLI'],
21+
'codex' => ['CODEX_SANDBOX', 'CODEX_THREAD_ID'],
22+
'augment-cli' => ['AUGMENT_AGENT'],
23+
'opencode' => ['OPENCODE_CLIENT', 'OPENCODE'],
24+
'amp' => ['AMP_CURRENT_THREAD_ID'],
25+
'claude' => ['CLAUDECODE', 'CLAUDE_CODE'],
26+
'replit' => ['REPL_ID'],
27+
];
28+
29+
foreach ($agentsWithEnvVars as $envVars) {
30+
foreach ($envVars as $envVar) {
31+
if (getenv($envVar) !== false) {
32+
return true;
33+
}
34+
}
35+
}
36+
37+
if (file_exists('/opt/.devin')) {
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
}

src/TicketSwapErrorFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static function link(
169169
$editorUrl,
170170
),
171171
'{relativePath}' => $relativePath,
172-
'{shortPath}' => self::trimPath($relativePath),
172+
'{shortPath}' => AgentDetector::isAgent() ? $relativePath : self::trimPath($relativePath),
173173
':{line}' => $line === 0 ? '' : ':' . $line,
174174
'{line}' => $line === 0 ? '' : (string) $line,
175175
],

tests/TicketSwapErrorFormatterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,31 @@ public function getWrites() : array
433433
};
434434
}
435435

436+
public function testLinkDoesNotTrimPathWhenAgent() : void
437+
{
438+
putenv('AI_AGENT=1');
439+
440+
try {
441+
$result = TicketSwapErrorFormatter::link(
442+
TicketSwapErrorFormatter::LINK_FORMAT_DEFAULT,
443+
20,
444+
self::isWindows() ? 'c:\www\project\src\Core\Admin\Controller\Dashboard\User\AddUserController.php' : '/www/project/src/Core/Admin/Controller/Dashboard/User/AddUserController.php',
445+
self::isWindows() ? 'src\Core\Admin\Controller\Dashboard\User\AddUserController.php' : 'src/Core/Admin/Controller/Dashboard/User/AddUserController.php',
446+
self::PHPSTORM_EDITOR_URL,
447+
true,
448+
);
449+
450+
$expectedRelativePath = self::isWindows()
451+
? 'src\Core\Admin\Controller\Dashboard\User\AddUserController.php'
452+
: 'src/Core/Admin/Controller/Dashboard/User/AddUserController.php';
453+
454+
self::assertStringContainsString($expectedRelativePath, $result);
455+
self::assertStringNotContainsString('...', $result);
456+
} finally {
457+
putenv('AI_AGENT');
458+
}
459+
}
460+
436461
public function testFormatErrorsNoErrorsWritesNoErrorsAndReturnsZero() : void
437462
{
438463
$analysisResult = new AnalysisResult(

0 commit comments

Comments
 (0)