prompty 2.0.0-alpha.11

Prompty is an asset class and format for LLM prompts
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Tracer registry, span emission, and `trace` wrapper.
//!
//! The registry holds named tracer factories. When a span starts, each factory
//! is called with the span signature; factories that return `Some` produce a
//! backend that receives key/value events for that span.

use std::collections::HashMap;
use std::sync::{OnceLock, RwLock};
use std::time::Instant;

use regex::Regex;
use serde_json::Value;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// A tracer backend receives events for a single span.
///
/// Each event is a key/value pair. The special key `__end__` signals the span
/// has finished.
pub trait TracerBackend: Send + Sync {
    fn emit(&self, key: &str, value: &Value);
}

/// A tracer factory creates a backend for a given span signature.
///
/// Returning `None` means this factory is not interested in the span.
pub trait TracerFactory: Send + Sync {
    fn create(&self, signature: &str) -> Option<Box<dyn TracerBackend>>;
}

/// A live span that fans out events to all active backends.
pub struct SpanEmitter {
    backends: Vec<Box<dyn TracerBackend>>,
    start: Instant,
}

impl SpanEmitter {
    /// Emit a key/value event to all backends.
    pub fn emit(&self, key: &str, value: &Value) {
        for b in &self.backends {
            // Swallow backend errors (like the TS implementation).
            let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                b.emit(key, value);
            }));
        }
    }

    /// Close the span: emit `duration_ms` and `__end__`.
    pub fn end(self) {
        let duration_ms = self.start.elapsed().as_millis() as u64;
        self.emit("duration_ms", &Value::from(duration_ms));
        self.emit("__end__", &Value::Null);
    }
}

// ---------------------------------------------------------------------------
// Global registry
// ---------------------------------------------------------------------------

type FactoryMap = HashMap<String, Box<dyn TracerFactory>>;

fn registry() -> &'static RwLock<FactoryMap> {
    static REG: OnceLock<RwLock<FactoryMap>> = OnceLock::new();
    REG.get_or_init(|| RwLock::new(HashMap::new()))
}

/// The `Tracer` namespace — manages the global set of tracer factories.
pub struct Tracer;

impl Tracer {
    /// Register a named tracer factory.
    pub fn add(name: &str, factory: impl TracerFactory + 'static) {
        let mut map = registry().write().unwrap();
        map.insert(name.to_string(), Box::new(factory));
    }

    /// Remove a tracer factory by name.
    pub fn remove(name: &str) {
        let mut map = registry().write().unwrap();
        map.remove(name);
    }

    /// Remove all tracer factories.
    pub fn clear() {
        let mut map = registry().write().unwrap();
        map.clear();
    }

    /// Start a new span. Calls each registered factory with the signature and
    /// collects the backends that opted in.
    pub fn start(signature: &str) -> SpanEmitter {
        let map = registry().read().unwrap();
        let mut backends = Vec::new();
        for factory in map.values() {
            if let Some(backend) = factory.create(signature) {
                backends.push(backend);
            }
        }
        SpanEmitter {
            backends,
            start: Instant::now(),
        }
    }
}

// ---------------------------------------------------------------------------
// trace() wrapper
// ---------------------------------------------------------------------------

/// Execute `f` inside a traced span. Emits `inputs`, `result` or `error`,
/// `duration_ms`, and `__end__`.
pub fn trace<F, T>(name: &str, inputs: &Value, f: F) -> Result<T, Box<dyn std::error::Error>>
where
    F: FnOnce() -> Result<T, Box<dyn std::error::Error>>,
    T: serde::Serialize,
{
    let span = Tracer::start(name);
    span.emit("inputs", &sanitize_value("inputs", inputs));

    match f() {
        Ok(result) => {
            if let Ok(val) = serde_json::to_value(&result) {
                span.emit("result", &val);
            }
            span.end();
            Ok(result)
        }
        Err(err) => {
            span.emit("error", &Value::String(err.to_string()));
            span.end();
            Err(err)
        }
    }
}

/// Execute `body(span)` inside a traced span. The body receives the span
/// emitter for manual event emission.
pub fn trace_span<F, T>(name: &str, body: F) -> Result<T, Box<dyn std::error::Error>>
where
    F: FnOnce(&SpanEmitter) -> Result<T, Box<dyn std::error::Error>>,
{
    let span = Tracer::start(name);
    match body(&span) {
        Ok(result) => {
            span.end();
            Ok(result)
        }
        Err(err) => {
            span.emit("error", &Value::String(err.to_string()));
            span.end();
            Err(err)
        }
    }
}

/// Async version of `trace_span`. Execute an async body inside a traced span.
/// The body receives a shared reference to the span emitter for manual event
/// emission. The span is automatically ended (with `duration_ms` and `__end__`)
/// when the body completes.
pub async fn trace_span_async<F, Fut, T>(
    name: &str,
    body: F,
) -> Result<T, Box<dyn std::error::Error>>
where
    F: FnOnce(std::sync::Arc<SpanEmitter>) -> Fut,
    Fut: std::future::Future<Output = Result<T, Box<dyn std::error::Error>>>,
{
    let span = std::sync::Arc::new(Tracer::start(name));
    match body(std::sync::Arc::clone(&span)).await {
        Ok(result) => {
            // Unwrap the Arc — we're the only holder after body completes.
            if let Ok(owned) = std::sync::Arc::try_unwrap(span) {
                owned.end();
            }
            Ok(result)
        }
        Err(err) => {
            span.emit("error", &Value::String(err.to_string()));
            if let Ok(owned) = std::sync::Arc::try_unwrap(span) {
                owned.end();
            }
            Err(err)
        }
    }
}

/// Async version of `trace`. Wraps an async function with automatic input/output tracing.
pub async fn trace_async<F, Fut, T>(
    name: &str,
    inputs: &Value,
    f: F,
) -> Result<T, Box<dyn std::error::Error>>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = Result<T, Box<dyn std::error::Error>>>,
    T: serde::Serialize,
{
    let span = Tracer::start(name);
    span.emit("inputs", &sanitize_value("inputs", inputs));

    match f().await {
        Ok(result) => {
            if let Ok(val) = serde_json::to_value(&result) {
                span.emit("result", &val);
            }
            span.end();
            Ok(result)
        }
        Err(err) => {
            span.emit("error", &Value::String(err.to_string()));
            span.end();
            Err(err)
        }
    }
}

// ---------------------------------------------------------------------------
// Sanitization
// ---------------------------------------------------------------------------

/// Check if a key matches sensitive patterns that should be redacted.
fn is_sensitive_key(key: &str) -> bool {
    static PAT: OnceLock<Regex> = OnceLock::new();
    let pat = PAT.get_or_init(|| {
        Regex::new(r"(?i)secret|password|credential|passphrase|bearer|cookie|authorization|api[_.]?key|token|auth")
            .unwrap()
    });

    if !pat.is_match(key) {
        return false;
    }

    // Exclude false positives that the TS version handles with lookahead:
    // "tokens" should NOT match (token(?!s) in TS)
    // "authors" should NOT match (auth(?!ors?\b) in TS)
    let lower = key.to_lowercase();
    if lower.contains("tokens") && !lower.contains("token_") && !lower.contains("token.") {
        return false;
    }
    if lower.contains("authors") || lower.contains("author") && !lower.contains("auth_") {
        // "author" / "authors" → not sensitive
        // But "auth_header" → sensitive (contains "auth" without "author")
        let auth_pos = lower.find("auth").unwrap();
        let after = &lower[auth_pos..];
        if after.starts_with("author") {
            return false;
        }
    }
    true
}

const REDACTED: &str = "***REDACTED***";

/// Recursively redact values whose keys match the sensitive pattern.
pub fn sanitize_value(key: &str, value: &Value) -> Value {
    if is_sensitive_key(key) {
        return Value::String(REDACTED.to_string());
    }
    match value {
        Value::Object(map) => {
            let sanitized: serde_json::Map<String, Value> = map
                .iter()
                .map(|(k, v)| (k.clone(), sanitize_value(k, v)))
                .collect();
            Value::Object(sanitized)
        }
        Value::Array(arr) => {
            let sanitized: Vec<Value> = arr.iter().map(|v| sanitize_value(key, v)).collect();
            Value::Array(sanitized)
        }
        _ => value.clone(),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use serial_test::serial;
    use std::sync::{Arc, Mutex};

    // A simple in-memory backend for testing.
    struct MemoryBackend {
        events: Arc<Mutex<Vec<(String, Value)>>>,
    }

    impl TracerBackend for MemoryBackend {
        fn emit(&self, key: &str, value: &Value) {
            let mut events = self.events.lock().unwrap();
            events.push((key.to_string(), value.clone()));
        }
    }

    struct MemoryFactory {
        events: Arc<Mutex<Vec<(String, Value)>>>,
    }

    impl TracerFactory for MemoryFactory {
        fn create(&self, _signature: &str) -> Option<Box<dyn TracerBackend>> {
            Some(Box::new(MemoryBackend {
                events: Arc::clone(&self.events),
            }))
        }
    }

    fn setup_memory_tracer() -> Arc<Mutex<Vec<(String, Value)>>> {
        Tracer::clear();
        let events = Arc::new(Mutex::new(Vec::new()));
        Tracer::add(
            "test",
            MemoryFactory {
                events: events.clone(),
            },
        );
        events
    }

    #[test]
    #[serial]
    fn test_trace_success() {
        let events = setup_memory_tracer();
        let result: Result<String, _> =
            trace("test_span", &json!({"x": 1}), || Ok("hello".to_string()));
        assert_eq!(result.unwrap(), "hello");

        let ev = events.lock().unwrap();
        assert_eq!(ev[0].0, "inputs");
        assert_eq!(ev[1].0, "result");
        assert_eq!(ev[1].1, json!("hello"));
        assert_eq!(ev[2].0, "duration_ms");
        assert_eq!(ev[3].0, "__end__");
        Tracer::clear();
    }

    #[test]
    #[serial]
    fn test_trace_error() {
        let events = setup_memory_tracer();
        let result: Result<String, _> = trace("err_span", &json!(null), || Err("boom".into()));
        assert!(result.is_err());

        let ev = events.lock().unwrap();
        assert_eq!(ev[0].0, "inputs");
        assert_eq!(ev[1].0, "error");
        assert_eq!(ev[1].1, json!("boom"));
        assert_eq!(ev[2].0, "duration_ms");
        assert_eq!(ev[3].0, "__end__");
        Tracer::clear();
    }

    #[test]
    #[serial]
    fn test_trace_span_manual() {
        let events = setup_memory_tracer();
        let result: Result<i32, _> = trace_span("manual", |span| {
            span.emit("step", &json!("one"));
            span.emit("step", &json!("two"));
            Ok(42)
        });
        assert_eq!(result.unwrap(), 42);

        let ev = events.lock().unwrap();
        assert_eq!(ev[0].0, "step");
        assert_eq!(ev[0].1, json!("one"));
        assert_eq!(ev[1].0, "step");
        assert_eq!(ev[1].1, json!("two"));
        assert_eq!(ev[2].0, "duration_ms");
        assert_eq!(ev[3].0, "__end__");
        Tracer::clear();
    }

    #[test]
    #[serial]
    fn test_tracer_add_remove() {
        Tracer::clear();
        let events = Arc::new(Mutex::new(Vec::new()));
        Tracer::add(
            "a",
            MemoryFactory {
                events: events.clone(),
            },
        );

        // Span should reach the backend.
        let span = Tracer::start("sig");
        span.emit("x", &json!(1));
        span.end();
        assert!(!events.lock().unwrap().is_empty());

        // After remove, no more events.
        Tracer::remove("a");
        events.lock().unwrap().clear();
        let span = Tracer::start("sig");
        span.emit("x", &json!(2));
        span.end();
        assert!(events.lock().unwrap().is_empty());
        Tracer::clear();
    }

    #[test]
    #[serial]
    fn test_sanitize_api_key() {
        let input = json!({"api_key": "sk-123", "name": "test"});
        let sanitized = sanitize_value("root", &input);
        assert_eq!(sanitized["api_key"], json!("***REDACTED***"));
        assert_eq!(sanitized["name"], json!("test"));
    }

    #[test]
    #[serial]
    fn test_sanitize_password() {
        let input = json!({"password": "hunter2", "data": "visible"});
        let sanitized = sanitize_value("root", &input);
        assert_eq!(sanitized["password"], json!("***REDACTED***"));
        assert_eq!(sanitized["data"], json!("visible"));
    }

    #[test]
    #[serial]
    fn test_sanitize_nested() {
        let input = json!({"config": {"secret": "shh", "host": "localhost"}});
        let sanitized = sanitize_value("root", &input);
        assert_eq!(sanitized["config"]["secret"], json!("***REDACTED***"));
        assert_eq!(sanitized["config"]["host"], json!("localhost"));
    }

    #[test]
    #[serial]
    fn test_sanitize_bearer_token() {
        let input = json!({"bearer": "abc", "token": "xyz", "tokens": "visible"});
        let sanitized = sanitize_value("root", &input);
        assert_eq!(sanitized["bearer"], json!("***REDACTED***"));
        assert_eq!(sanitized["token"], json!("***REDACTED***"));
        // "tokens" should NOT be redacted (regex has negative lookahead)
        assert_eq!(sanitized["tokens"], json!("visible"));
    }

    #[test]
    #[serial]
    fn test_sanitize_preserves_authors() {
        // "auth" pattern should not match "authors"
        let input = json!({"authors": ["Alice", "Bob"]});
        let sanitized = sanitize_value("root", &input);
        assert_eq!(sanitized["authors"], json!(["Alice", "Bob"]));
    }

    #[test]
    #[serial]
    fn test_sanitize_top_level_key() {
        // If the top-level key itself is sensitive, the whole value is redacted.
        let val = json!({"nested": "data"});
        let sanitized = sanitize_value("api_key", &val);
        assert_eq!(sanitized, json!("***REDACTED***"));
    }
}