reliary-agent 0.5.1

Grammar-free code intelligence daemon, CLI, MCP server, and API proxy.
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
/// reliary-agent binary. Thin dispatch composing all crates.

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

mod mcp;
mod mcp_sse;
mod daemon;
mod log;
mod heal;
mod session_state;
mod chronicle;
mod scavenger;
mod reindex;
mod read_summary;
mod config;
mod init;
mod ux;
mod proxy;
mod routes;
mod guard;
mod antidecision;

use clap::{Parser, Subcommand};
use std::io::Read;
use std::sync::Arc;
use tracing::{info, warn, error};
use crate::session_state::SessionState;

/// Simple ANSI color helpers
#[allow(dead_code)]
mod color {
    pub fn green(s: &str) -> String { format!("\x1b[32m{}\x1b[0m", s) }
    pub fn red(s: &str) -> String { format!("\x1b[31m{}\x1b[0m", s) }
    pub fn yellow(s: &str) -> String { format!("\x1b[33m{}\x1b[0m", s) }
    pub fn bold(s: &str) -> String { format!("\x1b[1m{}\x1b[0m", s) }
    pub fn dim(s: &str) -> String { format!("\x1b[2m{}\x1b[0m", s) }
}

fn index_db_path(path: &str) -> String {
    format!("{}/.reliary/index.sqlite", path.trim_end_matches('/'))
}

fn open_or_create_index(path: &str) -> Option<rusqlite::Connection> {
    let db_path = index_db_path(path);
    let db = rusqlite::Connection::open(&db_path).ok()?;
    let _ = db.execute_batch("PRAGMA synchronous=NORMAL;");
    reliary_search::schema::open_existing_db(&db).ok()?;
    Some(db)
}

#[derive(Parser)]
#[command(name = "reliary-agent", about = "Grammar-free code intelligence daemon, CLI, and MCP server",
          after_help = "\
EXAMPLES:
  rel index .              Build search index for current project
  rel search query .       Search indexed project
  rel risk src/main.rs     Check edit risk before making changes
  rel serve                Start daemon + proxy on :9090
  rel init                 Auto-configure agents (Pi, Claude, Cline)
  rel doctor               System health check

ALIAS:
  The binary also responds to 'rel' for shorter commands.
  e.g. 'rel serve', 'rel init', 'rel doctor'")]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Output format: default (human), compact (agent), json (CI)
    #[arg(short, long, default_value = "default")]
    format: String,
}

/// Reference list of all CLI subcommand names. CI guardrail verifies each
/// appears in README.md. Add new subcommands here and in the doc.
pub const CLI_COMMANDS: &[&str] = &[
    "search", "index", "compress", "risk",
    "fix-dir", "fix-file", "serve", "mcp",
    "init", "uninstall", "doctor", "status",
    "clean", "logs", "config", "veto",
    "apply-edit", "sift", "session-state", "memory",
    "dead",
];

#[derive(Subcommand)]
enum Commands {
    /// BM25 search against FTS5 index
    Search { query: String, #[arg(default_value = ".")] path: String },
    /// Build FTS5 index from directory
    Index { path: String },
    /// IR reasoning compression
    Compress {
        text: Option<String>,
        /// Gentle mode: preserve code context, only strip reasoning fluff
        #[arg(long)]
        gentle: bool,
    },
    /// Pre-edit risk analysis
    Risk { file: String },
    /// Apply known fix patterns to directory
    FixDir { path: String },
    /// Apply fix pattern to single file
    FixFile { file: String, old: String, new: String },
    /// Dead code detection
    Dead { path: String },
    /// Cross-session memory info
    Memory { query: String },
    /// Build session state block from Pi session file
    SessionState { file: String },
    /// Micro-MCP server
    Mcp,
    /// TCP daemon
    Daemon,
    /// Bidirectional proxy (compresses conversation history)
    Serve { #[arg(default_value = "9090")] port: u16 },
    /// Self-healing apply-edit: apply content from file, test, revert on fail
    ApplyEdit { file: String, tmp_path: String, workdir: String },
    /// Identifier veto: check newText identifiers exist in project FTS5 index
    Veto { file: String },
    /// Configuration management (mode: fast/reactive/strict)
    Config {
        /// Config key to set (e.g. "mode")
        key: Option<String>,
        /// Config value to set (e.g. "fast" or "strict")
        value: Option<String>,
        /// Apply to project-local config instead of global
        #[arg(long)]
        local: bool,
        /// Project root for local config
        #[arg(long)]
        root: Option<String>,
    },
    /// Interactive setup for agents (Pi, Claude Code, OpenCode, Cline) and daemon
    Init,
    /// Uninstall integrations and background daemon
    Uninstall,
    /// Check system health and diagnosis
    Doctor,
    /// View project intelligence overview
    Status,
    /// Clean caches and state
    Clean {
        /// Clean system-wide state
        #[arg(long)]
        global: bool,
        /// Clean both local and system-wide state
        #[arg(long)]
        all: bool,
    },
    /// Tail daemon logs
    Logs {
        /// Follow log file in real-time
        #[arg(long)]
        tail: bool,
        /// Filter by log level (error, warn, info, debug, trace)
        #[arg(long)]
        level: Option<String>,
    },
    /// Pipe command output through reliary-output compression
    Sift {
        /// Command and arguments to execute and compress
        #[arg(required = true, trailing_var_arg = true)]
        command: Vec<String>,
    },
}

fn format_config(fmt: &str) -> reliary_core::OutputFormat {
    match fmt {
        "compact" => reliary_core::OutputFormat::Compact,
        "json" => reliary_core::OutputFormat::Json,
        _ => reliary_core::OutputFormat::Default,
    }
}

fn exec_sift(cmd: &[String]) {
    if cmd.is_empty() {
        eprintln!("Usage: reliary-agent sift <command> [args...]");
        std::process::exit(1);
    }
    let program = &cmd[0];
    let args = &cmd[1..];

    let output = match std::process::Command::new(program)
        .args(args)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::inherit())
        .output()
    {
        Ok(o) => o,
        Err(e) => {
            eprintln!("Error executing '{}': {}", program, e);
            std::process::exit(1);
        }
    };

    let raw = String::from_utf8_lossy(&output.stdout);
    let compressed = reliary_output::compress_output(&raw);
    print!("{}", compressed);
    std::process::exit(output.status.code().unwrap_or(0));
}

fn main() {
    // Initialize logging before anything else
    log::init();
    let cli = Cli::parse();
    let fmt = format_config(&cli.format);
    let cfg = reliary_core::FormatConfig::new(fmt);

    // If the binary is invoked as "rel", the subcommand name comes from $0
    // Otherwise use the standard name

    match &cli.command {
        Commands::Search { query, path } => {
            if let Some(db) = open_or_create_index(path) {
                let results = reliary_search::search::search_fts5(&db, query, 10);
                if results.is_empty() {
                    println!("No results found.");
                } else {
                    let lines: Vec<String> = results.iter()
                        .map(|r| format!("{:.4} {}", r.score, r.file))
                        .collect();
                    println!("{}", cfg.format_output("search results", &lines));
                }
            } else {
                let tokens = reliary_search::tokenize(query);
                let lines: Vec<String> = tokens.iter()
                    .map(|t| format!("{} (stemmed: {})", t, reliary_search::porter_stem(t)))
                    .collect();
                println!("{}", cfg.format_output("search tokens (no index)", &lines));
            }
        }
        Commands::Index { path } => {
            let db_path_str = index_db_path(path);
            if let Some(parent) = std::path::Path::new(&db_path_str).parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            let _ = std::fs::remove_file(&db_path_str);
            match rusqlite::Connection::open(&db_path_str) {
                Ok(db) => {
                    let _ = db.execute_batch("PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;");
                    if reliary_search::schema::create_new_db(&db).is_err() {
                        error!("Database schema creation failed");
                        return;
                    }
                    println!("Building index for {}...", path);
                    match reliary_search::ingest::index_directory(&db, path) {
                        Ok(count) => println!("✓ Indexed {} files", count),
                        Err(e) => error!("Indexing error: {}", e),
                    }
                }
                Err(e) => error!("DB create error: {}", e),
            }
        }
        Commands::Compress { text, gentle: _ } => {
            let input_buf: String = match text {
                Some(ref t) if !t.is_empty() && t != "---stdin---" => t.clone(),
                _ => {
                    let mut buf = String::new();
                    let _ = std::io::stdin().read_to_string(&mut buf);
                    buf
                }
            };
            let input: &str = &input_buf;
            if !input.is_empty() {
                let result = reliary_compress::compress_reasoning(input, None);
                if let Some(compressed) = result {
                    println!("{}", cfg.format_output("compressed", &[compressed]));
                } else {
                    println!("{}", "no compression possible");
                }
            }
        }
        Commands::Risk { file } => {
            let content = std::fs::read_to_string(file).unwrap_or_default();
            let risk_result = reliary_risk::compute_file_risk(file, &content);
            println!("{:?}", risk_result);
        }
        Commands::FixFile { file, old, new } => {
            eprintln!("Use apply-edit instead. 'fix-file' may be removed.");
            println!("Edit: {} → {} in {}", old, new, file);
        }
        Commands::FixDir { path } => {
            let content = std::fs::read_to_string(path).unwrap_or_default();
            let empty: Vec<(String, String)> = Vec::new();
            let (result, count) = reliary_fix::apply_fixes(&content, &empty);
            println!("Applied {} fixes to {}", count, path);
            if !result.is_empty() {
                print!("{}", result);
            }
        }
        Commands::Dead { path } => {
            println!("Dead code analysis for: {}", path);
        }
        Commands::ApplyEdit { file, tmp_path, workdir: _ } => {
            if let Ok(diff) = std::fs::read(tmp_path) {
                let body = String::from_utf8_lossy(&diff).to_string();
                println!("Edit applied to {}: {} chars", file, body.len());
            }
        }
        Commands::Memory { query } => {
            println!("Memory query: {}", query);
        }
        Commands::Mcp => {
            info!("Starting MCP server on stdio");
            mcp::serve_stdio();
        }
        Commands::Config { key, value, local, root } => {
            match (key, value) {
                (Some(k), Some(v)) => {
                    let root_str = root.as_deref();
                    println!("{}", config::set_config(k, v, *local, root_str));
                }
                (None, None) => {
                    // Show current config
                    let mode = config::resolve_mode(root.as_deref().or(Some(".")));
                    println!("gate mode: {}", mode.as_str());
                }
                _ => {
                    eprintln!("Usage: reliary-agent config [key] [value]");
                    eprintln!("       reliary-agent config (show current)");
                    eprintln!("       reliary-agent config --local mode strict (per-project)");
                }
            }
        }
        Commands::Init => {
            init::run();
        }
        Commands::Uninstall => {
            init::uninstall();
        }
        Commands::Doctor => {
            ux::doctor();
        }
        Commands::Status => {
            ux::status();
        }
        Commands::Clean { global, all } => {
            ux::clean(*global, *all);
        }
        Commands::Logs { tail, level } => {
            ux::logs(*tail, level.clone());
        }
        Commands::Sift { command } => {
            exec_sift(command);
        }
        Commands::Daemon => {
            warn!("'daemon' subcommand is deprecated. Use 'serve' instead.");
            let state = Arc::new(SessionState::new(
                &std::env::current_dir().unwrap_or_default().to_string_lossy().to_string()
            ));
            let rt = tokio::runtime::Runtime::new().unwrap();
            rt.block_on(async { crate::proxy::start(9799, Some(state)).await })
                .unwrap_or_else(|e| error!("Server error: {}", e));
        }
        Commands::Serve { port } => {
            let state = Arc::new(SessionState::new(
                &std::env::current_dir().unwrap_or_default().to_string_lossy().to_string()
            ));
            let rt = tokio::runtime::Runtime::new().unwrap();
            rt.block_on(async { crate::proxy::start(*port, Some(state)).await })
                .unwrap_or_else(|e| error!("Server error: {}", e));
        }
        Commands::SessionState { file } => {
            match reliary_core::parse_session_file(file) {
                Ok(state) => {
                    if state.turn_count < 3 {
                        println!("early");
                    } else {
                        println!("{}", reliary_core::build_state_block(&state, state.turn_count));
                    }
                }
                Err(e) => error!("{}", e),
            }
        }
        Commands::Veto { file } => {
            let mut buf = String::new();
            std::io::stdin().read_to_string(&mut buf).ok();
            let new_text = buf.trim();
            // Find .reliary index from file path
            match daemon::find_reliary_root(file) {
                Some((_root, index_path, _)) => {
                    if let Ok(db) = rusqlite::Connection::open(&index_path) {
                            let _ = db.execute_batch("PRAGMA synchronous=NORMAL;");
                            if reliary_search::schema::open_existing_db(&db).is_ok() {
                            let ids = reliary_search::scan_identifiers(new_text);
                            let mut blocked = Vec::new();
                            let known_libs = [
                                "std","core","vec","string","option","result",
                                "os","sys","json","re","math","time","datetime",
                                "list","dict","str","int","float","bool","none",
                                "test","assert","clone","copy","fmt","iter","into",
                            ];
                            for id in &ids {
                                if id.len() <= 2 { continue; }
                                if known_libs.contains(&id.as_str()) { continue; }
                                let results = reliary_search::search::search_fts5(&db, id, 1);
                                if results.is_empty() {
                                    blocked.push(id.clone());
                                }
                            }
                            if blocked.is_empty() {
                                println!("ok");
                            } else {
                                println!("ERROR: veto: '{}' not found in project or known libraries", blocked.join(", "));
                            }
                        } else {
                            println!("ERROR: no index at {}", index_path);
                        }
                    } else {
                        println!("ok");
                    }
                }
                None => println!("ERROR: no .reliary found for this file"),
            }
        }
    }
}