Skip to content

Commit da5b83a

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

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

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

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
8989
rewritten_tools: list[str] = []
9090
escalated_tools: list[str] = []
9191

92+
phase = "plan"
93+
9294
try:
9395
if (time.monotonic() - started) > BUDGET.max_seconds:
9496
return stopped("max_seconds", phase="plan")
9597

9698
raw_plan = propose_action_plan(goal=goal, request=request)
9799
actions = validate_plan(raw_plan.get("actions"), max_actions=BUDGET.max_actions)
100+
phase = "execute"
98101

99102
for idx, action in enumerate(actions, start=1):
100103
if (time.monotonic() - started) > BUDGET.max_seconds:
@@ -156,6 +159,8 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
156159
if tool_fn is None:
157160
return stopped(f"tool_unmapped:{tool_name}", phase="execute")
158161

162+
# Keep the decision in trace even if execution fails later.
163+
trace.append(trace_item)
159164
observation = gateway.dispatch(
160165
tool_name=tool_name,
161166
tool_fn=tool_fn,
@@ -168,7 +173,6 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
168173

169174
trace_item["executed_from"] = executed_from
170175
trace_item["ok"] = True
171-
trace.append(trace_item)
172176

173177
history_item["executed_action"] = executed_action
174178
history_item["executed_from"] = executed_from
@@ -225,7 +229,7 @@ def stopped(stop_reason: str, *, phase: str, **extra: Any) -> dict[str, Any]:
225229
"history": history,
226230
}
227231
except StopRun as exc:
228-
return stopped(exc.reason, phase="execute")
232+
return stopped(exc.reason, phase=phase)
229233
finally:
230234
gateway.close()
231235

0 commit comments

Comments
 (0)