{"record":{"id":"694c8dbe10c5c245","repo":"vxcontrol/pentagi","slug":"failed-to-get-primary-agent-msg-chain-d-w","errorCode":null,"errorMessage":"failed to get primary agent msg chain %d: %w","messagePattern":"failed to get primary agent msg chain (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/providers/assistant.go","lineNumber":153,"sourceCode":"\tdefer span.End()\n\n\tlogger := logrus.WithContext(ctx).WithFields(logrus.Fields{\n\t\t\"provider\":     ap.fp.Type(),\n\t\t\"assistant_id\": ap.id,\n\t\t\"flow_id\":      ap.fp.ID(),\n\t\t\"msg_chain_id\": ap.msgChainID,\n\t})\n\n\tuseAgents, err := ap.getAssistantUseAgents(ctx)\n\tif err != nil {\n\t\tlogger.WithError(err).Error(\"failed to get assistant use agents\")\n\t\treturn fmt.Errorf(\"failed to get assistant use agents: %w\", err)\n\t}\n\n\tmsgChain, err := ap.fp.DB().GetMsgChain(ctx, ap.msgChainID)\n\tif err != nil {\n\t\tlogger.WithError(err).Error(\"failed to get primary agent msg chain\")\n\t\treturn fmt.Errorf(\"failed to get primary agent msg chain %d: %w\", ap.msgChainID, err)\n\t}\n\n\tvar chain []llms.MessageContent\n\tif err := json.Unmarshal(msgChain.Chain, &chain); err != nil {\n\t\tlogger.WithError(err).Error(\"failed to unmarshal primary agent msg chain\")\n\t\treturn fmt.Errorf(\"failed to unmarshal primary agent msg chain %d: %w\", ap.msgChainID, err)\n\t}\n\n\tadviser, err := ap.fp.GetAskAdviceHandler(ctx, nil, nil)\n\tif err != nil {\n\t\tlogger.WithError(err).Error(\"failed to get ask advice handler\")\n\t\treturn fmt.Errorf(\"failed to get ask advice handler: %w\", err)\n\t}\n\n\tcoder, err := ap.fp.GetCoderHandler(ctx, nil, nil)\n\tif err != nil {\n\t\tlogger.WithError(err).Error(\"failed to get coder handler\")\n\t\treturn fmt.Errorf(\"failed to get coder handler: %w\", err)","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/providers/assistant.go#L135-L171","documentation":"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.","triggerScenarios":"GetMsgChain query fails: msg_chains row for ap.msgChainID missing (id 0 or stale), DB connection failure, context canceled, schema/migration mismatch.","commonSituations":"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.","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."],"exampleFix":"// before\nif err := ap.PerformAgentChain(ctx); err != nil { ... }\n// after\nid, err := ap.PrepareAgentChain(ctx)\nif err != nil {\n    return fmt.Errorf(\"prepare chain: %w\", err)\n}\nap.SetMsgChainID(id)\nif err := ap.PerformAgentChain(ctx); err != nil { ... }","handlingStrategy":"validation","validationCode":"if ap.msgChainID == 0 { return errors.New(\"msg chain not prepared: call PrepareAgentChain first\") }","typeGuard":null,"tryCatchPattern":"msgChain, err := ap.fp.DB().GetMsgChain(ctx, ap.msgChainID)\nif err != nil {\n    if errors.Is(err, sql.ErrNoRows) { return ap.PrepareAgentChain(ctx) } // recover by re-preparing\n    return fmt.Errorf(\"failed to get primary agent msg chain %d: %w\", ap.msgChainID, err)\n}","preventionTips":["Always call PrepareAgentChain before PerformAgentChain","Never hand-edit msg_chains rows","Keep migrations current on upgrade","Watch for context cancellation during shutdown"],"tags":["database","postgres","msg-chain"],"backgroundTag":"database-query-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}