Composing runs with tower
polylint-service wraps the polylint-sdk handle in a tower::Service. A host already built on
tower gets one composition point for a request timeout, a concurrency limit, load shedding,
tracing and retries, instead of a bespoke field for each. The crate is optional and separate on
purpose: poly-core, poly-host-services and polylint-sdk stay synchronous and free of tokio
and tower.
One service per operation
Section titled “One service per operation”The wire request does not name the operation, so a service is built for one of them. The service
holds the long-lived Linter; each Service::call is exactly one run, so the same value serves
many runs.
use std::time::Duration;
use polylint_sdk::{Linter, TrustMode};use polylint_service::RunService;use poly_frontend_api::wire::{RunCall, RunRequest};use tower::{Service, ServiceBuilder, ServiceExt};
let linter = Linter::builder(TrustMode::Enforce).offline(true).build();let mut service = ServiceBuilder::new() .timeout(Duration::from_secs(30)) .concurrency_limit(4) .service(RunService::lint(linter));
let call = RunCall { request: RunRequest { paths: vec!["src".to_owned()], ..RunRequest::default() }, ..RunCall::default()};let reply = (&mut service) .ready() .await .expect("a ready service") .call(call) .await?;
println!("{}", reply.json);RunService::format(linter) is the same shape for format.run. apply on the wire request means
--fix for a lint service and “write” for a format service. The reply is the same RunReply the
existing host services return: the document, its JSON and TOON renderings, and is_error.
A timeout really stops the run
Section titled “A timeout really stops the run”A run is blocking and CPU-bound, so the adapter starts it on tokio::task::spawn_blocking. tower
layers only stop polling the returned future; they cannot stop that blocking work on their own. Each
call therefore carries a RunControl and a guard that cancels it when the future is dropped before
completion:
- a
Timeoutlayer, or a dropped client request, cancels the run, which stops at its next checkpoint; - once the blocking task reports its result, the guard is disarmed, so a completed call is never cancelled after the fact;
- a cancelled call has no reply: the future is gone, and the caller gets whatever the layer that
dropped it returns (
tower::timeout::error::Elapsedfor a timeout, or the disconnect’s error). The adapter never exposes the control and sets no deadline, so the SDK’sError::Stoppedis not something a layer here can produce.
A timed-out apply call may still write
Section titled “A timed-out apply call may still write”Cancellation is cooperative: the run checks the control between files, between phases and before
every write. A call that times out while mutating (apply: true) can keep writing until its next
checkpoint, after the timeout has already answered. Do not put a retry policy around a mutating
call: the retry starts a second run over a tree the first may still be changing. poly-mcp waits
for its blocking task for the same reason. Dry runs have no such overlap.
Concurrency versus the worker budget
Section titled “Concurrency versus the worker budget”Admission control is a layer in front of the service, not a field on it:
ServiceBuilder::new().concurrency_limit(n). The worker budget inside a run stays the SDK’s
LinterBuilder::workers (ADR 0103). The two bound different resources: runs in flight, and threads
inside one run.
The service’s poll_ready is always ready, and readiness is not backpressure: a LoadShed layer
directly above the service never sheds anything, because it only observes that readiness. Stack it
over the limit — ServiceBuilder::new().load_shed().concurrency_limit(n) — so it sheds when the
limit is at capacity.
What the adapter refuses
Section titled “What the adapter refuses”The adapter maps the wire fields the SDK can represent. A field that would change what the run does
but has no SDK equivalent — workspace, include_excluded, fix_generated, show_suppressed,
unsafe_fixes, or a non-default install / on_missing — fails the call with
Error::Unsupported rather than being silently ignored, so a host never receives a green result
that verified less than it asked for. max_skips is carried into the reply’s budget.