llman 0.0.68

A tool for managing LLM application rules(prompts) ...
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
use super::Backend;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{:02x}", b)).collect()
}

/// Compute sha256 hash of all spec files (sorted by path).
///
/// Hashes every `*.feature` spec file in each spec directory. Including harness content means a hand-edited `.feature`
/// still triggers staleness — there should be no silent divergence between the
/// index and the on-disk behavior artifacts.
pub fn compute_spec_hash(specs_dir: &Path) -> Result<String> {
    let mut entries: Vec<PathBuf> = Vec::new();
    for entry in fs::read_dir(specs_dir).context("Failed to read specs directory")? {
        let entry = entry?;
        if !entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
            continue;
        }
        let spec_dir = entry.path();
        // Single-track (r131): `.feature` files are the only spec artifacts;
        // a leftover spec.toon is ignored here (validate reports it).
        if let Ok(features) = feature_paths_in(&spec_dir) {
            entries.extend(features);
        }
    }
    entries.sort();

    let mut hasher = Sha256::new();
    for path in &entries {
        let content = fs::read(path).context(format!("Failed to read {}", path.display()))?;
        hasher.update(&content);
    }
    Ok(hex_encode(&hasher.finalize()))
}

/// Sorted `*.feature` paths directly under `spec_dir` (non-recursive).
fn feature_paths_in(spec_dir: &Path) -> Result<Vec<PathBuf>> {
    let pattern = spec_dir.join("*.feature");
    let mut paths: Vec<PathBuf> = glob::glob(pattern.to_string_lossy().as_ref())
        .ok()
        .into_iter()
        .flatten()
        .flatten()
        .collect();
    paths.sort();
    Ok(paths)
}

/// Freshness status of the pageindex tree index
#[derive(Debug, Clone, PartialEq)]
pub enum IndexFreshness {
    Fresh,
    Stale {
        current_hash: String,
        stored_hash: String,
    },
    Missing,
    Corrupted(String),
}

/// Subdirectory name under `.context/` for the pageindex backend's index storage.
pub fn backend_subdir(backend: Backend) -> &'static str {
    let _ = backend;
    "pageindex"
}

/// Resolve the directory holding the pageindex backend's index.
pub fn resolve_backend_dir(context_dir: &Path, backend: Backend) -> PathBuf {
    let _ = backend;
    context_dir.join(backend_subdir(backend))
}

/// Best-effort summary of a pageindex tree index: (doc_count, build_timestamp, chat_model).
///
/// Parsed as generic JSON so this works without depending on the `tree.rs` types.
pub fn pageindex_summary(backend_dir: &Path) -> Option<(usize, String, String)> {
    let content = fs::read_to_string(backend_dir.join("tree.json")).ok()?;
    let value: serde_json::Value = serde_json::from_str(&content).ok()?;
    let docs = value["docs"].as_array().map(|a| a.len()).unwrap_or(0);
    let ts = value["build_timestamp"]
        .as_str()
        .unwrap_or("unknown")
        .to_string();
    let model = value["chat_model"]
        .as_str()
        .unwrap_or("unknown")
        .to_string();
    Some((docs, ts, model))
}

enum FreshErr {
    Missing,
    Corrupted(String),
}

/// Read the stored spec_hash for the pageindex backend from `tree.json`.
fn read_pageindex_spec_hash(backend_dir: &Path) -> std::result::Result<String, FreshErr> {
    let tree_path = backend_dir.join("tree.json");
    if !tree_path.exists() {
        return Err(FreshErr::Missing);
    }
    let content = fs::read_to_string(&tree_path).map_err(|e| FreshErr::Corrupted(e.to_string()))?;
    let value: serde_json::Value =
        serde_json::from_str(&content).map_err(|e| FreshErr::Corrupted(e.to_string()))?;
    value["spec_hash"]
        .as_str()
        .map(|s| s.to_string())
        .ok_or_else(|| FreshErr::Corrupted("tree.json missing spec_hash".to_string()))
}

/// Check the freshness of the pageindex index.
pub fn check_freshness(context_dir: &Path, specs_dir: &Path, backend: Backend) -> IndexFreshness {
    let _ = backend;
    let backend_dir = resolve_backend_dir(context_dir, backend);
    let stored_hash = read_pageindex_spec_hash(&backend_dir);
    let stored_hash = match stored_hash {
        Ok(h) => h,
        Err(FreshErr::Missing) => return IndexFreshness::Missing,
        Err(FreshErr::Corrupted(msg)) => return IndexFreshness::Corrupted(msg),
    };

    let current_hash = match compute_spec_hash(specs_dir) {
        Ok(h) => h,
        Err(e) => return IndexFreshness::Corrupted(e.to_string()),
    };

    if stored_hash == current_hash {
        IndexFreshness::Fresh
    } else {
        IndexFreshness::Stale {
            current_hash,
            stored_hash,
        }
    }
}

/// Rebuild lock information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RebuildLock {
    pub pid: u32,
    pub started_at: String,
    pub chunks_total: usize,
    pub chunks_done: usize,
    pub progress_pct: f64,
}

/// RAII guard that removes `.rebuild.lock` on drop.
pub struct RebuildLockGuard {
    path: PathBuf,
}

impl Drop for RebuildLockGuard {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

const REBUILD_LOCK_MAX_AGE_SECS: i64 = 6 * 60 * 60;

/// Acquire an exclusive rebuild lock via `create_new`, clearing stale locks first.
pub fn acquire_rebuild_lock(context_dir: &Path) -> Result<RebuildLockGuard> {
    fs::create_dir_all(context_dir).context("create context dir for rebuild lock")?;
    let lock_path = context_dir.join(".rebuild.lock");

    for _ in 0..2 {
        match try_create_rebuild_lock(&lock_path) {
            Ok(guard) => return Ok(guard),
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
                if clear_stale_rebuild_lock(context_dir)? {
                    continue;
                }
                let holder = read_rebuild_lock(&lock_path)
                    .map(|l| l.pid.to_string())
                    .unwrap_or_else(|_| "?".to_string());
                anyhow::bail!("index rebuild already in progress (pid={holder})");
            }
            Err(err) => {
                return Err(err).context(format!("create rebuild lock {}", lock_path.display()));
            }
        }
    }
    anyhow::bail!("failed to acquire rebuild lock at {}", lock_path.display());
}

fn try_create_rebuild_lock(lock_path: &Path) -> std::io::Result<RebuildLockGuard> {
    use std::io::Write;
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(lock_path)?;
    let lock = RebuildLock {
        pid: std::process::id(),
        started_at: chrono::Utc::now().to_rfc3339(),
        chunks_total: 0,
        chunks_done: 0,
        progress_pct: 0.0,
    };
    let content = toml::to_string(&lock).map_err(std::io::Error::other)?;
    file.write_all(content.as_bytes())?;
    Ok(RebuildLockGuard {
        path: lock_path.to_path_buf(),
    })
}

fn read_rebuild_lock(lock_path: &Path) -> Result<RebuildLock> {
    let content = fs::read_to_string(lock_path)?;
    Ok(toml::from_str(&content)?)
}

fn clear_stale_rebuild_lock(context_dir: &Path) -> Result<bool> {
    let lock_path = context_dir.join(".rebuild.lock");
    if !lock_path.exists() {
        return Ok(true);
    }
    match read_rebuild_lock(&lock_path) {
        Ok(lock) if is_rebuild_lock_stale(&lock) => {
            let _ = fs::remove_file(&lock_path);
            Ok(true)
        }
        Ok(_) => Ok(false),
        Err(_) => {
            let _ = fs::remove_file(&lock_path);
            Ok(true)
        }
    }
}

fn is_rebuild_lock_stale(lock: &RebuildLock) -> bool {
    if lock_age_secs(lock).is_some_and(|age| age > REBUILD_LOCK_MAX_AGE_SECS) {
        return true;
    }
    #[cfg(unix)]
    {
        if !is_pid_alive(lock.pid) {
            return true;
        }
        if let Some(comm) = read_proc_comm(lock.pid) {
            let comm = comm.trim();
            if !(comm == "llman" || comm.starts_with("llman")) {
                return true;
            }
        }
        false
    }
    #[cfg(not(unix))]
    {
        false
    }
}

fn lock_age_secs(lock: &RebuildLock) -> Option<i64> {
    let started = chrono::DateTime::parse_from_rfc3339(&lock.started_at).ok()?;
    Some((chrono::Utc::now() - started.with_timezone(&chrono::Utc)).num_seconds())
}

#[cfg(unix)]
fn read_proc_comm(pid: u32) -> Option<String> {
    fs::read_to_string(format!("/proc/{pid}/comm")).ok()
}

/// Check if a rebuild lock file exists and if the holder is still alive.
pub fn check_rebuild_lock(context_dir: &Path) -> Result<Option<RebuildLock>> {
    let lock_path = context_dir.join(".rebuild.lock");
    if !lock_path.exists() {
        return Ok(None);
    }

    let lock = match read_rebuild_lock(&lock_path) {
        Ok(lock) => lock,
        Err(_) => {
            let _ = fs::remove_file(&lock_path);
            return Ok(None);
        }
    };

    if is_rebuild_lock_stale(&lock) {
        let _ = fs::remove_file(&lock_path);
        return Ok(None);
    }

    Ok(Some(lock))
}

/// Check if a process is alive (Unix: uses `kill -0`; Windows not supported)
fn is_pid_alive(pid: u32) -> bool {
    #[cfg(unix)]
    {
        std::process::Command::new("kill")
            .arg("-0")
            .arg(pid.to_string())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }
    #[cfg(not(unix))]
    {
        let _ = pid;
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pageindex_freshness_isolation() {
        let tmp = tempfile::TempDir::new().unwrap();
        let root = tmp.path();
        let context_dir = root.join(".context");
        let specs_dir = root.join("specs");
        std::fs::create_dir_all(&specs_dir).unwrap();

        // Simulate an old rag directory — must not satisfy pageindex freshness.
        let rag_dir = context_dir.join("rag");
        std::fs::create_dir_all(&rag_dir).unwrap();
        let hash = compute_spec_hash(&specs_dir).unwrap();
        std::fs::write(
            rag_dir.join("metadata.toml"),
            format!(
                "version = 1\nspec_hash = \"{hash}\"\nspec_count = 1\nchunk_count = 1\n\
                 build_timestamp = \"2026-01-01T00:00:00Z\"\nmodel = \"m\"\nembedding_dim = 4\n"
            ),
        )
        .unwrap();

        // pageindex dir absent → pageindex freshness is Missing, NOT Fresh.
        assert_eq!(
            check_freshness(&context_dir, &specs_dir, Backend::Pageindex),
            IndexFreshness::Missing,
            "pageindex must not silently use the rag index"
        );
        // resolve_backend_dir for pageindex points at pageindex/, never rag/.
        assert_eq!(
            resolve_backend_dir(&context_dir, Backend::Pageindex),
            context_dir.join("pageindex")
        );
    }

    #[test]
    fn test_backend_parse_rejects_rag() {
        use super::super::Backend;
        assert!(Backend::parse("rag").is_err());
        assert_eq!(Backend::parse("pageindex").unwrap(), Backend::Pageindex);
        assert_eq!(Backend::parse(" PageIndex ").unwrap(), Backend::Pageindex);
        assert!(Backend::parse("nope").is_err());
    }

    #[test]
    fn test_resolve_backend_default() {
        use super::super::Backend;
        use super::super::resolve_backend;
        assert_eq!(resolve_backend(None).unwrap(), Backend::Pageindex);
        assert_eq!(
            resolve_backend(Some("pageindex".to_string())).unwrap(),
            Backend::Pageindex
        );
        assert_eq!(
            resolve_backend(Some(String::new())).unwrap(),
            Backend::Pageindex
        );
    }

    #[test]
    fn test_acquire_rebuild_lock_is_exclusive() {
        let tmp = tempfile::TempDir::new().unwrap();
        let context_dir = tmp.path().join(".context");
        std::fs::create_dir_all(&context_dir).unwrap();

        let first = acquire_rebuild_lock(&context_dir).expect("first lock");
        let second = acquire_rebuild_lock(&context_dir);
        assert!(second.is_err(), "second acquire must fail while held");
        drop(first);
        acquire_rebuild_lock(&context_dir).expect("lock after release");
    }

    #[test]
    fn test_stale_rebuild_lock_cleared_by_age() {
        let tmp = tempfile::TempDir::new().unwrap();
        let context_dir = tmp.path().join(".context");
        std::fs::create_dir_all(&context_dir).unwrap();
        let lock_path = context_dir.join(".rebuild.lock");
        let stale = RebuildLock {
            pid: std::process::id(),
            started_at: (chrono::Utc::now() - chrono::Duration::hours(7)).to_rfc3339(),
            chunks_total: 0,
            chunks_done: 0,
            progress_pct: 0.0,
        };
        std::fs::write(&lock_path, toml::to_string(&stale).unwrap()).unwrap();
        assert!(check_rebuild_lock(&context_dir).unwrap().is_none());
        assert!(!lock_path.exists());
    }

    #[test]
    fn test_compute_spec_hash_tracks_feature_files_only() {
        let tmp = tempfile::TempDir::new().unwrap();
        let specs_dir = tmp.path().join("specs");
        let spec_dir = specs_dir.join("demo");
        std::fs::create_dir_all(&spec_dir).unwrap();

        // Legacy spec.toon alone must NOT produce a hash input (r131).
        std::fs::write(spec_dir.join("spec.toon"), "kind: llman.sdd.spec\n").unwrap();
        let hash_toon_only = compute_spec_hash(&specs_dir).unwrap();

        // Adding the single-track feature flips the hash.
        std::fs::write(spec_dir.join("demo.feature"), "功能: x\n").unwrap();
        let hash_after_add = compute_spec_hash(&specs_dir).unwrap();
        assert_ne!(
            hash_toon_only, hash_after_add,
            "adding a .feature must flip staleness"
        );

        // Editing the .feature must also change the hash.
        std::fs::write(spec_dir.join("demo.feature"), "功能: y\n").unwrap();
        let hash_after_edit = compute_spec_hash(&specs_dir).unwrap();
        assert_ne!(
            hash_after_add, hash_after_edit,
            "editing a .feature must flip staleness"
        );
    }
}