{"record":{"id":"a4f5c5c62d2e3c23","repo":"TencentCloud/TencentDB-Agent-Memory","slug":"metadata-systemuser-memory-userkey-sk-mem-a","errorCode":null,"errorMessage":"metadata.systemUser.memory.userKey 必须匹配 sk-mem-[A-Za-z0-9_-]{32}","messagePattern":"metadata\\.systemUser\\.memory\\.userKey 必须匹配 sk-mem-\\[A-Za-z0-9_-\\](.+?)","errorType":"validation","errorClass":"LlmResolveError","httpStatus":null,"severity":"error","filePath":"MemoryCore/src/gateway/llm-resolver.ts","lineNumber":76,"sourceCode":"  }\n  if (!instanceId || !instanceId.trim()) {\n    throw new LlmResolveError(\n      \"llm.provider=proxy 需要 instanceId，但 core 当前 instanceId 为空 —— \" +\n      \"service 模式下确保请求带 x-tdai-service-id，standalone 模式下确保 yaml 有 instanceId\",\n    );\n  }\n\n  const useSystemUserKey = llm.proxy?.useMemorySystemUserKey ?? true;\n  let effectiveApiKey = llm.apiKey;\n  if (useSystemUserKey) {\n    if (!memorySystemUser) {\n      throw new LlmResolveError(\n        \"llm.provider=proxy 且 llm.proxy.useMemorySystemUserKey=true 需要 \" +\n        \"metadata.systemUser.memory 完整配置（userId + userKey），当前缺失\",\n      );\n    }\n    if (!isValidMemorySystemUserKey(memorySystemUser.userKey)) {\n      throw new LlmResolveError(\n        \"metadata.systemUser.memory.userKey 必须匹配 sk-mem-[A-Za-z0-9_-]{32}\",\n      );\n    }\n    effectiveApiKey = memorySystemUser.userKey;\n  }\n\n  if (!effectiveApiKey) {\n    throw new LlmResolveError(\n      \"llm.provider=proxy 且 useMemorySystemUserKey=false 时必须显式配置 llm.apiKey\",\n    );\n  }\n\n  // baseUrl 拼接规则：去掉尾部斜杠，追加 /proxy/<iid>/v1\n  const cleanBase = llm.baseUrl.replace(/\\/+$/, \"\");\n  const proxyBaseUrl = `${cleanBase}/proxy/${encodeURIComponent(instanceId)}/v1`;\n\n  return {\n    ...llm,","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/3efcd317b84146d6a08518ac0f7ee7c8a8d200ec/MemoryCore/src/gateway/llm-resolver.ts#L58-L94","documentation":"When the memory system user key is used for proxy auth, isValidMemorySystemUserKey enforces the exact format sk-mem-[A-Za-z0-9_-]{32} (prefix \"sk-mem-\" plus 32 alphanumeric/underscore/hyphen chars). A userKey present but malformed throws this LlmResolveError.","triggerScenarios":"metadata.systemUser.memory.userKey set to a generic sk- key, a truncated key, one with invalid characters (spaces, dots), or wrong length (e.g. 16 or 64 chars after the prefix).","commonSituations":"Pasting a normal OpenAI-style sk- key instead of a memory key; copying the key with surrounding quotes/whitespace or truncation from a terminal; older deployments using a legacy key format before the sk-mem- scheme was introduced.","solutions":["Generate/obtain a correctly formatted key (sk-mem- + 32 chars of [A-Za-z0-9_-]) and set metadata.systemUser.memory.userKey to it.","Trim whitespace/quotes from the yaml value and re-verify length: 7 (prefix) + 32 = 39 chars total.","Check for stale legacy keys and rotate to the new sk-mem- format issued by your platform.","Alternatively set llm.proxy.useMemorySystemUserKey: false with a valid llm.apiKey to bypass the system-user key path."],"exampleFix":"// before\nuserKey: sk-abc123\n// after\nuserKey: sk-mem-AbCdEf0123456789AbCdEf0123456789  // 32 chars after sk-mem-","handlingStrategy":"validation","validationCode":"const KEY_RE = /^sk-mem-[A-Za-z0-9_-]{32}$/;\nif (!KEY_RE.test(metadata.systemUser?.memory?.userKey ?? '')) {\n  throw new Error('metadata.systemUser.memory.userKey must match sk-mem-[A-Za-z0-9_-]{32}');\n}","typeGuard":"function isValidMemorySystemUserKey(key: unknown): key is string {\n  return typeof key === 'string' && /^sk-mem-[A-Za-z0-9_-]{32}$/.test(key);\n}","tryCatchPattern":"try {\n  gateway.start();\n} catch (err) {\n  if (err instanceof LlmResolveError && err.message.includes('sk-mem-')) {\n    console.error('Regenerate the memory userKey in the sk-mem-xxxxxxxx... format (32 chars after prefix)');\n  }\n  throw err;\n}","preventionTips":["Check the key is 39 chars total (sk-mem- + 32)","Never paste regular sk- API keys into the memory userKey field","Trim whitespace/quotes when injecting keys via templating","Rotate legacy keys to the sk-mem- format during upgrades"],"tags":["configuration","api-key","validation"],"backgroundTag":"invalid-api-key-format","analyzedSha":"3efcd317b84146d6a08518ac0f7ee7c8a8d200ec","analyzedAt":"2026-09-01T05:44:22.276Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}