fix(embeddeddolt): commit pending changes before push (#5433) (#5795)

* fix(embeddeddolt): commit pending changes before push (#5433)

Every Pull variant (Pull, PullWithStrategy, PullRemoteWithStrategy,
PullRemote, PullFrom) auto-commits pending working-set changes first
(GH#2474, so a crashed command's leftovers don't make the merge
refuse to start), but no Push variant (Push, ForcePush, PushRemote,
PushTo) did. A write that landed in the working set but was never
explicitly committed -- auto-commit configured off, or a session that
ended right after a write with no intervening commit -- silently
pushed nothing new while `bd dolt push` still printed "Push
complete." and exited 0.

This is the half of #5433's silent-stall report the maintainer traced
and confirmed in code (issue comment, quoting exact line numbers this
matches on current main): "every Pull variant calls CommitPending
before pulling... no Push variant does... push never flushes the
working set, then prints 'Push complete.' on nil error with no
remote-ref or working-set check. Strongest hook for a narrow
push-honesty PR." Reporter confirmed: "Yes, please open the
push-honesty PR. That is the surface that actually cost us."

Scoped to exactly this half -- the confirmed, traced, mechanical gap
matching an already-established pattern (copy Pull's own guard to
Push). Did NOT attempt the harder half discussed in the same thread:
verifying the remote ref actually advanced and reporting honestly
when it didn't. That needs a deliberate design call the maintainer
themselves treated as separate (distinguishing a legitimate no-op
push, already in sync, from one that had something to send but
transferred nothing) and risks false positives if done carelessly --
a bigger change than this issue's narrowed scope asked for. Flagging
in the relay rather than guessing at that design.

Also confirmed the server-mode backend (internal/storage/dolt) has
the identical asymmetry -- Push/ForcePush/PushRemote all route through
pushToRemote, which likewise never calls CommitPending -- but the
issue's repro and the maintainer's trace are both embedded-mode
specific, so left server-mode untouched pending its own verification.

Does not claim to fix the mysterious full stall itself (bd dolt
commit also returning "Nothing to commit" while HEAD stays frozen,
even when called directly) -- the reporter and maintainer both
independently concluded that has no known deterministic trigger.
This closes one real, confirmed contributing gap: push not flushing
pending changes the way pull always has.

RED: added TestPushCommitsPendingChangesFirst, table-driven over all
four Push variants. Creates an issue (a genuinely pending, uncommitted
change -- verified via GetCurrentCommit before/after CreateIssue, so
the test would fail loudly if CreateIssue ever started auto-committing
on its own), then calls each Push variant and asserts HEAD moved.
Push/ForcePush/PushRemote route through the swappable vcPush/
vcForcePush vars (remote_peer_auth_test.go's existing
captureRemoteEntryPoints seam) so the test stops with errStopRemote
before any network IO; PushTo calls versioncontrolops.Push directly
with no equivalent seam, so it actually attempts the push and fails on
"remote not found" in this remote-less test store -- still proves
CommitPending ran first, since that failure only happens after this
fix's guard clause. All four failed pre-fix (HEAD did not move).
GREEN: all four pass after the fix.

Verified: full remote-verb/peer-auth suite (TestWithPeerAuth and its
variants, TestRemoteVerbsPresentStoredPeerCredentials,
TestRemoteVerbPlainRemote*) still green -- confirms this fix doesn't
disturb credential resolution or the swappable-seam test
infrastructure it shares. go vet clean, go build ./... clean across
the repo.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018c3vLFNVgPebSpCJrd8xTD

* chore(bazel): enumerate push_commit_pending_test.go in embeddeddolt go_test srcs

Generated by `bazel run //:gazelle`. The PR predates base's addition of
internal/storage/embeddeddolt/BUILD.bazel, so its new test file was not in
the explicit go_test srcs list; bazel would have silently run zero of its
cases. Base parity at the rebase base was 118/118 enumerated.

* fix(autopush): record post-push HEAD, and cover the clean-store push path

Maintainer review fixup for #5795 (adopt-pr review iteration 1).

Push now commits pending changes before pushing (GH#5433), so HEAD can move
across the call. maybeAutoPush read HEAD only before the push and recorded that
hash as LastCommit, which is now the parent of what was actually pushed, so the
next tick saw a mismatch and pushed once redundantly before re-latching. Route
the recorded hash through pushedCommitForState, which re-reads HEAD after a
successful push and falls back to the pre-push value if the re-read fails (the
push itself already succeeded, so dropping LastCommit would lose change
detection entirely).

Also:

- Add a clean-store arm to TestPushCommitsPendingChangesFirst. The guard relies
  on commitAll(tolerateEmpty=true) mapping Dolt's "nothing to commit" to
  (false, nil) for every push against an already-committed store, and nothing
  in this package asserted it. Flipping that flag reds the new arm alone, with
  the four pending-change arms still green.

- Correct the `bd dolt remote reset-data` confirmation, which promised the
  remote is "rebuilt from local HEAD" while the push now adds a commit created
  after the operator types y.

Not taken here, tracked separately: extending auto-push change detection to also
fire on a dirty working set (that gate predates this PR, so skipping it is an
incompleteness rather than a regression this PR introduces), and the PR-body
disclosure that push now commits on the auto-push and `bd sync` cadences.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
This commit is contained in:
Rongjun GENG 2026-09-30 15:58:51 -07:00 • committed by GitHub
commit 8da2ae4328
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 202 additions and 6 deletions

View file

@ -100,6 +100,24 @@ func pushWithContext(ctx context.Context, target autoPushTarget) error {
}
}
// pushedCommitForState returns the commit to record as LastCommit after a
// successful auto-push. Push commits pending changes before pushing (GH#5433),
// so HEAD can move during the push call; reusing the pre-push value would leave
// LastCommit pointing at the parent of what was actually pushed, and the next
// tick would see a mismatch and push once redundantly before re-latching.
//
// A failed re-read is not a push failure -- the push already succeeded -- so
// fall back to the pre-push commit rather than leaving LastCommit unset and
// losing change detection entirely.
func pushedCommitForState(ctx context.Context, target autoPushTarget, prePush string) string {
c, err := target.GetCurrentCommit(ctx)
if err != nil {
debug.Logf("dolt auto-push: failed to re-read commit after push: %v\n", err)
return prePush
}
return c
}
// maybeAutoPush pushes to the Dolt remote if enabled and the debounce interval has passed.
// Called from PersistentPostRun after auto-commit and auto-backup.
func maybeAutoPush(ctx context.Context) {
@ -196,9 +214,9 @@ func maybeAutoPush(ctx context.Context) {
return
}
// Record last push time and commit to local file
// Record last push time and commit to local file.
now := time.Now().UTC().Format(time.RFC3339)
if err := savePushState(&pushState{LastPush: now, LastCommit: currentCommit}); err != nil {
if err := savePushState(&pushState{LastPush: now, LastCommit: pushedCommitForState(ctx, st, currentCommit)}); err != nil {
debug.Logf("dolt auto-push: failed to save push state: %v\n", err)
}

View file

@ -12,12 +12,13 @@ import (
)
type fakeAutoPushTarget struct {
commit string
push func(context.Context) error
commit string
commitErr error
push func(context.Context) error
}
func (f *fakeAutoPushTarget) GetCurrentCommit(context.Context) (string, error) {
return f.commit, nil
return f.commit, f.commitErr
}
func (f *fakeAutoPushTarget) Push(ctx context.Context) error {
@ -265,3 +266,28 @@ func TestLoadPushState_CorruptJSON(t *testing.T) {
t.Error("loadPushState with corrupt JSON: expected error, got nil")
}
}
// TestPushedCommitForState pins the push-state hash recorded after a successful
// auto-push. Push commits pending changes before pushing (GH#5433), so HEAD can
// move during the push call and the pre-push hash is no longer what was pushed.
func TestPushedCommitForState(t *testing.T) {
ctx := context.Background()
t.Run("records the post-push commit", func(t *testing.T) {
target := &fakeAutoPushTarget{commit: "after"}
if got := pushedCommitForState(ctx, target, "before"); got != "after" {
t.Fatalf("pushedCommitForState() = %q, want %q (the commit the guard created, not its parent)", got, "after")
}
})
// The push already succeeded here, so a failed re-read must not drop
// LastCommit -- that would lose change detection entirely. The fake still
// returns a commit alongside the error, so this can only pass if the error
// branch is actually taken.
t.Run("falls back to the pre-push commit when the re-read fails", func(t *testing.T) {
target := &fakeAutoPushTarget{commit: "after", commitErr: errors.New("engine closed")}
if got := pushedCommitForState(ctx, target, "before"); got != "before" {
t.Fatalf("pushedCommitForState() = %q, want %q (pre-push fallback)", got, "before")
}
})
}

View file

@ -282,7 +282,7 @@ Examples:
}
fmt.Printf("This replaces all Dolt data stored on remote %q:\n", name)
fmt.Printf(" %s\n", url)
fmt.Println("The remote is rebuilt from local HEAD; other clones must re-clone.")
fmt.Println("The remote is rebuilt from local HEAD, including any pending changes; other clones must re-clone.")
fmt.Print("Proceed? (y/N): ")
reader := bufio.NewReader(os.Stdin)
response, rerr := reader.ReadString('\n')

View file

@ -184,6 +184,7 @@ go_test(
"promote_no_history_test.go",
"pull_config_wedge_test.go",
"pull_settle_test.go",
"push_commit_pending_test.go",
"querier_contract_test.go",
"reader_contract_test.go",
"ready_claimer_contract_test.go",

View file

@ -0,0 +1,131 @@
//go:build cgo
package embeddeddolt
import (
"errors"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func wantStopRemote(t *testing.T, err error) {
t.Helper()
if !errors.Is(err, errStopRemote) {
t.Fatalf("error = %v, want errStopRemote (stopped before the remote entry point)", err)
}
}
func wantRemoteNotFound(t *testing.T, err error) {
t.Helper()
if err == nil || !strings.Contains(err.Error(), "remote") {
t.Fatalf("error = %v, want a remote-not-found failure (no real remote is configured in this test store)", err)
}
}
// TestPushCommitsPendingChangesFirst is a regression test for #5433: every
// Pull variant auto-commits pending changes before pulling (GH#2474), but no
// Push variant did, so a write that landed in the working set but was never
// explicitly committed (auto-commit off, or a session that ended right after
// a write with no intervening commit) silently pushed nothing new while
// still reporting success. Push/ForcePush/PushRemote/PushTo now call
// CommitPending first, matching Pull's own pattern.
//
// Uses captureRemoteEntryPoints (remote_peer_auth_test.go) to stop each verb
// at its remote entry point with errStopRemote, keeping the test off the
// network while still exercising the real CommitPending call that runs
// before it.
func TestPushCommitsPendingChangesFirst(t *testing.T) {
ctx := t.Context()
cases := []struct {
name string
call func(*EmbeddedDoltStore) error
// wantErr checks the error from call. Push/ForcePush/PushRemote route
// through the swappable vcPush/vcForcePush vars that
// captureRemoteEntryPoints intercepts with errStopRemote before any
// network IO. PushTo calls versioncontrolops.Push directly (no
// swappable seam for it), so it actually attempts the push and fails
// on "remote not found" -- still proves CommitPending ran first,
// since that failure only happens after this fix's guard clause.
wantErr func(t *testing.T, err error)
// clean runs the verb with nothing pending instead of creating an issue
// first. The guard must tolerate an empty working set -- CommitPending
// -> commitAll(tolerateEmpty=true) maps Dolt's "nothing to commit" to
// (false, nil) -- and still reach the remote entry point with HEAD
// unmoved. That is the common case for a push against an
// already-committed store, and nothing else in this package covers it.
clean bool
}{
{name: "Push", call: func(s *EmbeddedDoltStore) error { return s.Push(ctx) }, wantErr: wantStopRemote},
{name: "ForcePush", call: func(s *EmbeddedDoltStore) error { return s.ForcePush(ctx) }, wantErr: wantStopRemote},
{name: "PushRemote", call: func(s *EmbeddedDoltStore) error { return s.PushRemote(ctx, defaultRemote, false) }, wantErr: wantStopRemote},
{name: "PushTo", call: func(s *EmbeddedDoltStore) error { return s.PushTo(ctx, defaultRemote) }, wantErr: wantRemoteNotFound},
{name: "PushCleanStore", call: func(s *EmbeddedDoltStore) error { return s.Push(ctx) }, wantErr: wantStopRemote, clean: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
store := newPeerAuthTestStore(t)
// newPeerAuthTestStore opens a bare store with no issue_prefix
// configured yet; give it one and commit so the fixture itself
// isn't the pending change this test is trying to isolate.
if err := store.SetConfig(ctx, "issue_prefix", "fedauth"); err != nil {
t.Fatalf("SetConfig(issue_prefix): %v", err)
}
if err := store.Commit(ctx, "bd init"); err != nil {
t.Fatalf("Commit(bd init): %v", err)
}
var got presentedAuth
captureRemoteEntryPoints(t, &got)
beforeCreate, err := store.GetCurrentCommit(ctx)
if err != nil {
t.Fatalf("GetCurrentCommit (before create): %v", err)
}
beforePush := beforeCreate
if !tc.clean {
issue := &types.Issue{
ID: "fedauth-" + tc.name,
Title: "pending change",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
}
if err := store.CreateIssue(ctx, issue, "tester"); err != nil {
t.Fatalf("CreateIssue: %v", err)
}
afterCreate, err := store.GetCurrentCommit(ctx)
if err != nil {
t.Fatalf("GetCurrentCommit (after create): %v", err)
}
if afterCreate != beforeCreate {
t.Fatalf("test invariant broken: CreateIssue committed on its own (HEAD moved %s -> %s) -- this test needs a genuinely pending change to be meaningful", beforeCreate, afterCreate)
}
beforePush = afterCreate
}
tc.wantErr(t, tc.call(store))
afterPush, err := store.GetCurrentCommit(ctx)
if err != nil {
t.Fatalf("GetCurrentCommit (after %s): %v", tc.name, err)
}
if tc.clean {
if afterPush != beforePush {
t.Fatalf("%s on a clean store must not create a commit: HEAD moved %s -> %s (the guard's tolerateEmpty tolerance is broken)", tc.name, beforePush, afterPush)
}
return
}
if afterPush == beforePush {
t.Fatalf("%s must commit pending changes before pushing (#5433): HEAD did not move (%s), the created issue is still uncommitted", tc.name, afterPush)
}
})
}
}

View file

@ -538,6 +538,14 @@ func (s *EmbeddedDoltStore) ListRemotes(ctx context.Context) ([]storage.RemoteIn
// where before it read it holding no lock at all.
func (s *EmbeddedDoltStore) Push(ctx context.Context) error {
// GH#5433: every Pull variant below auto-commits pending changes first;
// push never did, so a working set with pending-but-uncommitted changes
// (auto-commit off, or a session that ended right after a write with no
// intervening commit) silently pushed nothing new and still reported
// success.
if _, err := s.CommitPending(ctx, "beads"); err != nil {
return fmt.Errorf("commit pending before push: %w", err)
}
return s.withPeerAuth(ctx, defaultRemote, func(user string) error {
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
return vcPush(ctx, db, defaultRemote, s.branch, user)
@ -603,6 +611,10 @@ func (s *EmbeddedDoltStore) PullRemoteWithStrategy(ctx context.Context, remote,
}
func (s *EmbeddedDoltStore) ForcePush(ctx context.Context) error {
// GH#5433: see Push.
if _, err := s.CommitPending(ctx, "beads"); err != nil {
return fmt.Errorf("commit pending before push: %w", err)
}
return s.withPeerAuth(ctx, defaultRemote, func(user string) error {
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
return vcForcePush(ctx, db, defaultRemote, s.branch, user)
@ -611,6 +623,10 @@ func (s *EmbeddedDoltStore) ForcePush(ctx context.Context) error {
}
func (s *EmbeddedDoltStore) PushRemote(ctx context.Context, remote string, force bool) error {
// GH#5433: see Push.
if _, err := s.CommitPending(ctx, "beads"); err != nil {
return fmt.Errorf("commit pending before push: %w", err)
}
return s.withPeerAuth(ctx, remote, func(user string) error {
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
if force {
@ -647,6 +663,10 @@ func (s *EmbeddedDoltStore) Fetch(ctx context.Context, peer string) error {
}
func (s *EmbeddedDoltStore) PushTo(ctx context.Context, peer string) error {
// GH#5433: see Push.
if _, err := s.CommitPending(ctx, "beads"); err != nil {
return fmt.Errorf("commit pending before push: %w", err)
}
return s.withPeerAuth(ctx, peer, func(user string) error {
return s.withMutatingDBConn(ctx, func(db versioncontrolops.DBConn) error {
return versioncontrolops.Push(ctx, db, peer, s.branch, user)