Skip to content

Commit f48af72

Browse files
rustyconoverclaude
andcommitted
rpc: surface protocol implementation on CallContext
Add ``implementation`` to CallContext so framework-driven callbacks (producer-mode stream states, on_cancel hooks) can dispatch helper calls back through the public protocol surface without holding their own reference to the impl instance. Includes any meta-worker that the top-level RpcServer is fronting. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3a90f81 commit f48af72

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

vgi_rpc/http/server/_app_stream.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def _run_stream_init_sync(
225225
method_name=method_name,
226226
protocol_name=protocol_name,
227227
kind=app._server.transport_kind,
228+
implementation=app._server.implementation,
228229
)
229230

230231
# The chain-correlation id for all HTTP turns of this stream. Carried
@@ -492,6 +493,7 @@ def _run_stream_exchange_sync(
492493
method_name=method_name,
493494
protocol_name=protocol_name,
494495
kind=app._server.transport_kind,
496+
implementation=app._server.implementation,
495497
)
496498
try:
497499
state_obj.on_cancel(cancel_ctx)
@@ -641,6 +643,7 @@ def _run_http_exchange_turn(
641643
method_name=method_name,
642644
protocol_name=protocol_name,
643645
kind=app._server.transport_kind,
646+
implementation=app._server.implementation,
644647
)
645648
state.process(ab_in, out, process_ctx)
646649
if not out.finished:
@@ -831,6 +834,7 @@ def _emit_to_current(msg: Message) -> None:
831834
method_name=method_name,
832835
protocol_name=protocol_name,
833836
kind=app._server.transport_kind,
837+
implementation=app._server.implementation,
834838
)
835839
try:
836840
while True:

vgi_rpc/http/server/_app_unary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def _run_unary_sync(
121121
method_name=method_name,
122122
protocol_name=protocol_name,
123123
kind=app._server.transport_kind,
124+
implementation=app._server.implementation,
124125
)
125126

126127
schema = info.result_schema

vgi_rpc/rpc/_common.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class CallContext:
172172
"_server_id",
173173
"auth",
174174
"emit_client_log",
175+
"implementation",
175176
"kind",
176177
"transport_metadata",
177178
)
@@ -186,12 +187,24 @@ def __init__(
186187
method_name: str = "",
187188
protocol_name: str = "",
188189
kind: TransportKind | None = None,
190+
implementation: Any = None,
189191
) -> None:
190-
"""Initialize with auth context, client-log callback, and optional server context fields."""
192+
"""Initialize with auth context, client-log callback, and optional server context fields.
193+
194+
Args:
195+
implementation: The protocol implementation object the
196+
RpcServer was constructed with. Surfaced on the context so
197+
producer-mode stream states (and other framework-driven
198+
callbacks that lack direct access to the implementation
199+
instance) can dispatch helper calls back through the
200+
public protocol surface — including any meta-worker
201+
dispatching the top-level RpcServer is fronting.
202+
"""
191203
self.auth = auth
192204
self.emit_client_log = emit_client_log
193205
self.transport_metadata: Mapping[str, Any] = transport_metadata or {}
194206
self.kind: TransportKind | None = kind
207+
self.implementation: Any = implementation
195208
self._server_id = server_id
196209
self._method_name = method_name
197210
self._protocol_name = protocol_name

vgi_rpc/rpc/_server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def _prepare_method_call(
630630
method_name=info.name,
631631
protocol_name=self.protocol_name,
632632
kind=self._transport_kind,
633+
implementation=self._impl,
633634
)
634635
return sink, auth, transport_metadata
635636

@@ -818,6 +819,7 @@ def _serve_stream(
818819
method_name=info.name,
819820
protocol_name=protocol_name,
820821
kind=self._transport_kind,
822+
implementation=self._impl,
821823
)
822824
try:
823825
state.on_cancel(cancel_ctx)
@@ -862,6 +864,7 @@ def _serve_stream(
862864
method_name=info.name,
863865
protocol_name=protocol_name,
864866
kind=self._transport_kind,
867+
implementation=self._impl,
865868
)
866869
state.process(ab_in, out, process_ctx)
867870
if not out.finished:

0 commit comments

Comments
 (0)