{"record":{"id":"79e89642a11ae9a4","repo":"vxcontrol/pentagi","slug":"failed-to-stop-flow-w-79e896","errorCode":null,"errorMessage":"failed to stop flow: %w","messagePattern":"failed to stop flow: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/tools/flow_manager.go","lineNumber":650,"sourceCode":"\t\t\tbreak\n\t\t}\n\t}\n\n\tif !isRunning {\n\t\treturn \"No running task found — the flow is already in 'waiting' state and ready to accept input.\", nil\n\t}\n\n\tstopCtx, stopCancel := context.WithTimeout(ctx, flowOperationTimeout)\n\tdefer stopCancel()\n\n\tif err := t.handler(stopCtx, action.Reason); err != nil {\n\t\tif errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {\n\t\t\treturn \"\", fmt.Errorf(\n\t\t\t\t\"stop timed out after %s — the task may still be winding down. \"+\n\t\t\t\t\t\"Call %s to check the current state before proceeding\",\n\t\t\t\tflowOperationTimeout, GetFlowStatusToolName)\n\t\t}\n\t\treturn \"\", fmt.Errorf(\"failed to stop flow: %w\", err)\n\t}\n\n\t// Re-query task statuses to report the actual state the flow reached.\n\tnewTasks, err := t.db.GetFlowTasks(ctx, t.flowID)\n\tif err != nil {\n\t\t// Stop succeeded but we cannot verify the new state — return a safe partial message.\n\t\treturn fmt.Sprintf(\n\t\t\t\"Flow stop initiated (reason: %s). Could not verify new status: %s. \"+\n\t\t\t\t\"Call %s to confirm before proceeding.\",\n\t\t\taction.Reason, err, GetFlowStatusToolName), nil\n\t}\n\n\tfor _, task := range newTasks {\n\t\tif task.Status == database.TaskStatusRunning {\n\t\t\treturn fmt.Sprintf(\n\t\t\t\t\"Stop requested (reason: %s), but a task is still running — the flow has not reached 'waiting' yet. \"+\n\t\t\t\t\t\"Call %s to confirm before making further changes.\",\n\t\t\t\taction.Reason, GetFlowStatusToolName), nil","sourceCodeStart":632,"sourceCodeEnd":668,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/tools/flow_manager.go#L632-L668","documentation":"stop_flow invoked the flow-stop handler and it returned an error that is neither DeadlineExceeded nor Canceled. The raw error is wrapped, preserving the underlying reason (handler failure, internal error, store failure). The flow may or may not have been stopped.","triggerScenarios":"Calling stop_flow when the injected stop handler fails for a non-context reason — e.g. an internal error while signalling cancellation to the flow's goroutines, or a store error during the stop path.","commonSituations":"Backend-internal failure while propagating cancellation; race with flow lifecycle transitions; bugs or resource exhaustion (goroutines, DB pool) inside the stop handler.","solutions":["Read the wrapped cause (%v / errors.Is) to identify the handler failure.","Call get_flow_status to determine the flow's actual state before attempting anything else.","Retry stop_flow once transient conditions clear.","If persistent, capture backend logs and restart the pentagi service; report if reproducible."],"exampleFix":"// verify actual state after failed stop\nstatus, _ := getFlowStatus(flowId)\nif status == 'running' {\n    // retry stop_flow\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await tool.call('stop_flow', { reason: 'stop requested' });\n} catch (err) {\n  if (String(err).includes('failed to stop flow') && !String(err).includes('timed out')) {\n    log.error('stop handler failed', parseWrappedCause(err));\n    // verify state and retry\n    const status = await tool.call('get_flow_status', { detail: 'summary' });\n  }\n}","preventionTips":["Inspect the wrapped cause before retrying blindly","Check flow status after every failed stop","Watch backend logs for recurring handler failures","Restart the backend if stop handlers persistently fail"],"tags":["flow-stop","handler-error","internal"],"backgroundTag":"operation-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}