moeix 0.12.8

Sub-millisecond code search via sparse trigram indexing.
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
# Daemon Operation Runbook

**Purpose:** Operate and troubleshoot the `ixd` background indexing daemon  
**Last Verified:** 2026-06-10  
**Time to Complete:** 10 minutes

---

## Overview

The `ixd` daemon watches a directory for file changes and incrementally updates the trigram index. It provides:

- **Continuous indexing**: Automatic index updates on file changes
- **Unix domain socket**: Real-time notifications for editors/tooling
- **Memory safety**: LLMOSafe integration with 60% RSS ceiling
- **Concurrent instance guard**: Prevents multiple daemons on same root

**Binary:** `ixd` — standalone daemon binary

---

## Quick Start

### Step 1: Build the Daemon

```bash
cd /workspace/ix
cargo build --release --features notify
# Expected: "Finished release [optimized] target(s) in 45s"
```

### Step 2: Start the Daemon

```bash
# Watch a directory
./target/release/ixd /path/to/repo

# Expected output:
# ixd: watching /path/to/repo...
# ixd: initial build complete (1234 files, 56789 trigrams)
# ixd: socket at /run/user/1000/ixd/{hash}.sock
```

### Step 3: Verify Operation

```bash
# Check daemon is running
ps aux | grep ixd
# Expected: ixd process visible

# Check socket exists
ls -la $XDG_RUNTIME_DIR/ixd/*.sock
# Expected: socket file present

# Query status
echo '{"t":"status_query"}' | socat - UNIX-CONNECT:/path/to/socket
# Expected: {"pid":12345,"status":"idle","files":1234}
```

### Step 4: Test Indexing

```bash
# Modify a file
echo "// test" >> /path/to/repo/src/main.rs

# Check daemon logs
journalctl -u ixd -f  # if using systemd
# Or check console output
# Expected: "ixd: 1 files changed, updating index..."
```

---

## Configuration

### Configuration File (`.ixd.toml`)

The daemon and CLI read `.ixd.toml` files to scope indexing behavior. The daemon discovers config files at startup:

1. **Root config**: `.ixd.toml` at the watched root (e.g., `/path/to/repo/.ixd.toml`)
2. **Subdirectory configs**: One level deep (e.g., `/path/to/repo/project-a/.ixd.toml`)

Multiple configs are merged: root-level applies globally; subdirectory configs add `watch_roots` and `exclude_patterns`. Root-level `debounce_ms` takes precedence.

#### Schema

```toml
# .ixd.toml

# Directories within the watch root to index (optional).
# When empty, the daemon indexes the entire root recursively.
watch_roots = ["src", "lib", "tests"]

# Directory names to exclude from indexing.
# Defaults shown below — add or override as needed.
exclude_patterns = [
    ".git",
    "node_modules",
    "target",
    "vendor",
    "build",
]

# Debounce interval in milliseconds for file-watch event batching (optional).
# Minimum 50 ms, maximum 10 000 ms. Omit or set to null for the default (500 ms).
debounce_ms = 500
```

#### Fields

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `watch_roots` | `[String]` | `[]` | Subdirectories to scope indexing to. Empty = entire root. |
| `exclude_patterns` | `[String]` | `.git`, `node_modules`, `target` | Directory names skipped during file walk. |
| `debounce_ms` | `u64` or `null` | `null` (500 ms) | File-watch batching window in ms. Range: 50–10 000. Clamped to range. |

#### Examples

**Scope to specific directories:**
```toml
watch_roots = ["src", "lib", "include"]
```

Only `src/`, `lib/`, and `include/` will be indexed. All other directories under the watch root are ignored.

**Add custom exclusions:**
```toml
exclude_patterns = [".git", "node_modules", "target", "vendor", "build"]
```

**Tune debounce for faster or quieter change pickup:**
```toml
# Faster: 100ms debounce — changes become searchable ~100ms after save.
debounce_ms = 100

# Slower: 2000ms debounce — fewer rebuilds during bulk operations.
debounce_ms = 2000
```

The debounce window starts when the **first** file-change event arrives and resets every time a **new** event arrives within the window. Lower values give faster searchability; higher values reduce rebuild churn during bursty writes.

**Per-project scoping with multi-root daemon:**
```bash
# /home/ubuntu/.ixd.toml — global excludes only
# /home/ubuntu/project-a/.ixd.toml — watch src/ and lib/
# /home/ubuntu/project-b/.ixd.toml — watch app/ only

ixd /home/ubuntu
```

The daemon discovers all three config files. Project A indexes only its `src/` and `lib/`. Project B indexes only its `app/`. Global excludes apply everywhere.

**Verification:**
```bash
# Start daemon with verbose output to see config loading
ixd /path/to/root 2>&1 | grep "loaded config"

# Expected:
# ixd [project-name]: loaded config — 1 exclude patterns, 2 watch roots
```

For full schema and more examples, see [docs/.ixd.toml.md](.ixd.toml.md).

### Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `XDG_RUNTIME_DIR` | `/run/user/{uid}` | Socket location |
| `RUST_LOG` | `info` | Log level (debug/info/warn/error) |
| `IX_DEBUG_BUILD` | (unset) | If set, emits `IX-INDEXED:` and `IX-FLUSH:` per-file lines to stderr during indexing |

### Socket Locations

The daemon socket path is deterministic based on the watched root:

1. **Primary**: `$XDG_RUNTIME_DIR/ixd/{hash}.sock`
2. **Fallback**: `~/.local/run/ixd/{hash}.sock`
3. **Last resort**: `/tmp/ixd-{uid}-{hash}.sock`

Where `{hash}` = first 16 hex chars of `XXH64(canonical_root, 0)`.

**Example:**
```bash
# Calculate hash for /workspace/ix
echo -n "/workspace/ix" | xxh64 | cut -c1-16
# Output: a1b2c3d4e5f67890

# Socket path
ls -la /run/user/1000/ixd/a1b2c3d4e5f67890.sock
```

---

## Operation Modes

### Idle State

**Status string:** `"idle"`  
**Entropy:** < 1000  
**Behavior:** Waiting for file changes, socket queries served immediately

```json
{
  "t": "status",
  "pid": 12345,
  "status": "idle",
  "files": 1234,
  "daemon_status": {
    "state": "idle"
  }
}
```

### Indexing State

**Status string:** `"indexing (entropy: N)"`  
**Trigger:** File change detected  
**Behavior:** Updating delta index, queries may block

```json
{
  "t": "status",
  "pid": 12345,
  "status": "indexing (entropy: 450)",
  "files": 1234,
  "daemon_status": {
    "state": "indexing",
    "entropy": 450
  }
}
```

### Deferred State

**Status string:** `"deferred (entropy: N)`  
**Trigger:** System entropy > 1000  
**Behavior:** Indexing postponed, queries served from current index

```json
{
  "t": "status",
  "pid": 12345,
  "status": "deferred (entropy: 950)",
  "files": 1234,
  "daemon_status": {
    "state": "deferred",
    "entropy": 950
  }
}
```

### Safety States

| Status | Trigger | Recovery |
|--------|---------|----------|
| `"compacting"` | Full index rebuild (idle or delta size > 50 MB) | Automatic — completes within 30s–5min |
| `"warned: ..."` | Minor safety concern | Automatic after 300ms |
| `"escalated (entropy: N)"` | High entropy + surprise | Throttle, cooldown 1000ms |
| `"safety halt"` | Critical safety decision | Manual intervention required |
| `"safety exit"` | Unrecoverable error | Restart daemon |
| `"build failed: ..."` | Initial index build failed | Fix underlying issue, daemon will retry |

---

## Monitoring

### Health Checks

```bash
# Check if daemon is running
pgrep -f "ixd /path/to/repo"
# Exit code 0 = running, 1 = not running

# Query daemon status
echo '{"t":"status_query"}' | socat - UNIX-CONNECT:/path/to/socket

# Check socket responsiveness
timeout 5 bash -c 'echo -ne "{\"t\":\"status_query\"}\n" | socat - UNIX-CONNECT:/path/to/socket'
# Expected: response within 5 seconds
```

### Log Analysis

```bash
# Enable debug logging
export RUST_LOG=ix=debug
./target/release/ixd /path/to/repo 2>&1 | tee ixd.log

# Search for errors
grep -i error ixd.log
grep -i "failed\|panic" ixd.log

# Monitor file change rate
grep "files changed" ixd.log | tail -100
# Expected: frequency indicates indexing load
```

### Metrics Collection

```bash
# Extract metrics from logs
grep "files changed" ixd.log | awk '{print $4}' | sort -n | uniq -c

# Count indexing operations
grep -c "index updated" ixd.log

# Measure latency (time between change and completion)
grep -A1 "files changed" ixd.log | grep "index updated" | \
  awk '{print $2 - prev_time}'  # Requires timestamp parsing
```

---

## Troubleshooting

### Daemon Won't Start

**Symptom:** `ixd` exits immediately with error

**Check:**
```bash
# Check for concurrent instance
cat /path/to/repo/.ix/beacon.json
# If exists and PID is valid, another instance is running

# Check permissions
ls -la /path/to/repo/.ix/
# Expected: writable by current user

# Check disk space
df -h /path/to/repo
# Expected: >10% free space
```

**Fix:**
```bash
# Remove stale beacon (if PID not running)
rm /path/to/repo/.ix/beacon.json

# Rebuild index from scratch
rm -rf /path/to/repo/.ix/
ixd /path/to/repo
```

### High Entropy / Deferred Indexing

**Symptom:** Status shows `"deferred (entropy: N)"` frequently

**Check:**
```bash
# Check system memory pressure
free -h
cat /proc/stat | grep cpu

# Check daemon memory usage
ps -o pid,rss,vsz,cmd -p $(pgrep -f "ixd /path")
```

**Fix:**
```bash
# Reduce watched directory scope
# Edit .gitignore or watcher config to exclude large directories

# Increase system memory
# Close other memory-intensive applications

# Restart daemon (clears accumulated state)
pkill -f "ixd /path/to/repo"
ixd /path/to/repo
```

### Socket Connection Failures

**Symptom:** Client cannot connect to daemon socket

**Check:**
```bash
# Verify socket exists
ls -la $XDG_RUNTIME_DIR/ixd/*.sock

# Check socket permissions
stat -c "%a %U:%G" /path/to/socket
# Expected: 0755 or 0777

# Test connection
echo '{"t":"status_query"}' | socat - UNIX-CONNECT:/path/to/socket
```

**Fix:**
```bash
# Restart daemon (recreates socket)
pkill -f "ixd"
ixd /path/to/repo

# Check XDG_RUNTIME_DIR is accessible
echo $XDG_RUNTIME_DIR
ls -la $XDG_RUNTIME_DIR
```

### Index Corruption

**Symptom:** Search returns incorrect results or crashes

**Check:**
```bash
# Verify index integrity
ix --stats "test" 2>&1 | grep -i error

# Check delta file size
ls -la /path/to/repo/.ix/shard.ix.delta
# Expected: reasonable size (<100MB typical)
```

**Fix:**
```bash
# Delete delta and rebuild
rm /path/to/repo/.ix/shard.ix.delta
ix --build /path/to/repo

# Full rebuild if corruption persists
rm -rf /path/to/repo/.ix/
ix --build /path/to/repo
```

---

## Safety Features

### LLMOSafe Integration

The daemon uses `ResourceGuard` to monitor:

- **RSS Memory**: 60% system ceiling
- **Entropy**: System-wide pressure metric
- **Surprise**: Rate of change detection
- **Bias**: Long-term trend analysis

**Safety Decisions:**

| Decision | Trigger | Action |
|----------|---------|--------|
| `Proceed` | All metrics nominal | Continue indexing |
| `Warn` | Minor threshold breach | Log warning, 300ms pause |
| `Escalate` | High entropy + surprise | Throttle, 1000ms cooldown |
| `Halt` | Critical breach | Pause operations, manual review |
| `Exit` | Unrecoverable | Terminate daemon |

### Concurrent Instance Prevention

The daemon prevents multiple instances watching the same root:

```json
{
  "pid": 12345,
  "root": "/path/to/repo",
  "start_time": 1684089600,
  "status": "idle",
  "last_event_at": 1684089600,
  "socket_path": "/run/user/1000/ixd/a1b2c3d4e5f67890.sock",
  "instance_id": 1684089600000000000
}
```

**Verification:**
```bash
# Check beacon
cat /path/to/repo/.ix/beacon.json

# Verify PID is running
ps -p 12345

# Check if PID is actually ixd
cat /proc/12345/comm
# Expected: "ixd"
```

---

## Shutdown Procedures

### Graceful Shutdown

```bash
# Send SIGTERM (recommended)
kill -TERM $(pgrep -f "ixd /path/to/repo")

# Or use Ctrl+C if running in foreground
# Daemon will:
# 1. Complete current indexing operation
# 2. Write final status to beacon
# 3. Remove beacon file
# 4. Exit cleanly
```

### Forceful Shutdown

```bash
# Only if graceful shutdown fails
kill -KILL $(pgrep -f "ixd /path/to/repo")

# Clean up beacon manually
rm /path/to/repo/.ix/beacon.json
```

---

## Performance Tuning

### Memory Limits

```bash
# Set explicit memory ceiling (default: 60%)
export LLMOSAFE_CEILING=0.5  # 50% of system memory

# Monitor memory usage
watch -n1 'ps -o pid,rss,cmd -p $(pgrep -f ixd)'
```

### File Watch Limits

```bash
# Check inotify limits (Linux)
cat /proc/sys/fs/inotify/max_user_watches
# Expected: 8192 or higher

# Increase if needed
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
```

---

---

## See Also

- `DAEMON-SOCKET-INTERNALS.md` — Socket protocol specification
- `docs/DELTA-FORMAT.md` — Delta index format
- `src/lib/daemon.rs` — Full daemon implementation
- `src/lib/daemon_sock.rs` — Socket handling code

### Client Shutdown Protocol

When the daemon receives a shutdown signal (SIGTERM/SIGINT), it:

1. **Broadcasts** `ServerMessage::Shutdown` to all connected clients via the Unix socket
2. **Waits** 1000ms for clients to receive the notice
3. **Closes** the socket and exits

**Client Behavior:**
- Clients receive `{"t":"shutdown","reason":"signal","delay_ms":1000}`
- Clients have 1000ms to finish in-flight operations
- After delay, socket closes (EOF)
- Clients can distinguish graceful shutdown from crash

**Example Client Handling:**
```json
// Client receives:
{"t":"shutdown","reason":"signal","delay_ms":1000}

// Client should:
// 1. Complete current query (if any)
// 2. Save state if needed
// 3. Reconnect after delay (if auto-reconnect enabled)
```

**Optional Acknowledgment:**
Clients can send acknowledgment (fire-and-forget):
```json
{"t":"shutdown","ack":true}
```

This is logged by the daemon but not required - the shutdown proceeds regardless.