-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcls_blockchain.py
More file actions
320 lines (266 loc) · 12.1 KB
/
cls_blockchain.py
File metadata and controls
320 lines (266 loc) · 12.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
cls_blockchain.py
Blockchain Integrity System for Multi-Agent Chat
Ensures conversation logs cannot be tampered with without detection.
By Juan B. Gutiérrez, Professor of Mathematics
University of Texas at San Antonio.
License: Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
"""
import hashlib
import json
import time
from datetime import datetime
from typing import Dict, List, Optional, Tuple
class ConversationBlockchain:
"""
Implements blockchain-style integrity checking for conversation logs.
Each message becomes a block in the chain with cryptographic verification.
"""
def __init__(self, agent_name: str, salt: Optional[str] = None, global_salt: Optional[str] = None, genesis_hash: Optional[str] = None):
self.agent_name = agent_name
# Use global salt if provided, otherwise use provided salt, otherwise generate new
if global_salt:
self.salt = global_salt
elif salt:
self.salt = salt
else:
self.salt = self._generate_salt()
# Use provided genesis hash if available, otherwise create new one
if genesis_hash:
self.genesis_hash = genesis_hash
else:
self.genesis_hash = self._create_genesis_hash()
def _generate_salt(self) -> str:
"""Generate a unique salt for this agent's blockchain"""
timestamp = str(time.time())
content = f"{self.agent_name}_{timestamp}"
return hashlib.sha256(content.encode()).hexdigest()[:16]
def _create_genesis_hash(self) -> str:
"""Create the genesis block hash"""
genesis_data = {
"agent": self.agent_name,
"genesis": True,
"salt": self.salt,
"timestamp": "2025-07-19T00:00:00.000000" # Fixed timestamp for consistent genesis
}
return self._compute_hash(json.dumps(genesis_data, sort_keys=True))
def _compute_hash(self, data: str) -> str:
"""Compute SHA-256 hash of data"""
return hashlib.sha256((data + self.salt).encode()).hexdigest()
def _create_message_hash(self, role: str, content: str, timestamp: str,
previous_hash: str) -> str:
"""Create hash for a message block - excludes agent name for flexibility"""
message_data = {
"role": role,
"content": content,
"timestamp": timestamp,
"previous_hash": previous_hash
}
return self._compute_hash(json.dumps(message_data, sort_keys=True))
def add_message_to_chain(self, role: str, content: str,
timestamp: str, history: List[Dict]) -> Dict:
"""
Add a new message to the blockchain and return the complete entry
"""
# Get previous hash
if not history:
previous_hash = self.genesis_hash
else:
previous_hash = history[-1].get("blockchain", {}).get("current_hash", self.genesis_hash)
# Create current hash
current_hash = self._create_message_hash(role, content, timestamp, previous_hash)
# Create the complete message entry
message_entry = {
"role": role,
"content": content,
"timestamp": timestamp,
"hash": self._compute_hash(f"{role}_{content}_{timestamp}"), # Content-only hash
"blockchain": {
"current_hash": current_hash,
"previous_hash": previous_hash,
"block_index": len(history),
"integrity_verified": True
}
}
return message_entry
def verify_chain_integrity(self, history: List[Dict]) -> Tuple[bool, List[str]]:
"""
Verify the entire blockchain integrity
Returns (is_valid, list_of_errors)
"""
if not history:
return True, []
errors = []
previous_hash = self.genesis_hash
for i, entry in enumerate(history):
# Check if entry has blockchain data
if "blockchain" not in entry:
errors.append(f"Block {i}: Missing blockchain data")
continue
blockchain_data = entry["blockchain"]
stored_hash = blockchain_data.get("current_hash")
stored_previous = blockchain_data.get("previous_hash")
# Verify previous hash chain
if stored_previous != previous_hash:
errors.append(f"Block {i}: Previous hash mismatch. Expected: {previous_hash[:16]}..., Got: {stored_previous[:16] if stored_previous else 'None'}...")
# Verify current hash
calculated_hash = self._create_message_hash(
entry["role"],
entry["content"],
entry["timestamp"],
stored_previous
)
if stored_hash != calculated_hash:
errors.append(f"Block {i}: Hash verification failed. Content may have been tampered with.")
# Verify simple content hash if present
if "hash" in entry:
expected_content_hash = self._compute_hash(f"{entry['role']}_{entry['content']}_{entry['timestamp']}")
if entry["hash"] != expected_content_hash:
errors.append(f"Block {i}: Content hash mismatch. Message content may have been altered.")
previous_hash = stored_hash
return len(errors) == 0, errors
def rebuild_chain_from_index(self, history: List[Dict], start_index: int) -> List[Dict]:
"""
Rebuild the blockchain from a specific index onward.
Used when user legitimately edits history.
"""
if start_index == 0:
previous_hash = self.genesis_hash
else:
previous_hash = history[start_index - 1]["blockchain"]["current_hash"]
rebuilt_history = history[:start_index].copy()
for i in range(start_index, len(history)):
entry = history[i]
# Rebuild this block
new_entry = self.add_message_to_chain(
entry["role"],
entry["content"],
entry["timestamp"],
rebuilt_history
)
rebuilt_history.append(new_entry)
return rebuilt_history
def get_chain_metadata(self, history: List[Dict]) -> Dict:
"""Get metadata about the blockchain"""
if not history:
return {
"total_blocks": 0,
"genesis_hash": self.genesis_hash,
"last_hash": self.genesis_hash,
"agent": self.agent_name,
"salt": self.salt
}
return {
"total_blocks": len(history),
"genesis_hash": self.genesis_hash,
"last_hash": history[-1]["blockchain"]["current_hash"],
"agent": self.agent_name,
"salt": self.salt,
"integrity_verified": self.verify_chain_integrity(history)[0]
}
class IntegrityManager:
"""
Manages blockchain integrity across all agents in the multi-agent system.
Integrates with the existing cls_foo.py architecture.
"""
def __init__(self, global_salt: Optional[str] = None):
self.blockchains: Dict[str, ConversationBlockchain] = {}
self.global_salt = global_salt
def get_or_create_blockchain(self, agent_name: str,
existing_metadata: Optional[Dict] = None) -> ConversationBlockchain:
"""Get existing blockchain or create new one for agent"""
if agent_name not in self.blockchains:
salt = None
genesis_hash = None
if existing_metadata:
salt = existing_metadata.get("salt")
genesis_hash = existing_metadata.get("genesis_hash")
self.blockchains[agent_name] = ConversationBlockchain(
agent_name,
salt=salt,
global_salt=self.global_salt,
genesis_hash=genesis_hash
)
return self.blockchains[agent_name]
def add_message_with_integrity(self, agent_name: str, role: str,
content: str, timestamp: str,
history: List[Dict]) -> Dict:
"""Add a message with blockchain integrity"""
blockchain = self.get_or_create_blockchain(agent_name)
return blockchain.add_message_to_chain(role, content, timestamp, history)
def verify_agent_integrity(self, agent_name: str,
history: List[Dict]) -> Tuple[bool, List[str]]:
"""Verify integrity for a specific agent"""
blockchain = self.get_or_create_blockchain(agent_name)
return blockchain.verify_chain_integrity(history)
def rebuild_agent_chain(self, agent_name: str, history: List[Dict],
start_index: int) -> List[Dict]:
"""Rebuild chain for agent from specific index"""
blockchain = self.get_or_create_blockchain(agent_name)
return blockchain.rebuild_chain_from_index(history, start_index)
def get_integrity_report(self, agent_name: str,
history: List[Dict]) -> Dict:
"""Get comprehensive integrity report for an agent"""
blockchain = self.get_or_create_blockchain(agent_name)
is_valid, errors = blockchain.verify_chain_integrity(history)
metadata = blockchain.get_chain_metadata(history)
return {
"agent": agent_name,
"integrity_valid": is_valid,
"errors": errors,
"metadata": metadata,
"verification_timestamp": datetime.now().isoformat()
}
def migrate_existing_history(self, agent_name: str,
history: List[Dict]) -> List[Dict]:
"""
Migrate existing history without blockchain data to include integrity checking.
This is for backward compatibility with existing conversation files.
"""
blockchain = self.get_or_create_blockchain(agent_name)
migrated_history = []
for entry in history:
if "blockchain" not in entry:
# Add blockchain data to existing entry
new_entry = blockchain.add_message_to_chain(
entry["role"],
entry["content"],
entry.get("timestamp", datetime.now().isoformat()),
migrated_history
)
migrated_history.append(new_entry)
else:
# Entry already has blockchain data
migrated_history.append(entry)
return migrated_history
# Example usage and testing
if __name__ == "__main__":
# Test the blockchain integrity system
integrity_manager = IntegrityManager()
# Simulate adding messages
history = []
# Add first message
message1 = integrity_manager.add_message_with_integrity(
"TestAgent", "user", "Hello", "2025-07-19T10:00:00", history
)
history.append(message1)
print("Added message 1:", json.dumps(message1, indent=2))
# Add second message
message2 = integrity_manager.add_message_with_integrity(
"TestAgent", "assistant", "Hello! How can I help?", "2025-07-19T10:00:01", history
)
history.append(message2)
# Verify integrity
is_valid, errors = integrity_manager.verify_agent_integrity("TestAgent", history)
print(f"\nIntegrity check: {is_valid}")
if errors:
print("Errors:", errors)
# Simulate tampering
print("\n--- Simulating tampering ---")
history[0]["content"] = "Hello TAMPERED"
is_valid, errors = integrity_manager.verify_agent_integrity("TestAgent", history)
print(f"Integrity check after tampering: {is_valid}")
print("Errors:", errors)
# Get integrity report
report = integrity_manager.get_integrity_report("TestAgent", history)
print("\nIntegrity Report:", json.dumps(report, indent=2))