vxcontrol/pentagi · error

failed to get assistant use agents: %w

Error message

failed to get assistant use agents: %w

What it means

PerformAgentChain wraps the error from ap.getAssistantUseAgents(ctx), which calls DB().GetAssistantUseAgents(ctx, ap.id) to read the assistant's `use_agents` flag from PostgreSQL. This error means the database lookup for that flag failed, aborting the assistant agent-chain run before any LLM call.

Source

Thrown at backend/pkg/providers/assistant.go:147

	return ap.msgChainID, nil
}

func (ap *assistantProvider) PerformAgentChain(ctx context.Context) error {
	ctx, span := obs.Observer.NewSpan(ctx, obs.SpanKindInternal, "providers.assistantProvider.PerformAgentChain")
	defer span.End()

	logger := logrus.WithContext(ctx).WithFields(logrus.Fields{
		"provider":     ap.fp.Type(),
		"assistant_id": ap.id,
		"flow_id":      ap.fp.ID(),
		"msg_chain_id": ap.msgChainID,
	})

	useAgents, err := ap.getAssistantUseAgents(ctx)
	if err != nil {
		logger.WithError(err).Error("failed to get assistant use agents")
		return fmt.Errorf("failed to get assistant use agents: %w", err)
	}

	msgChain, err := ap.fp.DB().GetMsgChain(ctx, ap.msgChainID)
	if err != nil {
		logger.WithError(err).Error("failed to get primary agent msg chain")
		return fmt.Errorf("failed to get primary agent msg chain %d: %w", ap.msgChainID, err)
	}

	var chain []llms.MessageContent
	if err := json.Unmarshal(msgChain.Chain, &chain); err != nil {
		logger.WithError(err).Error("failed to unmarshal primary agent msg chain")
		return fmt.Errorf("failed to unmarshal primary agent msg chain %d: %w", ap.msgChainID, err)
	}

	adviser, err := ap.fp.GetAskAdviceHandler(ctx, nil, nil)
	if err != nil {
		logger.WithError(err).Error("failed to get ask advice handler")
		return fmt.Errorf("failed to get ask advice handler: %w", err)

View on GitHub (pinned to ea665308ba)

Solutions

  1. Check Postgres connectivity and that the backend can reach the DB (env DSN, docker network).
  2. Verify the assistant row with ap.id still exists in the assistants table; re-create the flow if it was deleted.
  3. Check server logs for the wrapped sql/GORM error to distinguish connection vs no-rows vs context-cancel.
  4. Increase connection pool limits or retry transient connection errors.

Example fix

// before
useAgents, err := ap.getAssistantUseAgents(ctx)
if err != nil {
    return fmt.Errorf("failed to get assistant use agents: %w", err)
}
// after
useAgents, err := ap.getAssistantUseAgents(ctx)
if err != nil {
    if errors.Is(err, context.Canceled) {
        return err // do not wrap shutdown cancels
    }
    logger.WithError(err).Error("failed to get assistant use agents")
    return fmt.Errorf("failed to get assistant use agents: %w", err)
}
Defensive patterns

Strategy: retry

Validate before calling

var exists bool
err := db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM assistants WHERE id=$1)`, assistantID).Scan(&exists)
if err != nil || !exists { return fmt.Errorf("assistant %d unavailable: %w", assistantID, err) }

Type guard

func dbReady(err error) bool { return err == nil || errors.Is(err, context.Canceled) }

Try / catch

useAgents, err := ap.getAssistantUseAgents(ctx)
if err != nil {
    if isTransientDBError(err) { return retryWithBackoff(ctx) }
    return fmt.Errorf("failed to get assistant use agents: %w", err)
}

Prevention

When it happens

Trigger: Database query in GetAssistantUseAgents errors: Postgres connection down/reset, assistant row with ap.id deleted mid-run, permission/schema issue, context canceled during shutdown.

Common situations: Postgres container restarting or unreachable; flow running while the assistant record was removed; connection-pool exhaustion under many concurrent flows; ctx canceled by graceful shutdown of the server.

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/85c7f84cd6836665. Report an issue: GitHub.