@@ -183,7 +183,7 @@ def _prepare_data_redacted_error(
183183 * ,
184184 trusted_error_message : str | None = None ,
185185) -> BaseException :
186- """Detach payload-owned state and return a safe error for a public boundary ."""
186+ """Build a safe replacement and detach only the source's retry traceback ."""
187187 error_type = type (error )
188188 process_control_error = _replace_data_redacted_process_control_error (error )
189189 if process_control_error is not None :
@@ -199,7 +199,7 @@ def _prepare_data_redacted_error(
199199 except BaseException :
200200 pass
201201
202- _discard_exception_graph (error )
202+ _detach_source_exception_traceback (error )
203203
204204 safe_error : BaseException = RuntimeError (_DATA_REDACTED_ERROR_MESSAGE )
205205 if error_type is ModelBehaviorError :
@@ -281,133 +281,32 @@ def _copy_data_redacted_process_control_error(
281281def _replace_data_redacted_process_control_error (
282282 error : BaseException ,
283283) -> BaseException | None :
284- """Discard a process-control source and return a fresh value-free replacement."""
284+ """Detach the source traceback and return a fresh process-control replacement."""
285285 safe_error = _copy_data_redacted_process_control_error (error )
286286 if safe_error is None :
287287 return None
288- _discard_exception_graph (error )
288+ _detach_source_exception_traceback (error )
289289 return safe_error
290290
291291
292- def _collect_nested_exceptions (value : object , linked : list [BaseException ]) -> None :
293- """Collect exceptions reachable through exact built-in containers without callbacks."""
294- pending = [value ]
295- seen : set [int ] = set ()
296- while pending :
297- current = pending .pop ()
298- if id (current ) in seen :
299- continue
300- seen .add (id (current ))
301- if issubclass (type (current ), BaseException ):
302- linked .append (cast (BaseException , current ))
303- elif type (current ) is dict :
304- for key , item in dict .items (current ):
305- pending .extend ((key , item ))
306- elif (
307- type (current ) is list
308- or type (current ) is tuple
309- or type (current ) is set
310- or type (current ) is frozenset
311- ):
312- pending .extend (current )
313-
292+ def _detach_source_exception_traceback (error : BaseException ) -> None :
293+ """Bound retry tracebacks without clearing caller frames or linked exceptions.
314294
315- def _discard_exception_graph (error : BaseException ) -> None :
316- """Best-effort clear discoverable state from an exception and its linked graph.
317-
318- Exact built-in descriptors avoid provider-controlled attribute, descriptor, and
319- metaclass callbacks. Read-only storage such as an exception group's message
320- cannot be changed in place, so public boundaries must return a fresh error that
321- does not retain the source exception.
295+ Re-raising a cached failure prepends new traceback nodes. Drop only the caught
296+ root's traceback; its fields and linked exception state remain caller-owned.
297+ Clearing frames can close suspended caller coroutines on Python before 3.13.
322298 """
323- pending = [error ]
324- seen : set [int ] = set ()
325- while pending :
326- current = pending .pop ()
327- if id (current ) in seen :
328- continue
329- seen .add (id (current ))
330-
331- linked : list [BaseException ] = []
332- group_exceptions : tuple [BaseException , ...] | None = None
333- current_type = type (current )
334- if issubclass (current_type , BaseExceptionGroup ):
335- group_exceptions = _base_exception_group_exceptions (current )
336- if group_exceptions is not None :
337- linked .extend (group_exceptions )
338- for descriptor in (
339- cast (Any , BaseException .__cause__ ),
340- cast (Any , BaseException .__context__ ),
341- ):
342- try :
343- candidate = descriptor .__get__ (current , current_type )
344- except BaseException :
345- continue
346- if issubclass (type (candidate ), BaseException ):
347- linked .append (cast (BaseException , candidate ))
348-
349- try :
350- args = cast (Any , BaseException .args ).__get__ (current , current_type )
351- _collect_nested_exceptions (args , linked )
352- except BaseException :
353- pass
354-
355- try :
356- source_traceback = cast (Any , BaseException .__traceback__ ).__get__ (current , current_type )
357- except BaseException :
358- source_traceback = None
359- if source_traceback is not None :
360- try :
361- traceback .clear_frames (source_traceback )
362- except BaseException :
363- pass
364-
365- state = _base_exception_instance_dict (current )
366- if state is not None :
367- _collect_nested_exceptions (state , linked )
368- state .clear ()
369-
370- current_metadata = _static_type_metadata (current_type )
371- if current_metadata is not None :
372- _ , current_mro = current_metadata
373- for base in current_mro :
374- base_metadata = _static_type_metadata (base )
375- if base_metadata is None :
376- continue
377- namespace , _ = base_metadata
378- for descriptor in namespace .values ():
379- if type (descriptor ) is not types .MemberDescriptorType :
380- continue
381- try :
382- value = descriptor .__get__ (current , current_type )
383- _collect_nested_exceptions (value , linked )
384- except BaseException :
385- pass
386- try :
387- descriptor .__delete__ (current )
388- except BaseException :
389- pass
390-
391- if group_exceptions is None :
392- safe_args : tuple [object , ...] = ()
393- else :
394- safe_args = (_DATA_REDACTED_ERROR_MESSAGE , group_exceptions )
395- for descriptor , value in (
396- (cast (Any , BaseException .args ), safe_args ),
397- (cast (Any , BaseException .__traceback__ ), None ),
398- (cast (Any , BaseException .__cause__ ), None ),
399- (cast (Any , BaseException .__context__ ), None ),
400- ):
401- try :
402- descriptor .__set__ (current , value )
403- except BaseException :
404- pass
405- pending .extend (linked )
299+ cast (Any , BaseException .__traceback__ ).__set__ (error , None )
406300
407301
408302def _raise_data_redacted_error (error : BaseException ) -> NoReturn :
409303 """Raise a detached redacted error from a frame that owns no payload data."""
410- raise error from None
304+ try :
305+ raise error from None
306+ finally :
307+ # Python attaches the caller's active exception even with ``from None``.
308+ # Detach that link on our replacement, never on the caller's exception.
309+ cast (Any , BaseException .__context__ ).__set__ (error , None )
411310
412311
413312@dataclass
0 commit comments