beads/cmd/bd/dolt_autopush.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

224 lines
7.2 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage"
)
// pushState tracks auto-push state in a local file (.beads/push-state.json)
// instead of the Dolt metadata table, to avoid merge conflicts on multi-machine
// setups (GH#2466).
type pushState struct {
LastPush string `json:"last_push"` // RFC3339 timestamp
LastCommit string `json:"last_commit"` // Dolt commit hash
}
type autoPushTarget interface {
GetCurrentCommit(ctx context.Context) (string, error)
Push(ctx context.Context) error
}
func pushStatePath() (string, error) {
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
return "", fmt.Errorf("%s", activeWorkspaceNotFoundError())
}
return filepath.Join(beadsDir, "push-state.json"), nil
}
func loadPushState() (*pushState, error) {
path, err := pushStatePath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(path) //nolint:gosec // path is constructed internally
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, err
}
var ps pushState
if err := json.Unmarshal(data, &ps); err != nil {
return nil, err
}
return &ps, nil
}
func savePushState(ps *pushState) error {
path, err := pushStatePath()
if err != nil {
return err
}
data, err := json.MarshalIndent(ps, "", " ")
if err != nil {
return err
}
return atomicWriteFile(path, data)
}
// autoPushTimeout bounds the st.Push() call that shells out to git fetch,
// which blocks indefinitely when the remote is unreachable (GH#3370).
const autoPushTimeout = 30 * time.Second
// isDoltAutoPushEnabled returns whether auto-push to Dolt remote should run.
// Returns true only when explicitly opted in via dolt.auto-push=true in
// .beads/config.yaml (or BD_DOLT_AUTO_PUSH=true env var).
//
// Previously the default was to auto-enable when an "origin" remote exists.
// That is unsafe at any concurrency above one writer: git+ssh remotes have no
// chunk-level upload atomicity, so concurrent dolt pushes race on the remote
// manifest and can leave it referencing chunks that were never uploaded. Any
// subsequent fetch/clone/push propagates the dangling reference. Set
// dolt.auto-push=true to restore the old behavior on single-writer setups.
func isDoltAutoPushEnabled(_ context.Context) bool {
return config.GetBool("dolt.auto-push")
}
// pushWithContext is a caller-side guard. Push implementations should honor
// ctx directly, but this keeps auto-push from blocking forever if one does not.
func pushWithContext(ctx context.Context, target autoPushTarget) error {
errCh := make(chan error, 1)
go func() {
errCh <- target.Push(ctx)
}()
select {
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}
// 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) {
if isSandboxMode() {
debug.Logf("dolt auto-push: skipped (sandbox mode)\n")
return
}
if isDoltLocalOnly() {
debug.Logf("dolt auto-push: skipped (dolt.local-only=true)\n")
return
}
if !isDoltAutoPushEnabled(ctx) {
return
}
st := getStore()
if st == nil {
return
}
if lm, ok := storage.UnwrapStore(st).(storage.LifecycleManager); ok && lm.IsClosed() {
return
}
// Load local push state (file-based, not in Dolt metadata table).
// This avoids merge conflicts on multi-machine setups (GH#2466).
ps, err := loadPushState()
if err != nil {
debug.Logf("dolt auto-push: failed to load push state: %v\n", err)
return
}
// Debounce: skip if we pushed recently
interval := config.GetDuration("dolt.auto-push-interval")
if interval == 0 {
interval = 5 * time.Minute
}
if ps != nil && ps.LastPush != "" {
lastPush, err := time.Parse(time.RFC3339, ps.LastPush)
if err == nil && time.Since(lastPush) < interval {
debug.Logf("dolt auto-push: throttled (last push %s ago, interval %s)\n",
time.Since(lastPush).Round(time.Second), interval)
return
}
}
// Change detection: skip if nothing changed since last push
currentCommit, err := st.GetCurrentCommit(ctx)
if err != nil {
debug.Logf("dolt auto-push: failed to get current commit: %v\n", err)
return
}
if ps != nil && currentCommit == ps.LastCommit && ps.LastCommit != "" {
debug.Logf("dolt auto-push: no changes since last push\n")
return
}
// Push with a bounded timeout so an unreachable remote doesn't block
// the CLI indefinitely (GH#3370). The timeout is configurable via
// dolt.auto-push-timeout (default 30s).
pushTimeout := config.GetDuration("dolt.auto-push-timeout")
if pushTimeout == 0 {
pushTimeout = autoPushTimeout
}
pushCtx, pushCancel := context.WithTimeout(ctx, pushTimeout)
defer pushCancel()
debug.Logf("dolt auto-push: pushing to origin (timeout %s)...\n", pushTimeout)
if err := pushWithContext(pushCtx, st); err != nil {
if !isQuiet() && !jsonOutput {
if pushCtx.Err() == context.DeadlineExceeded {
fmt.Fprintf(os.Stderr, "Warning: dolt auto-push timed out after %s (remote may be unreachable)\n", pushTimeout)
} else {
fmt.Fprintf(os.Stderr, "Warning: dolt auto-push failed: %v\n", err)
}
if isAncestorPKMismatchErr(err) {
printAncestorPKMismatchGuidance(err)
} else if isDivergedHistoryErr(err) {
printDivergedHistoryGuidance("push")
}
}
debug.Logf("dolt auto-push: push error: %v\n", err)
// Throttle retries after failure so a hanging remote doesn't make every
// subsequent bd command pay the push timeout. We record the attempt
// timestamp but NOT a new LastCommit, so when the remote recovers the
// change-detection check (currentCommit != LastCommit) still triggers.
if ps == nil {
ps = &pushState{}
}
ps.LastPush = time.Now().UTC().Format(time.RFC3339)
if saveErr := savePushState(ps); saveErr != nil {
debug.Logf("dolt auto-push: failed to save push state after error: %v\n", saveErr)
}
return
}
// Record last push time and commit to local file.
now := time.Now().UTC().Format(time.RFC3339)
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)
}
debug.Logf("dolt auto-push: pushed successfully\n")
}