{"record":{"id":"c04e8385fd776289","repo":"plandex-ai/plandex","slug":"too-many-contexts-to-update-found-d-limit-is-d","errorCode":null,"errorMessage":"too many contexts to update (found %d, limit is %d)","messagePattern":"too many contexts to update \\(found (.+?), limit is (.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/context_helpers_update.go","lineNumber":123,"sourceCode":"\t}\n\n\tfor id, params := range *req {\n\t\tsize := int64(len(params.Body))\n\n\t\tif size > shared.MaxContextBodySize {\n\t\t\treturn nil, fmt.Errorf(\"context body is too large: %d\", size)\n\t\t}\n\n\t\tif context, ok := contextsById[id]; ok {\n\t\t\ttotalBodySize += size - context.BodySize\n\t\t} else {\n\t\t\ttotalContextCount++\n\t\t\ttotalBodySize += size\n\t\t}\n\t}\n\n\tif totalContextCount > shared.MaxContextCount {\n\t\treturn nil, fmt.Errorf(\"too many contexts to update (found %d, limit is %d)\", totalContextCount, shared.MaxContextCount)\n\t}\n\n\tif totalBodySize > shared.MaxContextBodySize {\n\t\treturn nil, fmt.Errorf(\"total context body size exceeds limit (size %.2f MB, limit %d MB)\", float64(totalBodySize)/1024/1024, int(shared.MaxContextBodySize)/1024/1024)\n\t}\n\n\tvar updatedContexts []*shared.Context\n\n\tnumFiles := 0\n\tnumUrls := 0\n\tnumTrees := 0\n\tnumMaps := 0\n\n\tvar mu sync.Mutex\n\terrCh := make(chan error, len(*req))\n\n\tfor id, params := range *req {\n\t\tgo func(id string, params *shared.UpdateContextParams) {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/context_helpers_update.go#L105-L141","documentation":"Server-side limit check in UpdateContexts: after summing new/updated context bodies, totalContextCount exceeds shared.MaxContextCount. Guards the plan's context count per request; a client exceeding it is rejected before any DB writes.","triggerScenarios":"Calling UpdateContexts with a request map containing more than 1000 context IDs that are NOT already in contextsById (i.e., more than 1000 brand-new contexts being created in one update call).","commonSituations":"Bulk-adding an entire large repo's files as new contexts in a single update; a sync loop re-adding every file each run because IDs don't match existing contexts; batch tooling that ignores the 1000-context limit.","solutions":["Split the update into batches of <=1000 new contexts per call.","Reuse existing context IDs (update instead of create) so entries count as size deltas, not new contexts.","Filter the request to the most relevant files before updating.","Check for client bugs that generate fresh IDs for unchanged contexts."],"exampleFix":"// before\nUpdateContexts(orgId, planId, branch, reqForAll5000Files)\n// after\nfor _, batch := range chunkMap(req, shared.MaxContextCount) {\n    UpdateContexts(orgId, planId, branch, batch)\n}","handlingStrategy":"validation","validationCode":"newCount := 0\nfor id := range req {\n    if _, ok := existingById[id]; !ok { newCount++ }\n}\nif newCount > shared.MaxContextCount {\n    return fmt.Errorf(\"%d new contexts exceeds limit %d; batch the update\", newCount, shared.MaxContextCount)\n}","typeGuard":"func withinContextCountLimit(req map[string]*shared.UpdateContextParams, existingById map[string]*shared.Context) bool {\n    n := 0\n    for id := range req { if _, ok := existingById[id]; !ok { n++ } }\n    return n <= shared.MaxContextCount\n}","tryCatchPattern":"_, err := UpdateContexts(orgId, planId, branch, &req)\nif err != nil && strings.Contains(err.Error(), \"too many contexts to update\") {\n    // chunk req into batches of <= MaxContextCount and retry\n}","preventionTips":["Batch new-context updates to <=1000 per call","Reuse existing context IDs so updates count as deltas, not new contexts","Fix client loops that mint new IDs for unchanged contexts"],"tags":["go","context-count","limit-exceeded","update"],"backgroundTag":"payload-size-limit-exceeded","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}