Implementation:1ay1 Agentty Strong Newtype IDs
| Knowledge Sources | |
|---|---|
| Domains | Type_System, Core |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Zero-overhead strong newtype template for string-shaped IDs, keeping ThreadId, ToolCallId, ModelId and others apart at the type level.
Description
The agentty::Id<Tag> template is a zero-overhead strong newtype for string-shaped IDs. Following Rust's rule-of-thumb: never pass a raw `String` across an API boundary when the caller could plausibly confuse it with another `String`. A `ThreadId` and a `ToolCallId` are both "some hex" at runtime, but the compiler keeps them apart.
The template holds a single `std::string value`, is explicitly constructible, supports `<=>` and `==`, and has JSON serialization via `nlohmann::json`. Tags are empty structs (`ThreadIdTag`, `ToolCallIdTag`, `ModelIdTag`, `CheckpointIdTag`, `ToolNameTag`, `MessageIdTag`) — the tag is the type identity.
`MessageId` is generated at Message construction and persisted to disk so cache keys stay valid across reloads. Compact/remove/reorder of messages doesn't collide with stale cache entries the way `(thread_id, msg_idx)` keying would.
Usage
Import this when you need to use any of the strong ID types, or define a new tagged ID type. Never pass a raw `std::string` where an `Id<Tag>` is expected.
Code Reference
Source Location
- Repository: 1ay1_Agentty
- File: include/agentty/domain/id.hpp
- Lines: 1-62
Signature
namespace agentty {
template <typename Tag>
struct Id {
std::string value;
Id() = default;
explicit Id(std::string s) noexcept;
[[nodiscard]] bool empty() const noexcept;
[[nodiscard]] const char* c_str() const noexcept;
bool operator==(const Id&) const = default;
auto operator<=>(const Id&) const = default;
[[nodiscard]] bool operator==(std::string_view sv) const noexcept;
// nlohmann::json serialization
friend void to_json(nlohmann::json& j, const Id& id);
friend void from_json(const nlohmann::json& j, Id& id);
};
using ThreadId = Id<ThreadIdTag>;
using ToolCallId = Id<ToolCallIdTag>;
using ModelId = Id<ModelIdTag>;
using CheckpointId = Id<CheckpointIdTag>;
using ToolName = Id<ToolNameTag>;
using MessageId = Id<MessageIdTag>;
[[nodiscard]] MessageId new_message_id();
}
Import
#include "agentty/domain/id.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Tag | empty struct | Yes | Type identity (ThreadIdTag, ToolCallIdTag, ...) |
| value | std::string | Yes | The underlying string id |
Outputs
| Name | Type | Description |
|---|---|---|
| Id<Tag> | strong newtype | Zero-overhead wrapper — compiler-enforced type separation |
Usage Examples
Use Strong IDs in Function Signatures
#include "agentty/domain/id.hpp"
// The compiler rejects passing a ToolCallId where ThreadId is expected:
void delete_thread(agentty::ThreadId id);
void cancel_tool_call(agentty::ToolCallId id);
agentty::ThreadId tid{"abc123"};
agentty::ToolCallId tcid{"def456"};
delete_thread(tid); // OK
// delete_thread(tcid); // COMPILE ERROR — wrong Id type
JSON Serialization
#include <nlohmann/json.hpp>
agentty::ThreadId id{"abc123"};
nlohmann::json j = id; // "abc123"
agentty::ThreadId restored = j.get<agentty::ThreadId>();
// restored.value == "abc123"