From 1e1568fafa420274000ceeb33c45f89a7cd7152d Mon Sep 17 00:00:00 2001 From: gastown/crew/george Date: Sat, 21 Feb 2026 15:53:47 -0800 Subject: [PATCH] =?UTF-8?q?refactor(jsonl):=20remove=20JSONL=20plumbing=20?= =?UTF-8?q?=E2=80=94=20Dolt-native=20only=20(gt-vemg9q=20Phase=201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Delete 21 files and gut JSONL code paths from 13 more, removing ~6,400 lines of JSONL import/export/sync/integrity infrastructure that is superseded by Dolt git remotes for sync and portability. Deleted: import.go, export.go, sync_helpers.go, integrity.go, hook.go, events_export.go, stubs.go, doctor checks (jsonl_integrity, sync_divergence), and all associated tests. Removed .beads-hooks/ legacy hook templates. Gutted: hooks.go (now chain-only), sync.go (Dolt push only), repo.go (Dolt push), restore.go (stubbed for dolt_history_), delete.go, init.go, rename_prefix.go, doctor.go, doctor_fix.go (removed JSONL registrations). Phase 2 TODOs remain for: bootstrap clone, restore via dolt_history_, doctor recovery, create/rename flush, git-portable mode removal. Co-Authored-By: Claude Opus 4.6 Executed-By: gastown/crew/george Rig: gastown Role: crew --- .beads-hooks/post-checkout | 82 -- .beads-hooks/post-merge | 49 -- .beads-hooks/pre-commit | 46 -- .beads-hooks/pre-push | 111 --- cmd/bd/delete.go | 69 -- cmd/bd/doctor.go | 15 - cmd/bd/doctor/fix/jsonl_integrity.go | 87 -- cmd/bd/doctor/jsonl_integrity.go | 124 --- cmd/bd/doctor/jsonl_integrity_test.go | 43 - cmd/bd/doctor/migration_validation.go | 12 +- cmd/bd/doctor/sync_divergence.go | 214 ----- cmd/bd/doctor_fix.go | 2 - .../dolt_jsonl_roundtrip_integration_test.go | 283 ------- ...olt_native_no_fallback_integration_test.go | 208 ----- cmd/bd/events_export.go | 81 -- cmd/bd/export.go | 628 -------------- cmd/bd/hook.go | 776 ------------------ cmd/bd/hook_test.go | 223 ----- cmd/bd/hooks.go | 326 +------- cmd/bd/hooks_test.go | 743 ----------------- cmd/bd/import.go | 617 -------------- cmd/bd/init.go | 70 -- cmd/bd/init_test.go | 87 -- cmd/bd/integrity.go | 359 -------- cmd/bd/integrity_content_test.go | 262 ------ cmd/bd/integrity_test.go | 683 --------------- cmd/bd/rename_prefix.go | 63 -- cmd/bd/repo.go | 19 +- cmd/bd/restore.go | 59 +- cmd/bd/stubs.go | 40 - cmd/bd/sync.go | 47 +- cmd/bd/sync_helpers.go | 301 ------- cmd/bd/templates/hooks/post-checkout | 2 +- cmd/bd/templates/hooks/post-merge | 2 +- 34 files changed, 40 insertions(+), 6693 deletions(-) delete mode 100755 .beads-hooks/post-checkout delete mode 100755 .beads-hooks/post-merge delete mode 100755 .beads-hooks/pre-commit delete mode 100755 .beads-hooks/pre-push delete mode 100644 cmd/bd/doctor/fix/jsonl_integrity.go delete mode 100644 cmd/bd/doctor/jsonl_integrity.go delete mode 100644 cmd/bd/doctor/jsonl_integrity_test.go delete mode 100644 cmd/bd/doctor/sync_divergence.go delete mode 100644 cmd/bd/dolt_jsonl_roundtrip_integration_test.go delete mode 100644 cmd/bd/dolt_native_no_fallback_integration_test.go delete mode 100644 cmd/bd/events_export.go delete mode 100644 cmd/bd/export.go delete mode 100644 cmd/bd/hook.go delete mode 100644 cmd/bd/hook_test.go delete mode 100644 cmd/bd/hooks_test.go delete mode 100644 cmd/bd/import.go delete mode 100644 cmd/bd/integrity.go delete mode 100644 cmd/bd/integrity_content_test.go delete mode 100644 cmd/bd/integrity_test.go delete mode 100644 cmd/bd/stubs.go delete mode 100644 cmd/bd/sync_helpers.go diff --git a/.beads-hooks/post-checkout b/.beads-hooks/post-checkout deleted file mode 100755 index fc852f7ea..000000000 --- a/.beads-hooks/post-checkout +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env sh -# bd-hooks-version: 0.25.1 -# -# bd (beads) post-checkout hook -# -# This hook syncs the bd database after a branch checkout: -# 1. Checks if any .beads/*.jsonl file was updated -# 2. Runs 'bd sync --import-only' to import changes -# -# Arguments provided by git: -# $1 = ref of previous HEAD -# $2 = ref of new HEAD -# $3 = flag (1 if branch checkout, 0 if file checkout) -# -# Install: cp examples/git-hooks/post-checkout .git/hooks/post-checkout && chmod +x .git/hooks/post-checkout - -# Only run on branch checkouts -if [ "$3" != "1" ]; then - exit 0 -fi - -# Check if bd is available -if ! command -v bd >/dev/null 2>&1; then - exit 0 -fi - -# Check if we're in a bd workspace -if [ ! -d .beads ]; then - exit 0 -fi - -# Detect git worktree: compare --git-dir and --git-common-dir -GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) -GIT_COMMON_DIR=$(git rev-parse --git-common-dir 2>/dev/null) -if [ -n "$GIT_DIR" ] && [ -n "$GIT_COMMON_DIR" ]; then - GIT_DIR_ABS=$(cd "$GIT_DIR" 2>/dev/null && pwd || echo "$GIT_DIR") - GIT_COMMON_DIR_ABS=$(cd "$GIT_COMMON_DIR" 2>/dev/null && pwd || echo "$GIT_COMMON_DIR") - - if [ "$GIT_DIR_ABS" != "$GIT_COMMON_DIR_ABS" ]; then - # We're in a git worktree - cat >&2 <<'EOF' - -╔══════════════════════════════════════════════════════════════════════════╗ -║ Welcome to beads in git worktree! ║ -╠══════════════════════════════════════════════════════════════════════════╣ -║ Note: Daemon mode is not recommended with git worktrees. ║ -║ Worktrees share the same database, and the daemon may commit changes ║ -║ to the wrong branch. ║ -║ ║ -║ RECOMMENDED: Disable daemon for this session: ║ -║ export BEADS_NO_DAEMON=1 ║ -║ ║ -║ For more information: ║ -║ - Run: bd doctor ║ -║ - Read: docs/GIT_INTEGRATION.md (lines 10-53) ║ -║ ║ -║ Issue tracking: bd-dc9 ║ -╚══════════════════════════════════════════════════════════════════════════╝ - -EOF - fi -fi - -# Check if any JSONL file exists in .beads/ -if ! ls .beads/*.jsonl >/dev/null 2>&1; then - exit 0 -fi - -# Run bd sync --import-only to import the updated JSONL -if ! output=$(bd sync --import-only 2>&1); then - echo "Warning: Failed to sync bd changes after checkout" >&2 - echo "$output" >&2 - echo "" >&2 - echo "Run 'bd doctor --fix' to diagnose and repair" >&2 - # Don't fail the checkout, just warn -fi - -# Run quick health check (silent on success, hints if issues found) -# This catches version mismatches, outdated hooks, etc. -bd doctor --check-health 2>/dev/null || true - -exit 0 diff --git a/.beads-hooks/post-merge b/.beads-hooks/post-merge deleted file mode 100755 index 584d5e333..000000000 --- a/.beads-hooks/post-merge +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env sh -# bd-hooks-version: 0.25.1 -# -# bd (beads) post-merge hook -# -# This hook syncs the bd database after a git pull or merge: -# 1. Checks if any .beads/*.jsonl file was updated -# 2. Runs 'bd sync --import-only' to import changes -# -# Installation: -# cp examples/git-hooks/post-merge .git/hooks/post-merge -# chmod +x .git/hooks/post-merge -# -# Or use the install script: -# examples/git-hooks/install.sh - -# Check if bd is available -if ! command -v bd >/dev/null 2>&1; then - echo "Warning: bd command not found, skipping post-merge sync" >&2 - exit 0 -fi - -# Check if we're in a bd workspace -if [ ! -d .beads ]; then - # Not a bd workspace, nothing to do - exit 0 -fi - -# Check if any JSONL file exists in .beads/ -if ! ls .beads/*.jsonl >/dev/null 2>&1; then - exit 0 -fi - -# Run bd sync --import-only to import the updated JSONL -# This is more robust than direct import as it handles all edge cases -# Capture both stdout and stderr to show user what went wrong -if ! output=$(bd sync --import-only 2>&1); then - echo "Warning: Failed to sync bd changes after merge" >&2 - echo "$output" >&2 - echo "" >&2 - echo "Run 'bd doctor --fix' to diagnose and repair" >&2 - # Don't fail the merge, just warn -fi - -# Run quick health check (silent on success, hints if issues found) -# This catches version mismatches, outdated hooks, etc. -bd doctor --check-health 2>/dev/null || true - -exit 0 diff --git a/.beads-hooks/pre-commit b/.beads-hooks/pre-commit deleted file mode 100755 index 73619b457..000000000 --- a/.beads-hooks/pre-commit +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env sh -# bd-hooks-version: 0.29.0 -# -# bd (beads) pre-commit hook -# -# This hook ensures that any pending bd issue changes are flushed to -# .beads/issues.jsonl before the commit is created, preventing the -# race condition where daemon auto-flush fires after the commit. -# -# Installation: -# cp examples/git-hooks/pre-commit .git/hooks/pre-commit -# chmod +x .git/hooks/pre-commit -# -# Or use the install script: -# examples/git-hooks/install.sh - -# Check if bd is available -if ! command -v bd >/dev/null 2>&1; then - echo "Warning: bd command not found, skipping pre-commit flush" >&2 - exit 0 -fi - -# Check if we're in a bd workspace -if [ ! -d .beads ]; then - # Not a bd workspace, nothing to do - exit 0 -fi - -# Flush pending changes to JSONL -# Use --flush-only to skip git operations (we're already in a git hook) -# Suppress output unless there's an error -# Note: We warn but don't fail - this allows commits to proceed even if -# beads has issues (e.g., user removed .beads from their branch) -if ! bd sync --flush-only >/dev/null 2>&1; then - echo "Warning: Failed to flush bd changes to JSONL" >&2 - echo "Run 'bd sync --flush-only' manually to diagnose" >&2 - # Don't block the commit - user may have removed beads or have other issues -fi - -# Stage all tracked JSONL files (issues.jsonl is canonical, beads.jsonl for backward compat, deletions.jsonl for deletion propagation) -# git add is harmless if file doesn't exist -for f in .beads/beads.jsonl .beads/issues.jsonl .beads/deletions.jsonl; do - [ -f "$f" ] && git add "$f" 2>/dev/null || true -done - -exit 0 diff --git a/.beads-hooks/pre-push b/.beads-hooks/pre-push deleted file mode 100755 index 97497c26f..000000000 --- a/.beads-hooks/pre-push +++ /dev/null @@ -1,111 +0,0 @@ -#!/usr/bin/env sh -# bd-hooks-version: 0.24.2 -# -# bd (beads) pre-push hook -# -# This hook prevents pushing stale JSONL by: -# 1. Flushing any pending in-memory changes to JSONL (if bd available) -# 2. Checking for uncommitted changes (staged, unstaged, untracked, deleted) -# 3. Failing the push with clear instructions if changes found -# -# The pre-commit hook already exports changes, but this catches: -# - Changes made between commit and push -# - Pending debounced flushes (5s daemon delay) -# -# Installation: -# cp examples/git-hooks/pre-push .git/hooks/pre-push -# chmod +x .git/hooks/pre-push -# -# Or use the install script: -# examples/git-hooks/install.sh - -# Check if we're in a bd workspace -if [ ! -d .beads ]; then - # Not a bd workspace, nothing to do - exit 0 -fi - -# Skip if bd sync is already in progress (GH#532: prevents circular error) -# bd sync sets this env var when pushing from worktree -if [ -n "$BD_SYNC_IN_PROGRESS" ]; then - exit 0 -fi - -# Optionally flush pending bd changes so they surface in JSONL -# This prevents the race where a debounced flush lands after the check -if command -v bd >/dev/null 2>&1; then - bd sync --flush-only >/dev/null 2>&1 || true -fi - -# Collect all tracked or existing JSONL files (beads.jsonl, issues.jsonl for backward compat, deletions.jsonl for deletion propagation) -FILES="" -for f in .beads/beads.jsonl .beads/issues.jsonl .beads/deletions.jsonl; do - # Include file if it exists in working tree OR is tracked by git (even if deleted) - if git ls-files --error-unmatch "$f" >/dev/null 2>&1 || [ -f "$f" ]; then - FILES="$FILES $f" - fi -done - -# Check for any uncommitted changes using porcelain status -# This catches: staged, unstaged, untracked, deleted, renamed, and conflicts -if [ -n "$FILES" ]; then - # shellcheck disable=SC2086 - if [ -n "$(git status --porcelain -- $FILES 2>/dev/null)" ]; then - echo "❌ Error: Beads JSONL has uncommitted changes" >&2 - echo "" >&2 - echo "You made changes to bd issues between your last commit and this push." >&2 - echo "" >&2 - - # Check if bd is available and offer auto-sync - if command -v bd >/dev/null 2>&1; then - # Check if we're in an interactive terminal - if [ -t 0 ]; then - echo "Would you like to run 'bd sync' now to commit and push these changes? [y/N]" >&2 - read -r response - case "$response" in - [yY][eE][sS]|[yY]) - echo "" >&2 - echo "Running: bd sync" >&2 - if bd sync; then - echo "" >&2 - echo "✓ Sync complete. Continuing with push..." >&2 - exit 0 - else - echo "" >&2 - echo "❌ Sync failed. Push aborted." >&2 - exit 1 - fi - ;; - *) - echo "" >&2 - echo "Push aborted. Run 'bd sync' manually when ready:" >&2 - echo "" >&2 - echo " bd sync" >&2 - echo " git push" >&2 - echo "" >&2 - exit 1 - ;; - esac - else - # Non-interactive: just show the message - echo "Run 'bd sync' to commit these changes:" >&2 - echo "" >&2 - echo " bd sync" >&2 - echo "" >&2 - exit 1 - fi - else - # bd not available, fall back to manual git commands - echo "Please commit the updated JSONL before pushing:" >&2 - echo "" >&2 - # shellcheck disable=SC2086 - echo " git add $FILES" >&2 - echo ' git commit -m "Update bd JSONL"' >&2 - echo " git push" >&2 - echo "" >&2 - exit 1 - fi - fi -fi - -exit 0 diff --git a/cmd/bd/delete.go b/cmd/bd/delete.go index 7389c6301..c8f4ff118 100644 --- a/cmd/bd/delete.go +++ b/cmd/bd/delete.go @@ -3,7 +3,6 @@ package main import ( "bufio" "context" - "encoding/json" "errors" "fmt" "os" @@ -219,8 +218,6 @@ Force: Delete and orphan dependents if err := deleteIssue(ctx, issueID); err != nil { FatalError("deleting issue: %v", err) } - // Remove the issue from the JSONL file as well - _ = removeIssueFromJSONL(issueID) totalDepsRemoved := outgoingRemoved + inboundRemoved if jsonOutput { outputJSON(map[string]interface{}{ @@ -241,71 +238,6 @@ func deleteIssue(ctx context.Context, issueID string) error { return store.DeleteIssue(ctx, issueID) } -// removeIssueFromJSONL removes a deleted issue from the JSONL file -// Auto-flush cannot see deletions because the dirty_issues row is deleted with the issue -func removeIssueFromJSONL(issueID string) error { - path := findJSONLPath() - if path == "" { - return nil // No JSONL file yet - } - // Read all issues except the deleted one - // #nosec G304 - controlled path from config - f, err := os.Open(path) - if err != nil { - if os.IsNotExist(err) { - return nil // No file, nothing to clean - } - return fmt.Errorf("failed to open storage file: %w", err) - } - var issues []*types.Issue - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() - if strings.TrimSpace(line) == "" { - continue - } - var iss types.Issue - if err := json.Unmarshal([]byte(line), &iss); err != nil { - // Skip malformed lines - continue - } - if iss.ID != issueID { - issues = append(issues, &iss) - } - } - if err := scanner.Err(); err != nil { - _ = f.Close() - return fmt.Errorf("failed to read storage file: %w", err) - } - if err := f.Close(); err != nil { - return fmt.Errorf("failed to close storage file: %w", err) - } - // Write to temp file atomically - temp := fmt.Sprintf("%s.tmp.%d", path, os.Getpid()) - // #nosec G304 - controlled path from config - out, err := os.OpenFile(temp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600) - if err != nil { - return fmt.Errorf("failed to create temp file: %w", err) - } - enc := json.NewEncoder(out) - for _, iss := range issues { - if err := enc.Encode(iss); err != nil { - _ = out.Close() - _ = os.Remove(temp) - return fmt.Errorf("failed to write issue: %w", err) - } - } - if err := out.Close(); err != nil { - _ = os.Remove(temp) - return fmt.Errorf("failed to close temp file: %w", err) - } - // Atomic rename - if err := os.Rename(temp, path); err != nil { - _ = os.Remove(temp) - return fmt.Errorf("failed to rename temp file: %w", err) - } - return nil -} // deleteBatch handles deletion of multiple issues // @@ -527,7 +459,6 @@ func deleteBatchFallback(issueIDs []string, force bool, dryRun bool, cascade boo fmt.Fprintf(os.Stderr, "Error deleting issue %s: %v\n", issueID, err) continue } - _ = removeIssueFromJSONL(issueID) deletedCount++ } diff --git a/cmd/bd/doctor.go b/cmd/bd/doctor.go index ad01e259e..16b2654ee 100644 --- a/cmd/bd/doctor.go +++ b/cmd/bd/doctor.go @@ -434,13 +434,6 @@ func runDiagnostics(path string) doctorResult { result.Checks = append(result.Checks, roleCheck) // Don't fail overall check for role config, just warn - URL heuristic fallback still works - // Check 7d: JSONL integrity (malformed lines, missing IDs) - jsonlIntegrityCheck := convertWithCategory(doctor.CheckJSONLIntegrity(path), doctor.CategoryData) - result.Checks = append(result.Checks, jsonlIntegrityCheck) - if jsonlIntegrityCheck.Status == statusWarning || jsonlIntegrityCheck.Status == statusError { - result.OverallOK = false - } - // Check 7e: Stale lock files (bootstrap, sync, daemon, startup) staleLockCheck := convertDoctorCheck(doctor.CheckStaleLockFiles(path)) result.Checks = append(result.Checks, staleLockCheck) @@ -481,14 +474,6 @@ func runDiagnostics(path string) doctorResult { doltModeCheck := convertWithCategory(doctor.CheckDoltServerModeMismatch(path), doctor.CategoryFederation) result.Checks = append(result.Checks, doltModeCheck) - // Check 9a: Sync divergence (JSONL/SQLite/git) - GH#885 - syncDivergenceCheck := convertWithCategory(doctor.CheckSyncDivergence(path), doctor.CategoryData) - result.Checks = append(result.Checks, syncDivergenceCheck) - if syncDivergenceCheck.Status == statusError { - result.OverallOK = false - } - // Warning-level divergence is informational, doesn't fail overall - // Check 9: Permissions permCheck := convertWithCategory(doctor.CheckPermissions(path), doctor.CategoryCore) result.Checks = append(result.Checks, permCheck) diff --git a/cmd/bd/doctor/fix/jsonl_integrity.go b/cmd/bd/doctor/fix/jsonl_integrity.go deleted file mode 100644 index e87e481f4..000000000 --- a/cmd/bd/doctor/fix/jsonl_integrity.go +++ /dev/null @@ -1,87 +0,0 @@ -package fix - -import ( - "fmt" - "os" - "path/filepath" - "time" - - "github.com/steveyegge/beads/internal/beads" - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/utils" -) - -// JSONLIntegrity backs up a malformed JSONL export and regenerates it from the database. -// This is safe only when a database exists and is readable. -func JSONLIntegrity(path string) error { - if err := validateBeadsWorkspace(path); err != nil { - return err - } - - absPath, err := filepath.Abs(path) - if err != nil { - return fmt.Errorf("failed to resolve path: %w", err) - } - - beadsDir := filepath.Join(absPath, ".beads") - - // Resolve db path. - dbPath := filepath.Join(beadsDir, beads.CanonicalDatabaseName) - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" { - dbPath = cfg.DatabasePath(beadsDir) - } - if _, err := os.Stat(dbPath); os.IsNotExist(err) { - return fmt.Errorf("cannot auto-repair JSONL: no database found") - } - - // Resolve JSONL export path. - jsonlPath := "" - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil { - if cfg.JSONLExport != "" && !isSystemJSONLFilename(cfg.JSONLExport) { - p := cfg.JSONLPath(beadsDir) - if _, err := os.Stat(p); err == nil { - jsonlPath = p - } - } - } - if jsonlPath == "" { - p := utils.FindJSONLInDir(beadsDir) - if _, err := os.Stat(p); err == nil { - jsonlPath = p - } - } - if jsonlPath == "" { - return fmt.Errorf("cannot auto-repair JSONL: no JSONL file found") - } - - // Back up the JSONL. - ts := time.Now().UTC().Format("20060102T150405Z") - backup := jsonlPath + "." + ts + ".corrupt.backup.jsonl" - if err := moveFile(jsonlPath, backup); err != nil { - return fmt.Errorf("failed to back up JSONL: %w", err) - } - - binary, err := getBdBinary() - if err != nil { - _ = moveFile(backup, jsonlPath) // Best effort rollback: if restore fails, manual recovery from backup needed - return err - } - - // Re-export from DB. - cmd := newBdCmd(binary, "--db", dbPath, "export", "-o", jsonlPath, "--force") - cmd.Dir = absPath - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - // Best-effort rollback: restore the original JSONL, but keep the backup. - failedTS := time.Now().UTC().Format("20060102T150405Z") - if _, statErr := os.Stat(jsonlPath); statErr == nil { - failed := jsonlPath + "." + failedTS + ".failed.regen.jsonl" - _ = moveFile(jsonlPath, failed) // Best effort rollback: if restore fails, manual recovery from backup needed - } - _ = copyFile(backup, jsonlPath) // Best effort rollback: if restore fails, manual recovery from backup needed - return fmt.Errorf("failed to regenerate JSONL from database: %w (backup: %s)", err, backup) - } - - return nil -} diff --git a/cmd/bd/doctor/jsonl_integrity.go b/cmd/bd/doctor/jsonl_integrity.go deleted file mode 100644 index 2ccf3b9c5..000000000 --- a/cmd/bd/doctor/jsonl_integrity.go +++ /dev/null @@ -1,124 +0,0 @@ -package doctor - -import ( - "bufio" - "encoding/json" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/steveyegge/beads/internal/beads" - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/utils" -) - -func CheckJSONLIntegrity(path string) DoctorCheck { - // Follow redirect to resolve actual beads directory (bd-tvus fix) - beadsDir := resolveBeadsDir(filepath.Join(path, ".beads")) - - // Resolve JSONL path. - jsonlPath := "" - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil { - if cfg.JSONLExport != "" && !isSystemJSONLFilename(cfg.JSONLExport) { - p := cfg.JSONLPath(beadsDir) - if _, err := os.Stat(p); err == nil { - jsonlPath = p - } - } - } - if jsonlPath == "" { - // Fall back to a best-effort discovery within .beads/. - p := utils.FindJSONLInDir(beadsDir) - if _, err := os.Stat(p); err == nil { - jsonlPath = p - } - } - if jsonlPath == "" { - return DoctorCheck{Name: "JSONL Integrity", Status: StatusOK, Message: "N/A (no JSONL file)"} - } - - // Best-effort scan for malformed lines. - f, err := os.Open(jsonlPath) // #nosec G304 -- jsonlPath is within the workspace - if err != nil { - return DoctorCheck{ - Name: "JSONL Integrity", - Status: StatusWarning, - Message: "Unable to read JSONL file", - Detail: err.Error(), - } - } - defer f.Close() - - var malformed int - var examples []string - scanner := bufio.NewScanner(f) - lineNo := 0 - for scanner.Scan() { - lineNo++ - line := strings.TrimSpace(scanner.Text()) - if line == "" { - continue - } - var v struct { - ID string `json:"id"` - } - if err := json.Unmarshal([]byte(line), &v); err != nil || v.ID == "" { - malformed++ - if len(examples) < 5 { - if err != nil { - examples = append(examples, fmt.Sprintf("line %d: %v", lineNo, err)) - } else { - examples = append(examples, fmt.Sprintf("line %d: missing id", lineNo)) - } - } - } - } - if err := scanner.Err(); err != nil { - return DoctorCheck{ - Name: "JSONL Integrity", - Status: StatusWarning, - Message: "Unable to scan JSONL file", - Detail: err.Error(), - } - } - if malformed == 0 { - return DoctorCheck{ - Name: "JSONL Integrity", - Status: StatusOK, - Message: fmt.Sprintf("%s looks valid", filepath.Base(jsonlPath)), - } - } - - // If we have a database, we can auto-repair by re-exporting from DB. - dbPath := filepath.Join(beadsDir, beads.CanonicalDatabaseName) - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" { - dbPath = cfg.DatabasePath(beadsDir) - } - if _, err := os.Stat(dbPath); os.IsNotExist(err) { - return DoctorCheck{ - Name: "JSONL Integrity", - Status: StatusError, - Message: fmt.Sprintf("%s has %d malformed line(s)", filepath.Base(jsonlPath), malformed), - Detail: strings.Join(examples, "\n"), - Fix: "Restore the JSONL file from git or from a backup (no database available for auto-repair).", - } - } - - return DoctorCheck{ - Name: "JSONL Integrity", - Status: StatusError, - Message: fmt.Sprintf("%s has %d malformed line(s)", filepath.Base(jsonlPath), malformed), - Detail: strings.Join(examples, "\n"), - Fix: "Run 'bd doctor --fix' to back up the JSONL and regenerate it from the database.", - } -} - -func isSystemJSONLFilename(name string) bool { - switch name { - case "deletions.jsonl", "interactions.jsonl", "molecules.jsonl", "routes.jsonl": - return true - default: - return false - } -} diff --git a/cmd/bd/doctor/jsonl_integrity_test.go b/cmd/bd/doctor/jsonl_integrity_test.go deleted file mode 100644 index 772e16a56..000000000 --- a/cmd/bd/doctor/jsonl_integrity_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package doctor - -import ( - "os" - "path/filepath" - "testing" -) - -func TestCheckJSONLIntegrity_MalformedLine(t *testing.T) { - ws := t.TempDir() - beadsDir := filepath.Join(ws, ".beads") - if err := os.MkdirAll(beadsDir, 0755); err != nil { - t.Fatal(err) - } - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - if err := os.WriteFile(jsonlPath, []byte("{\"id\":\"t-1\"}\n{not json}\n"), 0644); err != nil { - t.Fatal(err) - } - // Ensure DB exists so check suggests auto-repair. - if err := os.WriteFile(filepath.Join(beadsDir, "beads.db"), []byte("x"), 0644); err != nil { - t.Fatal(err) - } - - check := CheckJSONLIntegrity(ws) - if check.Status != StatusError { - t.Fatalf("expected StatusError, got %v (%s)", check.Status, check.Message) - } - if check.Fix == "" { - t.Fatalf("expected Fix guidance") - } -} - -func TestCheckJSONLIntegrity_NoJSONL(t *testing.T) { - ws := t.TempDir() - beadsDir := filepath.Join(ws, ".beads") - if err := os.MkdirAll(beadsDir, 0755); err != nil { - t.Fatal(err) - } - check := CheckJSONLIntegrity(ws) - if check.Status != StatusOK { - t.Fatalf("expected StatusOK, got %v (%s)", check.Status, check.Message) - } -} diff --git a/cmd/bd/doctor/migration_validation.go b/cmd/bd/doctor/migration_validation.go index c2abbb1fb..2fe5db40e 100644 --- a/cmd/bd/doctor/migration_validation.go +++ b/cmd/bd/doctor/migration_validation.go @@ -382,7 +382,17 @@ func CheckDoltLocks(path string) DoctorCheck { // Helper functions -// Note: findJSONLFile is defined in sync_divergence.go +// findJSONLFile locates the JSONL file in a .beads directory. +// Temporary: will be removed with Phase 2c (doctor JSONL cleanup). +func findJSONLFile(beadsDir string) string { + for _, name := range []string{"issues.jsonl", "beads.jsonl"} { + p := filepath.Join(beadsDir, name) + if _, err := os.Stat(p); err == nil { + return p + } + } + return "" +} // getSQLiteDBPath returns the path to the SQLite database. func getSQLiteDBPath(beadsDir string) string { diff --git a/cmd/bd/doctor/sync_divergence.go b/cmd/bd/doctor/sync_divergence.go deleted file mode 100644 index e81560a6a..000000000 --- a/cmd/bd/doctor/sync_divergence.go +++ /dev/null @@ -1,214 +0,0 @@ -// Package doctor provides diagnostic checks for beads installations. -package doctor - -import ( - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" - - "github.com/steveyegge/beads/internal/config" - "github.com/steveyegge/beads/internal/configfile" -) - -// SyncDivergenceIssue represents a specific type of sync divergence detected. -type SyncDivergenceIssue struct { - Type string // "jsonl_git_mismatch", "sqlite_mtime_stale", "uncommitted_beads" - Description string - FixCommand string -} - -// CheckSyncDivergence checks for sync divergence issues between JSONL, SQLite, and git. -// This is part of GH#885 fix: recovery mechanism. -// -// Detects: -// 1. JSONL on disk differs from git HEAD version -// 2. SQLite last_import_time does not match JSONL mtime -// 3. Uncommitted .beads/ changes exist -func CheckSyncDivergence(path string) DoctorCheck { - // Check if we're in a git repository - cmd := exec.Command("git", "rev-parse", "--git-dir") - cmd.Dir = path - if err := cmd.Run(); err != nil { - return DoctorCheck{ - Name: "Sync Divergence", - Status: StatusOK, - Message: "N/A (not a git repository)", - Category: CategoryData, - } - } - - // Follow redirect to resolve actual beads directory - beadsDir := resolveBeadsDir(filepath.Join(path, ".beads")) - if _, err := os.Stat(beadsDir); os.IsNotExist(err) { - return DoctorCheck{ - Name: "Sync Divergence", - Status: StatusOK, - Message: "N/A (no .beads directory)", - Category: CategoryData, - } - } - - var issues []SyncDivergenceIssue - - // Check 1: JSONL differs from git HEAD - jsonlIssue := checkJSONLGitDivergence(path, beadsDir) - if jsonlIssue != nil { - issues = append(issues, *jsonlIssue) - } - - // Check 3: Uncommitted .beads/ changes - uncommittedIssue := checkUncommittedBeadsChanges(path, beadsDir) - if uncommittedIssue != nil { - issues = append(issues, *uncommittedIssue) - } - - if len(issues) == 0 { - return DoctorCheck{ - Name: "Sync Divergence", - Status: StatusOK, - Message: "JSONL, Dolt, and git are in sync", - Category: CategoryData, - } - } - - // Build detail and fix messages - var details []string - var fixes []string - for _, issue := range issues { - details = append(details, issue.Description) - if issue.FixCommand != "" { - fixes = append(fixes, issue.FixCommand) - } - } - - status := StatusWarning - if len(issues) > 1 { - // Multiple divergence issues are more serious - status = StatusError - } - - return DoctorCheck{ - Name: "Sync Divergence", - Status: status, - Message: fmt.Sprintf("%d sync divergence issue(s) detected", len(issues)), - Detail: strings.Join(details, "\n"), - Fix: strings.Join(fixes, " OR "), - Category: CategoryData, - } -} - -// checkJSONLGitDivergence checks if JSONL on disk differs from git HEAD version. -func checkJSONLGitDivergence(path, beadsDir string) *SyncDivergenceIssue { - // Find JSONL file - jsonlPath := findJSONLFile(beadsDir) - if jsonlPath == "" { - return nil // No JSONL file - } - - // Get relative path for git commands - relPath, err := filepath.Rel(path, jsonlPath) - if err != nil { - return nil - } - - // Check if file is tracked by git - cmd := exec.Command("git", "ls-files", "--error-unmatch", relPath) // #nosec G204 -- relPath is derived from validated file path - cmd.Dir = path - if err := cmd.Run(); err != nil { - // File not tracked by git - return nil - } - - // Compare current file with HEAD - cmd = exec.Command("git", "diff", "--quiet", "HEAD", "--", relPath) // #nosec G204 -- relPath is derived from validated file path - cmd.Dir = path - if err := cmd.Run(); err != nil { - // Exit code non-zero means there are differences - // In dolt-native mode, Dolt is source of truth - JSONL should be restored from git HEAD - fixCmd := "git add .beads/ && git commit -m 'sync beads'" - if config.GetSyncMode() == config.SyncModeDoltNative { - fixCmd = fmt.Sprintf("git restore %s", relPath) - } - return &SyncDivergenceIssue{ - Type: "jsonl_git_mismatch", - Description: fmt.Sprintf("JSONL file differs from git HEAD: %s", filepath.Base(jsonlPath)), - FixCommand: fixCmd, - } - } - - return nil -} - -// checkUncommittedBeadsChanges checks if there are uncommitted changes in .beads/ directory. -func checkUncommittedBeadsChanges(path, beadsDir string) *SyncDivergenceIssue { - // Get relative path of beads dir - relBeadsDir, err := filepath.Rel(path, beadsDir) - if err != nil { - relBeadsDir = ".beads" - } - - // Check for uncommitted changes in .beads/ - cmd := exec.Command("git", "status", "--porcelain", "--", relBeadsDir) // #nosec G204 -- relBeadsDir is derived from validated path - cmd.Dir = path - out, err := cmd.Output() - if err != nil { - return nil // Can't run git status - } - - status := strings.TrimSpace(string(out)) - if status == "" { - return nil // No uncommitted changes - } - - // Count changed files - lines := strings.Split(status, "\n") - fileCount := 0 - for _, line := range lines { - if strings.TrimSpace(line) != "" { - fileCount++ - } - } - - fixCmd := "bd sync" - // For dolt backend, bd sync/import-only workflows don't apply. - // In dolt-native mode, Dolt is source of truth - JSONL should be restored from git HEAD. - // In other dolt modes, recommend a plain git commit. - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.GetBackend() == configfile.BackendDolt { - if config.GetSyncMode() == config.SyncModeDoltNative { - fixCmd = fmt.Sprintf("git restore %s", relBeadsDir) - } else { - fixCmd = "git add .beads/ && git commit -m 'sync beads'" - } - } - - return &SyncDivergenceIssue{ - Type: "uncommitted_beads", - Description: fmt.Sprintf("Uncommitted .beads/ changes (%d file(s))", fileCount), - FixCommand: fixCmd, - } -} - -// findJSONLFile finds the JSONL file in the beads directory. -func findJSONLFile(beadsDir string) string { - // Check metadata.json for custom JSONL name - if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil { - if cfg.JSONLExport != "" && !isSystemJSONLFilename(cfg.JSONLExport) { - p := cfg.JSONLPath(beadsDir) - if _, err := os.Stat(p); err == nil { - return p - } - } - } - - // Try standard names - for _, name := range []string{"issues.jsonl", "beads.jsonl"} { - p := filepath.Join(beadsDir, name) - if _, err := os.Stat(p); err == nil { - return p - } - } - - return "" -} diff --git a/cmd/bd/doctor_fix.go b/cmd/bd/doctor_fix.go index 1c6c7ecc2..8e1204e9d 100644 --- a/cmd/bd/doctor_fix.go +++ b/cmd/bd/doctor_fix.go @@ -279,8 +279,6 @@ func applyFixList(path string, fixes []doctorCheck) { err = fix.DatabaseConfig(path) case "JSONL Config": err = fix.LegacyJSONLConfig(path) - case "JSONL Integrity": - err = fix.JSONLIntegrity(path) case "Untracked Files": err = fix.UntrackedJSONL(path) case "Merge Artifacts": diff --git a/cmd/bd/dolt_jsonl_roundtrip_integration_test.go b/cmd/bd/dolt_jsonl_roundtrip_integration_test.go deleted file mode 100644 index baed7ebae..000000000 --- a/cmd/bd/dolt_jsonl_roundtrip_integration_test.go +++ /dev/null @@ -1,283 +0,0 @@ -//go:build cgo && integration - -package main - -import ( - "bufio" - "encoding/json" - "os" - "path/filepath" - "runtime" - "sort" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" -) - -func parseCreateID(t *testing.T, out string) string { - t.Helper() - idx := strings.Index(out, "{") - if idx < 0 { - t.Fatalf("expected JSON in output, got:\n%s", out) - } - var m map[string]any - if err := json.Unmarshal([]byte(out[idx:]), &m); err != nil { - t.Fatalf("failed to parse create JSON: %v\n%s", err, out) - } - id, _ := m["id"].(string) - if id == "" { - t.Fatalf("missing id in create output:\n%s", out) - } - return id -} - -func readJSONLIssues(t *testing.T, path string) map[string]*types.Issue { - t.Helper() - f, err := os.Open(path) // #nosec G304 -- test-controlled path - if err != nil { - t.Fatalf("open %s: %v", path, err) - } - defer func() { _ = f.Close() }() - - scanner := bufio.NewScanner(f) - // allow larger issues - scanner.Buffer(make([]byte, 0, 64*1024), 2*1024*1024) - - out := make(map[string]*types.Issue) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line == "" { - continue - } - var iss types.Issue - if err := json.Unmarshal([]byte(line), &iss); err != nil { - t.Fatalf("unmarshal JSONL line: %v\nline=%s", err, line) - } - iss.SetDefaults() - copy := iss - out[iss.ID] = © - } - if err := scanner.Err(); err != nil { - t.Fatalf("scan %s: %v", path, err) - } - return out -} - -func writeJSONLIssues(t *testing.T, path string, issues map[string]*types.Issue) { - t.Helper() - f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600) // #nosec G304 -- test-controlled path - if err != nil { - t.Fatalf("open %s for write: %v", path, err) - } - defer func() { _ = f.Close() }() - - ids := make([]string, 0, len(issues)) - for id := range issues { - ids = append(ids, id) - } - sort.Strings(ids) - - w := bufio.NewWriter(f) - for _, id := range ids { - iss := issues[id] - if iss == nil { - continue - } - b, err := json.Marshal(iss) - if err != nil { - t.Fatalf("marshal issue %s: %v", id, err) - } - if _, err := w.Write(append(b, '\n')); err != nil { - t.Fatalf("write issue %s: %v", id, err) - } - } - if err := w.Flush(); err != nil { - t.Fatalf("flush %s: %v", path, err) - } -} - -func findCommentTimestamp(iss *types.Issue, author, text string) (time.Time, bool) { - if iss == nil { - return time.Time{}, false - } - for _, c := range iss.Comments { - if c.Author == author && strings.TrimSpace(c.Text) == strings.TrimSpace(text) { - return c.CreatedAt, true - } - } - return time.Time{}, false -} - -func findCommentTimestampByText(iss *types.Issue, text string) (time.Time, bool) { - if iss == nil { - return time.Time{}, false - } - for _, c := range iss.Comments { - if strings.TrimSpace(c.Text) == strings.TrimSpace(text) { - return c.CreatedAt, true - } - } - return time.Time{}, false -} - -func TestDoltJSONLRoundTrip_DepsLabelsCommentsDeletions(t *testing.T) { - if testing.Short() { - t.Skip("skipping slow integration test in short mode") - } - if runtime.GOOS == windowsOS { - t.Skip("dolt integration test not supported on windows") - } - - // Workspace 1: create data and export JSONL. - ws1 := createTempDirWithCleanup(t) - if err := runCommandInDir(ws1, "git", "init"); err != nil { - t.Fatalf("git init failed: %v", err) - } - _ = runCommandInDir(ws1, "git", "config", "user.email", "test@example.com") - _ = runCommandInDir(ws1, "git", "config", "user.name", "Test User") - - env := []string{ - "BEADS_TEST_MODE=1", - "BEADS_NO_DAEMON=1", - } - - initOut, initErr := runBDExecAllowErrorWithEnv(t, ws1, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet") - if initErr != nil { - lower := strings.ToLower(initOut) - if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "unknown")) { - t.Skipf("dolt backend not available: %s", initOut) - } - t.Fatalf("bd init --backend dolt failed: %v\n%s", initErr, initOut) - } - - outA, err := runBDExecAllowErrorWithEnv(t, ws1, env, "create", "Issue A", "--json") - if err != nil { - t.Fatalf("bd create A failed: %v\n%s", err, outA) - } - idA := parseCreateID(t, outA) - - outB, err := runBDExecAllowErrorWithEnv(t, ws1, env, "create", "Issue B", "--json") - if err != nil { - t.Fatalf("bd create B failed: %v\n%s", err, outB) - } - idB := parseCreateID(t, outB) - - // Add label + comment + dependency. - if out, err := runBDExecAllowErrorWithEnv(t, ws1, env, "label", "add", idA, "urgent"); err != nil { - t.Fatalf("bd label add failed: %v\n%s", err, out) - } - commentText := "Hello from JSONL round-trip" - if out, err := runBDExecAllowErrorWithEnv(t, ws1, env, "comments", "add", idA, commentText); err != nil { - t.Fatalf("bd comments add failed: %v\n%s", err, out) - } - if out, err := runBDExecAllowErrorWithEnv(t, ws1, env, "dep", "add", idA, idB); err != nil { - t.Fatalf("bd dep add failed: %v\n%s", err, out) - } - - jsonl1 := filepath.Join(ws1, ".beads", "issues.jsonl") - if out, err := runBDExecAllowErrorWithEnv(t, ws1, env, "export", "-o", jsonl1); err != nil { - t.Fatalf("bd export failed: %v\n%s", err, out) - } - - issues1 := readJSONLIssues(t, jsonl1) - if len(issues1) != 2 { - t.Fatalf("expected 2 issues in export1, got %d", len(issues1)) - } - if issues1[idA] == nil || issues1[idB] == nil { - t.Fatalf("expected exported issues to include %s and %s", idA, idB) - } - // Label present - foundUrgent := false - for _, l := range issues1[idA].Labels { - if l == "urgent" { - foundUrgent = true - break - } - } - if !foundUrgent { - t.Fatalf("expected label 'urgent' on %s in export1", idA) - } - // Dependency present - foundDep := false - for _, d := range issues1[idA].Dependencies { - if d.DependsOnID == idB { - foundDep = true - break - } - } - if !foundDep { - t.Fatalf("expected dependency %s -> %s in export1", idA, idB) - } - // Comment present + capture timestamp - ts1, ok := findCommentTimestampByText(issues1[idA], commentText) - if !ok || ts1.IsZero() { - t.Fatalf("expected comment on %s in export1", idA) - } - - // Mark issue B as closed+deleted in JSONL (simulating a soft delete). - now := time.Now().UTC() - issues1[idB].Status = types.StatusClosed - issues1[idB].ClosedAt = &now - issues1[idB].SetDefaults() - - jsonl1Deleted := filepath.Join(ws1, ".beads", "issues.deleted.jsonl") - writeJSONLIssues(t, jsonl1Deleted, issues1) - issues1Deleted := readJSONLIssues(t, jsonl1Deleted) - if issues1Deleted[idB].Status != types.StatusClosed { - t.Fatalf("expected %s to be closed in deleted JSONL, got %q", idB, issues1Deleted[idB].Status) - } - - // Workspace 2: import JSONL into fresh Dolt DB and re-export. - ws2 := createTempDirWithCleanup(t) - if err := runCommandInDir(ws2, "git", "init"); err != nil { - t.Fatalf("git init failed: %v", err) - } - _ = runCommandInDir(ws2, "git", "config", "user.email", "test@example.com") - _ = runCommandInDir(ws2, "git", "config", "user.name", "Test User") - - initOut2, initErr2 := runBDExecAllowErrorWithEnv(t, ws2, env, "init", "--backend", "dolt", "--prefix", "test", "--quiet") - if initErr2 != nil { - lower := strings.ToLower(initOut2) - if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "unknown")) { - t.Skipf("dolt backend not available: %s", initOut2) - } - t.Fatalf("bd init --backend dolt (ws2) failed: %v\n%s", initErr2, initOut2) - } - - // Copy JSONL into ws2 beads dir - jsonl2in := filepath.Join(ws2, ".beads", "issues.jsonl") - data, err := os.ReadFile(jsonl1Deleted) - if err != nil { - t.Fatalf("read export1: %v", err) - } - if err := os.WriteFile(jsonl2in, data, 0o600); err != nil { - t.Fatalf("write ws2 issues.jsonl: %v", err) - } - - if out, err := runBDExecAllowErrorWithEnv(t, ws2, env, "import", "-i", jsonl2in); err != nil { - t.Fatalf("bd import failed: %v\n%s", err, out) - } - - jsonl2out := filepath.Join(ws2, ".beads", "roundtrip.jsonl") - if out, err := runBDExecAllowErrorWithEnv(t, ws2, env, "export", "-o", jsonl2out); err != nil { - t.Fatalf("bd export (ws2) failed: %v\n%s", err, out) - } - - issues2 := readJSONLIssues(t, jsonl2out) - if len(issues2) != 2 { - t.Fatalf("expected 2 issues in export2 (including deleted), got %d", len(issues2)) - } - if issues2[idB].Status != types.StatusClosed { - t.Fatalf("expected %s to be closed in export2, got %q", idB, issues2[idB].Status) - } - // Ensure comment timestamp preserved across import/export - ts2, ok := findCommentTimestampByText(issues2[idA], commentText) - if !ok { - t.Fatalf("expected comment on %s in export2", idA) - } - if !ts2.Equal(ts1) { - t.Fatalf("expected comment timestamp preserved, export1=%s export2=%s", ts1.Format(time.RFC3339Nano), ts2.Format(time.RFC3339Nano)) - } -} diff --git a/cmd/bd/dolt_native_no_fallback_integration_test.go b/cmd/bd/dolt_native_no_fallback_integration_test.go deleted file mode 100644 index a8b4bdf9a..000000000 --- a/cmd/bd/dolt_native_no_fallback_integration_test.go +++ /dev/null @@ -1,208 +0,0 @@ -//go:build cgo && integration - -package main - -import ( - "encoding/json" - "os" - "path/filepath" - "runtime" - "strings" - "testing" - "time" -) - -// TestDoltNativeNoFallback verifies that when Dolt connection fails in dolt-native -// mode, bd does NOT create SQLite (beads.db) or JSONL (issues.jsonl) as fallback. -// -// This test validates the fix from bd-m2jr: the factory pattern ensures all code -// paths use the configured backend and don't fall back to SQLite on failure. -func TestDoltNativeNoFallback(t *testing.T) { - if testing.Short() { - t.Skip("skipping slow integration test in short mode") - } - if runtime.GOOS == windowsOS { - t.Skip("dolt native fallback test not supported on windows") - } - - tmpDir := createTempDirWithCleanup(t) - - // Set up a real git repo so bd commands behave normally. - if err := runCommandInDir(tmpDir, "git", "init"); err != nil { - t.Fatalf("git init failed: %v", err) - } - _ = runCommandInDir(tmpDir, "git", "config", "user.email", "test@example.com") - _ = runCommandInDir(tmpDir, "git", "config", "user.name", "Test User") - - beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.MkdirAll(beadsDir, 0o755); err != nil { - t.Fatalf("failed to create .beads dir: %v", err) - } - - // Write metadata.json configuring Dolt in server mode with a bad server. - // Port 12345 is intentionally invalid/unreachable to simulate connection failure. - metadata := map[string]interface{}{ - "database": "dolt", - "backend": "dolt", - "dolt_mode": "server", - "dolt_server_host": "127.0.0.1", - "dolt_server_port": 12345, // Unreachable port to trigger connection failure - "dolt_server_user": "root", - "dolt_database": "beads", - } - metadataBytes, err := json.MarshalIndent(metadata, "", " ") - if err != nil { - t.Fatalf("failed to marshal metadata: %v", err) - } - if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), metadataBytes, 0o600); err != nil { - t.Fatalf("failed to write metadata.json: %v", err) - } - - // Write config.yaml with sync.mode=dolt-native. - configYAML := `# Test config for dolt-native mode -issue-prefix: "test" -sync.mode: "dolt-native" -` - if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configYAML), 0o600); err != nil { - t.Fatalf("failed to write config.yaml: %v", err) - } - - env := []string{ - "BEADS_TEST_MODE=1", - "BEADS_NO_DAEMON=1", // Force direct mode to test factory behavior - "BEADS_DIR=" + beadsDir, - } - - // Attempt to run a command that requires storage access. - // This should FAIL because Dolt connection fails, but should NOT create SQLite/JSONL. - out, cmdErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "list", "--json") - - // We expect an error because the Dolt server is unreachable. - // The specific error varies by build (with/without CGO), but it should fail. - if cmdErr == nil { - // If dolt backend isn't available in this build, the error may be different. - lower := strings.ToLower(out) - if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "cgo")) { - t.Skipf("dolt backend not available in this build: %s", out) - } - // If it succeeded, that's unexpected for a bad server config. - t.Logf("Command output (expected error): %s", out) - // It might succeed with empty results if no issues exist, but let's check files. - } - - // Grace period for any async operations to manifest. - time.Sleep(50 * time.Millisecond) - - // CRITICAL ASSERTION: beads.db must NOT exist. - // This is the main regression test for bd-m2jr. - sqliteDB := filepath.Join(beadsDir, "beads.db") - if _, err := os.Stat(sqliteDB); err == nil { - t.Fatalf("REGRESSION: SQLite database %s was created as fallback in dolt-native mode", sqliteDB) - } else if !os.IsNotExist(err) { - t.Fatalf("unexpected error checking for beads.db: %v", err) - } - - // NOTE: In dolt-native mode, JSONL is now export-only (backup). However, since - // Dolt connection failed above, no storage was created and no export could happen. - // So JSONL should still not exist in this failure case. - jsonlFile := filepath.Join(beadsDir, "issues.jsonl") - if _, err := os.Stat(jsonlFile); err == nil { - t.Fatalf("REGRESSION: JSONL file %s was created in dolt-native mode with no storage", jsonlFile) - } else if !os.IsNotExist(err) { - t.Fatalf("unexpected error checking for issues.jsonl: %v", err) - } - - t.Logf("SUCCESS: No SQLite or JSONL fallback created when Dolt connection failed") -} - -// TestDoltNativeNoFallback_CreateCommand tests that the create command also -// does not fall back to SQLite when Dolt connection fails. -func TestDoltNativeNoFallback_CreateCommand(t *testing.T) { - if testing.Short() { - t.Skip("skipping slow integration test in short mode") - } - if runtime.GOOS == windowsOS { - t.Skip("dolt native fallback test not supported on windows") - } - - tmpDir := createTempDirWithCleanup(t) - - // Set up a real git repo. - if err := runCommandInDir(tmpDir, "git", "init"); err != nil { - t.Fatalf("git init failed: %v", err) - } - _ = runCommandInDir(tmpDir, "git", "config", "user.email", "test@example.com") - _ = runCommandInDir(tmpDir, "git", "config", "user.name", "Test User") - - beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.MkdirAll(beadsDir, 0o755); err != nil { - t.Fatalf("failed to create .beads dir: %v", err) - } - - // Write metadata.json with bad Dolt server config. - metadata := map[string]interface{}{ - "database": "dolt", - "backend": "dolt", - "dolt_mode": "server", - "dolt_server_host": "127.0.0.1", - "dolt_server_port": 12346, // Another unreachable port - "dolt_server_user": "root", - "dolt_database": "beads", - } - metadataBytes, err := json.MarshalIndent(metadata, "", " ") - if err != nil { - t.Fatalf("failed to marshal metadata: %v", err) - } - if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), metadataBytes, 0o600); err != nil { - t.Fatalf("failed to write metadata.json: %v", err) - } - - // Write config.yaml with sync.mode=dolt-native. - configYAML := `issue-prefix: "test" -sync.mode: "dolt-native" -` - if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(configYAML), 0o600); err != nil { - t.Fatalf("failed to write config.yaml: %v", err) - } - - env := []string{ - "BEADS_TEST_MODE=1", - "BEADS_NO_DAEMON=1", - "BEADS_DIR=" + beadsDir, - } - - // Attempt to create an issue. This should fail without SQLite fallback. - out, cmdErr := runBDExecAllowErrorWithEnv(t, tmpDir, env, "create", "test issue", "--json") - - // Check if dolt not available - if cmdErr == nil { - lower := strings.ToLower(out) - if strings.Contains(lower, "dolt") && (strings.Contains(lower, "not supported") || strings.Contains(lower, "not available") || strings.Contains(lower, "cgo")) { - t.Skipf("dolt backend not available in this build: %s", out) - } - } - - // The command should have failed (connection refused). - if cmdErr == nil { - // Check if it actually created something (which would be a regression). - t.Logf("create command output: %s", out) - } - - // Grace period for any async operations to manifest. - time.Sleep(50 * time.Millisecond) - - // Verify no SQLite fallback. - sqliteDB := filepath.Join(beadsDir, "beads.db") - if _, err := os.Stat(sqliteDB); err == nil { - t.Fatalf("REGRESSION: SQLite database created by 'create' command in dolt-native mode") - } - - // NOTE: In dolt-native mode, JSONL is now export-only (backup). However, since - // Dolt connection failed above, no storage was created and no export could happen. - jsonlFile := filepath.Join(beadsDir, "issues.jsonl") - if _, err := os.Stat(jsonlFile); err == nil { - t.Fatalf("REGRESSION: JSONL file created by 'create' command in dolt-native mode with no storage") - } - - t.Logf("SUCCESS: 'create' command did not fall back to SQLite/JSONL") -} diff --git a/cmd/bd/events_export.go b/cmd/bd/events_export.go deleted file mode 100644 index dd83b180e..000000000 --- a/cmd/bd/events_export.go +++ /dev/null @@ -1,81 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - "os" - "strconv" - - "github.com/steveyegge/beads/internal/storage/dolt" -) - -// exportEventsToJSONL appends new events to the events JSONL file. -// It reads the last exported event ID from metadata, fetches all events since then, -// appends them as JSON lines, and updates the metadata with the new high-water mark. -func exportEventsToJSONL(ctx context.Context, store *dolt.DoltStore, eventsPath string) error { - // Read last exported event ID from metadata - var sinceID int64 - lastIDStr, err := store.GetMetadata(ctx, "events_last_exported_id") - if err == nil && lastIDStr != "" { - sinceID, err = strconv.ParseInt(lastIDStr, 10, 64) - if err != nil { - return fmt.Errorf("invalid events_last_exported_id metadata %q: %w", lastIDStr, err) - } - } - - // Fetch all events since the last exported ID - events, err := store.GetAllEventsSince(ctx, sinceID) - if err != nil { - return fmt.Errorf("failed to get events since ID %d: %w", sinceID, err) - } - - if len(events) == 0 { - return nil // Nothing new to export - } - - // Open file for appending (create if it doesn't exist) - // #nosec G304 - controlled path from config - f, err := os.OpenFile(eventsPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) - if err != nil { - return fmt.Errorf("failed to open events file: %w", err) - } - defer f.Close() - - // Append each event as a JSON line - encoder := json.NewEncoder(f) - var maxID int64 - for _, event := range events { - if err := encoder.Encode(event); err != nil { - return fmt.Errorf("failed to encode event %d: %w", event.ID, err) - } - if event.ID > maxID { - maxID = event.ID - } - } - - // Update metadata with the new high-water mark - if err := store.SetMetadata(ctx, "events_last_exported_id", strconv.FormatInt(maxID, 10)); err != nil { - return fmt.Errorf("failed to update events_last_exported_id: %w", err) - } - - return nil -} - -// resetEventsExport resets the events export state by clearing the metadata -// and truncating the events JSONL file. Used with --events-reset flag. -func resetEventsExport(ctx context.Context, store *dolt.DoltStore, eventsPath string) error { - // Clear the high-water mark - if err := store.SetMetadata(ctx, "events_last_exported_id", ""); err != nil { - return fmt.Errorf("failed to clear events_last_exported_id: %w", err) - } - - // Truncate the events file if it exists - if _, err := os.Stat(eventsPath); err == nil { - if err := os.Truncate(eventsPath, 0); err != nil { - return fmt.Errorf("failed to truncate events file: %w", err) - } - } - - return nil -} diff --git a/cmd/bd/export.go b/cmd/bd/export.go deleted file mode 100644 index a62bad91b..000000000 --- a/cmd/bd/export.go +++ /dev/null @@ -1,628 +0,0 @@ -package main - -import ( - "cmp" - "encoding/json" - "fmt" - "os" - "path/filepath" - "slices" - "strings" - - "github.com/spf13/cobra" - "github.com/steveyegge/beads/internal/debug" - "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/utils" - "github.com/steveyegge/beads/internal/validation" -) - -// countIssuesInJSONL counts the number of issues in a JSONL file -func countIssuesInJSONL(path string) (int, error) { - // #nosec G304 - controlled path from config - file, err := os.Open(path) - if err != nil { - return 0, err - } - defer func() { - if err := file.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to close file: %v\n", err) - } - }() - - count := 0 - decoder := json.NewDecoder(file) - for { - var issue types.Issue - if err := decoder.Decode(&issue); err != nil { - if err.Error() == "EOF" { - break - } - // Return error for corrupt/invalid JSON - return count, fmt.Errorf("invalid JSON at issue %d: %w", count+1, err) - } - count++ - } - return count, nil -} - -// getIssueIDsFromJSONL reads a JSONL file and returns a set of issue IDs -func getIssueIDsFromJSONL(path string) (map[string]bool, error) { - // #nosec G304 - controlled path from config - file, err := os.Open(path) - if err != nil { - return nil, err - } - defer func() { - if err := file.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to close file: %v\n", err) - } - }() - - ids := make(map[string]bool) - decoder := json.NewDecoder(file) - lineNum := 0 - for { - var issue types.Issue - if err := decoder.Decode(&issue); err != nil { - if err.Error() == "EOF" { - break - } - // Return error for corrupt/invalid JSON - return ids, fmt.Errorf("invalid JSON at line %d: %w", lineNum+1, err) - } - ids[issue.ID] = true - lineNum++ - } - return ids, nil -} - -// validateExportPath checks if the output path is safe to write to -func validateExportPath(path string) error { - // Get absolute path to normalize it - absPath, err := filepath.Abs(path) - if err != nil { - return fmt.Errorf("invalid path: %v", err) - } - - // Convert to lowercase for case-insensitive comparison on Windows - absPathLower := strings.ToLower(absPath) - - // List of sensitive system directories to avoid - sensitiveDirs := []string{ - "c:\\windows", - "c:\\program files", - "c:\\program files (x86)", - "c:\\programdata", - "c:\\system volume information", - "c:\\$recycle.bin", - "c:\\boot", - "c:\\recovery", - } - - for _, dir := range sensitiveDirs { - if strings.HasPrefix(absPathLower, strings.ToLower(dir)) { - return fmt.Errorf("cannot write to sensitive system directory: %s", dir) - } - } - - return nil -} - -var exportCmd = &cobra.Command{ - Use: "export", - GroupID: "sync", - Short: "Export issues to JSONL or Obsidian format", - Long: `Export all issues to JSON Lines or Obsidian Tasks markdown format. -Issues are sorted by ID for consistent diffs. - -Output to stdout by default, or use -o flag for file output. -For obsidian format, defaults to ai_docs/changes-log.md - -Formats: - jsonl - JSON Lines format (one JSON object per line) [default] - obsidian - Obsidian Tasks markdown format with checkboxes, priorities, dates - -Examples: - bd export --status open -o open-issues.jsonl - bd export --format obsidian # outputs to ai_docs/changes-log.md - bd export --format obsidian -o custom.md # outputs to custom.md - bd export --type bug --priority-max 1 - bd export --created-after 2025-01-01 --assignee alice`, - Run: func(cmd *cobra.Command, args []string) { - format, _ := cmd.Flags().GetString("format") - output, _ := cmd.Flags().GetString("output") - statusFilter, _ := cmd.Flags().GetString("status") - force, _ := cmd.Flags().GetBool("force") - eventsFlag, _ := cmd.Flags().GetBool("events") - eventsReset, _ := cmd.Flags().GetBool("events-reset") - - // Additional filter flags - assignee, _ := cmd.Flags().GetString("assignee") - issueType, _ := cmd.Flags().GetString("type") - issueType = utils.NormalizeIssueType(issueType) // Expand aliases (mr→merge-request, etc.) - labels, _ := cmd.Flags().GetStringSlice("label") - labelsAny, _ := cmd.Flags().GetStringSlice("label-any") - priorityMinStr, _ := cmd.Flags().GetString("priority-min") - priorityMaxStr, _ := cmd.Flags().GetString("priority-max") - createdAfter, _ := cmd.Flags().GetString("created-after") - createdBefore, _ := cmd.Flags().GetString("created-before") - updatedAfter, _ := cmd.Flags().GetString("updated-after") - updatedBefore, _ := cmd.Flags().GetString("updated-before") - idFilter, _ := cmd.Flags().GetString("id") - parentID, _ := cmd.Flags().GetString("parent") - - debug.Logf("Debug: export flags - output=%q, force=%v\n", output, force) - - if format != "jsonl" && format != "obsidian" { - fmt.Fprintf(os.Stderr, "Error: format must be 'jsonl' or 'obsidian'\n") - os.Exit(1) - } - - // Default output path for obsidian format - if format == "obsidian" && output == "" { - output = "ai_docs/changes-log.md" - } - - // Export command requires direct database access for consistent snapshot - - // Note: We used to check database file timestamps here, but WAL files - // get created when opening the DB, making timestamp checks unreliable. - // Instead, we check issue counts after loading (see below). - - // Ensure we have a direct store connection - if store == nil { - var err error - if dbPath == "" { - fmt.Fprintf(os.Stderr, "Error: no database path found\n") - os.Exit(1) - } - beadsDir := filepath.Dir(dbPath) - store, err = dolt.NewFromConfigWithOptions(rootCtx, beadsDir, &dolt.Config{}) - if err != nil { - fmt.Fprintf(os.Stderr, "Error: failed to open database: %v\n", err) - os.Exit(1) - } - defer func() { _ = store.Close() }() - } - - // Handle --events and --events-reset flags - if eventsFlag || eventsReset { - eventsPath := filepath.Join(filepath.Dir(dbPath), "events.jsonl") - ctx := rootCtx - - if eventsReset { - if err := resetEventsExport(ctx, store, eventsPath); err != nil { - fmt.Fprintf(os.Stderr, "Error resetting events export: %v\n", err) - os.Exit(1) - } - fmt.Fprintf(os.Stderr, "Events export state reset\n") - } - - if eventsFlag { - if err := exportEventsToJSONL(ctx, store, eventsPath); err != nil { - fmt.Fprintf(os.Stderr, "Error exporting events: %v\n", err) - os.Exit(1) - } - fmt.Fprintf(os.Stderr, "Events exported to %s\n", eventsPath) - } - - return - } - - // Normalize labels: trim, dedupe, remove empty - labels = utils.NormalizeLabels(labels) - labelsAny = utils.NormalizeLabels(labelsAny) - - // Build filter - filter := types.IssueFilter{} - if statusFilter != "" { - status := types.Status(statusFilter) - filter.Status = &status - } - if assignee != "" { - filter.Assignee = &assignee - } - if issueType != "" { - t := types.IssueType(issueType) - filter.IssueType = &t - } - if len(labels) > 0 { - filter.Labels = labels - } - if len(labelsAny) > 0 { - filter.LabelsAny = labelsAny - } - if idFilter != "" { - ids := utils.NormalizeLabels(strings.Split(idFilter, ",")) - if len(ids) > 0 { - filter.IDs = ids - } - } - if parentID != "" { - filter.ParentID = &parentID - } - - // Priority exact match (use Changed() to properly handle P0) - if cmd.Flags().Changed("priority") { - priorityStr, _ := cmd.Flags().GetString("priority") - priority, err := validation.ValidatePriority(priorityStr) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --priority: %v\n", err) - os.Exit(1) - } - filter.Priority = &priority - } - - // Priority ranges - if cmd.Flags().Changed("priority-min") { - priorityMin, err := validation.ValidatePriority(priorityMinStr) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --priority-min: %v\n", err) - os.Exit(1) - } - filter.PriorityMin = &priorityMin - } - if cmd.Flags().Changed("priority-max") { - priorityMax, err := validation.ValidatePriority(priorityMaxStr) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --priority-max: %v\n", err) - os.Exit(1) - } - filter.PriorityMax = &priorityMax - } - - // Date ranges - if createdAfter != "" { - t, err := parseTimeFlag(createdAfter) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --created-after: %v\n", err) - os.Exit(1) - } - filter.CreatedAfter = &t - } - if createdBefore != "" { - t, err := parseTimeFlag(createdBefore) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --created-before: %v\n", err) - os.Exit(1) - } - filter.CreatedBefore = &t - } - if updatedAfter != "" { - t, err := parseTimeFlag(updatedAfter) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --updated-after: %v\n", err) - os.Exit(1) - } - filter.UpdatedAfter = &t - } - if updatedBefore != "" { - t, err := parseTimeFlag(updatedBefore) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing --updated-before: %v\n", err) - os.Exit(1) - } - filter.UpdatedBefore = &t - } - - // Get all issues - ctx := rootCtx - issues, err := store.SearchIssues(ctx, "", filter) - if err != nil { - fmt.Fprintf(os.Stderr, "Error: %v\n", err) - os.Exit(1) - } - - // Detect if any substantive filters are active (partial export) - // Safety checks should be skipped for filtered exports since the user - // intentionally wants a subset of issues, not the full database - isFilteredExport := filter.Status != nil || - filter.Assignee != nil || - filter.IssueType != nil || - len(filter.Labels) > 0 || - len(filter.LabelsAny) > 0 || - len(filter.IDs) > 0 || - filter.ParentID != nil || - filter.Priority != nil || - filter.PriorityMin != nil || - filter.PriorityMax != nil || - filter.CreatedAfter != nil || - filter.CreatedBefore != nil || - filter.UpdatedAfter != nil || - filter.UpdatedBefore != nil - - // Safety check: prevent exporting empty database over non-empty JSONL - // Skip this check for filtered exports - the user intentionally wants a subset - if len(issues) == 0 && output != "" && !force && !isFilteredExport { - existingCount, err := countIssuesInJSONL(output) - if err != nil { - // If we can't read the file, it might not exist yet, which is fine - if !os.IsNotExist(err) { - fmt.Fprintf(os.Stderr, "Warning: failed to read existing JSONL: %v\n", err) - } - } else if existingCount > 0 { - fmt.Fprintf(os.Stderr, "Error: refusing to export empty database over non-empty JSONL file\n") - fmt.Fprintf(os.Stderr, " Database has 0 issues, JSONL has %d issues\n", existingCount) - fmt.Fprintf(os.Stderr, " This would result in data loss!\n") - fmt.Fprintf(os.Stderr, "Hint: Use --force to override this safety check, or delete the JSONL file first:\n") - fmt.Fprintf(os.Stderr, " bd export -o %s --force\n", output) - fmt.Fprintf(os.Stderr, " rm %s\n", output) - os.Exit(1) - } - } - - // Safety check: prevent exporting stale database that would lose issues - // Skip this check for filtered exports - the user intentionally wants a subset - if output != "" && !force && !isFilteredExport { - debug.Logf("Debug: checking staleness - output=%s, force=%v\n", output, force) - - // Read existing JSONL to get issue IDs - jsonlIDs, err := getIssueIDsFromJSONL(output) - if err != nil && !os.IsNotExist(err) { - fmt.Fprintf(os.Stderr, "Warning: failed to read existing JSONL for staleness check: %v\n", err) - } - - if err == nil && len(jsonlIDs) > 0 { - // Build set of DB issue IDs - dbIDs := make(map[string]bool) - for _, issue := range issues { - dbIDs[issue.ID] = true - } - - // Check if JSONL has any issues that DB doesn't have - var missingIDs []string - for id := range jsonlIDs { - if !dbIDs[id] { - missingIDs = append(missingIDs, id) - } - } - - debug.Logf("Debug: JSONL has %d issues, DB has %d issues, missing %d\n", - len(jsonlIDs), len(issues), len(missingIDs)) - - if len(missingIDs) > 0 { - slices.Sort(missingIDs) - fmt.Fprintf(os.Stderr, "Error: refusing to export stale database that would lose issues\n") - fmt.Fprintf(os.Stderr, " Database has %d issues\n", len(issues)) - fmt.Fprintf(os.Stderr, " JSONL has %d issues\n", len(jsonlIDs)) - fmt.Fprintf(os.Stderr, " Export would lose %d issue(s):\n", len(missingIDs)) - - // Show first 10 missing issues - showCount := len(missingIDs) - if showCount > 10 { - showCount = 10 - } - for i := 0; i < showCount; i++ { - fmt.Fprintf(os.Stderr, " - %s\n", missingIDs[i]) - } - if len(missingIDs) > 10 { - fmt.Fprintf(os.Stderr, " ... and %d more\n", len(missingIDs)-10) - } - - fmt.Fprintf(os.Stderr, "\n") - fmt.Fprintf(os.Stderr, "This usually means:\n") - fmt.Fprintf(os.Stderr, " 1. You need to run 'bd import -i %s' to sync the latest changes\n", output) - fmt.Fprintf(os.Stderr, " 2. Or another workspace added issues that weren't synced to this database\n") - fmt.Fprintf(os.Stderr, "\n") - fmt.Fprintf(os.Stderr, "To force export anyway (will lose these issues):\n") - fmt.Fprintf(os.Stderr, " bd export -o %s --force\n", output) - os.Exit(1) - } - } - } - - // Filter out wisps - they should never be exported to JSONL - // Wisps exist only in SQLite and are shared via .beads/redirect, not JSONL. - filtered := make([]*types.Issue, 0, len(issues)) - for _, issue := range issues { - if !issue.Ephemeral { - filtered = append(filtered, issue) - } - } - issues = filtered - - // Sort by ID for consistent output - slices.SortFunc(issues, func(a, b *types.Issue) int { - return cmp.Compare(a.ID, b.ID) - }) - - // Populate dependencies for all issues in one query (avoids N+1 problem) - allDeps, err := store.GetAllDependencyRecords(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "Error getting dependencies: %v\n", err) - os.Exit(1) - } - for _, issue := range issues { - issue.Dependencies = allDeps[issue.ID] - } - - // Populate labels and comments for all issues (batch APIs) - ids := make([]string, 0, len(issues)) - for _, issue := range issues { - ids = append(ids, issue.ID) - } - - labelsMap, err := store.GetLabelsForIssues(ctx, ids) - if err != nil { - fmt.Fprintf(os.Stderr, "Error getting labels: %v\n", err) - os.Exit(1) - } - commentsMap, err := store.GetCommentsForIssues(ctx, ids) - if err != nil { - fmt.Fprintf(os.Stderr, "Error getting comments: %v\n", err) - os.Exit(1) - } - - for _, issue := range issues { - issue.Labels = labelsMap[issue.ID] - issue.Comments = commentsMap[issue.ID] - } - - // Open output - out := os.Stdout - var tempFile *os.File - var tempPath string - var finalPath string - if output != "" { - // Validate output path before creating files - if err := validateExportPath(output); err != nil { - fmt.Fprintf(os.Stderr, "Error: %v\n", err) - os.Exit(1) - } - - // Create temporary file in same directory for atomic rename - dir := filepath.Dir(output) - base := filepath.Base(output) - - // Ensure output directory exists - if err := os.MkdirAll(dir, 0755); err != nil { - fmt.Fprintf(os.Stderr, "Error creating output directory: %v\n", err) - os.Exit(1) - } - - var err error - tempFile, err = os.CreateTemp(dir, base+".tmp.*") - if err != nil { - fmt.Fprintf(os.Stderr, "Error creating temporary file: %v\n", err) - os.Exit(1) - } - tempPath = tempFile.Name() - finalPath = output - - // Ensure cleanup on failure - defer func() { - if tempFile != nil { - _ = tempFile.Close() - _ = os.Remove(tempPath) // Clean up temp file if we haven't renamed it - } - }() - - out = tempFile - } - - // Write output based on format - exportedIDs := make([]string, 0, len(issues)) - skippedCount := 0 - - if format == "obsidian" { - // Write Obsidian Tasks markdown format - if err := writeObsidianExport(out, issues); err != nil { - fmt.Fprintf(os.Stderr, "Error writing Obsidian export: %v\n", err) - os.Exit(1) - } - for _, issue := range issues { - exportedIDs = append(exportedIDs, issue.ID) - } - } else { - // Write JSONL (timestamp-only deduplication DISABLED due to bd-160) - encoder := json.NewEncoder(out) - for _, issue := range issues { - if err := encoder.Encode(issue); err != nil { - fmt.Fprintf(os.Stderr, "Error encoding issue %s: %v\n", issue.ID, err) - os.Exit(1) - } - exportedIDs = append(exportedIDs, issue.ID) - } - } - - // Report skipped issues if any (helps debugging bd-159) - if skippedCount > 0 && output == findJSONLPath() { - fmt.Fprintf(os.Stderr, "Skipped %d issue(s) with timestamp-only changes\n", skippedCount) - } - - // Suppress "unused" for exportedIDs (used in JSON stats below) - _ = exportedIDs - - // If writing to file, atomically replace the target file - if tempFile != nil { - // Close the temp file before renaming - if err := tempFile.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to close temporary file: %v\n", err) - } - tempFile = nil // Prevent cleanup - - // Atomically replace the target file - if err := os.Rename(tempPath, finalPath); err != nil { - _ = os.Remove(tempPath) // Clean up on failure - fmt.Fprintf(os.Stderr, "Error replacing output file: %v\n", err) - os.Exit(1) - } - - // Set appropriate file permissions (0600: rw-------) - // Skip chmod for symlinks - os.Chmod follows symlinks and would change the target's - // permissions, which may be in a read-only location (e.g., /nix/store on NixOS). - if info, err := os.Lstat(finalPath); err == nil && info.Mode()&os.ModeSymlink == 0 { - if err := os.Chmod(finalPath, 0600); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to set file permissions: %v\n", err) - } - } - - // Verify JSONL file integrity after export (skip for other formats) - if format == "jsonl" { - actualCount, err := countIssuesInJSONL(finalPath) - if err != nil { - fmt.Fprintf(os.Stderr, "Error: Export verification failed: %v\n", err) - os.Exit(1) - } - if actualCount != len(exportedIDs) { - fmt.Fprintf(os.Stderr, "Error: Export verification failed\n") - fmt.Fprintf(os.Stderr, " Expected: %d issues\n", len(exportedIDs)) - fmt.Fprintf(os.Stderr, " JSONL file: %d lines\n", actualCount) - fmt.Fprintf(os.Stderr, " Mismatch indicates export failed to write all issues\n") - os.Exit(1) - } - } - } - - // Output statistics if JSON format requested - if jsonOutput { - stats := map[string]interface{}{ - "success": true, - "exported": len(exportedIDs), - "skipped": skippedCount, - "total_issues": len(issues), - } - if output != "" { - stats["output_file"] = output - } - data, _ := json.MarshalIndent(stats, "", " ") - fmt.Fprintln(os.Stderr, string(data)) - } - }, -} - -func init() { - exportCmd.Flags().StringP("format", "f", "jsonl", "Export format: jsonl, obsidian") - exportCmd.Flags().StringP("output", "o", "", "Output file (default: stdout)") - exportCmd.Flags().StringP("status", "s", "", "Filter by status") - exportCmd.Flags().Bool("force", false, "Force export even if database is empty") - exportCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output export statistics in JSON format") - exportCmd.Flags().Bool("events", false, "Export events to .beads/events.jsonl (append-only)") - exportCmd.Flags().Bool("events-reset", false, "Reset events export state and truncate events.jsonl") - - // Filter flags - registerPriorityFlag(exportCmd, "") - exportCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") - exportCmd.Flags().StringP("type", "t", "", "Filter by type (bug, feature, task, epic, chore, decision, merge-request, molecule, gate). Aliases: mr→merge-request, feat→feature, mol→molecule, dec/adr→decision") - exportCmd.Flags().StringSliceP("label", "l", []string{}, "Filter by labels (AND: must have ALL)") - exportCmd.Flags().StringSlice("label-any", []string{}, "Filter by labels (OR: must have AT LEAST ONE)") - - // Priority filters - exportCmd.Flags().String("priority-min", "", "Filter by minimum priority (inclusive, 0-4 or P0-P4)") - exportCmd.Flags().String("priority-max", "", "Filter by maximum priority (inclusive, 0-4 or P0-P4)") - - // Date filters - exportCmd.Flags().String("created-after", "", "Filter issues created after date (YYYY-MM-DD or RFC3339)") - exportCmd.Flags().String("created-before", "", "Filter issues created before date (YYYY-MM-DD or RFC3339)") - exportCmd.Flags().String("updated-after", "", "Filter issues updated after date (YYYY-MM-DD or RFC3339)") - exportCmd.Flags().String("updated-before", "", "Filter issues updated before date (YYYY-MM-DD or RFC3339)") - - // ID filter - exportCmd.Flags().String("id", "", "Filter by specific issue IDs (comma-separated, e.g., bd-1,bd-5,bd-10)") - - // Parent filter - exportCmd.Flags().String("parent", "", "Filter by parent issue ID (shows children of specified issue)") - - rootCmd.AddCommand(exportCmd) -} diff --git a/cmd/bd/hook.go b/cmd/bd/hook.go deleted file mode 100644 index 363c86ea8..000000000 --- a/cmd/bd/hook.go +++ /dev/null @@ -1,776 +0,0 @@ -package main - -import ( - "bufio" - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - "os" - "os/exec" - "path/filepath" - "strings" - "time" - - "github.com/spf13/cobra" - "github.com/steveyegge/beads/internal/beads" - "github.com/steveyegge/beads/internal/config" - "github.com/steveyegge/beads/internal/configfile" - "github.com/steveyegge/beads/internal/git" - "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" -) - -// jsonlFilePaths lists all JSONL files that should be staged/tracked. -// Includes beads.jsonl for backwards compatibility with older installations. -var jsonlFilePaths = []string{ - ".beads/issues.jsonl", - ".beads/deletions.jsonl", - ".beads/interactions.jsonl", - ".beads/beads.jsonl", // Legacy filename, kept for backwards compatibility -} - -// hookCmd is the main "bd hook" command that git hooks call into. -// This is distinct from "bd hooks" (plural) which manages hook installation. -var hookCmd = &cobra.Command{ - Use: "hook [args...]", - Short: "Execute a git hook (called by hook scripts)", - Long: `Execute the logic for a git hook. This command is called by -hook scripts installed in .beads/hooks/ (or .git/hooks/). - -Supported hooks: - - pre-commit: Export database to JSONL, stage changes - - post-merge: Import JSONL to database after pull/merge - - post-checkout: Import JSONL after branch checkout (with guard) - -The hook scripts delegate to this command so hook behavior is always -in sync with the installed bd version. - -Configuration (.beads/config.yaml): - hooks: - chain_strategy: before # before | after | replace - chain_timeout_ms: 5000 # Timeout for chained hooks`, - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - hookName := args[0] - hookArgs := args[1:] - - var exitCode int - switch hookName { - case "pre-commit": - exitCode = hookPreCommit() - case "post-merge": - exitCode = hookPostMerge(hookArgs) - case "post-checkout": - exitCode = hookPostCheckout(hookArgs) - default: - FatalError("unknown hook: %s", hookName) - } - - os.Exit(exitCode) - }, -} - -// ============================================================================= -// Export State Tracking (per-worktree) -// ============================================================================= - -// ExportState tracks the export state for a specific worktree. -// This prevents polecats sharing a Dolt DB from exporting uncommitted -// work from other polecats. See design doc Part 21. -// -// Key insight: We track Dolt commit hash (not git commit) because: -// - Dolt is the source of truth for issue data -// - We use dolt_diff() to detect if changes exist since last export -// - Each worktree may have exported at different Dolt commits -type ExportState struct { - WorktreeRoot string `json:"worktree_root"` - WorktreeHash string `json:"worktree_hash,omitempty"` // Hash of worktree path (for debugging) - LastExportCommit string `json:"last_export_commit"` // Dolt commit hash when last exported - LastExportTime time.Time `json:"last_export_time"` - JSONLHash string `json:"jsonl_hash,omitempty"` // Hash of JSONL at last export -} - -// getWorktreeHash returns a hash of the worktree root for use in filenames. -func getWorktreeHash(worktreeRoot string) string { - h := sha256.Sum256([]byte(worktreeRoot)) - return hex.EncodeToString(h[:8]) // Use first 8 bytes (16 hex chars) -} - -// getExportStateDir returns the path to the export state directory. -func getExportStateDir(beadsDir string) string { - return filepath.Join(beadsDir, "export-state") -} - -// getExportStatePath returns the path to the export state file for this worktree. -func getExportStatePath(beadsDir, worktreeRoot string) string { - return filepath.Join(getExportStateDir(beadsDir), getWorktreeHash(worktreeRoot)+".json") -} - -// loadExportState loads the export state for the current worktree. -func loadExportState(beadsDir, worktreeRoot string) (*ExportState, error) { - path := getExportStatePath(beadsDir, worktreeRoot) - data, err := os.ReadFile(path) // #nosec G304 -- path is constructed from beadsDir - if err != nil { - if os.IsNotExist(err) { - return nil, nil // No state yet - } - return nil, err - } - - var state ExportState - if err := json.Unmarshal(data, &state); err != nil { - return nil, err - } - return &state, nil -} - -// saveExportState saves the export state for the current worktree. -func saveExportState(beadsDir, worktreeRoot string, state *ExportState) error { - dir := getExportStateDir(beadsDir) - if err := os.MkdirAll(dir, 0750); err != nil { - return fmt.Errorf("creating export-state directory: %w", err) - } - - data, err := json.MarshalIndent(state, "", " ") - if err != nil { - return err - } - - path := getExportStatePath(beadsDir, worktreeRoot) - // #nosec G306 -- state file in .beads directory - return os.WriteFile(path, data, 0644) -} - -// computeJSONLHashForHook computes a hash of the JSONL file contents for hook state tracking. -// This is a wrapper around computeJSONLHash that handles missing files gracefully. -func computeJSONLHashForHook(jsonlPath string) (string, error) { - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - if os.IsNotExist(err) { - return "", nil - } - return "", err - } - return hash, nil -} - -// getCurrentGitCommit returns the current git HEAD commit hash. -func getCurrentGitCommit() (string, error) { - cmd := exec.Command("git", "rev-parse", "HEAD") - out, err := cmd.Output() - if err != nil { - return "", err - } - return strings.TrimSpace(string(out)), nil -} - -// getWorktreeRoot returns the root of the current worktree. -func getWorktreeRoot() (string, error) { - cmd := exec.Command("git", "rev-parse", "--show-toplevel") - out, err := cmd.Output() - if err != nil { - return "", err - } - return strings.TrimSpace(string(out)), nil -} - -// ============================================================================= -// Hook Chaining Configuration -// ============================================================================= - -// HookChainStrategy defines how to chain with existing hooks. -type HookChainStrategy string - -const ( - ChainBefore HookChainStrategy = "before" // Run existing hook before bd hook - ChainAfter HookChainStrategy = "after" // Run existing hook after bd hook - ChainReplace HookChainStrategy = "replace" // Replace existing hook entirely -) - -// HookConfig holds hook-related configuration from config.yaml. -type HookConfig struct { - ChainStrategy HookChainStrategy `yaml:"chain_strategy" json:"chain_strategy"` - ChainTimeoutMs int `yaml:"chain_timeout_ms" json:"chain_timeout_ms"` -} - -// DefaultHookConfig returns the default hook configuration. -func DefaultHookConfig() *HookConfig { - return &HookConfig{ - ChainStrategy: ChainBefore, - ChainTimeoutMs: 5000, - } -} - -// loadHookConfig loads hook configuration from config.yaml. -func loadHookConfig(beadsDir string) *HookConfig { - cfg := DefaultHookConfig() - - configPath := filepath.Join(beadsDir, "config.yaml") - data, err := os.ReadFile(configPath) // #nosec G304 -- config path is trusted - if err != nil { - return cfg - } - - // Simple YAML parsing for hooks config - lines := strings.Split(string(data), "\n") - inHooks := false - for _, line := range lines { - trimmed := strings.TrimSpace(line) - if trimmed == "hooks:" { - inHooks = true - continue - } - if inHooks { - if !strings.HasPrefix(line, " ") && !strings.HasPrefix(line, "\t") && trimmed != "" { - inHooks = false - continue - } - if strings.HasPrefix(trimmed, "chain_strategy:") { - value := strings.TrimPrefix(trimmed, "chain_strategy:") - value = strings.TrimSpace(value) - value = strings.Trim(value, `"'`) - switch value { - case "before": - cfg.ChainStrategy = ChainBefore - case "after": - cfg.ChainStrategy = ChainAfter - case "replace": - cfg.ChainStrategy = ChainReplace - } - } - if strings.HasPrefix(trimmed, "chain_timeout_ms:") { - value := strings.TrimPrefix(trimmed, "chain_timeout_ms:") - value = strings.TrimSpace(value) - var timeout int - if _, err := fmt.Sscanf(value, "%d", &timeout); err == nil && timeout > 0 { - cfg.ChainTimeoutMs = timeout - } - } - } - } - - return cfg -} - -// runChainedHookWithConfig runs a chained hook with timeout from config. -func runChainedHookWithConfig(hookName string, args []string, cfg *HookConfig) int { - if cfg.ChainStrategy == ChainReplace { - return 0 // Skip chained hook - } - - // Get the hooks directory - hooksDir, err := getHooksDir() - if err != nil { - return 0 - } - - oldHookPath := filepath.Join(hooksDir, hookName+".old") - - // Check if the .old hook exists and is executable - info, err := os.Stat(oldHookPath) - if err != nil { - return 0 // No chained hook - } - if info.Mode().Perm()&0111 == 0 { - return 0 // Not executable - } - - // Check if .old is itself a bd shim - skip to prevent infinite recursion - versionInfo, err := getHookVersion(oldHookPath) - if err == nil && versionInfo.IsShim { - return 0 - } - - // Create context with timeout - timeout := time.Duration(cfg.ChainTimeoutMs) * time.Millisecond - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - - // Run the chained hook - // #nosec G204 -- hookName is from controlled list, path is from hooks directory - cmd := exec.CommandContext(ctx, oldHookPath, args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin - - if err := cmd.Run(); err != nil { - if ctx.Err() == context.DeadlineExceeded { - fmt.Fprintf(os.Stderr, "Warning: chained hook %s timed out after %dms\n", hookName, cfg.ChainTimeoutMs) - return 0 // Don't block on timeout, just warn - } - if exitErr, ok := err.(*exec.ExitError); ok { - return exitErr.ExitCode() - } - fmt.Fprintf(os.Stderr, "Warning: chained hook %s failed: %v\n", hookName, err) - return 1 - } - - return 0 -} - -// getHooksDir returns the hooks directory (.beads/hooks/ or .git/hooks/). -func getHooksDir() (string, error) { - // First check for .beads/hooks/ (preferred location) - beadsDir := beads.FindBeadsDir() - if beadsDir != "" { - beadsHooksDir := filepath.Join(beadsDir, "hooks") - if _, err := os.Stat(beadsHooksDir); err == nil { - return beadsHooksDir, nil - } - } - - // Fall back to git hooks directory - return git.GetGitHooksDir() -} - -// ============================================================================= -// Hook Implementations -// ============================================================================= - -// hookPreCommit implements the pre-commit hook: Export database to JSONL. -func hookPreCommit() int { - beadsDir := beads.FindBeadsDir() - if beadsDir == "" { - return 0 // Not a beads workspace - } - - cfg := loadHookConfig(beadsDir) - - // Run chained hook based on strategy - if cfg.ChainStrategy == ChainBefore { - if exitCode := runChainedHookWithConfig("pre-commit", nil, cfg); exitCode != 0 { - return exitCode - } - } - - // Get worktree root for per-worktree state tracking - worktreeRoot, err := getWorktreeRoot() - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not determine worktree root: %v\n", err) - worktreeRoot = beadsDir // Fallback - } - - // Check if we're using Dolt backend - use branch-then-merge pattern. - // Dolt export must run even when sync-branch is configured, otherwise - // the JSONL file never gets updated and bd doctor reports a count mismatch. - backend := dolt.GetBackendFromConfig(beadsDir) - if backend == configfile.BackendDolt { - exitCode := hookPreCommitDolt(beadsDir, worktreeRoot) - if cfg.ChainStrategy == ChainAfter && exitCode == 0 { - return runChainedHookWithConfig("pre-commit", nil, cfg) - } - return exitCode - } - - // Backend is always Dolt; non-Dolt paths removed. - return 0 -} - -// hookPreCommitDolt implements pre-commit for Dolt backend. -// Export Dolt → JSONL with per-worktree state tracking. -// -// Per design doc Part 21, this function: -// 1. Loads export state for the current worktree -// 2. Gets the current Dolt commit hash (not git commit) -// 3. Checks if export is needed (skip if already exported this commit) -// 4. Exports to JSONL and saves new state -// -// Future: Use dolt_diff() for incremental export and BD_ACTOR filtering. -func hookPreCommitDolt(beadsDir, worktreeRoot string) int { - // In dolt-native mode, Dolt is the source of truth — skip JSONL export - if config.GetSyncMode() == config.SyncModeDoltNative { - return 0 - } - - ctx := context.Background() - - // Load previous export state for this worktree - prevState, _ := loadExportState(beadsDir, worktreeRoot) - - // Open storage using config — use a local variable to avoid shadowing the global store. - hookStore, err := dolt.NewFromConfig(ctx, beadsDir) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not open database: %v\n", err) - return 0 - } - defer func() { _ = hookStore.Close() }() - - // Get current Dolt commit hash - currentDoltCommit, err := hookStore.GetCurrentCommit(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not get Dolt commit: %v\n", err) - // Fall back to full export without commit tracking - doExportAndSaveState(ctx, hookStore, beadsDir, worktreeRoot, "") - return 0 - } - - // Check if we've already exported for this Dolt commit (idempotency) - if prevState != nil && prevState.LastExportCommit == currentDoltCommit { - // Already exported for this commit, skip - return 0 - } - - // Check if there are actual changes to export (optimization) - if prevState != nil && prevState.LastExportCommit != "" { - hasChanges, err := hasDoltChanges(ctx, hookStore, prevState.LastExportCommit, currentDoltCommit) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not check for changes: %v\n", err) - // Continue with export to be safe - } else if !hasChanges { - // No changes, but update state to track new commit - updateExportStateCommit(beadsDir, worktreeRoot, currentDoltCommit) - return 0 - } - } - - doExportAndSaveState(ctx, hookStore, beadsDir, worktreeRoot, currentDoltCommit) - return 0 -} - -// doExportAndSaveState performs the export and saves state. Shared by main path and fallback. -// Uses the already-open store directly to avoid spawning a subprocess that would -// deadlock on the same Dolt access lock. -func doExportAndSaveState(ctx context.Context, s *dolt.DoltStore, beadsDir, worktreeRoot, doltCommit string) { - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - - // Export to JSONL using the already-open store - if err := exportToJSONLWithStore(ctx, s, jsonlPath); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not export to JSONL: %v\n", err) - return - } - - stageJSONLFiles(ctx) - - // Save export state - jsonlHash, _ := computeJSONLHashForHook(jsonlPath) - state := &ExportState{ - WorktreeRoot: worktreeRoot, - WorktreeHash: getWorktreeHash(worktreeRoot), - LastExportCommit: doltCommit, // Empty string if Dolt commit unavailable - LastExportTime: time.Now(), - JSONLHash: jsonlHash, - } - if err := saveExportState(beadsDir, worktreeRoot, state); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not save export state: %v\n", err) - } -} - -// hasDoltChanges checks if there are any changes between two Dolt commits. -func hasDoltChanges(ctx context.Context, store *dolt.DoltStore, fromCommit, toCommit string) (bool, error) { - diffs, err := store.Diff(ctx, fromCommit, toCommit) - if err != nil { - return false, err - } - return len(diffs) > 0, nil -} - -// updateExportStateCommit updates just the commit hash in the export state. -// Used when we detect no changes but want to track the new commit. -func updateExportStateCommit(beadsDir, worktreeRoot, doltCommit string) { - prevState, err := loadExportState(beadsDir, worktreeRoot) - if err != nil || prevState == nil { - return // Can't update what doesn't exist - } - prevState.LastExportCommit = doltCommit - prevState.LastExportTime = time.Now() - _ = saveExportState(beadsDir, worktreeRoot, prevState) // Best effort: export state is advisory -} - -// stageJSONLFiles stages JSONL files for git commit (unless BEADS_NO_AUTO_STAGE is set). -func stageJSONLFiles(ctx context.Context) { - if os.Getenv("BEADS_NO_AUTO_STAGE") != "" { - return - } - - rc, rcErr := beads.GetRepoContext() - for _, f := range jsonlFilePaths { - if _, err := os.Stat(f); err == nil { - var gitAdd *exec.Cmd - if rcErr == nil { - gitAdd = rc.GitCmdCWD(ctx, "add", f) - } else { - // #nosec G204 -- f comes from jsonlFilePaths (controlled, hardcoded paths) - gitAdd = exec.Command("git", "add", f) - } - _ = gitAdd.Run() // Best effort: file may not be tracked in git - } - } -} - -// hookPostMerge implements the post-merge hook: Import JSONL to database. -func hookPostMerge(args []string) int { - beadsDir := beads.FindBeadsDir() - if beadsDir == "" { - return 0 // Not a beads workspace - } - - cfg := loadHookConfig(beadsDir) - - // Run chained hook based on strategy - if cfg.ChainStrategy == ChainBefore { - if exitCode := runChainedHookWithConfig("post-merge", args, cfg); exitCode != 0 { - return exitCode - } - } - - // Skip during rebase - if isRebaseInProgress() { - if cfg.ChainStrategy == ChainAfter { - return runChainedHookWithConfig("post-merge", args, cfg) - } - return 0 - } - - // Check if any JSONL file exists - if !hasBeadsJSONL() { - if cfg.ChainStrategy == ChainAfter { - return runChainedHookWithConfig("post-merge", args, cfg) - } - return 0 - } - - // Check if we're using Dolt backend - use branch-then-merge pattern - backend := dolt.GetBackendFromConfig(beadsDir) - if backend == configfile.BackendDolt { - exitCode := hookPostMergeDolt(beadsDir) - if cfg.ChainStrategy == ChainAfter && exitCode == 0 { - return runChainedHookWithConfig("post-merge", args, cfg) - } - return exitCode - } - - // Backend is always Dolt; non-Dolt paths removed. - return 0 -} - -// hookPostMergeDolt implements post-merge for Dolt backend. -// Import JSONL → Dolt using branch-then-merge pattern: -// 1. Create jsonl-import branch -// 2. Import JSONL data to branch -// 3. Merge branch to main (cell-level conflict resolution) -// 4. Delete branch on success -func hookPostMergeDolt(beadsDir string) int { - ctx := context.Background() - - // Open storage using config to get the correct database name (e.g. "beads_bd") - store, err := dolt.NewFromConfig(ctx, beadsDir) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not open database: %v\n", err) - return 0 - } - defer func() { _ = store.Close() }() - - // Get current branch - currentBranch, err := store.CurrentBranch(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not get current branch: %v\n", err) - return 0 - } - - // Create import branch - importBranch := "jsonl-import-" + time.Now().Format("20060102-150405") - if err := store.Branch(ctx, importBranch); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not create import branch: %v\n", err) - return 0 - } - - // Checkout import branch - if err := store.Checkout(ctx, importBranch); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not checkout import branch: %v\n", err) - return 0 - } - - // Import JSONL to the import branch - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - if err := importFromJSONLToStore(ctx, store, jsonlPath); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not import JSONL: %v\n", err) - // Checkout back to original branch - _ = store.Checkout(ctx, currentBranch) // Best effort: restore branch on error path - return 0 - } - - // Commit changes on import branch - // This hook flow commits to Dolt explicitly; avoid redundant auto-commit in PersistentPostRun. - commandDidExplicitDoltCommit = true - if err := store.Commit(ctx, "Import from JSONL"); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not commit import: %v\n", err) - } - - // Checkout back to original branch - if err := store.Checkout(ctx, currentBranch); err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not checkout original branch: %v\n", err) - return 0 - } - - // Merge import branch (Dolt provides cell-level merge) - conflicts, err := store.Merge(ctx, importBranch) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: could not merge import branch: %v\n", err) - return 0 - } - if len(conflicts) > 0 { - fmt.Fprintf(os.Stderr, "Warning: %d conflict(s) detected during Dolt merge; resolve with 'bd federation conflicts' or Dolt conflict tooling\n", len(conflicts)) - // Best-effort: still return 0 to avoid blocking git merge, consistent with other hook warnings. - } - - // Commit the merge - // Still part of explicit hook commit flow. - commandDidExplicitDoltCommit = true - if err := store.Commit(ctx, "Merge JSONL import"); err != nil { - // May fail if nothing to commit (fast-forward merge) - // This is expected, not an error - } - - // Clean up import branch - if err := store.DeleteBranch(ctx, importBranch); err != nil { - // Non-fatal - branch cleanup is best-effort - fmt.Fprintf(os.Stderr, "Warning: could not delete import branch %s: %v\n", importBranch, err) - } - - return 0 -} - -// hookPostCheckout implements the post-checkout hook with guard. -// Only imports if JSONL actually changed since last import. -func hookPostCheckout(args []string) int { - // Only run on branch checkouts (flag=1) - if len(args) >= 3 && args[2] != "1" { - return 0 - } - - beadsDir := beads.FindBeadsDir() - if beadsDir == "" { - return 0 // Not a beads workspace - } - - cfg := loadHookConfig(beadsDir) - - // Run chained hook based on strategy - if cfg.ChainStrategy == ChainBefore { - if exitCode := runChainedHookWithConfig("post-checkout", args, cfg); exitCode != 0 { - return exitCode - } - } - - // Skip during rebase - if isRebaseInProgress() { - if cfg.ChainStrategy == ChainAfter { - return runChainedHookWithConfig("post-checkout", args, cfg) - } - return 0 - } - - // Check if any JSONL file exists - if !hasBeadsJSONL() { - if cfg.ChainStrategy == ChainAfter { - return runChainedHookWithConfig("post-checkout", args, cfg) - } - return 0 - } - - // Guard: Only import if JSONL actually changed - worktreeRoot, err := getWorktreeRoot() - if err != nil { - worktreeRoot = beadsDir // Fallback - } - - prevState, _ := loadExportState(beadsDir, worktreeRoot) - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - currentHash, _ := computeJSONLHashForHook(jsonlPath) - - if prevState != nil && prevState.JSONLHash == currentHash { - // JSONL hasn't changed, skip redundant import - if cfg.ChainStrategy == ChainAfter { - return runChainedHookWithConfig("post-checkout", args, cfg) - } - return 0 - } - - // Detect git worktree and show warning - if isGitWorktree() { - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "╔══════════════════════════════════════════════════════════════════════════╗") - fmt.Fprintln(os.Stderr, "║ Welcome to beads in git worktree! ║") - fmt.Fprintln(os.Stderr, "╠══════════════════════════════════════════════════════════════════════════╣") - fmt.Fprintln(os.Stderr, "║ Note: Daemon mode is not recommended with git worktrees. ║") - fmt.Fprintln(os.Stderr, "║ Worktrees share the same database, and the daemon may commit changes ║") - fmt.Fprintln(os.Stderr, "║ to the wrong branch. ║") - fmt.Fprintln(os.Stderr, "║ ║") - fmt.Fprintln(os.Stderr, "║ RECOMMENDED: Disable daemon for this session: ║") - fmt.Fprintln(os.Stderr, "║ export BEADS_NO_DAEMON=1 ║") - fmt.Fprintln(os.Stderr, "╚══════════════════════════════════════════════════════════════════════════╝") - fmt.Fprintln(os.Stderr, "") - } - - // Check if we're using Dolt backend - backend := dolt.GetBackendFromConfig(beadsDir) - if backend == configfile.BackendDolt { - exitCode := hookPostMergeDolt(beadsDir) // Same as post-merge for Dolt - // Update state after import - newHash, _ := computeJSONLHashForHook(jsonlPath) - currentCommit, _ := getCurrentGitCommit() - state := &ExportState{ - WorktreeRoot: worktreeRoot, - LastExportCommit: currentCommit, - LastExportTime: time.Now(), - JSONLHash: newHash, - } - _ = saveExportState(beadsDir, worktreeRoot, state) // Best effort: export state is advisory - - if cfg.ChainStrategy == ChainAfter && exitCode == 0 { - return runChainedHookWithConfig("post-checkout", args, cfg) - } - return exitCode - } - - // Backend is always Dolt; non-Dolt paths removed. - return 0 -} - -// ============================================================================= -// Helper Functions for Dolt Import/Export -// ============================================================================= - -// importFromJSONLToStore imports issues from JSONL to a store. -func importFromJSONLToStore(ctx context.Context, store *dolt.DoltStore, jsonlPath string) error { - // Parse JSONL into issues - // #nosec G304 - jsonlPath is derived from beadsDir (trusted workspace path) - f, err := os.Open(jsonlPath) - if err != nil { - return err - } - defer func() { _ = f.Close() }() - - scanner := bufio.NewScanner(f) - // 2MB buffer for large issues - scanner.Buffer(make([]byte, 0, 1024), 2*1024*1024) - - var allIssues []*types.Issue - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line == "" { - continue - } - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err != nil { - return err - } - issue.SetDefaults() - allIssues = append(allIssues, &issue) - } - if err := scanner.Err(); err != nil { - return err - } - - // Import using shared logic (no subprocess). - // Use store.Path() as the database path (works for both sqlite and dolt). - opts := ImportOptions{} - _, err = importIssuesCore(ctx, store.Path(), store, allIssues, opts) - return err -} - -func init() { - rootCmd.AddCommand(hookCmd) -} diff --git a/cmd/bd/hook_test.go b/cmd/bd/hook_test.go deleted file mode 100644 index 4e80a7882..000000000 --- a/cmd/bd/hook_test.go +++ /dev/null @@ -1,223 +0,0 @@ -package main - -import ( - "encoding/json" - "os" - "path/filepath" - "strings" - "testing" - "time" -) - -func TestGetWorktreeHash(t *testing.T) { - // Same input should produce same hash - hash1 := getWorktreeHash("/some/path/to/worktree") - hash2 := getWorktreeHash("/some/path/to/worktree") - if hash1 != hash2 { - t.Errorf("Same path produced different hashes: %s vs %s", hash1, hash2) - } - - // Different inputs should produce different hashes - hash3 := getWorktreeHash("/different/path") - if hash1 == hash3 { - t.Errorf("Different paths produced same hash: %s", hash1) - } - - // Hash should be 16 hex chars (8 bytes) - if len(hash1) != 16 { - t.Errorf("Hash length = %d, want 16", len(hash1)) - } -} - -func TestExportStatePaths(t *testing.T) { - beadsDir := "/tmp/test/.beads" - worktreeRoot := "/tmp/test/worktree" - - stateDir := getExportStateDir(beadsDir) - if stateDir != "/tmp/test/.beads/export-state" { - t.Errorf("getExportStateDir() = %s, want /tmp/test/.beads/export-state", stateDir) - } - - statePath := getExportStatePath(beadsDir, worktreeRoot) - expectedHash := getWorktreeHash(worktreeRoot) - expectedPath := "/tmp/test/.beads/export-state/" + expectedHash + ".json" - if statePath != expectedPath { - t.Errorf("getExportStatePath() = %s, want %s", statePath, expectedPath) - } -} - -func TestSaveAndLoadExportState(t *testing.T) { - tmpDir := t.TempDir() - beadsDir := filepath.Join(tmpDir, ".beads") - worktreeRoot := tmpDir - - // Initially no state - state, err := loadExportState(beadsDir, worktreeRoot) - if err != nil { - t.Fatalf("loadExportState() failed: %v", err) - } - if state != nil { - t.Errorf("loadExportState() returned non-nil for missing state") - } - - // Save state - now := time.Now().Truncate(time.Second) // Truncate for comparison - testState := &ExportState{ - WorktreeRoot: worktreeRoot, - WorktreeHash: getWorktreeHash(worktreeRoot), - LastExportCommit: "abc123def456", - LastExportTime: now, - JSONLHash: "hashvalue", - } - if err := saveExportState(beadsDir, worktreeRoot, testState); err != nil { - t.Fatalf("saveExportState() failed: %v", err) - } - - // Load state back - loaded, err := loadExportState(beadsDir, worktreeRoot) - if err != nil { - t.Fatalf("loadExportState() failed: %v", err) - } - if loaded == nil { - t.Fatal("loadExportState() returned nil") - } - - // Verify fields - if loaded.WorktreeRoot != testState.WorktreeRoot { - t.Errorf("WorktreeRoot = %s, want %s", loaded.WorktreeRoot, testState.WorktreeRoot) - } - if loaded.LastExportCommit != testState.LastExportCommit { - t.Errorf("LastExportCommit = %s, want %s", loaded.LastExportCommit, testState.LastExportCommit) - } - if loaded.JSONLHash != testState.JSONLHash { - t.Errorf("JSONLHash = %s, want %s", loaded.JSONLHash, testState.JSONLHash) - } -} - -func TestExportStateJSON(t *testing.T) { - state := &ExportState{ - WorktreeRoot: "/path/to/worktree", - WorktreeHash: "abc123", - LastExportCommit: "def456", - LastExportTime: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC), - JSONLHash: "hash789", - } - - data, err := json.MarshalIndent(state, "", " ") - if err != nil { - t.Fatalf("json.Marshal() failed: %v", err) - } - - // Verify JSON contains expected fields - jsonStr := string(data) - expectedFields := []string{ - `"worktree_root"`, - `"worktree_hash"`, - `"last_export_commit"`, - `"last_export_time"`, - `"jsonl_hash"`, - } - for _, field := range expectedFields { - if !strings.Contains(jsonStr, field) { - t.Errorf("JSON missing field %s: %s", field, jsonStr) - } - } - - // Verify omitempty works for empty optional fields - stateMinimal := &ExportState{ - WorktreeRoot: "/path", - LastExportCommit: "abc", - LastExportTime: time.Now(), - } - dataMinimal, _ := json.Marshal(stateMinimal) - jsonMinimal := string(dataMinimal) - - // WorktreeHash and JSONLHash should be omitted when empty - if strings.Contains(jsonMinimal, `"worktree_hash"`) { - t.Errorf("Empty worktree_hash should be omitted: %s", jsonMinimal) - } - if strings.Contains(jsonMinimal, `"jsonl_hash"`) { - t.Errorf("Empty jsonl_hash should be omitted: %s", jsonMinimal) - } -} - -func TestUpdateExportStateCommit(t *testing.T) { - tmpDir := t.TempDir() - beadsDir := filepath.Join(tmpDir, ".beads") - worktreeRoot := tmpDir - - // Create initial state - initialState := &ExportState{ - WorktreeRoot: worktreeRoot, - LastExportCommit: "old-commit", - LastExportTime: time.Now().Add(-time.Hour), - JSONLHash: "oldhash", - } - if err := saveExportState(beadsDir, worktreeRoot, initialState); err != nil { - t.Fatalf("saveExportState() failed: %v", err) - } - - // Update just the commit - updateExportStateCommit(beadsDir, worktreeRoot, "new-commit") - - // Load and verify - loaded, err := loadExportState(beadsDir, worktreeRoot) - if err != nil { - t.Fatalf("loadExportState() failed: %v", err) - } - - if loaded.LastExportCommit != "new-commit" { - t.Errorf("LastExportCommit = %s, want new-commit", loaded.LastExportCommit) - } - // JSONLHash should be preserved - if loaded.JSONLHash != "oldhash" { - t.Errorf("JSONLHash = %s, want oldhash (should be preserved)", loaded.JSONLHash) - } -} - -func TestComputeJSONLHashForHook(t *testing.T) { - tmpDir := t.TempDir() - - // Non-existent file should return empty string, no error - hash, err := computeJSONLHashForHook(filepath.Join(tmpDir, "nonexistent.jsonl")) - if err != nil { - t.Errorf("computeJSONLHashForHook() error for missing file: %v", err) - } - if hash != "" { - t.Errorf("computeJSONLHashForHook() = %s, want empty for missing file", hash) - } - - // Create a test file - testFile := filepath.Join(tmpDir, "test.jsonl") - content := `{"id": "test-1", "title": "Test"} -{"id": "test-2", "title": "Test 2"} -` - if err := os.WriteFile(testFile, []byte(content), 0644); err != nil { - t.Fatalf("Failed to write test file: %v", err) - } - - // Should get a hash - hash, err = computeJSONLHashForHook(testFile) - if err != nil { - t.Fatalf("computeJSONLHashForHook() failed: %v", err) - } - if hash == "" { - t.Error("computeJSONLHashForHook() returned empty hash for existing file") - } - - // Same content should produce same hash - hash2, _ := computeJSONLHashForHook(testFile) - if hash != hash2 { - t.Errorf("Same file produced different hashes: %s vs %s", hash, hash2) - } - - // Different content should produce different hash - testFile2 := filepath.Join(tmpDir, "test2.jsonl") - if err := os.WriteFile(testFile2, []byte(`{"different": true}`), 0644); err != nil { - t.Fatalf("Failed to write test file 2: %v", err) - } - hash3, _ := computeJSONLHashForHook(testFile2) - if hash == hash3 { - t.Errorf("Different files produced same hash: %s", hash) - } -} diff --git a/cmd/bd/hooks.go b/cmd/bd/hooks.go index b773d166e..2eacacfea 100644 --- a/cmd/bd/hooks.go +++ b/cmd/bd/hooks.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "context" "embed" "encoding/json" "fmt" @@ -13,7 +12,6 @@ import ( "github.com/spf13/cobra" "github.com/steveyegge/beads/internal/beads" - "github.com/steveyegge/beads/internal/config" "github.com/steveyegge/beads/internal/git" ) @@ -520,81 +518,18 @@ func runChainedHook(hookName string, args []string) int { return 0 } -// runPreCommitHook flushes pending changes to JSONL before commit. -// Returns 0 on success (or if not applicable), 1 if unstaged beads changes detected. +// runPreCommitHook runs chained hooks before commit. +// Returns 0 on success (or if not applicable). func runPreCommitHook() int { // Run chained hook first (if exists) if exitCode := runChainedHook("pre-commit", nil); exitCode != 0 { return exitCode } - - // Check if we're in a bd workspace - if _, err := os.Stat(".beads"); os.IsNotExist(err) { - return 0 // Not a bd workspace, nothing to do - } - - // In dolt-native mode, Dolt is the source of truth — skip JSONL export - if config.GetSyncMode() == config.SyncModeDoltNative { - return 0 - } - - // Flush pending changes to JSONL - // Use --flush-only to skip git operations (we're already in a git hook) - cmd := exec.Command("bd", "sync", "--flush-only") - if err := cmd.Run(); err != nil { - fmt.Fprintln(os.Stderr, "Warning: Failed to flush bd changes to JSONL") - fmt.Fprintln(os.Stderr, "Run 'bd sync --flush-only' manually to diagnose") - // Don't block the commit - user may have removed beads or have other issues - } - - // Stage JSONL files for commit - // By default, we auto-stage for convenience. Users with conflicting git hooks - // (e.g., hooks that read the staging area) can set BEADS_NO_AUTO_STAGE=1 to - // disable this and stage manually. See: https://github.com/steveyegge/beads/issues/826 - if os.Getenv("BEADS_NO_AUTO_STAGE") != "" { - // Safe mode: check for unstaged changes and block if found - var unstaged []string - for _, f := range jsonlFilePaths { - if _, err := os.Stat(f); err == nil { - if hasUnstagedChanges(f) { - unstaged = append(unstaged, f) - } - } - } - - if len(unstaged) > 0 { - fmt.Fprintln(os.Stderr, "❌ Unstaged beads changes detected:") - for _, f := range unstaged { - fmt.Fprintf(os.Stderr, " %s\n", f) - } - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Run: git add .beads/") - return 1 - } - } else { - // Default: auto-stage JSONL files - rc, rcErr := beads.GetRepoContext() - ctx := context.Background() - for _, f := range jsonlFilePaths { - if _, err := os.Stat(f); err == nil { - var gitAdd *exec.Cmd - if rcErr == nil { - gitAdd = rc.GitCmdCWD(ctx, "add", f) - } else { - // Fallback if RepoContext unavailable - // #nosec G204 -- f comes from jsonlFilePaths (controlled, hardcoded paths) - gitAdd = exec.Command("git", "add", f) - } - _ = gitAdd.Run() // Ignore errors - file may not exist - } - } - } - return 0 } -// runPostMergeHook imports JSONL after pull/merge. -// Returns 0 on success (or if not applicable), non-zero on error. +// runPostMergeHook runs chained hooks after merge. +// Returns 0 on success (or if not applicable). // //nolint:unparam // Always returns 0 by design - warnings don't block merges func runPostMergeHook() int { @@ -602,147 +537,22 @@ func runPostMergeHook() int { if exitCode := runChainedHook("post-merge", nil); exitCode != 0 { return exitCode } - - // Skip during rebase - if isRebaseInProgress() { - return 0 - } - - // Check if we're in a bd workspace - if _, err := os.Stat(".beads"); os.IsNotExist(err) { - return 0 - } - - // In dolt-native mode, Dolt is the source of truth — skip JSONL import - if config.GetSyncMode() == config.SyncModeDoltNative { - return 0 - } - - // Check if any JSONL file exists - if !hasBeadsJSONL() { - return 0 - } - - // Run bd sync --import-only --no-git-history - cmd := exec.Command("bd", "sync", "--import-only", "--no-git-history") - output, err := cmd.CombinedOutput() - if err != nil { - fmt.Fprintln(os.Stderr, "Warning: Failed to sync bd changes after merge") - fmt.Fprintln(os.Stderr, string(output)) - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Run 'bd doctor --fix' to diagnose and repair") - // Don't fail the merge, just warn - } - - // Run quick health check - healthCmd := exec.Command("bd", "doctor", "--check-health") - _ = healthCmd.Run() // Ignore errors - return 0 } -// runPrePushHook prevents pushing stale JSONL. +// runPrePushHook runs chained hooks before push. // Returns 0 to allow push, non-zero to block. func runPrePushHook(args []string) int { // Run chained hook first (if exists) if exitCode := runChainedHook("pre-push", args); exitCode != 0 { return exitCode } - - // Check if we're in a bd workspace - if _, err := os.Stat(".beads"); os.IsNotExist(err) { - return 0 - } - - // In dolt-native mode, Dolt is the source of truth — skip JSONL checks - if config.GetSyncMode() == config.SyncModeDoltNative { - return 0 - } - - // Skip if bd sync is already in progress (prevents circular error) - if os.Getenv("BD_SYNC_IN_PROGRESS") != "" { - return 0 - } - - // Get RepoContext for git operations (needed for flush and staging) - rc, rcErr := beads.GetRepoContext() - ctx := context.Background() - - // Flush pending bd changes - flushCmd := exec.Command("bd", "sync", "--flush-only") - _ = flushCmd.Run() // Ignore errors - - // Auto-stage JSONL files after flush to prevent race condition. - // Without this, flush creates uncommitted changes that the check below - // would detect, causing an infinite loop. See: GH#1208 - for _, f := range jsonlFilePaths { - if _, err := os.Stat(f); err == nil { - var gitAdd *exec.Cmd - if rcErr == nil { - gitAdd = rc.GitCmdCWD(ctx, "add", f) - } else { - // #nosec G204 -- f comes from jsonlFilePaths (controlled, hardcoded paths) - gitAdd = exec.Command("git", "add", f) - } - _ = gitAdd.Run() // Ignore errors - file may not exist - } - } - - // Check for uncommitted JSONL changes - files := []string{} - for _, f := range jsonlFilePaths { - // Check if file exists or is tracked - if _, err := os.Stat(f); err == nil { - files = append(files, f) - } else { - // Check if tracked by git - var checkCmd *exec.Cmd - if rcErr == nil { - checkCmd = rc.GitCmdCWD(ctx, "ls-files", "--error-unmatch", f) - } else { - // #nosec G204 - f is from jsonlFilePaths (controlled, hardcoded paths) - checkCmd = exec.Command("git", "ls-files", "--error-unmatch", f) - } - if checkCmd.Run() == nil { - files = append(files, f) - } - } - } - - if len(files) == 0 { - return 0 - } - - // Check for uncommitted changes using git status - statusArgs := append([]string{"status", "--porcelain", "--"}, files...) - var statusCmd *exec.Cmd - if rcErr == nil { - statusCmd = rc.GitCmdCWD(ctx, statusArgs...) - } else { - // #nosec G204 - statusArgs built from hardcoded list and git subcommands - statusCmd = exec.Command("git", statusArgs...) - } - output, _ := statusCmd.Output() - if len(output) > 0 { - fmt.Fprintln(os.Stderr, "❌ Error: Uncommitted changes detected") - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Before pushing, ensure all changes are committed. This includes:") - fmt.Fprintln(os.Stderr, " • bd JSONL updates (run 'bd sync')") - fmt.Fprintln(os.Stderr, " • any other modified files (run 'git status' to review)") - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Run 'bd sync' to commit these changes:") - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, " bd sync") - fmt.Fprintln(os.Stderr, "") - return 1 - } - return 0 } -// runPostCheckoutHook imports JSONL after branch checkout. +// runPostCheckoutHook runs chained hooks after branch checkout. // args: [previous-HEAD, new-HEAD, flag] where flag=1 for branch checkout -// Returns 0 on success (or if not applicable), non-zero on error. +// Returns 0 on success (or if not applicable). // //nolint:unparam // Always returns 0 by design - warnings don't block checkouts func runPostCheckoutHook(args []string) int { @@ -750,67 +560,6 @@ func runPostCheckoutHook(args []string) int { if exitCode := runChainedHook("post-checkout", args); exitCode != 0 { return exitCode } - - // Only run on branch checkouts (flag=1) - if len(args) >= 3 && args[2] != "1" { - return 0 - } - - // Skip during rebase - if isRebaseInProgress() { - return 0 - } - - // Check if we're in a bd workspace - if _, err := os.Stat(".beads"); os.IsNotExist(err) { - return 0 - } - - // In dolt-native mode, Dolt is the source of truth — skip JSONL import - if config.GetSyncMode() == config.SyncModeDoltNative { - return 0 - } - - // Detect git worktree and show warning - if isGitWorktree() { - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "╔══════════════════════════════════════════════════════════════════════════╗") - fmt.Fprintln(os.Stderr, "║ Welcome to beads in git worktree! ║") - fmt.Fprintln(os.Stderr, "╠══════════════════════════════════════════════════════════════════════════╣") - fmt.Fprintln(os.Stderr, "║ Note: Daemon mode is not recommended with git worktrees. ║") - fmt.Fprintln(os.Stderr, "║ Worktrees share the same database, and the daemon may commit changes ║") - fmt.Fprintln(os.Stderr, "║ to the wrong branch. ║") - fmt.Fprintln(os.Stderr, "║ ║") - fmt.Fprintln(os.Stderr, "║ RECOMMENDED: Disable daemon for this session: ║") - fmt.Fprintln(os.Stderr, "║ export BEADS_NO_DAEMON=1 ║") - fmt.Fprintln(os.Stderr, "║ ║") - fmt.Fprintln(os.Stderr, "║ For more information: ║") - fmt.Fprintln(os.Stderr, "║ - Run: bd doctor ║") - fmt.Fprintln(os.Stderr, "║ - Read: docs/GIT_INTEGRATION.md (lines 10-53) ║") - fmt.Fprintln(os.Stderr, "╚══════════════════════════════════════════════════════════════════════════╝") - fmt.Fprintln(os.Stderr, "") - } - - // Check if any JSONL file exists - if !hasBeadsJSONL() { - return 0 - } - - // Run bd sync --import-only --no-git-history - cmd := exec.Command("bd", "sync", "--import-only", "--no-git-history") - output, err := cmd.CombinedOutput() - if err != nil { - fmt.Fprintln(os.Stderr, "Warning: Failed to sync bd changes after checkout") - fmt.Fprintln(os.Stderr, string(output)) - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Run 'bd doctor --fix' to diagnose and repair") - // Don't fail the checkout, just warn - } - - // Run quick health check - healthCmd := exec.Command("bd", "doctor", "--check-health") - _ = healthCmd.Run() // Ignore errors - return 0 } @@ -994,59 +743,6 @@ func isRebaseInProgress() bool { return false } -// hasBeadsJSONL checks if any JSONL file exists in .beads/. -func hasBeadsJSONL() bool { - for _, f := range jsonlFilePaths { - if _, err := os.Stat(f); err == nil { - return true - } - } - return false -} - -// hasUnstagedChanges checks if a file has uncommitted changes (modified or untracked). -// Returns true if the file needs to be staged before commit. -func hasUnstagedChanges(path string) bool { - // Check git status for this specific file - rc, rcErr := beads.GetRepoContext() - var cmd *exec.Cmd - if rcErr == nil { - cmd = rc.GitCmdCWD(context.Background(), "status", "--porcelain", "--", path) - } else { - // #nosec G204 - path is from hardcoded list in caller - cmd = exec.Command("git", "status", "--porcelain", "--", path) - } - output, err := cmd.Output() - if err != nil { - return false // If git fails, assume no changes - } - - // Parse porcelain output: XY filename - // X = staged status, Y = unstaged status - // We care about Y (unstaged) being non-space, OR the file being untracked (??) - status := strings.TrimSpace(string(output)) - if status == "" { - return false // No changes - } - - // Check each line (usually just one for a single file) - for _, line := range strings.Split(status, "\n") { - if len(line) < 2 { - continue - } - x, y := line[0], line[1] - // Untracked file - if x == '?' && y == '?' { - return true - } - // Modified but not staged (Y is M, D, etc.) - if y != ' ' { - return true - } - } - - return false -} var hooksRunCmd = &cobra.Command{ Use: "run [args...]", @@ -1055,10 +751,10 @@ var hooksRunCmd = &cobra.Command{ thin shim scripts installed in .git/hooks/. Supported hooks: - - pre-commit: Flush pending changes to JSONL before commit - - post-merge: Import JSONL after pull/merge - - pre-push: Prevent pushing stale JSONL - - post-checkout: Import JSONL after branch checkout + - pre-commit: Run chained hooks before commit + - post-merge: Run chained hooks after pull/merge + - pre-push: Run chained hooks before push + - post-checkout: Run chained hooks after branch checkout - prepare-commit-msg: Add agent identity trailers for forensics The thin shim pattern ensures hook logic is always in sync with the diff --git a/cmd/bd/hooks_test.go b/cmd/bd/hooks_test.go deleted file mode 100644 index 5c1ae7185..000000000 --- a/cmd/bd/hooks_test.go +++ /dev/null @@ -1,743 +0,0 @@ -package main - -import ( - "os" - "path/filepath" - "runtime" - "strings" - "testing" - - "github.com/steveyegge/beads/internal/git" -) - -func TestGetEmbeddedHooks(t *testing.T) { - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - expectedHooks := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"} - for _, hookName := range expectedHooks { - content, ok := hooks[hookName] - if !ok { - t.Errorf("Missing hook: %s", hookName) - continue - } - if len(content) == 0 { - t.Errorf("Hook %s has empty content", hookName) - } - // Verify it's a shell script - if content[:2] != "#!" { - t.Errorf("Hook %s doesn't start with shebang: %s", hookName, content[:50]) - } - } -} - -func TestInstallHooks(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - if err := installHooks(hooks, false, false, false); err != nil { - t.Fatalf("installHooks() failed: %v", err) - } - - for hookName := range hooks { - hookPath := filepath.Join(gitDir, hookName) - if _, err := os.Stat(hookPath); os.IsNotExist(err) { - t.Errorf("Hook %s was not installed", hookName) - } - if runtime.GOOS == "windows" { - continue - } - - info, err := os.Stat(hookPath) - if err != nil { - t.Errorf("Failed to stat %s: %v", hookName, err) - continue - } - if info.Mode()&0111 == 0 { - t.Errorf("Hook %s is not executable", hookName) - } - } - }) -} - -func TestInstallHooksBackup(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - existingHook := filepath.Join(gitDir, "pre-commit") - existingContent := "#!/bin/sh\necho old hook\n" - if err := os.WriteFile(existingHook, []byte(existingContent), 0755); err != nil { - t.Fatalf("Failed to create existing hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - if err := installHooks(hooks, false, false, false); err != nil { - t.Fatalf("installHooks() failed: %v", err) - } - - backupPath := existingHook + ".backup" - if _, err := os.Stat(backupPath); os.IsNotExist(err) { - t.Errorf("Backup was not created") - } - - backupContent, err := os.ReadFile(backupPath) - if err != nil { - t.Fatalf("Failed to read backup: %v", err) - } - if string(backupContent) != existingContent { - t.Errorf("Backup content mismatch: got %q, want %q", string(backupContent), existingContent) - } - }) -} - -func TestInstallHooksForce(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - existingHook := filepath.Join(gitDir, "pre-commit") - if err := os.WriteFile(existingHook, []byte("old"), 0755); err != nil { - t.Fatalf("Failed to create existing hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - if err := installHooks(hooks, true, false, false); err != nil { - t.Fatalf("installHooks() failed: %v", err) - } - - backupPath := existingHook + ".backup" - if _, err := os.Stat(backupPath); !os.IsNotExist(err) { - t.Errorf("Backup should not have been created with --force") - } - }) -} - -func TestUninstallHooks(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - if err := installHooks(hooks, false, false, false); err != nil { - t.Fatalf("installHooks() failed: %v", err) - } - - if err := uninstallHooks(); err != nil { - t.Fatalf("uninstallHooks() failed: %v", err) - } - - for hookName := range hooks { - hookPath := filepath.Join(gitDir, hookName) - if _, err := os.Stat(hookPath); !os.IsNotExist(err) { - t.Errorf("Hook %s was not removed", hookName) - } - } - }) -} - -func TestHooksCheckGitHooks(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - statuses := CheckGitHooks() - for _, status := range statuses { - if status.Installed { - t.Errorf("Hook %s should not be installed initially", status.Name) - } - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - if err := installHooks(hooks, false, false, false); err != nil { - t.Fatalf("installHooks() failed: %v", err) - } - - // Build expected shim versions from embedded hook templates - expectedVersions := make(map[string]string) - for name, content := range hooks { - for _, line := range strings.Split(content, "\n") { - if strings.HasPrefix(line, shimVersionPrefix) { - expectedVersions[name] = strings.TrimSpace(strings.TrimPrefix(line, shimVersionPrefix)) - break - } - } - } - - statuses = CheckGitHooks() - for _, status := range statuses { - if !status.Installed { - t.Errorf("Hook %s should be installed", status.Name) - } - if !status.IsShim { - t.Errorf("Hook %s should be a thin shim", status.Name) - } - expectedVer := expectedVersions[status.Name] - if status.Version != expectedVer { - t.Errorf("Hook %s shim version mismatch: got %s, want %s", status.Name, status.Version, expectedVer) - } - if status.Outdated { - t.Errorf("Hook %s should not be outdated", status.Name) - } - } - }) -} - -func TestInstallHooksShared(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - if err := installHooks(hooks, false, true, false); err != nil { - t.Fatalf("installHooks() with shared=true failed: %v", err) - } - - sharedHooksDir := ".beads-hooks" - for hookName := range hooks { - hookPath := filepath.Join(sharedHooksDir, hookName) - if _, err := os.Stat(hookPath); os.IsNotExist(err) { - t.Errorf("Hook %s was not installed to .beads-hooks/", hookName) - } - if runtime.GOOS == "windows" { - continue - } - - info, err := os.Stat(hookPath) - if err != nil { - t.Errorf("Failed to stat %s: %v", hookName, err) - continue - } - if info.Mode()&0111 == 0 { - t.Errorf("Hook %s is not executable", hookName) - } - } - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - standardHooksDir := filepath.Join(gitDirPath, "hooks") - for hookName := range hooks { - hookPath := filepath.Join(standardHooksDir, hookName) - if _, err := os.Stat(hookPath); !os.IsNotExist(err) { - t.Errorf("Hook %s should not be in .git/hooks/ when using --shared", hookName) - } - } - }) -} - -func TestInstallHooksChaining(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create an existing hook - existingHook := filepath.Join(gitDir, "pre-commit") - existingContent := "#!/bin/sh\necho old hook\n" - if err := os.WriteFile(existingHook, []byte(existingContent), 0755); err != nil { - t.Fatalf("Failed to create existing hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - // Install with chain=true - if err := installHooks(hooks, false, false, true); err != nil { - t.Fatalf("installHooks() with chain=true failed: %v", err) - } - - // Verify the original hook was renamed to .old - oldPath := existingHook + ".old" - if _, err := os.Stat(oldPath); os.IsNotExist(err) { - t.Errorf("Existing hook was not renamed to .old for chaining") - } - - oldContent, err := os.ReadFile(oldPath) - if err != nil { - t.Fatalf("Failed to read .old hook: %v", err) - } - if string(oldContent) != existingContent { - t.Errorf(".old hook content mismatch: got %q, want %q", string(oldContent), existingContent) - } - - // Verify new hook was installed - if _, err := os.Stat(existingHook); os.IsNotExist(err) { - t.Errorf("New pre-commit hook was not installed") - } - - // Verify .backup was NOT created (chain mode uses .old, not .backup) - backupPath := existingHook + ".backup" - if _, err := os.Stat(backupPath); !os.IsNotExist(err) { - t.Errorf("Backup was created but should not be in chain mode") - } - }) -} - -func TestFormatHookWarnings(t *testing.T) { - tests := []struct { - name string - statuses []HookStatus - want string - }{ - { - name: "no issues", - statuses: []HookStatus{{Name: "pre-commit", Installed: true}}, - want: "", - }, - { - name: "one missing", - statuses: []HookStatus{{Name: "pre-commit", Installed: false}}, - want: "⚠️ Git hooks not installed (1 missing)", - }, - { - name: "multiple missing", - statuses: []HookStatus{ - {Name: "pre-commit", Installed: false}, - {Name: "post-merge", Installed: false}, - }, - want: "⚠️ Git hooks not installed (2 missing)", - }, - { - name: "one outdated", - statuses: []HookStatus{{Name: "pre-commit", Installed: true, Outdated: true}}, - want: "⚠️ Git hooks are outdated (1 hooks)", - }, - { - name: "mixed missing and outdated", - statuses: []HookStatus{ - {Name: "pre-commit", Installed: false}, - {Name: "post-merge", Installed: true, Outdated: true}, - }, - want: "⚠️ Git hooks not installed (1 missing)", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := FormatHookWarnings(tt.statuses) - if tt.want == "" && got != "" { - t.Errorf("FormatHookWarnings() = %q, want empty", got) - } else if tt.want != "" && !strContains(got, tt.want) { - t.Errorf("FormatHookWarnings() = %q, want to contain %q", got, tt.want) - } - }) - } -} - -func strContains(s, substr string) bool { - return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && (s[:len(substr)] == substr || strContains(s[1:], substr))) -} - -func TestIsRebaseInProgress(t *testing.T) { - tmpDir := t.TempDir() - t.Chdir(tmpDir) - - // Create .git directory - if err := os.MkdirAll(".git", 0755); err != nil { - t.Fatalf("Failed to create .git: %v", err) - } - - // Should be false initially - if isRebaseInProgress() { - t.Error("isRebaseInProgress() = true, want false (no rebase marker)") - } - - // Create rebase-merge marker - if err := os.MkdirAll(".git/rebase-merge", 0755); err != nil { - t.Fatalf("Failed to create rebase-merge: %v", err) - } - if !isRebaseInProgress() { - t.Error("isRebaseInProgress() = false, want true (rebase-merge exists)") - } - - // Remove rebase-merge - if err := os.RemoveAll(".git/rebase-merge"); err != nil { - t.Fatalf("Failed to remove rebase-merge: %v", err) - } - - // Create rebase-apply marker - if err := os.MkdirAll(".git/rebase-apply", 0755); err != nil { - t.Fatalf("Failed to create rebase-apply: %v", err) - } - if !isRebaseInProgress() { - t.Error("isRebaseInProgress() = false, want true (rebase-apply exists)") - } -} - -func TestHasBeadsJSONL(t *testing.T) { - tmpDir := t.TempDir() - t.Chdir(tmpDir) - - // Should be false initially (no .beads directory) - if hasBeadsJSONL() { - t.Error("hasBeadsJSONL() = true, want false (no .beads)") - } - - // Create .beads directory without any JSONL files - if err := os.MkdirAll(".beads", 0755); err != nil { - t.Fatalf("Failed to create .beads: %v", err) - } - if hasBeadsJSONL() { - t.Error("hasBeadsJSONL() = true, want false (no JSONL files)") - } - - // Create issues.jsonl - if err := os.WriteFile(".beads/issues.jsonl", []byte("{}"), 0644); err != nil { - t.Fatalf("Failed to create issues.jsonl: %v", err) - } - if !hasBeadsJSONL() { - t.Error("hasBeadsJSONL() = false, want true (issues.jsonl exists)") - } -} - -// TestInstallHooksChainingSkipsBdShim verifies that bd hooks install --chain -// does NOT rename existing bd shims to .old (which would cause infinite recursion). -// See: https://github.com/steveyegge/beads/issues/843 -func TestInstallHooksChainingSkipsBdShim(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create an existing hook that IS a bd shim - existingHook := filepath.Join(gitDir, "pre-commit") - shimContent := "#!/bin/sh\n# bd-shim v1\nexec bd hooks run pre-commit \"$@\"\n" - if err := os.WriteFile(existingHook, []byte(shimContent), 0755); err != nil { - t.Fatalf("Failed to create existing shim hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - // Install with chain=true - if err := installHooks(hooks, false, false, true); err != nil { - t.Fatalf("installHooks() with chain=true failed: %v", err) - } - - // Verify the shim was NOT renamed to .old (would cause infinite loop) - oldPath := existingHook + ".old" - if _, err := os.Stat(oldPath); !os.IsNotExist(err) { - t.Errorf("bd shim was renamed to .old - this would cause infinite recursion!") - } - - // Verify new hook was installed (overwrote the shim) - if _, err := os.Stat(existingHook); os.IsNotExist(err) { - t.Errorf("New pre-commit hook was not installed") - } - }) -} - -// TestRunChainedHookSkipsBdShim verifies that runChainedHook() skips -// .old hooks that are bd shims (to prevent infinite recursion). -// See: https://github.com/steveyegge/beads/issues/843 -func TestRunChainedHookSkipsBdShim(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create a .old hook that IS a bd shim (simulating the problematic state) - oldHook := filepath.Join(gitDir, "pre-commit.old") - shimContent := "#!/bin/sh\n# bd-shim v1\nexec bd hooks run pre-commit \"$@\"\n" - if err := os.WriteFile(oldHook, []byte(shimContent), 0755); err != nil { - t.Fatalf("Failed to create .old shim hook: %v", err) - } - - // runChainedHook should return 0 (skip the shim) instead of executing it - exitCode := runChainedHook("pre-commit", nil) - if exitCode != 0 { - t.Errorf("runChainedHook() = %d, want 0 (should skip bd shim)", exitCode) - } - }) -} - -// TestGetHookVersionRecognizesInlineHooks verifies that getHookVersion() -// correctly identifies inline bd hooks created by bd init. -// See: https://github.com/steveyegge/beads/issues/1120 -func TestGetHookVersionRecognizesInlineHooks(t *testing.T) { - tmpDir := t.TempDir() - - // Test inline hook from bd init - inlineHook := filepath.Join(tmpDir, "pre-commit") - inlineContent := `#!/bin/sh -# -# bd (beads) pre-commit hook (chained) -# -# This hook chains bd functionality with your existing pre-commit hook. - -if ! command -v bd >/dev/null 2>&1; then - echo "Warning: bd command not found" >&2 - exit 0 -fi - -bd sync --flush-only -` - if err := os.WriteFile(inlineHook, []byte(inlineContent), 0755); err != nil { - t.Fatalf("Failed to create inline hook: %v", err) - } - - info, err := getHookVersion(inlineHook) - if err != nil { - t.Fatalf("getHookVersion() failed: %v", err) - } - - if !info.IsBdHook { - t.Error("getHookVersion() IsBdHook = false, want true for inline bd hook") - } - if info.IsShim { - t.Error("getHookVersion() IsShim = true, want false for inline bd hook") - } - - // Test shim hook (should also be recognized as IsBdHook) - shimHook := filepath.Join(tmpDir, "pre-commit-shim") - shimContent := "#!/bin/sh\n# bd-shim v1\nexec bd hooks run pre-commit \"$@\"\n" - if err := os.WriteFile(shimHook, []byte(shimContent), 0755); err != nil { - t.Fatalf("Failed to create shim hook: %v", err) - } - - info, err = getHookVersion(shimHook) - if err != nil { - t.Fatalf("getHookVersion() failed: %v", err) - } - - if !info.IsBdHook { - t.Error("getHookVersion() IsBdHook = false, want true for shim") - } - if !info.IsShim { - t.Error("getHookVersion() IsShim = false, want true for shim") - } - - // Test non-bd hook - userHook := filepath.Join(tmpDir, "pre-commit-user") - userContent := "#!/bin/sh\necho 'User hook'\n" - if err := os.WriteFile(userHook, []byte(userContent), 0755); err != nil { - t.Fatalf("Failed to create user hook: %v", err) - } - - info, err = getHookVersion(userHook) - if err != nil { - t.Fatalf("getHookVersion() failed: %v", err) - } - - if info.IsBdHook { - t.Error("getHookVersion() IsBdHook = true, want false for user hook") - } -} - -// TestInstallHooksChainingSkipsInlineHook verifies that bd hooks install --chain -// does NOT rename existing inline bd hooks to .old (which would destroy user's original). -// See: https://github.com/steveyegge/beads/issues/1120 -func TestInstallHooksChainingSkipsInlineHook(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create an existing hook that IS an inline bd hook (from bd init) - existingHook := filepath.Join(gitDir, "pre-commit") - inlineContent := `#!/bin/sh -# -# bd (beads) pre-commit hook (chained) -# -bd sync --flush-only -` - if err := os.WriteFile(existingHook, []byte(inlineContent), 0755); err != nil { - t.Fatalf("Failed to create existing inline hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - // Install with chain=true - if err := installHooks(hooks, false, false, true); err != nil { - t.Fatalf("installHooks() with chain=true failed: %v", err) - } - - // Verify the inline hook was NOT renamed to .old (would destroy user's original) - oldPath := existingHook + ".old" - if _, err := os.Stat(oldPath); !os.IsNotExist(err) { - t.Errorf("inline bd hook was renamed to .old - this would destroy user's original hook!") - } - - // Verify new hook was installed (overwrote the inline hook) - if _, err := os.Stat(existingHook); os.IsNotExist(err) { - t.Errorf("New pre-commit hook was not installed") - } - }) -} - -// TestInstallHooksChainingPreservesExistingOld verifies that bd hooks install --chain -// does NOT overwrite an existing .old file (which would destroy user's original hook). -// See: https://github.com/steveyegge/beads/issues/1120 -func TestInstallHooksChainingPreservesExistingOld(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create the user's original hook as .old (simulating bd init already ran) - originalHook := filepath.Join(gitDir, "pre-commit.old") - originalContent := "#!/bin/sh\necho 'User original hook'\n" - if err := os.WriteFile(originalHook, []byte(originalContent), 0755); err != nil { - t.Fatalf("Failed to create original .old hook: %v", err) - } - - // Create a current hook that is NOT a bd hook (e.g., user modified it) - currentHook := filepath.Join(gitDir, "pre-commit") - currentContent := "#!/bin/sh\necho 'Some other hook'\n" - if err := os.WriteFile(currentHook, []byte(currentContent), 0755); err != nil { - t.Fatalf("Failed to create current hook: %v", err) - } - - hooks, err := getEmbeddedHooks() - if err != nil { - t.Fatalf("getEmbeddedHooks() failed: %v", err) - } - - // Install with chain=true - if err := installHooks(hooks, false, false, true); err != nil { - t.Fatalf("installHooks() with chain=true failed: %v", err) - } - - // Verify the original .old was preserved (not overwritten) - preservedContent, err := os.ReadFile(originalHook) - if err != nil { - t.Fatalf("Failed to read preserved .old hook: %v", err) - } - if string(preservedContent) != originalContent { - t.Errorf(".old hook was overwritten! got %q, want %q", string(preservedContent), originalContent) - } - - // Verify new hook was installed - if _, err := os.Stat(currentHook); os.IsNotExist(err) { - t.Errorf("New pre-commit hook was not installed") - } - }) -} - -// TestRunChainedHookSkipsInlineHook verifies that runChainedHook() skips -// .old hooks that are inline bd hooks (to prevent recursion). -// See: https://github.com/steveyegge/beads/issues/1120 -func TestRunChainedHookSkipsInlineHook(t *testing.T) { - tmpDir := newGitRepo(t) - runInDir(t, tmpDir, func() { - - gitDirPath, err := git.GetGitDir() - if err != nil { - t.Fatalf("git.GetGitDir() failed: %v", err) - } - gitDir := filepath.Join(gitDirPath, "hooks") - if err := os.MkdirAll(gitDir, 0750); err != nil { - t.Fatalf("Failed to create hooks directory: %v", err) - } - - // Create a .old hook that IS an inline bd hook - oldHook := filepath.Join(gitDir, "pre-commit.old") - inlineContent := `#!/bin/sh -# -# bd (beads) pre-commit hook (chained) -# -bd sync --flush-only -` - if err := os.WriteFile(oldHook, []byte(inlineContent), 0755); err != nil { - t.Fatalf("Failed to create .old inline hook: %v", err) - } - - // runChainedHook should return 0 (skip the inline hook) instead of executing it - exitCode := runChainedHook("pre-commit", nil) - if exitCode != 0 { - t.Errorf("runChainedHook() = %d, want 0 (should skip inline bd hook)", exitCode) - } - }) -} diff --git a/cmd/bd/import.go b/cmd/bd/import.go deleted file mode 100644 index 104546134..000000000 --- a/cmd/bd/import.go +++ /dev/null @@ -1,617 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "cmp" - "encoding/json" - "fmt" - "os" - "os/exec" - "path/filepath" - "slices" - "strings" - "time" - - "github.com/spf13/cobra" - "github.com/steveyegge/beads/internal/debug" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/utils" - "golang.org/x/term" -) - -// DeletionMarker represents a compact deletion marker in JSONL format. -// Format: {"id": "gt-123", "_deleted": true, "_deleted_at": "2026-01-17T10:30:00Z"} -// These markers are used for incremental sync to propagate deletions efficiently -// without sending full deletion records. -type DeletionMarker struct { - ID string `json:"id"` - Deleted bool `json:"_deleted"` -} - -// isDeletionMarker checks if a JSON line is a deletion marker. -// Returns the parsed marker and true if it's a deletion marker, otherwise nil and false. -func isDeletionMarker(line []byte) (*DeletionMarker, bool) { - var marker DeletionMarker - if err := json.Unmarshal(line, &marker); err != nil { - return nil, false - } - // A valid deletion marker must have an ID and _deleted=true - if marker.ID != "" && marker.Deleted { - return &marker, true - } - return nil, false -} - -var importCmd = &cobra.Command{ - Use: "import", - GroupID: "sync", - Short: "Import issues from JSONL format", - Long: `Import issues from JSON Lines format (one JSON object per line). - -Reads from stdin by default, or use -i flag for file input. - -Behavior: - - Existing issues (same ID) are updated - - New issues are created - - Collisions (same ID, different content) are detected and reported - - Use --dedupe-after to find and merge content duplicates after import - - Use --dry-run to preview changes without applying them - -NOTE: Import requires direct database access.`, - Run: func(cmd *cobra.Command, args []string) { - CheckReadonly("import") - // Check for positional arguments (common mistake: bd import file.jsonl instead of bd import -i file.jsonl) - if len(args) > 0 { - fmt.Fprintf(os.Stderr, "Error: Unexpected argument(s): %v\n\n", args) - fmt.Fprintf(os.Stderr, "Did you mean: bd import -i %s\n\n", args[0]) - fmt.Fprintf(os.Stderr, "The import command does not accept positional arguments.\n") - fmt.Fprintf(os.Stderr, "Use the -i flag to specify an input file:\n") - fmt.Fprintf(os.Stderr, " bd import -i .beads/issues.jsonl\n\n") - fmt.Fprintf(os.Stderr, "Or pipe data via stdin:\n") - fmt.Fprintf(os.Stderr, " cat data.jsonl | bd import\n") - os.Exit(1) - } - - // Ensure database directory exists (auto-create if needed) - dbDir := filepath.Dir(dbPath) - if err := os.MkdirAll(dbDir, 0750); err != nil { - FatalError("failed to create database directory: %v", err) - } - - // We'll check if database needs initialization after reading the JSONL - // so we can detect the prefix from the imported issues - - input, _ := cmd.Flags().GetString("input") - skipUpdate, _ := cmd.Flags().GetBool("skip-existing") - strict, _ := cmd.Flags().GetBool("strict") - dryRun, _ := cmd.Flags().GetBool("dry-run") - renameOnImport, _ := cmd.Flags().GetBool("rename-on-import") - dedupeAfter, _ := cmd.Flags().GetBool("dedupe-after") - clearDuplicateExternalRefs, _ := cmd.Flags().GetBool("clear-duplicate-external-refs") - orphanHandling, _ := cmd.Flags().GetString("orphan-handling") - force, _ := cmd.Flags().GetBool("force") - noGitHistory, _ := cmd.Flags().GetBool("no-git-history") - _ = noGitHistory // Accepted for compatibility with bd sync subprocess calls - - // Check if stdin is being used interactively (not piped) - if input == "" && term.IsTerminal(int(os.Stdin.Fd())) { - fmt.Fprintf(os.Stderr, "Error: No input specified.\n\n") - fmt.Fprintf(os.Stderr, "Usage:\n") - fmt.Fprintf(os.Stderr, " bd import -i .beads/issues.jsonl # Import from file\n") - fmt.Fprintf(os.Stderr, " bd import -i .beads/issues.jsonl --dry-run # Preview changes\n") - fmt.Fprintf(os.Stderr, " cat data.jsonl | bd import # Import from pipe\n") - fmt.Fprintf(os.Stderr, " bd sync --import-only # Import latest JSONL\n\n") - fmt.Fprintf(os.Stderr, "For more information, run: bd import --help\n") - os.Exit(1) - } - - // Open input - in := os.Stdin - if input != "" { - // #nosec G304 - user-provided file path is intentional - f, err := os.Open(input) - if err != nil { - FatalError("opening input file: %v", err) - } - defer func() { - if err := f.Close(); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to close input file: %v\n", err) - } - }() - in = f - } - - // Phase 1: Read and parse all JSONL - ctx := rootCtx - scanner := bufio.NewScanner(in) - - var allIssues []*types.Issue - var deletionMarkers []*DeletionMarker - lineNum := 0 - - for scanner.Scan() { - lineNum++ - rawLine := scanner.Bytes() - line := string(rawLine) - - // Skip empty lines - if line == "" { - continue - } - - // Detect git conflict markers in raw bytes (before JSON decoding) - // This prevents false positives when issue content contains these strings - trimmed := bytes.TrimSpace(rawLine) - if bytes.HasPrefix(trimmed, []byte("<<<<<<< ")) || - bytes.Equal(trimmed, []byte("=======")) || - bytes.HasPrefix(trimmed, []byte(">>>>>>> ")) { - fmt.Fprintf(os.Stderr, "Error: Git conflict markers detected in JSONL file (line %d)\n\n", lineNum) - fmt.Fprintf(os.Stderr, "To resolve:\n") - fmt.Fprintf(os.Stderr, " git checkout --ours .beads/issues.jsonl && bd import -i .beads/issues.jsonl\n") - fmt.Fprintf(os.Stderr, " git checkout --theirs .beads/issues.jsonl && bd import -i .beads/issues.jsonl\n") - os.Exit(1) - } - - // Check for deletion markers first - // Format: {"id": "gt-123", "_deleted": true, "_deleted_at": "..."} - if marker, ok := isDeletionMarker(rawLine); ok { - deletionMarkers = append(deletionMarkers, marker) - if debug.Enabled() { - debug.Logf("Found deletion marker for issue %s\n", marker.ID) - } - continue // Skip normal issue parsing for deletion markers - } - - // Parse JSON as regular issue - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err != nil { - FatalError("parsing line %d: %v", lineNum, err) - } - issue.SetDefaults() // Apply defaults for omitted fields (beads-399) - - if issue.Status == types.StatusClosed && issue.ClosedAt == nil { - now := time.Now() - issue.ClosedAt = &now - } - - allIssues = append(allIssues, &issue) - } - - if err := scanner.Err(); err != nil { - FatalError("reading input: %v", err) - } - - // Check if database needs initialization (prefix not set) - // Detect prefix from the imported issues - initCtx := rootCtx - configuredPrefix, err2 := store.GetConfig(initCtx, "issue_prefix") - if err2 != nil || strings.TrimSpace(configuredPrefix) == "" { - // Database exists but not initialized - detect prefix from issues - detectedPrefix := detectPrefixFromIssues(allIssues) - prefixSource := "issues" - if detectedPrefix == "" { - // No issues to import or couldn't detect prefix, use directory name - // But avoid using ".beads" as prefix - go up one level - cwd, err := os.Getwd() - if err != nil { - FatalError("failed to get current directory: %v", err) - } - dirName := filepath.Base(cwd) - if dirName == ".beads" || dirName == "beads" { - // Running from inside .beads/ - use parent directory - detectedPrefix = filepath.Base(filepath.Dir(cwd)) - } else { - detectedPrefix = dirName - } - prefixSource = "directory" - } - detectedPrefix = strings.TrimRight(detectedPrefix, "-") - - if err := store.SetConfig(initCtx, "issue_prefix", detectedPrefix); err != nil { - FatalError("failed to set issue prefix: %v", err) - } - - fmt.Fprintf(os.Stderr, "✓ Initialized database with prefix '%s' (detected from %s)\n", detectedPrefix, prefixSource) - } - - // Phase 2: Use shared import logic - // Extract deletion IDs from markers - var deletionIDs []string - for _, marker := range deletionMarkers { - deletionIDs = append(deletionIDs, marker.ID) - } - - opts := ImportOptions{ - DryRun: dryRun, - SkipUpdate: skipUpdate, - Strict: strict, - RenameOnImport: renameOnImport, - ClearDuplicateExternalRefs: clearDuplicateExternalRefs, - OrphanHandling: orphanHandling, - DeletionIDs: deletionIDs, - SkipPrefixValidation: cmd.Flags().Changed("skip-prefix-validation") || cmd.Flags().Changed("rename-on-import"), - } - - result, err := importIssuesCore(ctx, dbPath, store, allIssues, opts) - - // Check for uncommitted changes in JSONL after import - // Only check if we have an input file path (not stdin) and it's the default beads file - if result != nil && input != "" && (input == ".beads/issues.jsonl" || input == ".beads/beads.jsonl") { - checkUncommittedChanges(input, result) - } - - // Handle errors and special cases - if err != nil { - // Check if it's a prefix mismatch error - if result != nil && result.PrefixMismatch { - fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n") - fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix) - fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n") - for prefix, count := range result.MismatchPrefixes { - fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count) - } - fmt.Fprintf(os.Stderr, "\nOptions:\n") - fmt.Fprintf(os.Stderr, " --rename-on-import Auto-rename imported issues to match configured prefix\n") - fmt.Fprintf(os.Stderr, " --dry-run Preview what would be imported\n") - fmt.Fprintf(os.Stderr, "\nOr use 'bd rename-prefix' after import to fix the database.\n") - os.Exit(1) - } - - // Check if it's a collision error - if result != nil && len(result.CollisionIDs) > 0 { - // Print collision report before exiting - fmt.Fprintf(os.Stderr, "\n=== Collision Detection Report ===\n") - fmt.Fprintf(os.Stderr, "COLLISIONS DETECTED: %d\n\n", result.Collisions) - fmt.Fprintf(os.Stderr, "Colliding issue IDs: %v\n", result.CollisionIDs) - fmt.Fprintf(os.Stderr, "\nWith hash-based IDs, collisions should not occur.\n") - fmt.Fprintf(os.Stderr, "This may indicate manual ID manipulation or a bug.\n") - os.Exit(1) - } - FatalError("import failed: %v", err) - } - - // Handle dry-run mode - if dryRun { - if result.PrefixMismatch { - fmt.Fprintf(os.Stderr, "\n=== Prefix Mismatch Detected ===\n") - fmt.Fprintf(os.Stderr, "Database configured prefix: %s-\n", result.ExpectedPrefix) - fmt.Fprintf(os.Stderr, "Found issues with different prefixes:\n") - for prefix, count := range result.MismatchPrefixes { - fmt.Fprintf(os.Stderr, " %s- (%d issues)\n", prefix, count) - } - fmt.Fprintf(os.Stderr, "\nUse --rename-on-import to automatically fix prefixes during import.\n") - } - - if result.Collisions > 0 { - fmt.Fprintf(os.Stderr, "\n=== Collision Detection Report ===\n") - fmt.Fprintf(os.Stderr, "COLLISIONS DETECTED: %d\n", result.Collisions) - fmt.Fprintf(os.Stderr, "Colliding issue IDs: %v\n", result.CollisionIDs) - } else if !result.PrefixMismatch { - fmt.Fprintf(os.Stderr, "No collisions detected.\n") - } - msg := fmt.Sprintf("Would create %d new issues, update %d existing issues", result.Created, result.Updated) - if result.Deleted > 0 { - msg += fmt.Sprintf(", delete %d issues", result.Deleted) - } - if result.Unchanged > 0 { - msg += fmt.Sprintf(", %d unchanged", result.Unchanged) - } - fmt.Fprintf(os.Stderr, "%s\n", msg) - fmt.Fprintf(os.Stderr, "\nDry-run mode: no changes made\n") - os.Exit(0) - } - - // Print remapping report if collisions were resolved - if len(result.IDMapping) > 0 { - fmt.Fprintf(os.Stderr, "\n=== Remapping Report ===\n") - fmt.Fprintf(os.Stderr, "Issues remapped: %d\n\n", len(result.IDMapping)) - - // Sort by old ID for consistent output - type mapping struct { - oldID string - newID string - } - mappings := make([]mapping, 0, len(result.IDMapping)) - for oldID, newID := range result.IDMapping { - mappings = append(mappings, mapping{oldID, newID}) - } - slices.SortFunc(mappings, func(a, b mapping) int { - return cmp.Compare(a.oldID, b.oldID) - }) - - fmt.Fprintf(os.Stderr, "Remappings:\n") - for _, m := range mappings { - fmt.Fprintf(os.Stderr, " %s → %s\n", m.oldID, m.newID) - } - fmt.Fprintf(os.Stderr, "\nAll text and dependency references have been updated.\n") - } - - // Record that this command performed a write (for Dolt auto-commit). - if result.Created > 0 || result.Updated > 0 || result.Deleted > 0 || len(result.IDMapping) > 0 { - commandDidWrite.Store(true) - } - - // Update jsonl_content_hash metadata to enable content-based staleness detection - // This prevents git operations from resurrecting deleted issues by comparing content instead of mtime - // ALWAYS update metadata after successful import, even if no changes were made (fixes staleness check) - // This ensures that running `bd import` marks the database as fresh for staleness detection - // Renamed from last_import_hash - more accurate since updated on both import AND export - if input != "" { - if currentHash, err := computeJSONLHash(input); err == nil { - if err := store.SetMetadata(ctx, "jsonl_content_hash", currentHash); err != nil { - // Non-fatal warning: Metadata update failures are intentionally non-fatal to prevent blocking - // successful imports. System degrades gracefully to mtime-based staleness detection if metadata - // is unavailable. This ensures import operations always succeed even if metadata storage fails. - debug.Logf("Warning: failed to update jsonl_content_hash: %v", err) - } - // Use RFC3339Nano for nanosecond precision to avoid race with file mtime (fixes #399) - importTime := time.Now().Format(time.RFC3339Nano) - if err := store.SetMetadata(ctx, "last_import_time", importTime); err != nil { - // Non-fatal warning (see above comment about graceful degradation) - debug.Logf("Warning: failed to update last_import_time: %v", err) - } - // Note: mtime tracking removed in bd-v0y fix (git doesn't preserve mtime) - } else { - debug.Logf("Warning: failed to read JSONL for hash update: %v", err) - } - } - - // Update database mtime to reflect it's now in sync with JSONL - // This is CRITICAL even when import found 0 changes, because: - // 1. Import validates DB and JSONL are in sync (no content divergence) - // 2. Without mtime update, bd sync refuses to export (thinks JSONL is newer) - // 3. This can happen after git pull updates JSONL mtime but content is identical - // Fix for: refusing to export: JSONL is newer than database (import first to avoid data loss) - if err := TouchDatabaseFile(dbPath, input); err != nil { - debug.Logf("Warning: failed to update database mtime: %v", err) - } - - // Print summary - fmt.Fprintf(os.Stderr, "Import complete: %d created, %d updated", result.Created, result.Updated) - if result.Deleted > 0 { - fmt.Fprintf(os.Stderr, ", %d deleted", result.Deleted) - } - if result.Unchanged > 0 { - fmt.Fprintf(os.Stderr, ", %d unchanged", result.Unchanged) - } - if result.Skipped > 0 { - fmt.Fprintf(os.Stderr, ", %d skipped", result.Skipped) - } - if len(result.IDMapping) > 0 { - fmt.Fprintf(os.Stderr, ", %d issues remapped", len(result.IDMapping)) - } - fmt.Fprintf(os.Stderr, "\n") - - // Print skipped dependencies summary if any - if len(result.SkippedDependencies) > 0 { - fmt.Fprintf(os.Stderr, "\n⚠️ Warning: Skipped %d dependencies due to missing references:\n", len(result.SkippedDependencies)) - for _, dep := range result.SkippedDependencies { - fmt.Fprintf(os.Stderr, " - %s\n", dep) - } - fmt.Fprintf(os.Stderr, "\nThis can happen after merges that delete issues referenced by other issues.\n") - fmt.Fprintf(os.Stderr, "The import continued successfully - you may want to review the skipped dependencies.\n") - } - - // Print force message if metadata was updated despite no changes - if force && result.Created == 0 && result.Updated == 0 && len(result.IDMapping) == 0 { - fmt.Fprintf(os.Stderr, "Metadata updated (database already in sync with JSONL)\n") - } - - // Run duplicate detection if requested - if dedupeAfter { - fmt.Fprintf(os.Stderr, "\n=== Post-Import Duplicate Detection ===\n") - - // Get all issues (fresh after import) - allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{}) - if err != nil { - FatalError("fetching issues for deduplication: %v", err) - } - - duplicateGroups := findDuplicateGroups(allIssues) - if len(duplicateGroups) == 0 { - fmt.Fprintf(os.Stderr, "No duplicates found.\n") - return - } - - refCounts := countReferences(allIssues) - structuralScores := countStructuralRelationships(duplicateGroups) - - fmt.Fprintf(os.Stderr, "Found %d duplicate group(s)\n\n", len(duplicateGroups)) - - for i, group := range duplicateGroups { - target := chooseMergeTarget(group, refCounts, structuralScores) - fmt.Fprintf(os.Stderr, "Group %d: %s\n", i+1, group[0].Title) - - for _, issue := range group { - refs := refCounts[issue.ID] - depCount := 0 - if score, ok := structuralScores[issue.ID]; ok { - depCount = score.dependentCount - } - marker := " " - if issue.ID == target.ID { - marker = "→ " - } - fmt.Fprintf(os.Stderr, " %s%s (%s, P%d, %d dependents, %d refs)\n", - marker, issue.ID, issue.Status, issue.Priority, depCount, refs) - } - - sources := make([]string, 0, len(group)-1) - for _, issue := range group { - if issue.ID != target.ID { - sources = append(sources, issue.ID) - } - } - fmt.Fprintf(os.Stderr, " Suggested: bd merge %s --into %s\n\n", - strings.Join(sources, " "), target.ID) - } - - fmt.Fprintf(os.Stderr, "Run 'bd duplicates --auto-merge' to merge all duplicates.\n") - } - }, -} - -// TouchDatabaseFile updates the modification time of the database file. -// This is used after import AND export to ensure the database appears "in sync" with JSONL, -// preventing bd doctor and validatePreExport from incorrectly warning that JSONL is newer. -// -// In SQLite WAL mode, writes go to beads.db-wal and beads.db mtime may not update -// until a checkpoint. Since validation compares JSONL mtime to beads.db mtime only, -// we need to explicitly touch the DB file after both import and export operations. -// -// The function sets DB mtime to max(JSONL mtime, now) + 1ns to handle clock skew. -// If jsonlPath is empty or can't be read, falls back to time.Now(). -// -// Fixes issues #278, #301, #321: export leaving JSONL newer than DB. -func TouchDatabaseFile(dbPath, jsonlPath string) error { - targetTime := time.Now() - - // If we have the JSONL path, use max(JSONL mtime, now) to handle clock skew - // Use Lstat to get the symlink's own mtime, not the target's (NixOS fix). - if jsonlPath != "" { - if info, err := os.Lstat(jsonlPath); err == nil { - jsonlTime := info.ModTime() - if jsonlTime.After(targetTime) { - targetTime = jsonlTime.Add(time.Nanosecond) - } - } - } - - // Best-effort touch - don't fail import if this doesn't work - return os.Chtimes(dbPath, targetTime, targetTime) -} - -// checkUncommittedChanges detects if the JSONL file has uncommitted changes -// and warns the user if the working tree differs from git HEAD -func checkUncommittedChanges(filePath string, result *ImportResult) { - // Only warn if no actual changes were made (database already synced) - if result.Created > 0 || result.Updated > 0 { - return - } - - // Get the directory containing the file to use as git working directory - workDir := filepath.Dir(filePath) - - // Use git diff to check if working tree differs from HEAD - gitCmd := exec.Command("git", "diff", "--quiet", "HEAD", "--", filePath) - gitCmd.Dir = workDir - err := gitCmd.Run() - - // Exit code 0 = no changes, 1 = changes exist, >1 = error - if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 { - // Get line counts for context - workingTreeLines := countLines(filePath) - headLines := countLinesInGitHEAD(filePath, workDir) - - fmt.Fprintf(os.Stderr, "\n⚠️ Warning: %s has uncommitted changes\n", filePath) - fmt.Fprintf(os.Stderr, " Working tree: %d lines\n", workingTreeLines) - if headLines > 0 { - fmt.Fprintf(os.Stderr, " Git HEAD: %d lines\n", headLines) - } - fmt.Fprintf(os.Stderr, "\n Import complete: database already synced with working tree\n") - fmt.Fprintf(os.Stderr, " Run: git diff %s\n", filePath) - fmt.Fprintf(os.Stderr, " To review uncommitted changes\n") - } -} - -// countLines counts the number of lines in a file -func countLines(filePath string) int { - // #nosec G304 - file path is controlled by caller - f, err := os.Open(filePath) - if err != nil { - return 0 - } - defer func() { _ = f.Close() }() - - scanner := bufio.NewScanner(f) - lines := 0 - for scanner.Scan() { - lines++ - } - return lines -} - -// countLinesInGitHEAD counts lines in the file as it exists in git HEAD -func countLinesInGitHEAD(filePath string, workDir string) int { - // First, find the git root - gitRootCmd := exec.Command("git", "rev-parse", "--show-toplevel") - gitRootCmd.Dir = workDir - gitRootOutput, err := gitRootCmd.Output() - if err != nil { - return 0 - } - gitRoot := strings.TrimSpace(string(gitRootOutput)) - - // Make filePath relative to git root - absPath, err := filepath.Abs(filePath) - if err != nil { - return 0 - } - - relPath, err := filepath.Rel(gitRoot, absPath) - if err != nil { - return 0 - } - - // Get file content from git HEAD and count lines in Go (no shell pipe) - gitShowCmd := exec.Command("git", "show", "HEAD:"+relPath) //nolint:gosec // relPath is from filepath.Rel, not user input - gitShowCmd.Dir = workDir - output, err := gitShowCmd.Output() - if err != nil { - return 0 - } - - // Count lines in the output - lines := 0 - scanner := bufio.NewScanner(bytes.NewReader(output)) - for scanner.Scan() { - lines++ - } - return lines -} - -// detectPrefixFromIssues extracts the common prefix from issue IDs -// Uses utils.ExtractIssuePrefix which handles multi-part prefixes correctly -func detectPrefixFromIssues(issues []*types.Issue) string { - if len(issues) == 0 { - return "" - } - - // Count prefix occurrences - prefixCounts := make(map[string]int) - for _, issue := range issues { - prefix := utils.ExtractIssuePrefix(issue.ID) - if prefix != "" { - prefixCounts[prefix]++ - } - } - - // Find most common prefix - maxCount := 0 - commonPrefix := "" - for prefix, count := range prefixCounts { - if count > maxCount { - maxCount = count - commonPrefix = prefix - } - } - - return commonPrefix -} - -func init() { - importCmd.Flags().StringP("input", "i", "", "Input file (default: stdin)") - importCmd.Flags().BoolP("skip-existing", "s", false, "Skip existing issues instead of updating them") - importCmd.Flags().Bool("strict", false, "Fail on dependency errors instead of treating them as warnings") - importCmd.Flags().Bool("dedupe-after", false, "Detect and report content duplicates after import") - importCmd.Flags().Bool("dry-run", false, "Preview collision detection without making changes") - importCmd.Flags().Bool("rename-on-import", false, "Rename imported issues to match database prefix (updates all references)") - importCmd.Flags().Bool("skip-prefix-validation", false, "Skip issue ID prefix validation (implied by --rename-on-import)") - importCmd.Flags().Bool("clear-duplicate-external-refs", false, "Clear duplicate external_ref values (keeps first occurrence)") - importCmd.Flags().String("orphan-handling", "", "How to handle missing parent issues: strict/resurrect/skip/allow (default: use config or 'allow')") - importCmd.Flags().Bool("force", false, "Force metadata update even when database is already in sync with JSONL") - importCmd.Flags().Bool("no-git-history", false, "Skip git history backfill for deletions (passed by bd sync)") - importCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output import statistics in JSON format") - rootCmd.AddCommand(importCmd) -} diff --git a/cmd/bd/init.go b/cmd/bd/init.go index 04f5279aa..4a7916e63 100644 --- a/cmd/bd/init.go +++ b/cmd/bd/init.go @@ -2,9 +2,7 @@ package main import ( "bufio" - "bytes" "context" - "encoding/json" "fmt" "os" "os/exec" @@ -19,7 +17,6 @@ import ( "github.com/steveyegge/beads/internal/configfile" "github.com/steveyegge/beads/internal/git" "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/ui" "github.com/steveyegge/beads/internal/utils" "golang.org/x/term" @@ -711,73 +708,6 @@ func migrateOldDatabases(targetPath string, quiet bool) error { return nil } -// readFirstIssueFromJSONL reads the first issue from a JSONL file -func readFirstIssueFromJSONL(path string) (*types.Issue, error) { - // #nosec G304 -- helper reads JSONL file chosen by current bd command - file, err := os.Open(path) - if err != nil { - return nil, fmt.Errorf("failed to open JSONL file: %w", err) - } - defer file.Close() - - scanner := bufio.NewScanner(file) - lineNum := 0 - for scanner.Scan() { - lineNum++ - line := scanner.Text() - - // skip empty lines - if line == "" { - continue - } - - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err == nil { - return &issue, nil - } else { - // Skip malformed lines with warning - fmt.Fprintf(os.Stderr, "Warning: skipping malformed JSONL line %d: %v\n", lineNum, err) - continue - } - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("error reading JSONL file: %w", err) - } - - return nil, nil -} - -// readFirstIssueFromGit reads the first issue from a git ref (bd-0is: supports sync-branch) -func readFirstIssueFromGit(jsonlPath, gitRef string) (*types.Issue, error) { - output, err := readFromGitRef(jsonlPath, gitRef) - if err != nil { - return nil, err - } - - scanner := bufio.NewScanner(bytes.NewReader(output)) - for scanner.Scan() { - line := scanner.Text() - - // skip empty lines - if line == "" { - continue - } - - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err == nil { - return &issue, nil - } - // Skip malformed lines silently (called during auto-detection) - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("error scanning git content: %w", err) - } - - return nil, nil -} - // checkExistingBeadsDataAt checks for existing database at a specific beadsDir path. // This is extracted to support both BEADS_DIR and CWD-based resolution. func checkExistingBeadsDataAt(beadsDir string, prefix string) error { diff --git a/cmd/bd/init_test.go b/cmd/bd/init_test.go index 6a55301fc..e7127c34d 100644 --- a/cmd/bd/init_test.go +++ b/cmd/bd/init_test.go @@ -474,93 +474,6 @@ func TestInitNoDbMode(t *testing.T) { } } -// TestReadFirstIssueFromJSONL_ValidFile verifies reading first issue from valid JSONL -func TestReadFirstIssueFromJSONL_ValidFile(t *testing.T) { - tempDir := t.TempDir() - jsonlPath := filepath.Join(tempDir, "test.jsonl") - - // Create test JSONL file with multiple issues - content := `{"id":"bd-1","title":"First Issue","description":"First test"} -{"id":"bd-2","title":"Second Issue","description":"Second test"} -{"id":"bd-3","title":"Third Issue","description":"Third test"} -` - if err := os.WriteFile(jsonlPath, []byte(content), 0o600); err != nil { - t.Fatalf("Failed to write test file: %v", err) - } - - issue, err := readFirstIssueFromJSONL(jsonlPath) - if err != nil { - t.Fatalf("readFirstIssueFromJSONL failed: %v", err) - } - - if issue == nil { - t.Fatal("Expected non-nil issue, got nil") - } - - // Verify we got the FIRST issue - if issue.ID != "bd-1" { - t.Errorf("Expected ID 'bd-1', got '%s'", issue.ID) - } - if issue.Title != "First Issue" { - t.Errorf("Expected title 'First Issue', got '%s'", issue.Title) - } - if issue.Description != "First test" { - t.Errorf("Expected description 'First test', got '%s'", issue.Description) - } -} - -// TestReadFirstIssueFromJSONL_EmptyLines verifies skipping empty lines -func TestReadFirstIssueFromJSONL_EmptyLines(t *testing.T) { - tempDir := t.TempDir() - jsonlPath := filepath.Join(tempDir, "test.jsonl") - - // Create JSONL with empty lines before first valid issue - content := ` - -{"id":"bd-1","title":"First Valid Issue"} -{"id":"bd-2","title":"Second Issue"} -` - if err := os.WriteFile(jsonlPath, []byte(content), 0o600); err != nil { - t.Fatalf("Failed to write test file: %v", err) - } - - issue, err := readFirstIssueFromJSONL(jsonlPath) - if err != nil { - t.Fatalf("readFirstIssueFromJSONL failed: %v", err) - } - - if issue == nil { - t.Fatal("Expected non-nil issue, got nil") - } - - if issue.ID != "bd-1" { - t.Errorf("Expected ID 'bd-1', got '%s'", issue.ID) - } - if issue.Title != "First Valid Issue" { - t.Errorf("Expected title 'First Valid Issue', got '%s'", issue.Title) - } -} - -// TestReadFirstIssueFromJSONL_EmptyFile verifies handling of empty file -func TestReadFirstIssueFromJSONL_EmptyFile(t *testing.T) { - tempDir := t.TempDir() - jsonlPath := filepath.Join(tempDir, "empty.jsonl") - - // Create empty file - if err := os.WriteFile(jsonlPath, []byte(""), 0o600); err != nil { - t.Fatalf("Failed to write test file: %v", err) - } - - issue, err := readFirstIssueFromJSONL(jsonlPath) - if err != nil { - t.Fatalf("readFirstIssueFromJSONL should not error on empty file: %v", err) - } - - if issue != nil { - t.Errorf("Expected nil issue for empty file, got %+v", issue) - } -} - // TestSetupClaudeSettings_InvalidJSON verifies that invalid JSON in existing // settings.local.json returns an error instead of silently overwriting. // This is a regression test for bd-5bj where user settings were lost. diff --git a/cmd/bd/integrity.go b/cmd/bd/integrity.go deleted file mode 100644 index 745f7634a..000000000 --- a/cmd/bd/integrity.go +++ /dev/null @@ -1,359 +0,0 @@ -package main - -import ( - "bytes" - "cmp" - "context" - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - "os" - "path/filepath" - "slices" - "strings" - - "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" -) - -// isJSONLNewer checks if JSONL file is newer than database file. -// Returns true if JSONL is newer AND has different content, false otherwise. -// -// NOTE: This uses computeDBHash which is more expensive than hasJSONLChanged. -// For auto-import, prefer hasJSONLChanged() which uses metadata-based -// content tracking and is safe against git operations. -func isJSONLNewer(jsonlPath string) bool { - return isJSONLNewerWithStore(jsonlPath, nil) -} - -// isJSONLNewerWithStore is like isJSONLNewer but accepts an optional store parameter. -// If st is nil, it will try to use the global store. -func isJSONLNewerWithStore(jsonlPath string, st *dolt.DoltStore) bool { - jsonlInfo, jsonlStatErr := os.Stat(jsonlPath) - if jsonlStatErr != nil { - return false - } - - beadsDir := filepath.Dir(jsonlPath) - dbPath := filepath.Join(beadsDir, "beads.db") - dbInfo, dbStatErr := os.Stat(dbPath) - if dbStatErr != nil { - return false - } - - // Quick path: if DB is newer, JSONL is definitely not newer - if !jsonlInfo.ModTime().After(dbInfo.ModTime()) { - return false - } - - // JSONL is newer by timestamp - but this could be due to daemon auto-export - // or clock skew. Use content-based comparison to determine if import is needed. - // If we can't determine content hash (e.g., store not available), conservatively - // assume JSONL is newer to trigger auto-import. - if st == nil { - if ensureStoreActive() != nil || store == nil { - return true // Conservative: can't check content, assume different - } - st = store - } - - ctx := context.Background() - jsonlHash, err := computeJSONLHash(jsonlPath) - if err != nil { - return true // Conservative: can't read JSONL, assume different - } - - dbHash, err := computeDBHash(ctx, st) - if err != nil { - return true // Conservative: can't read DB, assume different - } - - // Compare hashes: if they match, JSONL and DB have same content - // despite timestamp difference (daemon auto-export case) - return jsonlHash != dbHash -} - -// computeJSONLHash computes SHA256 hash of JSONL file content. -// Returns hex-encoded hash string and any error encountered reading the file. -func computeJSONLHash(jsonlPath string) (string, error) { - jsonlData, err := os.ReadFile(jsonlPath) // #nosec G304 - controlled path - if err != nil { - return "", err - } - hasher := sha256.New() - hasher.Write(jsonlData) - return hex.EncodeToString(hasher.Sum(nil)), nil -} - -// hasJSONLChanged checks if JSONL content has changed since last import using SHA256 hash. -// Returns true if JSONL content differs from last import, false otherwise. -// This is safe against git operations that restore old files with recent mtimes. -// -// Performance optimization: Checks mtime first as a fast-path. Only computes expensive -// SHA256 hash if mtime changed. This makes 99% of checks instant (mtime unchanged = content -// unchanged) while still catching git operations that restore old content with new mtimes. -// -// In multi-repo mode, keySuffix should be the stable repo identifier (e.g., ".", "../frontend"). -// The keySuffix must not contain the ':' separator character. -func hasJSONLChanged(ctx context.Context, store *dolt.DoltStore, jsonlPath string, keySuffix string) bool { - // Validate keySuffix doesn't contain the separator character - if keySuffix != "" && strings.Contains(keySuffix, ":") { - // Invalid keySuffix - treat as changed to trigger proper error handling - return true - } - - // Build metadata keys with optional suffix for per-repo tracking - // Renamed from last_import_hash to jsonl_content_hash - more accurate name - // since this hash is updated on both import AND export - hashKey := "jsonl_content_hash" - oldHashKey := "last_import_hash" // Migration: check old key if new key missing - if keySuffix != "" { - hashKey += ":" + keySuffix - oldHashKey += ":" + keySuffix - } - - // Always compute content hash - // Previous mtime-based fast-path was unsafe: git operations (pull, checkout, rebase) - // can change file content without updating mtime, causing false negatives. - // Hash computation is fast enough for sync operations (~10-50ms even for large DBs). - currentHash, err := computeJSONLHash(jsonlPath) - if err != nil { - // If we can't read JSONL, assume no change (don't auto-import broken files) - return false - } - - // Get content hash from metadata (try new key first, fall back to old for migration) - lastHash, err := store.GetMetadata(ctx, hashKey) - if err != nil || lastHash == "" { - // Try old key for migration - lastHash, err = store.GetMetadata(ctx, oldHashKey) - if err != nil || lastHash == "" { - // No previous hash - this is the first run or metadata is missing - // Assume changed to trigger import - return true - } - } - - // Compare hashes - return currentHash != lastHash -} - -// validatePreExport performs integrity checks before exporting database to JSONL. -// Returns error if critical issues found that would cause data loss. -func validatePreExport(ctx context.Context, store *dolt.DoltStore, jsonlPath string) error { - // Check if JSONL content has changed since last import - if so, must import first - // Uses content-based detection instead of mtime-based to avoid false positives from git operations - // Use getRepoKeyForPath to get stable repo identifier for multi-repo support - repoKey := getRepoKeyForPath(jsonlPath) - if hasJSONLChanged(ctx, store, jsonlPath, repoKey) { - return fmt.Errorf("refusing to export: JSONL content has changed since last import (import first to avoid data loss)") - } - - jsonlInfo, jsonlStatErr := os.Stat(jsonlPath) - - // Get database issue count (fast path with COUNT(*) if available) - dbCount, err := countDBIssuesFast(ctx, store) - if err != nil { - return fmt.Errorf("failed to count database issues: %w", err) - } - - // Get JSONL issue count - jsonlCount := 0 - if jsonlStatErr == nil { - jsonlCount, err = countIssuesInJSONL(jsonlPath) - if err != nil { - // Conservative: if JSONL exists with content but we can't count it, - // and DB is empty, refuse to export (potential data loss) - if dbCount == 0 && jsonlInfo.Size() > 0 { - return fmt.Errorf("refusing to export empty DB over existing JSONL whose contents couldn't be verified: %w", err) - } - // Warning for other cases - fmt.Fprintf(os.Stderr, "WARNING: Failed to count issues in JSONL: %v\n", err) - } - } - - // Critical: refuse to export empty DB over non-empty JSONL - if dbCount == 0 && jsonlCount > 0 { - return fmt.Errorf("refusing to export empty DB over %d issues in JSONL (would cause data loss)", jsonlCount) - } - - // Note: The main protection is the reverse ZFC check in sync.go - // which runs BEFORE this validation. Here we only block empty DB. - // This allows legitimate deletions while sync.go catches stale DBs. - - return nil -} - -// checkDuplicateIDs detects duplicate issue IDs in the database. -// Dolt enforces UNIQUE constraints on issue IDs, so duplicates cannot occur. -func checkDuplicateIDs(_ context.Context, _ *dolt.DoltStore) error { - return nil // Dolt UNIQUE constraint prevents duplicates -} - -// checkOrphanedDeps finds dependencies pointing to or from non-existent issues. -// TODO: Implement using Dolt store's query interface instead of direct SQL. -func checkOrphanedDeps(_ context.Context, _ *dolt.DoltStore) ([]string, error) { - return nil, nil // Skip: needs Dolt-native implementation -} - -// validatePostImport checks that import didn't cause data loss. -// Returns error if issue count decreased unexpectedly (data loss) or nil if OK. -// -// Parameters: -// - before: issue count in DB before import -// - after: issue count in DB after import -// - jsonlPath: path to issues.jsonl (unused, kept for API compatibility) -func validatePostImport(before, after int, _ string) error { - return validatePostImportWithExpectedDeletions(before, after, 0, "") -} - -// validatePostImportWithExpectedDeletions checks that import didn't cause data loss, -// accounting for expected deletions. -// Returns error if issue count decreased unexpectedly (data loss) or nil if OK. -// -// Parameters: -// - before: issue count in DB before import -// - after: issue count in DB after import -// - expectedDeletions: number of issues known to have been deleted -// - jsonlPath: unused, kept for API compatibility -func validatePostImportWithExpectedDeletions(before, after, expectedDeletions int, _ string) error { - if after < before { - decrease := before - after - - // Account for expected deletions - if expectedDeletions > 0 && decrease <= expectedDeletions { - fmt.Fprintf(os.Stderr, "Import complete: %d → %d issues (-%d, expected deletions)\n", - before, after, decrease) - return nil - } - - // Unexpected decrease - warn but don't fail - // Decreases are unusual but can happen during cleanup or migration - fmt.Fprintf(os.Stderr, "Warning: import reduced issue count: %d → %d (-%d)\n", - before, after, decrease) - return nil - } - if after == before { - fmt.Fprintf(os.Stderr, "Import complete: no changes\n") - } else { - fmt.Fprintf(os.Stderr, "Import complete: %d → %d issues (+%d)\n", before, after, after-before) - } - return nil -} - -// countDBIssues returns the total number of issues in the database. -// This is the legacy interface kept for compatibility. -func countDBIssues(ctx context.Context, store *dolt.DoltStore) (int, error) { - return countDBIssuesFast(ctx, store) -} - -// countDBIssuesFast counts all issues in the database. -func countDBIssuesFast(ctx context.Context, store *dolt.DoltStore) (int, error) { - issues, err := store.SearchIssues(ctx, "", types.IssueFilter{}) - if err != nil { - return 0, fmt.Errorf("failed to count database issues: %w", err) - } - return len(issues), nil -} - -// dbNeedsExport checks if the database has changes that differ from JSONL. -// Returns true if export is needed, false if DB and JSONL are already in sync. -func dbNeedsExport(ctx context.Context, store *dolt.DoltStore, jsonlPath string) (bool, error) { - // Check if JSONL exists - jsonlInfo, err := os.Stat(jsonlPath) - if os.IsNotExist(err) { - // JSONL doesn't exist - always need to export - return true, nil - } - if err != nil { - return false, fmt.Errorf("failed to stat JSONL: %w", err) - } - - // Check database modification time - beadsDir := filepath.Dir(jsonlPath) - dbPath := filepath.Join(beadsDir, "beads.db") - dbInfo, err := os.Stat(dbPath) - if err != nil { - return false, fmt.Errorf("failed to stat database: %w", err) - } - - // If database is newer than JSONL, we need to export - if dbInfo.ModTime().After(jsonlInfo.ModTime()) { - return true, nil - } - - // If modification times suggest they're in sync, verify counts match - dbCount, err := countDBIssuesFast(ctx, store) - if err != nil { - return false, fmt.Errorf("failed to count database issues: %w", err) - } - - jsonlCount, err := countIssuesInJSONL(jsonlPath) - if err != nil { - return false, fmt.Errorf("failed to count JSONL issues: %w", err) - } - - // If counts don't match, we need to export - if dbCount != jsonlCount { - return true, nil - } - - // DB and JSONL appear to be in sync - return false, nil -} - -// computeDBHash computes a content hash of the database by exporting to memory. -// This is used to compare DB content with JSONL content without relying on timestamps. -func computeDBHash(ctx context.Context, store *dolt.DoltStore) (string, error) { - // Get all issues from DB - issues, err := store.SearchIssues(ctx, "", types.IssueFilter{}) - if err != nil { - return "", fmt.Errorf("failed to get issues: %w", err) - } - - // Sort by ID for consistent hash - slices.SortFunc(issues, func(a, b *types.Issue) int { - return cmp.Compare(a.ID, b.ID) - }) - - // Populate dependencies - allDeps, err := store.GetAllDependencyRecords(ctx) - if err != nil { - return "", fmt.Errorf("failed to get dependencies: %w", err) - } - for _, issue := range issues { - issue.Dependencies = allDeps[issue.ID] - } - - // Populate labels - for _, issue := range issues { - labels, err := store.GetLabels(ctx, issue.ID) - if err != nil { - return "", fmt.Errorf("failed to get labels for %s: %w", issue.ID, err) - } - issue.Labels = labels - } - - // Populate comments - for _, issue := range issues { - comments, err := store.GetIssueComments(ctx, issue.ID) - if err != nil { - return "", fmt.Errorf("failed to get comments for %s: %w", issue.ID, err) - } - issue.Comments = comments - } - - // Serialize to JSON and hash - var buf bytes.Buffer - encoder := json.NewEncoder(&buf) - for _, issue := range issues { - if err := encoder.Encode(issue); err != nil { - return "", fmt.Errorf("failed to encode issue %s: %w", issue.ID, err) - } - } - - hasher := sha256.New() - hasher.Write(buf.Bytes()) - return hex.EncodeToString(hasher.Sum(nil)), nil -} diff --git a/cmd/bd/integrity_content_test.go b/cmd/bd/integrity_content_test.go deleted file mode 100644 index 171508b14..000000000 --- a/cmd/bd/integrity_content_test.go +++ /dev/null @@ -1,262 +0,0 @@ -//go:build cgo && integration -// +build cgo,integration - -package main - -import ( - "context" - "os" - "path/filepath" - "testing" - "time" - - "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" -) - -// TestContentBasedComparison verifies that isJSONLNewer uses content comparison -// instead of just timestamp comparison to prevent false positives (bd-lm2q) -func TestContentBasedComparison(t *testing.T) { - if testing.Short() { - t.Skip("skipping slow test in short mode") - } - - tmpDir := t.TempDir() - beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.Mkdir(beadsDir, 0750); err != nil { - t.Fatal(err) - } - - dbPath := filepath.Join(beadsDir, "beads.db") - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - - ctx := context.Background() - - // Create and populate database - localStore, err := dolt.New(ctx, &dolt.Config{Path: dbPath}) - if err != nil { - t.Fatalf("Failed to create store: %v", err) - } - defer localStore.Close() - - // Initialize database with issue_prefix - if err := localStore.SetConfig(ctx, "issue_prefix", "test"); err != nil { - t.Fatalf("Failed to set issue_prefix: %v", err) - } - - // Create a test issue - issue := &types.Issue{ - ID: "test-1", - Title: "Test Issue", - Status: types.StatusOpen, - Priority: 2, - IssueType: types.TypeTask, - } - if err := localStore.CreateIssue(ctx, issue, "test-actor"); err != nil { - t.Fatalf("Failed to create issue: %v", err) - } - - // Export to JSONL - if err := exportToJSONLWithStore(ctx, localStore, jsonlPath); err != nil { - t.Fatalf("Export failed: %v", err) - } - - // Touch database to match JSONL - if err := TouchDatabaseFile(dbPath, jsonlPath); err != nil { - t.Fatalf("TouchDatabaseFile failed: %v", err) - } - - // Verify they're in sync (content matches, timestamps match) - if isJSONLNewerWithStore(jsonlPath, localStore) { - t.Error("isJSONLNewer should return false when content matches (before timestamp manipulation)") - } - - // ensure timestamp advances - time.Sleep(10 * time.Millisecond) - - // Simulate daemon auto-export: touch JSONL to make it newer - // This simulates clock skew or filesystem timestamp quirks - futureTime := time.Now().Add(2 * time.Second) - if err := os.Chtimes(jsonlPath, futureTime, futureTime); err != nil { - t.Fatalf("Failed to update JSONL timestamp: %v", err) - } - - // Verify JSONL is newer by timestamp - jsonlInfo, _ := os.Stat(jsonlPath) - dbInfo, _ := os.Stat(dbPath) - if !jsonlInfo.ModTime().After(dbInfo.ModTime()) { - t.Fatal("Test setup failed: JSONL should be newer than DB by timestamp") - } - - // KEY TEST: isJSONLNewer should return FALSE because content is identical - // despite timestamp difference (this is the bd-lm2q fix) - - // Compute hashes for debugging - jsonlHash1, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute JSONL hash: %v", err) - } - dbHash1, err := computeDBHash(ctx, localStore) - if err != nil { - t.Fatalf("Failed to compute DB hash: %v", err) - } - t.Logf("JSONL hash: %s", jsonlHash1) - t.Logf("DB hash: %s", dbHash1) - - if isJSONLNewerWithStore(jsonlPath, localStore) { - t.Error("isJSONLNewer should return false when content matches despite timestamp difference (bd-lm2q)") - t.Logf("Hashes: JSONL=%s, DB=%s", jsonlHash1, dbHash1) - } - - // Now modify the database (add a new issue) - issue2 := &types.Issue{ - ID: "test-2", - Title: "New Issue", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeBug, - } - if err := localStore.CreateIssue(ctx, issue2, "test-actor"); err != nil { - t.Fatalf("Failed to create second issue: %v", err) - } - - // Re-export to JSONL to reflect new content - if err := exportToJSONLWithStore(ctx, localStore, jsonlPath); err != nil { - t.Fatalf("Second export failed: %v", err) - } - - // Make JSONL appear older than DB by timestamp - pastTime := time.Now().Add(-2 * time.Second) - if err := os.Chtimes(jsonlPath, pastTime, pastTime); err != nil { - t.Fatalf("Failed to update JSONL timestamp to past: %v", err) - } - - // Verify JSONL is older by timestamp - jsonlInfo, _ = os.Stat(jsonlPath) - dbInfo, _ = os.Stat(dbPath) - if jsonlInfo.ModTime().After(dbInfo.ModTime()) { - t.Fatal("Test setup failed: JSONL should be older than DB by timestamp") - } - - // isJSONLNewer should return false because JSONL is older by timestamp - if isJSONLNewerWithStore(jsonlPath, localStore) { - t.Error("isJSONLNewer should return false when JSONL is older by timestamp") - } - - // Final test: Make JSONL newer AND different content - // First, manually edit JSONL to have different content - originalJSONL, err := os.ReadFile(jsonlPath) // #nosec G304 - test code - if err != nil { - t.Fatalf("Failed to read JSONL: %v", err) - } - - // Remove the second issue from database - if err := localStore.DeleteIssue(ctx, "test-2"); err != nil { - t.Fatalf("Failed to delete issue: %v", err) - } - - // Restore the JSONL with 2 issues (making it different from DB which has 1) - if err := os.WriteFile(jsonlPath, originalJSONL, 0600); err != nil { // #nosec G306 - test code - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Make JSONL newer by timestamp - if err := os.Chtimes(jsonlPath, futureTime, futureTime); err != nil { - t.Fatalf("Failed to update JSONL timestamp: %v", err) - } - - // Now isJSONLNewer should return TRUE (newer timestamp AND different content) - if !isJSONLNewerWithStore(jsonlPath, localStore) { - t.Error("isJSONLNewer should return true when JSONL is newer AND has different content") - } -} - -// TestContentHashComputation verifies the hash computation functions -func TestContentHashComputation(t *testing.T) { - tmpDir := t.TempDir() - beadsDir := filepath.Join(tmpDir, ".beads") - if err := os.Mkdir(beadsDir, 0750); err != nil { - t.Fatal(err) - } - - dbPath := filepath.Join(beadsDir, "beads.db") - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - - ctx := context.Background() - - // Create and populate database - localStore, err := dolt.New(ctx, &dolt.Config{Path: dbPath}) - if err != nil { - t.Fatalf("Failed to create store: %v", err) - } - defer localStore.Close() - - if err := localStore.SetConfig(ctx, "issue_prefix", "test"); err != nil { - t.Fatalf("Failed to set issue_prefix: %v", err) - } - - issue := &types.Issue{ - ID: "test-1", - Title: "Test Issue", - Status: types.StatusOpen, - Priority: 2, - IssueType: types.TypeTask, - } - if err := localStore.CreateIssue(ctx, issue, "test-actor"); err != nil { - t.Fatalf("Failed to create issue: %v", err) - } - - // Export to JSONL - if err := exportToJSONLWithStore(ctx, localStore, jsonlPath); err != nil { - t.Fatalf("Export failed: %v", err) - } - - // Compute hashes - jsonlHash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute JSONL hash: %v", err) - } - - dbHash, err := computeDBHash(ctx, localStore) - if err != nil { - t.Fatalf("Failed to compute DB hash: %v", err) - } - - // Hashes should match since we just exported - if jsonlHash != dbHash { - t.Errorf("Hash mismatch after export:\nJSONL: %s\nDB: %s", jsonlHash, dbHash) - } - - // Modify database by adding a new issue - issue2 := &types.Issue{ - ID: "test-2", - Title: "Second Issue", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeBug, - } - if err := localStore.CreateIssue(ctx, issue2, "test-actor"); err != nil { - t.Fatalf("Failed to create second issue: %v", err) - } - - // Compute new DB hash - newDBHash, err := computeDBHash(ctx, localStore) - if err != nil { - t.Fatalf("Failed to compute new DB hash: %v", err) - } - - // Hashes should differ now - if jsonlHash == newDBHash { - t.Error("Hash should differ after DB modification") - } - - // Hash should be consistent across multiple calls - dbHash2, err := computeDBHash(ctx, localStore) - if err != nil { - t.Fatalf("Failed to compute DB hash second time: %v", err) - } - - if newDBHash != dbHash2 { - t.Error("DB hash should be consistent across calls") - } -} diff --git a/cmd/bd/integrity_test.go b/cmd/bd/integrity_test.go deleted file mode 100644 index 818dc3a6e..000000000 --- a/cmd/bd/integrity_test.go +++ /dev/null @@ -1,683 +0,0 @@ -//go:build cgo - -package main - -import ( - "context" - "fmt" - "os" - "path/filepath" - "strings" - "testing" - "time" - - "github.com/steveyegge/beads/internal/types" -) - -func TestValidatePreExportSuite(t *testing.T) { - tmpDir := t.TempDir() - dbPath := filepath.Join(tmpDir, "test.db") - s := newTestStore(t, dbPath) - ctx := context.Background() - - t.Run("empty DB over non-empty JSONL fails", func(t *testing.T) { - // Create temp directory for JSONL - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - - // Create non-empty JSONL file - jsonlContent := `{"id":"test-v1","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Should fail validation - err := validatePreExport(ctx, s, jsonlPath) - if err == nil { - t.Error("Expected error for empty DB over non-empty JSONL, got nil") - } - }) - - t.Run("non-empty DB over non-empty JSONL succeeds", func(t *testing.T) { - // Create temp directory for JSONL - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - - // Add an issue with unique ID prefix - issue := &types.Issue{ - ID: "test-v2", - Title: "Test", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue", - } - if err := s.CreateIssue(ctx, issue, "test"); err != nil { - t.Fatalf("Failed to create issue: %v", err) - } - - // Create JSONL file - jsonlContent := `{"id":"test-v2","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Store hash metadata to indicate JSONL and DB are in sync - // bd-39o: renamed from last_import_hash to jsonl_content_hash - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash", hash); err != nil { - t.Fatalf("Failed to set hash metadata: %v", err) - } - - // Should pass validation - err = validatePreExport(ctx, s, jsonlPath) - if err != nil { - t.Errorf("Expected no error, got: %v", err) - } - }) - - t.Run("empty DB over missing JSONL succeeds", func(t *testing.T) { - // Create temp directory for JSONL - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - - // JSONL doesn't exist - - // Should pass validation (new repo scenario) - err := validatePreExport(ctx, s, jsonlPath) - if err != nil { - t.Errorf("Expected no error for empty DB with no JSONL, got: %v", err) - } - }) - - t.Run("empty DB over unreadable JSONL fails", func(t *testing.T) { - // Create temp directory for JSONL - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - - // Create corrupt/unreadable JSONL file with content - corruptContent := `{"id":"test-v4","title":INVALID JSON` - if err := os.WriteFile(jsonlPath, []byte(corruptContent), 0600); err != nil { - t.Fatalf("Failed to write corrupt JSONL: %v", err) - } - - // Should fail validation (can't verify JSONL content, DB is empty, file has content) - err := validatePreExport(ctx, s, jsonlPath) - if err == nil { - t.Error("Expected error for empty DB over unreadable non-empty JSONL, got nil") - } - }) - - t.Run("JSONL content changed fails", func(t *testing.T) { - // Create temp directory for JSONL - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - - // Create issue with unique ID prefix - issue := &types.Issue{ - ID: "test-v5", - Title: "Test", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue", - } - if err := s.CreateIssue(ctx, issue, "test"); err != nil { - t.Fatalf("Failed to create issue: %v", err) - } - - // Create initial JSONL file - jsonlContent := `{"id":"test-v5","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Store hash of original content (bd-39o: renamed to jsonl_content_hash) - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash", hash); err != nil { - t.Fatalf("Failed to set hash: %v", err) - } - - // Modify JSONL content (simulates git pull that changed JSONL) - modifiedContent := `{"id":"test-v5","title":"Modified","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(modifiedContent), 0600); err != nil { - t.Fatalf("Failed to write modified JSONL: %v", err) - } - - // Should fail validation (JSONL content changed, must import first) - err = validatePreExport(ctx, s, jsonlPath) - if err == nil { - t.Error("Expected error for changed JSONL content, got nil") - } - if err != nil && !strings.Contains(err.Error(), "JSONL content has changed") { - t.Errorf("Expected 'JSONL content has changed' error, got: %v", err) - } - }) -} - -func TestValidatePostImport(t *testing.T) { - // Note: validatePostImport no longer fails on decreases - it only warns. - // The deletions.jsonl validation has been removed. - - t.Run("issue count decreased warns but succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - // Decreases are allowed (just warn) - err := validatePostImport(10, 5, jsonlPath) - if err != nil { - t.Errorf("Expected no error (just warning) for decreased count, got: %v", err) - } - }) - - t.Run("issue count same succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - err := validatePostImport(10, 10, jsonlPath) - if err != nil { - t.Errorf("Expected no error for same count, got: %v", err) - } - }) - - t.Run("issue count increased succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - err := validatePostImport(10, 15, jsonlPath) - if err != nil { - t.Errorf("Expected no error for increased count, got: %v", err) - } - }) -} - -func TestValidatePostImportWithExpectedDeletions(t *testing.T) { - // Note: validatePostImportWithExpectedDeletions no longer fails on decreases - - // it only warns. The deletions.jsonl validation has been removed. - - t.Run("decrease fully accounted for by expected deletions succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - err := validatePostImportWithExpectedDeletions(26, 25, 1, jsonlPath) - if err != nil { - t.Errorf("Expected no error when decrease matches expected deletions, got: %v", err) - } - }) - - t.Run("decrease exceeds expected deletions warns but succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - // Decrease of 5, expected 2 - used to fail, now warns - err := validatePostImportWithExpectedDeletions(20, 15, 2, jsonlPath) - if err != nil { - t.Errorf("Expected no error (just warning) for decreased count, got: %v", err) - } - }) - - t.Run("no decrease succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - err := validatePostImportWithExpectedDeletions(10, 10, 5, jsonlPath) - if err != nil { - t.Errorf("Expected no error for same count, got: %v", err) - } - }) - - t.Run("increase succeeds", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - err := validatePostImportWithExpectedDeletions(10, 15, 0, jsonlPath) - if err != nil { - t.Errorf("Expected no error for increased count, got: %v", err) - } - }) -} - -func TestCountDBIssuesSuite(t *testing.T) { - tmpDir := t.TempDir() - dbPath := filepath.Join(tmpDir, "test.db") - s := newTestStoreWithPrefix(t, dbPath, "test") - ctx := context.Background() - - t.Run("count issues in database", func(t *testing.T) { - // Initially 0 - count, err := countDBIssues(ctx, s) - if err != nil { - t.Fatalf("Failed to count issues: %v", err) - } - if count != 0 { - t.Errorf("Expected 0 issues, got %d", count) - } - - // Add issues with unique IDs - for i := 1; i <= 3; i++ { - issue := &types.Issue{ - ID: fmt.Sprintf("test-count-%d", i), - Title: "Test", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue", - } - if err := s.CreateIssue(ctx, issue, "test"); err != nil { - t.Fatalf("Failed to create issue: %v", err) - } - } - - // Should be 3 - count, err = countDBIssues(ctx, s) - if err != nil { - t.Fatalf("Failed to count issues: %v", err) - } - if count != 3 { - t.Errorf("Expected 3 issues, got %d", count) - } - }) -} - -func TestHasJSONLChangedSuite(t *testing.T) { - tmpDir := t.TempDir() - dbPath := filepath.Join(tmpDir, "test.db") - s := newTestStore(t, dbPath) - ctx := context.Background() - - t.Run("hash matches - no change", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h1" - - // Create JSONL file - jsonlContent := `{"id":"test-h1","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Compute hash and store it - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash:"+keySuffix, hash); err != nil { - t.Fatalf("Failed to set metadata: %v", err) - } - - // Store mtime for fast-path - if info, err := os.Stat(jsonlPath); err == nil { - mtimeStr := fmt.Sprintf("%d", info.ModTime().Unix()) - if err := s.SetMetadata(ctx, "last_import_mtime:"+keySuffix, mtimeStr); err != nil { - t.Fatalf("Failed to set mtime: %v", err) - } - } - - // Should return false (no change) - if hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return false for matching hash") - } - }) - - t.Run("hash differs - has changed", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h2" - - // Create initial JSONL file - jsonlContent := `{"id":"test-h2","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Compute hash and store it - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash:"+keySuffix, hash); err != nil { - t.Fatalf("Failed to set metadata: %v", err) - } - - // Modify JSONL file - newContent := `{"id":"test-h2","title":"Modified","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(newContent), 0600); err != nil { - t.Fatalf("Failed to write modified JSONL: %v", err) - } - - // Should return true (content changed) - if !hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return true for different hash") - } - }) - - t.Run("empty file", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h3" - - // Create empty JSONL file - if err := os.WriteFile(jsonlPath, []byte(""), 0600); err != nil { - t.Fatalf("Failed to write empty JSONL: %v", err) - } - - // Should return true (no previous hash, first run) - if !hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return true for empty file with no metadata") - } - }) - - t.Run("missing metadata - first run", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h4" - - // Create JSONL file - jsonlContent := `{"id":"test-h4","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // No metadata stored - should return true (assume changed) - if !hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return true when no metadata exists") - } - }) - - t.Run("file read error", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "nonexistent.jsonl") - keySuffix := "h5" - - // File doesn't exist - should return false (don't auto-import broken files) - if hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return false for nonexistent file") - } - }) - - t.Run("mtime fast-path - unchanged", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h6" - - // Create JSONL file - jsonlContent := `{"id":"test-h6","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Get file info - info, err := os.Stat(jsonlPath) - if err != nil { - t.Fatalf("Failed to stat JSONL: %v", err) - } - - // Store hash and mtime - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash:"+keySuffix, hash); err != nil { - t.Fatalf("Failed to set hash: %v", err) - } - - mtimeStr := fmt.Sprintf("%d", info.ModTime().Unix()) - if err := s.SetMetadata(ctx, "last_import_mtime:"+keySuffix, mtimeStr); err != nil { - t.Fatalf("Failed to set mtime: %v", err) - } - - // Should return false using fast-path (mtime unchanged) - if hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return false using mtime fast-path") - } - }) - - t.Run("mtime changed but content same - git operation scenario", func(t *testing.T) { - jsonlTmpDir := t.TempDir() - jsonlPath := filepath.Join(jsonlTmpDir, "issues.jsonl") - keySuffix := "h7" - - // Create JSONL file - jsonlContent := `{"id":"test-h7","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - // Get initial file info - initialInfo, err := os.Stat(jsonlPath) - if err != nil { - t.Fatalf("Failed to stat JSONL: %v", err) - } - - // Store hash and old mtime - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - if err := s.SetMetadata(ctx, "jsonl_content_hash:"+keySuffix, hash); err != nil { - t.Fatalf("Failed to set hash: %v", err) - } - - oldMtime := fmt.Sprintf("%d", initialInfo.ModTime().Unix()-1000) // Old mtime - if err := s.SetMetadata(ctx, "last_import_mtime:"+keySuffix, oldMtime); err != nil { - t.Fatalf("Failed to set old mtime: %v", err) - } - - // Touch file to simulate git operation (new mtime, same content) - time.Sleep(10 * time.Millisecond) // ensure timestamp advances - futureTime := time.Now().Add(1 * time.Second) - if err := os.Chtimes(jsonlPath, futureTime, futureTime); err != nil { - t.Fatalf("Failed to touch JSONL: %v", err) - } - - // Should return false (content hasn't changed despite new mtime) - if hasJSONLChanged(ctx, s, jsonlPath, keySuffix) { - t.Error("Expected hasJSONLChanged to return false for git operation with same content") - } - }) -} - -func TestComputeJSONLHash(t *testing.T) { - t.Run("computes hash correctly", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "issues.jsonl") - - jsonlContent := `{"id":"test-ch1","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL: %v", err) - } - - hash, err := computeJSONLHash(jsonlPath) - if err != nil { - t.Fatalf("Failed to compute hash: %v", err) - } - - if hash == "" { - t.Error("Expected non-empty hash") - } - - if len(hash) != 64 { // SHA256 hex is 64 chars - t.Errorf("Expected hash length 64, got %d", len(hash)) - } - }) - - t.Run("same content produces same hash", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath1 := filepath.Join(tmpDir, "issues1.jsonl") - jsonlPath2 := filepath.Join(tmpDir, "issues2.jsonl") - - jsonlContent := `{"id":"test-ch2","title":"Test","status":"open","priority":1} -` - if err := os.WriteFile(jsonlPath1, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL 1: %v", err) - } - if err := os.WriteFile(jsonlPath2, []byte(jsonlContent), 0600); err != nil { - t.Fatalf("Failed to write JSONL 2: %v", err) - } - - hash1, err := computeJSONLHash(jsonlPath1) - if err != nil { - t.Fatalf("Failed to compute hash 1: %v", err) - } - - hash2, err := computeJSONLHash(jsonlPath2) - if err != nil { - t.Fatalf("Failed to compute hash 2: %v", err) - } - - if hash1 != hash2 { - t.Errorf("Expected same hash for same content, got %s and %s", hash1, hash2) - } - }) - - t.Run("different content produces different hash", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath1 := filepath.Join(tmpDir, "issues1.jsonl") - jsonlPath2 := filepath.Join(tmpDir, "issues2.jsonl") - - if err := os.WriteFile(jsonlPath1, []byte(`{"id":"test-ch3a"}`), 0600); err != nil { - t.Fatalf("Failed to write JSONL 1: %v", err) - } - if err := os.WriteFile(jsonlPath2, []byte(`{"id":"test-ch3b"}`), 0600); err != nil { - t.Fatalf("Failed to write JSONL 2: %v", err) - } - - hash1, err := computeJSONLHash(jsonlPath1) - if err != nil { - t.Fatalf("Failed to compute hash 1: %v", err) - } - - hash2, err := computeJSONLHash(jsonlPath2) - if err != nil { - t.Fatalf("Failed to compute hash 2: %v", err) - } - - if hash1 == hash2 { - t.Errorf("Expected different hashes for different content") - } - }) - - t.Run("nonexistent file returns error", func(t *testing.T) { - tmpDir := t.TempDir() - jsonlPath := filepath.Join(tmpDir, "nonexistent.jsonl") - - _, err := computeJSONLHash(jsonlPath) - if err == nil { - t.Error("Expected error for nonexistent file, got nil") - } - }) -} - -func TestCheckOrphanedDepsSuite(t *testing.T) { - tmpDir := t.TempDir() - dbPath := filepath.Join(tmpDir, "test.db") - s := newTestStoreWithPrefix(t, dbPath, "test") - ctx := context.Background() - - t.Run("function executes without error", func(t *testing.T) { - // Create two issues with unique IDs - issue1 := &types.Issue{ - ID: "test-orphan-1", - Title: "Test 1", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue 1", - } - if err := s.CreateIssue(ctx, issue1, "test"); err != nil { - t.Fatalf("Failed to create issue 1: %v", err) - } - - issue2 := &types.Issue{ - ID: "test-orphan-2", - Title: "Test 2", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue 2", - } - if err := s.CreateIssue(ctx, issue2, "test"); err != nil { - t.Fatalf("Failed to create issue 2: %v", err) - } - - // Add dependency - dep := &types.Dependency{ - IssueID: "test-orphan-1", - DependsOnID: "test-orphan-2", - Type: types.DepBlocks, - } - if err := s.AddDependency(ctx, dep, "test"); err != nil { - t.Fatalf("Failed to add dependency: %v", err) - } - - // Check for orphaned deps - should succeed without error - // Note: Database maintains referential integrity, so we can't easily create orphaned deps in tests - // This test verifies the function executes correctly - orphaned, err := checkOrphanedDeps(ctx, s) - if err != nil { - t.Fatalf("Failed to check orphaned deps: %v", err) - } - - // With proper foreign keys, there should be no orphaned dependencies - if len(orphaned) != 0 { - t.Logf("Note: Found %d orphaned dependencies (unexpected with FK constraints): %v", len(orphaned), orphaned) - } - }) - - t.Run("no orphaned dependencies", func(t *testing.T) { - // Create two issues with unique IDs - issue1 := &types.Issue{ - ID: "test-orphan-3", - Title: "Test 3", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue 3", - } - if err := s.CreateIssue(ctx, issue1, "test"); err != nil { - t.Fatalf("Failed to create issue 1: %v", err) - } - - issue2 := &types.Issue{ - ID: "test-orphan-4", - Title: "Test 4", - Status: types.StatusOpen, - Priority: 1, - IssueType: types.TypeTask, - Description: "Test issue 4", - } - if err := s.CreateIssue(ctx, issue2, "test"); err != nil { - t.Fatalf("Failed to create issue 2: %v", err) - } - - // Add valid dependency - dep := &types.Dependency{ - IssueID: "test-orphan-3", - DependsOnID: "test-orphan-4", - Type: types.DepBlocks, - } - if err := s.AddDependency(ctx, dep, "test"); err != nil { - t.Fatalf("Failed to add dependency: %v", err) - } - - // Check for orphaned deps - orphaned, err := checkOrphanedDeps(ctx, s) - if err != nil { - t.Fatalf("Failed to check orphaned deps: %v", err) - } - - if len(orphaned) != 0 { - t.Errorf("Expected 0 orphaned dependencies, got %d: %v", len(orphaned), orphaned) - } - }) -} diff --git a/cmd/bd/rename_prefix.go b/cmd/bd/rename_prefix.go index d376cd0ea..69486d047 100644 --- a/cmd/bd/rename_prefix.go +++ b/cmd/bd/rename_prefix.go @@ -88,36 +88,6 @@ NOTE: This is a rare operation. Most users never need this command.`, FatalError("%v", err) } - // Get JSONL path for sync operations - jsonlPath := findJSONLPath() - - // Force import from JSONL to ensure DB has all issues before rename - // This prevents data loss if JSONL has issues from other workspaces - if !dryRun && jsonlPath != "" { - if _, err := os.Stat(jsonlPath); err == nil { - // JSONL exists - force import to sync all issues to DB - issues, err := parseJSONLFile(jsonlPath) - if err != nil { - FatalError("failed to read JSONL before rename: %v", err) - } - if len(issues) > 0 { - opts := ImportOptions{ - DryRun: false, - SkipUpdate: false, - Strict: false, - SkipPrefixValidation: true, // Allow any prefix during rename - } - result, err := importIssuesCore(ctx, dbPath, store, issues, opts) - if err != nil { - FatalError("failed to sync JSONL before rename: %v", err) - } - if result.Created > 0 || result.Updated > 0 { - fmt.Printf("Synced %d issues from JSONL before rename\n", result.Created+result.Updated) - } - } - } - } - oldPrefix, err := store.GetConfig(ctx, "issue_prefix") if err != nil || oldPrefix == "" { FatalError("failed to get current prefix: %v", err) @@ -193,22 +163,6 @@ NOTE: This is a rare operation. Most users never need this command.`, FatalError("failed to rename prefix: %v", err) } - // Force export to JSONL with new IDs - // Safe because we imported all JSONL issues before rename - if jsonlPath != "" { - // Clear metadata hashes so integrity check doesn't fail - _ = store.SetMetadata(ctx, "jsonl_content_hash", "") // Best effort: stale hashes will be recomputed on next export - _ = store.SetMetadata(ctx, "export_hashes", "") // Best effort: stale hashes will be recomputed on next export - - // Export renamed issues directly to JSONL - if err := exportToJSONLWithStore(ctx, store, jsonlPath); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to export: %v\n", err) - fmt.Fprintf(os.Stderr, "Run 'bd export --force' to update JSONL\n") - } else { - fmt.Printf("Updated %s with new IDs\n", jsonlPath) - } - } - fmt.Printf("%s Successfully renamed prefix from %s to %s\n", ui.RenderPass("✓"), ui.RenderAccent(oldPrefix), ui.RenderAccent(newPrefix)) if jsonOutput { @@ -400,23 +354,6 @@ func repairPrefixes(ctx context.Context, st *dolt.DoltStore, actorName string, t return fmt.Errorf("failed to update config: %w", err) } - // Force export to JSONL with new IDs - // Safe because we imported all JSONL issues before repair (done in caller) - jsonlPath := findJSONLPath() - if jsonlPath != "" { - // Clear metadata hashes so integrity check doesn't fail - _ = st.SetMetadata(ctx, "jsonl_content_hash", "") // Best effort: stale hashes will be recomputed on next export - _ = st.SetMetadata(ctx, "export_hashes", "") // Best effort: stale hashes will be recomputed on next export - - // Export renamed issues directly to JSONL - if err := exportToJSONLWithStore(ctx, st, jsonlPath); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to export: %v\n", err) - fmt.Fprintf(os.Stderr, "Run 'bd export --force' to update JSONL\n") - } else { - fmt.Printf("Updated %s with new IDs\n", jsonlPath) - } - } - fmt.Printf("\n%s Successfully consolidated %d prefixes into %s\n", ui.RenderPass("✓"), len(prefixes), ui.RenderAccent(targetPrefix)) fmt.Printf(" %d issues repaired, %d issues unchanged\n", len(incorrectIssues), len(correctIssues)) diff --git a/cmd/bd/repo.go b/cmd/bd/repo.go index be4afe50c..b139ab10c 100644 --- a/cmd/bd/repo.go +++ b/cmd/bd/repo.go @@ -205,24 +205,17 @@ var repoSyncCmd = &cobra.Command{ Short: "Manually trigger multi-repo sync", Long: `Trigger synchronization from all configured repositories. -This hydrates issues from all repos in repos.additional into the -local database, then exports any local changes back to JSONL.`, +This triggers Dolt push/pull for configured repositories.`, RunE: func(cmd *cobra.Command, args []string) error { if err := ensureDirectMode("repo sync requires direct database access"); err != nil { return err } - ctx := rootCtx - - // Import from all repos - jsonlPath := findJSONLPath() - if err := importToJSONLWithStore(ctx, store, jsonlPath); err != nil { - return fmt.Errorf("import failed: %w", err) - } - - // Export to all repos - if err := exportToJSONLWithStore(ctx, store, jsonlPath); err != nil { - return fmt.Errorf("export failed: %w", err) + // Dolt handles sync natively via push/pull + if hasRemote, err := store.HasRemote(rootCtx, "origin"); err == nil && hasRemote { + if err := store.Push(rootCtx); err != nil { + return fmt.Errorf("dolt push failed: %w", err) + } } if jsonOutput { diff --git a/cmd/bd/restore.go b/cmd/bd/restore.go index 0116584bc..ce4c95ac7 100644 --- a/cmd/bd/restore.go +++ b/cmd/bd/restore.go @@ -59,61 +59,10 @@ This is read-only and does not modify the database or git state.`, os.Exit(1) } - commitHash := *issue.CompactedAtCommit - - // Find JSONL path - jsonlPath := findJSONLPath() - if jsonlPath == "" { - fmt.Fprintf(os.Stderr, "Error: not in a bd workspace (no .beads directory found)\n") - os.Exit(1) - } - - // Get current git HEAD for restoration - currentHead, err := getCurrentGitHead() - if err != nil { - fmt.Fprintf(os.Stderr, "Error: cannot determine current git HEAD: %v\n", err) - os.Exit(1) - } - - // Check for uncommitted changes - hasChanges, err := gitHasUncommittedChanges() - if err != nil { - fmt.Fprintf(os.Stderr, "Error checking git status: %v\n", err) - os.Exit(1) - } - if hasChanges { - fmt.Fprintf(os.Stderr, "Error: you have uncommitted changes\n") - fmt.Fprintf(os.Stderr, "Hint: commit or stash changes before running restore\n") - os.Exit(1) - } - - // Checkout the historical commit - if err := gitCheckout(commitHash); err != nil { - fmt.Fprintf(os.Stderr, "Error checking out commit %s: %v\n", commitHash, err) - os.Exit(1) - } - - // Ensure we return to current state - defer func() { - if err := gitCheckout(currentHead); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to return to %s: %v\n", currentHead, err) - } - }() - - // Read the issue from JSONL at this commit - historicalIssue, err := readIssueFromJSONL(jsonlPath, issueID) - if err != nil { - fmt.Fprintf(os.Stderr, "Error reading issue from historical JSONL: %v\n", err) - os.Exit(1) - } - - if historicalIssue == nil { - fmt.Fprintf(os.Stderr, "Error: issue %s not found in JSONL at commit %s\n", issueID, commitHash) - os.Exit(1) - } - - // Display the restored issue - displayRestoredIssue(historicalIssue, commitHash) + // TODO(gt-vemg9q.2): Replace with dolt_history_ table query + fmt.Fprintf(os.Stderr, "Error: bd restore requires Dolt history tables (JSONL restore removed)\n") + fmt.Fprintf(os.Stderr, "Hint: use 'bd show %s --as-of ' for historical data\n", issueID) + os.Exit(1) }, } diff --git a/cmd/bd/stubs.go b/cmd/bd/stubs.go deleted file mode 100644 index 451c2dac8..000000000 --- a/cmd/bd/stubs.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "fmt" - "os" - "os/exec" - "path/filepath" - - "github.com/steveyegge/beads/internal/beads" -) - -// findJSONLPath returns the path to the JSONL file for the current beads directory. -// Returns empty string if no beads directory is found. -func findJSONLPath() string { - beadsDir := beads.FindBeadsDir() - if beadsDir == "" { - return "" - } - for _, name := range []string{"issues.jsonl", "beads.jsonl"} { - p := filepath.Join(beadsDir, name) - if _, err := os.Stat(p); err == nil { - return p - } - } - return "" -} - -// readFromGitRef reads a file from a specific git ref (e.g., HEAD, branch name). -// Returns the raw file contents from git. -func readFromGitRef(filePath, gitRef string) ([]byte, error) { - // Use git show to read the file at the given ref. - // The ref:path argument is a single token for git-show, not a shell command. - arg := gitRef + ":" + filePath - cmd := exec.Command("git", "show", arg) // #nosec G204 -- args are not shell-interpreted - output, err := cmd.Output() - if err != nil { - return nil, fmt.Errorf("failed to read %s from git ref %s: %w", filePath, gitRef, err) - } - return output, nil -} diff --git a/cmd/bd/sync.go b/cmd/bd/sync.go index ecfcddb2a..9fde83b16 100644 --- a/cmd/bd/sync.go +++ b/cmd/bd/sync.go @@ -3,37 +3,24 @@ package main import ( "fmt" "os" - "path/filepath" "github.com/spf13/cobra" "github.com/steveyegge/beads/internal/beads" - "github.com/steveyegge/beads/internal/config" ) -// syncCmd exports Dolt database to JSONL for backward compatibility. -// With Dolt-native storage, writes are persisted immediately — but callers -// (hooks, scripts) still expect "bd sync" to produce an up-to-date JSONL file. +// syncCmd commits pending changes and pushes to Dolt remote. var syncCmd = &cobra.Command{ Use: "sync", GroupID: "sync", - Short: "Export database to JSONL (Dolt persists writes immediately)", - Long: `With Dolt-native storage, all writes are persisted immediately. -This command exports the database to JSONL so that the on-disk JSONL file -stays in sync with Dolt, which is required by bd doctor and git-based workflows. + Short: "Commit pending changes and push to Dolt remote", + Long: `Commits any pending Dolt changes and pushes to the configured remote. For Dolt remote operations, use: bd dolt push Push to Dolt remote - bd dolt pull Pull from Dolt remote - -For data interchange: - bd export Export database to JSONL - bd import Import JSONL into database`, + bd dolt pull Pull from Dolt remote`, Run: func(_ *cobra.Command, _ []string) { - // The global store is already opened by PersistentPreRun with the - // access lock held. Use it directly instead of spawning a subprocess - // (which would deadlock on the same lock). if store == nil { - return // No database open, nothing to export + return // No database open, nothing to sync } beadsDir := beads.FindBeadsDir() if beadsDir == "" { @@ -42,7 +29,6 @@ For data interchange: // Batch commit: if auto-commit mode is "batch", commit all pending // changes as a single logical Dolt commit before sync operations. - // This is the primary commit boundary for batch mode. if mode, err := getDoltAutoCommitMode(); err == nil && mode == doltAutoCommitBatch { if _, commitErr := store.CommitPending(rootCtx, getActor()); commitErr != nil { fmt.Fprintf(os.Stderr, "Warning: batch commit failed: %v\n", commitErr) @@ -52,27 +38,7 @@ For data interchange: // Mark as explicit commit so PersistentPostRun doesn't double-commit commandDidExplicitDoltCommit = true - // In dolt-native mode, skip JSONL export — Dolt is the source of truth. - // Only push to Dolt remote if configured. - if config.GetSyncMode() == config.SyncModeDoltNative { - if hasRemote, err := store.HasRemote(rootCtx, "origin"); err == nil && hasRemote { - if err := store.Push(rootCtx); err != nil { - fmt.Fprintf(os.Stderr, "Warning: Dolt push failed: %v\n", err) - } else { - fmt.Fprintf(os.Stderr, "Pushed to Dolt git remote\n") - } - } - return - } - - jsonlPath := filepath.Join(beadsDir, "issues.jsonl") - if err := exportToJSONLWithStore(rootCtx, store, jsonlPath); err != nil { - fmt.Fprintf(os.Stderr, "Warning: export failed: %v\n", err) - } - - // Dolt-in-Git: if the Dolt store has a git remote configured, - // push natively via DOLT_PUSH. This is additive — runs after - // JSONL export succeeds (backward compat preserved). + // Push to Dolt remote if configured if hasRemote, err := store.HasRemote(rootCtx, "origin"); err == nil && hasRemote { if err := store.Push(rootCtx); err != nil { fmt.Fprintf(os.Stderr, "Warning: Dolt push failed: %v\n", err) @@ -80,7 +46,6 @@ For data interchange: fmt.Fprintf(os.Stderr, "Pushed to Dolt git remote\n") } } - }, } diff --git a/cmd/bd/sync_helpers.go b/cmd/bd/sync_helpers.go deleted file mode 100644 index 10814d2f4..000000000 --- a/cmd/bd/sync_helpers.go +++ /dev/null @@ -1,301 +0,0 @@ -package main - -import ( - "bufio" - "cmp" - "context" - "encoding/json" - "fmt" - "log/slog" - "os" - "path/filepath" - "slices" - "strings" - "time" - - "github.com/steveyegge/beads/internal/config" - "github.com/steveyegge/beads/internal/storage/dolt" - "github.com/steveyegge/beads/internal/types" -) - -// getRepoKeyForPath extracts the stable repo identifier from a JSONL path. -// For single-repo mode, returns empty string (no suffix needed). -// For multi-repo mode, extracts the repo path (e.g., ".", "../frontend"). -func getRepoKeyForPath(jsonlPath string) string { - multiRepo := config.GetMultiRepoConfig() - if multiRepo == nil { - return "" // Single-repo mode - } - - const suffix = "/.beads/issues.jsonl" - if strings.HasSuffix(jsonlPath, suffix) { - repoPath := strings.TrimSuffix(jsonlPath, suffix) - - primaryPath := multiRepo.Primary - if primaryPath == "" { - primaryPath = "." - } - if repoPath == primaryPath { - return primaryPath - } - - for _, additional := range multiRepo.Additional { - if repoPath == additional { - return additional - } - } - } - - return "" -} - -// sanitizeMetadataKey replaces characters that conflict with metadata key format. -func sanitizeMetadataKey(key string) string { - return strings.ReplaceAll(key, ":", "_") -} - -// exportToJSONLWithStore exports issues to JSONL using the provided store. -// If multi-repo mode is configured, routes issues to their respective JSONL files. -// Otherwise, exports to a single JSONL file. -func exportToJSONLWithStore(ctx context.Context, store *dolt.DoltStore, jsonlPath string) error { - // Get all issues for export - issues, err := store.SearchIssues(ctx, "", types.IssueFilter{}) - if err != nil { - return fmt.Errorf("failed to get issues: %w", err) - } - - // Safety check: prevent exporting empty database over non-empty JSONL - if len(issues) == 0 { - existingCount, err := countIssuesInJSONL(jsonlPath) - if err != nil { - if !os.IsNotExist(err) { - return fmt.Errorf("warning: failed to read existing JSONL: %w", err) - } - } else if existingCount > 0 { - return fmt.Errorf("refusing to export empty database over non-empty JSONL file (database: 0 issues, JSONL: %d issues). This would result in data loss", existingCount) - } - } - - // Sort by ID for consistent output - slices.SortFunc(issues, func(a, b *types.Issue) int { - return cmp.Compare(a.ID, b.ID) - }) - - // Populate dependencies for all issues - allDeps, err := store.GetAllDependencyRecords(ctx) - if err != nil { - return fmt.Errorf("failed to get dependencies: %w", err) - } - for _, issue := range issues { - issue.Dependencies = allDeps[issue.ID] - } - - // Populate labels for all issues - for _, issue := range issues { - labels, err := store.GetLabels(ctx, issue.ID) - if err != nil { - return fmt.Errorf("failed to get labels for %s: %w", issue.ID, err) - } - issue.Labels = labels - } - - // Populate comments for all issues - for _, issue := range issues { - comments, err := store.GetIssueComments(ctx, issue.ID) - if err != nil { - return fmt.Errorf("failed to get comments for %s: %w", issue.ID, err) - } - issue.Comments = comments - } - - // Create temp file for atomic write - dir := filepath.Dir(jsonlPath) - base := filepath.Base(jsonlPath) - tempFile, err := os.CreateTemp(dir, base+".tmp.*") - if err != nil { - return fmt.Errorf("failed to create temp file: %w", err) - } - tempPath := tempFile.Name() - - // Use defer pattern for proper cleanup - var writeErr error - defer func() { - _ = tempFile.Close() - if writeErr != nil { - _ = os.Remove(tempPath) - } - }() - - // Write JSONL - for _, issue := range issues { - data, marshalErr := json.Marshal(issue) - if marshalErr != nil { - writeErr = fmt.Errorf("failed to marshal issue %s: %w", issue.ID, marshalErr) - return writeErr - } - if _, writeErr = tempFile.Write(data); writeErr != nil { - writeErr = fmt.Errorf("failed to write issue %s: %w", issue.ID, writeErr) - return writeErr - } - if _, writeErr = tempFile.WriteString("\n"); writeErr != nil { - writeErr = fmt.Errorf("failed to write newline: %w", writeErr) - return writeErr - } - } - - // Close before rename - if writeErr = tempFile.Close(); writeErr != nil { - writeErr = fmt.Errorf("failed to close temp file: %w", writeErr) - return writeErr - } - - // Atomic rename - if writeErr = os.Rename(tempPath, jsonlPath); writeErr != nil { - writeErr = fmt.Errorf("failed to rename temp file: %w", writeErr) - return writeErr - } - - return nil -} - -// importToJSONLWithStore imports issues from JSONL using the provided store. -func importToJSONLWithStore(ctx context.Context, store *dolt.DoltStore, jsonlPath string) error { - file, err := os.Open(jsonlPath) // #nosec G304 - controlled path from config - if err != nil { - return fmt.Errorf("failed to open JSONL: %w", err) - } - defer file.Close() - - var issues []*types.Issue - scanner := bufio.NewScanner(file) - const maxScannerBuffer = 1024 * 1024 // 1MB - scanner.Buffer(make([]byte, 0, maxScannerBuffer), maxScannerBuffer) - lineNum := 0 - - for scanner.Scan() { - lineNum++ - line := scanner.Text() - if line == "" { - continue - } - - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to parse JSONL line %d: %v\n", lineNum, err) - continue - } - issue.SetDefaults() - - if issue.Status == types.StatusClosed && issue.ClosedAt == nil { - now := time.Now() - issue.ClosedAt = &now - } - - issues = append(issues, &issue) - } - - if err := scanner.Err(); err != nil { - return fmt.Errorf("failed to read JSONL: %w", err) - } - - opts := ImportOptions{ - DryRun: false, - SkipUpdate: false, - Strict: false, - SkipPrefixValidation: true, - } - - _, err = importIssuesCore(ctx, "", store, issues, opts) - return err -} - -// exportToJSONL exports the current database state to the JSONL file. -// Delegates to exportToJSONLWithStore using the global store. -func exportToJSONL(ctx context.Context, jsonlPath string) error { - if err := ensureStoreActive(); err != nil { - return fmt.Errorf("activating store: %w", err) - } - if err := exportToJSONLWithStore(ctx, store, jsonlPath); err != nil { - return err - } - repoKey := getRepoKeyForPath(jsonlPath) - updateExportMetadata(ctx, store, jsonlPath, slog.Default(), repoKey) - return nil -} - -// importFromJSONL imports issues from a JSONL file into the database. -func importFromJSONL(ctx context.Context, jsonlPath string) error { - if err := ensureStoreActive(); err != nil { - return fmt.Errorf("activating store: %w", err) - } - return importToJSONLWithStore(ctx, store, jsonlPath) -} - -// loadIssuesFromJSONL reads a JSONL file and returns the parsed issues. -func loadIssuesFromJSONL(jsonlPath string) ([]*types.Issue, error) { - file, err := os.Open(jsonlPath) // #nosec G304 - controlled path - if err != nil { - if os.IsNotExist(err) { - return nil, nil - } - return nil, fmt.Errorf("opening JSONL: %w", err) - } - defer file.Close() - - var issues []*types.Issue - scanner := bufio.NewScanner(file) - const maxScannerBuffer = 1024 * 1024 // 1MB - scanner.Buffer(make([]byte, 0, maxScannerBuffer), maxScannerBuffer) - - lineNum := 0 - for scanner.Scan() { - lineNum++ - line := scanner.Text() - if line == "" { - continue - } - - var issue types.Issue - if err := json.Unmarshal([]byte(line), &issue); err != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to parse JSONL line %d: %v\n", lineNum, err) - continue - } - issues = append(issues, &issue) - } - - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("reading JSONL: %w", err) - } - - return issues, nil -} - -// updateExportMetadata updates jsonl_content_hash and related metadata after a successful export. -func updateExportMetadata(ctx context.Context, store *dolt.DoltStore, jsonlPath string, log *slog.Logger, keySuffix string) { - if keySuffix != "" { - keySuffix = sanitizeMetadataKey(keySuffix) - } - - currentHash, err := computeJSONLHash(jsonlPath) - if err != nil { - log.Info("Warning: failed to compute JSONL hash for metadata update", "error", err) - return - } - - hashKey := "jsonl_content_hash" - timeKey := "last_import_time" - if keySuffix != "" { - hashKey += ":" + keySuffix - timeKey += ":" + keySuffix - } - - if err := store.SetMetadata(ctx, hashKey, currentHash); err != nil { - log.Info("Warning: failed to update metadata", "key", hashKey, "error", err) - log.Info("Next export may require running 'bd import' first") - } - - exportTime := time.Now().Format(time.RFC3339Nano) - if err := store.SetMetadata(ctx, timeKey, exportTime); err != nil { - log.Info("Warning: failed to update metadata", "key", timeKey, "error", err) - } -} diff --git a/cmd/bd/templates/hooks/post-checkout b/cmd/bd/templates/hooks/post-checkout index c80444f4b..2c77d81b7 100755 --- a/cmd/bd/templates/hooks/post-checkout +++ b/cmd/bd/templates/hooks/post-checkout @@ -9,7 +9,7 @@ # in sync with the installed bd version - no manual updates needed. # # The 'bd hook' command (singular) supports: -# - Guard against frequent firing (only imports if JSONL changed) +# - Guard against frequent firing # - Per-worktree state tracking # - Dolt branch-then-merge pattern # - Hook chaining configuration diff --git a/cmd/bd/templates/hooks/post-merge b/cmd/bd/templates/hooks/post-merge index 7b501e31f..85536e6fd 100755 --- a/cmd/bd/templates/hooks/post-merge +++ b/cmd/bd/templates/hooks/post-merge @@ -9,7 +9,7 @@ # in sync with the installed bd version - no manual updates needed. # # The 'bd hook' command (singular) supports: -# - Branch-then-merge pattern for Dolt (cell-level conflict resolution) +# - Dolt push/pull sync # - Per-worktree state tracking # - Hook chaining configuration