AgentCall.Checkpoint (agent_call v0.1.0)

Copy Markdown View Source

The state after a completed tool turn, and the versioned envelope around it.

A checkpoint is a safe resume point: it carries the request, the model-visible transcript after the turn's tool results, the completed model turns in steps, the completed tool calls in tool_calls, and the accumulated and last-turn usage. The next model request is step steps + 1. Unlike AgentCall.Pending there is no approval/input payload, no partial turn and no response: a pause is a Pending and a final answer is an AgentCall.Result.

dump/1 writes a %AgentCall.Pending{} or a %AgentCall.Checkpoint{} as a versioned binary envelope, %{version: 1, type: :pending | :checkpoint, payload: struct}, encoded with :erlang.term_to_binary/2. load/1 decodes it with [:safe] and checks the envelope version and type before returning the struct. Another envelope version is {:error, :unsupported_version}. {:error, :invalid} covers a term that is not an envelope, a type that does not match its payload, a record that is not the exact shape the envelope names (including a Checkpoint whose own version is not the envelope's), or a payload carrying a fun, pid, port or reference. load/1 validates the record recursively, including every nested struct; resume_checkpoint/3 and resume/3 repeat the check for hand-built records before a run starts.

Compression is decided per record. A transcript that carries a media source (Content.Image, Content.File, Content.Video) is already-transparent base64, and deflating it costs time proportional to the payload while saving under a quarter of the bytes; such a record is written uncompressed, quickly. A text-only record deflates well and is written compressed, which is what a long transcript needs. dump/2 takes compress: to force either choice or name a zlib level (0-9); compress: 0 writes uncompressed, and any other level compresses at that level. Both encodings load through load/1 because the envelope does not record the choice.

[:safe] refuses a binary that would create new atoms or external fun references. It still decodes a fun or a process identifier whose atoms already exist, so load/1 also checks the payload is portable data: a fun, pid, port or reference is not a record that survives the process.

There is no JSON or map codec: the record embeds LlmCall and ToolCall structs. While the library is 0.x, a record is guaranteed to load only in the release that wrote it; cross-version resumption is not promised.

Summary

Functions

Encodes a Pending or a Checkpoint as a versioned envelope binary.

Encodes a record like dump/1, with an explicit compression decision.

Decodes an envelope binary.

The checkpoint record and envelope version this release writes.

Types

t()

@type t() :: %AgentCall.Checkpoint{
  last_usage: LlmCall.Usage.t() | nil,
  request: LlmCall.Request.t(),
  run_id: String.t(),
  steps: non_neg_integer(),
  tool_calls: non_neg_integer(),
  transcript: [LlmCall.Message.t()],
  usage: LlmCall.Usage.t() | nil,
  version: pos_integer()
}

Functions

dump(record)

@spec dump(AgentCall.Pending.t() | t()) :: binary()

Encodes a Pending or a Checkpoint as a versioned envelope binary.

A Pending is a pause checkpoint and a Checkpoint is a completed tool turn. The checkpoint's version is set to version/0 while dumping, so a record built by hand with a nil or different version still writes a binary this release loads.

The record is validated recursively before encoding. A hand-built record with a fake nested struct, a Usage field that is not nil or a non-negative integer, a ToolResult whose content is not text or whose is_error is not a boolean, a Message with invalid role/content, an invalid Content field (Text.text, image/file/video source, Thinking.text/signature, Raw data), a request with invalid system/tool_choice, a ToolSpec with invalid parameters, a ToolCall with invalid nullable fields, or a non-portable value raises ArgumentError; it would otherwise write a binary load/1 rejects. ToolResult/ToolCall identifiers may be nil; the protocol adapter decides which identifier it needs.

See dump/2 for the compressed and uncompressed choices.

dump(record, opts)

@spec dump(AgentCall.Pending.t() | t(), keyword()) :: binary()

Encodes a record like dump/1, with an explicit compression decision.

compress: is :auto (the default, see the module doc), false for uncompressed, true for the default zlib level, or a zlib level 1..9. Any other value raises ArgumentError, as does an unknown option. A record that fails validation raises before the compression level matters.

The level is not stored in the envelope: load/1 reads both encodings.

load(binary)

@spec load(binary()) ::
  {:ok, AgentCall.Pending.t() | t()} | {:error, :invalid | :unsupported_version}

Decodes an envelope binary.

Returns the record it carries, {:error, :unsupported_version} for an envelope version this release does not know, or {:error, :invalid} for a binary or payload that is not a supported record.

The record is validated recursively: every nested struct must have exactly the fields of its module, Usage fields must be nil or non-negative integers, ToolResult content must be text and is_error a boolean, a Message must have a :user/:assistant role and content parts, and content fields (Text.text, media source, Thinking.text/signature, Raw data) and a ToolCall's nullable fields must have their declared types. Request.model/provider_options/system/tool_choice/output/modalities/reasoning/ params/stream and all ToolSpec fields are checked as well. A tool-target Input also checks source, spec and inserted_at. A fun, pid, port or reference is rejected. ToolResult/ToolCall identifiers may be nil; the protocol adapter decides which identifier it needs. resume_checkpoint/3 and resume/3 repeat the check for hand-built records, so a malformed record is a :configuration error rather than a crash.

version()

@spec version() :: pos_integer()

The checkpoint record and envelope version this release writes.