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_test, loads the diagnostic manual as the priority knowledge base, isolates the actual procedure that failed (never an orchestrating "Parent Procedure"), reproduces and tests hypotheses always inside a TRANSACTION + ROLLBACK, rebuilds the execution context up to 3 levels deep, uses AI to confirm the root cause and generate the SQL patch, saves the result and notifies Teams — continuing through all remaining candidates for the same error, not just the first one.

Inputtb_jobs_test
Knowledge basemanual_diagnostico_ia.docx
OutputTB_ANALISE_IA + Teams card (per finding)
Isolated failurelogs it and moves to the next candidate/JOB
How to read the diagram
Phase 1 — select procedure
Phase 2 — manual + locate the block
Phase 3 — context + AI diagnosis
Phase 4 — log and notify
Decision point
Runs in sandbox (always ROLLBACK)
Exception / fallback path
Retry or bypass
01 Select the responsible procedure 02 Consult the manual and locate the causing block 03 Rebuild context and diagnose with AI 04 Log and notify no yes yes no no yes, next candidate no yes yes no yes no yes no Fetch pending errors SELECT * FROM tb_jobs_test Load diagnostic manual + general guidelines (once, outside the error loop) Are there pending errors? End Next error in the list Build candidate procedures referenced error + children (EXEC) + main (if not orchestrating) Is the main procedure an orchestrating Parent Procedure? Rank by evidence children before main; object/column referenced in the error Exclude main from candidates only maps the children — never a candidate itself Test the whole candidate procedure EXEC [schema].[procedure] TRANSACTION + ROLLBACK Did ANY real error occur? (matches the original message or not) Any candidates left? Log: no candidate reproduced a real error Consult the diagnostic manual by the error's code/category/keyword AI breaks the procedure into logical blocks Test blocks cumulatively block 1 → 1+2 → 1+2+3 ... TRANSACTION + ROLLBACK Did any block reproduce it? Fallback: locate block by the object referenced in the error Responsible block identified Rebuild execution context 3 fallback levels + re-declared parameters AI generates and runs diagnostic SELECTs read-only — guided by the manual TRANSACTION + ROLLBACK AI confirms the root cause manual + actual code + test results Cause confirmed? Write to TB_ANALISE_IA cause, action, patch, tokens, cost AI generates SQL patch ready to copy/paste No patch — manual review Send card to Teams per finding, not per JOB — a failure here is only logged More errors in the list? Close database connection End
Center column: normal flow of an analysis. Right column: branches (orchestrating Parent Procedure excluded, next candidate, static block fallback, no patch). The longest dashed line loops around the entire diagram when no candidate procedure reproduces any error at all — it jumps straight to "more errors in the list?", without entering the diagnosis phase.