Write a journal row for every committed bead mutation, in the SAME SQL
transaction as the mutation itself. Because the row and the mutation commit
atomically, the journal can never lag the data or record something that did not
happen; a rolled-back mutation takes its record — and its seq — with it.
Emission lives at the issueops seam because both write plumbings bottom out
there: the DoltStorage decorator chain and the unit-of-work repositories both
funnel through the same *InTx functions and the same insertEventRow, so the seq
mechanism is shared by construction and cannot drift between them. Each seq is
drawn from the single-row bd_events_seq counter inside the caller's transaction,
which is what makes concurrent allocators contend: on the SQL server the loser
aborts with a serialization error and the write path's existing retry redoes the
whole unit of work, while the embedded engine serializes writers on the row.
Either way the surviving seqs are gapless and commit-ordered, which is the
property a cursor consumer needs and AUTO_INCREMENT cannot give.
The vocabulary is six wire ops (create/update/close/delete/dep_add/dep_remove)
plus an engine-only `comment` op carrying a replayable comment payload, so a
consumer can reproduce comment text without re-reading the database. Deletes
record a null snapshot; dependency events carry {kind,target,metadata}; a
dependency event whose source vanished under cascade records a null snapshot
rather than failing. Every snapshot is taken after derived blocked-state
maintenance has settled, and is_blocked flips on affected neighbors journal their
own updates — otherwise a consumer would hold a stale readiness projection until
some later full reconciliation. Emission is never gated on EmitEvent: the audit
`events` table is a human history and the journal is a machine replay feed, and
suppressing audit noise must not punch a hole in a cursor.
Activation is per storage instance (storage.EventsJournalConfigurer), never a
process-global switch: a process holds several stores at once, and enabling one
must not enable the rest. It is off by default. In journal mode the Dolt store
pins its normally-split regular and ignored transactions into one, because the
counter, the journal row and the mutation must commit together — otherwise a
mixed durable+wisp callback makes the two transactions contend on the counter
row and the ignored commit can fail after the regular side has already
committed.
Completeness is structural, not a hand list. TestEveryBeadMutatorJournalsOrIsExempt
finds every exported function that writes a work-bead table by the DML it
executes and demands it journal or carry a written exemption; the domain/db
guard does the same for the repository surface. Emission deliberately does not
propagate across a call into the readiness-recompute family — almost every
mutator ends with a recompute, so counting that would let a mutator stop
recording itself entirely and still pass. Tightening that is what exposed the
gaps this change closes: the uow issue Update and dependency Insert/Delete
recorded nothing of their own, and persistence moves were covered only by
accident.
Instrumented beyond the #4916 lineage, which never saw them: the public
lifecycle surface (Execute*), the guarded CAS/savepoint close, unclaim and
conditional unclaim, defer wake, persistence moves, demote-to-wisp, wisp slot
and metadata writes, the storage.Transaction comment methods, creation-time
comments, and active wisp deletes — including the wisp GC batch path, which
resolves which ids it actually removes before deleting so it cannot record a
delete for a bead that was already gone. The bulk issue delete does the same,
since a batched DELETE reports a count rather than a set.
Emission is only half of coverage: the store must also turn journaling ON for
the transaction the mutation runs in, and that half was hand-enumerated and
wrong twice — first eight raw-transaction sites, then the wisp slot/metadata
pair, each committing mutations while writing an empty journal. Missing scoping
is invisible (the code runs, the write lands, the journal is simply empty), so
TestEveryRawTxJournalScopeIsScopedOrExempt now checks it structurally: any
function that begins its own transaction and hands it to journaling issueops
code must bind activation to that transaction or carry a written exemption. Its
mutator set is derived from the issueops package's own emit analysis rather than
listed, so a new journaling mutator widens the guard automatically. The two
exemptions are the is_blocked maintenance passes (post-merge recompute,
federation-filter restage): derived state a replay consumer reconstructs from
the graph, so recording it would add rows carrying nothing new.
Creation-time comments get their own ops rather than riding the create snapshot,
because issue hydration joins labels but not comments — an imported comment
would otherwise be unreplayable, which is precisely what the comment op exists
to prevent. They are emitted after the create, so a consumer is never told about
a comment on a bead it has not seen created.
A journal seq counter that has fallen BEHIND the journal (restored, hand-edited,
or copied between workspaces) used to wedge an instance permanently: every
mutation re-minted a taken seq, the primary key rejected it, and because the
journal row shares the mutation's transaction the user's write failed with it.
insertEventRow now heals that reactively off the duplicate-key failure — raise
the counter past the high-water mark, retry once, let a second failure surface —
so the happy path pays nothing and a stale counter self-repairs instead of
bricking writes.
Heartbeats deliberately do NOT journal, departing from the reference lineage,
which emitted on them. A heartbeat is a high-frequency lease keepalive writing
only the clone-local `leases` table; journaling it would put a full-snapshot
write plus the shared seq-counter serialization on the hottest write in the
fleet, and a replay consumer gains nothing because lease state is working-set
state that expires on its own. Lease RECLAIM still journals — it clears assignee
and reverts status, which is durable. TestExemptMutationsDoNotJournal pins that
ruling from the other side, so re-adding the emit fails rather than silently
reversing it.
Compaction and restore stay unjournaled for reference and enterprise shape
parity, and their exemption text now says what that costs instead of implying it
is free: a consumer's mirror of a compacted bead goes stale until that bead's
next journaled mutation delivers a fresh snapshot. Known contract gap, carried
deliberately, flagged for review.
types.Issue gains IsBlocked so a journal snapshot can carry the persisted
readiness projection a graph delta needs to be replayable. It is omitempty and
set only where the journal reads it back, so no other serialization changes —
note that enterprise serializes is_blocked unconditionally, so the two wire
shapes differ on the false case; the S2 consumer golden pins the OSS shape.
issueops.WithEventsJournal is documented as test-only and in-transaction-only:
it activates by context rather than by transaction, so pointing it at a pooled
autocommit handle would let the seq UPDATE and the SELECT that must observe it
land on different connections. No production path uses it.
Verification note: the full internal/storage/dolt shard could not be run to
completion locally (it throttles to two concurrent database tests and exceeds 90
minutes), so it was covered by targeted batches over every touched file and its
highest-risk neighbors — which is what caught the dependency_commit_boundary
fake driver still matching the pre-change `SELECT type FROM ...` probe. CI must
run that shard before merge.
Re-port of OSS PR #4916 (head 3666e5026), taking the 3bfed2431 hardening
generation as the shape of record.
Co-authored-by: CI Bot <ci@beads.test>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of julianknutsen
235 lines
7.7 KiB
Go
235 lines
7.7 KiB
Go
package dolt
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
// dependencyCommitBoundaryDriver models dependency writes through their SQL
|
|
// mutation and transaction commit. A lost commit response is returned after
|
|
// the mutation so an unsafe replay is visible as a second attempt.
|
|
type dependencyCommitBoundaryDriver struct {
|
|
mu sync.Mutex
|
|
|
|
activeWisp bool
|
|
commitErr error
|
|
newEdge bool
|
|
|
|
deletes int
|
|
inserts int
|
|
txAttempts int
|
|
commitCalls int
|
|
}
|
|
|
|
func (d *dependencyCommitBoundaryDriver) Open(string) (driver.Conn, error) {
|
|
return &dependencyCommitBoundaryConn{driver: d}, nil
|
|
}
|
|
|
|
func (d *dependencyCommitBoundaryDriver) Connect(context.Context) (driver.Conn, error) {
|
|
return &dependencyCommitBoundaryConn{driver: d}, nil
|
|
}
|
|
|
|
func (d *dependencyCommitBoundaryDriver) Driver() driver.Driver { return d }
|
|
|
|
type dependencyCommitBoundaryConn struct {
|
|
driver *dependencyCommitBoundaryDriver
|
|
}
|
|
|
|
func (c *dependencyCommitBoundaryConn) Prepare(string) (driver.Stmt, error) {
|
|
return nil, errors.New("dependency commit boundary driver does not prepare statements")
|
|
}
|
|
|
|
func (c *dependencyCommitBoundaryConn) Close() error { return nil }
|
|
|
|
func (c *dependencyCommitBoundaryConn) Begin() (driver.Tx, error) {
|
|
c.driver.mu.Lock()
|
|
c.driver.txAttempts++
|
|
c.driver.mu.Unlock()
|
|
return &dependencyCommitBoundaryTx{driver: c.driver}, nil
|
|
}
|
|
|
|
func (c *dependencyCommitBoundaryConn) QueryContext(_ context.Context, query string, _ []driver.NamedValue) (driver.Rows, error) {
|
|
c.driver.mu.Lock()
|
|
defer c.driver.mu.Unlock()
|
|
|
|
switch {
|
|
case strings.Contains(query, "SELECT 1 FROM wisps WHERE id = ? LIMIT 1"):
|
|
if c.driver.activeWisp {
|
|
return &dependencyCommitBoundaryRows{columns: []string{"exists"}, values: [][]driver.Value{{int64(1)}}}, nil
|
|
}
|
|
return &dependencyCommitBoundaryRows{columns: []string{"exists"}}, nil
|
|
case strings.Contains(query, "SELECT issue_type FROM wisps WHERE id = ?"):
|
|
return &dependencyCommitBoundaryRows{columns: []string{"issue_type"}, values: [][]driver.Value{{"task"}}}, nil
|
|
// The remove path reads the edge's metadata alongside its type: the events
|
|
// journal records the removed edge's full payload so a consumer can replay
|
|
// it (bd-opisf). The add path's existence probe still selects type alone.
|
|
case strings.Contains(query, "SELECT type, metadata FROM dependencies"),
|
|
strings.Contains(query, "SELECT type, metadata FROM wisp_dependencies"):
|
|
if c.driver.newEdge {
|
|
return &dependencyCommitBoundaryRows{columns: []string{"type", "metadata"}}, nil
|
|
}
|
|
return &dependencyCommitBoundaryRows{
|
|
columns: []string{"type", "metadata"},
|
|
values: [][]driver.Value{{"related", "{}"}},
|
|
}, nil
|
|
case strings.Contains(query, "SELECT type FROM dependencies"),
|
|
strings.Contains(query, "SELECT type FROM wisp_dependencies"):
|
|
if c.driver.newEdge {
|
|
return &dependencyCommitBoundaryRows{columns: []string{"type"}}, nil
|
|
}
|
|
return &dependencyCommitBoundaryRows{columns: []string{"type"}, values: [][]driver.Value{{"related"}}}, nil
|
|
default:
|
|
return &dependencyCommitBoundaryRows{columns: []string{"value"}}, nil
|
|
}
|
|
}
|
|
|
|
func (c *dependencyCommitBoundaryConn) ExecContext(_ context.Context, query string, _ []driver.NamedValue) (driver.Result, error) {
|
|
c.driver.mu.Lock()
|
|
defer c.driver.mu.Unlock()
|
|
if strings.Contains(query, "DELETE FROM dependencies") || strings.Contains(query, "DELETE FROM wisp_dependencies") {
|
|
c.driver.deletes++
|
|
}
|
|
if strings.Contains(query, "INSERT INTO wisp_dependencies") {
|
|
c.driver.inserts++
|
|
}
|
|
return driver.RowsAffected(1), nil
|
|
}
|
|
|
|
type dependencyCommitBoundaryTx struct {
|
|
driver *dependencyCommitBoundaryDriver
|
|
}
|
|
|
|
func (t *dependencyCommitBoundaryTx) Commit() error {
|
|
t.driver.mu.Lock()
|
|
defer t.driver.mu.Unlock()
|
|
t.driver.commitCalls++
|
|
return t.driver.commitErr
|
|
}
|
|
|
|
func (t *dependencyCommitBoundaryTx) Rollback() error { return nil }
|
|
|
|
type dependencyCommitBoundaryRows struct {
|
|
columns []string
|
|
values [][]driver.Value
|
|
index int
|
|
}
|
|
|
|
func (r *dependencyCommitBoundaryRows) Columns() []string { return r.columns }
|
|
func (r *dependencyCommitBoundaryRows) Close() error { return nil }
|
|
func (r *dependencyCommitBoundaryRows) Next(dest []driver.Value) error {
|
|
if r.index >= len(r.values) {
|
|
return io.EOF
|
|
}
|
|
copy(dest, r.values[r.index])
|
|
r.index++
|
|
return nil
|
|
}
|
|
|
|
func TestRemoveDependencySQLCommitResponseLossIsIndeterminateAndNotReplayed(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
activeWisp bool
|
|
}{
|
|
{name: "regular"},
|
|
{name: "wisp", activeWisp: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv("BEADS_TEST_MODE", "")
|
|
breaker := newTestCircuitBreaker(t)
|
|
driver := &dependencyCommitBoundaryDriver{
|
|
activeWisp: tc.activeWisp,
|
|
commitErr: testConnectionLoss,
|
|
}
|
|
store := &DoltStore{db: sql.OpenDB(driver), breaker: breaker}
|
|
t.Cleanup(func() { _ = store.db.Close() })
|
|
|
|
err := store.RemoveDependency(context.Background(), "dependency-source", "dependency-target", "alice")
|
|
if !errors.Is(err, ErrCommitIndeterminate) {
|
|
t.Fatalf("RemoveDependency() error = %v, want ErrCommitIndeterminate", err)
|
|
}
|
|
if !errors.Is(err, testConnectionLoss) {
|
|
t.Fatalf("RemoveDependency() error = %v, want cause %v", err, testConnectionLoss)
|
|
}
|
|
|
|
driver.mu.Lock()
|
|
defer driver.mu.Unlock()
|
|
if driver.deletes != 1 {
|
|
t.Fatalf("dependency delete attempts = %d, want 1", driver.deletes)
|
|
}
|
|
if driver.txAttempts != 1 || driver.commitCalls != 1 {
|
|
t.Fatalf("transaction attempts = %d, commit calls = %d, want 1 and 1", driver.txAttempts, driver.commitCalls)
|
|
}
|
|
|
|
state := breaker.readState()
|
|
if state.State != circuitClosed || state.Failures != 1 {
|
|
t.Fatalf("circuit state after one lost response = %+v, want closed with one failure", state)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAddExplicitIDWispDependencyIndeterminateCommitTripsCircuitBeforeNextWrite(t *testing.T) {
|
|
t.Setenv("BEADS_TEST_MODE", "")
|
|
breaker := newTestCircuitBreaker(t)
|
|
for range circuitFailureThreshold - 1 {
|
|
breaker.RecordFailure()
|
|
}
|
|
driver := &dependencyCommitBoundaryDriver{
|
|
activeWisp: true,
|
|
commitErr: testConnectionLoss,
|
|
newEdge: true,
|
|
}
|
|
store := &DoltStore{db: sql.OpenDB(driver), breaker: breaker}
|
|
t.Cleanup(func() { _ = store.db.Close() })
|
|
|
|
dep := &types.Dependency{
|
|
IssueID: "explicit-source",
|
|
DependsOnID: "explicit-target",
|
|
Type: types.DepRelated,
|
|
}
|
|
err := store.AddDependency(t.Context(), dep, "alice")
|
|
if !errors.Is(err, ErrCommitIndeterminate) {
|
|
t.Fatalf("AddDependency() error = %v, want ErrCommitIndeterminate", err)
|
|
}
|
|
if !errors.Is(err, testConnectionLoss) {
|
|
t.Fatalf("AddDependency() error = %v, want cause %v", err, testConnectionLoss)
|
|
}
|
|
if state := breaker.State(); state != circuitOpen {
|
|
t.Fatalf("circuit state after indeterminate wisp dependency commit = %q, want %q", state, circuitOpen)
|
|
}
|
|
|
|
driver.mu.Lock()
|
|
beforeTx := driver.txAttempts
|
|
beforeInserts := driver.inserts
|
|
commitCalls := driver.commitCalls
|
|
driver.mu.Unlock()
|
|
if beforeTx != 1 || commitCalls != 1 || beforeInserts != 1 {
|
|
t.Fatalf("first write attempts = transactions:%d commits:%d inserts:%d, want 1 each",
|
|
beforeTx, commitCalls, beforeInserts)
|
|
}
|
|
|
|
err = store.AddDependency(t.Context(), dep, "alice")
|
|
if !errors.Is(err, ErrCircuitOpen) {
|
|
t.Fatalf("next AddDependency() error = %v, want ErrCircuitOpen", err)
|
|
}
|
|
driver.mu.Lock()
|
|
afterTx := driver.txAttempts
|
|
afterInserts := driver.inserts
|
|
driver.mu.Unlock()
|
|
if afterTx != beforeTx || afterInserts != beforeInserts {
|
|
t.Fatalf("writes after circuit trip = transactions:%d inserts:%d, want unchanged %d and %d",
|
|
afterTx, afterInserts, beforeTx, beforeInserts)
|
|
}
|
|
}
|
|
|
|
var _ driver.Connector = (*dependencyCommitBoundaryDriver)(nil)
|
|
var _ driver.ExecerContext = (*dependencyCommitBoundaryConn)(nil)
|
|
var _ driver.QueryerContext = (*dependencyCommitBoundaryConn)(nil)
|