plandex-ai/plandex · error

error adding plan context tokens: %v

Error message

error adding plan context tokens: %v

What it means

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.

Source

Thrown at app/server/db/context_helpers_update.go:383

			}, nil
		}
	}

	updateRes := &shared.ContextUpdateResult{
		UpdatedContexts: updatedContexts,
		TokenDiffsById:  tokenDiffsById,
		TokensDiff:      aggregateTokensDiff,
		TotalTokens:     totalTokens,
		NumFiles:        numFiles,
		NumUrls:         numUrls,
		NumTrees:        numTrees,
		NumMaps:         numMaps,
		MaxTokens:       plannerMaxTokens,
	}

	err = AddPlanContextTokens(planId, branchName, aggregateTokensDiff)
	if err != nil {
		return nil, fmt.Errorf("error adding plan context tokens: %v", err)
	}

	commitMsg := shared.SummaryForUpdateContext(shared.SummaryForUpdateContextParams{
		NumFiles:    numFiles,
		NumTrees:    numTrees,
		NumUrls:     numUrls,
		NumMaps:     numMaps,
		TokensDiff:  aggregateTokensDiff,
		TotalTokens: totalTokens,
	}) + "\n\n" + shared.TableForContextUpdate(updateRes)
	return &shared.LoadContextResponse{
		TokensAdded: aggregateTokensDiff,
		TotalTokens: totalTokens,
		Msg:         commitMsg,
	}, nil
}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check the inner %v error and database connectivity; retry the update after DB recovers
  2. Verify the branches table schema matches migrations (context_tokens, plan_id, name columns)
  3. Check connection-pool saturation and long-running transactions blocking the update
  4. Confirm the plan/branch still exists (though missing branches silently no-op rather than error)
Defensive patterns

Strategy: retry

Try / catch

err := AddPlanContextTokens(planId, branch, diff)
if err != nil {
    if isTransientDBError(err) { // pgconn.SafeToRetry or timeout codes
        return retryWithBackoff(func() error { return AddPlanContextTokens(planId, branch, diff) })
    }
    return fmt.Errorf("error adding plan context tokens: %v", err)
}

Prevention

When it happens

Trigger: AddPlanContextTokens returns an error from Conn.Exec — Postgres unreachable, connection pool exhausted, query timeout, or schema mismatch on the branches table.

Common situations: Database restart/failover mid-request; network partition between server and Postgres; migration mismatch removing/renaming branches columns; connection leak exhausting the pool under load.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/ede35c33865bb32d. Report an issue: GitHub.