charmbracelet/crush · error

failed to create user message: %w

Error message

failed to create user message: %w

What it means

Raised when persisting the caller's prompt fails: a.messages.Create with Role User and the assembled parts (text plus attachmentParts) returns an error, and CreateAndStoreUserMessage-style code wraps it as 'failed to create user message: %w'.

Source

Thrown at internal/agent/agent.go:1522

	return map[string]string{
		"x-session-id":       hash,
		"x-session-affinity": hash,
	}
}

func (a *sessionAgent) createUserMessage(ctx context.Context, call SessionAgentCall) (message.Message, error) {
	parts := []message.ContentPart{message.TextContent{Text: call.Prompt}}
	var attachmentParts []message.ContentPart
	for _, attachment := range call.Attachments {
		attachmentParts = append(attachmentParts, message.BinaryContent{Path: attachment.FilePath, MIMEType: attachment.MimeType, Data: attachment.Content})
	}
	parts = append(parts, attachmentParts...)
	msg, err := a.messages.Create(ctx, call.SessionID, message.CreateMessageParams{
		Role:  message.User,
		Parts: parts,
	})
	if err != nil {
		return message.Message{}, fmt.Errorf("failed to create user message: %w", err)
	}
	return msg, nil
}

func (a *sessionAgent) preparePrompt(msgs []message.Message, supportsImages bool, attachments ...message.Attachment) ([]fantasy.Message, []fantasy.FilePart) {
	var history []fantasy.Message
	if !a.isSubAgent {
		history = append(history, fantasy.NewUserMessage(
			fmt.Sprintf(
				"<system_reminder>%s</system_reminder>",
				`This is a reminder that your todo list is currently empty. DO NOT mention this to the user explicitly because they are already aware.
If you are working on tasks that would benefit from a todo list please use the "todos" tool to create one.
If not, please feel free to ignore. Again do not mention this message to the user.`,
			),
		))
	}
	// Collect all tool call IDs present in assistant messages and all tool
	// result IDs present in tool messages. This lets us detect both orphaned

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Check DB health (disk space, locks, WAL) since this is a write-path failure
  2. Reduce attachment count/size and retry
  3. Inspect the wrapped error for constraint violations (e.g. FK to a missing session)
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the target session exists before creating the user message
if _, err := sessions.Get(ctx, call.SessionID); err != nil {
    return fmt.Errorf("cannot store prompt: %w", err)
}

Try / catch

msg, err := createAndStoreUserMessage(ctx, call)
if err != nil && strings.HasPrefix(err.Error(), "failed to create user message") {
    return retry(ctx, func() error { _, err := createAndStoreUserMessage(ctx, call); return err })
}

Prevention

When it happens

Trigger: messages.Create fails while inserting the user message — DB insert error, constraint violation, oversized content/attachments, or cancelled context.

Common situations: SQLite locked or disk full; attachment payload too large for the storage schema; context deadline exceeded during a slow insert with many attachments.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/914d66837dcc771e. Report an issue: GitHub.