{"record":{"id":"ede35c33865bb32d","repo":"plandex-ai/plandex","slug":"error-adding-plan-context-tokens-v","errorCode":null,"errorMessage":"error adding plan context tokens: %v","messagePattern":"error adding plan context tokens: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/context_helpers_update.go","lineNumber":383,"sourceCode":"\t\t\t}, nil\n\t\t}\n\t}\n\n\tupdateRes := &shared.ContextUpdateResult{\n\t\tUpdatedContexts: updatedContexts,\n\t\tTokenDiffsById:  tokenDiffsById,\n\t\tTokensDiff:      aggregateTokensDiff,\n\t\tTotalTokens:     totalTokens,\n\t\tNumFiles:        numFiles,\n\t\tNumUrls:         numUrls,\n\t\tNumTrees:        numTrees,\n\t\tNumMaps:         numMaps,\n\t\tMaxTokens:       plannerMaxTokens,\n\t}\n\n\terr = AddPlanContextTokens(planId, branchName, aggregateTokensDiff)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error adding plan context tokens: %v\", err)\n\t}\n\n\tcommitMsg := shared.SummaryForUpdateContext(shared.SummaryForUpdateContextParams{\n\t\tNumFiles:    numFiles,\n\t\tNumTrees:    numTrees,\n\t\tNumUrls:     numUrls,\n\t\tNumMaps:     numMaps,\n\t\tTokensDiff:  aggregateTokensDiff,\n\t\tTotalTokens: totalTokens,\n\t}) + \"\\n\\n\" + shared.TableForContextUpdate(updateRes)\n\treturn &shared.LoadContextResponse{\n\t\tTokensAdded: aggregateTokensDiff,\n\t\tTotalTokens: totalTokens,\n\t\tMsg:         commitMsg,\n\t}, nil\n}\n","sourceCodeStart":365,"sourceCodeEnd":400,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/context_helpers_update.go#L365-L400","documentation":"At the end of UpdateContexts, the aggregate token diff across all updated contexts is applied to the plan branch via AddPlanContextTokens, which runs 'UPDATE branches SET context_tokens = context_tokens + $1 WHERE plan_id = $2 AND name = $3'. This wrapper means that SQL update failed — almost always a database error (connection failure, missing branch row). Note the update silently succeeds (0 rows) if the branch doesn't exist; the error indicates a real DB failure.","triggerScenarios":"AddPlanContextTokens returns an error from Conn.Exec — Postgres unreachable, connection pool exhausted, query timeout, or schema mismatch on the branches table.","commonSituations":"Database restart/failover mid-request; network partition between server and Postgres; migration mismatch removing/renaming branches columns; connection leak exhausting the pool under load.","solutions":["Check the inner %v error and database connectivity; retry the update after DB recovers","Verify the branches table schema matches migrations (context_tokens, plan_id, name columns)","Check connection-pool saturation and long-running transactions blocking the update","Confirm the plan/branch still exists (though missing branches silently no-op rather than error)"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := AddPlanContextTokens(planId, branch, diff)\nif err != nil {\n    if isTransientDBError(err) { // pgconn.SafeToRetry or timeout codes\n        return retryWithBackoff(func() error { return AddPlanContextTokens(planId, branch, diff) })\n    }\n    return fmt.Errorf(\"error adding plan context tokens: %v\", err)\n}","preventionTips":["Use a connection pool with health checks and sane pool limits","Retry idempotent token-delta updates on transient DB errors","Keep branches-table migrations in sync with the binary","Alert on Postgres connection errors; token accounting fails silently on retry storms otherwise"],"tags":["database","postgres","tokens","go"],"backgroundTag":"database-update-failed","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"}