-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathinterrupt.py
More file actions
105 lines (81 loc) · 3.03 KB
/
interrupt.py
File metadata and controls
105 lines (81 loc) · 3.03 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
"""
Interrupt Controller - Cooperative cancellation for agent runs.
Provides thread-safe, cooperative cancellation mechanism for long-running agent
operations. Follows protocol-driven design with zero overhead when not used.
"""
import threading
from typing import Optional, Protocol
from dataclasses import dataclass, field
__all__ = ["InterruptControllerProtocol", "InterruptController"]
class InterruptControllerProtocol(Protocol):
"""Protocol for interrupt controller extension point."""
def request(self, reason: str = "user") -> None:
"""Request cancellation with optional reason."""
...
def clear(self) -> None:
"""Clear interrupt state."""
...
def is_set(self) -> bool:
"""Check if interrupt was requested."""
...
@property
def reason(self) -> Optional[str]:
"""Get interrupt reason if set."""
...
def check(self) -> None:
"""Check for interrupt and raise if set."""
...
@dataclass
class InterruptController:
"""Thread-safe cooperative cancellation for agent runs.
Provides a lightweight mechanism for requesting cancellation of agent
operations. Uses threading.Event for thread safety and cooperative
checking patterns.
Examples:
Basic usage:
>>> controller = InterruptController()
>>> # In another thread:
>>> controller.request("user_cancel")
>>> # In agent loop:
>>> if controller.is_set():
>>> return f"Cancelled: {controller.reason}"
"""
_flag: threading.Event = field(default_factory=threading.Event, init=False, repr=False)
_reason: Optional[str] = field(default=None, init=False)
_lock: threading.Lock = field(default_factory=threading.Lock, init=False, repr=False)
def request(self, reason: str = "user") -> None:
"""Request cancellation with a reason.
Args:
reason: Human-readable reason for cancellation
"""
with self._lock:
if not self._flag.is_set():
self._reason = reason
self._flag.set()
def clear(self) -> None:
"""Clear the cancellation request."""
with self._lock:
self._reason = None
self._flag.clear()
def is_set(self) -> bool:
"""Check if cancellation has been requested.
Returns:
True if cancellation was requested
"""
return self._flag.is_set()
@property
def reason(self) -> Optional[str]:
"""Get the reason for cancellation.
Returns:
Reason string if cancelled, None otherwise
"""
with self._lock:
return self._reason
def check(self) -> None:
"""Check for cancellation and raise if requested.
Raises:
InterruptedError: If cancellation was requested
"""
if self.is_set():
reason = self.reason or "unknown"
raise InterruptedError(f"Operation cancelled: {reason}")