zebflow 0.7.3

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Trait definitions for Zebflow's shared state bus.
//!
//! The state bus is the seam between Zebflow runtime features and the concrete mechanism used to
//! coordinate project state. It intentionally covers the full current `n.kv.*` feature
//! set so the existing in-memory behavior can move behind one contract before Redis or another
//! shared backend is introduced.
//!
//! Design goals:
//!
//! - preserve the current low-latency local mem behavior
//! - make namespace and key validation explicit early
//! - keep TTL, atomic increment, and pub/sub semantics visible in the contract
//! - expose capabilities and limits so future UI/cluster code can reason about backends

use std::sync::Arc;

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Maximum owner segment length accepted by the default state-bus validators.
pub const MAX_STATE_OWNER_LEN: usize = 96;
/// Maximum project segment length accepted by the default state-bus validators.
pub const MAX_STATE_PROJECT_LEN: usize = 96;
/// Maximum key length accepted by the default state-bus validators.
pub const MAX_STATE_KEY_LEN: usize = 256;
/// Maximum channel length accepted by the default state-bus validators.
pub const MAX_STATE_CHANNEL_LEN: usize = 256;

/// Shared subscription type used by pub/sub backends.
pub type StateSubscription = tokio::sync::broadcast::Receiver<Value>;

/// Typed state-bus error.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateBusError {
    /// Stable error code.
    pub code: String,
    /// Human-readable message.
    pub message: String,
}

impl StateBusError {
    /// Build a new state-bus error.
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }

    /// Invalid namespace segment helper.
    pub fn invalid_namespace(field: &str, message: impl Into<String>) -> Self {
        Self::new(
            "STATE_BUS_INVALID_NAMESPACE",
            format!("{field}: {}", message.into()),
        )
    }

    /// Invalid key helper.
    pub fn invalid_key(message: impl Into<String>) -> Self {
        Self::new("STATE_BUS_INVALID_KEY", message)
    }

    /// Invalid channel helper.
    pub fn invalid_channel(message: impl Into<String>) -> Self {
        Self::new("STATE_BUS_INVALID_CHANNEL", message)
    }

    /// Not implemented helper for placeholder backends.
    pub fn not_implemented(backend_id: &str) -> Self {
        Self::new(
            "STATE_BUS_NOT_IMPLEMENTED",
            format!("state bus backend '{backend_id}' is not implemented yet"),
        )
    }
}

impl std::fmt::Display for StateBusError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.code, self.message)
    }
}

impl std::error::Error for StateBusError {}

/// Stable capability summary for one state-bus implementation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateBusCapabilities {
    /// Whether TTL/expiry is supported.
    pub ttl: bool,
    /// Whether atomic integer increment is supported.
    pub atomic_incr: bool,
    /// Whether pub/sub is supported.
    pub pubsub: bool,
    /// Whether the backend is durable across process restarts.
    pub durable: bool,
    /// Whether the backend supports shared coordination across multiple runtimes.
    pub shared_coordination: bool,
}

impl Default for StateBusCapabilities {
    fn default() -> Self {
        Self {
            ttl: true,
            atomic_incr: true,
            pubsub: true,
            durable: false,
            shared_coordination: false,
        }
    }
}

/// Implementation limits exposed by the state bus.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateBusLimits {
    /// Maximum owner segment length.
    pub max_owner_len: usize,
    /// Maximum project segment length.
    pub max_project_len: usize,
    /// Maximum key length.
    pub max_key_len: usize,
    /// Maximum channel length.
    pub max_channel_len: usize,
}

impl Default for StateBusLimits {
    fn default() -> Self {
        Self {
            max_owner_len: MAX_STATE_OWNER_LEN,
            max_project_len: MAX_STATE_PROJECT_LEN,
            max_key_len: MAX_STATE_KEY_LEN,
            max_channel_len: MAX_STATE_CHANNEL_LEN,
        }
    }
}

/// Lightweight implementation stats.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct StateBusStats {
    /// Stable implementation id.
    pub implementation: String,
    /// Number of live entries currently held.
    pub entry_count: usize,
    /// Number of known pub/sub channels.
    pub channel_count: usize,
    /// Number of live subscribers across all channels.
    pub subscriber_count: usize,
}

/// Shared project-scoped KV + pub/sub contract.
///
/// This is intentionally close to the current `MemHub` behavior so the first migration can
/// be mechanical. Later implementations may add stronger lease or delivery guarantees.
pub trait StateBus: Send + Sync {
    /// Stable implementation identifier.
    fn id(&self) -> &'static str;

    /// Backend capability summary.
    fn capabilities(&self) -> StateBusCapabilities {
        StateBusCapabilities::default()
    }

    /// Backend validation and size limits.
    fn limits(&self) -> StateBusLimits {
        StateBusLimits::default()
    }

    /// Return lightweight implementation stats.
    fn stats(&self) -> Result<StateBusStats, StateBusError>;

    /// Validate one project namespace.
    fn validate_namespace(&self, owner: &str, project: &str) -> Result<(), StateBusError> {
        validate_namespace(owner, project, &self.limits())
    }

    /// Validate one key.
    fn validate_key(&self, key: &str) -> Result<(), StateBusError> {
        validate_state_key(key, self.limits().max_key_len)
    }

    /// Validate one channel.
    fn validate_channel(&self, channel: &str) -> Result<(), StateBusError> {
        validate_state_channel(channel, self.limits().max_channel_len)
    }

    /// Read one namespaced key.
    fn get(&self, owner: &str, project: &str, key: &str) -> Result<Option<Value>, StateBusError>;

    /// Set one namespaced key with an optional TTL in seconds.
    fn set(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        value: Value,
        ttl_secs: Option<u64>,
    ) -> Result<(), StateBusError>;

    /// Check whether one namespaced key exists and is still live.
    fn exists(&self, owner: &str, project: &str, key: &str) -> Result<bool, StateBusError>;

    /// Update the TTL of an existing key.
    fn expire(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        ttl_secs: Option<u64>,
    ) -> Result<bool, StateBusError>;

    /// Atomically increment one integer key.
    fn incr(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        amount: i64,
    ) -> Result<i64, StateBusError>;

    /// Delete one namespaced key.
    fn del(&self, owner: &str, project: &str, key: &str) -> Result<bool, StateBusError>;

    /// Publish a message onto a namespaced channel.
    fn publish(
        &self,
        owner: &str,
        project: &str,
        channel: &str,
        message: Value,
    ) -> Result<usize, StateBusError>;

    /// Subscribe to a namespaced channel.
    fn subscribe(
        &self,
        owner: &str,
        project: &str,
        channel: &str,
    ) -> Result<StateSubscription, StateBusError>;

    // ── Durable KV operations (SQLite-backed, survive restart) ─────────────

    /// Read one namespaced key from durable storage.
    fn durable_get(
        &self,
        owner: &str,
        project: &str,
        key: &str,
    ) -> Result<Option<Value>, StateBusError> {
        let _ = (owner, project, key);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }

    /// Set one namespaced key in durable storage with an optional TTL in seconds.
    fn durable_set(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        value: Value,
        ttl_secs: Option<u64>,
    ) -> Result<(), StateBusError> {
        let _ = (owner, project, key, value, ttl_secs);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }

    /// Check whether one namespaced key exists in durable storage.
    fn durable_exists(&self, owner: &str, project: &str, key: &str) -> Result<bool, StateBusError> {
        let _ = (owner, project, key);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }

    /// Update the TTL of an existing key in durable storage.
    fn durable_expire(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        ttl_secs: Option<u64>,
    ) -> Result<bool, StateBusError> {
        let _ = (owner, project, key, ttl_secs);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }

    /// Atomically increment one integer key in durable storage.
    fn durable_incr(
        &self,
        owner: &str,
        project: &str,
        key: &str,
        amount: i64,
    ) -> Result<i64, StateBusError> {
        let _ = (owner, project, key, amount);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }

    /// Delete one namespaced key from durable storage.
    fn durable_del(&self, owner: &str, project: &str, key: &str) -> Result<bool, StateBusError> {
        let _ = (owner, project, key);
        Err(StateBusError::new(
            "STATE_BUS_NO_DURABLE",
            "durable storage not available",
        ))
    }
}

/// Shared trait object used by schedulers, workers, and runtime services.
pub type DynStateBus = Arc<dyn StateBus>;

fn validate_namespace(
    owner: &str,
    project: &str,
    limits: &StateBusLimits,
) -> Result<(), StateBusError> {
    validate_namespace_segment("owner", owner, limits.max_owner_len)?;
    validate_namespace_segment("project", project, limits.max_project_len)?;
    Ok(())
}

fn validate_namespace_segment(
    field: &str,
    value: &str,
    max_len: usize,
) -> Result<(), StateBusError> {
    if value.is_empty() {
        return Err(StateBusError::invalid_namespace(field, "must not be empty"));
    }
    if value.len() > max_len {
        return Err(StateBusError::invalid_namespace(
            field,
            format!("must be at most {max_len} bytes"),
        ));
    }
    if value
        .bytes()
        .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.'))
    {
        Ok(())
    } else {
        Err(StateBusError::invalid_namespace(
            field,
            "contains unsupported characters; allowed: ASCII letters, digits, '_', '-', '.'",
        ))
    }
}

fn validate_state_key(key: &str, max_len: usize) -> Result<(), StateBusError> {
    validate_state_name(key, max_len, StateBusError::invalid_key)
}

fn validate_state_channel(channel: &str, max_len: usize) -> Result<(), StateBusError> {
    validate_state_name(channel, max_len, StateBusError::invalid_channel)
}

fn validate_state_name<F>(value: &str, max_len: usize, err: F) -> Result<(), StateBusError>
where
    F: Fn(String) -> StateBusError,
{
    if value.is_empty() {
        return Err(err("must not be empty".to_string()));
    }
    if value.len() > max_len {
        return Err(err(format!("must be at most {max_len} bytes")));
    }
    if value.bytes().all(is_valid_state_name_byte) {
        Ok(())
    } else {
        Err(err(
            "contains unsupported characters; allowed: ASCII letters, digits, '-', '_', '.', ':', '/', '@', '+', '='".to_string(),
        ))
    }
}

fn is_valid_state_name_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric()
        || matches!(byte, b'-' | b'_' | b'.' | b':' | b'/' | b'@' | b'+' | b'=')
}

#[cfg(test)]
mod tests {
    use super::{
        MAX_STATE_CHANNEL_LEN, MAX_STATE_KEY_LEN, MAX_STATE_OWNER_LEN, MAX_STATE_PROJECT_LEN,
        StateBusLimits, validate_namespace, validate_state_channel, validate_state_key,
    };

    #[test]
    fn validates_namespace_segments() {
        let limits = StateBusLimits::default();
        validate_namespace("superadmin", "default", &limits).expect("valid namespace");
        assert!(validate_namespace("bad/owner", "default", &limits).is_err());
        assert!(validate_namespace("bad owner", "default", &limits).is_err());
        assert!(validate_namespace("", "default", &limits).is_err());
    }

    #[test]
    fn validates_keys_and_channels() {
        validate_state_key("session:user@example.com", MAX_STATE_KEY_LEN).expect("valid key");
        validate_state_channel("events/order.created", MAX_STATE_CHANNEL_LEN)
            .expect("valid channel");
        assert!(validate_state_key("bad key", MAX_STATE_KEY_LEN).is_err());
        assert!(validate_state_channel("bad\tchannel", MAX_STATE_CHANNEL_LEN).is_err());
    }

    #[test]
    fn enforces_length_limits() {
        let owner = "o".repeat(MAX_STATE_OWNER_LEN + 1);
        let project = "p".repeat(MAX_STATE_PROJECT_LEN + 1);
        let key = "k".repeat(MAX_STATE_KEY_LEN + 1);
        let channel = "c".repeat(MAX_STATE_CHANNEL_LEN + 1);
        let limits = StateBusLimits::default();
        assert!(validate_namespace(&owner, "default", &limits).is_err());
        assert!(validate_namespace("superadmin", &project, &limits).is_err());
        assert!(validate_state_key(&key, MAX_STATE_KEY_LEN).is_err());
        assert!(validate_state_channel(&channel, MAX_STATE_CHANNEL_LEN).is_err());
    }
}