holon-sh 0.0.1.dev17__tar.gz → 0.0.1.dev18__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.dev17 → holon_sh-0.0.1.dev18}/PKG-INFO +1 -1
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/events/_server.py +49 -52
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/pyproject.toml +1 -1
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_events.py +19 -26
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/.gitignore +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/LICENSE +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/README.md +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/client.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/context.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/__init__.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/events/__init__.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/events/_client.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/events/_wire.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/files/__init__.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/files/_client.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/files/_server.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/ext/files/_wire.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/py.typed +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/holon/serve.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/scripts/verify_distribution.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_context.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_elicit.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_files.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_package.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_runtime.py +0 -0
- {holon_sh-0.0.1.dev17 → holon_sh-0.0.1.dev18}/tests/test_serve.py +0 -0
|
@@ -36,7 +36,7 @@ from mcp.types import (
|
|
|
36
36
|
from pydantic import Field, StrictInt, StrictStr, TypeAdapter, ValidationError
|
|
37
37
|
from pydantic_core import to_json, to_jsonable_python
|
|
38
38
|
|
|
39
|
-
from holon.context import
|
|
39
|
+
from holon.context import get_context
|
|
40
40
|
from holon.ext.events._wire import (
|
|
41
41
|
EVENTS_EXTENSION,
|
|
42
42
|
FORBIDDEN,
|
|
@@ -68,12 +68,13 @@ A = TypeVar("A")
|
|
|
68
68
|
T = TypeVar("T")
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
Context: TypeAlias = ServerRequestContext[Any, Any]
|
|
72
|
+
Match: TypeAlias = Callable[[Context, Event[D], A], bool]
|
|
73
|
+
Transform: TypeAlias = Callable[[Context, Event[D], A], object]
|
|
74
|
+
Authorize: TypeAlias = Callable[[Context, A], bool | Awaitable[bool]]
|
|
74
75
|
Cleanup: TypeAlias = Callable[[], None | Awaitable[None]]
|
|
75
76
|
SubscriptionHook: TypeAlias = Callable[
|
|
76
|
-
[
|
|
77
|
+
[Context, A, str], Cleanup | None | Awaitable[Cleanup | None]
|
|
77
78
|
]
|
|
78
79
|
|
|
79
80
|
|
|
@@ -116,7 +117,6 @@ class _Request:
|
|
|
116
117
|
|
|
117
118
|
type: EventType[Any, Any]
|
|
118
119
|
id: str
|
|
119
|
-
context: RequestContext
|
|
120
120
|
caller: str
|
|
121
121
|
arguments: Any
|
|
122
122
|
targetable: bool
|
|
@@ -135,8 +135,8 @@ class _Kept:
|
|
|
135
135
|
class _Entry:
|
|
136
136
|
"""A subscription, held by one or more streams or poll leases."""
|
|
137
137
|
|
|
138
|
-
setup: asyncio.Future[Cleanup | None]
|
|
139
138
|
holds: set[_Hold] = field(default_factory=set)
|
|
139
|
+
setup: asyncio.Future[Cleanup | None] | None = None
|
|
140
140
|
teardown: asyncio.Future[None] | None = None
|
|
141
141
|
|
|
142
142
|
|
|
@@ -295,16 +295,18 @@ class EventType(Generic[A, D]):
|
|
|
295
295
|
return sequence, truncated
|
|
296
296
|
|
|
297
297
|
# The occurrence as `request` receives it, or None when it is not for it.
|
|
298
|
-
def _shape(
|
|
298
|
+
def _shape(
|
|
299
|
+
self, kept: _Kept, request: _Request, ctx: Context
|
|
300
|
+
) -> dict[str, Any] | None:
|
|
299
301
|
if kept.target is not None:
|
|
300
302
|
if not request.targetable or kept.target != request.id:
|
|
301
303
|
return None
|
|
302
304
|
elif self._match is not None and not self._match(
|
|
303
|
-
|
|
305
|
+
ctx, kept.event, request.arguments
|
|
304
306
|
):
|
|
305
307
|
return None
|
|
306
308
|
data = (
|
|
307
|
-
self._transform(
|
|
309
|
+
self._transform(ctx, kept.event, request.arguments)
|
|
308
310
|
if self._transform
|
|
309
311
|
else kept.event.data
|
|
310
312
|
)
|
|
@@ -319,6 +321,7 @@ class EventType(Generic[A, D]):
|
|
|
319
321
|
def _since(
|
|
320
322
|
self,
|
|
321
323
|
request: _Request,
|
|
324
|
+
ctx: Context,
|
|
322
325
|
cursor: str,
|
|
323
326
|
max_age_ms: int | None,
|
|
324
327
|
limit: int,
|
|
@@ -329,7 +332,7 @@ class EventType(Generic[A, D]):
|
|
|
329
332
|
while len(events) < limit and (kept := self._next(reached)) is not None:
|
|
330
333
|
reached = kept.sequence
|
|
331
334
|
try:
|
|
332
|
-
occurrence = self._shape(kept, request)
|
|
335
|
+
occurrence = self._shape(kept, request, ctx)
|
|
333
336
|
except Exception as error: # noqa: BLE001
|
|
334
337
|
_described(error)
|
|
335
338
|
continue
|
|
@@ -342,10 +345,8 @@ class EventType(Generic[A, D]):
|
|
|
342
345
|
"hasMore": self._next(reached) is not None,
|
|
343
346
|
}
|
|
344
347
|
|
|
345
|
-
async def _allows(self,
|
|
346
|
-
return self._authorize is None or await _settle(
|
|
347
|
-
self._authorize(context, arguments)
|
|
348
|
-
)
|
|
348
|
+
async def _allows(self, ctx: Context, arguments: Any) -> bool:
|
|
349
|
+
return self._authorize is None or await _settle(self._authorize(ctx, arguments))
|
|
349
350
|
|
|
350
351
|
async def _settled(self, id: str) -> None:
|
|
351
352
|
while (entry := self._subscriptions.get(id)) and entry.teardown is not None:
|
|
@@ -386,7 +387,9 @@ class Events(Extension):
|
|
|
386
387
|
) -> EventType[A, D]:
|
|
387
388
|
"""Declare an event type.
|
|
388
389
|
|
|
389
|
-
Annotate the result as ``EventType[Arguments, Data]`` to type ``emit``.
|
|
390
|
+
Annotate the result as ``EventType[Arguments, Data]`` to type ``emit``. Each hook's ``context`` is the server's
|
|
391
|
+
context of the request it runs in. A subscription outlives the first request to name it, where ``on_subscribe``
|
|
392
|
+
runs, and other requests may share it: keep what the hook reads from ``context``, not ``context``.
|
|
390
393
|
|
|
391
394
|
Args:
|
|
392
395
|
input_schema: Type of a subscription's arguments.
|
|
@@ -479,13 +482,11 @@ class Events(Extension):
|
|
|
479
482
|
result["nextCursor"] = str(start + _LIST_PAGE_SIZE)
|
|
480
483
|
return result
|
|
481
484
|
|
|
482
|
-
async def _poll(
|
|
483
|
-
|
|
484
|
-
) -> HandlerResult:
|
|
485
|
-
request = await self._subscription(params, "poll")
|
|
485
|
+
async def _poll(self, ctx: Context, params: _SubscribeParams) -> HandlerResult:
|
|
486
|
+
request = await self._subscription(ctx, params, "poll")
|
|
486
487
|
declared = request.type
|
|
487
488
|
opened = declared._sequence
|
|
488
|
-
await self._lease(request)
|
|
489
|
+
await self._lease(request, ctx)
|
|
489
490
|
if params.cursor is None:
|
|
490
491
|
answer = {
|
|
491
492
|
"events": [],
|
|
@@ -496,6 +497,7 @@ class Events(Extension):
|
|
|
496
497
|
else:
|
|
497
498
|
answer = declared._since(
|
|
498
499
|
request,
|
|
500
|
+
ctx,
|
|
499
501
|
params.cursor,
|
|
500
502
|
params.max_age_ms,
|
|
501
503
|
params.max_events or _MAX_POLL_EVENTS,
|
|
@@ -518,7 +520,7 @@ class Events(Extension):
|
|
|
518
520
|
|
|
519
521
|
# Checked in the order of https://github.com/modelcontextprotocol/experimental-ext-triggers-events/blob/main/docs/design-sketch-proposal.md#error-codes
|
|
520
522
|
async def _subscription(
|
|
521
|
-
self, params: _SubscribeParams, delivery: Delivery
|
|
523
|
+
self, ctx: Context, params: _SubscribeParams, delivery: Delivery
|
|
522
524
|
) -> _Request:
|
|
523
525
|
self._loop = self._loop or asyncio.get_running_loop()
|
|
524
526
|
declared = self._types.get(params.name)
|
|
@@ -540,8 +542,7 @@ class Events(Extension):
|
|
|
540
542
|
raise MCPError(
|
|
541
543
|
INVALID_PARAMS, f"Invalid arguments for {params.name}: {problems}"
|
|
542
544
|
) from None
|
|
543
|
-
|
|
544
|
-
principal = context.principal
|
|
545
|
+
principal = get_context().principal
|
|
545
546
|
caller = ""
|
|
546
547
|
if principal is not None:
|
|
547
548
|
who = [
|
|
@@ -567,12 +568,11 @@ class Events(Extension):
|
|
|
567
568
|
request = _Request(
|
|
568
569
|
declared,
|
|
569
570
|
id,
|
|
570
|
-
context,
|
|
571
571
|
caller,
|
|
572
572
|
arguments,
|
|
573
573
|
targetable=principal is not None or delivery == "push",
|
|
574
574
|
)
|
|
575
|
-
if not await declared._allows(
|
|
575
|
+
if not await declared._allows(ctx, arguments):
|
|
576
576
|
declared.terminate(id, _FORBIDDEN)
|
|
577
577
|
if lease := self._leases.get(id):
|
|
578
578
|
self._stop(lease)
|
|
@@ -597,27 +597,26 @@ class Events(Extension):
|
|
|
597
597
|
},
|
|
598
598
|
)
|
|
599
599
|
self._held[request.caller] += 1
|
|
600
|
-
|
|
601
|
-
entry = subscriptions.get(request.id)
|
|
602
|
-
if entry is None:
|
|
603
|
-
setup = asyncio.ensure_future(self._set_up(request))
|
|
604
|
-
entry = subscriptions[request.id] = _Entry(setup)
|
|
600
|
+
entry = request.type._subscriptions.setdefault(request.id, _Entry())
|
|
605
601
|
hold = _Hold(request, entry)
|
|
606
602
|
entry.holds.add(hold)
|
|
607
603
|
return hold
|
|
608
604
|
|
|
609
|
-
async def _set_up(self, request: _Request) -> Cleanup | None:
|
|
605
|
+
async def _set_up(self, request: _Request, ctx: Context) -> Cleanup | None:
|
|
610
606
|
if request.type._on_subscribe is None:
|
|
611
607
|
return None
|
|
612
608
|
async with asyncio.timeout(_SUBSCRIBE_TIMEOUT):
|
|
613
609
|
return await _settle(
|
|
614
|
-
request.type._on_subscribe(
|
|
615
|
-
request.context, request.arguments, request.id
|
|
616
|
-
)
|
|
610
|
+
request.type._on_subscribe(ctx, request.arguments, request.id)
|
|
617
611
|
)
|
|
618
612
|
|
|
619
|
-
async def _ready(self, hold: _Hold) -> None:
|
|
620
|
-
|
|
613
|
+
async def _ready(self, hold: _Hold, ctx: Context) -> None:
|
|
614
|
+
entry = hold.entry
|
|
615
|
+
while entry.setup is not None and not entry.setup.done():
|
|
616
|
+
await asyncio.wait([entry.setup])
|
|
617
|
+
if entry.setup is None or entry.setup.cancelled():
|
|
618
|
+
entry.setup = asyncio.ensure_future(self._set_up(hold.request, ctx))
|
|
619
|
+
await entry.setup
|
|
621
620
|
if hold.ended is not None:
|
|
622
621
|
raise MCPError.from_error_data(hold.ended)
|
|
623
622
|
|
|
@@ -632,18 +631,20 @@ class Events(Extension):
|
|
|
632
631
|
|
|
633
632
|
# Cleanup follows only a setup that succeeded; a failing hook is logged.
|
|
634
633
|
async def _tear_down(self, hold: _Hold) -> None:
|
|
635
|
-
request = hold.request
|
|
634
|
+
request, setup = hold.request, hold.entry.setup
|
|
636
635
|
try:
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
636
|
+
if setup is not None:
|
|
637
|
+
await asyncio.wait([setup])
|
|
638
|
+
cleanup = None if setup.cancelled() else setup.result()
|
|
639
|
+
if cleanup is not None:
|
|
640
|
+
await _settle(cleanup())
|
|
640
641
|
except Exception:
|
|
641
642
|
logger.exception("An event subscription's setup or cleanup failed")
|
|
642
643
|
finally:
|
|
643
644
|
del request.type._subscriptions[request.id]
|
|
644
645
|
|
|
645
646
|
# A poll lease begins with the first poll, is renewed by each, and lapses when polls stop.
|
|
646
|
-
async def _lease(self, request: _Request) -> None:
|
|
647
|
+
async def _lease(self, request: _Request, ctx: Context) -> None:
|
|
647
648
|
await request.type._settled(request.id)
|
|
648
649
|
hold = self._leases.get(request.id)
|
|
649
650
|
if hold is None:
|
|
@@ -655,7 +656,7 @@ class Events(Extension):
|
|
|
655
656
|
_POLL_SUBSCRIPTION_TTL, self._stop, hold
|
|
656
657
|
)
|
|
657
658
|
try:
|
|
658
|
-
await self._ready(hold)
|
|
659
|
+
await self._ready(hold, ctx)
|
|
659
660
|
except Exception:
|
|
660
661
|
self._stop(hold)
|
|
661
662
|
raise
|
|
@@ -684,16 +685,14 @@ class Events(Extension):
|
|
|
684
685
|
self._sent.set()
|
|
685
686
|
self._sent = asyncio.Event()
|
|
686
687
|
|
|
687
|
-
async def _stream(
|
|
688
|
-
|
|
689
|
-
) -> HandlerResult:
|
|
690
|
-
request = await self._subscription(params, "push")
|
|
688
|
+
async def _stream(self, ctx: Context, params: _SubscribeParams) -> HandlerResult:
|
|
689
|
+
request = await self._subscription(ctx, params, "push")
|
|
691
690
|
declared = request.type
|
|
692
691
|
await declared._settled(request.id)
|
|
693
692
|
hold = self._open(request)
|
|
694
693
|
hold.seen = declared._sequence
|
|
695
694
|
try:
|
|
696
|
-
await self._ready(hold)
|
|
695
|
+
await self._ready(hold, ctx)
|
|
697
696
|
|
|
698
697
|
async def say(method: str, body: dict[str, Any], size: int = 0) -> None:
|
|
699
698
|
body["_meta"] = {SUBSCRIPTION_ID_META_KEY: ctx.request_id}
|
|
@@ -717,7 +716,7 @@ class Events(Extension):
|
|
|
717
716
|
async def send(kept: _Kept) -> None:
|
|
718
717
|
hold.seen = kept.sequence
|
|
719
718
|
try:
|
|
720
|
-
shaped = declared._shape(kept, request)
|
|
719
|
+
shaped = declared._shape(kept, request, ctx)
|
|
721
720
|
except Exception as error: # noqa: BLE001
|
|
722
721
|
await say("error", {"error": _described(error)})
|
|
723
722
|
return
|
|
@@ -732,9 +731,7 @@ class Events(Extension):
|
|
|
732
731
|
if time.monotonic() >= beat:
|
|
733
732
|
beat = time.monotonic() + _HEARTBEAT
|
|
734
733
|
try:
|
|
735
|
-
allowed = await declared._allows(
|
|
736
|
-
request.context, request.arguments
|
|
737
|
-
)
|
|
734
|
+
allowed = await declared._allows(ctx, request.arguments)
|
|
738
735
|
except Exception as error: # noqa: BLE001
|
|
739
736
|
await say("error", {"error": _described(error)})
|
|
740
737
|
continue
|
|
@@ -424,53 +424,46 @@ async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
424
424
|
|
|
425
425
|
|
|
426
426
|
@asynchronous
|
|
427
|
-
async def
|
|
427
|
+
async def test_a_poll_that_gives_up_while_it_is_set_up_stops_its_setup_and_holds_its_place_until_its_lease_lapses(
|
|
428
428
|
monkeypatch,
|
|
429
429
|
):
|
|
430
|
-
monkeypatch.setattr(_server, "_POLL_SUBSCRIPTION_TTL", 0.
|
|
430
|
+
monkeypatch.setattr(_server, "_POLL_SUBSCRIPTION_TTL", 0.5)
|
|
431
431
|
monkeypatch.setattr(_server, "_MAX_SUBSCRIPTIONS", 1)
|
|
432
|
-
|
|
432
|
+
ended, seen = asyncio.Event(), []
|
|
433
433
|
|
|
434
434
|
async def on_subscribe(context, arguments, id):
|
|
435
|
-
|
|
436
|
-
|
|
435
|
+
seen.append(context.request.headers.get("x-leaving"))
|
|
436
|
+
try:
|
|
437
|
+
if seen[-1]:
|
|
438
|
+
await asyncio.Event().wait()
|
|
439
|
+
except asyncio.CancelledError:
|
|
440
|
+
seen.append("stopped")
|
|
441
|
+
raise
|
|
437
442
|
|
|
438
443
|
async def cleanup():
|
|
439
444
|
ended.set()
|
|
440
445
|
|
|
441
446
|
return cleanup
|
|
442
447
|
|
|
443
|
-
events, _ = declare(on_subscribe=on_subscribe)
|
|
444
|
-
app, left = mounted(events), asyncio.Event()
|
|
445
|
-
|
|
446
|
-
# Tells when the server sees the poll that gives up disconnect.
|
|
447
|
-
async def noticing(scope, receive, send):
|
|
448
|
-
async def receiving():
|
|
449
|
-
message = await receive()
|
|
450
|
-
if message["type"] == "http.disconnect":
|
|
451
|
-
left.set()
|
|
452
|
-
return message
|
|
453
|
-
|
|
454
|
-
leaving = (b"x-leaving", b"1") in scope.get("headers", [])
|
|
455
|
-
await app(scope, receiving if leaving else receive, send)
|
|
456
|
-
|
|
457
448
|
async def give_up():
|
|
458
449
|
async with httpx2.AsyncClient(headers={"x-leaving": "1"}) as http:
|
|
459
450
|
await call(http, port, "events/poll", changed_on("b1"))
|
|
460
451
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
452
|
+
async with (
|
|
453
|
+
served(declare(on_subscribe=on_subscribe)[0]) as port,
|
|
454
|
+
httpx2.AsyncClient(timeout=10) as http,
|
|
455
|
+
):
|
|
456
|
+
giving_up = asyncio.ensure_future(give_up())
|
|
457
|
+
await until(lambda: seen)
|
|
465
458
|
exhausted = await call(http, port, "events/poll", changed_on("b2"))
|
|
466
459
|
limit = exhausted["error"]["data"]
|
|
467
460
|
assert (limit["limit"], limit["max"]) == ("subscriptions", 1)
|
|
468
461
|
giving_up.cancel()
|
|
469
|
-
await
|
|
470
|
-
|
|
462
|
+
await until(lambda: "stopped" in seen)
|
|
463
|
+
assert "result" in await call(http, port, "events/poll", changed_on("b1"))
|
|
464
|
+
assert seen == ["1", "stopped", None]
|
|
471
465
|
await ended.wait()
|
|
472
466
|
assert "result" in await call(http, port, "events/poll", changed_on("b2"))
|
|
473
|
-
await stop()
|
|
474
467
|
|
|
475
468
|
|
|
476
469
|
@asynchronous
|
|
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|