ito-core 0.1.31

Core functionality and business logic for Ito
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
//! Rules under the `repository/*` namespace.
//!
//! Both rules gate on `repository.mode == sqlite`. When the project uses
//! the filesystem-backed repository, neither rule fires.
//!
//! - `repository/sqlite-db-path-set` — the configured `db_path` must
//!   resolve inside the project root and have an existing parent
//!   directory.
//! - `repository/sqlite-db-not-committed` — the database file MUST NOT
//!   be tracked by git AND SHOULD be covered by a `.gitignore` entry.

use std::path::{Path, PathBuf};

use ito_config::types::{ItoConfig, RepositoryPersistenceMode};

use crate::errors::CoreError;
use crate::process::{ProcessRequest, ProcessRunner};
use crate::validate::{ValidationIssue, error, warning, with_metadata, with_rule_id};

use super::rule::{Rule, RuleContext, RuleId, RuleSeverity};

const SQLITE_DB_PATH_SET_ID: RuleId = RuleId::new("repository/sqlite-db-path-set");
const SQLITE_DB_NOT_COMMITTED_ID: RuleId = RuleId::new("repository/sqlite-db-not-committed");

/// True when the repository persistence mode is sqlite.
fn mode_is_sqlite(config: &ItoConfig) -> bool {
    match config.repository.mode {
        RepositoryPersistenceMode::Sqlite => true,
        RepositoryPersistenceMode::Filesystem => false,
    }
}

/// Return the configured sqlite db path resolved against `project_root`.
///
/// Returns `None` when the path is unset or empty.
fn resolve_db_path(config: &ItoConfig, project_root: &Path) -> Option<PathBuf> {
    let raw = config.repository.sqlite.db_path.as_ref()?.trim();
    if raw.is_empty() {
        return None;
    }
    let path = PathBuf::from(raw);
    if path.is_absolute() {
        Some(path)
    } else {
        Some(project_root.join(path))
    }
}

/// True when `path` is inside `root` after both lexical normalization
/// (collapsing `..` and `.` components) AND, when possible, OS-level
/// canonicalization (resolving symlinks).
///
/// `Path::starts_with` is component-based and does NOT resolve `..`, so a
/// fallback that uses it directly would let `<root>/.ito/state/../../etc/passwd`
/// claim to be "inside the root". The lexical normalisation below
/// closes that hole when the file does not yet exist (so
/// `Path::canonicalize` is unavailable).
fn path_inside_root(path: &Path, root: &Path) -> bool {
    let canonical_root = root.canonicalize().ok();

    // Best case: canonicalize both. Resolves symlinks too.
    if let Some(canonical_root) = canonical_root.as_ref()
        && let Ok(canonical_path) = path.canonicalize()
    {
        return canonical_path.starts_with(canonical_root);
    }

    // File doesn't exist yet (or root cannot be canonicalised). Lexically
    // normalise `path`, then compare against the canonicalised root (when
    // available) AND the original root — handles macOS where `/var` →
    // `/private/var` so a path under `/var/...` would not literally
    // start with `/private/var`.
    let normalized = lexical_normalize(path);
    if let Some(canonical_root) = canonical_root.as_ref()
        && normalized.starts_with(canonical_root)
    {
        return true;
    }
    normalized.starts_with(root)
}

/// Collapse `.` and `..` components without touching the filesystem.
///
/// Unlike `Path::canonicalize`, this is purely lexical — `..` after a
/// regular component pops the previous component; `..` after `..` or at
/// the start is kept (we cannot know what the parent of an unknown root
/// is).
fn lexical_normalize(path: &Path) -> PathBuf {
    use std::path::Component;
    let mut out: Vec<Component<'_>> = Vec::new();
    for component in path.components() {
        match component {
            Component::ParentDir => {
                let pops_prior_normal = match out.last() {
                    Some(Component::Normal(_)) => true,
                    Some(_) | None => false,
                };
                if pops_prior_normal {
                    out.pop();
                } else {
                    out.push(component);
                }
            }
            Component::CurDir => {}
            Component::Prefix(_) | Component::RootDir | Component::Normal(_) => {
                out.push(component);
            }
        }
    }
    out.iter().collect()
}

// ── repository/sqlite-db-path-set ────────────────────────────────────────

/// `repository/sqlite-db-path-set` — sqlite db_path must be set and
/// resolvable inside the project root.
pub(crate) struct SqliteDbPathSetRule;

impl Rule for SqliteDbPathSetRule {
    fn id(&self) -> RuleId {
        SQLITE_DB_PATH_SET_ID
    }

    fn severity(&self) -> RuleSeverity {
        RuleSeverity::Error
    }

    fn description(&self) -> &'static str {
        "SQLite db_path is set, resolves inside the project root, and has an existing parent."
    }

    fn gate(&self) -> Option<&'static str> {
        Some("repository.mode == sqlite")
    }

    fn is_active(&self, config: &ItoConfig) -> bool {
        mode_is_sqlite(config)
    }

    fn check(&self, ctx: &RuleContext<'_>) -> Result<Vec<ValidationIssue>, CoreError> {
        let Some(db_path) = resolve_db_path(ctx.config, ctx.project_root) else {
            let issue = error(
                ".ito/config.json",
                "`repository.sqlite.db_path` is empty or unset while `repository.mode = \"sqlite\"`. \
                 The runtime cannot open a database without a path.",
            );
            let issue = with_rule_id(issue, SQLITE_DB_PATH_SET_ID.as_str());
            let issue = with_metadata(
                issue,
                serde_json::json!({
                    "fix": "Set `repository.sqlite.db_path` to a project-relative path under \
                            `.ito/state/`, e.g. `.ito/state/ito.db`.",
                    "config_key": "repository.sqlite.db_path",
                }),
            );
            return Ok(vec![issue]);
        };

        if !path_inside_root(&db_path, ctx.project_root) {
            let issue = error(
                ".ito/config.json",
                format!(
                    "`repository.sqlite.db_path = \"{path}\"` resolves outside the project root \
                     (`{root}`). The repository runtime confines persistence to the project tree.",
                    path = db_path.display(),
                    root = ctx.project_root.display(),
                ),
            );
            let issue = with_rule_id(issue, SQLITE_DB_PATH_SET_ID.as_str());
            let issue = with_metadata(
                issue,
                serde_json::json!({
                    "fix": "Use a project-relative path (e.g. `.ito/state/ito.db`).",
                    "resolved": db_path.to_string_lossy(),
                    "project_root": ctx.project_root.to_string_lossy(),
                }),
            );
            return Ok(vec![issue]);
        }

        let mut issues = Vec::new();
        if let Some(parent) = db_path.parent()
            && !parent.exists()
        {
            let issue = warning(
                ".ito/config.json",
                format!(
                    "`repository.sqlite.db_path` parent directory does not exist: `{parent}`. \
                     SQLite cannot create parent directories on its own; the database open \
                     will fail at runtime if the directory is still missing.",
                    parent = parent.display(),
                ),
            );
            let issue = with_rule_id(issue, SQLITE_DB_PATH_SET_ID.as_str());
            issues.push(with_metadata(
                issue,
                serde_json::json!({
                    "fix": format!("Create the directory: `mkdir -p {}`", parent.display()),
                    "missing_parent": parent.to_string_lossy(),
                }),
            ));
        }

        Ok(issues)
    }
}

// ── repository/sqlite-db-not-committed ───────────────────────────────────

/// `repository/sqlite-db-not-committed` — db file must not be tracked by
/// git AND should be covered by `.gitignore`.
pub(crate) struct SqliteDbNotCommittedRule;

impl Rule for SqliteDbNotCommittedRule {
    fn id(&self) -> RuleId {
        SQLITE_DB_NOT_COMMITTED_ID
    }

    fn severity(&self) -> RuleSeverity {
        RuleSeverity::Error
    }

    fn description(&self) -> &'static str {
        "SQLite database file is not tracked by git and is covered by `.gitignore`."
    }

    fn gate(&self) -> Option<&'static str> {
        Some("repository.mode == sqlite")
    }

    fn is_active(&self, config: &ItoConfig) -> bool {
        mode_is_sqlite(config)
    }

    fn check(&self, ctx: &RuleContext<'_>) -> Result<Vec<ValidationIssue>, CoreError> {
        let Some(db_path) = resolve_db_path(ctx.config, ctx.project_root) else {
            // Companion rule `sqlite-db-path-set` reports the missing path.
            return Ok(Vec::new());
        };

        let rel_str = match db_path.strip_prefix(ctx.project_root) {
            Ok(rel) => rel.to_string_lossy().into_owned(),
            Err(_) => db_path.to_string_lossy().into_owned(),
        };

        if git_tracks_path(ctx.runner, ctx.project_root, &rel_str)? {
            let issue = error(
                rel_str.clone(),
                format!(
                    "SQLite database `{rel_str}` is currently tracked by git. \
                     Committing the live database leaks every entry through history \
                     and corrupts the file across machine clones.",
                ),
            );
            let issue = with_rule_id(issue, SQLITE_DB_NOT_COMMITTED_ID.as_str());
            let issue = with_metadata(
                issue,
                serde_json::json!({
                    "fix": format!(
                        "Untrack: `git rm --cached {rel_str}`. \
                         Then ensure `.gitignore` covers it (e.g. `{rel_str}` or \
                         `{parent}/*.db`).",
                        parent = std::path::Path::new(&rel_str)
                            .parent()
                            .map(std::path::Path::to_string_lossy)
                            .unwrap_or_default(),
                    ),
                    "untrack_command": format!("git rm --cached {rel_str}"),
                    "path": rel_str,
                }),
            );
            return Ok(vec![issue]);
        }

        if !git_check_ignore(ctx.runner, ctx.project_root, &rel_str)? {
            let issue = warning(
                ".gitignore",
                format!(
                    "SQLite database `{rel_str}` is not currently tracked by git but is \
                     also not covered by `.gitignore`. A future `git add .` would \
                     accidentally commit it.",
                ),
            );
            let issue = with_rule_id(issue, SQLITE_DB_NOT_COMMITTED_ID.as_str());
            return Ok(vec![with_metadata(
                issue,
                serde_json::json!({
                    "fix": format!("Append `{rel_str}` (or a glob like `*.db`) to `.gitignore`."),
                    "path": rel_str,
                }),
            )]);
        }

        Ok(Vec::new())
    }
}

/// Return `true` when `rel_path` is currently tracked by git.
///
/// Implementation: `git ls-files --error-unmatch <path>` exits 0 only when
/// the path is in the index; non-zero means untracked or missing.
fn git_tracks_path(
    runner: &dyn ProcessRunner,
    project_root: &Path,
    rel_path: &str,
) -> Result<bool, CoreError> {
    let request = ProcessRequest::new("git")
        .args(["ls-files", "--error-unmatch", "--", rel_path])
        .current_dir(project_root);

    let output = runner.run(&request).map_err(|err| {
        CoreError::process(format!(
            "Cannot run `git ls-files --error-unmatch {rel_path}`.\n\
             Why: {err}\n\
             Fix: ensure git is installed and `{root}` is a git repository.",
            root = project_root.display(),
        ))
    })?;
    Ok(output.success)
}

/// Return `true` when `rel_path` is matched by a `.gitignore` rule.
///
/// Implementation: `git check-ignore -q <path>` exits 0 when the path is
/// ignored, 1 when it is not, and 128+ on error. We treat error exits as
/// "unknown" and conservatively return `false` so the rule warns rather
/// than silently passes.
fn git_check_ignore(
    runner: &dyn ProcessRunner,
    project_root: &Path,
    rel_path: &str,
) -> Result<bool, CoreError> {
    let request = ProcessRequest::new("git")
        .args(["check-ignore", "-q", "--", rel_path])
        .current_dir(project_root);

    let output = runner.run(&request).map_err(|err| {
        CoreError::process(format!(
            "Cannot run `git check-ignore {rel_path}`.\n\
             Why: {err}\n\
             Fix: ensure git is installed and `{root}` is a git repository.",
            root = project_root.display(),
        ))
    })?;
    Ok(output.success)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::process::{ProcessExecutionError, ProcessOutput};
    use crate::validate_repo::staged::StagedFiles;
    use ito_config::types::{ItoConfig, RepositoryRuntimeConfig, RepositorySqliteConfig};
    use std::cell::RefCell;
    use std::time::Duration;
    use tempfile::TempDir;

    /// Test runner that returns canned exit codes per `(program, args[0..N])` prefix.
    ///
    /// The first matching rule wins; falls back to a default if no rule matches.
    struct ScriptedRunner {
        rules: Vec<(Vec<&'static str>, ProcessOutput)>,
        default: ProcessOutput,
        seen: RefCell<Vec<ProcessRequest>>,
    }

    fn ok_output(success: bool, exit_code: i32) -> ProcessOutput {
        ProcessOutput {
            exit_code,
            success,
            stdout: String::new(),
            stderr: String::new(),
            timed_out: false,
        }
    }

    impl ScriptedRunner {
        fn new(default: ProcessOutput) -> Self {
            Self {
                rules: Vec::new(),
                default,
                seen: RefCell::new(Vec::new()),
            }
        }

        fn with_rule(mut self, args_prefix: &[&'static str], output: ProcessOutput) -> Self {
            self.rules.push((args_prefix.to_vec(), output));
            self
        }
    }

    impl ProcessRunner for ScriptedRunner {
        fn run(&self, request: &ProcessRequest) -> Result<ProcessOutput, ProcessExecutionError> {
            self.seen.borrow_mut().push(request.clone());
            for (prefix, output) in &self.rules {
                if request.args.len() >= prefix.len()
                    && request.args.iter().zip(prefix.iter()).all(|(a, b)| a == b)
                {
                    return Ok(output.clone());
                }
            }
            Ok(self.default.clone())
        }
        fn run_with_timeout(
            &self,
            request: &ProcessRequest,
            _timeout: Duration,
        ) -> Result<ProcessOutput, ProcessExecutionError> {
            self.run(request)
        }
    }

    fn config(mode: RepositoryPersistenceMode, db_path: Option<&str>) -> ItoConfig {
        ItoConfig {
            repository: RepositoryRuntimeConfig {
                mode,
                sqlite: RepositorySqliteConfig {
                    db_path: db_path.map(str::to_string),
                },
            },
            ..ItoConfig::default()
        }
    }

    // ── activation ───────────────────────────────────────────────────────

    #[test]
    fn rules_inactive_in_filesystem_mode() {
        let cfg = config(
            RepositoryPersistenceMode::Filesystem,
            Some(".ito/state/ito.db"),
        );
        assert!(!SqliteDbPathSetRule.is_active(&cfg));
        assert!(!SqliteDbNotCommittedRule.is_active(&cfg));
    }

    #[test]
    fn rules_active_in_sqlite_mode() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        assert!(SqliteDbPathSetRule.is_active(&cfg));
        assert!(SqliteDbNotCommittedRule.is_active(&cfg));
    }

    // ── repository/sqlite-db-path-set ────────────────────────────────────

    #[test]
    fn sqlite_db_path_set_errors_when_path_unset() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, None);
        let tmp = TempDir::new().unwrap();
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbPathSetRule.check(&ctx).unwrap();
        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].level, "ERROR");
        assert!(issues[0].message.contains("empty or unset"));
    }

    #[test]
    fn sqlite_db_path_set_errors_when_path_outside_project_root() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some("/var/tmp/ito.db"));
        let tmp = TempDir::new().unwrap();
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbPathSetRule.check(&ctx).unwrap();
        assert_eq!(issues.len(), 1);
        assert!(issues[0].message.contains("outside the project root"));
    }

    #[test]
    fn sqlite_db_path_set_errors_when_path_escapes_root_via_dotdot() {
        // Regression test for a `..` traversal bypass in path_inside_root:
        // `Path::starts_with` is component-based and does not resolve `..`,
        // so a path of `.ito/state/../../../etc/passwd` could appear to
        // "start with" the project root while actually escaping it.
        let cfg = config(
            RepositoryPersistenceMode::Sqlite,
            Some(".ito/state/../../../etc/passwd"),
        );
        let tmp = TempDir::new().unwrap();
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbPathSetRule.check(&ctx).unwrap();
        assert_eq!(
            issues.len(),
            1,
            "expected one error for `..` traversal escape; got {issues:?}",
        );
        assert!(
            issues[0].message.contains("outside the project root"),
            "expected outside-root message; got: {}",
            issues[0].message,
        );
    }

    #[test]
    fn sqlite_db_path_set_warns_when_parent_directory_missing() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        let tmp = TempDir::new().unwrap();
        // Don't create `.ito/state/`.
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbPathSetRule.check(&ctx).unwrap();
        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].level, "WARNING");
        assert!(issues[0].message.contains("parent directory"));
    }

    #[test]
    fn sqlite_db_path_set_passes_when_parent_exists() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        let tmp = TempDir::new().unwrap();
        std::fs::create_dir_all(tmp.path().join(".ito/state")).unwrap();
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbPathSetRule.check(&ctx).unwrap();
        assert!(issues.is_empty(), "expected no issues; got {issues:?}");
    }

    // ── repository/sqlite-db-not-committed ───────────────────────────────

    #[test]
    fn sqlite_db_not_committed_errors_when_file_is_tracked() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        let tmp = TempDir::new().unwrap();
        // ls-files --error-unmatch returns success → tracked.
        let runner = ScriptedRunner::new(ok_output(false, 1))
            .with_rule(&["ls-files", "--error-unmatch"], ok_output(true, 0));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbNotCommittedRule.check(&ctx).unwrap();
        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].level, "ERROR");
        assert!(issues[0].message.contains("tracked by git"));
        let metadata = issues[0].metadata.as_ref().expect("metadata");
        let untrack = metadata
            .get("untrack_command")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        assert!(untrack.starts_with("git rm --cached"));
    }

    #[test]
    fn sqlite_db_not_committed_warns_when_untracked_and_not_ignored() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        let tmp = TempDir::new().unwrap();
        // ls-files --error-unmatch fails (untracked); check-ignore fails (not ignored).
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbNotCommittedRule.check(&ctx).unwrap();
        assert_eq!(issues.len(), 1);
        assert_eq!(issues[0].level, "WARNING");
        assert!(issues[0].message.contains("not covered by `.gitignore`"));
    }

    #[test]
    fn sqlite_db_not_committed_passes_when_untracked_and_ignored() {
        let cfg = config(RepositoryPersistenceMode::Sqlite, Some(".ito/state/ito.db"));
        let tmp = TempDir::new().unwrap();
        // ls-files fails (untracked); check-ignore succeeds (ignored).
        let runner = ScriptedRunner::new(ok_output(false, 1))
            .with_rule(&["check-ignore"], ok_output(true, 0));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbNotCommittedRule.check(&ctx).unwrap();
        assert!(issues.is_empty());
    }

    #[test]
    fn sqlite_db_not_committed_silent_when_path_unset() {
        // Companion rule `sqlite-db-path-set` reports the missing path.
        let cfg = config(RepositoryPersistenceMode::Sqlite, None);
        let tmp = TempDir::new().unwrap();
        let runner = ScriptedRunner::new(ok_output(false, 1));
        let staged = StagedFiles::empty();
        let ctx = RuleContext::new(&cfg, tmp.path(), &staged, &runner);

        let issues = SqliteDbNotCommittedRule.check(&ctx).unwrap();
        assert!(issues.is_empty());
    }
}