beads/internal/storage/dolt/version_commit_deferred_test.go
Steve Yegge 5eb7c25dd1
fix(dolt): honor --dolt-auto-commit batch/off in SQL-server mode (bd-4wamg) (#5389)
* fix(dolt): honor --dolt-auto-commit batch/off in SQL-server mode (bd-4wamg)

In server mode the auto-commit mode was declared but unenforced: the CLI's
policy hooks were all embedded-gated, and the storage layer minted one Dolt
commit inside every write transaction unconditionally — measured at one
commit per write through all five configuration paths (per-invocation flag,
config.yaml, env, bd config set, no-daemon). A constellation running ~482
versioned commits/h on a shared server could not use the documented
coalescing relief.

- issueOpsContext and transactHonoringAutoCommit are now mode-driven, not
  embedded-only: batch/off set the deferred-version-commit context / blank
  the commit message in server mode too.
- The storage layer's three per-write commit sites (doltAddAndCommitInTx,
  doltAddAndCommit, the deleter's inline site) honor the deferral and leave
  writes in the working set for an explicit commit point (bd dolt commit /
  CommitPending). Explicit commit APIs (Commit, CommitWithConfig,
  CommitPending) and the federation sync point are deliberately unguarded.
- The server-mode default flips off->on — a spelling change, not behavior:
  the old default resolved to 'off' while every value behaved like 'on'.
  With batch/off now real, 'on' is the compatible default. Help text stops
  claiming 'Default: off'.
- The embedded-only flush points (PersistentPostRun, create post-writes)
  keep their embedded gate via embeddedWritesCommitNow, now layered on the
  shared writesCommitNow.

On a shared server, staging is table-level: an on-mode writer's commit may
sweep up another client's deferred rows. Deferral bounds who creates
commits, not commit contents — acceptable for coalescing, documented in the
changelog.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(delete): thread batch/off auto-commit deferral through bd delete (bd-4wamg)

Fable adversarial review blocker: the deleter's VersionCommitDeferred
guard was dead code — delete.go passed a bare rootCtx on both the
single-ID and batch paths, so server-mode batch still minted one Dolt
commit per bd delete. Thread issueOpsContext exactly as update.go does.

Coverage: new TestDeferredVersionCommitDeletePath at the storage layer
(plain ctx advances history, deferred ctx leaves the delete in the
working set for CommitPending); batch-mode delete added to the embedded
container test; scope docs per review (proxied-server routes never
apply auto-commit policy — batch/off inert there, follow-up filed) and
the shutdown-flush help line qualified to live batch-mode processes.

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-07 00:45:03 -07:00

137 lines
5 KiB
Go

package dolt
import (
"testing"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
rootissueops "github.com/steveyegge/beads/issueops"
)
// bd-4wamg: a context carrying WithDeferredVersionCommit must leave writes in
// the Dolt working set — no per-write version commit — until an explicit
// commit point flushes them (CommitPending / bd dolt commit). This is the
// server-mode half of --dolt-auto-commit batch/off; the CLI sets the context
// in issueOpsContext, and this store honors it in doltAddAndCommitInTx.
func TestDeferredVersionCommitLeavesWritesForCommitPending(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
// Baseline: a plain create advances history.
before := doltCommitCount(ctx, t, store)
seedIssues(ctx, t, store, "test-defer-baseline")
if after := doltCommitCount(ctx, t, store); after <= before {
t.Fatalf("plain create: commit count %d -> %d, want an advance", before, after)
}
// Deferred: the same write must not advance history.
deferredCtx := issueops.WithDeferredVersionCommit(ctx)
before = doltCommitCount(ctx, t, store)
if err := store.CreateIssues(deferredCtx, []*types.Issue{
{ID: "test-defer-pending", Title: "deferred write", Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask},
}, "tester"); err != nil {
t.Fatalf("deferred create: %v", err)
}
if after := doltCommitCount(ctx, t, store); after != before {
t.Fatalf("deferred create advanced history: commit count %d -> %d, want unchanged", before, after)
}
// The write is durable in SQL regardless of version-commit deferral.
got, err := store.GetIssue(ctx, "test-defer-pending")
if err != nil || got == nil {
t.Fatalf("deferred issue not readable back: %v", err)
}
// An explicit commit point flushes the pending working set as one commit.
committed, err := store.CommitPending(ctx, "tester")
if err != nil {
t.Fatalf("CommitPending: %v", err)
}
if !committed {
t.Fatal("CommitPending reported nothing to commit; deferred write missing from working set")
}
if after := doltCommitCount(ctx, t, store); after != before+1 {
t.Fatalf("CommitPending: commit count %d -> %d, want exactly +1", before, after)
}
}
// TestDeferredVersionCommitUpdatePath covers the update verb's commit site
// (RunInIssueLifecycleTransaction -> doltAddAndCommitInTx) under deferral.
func TestDeferredVersionCommitUpdatePath(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-defer-update")
deferredCtx := issueops.WithDeferredVersionCommit(ctx)
before := doltCommitCount(ctx, t, store)
if err := store.UpdateIssue(deferredCtx, "test-defer-update", map[string]interface{}{
"priority": 1,
}, "tester"); err != nil {
t.Fatalf("deferred update: %v", err)
}
if after := doltCommitCount(ctx, t, store); after != before {
t.Fatalf("deferred update advanced history: commit count %d -> %d, want unchanged", before, after)
}
committed, err := store.CommitPending(ctx, "tester")
if err != nil {
t.Fatalf("CommitPending: %v", err)
}
if !committed {
t.Fatal("CommitPending reported nothing to commit after deferred update")
}
}
// TestDeferredVersionCommitDeletePath covers the delete verb's commit site
// (deleter.Delete's in-tx DOLT_COMMIT) under deferral — the site the bd-4wamg
// review found unwired at the CLI layer (delete.go passed a bare rootCtx).
func TestDeferredVersionCommitDeletePath(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
seedIssues(ctx, t, store, "test-defer-del-a", "test-defer-del-b")
del, err := store.Deleter()
if err != nil {
t.Fatalf("Deleter: %v", err)
}
// Plain ctx: a forced delete advances history (per-write commit).
before := doltCommitCount(ctx, t, store)
if _, err := del.Delete(ctx, rootissueops.DeleteRequest{
Actor: "tester", IDs: []string{"test-defer-del-a"}, Force: true,
}); err != nil {
t.Fatalf("plain delete: %v", err)
}
if after := doltCommitCount(ctx, t, store); after <= before {
t.Fatalf("plain delete: commit count %d -> %d, want an advance", before, after)
}
// Deferred ctx: the delete lands in SQL but must not advance history.
deferredCtx := issueops.WithDeferredVersionCommit(ctx)
before = doltCommitCount(ctx, t, store)
if _, err := del.Delete(deferredCtx, rootissueops.DeleteRequest{
Actor: "tester", IDs: []string{"test-defer-del-b"}, Force: true,
}); err != nil {
t.Fatalf("deferred delete: %v", err)
}
if after := doltCommitCount(ctx, t, store); after != before {
t.Fatalf("deferred delete advanced history: commit count %d -> %d, want unchanged", before, after)
}
if got, err := store.GetIssue(ctx, "test-defer-del-b"); err == nil && got != nil {
t.Fatal("deferred delete did not remove the row from SQL")
}
committed, err := store.CommitPending(ctx, "tester")
if err != nil {
t.Fatalf("CommitPending: %v", err)
}
if !committed {
t.Fatal("CommitPending reported nothing to commit after deferred delete")
}
}