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
11 changes: 11 additions & 0 deletions src/agent/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const AGENT_EXECUTABLES: Record<AgentId, readonly string[]> = {
pi: ['pi'],
};

const AGENT_RUNTIME_MARKERS: Partial<Record<AgentId, readonly [string, string]>> = {
'claude-code': ['CLAUDE_CODE_CHILD_SESSION', '1'],
opencode: ['OPENCODE_CLIENT', 'desktop'],
hermes: ['HERMES_AGENT', 'true'],
pi: ['PI_CODING_AGENT', 'true'],
};

export function detectAgentsOnPath(env: NodeJS.ProcessEnv = process.env): Set<AgentId> {
const pathValue = process.platform === 'win32' ? env.PATH ?? env.Path : env.PATH;
if (pathValue === undefined) return new Set();
Expand Down Expand Up @@ -40,5 +47,9 @@ export function detectAgentsOnPath(env: NodeJS.ProcessEnv = process.env): Set<Ag
export function detectAvailableAgents(env: NodeJS.ProcessEnv = process.env): Set<AgentId> {
const agents = detectAgentsOnPath(env);
if (env.CODEX_THREAD_ID?.trim()) agents.add('codex');
for (const agent of AGENT_IDS) {
const marker = AGENT_RUNTIME_MARKERS[agent];
if (marker !== undefined && env[marker[0]] === marker[1]) agents.add(agent);
}
return agents;
}
22 changes: 22 additions & 0 deletions test/agent/availability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe('agent availability', () => {
expect(detectAvailableAgents({ CODEX_THREAD_ID: 'thread-id' })).toEqual(new Set(['codex']));
});

it('recognizes supported agent runtimes from their exact markers', () => {
for (const [env, agent] of [
[{ CLAUDE_CODE_CHILD_SESSION: '1' }, 'claude-code'],
[{ OPENCODE_CLIENT: 'desktop' }, 'opencode'],
[{ HERMES_AGENT: 'true' }, 'hermes'],
[{ PI_CODING_AGENT: 'true' }, 'pi'],
] as const) {
expect(detectAvailableAgents(env)).toEqual(new Set([agent]));
}
});

it('keeps PATH detection when recognizing the active Codex runtime', () => {
const root = executableDirectory('pi');
expect(detectAvailableAgents({
Expand All @@ -82,4 +93,15 @@ describe('agent availability', () => {
expect(detectAvailableAgents({ CODEX_THREAD_ID: '' })).toEqual(new Set());
expect(detectAvailableAgents({ CODEX_THREAD_ID: ' ' })).toEqual(new Set());
});

it('does not infer runtimes from broad or near-match markers', () => {
expect(detectAvailableAgents({
CLAUDECODE: '1',
CLAUDE_CODE_CHILD_SESSION: 'true',
OPENCODE_CLIENT: 'cli',
HERMES_AGENT: '1',
PI_CODING_AGENT: '1',
AI_AGENT: 'pi',
})).toEqual(new Set());
});
});
Loading