plandex-ai/plandex · error

branch not found

Error message

branch not found

What it means

UpdateContexts looks up the plan's branch by name with GetDbBranch(planId, branchName); this error is returned when the lookup succeeds (no error) but returns a nil branch, meaning no branch with that name exists for the plan.

Source

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

	BranchName               string
	ContextsById             map[string]*Context
	SkipConflictInvalidation bool
}

func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse, error) {
	req := params.Req
	orgId := params.OrgId
	plan := params.Plan
	planId := plan.Id
	branchName := params.BranchName

	branch, err := GetDbBranch(planId, branchName)
	if err != nil {
		return nil, fmt.Errorf("error getting branch: %v", err)
	}

	if branch == nil {
		return nil, fmt.Errorf("branch not found")
	}

	totalTokens := branch.ContextTokens
	totalPlannerTokens := totalTokens

	totalMapTokens := 0

	totalBasicPlannerTokens := 0
	for _, context := range params.ContextsById {
		if context.ContextType != shared.ContextMapType && !context.AutoLoaded {
			totalBasicPlannerTokens += context.NumTokens
		}
	}

	settings, err := GetPlanSettings(plan)
	if err != nil {
		return nil, fmt.Errorf("error getting settings: %v", err)
	}

View on GitHub (pinned to e2d772072e)

Solutions

  1. Re-fetch the plan's current branches and retry with a valid branch name.
  2. Recreate the branch (or re-checkout in the client) before updating contexts.
  3. Verify the branch name spelling/case matches what GetDbBranch stores.
  4. Handle the nil-branch case in the caller by reloading plan state instead of reusing a cached name.

Example fix

// before
UpdateContexts(orgId, planId, "feature-old", req) // branch deleted
// after
branches := GetDbPlanBranches(orgId, planId)
if !contains(branches, branchName) {
    branchName = defaultBranchFor(planId) // refresh instead of failing
}
UpdateContexts(orgId, planId, branchName, req)
Defensive patterns

Strategy: validation

Validate before calling

branch, err := GetDbBranch(planId, branchName)
if err != nil { return err }
if branch == nil {
    return fmt.Errorf("branch %q not found for plan %s; refresh branches first", branchName, planId)
}

Type guard

func branchExists(planId, branchName string) bool {
    b, err := GetDbBranch(planId, branchName)
    return err == nil && b != nil
}

Try / catch

updated, err := UpdateContexts(orgId, planId, branchName, req)
if err != nil && strings.Contains(err.Error(), "branch not found") {
    branches, _ := GetDbPlanBranches(orgId, planId)
    // re-resolve a valid branch and retry once
}

Prevention

When it happens

Trigger: Calling UpdateContexts with a branchName that doesn't exist for the given planId — typically a deleted/renamed branch or a wrong branch name passed by the caller.

Common situations: The branch was deleted (e.g. after a git push/reset flow) while the client still cached its name; typo'd or case-mismatched branch name; updating contexts for a plan whose main branch was recreated under a different name.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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