beads/internal/storage/embeddeddolt/push_commit_pending_test.go
Rongjun GENG 8da2ae4328
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>
2026-09-30 15:58:51 -07:00

131 lines
5 KiB
Go

//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)
}
})
}
}