-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathexceptions.py
More file actions
284 lines (210 loc) · 9.33 KB
/
exceptions.py
File metadata and controls
284 lines (210 loc) · 9.33 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
"""
Nexus Exceptions
Custom exceptions for the Nexus Trust Exchange.
These exceptions drive the viral loop by directing unverified agents to register.
"""
from typing import Optional
class NexusError(Exception):
"""Base exception for all Nexus errors."""
def __init__(self, message: str, code: Optional[str] = None):
self.message = message
self.code = code or "NEXUS_ERROR"
super().__init__(f"[{self.code}] {message}")
class IATPUnverifiedPeerException(NexusError):
"""
Raised when a peer agent is not registered on Nexus.
This is THE viral mechanism - the error message includes a registration URL,
driving unverified agents to register on Nexus to communicate.
"""
REGISTRATION_BASE_URL = "https://nexus.agent-os.dev/register"
def __init__(self, peer_id: str, peer_address: Optional[str] = None):
self.peer_id = peer_id
self.peer_address = peer_address
self.registration_url = f"{self.REGISTRATION_BASE_URL}?agent={peer_id}"
message = (
f"Agent identity unknown: '{peer_id}' is not registered on Nexus. "
f"To establish an IATP handshake, the agent must register at: {self.registration_url}"
)
super().__init__(message, code="IATP_UNVERIFIED_PEER")
def to_iatp_error(self) -> dict:
"""Convert to IATP error response format."""
return {
"error": "IATP_UNVERIFIED_PEER",
"message": self.message,
"peer_id": self.peer_id,
"registration_url": self.registration_url,
"action_required": "Register the agent on Nexus to enable communication",
}
class IATPInsufficientTrustException(NexusError):
"""
Raised when a peer's trust score is below the required threshold.
Drives agents to improve their reputation through successful task completion.
"""
def __init__(
self,
peer_did: str,
current_score: int,
required_score: int,
improvement_url: Optional[str] = None
):
self.peer_did = peer_did
self.current_score = current_score
self.required_score = required_score
self.improvement_url = improvement_url or f"https://nexus.agent-os.dev/reputation/{peer_did}"
self.score_gap = required_score - current_score
message = (
f"Insufficient trust score for agent '{peer_did}': "
f"current={current_score}, required={required_score} (gap: {self.score_gap}). "
f"Improve reputation at: {self.improvement_url}"
)
super().__init__(message, code="IATP_INSUFFICIENT_TRUST")
def to_iatp_error(self) -> dict:
"""Convert to IATP error response format."""
return {
"error": "IATP_INSUFFICIENT_TRUST",
"message": self.message,
"peer_did": self.peer_did,
"current_score": self.current_score,
"required_score": self.required_score,
"score_gap": self.score_gap,
"improvement_url": self.improvement_url,
"action_required": "Complete tasks successfully to improve reputation",
}
class IATPAttestationExpiredException(NexusError):
"""Raised when a peer's attestation has expired."""
def __init__(self, peer_did: str, expired_at: str):
self.peer_did = peer_did
self.expired_at = expired_at
message = (
f"Attestation expired for agent '{peer_did}' at {expired_at}. "
f"Agent must renew attestation with Control Plane."
)
super().__init__(message, code="IATP_ATTESTATION_EXPIRED")
class IATPPolicyViolationException(NexusError):
"""Raised when a peer's policies don't meet requirements."""
def __init__(self, peer_did: str, violation: str, required_policy: str):
self.peer_did = peer_did
self.violation = violation
self.required_policy = required_policy
message = (
f"Policy violation for agent '{peer_did}': {violation}. "
f"Required policy: {required_policy}"
)
super().__init__(message, code="IATP_POLICY_VIOLATION")
class EscrowError(NexusError):
"""Base exception for escrow-related errors."""
def __init__(self, message: str, escrow_id: Optional[str] = None):
self.escrow_id = escrow_id
super().__init__(message, code="ESCROW_ERROR")
class EscrowNotFoundError(EscrowError):
"""Raised when an escrow cannot be found."""
def __init__(self, escrow_id: str):
super().__init__(f"Escrow not found: {escrow_id}", escrow_id=escrow_id)
self.code = "ESCROW_NOT_FOUND"
class EscrowExpiredError(EscrowError):
"""Raised when attempting to operate on an expired escrow."""
def __init__(self, escrow_id: str, expired_at: str):
self.expired_at = expired_at
super().__init__(
f"Escrow {escrow_id} expired at {expired_at}",
escrow_id=escrow_id
)
self.code = "ESCROW_EXPIRED"
class EscrowAlreadyResolvedError(EscrowError):
"""Raised when attempting to modify an already-resolved escrow."""
def __init__(self, escrow_id: str, resolution_status: str):
self.resolution_status = resolution_status
super().__init__(
f"Escrow {escrow_id} already resolved with status: {resolution_status}",
escrow_id=escrow_id
)
self.code = "ESCROW_ALREADY_RESOLVED"
class InsufficientCreditsError(EscrowError):
"""Raised when an agent doesn't have enough credits for escrow."""
def __init__(self, agent_did: str, required: int, available: int):
self.agent_did = agent_did
self.required = required
self.available = available
super().__init__(
f"Agent {agent_did} has insufficient credits: required={required}, available={available}"
)
self.code = "INSUFFICIENT_CREDITS"
class DisputeError(NexusError):
"""Base exception for dispute-related errors."""
def __init__(self, message: str, dispute_id: Optional[str] = None):
self.dispute_id = dispute_id
super().__init__(message, code="DISPUTE_ERROR")
class DisputeNotFoundError(DisputeError):
"""Raised when a dispute cannot be found."""
def __init__(self, dispute_id: str):
super().__init__(f"Dispute not found: {dispute_id}", dispute_id=dispute_id)
self.code = "DISPUTE_NOT_FOUND"
class DisputeAlreadyResolvedError(DisputeError):
"""Raised when attempting to modify a resolved dispute."""
def __init__(self, dispute_id: str):
super().__init__(
f"Dispute {dispute_id} has already been resolved",
dispute_id=dispute_id
)
self.code = "DISPUTE_ALREADY_RESOLVED"
class DisputeEvidenceError(DisputeError):
"""Raised when there's an issue with dispute evidence."""
def __init__(self, dispute_id: str, issue: str):
self.issue = issue
super().__init__(
f"Evidence issue for dispute {dispute_id}: {issue}",
dispute_id=dispute_id
)
self.code = "DISPUTE_EVIDENCE_ERROR"
class RegistryError(NexusError):
"""Base exception for registry-related errors."""
def __init__(self, message: str, agent_did: Optional[str] = None):
self.agent_did = agent_did
super().__init__(message, code="REGISTRY_ERROR")
class AgentAlreadyRegisteredError(RegistryError):
"""Raised when attempting to register an already-registered agent."""
def __init__(self, agent_did: str):
super().__init__(
f"Agent already registered: {agent_did}",
agent_did=agent_did
)
self.code = "AGENT_ALREADY_REGISTERED"
class AgentNotFoundError(RegistryError):
"""Raised when an agent cannot be found in the registry."""
def __init__(self, agent_did: str):
super().__init__(
f"Agent not found in registry: {agent_did}",
agent_did=agent_did
)
self.code = "AGENT_NOT_FOUND"
class InvalidManifestError(RegistryError):
"""Raised when an agent manifest is invalid."""
def __init__(self, agent_did: str, validation_errors: list[str]):
self.validation_errors = validation_errors
super().__init__(
f"Invalid manifest for {agent_did}: {', '.join(validation_errors)}",
agent_did=agent_did
)
self.code = "INVALID_MANIFEST"
class DMZError(NexusError):
"""Base exception for DMZ protocol errors."""
def __init__(self, message: str):
super().__init__(message, code="DMZ_ERROR")
class PolicyNotSignedError(DMZError):
"""Raised when data handling policy hasn't been signed."""
def __init__(self, agent_did: str, policy_hash: str):
self.agent_did = agent_did
self.policy_hash = policy_hash
super().__init__(
f"Agent {agent_did} has not signed policy {policy_hash}"
)
self.code = "POLICY_NOT_SIGNED"
class DataClassificationError(DMZError):
"""Raised when data classification prevents operation."""
def __init__(self, classification: str, reason: str):
self.classification = classification
self.reason = reason
super().__init__(
f"Data classification '{classification}' blocked: {reason}"
)
self.code = "DATA_CLASSIFICATION_ERROR"