tldr-cli 0.1.2

CLI binary for TLDR code analysis tool
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
# TLDR Daemon & Cache Commands - Behavioral Specification

**Version:** 1.0  
**Date:** 2026-02-03  
**Author:** architect-agent  
**Source Analysis:** Python implementation in `tldr/daemon/`, gap analysis in `thoughts/shared/gap-analysis/tldr-daemon-cache-gap.yaml`

## Overview

The daemon subsystem provides a persistent background process that holds indexes in memory for fast queries, implements Salsa-style query memoization, and tracks usage statistics. This specification covers 9 commands: 5 daemon lifecycle commands and 4 infrastructure commands.

### Architecture Summary

```
┌─────────────────────────────────────────────────────────────────────┐
│                           CLI Layer                                  │
│  daemon start | stop | status | query | notify | stats | warm | ... │
└───────────────────────────────┬─────────────────────────────────────┘
                    ┌───────────▼───────────┐
                    │    IPC Transport      │
                    │  Unix Socket (Unix)   │
                    │  TCP Socket (Windows) │
                    └───────────┬───────────┘
┌───────────────────────────────▼─────────────────────────────────────┐
│                        TLDRDaemon                                    │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐                  │
│  │ SalsaDB     │  │ Dedup Index │  │ Stats Store │                  │
│  │ (memoize)   │  │ (content-   │  │ (per-session│                  │
│  │             │  │  hash)      │  │  tracking)  │                  │
│  └─────────────┘  └─────────────┘  └─────────────┘                  │
│                                                                      │
│  ┌──────────────────────────────────────────────────────────────┐   │
│  │                    Command Handlers                           │   │
│  │  ping | status | shutdown | search | extract | impact | ...  │   │
│  └──────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────┘
```

## Types

### Core Types

```rust
use std::collections::{HashMap, HashSet};
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::RwLock;

/// Idle timeout before daemon auto-shutdown (30 minutes)
pub const IDLE_TIMEOUT: Duration = Duration::from_secs(30 * 60);

/// Default threshold for triggering semantic re-index
pub const DEFAULT_REINDEX_THRESHOLD: usize = 20;

/// Default flush interval for hook stats (every N invocations)
pub const HOOK_FLUSH_THRESHOLD: usize = 5;

/// Daemon configuration loaded from .tldr/config.json or .claude/settings.json
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DaemonConfig {
    /// Whether semantic search is enabled
    pub semantic_enabled: bool,
    
    /// Number of dirty files before auto re-index
    pub auto_reindex_threshold: usize,
    
    /// Embedding model for semantic search
    pub semantic_model: String,
    
    /// Idle timeout in seconds (default: 1800 = 30 min)
    pub idle_timeout_secs: u64,
}

impl Default for DaemonConfig {
    fn default() -> Self {
        Self {
            semantic_enabled: true,
            auto_reindex_threshold: DEFAULT_REINDEX_THRESHOLD,
            semantic_model: "bge-large-en-v1.5".to_string(),
            idle_timeout_secs: IDLE_TIMEOUT.as_secs(),
        }
    }
}

/// Daemon runtime status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DaemonStatus {
    /// Daemon is starting up, acquiring locks
    Initializing,
    /// Daemon is building initial indexes
    Indexing,
    /// Daemon is ready to accept queries
    Ready,
    /// Daemon is shutting down
    ShuttingDown,
    /// Daemon has stopped
    Stopped,
}

/// Statistics for Salsa-style query cache
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SalsaCacheStats {
    /// Number of cache hits (query result reused)
    pub hits: u64,
    
    /// Number of cache misses (query recomputed)
    pub misses: u64,
    
    /// Number of invalidations (file changed)
    pub invalidations: u64,
    
    /// Number of recomputations triggered by invalidation
    pub recomputations: u64,
}

impl SalsaCacheStats {
    /// Calculate hit rate as percentage (0-100)
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            return 0.0;
        }
        (self.hits as f64 / total as f64) * 100.0
    }
}

/// Statistics for content-hash deduplication
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DedupStats {
    /// Number of unique content hashes
    pub unique_hashes: usize,
    
    /// Number of duplicate content blocks avoided
    pub duplicates_avoided: usize,
    
    /// Bytes saved through deduplication
    pub bytes_saved: u64,
}

/// Per-session statistics for token tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStats {
    /// Session identifier (8-char truncated UUID)
    pub session_id: String,
    
    /// Raw tokens (what vanilla Claude would use)
    pub raw_tokens: u64,
    
    /// TLDR tokens (what was actually returned)
    pub tldr_tokens: u64,
    
    /// Number of requests in this session
    pub requests: u64,
    
    /// When session started
    pub started_at: chrono::DateTime<chrono::Utc>,
}

impl SessionStats {
    pub fn new(session_id: String) -> Self {
        Self {
            session_id,
            raw_tokens: 0,
            tldr_tokens: 0,
            requests: 0,
            started_at: chrono::Utc::now(),
        }
    }
    
    /// Record a request's token usage
    pub fn record_request(&mut self, raw_tokens: u64, tldr_tokens: u64) {
        self.raw_tokens += raw_tokens;
        self.tldr_tokens += tldr_tokens;
        self.requests += 1;
    }
    
    /// Tokens saved
    pub fn savings_tokens(&self) -> i64 {
        self.raw_tokens as i64 - self.tldr_tokens as i64
    }
    
    /// Savings as percentage (0-100)
    pub fn savings_percent(&self) -> f64 {
        if self.raw_tokens == 0 {
            return 0.0;
        }
        (self.savings_tokens() as f64 / self.raw_tokens as f64) * 100.0
    }
}

/// Per-hook activity statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookStats {
    /// Hook name
    pub hook_name: String,
    
    /// Total invocations
    pub invocations: u64,
    
    /// Successful invocations
    pub successes: u64,
    
    /// Failed invocations
    pub failures: u64,
    
    /// Hook-specific metrics (e.g., errors_found, queries_routed)
    pub metrics: HashMap<String, f64>,
    
    /// When tracking started
    pub started_at: chrono::DateTime<chrono::Utc>,
}

impl HookStats {
    pub fn new(hook_name: String) -> Self {
        Self {
            hook_name,
            invocations: 0,
            successes: 0,
            failures: 0,
            metrics: HashMap::new(),
            started_at: chrono::Utc::now(),
        }
    }
    
    /// Record a hook invocation
    pub fn record_invocation(&mut self, success: bool, metrics: Option<HashMap<String, f64>>) {
        self.invocations += 1;
        if success {
            self.successes += 1;
        } else {
            self.failures += 1;
        }
        if let Some(m) = metrics {
            for (key, value) in m {
                *self.metrics.entry(key).or_insert(0.0) += value;
            }
        }
    }
    
    /// Success rate as percentage (0-100)
    pub fn success_rate(&self) -> f64 {
        if self.invocations == 0 {
            return 100.0;
        }
        (self.successes as f64 / self.invocations as f64) * 100.0
    }
}

/// Aggregated global stats (from JSONL store)
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GlobalStats {
    /// Total number of invocations across all sessions
    pub total_invocations: u64,
    
    /// Estimated tokens saved across all sessions
    pub estimated_tokens_saved: i64,
    
    /// Total raw tokens processed
    pub raw_tokens_total: u64,
    
    /// Total TLDR tokens returned
    pub tldr_tokens_total: u64,
    
    /// Savings percentage (0-100)
    pub savings_percent: f64,
}

/// Cache file info for cache stats
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheFileInfo {
    /// Number of cache files
    pub file_count: usize,
    
    /// Total size in bytes
    pub total_bytes: u64,
    
    /// Size formatted as human-readable
    pub total_size_human: String,
}
```

### Error Types

```rust
/// Daemon-specific errors
#[derive(Debug, Error)]
pub enum DaemonError {
    #[error("daemon already running (PID: {pid})")]
    AlreadyRunning { pid: u32 },
    
    #[error("daemon not running")]
    NotRunning,
    
    #[error("failed to acquire PID file lock: {0}")]
    LockFailed(io::Error),
    
    #[error("failed to bind socket: {0}")]
    SocketBindFailed(io::Error),
    
    #[error("address already in use: {addr}")]
    AddressInUse { addr: String },
    
    #[error("connection refused")]
    ConnectionRefused,
    
    #[error("connection timeout after {timeout_secs}s")]
    ConnectionTimeout { timeout_secs: u64 },
    
    #[error("invalid IPC message: {0}")]
    InvalidMessage(String),
    
    #[error("unknown command: {cmd}")]
    UnknownCommand { cmd: String },
    
    #[error("missing required parameter: {param}")]
    MissingParameter { param: String },
    
    #[error("permission denied: {path}")]
    PermissionDenied { path: PathBuf },
    
    #[error("stale PID file (process {pid} not running)")]
    StalePidFile { pid: u32 },
    
    #[error("IO error: {0}")]
    Io(#[from] io::Error),
    
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

/// Result type for daemon operations
pub type DaemonResult<T> = Result<T, DaemonError>;
```

### IPC Message Types

```rust
/// Command sent to daemon via socket
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "cmd", rename_all = "snake_case")]
pub enum DaemonCommand {
    /// Health check
    Ping,
    
    /// Get daemon status
    Status {
        /// Optional session ID to get session-specific stats
        #[serde(skip_serializing_if = "Option::is_none")]
        session: Option<String>,
    },
    
    /// Graceful shutdown
    Shutdown,
    
    /// File change notification
    Notify {
        file: PathBuf,
    },
    
    /// Track hook activity
    Track {
        hook: String,
        #[serde(default = "default_true")]
        success: bool,
        #[serde(default)]
        metrics: HashMap<String, f64>,
    },
    
    /// Warm call graph cache
    Warm {
        #[serde(default)]
        language: Option<String>,
    },
    
    /// Semantic search (if model loaded)
    Semantic {
        query: String,
        #[serde(default = "default_top_k")]
        top_k: usize,
    },
    
    // Pass-through analysis commands
    Search { pattern: String, max_results: Option<usize> },
    Extract { file: PathBuf, session: Option<String> },
    Tree { path: Option<PathBuf> },
    Structure { path: PathBuf, lang: Option<String> },
    Context { entry: String, depth: Option<usize> },
    Cfg { file: PathBuf, function: String },
    Dfg { file: PathBuf, function: String },
    Slice { file: PathBuf, function: String, line: usize },
    Calls { path: Option<PathBuf> },
    Impact { func: String, depth: Option<usize> },
    Dead { path: Option<PathBuf>, entry: Option<Vec<String>> },
    Arch { path: Option<PathBuf> },
    Imports { file: PathBuf },
    Importers { module: String, path: Option<PathBuf> },
    Diagnostics { path: PathBuf, project: Option<bool> },
    ChangeImpact { files: Option<Vec<PathBuf>>, session: Option<bool>, git: Option<bool> },
}

fn default_true() -> bool { true }
fn default_top_k() -> usize { 10 }

/// Response from daemon
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DaemonResponse {
    /// Simple status response
    Status {
        status: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        message: Option<String>,
    },
    
    /// Full status response
    FullStatus {
        status: DaemonStatus,
        uptime: f64,
        files: usize,
        project: PathBuf,
        salsa_stats: SalsaCacheStats,
        #[serde(skip_serializing_if = "Option::is_none")]
        dedup_stats: Option<DedupStats>,
        #[serde(skip_serializing_if = "Option::is_none")]
        session_stats: Option<SessionStats>,
        all_sessions: AllSessionsSummary,
        hook_stats: HashMap<String, HookStats>,
    },
    
    /// Notify response
    NotifyResponse {
        status: String,
        dirty_count: usize,
        threshold: usize,
        reindex_triggered: bool,
    },
    
    /// Track response
    TrackResponse {
        status: String,
        hook: String,
        total_invocations: u64,
        flushed: bool,
    },
    
    /// Generic JSON result (for analysis commands)
    Result {
        status: String,
        #[serde(flatten)]
        data: serde_json::Value,
    },
    
    /// Error response
    Error {
        status: String,
        message: String,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllSessionsSummary {
    pub active_sessions: usize,
    pub total_raw_tokens: u64,
    pub total_tldr_tokens: u64,
    pub total_requests: u64,
}
```

## Commands

### daemon start

**CLI:** `tldr daemon start [--project PATH] [--foreground]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--project` | `-p` | Path | `.` | Project root directory |
| `--foreground` | `-f` | bool | false | Run in foreground (don't daemonize) |

**Behavior:**

1. Resolve project path to absolute path
2. Compute deterministic paths:
   - PID file: `/tmp/tldr-{hash}.pid` where hash = MD5(project_path)[:8]
   - Socket: `/tmp/tldr-{hash}.sock` (Unix) or TCP port 49152+hash%10000 (Windows)
3. Try to acquire exclusive lock on PID file:
   - **Unix:** `fcntl::flock(LOCK_EX | LOCK_NB)`
   - **Windows:** `LockFileEx(LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY)`
4. If lock fails → print "Daemon already running", exit
5. Ensure `.tldrignore` exists (create with defaults if missing)
6. If `--foreground`:
   - Write PID to locked file
   - Run daemon main loop directly
7. Else (background mode):
   - **Unix:** Fork with `os::fork()`, child calls `setsid()`, runs daemon
   - **Windows:** Spawn detached subprocess via `Command::new().creation_flags(DETACHED_PROCESS)`
8. Wait up to 10s for socket to become connectable
9. Print status: "Daemon started with PID {pid}\nSocket: {socket_path}"

**Success Output:**
```json
{
  "status": "ok",
  "pid": 12345,
  "socket": "/tmp/tldr-a1b2c3d4.sock"
}
```

**Errors:**
| Error | Message | Exit Code |
|-------|---------|-----------|
| `AlreadyRunning` | "Daemon already running" | 1 |
| `LockFailed` | "Failed to acquire PID file lock: {reason}" | 1 |
| `SocketBindFailed` | "Failed to bind socket: {reason}" | 1 |
| `PermissionDenied` | "Permission denied: {path}" | 1 |

**Invariants:**
- Only one daemon per project root (enforced by PID file lock)
- Lock held for daemon's entire lifetime
- Socket file cleaned up on graceful shutdown

---

### daemon stop

**CLI:** `tldr daemon stop [--project PATH]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--project` | `-p` | Path | `.` | Project root directory |

**Behavior:**

1. Resolve project path
2. Compute socket path from project hash
3. Connect to daemon socket (timeout: 5s)
4. Send `{"cmd": "shutdown"}` message
5. Daemon responds with `{"status": "shutting_down"}`
6. Daemon persists all stats before exiting
7. Daemon releases PID file lock, removes socket file
8. Print "Daemon stopped"

**Success Output:**
```json
{
  "status": "ok",
  "message": "Daemon stopped"
}
```

**Errors:**
| Error | Message | Exit Code |
|-------|---------|-----------|
| `NotRunning` | "Daemon not running" | 0 (not an error) |
| `ConnectionTimeout` | "Connection timeout" | 1 |

**Text Output:**
```
Daemon stopped
```
or
```
Daemon not running
```

---

### daemon status

**CLI:** `tldr daemon status [--project PATH] [--session SESSION_ID]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--project` | `-p` | Path | `.` | Project root directory |
| `--session` | `-s` | String | None | Session ID for session-specific stats |

**Behavior:**

1. Resolve project path
2. Connect to daemon socket
3. Send `{"cmd": "status", "session": "<session_id>"}` if session provided
4. Receive and display:
   - `status`: "running" | "initializing" | "indexing"
   - `uptime`: seconds (formatted as Xh Xm Xs)
   - `files`: indexed file count
   - `salsa_stats`: cache hit/miss/invalidation counts
   - `dedup_stats`: deduplication metrics (if loaded)
   - `session_stats`: per-session token stats (if session_id provided)
   - `all_sessions`: summary across all sessions
   - `hook_stats`: per-hook activity metrics

**Success Output (JSON):**
```json
{
  "status": "ready",
  "uptime": 3600.5,
  "uptime_human": "1h 0m 0s",
  "files": 150,
  "project": "/path/to/project",
  "salsa_stats": {
    "hits": 1234,
    "misses": 56,
    "hit_rate": 95.67,
    "invalidations": 10,
    "recomputations": 8
  },
  "dedup_stats": {
    "unique_hashes": 500,
    "duplicates_avoided": 120,
    "bytes_saved": 1048576
  },
  "all_sessions": {
    "active_sessions": 3,
    "total_raw_tokens": 500000,
    "total_tldr_tokens": 50000,
    "total_requests": 200
  }
}
```

**Text Output:**
```
TLDR Daemon Status
==================
Status:  running
Uptime:  1h 0m 0s
Project: /path/to/project
Files:   150

Cache Statistics
----------------
Hits:          1,234
Misses:        56
Hit Rate:      95.67%
Invalidations: 10

Session Summary
---------------
Active Sessions:    3
Total Requests:     200
Tokens Saved:       450,000 (90.0%)
```

**Errors:**
| Error | Message | Exit Code |
|-------|---------|-----------|
| `NotRunning` | "Daemon not running" | 0 |

---

### daemon query

**CLI:** `tldr daemon query CMD [--project PATH] [--json PARAMS]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `cmd` | - | String | required | Command name to send |
| `--project` | `-p` | Path | `.` | Project root directory |
| `--json` | `-j` | String | None | Additional JSON parameters |

**Behavior:**

1. Resolve project path
2. Connect to daemon socket
3. Build command: `{"cmd": "<cmd>", ...<parsed_json_params>}`
4. Send command, receive raw JSON response
5. Print response (pass-through)

**Supported Commands:**
- `ping` - health check
- `status` - daemon status
- `shutdown` - graceful shutdown
- `notify` - file change notification (requires `file` param)
- `track` - hook activity (requires `hook` param)
- `warm` - warm cache (optional `language` param)
- `semantic` - semantic search (requires `query` param)
- Plus all analysis commands: `search`, `extract`, `tree`, `structure`, `context`, `cfg`, `dfg`, `slice`, `calls`, `impact`, `dead`, `arch`, `imports`, `importers`, `diagnostics`, `change_impact`

**Example:**
```bash
tldr daemon query search --json '{"pattern": "fn main"}'
```

**Success Output:** Raw JSON response from daemon

**Errors:**
| Error | Message | Exit Code |
|-------|---------|-----------|
| `NotRunning` | "Error: Daemon not running" | 1 |
| `InvalidMessage` | "Invalid JSON parameters: {reason}" | 1 |

---

### daemon notify

**CLI:** `tldr daemon notify FILE [--project PATH]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `file` | - | Path | required | Path to changed file |
| `--project` | `-p` | Path | `.` | Project root directory |

**Behavior:**

1. Resolve project and file paths to absolute
2. Connect to daemon socket (fail silently if daemon not running)
3. Send `{"cmd": "notify", "file": "<absolute_path>"}`
4. Daemon:
   - Adds file to dirty set (if not already tracked)
   - Increments dirty count
   - Invalidates Salsa cache entries for this file
   - Updates dedup index if loaded
5. If `dirty_count >= threshold` (default 20):
   - Triggers background semantic re-index
   - Clears dirty set after spawn
6. Return response with dirty count and threshold

**Success Output:**
```json
{
  "status": "ok",
  "dirty_count": 5,
  "threshold": 20,
  "reindex_triggered": false
}
```

**Text Output:**
```
Tracked: 5/20 files
```
or
```
Reindex triggered (20/20 files)
```

**Errors:**
- On `ConnectionRefused` or socket not found: silently return (exit 0)
- File edits should never fail due to daemon status

**Use Case:** Editor hooks call this on file save to keep daemon cache fresh.

---

### stats

**CLI:** `tldr stats [--format json|text]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--format` | `-f` | String | `json` | Output format |

**Behavior:**

1. Load stats from `~/.tldr/stats.jsonl`
2. Aggregate totals across all entries:
   - `total_invocations`: sum of `requests`
   - `raw_tokens_total`: sum of `raw_tokens`
   - `tldr_tokens_total`: sum of `tldr_tokens`
3. Calculate:
   - `estimated_tokens_saved = raw_tokens_total - tldr_tokens_total`
   - `savings_percent = (estimated_tokens_saved / raw_tokens_total) * 100`
4. Return aggregated stats

**Success Output (JSON):**
```json
{
  "total_invocations": 1500,
  "estimated_tokens_saved": 4500000,
  "raw_tokens_total": 5000000,
  "tldr_tokens_total": 500000,
  "savings_percent": 90.0
}
```

**Text Output:**
```
TLDR Usage Statistics
=====================
Total Invocations:     1,500
Tokens Saved:          4,500,000 (90.0%)
Raw Tokens Processed:  5,000,000
TLDR Tokens Returned:  500,000
```

**Empty State:**
```json
{
  "message": "No usage recorded yet"
}
```

---

### warm

**CLI:** `tldr warm PATH [--background] [--lang LANG]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `path` | - | Path | required | Project root directory |
| `--background` | `-b` | bool | false | Run in background process |
| `--lang` | `-l` | String | `all` | Language to analyze |

**Behavior:**

1. If `--background`:
   - Spawn detached subprocess: `tldr warm <path> --lang <lang>`
   - Print "Warming cache in background..."
   - Return immediately
2. Foreground mode:
   - Ensure `.tldrignore` exists
   - If `lang == "all"`: auto-detect languages in project
   - For each language:
     - Scan project for files
     - Build call graph edges
   - Create `.tldr/cache/` directory
   - Write `call_graph.json`:
     ```json
     {
       "edges": [{"from_file": "...", "from_func": "...", "to_file": "...", "to_func": "..."}, ...],
       "languages": ["python", "typescript"],
       "timestamp": 1706918400
     }
     ```
   - Print summary: "Indexed {N} files, found {M} edges"

**Success Output:**
```json
{
  "status": "ok",
  "files": 150,
  "edges": 2500,
  "languages": ["python", "typescript"],
  "cache_path": ".tldr/cache/call_graph.json"
}
```

**Text Output:**
```
Warming call graph cache...
Indexed 150 files, found 2,500 edges
Languages: python, typescript
Cache written to: .tldr/cache/call_graph.json
```

---

### cache stats

**CLI:** `tldr cache stats [--project PATH]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--project` | `-p` | Path | `.` | Project root directory |

**Behavior:**

1. Resolve project path
2. Look for `.tldr/cache/salsa_stats.json`
3. If exists, read and display:
   - `cache_hits`
   - `cache_misses`
   - `hit_rate`
   - `invalidations`
   - `recomputations`
4. Scan `.tldr/cache/*.pkl` files:
   - Count files
   - Sum sizes
5. Return combined stats

**Success Output:**
```json
{
  "salsa_stats": {
    "hits": 1234,
    "misses": 56,
    "hit_rate": 95.67,
    "invalidations": 10,
    "recomputations": 8
  },
  "cache_files": {
    "file_count": 25,
    "total_bytes": 1048576,
    "total_size_human": "1.0 MB"
  }
}
```

**Text Output:**
```
Cache Statistics
================
Salsa Cache:
  Hits:          1,234
  Misses:        56
  Hit Rate:      95.67%
  Invalidations: 10
  Recomputations: 8

Cache Files:
  Count: 25 files
  Size:  1.0 MB
```

**Empty State:**
```
No cache statistics found
```

---

### cache clear

**CLI:** `tldr cache clear [--project PATH]`

**Arguments:**
| Arg | Short | Type | Default | Description |
|-----|-------|------|---------|-------------|
| `--project` | `-p` | Path | `.` | Project root directory |

**Behavior:**

1. Resolve project path
2. Locate `.tldr/cache/` directory
3. If exists:
   - Delete `salsa_stats.json` if exists
   - Delete all `*.pkl` files
   - Delete `call_graph.json` if exists
   - Count deleted files
4. Print result

**Success Output:**
```json
{
  "status": "ok",
  "files_removed": 26,
  "message": "Cache cleared: 26 file(s) removed"
}
```

**Text Output:**
```
Cache cleared: 26 file(s) removed
```

**Empty State:**
```
No cache directory found
```

---

## IPC Protocol

### Socket Path Computation

```rust
fn compute_socket_path(project: &Path) -> PathBuf {
    use md5::{Md5, Digest};
    
    let project_str = project.canonicalize()
        .unwrap_or_else(|_| project.to_path_buf())
        .to_string_lossy()
        .to_string();
    
    let mut hasher = Md5::new();
    hasher.update(project_str.as_bytes());
    let hash = hasher.finalize();
    let hash_str = format!("{:x}", hash)[..8].to_string();
    
    let tmp_dir = std::env::temp_dir();
    tmp_dir.join(format!("tldr-{}.sock", hash_str))
}

fn compute_pid_path(project: &Path) -> PathBuf {
    // Same hash computation
    let hash_str = /* ... */;
    let tmp_dir = std::env::temp_dir();
    tmp_dir.join(format!("tldr-{}.pid", hash_str))
}

fn compute_tcp_port(project: &Path) -> u16 {
    // For Windows
    let hash_str = /* ... */;
    let hash_int = u64::from_str_radix(&hash_str, 16).unwrap_or(0);
    49152 + (hash_int % 10000) as u16
}
```

### Message Format

- **Transport:** Newline-delimited JSON over Unix socket (Unix) or TCP (Windows)
- **Request:** `{"cmd": "...", ...params}\n`
- **Response:** `{...}\n`
- **Encoding:** UTF-8
- **Max message size:** 64KB (client recv buffer)

### Connection Handling

```rust
// Client side
async fn send_command(project: &Path, cmd: DaemonCommand) -> DaemonResult<DaemonResponse> {
    let socket_path = compute_socket_path(project);
    
    #[cfg(unix)]
    let stream = tokio::net::UnixStream::connect(&socket_path).await?;
    
    #[cfg(windows)]
    let stream = tokio::net::TcpStream::connect(("127.0.0.1", compute_tcp_port(project))).await?;
    
    let (reader, mut writer) = stream.into_split();
    
    // Send command
    let msg = serde_json::to_string(&cmd)?;
    writer.write_all(msg.as_bytes()).await?;
    writer.write_all(b"\n").await?;
    
    // Read response
    let mut reader = tokio::io::BufReader::new(reader);
    let mut line = String::new();
    reader.read_line(&mut line).await?;
    
    let response: DaemonResponse = serde_json::from_str(&line)?;
    Ok(response)
}
```

---

## Cache Behavior

### Salsa-Style Memoization

The daemon implements Salsa-style incremental computation:

1. **Query Functions:** Each analysis (search, extract, etc.) is wrapped in a memoizing query function
2. **Input Tracking:** Queries declare which files they depend on
3. **Revision Tracking:** Each file has a revision number (incremented on change)
4. **Automatic Invalidation:** When a file's revision changes, dependent queries are invalidated
5. **Lazy Recomputation:** Invalidated queries are recomputed on next access

```rust
/// Salsa-style database for query memoization
pub struct SalsaDB {
    /// Cache of query results: (query_key, args_hash) -> (result, revision)
    cache: DashMap<(String, u64), (serde_json::Value, u64)>,
    
    /// File revisions: file_path -> revision
    file_revisions: DashMap<PathBuf, u64>,
    
    /// Reverse dependencies: file_path -> Set<(query_key, args_hash)>
    dependents: DashMap<PathBuf, HashSet<(String, u64)>>,
    
    /// Statistics
    stats: RwLock<SalsaCacheStats>,
}

impl SalsaDB {
    /// Execute a query with memoization
    pub fn query<F, R>(&self, key: &str, args: impl Hash, deps: &[PathBuf], f: F) -> R
    where
        F: FnOnce() -> R,
        R: Serialize + DeserializeOwned,
    {
        let args_hash = hash(args);
        let cache_key = (key.to_string(), args_hash);
        
        // Check cache
        if let Some(entry) = self.cache.get(&cache_key) {
            let (result, cached_rev) = entry.value();
            if self.is_valid(deps, *cached_rev) {
                self.stats.write().unwrap().hits += 1;
                return serde_json::from_value(result.clone()).unwrap();
            }
        }
        
        // Cache miss - compute
        self.stats.write().unwrap().misses += 1;
        let result = f();
        let current_rev = self.current_revision(deps);
        
        // Store in cache
        self.cache.insert(cache_key.clone(), (serde_json::to_value(&result).unwrap(), current_rev));
        
        // Track dependencies
        for dep in deps {
            self.dependents.entry(dep.clone()).or_default().insert(cache_key.clone());
        }
        
        result
    }
    
    /// Notify that a file has changed
    pub fn set_file(&self, path: &Path, _content: &str) {
        // Increment revision
        let new_rev = self.file_revisions.entry(path.to_path_buf())
            .and_modify(|r| *r += 1)
            .or_insert(1);
        
        // Invalidate dependents
        if let Some(deps) = self.dependents.get(path) {
            let mut stats = self.stats.write().unwrap();
            for cache_key in deps.value() {
                if self.cache.remove(cache_key).is_some() {
                    stats.invalidations += 1;
                }
            }
        }
    }
}
```

### Cache Persistence

- **Salsa stats:** `.tldr/cache/salsa_stats.json`
- **Call graph cache:** `.tldr/cache/call_graph.json`
- **Pickle files:** `.tldr/cache/*.pkl` (optional, for large results)

### Invalidation Rules

| Trigger | Action |
|---------|--------|
| File modified (notify command) | Increment file revision, invalidate dependent queries |
| File deleted | Remove from revision map, invalidate dependents |
| Cache clear command | Remove all cache files and reset stats |
| Daemon restart | In-memory cache cleared, stats persist |

---

## PID File Locking

### Lock Acquisition

```rust
#[cfg(unix)]
fn try_acquire_lock(pid_path: &Path) -> DaemonResult<std::fs::File> {
    use std::os::unix::io::AsRawFd;
    
    let file = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(pid_path)?;
    
    // Non-blocking exclusive lock
    let ret = unsafe {
        libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB)
    };
    
    if ret != 0 {
        return Err(DaemonError::AlreadyRunning { pid: read_pid(&file)? });
    }
    
    Ok(file)
}

#[cfg(windows)]
fn try_acquire_lock(pid_path: &Path) -> DaemonResult<std::fs::File> {
    use std::os::windows::io::AsRawHandle;
    use windows_sys::Win32::Storage::FileSystem::*;
    
    let file = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(pid_path)?;
    
    let mut overlapped = unsafe { std::mem::zeroed::<OVERLAPPED>() };
    let ret = unsafe {
        LockFileEx(
            file.as_raw_handle() as _,
            LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
            0,
            1,
            0,
            &mut overlapped,
        )
    };
    
    if ret == 0 {
        return Err(DaemonError::AlreadyRunning { pid: read_pid(&file)? });
    }
    
    Ok(file)
}
```

### Stale PID Detection

```rust
fn is_process_running(pid: u32) -> bool {
    #[cfg(unix)]
    {
        // Signal 0 checks if process exists without sending signal
        unsafe { libc::kill(pid as i32, 0) == 0 }
    }
    
    #[cfg(windows)]
    {
        use windows_sys::Win32::System::Threading::*;
        unsafe {
            let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
            if handle.is_null() {
                return false;
            }
            CloseHandle(handle);
            true
        }
    }
}

fn check_daemon_alive(project: &Path) -> DaemonResult<bool> {
    let pid_path = compute_pid_path(project);
    
    match try_acquire_lock(&pid_path) {
        Ok(file) => {
            // We got the lock - check for stale PID
            if let Ok(content) = std::fs::read_to_string(&pid_path) {
                if let Ok(pid) = content.trim().parse::<u32>() {
                    if is_process_running(pid) {
                        // Process exists but we got lock? Shouldn't happen.
                        // Release and report alive.
                        drop(file);
                        return Ok(true);
                    }
                }
            }
            // Stale PID or no PID - daemon not running
            drop(file);
            Ok(false)
        }
        Err(DaemonError::AlreadyRunning { .. }) => Ok(true),
        Err(e) => Err(e),
    }
}
```

---

## Edge Cases

### Daemon Crash Recovery

1. **Stale Socket File:**
   - On bind failure with EADDRINUSE, try to connect to existing socket
   - If connection refused → stale socket, unlink and retry bind
   - If connection succeeds → another daemon running, exit

2. **Stale PID File:**
   - Lock acquisition succeeds but PID file has content
   - Check if PID process is running
   - If not running → stale, overwrite with new PID
   - If running but we got lock → edge case, treat as not running

3. **Stats Persistence on Crash:**
   - atexit handler registered to persist stats
   - Also persist periodically (every 10 requests or 5 hook invocations)
   - Stats are append-only JSONL, so partial writes don't corrupt

### Concurrent Access

1. **Multiple Clients:**
   - Daemon uses single-threaded async loop (tokio)
   - Commands processed sequentially
   - ThreadPoolExecutor (4 workers) for CPU-intensive operations

2. **File Notifications During Query:**
   - SalsaDB uses DashMap for thread-safe access
   - Invalidation can happen between cache check and result return
   - Acceptable: query returns stale result, next query will recompute

### Cross-Platform Differences

| Feature | Unix (Linux/macOS) | Windows |
|---------|-------------------|---------|
| Socket | Unix domain socket | TCP localhost |
| Daemonize | `fork()` + `setsid()` | `DETACHED_PROCESS` |
| PID lock | `flock()` | `LockFileEx()` |
| Process check | `kill(pid, 0)` | `OpenProcess()` |
| Signal handling | SIGTERM, SIGINT | SIGINT only |

---

## Dependencies

### Crate Dependencies

```toml
[dependencies]
# Async runtime
tokio = { version = "1", features = ["full", "net", "io-util", "sync", "time", "signal"] }

# Serialization
serde = { version = "1", features = ["derive"] }
serde_json = "1"

# CLI
clap = { version = "4", features = ["derive"] }

# Error handling
thiserror = "1"
anyhow = "1"

# Hashing
md5 = "0.7"

# Concurrent data structures
dashmap = "5"

# Time
chrono = { version = "0.4", features = ["serde"] }

# Platform-specific
[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52", features = ["Win32_Storage_FileSystem", "Win32_System_Threading"] }
```

### Internal Dependencies

- `tldr-core`: Analysis functions (extract, structure, calls, etc.)
- Existing command modules for pass-through queries

---

## Testing Strategy

### Unit Tests

1. **Socket path computation:** Verify deterministic hashing
2. **PID file locking:** Test lock acquisition and release
3. **SalsaDB:** Test cache hit/miss, invalidation cascades
4. **Stats aggregation:** Test token counting and percentage calculation

### Integration Tests

1. **Daemon lifecycle:** start → status → stop
2. **Query routing:** Send each command type, verify response format
3. **Cache behavior:** Query, modify file, query again
4. **Concurrent clients:** Multiple connections simultaneously
5. **Crash recovery:** Kill daemon, verify restart works

### Platform Tests

1. **Unix socket binding:** macOS and Linux
2. **Windows TCP binding:** Windows
3. **Daemonization:** Background process spawning
4. **Signal handling:** SIGTERM graceful shutdown

---

## Implementation Phases

### Phase 1: Foundation (Types & IPC)
- [ ] Define all types in `types.rs`
- [ ] Implement socket path computation
- [ ] Implement IPC client (send_command)
- [ ] Basic daemon start (foreground only)

### Phase 2: Lifecycle Commands
- [ ] `daemon start` with full daemonization
- [ ] `daemon stop` with graceful shutdown
- [ ] `daemon status` with basic info
- [ ] PID file locking

### Phase 3: Query Infrastructure
- [ ] Command routing in daemon
- [ ] SalsaDB implementation
- [ ] Pass-through to tldr-core functions

### Phase 4: Stats & Monitoring
- [ ] SessionStats tracking
- [ ] HookStats tracking
- [ ] `stats` command
- [ ] `daemon notify`

### Phase 5: Cache Commands
- [ ] `warm` command
- [ ] `cache stats` command
- [ ] `cache clear` command
- [ ] Cache persistence

### Phase 6: Polish
- [ ] Windows support
- [ ] Cross-platform testing
- [ ] Documentation
- [ ] Performance optimization