File size: 10,145 Bytes
e3e5444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from langgraph.graph import StateGraph, END
from typing import TypedDict, Dict, Any, List
import logging
import os
from sqlalchemy.exc import SQLAlchemyError
from app.db.database import SessionLocal
from app.db.models import AnalysisJob
from app.agent.nodes import (
    profile_node, question_node, plan_node, execute_node,
    insight_node, report_node, clean_proposal_node, execute_cleaning_node
)

logger = logging.getLogger(__name__)
MAX_PROGRESS_LOG_CHARS = int(os.getenv("MAX_PROGRESS_LOG_CHARS", "20000"))


class AgentState(TypedDict):
    job_id: str
    file_path: str
    df_profile: Dict[str, Any]
    cleaning_plan: List[Dict[str, Any]]
    analytical_questions: List[Dict[str, Any]]
    analysis_plan: List[Dict[str, Any]]
    execution_results: Dict[str, Any]
    visualizations: Dict[str, Any]
    insights: str
    recommendations: List[str]
    report_path: str
    report_paths: Dict[str, str]
    error: str


# ── Progress logging ──────────────────────────────────────────────────────────

def _update_progress(job_id: str, new_log: str) -> None:
    db = SessionLocal()
    try:
        job = db.query(AnalysisJob).filter(AnalysisJob.id == job_id).first()
        if job:
            logs = (job.progress_logs or "") + new_log + "\n"
            if len(logs) > MAX_PROGRESS_LOG_CHARS:
                logs = logs[-MAX_PROGRESS_LOG_CHARS:]
            job.progress_logs = logs
            db.commit()
    except SQLAlchemyError as db_err:
        db.rollback()
        logger.error("Failed to update progress log for job %s: %s", job_id, db_err)
    finally:
        db.close()


def log_progress_node(state: AgentState, message: str) -> None:
    job_id = state["job_id"]
    _update_progress(job_id, f"[System]: {message}")
    logger.info("Job %s: %s", job_id, message)


# ── Node wrappers (add progress logging around each node) ─────────────────────

def profile_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Starting dataset profiling...")
    result = profile_node(state)
    log_progress_node(state, "Dataset profiling completed.")
    return result


def clean_proposal_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Analyzing dataset for missing values...")
    result = clean_proposal_node(state)
    log_progress_node(state, "Cleaning proposals generated.")
    return result


def execute_cleaning_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Executing approved data cleaning...")
    result = execute_cleaning_node(state)
    log_progress_node(state, "Data cleaning applied.")
    return result


def question_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Generating ranked analytical questions with LLM...")
    result = question_node(state)
    log_progress_node(state, "Analytical questions generated.")
    return result


def plan_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Generating analysis plan with LLM...")
    result = plan_node(state)
    log_progress_node(state, "Analysis plan generated.")
    return result


def execute_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Executing analysis operations (safe, aggregated only)...")
    result = execute_node(state)
    log_progress_node(state, "Analysis execution completed.")
    return result


def insight_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Generating insights and visualization metadata with LLM...")
    result = insight_node(state)
    log_progress_node(state, "Insights generated.")
    return result


def report_wrapper(state: AgentState) -> AgentState:
    log_progress_node(state, "Building final report (JSON, HTML, PDF)...")
    result = report_node(state)
    log_progress_node(state, "Report generated. Raw dataset discarded from memory.")
    return result


def error_handler(state: AgentState) -> AgentState:
    job_id = state["job_id"]
    error_msg = state.get("error", "Unknown error occurred")
    _update_progress(job_id, f"[Error]: {error_msg}")

    # Ensure in-memory dataset is discarded even on error
    from app.utils.data_store import discard_dataset
    discard_dataset(job_id)

    db = SessionLocal()
    try:
        job = db.query(AnalysisJob).filter(AnalysisJob.id == job_id).first()
        if job:
            job.status = "error"
            job.error_message = error_msg
            db.commit()
    except SQLAlchemyError as db_err:
        db.rollback()
        logger.error("Failed to persist error state for job %s: %s", job_id, db_err)
    finally:
        db.close()
    return state


# ── Route functions ───────────────────────────────────────────────────────────

def _route_error_or(next_node: str):
    def route(state: AgentState) -> str:
        return "error_node" if state.get("error") else next_node
    return route


route_after_profile_cleaner = _route_error_or("clean_proposal")
route_after_cleaning = _route_error_or("profile")
route_after_profile = _route_error_or("question")
route_after_question = _route_error_or("plan")
route_after_plan = _route_error_or("execute")
route_after_execute = _route_error_or("insight")
route_after_insight = _route_error_or("report")


def route_after_report(state: AgentState) -> str:
    return "error_node" if state.get("error") else END


# ── UNIFIED AUTONOMOUS GRAPH (UPDATED README: Profileβ†’Questionβ†’Planβ†’Executeβ†’Insightβ†’Report) ──
# This is the primary pipeline for start_analysis. Fully autonomous, no human gates.

_unified_builder = StateGraph(AgentState)
_unified_builder.add_node("profile", profile_wrapper)
_unified_builder.add_node("question", question_wrapper)
_unified_builder.add_node("plan", plan_wrapper)
_unified_builder.add_node("execute", execute_wrapper)
_unified_builder.add_node("insight", insight_wrapper)
_unified_builder.add_node("report", report_wrapper)
_unified_builder.add_node("error_node", error_handler)
_unified_builder.set_entry_point("profile")
_unified_builder.add_conditional_edges("profile", route_after_profile)
_unified_builder.add_conditional_edges("question", route_after_question)
_unified_builder.add_conditional_edges("plan", route_after_plan)
_unified_builder.add_conditional_edges("execute", route_after_execute)
_unified_builder.add_conditional_edges("insight", route_after_insight)
_unified_builder.add_conditional_edges("report", route_after_report)
_unified_builder.add_edge("error_node", END)
unified_workflow = _unified_builder.compile()



# ── Helper: build initial state from DB job ───────────────────────────────────

def _build_initial_state(job, extra: dict = None) -> AgentState:
    import json as _json
    state = AgentState(
        job_id=job.id,
        file_path=job.file_path,
        df_profile={},
        cleaning_plan=_json.loads(job.cleaning_plan) if job.cleaning_plan else [],
        analytical_questions=_json.loads(job.analytical_questions) if job.analytical_questions else [],
        analysis_plan=_json.loads(job.analysis_plan) if job.analysis_plan else [],
        execution_results={},
        visualizations={},
        insights="",
        recommendations=[],
        report_path="",
        report_paths={},
        error=""
    )
    if extra:
        state.update(extra)
    return state


def _fail_job(job_id: str, error: str) -> None:
    db = SessionLocal()
    try:
        job = db.query(AnalysisJob).filter(AnalysisJob.id == job_id).first()
        if job:
            job.status = "error"
            job.error_message = error
            db.commit()
    except SQLAlchemyError:
        db.rollback()
    finally:
        db.close()
    # Discard in-memory dataset on failure
    from app.utils.data_store import discard_dataset
    discard_dataset(job_id)


# ── Primary entry point (autonomous pipeline) ─────────────────────────────────

def run_autonomous_pipeline(job_id: str) -> None:
    """
    Execute the full autonomous analysis pipeline in one pass:
    Profile β†’ Question β†’ Plan β†’ Execute β†’ Insight β†’ Report β†’ Discard Data

    This is the UPDATED README workflow β€” no human approval gates.
    """
    import json
    db = SessionLocal()
    try:
        job = db.query(AnalysisJob).filter(AnalysisJob.id == job_id).first()
        if not job or job.status != "processing":
            logger.warning("Skipping pipeline for job %s (status=%s)", job_id, getattr(job, "status", "not found"))
            return
        initial_state = _build_initial_state(job)
    finally:
        db.close()

    try:
        final_state = unified_workflow.invoke(initial_state)
        final_error = final_state.get("error")
        report_path = final_state.get("report_path", "")

        if final_error:
            raise RuntimeError(str(final_error))
        if not report_path:
            raise RuntimeError("Workflow completed without generating a report")

        db = SessionLocal()
        try:
            job = db.query(AnalysisJob).filter(AnalysisJob.id == job_id).first()
            if job and job.status != "error":
                job.status = "completed"
                job.result_path = report_path
                job.analysis_plan = json.dumps(final_state.get("analysis_plan", []))
                job.analytical_questions = json.dumps(final_state.get("analytical_questions", []))
                db.commit()
        except SQLAlchemyError as db_err:
            db.rollback()
            logger.error("Failed to finalize completed state for job %s: %s", job_id, db_err)
            raise
        finally:
            db.close()

    except Exception as e:
        logger.exception("Autonomous pipeline failed for job %s", job_id)
        _fail_job(job_id, str(e))