Spaces:
Sleeping
Sleeping
Download baseline.py from phoenixarg/flowOSsimple: direct link, hf CLI and curl.
- Browser
- Download file 46.3 kB
-
https://huggingface.co/spaces/phoenixarg/flowOSsimple/resolve/main/baseline.py
- Command line
-
hf download hf://spaces/phoenixarg/flowOSsimple/baseline.py
-
curl -L -o baseline.py https://huggingface.co/spaces/phoenixarg/flowOSsimple/resolve/main/baseline.py
46.3 kB
"""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}}
"""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]]: