Skip to content

Stream progress while keeping cancellation honest

Add a progress stream and cancellation path to Kestrel dispatch. Separate provisional text, accepted commands and confirmed carrier effects.

21 MIN

By AIAgentInterviews · Updated

TL;DR: Buffer tool arguments until a complete call passes validation, sequence progress events for reconnects, and treat cancellation as a request to stop future work. A dispatched side effect still needs reconciliation.

Where you are. Kestrel has durable state and scoped authority. Connect that state to user-visible progress and cancellation.

The dispatcher cancels after the carrier accepted

Kestrel displays “finding a replacement carrier” while its dispatch agent works. The dispatcher clicks cancel just as a booking request reaches the carrier. Closing the browser connection cannot undo that booking. A useful interface must distinguish stopping the conversation from reversing a business effect.

Give every run a state machine independent of the transport connection. Running, cancellation_requested, reconciling and terminal are different states. A terminal result names what completed, what did not start and what remains uncertain. The service owns that state; the model cannot announce cancellation as a substitute for checking it.

In plain language. A delta is a fragment of an in-progress message. A sequence number orders progress events for reconnection. Cancellation requests that work stop; it does not undo a service effect that has already committed.

A streamed delta is not a command

Models may stream text and fragments of tool arguments. Show provisional text only where the interface labels it as progress. Accumulate arguments by call identifier until the provider marks the call complete. Then parse, validate and authorize the whole call before dispatch. Never execute a refund or booking from a partial JSON fragment that happens to parse.

Do not stream unfiltered tool output or private reasoning to the user. Publish a small event contract: run identifier, sequence, event type, safe summary and optional artifact reference. Distinguish proposed, started and confirmed action events. “Booking requested” and “booking confirmed” must be backed by different service states.

Reconnect without replaying effects

Persist enough event history for a client to reconnect after its last sequence. Deduplicate rendered events by run and sequence. Transport replay must never call the action dispatcher again. An event stream describes work; it is not the command log that grants permission to perform it.

events = [(1, 'quote_started'), (2, 'quote_ready'),
          (2, 'quote_ready'), (3, 'approval_needed')]
last_seen = 1
shown = []
for sequence, label in events:
    if sequence > last_seen:
        shown.append(label)
        last_seen = sequence
print(shown)  # => ['quote_ready', 'approval_needed']

This client example assumes ordered delivery. A transport that can deliver out of order needs gap detection and resynchronization before advancing its cursor. The durable event source defines the ordering contract, not the browser's arrival timestamps.

Cancel at execution boundaries

Check cancellation before scheduling another model call, before dispatching a tool and after awaiting an observation. Signal in-flight work when its API supports cancellation, but do not assume the signal reversed anything. An already submitted carrier booking must be queried by its operation identifier and reconciled. If cancellation requires a compensating cancellation request, that is a new business action with its own permission and result.

A disconnected client does not necessarily mean the user canceled. Define whether the task continues in the background and show the durable result on return. Conversely, a deliberate cancel request should be recorded even if the client disappears before seeing its acknowledgment.

Keep one authoritative ending

Fence stale workers so they cannot append a second terminal result after a replacement worker has taken over. A late booking receipt may update reconciliation evidence through the permitted transition, but should not silently revive an abandoned run. The dispatcher needs a clear record of the remaining effect.

Test disconnect and cancellation at every boundary around dispatch. Prefer an honest “booking status still being checked” to a clean-looking canceled label that conceals a live booking.

Diagram
Cancellation crosses a business boundary

The UI stops waiting before the carrier necessarily stops working.

Put the cancel event on the timeline

Use three illustrative event orders for the same carrier booking:

Event orderRequired outcome
Cancel, then complete proposalReject dispatch of that proposal
Dispatch, then cancel, then receiptReconcile the accepted booking
Dispatch, lost response, then cancelKeep outcome unknown until reconciled

The customer can see the same “cancel” click in all three cases, but the system owes a different report. Saying “cancelled” immediately after setting a local flag would conceal a booking that already exists.

Keep progress delivery separate from operation execution. Reconnecting a browser may replay event 12 so the interface catches up. It must not replay the carrier operation represented by event 12. The event log describes work; it is not a queue of commands to execute again.

Likewise, streamed argument fragments are not complete proposals. An early fragment may contain a shipment ID while a later fragment changes the carrier or amount. Buffer by call identity, wait for completion, validate the assembled proposal and then recheck cancellation at the dispatch boundary.

The final status should name what is known: stopped before dispatch, completed despite cancellation, or pending reconciliation. That precision is more useful than a single optimistic cancellation label.

DiagramCancellation after dispatch still needs a receipt
  1. 1Harness→Carrier

    Dispatch booking

    A complete validated operation crosses the effect boundary.

DispatcherHarnessCarrierDispatch booking
1/4

Dispatch booking

A complete validated operation crosses the effect boundary.

Move the cancellation before dispatch in your fixture and check that the carrier sees no operation.

What if · interview follow-ups

Change one condition

A dispatcher cancels while the carrier request is in flight.

  1. Start here

    The UI has stopped receiving tokens. Has the booking stopped?

Do this before moving on

Script cancellation before dispatch, during a carrier timeout and after confirmation. Reconnect a client with duplicate progress events. Pass when no partial call executes, duplicate events do not duplicate actions, and uncertain carrier state remains reconciling until checked.

Review your result. Test all three event orders and a reconnect. Check both the final user-visible status and the carrier ledger. Partial argument delivery must produce zero dispatches.

Go deeper

Key takeaways

  • Complete and validate a call before dispatch, even when text streams.
  • Replaying progress must never execute the action again.
  • Cancellation does not prove an in-flight side effect was reversed.

Check yourself

Answer aloud or on paper before revealing. Producing the explanation unaided is exactly what a panel listens for.

  1. 1The browser disconnects after a booking request. Is the booking canceled?

  2. 2Why does the client example need an ordering assumption?

Sign in to track which lessons you have finished.