beads/internal/storage/dolt/settlement_commit_boundary_test.go
Julian Knutsen af45955aa2
fix(dolt): preserve transaction outcome boundaries (#5341)
* fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fixup! fix(dolt): preserve transaction outcome boundary (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): retain indeterminate mixed claim updates (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): retain indeterminate guarded update errors (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): prevent lifecycle callback replay (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): classify publication commit ambiguity (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(embeddeddolt): mark commit response loss indeterminate (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): retain indeterminate claim outcomes (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): retry rollback-safe transaction setup (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): surface working-set staging failures (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): account direct commit publication failures (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(embeddeddolt): mark commit call response loss indeterminate (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): account indeterminate transaction commits (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): restore transaction circuit accounting (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(storage): preserve settlement commit boundaries (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): guard direct write circuit boundaries (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): close remaining circuit write gaps (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): account wisp dependency commits (ga-f7v2ft.79.1)

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(dolt): record circuit success only after claim verify; cut retry/patch complexity

Maintainer review fixups for PR #5341 (three majors):

- Correctness: verify-gated claim paths (ClaimReadyIssue, UnclaimIssue,
  UnclaimIssueIfAssignee, readyClaimer.ClaimNext, issueClaimer.Claim) now wrap
  verify+write in withCircuitWrite, so the circuit breaker records success only
  at the boundary after post-write verification returns nil — never on the bare
  SQL commit ahead of verification. Adds a regression test proving a
  committed-but-unverified claim does not reset the breaker.

- Maintainability: replace hasNonCoordinationPatch's 25-branch || chain with a
  straight-line nonCoordinationPatchSignals []bool enumeration (cyclomatic
  25 -> 3). Adds reflective exhaustiveness and count-guard tests so a new
  IssuePatch/LabelPatch/MetadataPatch field fails until it is classified.

- Readability: split withRetryClassified (cognitive 33) into classifyManagedRetry
  and recordRetryFailure (cognitive 3/3), preserving retry, indeterminate-commit,
  and breaker-trip behavior for both callers.

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

* fix(dolt): record circuit success after facade update verify; skip ignored tables in commit

Maintainer review fixup for PR #5341 (attempt 2).

Major: IssueLifecycle.Update ran its write on an unmanaged context, so
withCircuitWrite recorded RecordSuccess when the SQL commit landed --
before verifiedClaimWrite's re-read could contradict it, laundering a
phantom claim into breaker optimism. Wrap the verify path in
withCircuitWrite and invoke write with the circuit-managed context so
terminal success is recorded once at the boundary, only after verify
returns nil (matching issueClaimer.Claim). The ordinary patch path keeps
its original context and eager accounting.

Minor: commitWorkingSet fed every dolt_status row into a fail-hard
DOLT_ADD loop, so an ordinary Commit's success depended on Dolt's
version-specific handling of DOLT_ADD on a dolt_ignore'd table. Filter
ignored tables with the same anti-join HasCommittablePending uses.

Nit: drop the dead nested-error branch in verifiedClaimWrite (err is
already nil there).

Regressions: facade claim and guarded-update verify failures must not
reset the circuit breaker; a dirty ignored wisp is non-committable and
Commit tolerates it.

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

* fix(dolt/test): match commitWorkingSet dolt_ignore anti-join in sqlmock expectations

commitWorkingSet selects dirty tables with the dolt_ignore anti-join
(`SELECT s.table_name FROM dolt_status s WHERE NOT EXISTS (...)`) so ignored
wisp/lease tables stay out of fail-hard staging, but the draincall and
settlement sqlmock regression tests still expected the pre-anti-join
`SELECT table_name FROM dolt_status`. Under QueryMatcherRegexp the status query
no longer matched, so the commit path returned "could not match actual sql"
before reaching the indeterminate-commit / staging-cause / circuit-trip
assertions, failing the macOS Test job.

Update the six status-query expectations to the anti-join form. Behavioral
assertions are unchanged; this only realigns the mock's expected SQL with the
production query the review validated.

---------

Co-authored-by: CI Bot <ci@beads.test>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-06 04:34:46 -07:00

191 lines
7.5 KiB
Go

package dolt
import (
"errors"
"regexp"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/steveyegge/beads/internal/storage"
)
func TestServerSettlementSQLCommitResponseLossIsIndeterminate(t *testing.T) {
t.Run("pull merge settlement", func(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
mock.ExpectBegin()
tx, err := db.BeginTx(t.Context(), nil)
if err != nil {
t.Fatalf("BeginTx: %v", err)
}
mock.ExpectQuery(regexp.QuoteMeta("SELECT `table`, num_conflicts FROM dolt_conflicts")).
WillReturnRows(sqlmock.NewRows([]string{"table", "num_conflicts"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT `table`, num_conflicts FROM dolt_conflicts")).
WillReturnRows(sqlmock.NewRows([]string{"table", "num_conflicts"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0")).
WillReturnRows(sqlmock.NewRows([]string{"table"}))
responseLoss := errors.New("pull settlement commit response lost")
mock.ExpectCommit().WillReturnError(responseLoss)
store := &DoltStore{db: db}
err = store.settleMergeInTx(t.Context(), tx, nil)
assertPublicCommitIndeterminate(t, err, responseLoss)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
t.Run("full blocked recompute", func(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
mock.ExpectBegin()
mock.ExpectQuery(regexp.QuoteMeta("SELECT DISTINCT table_name FROM dolt_status WHERE table_name IN (?,?)")).
WithArgs("issues", "dependencies").
WillReturnRows(sqlmock.NewRows([]string{"table_name"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM issues")).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM wisps")).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
responseLoss := errors.New("full recompute commit response lost")
mock.ExpectCommit().WillReturnError(responseLoss)
store := &DoltStore{db: db}
changed, err := store.recomputeAllBlockedWithDB(t.Context(), db)
if changed != 0 {
t.Fatalf("recomputeAllBlockedWithDB() changed = %d, want 0", changed)
}
assertPublicCommitIndeterminate(t, err, responseLoss)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
t.Run("scoped blocked recompute", func(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
mock.ExpectBegin()
mock.ExpectQuery(regexp.QuoteMeta("SELECT value FROM metadata WHERE `key` = ?")).
WithArgs("is_blocked_recompute_pending").
WillReturnRows(sqlmock.NewRows([]string{"value"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM issues")).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
mock.ExpectQuery(regexp.QuoteMeta("SELECT id FROM wisps")).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
responseLoss := errors.New("scoped recompute commit response lost")
mock.ExpectCommit().WillReturnError(responseLoss)
store := &DoltStore{db: db}
err = store.recomputeBlockedTxWithDB(t.Context(), db, "")
assertPublicCommitIndeterminate(t, err, responseLoss)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
t.Run("CLI conflict repair", func(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
mock.ExpectExec(regexp.QuoteMeta("SET @@dolt_allow_commit_conflicts = 1")).
WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectBegin()
mock.ExpectQuery(regexp.QuoteMeta("SELECT `table`, num_conflicts FROM dolt_conflicts")).
WillReturnRows(sqlmock.NewRows([]string{"table", "num_conflicts"}).AddRow("metadata", 1))
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CONFLICTS_RESOLVE('--theirs', 'metadata')")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_ADD('metadata')")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(regexp.QuoteMeta("SELECT `table` FROM dolt_constraint_violations WHERE num_violations > 0")).
WillReturnRows(sqlmock.NewRows([]string{"table"}))
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', 'auto-resolve merge conflicts: metadata, dependencies, schema_migrations, config, issues (field-level three-way merge), labels/comments/events (union)')")).
WillReturnResult(sqlmock.NewResult(0, 1))
responseLoss := errors.New("CLI repair SQL commit response lost")
mock.ExpectCommit().WillReturnError(responseLoss)
mock.ExpectExec(regexp.QuoteMeta("SET @@dolt_allow_commit_conflicts = 0")).
WillReturnResult(sqlmock.NewResult(0, 0))
store := &DoltStore{db: db}
resolved, err := store.autoResolveConflictsAfterCLIPull(t.Context())
if resolved {
t.Fatal("autoResolveConflictsAfterCLIPull() resolved = true after lost SQL commit response")
}
assertPublicCommitIndeterminate(t, err, responseLoss)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
}
func TestCommitWorkingSetAfterSQLCommitResponseLossIsIndeterminate(t *testing.T) {
for _, tc := range []struct {
name string
setup func(sqlmock.Sqlmock, error)
}{
{
name: "DOLT_ADD",
setup: func(mock sqlmock.Sqlmock, responseLoss error) {
mock.ExpectQuery(regexp.QuoteMeta("SELECT s.table_name FROM dolt_status")).
WillReturnRows(sqlmock.NewRows([]string{"table_name"}).AddRow("issues"))
mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD(?)")).
WithArgs("issues").
WillReturnError(responseLoss)
},
},
{
name: "DOLT_COMMIT",
setup: func(mock sqlmock.Sqlmock, responseLoss error) {
mock.ExpectQuery(regexp.QuoteMeta("SELECT s.table_name FROM dolt_status")).
WillReturnRows(sqlmock.NewRows([]string{"table_name"}).AddRow("issues"))
mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_ADD(?)")).
WithArgs("issues").
WillReturnRows(sqlmock.NewRows([]string{"status"}))
mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_COMMIT('-m', ?, '--author', ?)")).
WithArgs("bd: recompute is_blocked after pull", " <>").
WillReturnError(responseLoss)
},
},
} {
t.Run(tc.name, func(t *testing.T) {
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherRegexp))
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
responseLoss := errors.New(tc.name + " response lost after SQL commit")
tc.setup(mock, responseLoss)
store := &DoltStore{db: db}
err = store.commitWorkingSetAfterSQLCommit(t.Context(), "bd: recompute is_blocked after pull", configExclude)
assertPublicCommitIndeterminate(t, err, responseLoss)
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet sqlmock expectations: %v", err)
}
})
}
}
func assertPublicCommitIndeterminate(t *testing.T, err, cause error) {
t.Helper()
if !errors.Is(err, cause) {
t.Fatalf("error = %v, want cause %v", err, cause)
}
if !errors.Is(err, storage.ErrCommitIndeterminate) {
t.Fatalf("error = %v, want storage.ErrCommitIndeterminate", err)
}
}