lix 0.12.3

Embeddable version control for apps and AI agents.
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
use std::future::Future;
use std::sync::Arc;

use crate::telemetry::{
    ActiveTelemetrySpan, TelemetryAttribute, TelemetrySink, TelemetrySpanKind, TelemetrySpanStart,
    TelemetrySpanStatus, unix_time_ms,
};
use crate::{ExecuteResult, LixError};

const MAX_QUERY_TEXT_CHARS: usize = 4_096;

pub(crate) struct SqlStatementTelemetry {
    span: ActiveTelemetrySpan,
}

impl SqlStatementTelemetry {
    pub(crate) fn start(
        sink: Option<&Arc<dyn TelemetrySink>>,
        sql: &str,
        execution_kind: &'static str,
        batch_index: Option<usize>,
    ) -> Option<Self> {
        let sink = sink?;
        if !sink.enabled(TelemetrySpanKind::SqlQuery) {
            return None;
        }
        Some(Self {
            span: ActiveTelemetrySpan::start(
                sink,
                statement_start(sql, execution_kind, batch_index),
            ),
        })
    }

    pub(crate) async fn instrument<F>(&self, future: F) -> F::Output
    where
        F: Future,
    {
        self.span.instrument(future).await
    }

    pub(crate) fn finish(self, result: &Result<ExecuteResult, LixError>) {
        let (status, attributes) = statement_end(result);
        self.span.finish(status, attributes);
    }
}

fn statement_start(
    sql: &str,
    execution_kind: &'static str,
    batch_index: Option<usize>,
) -> TelemetrySpanStart {
    let query_text = sanitize_query_text(sql);
    let operation = query_operation(&query_text);
    let fingerprint = blake3::hash(query_text.as_bytes()).to_hex().to_string();
    let mut attributes = vec![
        TelemetryAttribute::string("otel.name", operation.clone()),
        TelemetryAttribute::string("otel.kind", "internal"),
        TelemetryAttribute::string("db.system.name", "lix"),
        TelemetryAttribute::string("db.operation.name", operation.clone()),
        TelemetryAttribute::string("db.query.summary", operation),
        TelemetryAttribute::string("db.query.text", query_text),
        TelemetryAttribute::string("lix.sql.fingerprint", fingerprint),
        TelemetryAttribute::string("lix.execution.kind", execution_kind),
    ];
    if let Some(batch_index) = batch_index {
        attributes.push(TelemetryAttribute::u64(
            "lix.batch.index",
            u64::try_from(batch_index).unwrap_or(u64::MAX),
        ));
    }
    TelemetrySpanStart {
        kind: TelemetrySpanKind::SqlQuery,
        name: "lix.sql.query",
        started_at_unix_ms: unix_time_ms(),
        attributes,
    }
}

fn statement_end(
    result: &Result<ExecuteResult, LixError>,
) -> (TelemetrySpanStatus, Vec<TelemetryAttribute>) {
    match result {
        Ok(result) => (
            TelemetrySpanStatus::Ok,
            vec![
                TelemetryAttribute::u64(
                    "db.response.returned_rows",
                    u64::try_from(result.len()).unwrap_or(u64::MAX),
                ),
                TelemetryAttribute::u64("lix.rows_affected", result.rows_affected()),
                TelemetryAttribute::string("otel.status_code", "OK"),
            ],
        ),
        Err(error) => (
            TelemetrySpanStatus::Error,
            vec![
                TelemetryAttribute::string("error.type", error.code.clone()),
                TelemetryAttribute::string("otel.status_code", "ERROR"),
            ],
        ),
    }
}

pub(crate) fn start_batch(
    sink: Option<&Arc<dyn TelemetrySink>>,
    kind: TelemetrySpanKind,
    size: usize,
) -> Option<ActiveTelemetrySpan> {
    let sink = sink?;
    if !sink.enabled(kind) {
        return None;
    }
    let (name, display_name, execution_kind) = match kind {
        TelemetrySpanKind::SqlBatch => ("lix.sql.batch", "SQL batch", "batch"),
        TelemetrySpanKind::SqlCoherentReadBatch => (
            "lix.sql.coherent_read_batch",
            "SQL coherent read batch",
            "coherent_read_batch",
        ),
        TelemetrySpanKind::SqlQuery => return None,
    };
    Some(ActiveTelemetrySpan::start(
        sink,
        TelemetrySpanStart {
            kind,
            name,
            started_at_unix_ms: unix_time_ms(),
            attributes: vec![
                TelemetryAttribute::string("otel.name", display_name),
                TelemetryAttribute::string("otel.kind", "internal"),
                TelemetryAttribute::string("db.system.name", "lix"),
                TelemetryAttribute::u64(
                    "db.operation.batch.size",
                    u64::try_from(size).unwrap_or(u64::MAX),
                ),
                TelemetryAttribute::string("lix.execution.kind", execution_kind),
            ],
        },
    ))
}

pub(crate) fn finish_operation<T>(span: ActiveTelemetrySpan, result: &Result<T, LixError>) {
    match result {
        Ok(_) => span.finish(
            TelemetrySpanStatus::Ok,
            vec![TelemetryAttribute::string("otel.status_code", "OK")],
        ),
        Err(error) => span.finish(
            TelemetrySpanStatus::Error,
            vec![
                TelemetryAttribute::string("error.type", error.code.clone()),
                TelemetryAttribute::string("otel.status_code", "ERROR"),
            ],
        ),
    }
}

#[cfg(test)]
fn statement_fingerprint(sql: &str) -> String {
    statement_start(sql, "execute", None)
        .attributes
        .into_iter()
        .find_map(|attribute| {
            if attribute.key == "lix.sql.fingerprint"
                && let crate::telemetry::TelemetryValue::String(value) = attribute.value
            {
                Some(value)
            } else {
                None
            }
        })
        .expect("statement telemetry includes a fingerprint")
}
fn query_operation(query_text: &str) -> String {
    const OPERATIONS: &[&str] = &[
        "SELECT", "INSERT", "UPDATE", "DELETE", "MERGE", "CREATE", "ALTER", "DROP", "TRUNCATE",
        "EXPLAIN", "SHOW", "DESCRIBE", "SET",
    ];
    query_text
        .split(|character: char| !character.is_ascii_alphanumeric() && character != '_')
        .map(str::to_ascii_uppercase)
        .find(|token| OPERATIONS.contains(&token.as_str()))
        .unwrap_or_else(|| "SQL".to_string())
}

/// Removes SQL comments and literal values while preserving statement shape and
/// placeholders. If a construct is ambiguous, it is redacted rather than
/// copied so telemetry cannot become a side channel for query parameters.
fn sanitize_query_text(sql: &str) -> String {
    let characters = sql.chars().collect::<Vec<_>>();
    let mut output = String::with_capacity(sql.len().min(MAX_QUERY_TEXT_CHARS));
    let mut output_chars = 0;
    let mut index = 0;
    let mut pending_space = false;

    'query: while index < characters.len() {
        let character = characters[index];
        if character.is_whitespace() {
            pending_space = true;
            index += 1;
            continue;
        }
        if character == '-' && characters.get(index + 1) == Some(&'-') {
            index += 2;
            while index < characters.len() && characters[index] != '\n' {
                index += 1;
            }
            pending_space = true;
            continue;
        }
        if character == '/' && characters.get(index + 1) == Some(&'*') {
            index += 2;
            let mut depth = 1_u32;
            while index < characters.len() && depth > 0 {
                if characters.get(index) == Some(&'/') && characters.get(index + 1) == Some(&'*') {
                    depth = depth.saturating_add(1);
                    index += 2;
                } else if characters.get(index) == Some(&'*')
                    && characters.get(index + 1) == Some(&'/')
                {
                    depth -= 1;
                    index += 2;
                } else {
                    index += 1;
                }
            }
            pending_space = true;
            continue;
        }

        if !push_pending_space(&mut output, &mut output_chars, &mut pending_space) {
            break;
        }

        if character == '\'' {
            if !push_query_text_char(&mut output, &mut output_chars, '?') {
                break;
            }
            index = skip_single_quoted_literal(&characters, index + 1);
            continue;
        }
        if character == '$' {
            if characters.get(index + 1).is_some_and(char::is_ascii_digit) {
                if !push_query_text_char(&mut output, &mut output_chars, '$') {
                    break;
                }
                index += 1;
                while index < characters.len() && characters[index].is_ascii_digit() {
                    if !push_query_text_char(&mut output, &mut output_chars, characters[index]) {
                        break 'query;
                    }
                    index += 1;
                }
                continue;
            }
            if let Some((delimiter, body_start)) = dollar_quote_delimiter(&characters, index) {
                if !push_query_text_char(&mut output, &mut output_chars, '?') {
                    break;
                }
                index = skip_dollar_quoted_literal(&characters, body_start, &delimiter);
                continue;
            }
        }
        if character.is_ascii_digit()
            && !characters
                .get(index.wrapping_sub(1))
                .is_some_and(|previous| previous.is_ascii_alphanumeric() || *previous == '_')
        {
            if !push_query_text_char(&mut output, &mut output_chars, '?') {
                break;
            }
            index = skip_numeric_literal(&characters, index + 1);
            continue;
        }
        if matches!(character, '"' | '`' | '[') {
            let closing = if character == '[' { ']' } else { character };
            if !push_query_text_char(&mut output, &mut output_chars, character) {
                break;
            }
            index += 1;
            while index < characters.len() {
                let current = characters[index];
                if !push_query_text_char(&mut output, &mut output_chars, current) {
                    break 'query;
                }
                index += 1;
                if current == closing {
                    if characters.get(index) == Some(&closing) {
                        if !push_query_text_char(&mut output, &mut output_chars, closing) {
                            break 'query;
                        }
                        index += 1;
                    } else {
                        break;
                    }
                }
            }
            continue;
        }

        if !push_query_text_char(&mut output, &mut output_chars, character) {
            break;
        }
        index += 1;
    }

    output.trim().to_string()
}

fn push_query_text_char(output: &mut String, output_chars: &mut usize, character: char) -> bool {
    if *output_chars >= MAX_QUERY_TEXT_CHARS {
        return false;
    }
    output.push(character);
    *output_chars += 1;
    true
}

fn push_pending_space(
    output: &mut String,
    output_chars: &mut usize,
    pending_space: &mut bool,
) -> bool {
    if *pending_space && !output.is_empty() && !output.ends_with(' ') {
        if !push_query_text_char(output, output_chars, ' ') {
            return false;
        }
    }
    *pending_space = false;
    true
}

fn skip_single_quoted_literal(characters: &[char], mut index: usize) -> usize {
    while index < characters.len() {
        if characters[index] == '\'' {
            if characters.get(index + 1) == Some(&'\'') {
                index += 2;
                continue;
            }
            return index + 1;
        }
        if characters[index] == '\\' && index + 1 < characters.len() {
            index += 2;
        } else {
            index += 1;
        }
    }
    index
}

fn dollar_quote_delimiter(characters: &[char], start: usize) -> Option<(Vec<char>, usize)> {
    let mut index = start + 1;
    while index < characters.len()
        && (characters[index].is_ascii_alphanumeric() || characters[index] == '_')
    {
        index += 1;
    }
    if characters.get(index) != Some(&'$') {
        return None;
    }
    if index > start + 1 && characters[start + 1].is_ascii_digit() {
        return None;
    }
    Some((characters[start..=index].to_vec(), index + 1))
}

fn skip_dollar_quoted_literal(characters: &[char], mut index: usize, delimiter: &[char]) -> usize {
    while index + delimiter.len() <= characters.len() {
        if &characters[index..index + delimiter.len()] == delimiter {
            return index + delimiter.len();
        }
        index += 1;
    }
    characters.len()
}

fn skip_numeric_literal(characters: &[char], mut index: usize) -> usize {
    while index < characters.len()
        && matches!(characters[index], '0'..='9' | '.' | 'e' | 'E' | '+' | '-' | 'x' | 'X' | 'a'..='f' | 'A'..='F' | '_')
    {
        index += 1;
    }
    index
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn absent_sink_disables_statement_telemetry() {
        assert!(SqlStatementTelemetry::start(None, "SELECT 'private'", "execute", None).is_none());
    }

    #[test]
    fn sanitizes_literals_comments_and_preserves_placeholders() {
        let sql = "SELECT value, 'private'' value', 42, $tag$secret$tag$ FROM lix_key_value -- hidden\n WHERE key = $1 AND other = ?";
        assert_eq!(
            sanitize_query_text(sql),
            "SELECT value, ?, ?, ? FROM lix_key_value WHERE key = $1 AND other = ?"
        );
    }

    #[test]
    fn preserves_quoted_identifiers() {
        assert_eq!(
            sanitize_query_text("SELECT \"odd table\".value FROM \"odd table\" WHERE id = 9"),
            "SELECT \"odd table\".value FROM \"odd table\" WHERE id = ?"
        );
    }

    #[test]
    fn caps_query_text_inside_quoted_identifiers() {
        let oversized_identifier = format!("SELECT \"{}", "a".repeat(MAX_QUERY_TEXT_CHARS * 2));
        let sanitized = sanitize_query_text(&oversized_identifier);

        assert_eq!(sanitized.chars().count(), MAX_QUERY_TEXT_CHARS);
        assert!(sanitized.starts_with("SELECT \""));
    }

    #[test]
    fn nested_comments_cannot_leak_text() {
        assert_eq!(
            sanitize_query_text("SELECT 1 /* outer /* private */ still-private */ FROM t"),
            "SELECT ? FROM t"
        );
    }

    #[test]
    fn fingerprint_depends_on_shape_not_literal_values() {
        let first = statement_fingerprint("SELECT * FROM t WHERE id = 1");
        let second = statement_fingerprint("SELECT * FROM t WHERE id = 999");
        assert_eq!(first, second);
    }
}