vxcontrol/pentagi · error

failed to create task in DB: %w

Error message

failed to create task in DB: %w

What it means

After generating a title, NewTaskWorker persists the task via CreateTask (DB insert). This error wraps that insert failure. The title was already generated (LLM cost incurred) but nothing is committed; the caller sees the task creation fail.

Source

Thrown at backend/pkg/controller/task.go:72

) (TaskWorker, error) {
	ctx, span := obs.Observer.NewSpan(ctx, obs.SpanKindInternal, "controller.NewTaskWorker")
	defer span.End()

	ctx = tools.PutAgentContext(ctx, database.MsgchainTypePrimaryAgent)

	title, err := flowCtx.Provider.GetTaskTitle(ctx, input)
	if err != nil {
		return nil, fmt.Errorf("failed to get task title: %w", err)
	}

	task, err := flowCtx.DB.CreateTask(ctx, database.CreateTaskParams{
		Status: database.TaskStatusCreated,
		Title:  title,
		Input:  input,
		FlowID: flowCtx.FlowID,
	})
	if err != nil {
		return nil, fmt.Errorf("failed to create task in DB: %w", err)
	}

	flowCtx.Publisher.TaskCreated(ctx, task, []database.Subtask{})

	taskCtx := &TaskContext{
		FlowContext: *flowCtx,
		TaskID:      task.ID,
		TaskTitle:   title,
		TaskInput:   input,
	}
	stc := NewSubtaskController(taskCtx)

	_, err = taskCtx.MsgLog.PutTaskMsg(
		ctx,
		database.MsglogTypeInput,
		taskCtx.TaskID,
		"", // thinking is empty because this is input
		input,

View on GitHub (pinned to ea665308ba)

Solutions

  1. Inspect the wrapped DB error — FK violation vs connection vs constraint
  2. Verify the flow exists before creating tasks against it
  3. Ensure input length fits the DB column definition
  4. Retry CreateTask once the DB issue is resolved
Defensive patterns

Strategy: try-catch

Validate before calling

if flowCtx.FlowID <= 0 { return fmt.Errorf("invalid flow id") }
if len(input) > maxInputLen { return fmt.Errorf("input too large") }

Try / catch

task, err := flowCtx.DB.CreateTask(ctx, params)
if err != nil {
    var pqErr *pq.Error
    if errors.As(err, &pqErr) && pqErr.Code == "23503" {
        return nil, fmt.Errorf("flow %d no longer exists", flowCtx.FlowID)
    }
    return nil, fmt.Errorf("failed to create task in DB: %w", err)
}

Prevention

When it happens

Trigger: DB connection loss, context cancellation between the LLM call and insert, FlowID referencing a missing flow (FK violation), or column constraint failures on title/input.

Common situations: Flow deleted concurrently while its task was being created (FK violation); Postgres pool exhaustion; oversized input exceeding the input column limit.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/ba8fdb9d234f2e5e. Report an issue: GitHub.