vxcontrol/pentagi · error

failed to get subtask primary msg chains for subtask %d: %w

Error message

failed to get subtask primary msg chains for subtask %d: %w

What it means

After resolving the subtask status, LoadSubtaskWorker fetches the subtask's primary message chains via GetSubtaskPrimaryMsgChains. This error wraps any database failure of that query, tagged with the subtask ID. The worker cannot be constructed without its message chain.

Source

Thrown at backend/pkg/controller/subtask.go:114

		var err error
		// if subtask is running, it means that it was not finished by previous run
		// so we need to set it to created and continue from the beginning
		subtask, err = taskCtx.DB.UpdateSubtaskStatus(ctx, database.UpdateSubtaskStatusParams{
			Status: database.SubtaskStatusCreated,
			ID:     subtask.ID,
		})
		if err != nil {
			return nil, fmt.Errorf("failed to update subtask %d status to created: %w", subtask.ID, err)
		}
	case database.SubtaskStatusCreated:
		return nil, fmt.Errorf("subtask %d has created yet: %w", subtask.ID, ErrNothingToLoad)
	default:
		return nil, fmt.Errorf("unexpected subtask status: %s", subtask.Status)
	}

	msgChains, err := taskCtx.DB.GetSubtaskPrimaryMsgChains(ctx, database.Int64ToNullInt64(&subtask.ID))
	if err != nil {
		return nil, fmt.Errorf("failed to get subtask primary msg chains for subtask %d: %w", subtask.ID, err)
	}

	if len(msgChains) == 0 {
		return nil, fmt.Errorf("subtask %d has no msg chains: %w", subtask.ID, ErrNothingToLoad)
	}

	return &subtaskWorker{
		mx: &sync.RWMutex{},
		subtaskCtx: &SubtaskContext{
			MsgChainID:         msgChains[0].ID,
			SubtaskID:          subtask.ID,
			SubtaskTitle:       subtask.Title,
			SubtaskDescription: subtask.Description,
			TaskContext:        *taskCtx,
		},
		updater:   updater,
		completed: completed,
		waiting:   waiting,

View on GitHub (pinned to ea665308ba)

Solutions

  1. Check the wrapped DB error for connectivity vs schema causes.
  2. Verify pgvector extension and msg-chain table migrations are installed (goose up).
  3. Retry the load with backoff if the DB was transiently unavailable during startup recovery.
  4. Inspect connection-pool settings if failures correlate with high concurrency.

Example fix

// before
chains, err := db.GetSubtaskPrimaryMsgChains(ctx, id)
if err != nil {
    panic(err)
}
// after
chains, err := db.GetSubtaskPrimaryMsgChains(ctx, id)
if err != nil {
    return nil, fmt.Errorf("load msg chains for subtask %d (check pgvector/migrations): %w", id, err)
}
Defensive patterns

Strategy: retry

Validate before calling

// ensure pgvector and required tables exist before loading chains
var ext bool
err := db.QueryRowContext(ctx,
    `SELECT EXISTS(SELECT 1 FROM pg_extension WHERE extname='vector')`).Scan(&ext)
if err != nil || !ext {
    return fmt.Errorf("pgvector extension missing")
}

Type guard

func IsMsgChainQueryError(err error) bool {
    return strings.Contains(err.Error(), "primary msg chains")
}

Try / catch

worker, err := LoadSubtaskWorker(ctx, taskCtx, subtask)
if err != nil {
    if isTransientDBError(err) {
        return retryWithBackoff(ctx, func() error {
            worker, err = LoadSubtaskWorker(ctx, taskCtx, subtask)
            return err
        })
    }
    return fmt.Errorf("recovery failed for subtask %d: %w", subtask.ID, err)
}

Prevention

When it happens

Trigger: LoadSubtaskWorker proceeds past the status switch and GetSubtaskPrimaryMsgChains fails — DB unreachable, query timeout, pgvector extension issues, or schema mismatch.

Common situations: Postgres restart in progress during recovery; missing pgvector extension after restoring a dump; migrations not applied so msg_chain tables/columns differ; connection-pool exhaustion under many concurrent loads.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


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