{"record":{"id":"327d5e7c9695be1d","repo":"plandex-ai/plandex","slug":"error-updating-plan-tokens-v","errorCode":null,"errorMessage":"error updating plan tokens: %v","messagePattern":"error updating plan tokens: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/server/db/plan_helpers.go","lineNumber":137,"sourceCode":"func GetPlanNamesById(planIds []string) (map[string]string, error) {\n\tvar plans []*Plan\n\terr := Conn.Select(&plans, \"SELECT id, name FROM plans WHERE id = ANY($1)\", pq.Array(planIds))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting plan names: %v\", err)\n\t}\n\n\tnamesMap := make(map[string]string)\n\tfor _, plan := range plans {\n\t\tnamesMap[plan.Id] = plan.Name\n\t}\n\n\treturn namesMap, nil\n}\n\nfunc AddPlanContextTokens(planId, branch string, addTokens int) error {\n\t_, err := Conn.Exec(\"UPDATE branches SET context_tokens = context_tokens + $1 WHERE plan_id = $2 AND name = $3\", addTokens, planId, branch)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error updating plan tokens: %v\", err)\n\t}\n\treturn nil\n}\n\nfunc AddPlanConvoMessage(msg *ConvoMessage, branch string) error {\n\terrCh := make(chan error, 2)\n\n\tgo func() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tlog.Printf(\"panic in AddPlanConvoMessage: %v\\n%s\", r, debug.Stack())\n\t\t\t\terrCh <- fmt.Errorf(\"panic in AddPlanConvoMessage: %v\\n%s\", r, debug.Stack())\n\t\t\t\truntime.Goexit() // don't allow outer function to continue and double-send to channel\n\t\t\t}\n\t\t}()\n\n\t\t_, err := Conn.Exec(\"UPDATE branches SET convo_tokens = convo_tokens + $1 WHERE plan_id = $2 AND name = $3\", msg.Tokens, msg.PlanId, branch)\n","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/plan_helpers.go#L119-L155","documentation":"AddPlanContextTokens: the UPDATE branches SET context_tokens = ... query fails for the given plan/branch, so context token accounting cannot be recorded. DB error — connection, constraint, or the row is locked/unavailable.","triggerScenarios":"Thrown at app/server/db/plan_helpers.go:137 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the wrapped DB error","Verify the branch row exists for plan_id + branch name","Retry; token accounting is idempotent-adjacent (increment) so a retry is safe after confirming state"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}