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
//! Command handlers for vipune CLI.
mod doctor;
mod handlers;
mod merge;
mod reindex;
#[cfg(test)]
mod doctor_projects_tests;
#[cfg(test)]
mod merge_tests;
#[cfg(test)]
mod reindex_tests;
use crate::config;
use crate::errors::Error;
use crate::memory::lifecycle::{MemoryStatus, MemoryType};
use crate::memory::{MemoryStore, UpdateParams};
use std::process::ExitCode;
/// Commands supported by vipune CLI.
#[derive(clap::Subcommand)]
pub enum Commands {
Validate {
/// Text to validate for embedding
text: String,
},
Add {
/// Memory text content
text: String,
/// Optional JSON metadata
#[arg(short = 'm', long)]
metadata: Option<String>,
/// Bypass conflict detection and store the memory unconditionally.
#[arg(long)]
force: bool,
/// Memory type (fact, preference, procedure, guard, observation)
#[arg(long, default_value = "fact")]
memory_type: String,
/// Memory status (active, candidate)
#[arg(long, default_value = "active")]
status: String,
/// Supersede an existing memory (atomic replacement)
#[arg(long)]
supersedes: Option<String>,
},
Search {
/// Search query text
query: String,
/// Maximum number of results (default: 5)
#[arg(short = 'l', long, default_value = "5")]
limit: usize,
/// Recency weight for search results (0.0 to 1.0)
#[arg(long)]
recency: Option<f64>,
/// Use hybrid search (semantic + BM25 with RRF fusion)
#[arg(long)]
hybrid: bool,
/// Disable hybrid search even when enabled in config
#[arg(long)]
no_hybrid: bool,
/// Filter by memory type (comma-separated)
#[arg(long)]
memory_type: Option<String>,
/// Filter by status (default: active)
#[arg(long)]
status: Option<String>,
/// Include candidate memories in results
#[arg(long)]
include_candidates: bool,
/// Do not update retrieval telemetry (retrieval_count, last_retrieved_at)
#[arg(long)]
no_touch: bool,
},
Get {
/// Memory ID
id: String,
/// Do not update retrieval telemetry
#[arg(long)]
no_touch: bool,
},
List {
/// Maximum number of results (default: 10)
#[arg(short = 'l', long, default_value = "10")]
limit: usize,
/// Filter by memory type (comma-separated)
#[arg(long)]
memory_type: Option<String>,
/// Filter by status (default: active)
#[arg(long)]
status: Option<String>,
/// Include candidate memories in results
#[arg(long)]
include_candidates: bool,
},
Delete {
/// Memory ID
id: String,
},
Update {
/// Memory ID
id: String,
/// New content (optional)
#[arg(short = 't', long)]
text: Option<String>,
/// Optional JSON metadata (replaces existing metadata)
#[arg(short = 'm', long)]
metadata: Option<String>,
/// Update memory type
#[arg(long)]
memory_type: Option<String>,
/// Update memory status
#[arg(long)]
status: Option<String>,
},
/// Diagnose database health.
#[command(group = clap::ArgGroup::new("doctor-mode").args(["embeddings", "projects"]).required(true).multiple(false))]
Doctor {
/// Check embedding quality (classifies real/mock/unknown)
#[arg(long)]
embeddings: bool,
/// Scan all projects for suspected split pairs (bare id vs owner/repo)
#[arg(long)]
projects: bool,
/// Project identifier (only relevant for --embeddings; ignored for --projects with a warning)
#[arg(long, short = 'p')]
project: Option<String>,
},
/// Re-embed rows with mock embeddings using the real model.
Reindex {
/// Reindex all projects in the database instead of only the current one
#[arg(long)]
all_projects: bool,
},
/// Project management operations.
Project {
#[command(subcommand)]
command: ProjectCommands,
},
Version,
#[cfg(feature = "mcp")]
/// Start MCP server over stdio
Mcp,
}
/// Subcommands under `vipune project`.
#[derive(clap::Subcommand)]
pub enum ProjectCommands {
/// Merge all rows from one project into another.
///
/// Moves every row whose project_id matches `from` to `to`.
/// The operation is atomic — either all rows move or none do.
/// Only the project_id column changes; all other data is preserved byte-identically.
/// Merging from a project into itself is a no-op.
Merge {
/// Source project id (rows moved from this)
from: String,
/// Target project id (rows moved to this)
to: String,
},
}
/// Execute a CLI command.
pub fn execute(
command: &Commands,
store: &mut MemoryStore,
project_id: String,
config: &config::Config,
json: bool,
) -> Result<ExitCode, Error> {
match command {
Commands::Validate { text } => {
handlers::handle_validate(text, &config.embedding_model, json)
}
Commands::Add {
text,
metadata,
force,
memory_type,
status,
supersedes,
} => handlers::handle_add(
store,
&project_id,
text,
metadata.as_deref(),
*force,
memory_type,
status,
supersedes.as_deref(),
json,
),
Commands::Search {
query,
limit,
recency,
hybrid,
no_hybrid,
memory_type,
status,
include_candidates,
no_touch,
} => handlers::handle_search(
store,
&project_id,
&handlers::SearchContext {
query: query.clone(),
limit: *limit,
recency: *recency,
hybrid: *hybrid,
no_hybrid: *no_hybrid,
memory_type: memory_type.clone(),
status: status.clone(),
include_candidates: *include_candidates,
no_touch: *no_touch,
},
config,
json,
),
Commands::Get { id, no_touch } => {
handlers::handle_get(store, id, &project_id, *no_touch, json)
}
Commands::List {
limit,
memory_type,
status,
include_candidates,
} => handlers::handle_list(
store,
&project_id,
*limit,
memory_type.as_deref(),
status.as_deref(),
*include_candidates,
json,
),
Commands::Delete { id } => handlers::handle_delete(store, id, &project_id, json),
Commands::Update {
id,
text,
metadata,
memory_type,
status,
} => {
let memory_type_val = memory_type
.as_deref()
.map(MemoryType::from_str)
.transpose()?;
let status_val = status.as_deref().map(MemoryStatus::from_str).transpose()?;
handlers::handle_update(
store,
id,
&project_id,
UpdateParams {
text: text.as_deref(),
metadata: metadata.as_deref(),
memory_type: memory_type_val,
status: status_val,
},
json,
)
}
Commands::Doctor {
embeddings: _,
projects,
project: doctor_project,
} => {
if *projects {
// doctor --projects always scans all projects; -p is ignored (with warning).
let project_filter = doctor_project.as_deref();
doctor::handle_doctor_projects(&config.database_path, project_filter, json)
} else {
// doctor --embeddings: use explicit -p or fall back to the detected project_id.
let project_filter = doctor_project.as_deref().or(Some(project_id.as_str()));
doctor::handle_doctor(&config.database_path, project_filter, json)
}
}
Commands::Reindex { all_projects } => {
let project_filter = if !*all_projects {
Some(project_id.as_str())
} else {
None
};
reindex::handle_reindex(
&config.database_path,
&config.embedding_model,
project_filter,
json,
)
}
Commands::Project { command } => match command {
ProjectCommands::Merge { from, to } => {
merge::handle_merge(&config.database_path, from, to, json)
}
},
Commands::Version => handlers::handle_version(json),
#[cfg(feature = "mcp")]
Commands::Mcp => unreachable!("Mcp is handled before execute"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_short_text() {
let result =
handlers::handle_validate("hello world", "not-a-real-model-should-fail", false);
// Should fail because model doesn't exist, not because of token count
assert!(result.is_err());
}
#[test]
fn test_validate_long_text() {
let long_text = "a".repeat(1000);
let result = handlers::handle_validate(&long_text, "not-a-real-model-should-fail", false);
// Should fail because model doesn't exist, not because of token count
assert!(result.is_err());
}
}