memvid-cli 2.0.140

Command-line interface for Memvid v2 - AI memory with crash-safe, single-file storage
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
//! Ticket management command handlers (list, issue, revoke, sync, apply)

use std::path::PathBuf;
use std::str::FromStr;

use anyhow::{anyhow, bail, Context, Result};
use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine};
use chrono::Utc;
use clap::{ArgAction, Args, Subcommand};
use memvid_core::types::{MemoryBinding, SignedTicket};
use memvid_core::{verify_ticket_signature, Memvid, Ticket};
use serde_json::json;
use uuid::Uuid;

/// A memory ID that accepts both UUID (32 chars) and MongoDB ObjectId (24 chars) formats
#[derive(Debug, Clone, Copy)]
pub struct MemoryId(Uuid);

impl serde::Serialize for MemoryId {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl MemoryId {
    pub fn as_uuid(&self) -> &Uuid {
        &self.0
    }
}

impl std::ops::Deref for MemoryId {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for MemoryId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for MemoryId {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        // Try parsing as standard UUID first
        if let Ok(uuid) = Uuid::parse_str(s) {
            return Ok(MemoryId(uuid));
        }

        // If it's 24 chars (MongoDB ObjectId), pad with zeros to make it a valid UUID
        if s.len() == 24 && s.chars().all(|c| c.is_ascii_hexdigit()) {
            // Pad to 32 chars by appending zeros
            let padded = format!("{}00000000", s);
            if let Ok(uuid) = Uuid::parse_str(&padded) {
                return Ok(MemoryId(uuid));
            }
        }

        Err(format!(
            "invalid memory ID '{}': expected UUID (32 chars) or ObjectId (24 chars)",
            s
        ))
    }
}

use crate::api::{
    apply_ticket as api_apply_ticket, fetch_ticket as api_fetch_ticket,
    ApplyTicketRequest as ApiApplyTicketRequest,
};
use crate::commands::{apply_ticket_with_warning, LockCliArgs};
use crate::config::CliConfig;
use crate::ticket_cache::{load as load_cached_ticket, store as store_cached_ticket, CachedTicket};
use crate::utils::{apply_lock_cli, open_read_only_mem};

/// Subcommands for the `tickets` command
#[derive(Subcommand)]
pub enum TicketsCommand {
    /// Show the currently applied ticket
    List(TicketsStatusArgs),
    /// Apply a new ticket
    Issue(TicketsIssueArgs),
    /// Clear ticket metadata while advancing the sequence
    Revoke(TicketsRevokeArgs),
    /// Synchronise the ticket state with the Memvid API
    Sync(TicketsSyncArgs),
    /// Submit a ticket fetched from the API back to the control plane
    Apply(TicketsApplyArgs),
}

/// Arguments for the `tickets` command
#[derive(Args)]
pub struct TicketsArgs {
    #[command(subcommand)]
    pub command: TicketsCommand,
}

/// Arguments for the `tickets sync` subcommand
#[derive(Args)]
pub struct TicketsSyncArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(
        long = "memory-id",
        value_name = "ID",
        help = "Memory ID (UUID or 24-char ObjectId)"
    )]
    pub memory_id: MemoryId,
    #[arg(long)]
    pub json: bool,

    #[command(flatten)]
    pub lock: LockCliArgs,
}

/// Arguments for the `tickets apply` subcommand
#[derive(Args)]
pub struct TicketsApplyArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(
        long = "memory-id",
        value_name = "ID",
        help = "Memory ID (UUID or 24-char ObjectId)"
    )]
    pub memory_id: MemoryId,
    #[arg(long = "from-api", action = ArgAction::SetTrue)]
    pub from_api: bool,
    #[arg(long)]
    pub json: bool,
}

/// Arguments for the `tickets issue` subcommand
#[derive(Args)]
pub struct TicketsIssueArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(long)]
    pub issuer: String,
    #[arg(long, value_name = "SEQ")]
    pub seq: i64,
    #[arg(long = "expires-in", value_name = "SECS")]
    pub expires_in: Option<u64>,
    #[arg(long, value_name = "BYTES")]
    pub capacity: Option<u64>,
    #[arg(long)]
    pub json: bool,

    #[command(flatten)]
    pub lock: LockCliArgs,
}

/// Arguments for the `tickets revoke` subcommand
#[derive(Args)]
pub struct TicketsRevokeArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(long)]
    pub json: bool,

    #[command(flatten)]
    pub lock: LockCliArgs,
}

/// Arguments for the `tickets list` subcommand
#[derive(Args)]
pub struct TicketsStatusArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(long)]
    pub json: bool,
}

// ============================================================================
// Tickets command handlers
// ============================================================================

pub fn handle_tickets(config: &CliConfig, args: TicketsArgs) -> Result<()> {
    match args.command {
        TicketsCommand::List(status) => handle_ticket_status(status),
        TicketsCommand::Issue(issue) => handle_ticket_issue(issue),
        TicketsCommand::Revoke(revoke) => handle_ticket_revoke(revoke),
        TicketsCommand::Sync(sync) => handle_ticket_sync(config, sync),
        TicketsCommand::Apply(apply) => handle_ticket_apply(config, apply),
    }
}

fn ticket_public_key(config: &CliConfig) -> Result<&ed25519_dalek::VerifyingKey> {
    config
        .ticket_pubkey
        .as_ref()
        .ok_or_else(|| anyhow!("MEMVID_TICKET_PUBKEY is not set"))
}

fn ticket_to_json(ticket: &memvid_core::TicketRef) -> serde_json::Value {
    json!({
        "issuer": ticket.issuer,
        "seq_no": ticket.seq_no,
        "expires_in_secs": ticket.expires_in_secs,
        "capacity_bytes": ticket.capacity_bytes,
        "verified": ticket.verified,
    })
}

pub fn handle_ticket_status(args: TicketsStatusArgs) -> Result<()> {
    let mem = open_read_only_mem(&args.file)?;
    let ticket = mem.current_ticket();
    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&ticket_to_json(&ticket))?
        );
    } else {
        println!("Ticket issuer: {}", ticket.issuer);
        println!("Sequence: {}", ticket.seq_no);
        println!("Expires in (secs): {}", ticket.expires_in_secs);
        if ticket.capacity_bytes != 0 {
            println!("Capacity bytes: {}", ticket.capacity_bytes);
        } else {
            println!("Capacity bytes: (tier default)");
        }
    }
    Ok(())
}

pub fn handle_ticket_issue(args: TicketsIssueArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;
    apply_lock_cli(&mut mem, &args.lock);
    let mut ticket = Ticket::new(&args.issuer, args.seq);
    if let Some(expires) = args.expires_in {
        ticket = ticket.expires_in_secs(expires);
    }
    if let Some(capacity) = args.capacity {
        ticket = ticket.capacity_bytes(capacity);
    }
    apply_ticket_with_warning(&mut mem, ticket)?;
    let ticket = mem.current_ticket();
    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&ticket_to_json(&ticket))?
        );
    } else {
        println!(
            "Applied ticket seq={} issuer={}",
            ticket.seq_no, ticket.issuer
        );
    }
    Ok(())
}

pub fn handle_ticket_revoke(args: TicketsRevokeArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;
    apply_lock_cli(&mut mem, &args.lock);
    let current = mem.current_ticket();
    let next_seq = current.seq_no.saturating_add(1).max(1);
    let ticket = Ticket::new("", next_seq);
    apply_ticket_with_warning(&mut mem, ticket)?;
    let ticket = mem.current_ticket();
    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&ticket_to_json(&ticket))?
        );
    } else {
        println!("Ticket cleared; seq advanced to {}", ticket.seq_no);
    }
    Ok(())
}

pub fn handle_ticket_sync(config: &CliConfig, args: TicketsSyncArgs) -> Result<()> {
    let pubkey = ticket_public_key(config)?;
    let response = api_fetch_ticket(config, &args.memory_id)?;

    // Get file info for registration
    let file_path = std::fs::canonicalize(&args.file)
        .with_context(|| format!("failed to get absolute path for {:?}", args.file))?;
    let file_metadata = std::fs::metadata(&args.file)
        .with_context(|| format!("failed to get file metadata for {:?}", args.file))?;
    let file_size = file_metadata.len() as i64;
    let file_name = args
        .file
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown.mv2");
    let machine_id = hostname::get()
        .map(|h| h.to_string_lossy().to_string())
        .unwrap_or_else(|_| "unknown".to_string());
    let signature_bytes = BASE64_STANDARD
        .decode(response.payload.signature.as_bytes())
        .context("ticket signature is not valid base64")?;

    // Extract values from response
    let issuer = response.payload.issuer.clone();
    let seq_no = response.payload.sequence;
    let expires_in = response.payload.expires_in;
    let capacity_bytes = response.payload.capacity_bytes;

    verify_ticket_signature(
        pubkey,
        &args.memory_id,
        &issuer,
        seq_no,
        expires_in,
        capacity_bytes,
        &signature_bytes,
    )
    .context("ticket signature verification failed")?;

    let mut mem = Memvid::open(&args.file)?;
    apply_lock_cli(&mut mem, &args.lock);

    // Check if the file already has this ticket or a newer one
    let current = mem.current_ticket();
    if current.seq_no >= seq_no {
        // Already bound with same or newer ticket
        let capacity = mem.get_capacity();

        // Still register/update file with the dashboard
        let file_path_str = file_path.to_string_lossy();
        let register_request = crate::api::RegisterFileRequest {
            file_name,
            file_path: &file_path_str,
            file_size,
            machine_id: &machine_id,
        };
        if let Some(api_key) = config.api_key.as_deref() {
            if let Err(e) =
                crate::api::register_file(config, &args.memory_id, &register_request, api_key)
            {
                log::warn!("Failed to register file with dashboard: {}", e);
            }
        }

        if args.json {
            let json = json!({
                "issuer": current.issuer,
                "seq_no": current.seq_no,
                "expires_in": current.expires_in_secs,
                "capacity_bytes": if current.capacity_bytes > 0 { Some(current.capacity_bytes) } else { None },
                "memory_id": args.memory_id,
                "verified": current.verified,
                "already_bound": true,
            });
            println!("{}", serde_json::to_string_pretty(&json)?);
        } else {
            let verified_str = if current.verified { "" } else { "" };
            println!(
                "Already bound to memory {} (seq={}, issuer={}{})",
                args.memory_id, current.seq_no, current.issuer, verified_str
            );
            println!(
                "Current capacity: {:.2} GB",
                capacity as f64 / 1024.0 / 1024.0 / 1024.0
            );
        }
        return Ok(());
    }

    // Register file with the dashboard FIRST (before binding locally)
    // This ensures only one file can be bound to each memory
    let file_path_str = file_path.to_string_lossy();
    let register_request = crate::api::RegisterFileRequest {
        file_name,
        file_path: &file_path_str,
        file_size,
        machine_id: &machine_id,
    };
    if let Some(api_key) = config.api_key.as_deref() {
        crate::api::register_file(config, &args.memory_id, &register_request, api_key)
            .context("failed to register file with dashboard - this memory may already be bound to another file")?;
    } else {
        bail!("API key required for binding. Set MEMVID_API_KEY.");
    }

    // Create memory binding first if not already bound
    let binding = MemoryBinding {
        memory_id: *args.memory_id,
        memory_name: issuer.clone(), // Use issuer as name for now
        bound_at: Utc::now(),
        api_url: config.api_url.clone(),
    };

    // Ensure memory is bound before applying signed ticket
    if mem.get_memory_binding().is_none() {
        // Create a temporary ticket for binding (seq-1 to allow signed ticket to apply)
        let temp_ticket =
            Ticket::new(&issuer, seq_no.saturating_sub(1)).expires_in_secs(expires_in);
        mem.bind_memory(binding, temp_ticket)
            .context("failed to bind memory")?;
    }

    // Create and apply signed ticket with cryptographic verification
    let signed_ticket = SignedTicket::new(
        &issuer,
        seq_no,
        expires_in,
        capacity_bytes,
        *args.memory_id,
        signature_bytes,
    );

    mem.apply_signed_ticket(signed_ticket)
        .context("failed to apply signed ticket")?;
    mem.commit()?;

    let cache_entry = CachedTicket {
        memory_id: *args.memory_id,
        issuer: issuer.clone(),
        seq_no,
        expires_in,
        capacity_bytes,
        signature: response.payload.signature.clone(),
    };
    store_cached_ticket(config, &cache_entry)?;

    let capacity = mem.get_capacity();

    if args.json {
        let json = json!({
            "issuer": issuer,
            "seq_no": seq_no,
            "expires_in": expires_in,
            "capacity_bytes": capacity_bytes,
            "memory_id": args.memory_id,
            "request_id": response.request_id,
            "verified": true,
        });
        println!("{}", serde_json::to_string_pretty(&json)?);
    } else {
        println!(
            "Bound to memory {} (seq={}, issuer={}) ✓",
            args.memory_id, seq_no, issuer
        );
        println!(
            "New capacity: {:.2} GB",
            capacity as f64 / 1024.0 / 1024.0 / 1024.0
        );
    }
    Ok(())
}

pub fn handle_ticket_apply(config: &CliConfig, args: TicketsApplyArgs) -> Result<()> {
    if !args.from_api {
        bail!("use --from-api to submit the ticket fetched from the API");
    }
    let cached = load_cached_ticket(config, &args.memory_id)
        .context("no cached ticket available; run `tickets sync` first")?;
    let request = ApiApplyTicketRequest {
        issuer: &cached.issuer,
        seq_no: cached.seq_no,
        expires_in: cached.expires_in,
        capacity_bytes: cached.capacity_bytes,
        signature: &cached.signature,
    };
    let request_id = api_apply_ticket(config, &args.memory_id, &request)?;
    if args.json {
        let json = json!({
            "memory_id": args.memory_id,
            "seq_no": cached.seq_no,
            "request_id": request_id,
        });
        println!("{}", serde_json::to_string_pretty(&json)?);
    } else {
        println!(
            "Submitted ticket seq={} for memory {} (request {})",
            cached.seq_no, args.memory_id, request_id
        );
    }
    Ok(())
}