{"record":{"id":"8e65aeba365d3c06","repo":"paperclipai/paperclip","slug":"company-is-required-for-agent-profiles","errorCode":null,"errorMessage":"Company is required for agent profiles","messagePattern":"Company is required for agent profiles","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/client/connect.ts","lineNumber":126,"sourceCode":"      tokenCreatedAt: key.createdAt,\n    }, opts.context);\n    setCurrentProfile(profileName, opts.context);\n    p.outro(pc.green(`Connected profile '${profileName}' as board.`));\n    return {\n      ok: true,\n      profile: profileName,\n      persona: \"board\",\n      apiBase,\n      companyId: company?.id ?? null,\n      key: publicKeyResult(key),\n      exports: buildExports({ apiBase, companyId: company?.id, agentId: undefined, envName: apiKeyEnvVarName, token: key.token }),\n    };\n  }\n\n  const company = await chooseCompany(companies, opts.companyId ?? resolvedProfile.profile.companyId, {\n    optional: false,\n  });\n  if (!company) throw new Error(\"Company is required for agent profiles\");\n  const agents = (await boardApi.get<Agent[]>(apiPath`/api/companies/${company.id}/agents`)) ?? [];\n  if (agents.length === 0) throw new Error(`Company '${company.name}' has no agents to connect.`);\n  const agent = await chooseAgent(agents, resolvedProfile.profile.agentId);\n  const tokenName = opts.tokenName?.trim() || `cli-agent-${new Date().toISOString()}`;\n  const key = await boardApi.post<CreatedAgentKey>(apiPath`/api/agents/${agent.id}/keys`, createAgentKeySchema.parse({ name: tokenName }));\n  if (!key) throw new Error(\"Failed to create agent token\");\n  upsertProfile(profileName, {\n    apiBase,\n    companyId: company.id,\n    persona: \"agent\",\n    agentId: agent.id,\n    agentName: agent.name,\n    apiKeyEnvVarName,\n    tokenName: key.name,\n    tokenId: key.id,\n    tokenCreatedAt: key.createdAt,\n  }, opts.context);\n  setCurrentProfile(profileName, opts.context);","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/client/connect.ts#L108-L144","documentation":"Thrown at the start of the agent-profile connection flow if chooseCompany(...) returns null even though it was called with optional:false. It is a defensive guard after a selection that, by contract, should never yield null.","triggerScenarios":"Effectively unreachable: chooseCompany(companies, preferredCompanyId, {optional:false}) throws on an empty companies list and returns companies[0] for a single-item list, so a null return cannot occur through its documented paths.","commonSituations":"Encountered only if chooseCompany's contract is violated (e.g., a future refactor changes its return semantics) or the companies array is mutated to an unexpected shape mid-call.","solutions":["Verify the board credential returns at least one company via GET /api/companies before running connect.","If you genuinely hit this, file a bug: with optional:false the throw should be unreachable."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before calling connect, ensure the board credential can see companies.\nconst companies = (await boardApi.get('/api/companies')) ?? [];\nif (companies.length === 0) {\n  throw new Error('Create a company in the board UI before connecting an agent profile.');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always create at least one company before running `paperclipai connect`.","Smoke-test the board credential with GET /api/companies first."],"tags":["cli","connect","profile","defensive"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}