agent_with_reconnect

command
v0.3.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

agent_with_reconnect

Focused demo of GetAgentStream: start a durable stream, simulate a mid-run crash after the first text chunk, then reconnect from a saved runID + event offset. Events print their offsets so you can see what to persist.

Works with Temporal and Restate (single process). Local runtime has no stream offsets. For real process-kill scenarios, see durable_agent/temporal or durable_agent/restate.

Prerequisites

  1. Configuration — set up examples/.env first: Configuration in examples/README.md.
  2. Working directory — run commands from examples/.
  3. Durable runtime — Temporal or Restate:
# Temporal
task infra:temporal:up && task infra:temporal:wait
AGENT_RUNTIME=temporal

# Restate
task infra:restate:up && task infra:restate:wait
AGENT_RUNTIME=restate

Run

AGENT_RUNTIME=temporal go run ./agent_with_reconnect "What time is it?"
# or:
AGENT_RUNTIME=restate go run ./agent_with_reconnect "What time is it?"

What happens

  1. Phase AStream starts; runID is taken from agentStream.ID() before events are consumed. The example tracks each event’s Offset(), then cancels the Events context after the first TEXT_MESSAGE_CONTENT chunk (simulated crash) and leaves Phase A immediately (does not wait for the channel to drain).
  2. Phase BGetAgentStream(ctx, runID) + Events(ctx, WithOffset(lastOffset)) resumes from that offset while the run is still live; remaining events print until RUN_FINISHED. If the run finishes first (ErrRunAlreadyCompleted), the example prints that and exits 0.
agentStream, err := a.Stream(ctx, prompt, nil)
runID := agentStream.ID()
eventCh, err := agentStream.Events(ctx)
// ... track Offset() on each event; cancel Events after first text chunk ...

agentStream, err = a.GetAgentStream(ctx, runID)
eventCh, err = agentStream.Events(ctx, agent.WithOffset(lastOffset))

Live run only. Once the durable run completes, the stream log may no longer be available for replay. This example cancels only the subscriber context so the run stays active for Phase B.

Caller-side protocol

  1. On Stream start, save runID (agentStream.ID()) with your correlation key.
  2. Track Offset() on each received event.
  3. On restart: GetAgentStream(ctx, savedRunID), then Events(ctx, WithOffset(savedOffset)).
  4. Events at offset ≤ saved offset may be redelivered; discard duplicates if needed.
  5. Clear saved state on RUN_FINISHED / RUN_ERROR.

Documentation

Overview

agent_with_reconnect demonstrates Agent.GetAgentStream: subscribe to a prior run's durable event log from a saved offset, simulating a mid-run crash and recovery in a single process. Works with Temporal and Restate (not local — no stream offsets).

Temporal embeds a worker; Restate embeds the SDK endpoint. Cancelling the Events context only closes the subscriber-side event channel — the Stream context (and durable run) keep running. Do not share one cancelable ctx for Stream and Events when simulating a subscriber crash.

AGENT_RUNTIME=temporal go run ./agent_with_reconnect [prompt]
AGENT_RUNTIME=restate  go run ./agent_with_reconnect [prompt]

Caller-side reconnect protocol:

  1. On Stream start, save runID alongside your correlation key.
  2. Track Offset() on each received event.
  3. On restart: GetAgentStream(ctx, savedRunID), then Events(ctx, WithOffset(savedOffset)).
  4. Events at offset ≤ savedOffset may be redelivered; discard duplicates if needed.
  5. Clear the saved runID on RUN_FINISHED or RUN_ERROR.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL