vxcontrol/pentagi · error

failed to restore assistant msg chain

Error message

failed to restore assistant msg chain: %w

What it means

PrepareAgentChain failed while restoring the assistant message chain via ap.fp.restoreChain, which loads (or creates) the persisted Msgchain row for the assistant agent. Without a restored chain ID the assistant cannot continue its conversation history, so the flow preparation aborts with this wrapped error.

Solutions

  1. Inspect the wrapped DB error from restoreChain
  2. Verify Postgres connectivity and that migrations in backend/migrations/sql are applied
  3. Check the msgchain/flow rows for the given flow_id (orphans or duplicates)
  4. Retry the flow creation; if persistent, check backend logs just before this error for the underlying sql error
Defensive patterns

Strategy: retry

Validate before calling

// ensure flow row exists before restoring the chain
// SELECT id FROM flows WHERE id = ?

Try / catch

id, err := ap.PrepareAgentChain(ctx)
if err != nil {
    if isTransientDBError(err) {
        time.Sleep(backoff)
        id, err = ap.PrepareAgentChain(ctx) // bounded retry
    }
    if err != nil { return err }
}

Prevention

When it happens

Trigger: restoreChain(ctx, nil, nil, OptionsTypeAssistant, MsgchainTypeAssistant, systemPrompt, "") fails: msgchain row lookup/insert fails, foreign-key issues on flow_id, DB unavailable, or context cancelled mid-query.

Common situations: Postgres outage or connection pool exhaustion; corrupted/partial flow data (flow deleted concurrently); migration drift leaving msgchain schema stale; restoring a chain for a flow whose ID no longer exists.

Related errors


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

Appendix: source

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

		"provider":     ap.fp.Type(),
		"assistant_id": ap.id,
		"flow_id":      ap.fp.ID(),
	})

	systemPrompt, err := ap.getAssistantSystemPrompt(ctx)
	if err != nil {
		logger.WithError(err).Error("failed to get assistant system prompt")
		return 0, fmt.Errorf("failed to get assistant system prompt: %w", err)
	}

	optAgentType := pconfig.OptionsTypeAssistant
	msgChainType := database.MsgchainTypeAssistant
	ap.msgChainID, _, err = ap.fp.restoreChain(
		ctx, nil, nil, optAgentType, msgChainType, systemPrompt, "",
	)
	if err != nil {
		logger.WithError(err).Error("failed to restore assistant msg chain")
		return 0, fmt.Errorf("failed to restore assistant msg chain: %w", err)
	}

	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 {

View on GitHub (pinned to ea665308ba)