{"record":{"id":"d5bac277ecec7091","repo":"TencentCloud/TencentDB-Agent-Memory","slug":"team-mismatch","errorCode":"team_mismatch","errorMessage":"cannot ensure chat_memory asset: agent ${params.agent_id} belongs to team ${agent.team_id}, not ${params.team_id}","messagePattern":"cannot ensure chat_memory asset: agent (.+?) belongs to team (.+?), not (.+?)","errorType":"error_code","errorClass":"MetadataError","httpStatus":null,"severity":"error","filePath":"MemoryCore/src/metadata/service/metadata-service.ts","lineNumber":1253,"sourceCode":"    if (this.ensuredChatMemoryAssets.has(assetId)) {\n      const cached = await this.getAssetById(assetId);\n      if (cached) return cached;\n      // 缓存脏了（被外部删除）—— 清掉重来\n      this.ensuredChatMemoryAssets.delete(assetId);\n    }\n\n    // 2. 拿 agent，取 owner + team 用于 create + canBindAsset\n    //    先拉 agent 是为了在任何路径下都能校验 team_mismatch，同时后续 bind\n    //    要用到 owner_user_id 作 created_by。\n    const agent = await this.getAgentById(params.agent_id);\n    if (!agent) {\n      throw new MetadataError(\n        \"agent_not_found\",\n        `cannot ensure chat_memory asset: agent ${params.agent_id} not found`,\n      );\n    }\n    if (agent.team_id !== params.team_id) {\n      throw new MetadataError(\n        \"team_mismatch\",\n        `cannot ensure chat_memory asset: agent ${params.agent_id} belongs to team ` +\n        `${agent.team_id}, not ${params.team_id}`,\n      );\n    }\n\n    // 3. 拿或建 asset：先看是否已在 store（冷启动 / 其他 pod 已建），否则新建。\n    //    createAsset 遇主键冲突 = 并发 race，回读兜底。\n    let asset = await this.getAssetById(assetId);\n    if (!asset) {\n      try {\n        asset = await this.createAsset({\n          asset_id: assetId,\n          team_id: params.team_id,\n          asset_type: \"chat_memory\",\n          name: `Memory of ${agent.name}`,\n          owner_user_id: agent.owner_user_id,\n          source_type: \"auto\",","sourceCodeStart":1235,"sourceCodeEnd":1271,"githubUrl":"https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/3efcd317b84146d6a08518ac0f7ee7c8a8d200ec/MemoryCore/src/metadata/service/metadata-service.ts#L1235-L1271","documentation":"MetadataService.ensureChatMemoryAsset verifies that the agent being addressed actually belongs to the team given in the request before creating/binding its chat_memory asset. The library throws MetadataError with code \"team_mismatch\" when agent.team_id differs from params.team_id, because assets are namespaced under the team and a cross-team (team_id, agent_id) pair would create a bound asset in the wrong team. The message includes both team ids so you can see exactly which side is stale.","triggerScenarios":"Calling ensureChatMemoryAsset({ team_id, agent_id }) where the agent row exists but was created under (or moved to) a different team than the team_id passed in the params.","commonSituations":"An agent was migrated to another team after the caller cached the old team_id; a config/env file supplies a team id from one environment while the agent id comes from another; copy-pasted ids between teams; a re-keyed or recreated agent kept its id but changed team.","solutions":["Fetch the agent (getAgentById) and use agent.team_id as the team_id you pass to ensureChatMemoryAsset instead of a hard-coded value.","If the agent was moved intentionally, update the caller's stored team_id or re-create the chat_memory asset under the new team.","If the team_id is wrong, correct the request/config value; the message shows both actual and expected team ids to identify which is stale.","Clear any in-process ensuredChatMemoryAssets cache after team reassignment so future calls re-validate."],"exampleFix":"// before\nawait svc.ensureChatMemoryAsset({ team_id: \"team-a\", agent_id: agentId });\n// after\nconst agent = await svc.getAgentById(agentId);\nif (!agent) throw new Error(`agent ${agentId} missing`);\nawait svc.ensureChatMemoryAsset({ team_id: agent.team_id, agent_id: agentId });","handlingStrategy":"validation","validationCode":"const agent = await svc.getAgentById(params.agent_id);\nif (agent && agent.team_id !== params.team_id) {\n  throw new Error(`team mismatch: agent belongs to ${agent.team_id}, requested ${params.team_id}`);\n}","typeGuard":"function canEnsureChatMemory(agent: AgentEntity | null, teamId: string): agent is AgentEntity {\n  return agent !== null && agent.team_id === teamId;\n}","tryCatchPattern":"try {\n  await svc.ensureChatMemoryAsset({ team_id, agent_id });\n} catch (err) {\n  if (err instanceof MetadataError && err.code === \"team_mismatch\") {\n    const agent = await svc.getAgentById(agentId);\n    teamId = agent.team_id; // re-sync to the authoritative team\n    return svc.ensureChatMemoryAsset({ team_id: agent.team_id, agent_id });\n  }\n  throw err;\n}","preventionTips":["Always derive team_id from the agent record, never from a separate cached/config value.","Revalidate stored (team_id, agent_id) pairs after any team migration.","Invalidate the ensuredChatMemoryAssets cache when agents change teams."],"tags":["team-mismatch","agent","validation","metadata"],"backgroundTag":"resource-team-mismatch","analyzedSha":"3efcd317b84146d6a08518ac0f7ee7c8a8d200ec","analyzedAt":"2026-09-01T05:44:22.276Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}