skilllite-core 0.1.15

SkillLite Core: config, skill metadata, path validation, observability
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
//! Observability: tracing init, audit log, security events.
//!
//! Uses config::ObservabilityConfig for SKILLLITE_QUIET, LOG_LEVEL, AUDIT_LOG, etc.

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use std::sync::Mutex;

use chrono::Utc;
use serde_json::json;
use tracing_subscriber::{prelude::*, EnvFilter};
use uuid::Uuid;

static SECURITY_EVENTS_PATH: Mutex<Option<String>> = Mutex::new(None);

/// Tracing initialization mode.
#[derive(Clone, Copy)]
pub enum TracingMode {
    /// Default: use SKILLLITE_LOG_LEVEL / SKILLLITE_QUIET from env
    Default,
    /// Chat: suppress agent-internal WARN (compaction, task planning) to keep UI clean
    Chat,
}

/// Initialize tracing. Call at process startup.
/// When SKILLLITE_QUIET=1 (or SKILLBOX_QUIET for compat), only WARN and above are logged.
pub fn init_tracing(mode: TracingMode) {
    let cfg = crate::config::ObservabilityConfig::from_env();
    let mut level: String = if cfg.quiet {
        "skilllite=warn".to_string()
    } else {
        cfg.log_level.clone()
    };

    // Chat mode: suppress agent-internal warnings (compaction, task planning) to avoid polluting the UI
    if matches!(mode, TracingMode::Chat) {
        level = format!("{},skilllite::agent=error", level);
    }

    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&level));

    let json = cfg.log_json;

    let _ = if json {
        tracing_subscriber::registry()
            .with(filter)
            .with(
                tracing_subscriber::fmt::layer()
                    .json()
                    .with_target(true)
                    .with_thread_ids(false),
            )
            .try_init()
    } else {
        tracing_subscriber::registry()
            .with(filter)
            .with(
                tracing_subscriber::fmt::layer()
                    .with_target(true)
                    .with_thread_ids(false),
            )
            .try_init()
    };
}

/// 解析审计日志实际写入路径。目录则按天存储 audit_YYYY-MM-DD.jsonl;.jsonl 文件则直接写入。
fn get_audit_path() -> Option<String> {
    let base = crate::config::ObservabilityConfig::from_env()
        .audit_log
        .clone()?;
    if base.is_empty() {
        return None;
    }
    let path = Path::new(&base);
    let file_path = if base.ends_with(".jsonl") {
        path.to_path_buf()
    } else {
        let today = chrono::Utc::now().format("%Y-%m-%d");
        path.join(format!("audit_{}.jsonl", today))
    };
    let file_path_str = file_path.to_string_lossy().into_owned();
    if let Some(parent) = file_path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    Some(file_path_str)
}

fn get_security_events_path() -> Option<String> {
    {
        let guard = SECURITY_EVENTS_PATH.lock().ok()?;
        if let Some(ref p) = *guard {
            return Some(p.clone());
        }
    }
    let path = crate::config::ObservabilityConfig::from_env()
        .security_events_log
        .clone()?;
    if path.is_empty() {
        return None;
    }
    if let Some(parent) = Path::new(&path).parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    {
        let mut guard = SECURITY_EVENTS_PATH.lock().ok()?;
        *guard = Some(path.clone());
    }
    Some(path)
}

fn append_jsonl(path: &str, record: &serde_json::Value) {
    if let Ok(mut f) = OpenOptions::new().create(true).append(true).open(path) {
        if let Ok(line) = serde_json::to_string(record) {
            let _ = writeln!(f, "{}", line);
            let _ = f.flush(); // 确保每条记录单独落盘,避免流式消费时行粘连
        }
    }
}

/// Audit: confirmation_requested (Rust-side L3 scan)
pub fn audit_confirmation_requested(
    skill_id: &str,
    code_hash: &str,
    issues_count: usize,
    severity: &str,
) {
    if let Some(path) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "confirmation_requested",
            "skill_id": skill_id,
            "code_hash": code_hash,
            "issues_count": issues_count,
            "severity": severity,
            "source": "rust"
        });
        append_jsonl(&path, &record);
    }
}

/// Audit: confirmation_response (Rust-side user/auto)
pub fn audit_confirmation_response(skill_id: &str, approved: bool, source: &str) {
    if let Some(path) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "confirmation_response",
            "skill_id": skill_id,
            "approved": approved,
            "source": source,
            "source_layer": "rust"
        });
        append_jsonl(&path, &record);
    }
}

/// Audit: execution_started (right before spawn — Python name: execution_started)
///
/// Also emits as "command_invoked" for backward compatibility.
pub fn audit_execution_started(skill_id: &str, cmd: &str, args: &[&str], cwd: &str) {
    if let Some(path) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "execution_started",
            "skill_id": skill_id,
            "cmd": cmd,
            "args": args,
            "cwd": cwd,
            "source": "rust"
        });
        append_jsonl(&path, &record);
    }
}

/// Audit: command_invoked — alias for execution_started (backward compat)
pub fn audit_command_invoked(skill_id: &str, cmd: &str, args: &[&str], cwd: &str) {
    audit_execution_started(skill_id, cmd, args, cwd);
}

/// Audit: execution_completed (Rust-side)
pub fn audit_execution_completed(
    skill_id: &str,
    exit_code: i32,
    duration_ms: u64,
    stdout_len: usize,
) {
    if let Some(path) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "execution_completed",
            "skill_id": skill_id,
            "exit_code": exit_code,
            "duration_ms": duration_ms,
            "stdout_len": stdout_len,
            "success": exit_code == 0,
            "source": "rust"
        });
        append_jsonl(&path, &record);
    }
}

/// Audit: skill_invocation (P0 可观测 - 记录谁在什么上下文调用了哪个 Skill、输入摘要、输出摘要)
pub fn audit_skill_invocation(
    skill_id: &str,
    entry_point: &str,
    cwd: &str,
    input_json: &str,
    output: &str,
    exit_code: i32,
    duration_ms: u64,
) {
    if let Some(path) = get_audit_path() {
        let context = crate::config::loader::env_optional(
            crate::config::env_keys::observability::SKILLLITE_AUDIT_CONTEXT,
            &[],
        )
        .unwrap_or_else(|| "cli".to_string());
        let input_summary = input_summary_bytes(input_json);
        let output_summary = output_summary_bytes(output);
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "skill_invocation",
            "skill_id": skill_id,
            "entry_point": entry_point,
            "cwd": cwd,
            "context": context,
            "input_summary": input_summary,
            "output_summary": output_summary,
            "exit_code": exit_code,
            "duration_ms": duration_ms,
            "success": exit_code == 0,
            "source": "rust"
        });
        append_jsonl(&path, &record);
    }
}

fn input_summary_bytes(input: &str) -> serde_json::Value {
    let preview: String = input.chars().take(100).collect();
    let truncated = input.chars().count() > 100;
    serde_json::json!({"len": input.len(), "preview": if truncated { format!("{}...", preview) } else { preview } })
}

fn output_summary_bytes(output: &str) -> serde_json::Value {
    let preview: String = output.chars().take(100).collect();
    let truncated = output.chars().count() > 100;
    serde_json::json!({"len": output.len(), "preview": if truncated { format!("{}...", preview) } else { preview } })
}

/// Security event: network blocked
pub fn security_blocked_network(skill_id: &str, blocked_target: &str, reason: &str) {
    tracing::warn!(
        skill_id = %skill_id,
        blocked_target = %blocked_target,
        reason = %reason,
        "Security: blocked network request"
    );
    if let Some(path) = get_security_events_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "type": "security_blocked",
            "category": "network",
            "skill_id": skill_id,
            "details": {
                "blocked_target": blocked_target,
                "reason": reason
            }
        });
        append_jsonl(&path, &record);
    }
}

/// Security event: scan found high/critical
pub fn security_scan_high(skill_id: &str, severity: &str, issues: &serde_json::Value) {
    if let Some(path) = get_security_events_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "type": "security_scan_high",
            "category": "code_scan",
            "skill_id": skill_id,
            "details": {
                "severity": severity,
                "issues": issues
            }
        });
        append_jsonl(&path, &record);
    }
}

/// Security event: scan approved — user approved after high/critical scan
pub fn security_scan_approved(skill_id: &str, scan_id: &str, issues_count: usize) {
    tracing::info!(
        skill_id = %skill_id,
        scan_id = %scan_id,
        issues_count = %issues_count,
        "Security: scan approved by user"
    );
    if let Some(path) = get_security_events_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "type": "security_scan_approved",
            "category": "code_scan",
            "skill_id": skill_id,
            "details": {
                "scan_id": scan_id,
                "issues_count": issues_count,
                "decision": "approved"
            }
        });
        append_jsonl(&path, &record);
    }
}

/// Security event: scan rejected — user rejected after high/critical scan
pub fn security_scan_rejected(skill_id: &str, scan_id: &str, issues_count: usize) {
    tracing::info!(
        skill_id = %skill_id,
        scan_id = %scan_id,
        issues_count = %issues_count,
        "Security: scan rejected by user"
    );
    if let Some(path) = get_security_events_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "type": "security_scan_rejected",
            "category": "code_scan",
            "skill_id": skill_id,
            "details": {
                "scan_id": scan_id,
                "issues_count": issues_count,
                "decision": "rejected"
            }
        });
        append_jsonl(&path, &record);
    }
}

// ─── Edit audit events (agent layer) ────────────────────────────────────────
//
// 结构约定:
// - path 提升到顶层,便于查询
// - edit_id 每条唯一,用于去重与关联
// - workspace/context 可选,用于多项目过滤

fn edit_audit_context() -> serde_json::Value {
    crate::config::loader::env_optional(
        crate::config::env_keys::observability::SKILLLITE_AUDIT_CONTEXT,
        &[],
    )
    .map(serde_json::Value::String)
    .unwrap_or(serde_json::Value::Null)
}

/// Audit: edit_applied — agent wrote a file change via search_replace
pub fn audit_edit_applied(
    path: &str,
    occurrences: usize,
    first_changed_line: usize,
    diff_excerpt: &str,
    workspace: Option<&str>,
) {
    if let Some(audit) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "edit_applied",
            "category": "edit",
            "source_layer": "agent",
            "edit_id": Uuid::new_v4().to_string(),
            "path": path,
            "workspace": workspace.unwrap_or(""),
            "context": edit_audit_context(),
            "details": {
                "occurrences": occurrences,
                "first_changed_line": first_changed_line,
                "diff_excerpt": diff_excerpt
            }
        });
        append_jsonl(&audit, &record);
    }
}

/// Audit: edit_previewed — agent computed a dry-run diff via preview_edit
pub fn audit_edit_previewed(
    path: &str,
    occurrences: usize,
    first_changed_line: usize,
    diff_excerpt: &str,
    workspace: Option<&str>,
) {
    if let Some(audit) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "edit_previewed",
            "category": "edit",
            "source_layer": "agent",
            "edit_id": Uuid::new_v4().to_string(),
            "path": path,
            "workspace": workspace.unwrap_or(""),
            "context": edit_audit_context(),
            "details": {
                "occurrences": occurrences,
                "first_changed_line": first_changed_line,
                "diff_excerpt": diff_excerpt
            }
        });
        append_jsonl(&audit, &record);
    }
}

/// Audit: edit_inserted — agent inserted lines via insert_lines
pub fn audit_edit_inserted(
    path: &str,
    line_num: usize,
    lines_inserted: usize,
    diff_excerpt: &str,
    workspace: Option<&str>,
) {
    if let Some(audit) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "edit_inserted",
            "category": "edit",
            "source_layer": "agent",
            "edit_id": Uuid::new_v4().to_string(),
            "path": path,
            "workspace": workspace.unwrap_or(""),
            "context": edit_audit_context(),
            "details": {
                "insert_after_line": line_num,
                "lines_inserted": lines_inserted,
                "diff_excerpt": diff_excerpt
            }
        });
        append_jsonl(&audit, &record);
    }
}

/// Audit: edit_failed — agent attempted an edit that failed (not found, non-unique, etc.)
pub fn audit_edit_failed(path: &str, tool_name: &str, reason: &str, workspace: Option<&str>) {
    if let Some(audit) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "edit_failed",
            "category": "edit",
            "source_layer": "agent",
            "edit_id": Uuid::new_v4().to_string(),
            "path": path,
            "reason": reason,
            "tool": tool_name,
            "workspace": workspace.unwrap_or(""),
            "context": edit_audit_context(),
            "details": {
                "path": path,
                "tool": tool_name,
                "reason": reason
            }
        });
        append_jsonl(&audit, &record);
    }
}

// ─── Security events ────────────────────────────────────────────────────────

// ─── Evolution audit events (EVO-5) ─────────────────────────────────────────

/// Audit: evolution event — logged when evolution produces changes or rolls back.
pub fn audit_evolution_event(event_type: &str, target_id: &str, reason: &str, txn_id: &str) {
    if let Some(path) = get_audit_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "event": "evolution",
            "category": "evolution",
            "source_layer": "agent",
            "details": {
                "type": event_type,
                "target_id": target_id,
                "reason": reason,
                "txn_id": txn_id
            }
        });
        append_jsonl(&path, &record);
    }
}

/// Security event: sandbox fallback (e.g. Seatbelt failed, using simple execution)
pub fn security_sandbox_fallback(skill_id: &str, reason: &str) {
    tracing::warn!(
        skill_id = %skill_id,
        reason = %reason,
        "Security: sandbox fallback to simple execution"
    );
    if let Some(path) = get_security_events_path() {
        let record = json!({
            "ts": Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
            "type": "sandbox_fallback",
            "category": "runtime",
            "skill_id": skill_id,
            "details": { "reason": reason }
        });
        append_jsonl(&path, &record);
    }
}