Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Typestate IO Lifecycle

From Leeroopedia
Knowledge Sources
Domains IO, Type_System
Last Updated 2026-10-28 12:00 GMT

Overview

Compile-time typestate machinery for IO/network lifecycles, making illegal state transitions unrepresentable at the type level.

Description

The agentty::io::fsm module is the generic, dependency-free core of agentty's typestate pattern. It carries no transport types (no OpenSSL, no nghttp2, no sockets) so it can be included anywhere. Concrete state structs that drive real transports are defined in the translation unit that owns those types.

`State<Tag>` is a move-only, non-copyable empty marker base — a capability token. You consume it to transition; once consumed it's gone, so you cannot drive a transition twice or out of order. `transition<From,To>` declares a legal edge; `legal_edge<From,To>` is a pure type-level trait resolved at compile time that generates no code. A transition method returns `Result` (`std::expected<S, E>`) gated by a `static_assert(is_legal_edge_v<From,To>)`, so an illegal edge fails to COMPILE — the diagnostic names the exact From/To pair.

Zero-cost: every state struct is empty-or-payload-only and move-only; no vtable, no type erasure, no heap. At runtime the typestate pipeline is exactly the same sequence of moves and `std::expected` checks you'd write by hand.

Usage

Import this when you need to: define a typestate machine for an IO/network lifecycle, declare legal state transitions, or write transition functions that are compile-time-checked for legality.

Code Reference

Source Location

Signature

namespace agentty::io::fsm {

// Move-only marker base — a capability token.
template <class Tag>
struct State {
    State() noexcept = default;
    State(const State&)            = delete;
    State& operator=(const State&) = delete;
    State(State&&) noexcept            = default;
    State& operator=(State&&) noexcept = default;
};

// Declare legal edges: add `using fsm_to = fsm::to<NextStates...>` to a state.
template <class... To> struct to {};

// Type-level adjacency: true iff From names To among its successors.
template <class From, class To> struct legal_edge;
template <class From, class To>
inline constexpr bool is_legal_edge_v = legal_edge<From, To>::value;

// Compile-time guard — placed at the top of every transition function.
template <class From, class To>
consteval void assert_legal_edge() noexcept;

// A transition outcome: advance to S, or fail with E.
template <class S, class E>
using Result = std::expected<S, E>;
}

// Usage pattern:
// struct Dialing      : fsm::State<DialingTag>      { using fsm_to = fsm::to<TcpConnected>; };
// struct TcpConnected : fsm::State<TcpConnectedTag> { socket_t fd; };
// fsm::Result<TcpConnected, HttpError> connect(Dialing&& s) {
//     fsm::assert_legal_edge<Dialing, TcpConnected>();
//     return TcpConnected{ .fd = fd };
// }

Import

#include "agentty/io/fsm.hpp"

I/O Contract

Inputs

Name Type Required Description
Tag incomplete type Yes Unique type distinguishing one state from another
fsm_to type list No Declares legal successors (in-class: `using fsm_to = fsm::to<...>`)

Outputs

Name Type Description
State<Tag> move-only struct Capability token — consumed by transitions
Result<S, E> std::expected Transition outcome: next state or typed error

Usage Examples

Define an HTTP Connection Lifecycle

#include "agentty/io/fsm.hpp"

struct Dialing      : agentty::io::fsm::State<struct DialingTag> {
    std::string host; int port;
    using fsm_to = agentty::io::fsm::to<TcpConnected>;
};
struct TcpConnected : agentty::io::fsm::State<struct TcpConnectedTag> {
    int fd;
    using fsm_to = agentty::io::fsm::to<TlsUp, H2Ready>;
};
struct TlsUp : agentty::io::fsm::State<struct TlsUpTag> {
    using fsm_to = agentty::io::fsm::to<H2Ready>;
};
struct H2Ready : agentty::io::fsm::State<struct H2ReadyTag> {};  // terminal

// Transition: Dialing → TcpConnected
agentty::io::fsm::Result<TcpConnected, HttpError> connect(Dialing&& s) {
    agentty::io::fsm::assert_legal_edge<Dialing, TcpConnected>();
    int fd = ::socket(...);
    if (fd < 0) return std::unexpected{HttpError{"dial failed"}};
    return TcpConnected{ .fd = fd };
}

// ILLEGAL transition — compile error:
// agentty::io::fsm::assert_legal_edge<Dialing, H2Ready>();
// → "FSM: illegal state transition — To is not among From's declared successors"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment