Class Cancellation
Originally the caller passed one to LlmGateway.chatStreaming: the
gateway registered an abortHook that closes the underlying HTTP call
so cancel() both flips the flag and tears down the network
connection. That's what makes the LLM cancel fast — the provider stops
generating tokens (and stops billing) as soon as the socket closes.
The same handle is now reused by the agent tool loop: each in-flight tool call registers a hook that interrupts its worker thread, so cancel propagates to bash subprocesses, OkHttp HTTP calls, and the tree-walking file tools.
Multiple hooks may register simultaneously; all are invoked on cancel().
A registration may be revoked via Cancellation.Registration.unregister() once its
operation completed normally (otherwise the hooks list would grow unbounded).
This handle is single-use. Once cancelled, it stays cancelled.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReturned byregisterAbort(Runnable)so callers can revoke their hook. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()Idempotent.booleanstatic Cancellationnone()Returns a no-op cancellation; useful for callers that don't need cancel.registerAbort(Runnable abortHook) Registers a hook to run on cancel.
-
Constructor Details
-
Cancellation
public Cancellation()
-
-
Method Details
-
registerAbort
Registers a hook to run on cancel. If cancel already fired, the hook runs immediately. Returns a handle the caller can use to unregister the hook after its operation completes normally — keeping the hook list bounded across many sequential tool calls on the same context. -
cancel
public void cancel()Idempotent. Sets the flag and runs every registered abort hook. -
isCancelled
public boolean isCancelled() -
none
Returns a no-op cancellation; useful for callers that don't need cancel.
-