{"record":{"id":"7501d090312f4340","repo":"coleam00/Archon","slug":"failed-to-get-active-workflow-run-err-message","errorCode":null,"errorMessage":"Failed to get active workflow run: ${err.message}","messagePattern":"Failed to get active workflow run: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/db/workflows.ts","lineNumber":539,"sourceCode":"    getLog().error({ err }, 'db.workflow_run_get_status_failed');\n    throw new Error(`Failed to get workflow run status: ${err.message}`);\n  }\n}\n\nexport async function getActiveWorkflowRun(conversationId: string): Promise<WorkflowRun | null> {\n  try {\n    const result = await pool.query<WorkflowRun>(\n      `SELECT * FROM remote_agent_workflow_runs\n       WHERE (conversation_id = $1 OR parent_conversation_id = $2) AND status = 'running'\n       ORDER BY started_at DESC LIMIT 1`,\n      [conversationId, conversationId]\n    );\n    const row = result.rows[0];\n    return row ? normalizeWorkflowRun(row) : null;\n  } catch (error) {\n    const err = error as Error;\n    getLog().error({ err }, 'db.workflow_run_get_active_failed');\n    throw new Error(`Failed to get active workflow run: ${err.message}`);\n  }\n}\n\n/**\n * Find a paused workflow run for a conversation (or its parent).\n * Used by the message handler to give the chat agent the open approval gate as\n * context for the turn (#2565).\n * Non-throwing: returns null on DB error so the caller can fall through to normal routing.\n */\nexport async function getPausedWorkflowRun(conversationId: string): Promise<WorkflowRun | null> {\n  try {\n    const result = await pool.query<WorkflowRun>(\n      `SELECT * FROM remote_agent_workflow_runs\n       WHERE (conversation_id = $1 OR parent_conversation_id = $2) AND status = 'paused'\n       ORDER BY started_at DESC LIMIT 1`,\n      [conversationId, conversationId]\n    );\n    const row = result.rows[0];","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/core/src/db/workflows.ts#L521-L557","documentation":"getActiveWorkflowRun looks up the non-terminal workflow run for a conversation and normalizes the row. Any pool.query failure is logged (db.workflow_run_get_active_failed) and rethrown with this message. It signals the DB access layer failed, not that no active run exists (that returns null).","triggerScenarios":"Calling getActiveWorkflowRun(conversationId) while the DB is down, the workflow_runs schema is absent or stale, the connection pool is exhausted, or a driver-level timeout fires on the SELECT ... WHERE conversation_id = $1 AND status NOT IN terminal query.","commonSituations":"DB migration not applied before app start; Postgres max_connections reached; network partition between app and database; wrong environment pointing at a database without the workflow tables.","solutions":["Read the wrapped err.message / log event db.workflow_run_get_active_failed for the driver-level cause.","Confirm migrations have created workflow_runs in the target database.","Check pool health and connection limits (max_connections, pool size config).","Restore DB connectivity, then retry; the call is read-only and safe to repeat."],"exampleFix":"// before\nconst run = await getActiveWorkflowRun(conversationId);\n// after\nlet run: WorkflowRun | null = null;\ntry {\n  run = await getActiveWorkflowRun(conversationId);\n} catch (err) {\n  getLog().error({ err }, 'chat.active_run_lookup_failed');\n}","handlingStrategy":"try-catch","validationCode":"const ping = await pool.query('SELECT 1'); // cheap reachability check","typeGuard":"function isActiveRunLookupError(err: unknown): err is Error {\n  return err instanceof Error && err.message.startsWith('Failed to get active workflow run:');\n}","tryCatchPattern":"try {\n  const run = await getActiveWorkflowRun(conversationId);\n} catch (err) {\n  // Distinguish 'DB failed' (throw) from 'no run' (null) — only the wrapper throws.\n  reportDbFailure(err);\n  throw err;\n}","preventionTips":["Treat null return (no active run) as normal; only catch for DB failures.","Apply schema migrations in your deploy pipeline before swapping binaries.","Monitor pool wait times to catch saturation early.","Retry read-only lookups on transient connection errors."],"tags":["database","postgresql","workflow-run"],"backgroundTag":"database-query-failed","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}