diff --git a/packages/cli/src/utils/client.ts b/packages/cli/src/utils/client.ts index 27d1a4dbcb..2a594753fe 100644 --- a/packages/cli/src/utils/client.ts +++ b/packages/cli/src/utils/client.ts @@ -236,6 +236,8 @@ interface AgentLike { title?: string | null; } +const FULL_AGENT_UUID_PATTERN = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i; + /** * Resolve an agent ID from a partial ID or name. * Supports: @@ -252,6 +254,11 @@ export function resolveAgentId(idOrName: string, agents: AgentLike[]): string | const query = idOrName.toLowerCase(); + // A complete agent UUID is an exact identity, never a name search fallback. + if (FULL_AGENT_UUID_PATTERN.test(idOrName)) { + return agents.find((a) => a.id.toLowerCase() === query)?.id ?? null; + } + // Try exact ID match first const exactMatch = agents.find((a) => a.id === idOrName); if (exactMatch) { diff --git a/packages/cli/tests/29-agent-id-resolution.test.ts b/packages/cli/tests/29-agent-id-resolution.test.ts index 721e307aea..bb59a35a34 100644 --- a/packages/cli/tests/29-agent-id-resolution.test.ts +++ b/packages/cli/tests/29-agent-id-resolution.test.ts @@ -18,25 +18,52 @@ const agents = [ } { - console.log("Test 2: ID prefix resolves"); + console.log("Test 2: full UUID resolves exactly, case-insensitively"); + const exactId = "9eac296c-4d60-4189-a0b5-ae71dd9c6ad9"; + const agentsWithNameCollision = [ + { id: exactId, title: "target agent" }, + { + id: "c9a97e5d-4a92-4646-a016-53032146d0a1", + title: `worker ${exactId}`, + }, + ]; + assert.strictEqual(resolveAgentId(exactId.toUpperCase(), agentsWithNameCollision), exactId); + console.log("✓ full UUID resolves exactly, case-insensitively\n"); +} + +{ + console.log("Test 3: missing full UUID does not match another agent name"); + const missingId = "9eac296c-4d60-4189-a0b5-ae71dd9c6ad9"; + const nameCollision = [ + { + id: "c9a97e5d-4a92-4646-a016-53032146d0a1", + title: `worker ${missingId}`, + }, + ]; + assert.strictEqual(resolveAgentId(missingId, nameCollision), null); + console.log("✓ missing full UUID does not match another agent name\n"); +} + +{ + console.log("Test 4: ID prefix resolves"); assert.strictEqual(resolveAgentId("def5678", agents), "def5678-other-agent-id"); console.log("✓ ID prefix resolves\n"); } { - console.log("Test 3: exact name match resolves case-insensitively"); + console.log("Test 5: exact name match resolves case-insensitively"); assert.strictEqual(resolveAgentId("fix archive flow", agents), "def5678-other-agent-id"); console.log("✓ exact name match resolves case-insensitively\n"); } { - console.log("Test 4: partial name match resolves when unique"); + console.log("Test 6: partial name match resolves when unique"); assert.strictEqual(resolveAgentId("review", agents), "987zyxw-third-agent-id"); console.log("✓ partial name match resolves when unique\n"); } { - console.log("Test 5: missing agent returns null"); + console.log("Test 7: missing agent returns null"); assert.strictEqual(resolveAgentId("does not exist", agents), null); console.log("✓ missing agent returns null\n"); }