Skip to content

Commit 2f13e1f

Browse files
author
AgentPatterns
committed
feat(examples/python): add code-execution-agent runnable example
1 parent e42a8b6 commit 2f13e1f

6 files changed

Lines changed: 744 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Code-Execution Agent (Python)
2+
3+
Production-style runnable example of a code-execution agent with:
4+
5+
- plan/generate code step
6+
- policy checks before execution
7+
- sandboxed process execution with timeout/output limits
8+
- output schema validation
9+
- trace/history for audit
10+
11+
Notes:
12+
13+
- Policy checks here include heuristic guards (for example, suspicious attribute names and URL literals).
14+
- This example uses a separate subprocess boundary; it is not a full security sandbox.
15+
16+
## Run
17+
18+
```bash
19+
python main.py
20+
```
21+
22+
No external dependencies are required.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
6+
def build_request(*, report_date: str, region: str, incident_id: str) -> dict[str, Any]:
7+
transactions: list[dict[str, Any]] = []
8+
for idx in range(60):
9+
is_failed = idx in {7, 34}
10+
transactions.append(
11+
{
12+
"transaction_id": f"txn_{idx + 1:03d}",
13+
"status": "failed" if is_failed else "paid",
14+
"chargeback": idx == 34,
15+
"latency_ms": 150 + (idx % 40) + (25 if is_failed else 0),
16+
}
17+
)
18+
19+
return {
20+
"request": {
21+
"report_date": report_date,
22+
"region": region.upper(),
23+
"incident_id": incident_id,
24+
"transactions": transactions,
25+
},
26+
"policy_hints": {
27+
"allowed_languages": ["python", "javascript"],
28+
"max_code_chars": 2400,
29+
"exec_timeout_seconds": 2.0,
30+
"network_access": "denied",
31+
},
32+
}

0 commit comments

Comments
 (0)