cursor-helper 0.2.2

CLI helper for Cursor IDE operations not exposed in the UI
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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Global storage operations
//!
//! Handles updates to ~/Library/Application Support/Cursor/User/globalStorage/storage.json

use anyhow::{Context, Result};
use rusqlite::{params, Connection};
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Update workspace references in storage.json
///
/// This updates:
/// - backupWorkspaces.folders[].folderUri
/// - profileAssociations.workspaces (key rename)
pub fn update_storage_json<P: AsRef<Path>>(
    storage_path: P,
    old_uri: &str,
    new_uri: &str,
    dry_run: bool,
) -> Result<bool> {
    let storage_path = storage_path.as_ref();

    if !storage_path.exists() {
        return Ok(false);
    }

    let content = fs::read_to_string(storage_path)
        .with_context(|| format!("Failed to read: {}", storage_path.display()))?;

    let mut json: Value = serde_json::from_str(&content).context("Failed to parse storage.json")?;

    // Update backupWorkspaces.folders[].folderUri
    let folders_modified = json
        .get_mut("backupWorkspaces")
        .and_then(|b| b.get_mut("folders"))
        .and_then(|f| f.as_array_mut())
        .map(|arr| {
            arr.iter_mut()
                .filter_map(|folder| folder.get_mut("folderUri"))
                .filter(|uri| uri.as_str() == Some(old_uri))
                .fold(false, |_, uri| {
                    *uri = Value::String(new_uri.to_string());
                    true
                })
        })
        .unwrap_or(false);

    // Update profileAssociations.workspaces (rename key)
    let assoc_modified = json
        .get_mut("profileAssociations")
        .and_then(|a| a.get_mut("workspaces"))
        .and_then(|w| w.as_object_mut())
        .and_then(|obj| obj.remove(old_uri).map(|v| (obj, v)))
        .map(|(obj, value)| {
            obj.insert(new_uri.to_string(), value);
            true
        })
        .unwrap_or(false);

    let modified = folders_modified || assoc_modified;

    if modified && !dry_run {
        let new_content = serde_json::to_string_pretty(&json)?;
        fs::write(storage_path, new_content)
            .with_context(|| format!("Failed to write: {}", storage_path.display()))?;
    }

    Ok(modified)
}

/// Update workspace references embedded in global `state.vscdb`.
///
/// Cursor stores several workspace mappings and references in the global database:
/// - `ItemTable.value` / `ItemTable.key`
/// - `cursorDiskKV.key` / `cursorDiskKV.value`
///
/// Updates any exact path/hash strings from old values to new ones, which
/// helps keep move/copy operations from leaving stale workspace IDs behind.
#[allow(clippy::too_many_arguments)]
pub fn update_global_state_db<P: AsRef<Path>>(
    state_db: P,
    old_path: &str,
    new_path: &str,
    old_uri: &str,
    new_uri: &str,
    old_workspace_hash: &str,
    new_workspace_hash: &str,
    dry_run: bool,
) -> Result<bool> {
    let state_db = state_db.as_ref();

    if !state_db.exists() {
        return Ok(false);
    }

    let conn = Connection::open(state_db)
        .with_context(|| format!("Failed to open global state DB: {}", state_db.display()))?;

    let mut modified = false;

    // Replace path, URI, and workspace hash references in all known text columns.
    // This is intentionally conservative to avoid schema-specific assumptions and
    // avoids overlap by applying all replacements through placeholders in-memory.
    let replacements = [
        (old_path, new_path),
        (old_uri, new_uri),
        (old_workspace_hash, new_workspace_hash),
    ];
    let normalized_replacements: Vec<(String, String)> = replacements
        .iter()
        .map(|(old, new)| (old.to_string(), new.to_string()))
        .collect();
    let targets = [
        ("ItemTable", "key"),
        ("ItemTable", "value"),
        ("cursorDiskKV", "key"),
        ("cursorDiskKV", "value"),
    ];

    for (table, column) in targets {
        if !table_exists(&conn, table)? || !column_exists(&conn, table, column)? {
            continue;
        }

        let query = format!("SELECT rowid, {column} FROM {table}");
        let mut stmt = conn.prepare(&query)?;
        let mut rows = stmt.query([])?;
        let mut pending_updates: Vec<(i64, String)> = Vec::new();

        while let Some(row) = rows.next()? {
            let rowid: i64 = row.get(0)?;
            let value: Option<String> = row.get(1)?;

            let Some(value) = value else {
                continue;
            };

            if !has_workspace_scoped_reference(&value, &normalized_replacements) {
                continue;
            }

            let normalized = normalize_text_replacements(&value, &normalized_replacements);

            if normalized == value {
                continue;
            }

            if dry_run {
                modified = true;
                continue;
            }

            pending_updates.push((rowid, normalized));
        }

        for (rowid, normalized) in pending_updates {
            conn.execute(
                &format!("UPDATE {table} SET {column} = ?1 WHERE rowid = ?2"),
                params![normalized, rowid],
            )
            .with_context(|| {
                format!("Failed to update {table}.{column} for row {rowid} in global state DB")
            })?;
            modified = true;
        }
    }

    Ok(modified)
}

fn has_workspace_scoped_reference(value: &str, replacements: &[(String, String)]) -> bool {
    let candidates: Vec<&str> = replacements
        .iter()
        .filter_map(|(old, new)| (old != new).then_some(old.as_str()))
        .collect();

    candidates
        .iter()
        .any(|old| has_safe_suffix_match(value, old))
}

fn has_safe_suffix_match(value: &str, pattern: &str) -> bool {
    if pattern.is_empty() {
        return false;
    }

    let mut offset = 0usize;
    while let Some(pos) = value[offset..].find(pattern) {
        let absolute_pos = offset + pos;
        let suffix = value[absolute_pos + pattern.len()..].chars().next();

        if is_workspace_value_suffix_terminator(suffix) {
            return true;
        }

        offset = absolute_pos + 1;
    }

    false
}

fn is_workspace_value_suffix_terminator(suffix: Option<char>) -> bool {
    match suffix {
        None => true,
        Some(suffix) => {
            !suffix.is_ascii_alphanumeric()
                && suffix != '_'
                && suffix != '-'
                && suffix != '.'
                && suffix != '%'
        }
    }
}

fn normalize_text_replacements(value: &str, replacements: &[(String, String)]) -> String {
    let normalized_replacements: Vec<(&str, &str)> = replacements
        .iter()
        .filter_map(|(old, new)| (old != new).then_some((old.as_str(), new.as_str())))
        .collect();

    if normalized_replacements.is_empty() {
        return value.to_string();
    }

    let mut token_seed = 0usize;
    let mut tokens = Vec::new();

    let mut staged = value.to_string();
    for (old, _) in &normalized_replacements {
        let mut token = format!("__CURSOR_HELPER_REPLACE_TOKEN_{token_seed}__");
        while staged.contains(&token) || tokens.iter().any(|(old_token, _)| old_token == &token) {
            token_seed += 1;
            token = format!("__CURSOR_HELPER_REPLACE_TOKEN_{token_seed}__");
        }

        staged = replace_workspace_scoped_matches(&staged, old, &token);
        tokens.push((token, *old));
        token_seed += 1;
    }

    let mut normalized = staged;
    for (token, old_value) in tokens {
        if let Some((_, new_value)) = normalized_replacements
            .iter()
            .find(|(old, _)| old == &old_value)
        {
            normalized = normalized.replace(&token, new_value);
        }
    }

    normalized
}

fn replace_workspace_scoped_matches(value: &str, pattern: &str, replacement: &str) -> String {
    if pattern.is_empty() {
        return value.to_string();
    }

    let mut offset = 0usize;
    let mut normalized = String::with_capacity(value.len());

    while let Some(pos) = value[offset..].find(pattern) {
        let absolute_pos = offset + pos;

        normalized.push_str(&value[offset..absolute_pos]);

        let next_offset = absolute_pos + pattern.len();
        let suffix = value[next_offset..].chars().next();
        if is_workspace_value_suffix_terminator(suffix) {
            normalized.push_str(replacement);
        } else {
            normalized.push_str(pattern);
        }

        offset = next_offset;
    }

    normalized.push_str(&value[offset..]);
    normalized
}

fn table_exists(conn: &Connection, table: &str) -> Result<bool> {
    let count: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name = ?1",
            params![table],
            |row| row.get(0),
        )
        .context("Failed to query sqlite_master")?;

    Ok(count > 0)
}

fn column_exists(conn: &Connection, table: &str, column: &str) -> Result<bool> {
    let query = format!("PRAGMA table_info({})", table);
    let mut stmt = conn
        .prepare(&query)
        .with_context(|| format!("Failed to read schema for table: {table}"))?;

    let mut rows = stmt
        .query([])
        .with_context(|| format!("Failed to query columns for {table}"))?;
    while let Some(row) = rows.next().context("Failed to iterate table info")? {
        let col_name: String = row.get(1)?;
        if col_name == column {
            return Ok(true);
        }
    }

    Ok(false)
}

/// A simpler representation of storage.json for reading
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct StorageJson {
    #[serde(rename = "backupWorkspaces")]
    pub backup_workspaces: Option<BackupWorkspaces>,

    #[serde(rename = "profileAssociations")]
    pub profile_associations: Option<ProfileAssociations>,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct BackupWorkspaces {
    pub folders: Option<Vec<FolderEntry>>,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct FolderEntry {
    #[serde(rename = "folderUri")]
    pub folder_uri: String,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct ProfileAssociations {
    pub workspaces: Option<HashMap<String, String>>,
}

impl StorageJson {
    /// Read storage.json from a file
    #[allow(dead_code)]
    pub fn read<P: AsRef<Path>>(path: P) -> Result<Self> {
        let content = fs::read_to_string(path.as_ref())
            .with_context(|| format!("Failed to read: {}", path.as_ref().display()))?;
        serde_json::from_str(&content).context("Failed to parse storage.json")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;
    use tempfile::TempDir;

    #[test]
    fn test_update_storage_json() {
        let mut file = NamedTempFile::new().unwrap();
        write!(
            file,
            r#"{{
    "backupWorkspaces": {{
        "folders": [
            {{ "folderUri": "file:///old/path" }},
            {{ "folderUri": "file:///other/path" }}
        ]
    }},
    "profileAssociations": {{
        "workspaces": {{
            "file:///old/path": "__default__profile__"
        }}
    }}
}}"#
        )
        .unwrap();

        let modified =
            update_storage_json(file.path(), "file:///old/path", "file:///new/path", false)
                .unwrap();

        assert!(modified);

        // Verify changes
        let content = fs::read_to_string(file.path()).unwrap();
        assert!(content.contains("file:///new/path"));
        assert!(!content.contains("file:///old/path"));
    }

    #[test]
    fn test_update_global_state_db() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state.vscdb");

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "CREATE TABLE cursorDiskKV (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///old/path",
                "meta:file:///old/path/hash-old",
            ),
        )
        .unwrap();
        conn.execute(
            "INSERT INTO cursorDiskKV(key, value) VALUES (?1, ?2)",
            ("composer:hash-old", "file:///old/path"),
        )
        .unwrap();
        drop(conn);

        let modified = update_global_state_db(
            &db_path,
            "file:///old/path",
            "file:///new/path",
            "file:///old/path",
            "file:///new/path",
            "hash-old",
            "hash-new",
            false,
        )
        .unwrap();

        assert!(modified);

        let conn = Connection::open(&db_path).unwrap();
        let item_key: String = conn
            .query_row(
                "SELECT key FROM ItemTable WHERE value LIKE '%new/path%'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let item_value: String = conn
            .query_row(
                "SELECT value FROM ItemTable WHERE key LIKE '%new/path%'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let disk_key: String = conn
            .query_row(
                "SELECT key FROM cursorDiskKV WHERE value LIKE '%new/path%'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let disk_value: String = conn
            .query_row(
                "SELECT value FROM cursorDiskKV WHERE key LIKE '%hash-new%'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        assert_eq!(item_key, "workspace.key.file:///new/path");
        assert_eq!(item_value, "meta:file:///new/path/hash-new");
        assert_eq!(disk_key, "composer:hash-new");
        assert_eq!(disk_value, "file:///new/path");
    }

    #[test]
    fn test_update_global_state_db_special_chars() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state-special.vscdb");

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///old%20path%2Fwith%25percent/hash_old",
                "meta:file:///old%20path%2Fwith%25percent",
            ),
        )
        .unwrap();
        drop(conn);

        let modified = update_global_state_db(
            &db_path,
            "file:///old%20path%2Fwith%25percent",
            "file:///new%20path%2Fwith%25percent",
            "file:///old%20path%2Fwith%25percent",
            "file:///new%20path%2Fwith%25percent",
            "hash_old",
            "hash-new",
            false,
        )
        .unwrap();

        assert!(modified);

        let conn = Connection::open(&db_path).unwrap();
        let item: (String, String) = conn
            .query_row(
                "SELECT key, value FROM ItemTable WHERE value LIKE 'meta:file:///new%';",
                [],
                |row| Ok((row.get(0).unwrap(), row.get(1).unwrap())),
            )
            .unwrap();

        assert_eq!(
            item.0,
            "workspace.key.file:///new%20path%2Fwith%25percent/hash-new"
        );
        assert_eq!(item.1, "meta:file:///new%20path%2Fwith%25percent");
    }

    #[test]
    fn test_update_global_state_db_path_then_uri_update_is_safe() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state-order.vscdb");

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///home/user/project",
                "file:///home/user/project/hash-old",
            ),
        )
        .unwrap();
        drop(conn);

        let modified = update_global_state_db(
            &db_path,
            "/home/user/project",
            "/home/user/project-copy",
            "file:///home/user/project",
            "file:///home/user/project-copy",
            "hash-old",
            "hash-new",
            false,
        )
        .unwrap();
        assert!(modified);

        let conn = Connection::open(&db_path).unwrap();
        let item_key: String = conn
            .query_row("SELECT key FROM ItemTable", [], |row| row.get(0))
            .unwrap();
        let item_value: String = conn
            .query_row("SELECT value FROM ItemTable", [], |row| row.get(0))
            .unwrap();

        assert_eq!(item_key, "workspace.key.file:///home/user/project-copy");
        assert_eq!(item_value, "file:///home/user/project-copy/hash-new");
        assert!(!item_value.contains("project-copy-copy"));
        assert!(!item_key.contains("project-copy-copy"));
        assert!(modified);
    }

    #[test]
    fn test_update_global_state_db_does_not_modify_workspace_prefix_matches() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state-prefix.vscdb");

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///home/user/project",
                "meta:file:///home/user/project/hash-old",
            ),
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///home/user/projects/foo",
                "meta:file:///home/user/projects/foo/hash-other",
            ),
        )
        .unwrap();
        drop(conn);

        let modified = update_global_state_db(
            &db_path,
            "/home/user/project",
            "/home/user/project-copy",
            "file:///home/user/project",
            "file:///home/user/project-copy",
            "hash-old",
            "hash-new",
            false,
        )
        .unwrap();
        assert!(modified);

        let conn = Connection::open(&db_path).unwrap();
        let updated_value: String = conn
            .query_row(
                "SELECT value FROM ItemTable WHERE key = 'workspace.key.file:///home/user/project-copy'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let untouched_value: String = conn
            .query_row(
                "SELECT value FROM ItemTable WHERE key = 'workspace.key.file:///home/user/projects/foo'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        assert_eq!(
            updated_value,
            "meta:file:///home/user/project-copy/hash-new"
        );
        assert_eq!(
            untouched_value,
            "meta:file:///home/user/projects/foo/hash-other"
        );
    }

    #[test]
    fn test_update_global_state_db_does_not_corrupt_prefix_values_in_same_cell() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("state-prefix-cell.vscdb");

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO ItemTable(key, value) VALUES (?1, ?2)",
            (
                "workspace.key.file:///home/user/project",
                r#"{"active":"file:///home/user/project","other":"file:///home/user/projects/foo","cache":"hash-old"}"#,
            ),
        )
        .unwrap();
        drop(conn);

        let modified = update_global_state_db(
            &db_path,
            "/home/user/project",
            "/home/user/project-copy",
            "file:///home/user/project",
            "file:///home/user/project-copy",
            "hash-old",
            "hash-new",
            false,
        )
        .unwrap();
        assert!(modified);

        let conn = Connection::open(&db_path).unwrap();
        let row_key: String = conn
            .query_row("SELECT key FROM ItemTable", [], |row| row.get(0))
            .unwrap();
        let row_value: String = conn
            .query_row("SELECT value FROM ItemTable", [], |row| row.get(0))
            .unwrap();

        assert_eq!(row_key, "workspace.key.file:///home/user/project-copy");
        assert_eq!(
            row_value,
            r#"{"active":"file:///home/user/project-copy","other":"file:///home/user/projects/foo","cache":"hash-new"}"#
        );
        assert!(!row_value.contains("project-copy-copy"));
    }
}