plandex-ai/plandex · error

error adding convo tokens: %v

Error message

error adding convo tokens: %v

What it means

This error wraps any failure returned by AddPlanConvoMessage, which stores the per-conversation token accounting after a convo message is persisted. StoreConvoMessage has already written the message row itself, so the message content exists but the token counters are stale. The %v verb interpolates the underlying cause (SQL error, constraint violation, connection issue).

Source

Thrown at app/server/db/convo_helpers.go:134

		return "", fmt.Errorf("error marshalling convo message: %v", err)
	}

	err = os.MkdirAll(convoDir, os.ModePerm)

	if err != nil {
		return "", fmt.Errorf("error creating convo dir: %v", err)
	}

	err = os.WriteFile(filepath.Join(convoDir, message.Id+".json"), bytes, os.ModePerm)

	if err != nil {
		return "", fmt.Errorf("error writing convo message: %v", err)
	}

	err = AddPlanConvoMessage(message, branch)

	if err != nil {
		return "", fmt.Errorf("error adding convo tokens: %v", err)
	}

	var desc string
	if message.Role == openai.ChatMessageRoleUser {
		desc = "💬 User prompt"
		// TODO: add user name
	} else {
		desc = "🤖 Plandex reply"
		if message.Stopped {
			desc += " | 🛑 " + color.New(color.FgHiRed).Sprint("stopped")
		}
	}

	replyTags := message.Flags.GetReplyTags()

	var msg string
	if len(replyTags) > 0 {
		msg = fmt.Sprintf("Message #%d | %s | %s | %d 🪙", message.Num, desc, strings.Join(replyTags, " | "), message.Tokens)

View on GitHub (pinned to e2d772072e)

Solutions

  1. Inspect the wrapped %v cause in the log to identify the underlying SQL error
  2. Run database migrations (MigrationsUp) to ensure token tables/columns exist
  3. Verify the branch parameter matches an existing plan branch before storing the message
  4. Check DB connectivity and retry StoreConvoMessage; note the message row was already written so avoid duplicating it

Example fix

// before
msg, err := db.StoreConvoMessage(ctx, msg, branch, true)
// after
if _, err := db.GetBranch(orgId, planId, branch); err != nil {
    branch = "main" // fall back to default branch
}
msg, err := db.StoreConvoMessage(ctx, msg, branch, true)
Defensive patterns

Strategy: try-catch

Validate before calling

if branch == "" || msg == "" {
    return fmt.Errorf("cannot store convo message: empty branch or message")
}
if err := Conn.Ping(); err != nil {
    return fmt.Errorf("db unavailable: %v", err)
}

Try / catch

msg, err := db.StoreConvoMessage(ctx, m, branch, commit)
if err != nil {
    if strings.Contains(err.Error(), "error adding convo tokens") {
        log.Printf("token accounting failed, message may be stored: %v", err)
        // retry only the token step or alert, don't duplicate the message
    }
    return err
}

Prevention

When it happens

Trigger: Calling StoreConvoMessage (directly or via StorePartialReply/storeAssistantReply) when AddPlanConvoMessage fails: invalid branch reference, DB connection drop, schema mismatch in the plan convo tokens table, or a nil message/branch argument producing invalid SQL parameters.

Common situations: Streaming a partial reply whose branch was deleted concurrently; migrations out of date so token columns are missing; database connectivity blips mid-request; passing a branch name that doesn't exist after a git/plan branch switch.

Related errors


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