fix(backup): anchor watermark before snapshot (#7044)

Record the commit read before BackupToDir as the newest revision guaranteed
to be in the snapshot. A commit landing during sync then remains pending for
the next backup instead of being hidden by an advanced watermark.

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Rongjun GENG
This commit is contained in:
Rongjun GENG 2026-10-04 20:01:43 -07:00 • committed by GitHub
commit 874e2866ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 66 additions and 9 deletions

View file

@ -524,6 +524,7 @@ go_test(
"backup_dolt_test.go",
"backup_embedded_test.go",
"backup_export_failure_test.go",
"backup_export_watermark_test.go",
"backup_lock_test.go",
"backup_proxied_external_integration_test.go",
"backup_proxied_local_integration_test.go",

View file

@ -162,12 +162,16 @@ func runBackupExport(ctx context.Context, backend localBackupBackend, force bool
return nil, err
}
// Read the watermark before the sync. A concurrent commit may land while
// BackupToDir is running; recording a later HEAD would claim that commit is
// present in a snapshot that started before it existed.
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current commit: %w", err)
}
// Change detection: skip if nothing changed (unless forced)
if !force {
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current commit: %w", err)
}
if currentCommit == state.LastDoltCommit && state.LastDoltCommit != "" {
debug.Logf("backup: no changes since last backup (commit %s)\n", truncateHash(currentCommit))
return state, nil
@ -191,11 +195,8 @@ func runBackupExport(ctx context.Context, backend localBackupBackend, force bool
return nil, err
}
// Update watermarks
currentCommit, err := backend.CurrentCommit(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get current commit for state: %w", err)
}
// The pre-sync commit is the newest commit this backup is guaranteed to
// contain. If HEAD moved during the sync, the next backup must see it.
state.LastDoltCommit = currentCommit
state.Timestamp = time.Now().UTC()

View file

@ -0,0 +1,55 @@
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/config"
)
type advancingBackupBackend struct {
commit string
backupCommit string
commitAfterSync string
}
func (b *advancingBackupBackend) CurrentCommit(context.Context) (string, error) {
return b.commit, nil
}
func (b *advancingBackupBackend) BackupToDir(context.Context, string) error {
b.backupCommit = b.commit
b.commit = b.commitAfterSync
return nil
}
func TestRunBackupExportWatermarkDoesNotAdvancePastSnapshot(t *testing.T) {
for _, force := range []bool{false, true} {
t.Run(map[bool]string{false: "changed", true: "forced"}[force], func(t *testing.T) {
repo := t.TempDir()
if err := os.MkdirAll(filepath.Join(repo, ".git"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("BD_BACKUP_GIT_REPO", repo)
config.ResetForTesting()
t.Cleanup(config.ResetForTesting)
if err := config.Initialize(); err != nil {
t.Fatalf("config.Initialize: %v", err)
}
backend := &advancingBackupBackend{commit: "head-before-sync", commitAfterSync: "head-after-sync"}
state, err := runBackupExport(context.Background(), backend, force)
if err != nil {
t.Fatalf("runBackupExport: %v", err)
}
if backend.backupCommit != "head-before-sync" {
t.Fatalf("backup captured %q, want head-before-sync", backend.backupCommit)
}
if state.LastDoltCommit != backend.backupCommit {
t.Fatalf("watermark = %q, backup captured %q; a commit that landed during sync must remain pending", state.LastDoltCommit, backend.backupCommit)
}
})
}
}