Skip to content

Commit c8ed3d5

Browse files
author
AgentPatterns
committed
feat(examples/python): add guarded-policy-agent runnable example
1 parent e8bd5dd commit c8ed3d5

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

  • examples/agent-patterns/guarded-policy-agent/python

examples/agent-patterns/guarded-policy-agent/python/main.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
from agent import compose_final_answer, propose_action_plan
99
from context import build_request
1010
from gateway import Budget, PolicyGateway, StopRun, validate_plan
11-
from tools import export_customer_data, fetch_incident_snapshot, send_status_update
11+
from tools import (
12+
create_manual_review_ticket,
13+
export_customer_data,
14+
fetch_incident_snapshot,
15+
send_status_update,
16+
)
1217

1318
GOAL = (
1419
"Prepare a customer-safe operations update for a US payments incident. "
@@ -31,16 +36,19 @@
3136
"fetch_incident_snapshot",
3237
"send_status_update",
3338
"export_customer_data",
39+
"create_manual_review_ticket",
3440
}
3541
ALLOWED_TOOLS_EXECUTION = {
3642
"fetch_incident_snapshot",
3743
"send_status_update",
44+
"create_manual_review_ticket",
3845
}
3946

4047
TOOLS: dict[str, Any] = {
4148
"fetch_incident_snapshot": fetch_incident_snapshot,
4249
"send_status_update": send_status_update,
4350
"export_customer_data": export_customer_data,
51+
"create_manual_review_ticket": create_manual_review_ticket,
4452
}
4553

4654

@@ -124,14 +132,20 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
124132
executed_from = "policy_rewrite"
125133
elif decision.kind == "escalate":
126134
escalated_tools.append(action["tool"])
135+
escalation_ticket = gateway.dispatch(
136+
tool_name="create_manual_review_ticket",
137+
tool_fn=TOOLS["create_manual_review_ticket"],
138+
args={"reason": decision.reason, "payload": (decision.enforced_action or action)},
139+
)
140+
history_item["escalation_ticket"] = escalation_ticket
127141
human = simulate_human_approval(
128142
action=(decision.enforced_action or action),
129143
reason=decision.reason,
130144
)
131145
if not human["approved"]:
132146
return stopped("policy_escalation_rejected", phase="execute")
133147
executed_action = human["action"]
134-
executed_from = "human_revised"
148+
executed_from = "human_approved"
135149
history_item["human"] = {
136150
"approved": True,
137151
"reason": human["reason"],
@@ -199,7 +213,12 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
199213
"stop_reason": "success",
200214
"outcome": "policy_guarded_success",
201215
"answer": answer,
202-
"plan": actions,
216+
"proposed_plan": actions,
217+
"executed_plan": [
218+
step["executed_action"]
219+
for step in history
220+
if isinstance(step, dict) and isinstance(step.get("executed_action"), dict)
221+
],
203222
"aggregate": aggregate,
204223
"policy_summary": policy_summary,
205224
"trace": trace,

0 commit comments

Comments
 (0)