This repository was archived by the owner on Sep 21, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (57 loc) · 2.55 KB
/
Copy pathmain.py
File metadata and controls
77 lines (57 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""CodeRifts enforcement with LangGraph — abort on BLOCK."""
import requests
CODERIFTS_URL = "https://app.coderifts.com/api/v1/public/preflight"
def coderifts_preflight(spec_url: str) -> dict:
"""Call CodeRifts public preflight."""
resp = requests.get(f"{CODERIFTS_URL}?url={spec_url}")
return resp.json()
def coderifts_guard(state: dict) -> dict:
"""LangGraph node: check API safety before tool execution."""
spec_url = state.get("spec_url", "")
decision = coderifts_preflight(spec_url)
state["coderifts_decision"] = decision["decision"]
state["coderifts_risk_score"] = decision.get("risk_score", 0)
state["safe_for_agent"] = decision.get("safe_for_agent", True)
print(f"[CODERIFTS] decision={decision['decision']} risk={decision.get('risk_score')}")
return state
def should_continue(state: dict) -> str:
"""LangGraph conditional edge: route to END if BLOCK."""
if state.get("coderifts_decision") == "BLOCK":
print("[LANGGRAPH] Routing to END — CodeRifts BLOCK")
return "END"
if state.get("coderifts_decision") == "PENDING":
print("[LANGGRAPH] Routing to RETRY — CodeRifts PENDING")
return "retry"
return "execute"
def execute_tool(state: dict) -> dict:
"""LangGraph node: execute the API call."""
print(f"[LANGGRAPH] Executing API call to {state.get('spec_url')}")
state["result"] = "success"
return state
# ── LangGraph graph definition ───────────────────────────────────────────
# This is a simplified representation. In production, use:
# from langgraph.graph import StateGraph
# graph = StateGraph(AgentState)
# graph.add_node("guard", coderifts_guard)
# graph.add_node("execute", execute_tool)
# graph.add_conditional_edges("guard", should_continue, {"END": END, "execute": "execute"})
def run_graph(spec_url: str):
"""Simulate the LangGraph execution loop."""
state = {"spec_url": spec_url}
# Step 1: CodeRifts guard
state = coderifts_guard(state)
# Step 2: Conditional routing
route = should_continue(state)
if route == "END":
print("[RESULT] Workflow aborted by CodeRifts")
return state
if route == "retry":
import time
time.sleep(5)
return run_graph(spec_url)
# Step 3: Execute tool
state = execute_tool(state)
print(f"[RESULT] Workflow completed: {state.get('result')}")
return state
if __name__ == "__main__":
run_graph("https://petstore3.swagger.io/api/v3/openapi.json")