{"record":{"id":"b5303e78e6585c11","repo":"vxcontrol/pentagi","slug":"failed-to-unmarshal-primary-agent-msg-chain-d-w","errorCode":null,"errorMessage":"failed to unmarshal primary agent msg chain %d: %w","messagePattern":"failed to unmarshal primary agent msg chain (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/providers/assistant.go","lineNumber":159,"sourceCode":"\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)\n\t}\n\n\tinstaller, err := ap.fp.GetInstallerHandler(ctx, nil, nil)\n\tif err != nil {\n\t\tlogger.WithError(err).Error(\"failed to get installer handler\")\n\t\treturn fmt.Errorf(\"failed to get installer handler: %w\", err)","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/providers/assistant.go#L141-L177","documentation":"PerformAgentChain wraps a json.Unmarshal failure when decoding msgChain.Chain (a []byte JSON column) into []llms.MessageContent. The stored chain JSON does not match the langchaingo llms.MessageContent schema expected by this build.","triggerScenarios":"Chain column contains corrupt/truncated JSON, was written by an older PentAGI version whose llms.MessageContent shape differs, or manual DB editing left invalid JSON.","commonSituations":"Upgrading PentAGI/langchaingo after the chain was serialized with a different struct layout; crash mid-write left partial JSON; operator edited the row by hand.","solutions":["Inspect the msg_chains.Chain value for the logged id; fix or delete the corrupt row so a fresh chain is prepared.","Check for a langchaingo/vxcontrol version bump in go.mod; roll back or migrate stored chains to the new schema.","Re-run the flow: PrepareAgentChain will create a new valid chain.","Add a schema-version column or migration for stored chains before upgrading."],"exampleFix":"// before\nvar chain []llms.MessageContent\nif err := json.Unmarshal(msgChain.Chain, &chain); err != nil {\n    return fmt.Errorf(\"failed to unmarshal primary agent msg chain %d: %w\", ap.msgChainID, err)\n}\n// after\nvar chain []llms.MessageContent\nif err := json.Unmarshal(msgChain.Chain, &chain); err != nil {\n    logger.WithError(err).Warn(\"corrupt chain, recreating\")\n    return ap.ResetAndPrepareChain(ctx) // rebuild chain instead of failing hard\n}","handlingStrategy":"fallback","validationCode":"if !json.Valid(msgChain.Chain) { return errors.New(\"stored chain is not valid JSON — recreate the chain\") }","typeGuard":"func isValidChain(raw []byte) bool {\n    var c []llms.MessageContent\n    return json.Unmarshal(raw, &c) == nil\n}","tryCatchPattern":"var chain []llms.MessageContent\nif err := json.Unmarshal(msgChain.Chain, &chain); err != nil {\n    logger.WithError(err).Warn(\"corrupt stored chain, rebuilding\")\n    return ap.PrepareAgentChain(ctx)\n}","preventionTips":["Pin the langchaingo/vxcontrol version; migrate chains on upgrade","Add a schema version to serialized chains","Never edit chain JSON manually","Validate JSON round-trip after writes"],"tags":["json","unmarshal","compatibility"],"backgroundTag":"json-unmarshal-error","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}