|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | + |
| 6 | +def propose_code_execution_plan(*, goal: str, request: dict[str, Any]) -> dict[str, Any]: |
| 7 | + req = request["request"] |
| 8 | + del goal |
| 9 | + |
| 10 | + code = """ |
| 11 | +import json |
| 12 | +import statistics |
| 13 | +
|
| 14 | +payload = json.loads(input()) |
| 15 | +rows = payload["transactions"] |
| 16 | +
|
| 17 | +total = len(rows) |
| 18 | +failed = sum(1 for row in rows if row["status"] != "paid") |
| 19 | +chargeback_alerts = sum(1 for row in rows if row.get("chargeback") is True) |
| 20 | +failed_rate = (failed / total) if total else 0.0 |
| 21 | +
|
| 22 | +latencies = [float(row["latency_ms"]) for row in rows] |
| 23 | +avg_latency = statistics.fmean(latencies) if latencies else 0.0 |
| 24 | +
|
| 25 | +if latencies: |
| 26 | + sorted_latencies = sorted(latencies) |
| 27 | + p95_idx = int(round((len(sorted_latencies) - 1) * 0.95)) |
| 28 | + p95_latency = sorted_latencies[p95_idx] |
| 29 | +else: |
| 30 | + p95_latency = 0.0 |
| 31 | +
|
| 32 | +severity = "P1" if failed_rate >= 0.03 else "P2" |
| 33 | +eta_minutes = 45 if severity == "P1" else 20 |
| 34 | +
|
| 35 | +print( |
| 36 | + json.dumps( |
| 37 | + { |
| 38 | + "failed_payment_rate": failed_rate, |
| 39 | + "chargeback_alerts": chargeback_alerts, |
| 40 | + "incident_severity": severity, |
| 41 | + "eta_minutes": eta_minutes, |
| 42 | + "affected_checkout_share": failed_rate, |
| 43 | + "avg_latency_ms": avg_latency, |
| 44 | + "p95_latency_ms": p95_latency, |
| 45 | + "sample_size": total, |
| 46 | + "incident_id": payload["incident_id"], |
| 47 | + "region": payload["region"], |
| 48 | + }, |
| 49 | + separators=(",", ":"), |
| 50 | + ) |
| 51 | +) |
| 52 | +""".strip() |
| 53 | + |
| 54 | + return { |
| 55 | + "action": { |
| 56 | + "id": "c1", |
| 57 | + "language": "python", |
| 58 | + "entrypoint": "main.py", |
| 59 | + "code": code, |
| 60 | + "input_payload": { |
| 61 | + "incident_id": req["incident_id"], |
| 62 | + "region": req["region"], |
| 63 | + "transactions": req["transactions"], |
| 64 | + }, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +def compose_final_answer( |
| 70 | + *, |
| 71 | + request: dict[str, Any], |
| 72 | + aggregate: dict[str, Any], |
| 73 | + execution_summary: dict[str, Any], |
| 74 | +) -> str: |
| 75 | + req = request["request"] |
| 76 | + metrics = aggregate["metrics"] |
| 77 | + |
| 78 | + return ( |
| 79 | + f"Code execution brief ({req['region']}, {req['report_date']}): incident {req['incident_id']} is " |
| 80 | + f"{metrics['incident_severity']} with failed payments at {metrics['failed_payment_rate_pct']}% and " |
| 81 | + f"{metrics['chargeback_alerts']} chargeback alerts. Affected checkout share is " |
| 82 | + f"{metrics['affected_checkout_share_pct']}%, average latency is {metrics['avg_latency_ms']} ms " |
| 83 | + f"(p95 {metrics['p95_latency_ms']} ms), and ETA is ~{metrics['eta_minutes']} minutes. " |
| 84 | + f"Executed in a separate subprocess boundary (best-effort, not a security sandbox) " |
| 85 | + f"({execution_summary['exec_ms']} ms, {execution_summary['stdout_bytes']} stdout bytes, " |
| 86 | + f"{execution_summary['stderr_bytes']} stderr bytes)." |
| 87 | + ) |
0 commit comments