"""Baseline policy helpers for the Developer Control Room benchmark.""" from __future__ import annotations import json import os import re 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 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" def debug_log(message: str) -> None: if DEBUG_LOGGING: print(message, file=sys.stderr, flush=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: start = text.find("{") end = text.rfind("}") + 1 if start != -1 and end > start: try: return json.loads(text[start:end]) except json.JSONDecodeError: return None return None 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" 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"} Return the next JSON action only. """ ).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) 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() return parse_model_action(text) except Exception as exc: debug_log(f"[DEBUG] Model request failed: {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) 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) if action_type == "inspect_schema": asset = (params.get("asset") or "").strip() return bool(asset) and asset in set(observation.known_assets) if action_type == "inspect_lineage": asset = (params.get("asset") or "").strip() return bool(asset) and asset in set(observation.known_assets) 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 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": return has_edit if action_type == "submit_workspace": return has_edit if action_type == "submit_review": return "primary" in queried.get("inspect_llm_draft", {}) 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"]}} return {"action_type": "search_workspace", "parameters": {"query": observation.developer_request}}