Fix bd-khnb: Prevent auto-import from resurrecting deleted issues

Replace mtime-based staleness detection with content-based (SHA256 hash) to prevent
git operations from resurrecting deleted issues.

**Problem:**
Auto-import used file modification time to detect if JSONL was "newer" than database.
Git operations (checkout, merge, pull) restore old files with recent mtimes, causing
auto-import to load stale data over current database state, resurrecting deleted issues.

**Solution:**
- Added computeJSONLHash() to compute SHA256 of JSONL content
- Added hasJSONLChanged() with two-tier check:
  1. Fast-path: Check mtime first (99% of checks are instant)
  2. Slow-path: Compute hash only if mtime changed (catches git operations)
- Store metadata: last_import_hash, last_import_mtime, last_import_time
- Updated auto-import in daemon_sync.go to use content-based check
- Updated validatePreExport to use content-based check (bd-xwo)
- Graceful degradation: metadata failures are non-fatal warnings

**Changes:**
- cmd/bd/integrity.go: Add computeJSONLHash(), hasJSONLChanged()
- cmd/bd/integrity_test.go: Add comprehensive tests for new functions
- cmd/bd/import.go: Update metadata after import
- cmd/bd/sync.go: Use hasJSONLChanged() instead of isJSONLNewer()
- cmd/bd/daemon_sync.go: Use hasJSONLChanged() in auto-import

**Testing:**
- Unit tests pass (TestHasJSONLChanged with 7 scenarios)
- Integration test passes (test_bd_khnb_fix.sh)
- Verified git resurrection scenario prevented

Fixes: bd-khnb
Related: bd-3bg, bd-xwo, bd-39o, bd-56p, bd-m8t, bd-rfj, bd-t5o

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-20 21:49:52 -05:00
parent 57253f93a3
commit 0020eb490c
5 changed files with 480 additions and 42 deletions
+347 -14
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
@@ -67,8 +68,17 @@ func TestValidatePreExport(t *testing.T) {
t.Fatalf("Failed to write JSONL: %v", err)
}
// Store hash metadata to indicate JSONL and DB are in sync
hash, err := computeJSONLHash(jsonlPath)
if err != nil {
t.Fatalf("Failed to compute hash: %v", err)
}
if err := store.SetMetadata(ctx, "last_import_hash", hash); err != nil {
t.Fatalf("Failed to set hash metadata: %v", err)
}
// Should pass validation
err := validatePreExport(ctx, store, jsonlPath)
err = validatePreExport(ctx, store, jsonlPath)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
@@ -114,7 +124,7 @@ func TestValidatePreExport(t *testing.T) {
}
})
t.Run("JSONL newer than DB fails", func(t *testing.T) {
t.Run("JSONL content changed fails", func(t *testing.T) {
// Create temp directory
tmpDir := t.TempDir()
beadsDir := filepath.Join(tmpDir, ".beads")
@@ -139,27 +149,36 @@ func TestValidatePreExport(t *testing.T) {
t.Fatalf("Failed to create issue: %v", err)
}
// Create JSONL file with newer timestamp
// Create initial JSONL file
jsonlContent := `{"id":"bd-1","title":"Test","status":"open","priority":1}
`
if err := os.WriteFile(jsonlPath, []byte(jsonlContent), 0600); err != nil {
t.Fatalf("Failed to write JSONL: %v", err)
}
// Touch JSONL to make it newer than DB
// (in real scenario, this happens when git pull updates JSONL but daemon hasn't imported yet)
futureTime := time.Now().Add(1 * time.Second)
if err := os.Chtimes(jsonlPath, futureTime, futureTime); err != nil {
t.Fatalf("Failed to touch JSONL: %v", err)
// Store hash of original content
hash, err := computeJSONLHash(jsonlPath)
if err != nil {
t.Fatalf("Failed to compute hash: %v", err)
}
if err := store.SetMetadata(ctx, "last_import_hash", hash); err != nil {
t.Fatalf("Failed to set hash: %v", err)
}
// Should fail validation (JSONL is newer, must import first)
err := validatePreExport(ctx, store, jsonlPath)
if err == nil {
t.Error("Expected error for JSONL newer than DB, got nil")
// Modify JSONL content (simulates git pull that changed JSONL)
modifiedContent := `{"id":"bd-1","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)
}
if err != nil && !strings.Contains(err.Error(), "JSONL is newer than database") {
t.Errorf("Expected 'JSONL is newer' error, got: %v", err)
// Should fail validation (JSONL content changed, must import first)
err = validatePreExport(ctx, store, 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)
}
})
}
@@ -232,6 +251,320 @@ func TestCountDBIssues(t *testing.T) {
})
}
func TestHasJSONLChanged(t *testing.T) {
ctx := context.Background()
t.Run("hash matches - no change", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// Create JSONL file
jsonlContent := `{"id":"bd-1","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 := store.SetMetadata(ctx, "last_import_hash", 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 := store.SetMetadata(ctx, "last_import_mtime", mtimeStr); err != nil {
t.Fatalf("Failed to set mtime: %v", err)
}
}
// Should return false (no change)
if hasJSONLChanged(ctx, store, jsonlPath) {
t.Error("Expected hasJSONLChanged to return false for matching hash")
}
})
t.Run("hash differs - has changed", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// Create initial JSONL file
jsonlContent := `{"id":"bd-1","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 := store.SetMetadata(ctx, "last_import_hash", hash); err != nil {
t.Fatalf("Failed to set metadata: %v", err)
}
// Modify JSONL file
newContent := `{"id":"bd-1","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, store, jsonlPath) {
t.Error("Expected hasJSONLChanged to return true for different hash")
}
})
t.Run("empty file", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// 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, store, jsonlPath) {
t.Error("Expected hasJSONLChanged to return true for empty file with no metadata")
}
})
t.Run("missing metadata - first run", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// Create JSONL file
jsonlContent := `{"id":"bd-1","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, store, jsonlPath) {
t.Error("Expected hasJSONLChanged to return true when no metadata exists")
}
})
t.Run("file read error", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "nonexistent.jsonl")
// Create database
store := newTestStore(t, dbPath)
// File doesn't exist - should return false (don't auto-import broken files)
if hasJSONLChanged(ctx, store, jsonlPath) {
t.Error("Expected hasJSONLChanged to return false for nonexistent file")
}
})
t.Run("mtime fast-path - unchanged", func(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// Create JSONL file
jsonlContent := `{"id":"bd-1","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 := store.SetMetadata(ctx, "last_import_hash", hash); err != nil {
t.Fatalf("Failed to set hash: %v", err)
}
mtimeStr := fmt.Sprintf("%d", info.ModTime().Unix())
if err := store.SetMetadata(ctx, "last_import_mtime", mtimeStr); err != nil {
t.Fatalf("Failed to set mtime: %v", err)
}
// Should return false using fast-path (mtime unchanged)
if hasJSONLChanged(ctx, store, jsonlPath) {
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) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
jsonlPath := filepath.Join(tmpDir, "issues.jsonl")
// Create database
store := newTestStore(t, dbPath)
// Create JSONL file
jsonlContent := `{"id":"bd-1","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 := store.SetMetadata(ctx, "last_import_hash", hash); err != nil {
t.Fatalf("Failed to set hash: %v", err)
}
oldMtime := fmt.Sprintf("%d", initialInfo.ModTime().Unix()-1000) // Old mtime
if err := store.SetMetadata(ctx, "last_import_mtime", 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 time passes
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, store, jsonlPath) {
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":"bd-1","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":"bd-1","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":"bd-1"}`), 0600); err != nil {
t.Fatalf("Failed to write JSONL 1: %v", err)
}
if err := os.WriteFile(jsonlPath2, []byte(`{"id":"bd-2"}`), 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 TestCheckOrphanedDeps(t *testing.T) {
t.Run("function executes without error", func(t *testing.T) {
// Create temp directory