Repository navigation
Description
Problem
The synchronous gRPC streaming implementation supports existing applications and has received latency optimizations, but its compatibility path uses per-stream waiting machinery. The SDK also lacks a public, push-style asynchronous server-streaming contract. Merely moving a blocking ServerStreamSession::Next() call to another worker thread would preserve that scalability limitation.
Goal
Introduce an opt-in async streaming API and native gRPC callback-reactor streaming transport for SendStreamingMessage and SubscribeToTask. Preserve existing synchronous ServerStreamSession, AgentExecutor, Dispatcher, GrpcServerTransport and registration patterns.
Ownership and issue boundaries (v0.6.0)
Owns: Async server stream/session contract, demand and bounded backpressure, shared task subscription publication, and native gRPC callback reactors for both streaming operations.
Prerequisites and integration: Reuse #268 PR 1 transport-neutral cancellation/deadline model before finalizing the session contract; do not wait for #268 gRPC unary. #270 SSE subsequently consumes the stream API.
Must not absorb: Do not create a second cancellation context, blocking per-stream pump, HTTP/SSE socket handling (#270) or async unary dispatcher (#268). Maintain sync subscriptions.
Issue management: Keep this single issue; each checkbox below is intended as a separately reviewable PR. Do not implement later checkpoints in an earlier PR just to close the issue faster. Close the issue only after the final acceptance criteria pass.
Implementation PR checklist
- PR 1 — Stream/session API and fake-sink tests: ownership, demand, ordered events, terminal outcome and cancellation races.
- PR 2 — Shared task publication/subscription integration with mixed sync/async consumers and bounded buffering.
- PR 3 — Native gRPC callback reactors for SendStreamingMessage and SubscribeToTask, with one in-flight write and once-only Finish/cleanup.
- PR 4 — Native concurrency, slow-consumer, cancel/shutdown/interoperability tests, c64 thread/latency evidence, and opt-in docs.
PR merge rule: Each PR must have a narrow diff, behavior/lifecycle tests for its scope, and the applicable repository checks from AGENTS.md. Benchmark performance-sensitive changes against equivalent baseline configurations. Never describe blocking worker adaptation as non-blocking network/database I/O.
Detailed technical requirements and implementation phases
Phase 1 — Define and test the streaming API
- Define
AsyncServerStreamSession(or a similarly clear abstraction) and an opt-in async executor interface. The API must specify event ownership, one outstanding write/demand, event order, terminal completion, cancellation, and error propagation. - Prefer a single comprehensible completion model over mixing unrelated callbacks, futures and coroutines. The chosen design must compose with the request cancellation/deadline concepts of [Enhancement]: Add opt-in async unary server execution and cancellation #268 without making [Enhancement]: Add opt-in async unary server execution and cancellation #268's gRPC implementation a prerequisite.
- Define how synchronous and asynchronous task subscriptions share the existing task publication core without duplicating stored event/history semantics.
- Ensure cancellation and completion racing one another have exactly-once outcome semantics; callback/session lifetime is explicit.
- Build contract tests using a fake transport/sink before changing gRPC transport scheduling.
Checkpoint: The API can be reviewed and tested without gRPC network changes.
Phase 2 — Native gRPC callback transport
- Add a parallel asynchronous gRPC server path using callback reactors for the two streaming RPCs. Do not replace the synchronous transport.
- Drive one write at a time, obey backpressure/demand, preserve event ordering, and send terminal status exactly once.
- Propagate
OnCancel()into the session promptly; callFinish()once and keep cleanup inOnDone()with safe object lifetime. - No blocking
Next(), polling, sleep, watcher/pump thread, or application callback that blocks a gRPC callback thread. - Share metadata, required-extension handling, validation, protobuf conversion and error mapping with the synchronous path.
Checkpoint: The two async gRPC streams work correctly while legacy gRPC streams continue using their existing implementation.
Phase 3 — Interoperability and scalability
- Exercise both sync and async subscription consumers against the same task publication flow.
- Cover finite streams, long-lived subscriptions, concurrent stream cancellation, client disconnect, completion/cancel races, shutdown, and a slow consumer.
- Validate thread scaling at representative concurrency (including c64), zero errors, first-event and completion latency; compare against the synchronous baseline with equivalent event counts.
- Document how applications opt in and what API guarantees are provided.
Relationship to other issues
- #270 owns the HTTP socket runtime and SSE framing. It may consume this async streaming API, but neither issue should implement the other's transport.
- #268 owns async unary execution and general request cancellation/deadline semantics. Keep shared cancellation vocabulary consistent without combining implementations.
- #269 concerns outbound clients, not server stream production.
Out of scope
- Removing synchronous interfaces or requiring existing executors to implement async methods.
- Creating a worker/pump thread per stream to adapt blocking sessions.
- Implementing async unary RPCs, event-driven HTTP socket handling, or async PostgreSQL stores.
- Redesigning the entire task/subscription model unless a narrowly justified shared invariant requires it.
Acceptance criteria
- Existing synchronous streaming APIs and applications remain source-compatible.
- Opt-in async streaming supports demand, ordered events, errors, cancellation, terminal delivery and safe shutdown.
- Async gRPC uses callback reactors for both streaming operations, with at most one outstanding write per stream.
- No per-stream SDK worker/watcher or blocking gRPC reactor operations exist on the async path.
- Cancellation is immediate and idempotent; finish/cleanup happen exactly once under races.
- Task subscriptions work in sync and async configurations using shared publication semantics.
- Native concurrency and interoperability tests, mandatory TCK and required performance checks pass.
- Benchmark evidence demonstrates bounded thread growth and documents the existing p95 ≤ 10 ms async completion target (or explicitly explains any inability to meet it under a comparable workload).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn Review
Activity
perf(http): move default streaming to a shared event-driven reactor (#…
perf(streaming): reduce synchronous streaming startup and first-event…
[-][Enhancement]: Add an opt-in asynchronous server-streaming API and gRPC callback transport[/-][+][Enhancement]: Add opt-in async server streaming and gRPC callback support[/+]feat(server): complete opt-in async unary execution and cancellation (#…
feat(server): add coroutine streaming and gRPC reactors (#321)