Diagnostic agent · SQL Agent + AI

From the failed JOB to a copy-paste-ready patch

Full flow run by Main/main.py on every round: fetches the pending errors from tb_jobs, isolates the actual procedure that failed, reproduces and tests hypotheses always inside a TRANSACTION + ROLLBACK, uses AI to confirm the root cause and generate the SQL patch, saves the result and notifies Teams.

InputDBA.tb_jobs
OutputTB_ANALISE_IA + Teams card
Per error1–4 calls to OpenAI
Isolated failurelogs it and moves to the next JOB
How to read the diagram
Phase 1 — select procedure
Phase 2 — locate the block
Phase 3 — AI diagnosis
Phase 4 — log and notify
Decision point
Runs in sandbox (always ROLLBACK)
Exception / fallback path
Retry or bypass
01Select the responsible procedure 02Locate the causing block 03AI diagnosis and patch 04Log and notify yes no yes no yes, next candidate no yes no yes no yes no Fetch pending errors SELECT * FROM tb_jobs_test Are there pending errors? Next error in the list Build candidate procedures referenced error + main + children (EXEC) Rank by evidence object / column referenced in the error Test the whole candidate procedure EXEC [schema].[procedure] TRANSACTION + ROLLBACK Reproduced OR is it the procedure already flagged by SQL Agent? AI breaks it into logical blocks Test blocks cumulatively block 1 → 1+2 → 1+2+3 ... TRANSACTION + ROLLBACK Did any block reproduce it? Responsible block identified AI generates and runs diagnostic SELECTs read-only — blocks writes/EXEC TRANSACTION + ROLLBACK AI confirms the root cause Cause confirmed? Write to TB_ANALISE_IA cause, action, patch, tokens, cost Send card to Teams a failure here is only logged, it doesn't stop the batch More errors in the list? Close database connection End End Any candidates left? Log: responsible procedure not identified Fallback: locate block by the object referenced in the error No patch — manual review AI generates SQL patch ready to copy/paste
Center column: normal flow of an analysis. Right column: branches (next candidate, static fallback, no patch). The long dashed line loops around the diagram when no candidate procedure reproduces the error — it jumps straight to "more errors in the list?".