fix: honor dolt.auto-commit in proxied server mode (#6499)
* fix(dolt): honor dolt.auto-commit=batch/off on the proxied-server write path (#4995)
In proxied-server mode every write minted a Dolt history commit whatever
dolt.auto-commit said. Two causes:
- cmd/bd/main.go never applied issueOpsContext(rootCtx) in the
proxiedServerMode branch of the root pre-run, so the context carried no
deferred-commit marker.
- internal/storage/uow/doltserver_tx.go: doltServerTx.Commit ignored
issueops.VersionCommitDeferred(ctx) and ran CALL DOLT_COMMIT('-Am', ?)
whenever a message was supplied.
Apply issueOpsContext to rootCtx in the proxied setup, and have
doltServerTx.Commit blank its message under a deferred context so the existing
plain-COMMIT branch persists the write to the working set without advancing
Dolt history. dolt.auto-commit=on is unchanged.
This commit alone strands batch/off writes in the working set; the explicit
flush point (`bd dolt commit`) follows in the later commits of this series.
(Originally recorded as "witness: salvage uncommitted work (ocb-ani)"; the
change itself is unmodified.)
* test(uow,cmd/bd): add regression tests for proxied dolt.auto-commit batch/off (#4995)
Add focused regression tests verifying that proxied dolt.auto-commit batch/off
defer Dolt version commits:
- cmd/bd: TestIssueOpsContextDefersByModeInProxiedServerMode verifies that
issueOpsContext defers version commits in proxied-server mode when
dolt.auto-commit is batch or off.
- internal/storage/uow: TestDoltServerTxCommitDefersDoltCommitUnderDeferredContext
and TestDoltServerTxRunTxWithDeferredContextSkipsDoltCommit verify that
doltServerTx.Commit blanks the commit message under a deferred context,
bypassing DOLT_COMMIT and status checks, and executing plain COMMIT to
persist writes into the working set without advancing Dolt history.
* fix(dolt): add the proxied flush point dolt.auto-commit=batch/off defers to (#4995)
The deferral half of this PR makes dolt.auto-commit=batch/off actually take
effect on the proxied-server write path. On its own that turns batch/off into
"never commit": proxied mode returns from the root pre-run before newDoltStore
runs, so getStore() is nil and `bd dolt commit` has no store to commit
through. Writes pile up in the sql-server working set where `dolt log`,
`dolt push` and backups never see them.
Add the missing half. cmd/bd/dolt_commit_proxied_server.go is the proxied dual
of `bd dolt commit`: it takes the uow.MaintenanceProvider route (RunNonTx) and
runs CALL DOLT_COMMIT('-Am', ?) on a pinned connection, gated on
issueops.HasPendingChanges. It deliberately does not go through
doltServerTx.Commit, which blanks its own message under the deferred context;
routing the flush through the deferral it exists to discharge would make the
flush a no-op. doltCommitCmd dispatches to it on usesProxiedServer(); the
message is built before the branch so both routes name the same default, and
the "Committed." / "Nothing to commit." contract is unchanged.
--dolt-auto-commit's help no longer claims proxied-server routes are
unaffected: it names the proxied flush point and keeps the SIGTERM/SIGHUP
flush marked embedded-only. docs/reference/configuration.md says the same.
`bd vc commit`'s proxied refusal now points at `bd dolt commit`.
Tests:
- cmd/bd/dolt_commit_proxied_integration_test.go: three batch writes leave
dolt_log unchanged and stay readable, `bd dolt commit` advances it by
exactly one, a second `bd dolt commit` reports "Nothing to commit." with
dolt_log unchanged, and one write under --dolt-auto-commit on advances it
by one. On the PR's original base it failed at the `bd dolt commit` step
with "no store available". On current main the front door refuses the
command before that code is reached, so this test is only green together
with the next commit, which moves `dolt commit` to supported in the proxy
capability registry.
- TestIssueOpsContextDefersByModeInProxiedServerMode renamed to
TestIssueOpsContextDefersByModePolicyTable and the proxiedServerMode
assignment dropped: issueOpsContext branches only on writesCommitNow(),
never on the storage mode, so the old name over-promised.
- doltserver_tx_test.go: "bd: update bd-4995" -> "bd: update GH#4995", which
no longer reads as a bead id.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(proxy): move `dolt commit` from refused to supported in the capability registry
Main's proxy capability registry (added by #6269, 24a9da6cc) declares
`dolt commit` direct-only: it has a "proxy.dolt_commit.unsupported" refusal
in proxyMaintenanceRefusals (cmd/bd/proxy_capability.go) and is classed
HistoryDirectOnly in historyCapabilityMatrix (cmd/bd/history_capability.go).
The front door therefore refuses `bd dolt commit` in proxied-server mode
before dispatch, so the proxied dual added earlier in this series is
unreachable, and dolt.auto-commit=batch/off has no way to flush.
the history/VC front-door contract, and neither the registry, its tests nor
the docs explain why `dolt commit` was listed. Nothing on main implemented
`dolt commit` for the proxied route at the time, which is consistent with the
entry being a statement of what existed rather than a design decision.
This flips a capability main had declared unsupported, deliberately: the
proxied route now has an implementation (runDoltCommitProxiedServer), and
batch/off need it as their flush point (GH#4995).
Declared the same way as the other proxied-supported command,
`dolt remote remove`: HistoryProxySupported in the history matrix, and no row
in proxyMaintenanceRefusals.
Tests follow: history_capability_test.go moves `dolt commit` from the
direct-only assertions and refusal contract to the proxy-supported ones
(including the nested front-door pass-through), and
history_remote_frontdoor_matrix_test.go drops the refusal row and adds a
`dolt commit` pass-through check to TestHistoryRemoteSupportedFrontDoorParity.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(proxy): exempt explicit commit points from the proxied deferral (#4995)
Maintainer review fixup for PR #6499, adopt-pr iterations 1-2.
Major (Contract & Interface Fidelity). The deferral is applied once, to
rootCtx, so it blanketed BOTH commit classes the direct route distinguishes by
which wrapper a verb picks: policy-honoring writes (transactHonoringAutoCommit,
per-verb issueOpsContext) and explicit commit points (transact, which passes
the caller's message whatever the policy says). Under dolt.auto-commit=batch a
proxied `bd batch -m "release batch"` therefore persisted the rows, discarded
the message and minted nothing, while the identical command and config on the
direct route committed it — and batch_proxied_server.go's own contract comment
rejects exactly that ("would persist these writes while silently bypassing
Dolt history"), as does batch.go's "the contract is the same on both backends".
issueops.WithImmediateVersionCommit clears the marker and cmd/bd's
explicitCommitPointContext applies it in the duals whose direct twin calls
transact. Scope is the intersection of the transact call sites with
proxyPermittedPaths: batch, mol bond/pour/squash, mol wisp create, and the
WISP half of mol burn (cook and migrate issues also call transact but are
refused in proxied mode, so they have no dual to exempt). The review sized the
radius at batch + 4 mol verbs; mol wisp create is the same class — its direct
twin reaches cloneSubgraph's transact through spawnMoleculeWithOptions — so it
is exempted with them.
Membership is per commit site, not per command (iteration 2). `bd mol burn` is
two classes on the direct route: wisps go through burnWisps -> transact, while
persistent molecules go through deleteBatch -> issueOpsContext ->
dolt/deleter.go, which returns before DOLT_COMMIT when the context defers.
Exempting the whole command minted one Dolt commit per persistent molecule
under batch/off — on a destructive verb, where the identical direct-route
command defers them all to `bd dolt commit`. runMolBurnProxiedServer now
applies the exemption to the wisp RunTxResult only, and the flag help,
configuration.md and the helper's own radius comment say so.
Minors:
- The flush passes '--author' like DoltStorage.CommitAll, so the one commit
batch mode keeps is attributed to the bd actor rather than the SQL session
user (without it dolt_log reads `root <root@localhost>`).
- runDoltCommitProxiedServer calls the shared runProxiedNonTx instead of
re-implementing its provider checks, so `bd --json dolt commit` keeps the
JSON error envelope; `dolt commit`'s RunE no longer re-wraps an
already-reported failure, and runProxiedNonTx's message gains the provider
%T the open-coded version had.
- The flag help and configuration.md drop two false claims: that the deferral
covers "every write" in proxied mode (the bd serve plane over the same
provider never touches rootCtx, so daemon writes still commit per write) and
that the SIGTERM/SIGHUP flush is "embedded-only" (flushBatchCommitOnShutdown
gates on an open store, which direct SQL-server mode has; only proxied mode
lacks one). Both surfaces now name the explicit-commit-point exemption.
- Both user-facing enumerations of that exemption are now derived from the
explicitCommitPointContext call sites rather than restated (iteration 2):
they had omitted mol wisp create, listed mol burn wholesale, and implied a
message-less `bd batch` defers when batch.go synthesizes a default message
and the dual exempts unconditionally, so every proxied batch commits.
- doltserver_tx.go's ephemeral-form comment names its second entry point —
RunTx/RunTxResult under a deferred context — where its
ONLY-dolt_ignored-state rationale stops applying.
Not done: regenerating docs/CLI_REFERENCE.md. Its --dolt-auto-commit drift is
base-preexisting, and the generated CLI docs are built from the release tag
pinned in docs/cli-docs.pin, not from this source, so touching them converts
the drift gate's inherited-drift warn-pass into a failure.
Tests. The uow blanking table gains the immediate-commit arm (tagless, so it
runs in every lane); the proxied integration test gains an explicit-batch-commit
step with a deferred plain create as its control, plus the flush attribution
assertions. Mutation-proved: reverting explicitCommitPointContext to a no-op
reds the new step (dolt_log 80 -> 80, no commit minted), and dropping
'--author' reds the attribution assertion with committer=root.
Iteration 2 adds the WIRING pin those left open — the mechanism was covered,
but deleting `ctx = explicitCommitPointContext(ctx)` from a dual was green in
every lane, which is why the mol burn over-application above was invisible.
dolt_autocommit_exemption_wiring_test.go is tagless and needs no server: a
uow.UnitOfWorkProvider that records the context RunTxResult hands NewUOW and
then refuses observes exactly the context each dual gives its transaction.
Mutation-proved per arm: neutering the exemption at batch/bond/squash reds
those three rows and only those; dropping it from the wisp RunTxResult reds
the wisp arm alone; and restoring the command-wide reassignment reds the
persistent arm alone, reproducing the defect this iteration fixes.
Validation: go build ./..., go vet (cgo and CGO_ENABLED=0), gofmt clean;
internal/storage/issueops; internal/storage/uow -run TestDoltServerTx; the
cmd/bd registry / history / policy / batch / mol / wisp / errors battery; and
the gated real-server lane BEADS_TEST_PROXIED_SERVER=1 green for both
TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce (41s) and
TestProxiedServerMolBurn (33s, all four subtests including the mixed
wisp+persistent burn this iteration changed).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
This commit is contained in:
parent
73702cc733
commit
edb86e3470
21 changed files with 873 additions and 40 deletions
|
|
@ -38,6 +38,9 @@ func runBatchProxiedServer(ctx context.Context, ops []batchOp, commitMsg string)
|
|||
if uowProvider == nil {
|
||||
return nil, HandleErrorRespectJSON("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Explicit commit point, like `transact` on the direct route: the commit
|
||||
// carries the caller's message whatever dolt.auto-commit says (GH#4995).
|
||||
ctx = explicitCommitPointContext(ctx)
|
||||
return uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) ([]batchOpResult, string, error) {
|
||||
// Built per attempt, not per call: a retried batch must report the
|
||||
// results of the attempt that actually committed.
|
||||
|
|
|
|||
|
|
@ -324,12 +324,13 @@ var proxyCapabilityRegistry = []capabilityRow{
|
|||
refusedPath("repo sync", "proxy.repo.unsupported", ProxyReasonDesign, ""),
|
||||
|
||||
// --- dolt version-control verbs -----------------------------------------
|
||||
// CALL DOLT_PUSH/DOLT_PULL/DOLT_COMMIT are SQL procedures and the generic
|
||||
// repository that wraps them is already written; these need a use case and
|
||||
// a route, not proxy work. `dolt remote remove` is already routed, which is
|
||||
// the precedent.
|
||||
refusedPath("dolt commit", "proxy.dolt_commit.unsupported", ProxyReasonUnimplemented, trackVersionCtl).
|
||||
withHistory(HistoryDirectOnly),
|
||||
// CALL DOLT_PUSH/DOLT_PULL are SQL procedures and the generic repository
|
||||
// that wraps them is already written; these need a use case and a route,
|
||||
// not proxy work. `dolt remote remove` is already routed, which is the
|
||||
// precedent — and `dolt commit` has now followed it: GH#4995 supplied the
|
||||
// use case (it is the flush point dolt.auto-commit=batch/off defers to) and
|
||||
// runDoltCommitProxiedServer supplied the route, so its row moved to the
|
||||
// routed-history block below.
|
||||
refusedPath("dolt push", "proxy.dolt_push.unsupported", ProxyReasonUnimplemented, trackVersionCtl).
|
||||
withHistory(HistoryDirectOnly),
|
||||
refusedPath("dolt pull", "proxy.dolt_pull.unsupported", ProxyReasonUnimplemented, trackVersionCtl).
|
||||
|
|
@ -441,10 +442,14 @@ var proxyCapabilityRegistry = []capabilityRow{
|
|||
{Path: "duplicates", ArgSet: "--auto-merge --dry-run", Rule: proxyCapabilityRule{Outcome: ProxyOutcomeHonored}},
|
||||
|
||||
// --- routed history -----------------------------------------------------
|
||||
// The two paths that already have a proxied route, recorded so the class is
|
||||
// The paths that already have a proxied route, recorded so the class is
|
||||
// asserted rather than remembered.
|
||||
permitted("history").withHistory(HistoryProxySupported),
|
||||
permitted("dolt remote remove").withHistory(HistoryProxySupported),
|
||||
// `dolt commit` is routed by runDoltCommitProxiedServer. It is also the
|
||||
// flush point dolt.auto-commit=batch/off defers to (GH#4995), so refusing it
|
||||
// at the front door would leave those modes with no way to flush.
|
||||
permitted("dolt commit").withHistory(HistoryProxySupported),
|
||||
|
||||
// --- untyped refusals inside RunE ---------------------------------------
|
||||
// The gate permits these paths and the command refuses itself with a bare
|
||||
|
|
|
|||
|
|
@ -14,13 +14,17 @@ func TestHistoryCapabilityMatrixExactPaths(t *testing.T) {
|
|||
if got, ok := LookupHistoryCapability("history"); !ok || got != HistoryProxySupported {
|
||||
t.Fatalf("history = %q, %v; want proxy-supported", got, ok)
|
||||
}
|
||||
for _, path := range []string{"branch", "conflicts", "repo", "federation", "vc", "flatten", "dolt push", "dolt pull", "dolt commit", "dolt remote", "sync"} {
|
||||
for _, path := range []string{"branch", "conflicts", "repo", "federation", "vc", "flatten", "dolt push", "dolt pull", "dolt remote", "sync"} {
|
||||
if got, ok := LookupHistoryCapability(path); !ok || got != HistoryDirectOnly {
|
||||
t.Errorf("%q = %q, %v; want direct-only", path, got, ok)
|
||||
}
|
||||
}
|
||||
if got, ok := LookupHistoryCapability("dolt remote remove"); !ok || got != HistoryProxySupported {
|
||||
t.Fatalf("dolt remote remove = %q, %v; want proxy-supported", got, ok)
|
||||
// dolt commit is the flush point dolt.auto-commit=batch/off defers to
|
||||
// (GH#4995), so it must stay reachable on the proxied route.
|
||||
for _, path := range []string{"dolt remote remove", "dolt commit"} {
|
||||
if got, ok := LookupHistoryCapability(path); !ok || got != HistoryProxySupported {
|
||||
t.Fatalf("%s = %q, %v; want proxy-supported", path, got, ok)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"dolt remote add", "dolt remote list", "dolt remote reset-data"} {
|
||||
if got, ok := LookupHistoryCapability(path); !ok || got != HistoryDirectOnly {
|
||||
|
|
@ -52,7 +56,6 @@ func TestHistoryDirectOnlyRefusalContract(t *testing.T) {
|
|||
"flatten": {"proxy.flatten.unsupported", "flatten is not supported in proxied-server mode"},
|
||||
"dolt push": {"proxy.dolt_push.unsupported", "dolt push is not supported in proxied-server mode"},
|
||||
"dolt pull": {"proxy.dolt_pull.unsupported", "dolt pull is not supported in proxied-server mode"},
|
||||
"dolt commit": {"proxy.dolt_commit.unsupported", "dolt commit is not supported in proxied-server mode"},
|
||||
"dolt remote add": {"proxy.dolt_remote.unsupported", "dolt remote add is not supported in proxied-server mode"},
|
||||
"dolt remote list": {"proxy.dolt_remote.unsupported", "dolt remote list is not supported in proxied-server mode"},
|
||||
"dolt remote reset-data": {"proxy.dolt_remote.unsupported", "dolt remote reset-data is not supported in proxied-server mode"},
|
||||
|
|
@ -99,7 +102,7 @@ func TestHistoryNestedFrontDoorsRefuseAndSupportedPathsPass(t *testing.T) {
|
|||
"conflicts resolve": {"proxy.conflicts.unsupported", "conflicts resolve is not supported in proxied-server mode"},
|
||||
"federation sync": {"proxy.federation.unsupported", "federation sync is not supported in proxied-server mode"},
|
||||
}
|
||||
for _, path := range []string{"vc merge", "vc commit", "repo add", "conflicts resolve", "federation sync", "dolt remote remove"} {
|
||||
for _, path := range []string{"vc merge", "vc commit", "repo add", "conflicts resolve", "federation sync", "dolt remote remove", "dolt commit"} {
|
||||
parts := strings.Split(path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: parts[0]}
|
||||
|
|
@ -110,7 +113,7 @@ func TestHistoryNestedFrontDoorsRefuseAndSupportedPathsPass(t *testing.T) {
|
|||
cmd = child
|
||||
}
|
||||
err := validateProxyRegistryBeforeProvider(cmd, ProxyTopologyManagedLocal)
|
||||
if path == "dolt remote remove" {
|
||||
if path == "dolt remote remove" || path == "dolt commit" {
|
||||
if err != nil {
|
||||
t.Fatalf("supported %s refused: %v", path, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -728,26 +728,47 @@ For more options (--stdin, custom messages), see: bd vc commit`,
|
|||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
st := getStore()
|
||||
if st == nil {
|
||||
return HandleError("no store available")
|
||||
}
|
||||
msg, _ := cmd.Flags().GetString("message")
|
||||
if msg == "" {
|
||||
msg = fmt.Sprintf("bd: dolt commit (auto-commit) by %s", getActor())
|
||||
}
|
||||
// CommitAll, not Commit: this command's contract is "any uncommitted
|
||||
// changes in the working set", including changes made externally and
|
||||
// the config table — which server-mode Commit excludes (GH#2455), so
|
||||
// out-of-band config dirt used to survive this command forever. Its
|
||||
// committed bool also replaces the HEAD-before/HEAD-after comparison
|
||||
// this command used to detect tolerated no-ops with, which cost two
|
||||
// extra HEAD reads and raced against concurrent writers.
|
||||
committed, err := st.CommitAll(ctx, msg)
|
||||
|
||||
var (
|
||||
committed bool
|
||||
err error
|
||||
)
|
||||
if usesProxiedServer() {
|
||||
// Proxied mode never opens a store — the root pre-run returns
|
||||
// before newDoltStore — so getStore() is nil here and the UOW
|
||||
// provider is the only handle on the server. This is the flush
|
||||
// point dolt.auto-commit=batch/off defers to on that route
|
||||
// (GH#4995). The message is built above so both routes name the
|
||||
// same default.
|
||||
committed, err = runDoltCommitProxiedServer(ctx, msg)
|
||||
} else {
|
||||
st := getStore()
|
||||
if st == nil {
|
||||
return HandleError("no store available")
|
||||
}
|
||||
// CommitAll, not Commit: this command's contract is "any uncommitted
|
||||
// changes in the working set", including changes made externally and
|
||||
// the config table — which server-mode Commit excludes (GH#2455), so
|
||||
// out-of-band config dirt used to survive this command forever. Its
|
||||
// committed bool also replaces the HEAD-before/HEAD-after comparison
|
||||
// this command used to detect tolerated no-ops with, which cost two
|
||||
// extra HEAD reads and raced against concurrent writers.
|
||||
committed, err = st.CommitAll(ctx, msg)
|
||||
}
|
||||
if err != nil {
|
||||
if isDoltNothingToCommit(err) {
|
||||
switch {
|
||||
case isDoltNothingToCommit(err):
|
||||
committed = false
|
||||
} else {
|
||||
case isReportedExit(err):
|
||||
// The proxied route reports provider failures itself, through
|
||||
// HandleErrorRespectJSON, so the JSON envelope is already on
|
||||
// stdout; re-wrapping would print a second, plain Error line.
|
||||
return err
|
||||
default:
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,6 +91,35 @@ func issueOpsContext(ctx context.Context) (context.Context, error) {
|
|||
return issueops.WithDeferredVersionCommit(ctx), nil
|
||||
}
|
||||
|
||||
// explicitCommitPointContext clears the dolt.auto-commit deferral for a proxied
|
||||
// dual whose direct-route twin is an explicit commit point.
|
||||
//
|
||||
// The direct route encodes two commit classes, and it encodes them by which
|
||||
// wrapper a verb picks: writes whose Dolt commit is auto-commit policy go
|
||||
// through transactHonoringAutoCommit or issueOpsContext above, while the
|
||||
// explicit commit points go through transact and mint a Dolt commit carrying
|
||||
// the caller's message whatever the policy says. The proxied route has no such
|
||||
// choice to make per verb — it applies the policy ONCE, to rootCtx in the root
|
||||
// pre-run (GH#4995) — so the second class has to opt back out here, or
|
||||
// doltServerTx.Commit blanks the message it was given: a proxied
|
||||
// `bd batch -m "release batch"` under dolt.auto-commit=batch would persist the
|
||||
// rows, discard the message and mint nothing, while the identical command and
|
||||
// config on the direct route commits it.
|
||||
//
|
||||
// Scope is the intersection of two in-tree lists: the transact() call sites,
|
||||
// and the paths the proxied front door permits (proxyPermittedPaths in
|
||||
// capability_registry.go). That is batch, mol bond/pour/squash, mol wisp create
|
||||
// and the WISP half of mol burn; cook and migrate issues also call transact but
|
||||
// are refused in proxied mode, so they have no dual to exempt.
|
||||
//
|
||||
// Membership is per commit site, not per command: mol burn's persistent half
|
||||
// reaches deleteBatch -> issueOpsContext on the direct route, which is the
|
||||
// policy-honoring class, so runMolBurnProxiedServer exempts only the wisp
|
||||
// transaction. TestProxiedDualsExemptTheirExplicitCommitPoints pins both arms.
|
||||
func explicitCommitPointContext(ctx context.Context) context.Context {
|
||||
return issueops.WithImmediateVersionCommit(ctx)
|
||||
}
|
||||
|
||||
type doltAutoCommitParams struct {
|
||||
// Command is the top-level bd command name (e.g., "create", "update").
|
||||
Command string
|
||||
|
|
|
|||
238
cmd/bd/dolt_autocommit_exemption_wiring_test.go
Normal file
238
cmd/bd/dolt_autocommit_exemption_wiring_test.go
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// The tests below pin the WIRING half of the GH#4995 exemption. The mechanism
|
||||
// half — that WithImmediateVersionCommit defeats the route-wide deferral —
|
||||
// is pinned in internal/storage/uow; what is pinned here is that each proxied
|
||||
// dual whose direct twin is an explicit commit point actually hands that
|
||||
// exemption to its transaction, and that the duals whose twin is NOT one do
|
||||
// not. Without this, deleting explicitCommitPointContext from a dual is green
|
||||
// in every lane that does not have a real proxied server.
|
||||
//
|
||||
// The seam is uow.RunTxResult's own first act: it asks the provider for a unit
|
||||
// of work, passing the caller's context. A provider that records that context
|
||||
// and then refuses observes exactly "the context this dual hands its
|
||||
// transaction", with no server, no store and no fixture beyond the reads the
|
||||
// dual makes to decide what to write.
|
||||
|
||||
var errStopBeforeProbeTx = errors.New("exemption wiring probe: stop before the transaction")
|
||||
|
||||
// exemptionProbeProvider serves readUOW to the first `reads` NewUOW calls (the
|
||||
// dual's own read phase) and refuses the rest, recording every context it was
|
||||
// handed. The refusal is not a serialization error, so RunTxResult treats it as
|
||||
// permanent and makes exactly one NewUOW call per transaction.
|
||||
type exemptionProbeProvider struct {
|
||||
readUOW uow.UnitOfWork
|
||||
reads int
|
||||
ctxs []context.Context
|
||||
}
|
||||
|
||||
func (p *exemptionProbeProvider) NewUOW(ctx context.Context) (uow.UnitOfWork, error) {
|
||||
p.ctxs = append(p.ctxs, ctx)
|
||||
if len(p.ctxs) <= p.reads {
|
||||
return p.readUOW, nil
|
||||
}
|
||||
return nil, errStopBeforeProbeTx
|
||||
}
|
||||
|
||||
func (p *exemptionProbeProvider) Close(context.Context) error { return nil }
|
||||
|
||||
// txCtxs is the contexts handed to RunTx*, in call order: everything after the
|
||||
// leading read opens.
|
||||
func (p *exemptionProbeProvider) txCtxs() []context.Context {
|
||||
if len(p.ctxs) <= p.reads {
|
||||
return nil
|
||||
}
|
||||
return p.ctxs[p.reads:]
|
||||
}
|
||||
|
||||
// installExemptionProbe points the package's proxied provider at the probe for
|
||||
// the duration of one test.
|
||||
func installExemptionProbe(t *testing.T, p *exemptionProbeProvider) {
|
||||
t.Helper()
|
||||
prev := uowProvider
|
||||
uowProvider = p
|
||||
t.Cleanup(func() { uowProvider = prev })
|
||||
}
|
||||
|
||||
// exemptionProbeUOW embeds the interface without implementing it: anything a
|
||||
// dual's read phase needs beyond the three use cases below panics and names
|
||||
// itself, rather than being silently satisfied.
|
||||
type exemptionProbeUOW struct {
|
||||
uow.UnitOfWork
|
||||
issues domain.IssueUseCase
|
||||
labels domain.LabelUseCase
|
||||
deps domain.DependencyUseCase
|
||||
}
|
||||
|
||||
func (u exemptionProbeUOW) Close(context.Context) {}
|
||||
func (u exemptionProbeUOW) IssueUseCase() domain.IssueUseCase { return u.issues }
|
||||
func (u exemptionProbeUOW) LabelUseCase() domain.LabelUseCase { return u.labels }
|
||||
func (u exemptionProbeUOW) DependencyUseCase() domain.DependencyUseCase { return u.deps }
|
||||
|
||||
// CommentUseCase is collected by workapi.NewUOWDetailSource when the read
|
||||
// phase resolves an id; nothing on that path calls it.
|
||||
func (u exemptionProbeUOW) CommentUseCase() domain.CommentUseCase { return nil }
|
||||
|
||||
type exemptionProbeIssues struct {
|
||||
domain.IssueUseCase
|
||||
byID map[string]*types.Issue
|
||||
}
|
||||
|
||||
func (s exemptionProbeIssues) GetIssue(_ context.Context, id string) (*types.Issue, error) {
|
||||
if issue, ok := s.byID[id]; ok && !issue.Ephemeral {
|
||||
return issue, nil
|
||||
}
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
|
||||
func (s exemptionProbeIssues) GetWisp(_ context.Context, id string) (*types.Issue, error) {
|
||||
if issue, ok := s.byID[id]; ok && issue.Ephemeral {
|
||||
return issue, nil
|
||||
}
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
|
||||
func (s exemptionProbeIssues) SearchIssues(_ context.Context, _ string, filter types.IssueFilter) (domain.SearchPage, error) {
|
||||
var page domain.SearchPage
|
||||
for _, id := range filter.IDs {
|
||||
if issue, ok := s.byID[id]; ok {
|
||||
page.Items = append(page.Items, issue)
|
||||
}
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
type exemptionProbeLabels struct {
|
||||
domain.LabelUseCase
|
||||
}
|
||||
|
||||
func (exemptionProbeLabels) GetLabels(context.Context, string) ([]string, error) { return nil, nil }
|
||||
func (exemptionProbeLabels) GetWispLabels(context.Context, string) ([]string, error) { return nil, nil }
|
||||
|
||||
type exemptionProbeDeps struct {
|
||||
domain.DependencyUseCase
|
||||
}
|
||||
|
||||
func (exemptionProbeDeps) ListWithIssueMetadata(context.Context, string, domain.DepListFilter) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (exemptionProbeDeps) GetForIssueIDs(context.Context, []string) (map[string][]*types.Dependency, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func newExemptionProbeUOW(issues ...*types.Issue) uow.UnitOfWork {
|
||||
byID := make(map[string]*types.Issue, len(issues))
|
||||
for _, issue := range issues {
|
||||
byID[issue.ID] = issue
|
||||
}
|
||||
return exemptionProbeUOW{
|
||||
issues: exemptionProbeIssues{byID: byID},
|
||||
labels: exemptionProbeLabels{},
|
||||
deps: exemptionProbeDeps{},
|
||||
}
|
||||
}
|
||||
|
||||
// deferredRouteContext is what the proxied root pre-run hands every dispatch
|
||||
// under dolt.auto-commit=batch/off.
|
||||
func deferredRouteContext() context.Context {
|
||||
return issueops.WithDeferredVersionCommit(context.Background())
|
||||
}
|
||||
|
||||
// TestProxiedDualsExemptTheirExplicitCommitPoints pins the duals that reach
|
||||
// their transaction directly. Deleting explicitCommitPointContext from any of
|
||||
// them reds its row here.
|
||||
func TestProxiedDualsExemptTheirExplicitCommitPoints(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
call func(context.Context)
|
||||
}{
|
||||
{
|
||||
name: "batch",
|
||||
call: func(ctx context.Context) {
|
||||
_, _ = runBatchProxiedServer(ctx, nil, "bd: batch probe")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mol bond",
|
||||
call: func(ctx context.Context) {
|
||||
_ = runMolBondProxiedServer(ctx, molBondInput{argA: "bd-a", argB: "bd-b"})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mol squash",
|
||||
call: func(ctx context.Context) {
|
||||
_ = runMolSquashProxiedServer(ctx, molSquashInput{moleculeArg: "bd-mol"})
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
probe := &exemptionProbeProvider{}
|
||||
installExemptionProbe(t, probe)
|
||||
|
||||
tc.call(deferredRouteContext())
|
||||
|
||||
txCtxs := probe.txCtxs()
|
||||
if len(txCtxs) != 1 {
|
||||
t.Fatalf("%s: got %d transaction context(s), want 1 — the dual never reached RunTx*", tc.name, len(txCtxs))
|
||||
}
|
||||
if issueops.VersionCommitDeferred(txCtxs[0]) {
|
||||
t.Errorf("%s: the context handed to RunTx* still defers the version commit; "+
|
||||
"its direct twin is an explicit commit point, so this dual must clear the deferral "+
|
||||
"(doltServerTx.Commit blanks the message and mints nothing)", tc.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestProxiedMolBurnExemptsOnlyTheWispHalf pins the split. bd mol burn is two
|
||||
// commit classes on the direct route: wisps go through burnWisps -> `transact`
|
||||
// (explicit), persistent molecules go through deleteBatch -> issueOpsContext,
|
||||
// whose deferral dolt's deleter honors by returning before DOLT_COMMIT. A
|
||||
// command-wide exemption mints one commit per persistent molecule under
|
||||
// batch/off, where the identical direct-route command defers them all.
|
||||
func TestProxiedMolBurnExemptsOnlyTheWispHalf(t *testing.T) {
|
||||
wisp := &types.Issue{ID: "bd-wisp1", Ephemeral: true}
|
||||
persistent := &types.Issue{ID: "bd-mol1"}
|
||||
|
||||
probe := &exemptionProbeProvider{
|
||||
reads: 1, // runMolBurnProxiedServer's classification read
|
||||
readUOW: newExemptionProbeUOW(wisp, persistent),
|
||||
}
|
||||
installExemptionProbe(t, probe)
|
||||
|
||||
// force=true skips the interactive confirmation; dryRun=false so both
|
||||
// halves reach their transaction.
|
||||
_ = runMolBurnProxiedServer(deferredRouteContext(), []string{wisp.ID, persistent.ID}, false, true)
|
||||
|
||||
txCtxs := probe.txCtxs()
|
||||
if len(txCtxs) != 2 {
|
||||
t.Fatalf("got %d transaction context(s), want 2 (the wisp half then the persistent half); "+
|
||||
"the classification read did not produce one of each", len(txCtxs))
|
||||
}
|
||||
|
||||
if issueops.VersionCommitDeferred(txCtxs[0]) {
|
||||
t.Errorf("wisp half: the context handed to RunTxResult still defers the version commit, " +
|
||||
"but its direct twin (burnWisps -> transact) commits whatever dolt.auto-commit says")
|
||||
}
|
||||
if !issueops.VersionCommitDeferred(txCtxs[1]) {
|
||||
t.Errorf("persistent half: the context handed to RunTxResult no longer defers the version commit, " +
|
||||
"so a proxied `bd mol burn` mints one Dolt commit per persistent molecule under " +
|
||||
"dolt.auto-commit=batch/off — its direct twin (deleteBatch -> issueOpsContext) defers them " +
|
||||
"all to `bd dolt commit`")
|
||||
}
|
||||
}
|
||||
|
|
@ -284,3 +284,37 @@ func TestTransactHonoringAutoCommitBlanksMessageInServerBatch(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssueOpsContextDefersByModePolicyTable pins the policy half of GH#4995:
|
||||
// issueOpsContext defers the Dolt version commit for batch/off and does not for
|
||||
// on/unset. It deliberately does not claim to cover the proxied-server wiring —
|
||||
// issueOpsContext branches only on writesCommitNow(), never on the storage
|
||||
// mode, so setting proxiedServerMode here would prove nothing. The proxied
|
||||
// wiring is pinned by TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce
|
||||
// and the deferral itself by the doltserver_tx tests.
|
||||
func TestIssueOpsContextDefersByModePolicyTable(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
mode string
|
||||
wantDeferred bool
|
||||
}{
|
||||
{mode: string(doltAutoCommitOn), wantDeferred: false},
|
||||
{mode: string(doltAutoCommitBatch), wantDeferred: true},
|
||||
{mode: string(doltAutoCommitOff), wantDeferred: true},
|
||||
{mode: "", wantDeferred: false},
|
||||
} {
|
||||
t.Run("mode="+tc.mode, func(t *testing.T) {
|
||||
saveStorageMode(t)
|
||||
serverMode = false
|
||||
proxiedServerMode = false
|
||||
doltAutoCommit = tc.mode
|
||||
|
||||
ctx, err := issueOpsContext(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("issueOpsContext: %v", err)
|
||||
}
|
||||
if got := storageissueops.VersionCommitDeferred(ctx); got != tc.wantDeferred {
|
||||
t.Fatalf("VersionCommitDeferred = %v, want %v for mode %q", got, tc.wantDeferred, tc.mode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
191
cmd/bd/dolt_commit_proxied_integration_test.go
Normal file
191
cmd/bd/dolt_commit_proxied_integration_test.go
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// sqlCountValue extracts a COUNT(*) cell from bdProxiedSQLJSON. sqlValueEquals
|
||||
// only answers "is it this number"; the batch-flush assertions need the number
|
||||
// itself so a failure can report how far HEAD actually moved.
|
||||
func sqlCountValue(v any) (float64, bool) {
|
||||
switch x := v.(type) {
|
||||
case float64:
|
||||
return x, true
|
||||
case string:
|
||||
f, err := strconv.ParseFloat(x, 64)
|
||||
return f, err == nil
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
// TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce pins every half of
|
||||
// GH#4995 on the proxied route: dolt.auto-commit=batch must leave policy writes
|
||||
// in the working set (no Dolt commit per write), `bd dolt commit` must mint
|
||||
// exactly one commit for the whole batch and attribute it to the actor, and the
|
||||
// explicit commit points must keep minting their own commit with the caller's
|
||||
// message despite the route-wide deferral.
|
||||
//
|
||||
// Before the proxied flush point existed, step 3 below failed with
|
||||
// "no store available": proxied mode returns from the root pre-run before
|
||||
// newDoltStore runs, so getStore() is nil and `bd dolt commit` had nothing to
|
||||
// commit through. batch/off therefore meant "never commit" on this route.
|
||||
func TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce(t *testing.T) {
|
||||
requireProxiedServerEnv(t)
|
||||
|
||||
bd := buildEmbeddedBD(t)
|
||||
p := bdProxiedInit(t, bd, "batchflush")
|
||||
|
||||
doltLogCount := func(t *testing.T) float64 {
|
||||
t.Helper()
|
||||
rows := bdProxiedSQLJSON(t, bd, p.dir, "SELECT COUNT(*) AS count FROM dolt_log")
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("expected one row from dolt_log count, got %d: %v", len(rows), rows)
|
||||
}
|
||||
n, ok := sqlCountValue(rows[0]["count"])
|
||||
if !ok {
|
||||
t.Fatalf("dolt_log count is not numeric: %#v", rows[0]["count"])
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
headCommit := func(t *testing.T) map[string]interface{} {
|
||||
t.Helper()
|
||||
rows := bdProxiedSQLJSON(t, bd, p.dir,
|
||||
"SELECT message, committer, email FROM dolt_log ORDER BY date DESC LIMIT 1")
|
||||
if len(rows) != 1 {
|
||||
t.Fatalf("expected one dolt_log row for HEAD, got %d: %v", len(rows), rows)
|
||||
}
|
||||
return rows[0]
|
||||
}
|
||||
|
||||
head0 := doltLogCount(t)
|
||||
|
||||
// 1. Three writes under the batch policy must not mint any Dolt commit.
|
||||
for i := 1; i <= 3; i++ {
|
||||
title := fmt.Sprintf("batch write %d", i)
|
||||
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir,
|
||||
"--dolt-auto-commit", "batch", "create", title, "-p", "1")
|
||||
if err != nil {
|
||||
t.Fatalf("bd create %q failed: %v\nstdout:\n%s\nstderr:\n%s", title, err, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
if got := doltLogCount(t); got != head0 {
|
||||
t.Fatalf("batch writes minted Dolt commits: dolt_log went %v -> %v (want unchanged)", head0, got)
|
||||
}
|
||||
|
||||
// 2. The writes are in the working set, not rolled back.
|
||||
listOut, listErr, err := bdProxiedRunBuffers(t, bd, p.dir, "list", "--json")
|
||||
if err != nil {
|
||||
t.Fatalf("bd list --json failed: %v\nstdout:\n%s\nstderr:\n%s", err, listOut, listErr)
|
||||
}
|
||||
for i := 1; i <= 3; i++ {
|
||||
title := fmt.Sprintf("batch write %d", i)
|
||||
if !strings.Contains(listOut, title) {
|
||||
t.Fatalf("deferred write %q is not readable after batch create:\n%s", title, listOut)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. The explicit flush point mints exactly one commit for the batch.
|
||||
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir,
|
||||
"--actor", "flush-actor", "dolt", "commit", "-m", "batch flush")
|
||||
if err != nil {
|
||||
t.Fatalf("bd dolt commit failed in proxied mode: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "Committed.") {
|
||||
t.Fatalf("bd dolt commit did not report a commit:\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
|
||||
}
|
||||
if got, want := doltLogCount(t), head0+1; got != want {
|
||||
t.Fatalf("bd dolt commit did not advance HEAD exactly once: dolt_log %v -> %v (want %v)", head0, got, want)
|
||||
}
|
||||
|
||||
// 3b. The flush is attributed to the bd actor, not the SQL session user.
|
||||
// Under batch mode this is the only commit a whole batch of writes leaves
|
||||
// behind, so `dolt log` has to be able to say whose batch it was — the
|
||||
// '--author' DoltStorage.CommitAll passes on the direct route.
|
||||
head := headCommit(t)
|
||||
if got := head["message"]; got != "batch flush" {
|
||||
t.Fatalf("flush commit message = %v, want %q (row: %v)", got, "batch flush", head)
|
||||
}
|
||||
if got := head["committer"]; got != "flush-actor" {
|
||||
t.Fatalf("flush commit is not attributed to the actor: committer = %v, want %q (row: %v)", got, "flush-actor", head)
|
||||
}
|
||||
if got := head["email"]; got != "test@test.com" {
|
||||
t.Fatalf("flush commit email = %v, want the git identity %q (row: %v)", got, "test@test.com", head)
|
||||
}
|
||||
|
||||
// 4. A second flush with nothing pending is a no-op, not a second commit.
|
||||
stdout, stderr, err = bdProxiedRunBuffers(t, bd, p.dir, "dolt", "commit", "-m", "second flush")
|
||||
if err != nil {
|
||||
t.Fatalf("second bd dolt commit failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "Nothing to commit.") {
|
||||
t.Fatalf("second bd dolt commit did not report an empty working set:\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
|
||||
}
|
||||
if got, want := doltLogCount(t), head0+1; got != want {
|
||||
t.Fatalf("second bd dolt commit advanced HEAD: dolt_log = %v (want %v)", got, want)
|
||||
}
|
||||
|
||||
// 5. The deferral is policy-driven, not unconditional: with the default
|
||||
// "on" policy a single create advances dolt_log by one on its own.
|
||||
before := doltLogCount(t)
|
||||
stdout, stderr, err = bdProxiedRunBuffers(t, bd, p.dir,
|
||||
"--dolt-auto-commit", "on", "create", "immediate write", "-p", "1")
|
||||
if err != nil {
|
||||
t.Fatalf("bd create with --dolt-auto-commit on failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if got, want := doltLogCount(t), before+1; got != want {
|
||||
t.Fatalf("auto-commit=on did not commit per write: dolt_log %v -> %v (want %v)", before, got, want)
|
||||
}
|
||||
|
||||
// 6. The deferral is per commit CLASS, not per route: `bd batch -m` is an
|
||||
// explicit commit point (its direct-route twin calls transact, which mints
|
||||
// regardless of policy), so under batch it must still commit, carrying the
|
||||
// caller's message. A plain create in the same policy is the control.
|
||||
before = doltLogCount(t)
|
||||
stdout, stderr, err = bdProxiedRunBuffers(t, bd, p.dir,
|
||||
"--dolt-auto-commit", "batch", "create", "deferred control write", "-p", "1")
|
||||
if err != nil {
|
||||
t.Fatalf("control create under batch failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if got := doltLogCount(t); got != before {
|
||||
t.Fatalf("control create minted a commit under batch: dolt_log %v -> %v (want unchanged)", before, got)
|
||||
}
|
||||
|
||||
script := filepath.Join(p.dir, "explicit-batch.txt")
|
||||
if err := os.WriteFile(script, []byte("create task 1 explicit batch write\n"), 0o600); err != nil {
|
||||
t.Fatalf("write batch script: %v", err)
|
||||
}
|
||||
stdout, stderr, err = bdProxiedRunBuffers(t, bd, p.dir,
|
||||
"--dolt-auto-commit", "batch", "batch", "-f", script, "-m", "explicit batch commit")
|
||||
if err != nil {
|
||||
t.Fatalf("bd batch under batch policy failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if got, want := doltLogCount(t), before+1; got != want {
|
||||
t.Fatalf("explicit batch commit did not mint exactly one commit under batch: dolt_log %v -> %v (want %v)", before, got, want)
|
||||
}
|
||||
// The message is the half a route-wide deferral silently drops: blanking it
|
||||
// selects the plain-COMMIT form, which persists the rows and records nothing.
|
||||
if got := headCommit(t)["message"]; got != "explicit batch commit" {
|
||||
t.Fatalf("explicit batch commit lost the caller's message: %v (want %q)", got, "explicit batch commit")
|
||||
}
|
||||
|
||||
// 7. That commit swept the deferred control write with it (DOLT_COMMIT
|
||||
// '-Am'), so the working set is clean and a flush now has nothing to do —
|
||||
// the exemption does not strand the writes the policy deferred.
|
||||
stdout, stderr, err = bdProxiedRunBuffers(t, bd, p.dir, "dolt", "commit", "-m", "post-batch flush")
|
||||
if err != nil {
|
||||
t.Fatalf("flush after explicit batch commit failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "Nothing to commit.") {
|
||||
t.Fatalf("flush after explicit batch commit found pending work:\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
|
||||
}
|
||||
}
|
||||
88
cmd/bd/dolt_commit_proxied_server.go
Normal file
88
cmd/bd/dolt_commit_proxied_server.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
)
|
||||
|
||||
// runDoltCommitProxiedServer is the proxied dual of `bd dolt commit`: the
|
||||
// explicit commit point that discharges dolt.auto-commit=batch/off on this
|
||||
// route (GH#4995). Proxied mode returns from the root pre-run before
|
||||
// newDoltStore ever runs, so getStore() is nil here and the UOW provider is the
|
||||
// only handle on the server — without this dual, batch/off on a proxied
|
||||
// checkout meant "never commit" and writes piled up in the working set forever.
|
||||
//
|
||||
// It deliberately does not open a unit of work: doltServerTx.Commit blanks its
|
||||
// message under a deferred context (that is the deferral this command exists to
|
||||
// discharge), so routing the flush through it would make the flush itself a
|
||||
// no-op. It runs DOLT_COMMIT('-Am') on a pinned maintenance connection instead,
|
||||
// which is the uow-side equivalent of DoltStorage.CommitAll: "everything in the
|
||||
// working set", including out-of-band and config-table dirt, attributed to the
|
||||
// same actor with '--author'.
|
||||
func runDoltCommitProxiedServer(ctx context.Context, message string) (bool, error) {
|
||||
committed := false
|
||||
// runProxiedNonTx, not an open-coded provider assertion: it is the shared
|
||||
// maintenance escape hatch (backup, compact, clean-databases) and it reports
|
||||
// provider failures through HandleErrorRespectJSON, so `bd --json dolt
|
||||
// commit` keeps emitting the JSON error envelope the rest of the proxied
|
||||
// surface emits.
|
||||
err := runProxiedNonTx(ctx, func(ctx context.Context, conn *sql.Conn) error {
|
||||
// The same gate doltServerTx.Commit uses, and for the same reason: an
|
||||
// empty DOLT_COMMIT is rejected server-side with "nothing to commit"
|
||||
// and floods the server log.
|
||||
pending, err := issueops.HasPendingChanges(ctx, conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !pending {
|
||||
return nil
|
||||
}
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', ?, '--author', ?);", message, proxiedCommitAuthor()); err != nil {
|
||||
if isDoltNothingToCommit(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return nil
|
||||
})
|
||||
return committed, err
|
||||
}
|
||||
|
||||
// proxiedCommitAuthor is the '--author' identity for the proxied flush, the
|
||||
// counterpart of DoltStorage.commitAuthorString on the direct route. Without it
|
||||
// the commit is attributed to the SQL session user, and under batch mode this is
|
||||
// the ONE commit that survives a whole batch of writes — so on a multi-agent
|
||||
// deployment `dolt log` could not say whose batch was flushed.
|
||||
//
|
||||
// The actor half is the same getActor() the default commit message names; the
|
||||
// address half comes from git, as it does for the committer identity the store
|
||||
// resolves for its own commits.
|
||||
func proxiedCommitAuthor() string {
|
||||
name, email := proxiedServerCommitter()
|
||||
if a := strings.TrimSpace(getActor()); a != "" {
|
||||
name = a
|
||||
}
|
||||
return renderDoltCommitAuthor(name, email)
|
||||
}
|
||||
|
||||
// renderDoltCommitAuthor formats a Dolt '--author' argument. Dolt parses the
|
||||
// git "Name <email>" grammar and rejects anything else, so the angle brackets
|
||||
// and newlines an actor name could carry are stripped rather than passed
|
||||
// through to fail the flush.
|
||||
func renderDoltCommitAuthor(name, email string) string {
|
||||
clean := strings.NewReplacer("<", "", ">", "", "\n", " ", "\r", " ")
|
||||
name = strings.TrimSpace(clean.Replace(name))
|
||||
email = strings.TrimSpace(clean.Replace(email))
|
||||
if name == "" {
|
||||
name = "beads"
|
||||
}
|
||||
if email == "" {
|
||||
email = "beads@localhost"
|
||||
}
|
||||
return fmt.Sprintf("%s <%s>", name, email)
|
||||
}
|
||||
29
cmd/bd/dolt_commit_proxied_server_test.go
Normal file
29
cmd/bd/dolt_commit_proxied_server_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestRenderDoltCommitAuthor pins the '--author' argument the proxied flush
|
||||
// passes to DOLT_COMMIT (GH#4995). Dolt parses the git "Name <email>" grammar
|
||||
// and rejects anything else, and the name half is an actor string the caller
|
||||
// controls, so a stray bracket or newline must be stripped rather than turned
|
||||
// into a failed flush — the flush is the only commit batch mode keeps.
|
||||
func TestRenderDoltCommitAuthor(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
actor string
|
||||
email string
|
||||
want string
|
||||
}{
|
||||
{name: "actor and email", actor: "agent-7", email: "agent7@example.com", want: "agent-7 <agent7@example.com>"},
|
||||
{name: "trims surrounding space", actor: " agent-7 ", email: " agent7@example.com\n", want: "agent-7 <agent7@example.com>"},
|
||||
{name: "strips brackets the grammar owns", actor: "agent <7>", email: "a@b", want: "agent 7 <a@b>"},
|
||||
{name: "strips embedded newline", actor: "agent\n7", email: "a@b", want: "agent 7 <a@b>"},
|
||||
{name: "defaults an empty identity", actor: "", email: "", want: "beads <beads@localhost>"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := renderDoltCommitAuthor(tc.actor, tc.email); got != tc.want {
|
||||
t.Fatalf("renderDoltCommitAuthor(%q, %q) = %q, want %q", tc.actor, tc.email, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,15 @@ func exitCodeFromError(err error) (int, bool) {
|
|||
return 0, false
|
||||
}
|
||||
|
||||
// isReportedExit reports whether err is an already-rendered failure — the
|
||||
// *exitError every Handle*Error returns after writing the message (or the JSON
|
||||
// error envelope) itself. A caller that re-wraps one prints a second Error line
|
||||
// and, in --json mode, a plain line after the envelope.
|
||||
func isReportedExit(err error) bool {
|
||||
_, reported := exitCodeFromError(err)
|
||||
return reported
|
||||
}
|
||||
|
||||
func activeWorkspaceNotFoundError() string {
|
||||
return "no active beads workspace found"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,6 @@ func TestHistoryRemoteRefusalFrontDoorMatrix(t *testing.T) {
|
|||
{"flatten", "proxy.flatten.unsupported", "flatten is not supported in proxied-server mode", []string{"flatten"}},
|
||||
{"dolt push", "proxy.dolt_push.unsupported", "dolt push is not supported in proxied-server mode", []string{"dolt", "push"}},
|
||||
{"dolt pull", "proxy.dolt_pull.unsupported", "dolt pull is not supported in proxied-server mode", []string{"dolt", "pull"}},
|
||||
{"dolt commit", "proxy.dolt_commit.unsupported", "dolt commit is not supported in proxied-server mode", []string{"dolt", "commit"}},
|
||||
{"dolt remote add", "proxy.dolt_remote.unsupported", "dolt remote add is not supported in proxied-server mode", []string{"dolt", "remote", "add", "backup", "https://example.invalid/backup"}},
|
||||
{"dolt remote list", "proxy.dolt_remote.unsupported", "dolt remote list is not supported in proxied-server mode", []string{"dolt", "remote", "list"}},
|
||||
{"dolt remote reset-data", "proxy.dolt_remote.unsupported", "dolt remote reset-data is not supported in proxied-server mode", []string{"dolt", "remote", "reset-data", "backup"}},
|
||||
|
|
@ -340,8 +339,10 @@ func startHistoryUnixBridge(t *testing.T, endpoint, upstreamPort string) *exec.C
|
|||
}
|
||||
}
|
||||
|
||||
// TestHistoryRemoteSupportedFrontDoorParity checks the two operations that
|
||||
// are intentionally shared by direct and proxied providers.
|
||||
// TestHistoryRemoteSupportedFrontDoorParity checks the operations that are
|
||||
// intentionally shared by direct and proxied providers: history --events,
|
||||
// dolt remote remove, and dolt commit (the flush point dolt.auto-commit=batch/off
|
||||
// defers to, GH#4995).
|
||||
func TestHistoryRemoteSupportedFrontDoorParity(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_PROXIED_SERVER") != "1" && os.Getenv(managedLocalProxiedEnvVar) != "1" {
|
||||
t.Skip("set a proxied test lane to run history parity")
|
||||
|
|
@ -376,6 +377,13 @@ func TestHistoryRemoteSupportedFrontDoorParity(t *testing.T) {
|
|||
if err != nil || !strings.Contains(string(out), "backup") {
|
||||
t.Fatalf("remote remove failed: %v\n%s", err, out)
|
||||
}
|
||||
// dolt commit must pass the front door rather than be refused. Exact
|
||||
// commit semantics are pinned by
|
||||
// TestProxiedServerBatchDefersThenDoltCommitAdvancesHeadOnce.
|
||||
out, err = bdProxiedRun(t, bd, p.dir, "--json", "dolt", "commit")
|
||||
if err != nil || strings.Contains(string(out), "proxy.dolt_commit.unsupported") {
|
||||
t.Fatalf("dolt commit refused or failed: %v\n%s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -890,7 +890,7 @@ func init() {
|
|||
rootCmd.PersistentFlags().BoolVar(&sandboxMode, "sandbox", false, "Sandbox mode: disables Dolt auto-push")
|
||||
rootCmd.PersistentFlags().BoolVar(&readonlyMode, "readonly", false, "Read-only mode: block write operations (for worker sandboxes)")
|
||||
rootCmd.PersistentFlags().BoolVar(&globalFlag, "global", false, "Use the global shared-server database (beads_global)")
|
||||
rootCmd.PersistentFlags().StringVar(&doltAutoCommit, "dolt-auto-commit", "", "Dolt auto-commit policy (off|on|batch). 'on': commit after each write. 'batch': defer commits to bd dolt commit; uncommitted changes persist in the working set until then (a live batch-mode bd process also flushes on SIGTERM/SIGHUP). Applies to embedded and direct SQL-server modes; proxied-server routes are unaffected. Default: on. Override via config key dolt.auto-commit")
|
||||
rootCmd.PersistentFlags().StringVar(&doltAutoCommit, "dolt-auto-commit", "", "Dolt auto-commit policy (off|on|batch). 'on': commit after each write. 'batch': defer commits to bd dolt commit; uncommitted changes persist in the working set until then (a live batch-mode bd process also flushes on SIGTERM/SIGHUP, except in proxied-server mode, where bd dolt commit is the only flush point). In proxied-server mode the deferral covers the writes the CLI makes on that route, including config and version metadata; explicit commit points (bd batch, bd mol bond/pour/squash, bd mol wisp create, and the wisp half of bd mol burn) still commit, and a bd serve process on the same database is unaffected. Default: on. Override via config key dolt.auto-commit")
|
||||
rootCmd.PersistentFlags().BoolVar(&cpuProfileEnabled, "cpu-profile", false, "Generate CPU profile for performance analysis")
|
||||
rootCmd.PersistentFlags().StringVar(&memProfilePath, "mem-profile", "", "Write heap profile to FILE on exit (also respects BEADS_MEM_PROFILE)")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Enable verbose/debug output")
|
||||
|
|
@ -1827,6 +1827,16 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
uowProvider = wireExternalDependencyUOWProvider(uow.NewNotifyingProvider(p, uowSinks))
|
||||
|
||||
// Honor dolt.auto-commit for proxied writes the same way
|
||||
// issueOpsContext already does for the direct/SQL-server routes
|
||||
// (bd-4wamg): batch/off defer the Dolt version commit rather than
|
||||
// minting one per write (GH#4995). uow.issueOperations reads this
|
||||
// off the context for every Create/Update/Close/Reopen it runs.
|
||||
rootCtx, err = issueOpsContext(rootCtx)
|
||||
if err != nil {
|
||||
return HandleError("failed to resolve dolt auto-commit policy: %v", err)
|
||||
}
|
||||
|
||||
if !previewMode {
|
||||
reconcileVersionProxiedServer(rootCtx)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ func runPourProxiedServer(ctx context.Context, in pourInput) error {
|
|||
if uowProvider == nil {
|
||||
return HandleError("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Explicit commit point, like the `transact` in cloneSubgraph the direct
|
||||
// route reaches through spawnMolecule (GH#4995).
|
||||
ctx = explicitCommitPointContext(ctx)
|
||||
|
||||
vars, err := parseVarFlags(in.varFlags)
|
||||
if err != nil {
|
||||
|
|
@ -372,6 +375,9 @@ func runMolBondProxiedServer(ctx context.Context, in molBondInput) error {
|
|||
if uowProvider == nil {
|
||||
return HandleErrorRespectJSON("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Explicit commit point, like `transact` in bondProtoMol/bondMolecules on
|
||||
// the direct route (GH#4995).
|
||||
ctx = explicitCommitPointContext(ctx)
|
||||
|
||||
if in.dryRun {
|
||||
uw, err := proxiedOpenReadUOW(ctx)
|
||||
|
|
@ -446,6 +452,9 @@ func runMolSquashProxiedServer(ctx context.Context, in molSquashInput) error {
|
|||
if uowProvider == nil {
|
||||
return HandleErrorRespectJSON("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Explicit commit point, like `transact` in runMolSquash on the direct
|
||||
// route (GH#4995).
|
||||
ctx = explicitCommitPointContext(ctx)
|
||||
|
||||
if in.dryRun {
|
||||
uw, err := proxiedOpenReadUOW(ctx)
|
||||
|
|
@ -516,6 +525,15 @@ func runMolBurnProxiedServer(ctx context.Context, args []string, dryRun, force b
|
|||
if uowProvider == nil {
|
||||
return HandleErrorRespectJSON("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Only the wisp half is an explicit commit point, so the exemption is
|
||||
// applied per commit site below rather than to this ctx (GH#4995).
|
||||
// runMolBurn splits on the direct route: wisps go through burnWisps ->
|
||||
// `transact`, which mints the commit whatever dolt.auto-commit says, while
|
||||
// persistent molecules go through deleteBatch -> issueOpsContext, the
|
||||
// policy-honoring class — dolt's deleter returns before DOLT_COMMIT when
|
||||
// that context defers. Exempting the whole command would mint one commit
|
||||
// per persistent molecule under batch/off, on a destructive verb, where the
|
||||
// identical direct-route command defers them all to `bd dolt commit`.
|
||||
|
||||
uw, err := proxiedOpenReadUOW(ctx)
|
||||
if err != nil {
|
||||
|
|
@ -617,7 +635,8 @@ func runMolBurnProxiedServer(ctx context.Context, args []string, dryRun, force b
|
|||
}
|
||||
|
||||
if len(wispIDs) > 0 {
|
||||
result, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (*BurnResult, string, error) {
|
||||
// The wisp half's direct twin is burnWisps -> `transact`.
|
||||
result, err := uow.RunTxResult(explicitCommitPointContext(ctx), uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (*BurnResult, string, error) {
|
||||
r, err := burnWispsInto(ctx, newUOWMolWriter(uw), wispIDs, actor)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
|
|
@ -635,6 +654,10 @@ func runMolBurnProxiedServer(ctx context.Context, args []string, dryRun, force b
|
|||
}
|
||||
|
||||
for _, id := range persistentIDs {
|
||||
// No exemption: the persistent half's direct twin is deleteBatch ->
|
||||
// issueOpsContext, so this stays on the inherited policy context and
|
||||
// the message is blanked under batch/off, exactly as the direct route
|
||||
// leaves these deletions in the working set for `bd dolt commit`.
|
||||
result, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (BurnResult, string, error) {
|
||||
subgraph, err := loadTemplateSubgraph(ctx, newUOWMolWriter(uw), id)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,10 @@ func runProxiedNonTx(ctx context.Context, fn func(ctx context.Context, conn *sql
|
|||
}
|
||||
mp, ok := uowProvider.(uow.MaintenanceProvider)
|
||||
if !ok {
|
||||
return HandleErrorRespectJSON("proxied-server provider does not support maintenance operations")
|
||||
// The %T is the whole diagnostic: every wrapper in the proxied chain
|
||||
// forwards RunNonTx with a compile-time assertion, so reaching this
|
||||
// means a new decorator dropped it, and its type names the culprit.
|
||||
return HandleErrorRespectJSON("proxied-server provider %T does not support maintenance operations", uowProvider)
|
||||
}
|
||||
return mp.RunNonTx(ctx, fn)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -179,7 +179,10 @@ Examples:
|
|||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("vc commit is not supported in proxied-server mode")
|
||||
// There IS a commit point on this route now — `bd dolt commit`
|
||||
// is the proxied flush point dolt.auto-commit=batch/off defers
|
||||
// to (GH#4995) — so name it rather than leaving a dead end.
|
||||
return HandleErrorRespectJSON("vc commit is not supported in proxied-server mode; use 'bd dolt commit' instead")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("vc-commit")
|
||||
defer func() {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,12 @@ func runWispCreateProxiedServer(ctx context.Context, in wispCreateInput) error {
|
|||
if uowProvider == nil {
|
||||
return HandleError("proxied-server UOW provider not initialized")
|
||||
}
|
||||
// Explicit commit point, like the `transact` in cloneSubgraph the direct
|
||||
// route reaches through spawnMoleculeWithOptions (GH#4995). The wisp tables
|
||||
// are dolt_ignored, so the commit this preserves is whatever tracked rows
|
||||
// the spawn touches — the class membership is what matters here, not the
|
||||
// size of the commit.
|
||||
ctx = explicitCommitPointContext(ctx)
|
||||
|
||||
vars, err := parseVarFlags(in.varFlags)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ Any key whose name contains `api_key`, `api-key`, `secret`, `token`, or `passwor
|
|||
| `agent.profile` | — | `BD_AGENT_PROFILE` | `conservative` | Policy profile `bd prime` uses for git/commit authority: `conservative`, `minimal`, `team-maintainer`; invalid values fall back to `conservative` |
|
||||
| `prime.max-memories` | `--max-memories` | `BD_PRIME_MAX_MEMORIES` | `0` | Max persistent memories injected by `bd prime` (0 = unlimited) |
|
||||
| `prime.max-memory-chars` | `--max-memory-chars` | `BD_PRIME_MAX_MEMORY_CHARS` | `0` | Max total bytes of memory entries injected by `bd prime`, at whole-memory boundaries (0 = unlimited) |
|
||||
| `dolt.auto-commit` | `--dolt-auto-commit` | `BD_DOLT_AUTO_COMMIT` | `on` | Create a Dolt history commit after each successful write (see [below](#auto-commit-sql-commits-vs-dolt-commits)) |
|
||||
| `dolt.auto-commit` | `--dolt-auto-commit` | `BD_DOLT_AUTO_COMMIT` | `on` | `off\|on\|batch`: `on` creates a Dolt history commit after each successful write; `batch`/`off` defer it to `bd dolt commit` (see [below](#auto-commit-sql-commits-vs-dolt-commits)) |
|
||||
| `dolt.auto-push` | — | `BD_DOLT_AUTO_PUSH` | `false` | Auto-push to Dolt remote after writes (opt-in; see [below](#auto-push)) |
|
||||
| `dolt.auto-push-interval` | — | `BD_DOLT_AUTO_PUSH_INTERVAL` | `5m` | Minimum time between auto-pushes |
|
||||
| `dolt.auto-push-timeout` | — | `BD_DOLT_AUTO_PUSH_TIMEOUT` | `30s` | Timeout for a single auto-push attempt |
|
||||
|
|
@ -196,6 +196,26 @@ dolt:
|
|||
auto-commit: off
|
||||
```
|
||||
|
||||
`batch` and `off` defer the Dolt history commit instead of skipping the write: the change is
|
||||
still durable in the working set, and `bd dolt commit` records the accumulated batch as a single
|
||||
Dolt commit. This applies on every storage mode — embedded, direct SQL-server, and
|
||||
proxied-server. In proxied-server mode the deferral covers the writes the CLI makes on that
|
||||
route, including workspace config and version metadata, and `bd dolt commit` is the only flush
|
||||
point there: the SIGTERM/SIGHUP flush of a live batch-mode process needs an open store, which
|
||||
proxied mode never has (it does run in embedded and direct SQL-server modes).
|
||||
|
||||
Two kinds of write stay outside the deferral, in every mode:
|
||||
|
||||
- **Explicit commit points** — `bd batch` (whose commit message defaults to a synthesized one
|
||||
when `-m` is absent, so every batch commits), `bd mol bond`, `bd mol pour`, `bd mol squash`,
|
||||
`bd mol wisp create`, and the wisp half of `bd mol burn` — commit what they wrote, with the
|
||||
message they were given: the commit is that command's contract rather than auto-commit
|
||||
policy. Burning a *persistent* molecule is an ordinary delete, so it honors the policy and
|
||||
defers with everything else.
|
||||
- **Writes made by a `bd serve` process**, whose HTTP API requests carry their own context
|
||||
instead of the CLI's, so a daemon keeps committing per write even while the CLI on the same
|
||||
database defers.
|
||||
|
||||
### Auto-backup
|
||||
|
||||
Periodic Dolt-native backup to `.beads/backup/` provides a recovery path independent of the live database. Local Dolt commits (via `dolt.auto-commit`) remain the primary safety net; backup is a secondary layer. Unlike `bd export` or `.beads/issues.jsonl`, this is a full database backup: it preserves tables, branches, commit history, and working-set data.
|
||||
|
|
|
|||
|
|
@ -19,6 +19,18 @@ func WithDeferredVersionCommit(ctx context.Context) context.Context {
|
|||
return context.WithValue(ctx, deferVersionCommitKey{}, true)
|
||||
}
|
||||
|
||||
// WithImmediateVersionCommit returns a context that creates its Dolt version
|
||||
// commit even when an ancestor context deferred it.
|
||||
//
|
||||
// A route that applies the deferral once, for every command it dispatches —
|
||||
// the proxied-server CLI sets it on the root context (GH#4995) — needs this to
|
||||
// re-express the exemption the per-verb routes get by not being wrapped at all:
|
||||
// an explicit commit point ("commit these writes now, with this message") is
|
||||
// not a write whose versioning is policy, so it must not inherit the deferral.
|
||||
func WithImmediateVersionCommit(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, deferVersionCommitKey{}, false)
|
||||
}
|
||||
|
||||
// VersionCommitDeferred reports whether ctx defers version commits.
|
||||
func VersionCommitDeferred(ctx context.Context) bool {
|
||||
deferred, _ := ctx.Value(deferVersionCommitKey{}).(bool)
|
||||
|
|
|
|||
|
|
@ -34,13 +34,28 @@ func (t *doltServerTx) Commit(ctx context.Context, message string) error {
|
|||
if t.done {
|
||||
return errors.New("uow: commit: already done")
|
||||
}
|
||||
// dolt.auto-commit=batch/off (GH#4995): defer the Dolt version commit to a
|
||||
// later explicit commit point (bd dolt commit), the same as the embedded
|
||||
// and direct SQL-server backends already do at their own commit sites
|
||||
// (bd-4wamg) — persist the write into the working set via a plain SQL
|
||||
// COMMIT below, without minting Dolt history, regardless of what message
|
||||
// the caller named. issueOpsContext sets this on the context before the
|
||||
// proxied CLI dispatch opens its unit of work.
|
||||
if message != "" && issueops.VersionCommitDeferred(ctx) {
|
||||
message = ""
|
||||
}
|
||||
// An empty message selects the EPHEMERAL commit form (bd-aq0ql): a plain
|
||||
// SQL COMMIT persists the transaction's writes into the working set
|
||||
// without minting a Dolt commit or history. This exists for work that
|
||||
// touches ONLY dolt_ignored state — today the leases table (bd-lrgn1),
|
||||
// whose heartbeats must never create commits — and is only reachable via
|
||||
// uow.RunTxEphemeral: RunTx/RunTxResult treat an empty commitMsg as
|
||||
// "nothing to commit" and never call Commit at all.
|
||||
// without minting a Dolt commit or history. It has two entry points:
|
||||
//
|
||||
// - uow.RunTxEphemeral, for work that touches ONLY dolt_ignored state —
|
||||
// today the leases table (bd-lrgn1), whose heartbeats must never create
|
||||
// commits. RunTx/RunTxResult treat an empty commitMsg as "nothing to
|
||||
// commit" and never call Commit at all, so they cannot reach it that way.
|
||||
// - RunTx/RunTxResult under a deferred-version-commit context, via the
|
||||
// blanking just above (GH#4995). There the writes are ordinary versioned
|
||||
// rows and the ONLY-dolt_ignored-state rationale does not apply: the
|
||||
// working set is meant to hold them until the explicit flush point.
|
||||
stmt, args := "CALL DOLT_COMMIT('-Am', ?);", []interface{}{message}
|
||||
if message == "" {
|
||||
stmt, args = "COMMIT;", nil
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
|
@ -202,3 +203,85 @@ func TestDoltServerTxEphemeralCommitSkipsPendingCheck(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
// TestDoltServerTxCommitDefersDoltCommitUnderDeferredContext pins GH#4995:
|
||||
// dolt.auto-commit=batch/off marks the context with WithDeferredVersionCommit.
|
||||
// doltServerTx.Commit must blank the commit message, bypassing DOLT_COMMIT
|
||||
// and the HasPendingChanges dolt_status check, and executing a plain COMMIT
|
||||
// to persist writes into the working set without advancing Dolt history.
|
||||
func TestDoltServerTxCommitDefersDoltCommitUnderDeferredContext(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
deferred bool
|
||||
}{
|
||||
{name: "on (not deferred)", deferred: false},
|
||||
{name: "batch or off (deferred)", deferred: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p, mock := newMockTxProvider(t)
|
||||
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
if tc.deferred {
|
||||
// With deferred version commit (GH#4995), the non-empty message is blanked,
|
||||
// skipping DOLT_COMMIT and executing plain COMMIT without status check.
|
||||
mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
} else {
|
||||
expectPendingChanges(mock, 1)
|
||||
mock.ExpectExec("DOLT_COMMIT").WithArgs("bd: real write").WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
}
|
||||
|
||||
tx, err := p.BeginTx(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
if tc.deferred {
|
||||
ctx = issueops.WithDeferredVersionCommit(ctx)
|
||||
}
|
||||
|
||||
err = tx.Commit(ctx, "bd: real write")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
assert.Equal(t, 1, p.db.Stats().OpenConnections, "session must cleanly return to pool")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDoltServerTxCommitHonorsImmediateVersionCommit pins the other half of the
|
||||
// GH#4995 policy: the proxied route applies the deferral ONCE, to the root
|
||||
// context, so the explicit commit points — the duals whose direct-route twin
|
||||
// calls transact and mints a Dolt commit whatever dolt.auto-commit says — opt
|
||||
// back out with issueops.WithImmediateVersionCommit. The caller's message must
|
||||
// then survive all the way to DOLT_COMMIT; blanking it would persist the rows
|
||||
// and record nothing, which is what `bd batch -m` must never do.
|
||||
func TestDoltServerTxCommitHonorsImmediateVersionCommit(t *testing.T) {
|
||||
p, mock := newMockTxProvider(t)
|
||||
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
expectPendingChanges(mock, 1)
|
||||
mock.ExpectExec("DOLT_COMMIT").WithArgs("bd: batch 3 ops by tester").WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
tx, err := p.BeginTx(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Composed in the order the proxied CLI composes them: the route-wide
|
||||
// deferral from the pre-run, then the exemption from the dual.
|
||||
ctx := issueops.WithImmediateVersionCommit(issueops.WithDeferredVersionCommit(context.Background()))
|
||||
require.NoError(t, tx.Commit(ctx, "bd: batch 3 ops by tester"))
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
assert.Equal(t, 1, p.db.Stats().OpenConnections, "session must cleanly return to pool")
|
||||
}
|
||||
|
||||
// TestDoltServerTxRunTxWithDeferredContextSkipsDoltCommit verifies that a complete
|
||||
// unit of work executed via RunTx under deferred version commit commits via plain
|
||||
// COMMIT and succeeds without advancing Dolt history (GH#4995).
|
||||
func TestDoltServerTxRunTxWithDeferredContextSkipsDoltCommit(t *testing.T) {
|
||||
p, mock := newMockTxProvider(t)
|
||||
mock.ExpectExec("START TRANSACTION").WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
mock.ExpectExec(matchPlainCommit).WillReturnResult(sqlmock.NewResult(0, 0))
|
||||
|
||||
ctx := issueops.WithDeferredVersionCommit(context.Background())
|
||||
err := RunTx(ctx, p, func(ctx context.Context, uw UnitOfWork) (string, error) {
|
||||
return "bd: update GH#4995", nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mock.ExpectationsWereMet())
|
||||
assert.Equal(t, 1, p.db.Stats().OpenConnections)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue