holon-sh 0.0.1.dev8__tar.gz → 0.0.1.dev9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/PKG-INFO +1 -1
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/client.py +2 -1
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/events.py +178 -182
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/pyproject.toml +1 -1
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_events.py +59 -28
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/.gitignore +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/LICENSE +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/README.md +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/binding.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/context.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/__init__.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/files/__init__.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/files/_client.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/files/_server.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/ext/files/_wire.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/holon/py.typed +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/scripts/verify_distribution.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/fixtures/caller/requirements.txt +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/fixtures/caller/server.json +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_context.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_elicit.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_files.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_package.py +0 -0
- {holon_sh-0.0.1.dev8 → holon_sh-0.0.1.dev9}/tests/test_runtime.py +0 -0
|
@@ -191,7 +191,8 @@ class _App:
|
|
|
191
191
|
if not name.startswith("_")
|
|
192
192
|
}
|
|
193
193
|
)
|
|
194
|
-
|
|
194
|
+
# Draft extensions, whose interfaces may change with the drafts.
|
|
195
|
+
self.ext = SimpleNamespace(events=AppEvents(self, contract))
|
|
195
196
|
|
|
196
197
|
async def call_tool(
|
|
197
198
|
self,
|
|
@@ -30,7 +30,6 @@ from typing import (
|
|
|
30
30
|
Generic,
|
|
31
31
|
Literal,
|
|
32
32
|
TypeAlias,
|
|
33
|
-
TypedDict,
|
|
34
33
|
TypeVar,
|
|
35
34
|
cast,
|
|
36
35
|
)
|
|
@@ -79,11 +78,11 @@ if TYPE_CHECKING:
|
|
|
79
78
|
__all__ = [
|
|
80
79
|
"EVENTS_EXTENSION",
|
|
81
80
|
"AppEvents",
|
|
82
|
-
"CheckResult",
|
|
83
81
|
"Event",
|
|
82
|
+
"EventResult",
|
|
84
83
|
"EventType",
|
|
85
84
|
"Events",
|
|
86
|
-
"
|
|
85
|
+
"NoCompatibleDeliveryMode",
|
|
87
86
|
"Subscription",
|
|
88
87
|
]
|
|
89
88
|
|
|
@@ -100,22 +99,31 @@ UNSUPPORTED = -32026
|
|
|
100
99
|
_PAGE = 1000
|
|
101
100
|
# Marks a request that came through `Events.asgi`, whose answer can be a stream.
|
|
102
101
|
_STREAMS = "holon.ext.events.streams"
|
|
102
|
+
# What each event type keeps of what it emits, for polls and for streams that are behind or reconnect: the sketch's
|
|
103
|
+
# emit buffer. For how long in seconds, how many events, and how many bytes their data takes as JSON. The newest is
|
|
104
|
+
# kept whatever it takes.
|
|
105
|
+
_KEEP_AGE = 600.0
|
|
106
|
+
_KEEP_EVENTS = 10_000
|
|
107
|
+
_KEEP_BYTES = 4 * 2**20
|
|
108
|
+
# Seconds a poll is told to wait, and how often a stream asks a type's check function.
|
|
109
|
+
_INTERVAL = 1.0
|
|
110
|
+
# Seconds between a stream's heartbeats. One whose reader takes nothing for two is closed, to reconnect from its cursor.
|
|
111
|
+
_HEARTBEAT = 30.0
|
|
112
|
+
# Seconds a poll subscription lasts after its last poll: a minute or three intervals.
|
|
113
|
+
_POLL_SUBSCRIPTION_TTL = max(60.0, 3 * _INTERVAL)
|
|
114
|
+
# The bytes of events every stream together is sending at once, of which one caller's streams may send a quarter.
|
|
115
|
+
_MAX_SENDING_BYTES = 16 * 2**20
|
|
116
|
+
# The subscriptions held at once, streams and poll leases alike, and those of one caller.
|
|
117
|
+
_MAX_SUBSCRIPTIONS = 10_000
|
|
118
|
+
_MAX_SUBSCRIPTIONS_PER_CALLER = 100
|
|
119
|
+
# How many types events/list returns at a time.
|
|
120
|
+
_LIST_PAGE_SIZE = 100
|
|
121
|
+
# Seconds a type's on_subscribe has to return before the subscription is refused.
|
|
122
|
+
_SUBSCRIBE_TIMEOUT = 30.0
|
|
103
123
|
|
|
104
124
|
A = TypeVar("A")
|
|
105
125
|
D = TypeVar("D")
|
|
106
126
|
Delivery: TypeAlias = Literal["poll", "push"]
|
|
107
|
-
Teardown: TypeAlias = Callable[[], object]
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
class Keep(TypedDict, total=False):
|
|
111
|
-
"""What each event type keeps of what it emits, for polls and streams that are behind or reconnect."""
|
|
112
|
-
|
|
113
|
-
events: int
|
|
114
|
-
"""How many events. Ten thousand unless given."""
|
|
115
|
-
age: float
|
|
116
|
-
"""For how long, in seconds. Ten minutes unless given."""
|
|
117
|
-
bytes: int
|
|
118
|
-
"""How many bytes their data takes as JSON. The newest is kept whatever it takes. 4 MiB unless given."""
|
|
119
127
|
|
|
120
128
|
|
|
121
129
|
@dataclass(frozen=True)
|
|
@@ -139,8 +147,8 @@ class Event(Generic[D]):
|
|
|
139
147
|
|
|
140
148
|
|
|
141
149
|
@dataclass(frozen=True)
|
|
142
|
-
class
|
|
143
|
-
"""What happened after a cursor, as a check function finds it."""
|
|
150
|
+
class EventResult(Generic[D]):
|
|
151
|
+
"""What happened after a cursor, as a check function finds it: the sketch's EventResult."""
|
|
144
152
|
|
|
145
153
|
events: Sequence[Event[D]]
|
|
146
154
|
"""Empty when the cursor was None, which asks only for the current position. A source without positions
|
|
@@ -154,14 +162,12 @@ class CheckResult(Generic[D]):
|
|
|
154
162
|
|
|
155
163
|
|
|
156
164
|
Check: TypeAlias = Callable[
|
|
157
|
-
[RequestContext, A, str | None],
|
|
165
|
+
[RequestContext, A, str | None], EventResult[D] | Awaitable[EventResult[D]]
|
|
158
166
|
]
|
|
159
167
|
Match: TypeAlias = Callable[[RequestContext, Event[D], A], bool]
|
|
160
168
|
Transform: TypeAlias = Callable[[RequestContext, Event[D], A], object]
|
|
161
169
|
Authorize: TypeAlias = Callable[[RequestContext, A], bool | Awaitable[bool]]
|
|
162
|
-
|
|
163
|
-
[RequestContext, A, str, Delivery], Teardown | None | Awaitable[Teardown | None]
|
|
164
|
-
]
|
|
170
|
+
SubscriptionHook: TypeAlias = Callable[[RequestContext, A, str], None | Awaitable[None]]
|
|
165
171
|
|
|
166
172
|
|
|
167
173
|
class _SubscribeParams(RequestParams):
|
|
@@ -258,7 +264,9 @@ class _Read:
|
|
|
258
264
|
class _Entry:
|
|
259
265
|
"""A subscription held by one or more streams or poll leases, set up once by the type's on_subscribe."""
|
|
260
266
|
|
|
261
|
-
ready: asyncio.Future[
|
|
267
|
+
ready: asyncio.Future[None]
|
|
268
|
+
request: _Request
|
|
269
|
+
"""The request that began it, whose context and arguments on_unsubscribe is given."""
|
|
262
270
|
holds: set[_Hold] = field(default_factory=set)
|
|
263
271
|
|
|
264
272
|
|
|
@@ -296,10 +304,10 @@ class EventType(Generic[A, D]):
|
|
|
296
304
|
_transform: Transform[D, A] | None
|
|
297
305
|
_key: Callable[[D], str] | None
|
|
298
306
|
_check: Check[A, D] | None
|
|
299
|
-
_interval: float
|
|
300
307
|
_delivery: list[Delivery]
|
|
301
308
|
_authorize: Authorize[A] | None
|
|
302
|
-
_on_subscribe:
|
|
309
|
+
_on_subscribe: SubscriptionHook[A] | None
|
|
310
|
+
_on_unsubscribe: SubscriptionHook[A] | None
|
|
303
311
|
# What was emitted, oldest first. `_dropped` is the last sequence let go for room or age.
|
|
304
312
|
_kept: list[_Kept] = field(default_factory=list)
|
|
305
313
|
_sequence: int = 0
|
|
@@ -307,7 +315,7 @@ class EventType(Generic[A, D]):
|
|
|
307
315
|
_bytes: int = 0
|
|
308
316
|
_latest: dict[tuple[str | None, str], _Kept] = field(default_factory=dict)
|
|
309
317
|
_subscriptions: dict[str, _Entry] = field(default_factory=dict)
|
|
310
|
-
# Subscriptions being
|
|
318
|
+
# Subscriptions being told they ended, which one that begins with the same id waits for.
|
|
311
319
|
_closing: dict[str, asyncio.Event] = field(default_factory=dict)
|
|
312
320
|
# Subscriptions of a type with a check function, held by nothing, that an event emitted to them could not reach,
|
|
313
321
|
# oldest first. Ids that never come back are let go once there are as many as the events kept.
|
|
@@ -319,12 +327,13 @@ class EventType(Generic[A, D]):
|
|
|
319
327
|
*,
|
|
320
328
|
event_id: str | None = None,
|
|
321
329
|
timestamp: datetime | str | None = None,
|
|
322
|
-
|
|
330
|
+
subscription_id: str | None = None,
|
|
323
331
|
) -> None:
|
|
324
332
|
"""Emit an occurrence to the subscriptions it matches.
|
|
325
333
|
|
|
326
334
|
Give the source's own ``event_id`` when it has one, so that a client drops an occurrence it has seen. With
|
|
327
|
-
``
|
|
335
|
+
``subscription_id``, the sketch's targeted emit, the occurrence goes to that subscription alone, whatever its
|
|
336
|
+
arguments. It is kept like any
|
|
328
337
|
other, so a subscription that reconnects or polls from its cursor still has it, or is told of a gap. A type
|
|
329
338
|
with a check function serves polls from the check function, so an occurrence emitted to a subscription that
|
|
330
339
|
has no open stream is reported to it as a gap.
|
|
@@ -341,21 +350,21 @@ class EventType(Generic[A, D]):
|
|
|
341
350
|
data, event_id or str(uuid.uuid4()), _iso(timestamp), name=self.name
|
|
342
351
|
)
|
|
343
352
|
self._keep(
|
|
344
|
-
_Kept(event, size,
|
|
353
|
+
_Kept(event, size, subscription_id, self._key(data) if self._key else None)
|
|
345
354
|
)
|
|
346
|
-
if
|
|
355
|
+
if subscription_id is None:
|
|
347
356
|
holds = [
|
|
348
357
|
hold for entry in self._subscriptions.values() for hold in entry.holds
|
|
349
358
|
]
|
|
350
359
|
else:
|
|
351
|
-
entry = self._subscriptions.get(
|
|
360
|
+
entry = self._subscriptions.get(subscription_id)
|
|
352
361
|
holds = list(entry.holds) if entry else []
|
|
353
362
|
# A type with a check function answers polls from it, so neither a poll nor a subscription held by
|
|
354
363
|
# nothing now can be given this.
|
|
355
364
|
if self._check is not None:
|
|
356
365
|
if not holds:
|
|
357
|
-
self._missed[
|
|
358
|
-
if len(self._missed) >
|
|
366
|
+
self._missed[subscription_id] = None
|
|
367
|
+
if len(self._missed) > _KEEP_EVENTS:
|
|
359
368
|
del self._missed[next(iter(self._missed))]
|
|
360
369
|
for hold in holds:
|
|
361
370
|
hold.missed |= hold.delivery == "poll" and hold.request.targetable
|
|
@@ -375,16 +384,16 @@ class EventType(Generic[A, D]):
|
|
|
375
384
|
)
|
|
376
385
|
return self._position()
|
|
377
386
|
|
|
378
|
-
def
|
|
379
|
-
self,
|
|
387
|
+
def terminate(
|
|
388
|
+
self, subscription_id: str | None = None, error: ErrorData | None = None
|
|
380
389
|
) -> None:
|
|
381
|
-
"""
|
|
382
|
-
is answered with it. Forbidden unless given."""
|
|
383
|
-
if
|
|
390
|
+
"""Terminate one subscription, or all of this type's: a stream is sent ``notifications/events/terminated``
|
|
391
|
+
with ``error`` and closed, and the next poll is answered with it. Forbidden unless given."""
|
|
392
|
+
if subscription_id is None:
|
|
384
393
|
entries = list(self._subscriptions.values())
|
|
385
394
|
else:
|
|
386
395
|
entries = (
|
|
387
|
-
[entry] if (entry := self._subscriptions.get(
|
|
396
|
+
[entry] if (entry := self._subscriptions.get(subscription_id)) else []
|
|
388
397
|
)
|
|
389
398
|
for hold in [hold for entry in entries for hold in entry.holds]:
|
|
390
399
|
if hold.ended is None:
|
|
@@ -395,7 +404,7 @@ class EventType(Generic[A, D]):
|
|
|
395
404
|
"""Stop offering this event type: its subscriptions end with NotFound."""
|
|
396
405
|
if self._events._types.get(self.name) is self:
|
|
397
406
|
del self._events._types[self.name]
|
|
398
|
-
self.
|
|
407
|
+
self.terminate(error=_REMOVED)
|
|
399
408
|
|
|
400
409
|
def _position(self, sequence: int | None = None) -> str:
|
|
401
410
|
return (
|
|
@@ -403,7 +412,6 @@ class EventType(Generic[A, D]):
|
|
|
403
412
|
)
|
|
404
413
|
|
|
405
414
|
def _keep(self, kept: _Kept) -> None:
|
|
406
|
-
events = self._events
|
|
407
415
|
self._sequence += 1
|
|
408
416
|
kept.sequence, kept.at = self._sequence, time.monotonic()
|
|
409
417
|
# An event with a key replaces the one before it with the same key, for the same subscriptions.
|
|
@@ -422,9 +430,9 @@ class EventType(Generic[A, D]):
|
|
|
422
430
|
# The newest is kept whatever it weighs, since streams read it from here.
|
|
423
431
|
dropped = 0
|
|
424
432
|
while len(self._kept) - dropped > 1 and (
|
|
425
|
-
len(self._kept) - dropped >
|
|
426
|
-
or self._bytes >
|
|
427
|
-
or self._kept[dropped].at < kept.at -
|
|
433
|
+
len(self._kept) - dropped > _KEEP_EVENTS
|
|
434
|
+
or self._bytes > _KEEP_BYTES
|
|
435
|
+
or self._kept[dropped].at < kept.at - _KEEP_AGE
|
|
428
436
|
):
|
|
429
437
|
oldest = self._kept[dropped]
|
|
430
438
|
self._dropped = oldest.sequence
|
|
@@ -614,51 +622,25 @@ class Events(Extension):
|
|
|
614
622
|
Pass it to ``MCPServer(extensions=[events])``, or call :meth:`serve` on a low-level ``Server``. Over Streamable
|
|
615
623
|
HTTP, push is offered only on an endpoint mounted through :meth:`asgi`; elsewhere clients poll.
|
|
616
624
|
|
|
617
|
-
Each type keeps what it emits,
|
|
618
|
-
those cursors last as long as the process, and after a restart a client is told that
|
|
619
|
-
type with a check function serves polls from it instead, with cursors that are its
|
|
620
|
-
again, and a poll told to come back, every
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
``
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
returned after ``subscribe_timeout`` seconds is refused, and may be tried again. With the defaults, events take
|
|
632
|
-
about 4 MiB kept for each type and 16 MiB being sent, whatever one caller does.
|
|
625
|
+
Each type keeps what it emits, for ten minutes and up to 10,000 events and 4 MiB, for polls and for streams that
|
|
626
|
+
are behind or reconnect; those cursors last as long as the process, and after a restart a client is told that
|
|
627
|
+
events were skipped. A type with a check function serves polls from it instead, with cursors that are its
|
|
628
|
+
source's. A type is asked again, and a poll told to come back, every second. A stream hears from the server every
|
|
629
|
+
30 seconds, and one whose reader has taken nothing for two heartbeats is closed, to reconnect from its cursor. A
|
|
630
|
+
poll subscription lasts a minute after its last poll.
|
|
631
|
+
|
|
632
|
+
A stream sends one event at a time and holds nothing else of its own. Every stream together sends at most 16 MiB
|
|
633
|
+
of events at once, and one caller's streams a quarter of it; an event is sent alone when nothing else is, whatever
|
|
634
|
+
it weighs. Waiting for room counts toward the two heartbeats. At most 10,000 subscriptions are held at once,
|
|
635
|
+
streams and poll leases alike, and 100 of one caller; callers without an identity count together as one.
|
|
636
|
+
``events/list`` returns 100 types at a time. A subscription whose ``on_subscribe`` has not returned after 30
|
|
637
|
+
seconds is refused, and may be tried again. Events thus take about 4 MiB kept for each type and 16 MiB being
|
|
638
|
+
sent, whatever one caller does.
|
|
633
639
|
"""
|
|
634
640
|
|
|
635
641
|
identifier = EVENTS_EXTENSION
|
|
636
642
|
|
|
637
|
-
def __init__(
|
|
638
|
-
self,
|
|
639
|
-
*,
|
|
640
|
-
keep: Keep | None = None,
|
|
641
|
-
interval: float = 1.0,
|
|
642
|
-
heartbeat: float = 30.0,
|
|
643
|
-
poll_lease: float | None = None,
|
|
644
|
-
max_sending_bytes: int = 16 * 2**20,
|
|
645
|
-
max_subscriptions: int = 10_000,
|
|
646
|
-
max_subscriptions_per_caller: int = 100,
|
|
647
|
-
page_size: int = 100,
|
|
648
|
-
subscribe_timeout: float = 30.0,
|
|
649
|
-
) -> None:
|
|
650
|
-
keep = keep or {}
|
|
651
|
-
self._keep_events = keep.get("events", 10_000)
|
|
652
|
-
self._keep_age = keep.get("age", 600.0)
|
|
653
|
-
self._keep_bytes = keep.get("bytes", 4 * 2**20)
|
|
654
|
-
self._interval = interval
|
|
655
|
-
self._heartbeat = heartbeat
|
|
656
|
-
self._poll_lease = poll_lease
|
|
657
|
-
self._max_sending = max_sending_bytes
|
|
658
|
-
self._max_subscriptions = max_subscriptions
|
|
659
|
-
self._max_per_caller = max_subscriptions_per_caller
|
|
660
|
-
self._page_size = page_size
|
|
661
|
-
self._subscribe_timeout = subscribe_timeout
|
|
643
|
+
def __init__(self) -> None:
|
|
662
644
|
# Cursors of kept events are positions in this process's memory; one from another process names nothing.
|
|
663
645
|
self._epoch = secrets.token_hex(6)
|
|
664
646
|
self._types: dict[str, EventType[Any, Any]] = {}
|
|
@@ -684,10 +666,10 @@ class Events(Extension):
|
|
|
684
666
|
transform: Transform[D, A] | None = None,
|
|
685
667
|
key: Callable[[D], str] | None = None,
|
|
686
668
|
check: Check[A, D] | None = None,
|
|
687
|
-
interval: float | None = None,
|
|
688
669
|
delivery: Sequence[Delivery] = ("poll", "push"),
|
|
689
670
|
authorize: Authorize[A] | None = None,
|
|
690
|
-
on_subscribe:
|
|
671
|
+
on_subscribe: SubscriptionHook[A] | None = None,
|
|
672
|
+
on_unsubscribe: SubscriptionHook[A] | None = None,
|
|
691
673
|
) -> EventType[A, D]:
|
|
692
674
|
"""Declare an event type.
|
|
693
675
|
|
|
@@ -705,19 +687,19 @@ class Events(Extension):
|
|
|
705
687
|
``match`` must then answer the same for every occurrence of a key. Do not give a key to events that
|
|
706
688
|
each say what changed.
|
|
707
689
|
check: ``(context, arguments, cursor)``: what happened after a cursor, for a source that keeps its own
|
|
708
|
-
history. Polls are answered by it, and streams ask it every
|
|
690
|
+
history. Polls are answered by it, and streams ask it every second. A None cursor asks for the
|
|
709
691
|
current position. It filters and shapes for the arguments itself; ``match`` and ``transform`` apply
|
|
710
692
|
only to what is emitted.
|
|
711
|
-
interval: Seconds a poll is told to wait, and how often a stream asks the check function.
|
|
712
693
|
delivery: The delivery modes this type offers.
|
|
713
694
|
authorize: ``(context, arguments)``: whether the caller may subscribe. A subscription is refused with
|
|
714
695
|
Forbidden when it may not, and one already held is ended when it no longer may: each poll asks, and
|
|
715
696
|
a stream asks at each heartbeat. A refused poll ends its lease at once.
|
|
716
|
-
on_subscribe: ``(context, arguments, subscription_id
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
697
|
+
on_subscribe: ``(context, arguments, subscription_id)``: called when a subscription begins, to set up
|
|
698
|
+
what its events come from. A subscription held by two streams at once is set up once. A server that
|
|
699
|
+
restarts calls it again, with the same id for a caller with an identity.
|
|
700
|
+
on_unsubscribe: ``(context, arguments, subscription_id)``: called when a subscription that was set up
|
|
701
|
+
ends: its last stream closes, its poll lease lapses because polls stopped, or the app terminates it.
|
|
702
|
+
One that begins again with the same id waits for it to return.
|
|
721
703
|
"""
|
|
722
704
|
if name in self._types:
|
|
723
705
|
raise ValueError(f"Event {name} is already declared.")
|
|
@@ -754,14 +736,16 @@ class Events(Extension):
|
|
|
754
736
|
transform,
|
|
755
737
|
key,
|
|
756
738
|
check,
|
|
757
|
-
self._interval if interval is None else interval,
|
|
758
739
|
list(delivery),
|
|
759
740
|
authorize,
|
|
760
741
|
on_subscribe,
|
|
742
|
+
on_unsubscribe,
|
|
761
743
|
)
|
|
762
744
|
self._types[name] = declared
|
|
763
745
|
return declared
|
|
764
746
|
|
|
747
|
+
# Declared under capabilities.extensions only. The sketch also declares capabilities.events, but the MCP SDK's
|
|
748
|
+
# ServerCapabilities drops top-level keys it does not know, and offers no way to add one.
|
|
765
749
|
def settings(self) -> dict[str, Any]:
|
|
766
750
|
# Event types change only in the process, and the SDK has no channel to tell every client of it.
|
|
767
751
|
return {"listChanged": False}
|
|
@@ -819,7 +803,7 @@ class Events(Extension):
|
|
|
819
803
|
),
|
|
820
804
|
len(declared),
|
|
821
805
|
)
|
|
822
|
-
page = declared[start : start +
|
|
806
|
+
page = declared[start : start + _LIST_PAGE_SIZE]
|
|
823
807
|
streams = _streams(ctx)
|
|
824
808
|
listed = []
|
|
825
809
|
for each in page:
|
|
@@ -828,7 +812,7 @@ class Events(Extension):
|
|
|
828
812
|
]:
|
|
829
813
|
listed.append({**each._descriptor, "delivery": delivery})
|
|
830
814
|
result: dict[str, Any] = {"events": listed}
|
|
831
|
-
if start +
|
|
815
|
+
if start + _LIST_PAGE_SIZE < len(declared):
|
|
832
816
|
result["nextCursor"] = str(page[-1]._order)
|
|
833
817
|
return result
|
|
834
818
|
|
|
@@ -850,7 +834,7 @@ class Events(Extension):
|
|
|
850
834
|
"cursor": read.cursor,
|
|
851
835
|
"truncated": read.truncated,
|
|
852
836
|
"hasMore": read.has_more,
|
|
853
|
-
"nextPollMs": round(
|
|
837
|
+
"nextPollMs": round(_INTERVAL * 1000),
|
|
854
838
|
}
|
|
855
839
|
|
|
856
840
|
async def _stream(
|
|
@@ -937,7 +921,7 @@ class Events(Extension):
|
|
|
937
921
|
targetable=principal is not None or delivery == "push",
|
|
938
922
|
)
|
|
939
923
|
if not await declared._allows(context, arguments):
|
|
940
|
-
declared.
|
|
924
|
+
declared.terminate(id, _FORBIDDEN)
|
|
941
925
|
if lease := self._leases.get(id):
|
|
942
926
|
self._stop(id, lease)
|
|
943
927
|
raise MCPError.from_error_data(_FORBIDDEN)
|
|
@@ -945,7 +929,7 @@ class Events(Extension):
|
|
|
945
929
|
|
|
946
930
|
# Begins holding a subscription, by a stream or a poll lease: counted against the limits first, so a refused one
|
|
947
931
|
# provisions nothing. The type's on_subscribe runs, apart from the request, when the first hold of an id begins,
|
|
948
|
-
# and its
|
|
932
|
+
# and its on_unsubscribe when the last ends. Await `_ready` before serving the hold.
|
|
949
933
|
def _open(
|
|
950
934
|
self,
|
|
951
935
|
declared: EventType[Any, Any],
|
|
@@ -955,24 +939,27 @@ class Events(Extension):
|
|
|
955
939
|
) -> _Hold:
|
|
956
940
|
if self._types.get(declared.name) is not declared:
|
|
957
941
|
raise MCPError.from_error_data(_REMOVED)
|
|
958
|
-
if self._held >=
|
|
942
|
+
if self._held >= _MAX_SUBSCRIPTIONS:
|
|
959
943
|
raise MCPError(
|
|
960
944
|
RESOURCE_EXHAUSTED,
|
|
961
945
|
"ResourceExhausted",
|
|
962
|
-
{"limit": "subscriptions", "max":
|
|
946
|
+
{"limit": "subscriptions", "max": _MAX_SUBSCRIPTIONS},
|
|
963
947
|
)
|
|
964
|
-
if self._callers.get(request.caller, 0) >=
|
|
948
|
+
if self._callers.get(request.caller, 0) >= _MAX_SUBSCRIPTIONS_PER_CALLER:
|
|
965
949
|
raise MCPError(
|
|
966
950
|
RESOURCE_EXHAUSTED,
|
|
967
951
|
"ResourceExhausted",
|
|
968
|
-
{
|
|
952
|
+
{
|
|
953
|
+
"limit": "subscriptionsPerCaller",
|
|
954
|
+
"max": _MAX_SUBSCRIPTIONS_PER_CALLER,
|
|
955
|
+
},
|
|
969
956
|
)
|
|
970
957
|
self._held += 1
|
|
971
958
|
self._callers[request.caller] = self._callers.get(request.caller, 0) + 1
|
|
972
959
|
entry = declared._subscriptions.get(request.id)
|
|
973
960
|
if entry is None:
|
|
974
961
|
entry = _Entry(
|
|
975
|
-
asyncio.ensure_future(self._set_up(declared, request,
|
|
962
|
+
asyncio.ensure_future(self._set_up(declared, request)), request
|
|
976
963
|
)
|
|
977
964
|
declared._subscriptions[request.id] = entry
|
|
978
965
|
hold = _Hold(declared, request, delivery, entry, wake)
|
|
@@ -982,17 +969,14 @@ class Events(Extension):
|
|
|
982
969
|
entry.holds.add(hold)
|
|
983
970
|
return hold
|
|
984
971
|
|
|
985
|
-
async def _set_up(
|
|
986
|
-
self, declared: EventType[Any, Any], request: _Request, delivery: Delivery
|
|
987
|
-
) -> Teardown | None:
|
|
972
|
+
async def _set_up(self, declared: EventType[Any, Any], request: _Request) -> None:
|
|
988
973
|
if declared._on_subscribe is None:
|
|
989
|
-
return
|
|
974
|
+
return
|
|
990
975
|
# A hook that never returns would hold its id for good: past the deadline the subscription is refused.
|
|
991
|
-
async with asyncio.timeout(
|
|
992
|
-
|
|
993
|
-
request.context, request.arguments, request.id
|
|
976
|
+
async with asyncio.timeout(_SUBSCRIBE_TIMEOUT):
|
|
977
|
+
await _settle(
|
|
978
|
+
declared._on_subscribe(request.context, request.arguments, request.id)
|
|
994
979
|
)
|
|
995
|
-
return cast(Teardown | None, await _settle(setup))
|
|
996
980
|
|
|
997
981
|
async def _ready(self, hold: _Hold) -> None:
|
|
998
982
|
await asyncio.shield(hold.entry.ready)
|
|
@@ -1017,26 +1001,30 @@ class Events(Extension):
|
|
|
1017
1001
|
if declared._subscriptions.get(id) is entry:
|
|
1018
1002
|
del declared._subscriptions[id]
|
|
1019
1003
|
closing = declared._closing[id] = asyncio.Event()
|
|
1020
|
-
task = asyncio.ensure_future(
|
|
1021
|
-
self._tear_down(declared, id, entry.ready, closing)
|
|
1022
|
-
)
|
|
1004
|
+
task = asyncio.ensure_future(self._tear_down(declared, id, entry, closing))
|
|
1023
1005
|
self._tasks.add(task)
|
|
1024
1006
|
task.add_done_callback(self._tasks.discard)
|
|
1025
1007
|
|
|
1026
|
-
#
|
|
1008
|
+
# Only a subscription that was set up is told it ended. A hook that fails has nobody to tell but the log, and must
|
|
1009
|
+
# not take the process down.
|
|
1027
1010
|
async def _tear_down(
|
|
1028
1011
|
self,
|
|
1029
1012
|
declared: EventType[Any, Any],
|
|
1030
1013
|
id: str,
|
|
1031
|
-
|
|
1014
|
+
entry: _Entry,
|
|
1032
1015
|
closing: asyncio.Event,
|
|
1033
1016
|
) -> None:
|
|
1034
1017
|
try:
|
|
1035
|
-
|
|
1036
|
-
if
|
|
1037
|
-
|
|
1018
|
+
await entry.ready
|
|
1019
|
+
if declared._on_unsubscribe is not None:
|
|
1020
|
+
request = entry.request
|
|
1021
|
+
await _settle(
|
|
1022
|
+
declared._on_unsubscribe(request.context, request.arguments, id)
|
|
1023
|
+
)
|
|
1038
1024
|
except Exception:
|
|
1039
|
-
logger.exception(
|
|
1025
|
+
logger.exception(
|
|
1026
|
+
"An event subscription's on_subscribe or on_unsubscribe failed"
|
|
1027
|
+
)
|
|
1040
1028
|
finally:
|
|
1041
1029
|
if declared._closing.get(id) is closing:
|
|
1042
1030
|
del declared._closing[id]
|
|
@@ -1059,13 +1047,8 @@ class Events(Extension):
|
|
|
1059
1047
|
# Renewed before waiting, so that a poll which gives up during setup still lets the lease lapse.
|
|
1060
1048
|
if lease.timer is not None:
|
|
1061
1049
|
lease.timer.cancel()
|
|
1062
|
-
length = (
|
|
1063
|
-
self._poll_lease
|
|
1064
|
-
if self._poll_lease is not None
|
|
1065
|
-
else max(60.0, 3 * declared._interval)
|
|
1066
|
-
)
|
|
1067
1050
|
lease.timer = asyncio.get_running_loop().call_later(
|
|
1068
|
-
|
|
1051
|
+
_POLL_SUBSCRIPTION_TTL, self._stop, request.id, lease
|
|
1069
1052
|
)
|
|
1070
1053
|
try:
|
|
1071
1054
|
await self._ready(lease.hold)
|
|
@@ -1085,9 +1068,9 @@ class Events(Extension):
|
|
|
1085
1068
|
# event of `size`. A caller may send a quarter of the whole, so that one cannot hold up the rest.
|
|
1086
1069
|
@asynccontextmanager
|
|
1087
1070
|
async def _send_budget(self, size: int, caller: str) -> AsyncIterator[None]:
|
|
1088
|
-
while (self._sending and self._sending + size >
|
|
1071
|
+
while (self._sending and self._sending + size > _MAX_SENDING_BYTES) or (
|
|
1089
1072
|
(mine := self._sending_by.get(caller, 0))
|
|
1090
|
-
and mine + size >
|
|
1073
|
+
and mine + size > _MAX_SENDING_BYTES // 4
|
|
1091
1074
|
):
|
|
1092
1075
|
await self._sent.wait()
|
|
1093
1076
|
self._sending += size
|
|
@@ -1124,7 +1107,7 @@ class Events(Extension):
|
|
|
1124
1107
|
**body.get("_meta", {}),
|
|
1125
1108
|
SUBSCRIPTION_ID_META_KEY: request_id,
|
|
1126
1109
|
}
|
|
1127
|
-
with anyio.move_on_after(2 *
|
|
1110
|
+
with anyio.move_on_after(2 * _HEARTBEAT) as waited:
|
|
1128
1111
|
async with self._send_budget(size, request.caller):
|
|
1129
1112
|
await notify(f"notifications/events/{method}", body)
|
|
1130
1113
|
if waited.cancelled_caught:
|
|
@@ -1151,16 +1134,16 @@ class Events(Extension):
|
|
|
1151
1134
|
await say("active", {"cursor": read.start, "truncated": read.truncated})
|
|
1152
1135
|
for occurrence, after in read.events:
|
|
1153
1136
|
await deliver(occurrence, after)
|
|
1154
|
-
due = time.monotonic() + (0 if read.has_more else
|
|
1137
|
+
due = time.monotonic() + (0 if read.has_more else _INTERVAL)
|
|
1155
1138
|
else:
|
|
1156
1139
|
seen, truncated = declared._resume(params.cursor, params.max_age_ms)
|
|
1157
1140
|
await say("active", {"cursor": position(), "truncated": truncated})
|
|
1158
1141
|
due = math.inf
|
|
1159
|
-
beat = time.monotonic() +
|
|
1142
|
+
beat = time.monotonic() + _HEARTBEAT
|
|
1160
1143
|
while hold.ended is None:
|
|
1161
1144
|
wake.clear()
|
|
1162
1145
|
if time.monotonic() >= beat:
|
|
1163
|
-
beat = time.monotonic() +
|
|
1146
|
+
beat = time.monotonic() + _HEARTBEAT
|
|
1164
1147
|
# Access is checked again while the subscription lasts, and one that is lost ends it.
|
|
1165
1148
|
try:
|
|
1166
1149
|
allowed = await declared._allows(
|
|
@@ -1180,16 +1163,14 @@ class Events(Extension):
|
|
|
1180
1163
|
)
|
|
1181
1164
|
except Exception as error: # noqa: BLE001
|
|
1182
1165
|
await say("error", {"error": _described(error)})
|
|
1183
|
-
due = time.monotonic() +
|
|
1166
|
+
due = time.monotonic() + _INTERVAL
|
|
1184
1167
|
continue
|
|
1185
1168
|
if read.truncated:
|
|
1186
1169
|
await say("active", {"cursor": read.start, "truncated": True})
|
|
1187
1170
|
for occurrence, after in read.events:
|
|
1188
1171
|
await deliver(occurrence, after)
|
|
1189
1172
|
cursor = read.cursor
|
|
1190
|
-
due = time.monotonic() + (
|
|
1191
|
-
0 if read.has_more else declared._interval
|
|
1192
|
-
)
|
|
1173
|
+
due = time.monotonic() + (0 if read.has_more else _INTERVAL)
|
|
1193
1174
|
elif seen < declared._dropped:
|
|
1194
1175
|
# What the stream had yet to send was let go: a gap, from the oldest still kept, or for a type
|
|
1195
1176
|
# with a check function, from where its check has reached.
|
|
@@ -1298,8 +1279,12 @@ def _refused(error: Exception) -> bool:
|
|
|
1298
1279
|
)
|
|
1299
1280
|
|
|
1300
1281
|
|
|
1282
|
+
class NoCompatibleDeliveryMode(Exception):
|
|
1283
|
+
"""The sketch's: none of the delivery modes the app offers for an event type is one this subscription uses."""
|
|
1284
|
+
|
|
1285
|
+
|
|
1301
1286
|
class AppEvents:
|
|
1302
|
-
"""The events of one app, as its client lists and subscribes to them: ``app.events``."""
|
|
1287
|
+
"""The events of one app, as its client lists and subscribes to them: ``app.ext.events``."""
|
|
1303
1288
|
|
|
1304
1289
|
def __init__(self, app: _App, contract: dict[str, Any] | None) -> None:
|
|
1305
1290
|
self._app = app
|
|
@@ -1350,14 +1335,15 @@ class AppEvents:
|
|
|
1350
1335
|
arguments: The subscription's arguments, as the type's ``inputSchema`` describes them.
|
|
1351
1336
|
cursor: Where to resume from, as a subscription's ``cursor`` was. From now unless given.
|
|
1352
1337
|
max_age: Seconds: events older than this are not replayed from ``cursor``.
|
|
1353
|
-
delivery: ``"push"`` or ``"poll"``. Push when the app offers it, else poll, unless given.
|
|
1338
|
+
delivery: ``"push"`` or ``"poll"``. Push when the app offers it, else poll, unless given. One the app
|
|
1339
|
+
does not offer raises :class:`NoCompatibleDeliveryMode`.
|
|
1354
1340
|
on_gap: Called when events were skipped, before the events after the gap.
|
|
1355
1341
|
|
|
1356
1342
|
Returns:
|
|
1357
1343
|
A :class:`Subscription` to enter with ``async with`` and iterate.
|
|
1358
1344
|
"""
|
|
1359
1345
|
return Subscription(
|
|
1360
|
-
self
|
|
1346
|
+
self, name, dict(arguments or {}), cursor, max_age, delivery, on_gap
|
|
1361
1347
|
)
|
|
1362
1348
|
|
|
1363
1349
|
|
|
@@ -1372,25 +1358,25 @@ class _Reached:
|
|
|
1372
1358
|
|
|
1373
1359
|
|
|
1374
1360
|
class Subscription(Generic[D]):
|
|
1375
|
-
"""A subscription to one event type, from ``app.events.subscribe``.
|
|
1361
|
+
"""A subscription to one event type, from ``app.ext.events.subscribe``.
|
|
1376
1362
|
|
|
1377
1363
|
Entering it with ``async with`` subscribes, and returns once the app has confirmed the subscription; a refusal
|
|
1378
1364
|
raises ``MCPError``. Iterating it yields each :class:`Event` once, in order, dropping one whose
|
|
1379
|
-
``event_id`` was seen. A dropped connection is reconnected, and resumes from the cursor.
|
|
1380
|
-
cancelling its task
|
|
1381
|
-
raises ``MCPError`` when the app
|
|
1365
|
+
``event_id`` was seen. A dropped connection is reconnected, and resumes from the cursor. :meth:`cancel`, leaving
|
|
1366
|
+
the block, or cancelling its task ends the subscription: breaking out of ``async for`` alone does not. Iteration
|
|
1367
|
+
raises ``MCPError`` when the app terminates it.
|
|
1382
1368
|
|
|
1383
1369
|
``cursor`` is the position after the last event iterated, to persist and resume from. ``delivery`` is how
|
|
1384
1370
|
events arrive, once entered.
|
|
1385
1371
|
|
|
1386
1372
|
Example:
|
|
1387
|
-
``async with app.events.subscribe("board.changed", {"board": "b1"}) as events:``
|
|
1373
|
+
``async with app.ext.events.subscribe("board.changed", {"board": "b1"}) as events:``
|
|
1388
1374
|
`` async for event in events: ...``
|
|
1389
1375
|
"""
|
|
1390
1376
|
|
|
1391
1377
|
def __init__(
|
|
1392
1378
|
self,
|
|
1393
|
-
|
|
1379
|
+
events: AppEvents,
|
|
1394
1380
|
name: str,
|
|
1395
1381
|
arguments: dict[str, Any],
|
|
1396
1382
|
cursor: str | None,
|
|
@@ -1401,7 +1387,8 @@ class Subscription(Generic[D]):
|
|
|
1401
1387
|
self.cursor = cursor
|
|
1402
1388
|
self.delivery = delivery
|
|
1403
1389
|
self._forced = delivery is not None
|
|
1404
|
-
self.
|
|
1390
|
+
self._events = events
|
|
1391
|
+
self._app = events._app
|
|
1405
1392
|
self._name = name
|
|
1406
1393
|
self._request: dict[str, Any] = {"name": name, "arguments": arguments}
|
|
1407
1394
|
if max_age is not None:
|
|
@@ -1419,21 +1406,23 @@ class Subscription(Generic[D]):
|
|
|
1419
1406
|
Event[Any] | _Gap | _Reached
|
|
1420
1407
|
]()
|
|
1421
1408
|
self._group: anyio.abc.TaskGroup | None = None
|
|
1409
|
+
self._running = anyio.CancelScope()
|
|
1422
1410
|
# The open stream request, which a notification ends when the app ended the subscription.
|
|
1423
1411
|
self._stream: anyio.CancelScope | None = None
|
|
1424
1412
|
self._ended: Exception | None = None
|
|
1425
1413
|
|
|
1426
1414
|
async def __aenter__(self) -> Subscription[D]:
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1415
|
+
listed = [
|
|
1416
|
+
each for each in await self._events.list() if each["name"] == self._name
|
|
1417
|
+
]
|
|
1418
|
+
# A type the app does not list is refused by the app, with NotFound.
|
|
1419
|
+
offered: list[str] = listed[0].get("delivery", []) if listed else ["poll"]
|
|
1420
|
+
wanted = [self.delivery] if self.delivery else ["push", "poll"]
|
|
1421
|
+
if not (usable := [mode for mode in wanted if mode in offered]):
|
|
1422
|
+
raise NoCompatibleDeliveryMode(
|
|
1423
|
+
f"{self._app._name} does not deliver {self._name} by {' or '.join(wanted)}."
|
|
1435
1424
|
)
|
|
1436
|
-
|
|
1425
|
+
self.delivery = cast(Delivery, usable[0])
|
|
1437
1426
|
self._group = anyio.create_task_group()
|
|
1438
1427
|
await self._group.__aenter__()
|
|
1439
1428
|
self._group.start_soon(self._run)
|
|
@@ -1456,6 +1445,12 @@ class Subscription(Generic[D]):
|
|
|
1456
1445
|
) -> None:
|
|
1457
1446
|
await self._close()
|
|
1458
1447
|
|
|
1448
|
+
async def cancel(self) -> None:
|
|
1449
|
+
"""The sketch's ``sub.cancel()``: ends the subscription, and iteration stops. Leaving the block does too."""
|
|
1450
|
+
# The block's own code runs inside the task group, so only the work behind the iterator is cancelled here.
|
|
1451
|
+
self._running.cancel()
|
|
1452
|
+
self._receive.close()
|
|
1453
|
+
|
|
1459
1454
|
async def _close(self) -> None:
|
|
1460
1455
|
if self._group is not None:
|
|
1461
1456
|
group, self._group = self._group, None
|
|
@@ -1485,31 +1480,32 @@ class Subscription(Generic[D]):
|
|
|
1485
1480
|
return item
|
|
1486
1481
|
|
|
1487
1482
|
async def _run(self) -> None:
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1483
|
+
with self._running:
|
|
1484
|
+
try:
|
|
1485
|
+
async with self._app._session(
|
|
1486
|
+
_TIMEOUT, partial(_ordered, notified=self._notified)
|
|
1487
|
+
) as client:
|
|
1488
|
+
try:
|
|
1489
|
+
if self.delivery == "push":
|
|
1490
|
+
try:
|
|
1491
|
+
await self._push(client)
|
|
1492
|
+
except MCPError as error:
|
|
1493
|
+
# An endpoint that cannot stream refuses push, and the subscription polls instead.
|
|
1494
|
+
if (
|
|
1495
|
+
self._forced
|
|
1496
|
+
or self._active
|
|
1497
|
+
or error.code != UNSUPPORTED
|
|
1498
|
+
):
|
|
1499
|
+
raise
|
|
1500
|
+
self.delivery = "poll"
|
|
1501
|
+
await self._poll(client)
|
|
1502
|
+
except Exception as error: # noqa: BLE001
|
|
1503
|
+
self._failure = error
|
|
1504
|
+
except Exception as error: # noqa: BLE001
|
|
1505
|
+
self._failure = self._failure or error
|
|
1506
|
+
finally:
|
|
1507
|
+
self._settled.set()
|
|
1508
|
+
self._send.close()
|
|
1513
1509
|
|
|
1514
1510
|
def _confirm(self) -> None:
|
|
1515
1511
|
self._active = True
|
|
@@ -1533,7 +1529,7 @@ class Subscription(Generic[D]):
|
|
|
1533
1529
|
if len(self._seen) > 1024:
|
|
1534
1530
|
del self._seen[next(iter(self._seen))]
|
|
1535
1531
|
data = occurrence.get("data")
|
|
1536
|
-
if (schema := self.
|
|
1532
|
+
if (schema := self._events._payloads.get(self._name)) is not None:
|
|
1537
1533
|
self._app._validate(
|
|
1538
1534
|
"events/" + self._name,
|
|
1539
1535
|
schema,
|
|
@@ -18,7 +18,8 @@ from starlette.applications import Starlette
|
|
|
18
18
|
from starlette.routing import Mount
|
|
19
19
|
|
|
20
20
|
from holon.client import Apps
|
|
21
|
-
from holon.ext
|
|
21
|
+
from holon.ext import events as events_module
|
|
22
|
+
from holon.ext.events import Events, NoCompatibleDeliveryMode
|
|
22
23
|
|
|
23
24
|
IDENTITY = {"name": "watcher", "version": "1", "requires": {"boards": {}}}
|
|
24
25
|
|
|
@@ -98,10 +99,6 @@ def test_push_delivers_what_matches_as_shaped_and_resumes_after_the_connection_d
|
|
|
98
99
|
events = Events()
|
|
99
100
|
began, ended = [], []
|
|
100
101
|
|
|
101
|
-
def on_subscribe(context, arguments, id, delivery):
|
|
102
|
-
began.append(id)
|
|
103
|
-
return lambda: ended.append(id)
|
|
104
|
-
|
|
105
102
|
changed = declare(
|
|
106
103
|
events,
|
|
107
104
|
match=lambda context, event, arguments: event.data.board == arguments.board,
|
|
@@ -109,12 +106,15 @@ def test_push_delivers_what_matches_as_shaped_and_resumes_after_the_connection_d
|
|
|
109
106
|
"board": event.data.board,
|
|
110
107
|
"text": event.data.text.upper(),
|
|
111
108
|
},
|
|
112
|
-
on_subscribe=
|
|
109
|
+
on_subscribe=lambda context, arguments, id: began.append(id),
|
|
110
|
+
on_unsubscribe=lambda context, arguments, id: ended.append(id),
|
|
113
111
|
)
|
|
114
112
|
server = MCPServer("boards", extensions=[events])
|
|
115
113
|
port, stop = await serve(mounted(server, events))
|
|
116
114
|
app = boards(port, user="ann")
|
|
117
|
-
async with app.events.subscribe(
|
|
115
|
+
async with app.ext.events.subscribe(
|
|
116
|
+
"board.changed", {"board": "b1"}
|
|
117
|
+
) as changes:
|
|
118
118
|
assert changes.delivery == "push"
|
|
119
119
|
changed.emit(Change(board="b2", text="elsewhere"))
|
|
120
120
|
changed.emit(Change(board="b1", text="first"))
|
|
@@ -138,8 +138,8 @@ def test_push_delivers_what_matches_as_shaped_and_resumes_after_the_connection_d
|
|
|
138
138
|
# A caller without an identity is nobody in particular: each stream is a subscription of its own.
|
|
139
139
|
anonymous = boards(port)
|
|
140
140
|
async with (
|
|
141
|
-
anonymous.events.subscribe("board.changed", {"board": "b1"}),
|
|
142
|
-
anonymous.events.subscribe("board.changed", {"board": "b1"}),
|
|
141
|
+
anonymous.ext.events.subscribe("board.changed", {"board": "b1"}),
|
|
142
|
+
anonymous.ext.events.subscribe("board.changed", {"board": "b1"}),
|
|
143
143
|
):
|
|
144
144
|
assert len(set(began[2:])) == 2
|
|
145
145
|
await stop()
|
|
@@ -147,16 +147,21 @@ def test_push_delivers_what_matches_as_shaped_and_resumes_after_the_connection_d
|
|
|
147
147
|
asyncio.run(asyncio.wait_for(exercise(), timeout=20))
|
|
148
148
|
|
|
149
149
|
|
|
150
|
-
def test_a_reader_that_falls_behind_what_is_kept_is_told_of_a_gap_and_goes_on(
|
|
150
|
+
def test_a_reader_that_falls_behind_what_is_kept_is_told_of_a_gap_and_goes_on(
|
|
151
|
+
monkeypatch,
|
|
152
|
+
):
|
|
153
|
+
monkeypatch.setattr(events_module, "_KEEP_EVENTS", 50)
|
|
154
|
+
monkeypatch.setattr(events_module, "_KEEP_BYTES", 2**30)
|
|
155
|
+
|
|
151
156
|
async def exercise():
|
|
152
|
-
events = Events(
|
|
157
|
+
events = Events()
|
|
153
158
|
changed = declare(events)
|
|
154
159
|
server = MCPServer("boards", extensions=[events])
|
|
155
160
|
port, stop = await serve(mounted(server, events))
|
|
156
161
|
received: list[str] = []
|
|
157
162
|
gaps = []
|
|
158
163
|
app = boards(port)
|
|
159
|
-
async with app.events.subscribe(
|
|
164
|
+
async with app.ext.events.subscribe(
|
|
160
165
|
"board.changed", {"board": "b1"}, on_gap=lambda: gaps.append(len(received))
|
|
161
166
|
) as changes:
|
|
162
167
|
# Far more than the connection holds while nobody reads.
|
|
@@ -175,16 +180,20 @@ def test_a_reader_that_falls_behind_what_is_kept_is_told_of_a_gap_and_goes_on():
|
|
|
175
180
|
asyncio.run(asyncio.wait_for(exercise(), timeout=30))
|
|
176
181
|
|
|
177
182
|
|
|
178
|
-
def test_poll_reports_a_gap_before_the_events_after_it_and_after_a_restart(
|
|
183
|
+
def test_poll_reports_a_gap_before_the_events_after_it_and_after_a_restart(
|
|
184
|
+
monkeypatch,
|
|
185
|
+
):
|
|
186
|
+
monkeypatch.setattr(events_module, "_KEEP_EVENTS", 2)
|
|
187
|
+
|
|
179
188
|
async def exercise():
|
|
180
|
-
before = Events(
|
|
189
|
+
before = Events()
|
|
181
190
|
changed = declare(before)
|
|
182
191
|
port, stop = await serve(
|
|
183
192
|
mounted(MCPServer("boards", extensions=[before]), before)
|
|
184
193
|
)
|
|
185
194
|
order = []
|
|
186
195
|
app = boards(port)
|
|
187
|
-
async with app.events.subscribe(
|
|
196
|
+
async with app.ext.events.subscribe(
|
|
188
197
|
"board.changed",
|
|
189
198
|
{"board": "b1"},
|
|
190
199
|
delivery="poll",
|
|
@@ -237,9 +246,12 @@ def request(method, params, id=1):
|
|
|
237
246
|
}
|
|
238
247
|
|
|
239
248
|
|
|
240
|
-
def test_the_messages_on_the_wire_are_the_sketchs():
|
|
249
|
+
def test_the_messages_on_the_wire_are_the_sketchs(monkeypatch):
|
|
250
|
+
monkeypatch.setattr(events_module, "_HEARTBEAT", 0.2)
|
|
251
|
+
monkeypatch.setattr(events_module, "_MAX_SUBSCRIPTIONS", 3)
|
|
252
|
+
|
|
241
253
|
async def exercise():
|
|
242
|
-
events = Events(
|
|
254
|
+
events = Events()
|
|
243
255
|
|
|
244
256
|
def shaped(context, event, arguments):
|
|
245
257
|
if event.data.text == "bad":
|
|
@@ -382,7 +394,7 @@ def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
382
394
|
heartbeat = json.loads((await anext(frames)).data)
|
|
383
395
|
assert heartbeat["method"] == "notifications/events/heartbeat"
|
|
384
396
|
assert heartbeat["params"]["cursor"] == changed.cursor()
|
|
385
|
-
changed.
|
|
397
|
+
changed.terminate()
|
|
386
398
|
terminated = json.loads((await anext(frames)).data)
|
|
387
399
|
assert terminated["method"] == "notifications/events/terminated"
|
|
388
400
|
assert terminated["params"]["error"] == {
|
|
@@ -396,16 +408,24 @@ def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
396
408
|
asyncio.run(asyncio.wait_for(exercise(), timeout=20))
|
|
397
409
|
|
|
398
410
|
|
|
399
|
-
def test_a_poll_that_gives_up_while_it_is_set_up_still_lets_its_lease_lapse(
|
|
411
|
+
def test_a_poll_that_gives_up_while_it_is_set_up_still_lets_its_lease_lapse(
|
|
412
|
+
monkeypatch,
|
|
413
|
+
):
|
|
414
|
+
monkeypatch.setattr(events_module, "_POLL_SUBSCRIPTION_TTL", 0.3)
|
|
415
|
+
monkeypatch.setattr(events_module, "_MAX_SUBSCRIPTIONS", 1)
|
|
416
|
+
|
|
400
417
|
async def exercise():
|
|
401
|
-
events = Events(
|
|
418
|
+
events = Events()
|
|
402
419
|
ended = []
|
|
403
420
|
|
|
404
|
-
async def on_subscribe(context, arguments, id
|
|
421
|
+
async def on_subscribe(context, arguments, id):
|
|
405
422
|
await asyncio.sleep(0.3)
|
|
406
|
-
return lambda: ended.append(id)
|
|
407
423
|
|
|
408
|
-
declare(
|
|
424
|
+
declare(
|
|
425
|
+
events,
|
|
426
|
+
on_subscribe=on_subscribe,
|
|
427
|
+
on_unsubscribe=lambda context, arguments, id: ended.append(id),
|
|
428
|
+
)
|
|
409
429
|
server = MCPServer("boards", extensions=[events])
|
|
410
430
|
port, stop = await serve(mounted(server, events))
|
|
411
431
|
url = f"http://127.0.0.1:{port}/mcp/"
|
|
@@ -452,23 +472,34 @@ def test_where_push_cannot_be_served_it_is_not_offered_and_a_subscription_polls(
|
|
|
452
472
|
assert listed["result"]["events"][0]["delivery"] == ["poll"]
|
|
453
473
|
stream = await http.post(url, **request("events/stream", params))
|
|
454
474
|
assert stream.json()["error"]["code"] == -32026
|
|
455
|
-
async with boards(port, path).events.subscribe(
|
|
475
|
+
async with boards(port, path).ext.events.subscribe(
|
|
456
476
|
"board.changed", {"board": "b1"}
|
|
457
477
|
) as changes:
|
|
458
478
|
assert changes.delivery == "poll"
|
|
459
479
|
changed.emit(Change(board="b1", text="polled"))
|
|
460
480
|
assert (await anext(changes)).data["text"] == "polled"
|
|
481
|
+
await changes.cancel()
|
|
482
|
+
assert await anext(changes, None) is None
|
|
483
|
+
with pytest.raises(NoCompatibleDeliveryMode):
|
|
484
|
+
async with boards(port, path).ext.events.subscribe(
|
|
485
|
+
"board.changed", {"board": "b1"}, delivery="push"
|
|
486
|
+
):
|
|
487
|
+
pass
|
|
461
488
|
await stop()
|
|
462
489
|
|
|
463
490
|
asyncio.run(asyncio.wait_for(exercise(), timeout=20))
|
|
464
491
|
|
|
465
492
|
|
|
466
|
-
def test_a_subscribe_hook_that_does_not_return_in_time_refuses_and_releases_the_id(
|
|
493
|
+
def test_a_subscribe_hook_that_does_not_return_in_time_refuses_and_releases_the_id(
|
|
494
|
+
monkeypatch,
|
|
495
|
+
):
|
|
496
|
+
monkeypatch.setattr(events_module, "_SUBSCRIBE_TIMEOUT", 0.2)
|
|
497
|
+
|
|
467
498
|
async def exercise():
|
|
468
|
-
events = Events(
|
|
499
|
+
events = Events()
|
|
469
500
|
calls = []
|
|
470
501
|
|
|
471
|
-
async def on_subscribe(context, arguments, id
|
|
502
|
+
async def on_subscribe(context, arguments, id):
|
|
472
503
|
calls.append(id)
|
|
473
504
|
if len(calls) == 1:
|
|
474
505
|
await asyncio.Event().wait()
|
|
@@ -536,7 +567,7 @@ def test_a_stream_is_served_on_a_stateless_endpoint_that_answers_in_json():
|
|
|
536
567
|
# Changed after the snapshot, in one synchronous run.
|
|
537
568
|
changed.emit(CellAdded(cell="b"))
|
|
538
569
|
changed.emit(CellRemoved(cell="a"))
|
|
539
|
-
async with notebook.events.subscribe(
|
|
570
|
+
async with notebook.ext.events.subscribe(
|
|
540
571
|
"notebook.changed", {}, cursor=state["cursor"]
|
|
541
572
|
) as changes:
|
|
542
573
|
assert changes.delivery == "push"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|