{"record":{"id":"115568af233fdbc0","repo":"JuliusBrussee/caveman","slug":"cacheengine-invalid-partition-key","errorCode":null,"errorMessage":"cacheengine: invalid partition key","messagePattern":"cacheengine: invalid partition key","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/engine.go","lineNumber":276,"sourceCode":"\t\tProfileID:      profile.ID,\n\t\tMode:           profile.Mode,\n\t\tAttribution:    profile.Attribution,\n\t\tPrefixSHA256:   result.PrefixSHA256,\n\t\tEconomicsBasis: economicsBasis,\n\t\tKeyShardCount:  1,\n\t\tWarnings:       warnings,\n\t}, nil\n}\n\nfunc validatePlanRequest(request PlanRequest) error {\n\tif !validIdentity(request.Scope, 4096, false) {\n\t\treturn errors.New(\"cacheengine: invalid scope\")\n\t}\n\tif !validIdentity(request.Epoch, 4096, false) {\n\t\treturn errors.New(\"cacheengine: invalid epoch\")\n\t}\n\tif !validIdentity(request.PartitionKey, 4096, true) {\n\t\treturn errors.New(\"cacheengine: invalid partition key\")\n\t}\n\tif request.ExpectedCalls < 0 || request.ExpectedRequestsPerMinute < 0 {\n\t\treturn errors.New(\"cacheengine: negative traffic expectation\")\n\t}\n\tprofile := normalizedProfile(request.Profile)\n\tif !validIdentity(profile.ID, 256, false) || !validIdentity(profile.Provider, 64, true) || !validIdentity(profile.OptimizerID, 256, true) {\n\t\treturn errors.New(\"cacheengine: invalid profile identity\")\n\t}\n\tif profile.Mode != ModeUnsupported && profile.Mode != ModeImplicit && profile.Mode != ModeAffinity && profile.Mode != ModeExplicit {\n\t\treturn fmt.Errorf(\"cacheengine: unknown mode %q\", profile.Mode)\n\t}\n\tif profile.Mode != ModeUnsupported {\n\t\tif profile.MaxBreakpoints <= 0 || profile.MinPrefixTokens < 0 || profile.MaxRPMPerKey < 0 || profile.TTL < 0 {\n\t\t\treturn errors.New(\"cacheengine: invalid cache thresholds\")\n\t\t}\n\t\tswitch profile.Attribution {\n\t\tcase AttributionNone, AttributionOrganic, AttributionAffinity, AttributionCausal:\n\t\tdefault:","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/cacheengine/engine.go#L258-L294","documentation":"Thrown by validatePlanRequest when request.PartitionKey fails validIdentity(key, 4096, true): it must be at most 4096 bytes and contain only characters accepted by the identity validator (empty is allowed because the flag is 'optional'). The partition key shards cache entries, so a malformed key would corrupt shard routing.","triggerScenarios":"Calling the engine's Plan entry point with a PartitionKey longer than 4096 bytes, containing characters outside the accepted identity charset, or otherwise failing validIdentity (e.g. control characters, invalid UTF-8) while Scope and Epoch already passed.","commonSituations":"Building the partition key by concatenating unbounded user data (tenant + session + prompt hash), passing a full prompt or URL as the key, or an encoding bug that embeds raw bytes/NULs into the key.","solutions":["Shrink or hash the partition key (e.g. hex(sha256(rawKey))) so it stays under 4096 bytes","Inspect PartitionKey for control characters or invalid UTF-8 before the call and sanitize it","If the key is derived from user input, truncate or map it through a fixed-size identifier"],"exampleFix":"// before\nreq := cacheengine.PlanRequest{Scope: scope, Epoch: epoch, PartitionKey: tenantID + \":\" + rawUserPrompt}\n\n// after\nsum := sha256.Sum256([]byte(tenantID + \":\" + rawUserPrompt))\nreq := cacheengine.PlanRequest{Scope: scope, Epoch: epoch, PartitionKey: tenantID + \":\" + hex.EncodeToString(sum[:])}","handlingStrategy":"validation","validationCode":"func validPartitionKey(k string) bool {\n    return len(k) <= 4096 // plus the identity charset used for PartitionKey (optional -> empty ok)\n}","typeGuard":"func isPartitionKeySafe(k string) bool { return len(k) <= 4096 && utf8.ValidString(k) }","tryCatchPattern":"if err := engine.Plan(req); err != nil {\n    if strings.Contains(err.Error(), \"invalid partition key\") { /* rehash/shrink key, retry once */ }\n    return err\n}","preventionTips":["Hash or truncate key material derived from unbounded user input","Never paste whole prompts or URLs into the partition key"],"tags":["cacheengine","validation","partitioning","go"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}