{"record":{"id":"e1d383f48793714d","repo":"mastra-ai/mastra","slug":"rule-metadata-is-too-large","errorCode":null,"errorMessage":"Rule metadata is too large.","messagePattern":"Rule metadata is too large\\.","errorType":"validation","errorClass":"FactoryRuleValidationError","httpStatus":null,"severity":"error","filePath":"mastracode/factory/src/rules/validation.ts","lineNumber":137,"sourceCode":"    const sanitized: Record<string, FactoryRuleJsonValue> = {};\n    for (const [key, entry] of entries) {\n      const normalizedKey = boundedString(key, 'Rule metadata key', 128, IDENTIFIER_RE);\n      sanitized[normalizedKey] = SENSITIVE_KEY_RE.test(normalizedKey)\n        ? '[REDACTED]'\n        : normalizeFactoryRuleJsonValue(entry, depth + 1, seen);\n    }\n    return sanitized;\n  } finally {\n    seen.delete(value as object);\n  }\n}\n\nfunction sanitizeMetadata(value: unknown): Record<string, FactoryRuleJsonValue> | undefined {\n  if (value === undefined) return undefined;\n  const sanitized = normalizeFactoryRuleJsonValue(value);\n  if (!isPlainObject(sanitized)) throw new FactoryRuleValidationError('Rule metadata must be an object.');\n  if (JSON.stringify(sanitized).length > MAX_METADATA_JSON_LENGTH) {\n    throw new FactoryRuleValidationError('Rule metadata is too large.');\n  }\n  return sanitized;\n}\n\nfunction validateBoardRules(rules: unknown, label: string): asserts rules is FactoryBoardRules {\n  if (!isPlainObject(rules)) throw new FactoryRuleValidationError(`${label} must be an object.`);\n  for (const [stage, sources] of Object.entries(rules)) {\n    enumValue(stage, FACTORY_RULE_STAGES, `${label} stage`);\n    if (!isPlainObject(sources)) throw new FactoryRuleValidationError(`${label}.${stage} must be an object.`);\n    for (const [source, leaf] of Object.entries(sources)) {\n      enumValue(source, FACTORY_RULE_SOURCES, `${label}.${stage} source`);\n      if (!isPlainObject(leaf)) throw new FactoryRuleValidationError(`${label}.${stage}.${source} must be an object.`);\n      assertExactKeys(leaf, ['onEnter', 'onExit'], `${label}.${stage}.${source}`);\n      for (const handler of Object.values(leaf)) {\n        if (handler !== undefined && typeof handler !== 'function') {\n          throw new FactoryRuleValidationError(`${label}.${stage}.${source} handlers must be functions.`);\n        }\n      }","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory/src/rules/validation.ts#L119-L155","documentation":"sanitizeMetadata validates the optional metadata payload of an upsertLinkedWorkItem rule decision. After normalizing the value to JSON-safe form, it serializes it and rejects it if the JSON exceeds MAX_METADATA_JSON_LENGTH (16,384 characters). The library caps metadata size to keep persisted decisions bounded and storage-friendly.","triggerScenarios":"Passing a metadata object longer than 16,384 characters when serialized to JSON in the metadata field of a {type:'upsertLinkedWorkItem', ...} decision, which reaches sanitizeMetadata via validateFactoryRuleDecision (and its public wrapper metadata).","commonSituations":"Embedding large API response payloads, logs, or long descriptions into work-item metadata; accumulating many fields per item; copying whole issue bodies into metadata instead of a URL or excerpt.","solutions":["Trim the metadata: keep only fields you actually need on the work item (IDs, labels, short status).","Move large content out-of-band: store a URL or external reference instead of inline payload.","Pre-check size with JSON.stringify(metadata).length <= 16384 before constructing the decision."],"exampleFix":"// before\nmetadata: { fullBody: issue.body, comments: issue.comments, logs: runLogs }\n// after\nmetadata: { issueId: issue.id, url: issue.url, labelCount: issue.labels.length }","handlingStrategy":"validation","validationCode":"function checkMetadataSize(metadata) {\n  if (metadata === undefined) return;\n  const json = JSON.stringify(metadata);\n  if (json.length > 16384) throw new Error(`Rule metadata JSON is ${json.length} chars; max is 16384`);\n}\ncheckMetadataSize(decision.metadata);","typeGuard":null,"tryCatchPattern":"try {\n  commit(decision);\n} catch (e) {\n  if (e.name === 'FactoryRuleValidationError' && /metadata is too large/.test(e.message)) {\n    decision.metadata = compactMetadata(decision.metadata); // strip bulky fields and retry once\n    commit(decision);\n  } else throw e;\n}","preventionTips":["Keep work-item metadata to small identifiers/links; store large payloads externally.","Add a unit test asserting serialized metadata stays under 16KB for your rules.","Pre-sanitize with normalizeFactoryRuleJsonValue to catch cycles/depth issues before size checks."],"tags":["validation","metadata","size-limit","factory-rules"],"backgroundTag":"payload-size-limit-exceeded","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}