From 412cab2de76da61b525996b9f3abe6cdbb79fc2e Mon Sep 17 00:00:00 2001 From: matt wilkie Date: Sat, 1 Aug 2026 09:38:34 -0700 Subject: [PATCH] fix(embeddeddolt): close adversarial review findings on cb8a2edc6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adversarial review of cb8a2edc6 (adding nothing-to-commit tolerance to EmbeddedDoltStore.Commit/CommitWithConfig) found three issues: 1. MAJOR: Sync's --strategy conflict-resolution commit called the now- tolerant Commit instead of CommitMergeResolution. For the --ours case (resolution dirties nothing), Commit's tolerance would swallow the nothing-to-commit response as success, leaving dolt_merge_status.is_merging true while Sync reports Merged=true and pushes — the exact re-wedge CommitMergeResolution's own doc comment says must not be swallowed. Switched the call site to CommitMergeResolution, mirroring the server-mode twin (dolt/federation.go's Sync). No embedded Sync-with-strategy test seam exists in this package (a full test needs two real federated stores over a working Dolt remote with an induced conflict, which none of the existing embedded tests set up outside //go:build integration); added a WHY comment at the call site instead, per review guidance. 2. MINOR: CommitPending did three sequential engine opens (HEAD read, Commit, HEAD read) to detect whether a commit landed, and had a TOCTOU window where a concurrent HEAD move could misattribute the result. Extracted the shared commit body into a new commitAll(ctx, message, tolerateEmpty) helper that runs the one DOLT_COMMIT('-Am') on a single connection and returns whether it committed; Commit and CommitMergeResolution now call it with tolerateEmpty true/false respectively (discarding the bool), and CommitPending uses the bool directly instead of comparing HEAD before/after. 3. NIT: softened the comment claiming GetMergeBlockers's doc "already flags this exact gap for embedded" — that doc (conflicts.go) is generic, not embedded-specific. Verified: gofmt clean; go vet -tags gms_pure_go ./internal/storage/embeddeddolt/ clean; go build -tags gms_pure_go ./... clean; CGO_ENABLED=1 go test -tags gms_pure_go ./internal/storage/embeddeddolt/ -count=1 passes (including TestEmbeddedSyncCommitsConfigBeforeMerge). Agent-Signature: claude-fable-5-high on behalf of maphew --- internal/storage/embeddeddolt/federation.go | 8 ++- internal/storage/embeddeddolt/store.go | 24 +++---- .../storage/embeddeddolt/version_control.go | 67 ++++++++++++------- 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/internal/storage/embeddeddolt/federation.go b/internal/storage/embeddeddolt/federation.go index caf82a067..a84aa570e 100644 --- a/internal/storage/embeddeddolt/federation.go +++ b/internal/storage/embeddeddolt/federation.go @@ -247,7 +247,13 @@ func (s *EmbeddedDoltStore) Sync(ctx context.Context, peer string, strategy stri } result.ConflictsResolved = true - if err := s.Commit(ctx, fmt.Sprintf("Resolve conflicts from %s using %s strategy", peer, strategy)); err != nil { + // CommitMergeResolution, not Commit: Commit's GH#3886 nothing-to-commit + // tolerance would swallow the --ours case (resolution dirties nothing) + // as a silent no-op here, leaving dolt_merge_status.is_merging true while + // this function reports result.Merged = true and pushes — the exact + // re-wedge CommitMergeResolution's doc comment describes. See the + // server-mode twin, dolt/federation.go's Sync. + if err := s.CommitMergeResolution(ctx, fmt.Sprintf("Resolve conflicts from %s using %s strategy", peer, strategy)); err != nil { result.Error = fmt.Errorf("commit conflict resolution: %w", err) return result, result.Error } diff --git a/internal/storage/embeddeddolt/store.go b/internal/storage/embeddeddolt/store.go index 193f3e986..d740164c1 100644 --- a/internal/storage/embeddeddolt/store.go +++ b/internal/storage/embeddeddolt/store.go @@ -782,24 +782,16 @@ func (s *EmbeddedDoltStore) ActiveDatabaseSize(ctx context.Context) (int64, erro // implemented in version_control.go via versioncontrolops. // CommitPending commits all working set changes and reports whether a commit -// actually landed. It compares HEAD before and after rather than inspecting -// Commit's error: as of GH#3886, Commit itself tolerates Dolt's "nothing to -// commit" response (matching the server store) and returns nil for it, so an -// error-based check here would report every clean-store call as "committed". +// actually landed. It gets that from commitAll's returned bool rather than +// inspecting Commit's error or reading HEAD before and after: as of GH#3886, +// Commit itself tolerates Dolt's "nothing to commit" response (matching the +// server store) and returns nil for it, so an error-based check here would +// report every clean-store call as "committed", and a HEAD-before/HEAD-after +// comparison would cost two extra engine opens on every call (this runs on +// every embedded pull/sync) and race against any concurrent HEAD movement. func (s *EmbeddedDoltStore) CommitPending(ctx context.Context, actor string) (bool, error) { - before, err := s.GetCurrentCommit(ctx) - if err != nil { - return false, fmt.Errorf("commit pending: get current commit: %w", err) - } msg := fmt.Sprintf("bd: commit pending changes by %s", actor) - if err := s.Commit(ctx, msg); err != nil { - return false, err - } - after, err := s.GetCurrentCommit(ctx) - if err != nil { - return false, fmt.Errorf("commit pending: get current commit: %w", err) - } - return before != after, nil + return s.commitAll(ctx, msg, true) } // CommitExists is implemented in version_control.go via versioncontrolops. diff --git a/internal/storage/embeddeddolt/version_control.go b/internal/storage/embeddeddolt/version_control.go index 086f4ba9a..6d906223d 100644 --- a/internal/storage/embeddeddolt/version_control.go +++ b/internal/storage/embeddeddolt/version_control.go @@ -98,6 +98,33 @@ func (s *EmbeddedDoltStore) withMutatingPinnedDBConn(ctx context.Context, fn fun return s.withPinnedDBConn(ctx, fn) } +// commitAll runs the single embedded commit statement, DOLT_COMMIT('-Am'), +// on one connection (via withConn) and reports whether a commit actually +// landed. When tolerateEmpty is true, Dolt's "nothing to commit" response is +// treated as a no-op (committed=false, err=nil) instead of an error — the +// GH#3886 parity behavior Commit relies on. When tolerateEmpty is false, that +// same response is returned as an error instead, which CommitMergeResolution +// relies on (see its doc comment). +// +// Callers that need to know whether a commit landed (CommitPending) get it +// from the returned bool instead of reading HEAD before and after: HEAD reads +// are extra engine opens and are subject to a HEAD-moved-between-reads race +// if anything else writes concurrently. +func (s *EmbeddedDoltStore) commitAll(ctx context.Context, message string, tolerateEmpty bool) (committed bool, err error) { + err = s.withConn(ctx, true, func(tx *sql.Tx) error { + if _, execErr := tx.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', ?)", message); execErr != nil { + if tolerateEmpty && issueops.IsNothingToCommitError(execErr) { + committed = false + return nil + } + return fmt.Errorf("dolt commit: %w", execErr) + } + committed = true + return nil + }) + return committed, err +} + // Commit stages and commits the full working set. A clean working set is not // an error here: the server store (DoltStore.Commit et al., via // isDoltNothingToCommit) has always tolerated Dolt's "nothing to commit" @@ -106,15 +133,8 @@ func (s *EmbeddedDoltStore) withMutatingPinnedDBConn(ctx context.Context, fn fun // CommitWithConfig (below) right after SetConfig on a pristine, otherwise-clean // store, died on it (GH#3886). Tolerating it here brings embedded to parity. func (s *EmbeddedDoltStore) Commit(ctx context.Context, message string) error { - return s.withConn(ctx, true, func(tx *sql.Tx) error { - if _, err := tx.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', ?)", message); err != nil { - if issueops.IsNothingToCommitError(err) { - return nil - } - return fmt.Errorf("dolt commit: %w", err) - } - return nil - }) + _, err := s.commitAll(ctx, message, true) + return err } // CommitWithConfig commits all working set changes including config. @@ -134,24 +154,19 @@ func (s *EmbeddedDoltStore) CommitWithConfig(ctx context.Context, message string // resolving the conflict dirtied nothing — DoltStore.CommitMergeResolution // handles exactly this by explicitly concluding the open merge instead of // treating the empty diff as a no-op (concludeOpenMerge, wy-36ilm F12; see -// versioncontrolops.mergeInProgress / GetMergeBlockers, whose doc comment notes -// this same gap already surfaces as "a raw dolt error from -// CommitMergeResolution" for embedded). Swallowing the error here without also -// concluding the merge would leave dolt_merge_status.is_merging true while -// reporting success, silently re-wedging the next pull/sync — worse than -// today's explicit failure. Whether embedded DOLT_COMMIT('-Am') already -// concludes an open merge on a clean working set (unlike the server-mode -// stored-procedure path, which requires the explicit conclude step) was not -// established here, so this keeps the pre-existing non-tolerant behavior -// rather than guess (GH#3886 scope: fix bootstrap's plain-Commit path, not -// merge conclusion semantics). +// versioncontrolops.GetMergeBlockers, which documents the same class of gap: +// merge state that a plain commit-error check cannot see). Swallowing the +// error here without also concluding the merge would leave +// dolt_merge_status.is_merging true while reporting success, silently +// re-wedging the next pull/sync — worse than today's explicit failure. +// Whether embedded DOLT_COMMIT('-Am') already concludes an open merge on a +// clean working set (unlike the server-mode stored-procedure path, which +// requires the explicit conclude step) was not established here, so this +// keeps the pre-existing non-tolerant behavior rather than guess (GH#3886 +// scope: fix bootstrap's plain-Commit path, not merge conclusion semantics). func (s *EmbeddedDoltStore) CommitMergeResolution(ctx context.Context, message string) error { - return s.withConn(ctx, true, func(tx *sql.Tx) error { - if _, err := tx.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', ?)", message); err != nil { - return fmt.Errorf("dolt commit: %w", err) - } - return nil - }) + _, err := s.commitAll(ctx, message, false) + return err } func (s *EmbeddedDoltStore) AddRemote(ctx context.Context, name, url string) error {