agentic_logging 0.1.3

Centralized JSONL logging infrastructure for agentic tools
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
//! Centralized JSONL logging infrastructure for agentic tools.
//!
//! This crate provides utilities for logging tool calls to JSONL files with:
//! - Atomic writes with file locking
//! - Optional markdown response files for large outputs
//! - Daily bucket organization
//! - Disable via `AGENTIC_LOGGING_DISABLED=1` environment variable

use atomicwrites::AtomicFile;
use atomicwrites::OverwriteBehavior;
use chrono::DateTime;
use chrono::Utc;

// Re-export chrono types for downstream crates
pub use chrono;
use fd_lock::RwLock;
use serde::Deserialize;
use serde::Serialize;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use thiserror::Error;
use uuid::Uuid;

/// Errors that can occur during logging operations.
#[derive(Error, Debug)]
pub enum LogError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
    #[error("Atomic write error: {0}")]
    AtomicWrite(String),
}

impl<E: std::fmt::Display> From<atomicwrites::Error<E>> for LogError {
    fn from(e: atomicwrites::Error<E>) -> Self {
        LogError::AtomicWrite(e.to_string())
    }
}

/// Token usage information for API calls.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenUsage {
    pub prompt: u32,
    pub completion: u32,
    pub total: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_tokens: Option<u32>,
}

/// A single tool call record for JSONL logging.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallRecord {
    /// Unique identifier for this call
    pub call_id: String,
    /// Server name (e.g., "gpt5_reasoner", "coding_agent_tools", "thoughts_tool")
    pub server: String,
    /// Tool name (e.g., "plan", "reasoning", "ask_agent", "ls")
    pub tool: String,
    /// When the call started
    pub started_at: DateTime<Utc>,
    /// When the call completed
    pub completed_at: DateTime<Utc>,
    /// Duration in milliseconds
    pub duration_ms: u128,
    /// Request parameters (serialized as JSON)
    pub request: serde_json::Value,
    /// Path to markdown response file, if one was written
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_file: Option<String>,
    /// Whether the call succeeded
    pub success: bool,
    /// Error message if the call failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// Model used for the call (e.g., "openai/gpt-5")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Token usage if available
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_usage: Option<TokenUsage>,
    /// Summary data for compact tools (e.g., {"entries": 10, "has_more": true})
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<serde_json::Value>,
}

/// Check if logging is disabled via environment variable.
pub fn logging_disabled() -> bool {
    match std::env::var("AGENTIC_LOGGING_DISABLED") {
        Ok(v) => matches!(v.to_lowercase().as_str(), "1" | "true" | "yes" | "on"),
        Err(_) => false,
    }
}

/// Timer utility for measuring call duration and generating call IDs.
pub struct CallTimer {
    /// Unique call identifier
    pub call_id: String,
    /// When the call started (UTC)
    pub started_at: DateTime<Utc>,
    /// Instant for measuring elapsed time
    start_instant: std::time::Instant,
}

impl CallTimer {
    /// Start a new timer with a fresh call ID.
    pub fn start() -> Self {
        Self {
            call_id: Uuid::new_v4().to_string(),
            started_at: Utc::now(),
            start_instant: std::time::Instant::now(),
        }
    }

    /// Finish the timer and return the completion time and duration.
    pub fn finish(&self) -> (DateTime<Utc>, u128) {
        let completed_at = Utc::now();
        let duration_ms = self.start_instant.elapsed().as_millis();
        (completed_at, duration_ms)
    }

    /// Return the elapsed duration in milliseconds without capturing a new timestamp.
    ///
    /// Useful when you need to reuse a previously captured `completed_at` timestamp
    /// for consistent day-bucket placement.
    pub fn elapsed_ms(&self) -> u128 {
        self.start_instant.elapsed().as_millis()
    }
}

/// Writer for JSONL log files and markdown response files.
pub struct LogWriter {
    base_logs_dir: PathBuf,
}

impl LogWriter {
    /// Create a new log writer with the given base logs directory.
    pub fn new(base_logs_dir: impl Into<PathBuf>) -> Self {
        Self {
            base_logs_dir: base_logs_dir.into(),
        }
    }

    /// Generate the day bucket name from a timestamp.
    fn day_bucket_name(date: DateTime<Utc>) -> String {
        date.format("tool_logs_%Y-%m-%d").to_string()
    }

    /// Ensure the JSONL file and markdown directory exist for a day bucket.
    pub fn ensure_day_dirs(&self, day_bucket: &str) -> Result<(PathBuf, PathBuf), LogError> {
        let jsonl = self.base_logs_dir.join(format!("{day_bucket}.jsonl"));
        let md_dir = self.base_logs_dir.join(day_bucket);
        std::fs::create_dir_all(&self.base_logs_dir)?;
        std::fs::create_dir_all(&md_dir)?;
        Ok((jsonl, md_dir))
    }

    /// Write a markdown response file and return its filename.
    ///
    /// Returns an empty string if logging is disabled.
    pub fn write_markdown_response(
        &self,
        completed_at: DateTime<Utc>,
        call_id: &str,
        content: &str,
    ) -> Result<String, LogError> {
        if logging_disabled() {
            return Ok(String::new());
        }
        let bucket = Self::day_bucket_name(completed_at);
        let (_jsonl, md_dir) = self.ensure_day_dirs(&bucket)?;
        let filename = format!("{call_id}.md");
        let target = md_dir.join(&filename);
        let af = AtomicFile::new(&target, OverwriteBehavior::AllowOverwrite);
        af.write(|f| f.write_all(content.as_bytes()))?;
        Ok(filename)
    }

    /// Append a tool call record to the JSONL log file.
    ///
    /// Uses file locking to prevent concurrent write corruption.
    /// Returns Ok(()) if logging is disabled.
    pub fn append_jsonl(&self, record: &ToolCallRecord) -> Result<(), LogError> {
        if logging_disabled() {
            return Ok(());
        }
        let bucket = Self::day_bucket_name(record.completed_at);
        let (jsonl_path, _md_dir) = self.ensure_day_dirs(&bucket)?;

        let file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&jsonl_path)?;
        let mut lock = RwLock::new(file);
        let mut guard = lock.write()?;
        serde_json::to_writer(&mut *guard, record)?;
        guard.write_all(b"\n")?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::io::Read;

    /// RAII guard that removes an environment variable on drop.
    /// Ensures cleanup even if a test panics.
    struct EnvGuard(&'static str);

    impl Drop for EnvGuard {
        fn drop(&mut self) {
            // SAFETY: Tests using this guard run with #[serial] to prevent concurrent env access
            unsafe {
                std::env::remove_var(self.0);
            }
        }
    }

    #[test]
    fn test_call_timer_generates_uuid() {
        let timer = CallTimer::start();
        assert!(!timer.call_id.is_empty());
        assert!(Uuid::parse_str(&timer.call_id).is_ok());
    }

    #[test]
    fn test_call_timer_measures_duration() {
        let timer = CallTimer::start();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let (completed_at, duration_ms) = timer.finish();
        assert!(duration_ms >= 10);
        assert!(completed_at >= timer.started_at);
    }

    #[test]
    #[serial]
    fn test_logging_disabled_env_var() {
        // Guard ensures cleanup even if assertions fail
        let _guard = EnvGuard("AGENTIC_LOGGING_DISABLED");

        // SAFETY: serial_test ensures no concurrent env access
        unsafe {
            // Test with various values
            std::env::set_var("AGENTIC_LOGGING_DISABLED", "1");
            assert!(logging_disabled());

            std::env::set_var("AGENTIC_LOGGING_DISABLED", "true");
            assert!(logging_disabled());

            std::env::set_var("AGENTIC_LOGGING_DISABLED", "yes");
            assert!(logging_disabled());

            std::env::set_var("AGENTIC_LOGGING_DISABLED", "on");
            assert!(logging_disabled());

            std::env::set_var("AGENTIC_LOGGING_DISABLED", "0");
            assert!(!logging_disabled());

            std::env::set_var("AGENTIC_LOGGING_DISABLED", "false");
            assert!(!logging_disabled());

            std::env::remove_var("AGENTIC_LOGGING_DISABLED");
            assert!(!logging_disabled());
        }
    }

    #[test]
    fn test_day_bucket_name_format() {
        let date = DateTime::parse_from_rfc3339("2025-03-15T10:30:00Z")
            .unwrap()
            .with_timezone(&Utc);
        assert_eq!(LogWriter::day_bucket_name(date), "tool_logs_2025-03-15");
    }

    #[test]
    #[serial]
    fn test_jsonl_append_creates_file() {
        let temp = tempfile::tempdir().unwrap();
        let writer = LogWriter::new(temp.path());

        let timer = CallTimer::start();
        let (completed_at, duration_ms) = timer.finish();

        let record = ToolCallRecord {
            call_id: timer.call_id.clone(),
            server: "test_server".into(),
            tool: "test_tool".into(),
            started_at: timer.started_at,
            completed_at,
            duration_ms,
            request: serde_json::json!({"param": "value"}),
            response_file: None,
            success: true,
            error: None,
            model: None,
            token_usage: None,
            summary: None,
        };

        writer.append_jsonl(&record).unwrap();

        // Check file was created
        let bucket = LogWriter::day_bucket_name(completed_at);
        let jsonl_path = temp.path().join(format!("{bucket}.jsonl"));
        assert!(jsonl_path.exists());

        // Check content
        let mut content = String::new();
        std::fs::File::open(&jsonl_path)
            .unwrap()
            .read_to_string(&mut content)
            .unwrap();
        assert!(content.contains("test_server"));
        assert!(content.contains("test_tool"));
        assert!(content.ends_with('\n'));
    }

    #[test]
    #[serial]
    fn test_jsonl_append_multiple_lines() {
        let temp = tempfile::tempdir().unwrap();
        let writer = LogWriter::new(temp.path());

        // Write two records
        for i in 0..2 {
            let timer = CallTimer::start();
            let (completed_at, duration_ms) = timer.finish();
            let record = ToolCallRecord {
                call_id: timer.call_id,
                server: "test".into(),
                tool: format!("tool_{i}"),
                started_at: timer.started_at,
                completed_at,
                duration_ms,
                request: serde_json::json!({}),
                response_file: None,
                success: true,
                error: None,
                model: None,
                token_usage: None,
                summary: None,
            };
            writer.append_jsonl(&record).unwrap();
        }

        // Verify two lines
        let bucket = LogWriter::day_bucket_name(Utc::now());
        let jsonl_path = temp.path().join(format!("{bucket}.jsonl"));
        let content = std::fs::read_to_string(&jsonl_path).unwrap();
        let lines: Vec<_> = content.lines().collect();
        assert_eq!(lines.len(), 2);
        assert!(lines[0].contains("tool_0"));
        assert!(lines[1].contains("tool_1"));
    }

    #[test]
    #[serial]
    fn test_markdown_response_file() {
        let temp = tempfile::tempdir().unwrap();
        let writer = LogWriter::new(temp.path());

        let timer = CallTimer::start();
        let (completed_at, _) = timer.finish();

        let content = "# Test Response\n\nThis is markdown content.";
        let filename = writer
            .write_markdown_response(completed_at, &timer.call_id, content)
            .unwrap();

        assert_eq!(filename, format!("{}.md", timer.call_id));

        // Verify file content
        let bucket = LogWriter::day_bucket_name(completed_at);
        let md_path = temp.path().join(&bucket).join(&filename);
        let read_content = std::fs::read_to_string(&md_path).unwrap();
        assert_eq!(read_content, content);
    }

    #[test]
    #[serial]
    fn test_disabled_logging_skips_writes() {
        // SAFETY: serial_test ensures no concurrent env access
        unsafe {
            std::env::set_var("AGENTIC_LOGGING_DISABLED", "1");
        }
        let _guard = EnvGuard("AGENTIC_LOGGING_DISABLED");

        let temp = tempfile::tempdir().unwrap();
        let writer = LogWriter::new(temp.path());

        let timer = CallTimer::start();
        let (completed_at, duration_ms) = timer.finish();

        // JSONL should be no-op
        let record = ToolCallRecord {
            call_id: timer.call_id.clone(),
            server: "test".into(),
            tool: "test".into(),
            started_at: timer.started_at,
            completed_at,
            duration_ms,
            request: serde_json::json!({}),
            response_file: None,
            success: true,
            error: None,
            model: None,
            token_usage: None,
            summary: None,
        };
        writer.append_jsonl(&record).unwrap();

        // Markdown should return empty string
        let filename = writer
            .write_markdown_response(completed_at, &timer.call_id, "content")
            .unwrap();
        assert!(filename.is_empty());

        // No files should be created
        let entries: Vec<_> = std::fs::read_dir(temp.path()).unwrap().collect();
        assert!(entries.is_empty());
        // Guard automatically removes env var on drop
    }

    #[test]
    fn test_token_usage_serialization() {
        let usage = TokenUsage {
            prompt: 100,
            completion: 50,
            total: 150,
            reasoning_tokens: Some(7),
        };
        let json = serde_json::to_string(&usage).unwrap();
        assert!(json.contains("\"prompt\":100"));
        assert!(json.contains("\"completion\":50"));
        assert!(json.contains("\"total\":150"));
        assert!(json.contains("\"reasoning_tokens\":7"));

        let usage_none = TokenUsage {
            prompt: 1,
            completion: 2,
            total: 3,
            reasoning_tokens: None,
        };
        let json_none = serde_json::to_string(&usage_none).unwrap();
        assert!(!json_none.contains("reasoning_tokens"));
    }

    #[test]
    fn test_tool_call_record_optional_fields_omitted() {
        let timer = CallTimer::start();
        let (completed_at, duration_ms) = timer.finish();

        let record = ToolCallRecord {
            call_id: timer.call_id,
            server: "test".into(),
            tool: "test".into(),
            started_at: timer.started_at,
            completed_at,
            duration_ms,
            request: serde_json::json!({}),
            response_file: None,
            success: true,
            error: None,
            model: None,
            token_usage: None,
            summary: None,
        };

        let json = serde_json::to_string(&record).unwrap();
        // Optional None fields should not appear
        assert!(!json.contains("response_file"));
        assert!(!json.contains("error"));
        assert!(!json.contains("model"));
        assert!(!json.contains("token_usage"));
        assert!(!json.contains("summary"));
    }
}