File size: 6,707 Bytes
ea4e80c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Local execution runtime for CSV-to-report simulation scenarios."""

from __future__ import annotations

import csv
import shutil
import tempfile
from pathlib import Path
from typing import Any

try:
    import duckdb
except ImportError:  # pragma: no cover - exercised only when dependency missing
    duckdb = None


def _parse_simple_yaml(text: str) -> dict[str, Any]:
    data: dict[str, Any] = {}
    current_list_key: str | None = None
    for raw_line in text.splitlines():
        line = raw_line.rstrip()
        if not line or line.lstrip().startswith("#"):
            continue
        stripped = line.strip()
        if stripped.startswith("- "):
            if current_list_key is None:
                continue
            data.setdefault(current_list_key, []).append(stripped[2:].strip())
            continue
        if ":" not in stripped:
            continue
        key, value = stripped.split(":", 1)
        key = key.strip()
        value = value.strip()
        if not value:
            current_list_key = key
            data[key] = []
        else:
            current_list_key = None
            data[key] = value.strip("\"'")
    return data


def _preview_rows(rows: list[tuple[Any, ...]], columns: list[str], limit: int = 3) -> list[dict[str, Any]]:
    preview: list[dict[str, Any]] = []
    for row in rows[:limit]:
        preview.append({column: value for column, value in zip(columns, row)})
    return preview


def execute_csv_report_runtime(scenario: dict[str, Any], state: dict[str, Any]) -> dict[str, Any]:
    files = dict(scenario.get("files", {}))
    files.update(state.get("edited_files", {}))

    result: dict[str, Any] = {
        "ran": True,
        "succeeded": False,
        "checks": {
            "storage_stage_check": False,
            "duckdb_load_check": False,
            "report_view_check": False,
            "output_schema_check": False,
        },
        "logs": [],
        "errors": [],
        "output_schema": [],
        "report_preview": [],
        "row_count": 0,
        "db_path": "",
        "staged_csv_path": "",
        "run_dir": "",
        "materialized_artifacts": {},
    }

    if duckdb is None:
        result["errors"].append("duckdb dependency is not installed.")
        return result

    target = scenario.get("simulation_target", {}) or scenario.get("llm_draft", {}).get("simulation_target", {})
    source_csv = str(target.get("source_csv", ""))
    pipeline_path = str(target.get("pipeline_path", ""))
    load_sql_path = str(target.get("load_sql_path", ""))
    build_sql_path = str(target.get("build_sql_path", ""))
    report_sql_path = str(target.get("report_sql_path", ""))

    csv_content = files.get(source_csv, "")
    pipeline_text = files.get(pipeline_path, "")
    load_sql = files.get(load_sql_path, "")
    build_sql = files.get(build_sql_path, "")
    report_sql = files.get(report_sql_path, "")
    if not csv_content.strip():
        result["errors"].append(f"Missing source CSV: {source_csv}")
        return result
    if not pipeline_text.strip():
        result["errors"].append(f"Missing pipeline file: {pipeline_path}")
        return result
    if not load_sql.strip():
        result["errors"].append(f"Missing SQL file: {load_sql_path}")
        return result
    if not build_sql.strip():
        result["errors"].append(f"Missing SQL file: {build_sql_path}")
        return result
    if not report_sql.strip():
        result["errors"].append(f"Missing SQL file: {report_sql_path}")
        return result

    pipeline = _parse_simple_yaml(pipeline_text)
    required_keys = ["storage_path", "raw_table", "load_sql", "build_sql", "report_sql", "final_view"]
    missing_keys = [key for key in required_keys if not pipeline.get(key)]
    if missing_keys:
        result["errors"].append(f"Pipeline missing keys: {', '.join(missing_keys)}")
        return result

    episode_id = str(state.get("episode_id") or "unknown-episode")
    workdir = Path(tempfile.mkdtemp(prefix=f"flowos-runtime-{episode_id}-"))
    staged_csv = workdir / pipeline["storage_path"]
    db_path = workdir / "runtime.duckdb"
    workdir.mkdir(parents=True, exist_ok=True)
    result["db_path"] = str(db_path)
    result["staged_csv_path"] = str(staged_csv)
    result["run_dir"] = str(workdir)

    try:
        staged_csv.parent.mkdir(parents=True, exist_ok=True)
        staged_csv.write_text(csv_content, encoding="utf-8")
        result["materialized_artifacts"] = {
            pipeline_path: pipeline_text,
            load_sql_path: load_sql,
            build_sql_path: build_sql,
            report_sql_path: report_sql,
            source_csv: csv_content,
        }
        result["checks"]["storage_stage_check"] = True
        result["logs"].append(f"staged csv to {staged_csv}")

        con = duckdb.connect(str(db_path))
        raw_table = str(pipeline["raw_table"])
        quoted_csv_path = str(staged_csv).replace("'", "''")
        con.execute(
            f"create or replace table {raw_table} as "
            f"select * from read_csv_auto('{quoted_csv_path}', header=true)"
        )
        result["checks"]["duckdb_load_check"] = True
        result["logs"].append(f"loaded raw table {raw_table}")

        con.execute(load_sql)
        result["logs"].append(f"executed {load_sql_path}")

        con.execute(build_sql)
        result["logs"].append(f"executed {build_sql_path}")

        con.execute(report_sql)
        final_view = str(pipeline["final_view"])
        columns = [row[1] for row in con.execute(f"pragma table_info('{final_view}')").fetchall()]
        result["output_schema"] = columns
        result["checks"]["report_view_check"] = bool(columns)

        expected_columns = list(target.get("required_output_columns", []))
        if columns == expected_columns:
            result["checks"]["output_schema_check"] = True

        rows = con.execute(f"select * from {final_view} order by 1, 2 limit 3").fetchall()
        count = con.execute(f"select count(*) from {final_view}").fetchone()
        result["report_preview"] = _preview_rows(rows, columns)
        result["row_count"] = int(count[0]) if count else 0
        result["succeeded"] = all(result["checks"].values())
        result["logs"].append(f"queried {final_view} rows={result['row_count']}")
        con.close()
        result["db_path"] = ""
        result["staged_csv_path"] = ""
        result["run_dir"] = ""
        return result
    except Exception as exc:
        result["errors"].append(str(exc))
        result["db_path"] = ""
        result["staged_csv_path"] = ""
        result["run_dir"] = ""
        return result
    finally:
        shutil.rmtree(workdir, ignore_errors=True)