Skip to main content
Demonstrates GetAgentStream: start a stream, simulate a mid-stream crash by cancelling the Events context (subscriber only), then reconnect from the last saved offset to receive remaining events — including events emitted while the subscriber was gone. Source: examples/agent_with_reconnect/

What it demonstrates

  • The 5-step caller-side reconnect protocol
  • Capturing runID from stream.ID() before consuming events
  • Tracking the Offset() on each event and saving it as the resume point
  • Calling GetAgentStream(ctx, runID) then Events(ctx, WithOffset(lastOffset)) from a fresh context
  • Discarding already-processed events (offset ≤ saved offset) after reconnect
  • Handling ErrRunAlreadyCompleted when the run finished while disconnected

Requirements

Requires a durable runtime — Temporal or Restate. Local runtime has no stream offsets. The same public API (GetAgentStream + WithOffset, GetAgentRun) works on both. Single process: Temporal embeds a worker; Restate embeds the SDK endpoint.
Cancelling the Events context only closes the subscriber-side event channel. Cancelling the Stream context cancels the agent run. Use separate contexts — do not share one cancelable ctx for Stream and Events when simulating a subscriber crash. The durable run continues server-side, and GetAgentStream resumes from the saved offset.

Key code

Expected output

Events emitted while the subscriber was “gone” (after the simulated crash) are delivered in Phase B. The simulated crash cancels only the Events context (not Stream), not a real kill -9. For a real process-kill story, see Durable Agent (Temporal) or Durable Agent (Restate).

Learn more

Durable Execution

Full protocol, API reference, and offset semantics

Durable Agent (Restate)

Real process kill + state file reconnect (single process)

Streaming

RunID, offsets, and the streaming API

Restate Runtime

Embedded endpoint and durable event delivery