bd sync: 2025-11-25 10:59:11
This commit is contained in:
File diff suppressed because one or more lines are too long
157
internal/deletions/deletions.go
Normal file
157
internal/deletions/deletions.go
Normal file
@@ -0,0 +1,157 @@
|
||||
// Package deletions handles the deletions manifest for tracking deleted issues.
|
||||
// The deletions.jsonl file is an append-only log that records when issues are
|
||||
// deleted, enabling propagation of deletions across repo clones via git sync.
|
||||
package deletions
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DeletionRecord represents a single deletion entry in the manifest.
|
||||
type DeletionRecord struct {
|
||||
ID string `json:"id"` // Issue ID that was deleted
|
||||
Timestamp time.Time `json:"ts"` // When the deletion occurred
|
||||
Actor string `json:"by"` // Who performed the deletion
|
||||
Reason string `json:"reason,omitempty"` // Optional reason for deletion
|
||||
}
|
||||
|
||||
// LoadDeletions reads the deletions manifest and returns a map for O(1) lookup.
|
||||
// It returns the records, the count of skipped (corrupt) lines, and any error.
|
||||
// Corrupt JSON lines are skipped with a warning rather than failing the load.
|
||||
func LoadDeletions(path string) (map[string]DeletionRecord, int, error) {
|
||||
f, err := os.Open(path) // #nosec G304 - controlled path from caller
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// No deletions file yet - return empty map
|
||||
return make(map[string]DeletionRecord), 0, nil
|
||||
}
|
||||
return nil, 0, fmt.Errorf("failed to open deletions file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
records := make(map[string]DeletionRecord)
|
||||
skipped := 0
|
||||
lineNo := 0
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
// Allow large lines (up to 1MB) in case of very long reasons
|
||||
scanner.Buffer(make([]byte, 0, 1024), 1024*1024)
|
||||
|
||||
for scanner.Scan() {
|
||||
lineNo++
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
var record DeletionRecord
|
||||
if err := json.Unmarshal([]byte(line), &record); err != nil {
|
||||
// Skip corrupt line with warning to stderr
|
||||
fmt.Fprintf(os.Stderr, "Warning: skipping corrupt line %d in deletions manifest: %v\n", lineNo, err)
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if record.ID == "" {
|
||||
fmt.Fprintf(os.Stderr, "Warning: skipping line %d in deletions manifest: missing ID\n", lineNo)
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Use the most recent record for each ID (last write wins)
|
||||
records[record.ID] = record
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, skipped, fmt.Errorf("error reading deletions file: %w", err)
|
||||
}
|
||||
|
||||
return records, skipped, nil
|
||||
}
|
||||
|
||||
// AppendDeletion appends a single deletion record to the manifest.
|
||||
// Creates the file if it doesn't exist.
|
||||
func AppendDeletion(path string, record DeletionRecord) error {
|
||||
// Ensure directory exists
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
// Open file for appending (create if not exists)
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) // #nosec G304 - controlled path
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open deletions file for append: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Marshal record to JSON
|
||||
data, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal deletion record: %w", err)
|
||||
}
|
||||
|
||||
// Write line with newline
|
||||
if _, err := f.Write(append(data, '\n')); err != nil {
|
||||
return fmt.Errorf("failed to write deletion record: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteDeletions atomically writes the entire deletions manifest.
|
||||
// Used for compaction to deduplicate and prune old entries.
|
||||
func WriteDeletions(path string, records []DeletionRecord) error {
|
||||
// Ensure directory exists
|
||||
dir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
// Create temp file in same directory for atomic rename
|
||||
base := filepath.Base(path)
|
||||
tempFile, err := os.CreateTemp(dir, base+".tmp.*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temp file: %w", err)
|
||||
}
|
||||
tempPath := tempFile.Name()
|
||||
defer func() {
|
||||
_ = tempFile.Close()
|
||||
_ = os.Remove(tempPath) // Clean up temp file on error
|
||||
}()
|
||||
|
||||
// Write each record as a JSON line
|
||||
for _, record := range records {
|
||||
data, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal deletion record: %w", err)
|
||||
}
|
||||
if _, err := tempFile.Write(append(data, '\n')); err != nil {
|
||||
return fmt.Errorf("failed to write deletion record: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Close before rename
|
||||
if err := tempFile.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close temp file: %w", err)
|
||||
}
|
||||
|
||||
// Atomic replace
|
||||
if err := os.Rename(tempPath, path); err != nil {
|
||||
return fmt.Errorf("failed to replace deletions file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DefaultPath returns the default path for the deletions manifest.
|
||||
// beadsDir is typically .beads/
|
||||
func DefaultPath(beadsDir string) string {
|
||||
return filepath.Join(beadsDir, "deletions.jsonl")
|
||||
}
|
||||
310
internal/deletions/deletions_test.go
Normal file
310
internal/deletions/deletions_test.go
Normal file
@@ -0,0 +1,310 @@
|
||||
package deletions
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadDeletions_Empty(t *testing.T) {
|
||||
// Non-existent file should return empty map
|
||||
records, skipped, err := LoadDeletions("/nonexistent/path/deletions.jsonl")
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error for non-existent file, got: %v", err)
|
||||
}
|
||||
if skipped != 0 {
|
||||
t.Errorf("expected 0 skipped, got %d", skipped)
|
||||
}
|
||||
if len(records) != 0 {
|
||||
t.Errorf("expected empty map, got %d records", len(records))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
// Create test records
|
||||
now := time.Now().Truncate(time.Millisecond) // Truncate for JSON round-trip
|
||||
record1 := DeletionRecord{
|
||||
ID: "bd-123",
|
||||
Timestamp: now,
|
||||
Actor: "testuser",
|
||||
Reason: "duplicate",
|
||||
}
|
||||
record2 := DeletionRecord{
|
||||
ID: "bd-456",
|
||||
Timestamp: now.Add(time.Hour),
|
||||
Actor: "testuser",
|
||||
}
|
||||
|
||||
// Append records
|
||||
if err := AppendDeletion(path, record1); err != nil {
|
||||
t.Fatalf("AppendDeletion failed: %v", err)
|
||||
}
|
||||
if err := AppendDeletion(path, record2); err != nil {
|
||||
t.Fatalf("AppendDeletion failed: %v", err)
|
||||
}
|
||||
|
||||
// Load and verify
|
||||
records, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
if skipped != 0 {
|
||||
t.Errorf("expected 0 skipped, got %d", skipped)
|
||||
}
|
||||
if len(records) != 2 {
|
||||
t.Fatalf("expected 2 records, got %d", len(records))
|
||||
}
|
||||
|
||||
// Verify record1
|
||||
r1, ok := records["bd-123"]
|
||||
if !ok {
|
||||
t.Fatal("record bd-123 not found")
|
||||
}
|
||||
if r1.Actor != "testuser" {
|
||||
t.Errorf("expected actor 'testuser', got '%s'", r1.Actor)
|
||||
}
|
||||
if r1.Reason != "duplicate" {
|
||||
t.Errorf("expected reason 'duplicate', got '%s'", r1.Reason)
|
||||
}
|
||||
|
||||
// Verify record2
|
||||
r2, ok := records["bd-456"]
|
||||
if !ok {
|
||||
t.Fatal("record bd-456 not found")
|
||||
}
|
||||
if r2.Reason != "" {
|
||||
t.Errorf("expected empty reason, got '%s'", r2.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDeletions_CorruptLines(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
// Write mixed valid and corrupt content
|
||||
content := `{"id":"bd-001","ts":"2024-01-01T00:00:00Z","by":"user1"}
|
||||
this is not valid json
|
||||
{"id":"bd-002","ts":"2024-01-02T00:00:00Z","by":"user2"}
|
||||
{"broken json
|
||||
{"id":"bd-003","ts":"2024-01-03T00:00:00Z","by":"user3","reason":"test"}
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
records, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions should not fail on corrupt lines: %v", err)
|
||||
}
|
||||
if skipped != 2 {
|
||||
t.Errorf("expected 2 skipped lines, got %d", skipped)
|
||||
}
|
||||
if len(records) != 3 {
|
||||
t.Errorf("expected 3 valid records, got %d", len(records))
|
||||
}
|
||||
|
||||
// Verify valid records were loaded
|
||||
for _, id := range []string{"bd-001", "bd-002", "bd-003"} {
|
||||
if _, ok := records[id]; !ok {
|
||||
t.Errorf("expected record %s to be loaded", id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDeletions_MissingID(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
// Write record without ID
|
||||
content := `{"id":"bd-001","ts":"2024-01-01T00:00:00Z","by":"user1"}
|
||||
{"ts":"2024-01-02T00:00:00Z","by":"user2"}
|
||||
{"id":"","ts":"2024-01-03T00:00:00Z","by":"user3"}
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
records, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
// Two lines should be skipped: one missing "id" field, one with empty "id"
|
||||
if skipped != 2 {
|
||||
t.Errorf("expected 2 skipped lines (missing/empty ID), got %d", skipped)
|
||||
}
|
||||
if len(records) != 1 {
|
||||
t.Errorf("expected 1 valid record, got %d", len(records))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDeletions_LastWriteWins(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
// Write same ID twice with different data
|
||||
content := `{"id":"bd-001","ts":"2024-01-01T00:00:00Z","by":"user1","reason":"first"}
|
||||
{"id":"bd-001","ts":"2024-01-02T00:00:00Z","by":"user2","reason":"second"}
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
records, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
if skipped != 0 {
|
||||
t.Errorf("expected 0 skipped, got %d", skipped)
|
||||
}
|
||||
if len(records) != 1 {
|
||||
t.Errorf("expected 1 record (deduplicated), got %d", len(records))
|
||||
}
|
||||
|
||||
r := records["bd-001"]
|
||||
if r.Actor != "user2" {
|
||||
t.Errorf("expected last write to win (user2), got '%s'", r.Actor)
|
||||
}
|
||||
if r.Reason != "second" {
|
||||
t.Errorf("expected last reason 'second', got '%s'", r.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteDeletions_Atomic(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
now := time.Now().Truncate(time.Millisecond)
|
||||
records := []DeletionRecord{
|
||||
{ID: "bd-001", Timestamp: now, Actor: "user1"},
|
||||
{ID: "bd-002", Timestamp: now, Actor: "user2", Reason: "cleanup"},
|
||||
}
|
||||
|
||||
if err := WriteDeletions(path, records); err != nil {
|
||||
t.Fatalf("WriteDeletions failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify by loading
|
||||
loaded, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
if skipped != 0 {
|
||||
t.Errorf("expected 0 skipped, got %d", skipped)
|
||||
}
|
||||
if len(loaded) != 2 {
|
||||
t.Errorf("expected 2 records, got %d", len(loaded))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteDeletions_Overwrite(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
now := time.Now().Truncate(time.Millisecond)
|
||||
|
||||
// Write initial records
|
||||
initial := []DeletionRecord{
|
||||
{ID: "bd-001", Timestamp: now, Actor: "user1"},
|
||||
{ID: "bd-002", Timestamp: now, Actor: "user2"},
|
||||
{ID: "bd-003", Timestamp: now, Actor: "user3"},
|
||||
}
|
||||
if err := WriteDeletions(path, initial); err != nil {
|
||||
t.Fatalf("initial WriteDeletions failed: %v", err)
|
||||
}
|
||||
|
||||
// Overwrite with fewer records (simulates compaction pruning)
|
||||
compacted := []DeletionRecord{
|
||||
{ID: "bd-002", Timestamp: now, Actor: "user2"},
|
||||
}
|
||||
if err := WriteDeletions(path, compacted); err != nil {
|
||||
t.Fatalf("compacted WriteDeletions failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify only compacted records remain
|
||||
loaded, _, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
if len(loaded) != 1 {
|
||||
t.Errorf("expected 1 record after compaction, got %d", len(loaded))
|
||||
}
|
||||
if _, ok := loaded["bd-002"]; !ok {
|
||||
t.Error("expected bd-002 to remain after compaction")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendDeletion_CreatesDirectory(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "nested", "dir", "deletions.jsonl")
|
||||
|
||||
record := DeletionRecord{
|
||||
ID: "bd-001",
|
||||
Timestamp: time.Now(),
|
||||
Actor: "testuser",
|
||||
}
|
||||
|
||||
if err := AppendDeletion(path, record); err != nil {
|
||||
t.Fatalf("AppendDeletion should create parent directories: %v", err)
|
||||
}
|
||||
|
||||
// Verify file exists
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Errorf("file should exist after append: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteDeletions_CreatesDirectory(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "nested", "dir", "deletions.jsonl")
|
||||
|
||||
records := []DeletionRecord{
|
||||
{ID: "bd-001", Timestamp: time.Now(), Actor: "testuser"},
|
||||
}
|
||||
|
||||
if err := WriteDeletions(path, records); err != nil {
|
||||
t.Fatalf("WriteDeletions should create parent directories: %v", err)
|
||||
}
|
||||
|
||||
// Verify file exists
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
t.Errorf("file should exist after write: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultPath(t *testing.T) {
|
||||
path := DefaultPath("/home/user/project/.beads")
|
||||
expected := "/home/user/project/.beads/deletions.jsonl"
|
||||
if path != expected {
|
||||
t.Errorf("expected %s, got %s", expected, path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDeletions_EmptyLines(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
path := filepath.Join(tmpDir, "deletions.jsonl")
|
||||
|
||||
// Write content with empty lines
|
||||
content := `{"id":"bd-001","ts":"2024-01-01T00:00:00Z","by":"user1"}
|
||||
|
||||
{"id":"bd-002","ts":"2024-01-02T00:00:00Z","by":"user2"}
|
||||
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write test file: %v", err)
|
||||
}
|
||||
|
||||
records, skipped, err := LoadDeletions(path)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadDeletions failed: %v", err)
|
||||
}
|
||||
if skipped != 0 {
|
||||
t.Errorf("empty lines should not count as skipped, got %d", skipped)
|
||||
}
|
||||
if len(records) != 2 {
|
||||
t.Errorf("expected 2 records, got %d", len(records))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user