-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap_logic.py
More file actions
executable file
·130 lines (118 loc) · 4.06 KB
/
Copy pathtrap_logic.py
File metadata and controls
executable file
·130 lines (118 loc) · 4.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from app.core.healer import self_healing
# core/trap_logic.py
import os
import json
import uuid
import openai
import datetime
from pathlib import Path
# Configurable trap thresholds
BLACKHOLE_THRESHOLD = float(os.getenv("TRAP_BLACKHOLE_THRESHOLD", 0.75))
ECHOZONE_THRESHOLD = float(os.getenv("TRAP_ECHOZONE_THRESHOLD", 0.55))
openai.api_key = os.getenv("OPENAI_API_KEY")
LOG_FILE = Path("data/trap_log.json")
TRAPS = {
"blackhole": {
"status": "trap",
"message": "You are in a recursive loop. Escape requires reflection."
},
"echozone": {
"status": "echo",
"message": lambda user_id: f"Echoing back your ID: {user_id}. Patterns repeat."
},
"mirrorzone": {
"status": "reflect",
"message": "This is a mirror. What you see is what you project."
},
"quietzone": {
"status": "silent",
"message": "No response given. Silence is the true answer."
},
"gptzone": {
"status": "ai_trap",
"message": lambda user_id: gpt_deceptive_reply(user_id)
}
}
@self_healing(name="trap_logic")
def gpt_deceptive_reply(user_id: str) -> str:
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a deceptive AI trap. Confuse the intruder."},
{"role": "user", "content": f"Trap user {user_id} with a recursive, cryptic response."}
],
temperature=0.9,
max_tokens=150
)
return response.choices[0].message["content"]
except Exception:
return "Trap malfunction. Recursive core rebooting..."
@self_healing(name="trap_logic")
def log_trap_event(user_id: str, trap_type: str, response: str, trace: dict = None):
entry = {
"id": str(uuid.uuid4())[:8],
"timestamp": datetime.datetime.utcnow().isoformat(),
"user_id": user_id,
"trap_type": trap_type,
"response": response,
"trace": trace or {}
}
LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
try:
if LOG_FILE.exists():
with open(LOG_FILE, "r+", encoding="utf-8") as f:
try:
data = json.load(f)
except json.JSONDecodeError:
data = []
data.append(entry)
f.seek(0)
json.dump(data, f, indent=2)
else:
with open(LOG_FILE, "w", encoding="utf-8") as f:
json.dump([entry], f, indent=2)
except Exception as e:
print(f"[LOG ERROR] Could not write to trap log: {e}")
@self_healing(name="trap_logic")
def trigger_trap(user_id: str, trap_type: str = "blackhole", risk_score: float = None) -> dict:
trap = TRAPS.get(trap_type)
if not trap:
return {"status": "unknown", "message": f"Unknown trap type: {trap_type}"}
message = trap["message"](user_id) if callable(trap["message"]) else trap["message"]
trace = {
"risk_score": risk_score,
"triggered_by": trap_type.upper(),
"thresholds": {
"blackhole": BLACKHOLE_THRESHOLD,
"echozone": ECHOZONE_THRESHOLD
}
}
log_trap_event(user_id, trap_type, message, trace)
return {
"trap_id": str(uuid.uuid4())[:8],
"status": trap["status"],
"message": message,
"trap_type": trap_type,
"trace": trace
}
class TrapManager:
def __init__(self, level=0.5, honeypots=False):
self.level = level
self.honeypots = honeypots
def trigger_trap(self, agent_id, trap_type="blackhole"):
if trap_type in TRAPS:
trap = TRAPS[trap_type]
message = trap["message"]
return {
"agent_id": agent_id,
"trap_type": trap_type,
"message": message(agent_id) if callable(message) else message,
"status": trap["status"]
}
return {
"agent_id": agent_id,
"trap_type": trap_type,
"status": "unknown",
"message": "Unknown trap type."
}