beads/internal/storage/embeddeddolt/dependencies.go
Jim Wordelman 331bc3aed2
perf(storage): amortize per-ID wisp routing via WispIDSetInTx (#3453)
* perf(storage): amortize per-ID wisp routing via WispIDSetInTx

Implements D2 build per ADR be-nu4 §4.D2 (bead be-nu4.2.1).

Add WispIDSetInTx helper that returns the set of currently-active
wisp IDs, consistent for the tx's lifetime under Dolt MVCC. The hot
batch-routing paths (GetLabelsForIssuesInTx, GetIssuesByIDsInTx)
gain a trailing wispSet parameter; callers that pass nil trigger a
single internal SELECT id FROM wisps rather than the N-per-ID
IsActiveWispInTx loop the helpers previously ran.

IsActiveWispInTx is retained unchanged for single-ID callers
(AddLabelInTx etc.) per the ADR audit note.

Mixed-ID routing is the sacred invariant (ADR §11.5); covered by a
new hard-gate test TestWispIDSetInTx_HardGate that exercises both
refactored helpers with a permanent issue and an active wisp in the
same input slice. Pure-fn unit coverage (partitionByWispSet) runs
without a testcontainer.

Adds BenchmarkGetLabelsForIssues_Mixed and
BenchmarkGetIssuesByIDs_Mixed at 1K / 10K / 50K with 25% wisp share.

Note for reviewer: two additional per-ID hot loops share the same
pattern (GetDependencyRecordsForIssuesInTx,
GetBlockingInfoForIssuesInTx in dependency_queries.go) and would
benefit from the same treatment. Left alone here to respect ADR
scope; flagged for a follow-up bead.

Dolt testcontainer runs flaky on this worktree (Docker 29.4 + ryuk
0.13.0). TESTCONTAINERS_RYUK_DISABLED=true works when infra
cooperates; unit-level coverage (TestPartitionByWispSet) runs
container-free and is green.

Refs: be-nu4.2.1, be-nu4

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore: release gate PASS for be-eqw

* fix(storage): scope WispIDSetInTx to input IDs

Reviewer maphew on #3453 flagged that the new default helper builds
the active wisp set with an unscoped SELECT id FROM wisps for
nil-set callers. That turns a small hydration batch (N IDs) into a
full scan of the wisps table (W rows), which is worse than the
pre-PR per-ID IsActiveWispInTx path for small N against large W.

Scope the helper query to the input IDs so cost becomes O(N·log W)
+ 1 round-trip — strictly better than both the old per-ID path and
the current unscoped path.

Signature change: WispIDSetInTx now takes ids []string. Both
internal call sites (GetIssuesByIDsInTx, GetLabelsForIssuesInTx)
pass their input IDs through; all 13 existing nil-set callers
continue to pass nil and get a scoped internal build. Doc updated
on the wispSet parameter to reflect "set scoped to cover these
IDs."

Note: committed with --no-verify (authorized) because release/be-eqw
inherits a pre-existing pre-commit hook blocker — sanitizeDBName is
only defined in the cgo-tagged store_factory.go and the hook runs the
linter under CGO_ENABLED=0. be-4lf (on main) deletes the duplicate
extraction file; once release/be-eqw rebases onto main post-merge the
hook passes naturally. Green-field build/vet/gofmt and all wisp
routing tests were run locally before committing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(storage): be-rgm WispIDSetInTx scoped path + small-N bench

be-3l2 fills the codecov gap on PR #3453 flagged by maphew:
WispIDSetInTx scope-to-input-IDs (14 missing lines on
wisp_routing.go, 0% on dependencies.go/labels.go hydration paths).

New tests in internal/storage/dolt/wisp_set_scoped_test.go:
- TestWispIDSetInTx_Scoped_ReturnsOnlyInputIDs — "lock the fix"
  regression: seeds 5 wisps, passes 1 ID, asserts the other 4
  are NOT in the set. Reverting to `SELECT id FROM wisps` fails this.
- TestWispIDSetInTx_Scoped_AcrossBatchBoundary — 250 wisps (past
  queryBatchSize=200) all pass through the batched IN-clause loop.
- TestWispIDSetInTx_Scoped_UnknownIDsReturnEmpty — unknown IDs
  return empty set; a bait wisp outside the input must not leak.
- TestGetLabelsForIssuesInTx_SmallInputLargeWispTable — end-to-end
  hydration with nil wispSet against 20 noise wisps; exercises the
  dependencies.go/labels.go callers that pass input IDs through.
- TestGetIssuesByIDsInTx_SmallInputLargeWispTable — same shape for
  issue hydration.

New benches in internal/storage/dolt/dolt_benchmark_test.go:
- BenchmarkGetLabelsForIssues_SmallNLargeW_10_5K / _10_10K / _100_5K
- BenchmarkGetIssuesByIDs_SmallNLargeW_10_5K / _10_10K / _100_5K
These measure the maphew regression shape: small input batch
against a large wisps table. Local 10_5K: ~2.6 ms/op, confirming
the scoped N·log(W) path wins cleanly over the pre-be-rgm
full-scan path (existing Mixed10K is ~1.35 s/op for the full
hydration, but that loads 10K not 10).

Note: committed with --no-verify (authorized). release/be-eqw
inherits a pre-existing pre-commit hook blocker — golangci-lint
on the hook is built with go1.25 while go.mod targets 1.26.2,
so the lint step errors before seeing the diff. Green-field
go vet, go build, and all five new tests pass locally against
an isolated Dolt test container.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 13:39:42 -07:00

72 lines
2.5 KiB
Go

//go:build cgo
package embeddeddolt
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
func (s *EmbeddedDoltStore) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error {
return s.withConn(ctx, true, func(tx *sql.Tx) error {
return issueops.AddDependencyInTx(ctx, tx, dep, actor, issueops.AddDependencyOpts{
IsCrossPrefix: types.ExtractPrefix(dep.IssueID) != types.ExtractPrefix(dep.DependsOnID),
})
})
}
// RemoveDependency removes a dependency between two issues.
func (s *EmbeddedDoltStore) RemoveDependency(ctx context.Context, issueID, dependsOnID string, actor string) error {
return s.withConn(ctx, true, func(tx *sql.Tx) error {
return issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID)
})
}
// GetIssuesByIDs retrieves multiple issues by ID.
func (s *EmbeddedDoltStore) GetIssuesByIDs(ctx context.Context, ids []string) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetIssuesByIDsInTx(ctx, tx, ids, nil)
return err
})
return result, err
}
// GetDependenciesWithMetadata returns issues that the given issue depends on,
// along with the dependency type.
func (s *EmbeddedDoltStore) GetDependenciesWithMetadata(ctx context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
var result []*types.IssueWithDependencyMetadata
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependenciesWithMetadataInTx(ctx, tx, issueID)
return err
})
return result, err
}
// GetDependentsWithMetadata returns issues that depend on the given issue,
// along with the dependency type.
func (s *EmbeddedDoltStore) GetDependentsWithMetadata(ctx context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
var result []*types.IssueWithDependencyMetadata
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependentsWithMetadataInTx(ctx, tx, issueID)
return err
})
return result, err
}
// DetectCycles finds dependency cycles across both permanent and wisp dependencies.
func (s *EmbeddedDoltStore) DetectCycles(ctx context.Context) ([][]*types.Issue, error) {
var result [][]*types.Issue
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.DetectCyclesInTx(ctx, tx)
return err
})
return result, err
}