beads/internal/storage/dolt/errors_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

330 lines
10 KiB
Go

package dolt
import (
"database/sql"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
mysql "github.com/go-sql-driver/mysql"
"github.com/steveyegge/beads/internal/storage"
)
func TestIsIndeterminateCommitResponse(t *testing.T) {
for _, tc := range []struct {
name string
err error
want bool
}{
{name: "unexpected EOF", err: io.ErrUnexpectedEOF, want: true},
{name: "lost connection", err: errors.New("lost connection to MySQL server"), want: true},
{name: "packet protocol desync", err: mysql.ErrPktSync, want: true},
{name: "otherwise unproven untyped commit error", err: errors.New("commit rejected without typed response"), want: true},
{name: "typed semantic MySQL error", err: &mysql.MySQLError{Number: 1105, Message: "connection lost while validating commit"}, want: false},
{name: "typed rollback-guaranteed MySQL error", err: &mysql.MySQLError{Number: 1213, Message: "deadlock"}, want: false},
} {
t.Run(tc.name, func(t *testing.T) {
if got := isIndeterminateCommitResponse(tc.err); got != tc.want {
t.Errorf("isIndeterminateCommitResponse(%v) = %v, want %v", tc.err, got, tc.want)
}
})
}
}
func TestWrapSQLCommitError(t *testing.T) {
if err := wrapSQLCommitError("commit wisp", nil); err != nil {
t.Fatalf("wrapSQLCommitError(nil) = %v, want nil", err)
}
}
func TestWrapDBError(t *testing.T) {
t.Run("nil error returns nil", func(t *testing.T) {
if err := wrapDBError("op", nil); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("sql.ErrNoRows converts to storage.ErrNotFound", func(t *testing.T) {
err := wrapDBError("get issue", sql.ErrNoRows)
if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("expected ErrNotFound, got %v", err)
}
if err.Error() != "get issue: not found" {
t.Errorf("unexpected message: %s", err.Error())
}
})
t.Run("other errors are wrapped with context", func(t *testing.T) {
original := fmt.Errorf("connection refused")
err := wrapDBError("query users", original)
if !errors.Is(err, original) {
t.Errorf("expected to wrap original error")
}
if err.Error() != "query users: connection refused" {
t.Errorf("unexpected message: %s", err.Error())
}
})
}
func TestWrapTransactionError(t *testing.T) {
t.Run("nil error returns nil", func(t *testing.T) {
if err := wrapTransactionError("begin", nil); err != nil {
t.Errorf("expected nil, got %v", err)
}
})
t.Run("wraps with ErrTransaction sentinel", func(t *testing.T) {
original := fmt.Errorf("connection reset")
err := wrapTransactionError("begin tx", original)
if !errors.Is(err, ErrTransaction) {
t.Errorf("expected ErrTransaction in chain")
}
if !errors.Is(err, original) {
t.Errorf("expected original error in chain")
}
})
}
func TestWrapScanError(t *testing.T) {
t.Run("wraps with ErrScan sentinel", func(t *testing.T) {
original := fmt.Errorf("invalid column type")
err := wrapScanError("scan issue", original)
if !errors.Is(err, ErrScan) {
t.Errorf("expected ErrScan in chain")
}
if !errors.Is(err, original) {
t.Errorf("expected original error in chain")
}
})
}
func TestWrapQueryError(t *testing.T) {
t.Run("wraps with ErrQuery sentinel", func(t *testing.T) {
original := fmt.Errorf("syntax error")
err := wrapQueryError("search issues", original)
if !errors.Is(err, ErrQuery) {
t.Errorf("expected ErrQuery in chain")
}
})
}
func TestWrapExecError(t *testing.T) {
t.Run("wraps with ErrExec sentinel", func(t *testing.T) {
original := fmt.Errorf("duplicate key")
err := wrapExecError("insert issue", original)
if !errors.Is(err, ErrExec) {
t.Errorf("expected ErrExec in chain")
}
})
}
func TestDatabaseNotFoundHint(t *testing.T) {
baseCfg := Config{
Database: "beads_test",
ServerHost: "127.0.0.1",
ServerPort: 3309,
}
t.Run("hint suggests setting sync.remote when empty", func(t *testing.T) {
cfg := baseCfg // SyncRemote is empty by default
err := databaseNotFoundError(&cfg)
msg := err.Error()
// FR-001: Must contain the setup hint (line-wrapped in output)
if !strings.Contains(msg, "set sync.remote") {
t.Errorf("expected hint to set sync.remote, got:\n%s", msg)
}
if !strings.Contains(msg, ".beads/config.yaml") {
t.Errorf("expected .beads/config.yaml reference, got:\n%s", msg)
}
// Must still contain the original error context
if !strings.Contains(msg, `"beads_test"`) {
t.Errorf("expected database name in error, got:\n%s", msg)
}
if !strings.Contains(msg, "127.0.0.1:3309") {
t.Errorf("expected server address in error, got:\n%s", msg)
}
// Must contain recovery suggestions
if !strings.Contains(msg, "bd bootstrap") {
t.Errorf("expected bd bootstrap suggestion, got:\n%s", msg)
}
if !strings.Contains(msg, "bd doctor") {
t.Errorf("expected bd doctor suggestion, got:\n%s", msg)
}
if strings.Contains(msg, "re-run bd init") {
t.Errorf("did not expect init-first recovery guidance, got:\n%s", msg)
}
})
t.Run("hint mentions configured sync.remote when set", func(t *testing.T) {
cfg := baseCfg
cfg.SyncRemote = "https://doltremoteapi.dolthub.com/myorg/beads"
err := databaseNotFoundError(&cfg)
msg := err.Error()
// FR-002: Must mention it's configured and show the URL
if !strings.Contains(msg, "sync.remote is configured") {
t.Errorf("expected configured hint, got:\n%s", msg)
}
if !strings.Contains(msg, "https://doltremoteapi.dolthub.com/myorg/beads") {
t.Errorf("expected remote URL in hint, got:\n%s", msg)
}
if !strings.Contains(msg, "bd bootstrap") {
t.Errorf("expected bd bootstrap suggestion, got:\n%s", msg)
}
})
t.Run("hint detects backup files in beads dir (GH#2327)", func(t *testing.T) {
// Create a temp .beads/backup/ with a JSONL file
tmpDir := t.TempDir()
backupDir := filepath.Join(tmpDir, "backup")
if err := os.MkdirAll(backupDir, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(backupDir, "issues.jsonl"), []byte(`{"id":"x"}`), 0600); err != nil {
t.Fatal(err)
}
cfg := baseCfg
cfg.BeadsDir = tmpDir
err := databaseNotFoundError(&cfg)
msg := err.Error()
if !strings.Contains(msg, "Backup files found") {
t.Errorf("expected backup detection hint, got:\n%s", msg)
}
if !strings.Contains(msg, "bd backup restore") {
t.Errorf("expected bd backup restore suggestion, got:\n%s", msg)
}
if !strings.Contains(msg, "bd bootstrap") {
t.Errorf("expected bd bootstrap suggestion, got:\n%s", msg)
}
// Should still mention branch switching as a common cause
if !strings.Contains(msg, "branch") {
t.Errorf("expected branch-switch mention, got:\n%s", msg)
}
})
t.Run("no backup hint when no backup files exist", func(t *testing.T) {
tmpDir := t.TempDir()
cfg := baseCfg
cfg.BeadsDir = tmpDir
err := databaseNotFoundError(&cfg)
msg := err.Error()
if strings.Contains(msg, "Backup files found") {
t.Errorf("should not mention backups when none exist, got:\n%s", msg)
}
})
}
func TestHasBackupFiles(t *testing.T) {
t.Run("returns false for empty beadsDir", func(t *testing.T) {
if HasBackupFiles("") {
t.Error("expected false for empty beadsDir")
}
})
t.Run("returns false when backup dir does not exist", func(t *testing.T) {
if HasBackupFiles(t.TempDir()) {
t.Error("expected false when backup dir missing")
}
})
t.Run("returns false when backup dir is empty", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(tmpDir, "backup"), 0700); err != nil {
t.Fatal(err)
}
if HasBackupFiles(tmpDir) {
t.Error("expected false when backup dir is empty")
}
})
t.Run("returns false when backup dir has non-jsonl files", func(t *testing.T) {
tmpDir := t.TempDir()
backupDir := filepath.Join(tmpDir, "backup")
if err := os.MkdirAll(backupDir, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(backupDir, "state.json"), []byte("{}"), 0600); err != nil {
t.Fatal(err)
}
if HasBackupFiles(tmpDir) {
t.Error("expected false when only non-jsonl files present")
}
})
t.Run("returns true when backup dir has jsonl files", func(t *testing.T) {
tmpDir := t.TempDir()
backupDir := filepath.Join(tmpDir, "backup")
if err := os.MkdirAll(backupDir, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(backupDir, "issues.jsonl"), []byte(`{"id":"x"}`), 0600); err != nil {
t.Fatal(err)
}
if !HasBackupFiles(tmpDir) {
t.Error("expected true when jsonl files present")
}
})
}
func TestIsBranchTrackingError(t *testing.T) {
t.Parallel()
t.Run("matches dolt branch tracking error", func(t *testing.T) {
err := fmt.Errorf("Error 1105: You asked to pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch.")
if !isBranchTrackingError(err) {
t.Error("expected true for branch tracking error")
}
})
t.Run("does not match unrelated errors", func(t *testing.T) {
if isBranchTrackingError(fmt.Errorf("connection refused")) {
t.Error("expected false for connection error")
}
})
t.Run("nil error returns false", func(t *testing.T) {
if isBranchTrackingError(nil) {
t.Error("expected false for nil error")
}
})
}
func TestIsDoltAutocommitRollbackError(t *testing.T) {
exact := &mysql.MySQLError{
Number: 1105,
Message: "Merge conflict detected, @autocommit transaction rolled back",
}
for _, tc := range []struct {
name string
err error
want bool
}{
{name: "exact typed Dolt rollback", err: fmt.Errorf("dolt commit: %w", exact), want: true},
{name: "typed Dolt rollback with recovery guidance", err: &mysql.MySQLError{Number: 1105, Message: exact.Message + ". @autocommit must be disabled"}, want: true},
{name: "untyped matching text", err: errors.New(exact.Error()), want: false},
{name: "other typed 1105", err: &mysql.MySQLError{Number: 1105, Message: "Merge conflict detected"}, want: false},
{name: "typed connection-like 1105", err: &mysql.MySQLError{Number: 1105, Message: "connection lost while validating commit"}, want: false},
{name: "same text wrong code", err: &mysql.MySQLError{Number: 1213, Message: exact.Message}, want: false},
} {
t.Run(tc.name, func(t *testing.T) {
if got := isDoltAutocommitRollbackError(tc.err); got != tc.want {
t.Errorf("isDoltAutocommitRollbackError(%v) = %v, want %v", tc.err, got, tc.want)
}
})
}
}