{"record":{"id":"170cabe35a072b8c","repo":"RocketChat/Rocket.Chat","slug":"invalid-agentid","errorCode":null,"errorMessage":"Invalid agentId","messagePattern":"Invalid agentId","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/app/apps/server/bridges/livechat.ts","lineNumber":164,"sourceCode":"\t\tconst visitor = this.orch.getConverters()?.get('visitors').convertAppVisitor(room.visitor);\n\n\t\tconst closeData: any = {\n\t\t\troom: await this.orch.getConverters()?.get('rooms').convertAppRoom(room),\n\t\t\tcomment,\n\t\t\t...(user && { user }),\n\t\t\t...(visitor && { visitor }),\n\t\t};\n\n\t\tawait closeRoom(closeData);\n\n\t\treturn true;\n\t}\n\n\tprotected async findOpenRoomsByAgentId(agentId: string, appId: string): Promise<ILivechatRoom[]> {\n\t\tthis.orch.debugLog(`The App ${appId} is looking for livechat rooms associated with agent ${agentId}`);\n\n\t\tif (!agentId) {\n\t\t\tthrow new Error('Invalid agentId');\n\t\t}\n\n\t\tconst rooms = await LivechatRooms.findOpenByAgent(agentId).toArray();\n\t\treturn Promise.all(rooms.map((room) => this.orch.getConverters()?.get('rooms').convertRoom(room) as Promise<ILivechatRoom>));\n\t}\n\n\tprotected async countOpenRoomsByAgentId(agentId: string, appId: string): Promise<number> {\n\t\tthis.orch.debugLog(`The App ${appId} is counting livechat rooms associated with agent ${agentId}`);\n\n\t\tif (!agentId) {\n\t\t\tthrow new Error('Invalid agentId');\n\t\t}\n\n\t\treturn LivechatRooms.countOpenByAgent(agentId);\n\t}\n\n\tprotected async findRooms(visitor: IVisitor, departmentId: string | null, appId: string): Promise<Array<ILivechatRoom>> {\n\t\tthis.orch.debugLog(`The App ${appId} is looking for livechat visitors.`);","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/app/apps/server/bridges/livechat.ts#L146-L182","documentation":"Thrown by AppLivechatBridge.findOpenRoomsByAgentId when the supplied agentId is falsy. The bridge refuses to query LivechatRooms.findOpenByAgent with an empty id because it would either return nothing meaningful or match unintended documents. This is a guard against undefined/null leaking from App code.","triggerScenarios":"An App calls the livechat read accessor to find open rooms for an agent (e.g. app.getLivechatRead().getOpenRooms(agentId)) with agentId being undefined, null, or empty string.","commonSituations":"App reads agentId from a context object that did not populate it; App iterates a list and passes an empty slot; App passes a visitor token or username where an agent user id is expected; refactoring dropped the agentId argument.","solutions":["Ensure agentId is a non-empty string before calling the accessor (guard with an if or early return).","Trace where agentId is sourced and confirm the producer always sets it.","Add a runtime assertion / parameter validation in the App before invoking the read.","Distinguish 'no agent' from 'empty agent' at the call site."],"exampleFix":"// before\nconst rooms = await app.getLivechatRead().getOpenRooms(agent?.id);\n\n// after\nif (!agent?.id) {\n  return [];\n}\nconst rooms = await app.getLivechatRead().getOpenRooms(agent.id);","handlingStrategy":"validation","validationCode":"function assertAgentId(agentId: string | undefined): asserts agentId is string {\n  if (!agentId || typeof agentId !== 'string') {\n    throw new Error('agentId is required to find open livechat rooms');\n  }\n}\nassertAgentId(agentId);\nawait app.getLivechatRead().getOpenRooms(agentId);","typeGuard":"const isNonEmptyAgentId = (id: unknown): id is string =>\n  typeof id === 'string' && id.trim().length > 0;","tryCatchPattern":"try {\n  return await app.getLivechatRead().getOpenRooms(agentId);\n} catch (e) {\n  if ((e as Error).message === 'Invalid agentId') return [];\n  throw e;\n}","preventionTips":["Validate agentId is a non-empty string before the call.","Return an empty array when the agent is unknown rather than calling the bridge.","Trace agentId producers to ensure they always populate the field."],"tags":["apps-engine","livechat","validation","agent","argument-validation"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}