vxcontrol/pentagi · error
failed to get primary agent msg chain %d: %w
Error message
failed to get primary agent msg chain %d: %w
What it means
PerformAgentChain wraps the error from DB().GetMsgChain(ctx, ap.msgChainID). This error means loading the assistant's persisted message chain row from PostgreSQL failed, so the LLM conversation cannot be resumed.
Source
Thrown at backend/pkg/providers/assistant.go:153
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)
}
coder, err := ap.fp.GetCoderHandler(ctx, nil, nil)
if err != nil {
logger.WithError(err).Error("failed to get coder handler")
return fmt.Errorf("failed to get coder handler: %w", err)View on GitHub (pinned to ea665308ba)
Solutions
- Call PrepareAgentChain before PerformAgentChain so msgChainID is a valid persisted row id.
- Check Postgres availability and the wrapped DB error in logs.
- Verify the msg_chains row for the logged id exists and migrations are up to date.
- Guard against msgChainID == 0 before performing the chain.
Example fix
// before
if err := ap.PerformAgentChain(ctx); err != nil { ... }
// after
id, err := ap.PrepareAgentChain(ctx)
if err != nil {
return fmt.Errorf("prepare chain: %w", err)
}
ap.SetMsgChainID(id)
if err := ap.PerformAgentChain(ctx); err != nil { ... } Defensive patterns
Strategy: validation
Validate before calling
if ap.msgChainID == 0 { return errors.New("msg chain not prepared: call PrepareAgentChain first") } Try / catch
msgChain, err := ap.fp.DB().GetMsgChain(ctx, ap.msgChainID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) { return ap.PrepareAgentChain(ctx) } // recover by re-preparing
return fmt.Errorf("failed to get primary agent msg chain %d: %w", ap.msgChainID, err)
} Prevention
- Always call PrepareAgentChain before PerformAgentChain
- Never hand-edit msg_chains rows
- Keep migrations current on upgrade
- Watch for context cancellation during shutdown
When it happens
Trigger: GetMsgChain query fails: msg_chains row for ap.msgChainID missing (id 0 or stale), DB connection failure, context canceled, schema/migration mismatch.
Common situations: SetMsgChainID was never called so msgChainID is 0; the chain row was cleaned up while the flow still ran; Postgres outage or pool exhaustion; failed migration left an old schema.
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
- failed to create flow in DB: %w
- failed to delete assistant %d: %w
- failed to rename flow %d: %w
- failed to update flow provider in DB: %w
- failed to get subtask primary msg chains for subtask %d: %w
AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01).
Data as JSON: /api/errors/694c8dbe10c5c245.
Report an issue: GitHub.