{"record":{"id":"d1c637b1a21502d2","repo":"vxcontrol/pentagi","slug":"unexpected-error-flow-d-not-found","errorCode":null,"errorMessage":"unexpected error: flow %d not found","messagePattern":"unexpected error: flow (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/controller/flows.go","lineNumber":304,"sourceCode":"\t\tcase database.FlowStatusCreated:\n\t\t\treturn nil, fmt.Errorf(\"flow %d is not completed\", flowID)\n\t\tcase database.FlowStatusFinished, database.FlowStatusFailed:\n\t\t\tif err := loadFlow(); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\tcase database.FlowStatusRunning, database.FlowStatusWaiting:\n\t\t\tbreak\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"flow %d is in unknown status: %s\", flowID, status)\n\t\t}\n\t} else {\n\t\tif err := loadFlow(); err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tif fw == nil { // just double check, this should never happen\n\t\treturn nil, fmt.Errorf(\"unexpected error: flow %d not found\", flowID)\n\t}\n\n\taw, err := NewAssistantWorker(ctx, newAssistantWorkerCtx{\n\t\tuserID:        userID,\n\t\tflowID:        flowID,\n\t\tinput:         input,\n\t\tprvname:       prvname,\n\t\tprvtype:       prvtype,\n\t\tuseAgents:     useAgents,\n\t\tfunctions:     functions,\n\t\tresources:     resources,\n\t\tfw:            fw,\n\t\tflowWorkerCtx: flowWorkerCtx,\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create assistant: %w\", err)\n\t}\n","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/controller/flows.go#L286-L322","documentation":"A defensive double-check in CreateAssistant: after all branches (newFlow/loadFlow/map hit), fw must be non-nil. Returning this error means an internal invariant was violated — the control flow produced no worker even though no error path was taken. The code itself notes this should never happen.","triggerScenarios":"Only via an internal bug: a branch that neither sets fw nor returns an error (e.g. future code changes adding a status case that falls through, or fc.flows returning a nil-valued entry for an existing key).","commonSituations":"Practically never in normal operation; may appear after custom modifications to CreateAssistant or if the flows map was populated with nil workers by other code paths.","solutions":["Treat as an internal bug: report it with backend logs and the flowID.","Audit recent modifications to CreateAssistant for a branch that leaves fw unset.","Ensure nothing inserts nil FlowWorker values into fc.flows.","Restart the backend to clear any corrupt in-memory map state."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := ctl.CreateAssistant(...); err != nil {\n    if strings.Contains(err.Error(), \"unexpected error: flow\") {\n        log.Error(\"internal invariant violated; report with logs\")\n    }\n    return err\n}","preventionTips":["Don't modify CreateAssistant control flow without preserving the fw-nil invariant.","Never insert nil FlowWorker values into the flows map.","Report occurrences as bugs with full logs and flowID."],"tags":["go","internal-invariant","defensive-check"],"backgroundTag":"nil-worker-invariant-violation","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}