Repository navigation
Expand file tree
/
Copy pathlock_cmd.rs
More file actions
511 lines (436 loc) · 15.5 KB
/
Copy pathlock_cmd.rs
File metadata and controls
511 lines (436 loc) · 15.5 KB
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
//! Lock command for protecting sessions from deletion.
//!
//! Provides session protection functionality:
//! - Lock sessions to prevent deletion
//! - Unlock sessions
//! - List locked sessions
use anyhow::{Result, bail};
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;
/// Lock CLI command.
#[derive(Debug, Parser)]
pub struct LockCli {
#[command(subcommand)]
pub subcommand: Option<LockSubcommand>,
/// Session ID to lock (if no subcommand)
#[arg()]
pub session_id: Option<String>,
}
/// Lock subcommands.
#[derive(Debug, clap::Subcommand)]
pub enum LockSubcommand {
/// Lock a session
#[command(visible_alias = "protect")]
Add(LockAddArgs),
/// Unlock a session
#[command(visible_aliases = ["unprotect", "rm"])]
Remove(LockRemoveArgs),
/// List locked sessions
#[command(visible_alias = "ls")]
List(LockListArgs),
/// Check if a session is locked
Check(LockCheckArgs),
}
/// Arguments for lock add command.
#[derive(Debug, Parser)]
pub struct LockAddArgs {
/// Session ID(s) to lock
#[arg(required = true)]
pub session_ids: Vec<String>,
/// Reason for locking
#[arg(long, short = 'r')]
pub reason: Option<String>,
}
/// Arguments for lock remove command.
#[derive(Debug, Parser)]
pub struct LockRemoveArgs {
/// Session ID(s) to unlock
#[arg(required = true)]
pub session_ids: Vec<String>,
/// Skip confirmation prompt
#[arg(long, short = 'y')]
pub yes: bool,
}
/// Arguments for lock list command.
#[derive(Debug, Parser)]
pub struct LockListArgs {
/// Output as JSON
#[arg(long)]
pub json: bool,
}
/// Arguments for lock check command.
#[derive(Debug, Parser)]
pub struct LockCheckArgs {
/// Session ID to check
pub session_id: String,
}
/// Lock entry information.
#[derive(Debug, Serialize, Deserialize)]
struct LockEntry {
session_id: String,
locked_at: String,
#[serde(skip_serializing_if = "Option::is_none")]
reason: Option<String>,
}
/// Lock file containing all locked sessions.
#[derive(Debug, Default, Serialize, Deserialize)]
struct LockFile {
version: u32,
locked_sessions: Vec<LockEntry>,
}
/// Validate session ID format (must be valid UUID or 8-char prefix)
fn validate_session_id(session_id: &str) -> Result<()> {
// Check if it's a valid UUID
if uuid::Uuid::parse_str(session_id).is_ok() {
return Ok(());
}
// Check if it's a valid 8-character hex prefix
if session_id.len() == 8 && session_id.chars().all(|c| c.is_ascii_hexdigit()) {
return Ok(());
}
bail!(
"Invalid session ID: '{}'. Expected a full UUID (e.g., '550e8400-e29b-41d4-a716-446655440000') or an 8-character prefix.",
session_id
)
}
/// Get the lock file path.
fn get_lock_file_path() -> PathBuf {
dirs::home_dir()
.map(|h| h.join(".cortex").join("session_locks.json"))
.unwrap_or_else(|| PathBuf::from(".cortex/session_locks.json"))
}
/// Load the lock file.
fn load_lock_file() -> Result<LockFile> {
let path = get_lock_file_path();
if !path.exists() {
return Ok(LockFile {
version: 1,
locked_sessions: Vec::new(),
});
}
let content = std::fs::read_to_string(&path)?;
let lock_file: LockFile = serde_json::from_str(&content)?;
Ok(lock_file)
}
/// Save the lock file.
fn save_lock_file(lock_file: &LockFile) -> Result<()> {
let path = get_lock_file_path();
// Create parent directory if it doesn't exist
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let content = serde_json::to_string_pretty(lock_file)?;
std::fs::write(&path, content)?;
Ok(())
}
/// Check if a session is locked.
pub fn is_session_locked(session_id: &str) -> bool {
match load_lock_file() {
Ok(lock_file) => lock_file.locked_sessions.iter().any(|entry| {
entry.session_id == session_id
|| session_id.starts_with(&entry.session_id[..8.min(entry.session_id.len())])
}),
Err(_) => false,
}
}
impl LockCli {
/// Run the lock command.
pub async fn run(self) -> Result<()> {
match self.subcommand {
Some(LockSubcommand::Add(args)) => run_add(args).await,
Some(LockSubcommand::Remove(args)) => run_remove(args).await,
Some(LockSubcommand::List(args)) => run_list(args).await,
Some(LockSubcommand::Check(args)) => run_check(args).await,
None => {
// Lock the session directly if ID provided
if let Some(session_id) = self.session_id {
run_add(LockAddArgs {
session_ids: vec![session_id],
reason: None,
})
.await
} else {
println!("Session Lock Management");
println!("{}", "=".repeat(50));
println!();
println!("Protect sessions from accidental deletion:");
println!();
println!(" cortex lock <session-id> Lock a session");
println!(" cortex lock add <session-id> Lock a session");
println!(" cortex lock remove <session-id> Unlock a session");
println!(" cortex lock list List locked sessions");
println!(" cortex lock check <session-id> Check if locked");
println!();
println!("Options:");
println!(" --reason <text> Add reason for locking");
println!();
println!("Example:");
println!(" cortex lock abc12345 --reason \"Important conversation\"");
Ok(())
}
}
}
}
}
async fn run_add(args: LockAddArgs) -> Result<()> {
// Validate all session IDs first (Issue #3696)
for session_id in &args.session_ids {
validate_session_id(session_id)?;
}
let mut lock_file = load_lock_file()?;
let timestamp = chrono::Utc::now().to_rfc3339();
// Get existing locked session IDs
let existing_ids: HashSet<String> = lock_file
.locked_sessions
.iter()
.map(|e| e.session_id.clone())
.collect();
let mut added = 0;
let mut skipped = 0;
for session_id in &args.session_ids {
if existing_ids.contains(session_id) {
println!("Session '{}' is already locked.", session_id);
skipped += 1;
} else {
lock_file.locked_sessions.push(LockEntry {
session_id: session_id.clone(),
locked_at: timestamp.clone(),
reason: args.reason.clone(),
});
added += 1;
}
}
if added > 0 {
save_lock_file(&lock_file)?;
println!(
"Locked {} session(s){}.",
added,
if skipped > 0 {
format!(" ({} already locked)", skipped)
} else {
String::new()
}
);
} else if skipped > 0 {
println!("All sessions were already locked.");
}
Ok(())
}
async fn run_remove(args: LockRemoveArgs) -> Result<()> {
let mut lock_file = load_lock_file()?;
// Find sessions to unlock
let session_ids: HashSet<String> = args.session_ids.iter().cloned().collect();
let to_remove: Vec<_> = lock_file
.locked_sessions
.iter()
.filter(|e| session_ids.contains(&e.session_id))
.map(|e| e.session_id.clone())
.collect();
if to_remove.is_empty() {
bail!("None of the specified sessions are locked.");
}
if !args.yes {
println!(
"Are you sure you want to unlock {} session(s)? (y/N)",
to_remove.len()
);
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if !input.trim().eq_ignore_ascii_case("y") {
println!("Aborted.");
return Ok(());
}
}
lock_file
.locked_sessions
.retain(|e| !session_ids.contains(&e.session_id));
save_lock_file(&lock_file)?;
println!("Unlocked {} session(s).", to_remove.len());
Ok(())
}
async fn run_list(args: LockListArgs) -> Result<()> {
let lock_file = load_lock_file()?;
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&lock_file.locked_sessions)?
);
} else if lock_file.locked_sessions.is_empty() {
println!("No sessions are locked.");
println!();
println!("Use 'cortex lock <session-id>' to protect a session.");
} else {
println!("Locked Sessions:");
println!("{}", "-".repeat(60));
for entry in &lock_file.locked_sessions {
let short_id = &entry.session_id[..8.min(entry.session_id.len())];
println!(" {} - locked at {}", short_id, entry.locked_at);
if let Some(ref reason) = entry.reason {
println!(" Reason: {}", reason);
}
}
println!();
println!(
"Total: {} locked session(s)",
lock_file.locked_sessions.len()
);
}
Ok(())
}
async fn run_check(args: LockCheckArgs) -> Result<()> {
let lock_file = load_lock_file()?;
let is_locked = lock_file.locked_sessions.iter().any(|e| {
e.session_id == args.session_id
|| args
.session_id
.starts_with(&e.session_id[..8.min(e.session_id.len())])
});
if is_locked {
println!("Session '{}' is LOCKED.", args.session_id);
// Find and print the reason if any
if let Some(entry) = lock_file.locked_sessions.iter().find(|e| {
e.session_id == args.session_id
|| args
.session_id
.starts_with(&e.session_id[..8.min(e.session_id.len())])
}) && let Some(ref reason) = entry.reason
{
println!("Reason: {}", reason);
}
std::process::exit(0);
} else {
println!("Session '{}' is not locked.", args.session_id);
std::process::exit(1);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lock_entry_serialization_with_reason() {
let entry = LockEntry {
session_id: "abc123def456".to_string(),
locked_at: "2024-01-15T10:30:00Z".to_string(),
reason: Some("Important session".to_string()),
};
let json = serde_json::to_string(&entry).expect("Failed to serialize LockEntry");
let parsed: LockEntry =
serde_json::from_str(&json).expect("Failed to deserialize LockEntry");
assert_eq!(parsed.session_id, "abc123def456");
assert_eq!(parsed.locked_at, "2024-01-15T10:30:00Z");
assert_eq!(parsed.reason, Some("Important session".to_string()));
}
#[test]
fn test_lock_entry_serialization_without_reason() {
let entry = LockEntry {
session_id: "xyz789".to_string(),
locked_at: "2024-02-20T15:45:00Z".to_string(),
reason: None,
};
let json = serde_json::to_string(&entry).expect("Failed to serialize LockEntry");
// Verify reason field is omitted when None (due to skip_serializing_if)
assert!(!json.contains("reason"));
let parsed: LockEntry =
serde_json::from_str(&json).expect("Failed to deserialize LockEntry");
assert_eq!(parsed.session_id, "xyz789");
assert_eq!(parsed.locked_at, "2024-02-20T15:45:00Z");
assert_eq!(parsed.reason, None);
}
#[test]
fn test_lock_entry_deserialization_from_json() {
let json =
r#"{"session_id":"test123","locked_at":"2024-03-01T00:00:00Z","reason":"Test reason"}"#;
let entry: LockEntry =
serde_json::from_str(json).expect("Failed to deserialize LockEntry from JSON");
assert_eq!(entry.session_id, "test123");
assert_eq!(entry.locked_at, "2024-03-01T00:00:00Z");
assert_eq!(entry.reason, Some("Test reason".to_string()));
}
#[test]
fn test_lock_entry_deserialization_without_reason_field() {
let json = r#"{"session_id":"no_reason_session","locked_at":"2024-04-10T12:00:00Z"}"#;
let entry: LockEntry =
serde_json::from_str(json).expect("Failed to deserialize LockEntry without reason");
assert_eq!(entry.session_id, "no_reason_session");
assert_eq!(entry.locked_at, "2024-04-10T12:00:00Z");
assert_eq!(entry.reason, None);
}
#[test]
fn test_lock_file_default() {
let lock_file = LockFile::default();
assert_eq!(lock_file.version, 0);
assert!(lock_file.locked_sessions.is_empty());
}
#[test]
fn test_lock_file_serialization_empty() {
let lock_file = LockFile {
version: 1,
locked_sessions: Vec::new(),
};
let json = serde_json::to_string(&lock_file).expect("Failed to serialize empty LockFile");
let parsed: LockFile =
serde_json::from_str(&json).expect("Failed to deserialize empty LockFile");
assert_eq!(parsed.version, 1);
assert!(parsed.locked_sessions.is_empty());
}
#[test]
fn test_lock_file_serialization_with_entries() {
let lock_file = LockFile {
version: 1,
locked_sessions: vec![
LockEntry {
session_id: "session_one".to_string(),
locked_at: "2024-01-01T00:00:00Z".to_string(),
reason: Some("First session".to_string()),
},
LockEntry {
session_id: "session_two".to_string(),
locked_at: "2024-01-02T00:00:00Z".to_string(),
reason: None,
},
],
};
let json =
serde_json::to_string(&lock_file).expect("Failed to serialize LockFile with entries");
let parsed: LockFile =
serde_json::from_str(&json).expect("Failed to deserialize LockFile with entries");
assert_eq!(parsed.version, 1);
assert_eq!(parsed.locked_sessions.len(), 2);
assert_eq!(parsed.locked_sessions[0].session_id, "session_one");
assert_eq!(
parsed.locked_sessions[0].reason,
Some("First session".to_string())
);
assert_eq!(parsed.locked_sessions[1].session_id, "session_two");
assert_eq!(parsed.locked_sessions[1].reason, None);
}
#[test]
fn test_lock_file_deserialization_from_json() {
let json = r#"{
"version": 2,
"locked_sessions": [
{"session_id": "sess_abc", "locked_at": "2024-05-15T08:00:00Z", "reason": "Production"}
]
}"#;
let lock_file: LockFile =
serde_json::from_str(json).expect("Failed to deserialize LockFile from JSON");
assert_eq!(lock_file.version, 2);
assert_eq!(lock_file.locked_sessions.len(), 1);
assert_eq!(lock_file.locked_sessions[0].session_id, "sess_abc");
assert_eq!(
lock_file.locked_sessions[0].reason,
Some("Production".to_string())
);
}
#[test]
fn test_get_lock_file_path_returns_valid_path() {
let path = get_lock_file_path();
// Path should end with session_locks.json
assert!(path.ends_with("session_locks.json"));
// Path should include .cortex directory
let path_str = path.to_string_lossy();
assert!(path_str.contains(".cortex"));
}
}