Class Cancellation

java.lang.Object
ai.mindconnect.common.Cancellation

public final class Cancellation extends Object
Cooperative-plus-hard cancellation handle for an in-flight operation.

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.

  • Constructor Details

    • Cancellation

      public Cancellation()
  • Method Details

    • registerAbort

      public Cancellation.Registration registerAbort(Runnable abortHook)
      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

      public static Cancellation none()
      Returns a no-op cancellation; useful for callers that don't need cancel.