seshat-cli 0.5.1

CLI commands and TUI for Seshat
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
//! Integration tests for US-012: git-unavailable single-branch fallback.
//!
//! Verifies that the full lifecycle — scan + review + decide + rescan —
//! works in a non-git tmp directory, locking the synthetic single-branch
//! contract end-to-end. The individual freshness-check helper cases
//! (`scan_records_head.rs::scan_project_records_no_commit_when_git_unavailable`,
//! `review_freshness.rs::review_handles_git_unavailable_gracefully`) pin
//! their respective layers; this file pins the merged user-facing flow.
//!
//! AC mapping (PRD §US-012):
//! 1. `detect_branch` returns "main" when no `.git` is found
//!    → [`detect_branch_falls_back_to_main_when_no_git`]
//! 2. Freshness comparisons treat git rev-parse HEAD failure as "no change"
//!    → [`full_lifecycle_works_without_git`] (asserts
//!    `check_branch_freshness == GitUnavailable`)
//! 3. All scan paths set `last_scanned_commit = NULL` when git is unavailable
//!    → [`full_lifecycle_works_without_git`] (sentinel stays NULL across
//!    both initial scan AND rescan)
//! 4. Decision flow operates as on a single-branch project
//!    (`decided_on_branch = "main"`)
//!    → [`full_lifecycle_works_without_git`]
//! 5. Integration test: scan + review + decide + rescan in a non-git tmp dir
//!    → decisions persist, no errors
//!    → [`full_lifecycle_works_without_git`]

use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::{Arc, Mutex};

use seshat_cli::tui::app::{ReviewAction, apply_review_actions};
use seshat_core::{BranchId, DetectionConfig, KnowledgeNode, ScanConfig};
use seshat_detectors::{ProjectContext, aggregate_findings, run_all_detectors};
use seshat_graph::compute_description_hash;
use seshat_scanner::{
    FreshnessCheck, check_branch_freshness, record_branch_scan_complete, scan_project,
};
use seshat_storage::{
    BranchRepository, Database, DecisionRepository, DecisionState, FileIRRepository,
    NodeRepository, SqliteBranchRepository, SqliteDecisionRepository, SqliteFileIRRepository,
    SqliteNodeRepository,
};
use tempfile::tempdir;

/// Drop a small tree of Rust source files into `root` so the detector
/// pipeline has enough material to surface at least one convention.
/// Crucially, this helper does NOT call `git init` — `root` stays a
/// plain directory for the duration of the test.
fn write_rust_sources(root: &Path) {
    let src = root.join("src");
    fs::create_dir_all(&src).expect("create src dir");

    fs::write(
        src.join("lib.rs"),
        r#"
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn subtract(a: i32, b: i32) -> i32 {
    a - b
}

pub fn multiply(a: i32, b: i32) -> i32 {
    a * b
}
"#,
    )
    .expect("write lib.rs");

    fs::write(
        src.join("main.rs"),
        r#"
mod lib;

fn main() {
    let result = lib::add(1, 2);
    println!("{}", result);
}
"#,
    )
    .expect("write main.rs");

    fs::write(
        src.join("errors.rs"),
        r#"
use std::fmt;

#[derive(Debug)]
pub enum AppError {
    NotFound(String),
    InvalidInput(String),
}

impl fmt::Display for AppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AppError::NotFound(msg) => write!(f, "Not found: {msg}"),
            AppError::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
        }
    }
}

impl std::error::Error for AppError {}
"#,
    )
    .expect("write errors.rs");
}

/// Run the production scan + detection + persist pipeline against `repo`,
/// then call the post-scan freshness hook (`record_branch_scan_complete`)
/// the way `seshat scan` and the serve/watcher sync paths do. Returns the
/// auto-detected conventions visible after persist.
///
/// This mirrors `crates/seshat-cli/tests/tui_review_integration.rs::scan_and_get_conventions`
/// but with the post-scan hook layered on so the test actually exercises
/// the AC#3 invariant (`last_scanned_commit = NULL` when git is unavailable).
fn scan_and_persist(repo: &Path) -> Vec<KnowledgeNode> {
    let db_path = repo.join("seshat.db");
    let db = Database::open(&db_path).expect("open DB");

    let branch_id = BranchId::from("main");
    let scan_result = scan_project(repo, &ScanConfig::default(), &db, branch_id.clone())
        .expect("scan must succeed without git");

    let conn = db.connection().clone();
    let file_ir_repo = SqliteFileIRRepository::new(conn.clone());
    let all_files = file_ir_repo
        .get_by_branch(&branch_id)
        .expect("load files for detection");

    let detection_config = DetectionConfig::default();
    let project_context = ProjectContext::from_files(&all_files);
    let detector_results = run_all_detectors(
        &all_files,
        &scan_result.source_map,
        &detection_config,
        &project_context,
        None,
    );
    let all_findings: Vec<seshat_core::ConventionFinding> = detector_results
        .into_iter()
        .flat_map(|dr| dr.findings)
        .collect();

    let aggregated = aggregate_findings(
        &all_findings,
        &detection_config,
        &HashMap::new(),
        chrono::Utc::now().timestamp(),
    );

    seshat_graph::persist_and_index(&conn, &branch_id, &aggregated, &all_findings)
        .expect("persist conventions");

    // Production paths (run_scan, background_sync, fallback_rescan,
    // execute_bulk_rescan) all call this helper after a successful scan.
    // It MUST be a silent no-op when git is unavailable (US-009 AC).
    let branch_repo = SqliteBranchRepository::new(conn.clone());
    record_branch_scan_complete(&branch_repo, repo, &branch_id);

    let node_repo = SqliteNodeRepository::new(conn);
    node_repo
        .find_conventions_by_branch(&branch_id)
        .expect("query conventions")
}

/// AC#1: `detect_branch` returns "main" when no `.git` is found. This is
/// the "synthetic-branch identity" that everything else in this story
/// relies on — without it, decisions would be scoped to whatever weird
/// fallback string `detect_branch` produced.
#[test]
fn detect_branch_falls_back_to_main_when_no_git() {
    let workdir = tempdir().expect("tempdir");
    // Sanity: the temp dir really has no .git anywhere up the tree —
    // tempdir() uses /tmp on macOS/Linux, which is not inside a git repo.
    assert!(
        !workdir.path().join(".git").exists(),
        "fixture must NOT contain a .git directory"
    );

    let branch = seshat_cli::db::detect_branch(workdir.path());
    assert_eq!(
        branch, "main",
        "non-git dir must yield the synthetic 'main'"
    );
}

/// AC#5 (the omnibus integration test): scan + review + decide + rescan
/// in a non-git tmp dir → decisions persist, no errors. Exercises every
/// AC point in one flow:
///
/// - AC#1: `detect_branch` returns "main"
/// - AC#2: `check_branch_freshness` returns `GitUnavailable` → no sync triggered
/// - AC#3: `last_scanned_commit` stays NULL across both the initial scan AND the rescan
/// - AC#4: the decision row's `decided_on_branch` is "main"
/// - AC#5: the decision survives the rescan AND the convention is not re-emitted
#[test]
fn full_lifecycle_works_without_git() {
    let workdir = tempdir().expect("tempdir");
    let repo = workdir.path();
    write_rust_sources(repo);
    assert!(
        !repo.join(".git").exists(),
        "fixture MUST NOT be a git repo for the AC to be meaningful"
    );

    // AC#1: synthetic-branch identity.
    let branch_str = seshat_cli::db::detect_branch(repo);
    assert_eq!(
        branch_str, "main",
        "non-git dir must yield the synthetic 'main' (AC#1)"
    );
    let branch = BranchId::from(branch_str.as_str());

    // ── First scan ──────────────────────────────────────────────────────
    let conventions1 = scan_and_persist(repo);
    assert!(
        !conventions1.is_empty(),
        "scan must surface at least one auto-detected convention even without git"
    );

    // AC#3: sentinel must stay NULL after a successful scan when git is unavailable.
    let db_path = repo.join("seshat.db");
    let db = Database::open(&db_path).expect("reopen DB");
    let conn = db.connection().clone();
    let branch_repo = SqliteBranchRepository::new(conn.clone());
    let sentinel_after_first = branch_repo
        .get_last_scanned_commit(&branch)
        .expect("read sentinel");
    assert_eq!(
        sentinel_after_first, None,
        "last_scanned_commit must stay NULL after scan when git is unavailable (AC#3)"
    );

    // AC#2: the freshness gate must short-circuit to GitUnavailable so neither
    // `seshat serve` nor `seshat review` trigger a sync.
    assert_eq!(
        check_branch_freshness(&branch_repo, repo, &branch),
        FreshnessCheck::GitUnavailable,
        "freshness gate must report GitUnavailable for a non-git dir (AC#2)"
    );

    // ── Decide: confirm the first auto-detected convention via the review action path ──
    let first = &conventions1[0];
    let first_description = first.description.clone();
    let conn_arc: Arc<Mutex<rusqlite::Connection>> = conn.clone();
    let actions = vec![ReviewAction::Confirm {
        node_id: first.id.0,
        description: first_description.clone(),
        examples: Vec::new(),
    }];
    apply_review_actions(&conn_arc, "main", &actions)
        .expect("apply_review_actions must succeed without git");

    // AC#4: the decision row exists, is keyed by description_hash, has
    // state=Approved, and decided_on_branch="main" (the synthetic identity).
    let decision_repo = SqliteDecisionRepository::new(conn_arc.clone());
    let hash = compute_description_hash(&first_description);
    let decision = decision_repo
        .get_by_hash(&hash)
        .expect("get_by_hash should succeed")
        .expect("decision row must exist after Confirm");
    assert_eq!(decision.state, DecisionState::Approved);
    assert_eq!(
        decision.decided_on_branch,
        BranchId::from("main"),
        "decision must be scoped to the synthetic 'main' branch (AC#4)"
    );

    // ── Rescan ─────────────────────────────────────────────────────────
    let conventions2 = scan_and_persist(repo);

    // AC#5: the decision survives the rescan.
    let decision_after = decision_repo
        .get_by_hash(&hash)
        .expect("get_by_hash should succeed post-rescan")
        .expect("decision row must persist across rescans (AC#5)");
    assert_eq!(decision_after.state, DecisionState::Approved);
    assert_eq!(decision_after.decided_on_branch, BranchId::from("main"));

    // AC#5 (cont.): the confirmed convention is NOT re-emitted as auto-detected
    // because `persist_conventions` (US-008) consults the decisions table on
    // every insert. The merge-aware contract holds even with no git history.
    let post_rescan_descriptions: Vec<_> =
        conventions2.iter().map(|c| c.description.clone()).collect();
    assert!(
        !post_rescan_descriptions.contains(&first_description),
        "decided convention must not be re-emitted on rescan (AC#5); got {post_rescan_descriptions:?}"
    );

    // AC#3 (cont.): the sentinel must STILL be NULL after the rescan.
    let sentinel_after_rescan = branch_repo
        .get_last_scanned_commit(&branch)
        .expect("read sentinel post-rescan");
    assert_eq!(
        sentinel_after_rescan, None,
        "last_scanned_commit must stay NULL across rescans without git (AC#3)"
    );
}

/// Defensive guard: `find_conventions_by_branch` queried with the synthetic
/// "main" branch returns rows in a non-git directory. Without this the AC#4
/// "decision flow operates as on a single-branch project" claim is unprovable
/// — every decision would be scoped to "main" but the queries that read
/// decisions/nodes back must agree.
#[test]
fn queries_scoped_to_main_return_results_in_non_git_dir() {
    let workdir = tempdir().expect("tempdir");
    let repo = workdir.path();
    write_rust_sources(repo);
    assert!(!repo.join(".git").exists());

    let conventions = scan_and_persist(repo);
    assert!(!conventions.is_empty());

    let db_path = repo.join("seshat.db");
    let db = Database::open(&db_path).expect("open DB");
    let conn = db.connection().clone();

    // The branch registry contains exactly one branch — "main" — because
    // every scan path registers the active branch via `ensure_branch_exists`
    // (US-003) and `detect_branch` always falls back to "main" without git.
    let branch_repo = SqliteBranchRepository::new(conn.clone());
    let branches = branch_repo.list_branches().expect("list branches");
    assert!(
        branches.contains(&BranchId::from("main")),
        "branches table must contain the synthetic 'main' branch; got {branches:?}"
    );

    // Node lookups scoped to "main" return the auto-detected conventions
    // produced by the scan — proves the same-branch read path works.
    let node_repo = SqliteNodeRepository::new(conn);
    let by_main = node_repo
        .find_conventions_by_branch(&BranchId::from("main"))
        .expect("query nodes scoped to main");
    assert!(
        !by_main.is_empty(),
        "find_conventions_by_branch('main') must return rows in a non-git dir"
    );
}

// ── T18: non-git → git transition ───────────────────────────────────────

/// T18: a project may start as a plain directory (synthetic "main"
/// branch, no sentinel), then later become a git repo. Decisions
/// recorded under the synthetic branch must survive the transition
/// and dedup the same auto-detected convention after `git init`.
#[test]
fn decisions_survive_non_git_to_git_transition() {
    use seshat_storage::DecisionRepository;
    use std::process::{Command, Stdio};

    fn run_git(args: &[&str], cwd: &std::path::Path) {
        let out = Command::new("git")
            .args(args)
            .current_dir(cwd)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .unwrap_or_else(|e| panic!("git {args:?}: {e}"));
        assert!(
            out.status.success(),
            "git {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    let workdir = tempdir().expect("tempdir");
    let repo = workdir.path();
    write_rust_sources(repo);
    assert!(!repo.join(".git").exists(), "fixture starts non-git");

    // Initial scan in non-git mode → synthetic "main" branch.
    let conventions = scan_and_persist(repo);
    assert!(!conventions.is_empty());
    let target = &conventions[0];
    let target_description = target.description.clone();
    let target_node_id = target.id.0;
    let target_hash = compute_description_hash(&target_description);

    let db_path = repo.join("seshat.db");
    let db = Database::open(&db_path).expect("reopen DB");
    let conn: Arc<Mutex<rusqlite::Connection>> = db.connection().clone();

    // Approve the convention while still non-git.
    apply_review_actions(
        &conn,
        "main",
        &[ReviewAction::Confirm {
            node_id: target_node_id,
            description: target_description.clone(),
            examples: Vec::new(),
        }],
    )
    .expect("apply Confirm in non-git mode");

    let dec_repo = SqliteDecisionRepository::new(conn.clone());
    let row_before = dec_repo.get_by_hash(&target_hash).unwrap().unwrap();
    assert_eq!(row_before.state, DecisionState::Approved);

    // Now flip the project to git: init + first commit on main.
    run_git(&["init", "--initial-branch=main"], repo);
    run_git(&["config", "user.email", "test@seshat.dev"], repo);
    run_git(&["config", "user.name", "Seshat Test"], repo);
    fs::write(repo.join(".gitignore"), "seshat.db\nseshat.db-*\n").unwrap();
    run_git(&["add", "."], repo);
    run_git(&["commit", "-m", "initial commit"], repo);

    // Rescan now-git project. The auto-detected convention must NOT
    // re-emit — its description hash matches the row we recorded
    // pre-transition.
    let conventions_after = scan_and_persist(repo);
    let descs: Vec<&str> = conventions_after
        .iter()
        .map(|c| c.description.as_str())
        .collect();
    assert!(
        !descs.contains(&target_description.as_str()),
        "decision recorded in non-git mode must dedup after `git init`; \
         got {descs:?}"
    );

    // Decision row is unchanged.
    let row_after = dec_repo.get_by_hash(&target_hash).unwrap().unwrap();
    assert_eq!(row_after.state, DecisionState::Approved);
    assert_eq!(row_after.description, target_description);
}