Spaces:
Sleeping
Sleeping
File size: 46,286 Bytes
f676764 fed29a8 f676764 ca9d470 f676764 fed29a8 f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 d1dd29d ca9d470 f676764 ca9d470 f676764 d1dd29d ca9d470 f676764 fed29a8 e87d396 fed29a8 ca9d470 f676764 082fc7c f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 f676764 ca9d470 082fc7c ca9d470 f676764 ca9d470 f676764 ca9d470 f676764 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d 91aacc4 d1dd29d f676764 | 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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 | """Baseline policy helpers for the Developer Control Room benchmark."""
from __future__ import annotations
import json
import os
import re
import time
import sys
import textwrap
from typing import Any, List, Optional
import requests
from openai import OpenAI
from client import DeveloperControlRoomEnv
from models import ActionParameters, DeveloperControlRoomAction
from tasks import SCENARIO_REGISTRY, TASK_DEFINITIONS, get_task
SYSTEM_PROMPT = textwrap.dedent(
"""
You are controlling a developer-assistant benchmark for data-platform workflows.
Reply with exactly one JSON object with keys `action_type` and `parameters`.
Do not include markdown, prose, or code fences.
Valid actions:
{"action_type":"search_workspace","parameters":{"query":"..."}}
{"action_type":"read_file","parameters":{"path":"..."}}
{"action_type":"inspect_schema","parameters":{"asset":"..."}}
{"action_type":"inspect_lineage","parameters":{"asset":"..."}}
{"action_type":"inspect_llm_draft","parameters":{"draft_id":"primary"}}
{"action_type":"edit_file","parameters":{"path":"...","content":"..."}}
{"action_type":"run_validator","parameters":{"validator":"..."}}
{"action_type":"submit_repair","parameters":{"root_cause":"...","fix_path":"...","summary":"..."}}
{"action_type":"submit_review","parameters":{"verdict":"approve|reject","issue_type":"...","summary":"..."}}
{"action_type":"submit_workspace","parameters":{"summary":"..."}}
Use exact file paths, assets, and validator names from the observation.
Prefer short, efficient trajectories and submit when enough evidence is gathered.
"""
).strip()
DEBUG_LOGGING = os.getenv("DEVELOPER_CONTROL_ROOM_DEBUG", "false").lower() == "true"
MODEL_RETRY_COUNT = int(os.getenv("DEVELOPER_CONTROL_ROOM_MODEL_RETRY_COUNT", "3"))
MODEL_RETRY_DELAY_SECONDS = float(os.getenv("DEVELOPER_CONTROL_ROOM_MODEL_RETRY_DELAY_SECONDS", "2"))
def debug_log(message: str) -> None:
if DEBUG_LOGGING:
print(message, file=sys.stderr, flush=True)
def allowed_review_issue_types() -> set[str]:
issue_types: set[str] = set()
for task in TASK_DEFINITIONS.values():
if task.get("grader_family") != "review":
continue
for scenario_id in task.get("scenario_ids", []):
scenario = SCENARIO_REGISTRY.get(scenario_id, {})
review_target = scenario.get("review_target", {})
issue_type = str(review_target.get("correct_issue_type") or "").strip()
if issue_type:
issue_types.add(issue_type)
return issue_types
def _matches_group(text: str, group: list[str]) -> bool:
lowered = text.lower()
return all(term.lower() in lowered for term in group)
def review_submission_is_grounded(observation: Any, params: dict[str, Any]) -> bool:
scenario = SCENARIO_REGISTRY.get(observation.scenario_id, {})
review_target = scenario.get("review_target", {})
expected_issue_type = str(review_target.get("correct_issue_type") or "").strip()
summary_groups = review_target.get("summary_groups", [])
validator_targets = set(review_target.get("validator_targets", []))
submitted_issue_type = str(params.get("issue_type") or "").strip()
summary = str(params.get("summary") or "")
if expected_issue_type and submitted_issue_type != expected_issue_type:
return False
matched_groups = sum(1 for group in summary_groups if _matches_group(summary, group))
required_matches = 0
if summary_groups:
required_matches = 2
if len(summary_groups) >= 4:
required_matches = 3
if matched_groups < required_matches:
return False
if validator_targets and not validator_targets.issubset(set(observation.validator_status)):
return False
return True
def compact_json(data: dict[str, Any]) -> str:
return json.dumps(data, separators=(",", ":"), ensure_ascii=True)
def parse_model_action(response_text: str) -> Optional[dict[str, Any]]:
text = response_text.strip()
if not text:
return None
if text.startswith("```"):
text = "\n".join(line for line in text.splitlines() if not line.startswith("```")).strip()
try:
return json.loads(text)
except json.JSONDecodeError:
decoder = json.JSONDecoder()
starts = [match.start() for match in re.finditer(r"\{", text)]
for start in starts:
try:
candidate, _ = decoder.raw_decode(text[start:])
if isinstance(candidate, dict):
return candidate
except json.JSONDecodeError:
continue
return None
def get_phase_guidance(step: int, task_id: str) -> str:
grader_family = get_task(task_id)["grader_family"]
if step <= 4:
return (
"Early phase: gather evidence only. Prefer read_file, inspect_schema, inspect_lineage, or "
"inspect_llm_draft. Do not submit. Avoid edit_file unless you have read the target and, when relevant, "
"inspected the key asset."
)
if step <= 10:
if grader_family == "review":
return (
"Middle phase: finish evidence gathering, then run relevant validators. Submit a review only after "
"you have inspected the draft and read supporting policy or contract material."
)
return (
"Middle phase: make at most one targeted edit, then run validators. Avoid repeated edits without using "
"validator feedback. Do not submit until validators or evidence indicate the fix is grounded."
)
return (
"Late phase: prefer validators or submission. If the workspace is not ready, choose one safe action that "
"unblocks submission. Return exactly one JSON object and never include multiple actions."
)
def get_task_specific_guidance(observation: Any) -> str:
task_id = observation.task_id
if task_id.startswith("review_"):
return (
"For submit_review, use concise issue_type terminology grounded in the observed draft, policy text, and validator feedback. "
"Prefer terminology already present in the workspace or validator context over invented synonyms."
)
if task_id.startswith("synthesize_"):
return (
"For workflow creation tasks, mirror the naming, column shapes, and artifact patterns shown in the referenced files and templates. "
"Avoid inventing extra columns, fields, or file structures unless the observed standards clearly require them."
)
if task_id == "simulate_csv_report_workflow":
return (
"For the simulation workflow, respect the active role. Builder should create the initial pipeline YAML and SQL artifacts. "
"Fixer should use runtime_status, execution_logs, and output_schema to repair the same files until the runtime checks pass."
)
if task_id.startswith("repair_"):
return (
"For repair tasks, preserve published contracts and existing target names exactly. "
"Prefer minimal edits that align with observed schema, path, and validator clues."
)
return "Prefer existing terminology, shapes, and naming from the observed workspace over invented alternatives."
def build_user_prompt(
step: int,
observation: Any,
history: List[str],
episode_memory: str = "",
) -> str:
history_block = "\n".join(history[-4:]) if history else "None"
memory_block = episode_memory if episode_memory else "None"
phase_guidance = get_phase_guidance(step, observation.task_id)
task_guidance = get_task_specific_guidance(observation) or "None"
return textwrap.dedent(
f"""
Step: {step}
Task: {observation.task_id}
Scenario: {observation.scenario_id}
Request: {observation.developer_request}
Workspace summary: {observation.workspace_summary}
Known files: {json.dumps(observation.known_files)}
Editable targets: {json.dumps(observation.editable_targets)}
Known assets: {json.dumps(observation.known_assets)}
Available validators: {json.dumps(observation.available_validators)}
Previous steps:
{history_block}
Recent episode memory:
{memory_block}
Gathered data: {json.dumps(observation.queried_data)}
Edited files: {json.dumps(list(observation.edited_files.keys()))}
Validator status: {json.dumps(observation.validator_status)}
Last feedback: {observation.feedback}
Last action error: {observation.last_action_error or "null"}
Active role: {getattr(observation, "active_role", "builder")}
Runtime status: {json.dumps(getattr(observation, "runtime_status", {}))}
Execution logs: {json.dumps(getattr(observation, "execution_logs", []))}
Output schema: {json.dumps(getattr(observation, "output_schema", []))}
Phase guidance: {phase_guidance}
Task-specific guidance: {task_guidance}
Return exactly one JSON action object only.
Never return multiple JSON objects.
Never include markdown, explanations, or prose.
"""
).strip()
def get_model_action(
client: OpenAI,
model_name: str,
temperature: float,
max_tokens: int,
timeout_seconds: float,
step: int,
observation: Any,
history: List[str],
episode_memory: str = "",
) -> Optional[dict[str, Any]]:
user_prompt = build_user_prompt(step, observation, history, episode_memory)
last_exc: Exception | None = None
attempts = max(1, MODEL_RETRY_COUNT)
for attempt in range(1, attempts + 1):
try:
if os.getenv("DEVELOPER_CONTROL_ROOM_FORCE_MODEL_TIMEOUT", "false").lower() == "true":
raise TimeoutError("Forced model timeout for fallback-path validation.")
completion = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout_seconds,
stream=False,
)
text = (completion.choices[0].message.content or "").strip()
debug_log(f"BASELINE:: LLM IS BEING CALLED!")
return parse_model_action(text)
except Exception as exc:
last_exc = exc
debug_log(f"[DEBUG] Model request failed on attempt {attempt}/{attempts}: {exc}")
if attempt < attempts:
time.sleep(MODEL_RETRY_DELAY_SECONDS)
if last_exc is not None:
debug_log(f"[DEBUG] Falling back after model failure: {last_exc}")
return None
def action_is_valid(action: Optional[dict[str, Any]], observation: Any) -> bool:
if not isinstance(action, dict):
return False
action_type = action.get("action_type")
if action_type not in observation.available_actions:
return False
params = action.get("parameters", {})
if not isinstance(params, dict):
return False
queried = observation.queried_data
read_paths = set(queried.get("read_file", {}))
inspected_assets = set(queried.get("inspect_schema", {})) | set(queried.get("inspect_lineage", {}))
has_edit = bool(observation.edited_files)
repair_all_validators_passed = (
observation.task_id.startswith("repair_")
and bool(observation.available_validators)
and all(
observation.validator_status.get(validator, {}).get("passed", False)
for validator in observation.available_validators
)
)
if repair_all_validators_passed and action_type != "submit_repair":
return False
if action_type == "read_file":
path = (params.get("path") or "").strip()
return (
bool(path)
and path in set(observation.known_files) | set(observation.editable_targets)
and path not in read_paths
)
if action_type == "inspect_schema":
asset = (params.get("asset") or "").strip()
return bool(asset) and asset in set(observation.known_assets) and asset not in queried.get("inspect_schema", {})
if action_type == "inspect_lineage":
asset = (params.get("asset") or "").strip()
return bool(asset) and asset in set(observation.known_assets) and asset not in queried.get("inspect_lineage", {})
if action_type == "inspect_llm_draft":
return (params.get("draft_id") or "primary").strip() == "primary"
if action_type == "run_validator":
validator = (params.get("validator") or "").strip()
if not validator or validator not in set(observation.available_validators):
return False
if validator in observation.validator_status:
return False
if observation.task_id.startswith("repair_") or observation.task_id.startswith("synthesize_"):
return has_edit
return True
if action_type == "edit_file":
path = (params.get("path") or "").strip()
content = (params.get("content") or "").strip()
if not path or not content or path not in set(observation.editable_targets):
return False
if observation.task_id.startswith("synthesize_"):
return bool(read_paths) and bool(inspected_assets)
if observation.task_id.startswith("repair_"):
return path in read_paths or bool(read_paths & set(observation.known_files))
return True
if action_type == "submit_repair":
submission = {"root_cause", "fix_path", "summary"}
fix_path = str(params.get("fix_path") or "").strip()
return (
has_edit
and bool(observation.validator_status)
and submission.issubset(params)
and all(str(params.get(key) or "").strip() for key in submission)
and fix_path in set(observation.editable_targets)
)
if action_type == "submit_workspace":
return has_edit and bool(observation.validator_status) and bool(str(params.get("summary") or "").strip())
if action_type == "submit_review":
has_supporting_evidence = bool(read_paths) or bool(observation.validator_status)
required = {"verdict", "issue_type", "summary"}
return (
"primary" in queried.get("inspect_llm_draft", {})
and has_supporting_evidence
and required.issubset(params)
and str(params.get("issue_type") or "").strip() in allowed_review_issue_types()
and all(str(params.get(key) or "").strip() for key in required)
and review_submission_is_grounded(observation, params)
)
return True
def build_action(action_dict: dict[str, Any]) -> DeveloperControlRoomAction:
return DeveloperControlRoomAction(
action_type=action_dict.get("action_type", ""),
parameters=ActionParameters(**action_dict.get("parameters", {})),
)
def get_http_base(env: DeveloperControlRoomEnv, env_url: Optional[str]) -> Optional[str]:
if env_url:
return env_url.rstrip("/")
ws_url = getattr(env, "_ws_url", "")
if not ws_url:
return None
base = ws_url[:-3] if ws_url.endswith("/ws") else ws_url
if base.startswith("ws://"):
return "http://" + base[len("ws://") :]
if base.startswith("wss://"):
return "https://" + base[len("wss://") :]
return base
def fetch_grader_result(env: DeveloperControlRoomEnv, env_url: Optional[str]) -> dict[str, Any]:
base = get_http_base(env, env_url)
if not base:
return {"total": 0.0, "solved": False}
try:
response = requests.get(f"{base}/grader", timeout=20)
response.raise_for_status()
payload = response.json()
return {
"total": float(payload.get("total", 0.0)),
"solved": bool(payload.get("solved", False)),
}
except Exception as exc:
debug_log(f"[DEBUG] grader fetch failed: {exc}")
return {"total": 0.0, "solved": False}
def repair_sql() -> str:
return """with base as (
select
o.order_id,
o.customer_id,
o.event_time as order_ts,
date(o.event_time) as order_date,
o.amount_cents / 100.0 as total_amount
from raw.orders_events o
where o.status = 'completed'
)
select * from base;
"""
def repair_path_yaml() -> str:
return """name: customer_events_ingest
schedule: "0 * * * *"
owner: ingestion-platform
source_path: "abfss://bronze@datalake.dfs.core.windows.net/customer-events/v2/dt={{ ds }}/"
format: parquet
target: customer_events_raw
"""
def repair_jenkins_job() -> str:
return """pipeline {
agent any
stages {
stage('Run') {
steps {
sh './scripts/run_orders_daily.sh'
}
}
}
}
"""
def repair_dependency_yaml() -> str:
return """name: revenue_dashboard_refresh
schedule: "0 5 * * *"
owner: finance-analytics
sources:
- orders_daily
target: revenue_dashboard_refresh
"""
def repair_release_yaml() -> str:
return repair_release_yaml_for("TARGET_WAREHOUSE_ROLE", "analytics_writer")
def repair_release_yaml_for(env_key: str, role: str) -> str:
return f"""job: reporting_release
runner_image: reporting-release:latest
script: ./scripts/release_reporting_asset.sh
env:
{env_key}: {role}
ALERT_ROUTE: finance-reporting
"""
def repair_type_alignment_sql() -> str:
return repair_type_alignment_sql_for("customer_code")
def repair_type_alignment_sql_for(id_col: str) -> str:
return f"""insert into customer_profile_dim (
{id_col},
loyalty_tier,
state_code
)
select
s.{id_col},
s.loyalty_tier,
s.state_code
from customer_profile_stage s;
"""
def repair_archive_load_yaml() -> str:
return """job: orders_archive_load
target_table: orders_archive
source_columns:
- order_id
- customer_id
- order_ts
- order_status
- gross_amount_usd
- currency_code
defaults:
ingest_source: csv_archive
ingest_batch_id: current_batch_id
created_at: current_timestamp
updated_at: current_timestamp
archive_status: active
notes: ''
"""
def repair_pk_merge_sql() -> str:
return """with deduped as (
select
d.customer_id,
d.customer_name,
d.state_code,
d.updated_at,
row_number() over (
partition by customer_id
order by d.updated_at desc
) as rn
from dim_customer_delta d
)
insert into dim_customer (
customer_id,
customer_name,
state_code,
updated_at
)
select
deduped.customer_id,
deduped.customer_name,
deduped.state_code,
deduped.updated_at
from deduped
where deduped.rn = 1;
"""
def merchant_pipeline() -> str:
return """name: merchant_risk_features
schedule: "0 1 * * *"
owner: risk-platform
sources:
- transactions_silver
- chargebacks_silver
- merchant_dim
target: merchant_risk_features
checks_file: checks/merchant_risk_features.yaml
downstreams:
- risk_model_scoring
"""
def merchant_sql() -> str:
return """with tx as (
select
t.merchant_id,
count(*) as txn_count_30d
from transactions_silver t
join merchant_dim m on t.merchant_id = m.merchant_id
where m.is_test = false
and t.processed_at >= current_date - interval '30 day'
group by 1
),
cb as (
select
c.merchant_id,
count(*) as chargeback_count_30d
from chargebacks_silver c
where c.processed_at >= current_date - interval '30 day'
group by 1
)
select
tx.merchant_id,
tx.txn_count_30d,
coalesce(cb.chargeback_count_30d, 0) * 1.0 / nullif(tx.txn_count_30d, 0) as chargeback_rate_30d,
current_date as snapshot_date
from tx
left join cb on tx.merchant_id = cb.merchant_id;
"""
def merchant_checks() -> str:
return """checks:
- type: freshness
column: snapshot_date
- type: not_null
column: merchant_id
- type: not_null
column: snapshot_date
"""
def merchant_schema() -> str:
return """{
"name": "merchant_risk_features",
"columns": [
{"name": "merchant_id", "type": "string"},
{"name": "txn_count_30d", "type": "integer"},
{"name": "chargeback_rate_30d", "type": "float"},
{"name": "snapshot_date", "type": "date"}
]
}
"""
def weekly_view_sql() -> str:
return """with invoices as (
select
i.account_id,
date_trunc('week', i.invoice_ts) as revenue_week,
sum(i.net_revenue_usd) as invoice_revenue_usd
from invoice_line_items i
group by 1, 2
),
credits as (
select
c.account_id,
date_trunc('week', c.credit_memo_date) as revenue_week,
sum(c.credit_amount_usd) as credit_amount_usd
from credit_memos_daily c
group by 1, 2
)
select
invoices.account_id,
invoices.revenue_week,
invoices.invoice_revenue_usd - coalesce(credits.credit_amount_usd, 0) as net_revenue_usd
from invoices
left join credits
on invoices.account_id = credits.account_id
and invoices.revenue_week = credits.revenue_week;
"""
def weekly_view_schema() -> str:
return """{
"name": "finance_weekly_net_revenue",
"columns": [
{"name": "account_id", "type": "string"},
{"name": "revenue_week", "type": "timestamp"},
{"name": "net_revenue_usd", "type": "float"}
]
}
"""
def margin_watch_sql() -> str:
return margin_watch_sql_for("margin_delta_pct")
def margin_watch_sql_for(metric: str) -> str:
return f"""with invoices as (
select
i.account_id,
date(i.invoice_ts) as margin_date,
sum(i.net_revenue_usd) as net_revenue_usd,
sum(i.margin_usd) as margin_usd
from invoice_line_items i
group by 1, 2
),
credits as (
select
c.account_id,
c.credit_memo_date as margin_date,
sum(c.credit_amount_usd) as credit_amount_usd
from credit_memos_daily c
group by 1, 2
)
select
invoices.account_id,
invoices.margin_date,
invoices.margin_usd - coalesce(credits.credit_amount_usd, 0) as margin_usd,
(
(invoices.margin_usd - coalesce(credits.credit_amount_usd, 0))
- lag(invoices.margin_usd - coalesce(credits.credit_amount_usd, 0))
over (partition by invoices.account_id order by invoices.margin_date)
) / nullif(
lag(invoices.margin_usd - coalesce(credits.credit_amount_usd, 0))
over (partition by invoices.account_id order by invoices.margin_date),
0
) as {metric}
from invoices
left join credits
on invoices.account_id = credits.account_id
and invoices.margin_date = credits.margin_date;
"""
def margin_watch_schema() -> str:
return margin_watch_schema_for("margin_delta_pct")
def margin_watch_schema_for(metric: str) -> str:
return f"""{{
"name": "finance_margin_watch",
"columns": [
{{"name": "account_id", "type": "string"}},
{{"name": "margin_date", "type": "date"}},
{{"name": "margin_usd", "type": "float"}},
{{"name": "{metric}", "type": "float"}}
]
}}
"""
def margin_watch_alert() -> str:
return margin_watch_alert_for("margin_delta_pct", "-0.10")
def margin_watch_alert_for(metric: str, threshold: str) -> str:
return f"""name: finance_margin_watch
dataset: finance_margin_watch
metric: {metric}
threshold: {threshold}
severity: medium
"""
def _match_first(text: str, options: list[str], default: str) -> str:
lowered = text.lower()
for option in options:
if option.lower() in lowered:
return option
return default
def _detect_release_variant(observation: Any) -> tuple[str, str, str]:
text = f"{observation.developer_request} {observation.workspace_summary}"
env_key = _match_first(text, ["TARGET_REPORTING_ROLE", "TARGET_WAREHOUSE_ROLE"], "TARGET_WAREHOUSE_ROLE")
if env_key == "TARGET_REPORTING_ROLE":
return env_key, "reporting_writer", "REPORTING_ROLE"
return env_key, "analytics_writer", "WAREHOUSE_ROLE"
def _detect_id_column(observation: Any) -> str:
text = f"{observation.developer_request} {observation.workspace_summary}"
return _match_first(text, ["profile_code", "member_code", "customer_code"], "customer_code")
def _detect_contract_metric(observation: Any) -> tuple[str, str]:
text = f"{observation.developer_request} {observation.workspace_summary}"
metric = _match_first(text, ["net_revenue_usd", "arr_usd", "mrr_usd"], "mrr_usd")
replacements = {
"mrr_usd": "monthly_recurring_revenue_usd",
"arr_usd": "annual_recurring_revenue_usd",
"net_revenue_usd": "net_revenue_amount_usd",
}
return metric, replacements[metric]
def _detect_watch_variant(observation: Any) -> tuple[str, str]:
text = f"{observation.developer_request} {observation.workspace_summary}"
metric = _match_first(text, ["net_revenue_delta_pct", "margin_delta_pct"], "margin_delta_pct")
threshold_match = re.search(r"below `(-?\d+\.\d+)`", text)
threshold = threshold_match.group(1) if threshold_match else ("-0.12" if metric == "net_revenue_delta_pct" else "-0.10")
return metric, threshold
def margin_pipeline() -> str:
return """name: customer_margin_mart
schedule: "0 1 * * *"
owner: data-platform
sources:
- orders_silver
- refunds_silver
- customer_dim
target: customer_margin_mart
checks_file: checks/customer_margin_mart.yaml
"""
def margin_sql() -> str:
return """with orders as (
select
o.customer_id,
sum(o.gross_amount_usd) as gross_revenue_usd
from orders_silver o
join customer_dim c on o.customer_id = c.customer_id
where c.is_test = false
group by 1
),
refunds as (
select
r.customer_id,
sum(r.refund_amount_usd) as refund_amount_usd
from refunds_silver r
join customer_dim c on r.customer_id = c.customer_id
where c.is_test = false
group by 1
)
select
orders.customer_id,
orders.gross_revenue_usd,
coalesce(refunds.refund_amount_usd, 0) as refund_amount_usd,
orders.gross_revenue_usd - coalesce(refunds.refund_amount_usd, 0) as net_margin_usd,
current_date as snapshot_date
from orders
left join refunds on orders.customer_id = refunds.customer_id;
"""
def margin_checks() -> str:
return """checks:
- type: freshness
column: snapshot_date
- type: not_null
column: customer_id
- type: not_null
column: snapshot_date
"""
def margin_schema() -> str:
return """{
"name": "customer_margin_mart",
"columns": [
{"name": "customer_id", "type": "string"},
{"name": "gross_revenue_usd", "type": "float"},
{"name": "refund_amount_usd", "type": "float"},
{"name": "net_margin_usd", "type": "float"},
{"name": "snapshot_date", "type": "date"}
]
}
"""
def fallback_action(task_name: str, observation: Any) -> dict[str, Any]:
queried = observation.queried_data
validators = observation.validator_status
edited = observation.edited_files
scenario_id = observation.scenario_id
task_def = get_task(task_name)
grader_family = task_def["grader_family"]
if grader_family == "repair":
repair_specs = {
"PR-001": {
"read_paths": ["transforms/orders_daily.sql"],
"schema_assets": ["raw.orders_events"],
"edit_path": "transforms/orders_daily.sql",
"content": repair_sql(),
"validators": ["sql_compile", "contract_guard"],
"submission": {
"root_cause": "Schema rename changed event_ts to event_time in raw.orders_events.",
"fix_path": "transforms/orders_daily.sql",
"summary": "Updated the transform to read event_time while aliasing back to order_ts so the published contract stays stable.",
},
},
"PR-002": {
"read_paths": ["pipelines/customer_events_ingest.yaml", "docs/storage_layout.md"],
"schema_assets": [],
"edit_path": "pipelines/customer_events_ingest.yaml",
"content": repair_path_yaml(),
"validators": ["path_resolver", "target_guard"],
"submission": {
"root_cause": "The ADLS landing path still used the old date partition instead of the new dt folder layout.",
"fix_path": "pipelines/customer_events_ingest.yaml",
"summary": "Updated the ingest path to customer-events/v2 with dt partitions.",
},
},
"PR-003": {
"read_paths": ["ci/jenkins/orders_daily.groovy", "docs/ci_conventions.md"],
"schema_assets": [],
"edit_path": "ci/jenkins/orders_daily.groovy",
"content": repair_jenkins_job(),
"validators": ["jenkins_lint"],
"submission": {
"root_cause": "The Jenkins job called a hyphenated runner path that does not exist after the CI cleanup.",
"fix_path": "ci/jenkins/orders_daily.groovy",
"summary": "Restored the run_orders_daily.sh runner path in the Jenkins job definition.",
},
},
"PR-004": {
"read_paths": ["pipelines/revenue_dashboard_refresh.yaml", "docs/lineage_notes.md"],
"schema_assets": [],
"edit_path": "pipelines/revenue_dashboard_refresh.yaml",
"content": repair_dependency_yaml(),
"validators": ["dependency_guard"],
"submission": {
"root_cause": "The dashboard pipeline still depended on the deprecated orders_daily_v2 alias instead of the canonical upstream.",
"fix_path": "pipelines/revenue_dashboard_refresh.yaml",
"summary": "Repointed revenue_dashboard_refresh to orders_daily so the dashboard can refresh from the canonical dependency.",
},
},
"PR-005": {
"read_paths": ["deploy/reporting_release.yaml", "docs/deploy_runbook.md"],
"schema_assets": [],
"edit_path": "deploy/reporting_release.yaml",
"content": "",
"validators": ["deploy_lint", "role_guard"],
"submission": {},
},
"PR-006": {
"read_paths": [
"transforms/customer_profile_import.sql",
"samples/customer_profile_import.csv",
"docs/load_contracts.md",
],
"schema_assets": [],
"edit_path": "transforms/customer_profile_import.sql",
"content": "",
"validators": ["type_alignment_guard", "load_contract_guard"],
"submission": {},
},
"PR-007": {
"read_paths": [
"pipelines/orders_archive_load.yaml",
"schemas/orders_archive.json",
"docs/archive_load_guide.md",
],
"schema_assets": [],
"edit_path": "pipelines/orders_archive_load.yaml",
"content": repair_archive_load_yaml(),
"validators": ["column_mapping_guard", "archive_defaults_guard"],
"submission": {
"root_cause": "The extract only supplies 6 source columns, but the archive table now expects 12 columns with deterministic warehouse defaults.",
"fix_path": "pipelines/orders_archive_load.yaml",
"summary": "Added the required archive default mappings so the 6-column extract can load into the 12-column target table.",
},
},
"PR-008": {
"read_paths": [
"transforms/dim_customer_merge.sql",
"samples/dim_customer_delta.csv",
"docs/merge_strategy.md",
],
"schema_assets": [],
"edit_path": "transforms/dim_customer_merge.sql",
"content": repair_pk_merge_sql(),
"validators": ["pk_uniqueness_guard", "merge_strategy_guard"],
"submission": {
"root_cause": "Duplicate customer_id rows in the delta batch caused a primary key violation because the load inserted the full batch without deduplicating.",
"fix_path": "transforms/dim_customer_merge.sql",
"summary": "Updated the merge SQL to deduplicate by customer_id with row_number and keep the latest row before loading.",
},
},
}
spec = repair_specs.get(scenario_id, repair_specs["PR-001"])
if scenario_id == "PR-005":
env_key, role, stale_key = _detect_release_variant(observation)
spec = {
**spec,
"content": repair_release_yaml_for(env_key, role),
"submission": {
"root_cause": f"The release config still used {stale_key} instead of {env_key} after the deploy cleanup.",
"fix_path": "deploy/reporting_release.yaml",
"summary": f"Updated the reporting release config to pass {env_key} with the {role} role.",
},
}
if scenario_id == "PR-006":
id_col = _detect_id_column(observation)
spec = {
**spec,
"content": repair_type_alignment_sql_for(id_col),
"submission": {
"root_cause": f"The load cast {id_col} to integer even though the warehouse contract keeps it as a string with leading zeroes.",
"fix_path": "transforms/customer_profile_import.sql",
"summary": f"Updated the load mapping to preserve {id_col} as a string instead of casting it to integer.",
},
}
for path in spec["read_paths"]:
if path not in queried.get("read_file", {}):
return {"action_type": "read_file", "parameters": {"path": path}}
for asset in spec["schema_assets"]:
if asset not in queried.get("inspect_schema", {}):
return {"action_type": "inspect_schema", "parameters": {"asset": asset}}
if spec["edit_path"] not in edited:
return {
"action_type": "edit_file",
"parameters": {"path": spec["edit_path"], "content": spec["content"]},
}
for validator in spec["validators"]:
if validator not in validators:
return {"action_type": "run_validator", "parameters": {"validator": validator}}
return {"action_type": "submit_repair", "parameters": spec["submission"]}
if grader_family == "review":
review_specs = {
"LR-001": {
"read_paths": ["policies/data_governance.md"],
"validators": ["privacy_guard"],
"submission": {
"verdict": "reject",
"issue_type": "pii_exposure",
"summary": "Reject the patch because it adds customer_email to a shared analytics dataset and violates policy.",
},
},
"LR-002": {
"read_paths": ["docs/settlement_conventions.md"],
"validators": ["logic_guard"],
"submission": {
"verdict": "reject",
"issue_type": "technical_incorrectness",
"summary": "Reject the patch because the join key is wrong and the enrichment logic is incorrect.",
},
},
"LR-003": {
"read_paths": ["policies/published_contracts.md"],
"validators": ["contract_guard"],
"submission": {
"verdict": "reject",
"issue_type": "contract_breakage",
"summary": "",
},
},
"LR-004": {
"read_paths": ["policies/recovery_playbook.md"],
"validators": ["safety_guard"],
"submission": {
"verdict": "reject",
"issue_type": "unsafe_operation",
"summary": "Reject the recommendation because it suggests dropping a production mart and disabling checks.",
},
},
}
spec = review_specs.get(scenario_id, review_specs["LR-001"])
if scenario_id == "LR-003":
metric, replacement = _detect_contract_metric(observation)
spec = {
**spec,
"submission": {
"verdict": "reject",
"issue_type": "contract_breakage",
"summary": f"Reject the patch because it renames {metric} to {replacement} in a published contract.",
},
}
if "primary" not in queried.get("inspect_llm_draft", {}):
return {"action_type": "inspect_llm_draft", "parameters": {"draft_id": "primary"}}
for path in spec["read_paths"]:
if path not in queried.get("read_file", {}):
return {"action_type": "read_file", "parameters": {"path": path}}
for validator in spec["validators"]:
if validator not in validators:
return {"action_type": "run_validator", "parameters": {"validator": validator}}
return {"action_type": "submit_review", "parameters": spec["submission"]}
if grader_family == "workflow":
workflow_specs = {
"WS-001": {
"read_paths": ["templates/pipeline_template.yaml"],
"schema_assets": ["transactions_silver"],
"lineage_assets": [],
"edits": [
("pipelines/merchant_risk_features.yaml", merchant_pipeline()),
("sql/merchant_risk_features.sql", merchant_sql()),
("checks/merchant_risk_features.yaml", merchant_checks()),
("schemas/merchant_risk_features.json", merchant_schema()),
],
"validators": ["workflow_lint", "governance_check"],
"summary": "Created merchant_risk_features with the required workflow artifacts and governance-safe SQL.",
},
"WS-002": {
"read_paths": [
"sql/views/finance_daily_revenue.sql",
"docs/reporting_view_standards.md",
"schemas/views/finance_daily_revenue.json",
],
"schema_assets": ["invoice_line_items", "credit_memos_daily"],
"lineage_assets": [],
"edits": [
("sql/views/finance_weekly_net_revenue.sql", weekly_view_sql()),
("schemas/views/finance_weekly_net_revenue.json", weekly_view_schema()),
],
"validators": ["view_sql_check", "view_contract_check"],
"summary": "Created the finance_weekly_net_revenue reporting view with weekly grain and a matching schema contract.",
},
"WS-003": {
"read_paths": [
"templates/pipeline_template.yaml",
"docs/platform_standards.md",
"checks/order_features.yaml",
],
"schema_assets": ["orders_silver", "customer_dim"],
"lineage_assets": [],
"edits": [
("pipelines/customer_margin_mart.yaml", margin_pipeline()),
("sql/customer_margin_mart.sql", margin_sql()),
("checks/customer_margin_mart.yaml", margin_checks()),
("schemas/customer_margin_mart.json", margin_schema()),
],
"validators": ["mart_lint", "mart_governance"],
"summary": "Created customer_margin_mart with daily pipeline metadata and a contract-safe schema.",
},
"WS-004": {
"read_paths": [
"sql/views/finance_daily_revenue.sql",
"docs/reporting_view_standards.md",
"docs/alerting_playbook.md",
],
"schema_assets": ["invoice_line_items"],
"lineage_assets": [],
"edits": [
("sql/views/finance_margin_watch.sql", ""),
("schemas/views/finance_margin_watch.json", ""),
("alerts/finance_margin_watch.yaml", ""),
],
"validators": ["view_sql_check", "alert_config_check"],
"summary": "",
},
}
spec = workflow_specs.get(scenario_id, workflow_specs["WS-001"])
if scenario_id == "WS-004":
metric, threshold = _detect_watch_variant(observation)
spec = {
**spec,
"edits": [
("sql/views/finance_margin_watch.sql", margin_watch_sql_for(metric)),
("schemas/views/finance_margin_watch.json", margin_watch_schema_for(metric)),
("alerts/finance_margin_watch.yaml", margin_watch_alert_for(metric, threshold)),
],
"summary": f"Created the finance_margin_watch reporting view and alert config for {metric} monitoring.",
}
for path in spec["read_paths"]:
if path not in queried.get("read_file", {}):
return {"action_type": "read_file", "parameters": {"path": path}}
for asset in spec["schema_assets"]:
if asset not in queried.get("inspect_schema", {}):
return {"action_type": "inspect_schema", "parameters": {"asset": asset}}
for asset in spec["lineage_assets"]:
if asset not in queried.get("inspect_lineage", {}):
return {"action_type": "inspect_lineage", "parameters": {"asset": asset}}
for path, content in spec["edits"]:
if path not in edited:
return {
"action_type": "edit_file",
"parameters": {"path": path, "content": content},
}
for validator in spec["validators"]:
if validator not in validators:
return {"action_type": "run_validator", "parameters": {"validator": validator}}
return {"action_type": "submit_workspace", "parameters": {"summary": spec["summary"]}}
if grader_family == "simulation":
scenario = SCENARIO_REGISTRY.get(observation.scenario_id, {})
simulation_target = scenario.get("simulation_target", {})
reference_solution = scenario.get("llm_draft", {}).get("reference_solution", {})
source_csv = simulation_target.get("source_csv", "data/sales_orders.csv")
final_view_name = (
simulation_target.get("expected_final_view", {}).get("name")
or os.path.splitext(os.path.basename(source_csv))[0]
)
raw_asset = next(
(
asset
for asset in observation.known_assets
if asset.startswith("raw.")
),
"raw.sales_orders_csv",
)
read_order = [
"docs/runtime_contract.md",
source_csv,
]
for path in read_order:
if path in observation.known_files and path not in queried.get("read_file", {}):
return {"action_type": "read_file", "parameters": {"path": path}}
if raw_asset in observation.known_assets and raw_asset not in queried.get("inspect_schema", {}):
return {"action_type": "inspect_schema", "parameters": {"asset": raw_asset}}
simulation_edits = [
(
"pipelines/report_job.yaml",
reference_solution.get(
"pipeline_yaml",
"""name: customer_daily_report_job
storage_path: mock_s3/staged/sales_orders.csv
raw_table: raw_sales_orders
load_sql: sql/load_raw.sql
build_sql: sql/build_table.sql
report_sql: sql/report_view.sql
final_view: customer_daily_report
""",
),
),
(
"sql/load_raw.sql",
reference_solution.get(
"load_sql",
"""create or replace view staged_sales_orders as
select
order_id,
customer_id,
cast(order_date as date) as order_date,
quantity,
unit_price
from raw_sales_orders;
""",
),
),
(
"sql/build_table.sql",
reference_solution.get(
"build_sql",
"""create or replace table customer_summary as
select
customer_id,
order_date,
count(*) as order_count,
sum(quantity * unit_price) as gross_revenue_usd
from staged_sales_orders
group by 1, 2;
""",
),
),
(
"sql/report_view.sql",
reference_solution.get(
"report_sql",
"""create or replace view customer_daily_report as
select
customer_id,
order_date,
order_count,
gross_revenue_usd
from customer_summary;
""",
),
),
]
for path, content in simulation_edits:
if path not in edited:
return {"action_type": "edit_file", "parameters": {"path": path, "content": content}}
for validator in observation.available_validators:
if validator not in validators:
return {"action_type": "run_validator", "parameters": {"validator": validator}}
return {
"action_type": "submit_workspace",
"parameters": {
"summary": (
f"Built a DuckDB pipeline from {os.path.basename(source_csv)} and published {final_view_name}."
)
},
}
return {"action_type": "search_workspace", "parameters": {"query": observation.developer_request}}
|