Implementation:1ay1 Agentty Typestate IO Lifecycle
| 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
- Repository: 1ay1_Agentty
- File: include/agentty/io/fsm.hpp
- Lines: 1-170
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"