TencentCloud/TencentDB-Agent-Memory · error · MetadataError

team_mismatch

team_mismatch

Error message

cannot ensure chat_memory asset: agent ${params.agent_id} belongs to team ${agent.team_id}, not ${params.team_id}

What it means

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.

Source

Thrown at MemoryCore/src/metadata/service/metadata-service.ts:1253

    if (this.ensuredChatMemoryAssets.has(assetId)) {
      const cached = await this.getAssetById(assetId);
      if (cached) return cached;
      // 缓存脏了(被外部删除)—— 清掉重来
      this.ensuredChatMemoryAssets.delete(assetId);
    }

    // 2. 拿 agent,取 owner + team 用于 create + canBindAsset
    //    先拉 agent 是为了在任何路径下都能校验 team_mismatch,同时后续 bind
    //    要用到 owner_user_id 作 created_by。
    const agent = await this.getAgentById(params.agent_id);
    if (!agent) {
      throw new MetadataError(
        "agent_not_found",
        `cannot ensure chat_memory asset: agent ${params.agent_id} not found`,
      );
    }
    if (agent.team_id !== params.team_id) {
      throw new MetadataError(
        "team_mismatch",
        `cannot ensure chat_memory asset: agent ${params.agent_id} belongs to team ` +
        `${agent.team_id}, not ${params.team_id}`,
      );
    }

    // 3. 拿或建 asset:先看是否已在 store(冷启动 / 其他 pod 已建),否则新建。
    //    createAsset 遇主键冲突 = 并发 race,回读兜底。
    let asset = await this.getAssetById(assetId);
    if (!asset) {
      try {
        asset = await this.createAsset({
          asset_id: assetId,
          team_id: params.team_id,
          asset_type: "chat_memory",
          name: `Memory of ${agent.name}`,
          owner_user_id: agent.owner_user_id,
          source_type: "auto",

View on GitHub (pinned to 3efcd317b8)

Solutions

  1. Fetch the agent (getAgentById) and use agent.team_id as the team_id you pass to ensureChatMemoryAsset instead of a hard-coded value.
  2. If the agent was moved intentionally, update the caller's stored team_id or re-create the chat_memory asset under the new team.
  3. 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.
  4. Clear any in-process ensuredChatMemoryAssets cache after team reassignment so future calls re-validate.

Example fix

// before
await svc.ensureChatMemoryAsset({ team_id: "team-a", agent_id: agentId });
// after
const agent = await svc.getAgentById(agentId);
if (!agent) throw new Error(`agent ${agentId} missing`);
await svc.ensureChatMemoryAsset({ team_id: agent.team_id, agent_id: agentId });
Defensive patterns

Strategy: validation

Validate before calling

const agent = await svc.getAgentById(params.agent_id);
if (agent && agent.team_id !== params.team_id) {
  throw new Error(`team mismatch: agent belongs to ${agent.team_id}, requested ${params.team_id}`);
}

Type guard

function canEnsureChatMemory(agent: AgentEntity | null, teamId: string): agent is AgentEntity {
  return agent !== null && agent.team_id === teamId;
}

Try / catch

try {
  await svc.ensureChatMemoryAsset({ team_id, agent_id });
} catch (err) {
  if (err instanceof MetadataError && err.code === "team_mismatch") {
    const agent = await svc.getAgentById(agentId);
    teamId = agent.team_id; // re-sync to the authoritative team
    return svc.ensureChatMemoryAsset({ team_id: agent.team_id, agent_id });
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TencentCloud/TencentDB-Agent-Memory@3efcd317b8 (2026-09-01). Data as JSON: /api/errors/d5bac277ecec7091. Report an issue: GitHub.