stygian-graph 0.9.2

High-performance graph-based web scraping engine with AI extraction, multi-modal support, and anti-bot capabilities
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Idempotency key tracking system
//!
//! Ensures scraping operations can be safely retried without re-executing
//! expensive work. Each operation is tagged with a ULID-based key; results
//! are stored in the cache with configurable TTL (default 24 h).
//!
//! # Example
//!
//! ```no_run
//! use stygian_graph::domain::idempotency::{IdempotencyKey, IdempotencyStore};
//! use stygian_graph::adapters::cache::MemoryCache;
//! use stygian_graph::ports::ServiceOutput;
//! use serde_json::json;
//! use std::sync::Arc;
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! let cache = Arc::new(MemoryCache::new());
//! let store = IdempotencyStore::new(cache);
//!
//! let key = IdempotencyKey::generate();
//! let result = ServiceOutput { data: "page html".into(), metadata: json!({}) };
//!
//! store.store(key, result.clone(), None).await.unwrap();
//! let cached = store.get(key).await.unwrap();
//! assert!(cached.is_some());
//! # });
//! ```

use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};
use ulid::Ulid;

use crate::domain::error::Result;
use crate::ports::{CachePort, ServiceOutput};

/// Default time-to-live for idempotency records (24 hours)
pub const DEFAULT_TTL: Duration = Duration::from_secs(24 * 60 * 60);

/// Maximum time-to-live for idempotency records (72 hours)
pub const MAX_TTL: Duration = Duration::from_secs(72 * 60 * 60);

/// An idempotency key for a scraping operation.
///
/// Based on ULID for lexicographic ordering and embedded timestamp.
///
/// # Example
///
/// ```
/// use stygian_graph::domain::idempotency::IdempotencyKey;
///
/// let key = IdempotencyKey::generate();
/// let encoded = key.to_string();
/// let decoded: IdempotencyKey = encoded.parse().unwrap();
/// assert_eq!(key, decoded);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IdempotencyKey(Ulid);

impl IdempotencyKey {
    /// Generate a new unique idempotency key.
    pub fn generate() -> Self {
        Self(Ulid::new())
    }

    /// Parse an idempotency key from its string representation.
    ///
    /// # Errors
    ///
    /// Returns an error if the string is not a valid ULID.
    pub fn parse(s: &str) -> std::result::Result<Self, ulid::DecodeError> {
        s.parse::<Ulid>().map(Self)
    }

    /// The cache key used to store this record.
    pub fn cache_key(&self) -> String {
        format!("idempotency:{}", &self.0)
    }
}

impl std::str::FromStr for IdempotencyKey {
    type Err = ulid::DecodeError;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        s.parse::<Ulid>().map(IdempotencyKey)
    }
}

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

impl Default for IdempotencyKey {
    fn default() -> Self {
        Self::generate()
    }
}

/// Status of an idempotent operation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OperationStatus {
    /// Operation is in progress
    Pending,
    /// Operation completed successfully
    Completed,
    /// Operation failed
    Failed,
}

/// A stored idempotency record.
///
/// Contains the result of an operation along with metadata for expiry checks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdempotencyRecord {
    /// The key uniquely identifying this operation
    pub key: String,
    /// Status of the operation
    pub status: OperationStatus,
    /// The cached output (only set when status is Completed)
    pub output: Option<CachedOutput>,
    /// Unix timestamp (seconds) when this record was created
    pub created_at: u64,
    /// Unix timestamp (seconds) when this record expires
    pub expires_at: u64,
}

/// Serializable version of `ServiceOutput` for cache storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedOutput {
    /// Raw scraped data
    pub data: String,
    /// Operation metadata
    pub metadata: serde_json::Value,
}

impl From<ServiceOutput> for CachedOutput {
    fn from(output: ServiceOutput) -> Self {
        Self {
            data: output.data,
            metadata: output.metadata,
        }
    }
}

impl From<CachedOutput> for ServiceOutput {
    fn from(cached: CachedOutput) -> Self {
        Self {
            data: cached.data,
            metadata: cached.metadata,
        }
    }
}

impl IdempotencyRecord {
    /// Create a new pending record (marks operation as in-flight).
    pub fn new_pending(key: IdempotencyKey, ttl: Duration) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        Self {
            key: key.to_string(),
            status: OperationStatus::Pending,
            output: None,
            created_at: now,
            expires_at: now + ttl.as_secs(),
        }
    }

    /// Check whether this record has expired.
    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now > self.expires_at
    }
}

/// Cache-backed store for idempotency records.
///
/// Uses `CachePort` as an abstraction so it works with any backend
/// (in-memory, Redis, etc.). Provides atomic check-and-mark via optimistic
/// locking through the cache layer.
pub struct IdempotencyStore<C: CachePort> {
    cache: Arc<C>,
    default_ttl: Duration,
}

impl<C: CachePort> IdempotencyStore<C> {
    /// Create a new store with the given cache backend and default TTL.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use stygian_graph::domain::idempotency::IdempotencyStore;
    /// use stygian_graph::adapters::cache::MemoryCache;
    /// use std::sync::Arc;
    ///
    /// let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
    /// ```
    pub const fn new(cache: Arc<C>) -> Self {
        Self {
            cache,
            default_ttl: DEFAULT_TTL,
        }
    }

    /// Create a store with a custom TTL.
    pub fn with_ttl(cache: Arc<C>, ttl: Duration) -> Self {
        let ttl = ttl.min(MAX_TTL);
        Self {
            cache,
            default_ttl: ttl,
        }
    }

    /// Check whether a result is already cached for this key.
    ///
    /// Returns `None` if not found or expired.
    pub async fn get(&self, key: IdempotencyKey) -> Result<Option<IdempotencyRecord>> {
        let cache_key = key.cache_key();
        let Some(json) = self.cache.get(&cache_key).await? else {
            return Ok(None);
        };

        let record: IdempotencyRecord = serde_json::from_str(&json).map_err(|e| {
            crate::domain::error::StygianError::Cache(crate::domain::error::CacheError::ReadFailed(
                e.to_string(),
            ))
        })?;

        if record.is_expired() {
            return Ok(None);
        }

        Ok(Some(record))
    }

    /// Mark a key as pending (in-flight) atomically.
    ///
    /// Returns `true` if the claim was acquired (key was not already present).
    /// Returns `false` if another worker already claimed this key.
    pub async fn claim(&self, key: IdempotencyKey) -> Result<bool> {
        let cache_key = key.cache_key();

        // Check first — if already exists, don't overwrite
        if self.cache.exists(&cache_key).await? {
            return Ok(false);
        }

        let record = IdempotencyRecord::new_pending(key, self.default_ttl);
        let json = serde_json::to_string(&record).unwrap_or_default();
        self.cache
            .set(&cache_key, json, Some(self.default_ttl))
            .await?;
        Ok(true)
    }

    /// Store a completed result for the given key.
    pub async fn store(
        &self,
        key: IdempotencyKey,
        output: ServiceOutput,
        ttl: Option<Duration>,
    ) -> Result<()> {
        let ttl = ttl.unwrap_or(self.default_ttl).min(MAX_TTL);
        let cache_key = key.cache_key();

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let record = IdempotencyRecord {
            key: key.to_string(),
            status: OperationStatus::Completed,
            output: Some(CachedOutput::from(output)),
            created_at: now,
            expires_at: now + ttl.as_secs(),
        };

        let json = serde_json::to_string(&record).unwrap_or_default();
        self.cache.set(&cache_key, json, Some(ttl)).await
    }

    /// Mark an operation as failed.
    pub async fn mark_failed(&self, key: IdempotencyKey) -> Result<()> {
        let cache_key = key.cache_key();

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let record = IdempotencyRecord {
            key: key.to_string(),
            status: OperationStatus::Failed,
            output: None,
            created_at: now,
            expires_at: now + self.default_ttl.as_secs(),
        };

        let json = serde_json::to_string(&record).unwrap_or_default();
        self.cache
            .set(&cache_key, json, Some(self.default_ttl))
            .await
    }

    /// Invalidate a key (force re-execution on next attempt).
    pub async fn invalidate(&self, key: IdempotencyKey) -> Result<()> {
        self.cache.invalidate(&key.cache_key()).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapters::cache::MemoryCache;
    use crate::ports::ServiceOutput;

    fn make_output() -> ServiceOutput {
        ServiceOutput {
            data: "scraped content".into(),
            metadata: serde_json::json!({"status": 200}),
        }
    }

    #[tokio::test]
    async fn test_store_and_retrieve() -> Result<()> {
        let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
        let key = IdempotencyKey::generate();

        store.store(key, make_output(), None).await?;

        let maybe_record = store.get(key).await?;
        assert!(maybe_record.is_some());
        if let Some(record) = maybe_record {
            assert_eq!(record.status, OperationStatus::Completed);
            assert!(record.output.is_some());
            if let Some(ref output) = record.output {
                assert_eq!(output.data, "scraped content");
            }
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_missing_key_returns_none() -> Result<()> {
        let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
        let key = IdempotencyKey::generate();

        let result = store.get(key).await?;
        assert!(result.is_none());
        Ok(())
    }

    #[tokio::test]
    async fn test_claim_prevents_duplicate() -> Result<()> {
        let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
        let key = IdempotencyKey::generate();

        let first = store.claim(key).await?;
        assert!(first, "First claim should succeed");

        let second = store.claim(key).await?;
        assert!(!second, "Second claim should fail (duplicate)");

        Ok(())
    }

    #[tokio::test]
    async fn test_invalidate_removes_record() -> Result<()> {
        let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
        let key = IdempotencyKey::generate();

        store.store(key, make_output(), None).await?;
        store.invalidate(key).await?;

        let result = store.get(key).await?;
        assert!(result.is_none());
        Ok(())
    }

    #[tokio::test]
    async fn test_mark_failed_stores_failed_status() -> Result<()> {
        let store = IdempotencyStore::new(Arc::new(MemoryCache::new()));
        let key = IdempotencyKey::generate();

        store.mark_failed(key).await?;

        let maybe_record = store.get(key).await?;
        assert!(maybe_record.is_some());
        if let Some(record) = maybe_record {
            assert_eq!(record.status, OperationStatus::Failed);
            assert!(record.output.is_none());
        }
        Ok(())
    }

    #[tokio::test]
    async fn test_expired_record_returns_none() -> Result<()> {
        let store =
            IdempotencyStore::with_ttl(Arc::new(MemoryCache::new()), Duration::from_nanos(1));
        let key = IdempotencyKey::generate();

        store
            .store(key, make_output(), Some(Duration::from_nanos(1)))
            .await?;

        // Sleep briefly so the record expires
        tokio::time::sleep(Duration::from_millis(5)).await;

        // Note: MemoryCache doesn't enforce TTL, so create a record with past expires_at
        // to test the is_expired() logic directly
        let record = IdempotencyRecord {
            key: key.to_string(),
            status: OperationStatus::Completed,
            output: None,
            created_at: 0,
            expires_at: 1, // Far in the past
        };
        assert!(record.is_expired());
        Ok(())
    }

    #[test]
    fn test_key_roundtrip() -> std::result::Result<(), Box<dyn std::error::Error>> {
        let key = IdempotencyKey::generate();
        let s = key.to_string();
        let parsed: IdempotencyKey = s.parse()?;
        assert_eq!(key, parsed);
        Ok(())
    }

    #[test]
    fn test_ttl_capped_at_max() {
        let huge_ttl = Duration::from_secs(99_999_999);
        let store = IdempotencyStore::with_ttl(Arc::new(MemoryCache::new()), huge_ttl);
        assert_eq!(store.default_ttl, MAX_TTL);
    }
}