Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
identity.hpp
Go to the documentation of this file.
1#pragma once
2
3// Identity System for Pulp
4// Provides UUID generation and typed identity primitives for:
5// - Session tracking (SessionId)
6// - AI/tool execution tracking (RunId)
7// - Domain object identity (ObjectId)
8// - Cross-subsystem event correlation (CorrelationId)
9//
10// Uses UUIDv4 (random) for all identity types.
11// Lightweight: each ID is a 128-bit value with type safety via distinct types.
12
13#include <cstdint>
14#include <string>
15#include <array>
16#include <cstring>
17#include <functional>
18
19namespace pulp::runtime {
20
21// ── Raw UUID ────────────────────────────────────────────────────────────
22
23// 128-bit UUID stored as two 64-bit integers
24struct Uuid {
25 uint64_t hi = 0;
26 uint64_t lo = 0;
27
28 bool is_nil() const { return hi == 0 && lo == 0; }
29
30 bool operator==(const Uuid& other) const {
31 return hi == other.hi && lo == other.lo;
32 }
33 bool operator!=(const Uuid& other) const { return !(*this == other); }
34 bool operator<(const Uuid& other) const {
35 return hi < other.hi || (hi == other.hi && lo < other.lo);
36 }
37
38 // Generate a UUIDv4 (random)
39 static Uuid generate();
40
41 // Parse from string "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
42 static Uuid from_string(std::string_view str);
43
44 // Format as standard UUID string
45 std::string to_string() const;
46
47 // Format as compact hex (no dashes, 32 chars)
48 std::string to_hex() const;
49};
50
51// ── Typed Identity Wrappers ─────────────────────────────────────────────
52// Each identity type is a distinct type wrapping a Uuid.
53// This prevents accidental conflation of SessionId with ObjectId, etc.
54
55// Identifies one user session, workspace open, or AI conversation context.
56// Created on app launch or project open. Does NOT survive save/load —
57// a new session starts when the project is reopened.
58struct SessionId {
60
61 static SessionId generate() { return {Uuid::generate()}; }
62 static SessionId nil() { return {}; }
63 bool is_nil() const { return value.is_nil(); }
64 std::string to_string() const { return value.to_string(); }
65
66 bool operator==(const SessionId& o) const { return value == o.value; }
67 bool operator!=(const SessionId& o) const { return value != o.value; }
68};
69
70// Identifies one discrete AI/model/tool execution within a session.
71// Created per model call, tool invocation, or undo group.
72// Transient — does not survive save/load.
73struct RunId {
75
76 static RunId generate() { return {Uuid::generate()}; }
77 static RunId nil() { return {}; }
78 bool is_nil() const { return value.is_nil(); }
79 std::string to_string() const { return value.to_string(); }
80
81 bool operator==(const RunId& o) const { return value == o.value; }
82 bool operator!=(const RunId& o) const { return value != o.value; }
83};
84
85// Stable identifier for a persisted domain object: document, preset, plugin
86// instance, audio clip, generated output, etc.
87// Created when the object is first created. Survives save/load.
88// On duplication, the clone gets a NEW ObjectId; source_id preserves lineage.
89struct ObjectId {
91
92 static ObjectId generate() { return {Uuid::generate()}; }
93 static ObjectId nil() { return {}; }
94 static ObjectId from_string(std::string_view s) { return {Uuid::from_string(s)}; }
95 bool is_nil() const { return value.is_nil(); }
96 std::string to_string() const { return value.to_string(); }
97
98 bool operator==(const ObjectId& o) const { return value == o.value; }
99 bool operator!=(const ObjectId& o) const { return value != o.value; }
100 bool operator<(const ObjectId& o) const { return value < o.value; }
101};
102
103// Used for log/trace/callback correlation across subsystems.
104// Transient — attached to log entries, async operations, streaming chunks.
107
108 static CorrelationId generate() { return {Uuid::generate()}; }
109 static CorrelationId nil() { return {}; }
110 bool is_nil() const { return value.is_nil(); }
111 std::string to_string() const { return value.to_string(); }
112
113 bool operator==(const CorrelationId& o) const { return value == o.value; }
114 bool operator!=(const CorrelationId& o) const { return value != o.value; }
115};
116
117// ── Event Envelope ──────────────────────────────────────────────────────
118// Every mutation or event carries identity metadata for attribution,
119// correlation, and undo/redo support.
120
122 double timestamp = 0.0; // Unix epoch seconds
124 ObjectId object_id; // The object being mutated
125 RunId run_id; // Nil if not an AI operation
126 CorrelationId correlation_id; // For log/trace grouping
127 std::string actor; // "user", "ai", "host", subsystem name
128 std::string action; // "create", "modify", "delete", etc.
129};
130
131} // namespace pulp::runtime
132
133// ── Hash support for use in unordered containers ────────────────────────
134
135namespace std {
136template<> struct hash<pulp::runtime::Uuid> {
137 size_t operator()(const pulp::runtime::Uuid& id) const noexcept {
138 return hash<uint64_t>{}(id.hi) ^ (hash<uint64_t>{}(id.lo) << 1);
139 }
140};
141template<> struct hash<pulp::runtime::ObjectId> {
142 size_t operator()(const pulp::runtime::ObjectId& id) const noexcept {
143 return hash<pulp::runtime::Uuid>{}(id.value);
144 }
145};
146template<> struct hash<pulp::runtime::SessionId> {
147 size_t operator()(const pulp::runtime::SessionId& id) const noexcept {
148 return hash<pulp::runtime::Uuid>{}(id.value);
149 }
150};
151template<> struct hash<pulp::runtime::RunId> {
152 size_t operator()(const pulp::runtime::RunId& id) const noexcept {
153 return hash<pulp::runtime::Uuid>{}(id.value);
154 }
155};
156template<> struct hash<pulp::runtime::CorrelationId> {
157 size_t operator()(const pulp::runtime::CorrelationId& id) const noexcept {
158 return hash<pulp::runtime::Uuid>{}(id.value);
159 }
160};
161} // namespace std
Definition generated_event_source.hpp:33
@ other
Definition udev_monitor.hpp:26
Definition analyzer_provider.hpp:12
STL namespace.
Definition identity.hpp:105
bool is_nil() const
Definition identity.hpp:110
bool operator==(const CorrelationId &o) const
Definition identity.hpp:113
bool operator!=(const CorrelationId &o) const
Definition identity.hpp:114
std::string to_string() const
Definition identity.hpp:111
Uuid value
Definition identity.hpp:106
static CorrelationId generate()
Definition identity.hpp:108
static CorrelationId nil()
Definition identity.hpp:109
Definition identity.hpp:121
double timestamp
Definition identity.hpp:122
std::string actor
Definition identity.hpp:127
ObjectId object_id
Definition identity.hpp:124
RunId run_id
Definition identity.hpp:125
CorrelationId correlation_id
Definition identity.hpp:126
std::string action
Definition identity.hpp:128
SessionId session_id
Definition identity.hpp:123
Definition identity.hpp:89
std::string to_string() const
Definition identity.hpp:96
bool operator<(const ObjectId &o) const
Definition identity.hpp:100
static ObjectId generate()
Definition identity.hpp:92
bool operator==(const ObjectId &o) const
Definition identity.hpp:98
static ObjectId from_string(std::string_view s)
Definition identity.hpp:94
Uuid value
Definition identity.hpp:90
bool is_nil() const
Definition identity.hpp:95
static ObjectId nil()
Definition identity.hpp:93
bool operator!=(const ObjectId &o) const
Definition identity.hpp:99
Definition identity.hpp:73
static RunId nil()
Definition identity.hpp:77
bool operator!=(const RunId &o) const
Definition identity.hpp:82
bool is_nil() const
Definition identity.hpp:78
static RunId generate()
Definition identity.hpp:76
bool operator==(const RunId &o) const
Definition identity.hpp:81
Uuid value
Definition identity.hpp:74
std::string to_string() const
Definition identity.hpp:79
Definition identity.hpp:58
std::string to_string() const
Definition identity.hpp:64
bool operator==(const SessionId &o) const
Definition identity.hpp:66
static SessionId generate()
Definition identity.hpp:61
Uuid value
Definition identity.hpp:59
static SessionId nil()
Definition identity.hpp:62
bool is_nil() const
Definition identity.hpp:63
bool operator!=(const SessionId &o) const
Definition identity.hpp:67
Definition identity.hpp:24
uint64_t lo
Definition identity.hpp:26
std::string to_string() const
bool operator==(const Uuid &other) const
Definition identity.hpp:30
uint64_t hi
Definition identity.hpp:25
bool operator<(const Uuid &other) const
Definition identity.hpp:34
bool operator!=(const Uuid &other) const
Definition identity.hpp:33
static Uuid generate()
bool is_nil() const
Definition identity.hpp:28
static Uuid from_string(std::string_view str)
std::string to_hex() const
size_t operator()(const pulp::runtime::CorrelationId &id) const noexcept
Definition identity.hpp:157
size_t operator()(const pulp::runtime::ObjectId &id) const noexcept
Definition identity.hpp:142
size_t operator()(const pulp::runtime::RunId &id) const noexcept
Definition identity.hpp:152
size_t operator()(const pulp::runtime::SessionId &id) const noexcept
Definition identity.hpp:147
size_t operator()(const pulp::runtime::Uuid &id) const noexcept
Definition identity.hpp:137