splice 2.6.2

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! High-level execution logging functions.
//!
//! This module provides a non-blocking logging interface for recording
//! Splice operations to the execution log database.
//!
//! # Error Handling
//!
//! Execution logging functions return `Result<()>` to allow callers
//! to handle errors appropriately. For production use, logging failures
//! are non-fatal - callers should use `if let Err(e)` patterns to log
//! warnings without failing the primary operation.
//!
//! This design ensures:
//! - Logging failures don't break the primary operation
//! - Errors are visible in logs for debugging
//! - Callers can choose their error handling strategy

use crate::error::{Result, SpliceError};
use crate::execution::{
    init_execution_log_db, insert_execution_log, ExecutionLog, ExecutionLogBuilder,
};
use crate::output::OperationResult;
use rusqlite::Connection;
use std::path::PathBuf;

/// Environment variable to enable/disable execution logging.
const EXECUTION_LOG_ENV: &str = "SPLICE_EXECUTION_LOG";

/// Splice directory name.
const SPLICE_DIR: &str = ".splice";

/// Configuration for execution logging behavior.
///
/// This struct allows dependency injection for testing and programmatic
/// control of execution logging without relying on environment variables.
#[derive(Debug, Clone, PartialEq)]
pub struct ExecutionLogConfig {
    /// Whether execution logging is enabled.
    ///
    /// If None, reads from SPLICE_EXECUTION_LOG environment variable.
    /// If Some(bool), uses the provided value explicitly.
    pub enabled: Option<bool>,
}

impl ExecutionLogConfig {
    /// Create a config that explicitly enables logging.
    #[must_use]
    pub fn enabled() -> Self {
        Self {
            enabled: Some(true),
        }
    }

    /// Create a config that explicitly disables logging.
    #[must_use]
    pub fn disabled() -> Self {
        Self {
            enabled: Some(false),
        }
    }

    /// Create a config that respects the environment variable.
    #[must_use]
    pub fn from_env() -> Self {
        Self { enabled: None }
    }

    /// Check if logging is enabled with this configuration.
    ///
    /// - If `enabled` is `Some(bool)`, returns that value
    /// - If `enabled` is `None`, reads from SPLICE_EXECUTION_LOG environment variable
    #[must_use]
    pub fn is_enabled(&self) -> bool {
        match self.enabled {
            Some(explicit) => explicit,
            None => {
                // Read from environment variable
                std::env::var(EXECUTION_LOG_ENV)
                    .map(|v| {
                        let v_lower = v.to_lowercase();
                        v_lower != "false" && v_lower != "0" && v_lower != "no"
                    })
                    .unwrap_or(true) // Default: enabled
            }
        }
    }
}

impl Default for ExecutionLogConfig {
    fn default() -> Self {
        Self::from_env()
    }
}

/// Get execution log database path.
///
/// Returns the path to `.splice/operations.db` in the current directory.
pub fn db_path() -> PathBuf {
    std::env::current_dir()
        .unwrap_or_else(|_| PathBuf::from("."))
        .join(SPLICE_DIR)
        .join("operations.db")
}

/// Check if execution log is enabled.
///
/// Checks the `SPLICE_EXECUTION_LOG` environment variable.
/// Default: `true` (enabled).
///
/// Set to `false`, `0`, or `no` to disable logging.
///
/// For testable or programmatic control, use `is_enabled_with_config` instead.
///
/// # Examples
///
/// ```no_run
/// use splice::execution::log::is_enabled;
///
/// if is_enabled() {
///     println!("Execution logging is active");
/// }
/// ```
pub fn is_enabled() -> bool {
    is_enabled_with_config(None)
}

/// Check if execution log is enabled with optional config override.
///
/// This is the preferred function for code that needs testability
/// or programmatic control over execution logging.
///
/// # Arguments
///
/// * `config` - Optional configuration override.
///   - `None`: Uses `ExecutionLogConfig::from_env()` (reads environment variable)
///   - `Some(config)`: Uses the provided configuration
///
/// # Examples
///
/// ```
/// use splice::execution::log::{is_enabled_with_config, ExecutionLogConfig};
///
/// // Default behavior (respects environment variable)
/// let enabled = is_enabled_with_config(None);
///
/// // Explicit control (useful for testing)
/// let config = ExecutionLogConfig::disabled();
/// let enabled = is_enabled_with_config(Some(&config));
/// ```
#[must_use]
pub fn is_enabled_with_config(config: Option<&ExecutionLogConfig>) -> bool {
    config
        .unwrap_or(&ExecutionLogConfig::from_env())
        .is_enabled()
}

/// Initialize execution log database.
///
/// Creates the database file and tables if they don't exist.
/// This function is idempotent - calling it multiple times is safe.
///
/// # Returns
///
/// Returns a `rusqlite::Connection` to the database.
///
/// # Errors
///
/// Returns an error if:
/// - The directory cannot be created
/// - The database cannot be opened
/// - Table creation fails
pub fn init_db() -> Result<Connection> {
    let db_dir = std::env::current_dir()
        .unwrap_or_else(|_| PathBuf::from("."))
        .join(SPLICE_DIR);

    init_execution_log_db(&db_dir)
}

/// Initialize execution log database in a specific directory.
///
/// This is useful for testing where you want to avoid changing the current directory.
///
/// # Arguments
///
/// * `base_dir` - Base directory where `.splice` will be created
///
/// # Returns
///
/// Returns a `rusqlite::Connection` to the database.
///
/// # Errors
///
/// Returns an error if:
/// - The directory cannot be created
/// - The database cannot be opened
/// - Table creation fails
pub fn init_db_in_dir(base_dir: &std::path::Path) -> Result<Connection> {
    let db_dir = base_dir.join(SPLICE_DIR);
    init_execution_log_db(&db_dir)
}

/// Record operation execution to log.
///
/// Extracts data from `OperationResult` and records it to the database.
/// This function is non-blocking - errors are logged but don't fail the operation.
///
/// # Arguments
///
/// * `result` - Operation result to record
/// * `duration_ms` - Operation duration in milliseconds
/// * `command_line` - Optional full command line for reproducibility
///
/// # Returns
///
/// Returns `Ok(())` if recording succeeds, or `Err` if it fails.
/// Callers should log errors as warnings, not fail the operation.
pub fn record_execution(
    result: &OperationResult,
    duration_ms: i64,
    command_line: Option<String>,
) -> Result<()> {
    let log_entry = build_log_entry(result, duration_ms, command_line, None)?;
    let conn = init_db()?;
    insert_execution_log(&conn, &log_entry)?;
    Ok(())
}

/// Record operation with parameters.
///
/// Same as `record_execution` but includes operation-specific parameters.
///
/// # Arguments
///
/// * `result` - Operation result to record
/// * `duration_ms` - Operation duration in milliseconds
/// * `command_line` - Optional full command line for reproducibility
/// * `parameters` - Operation-specific parameters (JSON)
pub fn record_execution_with_params(
    result: &OperationResult,
    duration_ms: i64,
    command_line: Option<String>,
    parameters: serde_json::Value,
) -> Result<()> {
    let log_entry = build_log_entry(result, duration_ms, command_line, Some(parameters))?;
    let conn = init_db()?;
    insert_execution_log(&conn, &log_entry)?;
    Ok(())
}

/// Record operation failure.
///
/// Records a failed operation to the execution log.
/// This is useful for tracking errors and debugging issues.
///
/// # Arguments
///
/// * `execution_id` - Unique operation identifier (UUID)
/// * `operation_type` - Type of operation (e.g., "patch", "delete")
/// * `error` - The error that occurred
/// * `duration_ms` - Operation duration before failure
/// * `command_line` - Optional full command line for reproducibility
pub fn record_execution_failure(
    execution_id: &str,
    operation_type: &str,
    error: &SpliceError,
    duration_ms: i64,
    command_line: Option<String>,
) -> Result<()> {
    let timestamp = chrono::Utc::now().to_rfc3339();
    let created_at = chrono::Utc::now().timestamp();

    // Build error details JSON
    let error_details = serde_json::json!({
        "kind": std::format!("{:?}", std::error::Error::source(error).map_or_else(
            || error.to_string(),
            |e| e.to_string()
        )),
        "message": error.to_string(),
    });

    let log_entry = ExecutionLog {
        id: 0,
        execution_id: execution_id.to_string(),
        operation_type: operation_type.to_string(),
        status: "error".to_string(),
        timestamp,
        workspace: std::env::current_dir()
            .ok()
            .map(|p| p.to_string_lossy().to_string()),
        command_line,
        parameters: None,
        result_summary: None,
        error_details: Some(error_details),
        duration_ms: Some(duration_ms),
        created_at,
    };

    let conn = init_db()?;
    insert_execution_log(&conn, &log_entry)?;
    Ok(())
}

/// Build execution log entry from operation result.
fn build_log_entry(
    result: &OperationResult,
    duration_ms: i64,
    command_line: Option<String>,
    parameters: Option<serde_json::Value>,
) -> Result<ExecutionLog> {
    let mut builder =
        ExecutionLogBuilder::new(result.execution_id.clone(), result.operation_type.clone())
            .status(result.status.clone())
            .timestamp(result.timestamp.clone())
            .duration_ms(duration_ms);

    if let Some(ref workspace) = result.workspace {
        builder = builder.workspace(workspace.clone());
    }

    if let Some(cmd) = command_line {
        builder = builder.command_line(cmd);
    }

    if let Some(params) = parameters {
        builder = builder.parameters(params);
    }

    // Build result summary JSON
    let result_summary = serde_json::json!({
        "message": result.message,
    });
    builder = builder.result_summary(result_summary);

    Ok(builder.build())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Mutex, OnceLock};
    use tempfile::TempDir;

    /// Get a lock for synchronizing environment variable access in tests.
    ///
    /// The SPLICE_EXECUTION_LOG environment variable is process-global state.
    /// Tests that modify this variable must hold this lock for their entire
    /// duration to prevent race conditions with parallel test execution.
    ///
    /// # Example
    /// ```ignore
    /// #[test]
    /// fn test_something() {
    ///     let _guard = env_lock().lock().unwrap();
    ///     std::env::set_var(EXECUTION_LOG_ENV, "false");
    ///     // ... test code ...
    ///     // _guard dropped here, after test completes
    /// }
    /// ```
    fn env_lock() -> &'static Mutex<()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
    }

    #[test]
    fn test_db_path() {
        let path = db_path();
        assert!(path.ends_with(".splice/operations.db"));
    }

    #[test]
    fn test_is_enabled_default() {
        let _guard = env_lock().lock().unwrap();
        // Clear environment variable to test default
        std::env::remove_var(EXECUTION_LOG_ENV);
        assert!(is_enabled(), "Execution log should be enabled by default");
        std::env::set_var(EXECUTION_LOG_ENV, "true"); // Reset to default
    }

    #[test]
    fn test_is_enabled_false() {
        let _guard = env_lock().lock().unwrap();
        std::env::set_var(EXECUTION_LOG_ENV, "false");
        assert!(
            !is_enabled(),
            "Execution log should be disabled when set to false"
        );
        std::env::set_var(EXECUTION_LOG_ENV, "true"); // Reset to default
    }

    #[test]
    fn test_is_enabled_true() {
        let _guard = env_lock().lock().unwrap();
        std::env::set_var(EXECUTION_LOG_ENV, "TRUE"); // Test case insensitivity
        assert!(
            is_enabled(),
            "Execution log should be enabled when set to TRUE"
        );
        std::env::set_var(EXECUTION_LOG_ENV, "true"); // Reset to default
    }

    #[test]
    fn test_init_db_creates_tables() {
        let temp_dir = TempDir::new().unwrap();

        // Use init_db_in_dir to avoid changing current directory (fixes parallel test race)
        let conn = init_db_in_dir(temp_dir.path()).unwrap();

        // Verify table exists
        let table_exists: bool = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='execution_log'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        assert!(table_exists, "execution_log table should be created");
    }

    #[test]
    fn test_record_execution() {
        let temp_dir = TempDir::new().unwrap();

        use crate::execution::insert_execution_log;
        use crate::output::OperationResult;
        use uuid::Uuid;

        let execution_id = Uuid::new_v4().to_string();
        let result =
            OperationResult::with_execution_id("patch".to_string(), Some(execution_id.clone()))
                .success("Test operation".to_string())
                .with_workspace("/test/workspace".to_string());

        let command_line = Some("splice patch test_symbol".to_string());

        // Use init_db_in_dir to avoid changing current directory (fixes parallel test race)
        let conn = init_db_in_dir(temp_dir.path()).unwrap();

        // Build log entry and insert directly
        let log_entry = build_log_entry(&result, 1234, command_line, None).unwrap();
        let insert_result = insert_execution_log(&conn, &log_entry);
        assert!(
            insert_result.is_ok(),
            "Recording should succeed: {:?}",
            insert_result.err()
        );
    }

    #[test]
    fn test_record_execution_with_params() {
        let temp_dir = TempDir::new().unwrap();

        use crate::execution::insert_execution_log;
        use crate::output::OperationResult;
        use uuid::Uuid;

        let execution_id = Uuid::new_v4().to_string();
        let result =
            OperationResult::with_execution_id("delete".to_string(), Some(execution_id.clone()))
                .success("Test delete".to_string());

        let parameters = serde_json::json!({
            "file": "/test/file.rs",
            "symbol": "test_function",
        });

        // Use init_db_in_dir to avoid changing current directory (fixes parallel test race)
        let conn = init_db_in_dir(temp_dir.path()).unwrap();

        // Build log entry and insert directly
        let log_entry = build_log_entry(&result, 567, None, Some(parameters)).unwrap();
        let insert_result = insert_execution_log(&conn, &log_entry);
        assert!(
            insert_result.is_ok(),
            "Recording with params should succeed: {:?}",
            insert_result.err()
        );
    }

    #[test]
    fn test_record_execution_failure() {
        let temp_dir = TempDir::new().unwrap();

        use crate::execution::{insert_execution_log, ExecutionLog};
        use uuid::Uuid;

        let execution_id = Uuid::new_v4().to_string();
        let error = SpliceError::Other("Test error".to_string());

        // Build the log entry directly (same as record_execution_failure does)
        let timestamp = chrono::Utc::now().to_rfc3339();
        let created_at = chrono::Utc::now().timestamp();

        let error_details = serde_json::json!({
            "kind": std::format!("{:?}", std::error::Error::source(&error).map_or_else(
                || error.to_string(),
                |e| e.to_string()
            )),
            "message": error.to_string(),
        });

        let log_entry = ExecutionLog {
            id: 0,
            execution_id: execution_id.clone(),
            operation_type: "patch".to_string(),
            status: "error".to_string(),
            timestamp,
            workspace: temp_dir.path().to_str().map(|s| s.to_string()),
            command_line: Some("splice patch test".to_string()),
            parameters: None,
            result_summary: None,
            error_details: Some(error_details),
            duration_ms: Some(100),
            created_at,
        };

        // Use init_db_in_dir to avoid changing current directory (fixes parallel test race)
        let conn = init_db_in_dir(temp_dir.path()).unwrap();

        // Insert directly
        let insert_result = insert_execution_log(&conn, &log_entry);
        assert!(
            insert_result.is_ok(),
            "Recording failure should succeed: {:?}",
            insert_result.err()
        );
    }
}