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

107 lines
4.1 KiB
Go

package dolt
import (
"context"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestCommitToleratesDirtyIgnoredWisp locks in that a dirty dolt_ignore'd table
// (a wisp) is invisible to the commit path: HasCommittablePending reports
// nothing to commit and Commit succeeds without staging or destroying it.
//
// A dirty wisp is the normal steady state — creating an ephemeral issue writes
// the wisps table and deliberately does not DOLT_COMMIT it (issues.go). Both
// commitWorkingSet and HasCommittablePending exclude such tables with a
// dolt_ignore anti-join; before that filter, commitWorkingSet fed every
// dolt_status row into a fail-hard DOLT_ADD loop, so an ordinary Commit could
// fail (or its behavior silently depended on Dolt's version-specific handling
// of DOLT_ADD on an ignored table — a no-op on 2.2.0). This test dirties a wisp
// and asserts Commit() succeeds, so a future Dolt that changes ignored-table
// staging cannot regress that silently.
//
// The assertion bites on this harness: setupTestStore's shared branch-per-test
// database materializes ignored tables at HEAD, so a dirty wisp really does
// surface in dolt_status (the same reason TestCreateIssueCommitsInitialRelational
// Data must exclude the dolt_ignore'd events table from its clean check). Without
// the anti-join, HasCommittablePending would therefore see the wisp and wrongly
// report committable work.
func TestCommitToleratesDirtyIgnoredWisp(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
// A durable create auto-commits, leaving a clean committed baseline and
// proving the store commits real work — so a later "nothing to commit" is
// the anti-join at work, not a dead store.
durable := &types.Issue{
ID: "dirty-wisp-durable",
Title: "Durable baseline",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
}
if err := store.CreateIssue(ctx, durable, "tester"); err != nil {
t.Fatalf("CreateIssue durable: %v", err)
}
requireCleanTables(ctx, t, store, "issues")
// An ephemeral create writes the wisps table without committing it.
wisp := &types.Issue{
ID: "dirty-wisp-ephemeral",
Title: "Ephemeral wisp",
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
Ephemeral: true,
}
if err := store.CreateIssue(ctx, wisp, "tester"); err != nil {
t.Fatalf("CreateIssue wisp: %v", err)
}
// Precondition: the ignored wisps table is genuinely dirty, so the
// anti-join is actually exercised rather than passing vacuously. If a future
// Dolt stops surfacing ignored tables here, this fails loudly — the premise
// changed and the anti-join's coverage must be re-examined.
if !tableDirty(ctx, t, store, "wisps") {
t.Fatal("wisps not dirty after ephemeral create; anti-join would pass vacuously")
}
// The wisp is the only dirty table, and it is ignored, so the commit path
// must see nothing committable.
pending, err := store.HasCommittablePending(ctx)
if err != nil {
t.Fatalf("HasCommittablePending: %v", err)
}
if pending {
t.Fatal("HasCommittablePending = true with only a dirty ignored wisp; anti-join not applied")
}
// The reviewer's core ask: Commit tolerates the dirty ignored table.
if err := store.Commit(ctx, "commit over a dirty wisp"); err != nil {
t.Fatalf("Commit over a dirty ignored wisp: %v", err)
}
// Commit left the ignored table exactly as it was — neither staged into a
// commit nor discarded. The wisp is still dirty and still readable.
if !tableDirty(ctx, t, store, "wisps") {
t.Fatal("wisps no longer dirty after Commit; the ignored table was touched")
}
if _, err := store.GetIssue(ctx, wisp.ID); err != nil {
t.Fatalf("GetIssue wisp after Commit: %v", err)
}
}
// tableDirty reports whether a table shows uncommitted changes in dolt_status.
func tableDirty(ctx context.Context, t *testing.T, store *DoltStore, table string) bool {
t.Helper()
var n int
if err := store.db.QueryRowContext(ctx,
"SELECT COUNT(*) FROM dolt_status WHERE table_name = ?", table).Scan(&n); err != nil {
t.Fatalf("query dolt_status for %s: %v", table, err)
}
return n > 0
}