{"record":{"id":"5b309d81cdeec5f0","repo":"paperclipai/paperclip","slug":"agent-selection-failed","errorCode":null,"errorMessage":"Agent selection failed","messagePattern":"Agent selection failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/client/connect.ts","lineNumber":230,"sourceCode":"  if (!selected) return null;\n  return companies.find((company) => company.id === selected) ?? null;\n}\n\nasync function chooseAgent(agents: Agent[], preferredAgentId: string | undefined): Promise<Agent> {\n  const selected = await p.select({\n    message: \"Agent\",\n    initialValue: preferredAgentId && agents.some((agent) => agent.id === preferredAgentId)\n      ? preferredAgentId\n      : agents[0]?.id,\n    options: agents.map((agent) => ({\n      value: agent.id,\n      label: agent.name,\n      hint: agent.role,\n    })),\n  });\n  assertNotCancelled(selected);\n  const agent = agents.find((item) => item.id === selected);\n  if (!agent) throw new Error(\"Agent selection failed\");\n  return agent;\n}\n\nfunction buildExports(input: {\n  apiBase: string;\n  companyId?: string;\n  agentId?: string;\n  envName: string;\n  token: string;\n}): string {\n  const escaped = (value: string) => value.replace(/'/g, \"'\\\"'\\\"'\");\n  return [\n    `export PAPERCLIP_API_URL='${escaped(input.apiBase)}'`,\n    input.companyId ? `export PAPERCLIP_COMPANY_ID='${escaped(input.companyId)}'` : null,\n    input.agentId ? `export PAPERCLIP_AGENT_ID='${escaped(input.agentId)}'` : null,\n    `export ${input.envName}='${escaped(input.token)}'`,\n  ].filter((line): line is string => Boolean(line)).join(\"\\n\");\n}","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/client/connect.ts#L212-L248","documentation":"Defensive guard in chooseAgent: after the @clack select prompt resolves, agents.find(...) by the selected id returns undefined. Because the select options are built from the same agents array, this should not happen.","triggerScenarios":"Only if the agents array is mutated between building the select options and the find call (race with concurrent agent deletion), or a future refactor breaks the option/value pairing.","commonSituations":"Another operator deletes the agent while you are at the prompt; profile.agentId pointed at an id that vanished.","solutions":["Re-run connect; refresh fetches the current agents list.","Confirm no concurrent agent deletion is happening in the target company."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const agent = agents.find((a) => a.id === selected);\nif (!agent) throw new Error('Agent selection failed');","typeGuard":"const isAgent = (v: unknown): v is Agent =>\n  !!v && typeof v === 'object' && typeof (v as any).id === 'string';","tryCatchPattern":null,"preventionTips":["Avoid concurrent agent deletion during interactive connect.","Re-run connect if the agents list may have changed."],"tags":["cli","connect","defensive","race"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}