test(proxy): topology-parameterized history and remote front-door matrix (#6269)
* feat(ga-p9iuv.22): add shared tracker proxy storage seam
* fix(ga-p9iuv.22): preserve tracker storage capabilities safely
* test(ga-p9iuv.22): cover tracker adapter label semantics
* test(ga-p9iuv.22): harden tracker adapter contracts
* fix(ga-p9iuv.22): reject invalid proxied tracker stores
* feat(ga-p9iuv.1): route Linear and Jira front doors through proxy store
* test(ga-p9iuv.1): exercise tracker mutations through UOW store
* fix(ga-p9iuv.1): preserve proxied Linear endpoint config
* test(ga-p9iuv.1): cover proxied Linear endpoint and direct config
* feat(ga-p9iuv.19): add typed proxied capability matrix
* feat(proxy): enforce typed capability matrix before provider
* test(proxy): cover command capability rows and claim caps
* test(proxy): check capability rows across topologies
* fix(proxy): validate capabilities before workspace side effects
* fix(proxy): refuse ready claim max rows caps
* feat(proxy): add shared maintenance capability policy
* fix(proxy): key maintenance refusals by command path
* test(proxy): cover nested maintenance refusals
* fix(proxy): enumerate nested maintenance command paths
* fix(proxy): refine compact and nested maintenance policy
* test(proxy): prove maintenance refusals preserve files
* fix(proxy): cover admin and remote maintenance refusals
* fix(proxy): refuse doctor before store initialization
* test(proxy): define history capability command matrix
* fix(proxy): wire history capability refusals to front door
* fix(proxy): refuse top-level sync history path
* fix(proxy): preserve maintenance refusal codes
* feat(proxy): classify history as supported UOW API
* feat(proxy): classify transform capability paths
* test(proxy): harden transform capability matrix
* fix(proxy): drive transform validation from exact rows
* fix(proxy): drive transform matrix from normalized flags
* fix(proxy): bound external backend dials per attempt
* test(proxy): cover bounded blocked backend dial
* fix(proxy): make transform capability policy row-driven
* fix(proxy): make transform capability policy row-driven
* fix(proxy): classify formula swarm and merge-slot refusals
* test(proxy): enforce workflow refusal contract
* fix(proxy): classify Dolt remote history boundaries
* test(proxy): enforce history refusal contract
* test(proxy): cover canceled backend dial cleanup
* test(proxy): cover real TCP blackhole cancellation
* test(proxy): cover external transport lifecycle matrix
* test(proxy): assert stable dial failure diagnostics
* test(proxy): add outage reconnect acceptance matrix
* test(proxy): make unix outage bridge restartable
* test(proxy): bound outage subprocess cleanup
* test(proxy): cover upstream restart with retained endpoint bridge
* test(proxy): stabilize managed-local outage contract
* fix(proxy): retire managed proxy when backend exits
* test(proxy): accept managed child retirement restart
* test(proxy): cover workflow refusals at bd front door
* test(proxy): cover history VC front doors
* test(proxy): pass valid workflow refusal arguments
* test(proxy): assert text and json refusal contracts
* test(proxy): assert exact workflow refusal codes
* test(proxy): guard refusal issue-row stability
* fix(proxy): expose non-mutating capability refusals
* test(proxy): match stable refusal text
* test(proxy): compare refusal artifact snapshots
* test(proxy): assert exact refusal messages
* test(proxy): retain direct workflow behavior
* test(proxy): match nested refusal messages
* test(proxy): cover direct-only front-door refusals
* test(proxy): provide valid direct-only command args
* fix(migrate): repair missing proxied sidecar on reentry
* fix: checkpoint Dolt mode migrations for safe retry
* fix: harden migration journal topology and retries
* fix: fsync migration journal removal
* fix: fail on migration journal directory sync errors
* fix: reject external ownership in every migration phase
* fix: enforce canonical shared migration topology
* fix: enforce live migration topology
* test: add migration front-door and topology regressions
* fix: require physical Dolt roots for migration
* test: strengthen migration front-door contracts
* fix: reject missing or invalid Dolt migration roots
* fix: keep shared migration dry-runs side-effect free
* fix: require Dolt repository identity for migrations
* fix: validate Dolt repository identity metadata
* fix: require typed Dolt repo state identity
* test: expand migration front-door CRUD assertions
* test: make migration JSON dry-run front-door safe
* test: sanitize Dolt environment for migration front door
* test: use cgo binary for migration front door
* test: omit empty Dolt overrides in migration front door
* test: isolate migration front door from test mode
* test: isolate home for migration front door
* test: stop supervised Dolt before reverse migration
* fix: reject empty Dolt head refs
* test: harden migration topology invariants
* test: harden external and shared topology refusals
* test: snapshot migration refusal artifacts
* test: cover external migration refusal phases
* fix: refuse stale proxied sidecars during migration
* test: strengthen migration artifact and external front door checks
* feat: type external migration refusals
* test: assert external refusal starts no proxy
* fix: validate migration journal sidecar topology
* fix: make sidecar recovery phase aware
* fix: honor persisted shared topology during migration
* test: exercise external journal migration front door matrix
* test(tracker): add shared conformance harness
* test: harden external migration matrix fixtures
* test: preserve durable dolt gate lock in matrix
* test: preseed gate lock for refusal matrix
* test: cover malformed migration state front door
* fix: type malformed migration config refusals
* fix: type migration preflight state errors
* test: separate migration JSON stdout from warnings
* test: reach typed malformed journal refusal
* test: assert migration retry phase and idempotence
* test: cover shared migration checkpoint retries
* fix: make reverse migration idempotent without sidecar
* test: cover proxied to shared retries
* fix: derive shared topology from persisted YAML
* fix: validate shared migration no-op state
* fix: allow completed proxied shared no-op
* fix: validate forward proxied no-op sidecar
* test: reject bad already-proxied sidecars
* fix: reject stale proxy controls on no-op
* fix: fail closed on malformed migration workspace yaml
* test: pin migration yaml topology precedence
* Cover forward migration no-op ownership refusals (ga-p9iuv.26.5.2)
Agent-Signature: unknown-model/unknown-reasoning
* Strengthen migration retry sentinel coverage (ga-p9iuv.26.1)
Agent-Signature: unknown-model/unknown-reasoning
* Expand migration fault retry artifact coverage (ga-p9iuv.26.1)
Agent-Signature: unknown-model/unknown-reasoning
* Add reverse migration frontdoor fault matrix (ga-p9iuv.26.1.1)
Agent-Signature: unknown-model/unknown-reasoning
* test: cover shared migration frontdoor fault matrices
* test: strengthen direct migration matrix assertions
* test: preserve migration dependency edges
* test: assert exact migration dependency edges
* test: parse migration dependency edges from batch list
* test: fix shared migration matrix compile
* test: verify shared dependencies before shutdown
* test(proxy): cover history remote front-door topology matrix
* docs: define direct-local ownership handoff contract
* fix(ci): repair pre-review CI failures on proxy parity branch
Five PR-introduced failures across the docs, PR Core, and Proxied Dolt Cmd
lanes. Each fix is scoped to the evidenced defect.
- test/docsync: TestEveryDocsPageIsPublished flagged
docs/architecture/ownership-handoff-contract.md as unpublished. It is an
internal Beads/Gas City handoff contract with no Mintlify frontmatter and
no inbound links, so move it to engdocs/design/ beside its peers rather
than publishing it.
- cmd/bd: TestProxyWorkflowRefusalContractAndNoMutation asserted on
commandDidWrite, a process-wide atomic latch set by 71 non-test sites and
never reset, so it reported an earlier test's write. Baseline and restore
it around the assertion, matching the save/reset/restore idiom already
used in proxied_lookup_failure_test.go and readonly_policy_test.go.
- cmd/bd: the new pre-provider gate short-circuited compact.go's refusal
with new wording, breaking TestProxiedServerCompactDolt/non_dolt_mode_-
rejected in an unchanged test. Restore the long-standing message; the
proxy.compact.unsupported code is unchanged.
- internal/storage/dbproxy/proxy: the unix subtests bound sockets under
t.TempDir(), whose embedded test name overflows macOS's ~104-byte
sun_path limit ("bind: invalid argument"). Add unixSocketPath() to place
them in a short temp dir.
- cmd/bd: migration topology compared recorded and canonical Dolt roots
lexically, so macOS's /var -> /private/var symlink made equivalent paths
compare unequal. Add sameMigrationRoot(), mirroring the existing
resolveExistingPrefix idiom in cmd/bd/doctor/git.go.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(review): address blocker and major findings from adopt-pr review
Maintainer review fixup for the proxy parity branch, iterations 1-2.
Blockers and majors from the review synthesis and quality scorecard.
Blockers:
- cmd/bd/migrate_dolt_mode.go: resuming from migratePrepared rebuilt the
migration plan from the current argv instead of replaying the recorded
one. A resume that omitted or changed --idle-timeout wrote a sidecar the
target_configured check then rejected, and because that check runs only
after the phase advances past migratePrepared, the block never ran again
— the workspace stayed wedged in proxied-server mode with no in-tool
recovery. Replay j.Sidecar and warn when the supplied value is ignored.
- internal/tracker/store.go: NewStore double-wrapped already-adapted values.
directStore embeds the storage.Storage interface, so *directStore also
satisfies storage.Storage and was re-wrapped as
&directStore{Storage: &directStore{...}}; the inner value can never
satisfy storage.IssueLifecycleStore, which silently degraded
ApplyIssueUpdate to a bare UpdateIssue — no labels, no lifecycle
transaction. Match the tracker's own adapters by concrete type first.
Testing value.(Store) first, as the review suggested, does not work:
storage.Storage is a strict superset of Store, so a raw direct store
satisfies Store too and would be returned un-adapted, relocating the same
bug. The branch order is documented at the call site.
Majors:
- internal/tracker/store.go: directStore.ApplyIssueUpdate now calls
syncIssueLabels, which normalizes both label sets (trim, drop empty) and
restores the semantics the pull path had before this seam moved behind
ApplyIssueUpdate. The un-normalized local copy churned remove+add on
whitespace-only differences and let an empty label reach AddLabel; it is
deleted rather than left as a second, divergent implementation.
- Both regressions were invisible to the suite. Adds
TestNewEngineKeepsDirectStoreAdaptationIntact, TestNewStoreIsIdempotent,
TestDirectStoreApplyIssueUpdateNormalizesLabels, and
TestMigrateToProxiedServer_ResumeReplaysRecordedIdleTimeout (changed /
omitted / never subtests, each also asserting the reverse-migration escape
hatch still recovers). All four were verified to fail against the
pre-fix production code with the predicted symptoms.
Non-gating follow-ups from the scorecard, fixed while nearby:
- cmd/bd/proxy_capability.go: treat ProxyOutcomeNA as "this command has no
such flag" rather than a refusal, so an assert on an N/A row cannot return
a non-nil error carrying an empty message.
- cmd/bd/errors.go: emit the rule's real Mutates value in the JSON refusal
envelope instead of a hardcoded false.
- cmd/bd/main.go: the doctor pre-provider check returned unconditionally,
so a nil result (every doctor subcommand) skipped the legacy-store guard
and autocommit-mode resolution that other skipsStoreInit commands run.
Return only on a real refusal.
- internal/tracker/engine.go: type NewEngine's store parameter as Store so
the seam names its contract, and drop the now-dead applyPullIssueUpdate.
- CHANGELOG.md: record the proxied `bd ready --claim` row-cap refusal, which
was a behavior change shipped without a release note.
Iteration 2 -- gating major (uowStore/directStore external_ref divergence):
- internal/tracker/uow_store.go: GetIssueByExternalRef resolved through a
single merged search over both storage planes, which has no plane
preference: it is a UNION ALL ordered by content and terminating in
`id ASC`, so one external_ref present on both planes resolved to whichever
row's id sorted first. The direct backend is explicitly issues-first --
issueops.GetIssueByExternalRefInTx queries `issues` and falls through to
`wisps` only on sql.ErrNoRows, and says why ("so that pushed ephemeral
beads are found during pull dedup"). The consumer is the pull dedup, which
updates whatever row comes back, so the divergence was a silent write to
the wrong local bead under --proxied-server only. Query the issues plane
first via SkipWisps, then fall back.
The fallback is the merged search rather than the review's literal
Ephemeral=true: that filter contributes an `ephemeral = 1` predicate the
direct backend's `SELECT id FROM wisps WHERE external_ref = ?` does not
have, and wisp-plane rows legitimately carry ephemeral=0 (NoHistory beads;
see types.IssueFilter.EphemeralTier). Taking it literally would have traded
one divergence for a narrower one. Verified: implementing the prescription
fails the ephemeral_flag_clear subtest below.
- internal/tracker/uow_store_engine_test.go: the engine fake now models plane
routing (SkipWisps / Ephemeral / merged) instead of one flat map, so the
contract is expressible at all. Adds
TestUOWStoreGetIssueByExternalRefPrefersIssuePlane, whose wisp id sorts
first so "first merged hit" fails it, and
TestUOWStoreGetIssueByExternalRefFallsBackToWispPlane over both ephemeral
values. Verified to fail against the pre-fix uow_store.go.
- internal/tracker/conformance: the shared suite gains a Wisps plane and
external_ref_resolution_prefers_issue_plane, so this is a contract both
backends are held to rather than one adapter's unit test.
Iteration 2 -- non-gating follow-ups from the scorecard:
- cmd/bd/migrate_dolt_mode.go: scope the migratePrepared sidecar-equivalence
check in runMigrateFromProxiedServer to reverse journals. A forward journal
at that phase records the sidecar it is about to write, so a crash in the
pre-write window leaves none on disk -- the correct state -- and the
unscoped check refused the documented escape hatch by naming a missing
file, stranding the workspace. Tested against TargetMode's forward value so
the guard stays live for both reverse targets, "server" and
"shared-server". Adds
TestMigrateFromProxiedServer_ForwardJournalAtPreparedIsNotSidecarRefused,
verified to fail against the unscoped guard.
- cmd/bd/linear.go: buildLinearClientAPIOnly returned the OAuth client inline
and never reached the WithEndpoint block, so a configured
linear.api_endpoint was honored for an API key and ignored for OAuth. Hoist
the endpoint application after the branch, as Tracker.Init does, and add
the OAuth mirror of the existing endpoint test.
- cmd/bd/ready_max_rows_proxied_test.go: pin the proxied --max-rows refusal
on `bd ready --claim`. It lives in RunE above the dispatch and nothing
below it can enforce a cap, but the existing coverage all sits below it.
Covers both cap sources (--max-rows and BEADS_MAX_ROWS) and the negative
case that the refusal is conditioned on a live cap, not on --claim.
- engdocs/design/ownership-handoff-contract.md: trim the trailing blank line
at EOF flagged by `git diff --check`.
Iteration 3 -- non-gating residual on the iteration-2 fix:
- internal/tracker/uow_store.go: the issues-first fix above split one logical
lookup across two read transactions. uow.RunTxRead opens a fresh unit of
work per call, so calling s.SearchIssues twice left the wisp fallback's
premise -- "the issues plane is already known empty" -- without a shared
snapshot: a concurrent same-ref insert into the issues plane landing between
the two reads could let the merged fallback return the wisp, re-creating the
wrong-row write that ordering exists to prevent. The consumer is the pull
dedup loop, so it runs per imported issue. Both plane reads now share a
single RunTxRead, which is also the shape the direct backend uses (both
planes inside one withReadTx). Plane ordering is unchanged.
- internal/tracker/uow_store_engine_test.go: the engine fake counts the units
of work it hands out, and
TestUOWStoreGetIssueByExternalRefUsesOneReadTransaction pins that count at 1
across the two paths that actually perform both reads -- the wisp fallback
and the miss. An issues-plane hit short-circuits after one read and would
pass either way, so it is not a case. Verified to fail against the
two-transaction version with "opened 2, want 1".
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
62884bb87d
commit
24a9da6cc7
55 changed files with 7103 additions and 256 deletions
|
|
@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Changed
|
||||
|
||||
- **`bd ready --claim` now refuses a row cap under `--proxied-server`.** The
|
||||
proxied ready role cannot enforce `--max-rows`/`BEADS_MAX_ROWS`, so bd fails
|
||||
loudly rather than silently dropping the limit; `--claim` no longer exempts
|
||||
the command. Agent rigs that set the cap globally must unset it for proxied
|
||||
`bd ready --claim`. Direct mode is unchanged, and a claim there still
|
||||
succeeds against a ready pool larger than the cap.
|
||||
|
||||
## [1.3.0] - 2026-08-28
|
||||
|
||||
The first tested release off `main` since the 1.1 line. [1.2.2] was a recovery
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ func resolveProxiedCustomTypes(dbTypes []string) []string {
|
|||
|
||||
func runCreateProxiedServer(cmd *cobra.Command, ctx context.Context, in createInput) error {
|
||||
if in.repoOverrideSet {
|
||||
return HandleError("--repo is not supported with --proxied-server")
|
||||
return HandleProxyCapabilityError(AssertProxyCapability(ProxyModeProxied, ProxyCapRepo))
|
||||
}
|
||||
switch {
|
||||
case in.graphFile != "":
|
||||
|
|
|
|||
|
|
@ -76,6 +76,36 @@ func buildJSONError(message, hint string) interface{} {
|
|||
return inner
|
||||
}
|
||||
|
||||
func buildJSONCapabilityError(e *ProxyCapabilityError) interface{} {
|
||||
inner := map[string]interface{}{"error": e.Message, "code": e.Code, "mutates": e.Mutates}
|
||||
if jsonEnvelopeEnabled() {
|
||||
return map[string]interface{}{"schema_version": JSONSchemaVersion, "data": inner}
|
||||
}
|
||||
inner["schema_version"] = JSONSchemaVersion
|
||||
return inner
|
||||
}
|
||||
|
||||
// HandleProxyCapabilityError renders a stable capability refusal while
|
||||
// preserving the normal text/JSON front-door conventions.
|
||||
func HandleProxyCapabilityError(err error) error {
|
||||
var capErr *ProxyCapabilityError
|
||||
if !errors.As(err, &capErr) {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
code := capErr.ExitCode
|
||||
if code == 0 {
|
||||
code = 1
|
||||
}
|
||||
if jsonOutput {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
_ = enc.Encode(buildJSONCapabilityError(capErr))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Error: %s\n", capErr.Message)
|
||||
}
|
||||
return &exitError{Code: code}
|
||||
}
|
||||
|
||||
func jsonStderrError(message, hint string) {
|
||||
encoder := json.NewEncoder(os.Stderr)
|
||||
encoder.SetIndent("", " ")
|
||||
|
|
|
|||
28
cmd/bd/history_capability.go
Normal file
28
cmd/bd/history_capability.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
// HistoryCapabilityClass classifies history/version-control commands on the
|
||||
// proxied topology without coupling them to a particular backend engine.
|
||||
type HistoryCapabilityClass string
|
||||
|
||||
const (
|
||||
HistoryProxySupported HistoryCapabilityClass = "proxy-supported"
|
||||
HistoryProxyAPI HistoryCapabilityClass = "proxy-api"
|
||||
HistoryDirectOnly HistoryCapabilityClass = "direct-only"
|
||||
)
|
||||
|
||||
var historyCapabilityMatrix = map[string]HistoryCapabilityClass{
|
||||
"history": HistoryProxySupported,
|
||||
"branch": HistoryDirectOnly, "conflicts": HistoryDirectOnly, "repo": HistoryDirectOnly,
|
||||
"federation": HistoryDirectOnly, "vc": HistoryDirectOnly, "flatten": HistoryDirectOnly,
|
||||
"dolt push": HistoryDirectOnly, "dolt pull": HistoryDirectOnly, "dolt commit": HistoryDirectOnly,
|
||||
"dolt remote": HistoryDirectOnly, "dolt remote add": HistoryDirectOnly, "dolt remote list": HistoryDirectOnly,
|
||||
"dolt remote reset-data": HistoryDirectOnly, "dolt remote remove": HistoryProxySupported,
|
||||
"sync": HistoryDirectOnly,
|
||||
}
|
||||
|
||||
// LookupHistoryCapability classifies an exact command path. Unknown paths are
|
||||
// intentionally absent so callers cannot accidentally advertise support.
|
||||
func LookupHistoryCapability(commandPath string) (HistoryCapabilityClass, bool) {
|
||||
c, ok := historyCapabilityMatrix[commandPath]
|
||||
return c, ok
|
||||
}
|
||||
138
cmd/bd/history_capability_test.go
Normal file
138
cmd/bd/history_capability_test.go
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
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"} {
|
||||
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)
|
||||
}
|
||||
for _, path := range []string{"dolt remote add", "dolt remote list", "dolt remote reset-data"} {
|
||||
if got, ok := LookupHistoryCapability(path); !ok || got != HistoryDirectOnly {
|
||||
t.Errorf("%q = %q, %v; want direct-only", path, got, ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncRefusesBeforeProvider(t *testing.T) {
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: "sync"}
|
||||
root.AddCommand(cmd)
|
||||
err := validateProxyMaintenanceBeforeProvider(cmd)
|
||||
if err == nil {
|
||||
t.Fatalf("sync refusal = %#v", err)
|
||||
}
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("sync exit = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryDirectOnlyRefusalContract(t *testing.T) {
|
||||
expected := map[string]struct{ code, message string }{
|
||||
"branch": {"proxy.branch.unsupported", "branch is not supported in proxied-server mode"},
|
||||
"conflicts": {"proxy.conflicts.unsupported", "conflicts is not supported in proxied-server mode"},
|
||||
"repo": {"proxy.repo.unsupported", "repo is not supported in proxied-server mode"},
|
||||
"federation": {"proxy.federation.unsupported", "federation is not supported in proxied-server mode"},
|
||||
"vc": {"proxy.vc.unsupported", "vc is not supported in proxied-server mode"},
|
||||
"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"},
|
||||
"sync": {"proxy.sync.unsupported", "sync is not supported in proxied-server mode"},
|
||||
}
|
||||
oldProvider := uowProvider
|
||||
oldJSON := jsonOutput
|
||||
t.Cleanup(func() { uowProvider = oldProvider; jsonOutput = oldJSON })
|
||||
for _, path := range []string{"branch", "conflicts", "repo", "federation", "vc", "flatten", "dolt push", "dolt pull", "dolt commit", "dolt remote add", "sync"} {
|
||||
parts := strings.Split(path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: parts[0]}
|
||||
root.AddCommand(cmd)
|
||||
for _, part := range parts[1:] {
|
||||
child := &cobra.Command{Use: part}
|
||||
cmd.AddCommand(child)
|
||||
cmd = child
|
||||
}
|
||||
uowProvider = nil
|
||||
jsonOutput = true
|
||||
out := captureStdout(t, func() error { _ = validateProxyMaintenanceBeforeProvider(cmd); return nil })
|
||||
var got map[string]any
|
||||
want := expected[path]
|
||||
if err := json.Unmarshal([]byte(out), &got); err != nil || got["code"] != want.code || got["error"] != want.message || got["mutates"] != false {
|
||||
t.Fatalf("%s refusal = %q (%v)", path, out, err)
|
||||
}
|
||||
if uowProvider != nil {
|
||||
t.Fatal("refusal initialized provider")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryNestedFrontDoorsRefuseAndSupportedPathsPass(t *testing.T) {
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
expected := map[string]struct{ code, message string }{
|
||||
"vc merge": {"proxy.vc.unsupported", "vc merge is not supported in proxied-server mode"},
|
||||
"vc commit": {"proxy.vc.unsupported", "vc commit is not supported in proxied-server mode"},
|
||||
"repo add": {"proxy.repo.unsupported", "repo add is not supported in proxied-server mode"},
|
||||
"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"} {
|
||||
parts := strings.Split(path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: parts[0]}
|
||||
root.AddCommand(cmd)
|
||||
for _, part := range parts[1:] {
|
||||
child := &cobra.Command{Use: part}
|
||||
cmd.AddCommand(child)
|
||||
cmd = child
|
||||
}
|
||||
err := validateProxyMaintenanceBeforeProvider(cmd)
|
||||
if path == "dolt remote remove" {
|
||||
if err != nil {
|
||||
t.Fatalf("supported %s refused: %v", path, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("direct-only %s unexpectedly allowed", path)
|
||||
}
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("%s exit=%v, want 1", path, err)
|
||||
}
|
||||
var got map[string]any
|
||||
// validateProxyMaintenanceBeforeProvider renders the typed refusal to
|
||||
// stdout in JSON mode; the command must retain its nested path.
|
||||
out := captureStdout(t, func() error { _ = validateProxyMaintenanceBeforeProvider(cmd); return nil })
|
||||
if err := json.Unmarshal([]byte(out), &got); err != nil {
|
||||
t.Fatalf("%s refusal JSON: %v (%q)", path, err, out)
|
||||
}
|
||||
want := expected[path]
|
||||
if got["code"] != want.code || got["error"] != want.message || got["mutates"] != false {
|
||||
t.Fatalf("%s refusal = %#v, want code=%q message=%q mutates=false", path, got, want.code, want.message)
|
||||
}
|
||||
}
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
history := &cobra.Command{Use: "history"}
|
||||
root.AddCommand(history)
|
||||
if err := validateProxyMaintenanceBeforeProvider(history); err != nil {
|
||||
t.Fatalf("history --events supported path refused: %v", err)
|
||||
}
|
||||
}
|
||||
167
cmd/bd/history_remote_frontdoor_matrix_test.go
Normal file
167
cmd/bd/history_remote_frontdoor_matrix_test.go
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
//go:build cgo && unix
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
)
|
||||
|
||||
// TestHistoryRemoteRefusalFrontDoorMatrix runs the real bd binary through all
|
||||
// supported proxied transports. Valid command arguments are intentional: a
|
||||
// typed capability refusal must win over Cobra usage validation and must not
|
||||
// start a provider or touch durable state.
|
||||
func TestHistoryRemoteRefusalFrontDoorMatrix(t *testing.T) {
|
||||
bd := buildEmbeddedBD(t)
|
||||
type fixture struct {
|
||||
name string
|
||||
make func(*testing.T) proxiedProject
|
||||
}
|
||||
fixtures := []fixture{{
|
||||
name: "managed-local",
|
||||
make: func(t *testing.T) proxiedProject {
|
||||
requireManagedLocalProxiedEnv(t)
|
||||
return bdManagedLocalInit(t, bd, "hm_managed", 5*time.Minute)
|
||||
},
|
||||
}}
|
||||
if os.Getenv("BEADS_TEST_PROXIED_SERVER") == "1" {
|
||||
fixtures = append(fixtures,
|
||||
fixture{name: "external-tcp", make: func(t *testing.T) proxiedProject {
|
||||
return newSharedProxiedProject(t, bd, "hm_tcp")
|
||||
}},
|
||||
fixture{name: "external-unix", make: func(t *testing.T) proxiedProject {
|
||||
requireProxiedServerEnv(t)
|
||||
upstream := testutil.StartIsolatedDoltContainerHandle(t)
|
||||
socket := filepath.Join(t.TempDir(), "dolt.sock")
|
||||
bridge := startOutageBridge(t, socket, upstream.Port, true)
|
||||
t.Cleanup(func() { _ = bridge.Process.Kill() })
|
||||
return bdProxiedInit(t, bd, "hm_unix", "--proxied-server-external-socket-path", socket)
|
||||
}},
|
||||
)
|
||||
}
|
||||
commands := []struct {
|
||||
name, code, message string
|
||||
args []string
|
||||
}{
|
||||
{"branch", "proxy.branch.unsupported", "branch is not supported in proxied-server mode", []string{"branch", "feature"}},
|
||||
{"conflicts list", "proxy.conflicts.unsupported", "conflicts list is not supported in proxied-server mode", []string{"conflicts", "list"}},
|
||||
{"repo add", "proxy.repo.unsupported", "repo add is not supported in proxied-server mode", []string{"repo", "add", "."}},
|
||||
{"federation sync", "proxy.federation.unsupported", "federation sync is not supported in proxied-server mode", []string{"federation", "sync"}},
|
||||
{"vc merge", "proxy.vc.unsupported", "vc merge is not supported in proxied-server mode", []string{"vc", "merge", "feature"}},
|
||||
{"vc commit", "proxy.vc.unsupported", "vc commit is not supported in proxied-server mode", []string{"vc", "commit"}},
|
||||
{"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"}},
|
||||
{"sync", "proxy.sync.unsupported", "sync is not supported in proxied-server mode", []string{"sync"}},
|
||||
}
|
||||
for _, f := range fixtures {
|
||||
t.Run(f.name, func(t *testing.T) {
|
||||
p := f.make(t)
|
||||
bdProxiedCreate(t, bd, p.dir, "matrix sentinel")
|
||||
_ = proxy.Shutdown(p.proxyRoot)
|
||||
before := snapshotHistoryArtifacts(t, p)
|
||||
for _, tc := range commands {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Put the global flag before the subcommand. A few nested
|
||||
// front doors do not inherit persistent flags after their args.
|
||||
args := append([]string{"--json"}, tc.args...)
|
||||
stdout, stderr, err := bdProxiedRunBuffersWithEnv(t, bd, p.dir, []string{"BEADS_JSON=1"}, args...)
|
||||
if err == nil {
|
||||
t.Fatalf("%v unexpectedly succeeded", tc.args)
|
||||
}
|
||||
var got struct {
|
||||
Code string `json:"code"`
|
||||
Error string `json:"error"`
|
||||
Mutates bool `json:"mutates"`
|
||||
}
|
||||
if jsonErr := json.Unmarshal([]byte(strings.TrimSpace(stdout)), &got); jsonErr == nil {
|
||||
if got.Code != tc.code || got.Error != tc.message || got.Mutates {
|
||||
t.Fatalf("refusal=%+v, want code=%q error=%q mutates=false", got, tc.code, tc.message)
|
||||
}
|
||||
} else if strings.TrimSpace(stderr) != "Error: "+tc.message {
|
||||
// Some nested commands render the same typed refusal through
|
||||
// the text path before config-backed JSON is applied.
|
||||
t.Fatalf("refusal JSON=%v stdout=%q stderr=%q", jsonErr, stdout, stderr)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(p.proxyRoot, proxy.PIDFileName)); statErr == nil {
|
||||
t.Fatal("provider proxy started for direct-only refusal")
|
||||
}
|
||||
after := snapshotHistoryArtifacts(t, p)
|
||||
if string(before) != string(after) {
|
||||
t.Fatal("durable artifacts changed during refusal")
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func snapshotHistoryArtifacts(t *testing.T, p proxiedProject) []byte {
|
||||
t.Helper()
|
||||
var out []byte
|
||||
for _, path := range []string{
|
||||
filepath.Join(p.beadsDir, "config.yaml"), filepath.Join(p.beadsDir, "metadata.json"),
|
||||
filepath.Join(p.beadsDir, "events.jsonl"), filepath.Join(p.beadsDir, ".local_version"),
|
||||
filepath.Join(p.proxyRoot, proxy.LockFileName), filepath.Join(p.proxyRoot, proxy.PIDFileName),
|
||||
} {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
t.Fatalf("snapshot %s: %v", path, err)
|
||||
}
|
||||
out = append(out, []byte(strconv.Itoa(len(path))+":"+path+":"+string(b)+"\n")...)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestHistoryRemoteSupportedFrontDoorParity checks the two operations that
|
||||
// are intentionally shared by direct and proxied providers.
|
||||
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")
|
||||
}
|
||||
bd := buildEmbeddedBD(t)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
make func(*testing.T) proxiedProject
|
||||
}{
|
||||
{"managed-local", func(t *testing.T) proxiedProject {
|
||||
requireManagedLocalProxiedEnv(t)
|
||||
return bdManagedLocalInit(t, bd, "hs_managed", 5*time.Minute)
|
||||
}},
|
||||
{"external-tcp", func(t *testing.T) proxiedProject {
|
||||
requireProxiedServerEnv(t)
|
||||
return newSharedProxiedProject(t, bd, "hs_tcp")
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := tc.make(t)
|
||||
issue := bdProxiedCreate(t, bd, p.dir, "history parity")
|
||||
bdProxiedUpdate(t, bd, p.dir, issue.ID, "--title", "history parity updated")
|
||||
out, err := bdProxiedRun(t, bd, p.dir, "--json", "history", issue.ID, "--events")
|
||||
if err != nil || !strings.Contains(string(out), issue.ID) {
|
||||
t.Fatalf("history --events failed: %v\n%s", err, out)
|
||||
}
|
||||
db := openProxiedDB(t, p)
|
||||
if _, err := db.ExecContext(context.Background(), "CALL DOLT_REMOTE('add', ?, ?)", "backup", "https://example.invalid/backup"); err != nil {
|
||||
t.Fatalf("seed remote: %v", err)
|
||||
}
|
||||
out, err = bdProxiedRun(t, bd, p.dir, "--json", "dolt", "remote", "remove", "backup")
|
||||
if err != nil || !strings.Contains(string(out), "backup") {
|
||||
t.Fatalf("remote remove failed: %v\n%s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -96,9 +96,6 @@ func init() {
|
|||
}
|
||||
|
||||
func runJiraSync(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("jira sync is not supported in proxied-server mode")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("jira-sync")
|
||||
defer func() {
|
||||
if c := metrics.Global(); c != nil {
|
||||
|
|
@ -122,11 +119,12 @@ func runJiraSync(cmd *cobra.Command, args []string) error {
|
|||
return HandleErrorRespectJSON("cannot use both --prefer-local and --prefer-jira")
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("database not available: %v", err)
|
||||
}
|
||||
|
||||
if err := validateJiraConfig(); err != nil {
|
||||
if err := validateJiraConfigForStore(trackerStore); err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
||||
|
|
@ -137,15 +135,15 @@ func runJiraSync(cmd *cobra.Command, args []string) error {
|
|||
if len(cliProjects) > 0 {
|
||||
jt.SetProjectKeys(tracker.DeduplicateStrings(cliProjects))
|
||||
}
|
||||
if err := jt.Init(ctx, store); err != nil {
|
||||
if err := jt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleErrorRespectJSON("initializing Jira tracker: %v", err)
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(jt, store, actor)
|
||||
engine := tracker.NewEngine(jt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
|
||||
engine.PushHooks = buildJiraPushHooks(ctx)
|
||||
engine.PushHooks = buildJiraPushHooksForStore(ctx, trackerStore)
|
||||
|
||||
opts := tracker.SyncOptions{
|
||||
Pull: pull,
|
||||
|
|
@ -207,9 +205,13 @@ func runJiraSync(cmd *cobra.Command, args []string) error {
|
|||
|
||||
// buildJiraPushHooks creates PushHooks for Jira-specific push behavior.
|
||||
func buildJiraPushHooks(ctx context.Context) *tracker.PushHooks {
|
||||
return buildJiraPushHooksForStore(ctx, tracker.NewStore(store))
|
||||
}
|
||||
|
||||
func buildJiraPushHooksForStore(ctx context.Context, st tracker.Store) *tracker.PushHooks {
|
||||
return &tracker.PushHooks{
|
||||
ShouldPush: func(issue *types.Issue) bool {
|
||||
pushPrefix, _ := store.GetConfig(ctx, "jira.push_prefix")
|
||||
pushPrefix, _ := st.GetConfig(ctx, "jira.push_prefix")
|
||||
if pushPrefix == "" {
|
||||
return true
|
||||
}
|
||||
|
|
@ -226,9 +228,6 @@ func buildJiraPushHooks(ctx context.Context) *tracker.PushHooks {
|
|||
}
|
||||
|
||||
func runJiraStatus(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("jira status is not supported in proxied-server mode")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("jira-status")
|
||||
defer func() {
|
||||
if c := metrics.Global(); c != nil {
|
||||
|
|
@ -238,22 +237,23 @@ func runJiraStatus(cmd *cobra.Command, args []string) error {
|
|||
|
||||
ctx := rootCtx
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
||||
jiraURL, _ := store.GetConfig(ctx, "jira.url")
|
||||
lastSync, _ := store.GetConfig(ctx, "jira.last_sync")
|
||||
jiraURL, _ := trackerStore.GetConfig(ctx, "jira.url")
|
||||
lastSync, _ := trackerStore.GetConfig(ctx, "jira.last_sync")
|
||||
|
||||
pluralProjects, _ := store.GetConfig(ctx, "jira.projects")
|
||||
singularProject, _ := store.GetConfig(ctx, "jira.project")
|
||||
pluralProjects, _ := trackerStore.GetConfig(ctx, "jira.projects")
|
||||
singularProject, _ := trackerStore.GetConfig(ctx, "jira.project")
|
||||
projectKeys := tracker.ResolveProjectIDs(nil, pluralProjects, singularProject)
|
||||
|
||||
configured := jiraURL != "" && len(projectKeys) > 0
|
||||
|
||||
// jira sync is a round-trip path — opt out of BEADS_MAX_ROWS
|
||||
// (designer §4.1) so a misconfigured env doesn't abort partway.
|
||||
allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{
|
||||
allIssues, err := trackerStore.SearchIssues(ctx, "", types.IssueFilter{
|
||||
MaxRows: 0,
|
||||
MaxRowsSource: "",
|
||||
})
|
||||
|
|
@ -329,26 +329,29 @@ func runJiraStatus(cmd *cobra.Command, args []string) error {
|
|||
|
||||
// validateJiraConfig checks that required Jira configuration is present.
|
||||
func validateJiraConfig() error {
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
return fmt.Errorf("database not available: %w", err)
|
||||
}
|
||||
return validateJiraConfigForStore(tracker.NewStore(store))
|
||||
}
|
||||
|
||||
func validateJiraConfigForStore(st tracker.Store) error {
|
||||
ctx := rootCtx
|
||||
jiraURL, _ := store.GetConfig(ctx, "jira.url")
|
||||
if st == nil {
|
||||
return fmt.Errorf("database not available")
|
||||
}
|
||||
jiraURL, _ := st.GetConfig(ctx, "jira.url")
|
||||
|
||||
if jiraURL == "" {
|
||||
return fmt.Errorf("jira.url not configured\nRun: bd config set jira.url \"https://company.atlassian.net\"")
|
||||
}
|
||||
|
||||
// Check for project configuration (singular or plural).
|
||||
pluralProjects, _ := store.GetConfig(ctx, "jira.projects")
|
||||
singularProject, _ := store.GetConfig(ctx, "jira.project")
|
||||
pluralProjects, _ := st.GetConfig(ctx, "jira.projects")
|
||||
singularProject, _ := st.GetConfig(ctx, "jira.project")
|
||||
projectKeys := tracker.ResolveProjectIDs(nil, pluralProjects, singularProject)
|
||||
if len(projectKeys) == 0 {
|
||||
return fmt.Errorf("no Jira project configured\nRun: bd config set jira.project \"PROJ\"\nOr: bd config set jira.projects \"PROJ1,PROJ2\"")
|
||||
}
|
||||
|
||||
apiToken, _ := store.GetConfig(ctx, "jira.api_token")
|
||||
apiToken, _ := st.GetConfig(ctx, "jira.api_token")
|
||||
if apiToken == "" && os.Getenv("JIRA_API_TOKEN") == "" {
|
||||
return fmt.Errorf("Jira API token not configured\nRun: bd config set jira.api_token \"YOUR_TOKEN\"\nOr: export JIRA_API_TOKEN=YOUR_TOKEN")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
|
@ -124,7 +123,7 @@ type stubMaxRowsTracker struct{}
|
|||
func (*stubMaxRowsTracker) Name() string { return "stubmr" }
|
||||
func (*stubMaxRowsTracker) DisplayName() string { return "StubMR" }
|
||||
func (*stubMaxRowsTracker) ConfigPrefix() string { return "stubmr" }
|
||||
func (*stubMaxRowsTracker) Init(_ context.Context, _ storage.Storage) error { return nil }
|
||||
func (*stubMaxRowsTracker) Init(_ context.Context, _ tracker.Store) error { return nil }
|
||||
func (*stubMaxRowsTracker) Validate() error { return nil }
|
||||
func (*stubMaxRowsTracker) Close() error { return nil }
|
||||
func (*stubMaxRowsTracker) FieldMapper() tracker.FieldMapper { return nil }
|
||||
|
|
|
|||
172
cmd/bd/linear.go
172
cmd/bd/linear.go
|
|
@ -215,9 +215,6 @@ func init() {
|
|||
}
|
||||
|
||||
func runLinearSync(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("linear sync is not supported in proxied-server mode")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("linear-sync")
|
||||
defer func() {
|
||||
if c := metrics.Global(); c != nil {
|
||||
|
|
@ -312,16 +309,17 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
return HandleErrorRespectJSON("--milestones only applies when pulling from Linear")
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("database not available: %v", err)
|
||||
}
|
||||
|
||||
if err := validateLinearConfig(cliTeams); err != nil {
|
||||
if err := validateLinearConfigForStore(trackerStore, cliTeams); err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
teamIDs := getLinearTeamIDs(ctx, cliTeams)
|
||||
teamIDs := getLinearTeamIDsForStore(ctx, trackerStore, cliTeams)
|
||||
willPush := push || !pull
|
||||
|
||||
if willPush && len(teamIDs) > 1 && len(cliTeams) == 0 {
|
||||
|
|
@ -331,7 +329,7 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
|
||||
lt := &linear.Tracker{}
|
||||
lt.SetTeamIDs(teamIDs)
|
||||
if err := lt.Init(ctx, store); err != nil {
|
||||
if err := lt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleErrorRespectJSON("initializing Linear tracker: %v", err)
|
||||
}
|
||||
if willPush {
|
||||
|
|
@ -340,11 +338,11 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(lt, store, actor)
|
||||
engine := tracker.NewEngine(lt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
|
||||
engine.PullHooks = buildLinearPullHooks(ctx, linearPullHookOptions{
|
||||
engine.PullHooks = buildLinearPullHooksForStore(ctx, trackerStore, linearPullHookOptions{
|
||||
Milestones: milestones,
|
||||
DryRun: dryRun,
|
||||
Actor: actor,
|
||||
|
|
@ -365,7 +363,7 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
for _, t := range excludeTypes {
|
||||
opts.ExcludeTypes = append(opts.ExcludeTypes, types.IssueType(strings.ToLower(t)))
|
||||
}
|
||||
applyLinearExcludeIDConfig(ctx, store, &opts)
|
||||
applyLinearExcludeIDConfig(ctx, trackerStore, &opts)
|
||||
if !includeEphemeral {
|
||||
opts.ExcludeEphemeral = true
|
||||
}
|
||||
|
|
@ -375,7 +373,7 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
allowProjectCreates := opts.ParentID != "" || len(opts.IssueIDs) > 0
|
||||
|
||||
engine.PushHooks = buildLinearPushHooks(ctx, lt, allowProjectCreates)
|
||||
engine.PushHooks = buildLinearPushHooksForStore(ctx, trackerStore, lt, allowProjectCreates)
|
||||
|
||||
if preferLocal {
|
||||
opts.ConflictResolution = tracker.ConflictLocal
|
||||
|
|
@ -420,7 +418,7 @@ func runLinearSync(cmd *cobra.Command, args []string) error {
|
|||
// receives opts by value.
|
||||
effectivePush := push || (!push && !pull)
|
||||
if effectivePush && result.Success && !syncIsScoped(&opts) {
|
||||
reconcileLinearParents(ctx, lt, dryRun, jsonOutput, &result.Warnings)
|
||||
reconcileLinearParentsForStore(ctx, trackerStore, lt, dryRun, jsonOutput, &result.Warnings)
|
||||
}
|
||||
|
||||
// Record successful pull timestamp
|
||||
|
|
@ -480,12 +478,12 @@ type linearPullHookOptions struct {
|
|||
|
||||
// buildLinearPullHooks creates PullHooks for Linear-specific pull behavior.
|
||||
func buildLinearPullHooks(ctx context.Context, opts linearPullHookOptions) *tracker.PullHooks {
|
||||
return buildLinearPullHooksForStore(ctx, store, opts)
|
||||
return buildLinearPullHooksForStore(ctx, tracker.NewStore(store), opts)
|
||||
}
|
||||
|
||||
func buildLinearPullHooksForStore(ctx context.Context, st storage.Storage, opts linearPullHookOptions) *tracker.PullHooks {
|
||||
idMode := getLinearIDMode(ctx)
|
||||
hashLength := getLinearHashLength(ctx)
|
||||
func buildLinearPullHooksForStore(ctx context.Context, st tracker.Store, opts linearPullHookOptions) *tracker.PullHooks {
|
||||
idMode := getLinearIDModeForStore(ctx, st)
|
||||
hashLength := getLinearHashLengthForStore(ctx, st)
|
||||
|
||||
hooks := &tracker.PullHooks{}
|
||||
hookActor := opts.Actor
|
||||
|
|
@ -573,7 +571,7 @@ func isLinearMilestoneExternalRef(ref string) bool {
|
|||
return strings.HasPrefix(strings.TrimSpace(ref), linearMilestoneExternalRefPrefix)
|
||||
}
|
||||
|
||||
func ensureLinearMilestoneEpic(ctx context.Context, st storage.Storage, ms *linear.ProjectMilestone, actor string, generateID func(context.Context, *types.Issue) error) (string, error) {
|
||||
func ensureLinearMilestoneEpic(ctx context.Context, st tracker.Store, ms *linear.ProjectMilestone, actor string, generateID func(context.Context, *types.Issue) error) (string, error) {
|
||||
milestoneID := strings.TrimSpace(ms.ID)
|
||||
if milestoneID == "" {
|
||||
return "", fmt.Errorf("Linear project milestone is missing id")
|
||||
|
|
@ -644,7 +642,7 @@ func ensureLinearMilestoneEpic(ctx context.Context, st storage.Storage, ms *line
|
|||
return ref, nil
|
||||
}
|
||||
|
||||
func findLinearMilestoneEpic(ctx context.Context, st storage.Storage, ref, milestoneID, title string) (*types.Issue, error) {
|
||||
func findLinearMilestoneEpic(ctx context.Context, st tracker.Store, ref, milestoneID, title string) (*types.Issue, error) {
|
||||
if existing, err := st.GetIssueByExternalRef(ctx, ref); err == nil {
|
||||
return existing, nil
|
||||
} else if !errors.Is(err, storage.ErrNotFound) {
|
||||
|
|
@ -747,6 +745,10 @@ func isLinearMilestoneIssue(issue *types.Issue) bool {
|
|||
|
||||
// buildLinearPushHooks creates PushHooks for Linear-specific push behavior.
|
||||
func buildLinearPushHooks(ctx context.Context, lt *linear.Tracker, allowProjectCreates bool) *tracker.PushHooks {
|
||||
return buildLinearPushHooksForStore(ctx, tracker.NewStore(store), lt, allowProjectCreates)
|
||||
}
|
||||
|
||||
func buildLinearPushHooksForStore(ctx context.Context, st tracker.Store, lt *linear.Tracker, allowProjectCreates bool) *tracker.PushHooks {
|
||||
config := lt.MappingConfig()
|
||||
var labelOnce sync.Once
|
||||
var labelCache *linear.LabelCache
|
||||
|
|
@ -790,7 +792,7 @@ func buildLinearPushHooks(ctx context.Context, lt *linear.Tracker, allowProjectC
|
|||
if isLinearMilestoneIssue(issue) {
|
||||
return false
|
||||
}
|
||||
if projectID, _ := store.GetConfig(ctx, "linear.project_id"); projectID != "" {
|
||||
if projectID, _ := st.GetConfig(ctx, "linear.project_id"); projectID != "" {
|
||||
if issue.ExternalRef == nil || strings.TrimSpace(*issue.ExternalRef) == "" {
|
||||
if !allowProjectCreates {
|
||||
return false
|
||||
|
|
@ -799,7 +801,7 @@ func buildLinearPushHooks(ctx context.Context, lt *linear.Tracker, allowProjectC
|
|||
}
|
||||
|
||||
// Apply push prefix filtering if configured
|
||||
pushPrefix, _ := store.GetConfig(ctx, "linear.push_prefix")
|
||||
pushPrefix, _ := st.GetConfig(ctx, "linear.push_prefix")
|
||||
if pushPrefix == "" {
|
||||
return true
|
||||
}
|
||||
|
|
@ -816,9 +818,6 @@ func buildLinearPushHooks(ctx context.Context, lt *linear.Tracker, allowProjectC
|
|||
}
|
||||
|
||||
func runLinearStatus(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("linear status is not supported in proxied-server mode")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("linear-status")
|
||||
defer func() {
|
||||
if c := metrics.Global(); c != nil {
|
||||
|
|
@ -828,20 +827,21 @@ func runLinearStatus(cmd *cobra.Command, args []string) error {
|
|||
|
||||
ctx := rootCtx
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
||||
apiKey, _ := getLinearConfig(ctx, "linear.api_key")
|
||||
oauthClientID, _ := getLinearConfig(ctx, "linear.oauth_client_id")
|
||||
oauthClientSecret, _ := getLinearConfig(ctx, "linear.oauth_client_secret")
|
||||
teamIDs := getLinearTeamIDs(ctx, nil)
|
||||
lastSync, _ := store.GetConfig(ctx, "linear.last_sync")
|
||||
apiKey, _ := getLinearConfigForStore(ctx, trackerStore, "linear.api_key")
|
||||
oauthClientID, _ := getLinearConfigForStore(ctx, trackerStore, "linear.oauth_client_id")
|
||||
oauthClientSecret, _ := getLinearConfigForStore(ctx, trackerStore, "linear.oauth_client_secret")
|
||||
teamIDs := getLinearTeamIDsForStore(ctx, trackerStore, nil)
|
||||
lastSync, _ := trackerStore.GetConfig(ctx, "linear.last_sync")
|
||||
|
||||
hasOAuth := oauthClientID != "" && oauthClientSecret != ""
|
||||
configured := (apiKey != "" || hasOAuth) && len(teamIDs) > 0
|
||||
|
||||
allIssues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
allIssues, err := trackerStore.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
|
@ -932,9 +932,6 @@ func runLinearStatus(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
func runLinearTeams(cmd *cobra.Command, args []string) error {
|
||||
if usesProxiedServer() {
|
||||
return HandleErrorRespectJSON("linear teams is not supported in proxied-server mode")
|
||||
}
|
||||
evt := metrics.NewCommandEvent("linear-teams")
|
||||
defer func() {
|
||||
if c := metrics.Global(); c != nil {
|
||||
|
|
@ -944,7 +941,19 @@ func runLinearTeams(cmd *cobra.Command, args []string) error {
|
|||
|
||||
ctx := rootCtx
|
||||
|
||||
client, err := buildLinearClient(ctx, "")
|
||||
// Team discovery is API-only and must not initialize or probe a local
|
||||
// beads store. In proxied mode, read config through the existing UOW seam.
|
||||
var configStore tracker.Store
|
||||
var err error
|
||||
if usesProxiedServer() {
|
||||
configStore, err = trackerStoreForCommand(ctx)
|
||||
if err != nil {
|
||||
return HandleError("database not available: %v", err)
|
||||
}
|
||||
} else if store != nil {
|
||||
configStore = tracker.NewStore(store)
|
||||
}
|
||||
client, err := buildLinearClientAPIOnly(ctx, "", configStore)
|
||||
if err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
|
@ -1000,19 +1009,19 @@ func isValidUUID(s string) bool {
|
|||
// validateLinearConfig checks that required Linear configuration is present.
|
||||
// cliTeams is the list of team IDs from the --team flag (may be nil).
|
||||
func validateLinearConfig(cliTeams []string) error {
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
return fmt.Errorf("database not available: %w", err)
|
||||
}
|
||||
return validateLinearConfigForStore(tracker.NewStore(store), cliTeams)
|
||||
}
|
||||
|
||||
func validateLinearConfigForStore(st tracker.Store, cliTeams []string) error {
|
||||
ctx := rootCtx
|
||||
|
||||
// Accept either OAuth credentials or API key.
|
||||
oauthClientID, _ := getLinearConfig(ctx, "linear.oauth_client_id")
|
||||
oauthClientSecret, _ := getLinearConfig(ctx, "linear.oauth_client_secret")
|
||||
oauthClientID, _ := getLinearConfigForStore(ctx, st, "linear.oauth_client_id")
|
||||
oauthClientSecret, _ := getLinearConfigForStore(ctx, st, "linear.oauth_client_secret")
|
||||
hasOAuth := oauthClientID != "" && oauthClientSecret != ""
|
||||
|
||||
if !hasOAuth {
|
||||
apiKey, _ := getLinearConfig(ctx, "linear.api_key")
|
||||
apiKey, _ := getLinearConfigForStore(ctx, st, "linear.api_key")
|
||||
if apiKey == "" {
|
||||
return fmt.Errorf("Linear authentication not configured\n" +
|
||||
"Options:\n" +
|
||||
|
|
@ -1021,7 +1030,7 @@ func validateLinearConfig(cliTeams []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
teamIDs := getLinearTeamIDs(ctx, cliTeams)
|
||||
teamIDs := getLinearTeamIDsForStore(ctx, st, cliTeams)
|
||||
if len(teamIDs) == 0 {
|
||||
return fmt.Errorf("no Linear team ID configured\nRun: bd config set linear.team_id \"TEAM_ID\"\nOr: bd config set linear.team_ids \"TEAM_ID1,TEAM_ID2\"\nOr: export LINEAR_TEAM_ID=TEAM_ID")
|
||||
}
|
||||
|
|
@ -1048,6 +1057,10 @@ func maskAPIKey(key string) string {
|
|||
// Priority: environment variable > project config.
|
||||
// Env vars take precedence so CI workers can override config without modifying config.yaml.
|
||||
func getLinearConfig(ctx context.Context, key string) (value string, source string) {
|
||||
return getLinearConfigForStore(ctx, tracker.NewStore(store), key)
|
||||
}
|
||||
|
||||
func getLinearConfigForStore(ctx context.Context, st tracker.Store, key string) (value string, source string) {
|
||||
// Secret keys (e.g. linear.api_key) are stored in config.yaml, not the
|
||||
// Dolt database, to avoid leaking secrets when pushing to remotes.
|
||||
// Env vars are checked first so that LINEAR_OAUTH_CLIENT_ID/SECRET etc.
|
||||
|
|
@ -1066,8 +1079,8 @@ func getLinearConfig(ctx context.Context, key string) (value string, source stri
|
|||
}
|
||||
|
||||
// Try to read from store (works in direct mode)
|
||||
if store != nil {
|
||||
value, _ = store.GetConfig(ctx, key) // Best effort: empty value is valid fallback
|
||||
if st != nil {
|
||||
value, _ = st.GetConfig(ctx, key) // Best effort: empty value is valid fallback
|
||||
if value != "" {
|
||||
return value, "project config (bd config)"
|
||||
}
|
||||
|
|
@ -1115,8 +1128,12 @@ func linearConfigToEnvVar(key string) string {
|
|||
// getLinearTeamIDs resolves the effective team IDs from all config sources.
|
||||
// Precedence: cliTeams (--team flag) > linear.team_ids > LINEAR_TEAM_IDS > linear.team_id > LINEAR_TEAM_ID
|
||||
func getLinearTeamIDs(ctx context.Context, cliTeams []string) []string {
|
||||
pluralVal, _ := getLinearConfig(ctx, "linear.team_ids")
|
||||
singularVal, _ := getLinearConfig(ctx, "linear.team_id")
|
||||
return getLinearTeamIDsForStore(ctx, tracker.NewStore(store), cliTeams)
|
||||
}
|
||||
|
||||
func getLinearTeamIDsForStore(ctx context.Context, st tracker.Store, cliTeams []string) []string {
|
||||
pluralVal, _ := getLinearConfigForStore(ctx, st, "linear.team_ids")
|
||||
singularVal, _ := getLinearConfigForStore(ctx, st, "linear.team_id")
|
||||
return tracker.ResolveProjectIDs(cliTeams, pluralVal, singularVal)
|
||||
}
|
||||
|
||||
|
|
@ -1189,6 +1206,40 @@ func buildLinearClient(ctx context.Context, teamID string) (*linear.Client, erro
|
|||
return linear.NewClient(apiKey, teamID), nil
|
||||
}
|
||||
|
||||
func buildLinearClientAPIOnly(ctx context.Context, teamID string, st tracker.Store) (*linear.Client, error) {
|
||||
oauthClientID := os.Getenv("LINEAR_OAUTH_CLIENT_ID")
|
||||
oauthClientSecret := os.Getenv("LINEAR_OAUTH_CLIENT_SECRET")
|
||||
if oauthClientID == "" {
|
||||
oauthClientID = config.GetString("linear.oauth_client_id")
|
||||
}
|
||||
if oauthClientSecret == "" {
|
||||
oauthClientSecret = config.GetString("linear.oauth_client_secret")
|
||||
}
|
||||
// Select the auth branch first and apply linear.api_endpoint once, after
|
||||
// it — the shape Tracker.Init uses. Returning the OAuth client inline
|
||||
// would leave this helper asymmetric between its own two branches, with a
|
||||
// custom endpoint honored for an API key and ignored for OAuth.
|
||||
var client *linear.Client
|
||||
if oauthClientID != "" && oauthClientSecret != "" {
|
||||
client = linear.NewOAuthClient(linear.OAuthConfig{ClientID: oauthClientID, ClientSecret: oauthClientSecret}, teamID)
|
||||
} else {
|
||||
apiKey := os.Getenv("LINEAR_API_KEY")
|
||||
if apiKey == "" {
|
||||
apiKey = config.GetString("linear.api_key")
|
||||
}
|
||||
if apiKey == "" {
|
||||
return nil, fmt.Errorf("Linear authentication not configured")
|
||||
}
|
||||
client = linear.NewClient(apiKey, teamID)
|
||||
}
|
||||
if st != nil {
|
||||
if endpoint, _ := st.GetConfig(ctx, "linear.api_endpoint"); endpoint != "" {
|
||||
client = client.WithEndpoint(endpoint)
|
||||
}
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// storeConfigLoader adapts the store to the linear.ConfigLoader interface.
|
||||
type storeConfigLoader struct {
|
||||
ctx context.Context
|
||||
|
|
@ -1209,7 +1260,11 @@ func loadLinearMappingConfig(ctx context.Context) *linear.MappingConfig {
|
|||
// getLinearIDMode returns the configured ID mode for Linear imports.
|
||||
// Supported values: "hash" (default) or "db".
|
||||
func getLinearIDMode(ctx context.Context) string {
|
||||
mode, _ := getLinearConfig(ctx, "linear.id_mode")
|
||||
return getLinearIDModeForStore(ctx, tracker.NewStore(store))
|
||||
}
|
||||
|
||||
func getLinearIDModeForStore(ctx context.Context, st tracker.Store) string {
|
||||
mode, _ := getLinearConfigForStore(ctx, st, "linear.id_mode")
|
||||
mode = strings.ToLower(strings.TrimSpace(mode))
|
||||
if mode == "" {
|
||||
return "hash"
|
||||
|
|
@ -1244,7 +1299,11 @@ func applyLinearExcludeIDConfig(ctx context.Context, reader configReader, opts *
|
|||
// getLinearHashLength returns the configured hash length for Linear imports.
|
||||
// Values are clamped to the supported range 3-8.
|
||||
func getLinearHashLength(ctx context.Context) int {
|
||||
raw, _ := getLinearConfig(ctx, "linear.hash_length")
|
||||
return getLinearHashLengthForStore(ctx, tracker.NewStore(store))
|
||||
}
|
||||
|
||||
func getLinearHashLengthForStore(ctx context.Context, st tracker.Store) int {
|
||||
raw, _ := getLinearConfigForStore(ctx, st, "linear.hash_length")
|
||||
if raw == "" {
|
||||
return 6
|
||||
}
|
||||
|
|
@ -1315,10 +1374,14 @@ func syncIsScoped(opts *tracker.SyncOptions) bool {
|
|||
// Warnings (per-link failures, missing refs) are appended to the engine's
|
||||
// warning slice so the user sees them in the standard sync output.
|
||||
func reconcileLinearParents(ctx context.Context, lt *linear.Tracker, dryRun, jsonOutput bool, warnings *[]string) {
|
||||
if lt == nil || store == nil {
|
||||
reconcileLinearParentsForStore(ctx, tracker.NewStore(store), lt, dryRun, jsonOutput, warnings)
|
||||
}
|
||||
|
||||
func reconcileLinearParentsForStore(ctx context.Context, st tracker.Store, lt *linear.Tracker, dryRun, jsonOutput bool, warnings *[]string) {
|
||||
if lt == nil || st == nil {
|
||||
return
|
||||
}
|
||||
links, err := buildLinearParentLinks(ctx, lt)
|
||||
links, err := buildLinearParentLinksForStore(ctx, st, lt)
|
||||
if err != nil {
|
||||
*warnings = append(*warnings, fmt.Sprintf("parent reconcile: building link set failed: %v", err))
|
||||
return
|
||||
|
|
@ -1364,7 +1427,14 @@ func reconcileLinearParents(ctx context.Context, lt *linear.Tracker, dryRun, jso
|
|||
// they'll get picked up on a subsequent sync once the parent has an
|
||||
// external_ref.
|
||||
func buildLinearParentLinks(ctx context.Context, lt *linear.Tracker) ([]linear.ParentLink, error) {
|
||||
issues, err := store.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
return buildLinearParentLinksForStore(ctx, tracker.NewStore(store), lt)
|
||||
}
|
||||
|
||||
func buildLinearParentLinksForStore(ctx context.Context, st tracker.Store, lt *linear.Tracker) ([]linear.ParentLink, error) {
|
||||
if st == nil {
|
||||
return nil, fmt.Errorf("database not available")
|
||||
}
|
||||
issues, err := st.SearchIssues(ctx, "", types.IssueFilter{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -1396,7 +1466,7 @@ func buildLinearParentLinks(ctx context.Context, lt *linear.Tracker) ([]linear.P
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
deps, err := store.GetDependenciesWithMetadata(ctx, issue.ID)
|
||||
deps, err := st.GetDependenciesWithMetadata(ctx, issue.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading deps for %s: %w", issue.ID, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import (
|
|||
|
||||
func runListProxiedServer(cmd *cobra.Command, ctx context.Context, out io.Writer, in listInput) error {
|
||||
if in.repoOverrideSet {
|
||||
return errors.New("--repo is not supported with --proxied-server")
|
||||
return HandleProxyCapabilityError(AssertProxyCapability(ProxyModeProxied, ProxyCapRepo))
|
||||
}
|
||||
switch {
|
||||
case in.watchMode:
|
||||
|
|
|
|||
|
|
@ -1256,10 +1256,29 @@ var rootCmd = &cobra.Command{
|
|||
fmt.Fprintf(os.Stderr, "warning: %v\n", err)
|
||||
}
|
||||
}
|
||||
if cmdName == "doctor" && usesProxiedServer() {
|
||||
// Refuse only on a real refusal. validateProxyMaintenance...
|
||||
// returns nil for doctor subcommands, and returning early on
|
||||
// that would skip the legacy-store guard and autocommit-mode
|
||||
// resolution every other skipsStoreInit command still runs.
|
||||
if err := validateProxyMaintenanceBeforeProvider(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if beadsDir == "" {
|
||||
beadsDir = beads.FindBeadsDir()
|
||||
}
|
||||
if err := guardLegacyNoStoreCommand(cmd, beadsDir); err != nil {
|
||||
isMigrationCommand := false
|
||||
for current := cmd; current != nil; current = current.Parent() {
|
||||
if current.Name() == "migrate" {
|
||||
isMigrationCommand = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isMigrationCommand {
|
||||
return HandleProxyCapabilityError(&ProxyCapabilityError{Code: "proxy.migrate.invalid_state", Message: err.Error(), ExitCode: 1, Mutates: false})
|
||||
}
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if _, err := getDoltAutoCommitMode(); err != nil {
|
||||
|
|
@ -1447,6 +1466,26 @@ var rootCmd = &cobra.Command{
|
|||
if backendErr := validateConfiguredBackend(cfg, beadsDir); backendErr != nil {
|
||||
return HandleError("%v", backendErr)
|
||||
}
|
||||
// Reject proxy capability combinations before any workspace side effect
|
||||
// (version tracking, migration, auto-start, or provider construction).
|
||||
if cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
if err := validateProxyCapabilitiesBeforeProvider(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateProxyMaintenanceBeforeProvider(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateProxyTransformBeforeProvider(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// The proxied provider cannot guarantee strict read-only semantics. Refuse
|
||||
// before provider construction so no connection, migration, or mutation
|
||||
// is attempted; expose the same stable capability code as other proxy
|
||||
// front-door refusals.
|
||||
if readonlyMode && cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
return HandleProxyCapabilityError(AssertProxyCapability(ProxyModeProxied, ProxyCapReadonly))
|
||||
}
|
||||
if readonlyMode && !backendSupportsStrictReadonly(cfg) {
|
||||
return HandleError("strict readonly is unavailable for dolt proxied-server backend; refusing to open a store that cannot guarantee mutation-free access")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ func rejectMaxRowsUnderProxiedServer(cmd *cobra.Command) error {
|
|||
// that warning under proxied mode (be-x42v.4 round-4 follow-up).
|
||||
func rejectResolvedMaxRowsUnderProxiedServer(maxRows int) error {
|
||||
if maxRows > 0 {
|
||||
return HandleErrorRespectJSON("--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode")
|
||||
return HandleProxyCapabilityError(AssertProxyCapability(ProxyModeProxied, ProxyCapMaxRows))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
916
cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go
Normal file
916
cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go
Normal file
|
|
@ -0,0 +1,916 @@
|
|||
//go:build cgo && integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func runBDExecSeparated(t *testing.T, bd, dir string, env []string, args ...string) (string, string, error) {
|
||||
t.Helper()
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir, cmd.Env = dir, env
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
err := cmd.Run()
|
||||
return stdout.String(), stderr.String(), err
|
||||
}
|
||||
|
||||
func createDependencyPair(t *testing.T, bd, dir string, env []string, sentinelID string) string {
|
||||
t.Helper()
|
||||
blockerOut, err := runBDExecWithBinary(t, bd, dir, env, "create", "migration blocker", "--json")
|
||||
require.NoError(t, err, "create blocker: %s", blockerOut)
|
||||
var blocker struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(blockerOut), &blocker))
|
||||
require.NotEmpty(t, blocker.ID)
|
||||
depOut, depErr := runBDExecWithBinary(t, bd, dir, env, "dep", "add", sentinelID, blocker.ID)
|
||||
require.NoError(t, depErr, "dep add: %s", depOut)
|
||||
return blocker.ID
|
||||
}
|
||||
|
||||
func assertDependencyPair(t *testing.T, bd, dir string, env []string, sentinelID, blockerID string) {
|
||||
t.Helper()
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "dep", "list", sentinelID, blockerID, "--json")
|
||||
require.NoError(t, err, "dep list: %s", out)
|
||||
var edges []struct {
|
||||
IssueID string `json:"issue_id"`
|
||||
DependsOnID string `json:"depends_on_id"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(out), &edges), "dep list JSON: %s", out)
|
||||
found := false
|
||||
for _, edge := range edges {
|
||||
if (edge.IssueID == "" || edge.IssueID == sentinelID) && edge.DependsOnID == blockerID && edge.Type == "blocks" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
assert.True(t, found, "dependency edge missing: %s -> %s", sentinelID, blockerID)
|
||||
}
|
||||
|
||||
func migrationFrontDoorBinary(t *testing.T) string {
|
||||
if p := os.Getenv("BEADS_TEST_BD_BINARY"); p != "" {
|
||||
return p
|
||||
}
|
||||
out := filepath.Join(t.TempDir(), "bd")
|
||||
cmd := exec.Command("go", "build", "-o", out, ".")
|
||||
cmd.Dir = "."
|
||||
if b, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build cgo bd: %v\n%s", err, b)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func migrationFrontDoorEnv(home string) []string {
|
||||
env := make([]string, 0)
|
||||
for _, kv := range os.Environ() {
|
||||
key := kv
|
||||
if i := strings.IndexByte(kv, '='); i >= 0 {
|
||||
key = kv[:i]
|
||||
}
|
||||
// The cmd/bd package TestMain sets BEADS_TEST_MODE and related
|
||||
// controls for its in-process tests. Never inherit those into the
|
||||
// real front-door subprocess: BEADS_TEST_MODE disables auto-start
|
||||
// and makes a freshly initialized server resolve to port 1.
|
||||
if strings.HasPrefix(key, "BEADS_") || strings.HasPrefix(key, "BD_") || key == "HOME" || key == "USERPROFILE" {
|
||||
continue
|
||||
}
|
||||
env = append(env, kv)
|
||||
}
|
||||
return append(env, "HOME="+home, "USERPROFILE="+home)
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeFrontDoor exercises migration through the real bd
|
||||
// executable. It is opt-in because it requires a local Dolt installation.
|
||||
func TestMigrateDoltModeFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
dir := t.TempDir()
|
||||
home := t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--server", "--prefix", "fd", "--quiet")
|
||||
if err != nil {
|
||||
t.Fatalf("init: %v\n%s", err, out)
|
||||
}
|
||||
createOut, err := runBDExecWithBinary(t, bd, dir, env, "create", "front-door sentinel", "--json")
|
||||
if err != nil {
|
||||
t.Fatalf("create sentinel: %v\n%s", err, createOut)
|
||||
}
|
||||
statusOut, _ := runBDExecWithBinary(t, bd, dir, env, "dolt", "status")
|
||||
if strings.Contains(statusOut, "Dolt server: running") {
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop"); err != nil {
|
||||
t.Fatalf("stop running server: %v\n%s", err, out)
|
||||
}
|
||||
}
|
||||
var created struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(createOut), &created); err != nil || created.ID == "" {
|
||||
t.Fatalf("invalid create JSON: %s", createOut)
|
||||
}
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "migrate", "from-server-to-proxied-server"); err != nil {
|
||||
t.Fatalf("forward migration: %v\n%s", err, out)
|
||||
}
|
||||
metaProxy, _ := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json"))
|
||||
var proxyMetadata map[string]any
|
||||
if err := json.Unmarshal(metaProxy, &proxyMetadata); err != nil || proxyMetadata["dolt_mode"] != "proxied-server" {
|
||||
t.Fatalf("forward did not persist proxied mode: %s", metaProxy)
|
||||
}
|
||||
info, err := os.ReadFile(filepath.Join(dir, ".beads", "proxied_server_client_info.json"))
|
||||
if err != nil || len(info) == 0 {
|
||||
t.Fatalf("missing proxied sidecar: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, "Switched to proxied-server mode") {
|
||||
t.Fatalf("unexpected forward output: %s", out)
|
||||
}
|
||||
list, err := runBDExecWithBinary(t, bd, dir, env, "list", "--json")
|
||||
if err != nil || !json.Valid([]byte(list)) || !strings.Contains(list, created.ID) {
|
||||
t.Fatalf("proxied list JSON: %v\n%s", err, list)
|
||||
}
|
||||
// A completed forward migration must refuse while its proxy process still
|
||||
// owns the target. This is exercised through the actual bd front door,
|
||||
// before provider startup can hide the ownership conflict.
|
||||
runningOut, runningErr := runBDExecWithBinary(t, bd, dir, env, "--json", "migrate", "from-server-to-proxied-server")
|
||||
if runningErr == nil {
|
||||
t.Fatalf("forward no-op unexpectedly succeeded while proxy was running: %s", runningOut)
|
||||
}
|
||||
var runningRefusal map[string]any
|
||||
if err := json.Unmarshal([]byte(runningOut), &runningRefusal); err != nil {
|
||||
t.Fatalf("running-proxy refusal JSON: %v\n%s", err, runningOut)
|
||||
}
|
||||
if data, ok := runningRefusal["data"].(map[string]any); ok {
|
||||
runningRefusal = data
|
||||
}
|
||||
if runningRefusal["code"] != "proxy.migrate.invalid_state" || runningRefusal["mutates"] != false {
|
||||
t.Fatalf("running-proxy refusal = %s", runningOut)
|
||||
}
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "update", created.ID, "--title", "front-door sentinel updated"); err != nil {
|
||||
t.Fatalf("proxied update: %v\n%s", err, out)
|
||||
}
|
||||
statusOut, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "status")
|
||||
if strings.Contains(statusOut, "Dolt server: running") {
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop"); err != nil {
|
||||
t.Fatalf("stop proxied server: %v\n%s", err, out)
|
||||
}
|
||||
}
|
||||
// The proxy can supervise a Dolt child whose status text is stale; stop is
|
||||
// idempotent and its "not running" result is safe to ignore.
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop"); err != nil && !strings.Contains(strings.ToLower(out), "not running") {
|
||||
t.Fatalf("stop before reverse: %v\n%s", err, out)
|
||||
}
|
||||
sidecarBefore, err := os.ReadFile(filepath.Join(dir, ".beads", "proxied_server_client_info.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metaBefore, err := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "--json", "migrate", "from-proxied-server-to-server", "--dry-run"); err != nil {
|
||||
t.Fatalf("reverse dry-run: %v\n%s", err, out)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, ".beads", "proxied_server_client_info.json")); err != nil {
|
||||
t.Fatalf("dry-run removed sidecar: %v", err)
|
||||
}
|
||||
if got, _ := os.ReadFile(filepath.Join(dir, ".beads", "proxied_server_client_info.json")); string(got) != string(sidecarBefore) {
|
||||
t.Fatal("dry-run changed sidecar")
|
||||
}
|
||||
if got, _ := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json")); string(got) != string(metaBefore) {
|
||||
t.Fatal("dry-run changed metadata")
|
||||
}
|
||||
if out, err = runBDExecWithBinary(t, bd, dir, env, "migrate", "from-proxied-server-to-server"); err != nil {
|
||||
t.Fatalf("reverse migration: %v\n%s", err, out)
|
||||
}
|
||||
show, err := runBDExecWithBinary(t, bd, dir, env, "show", created.ID, "--json")
|
||||
if err != nil {
|
||||
t.Fatalf("show sentinel: %v\n%s", err, show)
|
||||
}
|
||||
if !json.Valid([]byte(show)) || !strings.Contains(show, "front-door sentinel updated") {
|
||||
t.Fatalf("sentinel missing or invalid JSON: %s", show)
|
||||
}
|
||||
meta, err := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json"))
|
||||
var serverMetadata map[string]any
|
||||
if err != nil || json.Unmarshal(meta, &serverMetadata) != nil || serverMetadata["dolt_mode"] != "server" {
|
||||
t.Fatalf("metadata mode not restored: %s", meta)
|
||||
}
|
||||
if _, err = os.Stat(filepath.Join(dir, ".beads", "proxied_server_client_info.json")); !os.IsNotExist(err) {
|
||||
t.Fatal("sidecar remains after reverse")
|
||||
}
|
||||
list, err = runBDExecWithBinary(t, bd, dir, env, "list", "--json")
|
||||
if err != nil || !json.Valid([]byte(list)) || !strings.Contains(list, "front-door sentinel updated") {
|
||||
t.Fatalf("direct list JSON: %v\n%s", err, list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateDoltModeFrontDoorRefusesMalformedSidecar(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
if out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--proxied-server", "--quiet"); err != nil {
|
||||
t.Fatalf("init: %v\n%s", err, out)
|
||||
}
|
||||
path := filepath.Join(dir, ".beads", "proxied_server_client_info.json")
|
||||
if err := os.WriteFile(path, []byte("{malformed"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, _ := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json"))
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "--json", "migrate", "from-proxied-server-to-server")
|
||||
if err == nil {
|
||||
t.Fatal("malformed sidecar unexpectedly succeeded")
|
||||
}
|
||||
if !json.Valid([]byte(out)) {
|
||||
t.Fatalf("expected JSON error output, got %s", out)
|
||||
}
|
||||
after, _ := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json"))
|
||||
if string(before) != string(after) {
|
||||
t.Fatal("refusal mutated metadata")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateDoltModeFrontDoorRefusesExternalEndpoint(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
want string
|
||||
}{
|
||||
{name: "tcp", want: "externally hosted proxied Dolt endpoint"},
|
||||
{name: "unix", want: "externally hosted proxied Dolt endpoint"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
if err := os.MkdirAll(filepath.Join(root, ".dolt"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"database":"myproj","backend":"dolt","dolt_mode":"proxied-server"}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(beadsDir, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metaBefore, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sidecarPath := filepath.Join(beadsDir, "proxied_server_client_info.json")
|
||||
// Replace the managed sidecar with an externally-owned endpoint.
|
||||
// This exercises migration refusal without attempting a network
|
||||
// connection to an unreachable host/socket during init.
|
||||
external := map[string]any{"root_path": filepath.Join(beadsDir, "dolt"), "external": map[string]any{}}
|
||||
if tc.name == "tcp" {
|
||||
external["external"] = map[string]any{"host": "db.example", "port": 3307}
|
||||
} else {
|
||||
external["external"] = map[string]any{"socket": "/tmp/beads-front-door.sock"}
|
||||
}
|
||||
sidecarData, _ := json.Marshal(external)
|
||||
if err := os.WriteFile(sidecarPath, sidecarData, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sidecarBefore, err := os.ReadFile(sidecarPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
treeBefore := snapshotMigrationTree(t, beadsDir)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "--json", "migrate", "from-proxied-server-to-server")
|
||||
if err == nil {
|
||||
t.Fatal("external endpoint migration unexpectedly succeeded")
|
||||
}
|
||||
if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
|
||||
t.Fatalf("expected exit code 1, got %v", err)
|
||||
}
|
||||
var refusal map[string]any
|
||||
if err := json.Unmarshal([]byte(out), &refusal); err != nil {
|
||||
t.Fatalf("invalid refusal JSON: %v\n%s", err, out)
|
||||
}
|
||||
if data, ok := refusal["data"].(map[string]any); ok {
|
||||
refusal = data
|
||||
}
|
||||
if refusal["code"] != "proxy.migrate.external_endpoint" || refusal["mutates"] != false {
|
||||
t.Fatalf("expected typed non-mutating refusal, got: %s", out)
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(out), strings.ToLower(tc.want)) {
|
||||
t.Fatalf("expected refusal text %q, got: %s", tc.want, out)
|
||||
}
|
||||
// Refusal must happen before any proxy/provider process starts.
|
||||
probe := exec.Command("pgrep", "-af", "[d]b-proxy-child --root "+root)
|
||||
if processOut, probeErr := probe.CombinedOutput(); probeErr == nil && len(strings.TrimSpace(string(processOut))) > 0 {
|
||||
t.Fatalf("external refusal started a proxy process: %s", processOut)
|
||||
}
|
||||
if got, _ := os.ReadFile(filepath.Join(beadsDir, "metadata.json")); string(got) != string(metaBefore) {
|
||||
t.Fatal("external refusal mutated metadata")
|
||||
}
|
||||
if got, _ := os.ReadFile(sidecarPath); string(got) != string(sidecarBefore) {
|
||||
t.Fatal("external refusal mutated sidecar")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(beadsDir, "dolt-mode-migration.json")); !os.IsNotExist(err) {
|
||||
t.Fatal("external refusal created a migration journal")
|
||||
}
|
||||
if got := snapshotMigrationTree(t, beadsDir); !reflect.DeepEqual(treeBefore, got) {
|
||||
t.Fatal("external refusal mutated migration artifacts")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateDoltModeFrontDoorExternalJournalMatrix(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
phases := []string{"prepared", "target_configured", "old_controls_retired", "verified", "committed"}
|
||||
for _, reverse := range []bool{false, true} {
|
||||
for _, phase := range phases {
|
||||
for _, unix := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/%s/%s", map[bool]string{false: "forward", true: "reverse"}[reverse], phase, map[bool]string{false: "tcp", true: "unix"}[unix]), func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
beadsDir, root := filepath.Join(dir, ".beads"), filepath.Join(dir, ".beads", "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
src, target := "server", "proxied-server"
|
||||
command := []string{"from-server-to-proxied-server"}
|
||||
if reverse {
|
||||
src, target, command = "proxied-server", "server", []string{"from-proxied-server-to-server"}
|
||||
}
|
||||
mode := src
|
||||
if phase != "prepared" {
|
||||
mode = target
|
||||
}
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(fmt.Sprintf(`{"database":"myproj","backend":"dolt","dolt_mode":%q}`, mode)), 0o600))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, ".local_version"), []byte(Version), 0o600))
|
||||
require.NoError(t, os.Chmod(beadsDir, 0o700))
|
||||
ext := map[string]any{"host": "db.example", "port": 3307}
|
||||
if unix {
|
||||
ext = map[string]any{"socket": "/tmp/beads-front-door.sock"}
|
||||
}
|
||||
sidecar, _ := json.Marshal(map[string]any{"root_path": root, "external": ext})
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "proxied_server_client_info.json"), sidecar, 0o600))
|
||||
journal, _ := json.Marshal(map[string]any{"version": 1, "source_mode": src, "target_mode": target, "root_path": root, "external": ext, "sidecar": map[string]any{"root_path": root, "external": ext}, "ownership": "external", "attempt": 1, "phase": phase})
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, migrateJournalFileName), journal, 0o600))
|
||||
gatePath := filepath.Join(beadsDir, "dolt.gate.lock")
|
||||
require.NoError(t, os.WriteFile(gatePath, []byte("preseeded-gate-lock"), 0o600))
|
||||
gateBefore, gateErr := os.ReadFile(gatePath)
|
||||
gateExists := gateErr == nil
|
||||
tree := snapshotMigrationTree(t, beadsDir)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, append([]string{"--json", "migrate"}, command...)...)
|
||||
require.Error(t, err)
|
||||
exitErr, ok := err.(*exec.ExitError)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, exitErr.ExitCode())
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(out), &payload), out)
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
payload = data
|
||||
}
|
||||
assert.Equal(t, "proxy.migrate.external_endpoint", payload["code"])
|
||||
assert.Equal(t, false, payload["mutates"])
|
||||
assert.Contains(t, payload["error"], "externally hosted proxied Dolt endpoint")
|
||||
assert.Equal(t, tree, snapshotMigrationTree(t, beadsDir))
|
||||
for _, p := range append([]string{migrateLockFileName}, serverAssetNames()...) {
|
||||
_, statErr := os.Stat(filepath.Join(beadsDir, p))
|
||||
assert.True(t, os.IsNotExist(statErr), "unexpected artifact %s", p)
|
||||
}
|
||||
gateAfter, gateAfterErr := os.ReadFile(gatePath)
|
||||
assert.Equal(t, gateExists, gateAfterErr == nil, "dolt gate lock existence changed")
|
||||
if gateExists {
|
||||
assert.Equal(t, gateBefore, gateAfter, "dolt gate lock changed")
|
||||
}
|
||||
for _, p := range proxiedAssetNames() {
|
||||
_, statErr := os.Stat(filepath.Join(root, p))
|
||||
assert.True(t, os.IsNotExist(statErr), "unexpected proxy artifact %s", p)
|
||||
}
|
||||
probe := exec.Command("pgrep", "-af", "[d]b-proxy-child --root "+root)
|
||||
processOut, probeErr := probe.CombinedOutput()
|
||||
assert.True(t, probeErr != nil || len(strings.TrimSpace(string(processOut))) == 0, "proxy process started: %s", processOut)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateDoltModeFrontDoorMalformedStateRefuses(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
file string
|
||||
body string
|
||||
}{
|
||||
{"metadata", "metadata.json", "{malformed"},
|
||||
{"journal", migrateJournalFileName, "{malformed"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
require.NoError(t, os.MkdirAll(beadsDir, 0o700))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, ".local_version"), []byte(Version), 0o600))
|
||||
if tc.name == "journal" {
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"database":"myproj","backend":"dolt","dolt_mode":"proxied-server"}`), 0o600))
|
||||
}
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, tc.file), []byte(tc.body), 0o600))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
out, _, err := runBDExecSeparated(t, bd, dir, env, "--json", "migrate", "from-proxied-server-to-server")
|
||||
require.Error(t, err)
|
||||
exitErr, ok := err.(*exec.ExitError)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, exitErr.ExitCode())
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(out), &payload), out)
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
payload = data
|
||||
}
|
||||
assert.Equal(t, "proxy.migrate.invalid_state", payload["code"])
|
||||
assert.Equal(t, false, payload["mutates"])
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeForwardNoopRefusalsFrontDoor keeps the completed-target
|
||||
// path fail-closed at the real CLI boundary. Both output modes must preserve
|
||||
// the workspace byte-for-byte while refusing ambiguous ownership/artifacts.
|
||||
func TestMigrateDoltModeForwardNoopRefusalsFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
cases := []struct {
|
||||
name string
|
||||
shared bool
|
||||
info map[string]any
|
||||
asset string
|
||||
want string
|
||||
}{
|
||||
{name: "direct external sidecar", info: map[string]any{"external": map[string]any{"host": "db.example", "port": 3307}}, want: "externally hosted"},
|
||||
{name: "direct mismatched sidecar", info: map[string]any{"root_path": "/tmp/other"}, want: "canonical"},
|
||||
{name: "direct stale control", asset: "proxy.log", want: "stale proxy control"},
|
||||
{name: "shared mismatched sidecar", shared: true, info: map[string]any{"root_path": "/tmp/other"}, want: "canonical"},
|
||||
{name: "shared external sidecar", shared: true, info: map[string]any{"external": map[string]any{"host": "db.example", "port": 3307}}, want: "externally hosted"},
|
||||
{name: "shared stale control", shared: true, asset: "proxy.log", want: "stale proxy control"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
if tc.shared {
|
||||
shared := filepath.Join(home, "shared-server")
|
||||
env = append(env, "BEADS_SHARED_SERVER_DIR="+shared, "BEADS_DOLT_SHARED_SERVER=1")
|
||||
root = filepath.Join(shared, "dolt")
|
||||
}
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
require.NoError(t, os.MkdirAll(beadsDir, 0o700))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"database":"myproj","backend":"dolt","dolt_mode":"proxied-server"}`), 0o600))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, ".local_version"), []byte(Version), 0o600))
|
||||
if tc.shared {
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: false\n"), 0o600))
|
||||
}
|
||||
if tc.info == nil {
|
||||
tc.info = map[string]any{"root_path": root}
|
||||
}
|
||||
if _, ok := tc.info["root_path"]; !ok {
|
||||
tc.info["root_path"] = root
|
||||
}
|
||||
b, err := json.Marshal(tc.info)
|
||||
require.NoError(t, err)
|
||||
sidecar := filepath.Join(beadsDir, "proxied_server_client_info.json")
|
||||
require.NoError(t, os.WriteFile(sidecar, b, 0o600))
|
||||
if tc.asset != "" {
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, tc.asset), []byte("stale"), 0o600))
|
||||
}
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
for _, jsonMode := range []bool{false, true} {
|
||||
args := []string{"migrate", "from-server-to-proxied-server"}
|
||||
if tc.shared {
|
||||
args[1] = "from-shared-server-to-proxied-server"
|
||||
}
|
||||
if jsonMode {
|
||||
args = append([]string{"--json"}, args...)
|
||||
}
|
||||
stdout, stderr, runErr := runBDExecSeparated(t, bd, dir, env, args...)
|
||||
require.Error(t, runErr)
|
||||
exitErr, ok := runErr.(*exec.ExitError)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, exitErr.ExitCode())
|
||||
if jsonMode {
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(stdout), &payload), stdout)
|
||||
if data, ok := payload["data"].(map[string]any); ok {
|
||||
payload = data
|
||||
}
|
||||
assert.Equal(t, "proxy.migrate.invalid_state", payload["code"])
|
||||
assert.Equal(t, false, payload["mutates"])
|
||||
} else {
|
||||
assert.Contains(t, strings.ToLower(stderr), strings.ToLower(tc.want))
|
||||
}
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir, root))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeDirectForwardFaultsFrontDoor runs every checkpoint fault
|
||||
// through a real managed-local bd process, then retries twice. The issue row
|
||||
// proves the physical Dolt data survived the journal repair and idempotent
|
||||
// second invocation.
|
||||
func TestMigrateDoltModeDirectForwardFaultsFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, phase := range []string{"prepared", "target_configured", "old_controls_retired", "verified", "committed"} {
|
||||
t.Run(phase, func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--server", "--prefix", "fault", "--quiet")
|
||||
require.NoError(t, err, "init: %s", out)
|
||||
created, err := runBDExecWithBinary(t, bd, dir, env, "create", "migration sentinel", "--json")
|
||||
require.NoError(t, err, "create: %s", created)
|
||||
var row struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(created), &row))
|
||||
blockerID := createDependencyPair(t, bd, dir, env, row.ID)
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
touchFile(t, filepath.Join(root, "migration-sentinel"))
|
||||
sentinelBefore := snapshotMigrationTree(t, beadsDir, root)
|
||||
faultEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE="+phase)
|
||||
out, err = runBDExecWithBinary(t, bd, dir, faultEnv, "migrate", "from-server-to-proxied-server")
|
||||
require.Error(t, err, "fault phase unexpectedly succeeded: %s", out)
|
||||
exitErr, ok := err.(*exec.ExitError)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, exitErr.ExitCode())
|
||||
j, jErr := os.Stat(filepath.Join(beadsDir, migrateJournalFileName))
|
||||
require.NoError(t, jErr)
|
||||
require.False(t, j.IsDir())
|
||||
afterFailure := snapshotMigrationTree(t, beadsDir, root)
|
||||
require.Equal(t, sentinelBefore[filepath.Join(root, "migration-sentinel")], afterFailure[filepath.Join(root, "migration-sentinel")])
|
||||
retryEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE=")
|
||||
require.NoError(t, func() error {
|
||||
_, e := runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-server-to-proxied-server")
|
||||
return e
|
||||
}())
|
||||
after := snapshotMigrationTree(t, beadsDir, root)
|
||||
require.NoError(t, func() error {
|
||||
_, e := runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-server-to-proxied-server")
|
||||
return e
|
||||
}())
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir, root))
|
||||
assertDirectMigrationFinal(t, beadsDir, root, false, filepath.Join(root, "migration-sentinel"))
|
||||
assertDependencyPair(t, bd, dir, retryEnv, row.ID, blockerID)
|
||||
show, showErr := runBDExecWithBinary(t, bd, dir, retryEnv, "show", row.ID, "--json")
|
||||
require.NoError(t, showErr, "show sentinel: %s", show)
|
||||
assert.Contains(t, show, "migration sentinel")
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, retryEnv, "dolt", "stop")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeDirectReverseFaultsFrontDoor covers the reverse
|
||||
// managed-local transition through the executable for every checkpoint.
|
||||
func TestMigrateDoltModeDirectReverseFaultsFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, phase := range []string{"prepared", "target_configured", "old_controls_retired", "verified", "committed"} {
|
||||
t.Run(phase, func(t *testing.T) {
|
||||
dir, home := t.TempDir(), t.TempDir()
|
||||
env := migrationFrontDoorEnv(home)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--server", "--prefix", "reverse", "--quiet")
|
||||
require.NoError(t, err, "init: %s", out)
|
||||
created, err := runBDExecWithBinary(t, bd, dir, env, "create", "reverse migration sentinel", "--json")
|
||||
require.NoError(t, err, "create: %s", created)
|
||||
var row struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(created), &row))
|
||||
blockerID := createDependencyPair(t, bd, dir, env, row.ID)
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
_, err = runBDExecWithBinary(t, bd, dir, env, "migrate", "from-server-to-proxied-server")
|
||||
require.NoError(t, err)
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
touchFile(t, filepath.Join(root, "migration-sentinel"))
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
faultEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE="+phase)
|
||||
out, err = runBDExecWithBinary(t, bd, dir, faultEnv, "migrate", "from-proxied-server-to-server")
|
||||
require.Error(t, err, "fault phase unexpectedly succeeded: %s", out)
|
||||
exitErr, ok := err.(*exec.ExitError)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 1, exitErr.ExitCode())
|
||||
require.Equal(t, before[filepath.Join(root, "migration-sentinel")], snapshotMigrationTree(t, beadsDir, root)[filepath.Join(root, "migration-sentinel")])
|
||||
retryEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE=")
|
||||
out, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-proxied-server-to-server")
|
||||
require.NoError(t, err, "retry: %s", out)
|
||||
after := snapshotMigrationTree(t, beadsDir, root)
|
||||
_, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-proxied-server-to-server")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir, root))
|
||||
assertDirectMigrationFinal(t, beadsDir, root, true, filepath.Join(root, "migration-sentinel"))
|
||||
assertDependencyPair(t, bd, dir, retryEnv, row.ID, blockerID)
|
||||
show, err := runBDExecWithBinary(t, bd, dir, retryEnv, "show", row.ID, "--json")
|
||||
require.NoError(t, err, "show: %s", show)
|
||||
assert.Contains(t, show, "reverse migration sentinel")
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, retryEnv, "dolt", "stop")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertDirectMigrationFinal(t *testing.T, beadsDir, root string, reverse bool, sentinel string) {
|
||||
t.Helper()
|
||||
meta, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
||||
require.NoError(t, err)
|
||||
var got map[string]any
|
||||
require.NoError(t, json.Unmarshal(meta, &got))
|
||||
if reverse {
|
||||
assert.Equal(t, "server", got["dolt_mode"])
|
||||
_, err = os.Stat(filepath.Join(beadsDir, configfile.ProxiedServerClientInfoFileName))
|
||||
assert.True(t, os.IsNotExist(err), "reverse sidecar remains")
|
||||
if value, ok := config.WorkspaceYamlValue(beadsDir, "dolt.shared-server"); ok {
|
||||
assert.Equal(t, "false", strings.ToLower(value), "direct reverse must not enable shared topology")
|
||||
}
|
||||
for _, path := range proxy.ControlFilePaths(root) {
|
||||
_, statErr := os.Stat(path)
|
||||
if statErr == nil && (filepath.Base(path) == proxy.LockFileName || filepath.Base(path) == server.LockFileName) {
|
||||
lock, lockErr := util.TryLock(path)
|
||||
if lockErr == nil {
|
||||
lock.Unlock()
|
||||
continue
|
||||
}
|
||||
}
|
||||
assert.True(t, os.IsNotExist(statErr), "stale proxy control %s", path)
|
||||
}
|
||||
} else {
|
||||
assert.Equal(t, "proxied-server", got["dolt_mode"])
|
||||
info, infoErr := configfile.LoadProxiedServerClientInfo(beadsDir)
|
||||
require.NoError(t, infoErr)
|
||||
require.NotNil(t, info)
|
||||
assert.Equal(t, filepath.Clean(root), filepath.Clean(info.ResolvedRootPath(beadsDir)))
|
||||
for _, path := range serverAssetNames() {
|
||||
_, statErr := os.Stat(filepath.Join(beadsDir, path))
|
||||
assert.True(t, os.IsNotExist(statErr), "stale server control %s", path)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{filepath.Join(beadsDir, migrateJournalFileName), filepath.Join(beadsDir, migrateLockFileName)} {
|
||||
_, statErr := os.Stat(path)
|
||||
assert.True(t, os.IsNotExist(statErr), "stale migration artifact %s", path)
|
||||
}
|
||||
sentinelBytes, sentinelErr := os.ReadFile(sentinel)
|
||||
require.NoError(t, sentinelErr)
|
||||
assert.Equal(t, []byte("x"), sentinelBytes, "migration changed Dolt root sentinel")
|
||||
doltState, _ := doltserver.IsRunning(beadsDir)
|
||||
assert.False(t, doltState != nil && doltState.Running, "direct Dolt process still running")
|
||||
proxyRunning, _ := proxy.IsRunning(root)
|
||||
assert.False(t, proxyRunning, "direct proxy process still running")
|
||||
}
|
||||
|
||||
// sharedMigrationFrontDoorSnapshot records both durable migration artifacts
|
||||
// and the live ownership probes. The latter are deliberately queried rather
|
||||
// than represented by pid/status files, which can be stale after a crash.
|
||||
type sharedMigrationFrontDoorSnapshot struct {
|
||||
Tree map[string][]byte
|
||||
Controls map[string]string
|
||||
DoltRunning bool
|
||||
DoltPID int
|
||||
DoltPort int
|
||||
ProxyRunning bool
|
||||
}
|
||||
|
||||
func snapshotSharedMigrationFrontDoor(t *testing.T, beadsDir, sharedDir, root string) sharedMigrationFrontDoorSnapshot {
|
||||
t.Helper()
|
||||
doltState, _ := doltserver.IsRunning(sharedDir)
|
||||
doltRunning := doltState != nil && doltState.Running
|
||||
doltPID, doltPort := 0, 0
|
||||
if doltState != nil {
|
||||
doltPID, doltPort = doltState.PID, doltState.Port
|
||||
}
|
||||
proxyRunning, _ := proxy.IsRunning(root)
|
||||
controls := map[string]string{}
|
||||
paths := append([]string{
|
||||
filepath.Join(beadsDir, "metadata.json"),
|
||||
filepath.Join(beadsDir, "config.yaml"),
|
||||
filepath.Join(beadsDir, configfile.ProxiedServerClientInfoFileName),
|
||||
filepath.Join(beadsDir, migrateJournalFileName),
|
||||
filepath.Join(beadsDir, migrateLockFileName),
|
||||
filepath.Join(beadsDir, "dolt.gate.lock"),
|
||||
}, doltserver.StateFilePaths(sharedDir)...)
|
||||
paths = append(paths, proxy.ControlFilePaths(root)...)
|
||||
for _, path := range paths {
|
||||
contents, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
controls[path] = "missing"
|
||||
continue
|
||||
}
|
||||
controls[path] = string(contents)
|
||||
}
|
||||
return sharedMigrationFrontDoorSnapshot{
|
||||
Tree: snapshotMigrationTree(t, beadsDir, sharedDir),
|
||||
Controls: controls,
|
||||
DoltRunning: doltRunning,
|
||||
DoltPID: doltPID,
|
||||
DoltPort: doltPort,
|
||||
ProxyRunning: proxyRunning,
|
||||
}
|
||||
}
|
||||
|
||||
func setupSharedMigrationFrontDoor(t *testing.T, bd string) (dir, home, sharedDir, sharedRoot, issueID, blockerID string, env []string) {
|
||||
t.Helper()
|
||||
dir, home = t.TempDir(), t.TempDir()
|
||||
sharedDir = filepath.Join(home, "shared-server")
|
||||
sharedRoot = filepath.Join(sharedDir, "dolt")
|
||||
env = migrationFrontDoorEnv(home)
|
||||
portProbe, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := portProbe.Addr().(*net.TCPAddr).Port
|
||||
require.NoError(t, portProbe.Close())
|
||||
env = append(env, "BEADS_SHARED_SERVER_DIR="+sharedDir, "BEADS_DOLT_SHARED_SERVER=1", "BEADS_DOLT_SERVER_PORT="+strconv.Itoa(port))
|
||||
out, err := runBDExecWithBinary(t, bd, dir, env, "init", "--backend", "dolt", "--shared-server", "--prefix", "shared", "--quiet")
|
||||
require.NoError(t, err, "shared init: %s", out)
|
||||
created, err := runBDExecWithBinary(t, bd, dir, env, "create", "shared migration sentinel", "--json")
|
||||
require.NoError(t, err, "shared create: %s", created)
|
||||
var row struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal([]byte(created), &row))
|
||||
blockerID = createDependencyPair(t, bd, dir, env, row.ID)
|
||||
require.NotEmpty(t, row.ID)
|
||||
// Migrations require exclusive ownership. Stop the real shared Dolt
|
||||
// process before taking the baseline snapshot.
|
||||
status, _ := runBDExecWithBinary(t, bd, dir, env, "dolt", "status")
|
||||
if strings.Contains(status, "Dolt server: running") {
|
||||
stop, stopErr := runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
require.NoError(t, stopErr, "stop shared server: %s", stop)
|
||||
}
|
||||
return dir, home, sharedDir, sharedRoot, row.ID, blockerID, env
|
||||
}
|
||||
|
||||
func assertSharedMigrationResult(t *testing.T, bd, dir string, env []string, beadsDir, sharedDir, sharedRoot, issueID, blockerID string, wantShared bool) {
|
||||
t.Helper()
|
||||
meta, err := os.ReadFile(filepath.Join(beadsDir, "metadata.json"))
|
||||
require.NoError(t, err)
|
||||
var metadata map[string]any
|
||||
require.NoError(t, json.Unmarshal(meta, &metadata))
|
||||
wantMode := "proxied-server"
|
||||
if wantShared {
|
||||
wantMode = "server"
|
||||
}
|
||||
assert.Equal(t, wantMode, metadata["dolt_mode"])
|
||||
yamlValue, yamlSet := readWorkspaceYamlValueForFrontDoor(t, beadsDir, "dolt.shared-server")
|
||||
assert.True(t, yamlSet)
|
||||
assert.Equal(t, fmt.Sprintf("%t", wantShared), strings.ToLower(yamlValue))
|
||||
sidecarPath := filepath.Join(beadsDir, "proxied_server_client_info.json")
|
||||
if wantShared {
|
||||
_, statErr := os.Stat(sidecarPath)
|
||||
assert.True(t, os.IsNotExist(statErr), "reverse migration must remove sidecar")
|
||||
} else {
|
||||
sidecar, sidecarErr := os.ReadFile(sidecarPath)
|
||||
require.NoError(t, sidecarErr)
|
||||
assert.Contains(t, string(sidecar), sharedRoot)
|
||||
}
|
||||
_, journalErr := os.Stat(filepath.Join(beadsDir, migrateJournalFileName))
|
||||
assert.True(t, os.IsNotExist(journalErr), "migration journal must be finalized")
|
||||
// Probe ownership before `show`, because opening a shared workspace can
|
||||
// legitimately auto-start its managed server.
|
||||
doltState, _ := doltserver.IsRunning(sharedDir)
|
||||
assert.False(t, doltState != nil && doltState.Running, "shared Dolt process still running before show")
|
||||
proxyRunning, _ := proxy.IsRunning(sharedRoot)
|
||||
assert.False(t, proxyRunning, "proxy process still running before show")
|
||||
if wantShared {
|
||||
// Shared-server mode is an externally addressed managed server and does
|
||||
// not auto-start on read paths. Start it explicitly to prove the real
|
||||
// issue row remains readable after reverse migration.
|
||||
start, startErr := runBDExecWithBinary(t, bd, dir, env, "dolt", "start")
|
||||
require.NoError(t, startErr, "start shared server for verification: %s", start)
|
||||
}
|
||||
show, showErr := runBDExecWithBinary(t, bd, dir, env, "show", issueID, "--json")
|
||||
require.NoError(t, showErr, "show migrated issue: %s", show)
|
||||
assert.Contains(t, show, "shared migration sentinel")
|
||||
if blockerID != "" {
|
||||
assertDependencyPair(t, bd, dir, env, issueID, blockerID)
|
||||
}
|
||||
// `show` may auto-start the managed shared server. Stop it before probing
|
||||
// final ownership so the idempotence assertion covers the migration itself.
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
// Neither migration direction may leave a managed child alive.
|
||||
doltState, _ = doltserver.IsRunning(sharedDir)
|
||||
assert.False(t, doltState != nil && doltState.Running, "shared Dolt process still running")
|
||||
proxyRunning, _ = proxy.IsRunning(sharedRoot)
|
||||
assert.False(t, proxyRunning, "proxy process still running")
|
||||
}
|
||||
|
||||
func readWorkspaceYamlValueForFrontDoor(t *testing.T, beadsDir, key string) (string, bool) {
|
||||
t.Helper()
|
||||
value, ok := config.WorkspaceYamlValue(beadsDir, key)
|
||||
return value, ok
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeSharedForwardFaultsFrontDoor exercises all five
|
||||
// checkpoints through the real cgo bd subprocess using a disposable managed
|
||||
// shared-server root. Each injected fault is repaired by a retry and a second
|
||||
// successful invocation proves idempotence without changing durable state.
|
||||
func TestMigrateDoltModeSharedForwardFaultsFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, phase := range []string{"prepared", "target_configured", "old_controls_retired", "verified", "committed"} {
|
||||
t.Run(phase, func(t *testing.T) {
|
||||
dir, _, sharedDir, sharedRoot, issueID, blockerID, env := setupSharedMigrationFrontDoor(t, bd)
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
touchFile(t, filepath.Join(sharedRoot, "migration-sentinel"))
|
||||
before := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
faultEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE="+phase)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, faultEnv, "migrate", "from-shared-server-to-proxied-server")
|
||||
require.Error(t, err, "fault phase unexpectedly succeeded: %s", out)
|
||||
require.FileExists(t, filepath.Join(beadsDir, migrateJournalFileName))
|
||||
failed := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
assert.Equal(t, before.Tree[filepath.Join(sharedRoot, "migration-sentinel")], failed.Tree[filepath.Join(sharedRoot, "migration-sentinel")])
|
||||
retryEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE=")
|
||||
out, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-shared-server-to-proxied-server")
|
||||
require.NoError(t, err, "retry: %s", out)
|
||||
after := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
out, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-shared-server-to-proxied-server")
|
||||
require.NoError(t, err, "second success: %s", out)
|
||||
assert.Equal(t, after, snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot))
|
||||
assertSharedMigrationResult(t, bd, dir, retryEnv, beadsDir, sharedDir, sharedRoot, issueID, blockerID, false)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateDoltModeSharedReverseFaultsFrontDoor is the reverse-direction
|
||||
// counterpart of TestMigrateDoltModeSharedForwardFaultsFrontDoor.
|
||||
func TestMigrateDoltModeSharedReverseFaultsFrontDoor(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_MIGRATION_FRONTDOOR") != "1" {
|
||||
t.Skip("set BEADS_TEST_MIGRATION_FRONTDOOR=1")
|
||||
}
|
||||
bd := migrationFrontDoorBinary(t)
|
||||
for _, phase := range []string{"prepared", "target_configured", "old_controls_retired", "verified", "committed"} {
|
||||
t.Run(phase, func(t *testing.T) {
|
||||
dir, _, sharedDir, sharedRoot, issueID, blockerID, env := setupSharedMigrationFrontDoor(t, bd)
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
_, err := runBDExecWithBinary(t, bd, dir, env, "migrate", "from-shared-server-to-proxied-server")
|
||||
require.NoError(t, err)
|
||||
// Forward migration leaves the proxy stopped; keep this explicit so
|
||||
// the reverse matrix never races a child process from auto-start.
|
||||
_, _ = runBDExecWithBinary(t, bd, dir, env, "dolt", "stop")
|
||||
touchFile(t, filepath.Join(sharedRoot, "migration-sentinel"))
|
||||
before := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
faultEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE="+phase)
|
||||
out, err := runBDExecWithBinary(t, bd, dir, faultEnv, "migrate", "from-proxied-server-to-shared-server")
|
||||
require.Error(t, err, "fault phase unexpectedly succeeded: %s", out)
|
||||
require.FileExists(t, filepath.Join(beadsDir, migrateJournalFileName))
|
||||
failed := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
assert.Equal(t, before.Tree[filepath.Join(sharedRoot, "migration-sentinel")], failed.Tree[filepath.Join(sharedRoot, "migration-sentinel")])
|
||||
retryEnv := append(append([]string(nil), env...), "BEADS_MIGRATION_FAIL_PHASE=")
|
||||
out, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-proxied-server-to-shared-server")
|
||||
require.NoError(t, err, "retry: %s", out)
|
||||
after := snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot)
|
||||
out, err = runBDExecWithBinary(t, bd, dir, retryEnv, "migrate", "from-proxied-server-to-shared-server")
|
||||
require.NoError(t, err, "second success: %s", out)
|
||||
assert.Equal(t, after, snapshotSharedMigrationFrontDoor(t, beadsDir, sharedDir, sharedRoot))
|
||||
assertSharedMigrationResult(t, bd, dir, retryEnv, beadsDir, sharedDir, sharedRoot, issueID, blockerID, true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +1,74 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
|
||||
)
|
||||
|
||||
func captureMigrationJSON(t *testing.T, fn func() error) (string, error) {
|
||||
t.Helper()
|
||||
stdioMutex.Lock()
|
||||
defer stdioMutex.Unlock()
|
||||
old := os.Stdout
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
os.Stdout = w
|
||||
done := make(chan string, 1)
|
||||
go func() {
|
||||
var buf bytes.Buffer
|
||||
_, _ = io.Copy(&buf, r)
|
||||
done <- buf.String()
|
||||
}()
|
||||
callErr := fn()
|
||||
_ = w.Close()
|
||||
os.Stdout = old
|
||||
out := <-done
|
||||
_ = r.Close()
|
||||
return out, callErr
|
||||
}
|
||||
|
||||
func snapshotMigrationTree(t *testing.T, roots ...string) map[string][]byte {
|
||||
t.Helper()
|
||||
out := map[string][]byte{}
|
||||
for _, root := range roots {
|
||||
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if strings.HasSuffix(path, ".gate.lock") || strings.HasSuffix(path, migrateLockFileName) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case info.Mode()&os.ModeSymlink != 0:
|
||||
target, _ := os.Readlink(path)
|
||||
out[path] = []byte("symlink:" + target)
|
||||
case info.IsDir():
|
||||
out[path] = []byte("dir:" + info.Mode().String())
|
||||
case info.Mode().IsRegular():
|
||||
b, _ := os.ReadFile(path)
|
||||
out[path] = append([]byte("file:"+info.Mode().String()+"\x00"), b...)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func migrateModeWorkspace(t *testing.T, mode string) string {
|
||||
t.Helper()
|
||||
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", "")
|
||||
|
|
@ -22,6 +79,8 @@ func migrateModeWorkspace(t *testing.T, mode string) string {
|
|||
dir := t.TempDir()
|
||||
beadsDir := filepath.Join(dir, ".beads")
|
||||
require.NoError(t, os.MkdirAll(beadsDir, 0o755))
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(beadsDir, "dolt", ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "dolt", ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
writeMetadataConfig(t, beadsDir, mode, "myproj")
|
||||
t.Chdir(dir)
|
||||
return beadsDir
|
||||
|
|
@ -33,6 +92,13 @@ func touchFile(t *testing.T, path string) {
|
|||
require.NoError(t, os.WriteFile(path, []byte("x"), 0o600))
|
||||
}
|
||||
|
||||
func mustReadFile(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(path)
|
||||
require.NoError(t, err)
|
||||
return b
|
||||
}
|
||||
|
||||
func serverAssetNames() []string {
|
||||
return []string{"dolt-server.pid", "dolt-server.port", "dolt-server.lock", "dolt-server.log", "dolt-server.log.1"}
|
||||
}
|
||||
|
|
@ -73,6 +139,33 @@ func TestMigrateToProxiedServer_RejectsNonServerMode(t *testing.T) {
|
|||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_MissingRootFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.RemoveAll(filepath.Join(beadsDir, "dolt")))
|
||||
before, err := os.ReadFile(configfile.ConfigPath(beadsDir))
|
||||
require.NoError(t, err)
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
after, err := os.ReadFile(configfile.ConfigPath(beadsDir))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, before, after)
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_MalformedRootIdentityFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "dolt", ".dolt", "repo_state.json"), []byte("not-json"), 0o600))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_InvalidRootIdentityShapesFailClosed(t *testing.T) {
|
||||
for _, body := range []string{`{}`, `null`, `{"garbage":true}`, `{"head":null,"remotes":{},"backups":{},"branches":{}}`, `{"head":"refs/heads/","remotes":{},"backups":{},"branches":{}}`} {
|
||||
t.Run(body, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "dolt", ".dolt", "repo_state.json"), []byte(body), 0o600))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_DryRunWritesNothing(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
touchFile(t, filepath.Join(beadsDir, "dolt-server.log"))
|
||||
|
|
@ -90,6 +183,133 @@ func TestMigrateToProxiedServer_DryRunWritesNothing(t *testing.T) {
|
|||
require.NoError(t, assetErr, "dry-run must not delete assets")
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_MalformedWorkspaceYAMLFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
configPath := filepath.Join(beadsDir, "config.yaml")
|
||||
require.NoError(t, os.WriteFile(configPath, []byte("dolt: ["), 0o600))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = runMigrateToProxiedServer(false, 0, false) })
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, strings.ToLower(stderr), "parsing workspace config")
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
}
|
||||
|
||||
func TestMigrationSharedTopology_ExplicitYAMLOverridesEnvironment(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
env string
|
||||
yaml string
|
||||
want bool
|
||||
}{
|
||||
{name: "false overrides true env", env: "1", yaml: "false", want: false},
|
||||
{name: "true overrides empty env", env: "", yaml: "true", want: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", tc.env)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: "+tc.yaml+"\n"), 0o600))
|
||||
live, value, present, err := migrationSharedTopology(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, present)
|
||||
assert.Equal(t, tc.want, value)
|
||||
assert.Equal(t, tc.want, live)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_UnreadableWorkspaceYAMLFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
configPath := filepath.Join(beadsDir, "config.yaml")
|
||||
require.NoError(t, os.Mkdir(configPath, 0o700))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = runMigrateToProxiedServer(false, 0, false) })
|
||||
require.Error(t, err)
|
||||
code, ok := exitCodeFromError(err)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, code)
|
||||
assert.Contains(t, strings.ToLower(stderr), "reading workspace config")
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedRejectsStaleControl(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
touchFile(t, filepath.Join(root, "proxy.log"))
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir, root))
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedRejectsBadSidecar(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
info *configfile.ProxiedServerClientInfo
|
||||
}{
|
||||
{"external", &configfile.ProxiedServerClientInfo{RootPath: "/tmp/ext", External: &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}}},
|
||||
{"mismatch", &configfile.ProxiedServerClientInfo{RootPath: "/tmp/other"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, tc.info))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_StaleSidecarFailsClosed(t *testing.T) {
|
||||
rootCases := []struct {
|
||||
name string
|
||||
info *configfile.ProxiedServerClientInfo
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "external endpoint",
|
||||
info: &configfile.ProxiedServerClientInfo{
|
||||
RootPath: filepath.Join("/", "external-root"),
|
||||
External: &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307},
|
||||
},
|
||||
want: "externally hosted proxied dolt endpoint",
|
||||
},
|
||||
{
|
||||
name: "mismatched local root",
|
||||
info: &configfile.ProxiedServerClientInfo{RootPath: filepath.Join("/", "another-repo")},
|
||||
want: "sidecar root",
|
||||
},
|
||||
{
|
||||
name: "matching relative root",
|
||||
info: &configfile.ProxiedServerClientInfo{RootPath: "dolt"},
|
||||
want: "exists without a recoverable journal",
|
||||
},
|
||||
}
|
||||
for _, tc := range rootCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, tc.info))
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
var migrateErr error
|
||||
stderr := captureStderr(t, func() {
|
||||
migrateErr = runMigrateToProxiedServer(false, 0, false)
|
||||
})
|
||||
require.Error(t, migrateErr)
|
||||
assert.Contains(t, strings.ToLower(stderr), tc.want)
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir, root))
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, cfg.IsDoltServerMode())
|
||||
journal, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, journal)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateToServer_FlipsModeAndRemovesSidecar(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{}))
|
||||
|
|
@ -226,6 +446,8 @@ func TestMigrateSharedToProxiedServer_RootsAtSharedDir(t *testing.T) {
|
|||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(sharedDir, "dolt", ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(sharedDir, "dolt", ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
|
||||
|
|
@ -244,6 +466,50 @@ func TestMigrateSharedToProxiedServer_RootsAtSharedDir(t *testing.T) {
|
|||
assert.NotContains(t, string(body), "shared-server: true", "dolt.shared-server must be turned off")
|
||||
}
|
||||
|
||||
func TestMigrateSharedToProxiedServer_AllCheckpointFaultsRetry(t *testing.T) {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
root := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
sentinel := filepath.Join(root, "migration-sentinel")
|
||||
touchFile(t, sentinel)
|
||||
sentinelBefore, err := os.ReadFile(sentinel)
|
||||
require.NoError(t, err)
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(phase))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, true))
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, j)
|
||||
assert.Equal(t, phase, j.Phase)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, true))
|
||||
after := snapshotMigrationTree(t, beadsDir, root)
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, true))
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir, root), "retry changed durable migration artifacts")
|
||||
assert.Equal(t, sentinelBefore, mustReadFile(t, sentinel), "migration sentinel changed")
|
||||
running, _ := proxy.IsRunning(root)
|
||||
assert.False(t, running, "retry left a proxy process running")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateSharedToProxiedServer_DryRunDoesNotCreateRoot(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
require.NoError(t, runMigrateToProxiedServer(true, 0, true))
|
||||
_, err := os.Stat(filepath.Join(sharedDir, "dolt"))
|
||||
assert.True(t, os.IsNotExist(err), "dry-run must not create shared root")
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_RejectsSharedRepo(t *testing.T) {
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
|
|
@ -262,6 +528,7 @@ func TestMigrateProxiedToSharedServer_Reverse(t *testing.T) {
|
|||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: false\n"), 0o600))
|
||||
sharedDolt := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(sharedDolt, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(sharedDolt, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: sharedDolt}))
|
||||
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, true))
|
||||
|
|
@ -277,12 +544,66 @@ func TestMigrateProxiedToSharedServer_Reverse(t *testing.T) {
|
|||
assert.Contains(t, string(body), "shared-server: true", "dolt.shared-server must be re-enabled")
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedToSharedServer_AllCheckpointFaultsRetry(t *testing.T) {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "")
|
||||
root := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
sentinel := filepath.Join(root, "migration-sentinel")
|
||||
touchFile(t, sentinel)
|
||||
sentinelBefore, err := os.ReadFile(sentinel)
|
||||
require.NoError(t, err)
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: false\n"), 0o600))
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(phase))
|
||||
require.Error(t, runMigrateFromProxiedServer(false, true))
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, j)
|
||||
assert.Equal(t, phase, j.Phase)
|
||||
assert.Equal(t, 1, j.Attempt)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, true))
|
||||
after := snapshotMigrationTree(t, beadsDir, root)
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, true))
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir, root), "retry changed durable migration artifacts")
|
||||
assert.Equal(t, sentinelBefore, mustReadFile(t, sentinel), "migration sentinel changed")
|
||||
running, _ := proxy.IsRunning(root)
|
||||
assert.False(t, running, "retry left a proxy process running")
|
||||
cfg, _ := configfile.Load(beadsDir)
|
||||
assert.True(t, cfg.IsDoltServerMode())
|
||||
v, ok := config.WorkspaceYamlValue(beadsDir, "dolt.shared-server")
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "true", strings.ToLower(v))
|
||||
_, err = os.Stat(configfile.ProxiedServerClientInfoPath(beadsDir))
|
||||
assert.True(t, os.IsNotExist(err))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateProxiedToSharedServer_DryRunDoesNotCreateRoot(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
sharedDolt := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: sharedDolt}))
|
||||
require.NoError(t, runMigrateFromProxiedServer(true, true))
|
||||
_, err := os.Stat(sharedDolt)
|
||||
assert.True(t, os.IsNotExist(err), "dry-run must not create shared root")
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedToServer_RejectsSharedRooted(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
sharedDolt := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(sharedDolt, 0o755))
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(sharedDolt, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(sharedDolt, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: sharedDolt}))
|
||||
|
||||
require.Error(t, runMigrateFromProxiedServer(false, false), "non-shared reverse must reject a shared-rooted proxied repo")
|
||||
|
|
@ -290,9 +611,511 @@ func TestMigrateFromProxiedToServer_RejectsSharedRooted(t *testing.T) {
|
|||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedIsNoop(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: filepath.Join(beadsDir, "dolt")}))
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, false))
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, cfg.IsDoltProxiedServerMode())
|
||||
}
|
||||
|
||||
func TestMigrateSharedToProxiedServer_AlreadyProxiedUsesSharedRootAndStaleEnv(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "true") // stale ambient state after migration
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: false\n"), 0o600))
|
||||
root := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, true))
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedAllowsUnlockedLockMarkers(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
for _, name := range []string{proxy.LockFileName, server.LockFileName} {
|
||||
touchFile(t, filepath.Join(root, name))
|
||||
}
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, false))
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedRefusalsAreTyped(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
info *configfile.ProxiedServerClientInfo
|
||||
control string
|
||||
}{
|
||||
{name: "external sidecar", info: &configfile.ProxiedServerClientInfo{RootPath: "/tmp/external", External: &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}}},
|
||||
{name: "mismatched sidecar", info: &configfile.ProxiedServerClientInfo{RootPath: "/tmp/other"}},
|
||||
{name: "stale control", control: proxy.LogFileName},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
if tc.info == nil {
|
||||
tc.info = &configfile.ProxiedServerClientInfo{RootPath: root}
|
||||
}
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, tc.info))
|
||||
if tc.control != "" {
|
||||
touchFile(t, filepath.Join(root, tc.control))
|
||||
}
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
out, err := captureMigrationJSON(t, func() error { return runMigrateToProxiedServer(false, 0, false) })
|
||||
code, ok := exitCodeFromError(err)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, code)
|
||||
var got map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(out), &got), "output=%q", out)
|
||||
assert.Equal(t, "proxy.migrate.invalid_state", got["code"])
|
||||
assert.Equal(t, false, got["mutates"])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AlreadyProxiedRefusesHeldLockAsTyped(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
held, err := util.TryLock(filepath.Join(root, proxy.LockFileName))
|
||||
require.NoError(t, err)
|
||||
defer held.Unlock()
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
out, err := captureMigrationJSON(t, func() error { return runMigrateToProxiedServer(false, 0, false) })
|
||||
code, ok := exitCodeFromError(err)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, code)
|
||||
var got map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(out), &got))
|
||||
assert.Equal(t, "proxy.migrate.invalid_state", got["code"])
|
||||
assert.Equal(t, false, got["mutates"])
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_RetryAfterCheckpointFault(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(migrateTargetConfigured))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, j)
|
||||
assert.Equal(t, migrateTargetConfigured, j.Phase)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, false))
|
||||
j, err = loadMigrateJournal(beadsDir)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, j, "successful migration removes its journal")
|
||||
}
|
||||
|
||||
// TestMigrateToProxiedServer_ResumeReplaysRecordedIdleTimeout covers the resume
|
||||
// case every other fault-retry test misses: the retry replays identical argv, so
|
||||
// the sidecar is only ever rebuilt in its trivially-consistent form. A resume
|
||||
// that changes --idle-timeout (or omits it, which resolves differently again)
|
||||
// must replay the journal's recorded plan. Rebuilding the sidecar from the
|
||||
// current argv persists the divergence before the equality check detects it, and
|
||||
// because the rebuild is gated on phase == migratePrepared, re-supplying the
|
||||
// original value cannot repair it afterwards.
|
||||
func TestMigrateToProxiedServer_ResumeReplaysRecordedIdleTimeout(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
resume time.Duration
|
||||
}{
|
||||
{name: "changed", resume: 5 * time.Minute},
|
||||
{name: "omitted", resume: 0},
|
||||
{name: "never", resume: proxy.IdleTimeoutNever},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
recorded := 90 * time.Second
|
||||
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(migratePrepared))
|
||||
require.Error(t, runMigrateToProxiedServer(false, recorded, false))
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, j)
|
||||
require.Equal(t, migratePrepared, j.Phase)
|
||||
require.NotNil(t, j.Sidecar)
|
||||
require.Equal(t, recorded, j.Sidecar.IdleTimeout)
|
||||
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateToProxiedServer(false, tc.resume, false))
|
||||
|
||||
j, err = loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, j, "successful migration removes its journal")
|
||||
info, err := configfile.LoadProxiedServerClientInfo(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, info)
|
||||
assert.Equal(t, recorded, info.IdleTimeout, "sidecar must hold the recorded plan, not the resume argv")
|
||||
|
||||
// The reverse migration is the documented escape hatch; it compares
|
||||
// the same two values, so it must also accept the resumed state.
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, false))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateJournal_UnknownPhaseFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
path := migrateJournalPath(beadsDir)
|
||||
require.NoError(t, os.WriteFile(path, []byte(`{"version":1,"source_mode":"server","target_mode":"proxied-server","phase":"surprise","attempt":1}`), 0o600))
|
||||
_, err := loadMigrateJournal(beadsDir)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMigrateToProxiedServer_AllCheckpointFaultsRetry(t *testing.T) {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(phase))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, false))
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, j)
|
||||
assert.Equal(t, phase, j.Phase)
|
||||
assert.Equal(t, 1, j.Attempt)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, false))
|
||||
after := snapshotMigrationTree(t, beadsDir)
|
||||
j, err = loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, j)
|
||||
require.NoError(t, runMigrateToProxiedServer(false, 0, false))
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_MalformedSidecarFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, os.WriteFile(configfile.ProxiedServerClientInfoPath(beadsDir), []byte("{bad"), 0o600))
|
||||
require.Error(t, runMigrateFromProxiedServer(false, false))
|
||||
data, err := os.ReadFile(configfile.ConfigPath(beadsDir))
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(data), configfile.DoltModeProxiedServer)
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_AlreadyServerRejectsMalformedSidecar(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
path := configfile.ProxiedServerClientInfoPath(beadsDir)
|
||||
require.NoError(t, os.WriteFile(path, []byte("{bad"), 0o600))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = runMigrateFromProxiedServer(false, false) })
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, strings.ToLower(stderr), "sidecar")
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_AlreadyTargetRejectsStaleSidecar(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: filepath.Join(beadsDir, "dolt")}))
|
||||
before := snapshotMigrationTree(t, beadsDir)
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = runMigrateFromProxiedServer(false, false) })
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, strings.ToLower(stderr), "stale")
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir))
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedToSharedServer_AlreadyTargetRejectsStaleSidecar(t *testing.T) {
|
||||
sharedDir := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", sharedDir)
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
root := filepath.Join(sharedDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = runMigrateFromProxiedServer(false, true) })
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, strings.ToLower(stderr), "stale")
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir, root))
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_JournalPreservesExternalSidecar(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
ext := &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307, User: "alice"}
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root, External: ext}))
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.Error(t, runMigrateFromProxiedServer(false, false), "external endpoint cannot be converted to local server")
|
||||
got, err := configfile.LoadProxiedServerClientInfo(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, ext, got.External)
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, cfg.IsDoltProxiedServerMode())
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_AllCheckpointFaultsRetry(t *testing.T) {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
sentinel := filepath.Join(root, "migration-sentinel")
|
||||
touchFile(t, sentinel)
|
||||
sentinelBefore, err := os.ReadFile(sentinel)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: root}))
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(phase))
|
||||
require.Error(t, runMigrateFromProxiedServer(false, false))
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, false))
|
||||
after := snapshotMigrationTree(t, beadsDir)
|
||||
assert.Equal(t, sentinelBefore, mustReadFile(t, sentinel), "migration sentinel changed")
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, j)
|
||||
require.NoError(t, runMigrateFromProxiedServer(false, false))
|
||||
assert.Equal(t, after, snapshotMigrationTree(t, beadsDir))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_ExternalJournalAlwaysFailsClosed(t *testing.T) {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(root, 0o755))
|
||||
if phase != migratePrepared {
|
||||
writeMetadataConfig(t, beadsDir, configfile.DoltModeServer, "myproj")
|
||||
}
|
||||
wantMode := configfile.DoltModeServer
|
||||
if phase == migratePrepared {
|
||||
wantMode = configfile.DoltModeProxiedServer
|
||||
}
|
||||
ext := &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeProxiedServer, TargetMode: configfile.DoltModeServer, RootPath: root, External: ext, Ownership: "external", Attempt: 1, Phase: phase, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root, External: ext}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
require.Error(t, runMigrateFromProxiedServer(false, false))
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, wantMode, cfg.GetDoltMode())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateExternalJournalAlwaysFailsClosed_TCPAndUnixForwardReverse(t *testing.T) {
|
||||
endpoints := []struct {
|
||||
name string
|
||||
cfg *configfile.ExternalDoltConfig
|
||||
}{
|
||||
{"tcp", &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}},
|
||||
{"unix", &configfile.ExternalDoltConfig{Socket: "/tmp/beads-dolt.sock"}},
|
||||
}
|
||||
for _, ep := range endpoints {
|
||||
for _, reverse := range []bool{false, true} {
|
||||
for _, phase := range []migratePhase{migratePrepared, migrateTargetConfigured, migrateOldControlsRetired, migrateVerified, migrateCommitted} {
|
||||
t.Run(ep.name+"/"+string(phase), func(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
want := configfile.DoltModeServer
|
||||
src := configfile.DoltModeServer
|
||||
target := configfile.DoltModeProxiedServer
|
||||
if reverse {
|
||||
src, target = configfile.DoltModeProxiedServer, configfile.DoltModeServer
|
||||
want = configfile.DoltModeProxiedServer
|
||||
}
|
||||
// A prepared journal may legitimately coexist with either
|
||||
// source or target metadata. Later phases require target
|
||||
// metadata so validation reaches the ownership refusal.
|
||||
metadataMode := src
|
||||
if phase != migratePrepared {
|
||||
metadataMode = target
|
||||
}
|
||||
want = metadataMode
|
||||
writeMetadataConfig(t, beadsDir, metadataMode, "myproj")
|
||||
j := migrateJournal{Version: 1, SourceMode: src, TargetMode: target, RootPath: root, External: ep.cfg, Ownership: "external", Attempt: 1, Phase: phase, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root, External: ep.cfg}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
treeBefore := snapshotMigrationTree(t, beadsDir, root)
|
||||
before, _ := os.ReadFile(configfile.ConfigPath(beadsDir))
|
||||
var migrateErr error
|
||||
stderr := captureStderr(t, func() {
|
||||
if reverse {
|
||||
migrateErr = runMigrateFromProxiedServer(false, false)
|
||||
} else {
|
||||
migrateErr = runMigrateToProxiedServer(false, 0, false)
|
||||
}
|
||||
})
|
||||
require.Error(t, migrateErr)
|
||||
assert.Contains(t, strings.ToLower(stderr), "externally hosted proxied dolt endpoint")
|
||||
after, _ := os.ReadFile(configfile.ConfigPath(beadsDir))
|
||||
assert.Equal(t, before, after)
|
||||
cfg, _ := configfile.Load(beadsDir)
|
||||
assert.Equal(t, want, cfg.GetDoltMode())
|
||||
assert.Equal(t, treeBefore, snapshotMigrationTree(t, beadsDir, root))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateSharedJournalRootMismatchFailsClosed(t *testing.T) {
|
||||
shared := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", shared)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeServer, TargetMode: configfile.DoltModeProxiedServer, Shared: true, RootPath: root, Ownership: "managed-local", Attempt: 1, Phase: migratePrepared, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, true))
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, cfg.IsDoltServerMode())
|
||||
}
|
||||
|
||||
func TestMigrateSharedJournalMissingYAMLFailsClosed(t *testing.T) {
|
||||
shared := t.TempDir()
|
||||
t.Setenv("BEADS_SHARED_SERVER_DIR", shared)
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
root := filepath.Join(shared, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(root, ".dolt", "repo_state.json"), []byte(`{"head":"refs/heads/main","remotes":{},"backups":{},"branches":{}}`), 0o600))
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeServer, TargetMode: configfile.DoltModeProxiedServer, Shared: true, RootPath: root, Ownership: "managed-local", Attempt: 1, Phase: migratePrepared, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root}}
|
||||
b, _ := json.Marshal(j)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
require.Error(t, runMigrateToProxiedServer(false, 0, true))
|
||||
}
|
||||
|
||||
func TestMigrateNonSharedJournalRejectsPersistedSharedYAML(t *testing.T) {
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1") // stale ambient environment
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: true\n"), 0o600))
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeServer, TargetMode: configfile.DoltModeProxiedServer, RootPath: root, Ownership: "managed-local", Attempt: 1, Phase: migrateTargetConfigured, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
err = runMigrateToProxiedServer(false, 0, false)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMigrateJournalSidecarRootMismatchFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeServer, TargetMode: configfile.DoltModeProxiedServer, RootPath: filepath.Join(beadsDir, "dolt"), Ownership: "managed-local", Attempt: 1, Phase: migratePrepared, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: filepath.Join(beadsDir, "other")}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
_, err = loadMigrateJournal(beadsDir)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestMigrateJournalExternalTopologyMismatchFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
ext := &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
ownership string
|
||||
journal *configfile.ExternalDoltConfig
|
||||
sidecar *configfile.ExternalDoltConfig
|
||||
}{
|
||||
{name: "journal external differs", ownership: "external", journal: &configfile.ExternalDoltConfig{Host: "other.example", Port: 3307}, sidecar: ext},
|
||||
{name: "journal unexpected external", ownership: "managed-local", journal: ext, sidecar: nil},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeProxiedServer, TargetMode: configfile.DoltModeServer, RootPath: root, Ownership: tc.ownership, Attempt: 1, Phase: migratePrepared, External: tc.journal, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root, External: tc.sidecar}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
_, err = loadMigrateJournal(beadsDir)
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateJournalLegacyExternalFieldInfersSidecar(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
ext := &configfile.ExternalDoltConfig{Host: "db.example", Port: 3307}
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeProxiedServer, TargetMode: configfile.DoltModeServer, RootPath: root, Ownership: "external", Attempt: 1, Phase: migratePrepared, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root, External: ext}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
got, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, got)
|
||||
assert.Equal(t, ext, got.External)
|
||||
}
|
||||
|
||||
func TestMigrateFromProxiedServer_JournalSidecarChangedFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(root, ".dolt"), 0o755))
|
||||
wantSidecar := &configfile.ProxiedServerClientInfo{RootPath: root, LogPath: filepath.Join(root, "server.log")}
|
||||
gotSidecar := &configfile.ProxiedServerClientInfo{RootPath: root, LogPath: filepath.Join(root, "changed.log")}
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, gotSidecar))
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeProxiedServer, TargetMode: configfile.DoltModeServer, RootPath: root, Ownership: "managed-local", Attempt: 1, Phase: migratePrepared, Sidecar: wantSidecar}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
journalPath := migrateJournalPath(beadsDir)
|
||||
require.NoError(t, os.WriteFile(journalPath, b, 0o600))
|
||||
before := snapshotMigrationTree(t, beadsDir, root)
|
||||
var migrateErr error
|
||||
captureStderr(t, func() { migrateErr = runMigrateFromProxiedServer(false, false) })
|
||||
require.Error(t, migrateErr)
|
||||
assert.Equal(t, before, snapshotMigrationTree(t, beadsDir, root))
|
||||
}
|
||||
|
||||
// TestMigrateFromProxiedServer_ForwardJournalAtPreparedIsNotSidecarRefused is
|
||||
// the complement of the test above, and the reason its guard is scoped to
|
||||
// reverse journals.
|
||||
//
|
||||
// The two directions mean different things by a migratePrepared journal that
|
||||
// names a sidecar. A reverse journal names one that is already on disk, so a
|
||||
// mismatch is real corruption -- that is the case above. A forward journal
|
||||
// names the sidecar it is about to write, and only advances past
|
||||
// migratePrepared once the write lands, so a crash in that pre-write window
|
||||
// leaves no sidecar on disk at all. That is the correct state for the phase,
|
||||
// not a mismatch.
|
||||
//
|
||||
// This command is the documented escape hatch out of exactly that wreck: the
|
||||
// non-proxied-mode refusal is gated on j == nil, so a leftover journal is what
|
||||
// lets the operator run it with the live mode still on "server". An unscoped
|
||||
// sidecar check refuses here by naming a missing file, which strands the
|
||||
// workspace with no way forward and no way back.
|
||||
func TestMigrateFromProxiedServer_ForwardJournalAtPreparedIsNotSidecarRefused(t *testing.T) {
|
||||
// Live mode is still the forward migration's source: it flips only after
|
||||
// the sidecar is written, so a crash before that leaves it untouched.
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
j := migrateJournal{Version: 1, SourceMode: configfile.DoltModeServer, TargetMode: configfile.DoltModeProxiedServer, RootPath: root, Ownership: "managed-local", Attempt: 1, Phase: migratePrepared, Sidecar: &configfile.ProxiedServerClientInfo{RootPath: root}}
|
||||
b, err := json.Marshal(j)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(migrateJournalPath(beadsDir), b, 0o600))
|
||||
|
||||
// Precondition: no sidecar on disk, which is what the guard would have
|
||||
// compared against the journal's.
|
||||
_, statErr := os.Stat(configfile.ProxiedServerClientInfoPath(beadsDir))
|
||||
require.True(t, os.IsNotExist(statErr), "precondition: sidecar must be absent, got %v", statErr)
|
||||
|
||||
var migrateErr error
|
||||
stderr := captureStderr(t, func() { migrateErr = runMigrateFromProxiedServer(false, false) })
|
||||
|
||||
// Deliberately not asserting success: what this command does next with a
|
||||
// forward journal is its own behavior and has its own coverage. The pin is
|
||||
// only that it does not stop at the sidecar comparison, whose message
|
||||
// would be a lie about the workspace.
|
||||
if migrateErr != nil {
|
||||
assert.NotContains(t, stderr, "migration sidecar does not match prepared state")
|
||||
assert.NotContains(t, migrateErr.Error(), "migration sidecar does not match prepared state")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
cmd/bd/proxied_direct_only_refusal_frontdoor_test.go
Normal file
26
cmd/bd/proxied_direct_only_refusal_frontdoor_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//go:build cgo && unix
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProxiedDirectOnlyRefusalsFrontDoor(t *testing.T) {
|
||||
requireManagedLocalProxiedEnv(t)
|
||||
bd := buildEmbeddedBD(t)
|
||||
p := bdManagedLocalInit(t, bd, "directonly", 5*time.Minute)
|
||||
for _, args := range [][]string{{"sync"}, {"dolt", "remote", "add", "origin", "https://example.com/repo"}, {"dolt", "remote", "list"}, {"dolt", "remote", "reset-data", "origin"}, {"dolt", "push"}, {"dolt", "pull"}, {"dolt", "commit"}} {
|
||||
t.Run(strings.Join(args, "/"), func(t *testing.T) {
|
||||
out, errOut, err := bdProxiedRunBuffers(t, bd, p.dir, append(args, "--json")...)
|
||||
if err == nil {
|
||||
t.Fatal("expected refusal")
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(out+errOut), "unsupported") && !strings.Contains(strings.ToLower(out+errOut), "proxied") {
|
||||
t.Fatalf("unexpected refusal: %s%s", out, errOut)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
240
cmd/bd/proxied_outage_reconnect_integration_test.go
Normal file
240
cmd/bd/proxied_outage_reconnect_integration_test.go
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
//go:build cgo && unix
|
||||
|
||||
package main
|
||||
|
||||
// Real front-door outage/reconnect acceptance matrix. Each case seeds a
|
||||
// sentinel through bd, takes only the upstream out of service, and verifies a
|
||||
// bounded failure without mutation before restoring the same endpoint. Proxy
|
||||
// ownership is exercised separately by killing the proxy and letting bd adopt
|
||||
// a replacement on the next invocation.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/testutil"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestProxiedOutageReconnectAcceptanceMatrix(t *testing.T) {
|
||||
requireProxiedServerEnv(t)
|
||||
bd := buildEmbeddedBD(t)
|
||||
for _, topology := range []struct {
|
||||
name string
|
||||
socket bool
|
||||
}{
|
||||
{name: "external-tcp"},
|
||||
{name: "external-unix-socket", socket: true},
|
||||
} {
|
||||
t.Run(topology.name, func(t *testing.T) {
|
||||
upstream := testutil.StartIsolatedDoltContainerHandle(t)
|
||||
if _, err := exec.LookPath("socat"); err != nil {
|
||||
t.Skip("socat is required")
|
||||
}
|
||||
gatePort, err := proxy.PickFreePort()
|
||||
require.NoError(t, err)
|
||||
var endpoint string
|
||||
if topology.socket {
|
||||
endpoint = filepath.Join(t.TempDir(), "dolt.sock")
|
||||
} else {
|
||||
endpoint = strconv.Itoa(gatePort)
|
||||
}
|
||||
bridge := startOutageBridge(t, endpoint, upstream.Port, topology.socket)
|
||||
|
||||
var p proxiedProject
|
||||
if topology.socket {
|
||||
p = bdProxiedInit(t, bd, "outage_socket", "--proxied-server-external-socket-path", endpoint)
|
||||
} else {
|
||||
p = bdProxiedInit(t, bd, "outage_tcp", "--proxied-server-external-host", "127.0.0.1", "--proxied-server-external-port", endpoint)
|
||||
}
|
||||
sentinel := bdProxiedCreate(t, bd, p.dir, "outage sentinel")
|
||||
before := bdProxiedShow(t, bd, p.dir, sentinel.ID)
|
||||
if _, err := os.Stat(filepath.Join(p.proxyRoot, "proxy-child.pid")); err == nil {
|
||||
t.Fatal("external topology spawned a local Dolt sidecar")
|
||||
}
|
||||
|
||||
// Stop only the upstream. The proxy remains alive and must surface a
|
||||
// bounded transport failure; no local fallback is permitted.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
require.NoError(t, upstream.Stop(ctx))
|
||||
cancel()
|
||||
_, stderr, err, timedOut := runProxiedDeadline(t, bd, p.dir, 3*time.Second, "show", sentinel.ID, "--json")
|
||||
if timedOut {
|
||||
t.Fatal("upstream outage command exceeded 3s bound")
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("show unexpectedly succeeded while upstream was stopped")
|
||||
}
|
||||
if !actionableTransportError(stderr) {
|
||||
t.Fatalf("outage error is not actionable transport refusal: %q", stderr)
|
||||
}
|
||||
// Restore and verify the exact sentinel survives the outage.
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
||||
require.NoError(t, upstream.Start(ctx))
|
||||
newPort, err := upstream.CurrentPort(ctx)
|
||||
cancel()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, bridge.Process.Kill())
|
||||
bridge = startOutageBridge(t, endpoint, newPort, topology.socket)
|
||||
var after *types.Issue
|
||||
require.Eventually(t, func() bool {
|
||||
stdout, _, runErr := bdProxiedRunBuffers(t, bd, p.dir, "show", sentinel.ID, "--json")
|
||||
if runErr != nil {
|
||||
return false
|
||||
}
|
||||
after = parseIssueJSON(t, []byte(stdout))
|
||||
return true
|
||||
}, 10*time.Second, 100*time.Millisecond, "bd reconnect after upstream restart")
|
||||
if after.ID != before.ID || after.Title != before.Title {
|
||||
t.Fatalf("sentinel changed after reconnect: before=%q/%q after=%q/%q", before.ID, before.Title, after.ID, after.Title)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(p.proxyRoot, "proxy-child.pid")); err == nil {
|
||||
t.Fatal("external topology created a local Dolt sidecar during reconnect")
|
||||
}
|
||||
|
||||
// Killing the proxy itself must cause a fresh proxy to be adopted on
|
||||
// the next command, still reaching the same upstream data.
|
||||
if err := proxy.Shutdown(p.proxyRoot); err != nil {
|
||||
t.Fatalf("shutdown proxy: %v", err)
|
||||
}
|
||||
afterProxy := bdProxiedShow(t, bd, p.dir, sentinel.ID)
|
||||
if afterProxy.ID != sentinel.ID || afterProxy.Title != sentinel.Title {
|
||||
t.Fatalf("sentinel lost after proxy restart: %+v", afterProxy)
|
||||
}
|
||||
|
||||
// Endpoint identity changes must refuse stale-proxy adoption.
|
||||
info, err := configfile.LoadProxiedServerClientInfo(p.beadsDir)
|
||||
if err != nil || info == nil || info.External == nil {
|
||||
t.Fatalf("load endpoint identity: info=%+v err=%v", info, err)
|
||||
}
|
||||
original := *info.External
|
||||
if topology.socket {
|
||||
info.External.Socket = filepath.Join(t.TempDir(), "stale.sock")
|
||||
} else {
|
||||
info.External.Port++
|
||||
}
|
||||
if err := configfile.SaveProxiedServerClientInfo(p.beadsDir, info); err != nil {
|
||||
t.Fatalf("rewrite endpoint identity: %v", err)
|
||||
}
|
||||
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "show", sentinel.ID, "--json")
|
||||
if err == nil {
|
||||
t.Fatalf("stale endpoint unexpectedly succeeded:\nstdout=%s\nstderr=%s", stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(stdout+stderr), "endpoint") {
|
||||
t.Fatalf("stale endpoint refusal lacks identity context: %s%s", stdout, stderr)
|
||||
}
|
||||
info.External = &original
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(p.beadsDir, info))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedLocalProxiedOutageReconnectContract(t *testing.T) {
|
||||
requireManagedLocalProxiedEnv(t)
|
||||
bd := buildEmbeddedBD(t)
|
||||
p := bdManagedLocalInit(t, bd, "outage_managed", 5*time.Minute)
|
||||
sentinel := bdProxiedCreate(t, bd, p.dir, "managed outage sentinel")
|
||||
proxyBefore := readManagedProxyPidFile(t, p)
|
||||
if proxyBefore == nil || !processAlive(proxyBefore.Pid) {
|
||||
t.Fatalf("managed-local proxy is not running: %+v", proxyBefore)
|
||||
}
|
||||
backend := readManagedBackendPidFile(t, p)
|
||||
if backend == nil || !processAlive(backend.Pid) {
|
||||
t.Fatal("managed-local backend is not running")
|
||||
}
|
||||
proc, err := os.FindProcess(backend.Pid)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, proc.Kill())
|
||||
require.Eventually(t, func() bool { return !processAlive(proxyBefore.Pid) }, 3*time.Second, 50*time.Millisecond, "managed proxy did not retire after child death")
|
||||
// 1.3.0 contract: child death terminates the owned proxy; the next bd
|
||||
// invocation starts a fresh proxy/backend rather than in-place restarting.
|
||||
got := bdProxiedShow(t, bd, p.dir, sentinel.ID)
|
||||
if got.ID != sentinel.ID || got.Title != "managed outage sentinel" {
|
||||
t.Fatalf("managed-local sentinel lost after command-level restart: %+v", got)
|
||||
}
|
||||
proxyAfter := readManagedProxyPidFile(t, p)
|
||||
backendAfter := readManagedBackendPidFile(t, p)
|
||||
if proxyBefore == nil || processAlive(proxyBefore.Pid) || processAlive(backend.Pid) {
|
||||
t.Fatalf("managed child death left old processes alive: proxy=%v backend=%v", processAlive(proxyBefore.Pid), processAlive(backend.Pid))
|
||||
}
|
||||
if backendAfter == nil || !processAlive(backendAfter.Pid) {
|
||||
t.Fatalf("managed restart backend not alive: %+v", backendAfter)
|
||||
}
|
||||
if proxyBefore == nil || proxyAfter == nil || proxyAfter.Pid == proxyBefore.Pid {
|
||||
t.Fatalf("managed child death did not force a fresh proxy: before=%+v after=%+v", proxyBefore, proxyAfter)
|
||||
}
|
||||
}
|
||||
|
||||
func runProxiedDeadline(t *testing.T, bd, dir string, timeout time.Duration, args ...string) (stdout, stderr string, err error, timedOut bool) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, bd, args...)
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
cmd.Dir, cmd.Env = dir, bdProxiedEnv(dir)
|
||||
var out, errOut strings.Builder
|
||||
cmd.Stdout, cmd.Stderr = &out, &errOut
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if cmd.Process != nil {
|
||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
case <-done:
|
||||
}
|
||||
}()
|
||||
err = cmd.Run()
|
||||
close(done)
|
||||
return out.String(), errOut.String(), err, ctx.Err() != nil
|
||||
}
|
||||
|
||||
func actionableTransportError(s string) bool {
|
||||
s = strings.ToLower(s)
|
||||
for _, token := range []string{"connection refused", "connect:", "endpoint", "dial", "upstream", "unexpected eof", "invalid connection"} {
|
||||
if strings.Contains(s, token) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func waitForSocket(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
require.Eventually(t, func() bool { _, err := os.Stat(path); return err == nil }, 3*time.Second, 20*time.Millisecond,
|
||||
"unix socket did not appear")
|
||||
}
|
||||
|
||||
func startOutageBridge(t *testing.T, endpoint, upstreamPort string, socket bool) *exec.Cmd {
|
||||
t.Helper()
|
||||
var listen string
|
||||
if socket {
|
||||
// socat refuses to bind over a stale pathname after an outage.
|
||||
_ = os.Remove(endpoint)
|
||||
listen = "UNIX-LISTEN:" + endpoint + ",fork"
|
||||
} else {
|
||||
listen = "TCP-LISTEN:" + endpoint + ",bind=127.0.0.1,reuseaddr,fork"
|
||||
}
|
||||
cmd := exec.Command("socat", listen, "TCP:127.0.0.1:"+upstreamPort)
|
||||
require.NoError(t, cmd.Start())
|
||||
require.Eventually(t, func() bool { return processAlive(cmd.Process.Pid) }, time.Second, 20*time.Millisecond, "outage bridge process did not remain alive")
|
||||
if socket {
|
||||
waitForSocket(t, endpoint)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if cmd.Process != nil {
|
||||
_ = cmd.Process.Kill()
|
||||
}
|
||||
})
|
||||
return cmd
|
||||
}
|
||||
118
cmd/bd/proxied_workflow_refusal_frontdoor_test.go
Normal file
118
cmd/bd/proxied_workflow_refusal_frontdoor_test.go
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
//go:build cgo && unix
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDirectWorkflowRepresentativeBehavior(t *testing.T) {
|
||||
bd := buildEmbeddedBD(t)
|
||||
dir, _, _ := bdInit(t, bd, "--prefix", "directwf", "--skip-hooks", "--skip-agents")
|
||||
cmd := exec.Command(bd, "ship", "refusal")
|
||||
cmd.Dir = dir
|
||||
cmd.Env = os.Environ()
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout, cmd.Stderr = &out, &out
|
||||
if err := cmd.Run(); err == nil {
|
||||
t.Fatal("direct ship unexpectedly succeeded")
|
||||
}
|
||||
if strings.Contains(strings.ToLower(out.String()), "proxied-server mode") {
|
||||
t.Fatalf("direct mode used proxy refusal: %s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestProxiedWorkflowRefusalsFrontDoor executes the real bd binary for every
|
||||
// workflow family rejected by the proxy capability contract. Refusals must
|
||||
// happen before provider startup and leave the workspace untouched.
|
||||
func TestProxiedWorkflowRefusalsFrontDoor(t *testing.T) {
|
||||
requireManagedLocalProxiedEnv(t)
|
||||
bd := buildEmbeddedBD(t)
|
||||
p := bdManagedLocalInit(t, bd, "refusal", 5*time.Minute)
|
||||
epic := bdProxiedCreate(t, bd, p.dir, "refusal epic", "--type", "epic")
|
||||
formula := filepath.Join(t.TempDir(), "refusal.toml")
|
||||
if err := os.WriteFile(formula, []byte("name = \"refusal\"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.ReadFile(filepath.Join(p.proxyRoot, "config.yaml"))
|
||||
if err != nil {
|
||||
t.Fatalf("read config before refusal: %v", err)
|
||||
}
|
||||
issuesBefore := bdProxiedListJSON(t, bd, p, "--all")
|
||||
artifactBefore := map[string][]byte{}
|
||||
for _, name := range []string{".local_version", "events.jsonl", "metadata.json", "proxy.pid", "proxy-child.pid", "proxy.lock", "proxy-child.lock"} {
|
||||
b, _ := os.ReadFile(filepath.Join(p.beadsDir, name))
|
||||
artifactBefore[name] = b
|
||||
}
|
||||
commands := [][]string{{"cook", formula, "--persist"}, {"ship", "refusal"}, {"swarm", "create", epic.ID}, {"swarm", "list"}, {"merge-slot", "create"}, {"merge-slot", "check"}, {"merge-slot", "acquire"}, {"merge-slot", "release"}}
|
||||
expectedCode := func(args []string) string {
|
||||
if args[0] == "swarm" {
|
||||
return "proxy.swarm.unsupported"
|
||||
}
|
||||
if args[0] == "merge-slot" {
|
||||
return "proxy.merge_slot.unsupported"
|
||||
}
|
||||
return "proxy.formula.unsupported"
|
||||
}
|
||||
expectedMessage := func(args []string) string {
|
||||
name := args[0]
|
||||
if (args[0] == "swarm" || args[0] == "merge-slot") && len(args) > 1 {
|
||||
name += " " + args[1]
|
||||
}
|
||||
return name + " is not supported in proxied-server mode"
|
||||
}
|
||||
for _, args := range commands {
|
||||
t.Run(strings.Join(args, "/"), func(t *testing.T) {
|
||||
for _, jsonMode := range []bool{false, true} {
|
||||
invoke := append([]string(nil), args...)
|
||||
if jsonMode {
|
||||
invoke = append(invoke, "--json")
|
||||
}
|
||||
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, invoke...)
|
||||
if err == nil {
|
||||
t.Fatalf("%v unexpectedly succeeded: %s", invoke, stdout)
|
||||
}
|
||||
out := strings.ToLower(stdout + stderr)
|
||||
if !jsonMode && !strings.Contains(stderr, expectedMessage(args)) {
|
||||
t.Fatalf("%v refusal lacks stable proxy/unsupported shape: %s", invoke, out)
|
||||
}
|
||||
if jsonMode {
|
||||
var payload struct {
|
||||
Code string `json:"code"`
|
||||
Mutates bool `json:"mutates"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(stdout), &payload); err != nil {
|
||||
t.Fatalf("invalid refusal JSON: %v", err)
|
||||
}
|
||||
if payload.Code != expectedCode(args) || payload.Mutates {
|
||||
t.Fatalf("typed refusal = %+v", payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
after, err := os.ReadFile(filepath.Join(p.proxyRoot, "config.yaml"))
|
||||
if err != nil {
|
||||
t.Fatalf("read config after refusal: %v", err)
|
||||
}
|
||||
if string(before) != string(after) {
|
||||
t.Fatal("config changed during refused workflow commands")
|
||||
}
|
||||
issuesAfter := bdProxiedListJSON(t, bd, p, "--all")
|
||||
if len(issuesBefore) != len(issuesAfter) {
|
||||
t.Fatalf("issue rows changed during refusals: before=%d after=%d", len(issuesBefore), len(issuesAfter))
|
||||
}
|
||||
for name, want := range artifactBefore {
|
||||
got, _ := os.ReadFile(filepath.Join(p.beadsDir, name))
|
||||
if string(got) != string(want) {
|
||||
t.Fatalf("artifact %s changed", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
361
cmd/bd/proxy_capability.go
Normal file
361
cmd/bd/proxy_capability.go
Normal file
|
|
@ -0,0 +1,361 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ProxyCapability identifies a command feature whose availability differs by
|
||||
// storage topology.
|
||||
type ProxyCapability string
|
||||
|
||||
const (
|
||||
ProxyCapReadonly ProxyCapability = "readonly"
|
||||
ProxyCapMaxRows ProxyCapability = "max-rows"
|
||||
ProxyCapWatch ProxyCapability = "watch"
|
||||
ProxyCapRepo ProxyCapability = "repo"
|
||||
)
|
||||
|
||||
// ProxyMode identifies the selected storage topology.
|
||||
type ProxyMode string
|
||||
|
||||
const (
|
||||
ProxyModeDirect ProxyMode = "direct"
|
||||
ProxyModeProxied ProxyMode = "proxied-server"
|
||||
)
|
||||
|
||||
// ProxyTopology distinguishes the provider deployment shape. Capability
|
||||
// policy is currently identical across proxied shapes, but retaining this
|
||||
// dimension prevents an external TCP server from being conflated with a local
|
||||
// managed one as more surfaces are added.
|
||||
type ProxyTopology string
|
||||
|
||||
const (
|
||||
ProxyTopologyAny ProxyTopology = "any"
|
||||
ProxyTopologyManagedLocal ProxyTopology = "managed-local"
|
||||
ProxyTopologyExternalTCP ProxyTopology = "external-tcp"
|
||||
ProxyTopologyExternalUnix ProxyTopology = "external-unix"
|
||||
)
|
||||
|
||||
// ProxyCapabilityOutcome describes what the front door does with a feature.
|
||||
type ProxyCapabilityOutcome string
|
||||
|
||||
const (
|
||||
ProxyOutcomeHonored ProxyCapabilityOutcome = "honored"
|
||||
ProxyOutcomeRefused ProxyCapabilityOutcome = "refused"
|
||||
ProxyOutcomeDelegated ProxyCapabilityOutcome = "delegated"
|
||||
ProxyOutcomeNA ProxyCapabilityOutcome = "N/A"
|
||||
)
|
||||
|
||||
// proxyCapabilityRule is the stable contract for one command/argument/topology
|
||||
// capability. Mutates is false for all refusals; ExitCode is used by the CLI
|
||||
// when rendering a typed refusal.
|
||||
type proxyCapabilityRule struct {
|
||||
Outcome ProxyCapabilityOutcome
|
||||
Code string
|
||||
Message string
|
||||
ExitCode int
|
||||
Mutates bool
|
||||
}
|
||||
|
||||
// ProxyCapabilityError is a machine-identifiable front-door refusal.
|
||||
type ProxyCapabilityError struct {
|
||||
Code string
|
||||
Message string
|
||||
ExitCode int
|
||||
Mutates bool
|
||||
}
|
||||
|
||||
func (e *ProxyCapabilityError) Error() string { return e.Message }
|
||||
|
||||
func refused(code, message string) proxyCapabilityRule {
|
||||
return proxyCapabilityRule{Outcome: ProxyOutcomeRefused, Code: code, Message: message, ExitCode: 1}
|
||||
}
|
||||
|
||||
func honored() proxyCapabilityRule {
|
||||
return proxyCapabilityRule{Outcome: ProxyOutcomeHonored, ExitCode: 0}
|
||||
}
|
||||
|
||||
func notApplicable() proxyCapabilityRule { return proxyCapabilityRule{Outcome: ProxyOutcomeNA} }
|
||||
|
||||
// ProxyCapabilityKey identifies a command's flag/argument on a topology.
|
||||
// Argument is intentionally explicit (for example, "--watch"), allowing
|
||||
// callers and tests to distinguish a command that lacks a flag (N/A) from one
|
||||
// that refuses it.
|
||||
type ProxyCapabilityKey struct {
|
||||
Command string
|
||||
Argument string
|
||||
Mode ProxyMode
|
||||
Topology ProxyTopology
|
||||
}
|
||||
|
||||
// ProxyCapabilityRow is an inspectable command/argument/topology policy row.
|
||||
type ProxyCapabilityRow struct {
|
||||
ProxyCapabilityKey
|
||||
Rule proxyCapabilityRule
|
||||
}
|
||||
|
||||
var proxyMaintenanceRefusals = map[string]proxyCapabilityRule{
|
||||
"doctor": refused("proxy.doctor.unsupported", "doctor is not supported in proxied-server mode"),
|
||||
"backup": refused("proxy.backup.unsupported", "backup is not supported in proxied-server mode"),
|
||||
"restore": refused("proxy.restore.unsupported", "restore is not supported in proxied-server mode"),
|
||||
"diff": refused("proxy.diff.unsupported", "diff is not supported in proxied-server mode"),
|
||||
"flatten": refused("proxy.flatten.unsupported", "flatten is not supported in proxied-server mode"),
|
||||
"migrate": refused("proxy.migrate.unsupported", "migrate is not supported in proxied-server mode"),
|
||||
"migrate-personal": refused("proxy.migrate.unsupported", "migrate-personal is not supported in proxied-server mode"),
|
||||
"branch": refused("proxy.branch.unsupported", "branch is not supported in proxied-server mode"),
|
||||
"conflicts": refused("proxy.conflicts.unsupported", "conflicts is not supported in proxied-server mode"),
|
||||
"vc": refused("proxy.vc.unsupported", "vc is not supported in proxied-server mode"),
|
||||
"federation": refused("proxy.federation.unsupported", "federation is not supported in proxied-server mode"),
|
||||
"repo": refused("proxy.repo.unsupported", "repo is not supported in proxied-server mode"),
|
||||
// Wording matches the long-standing compact.go refusal this pre-provider
|
||||
// gate now short-circuits, so the user-facing message does not change.
|
||||
"compact": refused("proxy.compact.unsupported", "only 'compact --dolt' is supported in proxied-server mode"),
|
||||
"backup init": refused("proxy.backup.unsupported", "backup init is not supported in proxied-server mode"),
|
||||
"backup sync": refused("proxy.backup.unsupported", "backup sync is not supported in proxied-server mode"),
|
||||
"backup remove": refused("proxy.backup.unsupported", "backup remove is not supported in proxied-server mode"),
|
||||
"backup status": refused("proxy.backup.unsupported", "backup status is not supported in proxied-server mode"),
|
||||
"backup restore": refused("proxy.backup.unsupported", "backup restore is not supported in proxied-server mode"),
|
||||
"migrate sync": refused("proxy.migrate.unsupported", "migrate sync is not supported in proxied-server mode"),
|
||||
"migrate-issues": refused("proxy.migrate.unsupported", "migrate-issues is not supported in proxied-server mode"),
|
||||
"gate discover": refused("proxy.gate.unsupported", "gate discover is not supported in proxied-server mode"),
|
||||
"admin cleanup": refused("proxy.admin.unsupported", "admin cleanup is not supported in proxied-server mode"),
|
||||
"admin reset": refused("proxy.admin.unsupported", "admin reset is not supported in proxied-server mode"),
|
||||
"dolt push": refused("proxy.dolt_push.unsupported", "dolt push is not supported in proxied-server mode"),
|
||||
"dolt pull": refused("proxy.dolt_pull.unsupported", "dolt pull is not supported in proxied-server mode"),
|
||||
"dolt commit": refused("proxy.dolt_commit.unsupported", "dolt commit is not supported in proxied-server mode"),
|
||||
"dolt remote": refused("proxy.dolt_remote.unsupported", "dolt remote is not supported in proxied-server mode"),
|
||||
"dolt remote add": refused("proxy.dolt_remote.unsupported", "dolt remote add is not supported in proxied-server mode"),
|
||||
"dolt remote list": refused("proxy.dolt_remote.unsupported", "dolt remote list is not supported in proxied-server mode"),
|
||||
"cook": refused("proxy.formula.unsupported", "cook is not supported in proxied-server mode"),
|
||||
"ship": refused("proxy.formula.unsupported", "ship is not supported in proxied-server mode"),
|
||||
"swarm create": refused("proxy.swarm.unsupported", "swarm create is not supported in proxied-server mode"),
|
||||
"swarm list": refused("proxy.swarm.unsupported", "swarm list is not supported in proxied-server mode"),
|
||||
"merge-slot create": refused("proxy.merge_slot.unsupported", "merge-slot create is not supported in proxied-server mode"),
|
||||
"merge-slot check": refused("proxy.merge_slot.unsupported", "merge-slot check is not supported in proxied-server mode"),
|
||||
"merge-slot acquire": refused("proxy.merge_slot.unsupported", "merge-slot acquire is not supported in proxied-server mode"),
|
||||
"merge-slot release": refused("proxy.merge_slot.unsupported", "merge-slot release is not supported in proxied-server mode"),
|
||||
"dolt remote reset-data": refused("proxy.dolt_remote.unsupported", "dolt remote reset-data is not supported in proxied-server mode"),
|
||||
"sync": refused("proxy.sync.unsupported", "sync is not supported in proxied-server mode"),
|
||||
}
|
||||
|
||||
func init() {
|
||||
for parent, children := range map[string][]string{
|
||||
"vc": {"merge", "commit", "status"},
|
||||
"federation": {"sync", "status", "add-peer", "remove-peer", "list-peers"},
|
||||
"repo": {"add", "remove", "list", "sync"},
|
||||
"conflicts": {"list", "show", "resolve"},
|
||||
"migrate": {"hooks", "issues"},
|
||||
} {
|
||||
rule := proxyMaintenanceRefusals[parent]
|
||||
for _, child := range children {
|
||||
path := parent + " " + child
|
||||
proxyMaintenanceRefusals[path] = refused(rule.Code, path+" is not supported in proxied-server mode")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validateProxyMaintenanceBeforeProvider rejects known direct-only commands
|
||||
// before migrations, auto-start, or provider construction.
|
||||
func validateProxyMaintenanceBeforeProvider(cmd *cobra.Command) error {
|
||||
if cmd == nil {
|
||||
return nil
|
||||
}
|
||||
name := cmd.Name()
|
||||
path := strings.TrimSpace(strings.TrimPrefix(cmd.CommandPath(), cmd.Root().Name()))
|
||||
if name == "compact" {
|
||||
if cmd.Flags().Lookup("dolt") == nil {
|
||||
return nil // root `bd compact` is the Dolt history command
|
||||
}
|
||||
dolt, _ := cmd.Flags().GetBool("dolt")
|
||||
if dolt {
|
||||
return nil
|
||||
}
|
||||
rule := proxyMaintenanceRefusals["compact"]
|
||||
return HandleProxyCapabilityError(&ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode})
|
||||
}
|
||||
if rule, ok := proxyMaintenanceRefusals[path]; ok {
|
||||
return HandleProxyCapabilityError(&ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode})
|
||||
}
|
||||
if class, ok := LookupHistoryCapability(path); ok && class == HistoryDirectOnly {
|
||||
rule := refused("proxy.history.unsupported", path+" is not supported in proxied-server mode")
|
||||
if specific, found := proxyMaintenanceRefusals[path]; found {
|
||||
rule = specific
|
||||
}
|
||||
return HandleProxyCapabilityError(&ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode, Mutates: rule.Mutates})
|
||||
}
|
||||
if strings.Contains(path, " ") {
|
||||
return nil
|
||||
}
|
||||
if rule, ok := proxyMaintenanceRefusals[name]; ok {
|
||||
return HandleProxyCapabilityError(&ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LookupProxyCapabilityFor returns the command/argument-specific rule. An
|
||||
// absent command row falls back to the topology-wide default.
|
||||
func LookupProxyCapabilityFor(command, argument string, mode ProxyMode) (proxyCapabilityRule, bool) {
|
||||
capability := ProxyCapability(argument)
|
||||
if len(argument) > 2 && argument[:2] == "--" {
|
||||
capability = ProxyCapability(argument[2:])
|
||||
}
|
||||
if commands, ok := proxyCommandCapabilities[command]; ok {
|
||||
if modes, ok := commands[mode]; ok {
|
||||
if rule, ok := modes[capability]; ok {
|
||||
return rule, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return LookupProxyCapability(mode, capability)
|
||||
}
|
||||
|
||||
var proxyCapabilityMatrix = map[ProxyMode]map[ProxyCapability]proxyCapabilityRule{
|
||||
ProxyModeDirect: {
|
||||
ProxyCapReadonly: honored(), ProxyCapMaxRows: honored(),
|
||||
ProxyCapWatch: honored(), ProxyCapRepo: honored(),
|
||||
},
|
||||
ProxyModeProxied: {
|
||||
ProxyCapReadonly: refused("proxy.readonly.unsupported", "strict readonly is unavailable for dolt proxied-server backend; refusing to open a store that cannot guarantee mutation-free access"),
|
||||
ProxyCapMaxRows: refused("proxy.max_rows.unsupported", "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode"),
|
||||
ProxyCapWatch: refused("proxy.watch.unsupported", "watch mode not supported in proxied-server mode"),
|
||||
ProxyCapRepo: refused("proxy.repo.unsupported", "--repo is not supported with --proxied-server"),
|
||||
},
|
||||
}
|
||||
|
||||
var proxyCommandCapabilities = map[string]map[ProxyMode]map[ProxyCapability]proxyCapabilityRule{
|
||||
"show": {ProxyModeProxied: {ProxyCapWatch: refused("proxy.watch.unsupported", "watch mode not supported in proxied-server mode")}},
|
||||
"list": {ProxyModeProxied: {ProxyCapWatch: honored(), ProxyCapMaxRows: honored(), ProxyCapRepo: notApplicable()}},
|
||||
"dep tree": {ProxyModeProxied: {ProxyCapMaxRows: honored()}},
|
||||
"ready": {ProxyModeProxied: {ProxyCapMaxRows: refused("proxy.max_rows.unsupported", "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode")}},
|
||||
"graph": {ProxyModeProxied: {ProxyCapMaxRows: refused("proxy.max_rows.unsupported", "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode")}},
|
||||
"find-duplicates": {ProxyModeProxied: {ProxyCapMaxRows: refused("proxy.max_rows.unsupported", "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode")}},
|
||||
}
|
||||
|
||||
// proxyCapabilityRows materializes the policy for every supported proxied
|
||||
// topology. Keeping rows explicit makes matrix audits and front-door tests
|
||||
// deterministic even though the current provider implementations share rules.
|
||||
var proxyCapabilityRows = buildProxyCapabilityRows()
|
||||
|
||||
func buildProxyCapabilityRows() []ProxyCapabilityRow {
|
||||
topologies := []ProxyTopology{ProxyTopologyManagedLocal, ProxyTopologyExternalTCP, ProxyTopologyExternalUnix}
|
||||
var rows []ProxyCapabilityRow
|
||||
for _, topology := range topologies {
|
||||
for _, capability := range []ProxyCapability{ProxyCapReadonly, ProxyCapMaxRows, ProxyCapWatch, ProxyCapRepo} {
|
||||
rule, _ := LookupProxyCapability(ProxyModeProxied, capability)
|
||||
rows = append(rows, ProxyCapabilityRow{ProxyCapabilityKey{"", string(capability), ProxyModeProxied, topology}, rule})
|
||||
}
|
||||
for command, modes := range proxyCommandCapabilities {
|
||||
for mode, capabilities := range modes {
|
||||
for capability, rule := range capabilities {
|
||||
rows = append(rows, ProxyCapabilityRow{ProxyCapabilityKey{command, string(capability), mode, topology}, rule})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// LookupProxyCapabilityAt returns a topology-keyed rule. Unknown topology is
|
||||
// rejected; ProxyTopologyAny applies the topology-independent direct policy.
|
||||
func LookupProxyCapabilityAt(command, argument string, mode ProxyMode, topology ProxyTopology) (proxyCapabilityRule, bool) {
|
||||
if topology != ProxyTopologyAny && topology != ProxyTopologyManagedLocal && topology != ProxyTopologyExternalTCP && topology != ProxyTopologyExternalUnix {
|
||||
return proxyCapabilityRule{}, false
|
||||
}
|
||||
if len(argument) > 2 && argument[:2] == "--" {
|
||||
argument = argument[2:]
|
||||
}
|
||||
for _, row := range proxyCapabilityRows {
|
||||
if row.Command == command && row.Argument == argument && row.Mode == mode && row.Topology == topology {
|
||||
return row.Rule, true
|
||||
}
|
||||
}
|
||||
if topology == ProxyTopologyAny {
|
||||
return LookupProxyCapabilityFor(command, argument, mode)
|
||||
}
|
||||
return proxyCapabilityRule{}, false
|
||||
}
|
||||
|
||||
// LookupProxyCapability returns the typed rule for a mode/capability pair.
|
||||
func LookupProxyCapability(mode ProxyMode, capability ProxyCapability) (proxyCapabilityRule, bool) {
|
||||
rules, ok := proxyCapabilityMatrix[mode]
|
||||
if !ok {
|
||||
return proxyCapabilityRule{}, false
|
||||
}
|
||||
rule, ok := rules[capability]
|
||||
return rule, ok
|
||||
}
|
||||
|
||||
// AssertProxyCapability rejects unsupported features before provider setup.
|
||||
func AssertProxyCapability(mode ProxyMode, capability ProxyCapability) error {
|
||||
return AssertProxyCommandCapability("", mode, capability)
|
||||
}
|
||||
|
||||
// AssertProxyCommandCapability checks a command-specific capability rule.
|
||||
func AssertProxyCommandCapability(command string, mode ProxyMode, capability ProxyCapability) error {
|
||||
if commands, ok := proxyCommandCapabilities[command]; ok {
|
||||
if modes, ok := commands[mode]; ok {
|
||||
if rule, ok := modes[capability]; ok {
|
||||
// N/A means the command has no such flag, which is not a
|
||||
// refusal. Without it, a future assert call site on an N/A row
|
||||
// would return a non-nil error carrying an empty message.
|
||||
if rule.Outcome == ProxyOutcomeHonored || rule.Outcome == ProxyOutcomeDelegated || rule.Outcome == ProxyOutcomeNA {
|
||||
return nil
|
||||
}
|
||||
if rule.Code != "" {
|
||||
return &ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode, Mutates: rule.Mutates}
|
||||
}
|
||||
return fmt.Errorf("%s", rule.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
rule, ok := LookupProxyCapability(mode, capability)
|
||||
if !ok || (rule.Outcome != ProxyOutcomeHonored && rule.Outcome != ProxyOutcomeDelegated) {
|
||||
if rule.Code != "" {
|
||||
return &ProxyCapabilityError{Code: rule.Code, Message: rule.Message, ExitCode: rule.ExitCode, Mutates: rule.Mutates}
|
||||
}
|
||||
if rule.Message != "" {
|
||||
return fmt.Errorf("%s", rule.Message)
|
||||
}
|
||||
return fmt.Errorf("%s is not supported in %s mode", capability, mode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateProxyCapabilitiesBeforeProvider runs refusals that can be decided
|
||||
// from argv before the proxied provider is opened.
|
||||
func validateProxyCapabilitiesBeforeProvider(cmd *cobra.Command) error {
|
||||
if cmd == nil {
|
||||
return nil
|
||||
}
|
||||
name := cmd.Name()
|
||||
if name == "create" && cmd.Flags().Changed("repo") {
|
||||
return HandleProxyCapabilityError(AssertProxyCapability(ProxyModeProxied, ProxyCapRepo))
|
||||
}
|
||||
if name == "show" {
|
||||
if watch, _ := cmd.Flags().GetBool("watch"); watch {
|
||||
return HandleProxyCapabilityError(AssertProxyCommandCapability("show", ProxyModeProxied, ProxyCapWatch))
|
||||
}
|
||||
}
|
||||
if name == "ready" {
|
||||
maxRows, _, err := resolveMaxRows(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if maxRows > 0 {
|
||||
return HandleProxyCapabilityError(AssertProxyCommandCapability(name, ProxyModeProxied, ProxyCapMaxRows))
|
||||
}
|
||||
}
|
||||
if name == "graph" || name == "find-duplicates" {
|
||||
maxRows, _, err := resolveMaxRows(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if maxRows > 0 {
|
||||
return HandleProxyCapabilityError(AssertProxyCommandCapability(name, ProxyModeProxied, ProxyCapMaxRows))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
271
cmd/bd/proxy_capability_test.go
Normal file
271
cmd/bd/proxy_capability_test.go
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func TestProxyCapabilityMatrix(t *testing.T) {
|
||||
for _, cap := range []ProxyCapability{ProxyCapReadonly, ProxyCapMaxRows} {
|
||||
err := AssertProxyCapability(ProxyModeProxied, cap)
|
||||
if err == nil {
|
||||
t.Errorf("%s unexpectedly honored", cap)
|
||||
}
|
||||
var typed *ProxyCapabilityError
|
||||
if !errors.As(err, &typed) || typed.Code == "" || typed.ExitCode != 1 || typed.Mutates {
|
||||
t.Errorf("%s error = %#v, want stable non-mutating refusal", cap, err)
|
||||
}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
cap ProxyCapability
|
||||
want string
|
||||
}{
|
||||
{ProxyCapWatch, "watch mode not supported in proxied-server mode"},
|
||||
{ProxyCapRepo, "--repo is not supported with --proxied-server"},
|
||||
} {
|
||||
err := AssertProxyCapability(ProxyModeProxied, tc.cap)
|
||||
if err == nil || err.Error() != tc.want {
|
||||
t.Errorf("%s error = %v, want %q", tc.cap, err, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityRowsCoverTopologies(t *testing.T) {
|
||||
for _, topology := range []ProxyTopology{ProxyTopologyManagedLocal, ProxyTopologyExternalTCP, ProxyTopologyExternalUnix} {
|
||||
for _, arg := range []string{"--readonly", "--max-rows", "--watch", "--repo"} {
|
||||
if _, ok := LookupProxyCapabilityAt("", arg, ProxyModeProxied, topology); !ok {
|
||||
t.Errorf("missing proxied row topology=%s argument=%s", topology, arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityCommandRows(t *testing.T) {
|
||||
cases := []struct {
|
||||
command, argument string
|
||||
outcome ProxyCapabilityOutcome
|
||||
}{
|
||||
{"list", "--max-rows", ProxyOutcomeHonored},
|
||||
{"dep tree", "--max-rows", ProxyOutcomeHonored},
|
||||
{"ready", "--max-rows", ProxyOutcomeRefused},
|
||||
{"graph", "--max-rows", ProxyOutcomeRefused},
|
||||
{"find-duplicates", "--max-rows", ProxyOutcomeRefused},
|
||||
{"show", "--watch", ProxyOutcomeRefused},
|
||||
{"list", "--watch", ProxyOutcomeHonored},
|
||||
}
|
||||
for _, topology := range []ProxyTopology{ProxyTopologyManagedLocal, ProxyTopologyExternalTCP, ProxyTopologyExternalUnix} {
|
||||
for _, tc := range cases {
|
||||
rule, ok := LookupProxyCapabilityAt(tc.command, tc.argument, ProxyModeProxied, topology)
|
||||
if !ok || rule.Outcome != tc.outcome {
|
||||
t.Errorf("topology=%s %s %s outcome=%q ok=%v, want %q", topology, tc.command, tc.argument, rule.Outcome, ok, tc.outcome)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyMaintenanceNestedPathsRefuseBeforeProvider(t *testing.T) {
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
for _, path := range []string{"backup init", "backup status", "migrate sync", "gate discover"} {
|
||||
parts := strings.Split(path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
parent := &cobra.Command{Use: parts[0]}
|
||||
child := &cobra.Command{Use: parts[1]}
|
||||
root.AddCommand(parent)
|
||||
parent.AddCommand(child)
|
||||
out := captureStdout(t, func() error {
|
||||
_ = validateProxyMaintenanceBeforeProvider(child)
|
||||
return nil
|
||||
})
|
||||
if !strings.Contains(out, `"code":`) {
|
||||
t.Errorf("%s produced no typed refusal: %s", path, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyFormulaSwarmMergeSlotRefusals(t *testing.T) {
|
||||
for _, path := range []string{"cook", "ship", "swarm create", "swarm list", "merge-slot create", "merge-slot check", "merge-slot acquire", "merge-slot release"} {
|
||||
parts := strings.Split(path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: parts[0]}
|
||||
root.AddCommand(cmd)
|
||||
for _, childName := range parts[1:] {
|
||||
child := &cobra.Command{Use: childName}
|
||||
cmd.AddCommand(child)
|
||||
cmd = child
|
||||
}
|
||||
err := validateProxyMaintenanceBeforeProvider(cmd)
|
||||
if err == nil {
|
||||
t.Fatalf("%s unexpectedly allowed", path)
|
||||
}
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("%s exit=%v, want 1", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyWorkflowRefusalContractAndNoMutation(t *testing.T) {
|
||||
cases := []struct {
|
||||
path, code, message string
|
||||
}{
|
||||
{"cook", "proxy.formula.unsupported", "cook is not supported in proxied-server mode"},
|
||||
{"ship", "proxy.formula.unsupported", "ship is not supported in proxied-server mode"},
|
||||
{"swarm create", "proxy.swarm.unsupported", "swarm create is not supported in proxied-server mode"},
|
||||
{"merge-slot acquire", "proxy.merge_slot.unsupported", "merge-slot acquire is not supported in proxied-server mode"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.path, func(t *testing.T) {
|
||||
parts := strings.Split(tc.path, " ")
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: parts[0]}
|
||||
root.AddCommand(cmd)
|
||||
if tc.path == "cook" {
|
||||
cmd.Flags().Bool("persist", false, "")
|
||||
_ = cmd.Flags().Set("persist", "true")
|
||||
}
|
||||
for _, name := range parts[1:] {
|
||||
child := &cobra.Command{Use: name}
|
||||
cmd.AddCommand(child)
|
||||
cmd = child
|
||||
}
|
||||
row, ok := lookupProxyMaintenanceRuleForTest(tc.path)
|
||||
if !ok || row.Code != tc.code || row.Message != tc.message || row.ExitCode != 1 || row.Mutates {
|
||||
t.Fatalf("row = %#v, ok=%v", row, ok)
|
||||
}
|
||||
var typed *ProxyCapabilityError = &ProxyCapabilityError{Code: row.Code, Message: row.Message, ExitCode: row.ExitCode, Mutates: row.Mutates}
|
||||
if typed.Code != tc.code || typed.Message != tc.message || typed.ExitCode != 1 || typed.Mutates {
|
||||
t.Fatalf("typed refusal = %#v", typed)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
before := []byte("unchanged\n")
|
||||
for _, name := range []string{"issues.jsonl", "config.yaml", "events.jsonl"} {
|
||||
if err := os.WriteFile(dir+"/"+name, before, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
oldProvider := uowProvider
|
||||
uowProvider = nil
|
||||
t.Cleanup(func() { uowProvider = oldProvider })
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
// commandDidWrite is a process-wide latch that any earlier test in
|
||||
// this package may already have set, so it has to be baselined
|
||||
// here or the assertion below reports another test's write.
|
||||
oldDidWrite := commandDidWrite.Load()
|
||||
commandDidWrite.Store(false)
|
||||
t.Cleanup(func() { commandDidWrite.Store(oldDidWrite) })
|
||||
out := captureStdout(t, func() error { _ = validateProxyMaintenanceBeforeProvider(cmd); return nil })
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal([]byte(out), &got); err != nil || got["code"] != tc.code || got["error"] != tc.message {
|
||||
t.Fatalf("JSON refusal = %q (%v)", out, err)
|
||||
}
|
||||
if uowProvider != nil || commandDidWrite.Load() {
|
||||
t.Fatal("refusal initialized provider or marked a write")
|
||||
}
|
||||
for _, name := range []string{"issues.jsonl", "config.yaml", "events.jsonl"} {
|
||||
gotBytes, err := os.ReadFile(dir + "/" + name)
|
||||
if err != nil || !bytes.Equal(gotBytes, before) {
|
||||
t.Fatalf("%s mutated: %v", name, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func lookupProxyMaintenanceRuleForTest(path string) (proxyCapabilityRule, bool) {
|
||||
rule, ok := proxyMaintenanceRefusals[path]
|
||||
return rule, ok
|
||||
}
|
||||
|
||||
func TestProxyMaintenanceRefusalLeavesFilesUntouched(t *testing.T) {
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
migrate := &cobra.Command{Use: "migrate"}
|
||||
hooks := &cobra.Command{Use: "hooks"}
|
||||
root.AddCommand(migrate)
|
||||
migrate.AddCommand(hooks)
|
||||
before := []byte("hooks-state")
|
||||
path := t.TempDir() + "/.local_version"
|
||||
if err := os.WriteFile(path, before, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldProvider := uowProvider
|
||||
uowProvider = nil
|
||||
t.Cleanup(func() { uowProvider = oldProvider })
|
||||
err := validateProxyMaintenanceBeforeProvider(hooks)
|
||||
if err == nil {
|
||||
t.Fatal("expected typed maintenance refusal")
|
||||
}
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("exit = %v, want 1", err)
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, before) {
|
||||
t.Fatalf("refusal mutated %s", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityRefusalFrontDoorTextBeforeProvider(t *testing.T) {
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = false
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
cmd := &cobra.Command{Use: "show"}
|
||||
cmd.Flags().Bool("watch", false, "")
|
||||
if err := cmd.Flags().Set("watch", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := captureStderr(t, func() { _ = validateProxyCapabilitiesBeforeProvider(cmd) })
|
||||
if !strings.Contains(got, "watch mode not supported in proxied-server mode") {
|
||||
t.Fatalf("text refusal = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityRefusalFrontDoorJSONIncludesCode(t *testing.T) {
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
cmd := &cobra.Command{Use: "show"}
|
||||
cmd.Flags().Bool("watch", false, "")
|
||||
_ = cmd.Flags().Set("watch", "true")
|
||||
out := captureStdout(t, func() error {
|
||||
_ = validateProxyCapabilitiesBeforeProvider(cmd)
|
||||
return nil
|
||||
})
|
||||
if !strings.Contains(out, `"code": "proxy.watch.unsupported"`) {
|
||||
t.Fatalf("JSON refusal = %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityRefusalDoesNotNeedProvider(t *testing.T) {
|
||||
oldProvider := uowProvider
|
||||
uowProvider = nil
|
||||
t.Cleanup(func() { uowProvider = oldProvider })
|
||||
oldJSON := jsonOutput
|
||||
jsonOutput = true
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
out := captureStdout(t, func() error {
|
||||
_ = runCreateProxiedServer(nil, t.Context(), createInput{repoOverrideSet: true})
|
||||
return nil
|
||||
})
|
||||
if !strings.Contains(out, `"code": "proxy.repo.unsupported"`) {
|
||||
t.Fatalf("refusal = %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyCapabilityDirectEscapeHatch(t *testing.T) {
|
||||
for _, cap := range []ProxyCapability{ProxyCapReadonly, ProxyCapMaxRows, ProxyCapWatch, ProxyCapRepo} {
|
||||
if err := AssertProxyCapability(ProxyModeDirect, cap); err != nil {
|
||||
t.Errorf("direct %s refused: %v", cap, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -59,25 +59,11 @@ This is useful for agents executing molecules to see which steps can run next.`,
|
|||
}
|
||||
|
||||
if usesProxiedServer() {
|
||||
// --claim consumes exactly one row, same reasoning as the
|
||||
// direct-path fix in issueops/claim.go: a rig-wide cap sized
|
||||
// for bulk list/ready reads must not block a single-row claim.
|
||||
// Only the bulk (non-claim) proxied ready listing rejects an
|
||||
// active cap.
|
||||
if !claimReady {
|
||||
if err := rejectMaxRowsUnderProxiedServer(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Still validate --max-rows/BEADS_MAX_ROWS here even though
|
||||
// the resolved cap is ignored below: resolveMaxRows is also
|
||||
// where a malformed value (e.g. --max-rows -1) is rejected
|
||||
// with exit 1, and skipping it entirely for the claim-exempt
|
||||
// branch would silently accept a usage error that every
|
||||
// other command (direct or proxied) rejects.
|
||||
if _, _, err := resolveMaxRows(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
// The proxied ready role cannot enforce a row cap, including on
|
||||
// --claim. Refuse any positive cap rather than silently dropping
|
||||
// this safety limit; malformed values remain usage errors.
|
||||
if err := rejectMaxRowsUnderProxiedServer(cmd); err != nil {
|
||||
return err
|
||||
}
|
||||
return runReadyProxiedServer(cmd, rootCtx)
|
||||
}
|
||||
|
|
|
|||
110
cmd/bd/ready_max_rows_proxied_test.go
Normal file
110
cmd/bd/ready_max_rows_proxied_test.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// pinProxiedServerMode routes usesProxiedServer() down the proxied branch for
|
||||
// the duration of one test. resetCommandContext() is what makes the global
|
||||
// authoritative: usesProxiedServer prefers cmdCtx whenever an earlier test left
|
||||
// one behind, so setting the global alone is not enough to pin the route.
|
||||
func pinProxiedServerMode(t *testing.T) {
|
||||
t.Helper()
|
||||
restore := proxiedServerMode
|
||||
proxiedServerMode = true
|
||||
resetCommandContext()
|
||||
t.Cleanup(func() {
|
||||
proxiedServerMode = restore
|
||||
resetCommandContext()
|
||||
})
|
||||
}
|
||||
|
||||
// TestReadyRefusesMaxRowsUnderProxiedServerOnClaim pins the --max-rows refusal
|
||||
// on `bd ready --claim` specifically.
|
||||
//
|
||||
// The refusal lives in readyCmd's RunE, above the dispatch into
|
||||
// runReadyProxiedServer, and --claim is the one ready invocation that takes a
|
||||
// mutating path on the far side of it. Nothing below that dispatch can enforce
|
||||
// the cap: runReadyProxiedServer deliberately passes a nil resolver to
|
||||
// gatherReadyInput, and the proxied claim role never threads MaxRows. So the
|
||||
// only thing standing between an operator's explicit row cap and a silent
|
||||
// unbounded claim is this one call, and it is reachable only through RunE --
|
||||
// the existing gatherReadyInput coverage sits entirely below it and cannot see
|
||||
// it at all.
|
||||
//
|
||||
// Both cap sources are exercised: resolveMaxRows honors --max-rows and
|
||||
// BEADS_MAX_ROWS, and a guard wired to only the flag would leave every rig that
|
||||
// sets the env var running uncapped.
|
||||
func TestReadyRefusesMaxRowsUnderProxiedServerOnClaim(t *testing.T) {
|
||||
pinProxiedServerMode(t)
|
||||
pinJSONOutput(t, false)
|
||||
|
||||
// The regression safety net. If the refusal ever stops firing, RunE falls
|
||||
// through to runReadyProxiedServer, which hits an uninitialized provider
|
||||
// and returns its own error rather than dialing a real server -- so the
|
||||
// stderr assertion below, not the exit code, is what fails. Both errors
|
||||
// carry exit code 1.
|
||||
if uowProvider != nil {
|
||||
t.Fatal("precondition: uowProvider must be nil so a regression cannot open a real proxied connection")
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
args []string
|
||||
env string
|
||||
}{
|
||||
{name: "flag", args: []string{"--claim", "--max-rows", "5"}},
|
||||
{name: "env", args: []string{"--claim"}, env: "5"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv(maxRowsEnvVar, tc.env)
|
||||
|
||||
// A cloned flag set, not readyCmd's own: RunE reads its flags off
|
||||
// the command it is handed, and mutating the shared readyCmd would
|
||||
// leak --claim into every other test in the package.
|
||||
cmd := newReadyFlagsCommand(t, tc.args...)
|
||||
|
||||
var err error
|
||||
stderr := captureStderr(t, func() { err = readyCmd.RunE(cmd, nil) })
|
||||
|
||||
assertExitCode(t, err, 1)
|
||||
if !strings.Contains(stderr, "--max-rows / BEADS_MAX_ROWS is not supported in proxied-server mode") {
|
||||
t.Fatalf("stderr = %q, want the proxied --max-rows refusal", stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestReadyClaimWithoutLiveCapIsNotRefusedUnderProxiedServer is the other half:
|
||||
// the refusal is conditioned on a live cap, not on --claim. Without it, a guard
|
||||
// that rejected every proxied --claim would pass the test above.
|
||||
//
|
||||
// This one calls the helper directly rather than going through RunE. With
|
||||
// nothing to refuse, RunE proceeds into the proxied query path, and the
|
||||
// assertion would then be about whatever that path does next instead of about
|
||||
// the guard.
|
||||
func TestReadyClaimWithoutLiveCapIsNotRefusedUnderProxiedServer(t *testing.T) {
|
||||
pinProxiedServerMode(t)
|
||||
pinJSONOutput(t, false)
|
||||
t.Setenv(maxRowsEnvVar, "")
|
||||
|
||||
// --max-rows 0 is an explicit disable that outranks BEADS_MAX_ROWS, so it
|
||||
// is not a live cap either even with the env var set.
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
args []string
|
||||
env string
|
||||
}{
|
||||
{name: "no_cap", args: []string{"--claim"}},
|
||||
{name: "flag_zero", args: []string{"--claim", "--max-rows", "0"}},
|
||||
{name: "flag_zero_overrides_env", args: []string{"--claim", "--max-rows", "0"}, env: "5"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv(maxRowsEnvVar, tc.env)
|
||||
if err := rejectMaxRowsUnderProxiedServer(newReadyFlagsCommand(t, tc.args...)); err != nil {
|
||||
t.Fatalf("proxied `bd ready %s` was refused with no live cap: %v", strings.Join(tc.args, " "), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -667,29 +667,18 @@ func TestProxiedServerReady2(t *testing.T) {
|
|||
t.Errorf("expected BEADS_MAX_ROWS proxied-server rejection for bulk ready, got: %s", out)
|
||||
}
|
||||
|
||||
before := bdProxiedReadyJSON(t, bd, p, "--label", "rcmr-claim")
|
||||
stdout, stderr, err = bdProxiedRunBuffersWithEnv(t, bd, p.dir,
|
||||
[]string{"BEADS_MAX_ROWS=1"}, "ready", "--claim", "--json", "--label", "rcmr-claim")
|
||||
if err != nil {
|
||||
t.Fatalf("bd ready --claim --json under BEADS_MAX_ROWS=1 should succeed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
||||
if err == nil {
|
||||
t.Fatalf("bd ready --claim --json under BEADS_MAX_ROWS=1 should refuse")
|
||||
}
|
||||
var claimed []types.IssueWithCounts
|
||||
s := strings.TrimSpace(stdout)
|
||||
start := strings.Index(s, "[")
|
||||
if start < 0 {
|
||||
t.Fatalf("no JSON array in claim output:\n%s", stdout)
|
||||
if out := stdout + stderr; !strings.Contains(out, "not supported in proxied-server mode") {
|
||||
t.Fatalf("expected cap refusal, got:\n%s", out)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(s[start:]), &claimed); err != nil {
|
||||
t.Fatalf("parse claim JSON: %v\n%s", err, s[start:])
|
||||
}
|
||||
if len(claimed) != 1 {
|
||||
t.Fatalf("expected exactly one claimed issue, got %d: %s", len(claimed), stdout)
|
||||
}
|
||||
if claimed[0].Status != types.StatusInProgress {
|
||||
t.Errorf("Status = %s, want %s", claimed[0].Status, types.StatusInProgress)
|
||||
}
|
||||
persisted := bdProxiedShow(t, bd, p.dir, claimed[0].ID)
|
||||
if persisted.Status != types.StatusInProgress || persisted.Assignee == "" {
|
||||
t.Errorf("persisted claim = %+v, want in_progress with assignee", persisted)
|
||||
after := bdProxiedReadyJSON(t, bd, p, "--label", "rcmr-claim")
|
||||
if len(after) != len(before) {
|
||||
t.Fatalf("claim refusal mutated ready set: before=%d after=%d", len(before), len(after))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ func runShowProxiedServer(cmd *cobra.Command, ctx context.Context, args []string
|
|||
in := gatherShowProxiedInput(cmd, args)
|
||||
|
||||
if in.watchMode {
|
||||
return HandleErrorRespectJSON("watch mode not supported in proxied-server mode")
|
||||
return HandleProxyCapabilityError(AssertProxyCommandCapability("show", ProxyModeProxied, ProxyCapWatch))
|
||||
}
|
||||
|
||||
uw, err := proxiedOpenReadUOW(ctx)
|
||||
|
|
|
|||
|
|
@ -355,23 +355,24 @@ func runJiraPush(cmd *cobra.Command, args []string) error {
|
|||
CheckReadonly("jira push")
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleError("database not available: %v", err)
|
||||
}
|
||||
if err := validateJiraConfig(); err != nil {
|
||||
if err := validateJiraConfigForStore(trackerStore); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
jt := &jira.Tracker{}
|
||||
if err := jt.Init(ctx, store); err != nil {
|
||||
if err := jt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleError("initializing Jira tracker: %v", err)
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(jt, store, actor)
|
||||
engine := tracker.NewEngine(jt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
engine.PushHooks = buildJiraPushHooks(ctx)
|
||||
engine.PushHooks = buildJiraPushHooksForStore(ctx, trackerStore)
|
||||
|
||||
result, err := engine.Sync(ctx, tracker.SyncOptions{
|
||||
Push: true,
|
||||
|
|
@ -402,20 +403,21 @@ func runJiraPull(cmd *cobra.Command, args []string) error {
|
|||
CheckReadonly("jira pull")
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleError("database not available: %v", err)
|
||||
}
|
||||
if err := validateJiraConfig(); err != nil {
|
||||
if err := validateJiraConfigForStore(trackerStore); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
jt := &jira.Tracker{}
|
||||
if err := jt.Init(ctx, store); err != nil {
|
||||
if err := jt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleError("initializing Jira tracker: %v", err)
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(jt, store, actor)
|
||||
engine := tracker.NewEngine(jt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
|
||||
|
|
@ -462,32 +464,33 @@ func runLinearPush(cmd *cobra.Command, args []string) error {
|
|||
}()
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleError("database not available: %v", err)
|
||||
}
|
||||
if err := validateLinearConfig(nil); err != nil {
|
||||
if err := validateLinearConfigForStore(trackerStore, nil); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
teamIDs := getLinearTeamIDs(ctx, nil)
|
||||
teamIDs := getLinearTeamIDsForStore(ctx, trackerStore, nil)
|
||||
if len(teamIDs) > 1 {
|
||||
return HandleError("linear push does not support multiple configured teams\nUse: bd linear sync --push --team <TEAM_ID>")
|
||||
}
|
||||
|
||||
lt := &linear.Tracker{}
|
||||
lt.SetTeamIDs(teamIDs)
|
||||
if err := lt.Init(ctx, store); err != nil {
|
||||
if err := lt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleError("initializing Linear tracker: %v", err)
|
||||
}
|
||||
if err := lt.ValidatePushStateMappings(ctx); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(lt, store, actor)
|
||||
engine := tracker.NewEngine(lt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
engine.PushHooks = buildLinearPushHooks(ctx, lt, len(args) > 0)
|
||||
engine.PushHooks = buildLinearPushHooksForStore(ctx, trackerStore, lt, len(args) > 0)
|
||||
|
||||
result, err := engine.Sync(ctx, tracker.SyncOptions{
|
||||
Push: true,
|
||||
|
|
@ -532,26 +535,27 @@ func runLinearPull(cmd *cobra.Command, args []string) error {
|
|||
}()
|
||||
}
|
||||
|
||||
if err := ensureStoreActive(); err != nil {
|
||||
trackerStore, err := trackerStoreForCommand(rootCtx)
|
||||
if err != nil {
|
||||
return HandleError("database not available: %v", err)
|
||||
}
|
||||
if err := validateLinearConfig(nil); err != nil {
|
||||
if err := validateLinearConfigForStore(trackerStore, nil); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
ctx := rootCtx
|
||||
teamIDs := getLinearTeamIDs(ctx, nil)
|
||||
teamIDs := getLinearTeamIDsForStore(ctx, trackerStore, nil)
|
||||
|
||||
lt := &linear.Tracker{}
|
||||
lt.SetTeamIDs(teamIDs)
|
||||
if err := lt.Init(ctx, store); err != nil {
|
||||
if err := lt.Init(ctx, trackerStore); err != nil {
|
||||
return HandleError("initializing Linear tracker: %v", err)
|
||||
}
|
||||
|
||||
engine := tracker.NewEngine(lt, store, actor)
|
||||
engine := tracker.NewEngine(lt, trackerStore, actor)
|
||||
engine.OnMessage = func(msg string) { fmt.Println(" " + msg) }
|
||||
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
|
||||
engine.PullHooks = buildLinearPullHooks(ctx, linearPullHookOptions{
|
||||
engine.PullHooks = buildLinearPullHooksForStore(ctx, trackerStore, linearPullHookOptions{
|
||||
DryRun: dryRun,
|
||||
Actor: actor,
|
||||
})
|
||||
|
|
|
|||
202
cmd/bd/tracker_frontdoor_proxy_test.go
Normal file
202
cmd/bd/tracker_frontdoor_proxy_test.go
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/linear"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// configOnlyTrackerStore exercises front-door validation without constructing
|
||||
// a local Dolt store. The embedded interface is intentionally unused by these
|
||||
// tests; any accidental persistence call will panic, making the boundary
|
||||
// regression visible.
|
||||
type configOnlyTrackerStore struct {
|
||||
tracker.Store
|
||||
config map[string]string
|
||||
}
|
||||
|
||||
func (s *configOnlyTrackerStore) GetConfig(_ context.Context, key string) (string, error) {
|
||||
return s.config[key], nil
|
||||
}
|
||||
|
||||
func TestValidateLinearConfigForStoreUsesProxiedStore(t *testing.T) {
|
||||
old := rootCtx
|
||||
rootCtx = context.Background()
|
||||
t.Cleanup(func() { rootCtx = old })
|
||||
t.Setenv("LINEAR_OAUTH_CLIENT_ID", "client")
|
||||
t.Setenv("LINEAR_OAUTH_CLIENT_SECRET", "secret")
|
||||
t.Setenv("LINEAR_TEAM_ID", "12345678-1234-1234-1234-123456789abc")
|
||||
|
||||
st := &configOnlyTrackerStore{config: map[string]string{}}
|
||||
if err := validateLinearConfigForStore(st, nil); err != nil {
|
||||
t.Fatalf("proxied Linear validation failed: %v", err)
|
||||
}
|
||||
got := getLinearTeamIDsForStore(context.Background(), st, nil)
|
||||
if len(got) != 1 || got[0] != "12345678-1234-1234-1234-123456789abc" {
|
||||
t.Fatalf("team IDs = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateJiraConfigForStoreUsesProxiedStore(t *testing.T) {
|
||||
old := rootCtx
|
||||
rootCtx = context.Background()
|
||||
t.Cleanup(func() { rootCtx = old })
|
||||
|
||||
st := &configOnlyTrackerStore{config: map[string]string{
|
||||
"jira.url": "https://example.atlassian.net",
|
||||
"jira.project": "GC",
|
||||
"jira.api_token": "token",
|
||||
}}
|
||||
if err := validateJiraConfigForStore(st); err != nil {
|
||||
t.Fatalf("proxied Jira validation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateJiraConfigForStoreRejectsMissingStore(t *testing.T) {
|
||||
if err := validateJiraConfigForStore(nil); err == nil {
|
||||
t.Fatal("expected missing-store error")
|
||||
}
|
||||
}
|
||||
|
||||
type parentLinkStore struct {
|
||||
configOnlyTrackerStore
|
||||
issues []*types.Issue
|
||||
deps map[string][]*types.IssueWithDependencyMetadata
|
||||
}
|
||||
|
||||
func (s *parentLinkStore) SearchIssues(context.Context, string, types.IssueFilter) ([]*types.Issue, error) {
|
||||
return s.issues, nil
|
||||
}
|
||||
|
||||
func (s *parentLinkStore) GetDependenciesWithMetadata(_ context.Context, id string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return s.deps[id], nil
|
||||
}
|
||||
|
||||
func TestBuildLinearParentLinksForStoreUsesProxiedStore(t *testing.T) {
|
||||
parentRef := "https://linear.app/team/issue/ENG-1/parent"
|
||||
childRef := "https://linear.app/team/issue/ENG-2/child"
|
||||
parent := &types.Issue{ID: "bd-parent", ExternalRef: &parentRef}
|
||||
child := &types.Issue{ID: "bd-child", ExternalRef: &childRef}
|
||||
st := &parentLinkStore{
|
||||
issues: []*types.Issue{parent, child},
|
||||
deps: map[string][]*types.IssueWithDependencyMetadata{
|
||||
"bd-child": {{Issue: *parent, DependencyType: types.DepParentChild}},
|
||||
},
|
||||
}
|
||||
links, err := buildLinearParentLinksForStore(context.Background(), st, &linear.Tracker{})
|
||||
if err != nil {
|
||||
t.Fatalf("build links: %v", err)
|
||||
}
|
||||
if len(links) != 1 || links[0].ChildIdentifier != "ENG-2" || links[0].ParentIdentifier != "ENG-1" {
|
||||
t.Fatalf("links = %#v", links)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildLinearClientAPIOnlyDoesNotProbeLocalStore(t *testing.T) {
|
||||
oldPath, oldStore := dbPath, store
|
||||
dbPath, store = "/definitely/missing/local/beads.db", nil
|
||||
t.Cleanup(func() { dbPath, store = oldPath, oldStore })
|
||||
t.Setenv("LINEAR_API_KEY", "api-key")
|
||||
client, err := buildLinearClientAPIOnly(context.Background(), "", nil)
|
||||
if err != nil || client == nil {
|
||||
t.Fatalf("API-only client: client=%v err=%v", client, err)
|
||||
}
|
||||
}
|
||||
|
||||
type teamsRoundTripper struct{ called bool }
|
||||
|
||||
func (rt *teamsRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
rt.called = true
|
||||
if req.Method != http.MethodPost || req.URL.String() != "https://api.linear.app/graphql" {
|
||||
return nil, fmt.Errorf("unexpected request %s %s", req.Method, req.URL)
|
||||
}
|
||||
body := `{"data":{"teams":{"nodes":[{"id":"team-1","name":"Engineering","key":"ENG"}]}}}`
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: req}, nil
|
||||
}
|
||||
|
||||
func TestRunLinearTeamsAPIOnlyDoesNotOpenLocalStore(t *testing.T) {
|
||||
oldPath, oldStore, oldJSON, oldCtx := dbPath, store, jsonOutput, rootCtx
|
||||
oldTransport := http.DefaultTransport
|
||||
t.Cleanup(func() {
|
||||
dbPath, store, jsonOutput, rootCtx, http.DefaultTransport = oldPath, oldStore, oldJSON, oldCtx, oldTransport
|
||||
})
|
||||
dbPath, store, jsonOutput, rootCtx = "/definitely/missing/local/beads.db", nil, true, context.Background()
|
||||
t.Setenv("LINEAR_API_KEY", "api-key")
|
||||
rt := &teamsRoundTripper{}
|
||||
http.DefaultTransport = rt
|
||||
if err := runLinearTeams(nil, nil); err != nil {
|
||||
t.Fatalf("runLinearTeams: %v", err)
|
||||
}
|
||||
if !rt.called {
|
||||
t.Fatal("expected Linear teams API request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildLinearClientAPIOnlyUsesProxiedEndpointConfig(t *testing.T) {
|
||||
t.Setenv("LINEAR_API_KEY", "api-key")
|
||||
st := &configOnlyTrackerStore{config: map[string]string{"linear.api_endpoint": "https://proxy.example/graphql"}}
|
||||
oldTransport := http.DefaultTransport
|
||||
t.Cleanup(func() { http.DefaultTransport = oldTransport })
|
||||
http.DefaultTransport = roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.String() != "https://proxy.example/graphql" {
|
||||
return nil, fmt.Errorf("unexpected endpoint %s", req.URL)
|
||||
}
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"data":{"teams":{"nodes":[]}}}`)), Header: make(http.Header), Request: req}, nil
|
||||
})
|
||||
client, err := buildLinearClientAPIOnly(context.Background(), "", st)
|
||||
if err != nil {
|
||||
t.Fatalf("build client: %v", err)
|
||||
}
|
||||
if _, err := client.FetchTeams(context.Background()); err != nil {
|
||||
t.Fatalf("fetch teams: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildLinearClientAPIOnlyUsesProxiedEndpointConfigOnOAuth is the OAuth
|
||||
// mirror of the test above. The helper must apply linear.api_endpoint on BOTH
|
||||
// auth branches, as Tracker.Init does; an early return on the OAuth branch
|
||||
// silently ignores a configured custom endpoint whenever OAuth credentials are
|
||||
// present, which is the whole set of CI environments.
|
||||
func TestBuildLinearClientAPIOnlyUsesProxiedEndpointConfigOnOAuth(t *testing.T) {
|
||||
t.Setenv("LINEAR_OAUTH_CLIENT_ID", "client")
|
||||
t.Setenv("LINEAR_OAUTH_CLIENT_SECRET", "secret")
|
||||
st := &configOnlyTrackerStore{config: map[string]string{"linear.api_endpoint": "https://proxy.example/graphql"}}
|
||||
oldTransport := http.DefaultTransport
|
||||
t.Cleanup(func() { http.DefaultTransport = oldTransport })
|
||||
|
||||
// The OAuth branch makes two requests, and only the second is the one under
|
||||
// test: the token exchange goes to the OAuth token endpoint, which
|
||||
// linear.api_endpoint does not and should not redirect. Serve that one a
|
||||
// token and record where the GraphQL call actually lands.
|
||||
var graphQLURL string
|
||||
http.DefaultTransport = roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.String() == linear.DefaultOAuthTokenURL {
|
||||
body := `{"access_token":"tok","token_type":"Bearer","expires_in":3600}`
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: req}, nil
|
||||
}
|
||||
graphQLURL = req.URL.String()
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"data":{"teams":{"nodes":[]}}}`)), Header: make(http.Header), Request: req}, nil
|
||||
})
|
||||
|
||||
client, err := buildLinearClientAPIOnly(context.Background(), "", st)
|
||||
if err != nil {
|
||||
t.Fatalf("build client: %v", err)
|
||||
}
|
||||
if _, err := client.FetchTeams(context.Background()); err != nil {
|
||||
t.Fatalf("fetch teams: %v", err)
|
||||
}
|
||||
if graphQLURL != "https://proxy.example/graphql" {
|
||||
t.Fatalf("OAuth branch sent GraphQL to %q, want the configured linear.api_endpoint", graphQLURL)
|
||||
}
|
||||
}
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
27
cmd/bd/tracker_store.go
Normal file
27
cmd/bd/tracker_store.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
)
|
||||
|
||||
// trackerStoreForCommand selects the tracker synchronization seam without
|
||||
// opening a second direct store in proxied mode.
|
||||
func trackerStoreForCommand(ctx context.Context) (tracker.Store, error) {
|
||||
if usesProxiedServer() {
|
||||
if uowProvider == nil {
|
||||
return nil, fmt.Errorf("proxied-server provider not initialized")
|
||||
}
|
||||
store := tracker.NewUOWStore(uowProvider)
|
||||
if store == nil {
|
||||
return nil, fmt.Errorf("proxied-server provider is invalid")
|
||||
}
|
||||
return store, nil
|
||||
}
|
||||
if err := ensureStoreActiveWithContext(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tracker.NewStore(store), nil
|
||||
}
|
||||
90
cmd/bd/transform_capability.go
Normal file
90
cmd/bd/transform_capability.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type TransformCapability struct {
|
||||
Path string
|
||||
Argument string
|
||||
Outcome ProxyCapabilityOutcome
|
||||
Code string
|
||||
Message string
|
||||
ExitCode int
|
||||
Mutates bool
|
||||
}
|
||||
|
||||
var transformCapabilityRows = []TransformCapability{
|
||||
transformRefused("rename"),
|
||||
transformRefused("rename-prefix"),
|
||||
transformRefusedWithArgs("rename-prefix", "--dry-run"),
|
||||
transformRefusedWithArgs("rename-prefix", "--repair"),
|
||||
transformRefusedWithArgs("rename-prefix", "--dry-run --repair"),
|
||||
transformRefused("duplicate"),
|
||||
transformRefusedWithArgs("duplicate", "--of"),
|
||||
transformRefused("supersede"),
|
||||
transformRefusedWithArgs("supersede", "--with"),
|
||||
{Path: "duplicates", Outcome: ProxyOutcomeHonored},
|
||||
transformRefusedWithArgs("duplicates", "--auto-merge"),
|
||||
{Path: "duplicates", Argument: "--auto-merge --dry-run", Outcome: ProxyOutcomeHonored},
|
||||
}
|
||||
|
||||
func transformRefused(path string) TransformCapability {
|
||||
return transformRefusedWithArgs(path, "")
|
||||
}
|
||||
|
||||
func transformRefusedWithArgs(path, argument string) TransformCapability {
|
||||
displayPath := path
|
||||
if argument != "" {
|
||||
displayPath += " " + argument
|
||||
}
|
||||
return TransformCapability{
|
||||
Path: path, Argument: argument, Outcome: ProxyOutcomeRefused,
|
||||
Code: "proxy.transform.unsupported", Message: displayPath + " is not supported in proxied-server mode", ExitCode: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func transformCapabilityError(row TransformCapability) *ProxyCapabilityError {
|
||||
return &ProxyCapabilityError{Code: row.Code, Message: row.Message, ExitCode: row.ExitCode, Mutates: row.Mutates}
|
||||
}
|
||||
|
||||
func lookupTransformCapability(cmd *cobra.Command) (TransformCapability, bool) {
|
||||
path := cmd.CommandPath()
|
||||
path = strings.TrimSpace(strings.TrimPrefix(path, cmd.Root().Name()))
|
||||
var args []string
|
||||
cmd.Flags().Visit(func(f *pflag.Flag) {
|
||||
if transformPolicyFlags[f.Name] {
|
||||
args = append(args, "--"+f.Name)
|
||||
}
|
||||
})
|
||||
sort.Strings(args)
|
||||
arg := strings.Join(args, " ")
|
||||
for _, row := range transformCapabilityRows {
|
||||
if row.Path == path && row.Argument == arg {
|
||||
return row, true
|
||||
}
|
||||
}
|
||||
return TransformCapability{}, false
|
||||
}
|
||||
|
||||
// Only flags that alter a transform's execution belong in the capability key.
|
||||
// Output, tracing, and other inherited root flags must not bypass a refusal.
|
||||
var transformPolicyFlags = map[string]bool{
|
||||
"auto-merge": true, "dry-run": true, "repair": true, "of": true, "with": true,
|
||||
}
|
||||
|
||||
func validateProxyTransformBeforeProvider(cmd *cobra.Command) error {
|
||||
if cmd == nil {
|
||||
return nil
|
||||
}
|
||||
if row, ok := lookupTransformCapability(cmd); ok {
|
||||
if row.Outcome == ProxyOutcomeRefused {
|
||||
return HandleProxyCapabilityError(transformCapabilityError(row))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
187
cmd/bd/transform_capability_test.go
Normal file
187
cmd/bd/transform_capability_test.go
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func transformTestCommand(path string, flags ...string) *cobra.Command {
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
cmd := &cobra.Command{Use: path}
|
||||
for _, flag := range flags {
|
||||
if flag == "of" || flag == "with" {
|
||||
cmd.Flags().String(flag, "", "")
|
||||
} else {
|
||||
cmd.Flags().Bool(flag, false, "")
|
||||
}
|
||||
}
|
||||
root.AddCommand(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func TestProxyTransformCapabilityRows(t *testing.T) {
|
||||
cases := []struct {
|
||||
name, path, argument string
|
||||
flags []string
|
||||
outcome ProxyCapabilityOutcome
|
||||
code, message string
|
||||
exit int
|
||||
mutates bool
|
||||
}{
|
||||
{name: "rename", path: "rename", outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "rename is not supported in proxied-server mode", exit: 1},
|
||||
{name: "rename-prefix", path: "rename-prefix", outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "rename-prefix is not supported in proxied-server mode", exit: 1},
|
||||
{name: "rename-prefix dry-run", path: "rename-prefix", argument: "--dry-run", flags: []string{"dry-run"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "rename-prefix --dry-run is not supported in proxied-server mode", exit: 1},
|
||||
{name: "rename-prefix repair", path: "rename-prefix", argument: "--repair", flags: []string{"repair"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "rename-prefix --repair is not supported in proxied-server mode", exit: 1},
|
||||
{name: "rename-prefix repair dry-run", path: "rename-prefix", argument: "--dry-run --repair", flags: []string{"dry-run", "repair"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "rename-prefix --dry-run --repair is not supported in proxied-server mode", exit: 1},
|
||||
{name: "duplicate", path: "duplicate", outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "duplicate is not supported in proxied-server mode", exit: 1},
|
||||
{name: "duplicate of", path: "duplicate", argument: "--of", flags: []string{"of"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "duplicate --of is not supported in proxied-server mode", exit: 1},
|
||||
{name: "supersede", path: "supersede", outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "supersede is not supported in proxied-server mode", exit: 1},
|
||||
{name: "supersede with", path: "supersede", argument: "--with", flags: []string{"with"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "supersede --with is not supported in proxied-server mode", exit: 1},
|
||||
{name: "duplicates", path: "duplicates", flags: []string{"auto-merge", "dry-run"}, outcome: ProxyOutcomeHonored},
|
||||
{name: "duplicates auto-merge", path: "duplicates", argument: "--auto-merge", flags: []string{"auto-merge", "dry-run"}, outcome: ProxyOutcomeRefused, code: "proxy.transform.unsupported", message: "duplicates --auto-merge is not supported in proxied-server mode", exit: 1},
|
||||
{name: "duplicates auto-merge dry-run", path: "duplicates", argument: "--auto-merge --dry-run", flags: []string{"auto-merge", "dry-run"}, outcome: ProxyOutcomeHonored},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cmd := transformTestCommand(tc.path, tc.flags...)
|
||||
if tc.argument != "" {
|
||||
for _, flag := range strings.Fields(tc.argument) {
|
||||
if err := cmd.Flags().Set(strings.TrimPrefix(flag, "--"), "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
row, ok := lookupTransformCapability(cmd)
|
||||
if !ok {
|
||||
t.Fatalf("lookup(%q, %q) missing row", tc.path, tc.argument)
|
||||
}
|
||||
if row.Path != tc.path || row.Argument != tc.argument || row.Outcome != tc.outcome {
|
||||
t.Fatalf("row = %#v, want path=%q argument=%q outcome=%q", row, tc.path, tc.argument, tc.outcome)
|
||||
}
|
||||
if tc.outcome == ProxyOutcomeRefused {
|
||||
if row.Code != tc.code || row.Message != tc.message || row.ExitCode != tc.exit || row.Mutates != tc.mutates {
|
||||
t.Fatalf("refusal fields = code=%q message=%q exit=%d mutates=%t", row.Code, row.Message, row.ExitCode, row.Mutates)
|
||||
}
|
||||
typed := transformCapabilityError(row)
|
||||
var got *ProxyCapabilityError
|
||||
if !errors.As(typed, &got) || got.Code != tc.code || got.ExitCode != 1 || got.Mutates {
|
||||
t.Fatalf("typed refusal = %#v", typed)
|
||||
}
|
||||
}
|
||||
if err := validateProxyTransformBeforeProvider(cmd); tc.outcome == ProxyOutcomeHonored && err != nil {
|
||||
t.Fatalf("honored transform refused: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyDuplicatesAutoMergeDryRunMatrix(t *testing.T) {
|
||||
cmd := transformTestCommand("duplicates", "auto-merge", "dry-run")
|
||||
_ = cmd.Flags().Set("auto-merge", "true")
|
||||
if err := validateProxyTransformBeforeProvider(cmd); err == nil {
|
||||
t.Fatalf("non-dry auto-merge error = %v", err)
|
||||
}
|
||||
_ = cmd.Flags().Set("dry-run", "true")
|
||||
if err := validateProxyTransformBeforeProvider(cmd); err != nil {
|
||||
t.Fatalf("dry-run auto-merge refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyTransformRefusalRendering(t *testing.T) {
|
||||
t.Setenv("BD_JSON_ENVELOPE", "")
|
||||
cmd := transformTestCommand("rename")
|
||||
oldJSON := jsonOutput
|
||||
t.Cleanup(func() { jsonOutput = oldJSON })
|
||||
|
||||
jsonOutput = false
|
||||
var textErr string
|
||||
stderr := captureStderr(t, func() {
|
||||
err := validateProxyTransformBeforeProvider(cmd)
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("exit = %v, want 1", err)
|
||||
}
|
||||
textErr = err.Error()
|
||||
})
|
||||
if textErr != "exit code 1" || stderr != "Error: rename is not supported in proxied-server mode\n" {
|
||||
t.Fatalf("text refusal = %q (err %q)", stderr, textErr)
|
||||
}
|
||||
|
||||
jsonOutput = true
|
||||
stdout := captureStdout(t, func() error {
|
||||
_ = validateProxyTransformBeforeProvider(cmd)
|
||||
return nil
|
||||
})
|
||||
var envelope map[string]any
|
||||
if err := json.Unmarshal([]byte(stdout), &envelope); err != nil {
|
||||
t.Fatalf("JSON refusal: %v (%q)", err, stdout)
|
||||
}
|
||||
if envelope["code"] != "proxy.transform.unsupported" || envelope["error"] != "rename is not supported in proxied-server mode" {
|
||||
t.Fatalf("JSON refusal = %#v", envelope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyTransformMatcherIgnoresInheritedOutputFlags(t *testing.T) {
|
||||
root := &cobra.Command{Use: "bd"}
|
||||
root.PersistentFlags().Bool("json", false, "output JSON")
|
||||
cmd := &cobra.Command{Use: "duplicates"}
|
||||
cmd.Flags().Bool("auto-merge", false, "")
|
||||
cmd.Flags().Bool("dry-run", false, "")
|
||||
root.AddCommand(cmd)
|
||||
if err := cmd.Flags().Set("auto-merge", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := root.PersistentFlags().Set("json", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
row, ok := lookupTransformCapability(cmd)
|
||||
if !ok || row.Argument != "--auto-merge" || row.Outcome != ProxyOutcomeRefused {
|
||||
t.Fatalf("row with inherited --json = %#v, ok=%v", row, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyTransformRefusalHasNoProviderOrFileMutation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
version := []byte("version-before\n")
|
||||
issues := []byte(`{"id":"before","title":"untouched"}` + "\n")
|
||||
versionPath := dir + "/.local_version"
|
||||
issuesPath := dir + "/issues.jsonl"
|
||||
if err := os.WriteFile(versionPath, version, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(issuesPath, issues, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldProvider := uowProvider
|
||||
uowProvider = nil
|
||||
t.Cleanup(func() { uowProvider = oldProvider })
|
||||
|
||||
cmd := transformTestCommand("supersede")
|
||||
err := validateProxyTransformBeforeProvider(cmd)
|
||||
if err == nil {
|
||||
t.Fatal("expected transform refusal")
|
||||
}
|
||||
if code, ok := exitCodeFromError(err); !ok || code != 1 {
|
||||
t.Fatalf("exit = %v, want 1", err)
|
||||
}
|
||||
if uowProvider != nil {
|
||||
t.Fatal("transform refusal initialized a provider")
|
||||
}
|
||||
gotVersion, err := os.ReadFile(versionPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gotIssues, err := os.ReadFile(issuesPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(gotVersion, version) || !bytes.Equal(gotIssues, issues) {
|
||||
t.Fatal("transform refusal mutated workspace files")
|
||||
}
|
||||
}
|
||||
45
engdocs/design/ownership-handoff-contract.md
Normal file
45
engdocs/design/ownership-handoff-contract.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Direct-local ownership handoff contract
|
||||
|
||||
This contract defines the Beads-side boundary required before Gas City can
|
||||
retire a legacy GC-managed direct-local Dolt server. It is intentionally an
|
||||
explicit operation; normal `bd` or `gc` startup never invokes it.
|
||||
|
||||
## Request identity
|
||||
|
||||
The handoff request must name the canonical, real-path Dolt data root, Dolt
|
||||
database, Beads project/workspace identity, endpoint (host/port or socket),
|
||||
and current owner (`legacy-gc`). The provider rejects missing, symlinked,
|
||||
non-canonical, or conflicting identity. It must never create a replacement
|
||||
root or database when identity cannot be proved.
|
||||
|
||||
## Journaled phases
|
||||
|
||||
The operation persists an atomic journal beside the Beads metadata. Every
|
||||
retry resumes from the last durable phase and a committed retry is a no-op.
|
||||
|
||||
1. `prepared`: snapshot metadata, config, endpoint, ownership, and sentinel.
|
||||
2. `target_configured`: validate that bd can open the exact root/database.
|
||||
3. `old_owner_stopped`: stop only the positively identified GC owner through
|
||||
its existing lifecycle interface; a live or unknown process fails closed.
|
||||
4. `verified`: start through bd and verify endpoint, database identity, and
|
||||
sentinel rows.
|
||||
5. `committed`: atomically record owner `bd` and retire only GC artifacts whose
|
||||
ownership matches the snapshot.
|
||||
|
||||
Any failure before `committed` leaves `legacy-gc` authoritative. Rollback
|
||||
restores the byte/row/config snapshot and never kills an unverified process.
|
||||
The journal records the error and phase so operators can retry or inspect it.
|
||||
|
||||
## Front-door shape
|
||||
|
||||
The eventual CLI/API must expose dry-run, execute, and resume behavior with a
|
||||
typed result containing `phase`, `owner`, `root`, `database`, `endpoint`,
|
||||
`mutates`, and `error_code`. Text and JSON responses share the same values;
|
||||
non-zero exits are required for every refusal. Dry-run and all refusals have
|
||||
`mutates=false` and must not open a provider.
|
||||
|
||||
External TCP/Unix endpoints, embedded stores, wrong-root/database requests,
|
||||
missing or stale runtime state, concurrent starts, and duplicate-owner
|
||||
conditions are refusal boundaries. Existing rows, event history, metadata,
|
||||
locks, PID records, and unrelated files are no-mutation artifacts in those
|
||||
cases.
|
||||
|
|
@ -11,7 +11,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -37,7 +36,7 @@ var adoShorthandPattern = regexp.MustCompile(`^ado:([1-9]\d*)$`)
|
|||
// ADO and the local beads database.
|
||||
type Tracker struct {
|
||||
client *Client
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
mapper tracker.FieldMapper
|
||||
baseURL string // Resolved base URL for external ref matching
|
||||
org string
|
||||
|
|
@ -75,7 +74,7 @@ func (t *Tracker) ConfigPrefix() string { return "ado" }
|
|||
|
||||
// Init initializes the tracker with configuration from the beads config store.
|
||||
// No network calls are made during initialization.
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
|
||||
pat := t.getConfig(ctx, "ado.pat", "AZURE_DEVOPS_PAT")
|
||||
|
|
|
|||
|
|
@ -364,6 +364,69 @@ func WorkspaceYamlValue(beadsDir, key string) (string, bool) {
|
|||
return readYamlValueAtPath(filepath.Join(beadsDir, "config.yaml"), key)
|
||||
}
|
||||
|
||||
// WorkspaceYamlValueStrict reads one dotted key from a workspace config.yaml
|
||||
// without conflating a malformed or unreadable file with an absent key. A
|
||||
// missing file or key returns present=false and no error; all other I/O and
|
||||
// YAML shape errors are returned to the caller.
|
||||
func WorkspaceYamlValueStrict(beadsDir, key string) (value string, present bool, err error) {
|
||||
if beadsDir == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
data, err := os.ReadFile(filepath.Join(beadsDir, "config.yaml")) //nolint:gosec // beadsDir is caller-resolved workspace state
|
||||
if os.IsNotExist(err) {
|
||||
return "", false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", false, fmt.Errorf("reading workspace config.yaml: %w", err)
|
||||
}
|
||||
var root map[string]interface{}
|
||||
if err := yaml.Unmarshal(data, &root); err != nil {
|
||||
return "", false, fmt.Errorf("parsing workspace config.yaml: %w", err)
|
||||
}
|
||||
if root == nil {
|
||||
return "", false, nil
|
||||
}
|
||||
if raw, ok := root[key]; ok {
|
||||
value, err := strictYamlScalarString(raw)
|
||||
if err != nil {
|
||||
return "", true, err
|
||||
}
|
||||
return value, true, nil
|
||||
}
|
||||
var node interface{} = root
|
||||
parts := strings.Split(key, ".")
|
||||
for i, part := range parts {
|
||||
m, ok := node.(map[string]interface{})
|
||||
if !ok {
|
||||
return "", true, fmt.Errorf("workspace config key %q has a non-map parent", key)
|
||||
}
|
||||
node, ok = m[part]
|
||||
if !ok {
|
||||
return "", false, nil
|
||||
}
|
||||
if i == len(parts)-1 {
|
||||
value, err := strictYamlScalarString(node)
|
||||
if err != nil {
|
||||
return "", true, err
|
||||
}
|
||||
return value, true, nil
|
||||
}
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
func strictYamlScalarString(v interface{}) (string, error) {
|
||||
if v == nil {
|
||||
return "", fmt.Errorf("workspace config value is null")
|
||||
}
|
||||
switch v.(type) {
|
||||
case map[string]interface{}, []interface{}:
|
||||
return "", fmt.Errorf("workspace config value must be a scalar")
|
||||
default:
|
||||
return fmt.Sprintf("%v", v), nil
|
||||
}
|
||||
}
|
||||
|
||||
func readYamlValueAtPath(path, key string) (string, bool) {
|
||||
data, err := os.ReadFile(path) //nolint:gosec // path is a resolved config.yaml path, not user input
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -409,6 +409,53 @@ func TestSetYamlConfigInDir_WritesTargetConfigDespiteLocalStub(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestWorkspaceYamlValueStrictDistinguishesMissingAndMalformed(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if value, present, err := WorkspaceYamlValueStrict(beadsDir, "dolt.shared-server"); err != nil || present || value != "" {
|
||||
t.Fatalf("missing config = (%q, %v, %v), want empty/false/nil", value, present, err)
|
||||
}
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt: ["), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := WorkspaceYamlValueStrict(beadsDir, "dolt.shared-server"); err == nil {
|
||||
t.Fatal("malformed config should return an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkspaceYamlValueStrictReadsNestedScalar(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte("dolt:\n shared-server: false\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, present, err := WorkspaceYamlValueStrict(beadsDir, "dolt.shared-server")
|
||||
if err != nil || !present || value != "false" {
|
||||
t.Fatalf("nested scalar = (%q, %v, %v), want false/true/nil", value, present, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkspaceYamlValueStrictRejectsNullAndWrongParent(t *testing.T) {
|
||||
for _, body := range []string{"dolt:\n shared-server: null\n", "dolt: false\n", "dolt: []\n"} {
|
||||
t.Run(strings.ReplaceAll(body, "\n", "_"), func(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "config.yaml"), []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := WorkspaceYamlValueStrict(beadsDir, "dolt.shared-server"); err == nil {
|
||||
t.Fatalf("config %q should be rejected", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetYamlConfigInDir_ValidatesBeforeOpeningConfig(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
|
||||
|
|
|
|||
|
|
@ -48,9 +48,38 @@ func SaveProxiedServerClientInfo(beadsDir string, info *ProxiedServerClientInfo)
|
|||
return fmt.Errorf("marshaling %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
path := ProxiedServerClientInfoPath(beadsDir)
|
||||
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||||
tmp, err := os.CreateTemp(beadsDir, ".proxied-server-client-info-*.tmp")
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
tmpPath := tmp.Name()
|
||||
defer func() { _ = os.Remove(tmpPath) }()
|
||||
if err = tmp.Chmod(0o600); err == nil {
|
||||
_, err = tmp.Write(data)
|
||||
}
|
||||
if err == nil {
|
||||
err = tmp.Sync()
|
||||
}
|
||||
if closeErr := tmp.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
if err = os.Rename(tmpPath, path); err != nil {
|
||||
return fmt.Errorf("writing %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
d, err := os.Open(beadsDir) // #nosec G304 -- beadsDir is the discovered workspace directory
|
||||
if err != nil {
|
||||
return fmt.Errorf("syncing %s directory: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
if err = d.Sync(); err != nil {
|
||||
_ = d.Close()
|
||||
return fmt.Errorf("syncing %s directory: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
if err = d.Close(); err != nil {
|
||||
return fmt.Errorf("closing %s directory: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -32,14 +31,14 @@ var ghShorthandPattern = regexp.MustCompile(`^github:([1-9]\d*)$`)
|
|||
type Tracker struct {
|
||||
client *Client
|
||||
config *MappingConfig
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
}
|
||||
|
||||
func (t *Tracker) Name() string { return "github" }
|
||||
func (t *Tracker) DisplayName() string { return "GitHub" }
|
||||
func (t *Tracker) ConfigPrefix() string { return "github" }
|
||||
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
|
||||
token := t.getConfig(ctx, "github.token", "GITHUB_TOKEN")
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -36,7 +35,7 @@ const gitLabMilestoneIdentifierPrefix = "milestone:"
|
|||
type Tracker struct {
|
||||
client *Client
|
||||
config *MappingConfig
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
filter *IssueFilter // Optional filters for issue fetching
|
||||
projectPath string // GitLab project path (e.g., "socwave/socwave") for GraphQL
|
||||
}
|
||||
|
|
@ -48,7 +47,7 @@ func (t *Tracker) ConfigPrefix() string { return "gitlab" }
|
|||
// GitLabClient returns the underlying GitLab API client.
|
||||
func (t *Tracker) GitLabClient() *Client { return t.client }
|
||||
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
|
||||
token, err := t.getConfig(ctx, "gitlab.token", "GITLAB_TOKEN")
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -24,7 +23,7 @@ func init() {
|
|||
// Tracker implements tracker.IssueTracker for Jira.
|
||||
type Tracker struct {
|
||||
client *Client
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
jiraURL string
|
||||
projectKeys []string // one or more project keys (first is primary)
|
||||
apiVersion string // "2" or "3" (default: "3")
|
||||
|
|
@ -58,7 +57,7 @@ func (t *Tracker) Name() string { return "jira" }
|
|||
func (t *Tracker) DisplayName() string { return "Jira" }
|
||||
func (t *Tracker) ConfigPrefix() string { return "jira" }
|
||||
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
|
||||
jiraURL, err := t.getConfig(ctx, "jira.url", "JIRA_URL")
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -26,7 +25,7 @@ func init() {
|
|||
type Tracker struct {
|
||||
clients map[string]*Client // keyed by team ID
|
||||
config *MappingConfig
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
teamIDs []string // ordered list of configured team IDs
|
||||
projectID string
|
||||
}
|
||||
|
|
@ -41,7 +40,7 @@ func (t *Tracker) Name() string { return "linear" }
|
|||
func (t *Tracker) DisplayName() string { return "Linear" }
|
||||
func (t *Tracker) ConfigPrefix() string { return "linear" }
|
||||
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
|
||||
// Resolve authentication: OAuth client-credentials takes precedence over API key.
|
||||
|
|
@ -808,7 +807,7 @@ func BuildLabelCacheFromTracker(ctx context.Context, t *Tracker) (*LabelCache, e
|
|||
// configLoaderAdapter wraps storage.Storage to implement linear.ConfigLoader.
|
||||
type configLoaderAdapter struct {
|
||||
ctx context.Context
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
}
|
||||
|
||||
func (c *configLoaderAdapter) GetAllConfig() (map[string]string, error) {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
itracker "github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
|
@ -47,7 +47,7 @@ func init() {
|
|||
|
||||
type Tracker struct {
|
||||
client notionAPI
|
||||
store storage.Storage
|
||||
store tracker.Store
|
||||
config *MappingConfig
|
||||
dataSourceID string
|
||||
viewURL string
|
||||
|
|
@ -65,7 +65,7 @@ func (t *Tracker) Name() string { return "notion" }
|
|||
func (t *Tracker) DisplayName() string { return "Notion" }
|
||||
func (t *Tracker) ConfigPrefix() string { return "notion" }
|
||||
|
||||
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
|
||||
func (t *Tracker) Init(ctx context.Context, store tracker.Store) error {
|
||||
t.store = store
|
||||
t.dataSourceID = t.getConfig(ctx, "notion.data_source_id", "NOTION_DATA_SOURCE_ID")
|
||||
t.viewURL = t.getConfig(ctx, "notion.view_url", "NOTION_VIEW_URL")
|
||||
|
|
|
|||
102
internal/storage/dbproxy/proxy/backend_dial_timeout_test.go
Normal file
102
internal/storage/dbproxy/proxy/backend_dial_timeout_test.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type blockingBackend struct{}
|
||||
|
||||
func (blockingBackend) ID(context.Context) string { return "blocking" }
|
||||
func (blockingBackend) DSN(context.Context, string, string, string) string { return "" }
|
||||
func (blockingBackend) Start(context.Context) error { return nil }
|
||||
func (blockingBackend) Stop(context.Context) error { return nil }
|
||||
func (blockingBackend) Running(context.Context) bool { return true }
|
||||
func (blockingBackend) Dial(ctx context.Context) (net.Conn, error) {
|
||||
<-ctx.Done()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
func TestHandleConnBoundsBlockedBackendDial(t *testing.T) {
|
||||
p := NewProxyServer(ProxyOpts{Server: blockingBackend{}})
|
||||
p.logger = log.Default()
|
||||
client, peer := net.Pipe()
|
||||
defer peer.Close()
|
||||
start := time.Now()
|
||||
err := p.handleConn(context.Background(), client)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("error = %v, want deadline exceeded", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > backendDialTimeout+time.Second {
|
||||
t.Fatalf("blocked dial took %s", elapsed)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, peer)
|
||||
}
|
||||
|
||||
func TestHandleConnCanceledClientDialClosesAndDrains(t *testing.T) {
|
||||
stats := &Stats{}
|
||||
p := NewProxyServer(ProxyOpts{Server: blockingBackend{}, Stats: stats})
|
||||
p.logger = log.Default()
|
||||
client, peer := net.Pipe()
|
||||
defer peer.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
err := p.handleConn(ctx, client)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error = %v, want context canceled", err)
|
||||
}
|
||||
if got := p.activeConns.Load(); got != 0 {
|
||||
t.Fatalf("active connections = %d, want 0", got)
|
||||
}
|
||||
snapshot := stats.Snapshot()
|
||||
if snapshot.BackendDialAttempts != 1 || snapshot.BackendDialErrors != 1 || snapshot.HandledConns != 0 {
|
||||
t.Fatalf("stats = %+v, want one failed dial and no handled conns", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
type tcpBlackholeBackend struct{ address string }
|
||||
|
||||
func (b tcpBlackholeBackend) ID(context.Context) string { return "tcp-blackhole" }
|
||||
func (b tcpBlackholeBackend) DSN(context.Context, string, string, string) string { return b.address }
|
||||
func (b tcpBlackholeBackend) Start(context.Context) error { return nil }
|
||||
func (b tcpBlackholeBackend) Stop(context.Context) error { return nil }
|
||||
func (b tcpBlackholeBackend) Running(context.Context) bool { return true }
|
||||
func (b tcpBlackholeBackend) Dial(ctx context.Context) (net.Conn, error) {
|
||||
d := net.Dialer{}
|
||||
return d.DialContext(ctx, "tcp", b.address)
|
||||
}
|
||||
|
||||
func TestHandleConnTCPBlackholeIsBounded(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
accepted := make(chan net.Conn, 1)
|
||||
go func() { conn, _ := listener.Accept(); accepted <- conn }()
|
||||
p := NewProxyServer(ProxyOpts{Server: tcpBlackholeBackend{address: listener.Addr().String()}, Stats: &Stats{}})
|
||||
p.logger = log.Default()
|
||||
client, peer := net.Pipe()
|
||||
defer peer.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
start := time.Now()
|
||||
err = p.handleConn(ctx, client)
|
||||
if conn := <-accepted; conn != nil {
|
||||
_ = conn.Close()
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("blackhole connection unexpectedly completed without cancellation")
|
||||
}
|
||||
if time.Since(start) > time.Second {
|
||||
t.Fatalf("blackhole dial exceeded bound")
|
||||
}
|
||||
if p.activeConns.Load() != 0 {
|
||||
t.Fatal("active connection leaked")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,464 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/pidfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/server"
|
||||
)
|
||||
|
||||
// unixSocketPath returns a bindable Unix socket path. t.TempDir() embeds the
|
||||
// full test name, which on macOS sits under a long /var/folders/... root and
|
||||
// overflows the ~104-byte sun_path limit, so bind fails with "invalid
|
||||
// argument". A short unnamed temp dir stays well inside the limit.
|
||||
func unixSocketPath(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir, err := os.MkdirTemp("", "bdproxy")
|
||||
if err != nil {
|
||||
t.Fatalf("temp dir for unix socket: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
||||
return filepath.Join(dir, "u.sock")
|
||||
}
|
||||
|
||||
// externalTransportFixture is a deliberately small real TCP/Unix listener
|
||||
// used to exercise the production proxy transport. It withholds all bytes in
|
||||
// blackhole mode and echoes bytes in echo mode. The open set lets tests prove
|
||||
// that the proxy closes the upstream socket instead of relying on fixture
|
||||
// cleanup to hide a leak.
|
||||
type externalTransportFixture struct {
|
||||
t *testing.T
|
||||
network string
|
||||
address string
|
||||
listener net.Listener
|
||||
blackhole bool
|
||||
|
||||
mu sync.Mutex
|
||||
open map[net.Conn]struct{}
|
||||
accepted chan net.Conn
|
||||
doneCh chan struct{}
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func newExternalTransportFixture(t *testing.T, network, address string, blackhole bool) *externalTransportFixture {
|
||||
t.Helper()
|
||||
if network == "unix" {
|
||||
_ = os.Remove(address)
|
||||
}
|
||||
ln, err := net.Listen(network, address)
|
||||
if err != nil {
|
||||
t.Fatalf("listen upstream %s %q: %v", network, address, err)
|
||||
}
|
||||
f := &externalTransportFixture{
|
||||
t: t,
|
||||
network: network,
|
||||
address: ln.Addr().String(),
|
||||
listener: ln,
|
||||
blackhole: blackhole,
|
||||
open: make(map[net.Conn]struct{}),
|
||||
accepted: make(chan net.Conn, 16),
|
||||
doneCh: make(chan struct{}),
|
||||
}
|
||||
go f.acceptLoop()
|
||||
t.Cleanup(func() { f.Close() })
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) acceptLoop() {
|
||||
defer close(f.doneCh)
|
||||
for {
|
||||
conn, err := f.listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
f.mu.Lock()
|
||||
f.open[conn] = struct{}{}
|
||||
f.mu.Unlock()
|
||||
select {
|
||||
case f.accepted <- conn:
|
||||
default:
|
||||
_ = conn.Close()
|
||||
}
|
||||
go f.serve(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) serve(conn net.Conn) {
|
||||
defer func() {
|
||||
f.mu.Lock()
|
||||
delete(f.open, conn)
|
||||
f.mu.Unlock()
|
||||
_ = conn.Close()
|
||||
}()
|
||||
if f.blackhole {
|
||||
// Reading, without writing a protocol response, models an upstream
|
||||
// that accepted the transport but never completes a SQL handshake.
|
||||
var b [1]byte
|
||||
_, _ = conn.Read(b[:])
|
||||
return
|
||||
}
|
||||
_, _ = io.Copy(conn, conn)
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) nextAccepted(timeout time.Duration) net.Conn {
|
||||
f.t.Helper()
|
||||
select {
|
||||
case conn := <-f.accepted:
|
||||
return conn
|
||||
case <-time.After(timeout):
|
||||
f.t.Fatalf("upstream %s did not accept a connection within %s", f.network, timeout)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) openCount() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return len(f.open)
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) Close() {
|
||||
f.closeOnce.Do(func() {
|
||||
f.mu.Lock()
|
||||
listener := f.listener
|
||||
conns := make([]net.Conn, 0, len(f.open))
|
||||
for conn := range f.open {
|
||||
conns = append(conns, conn)
|
||||
}
|
||||
f.mu.Unlock()
|
||||
_ = listener.Close()
|
||||
for _, conn := range conns {
|
||||
_ = conn.Close()
|
||||
}
|
||||
select {
|
||||
case <-f.doneCh:
|
||||
case <-time.After(2 * time.Second):
|
||||
f.t.Errorf("upstream %s accept loop did not stop", f.network)
|
||||
}
|
||||
if f.network == "unix" {
|
||||
_ = os.Remove(f.address)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (f *externalTransportFixture) reopen(t *testing.T, blackhole bool) *externalTransportFixture {
|
||||
t.Helper()
|
||||
address := f.address
|
||||
f.Close()
|
||||
return newExternalTransportFixture(t, f.network, address, blackhole)
|
||||
}
|
||||
|
||||
func externalServerConfig(t *testing.T, network, address string) configfile.ExternalDoltConfig {
|
||||
t.Helper()
|
||||
if network == "unix" {
|
||||
return configfile.ExternalDoltConfig{Socket: address}
|
||||
}
|
||||
host, portText, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
t.Fatalf("split upstream address %q: %v", address, err)
|
||||
}
|
||||
port, err := strconv.Atoi(portText)
|
||||
if err != nil {
|
||||
t.Fatalf("parse upstream port %q: %v", portText, err)
|
||||
}
|
||||
return configfile.ExternalDoltConfig{Host: host, Port: port}
|
||||
}
|
||||
|
||||
type runningExternalProxy struct {
|
||||
proxy *proxyServer
|
||||
cancel context.CancelFunc
|
||||
done chan error
|
||||
finished chan struct{}
|
||||
root string
|
||||
port int
|
||||
}
|
||||
|
||||
func startExternalProxy(t *testing.T, fixture *externalTransportFixture) *runningExternalProxy {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
upstream, err := server.NewExternalDoltServer(externalServerConfig(t, fixture.network, fixture.address))
|
||||
if err != nil {
|
||||
t.Fatalf("new external Dolt server: %v", err)
|
||||
}
|
||||
port := freeTCPPortForInternalTest(t)
|
||||
p := NewProxyServer(ProxyOpts{
|
||||
RootDir: root,
|
||||
Port: port,
|
||||
IdleTimeout: IdleTimeoutNever,
|
||||
Server: upstream,
|
||||
Stats: &Stats{},
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
finished := make(chan struct{})
|
||||
go func() {
|
||||
defer close(finished)
|
||||
done <- p.ListenAndServe(ctx)
|
||||
}()
|
||||
waitForInternalPid(t, root, 5*time.Second)
|
||||
t.Cleanup(func() {
|
||||
cancel()
|
||||
select {
|
||||
case <-finished:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Errorf("proxy did not stop during cleanup")
|
||||
}
|
||||
})
|
||||
return &runningExternalProxy{proxy: p, cancel: cancel, done: done, finished: finished, root: root, port: port}
|
||||
}
|
||||
|
||||
func freeTCPPortForInternalTest(t *testing.T) int {
|
||||
t.Helper()
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("allocate proxy port: %v", err)
|
||||
}
|
||||
port := ln.Addr().(*net.TCPAddr).Port
|
||||
_ = ln.Close()
|
||||
return port
|
||||
}
|
||||
|
||||
func waitForInternalPid(t *testing.T, root string, timeout time.Duration) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
pf, err := pidfile.Read(root, PIDFileName)
|
||||
if err == nil && pf != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("proxy pidfile did not appear within %s", timeout)
|
||||
}
|
||||
|
||||
func eventuallyInternal(t *testing.T, timeout time.Duration, check func() bool, message string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if check() {
|
||||
return
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("eventually: %s", message)
|
||||
}
|
||||
|
||||
func dialInternalProxy(t *testing.T, port int) net.Conn {
|
||||
t.Helper()
|
||||
conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(port)), 2*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("dial proxy: %v", err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func consumeReadiness(t *testing.T, fixture *externalTransportFixture) {
|
||||
t.Helper()
|
||||
conn := fixture.nextAccepted(5 * time.Second)
|
||||
if conn == nil {
|
||||
return
|
||||
}
|
||||
eventuallyInternal(t, 2*time.Second, func() bool { return fixture.openCount() == 0 }, "readiness upstream connection closed")
|
||||
}
|
||||
|
||||
func TestExternalProxyEstablishedTunnelClientCloseDrainsTCPAndUnix(t *testing.T) {
|
||||
for _, network := range []string{"tcp", "unix"} {
|
||||
network := network
|
||||
t.Run(network, func(t *testing.T) {
|
||||
if network == "unix" && runtime.GOOS == "windows" {
|
||||
t.Skip("Unix sockets are unavailable on Windows")
|
||||
}
|
||||
address := ""
|
||||
if network == "unix" {
|
||||
address = unixSocketPath(t)
|
||||
}
|
||||
fixture := newExternalTransportFixture(t, network, address, true)
|
||||
running := startExternalProxy(t, fixture)
|
||||
consumeReadiness(t, fixture)
|
||||
client := dialInternalProxy(t, running.port)
|
||||
defer client.Close()
|
||||
fixture.nextAccepted(5 * time.Second)
|
||||
client.Close()
|
||||
eventuallyInternal(t, 2*time.Second, func() bool {
|
||||
return running.proxy.activeConns.Load() == 0 && fixture.openCount() == 0
|
||||
}, "client close did not drain proxy and upstream connections")
|
||||
snapshot := running.proxy.stats.Snapshot()
|
||||
if snapshot.BackendDialAttempts < 1 || snapshot.BackendDialSuccess < 1 || snapshot.HandledConns < 1 {
|
||||
t.Fatalf("stats = %+v, want handled client tunnel", snapshot)
|
||||
}
|
||||
select {
|
||||
case err := <-running.done:
|
||||
t.Fatalf("proxy exited before explicit cancellation: %v", err)
|
||||
default:
|
||||
}
|
||||
running.cancel()
|
||||
select {
|
||||
case <-running.done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("proxy did not stop after cancellation")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalProxyShutdownClosesEstablishedTCPAndUnixTunnels(t *testing.T) {
|
||||
for _, network := range []string{"tcp", "unix"} {
|
||||
network := network
|
||||
t.Run(network, func(t *testing.T) {
|
||||
address := ""
|
||||
if network == "unix" {
|
||||
address = unixSocketPath(t)
|
||||
}
|
||||
fixture := newExternalTransportFixture(t, network, address, true)
|
||||
running := startExternalProxy(t, fixture)
|
||||
consumeReadiness(t, fixture)
|
||||
client := dialInternalProxy(t, running.port)
|
||||
fixture.nextAccepted(5 * time.Second)
|
||||
running.cancel()
|
||||
select {
|
||||
case <-running.done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("proxy shutdown exceeded bounded timeout")
|
||||
}
|
||||
_ = client.SetReadDeadline(time.Now().Add(time.Second))
|
||||
var b [1]byte
|
||||
if _, err := client.Read(b[:]); err == nil {
|
||||
t.Fatal("client read succeeded after proxy shutdown")
|
||||
}
|
||||
_ = client.Close()
|
||||
eventuallyInternal(t, 2*time.Second, func() bool {
|
||||
return running.proxy.activeConns.Load() == 0 && fixture.openCount() == 0
|
||||
}, "proxy shutdown did not drain established tunnel")
|
||||
if got := running.proxy.stats.Snapshot().BackendStopCalls; got != 1 {
|
||||
t.Fatalf("backend stop calls = %d, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalProxyOutageAndRestoreReDialsTCPAndUnix(t *testing.T) {
|
||||
for _, network := range []string{"tcp", "unix"} {
|
||||
network := network
|
||||
t.Run(network, func(t *testing.T) {
|
||||
address := ""
|
||||
if network == "unix" {
|
||||
address = unixSocketPath(t)
|
||||
}
|
||||
fixture := newExternalTransportFixture(t, network, address, false)
|
||||
running := startExternalProxy(t, fixture)
|
||||
consumeReadiness(t, fixture)
|
||||
client := dialInternalProxy(t, running.port)
|
||||
fixture.nextAccepted(5 * time.Second)
|
||||
marker := []byte("proxy-recovery-marker")
|
||||
if _, err := client.Write(marker); err != nil {
|
||||
t.Fatalf("write healthy marker: %v", err)
|
||||
}
|
||||
got := make([]byte, len(marker))
|
||||
if _, err := io.ReadFull(client, got); err != nil {
|
||||
t.Fatalf("read healthy marker: %v", err)
|
||||
}
|
||||
if string(got) != string(marker) {
|
||||
t.Fatalf("healthy echo = %q, want %q", got, marker)
|
||||
}
|
||||
_ = client.Close()
|
||||
eventuallyInternal(t, 2*time.Second, func() bool { return running.proxy.activeConns.Load() == 0 }, "healthy tunnel did not close")
|
||||
|
||||
fixture = fixture.reopen(t, false)
|
||||
// Close the restored listener to create a deterministic outage, then
|
||||
// rebind it in the same test so the proxy must re-dial the endpoint.
|
||||
fixture.Close()
|
||||
failed := dialInternalProxy(t, running.port)
|
||||
_ = failed.SetReadDeadline(time.Now().Add(2 * time.Second))
|
||||
var b [1]byte
|
||||
if _, err := failed.Read(b[:]); err == nil {
|
||||
t.Fatal("outage client unexpectedly received data")
|
||||
}
|
||||
_ = failed.Close()
|
||||
eventuallyInternal(t, 2*time.Second, func() bool {
|
||||
return running.proxy.stats.Snapshot().BackendDialErrors >= 1
|
||||
}, "proxy did not record outage dial error")
|
||||
|
||||
fixture = newExternalTransportFixture(t, network, fixture.address, false)
|
||||
recovered := dialInternalProxy(t, running.port)
|
||||
defer recovered.Close()
|
||||
fixture.nextAccepted(5 * time.Second)
|
||||
if _, err := recovered.Write(marker); err != nil {
|
||||
t.Fatalf("write restored marker: %v", err)
|
||||
}
|
||||
got = make([]byte, len(marker))
|
||||
if _, err := io.ReadFull(recovered, got); err != nil {
|
||||
t.Fatalf("read restored marker: %v", err)
|
||||
}
|
||||
if string(got) != string(marker) {
|
||||
t.Fatalf("restored echo = %q, want %q", got, marker)
|
||||
}
|
||||
snapshot := running.proxy.stats.Snapshot()
|
||||
if snapshot.BackendDialSuccess < 2 || snapshot.HandledConns < 2 {
|
||||
t.Fatalf("recovery stats = %+v, want readiness, healthy, and restored dials", snapshot)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalProxyUnavailableStartupIsBoundedForTCPAndUnix(t *testing.T) {
|
||||
for _, network := range []string{"tcp", "unix"} {
|
||||
network := network
|
||||
t.Run(network, func(t *testing.T) {
|
||||
address := ""
|
||||
if network == "tcp" {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address = ln.Addr().String()
|
||||
_ = ln.Close()
|
||||
} else {
|
||||
address = unixSocketPath(t)
|
||||
}
|
||||
upstream, err := server.NewExternalDoltServer(externalServerConfig(t, network, address))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := t.TempDir()
|
||||
port := freeTCPPortForInternalTest(t)
|
||||
p := NewProxyServer(ProxyOpts{RootDir: root, Port: port, Server: upstream, Stats: &Stats{}})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 700*time.Millisecond)
|
||||
defer cancel()
|
||||
start := time.Now()
|
||||
err = p.ListenAndServe(ctx)
|
||||
if time.Since(start) > 2*time.Second {
|
||||
t.Fatalf("unavailable startup exceeded bound: %s", time.Since(start))
|
||||
}
|
||||
if err == nil || (!errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, context.Canceled) && !containsInternal(err.Error(), "database server not ready")) {
|
||||
t.Fatalf("unavailable startup error = %v", err)
|
||||
}
|
||||
pf, readErr := pidfile.Read(root, PIDFileName)
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if pf != nil {
|
||||
t.Fatalf("unavailable startup published pidfile: %+v", pf)
|
||||
}
|
||||
if got := p.stats.Snapshot().BackendStopCalls; got != 1 {
|
||||
t.Fatalf("backend stop calls = %d, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func containsInternal(value, needle string) bool {
|
||||
return strings.Contains(value, needle)
|
||||
}
|
||||
|
|
@ -84,9 +84,11 @@ const (
|
|||
readyDialTimeout = 2 * time.Second
|
||||
readyInitialBackoff = 50 * time.Millisecond
|
||||
readyMaxBackoff = 1 * time.Second
|
||||
backendDialTimeout = 5 * time.Second
|
||||
idleWatcherMinInterval = 1 * time.Second
|
||||
backendStopTimeout = 5 * time.Minute
|
||||
tcpKeepAlivePeriod = 30 * time.Second
|
||||
backendHealthInterval = 100 * time.Millisecond
|
||||
)
|
||||
|
||||
var errIdleTimeout = errors.New("idle timeout reached")
|
||||
|
|
@ -337,6 +339,20 @@ func (p *proxyServer) ListenAndServe(parentCtx context.Context) error {
|
|||
return nil
|
||||
})
|
||||
g.Go(func() error { return p.idleWatcher(gctx) })
|
||||
g.Go(func() error {
|
||||
t := time.NewTicker(backendHealthInterval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-gctx.Done():
|
||||
return nil
|
||||
case <-t.C:
|
||||
if !p.server.Running(gctx) {
|
||||
return errors.New("database server exited")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
g.Go(func() error { return p.acceptLoop(gctx) })
|
||||
|
||||
runErr := g.Wait()
|
||||
|
|
@ -437,7 +453,9 @@ func (p *proxyServer) handleConn(ctx context.Context, client net.Conn) error {
|
|||
}()
|
||||
|
||||
p.stats.IncBackendDialAttempt()
|
||||
backend, err := p.server.Dial(ctx)
|
||||
dialCtx, cancelDial := context.WithTimeout(ctx, backendDialTimeout)
|
||||
backend, err := p.server.Dial(dialCtx)
|
||||
cancelDial()
|
||||
if err != nil {
|
||||
p.tracef("handleConn(%s) backend dial error: %v", addr, err)
|
||||
p.stats.IncBackendDialError()
|
||||
|
|
|
|||
|
|
@ -406,6 +406,53 @@ func TestProxy_BackendDialError(t *testing.T) {
|
|||
assertNoPidFile(t, root)
|
||||
}
|
||||
|
||||
func TestProxy_BackendDialFailureHasStableDiagnosticAndNoFallbackFiles(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ts := server.New()
|
||||
stats := &proxy.Stats{}
|
||||
port := freeTCPPort(t)
|
||||
root := t.TempDir()
|
||||
h := runProxy(t, proxy.ProxyOpts{
|
||||
RootDir: root, Port: port, Server: ts, Stats: stats,
|
||||
})
|
||||
waitListening(t, root, listenWait)
|
||||
|
||||
before, err := os.ReadDir(root)
|
||||
require.NoError(t, err)
|
||||
ts.SetDialErr(errors.New("upstream unavailable"))
|
||||
c := dialProxy(t, port)
|
||||
_, err = c.Read(make([]byte, 1))
|
||||
assert.Error(t, err)
|
||||
require.NoError(t, c.Close())
|
||||
require.Eventually(t, func() bool {
|
||||
return stats.Snapshot().BackendDialErrors >= 1
|
||||
}, ioTimeout, 10*time.Millisecond)
|
||||
|
||||
logData, err := os.ReadFile(filepath.Join(root, proxy.LogFileName))
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(logData), "backend dial error:")
|
||||
assert.NotContains(t, string(logData), "fallback")
|
||||
after, err := os.ReadDir(root)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, dirEntryNames(before), dirEntryNames(after), "dial failure must not create fallback files")
|
||||
pf, err := pidfile.Read(root, proxy.PIDFileName)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, pf, "proxy remains alive after one upstream dial failure")
|
||||
|
||||
h.Cancel()
|
||||
require.NoError(t, h.waitErr(t, shutdownWait))
|
||||
assertNoPidFile(t, root)
|
||||
}
|
||||
|
||||
func dirEntryNames(entries []os.DirEntry) []string {
|
||||
names := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
names = append(names, entry.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func TestProxy_IdleTimeout_Fires(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
|||
|
|
@ -243,6 +243,35 @@ type IsolatedDoltContainer struct {
|
|||
ctr *dolt.DoltContainer
|
||||
}
|
||||
|
||||
// Stop pauses the container while retaining its data volume.
|
||||
func (c *IsolatedDoltContainer) Stop(ctx context.Context) error {
|
||||
if c == nil || c.ctr == nil {
|
||||
return fmt.Errorf("no Dolt container running")
|
||||
}
|
||||
return c.ctr.Stop(ctx, nil)
|
||||
}
|
||||
|
||||
// Start resumes a container previously stopped with Stop.
|
||||
func (c *IsolatedDoltContainer) Start(ctx context.Context) error {
|
||||
if c == nil || c.ctr == nil {
|
||||
return fmt.Errorf("no Dolt container running")
|
||||
}
|
||||
return c.ctr.Start(ctx)
|
||||
}
|
||||
|
||||
// CurrentPort returns the container's currently mapped SQL port. Container
|
||||
// runtimes may assign a new host port after Stop/Start.
|
||||
func (c *IsolatedDoltContainer) CurrentPort(ctx context.Context) (string, error) {
|
||||
if c == nil || c.ctr == nil {
|
||||
return "", fmt.Errorf("no Dolt container running")
|
||||
}
|
||||
p, err := c.ctr.MappedPort(ctx, "3306/tcp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return p.Port(), nil
|
||||
}
|
||||
|
||||
// Exec runs cmd inside the container and returns its exit code and combined
|
||||
// output, demultiplexed (see containerExec).
|
||||
func (c *IsolatedDoltContainer) Exec(ctx context.Context, cmd []string) (int, string, error) {
|
||||
|
|
|
|||
109
internal/tracker/conformance/conformance_test.go
Normal file
109
internal/tracker/conformance/conformance_test.go
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
package conformance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestRunWithEngineAndUOWFixture(t *testing.T) {
|
||||
Run(t, func(_ *testing.T, f *Fixture) Setup {
|
||||
store := f.StoreFactory.Open().(*Store)
|
||||
store.Config["test.project"] = "PROJ"
|
||||
ref := "https://tracker.test/EXT-1"
|
||||
store.Issues["bd-1"] = &types.Issue{ID: "bd-1", Title: "local", Status: types.StatusOpen, IssueType: types.TypeTask, Priority: 2, Labels: []string{"old"}, ExternalRef: &ref, UpdatedAt: time.Date(2026, 9, 3, 0, 0, 0, 0, time.UTC)}
|
||||
remote := &mockTracker{}
|
||||
return Setup{
|
||||
Engine: tracker.NewEngine(remote, store, "conformance"),
|
||||
Store: store,
|
||||
Expected: Expected{ExternalRef: ref, ConfigKey: "test.project", MetadataKey: "test.last_sync"},
|
||||
Refusal: func(context.Context) (*tracker.SyncResult, error) {
|
||||
return nil, &storage.ErrUnsupported{Op: "proxy-only operation", Backend: "conformance"}
|
||||
},
|
||||
APIOnly: func(ctx context.Context, _ func() tracker.Store) error {
|
||||
f.HTTP.Enqueue(Response{Status: http.StatusOK, Body: `{"teams":[]}`})
|
||||
_, err := f.HTTP.Client().Get("https://tracker.test/teams")
|
||||
return err
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTPDoubleRecordsBodyAndTransportError(t *testing.T) {
|
||||
d := NewHTTPDouble()
|
||||
d.Enqueue(Response{Status: http.StatusCreated, Body: `{"id":"1"}`})
|
||||
resp, err := d.Client().Post("https://tracker.test/issues", "application/json", strings.NewReader(`{"title":"x"}`))
|
||||
if err != nil || resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("response=(%v,%v)", resp, err)
|
||||
}
|
||||
d.Enqueue(Response{Err: context.DeadlineExceeded})
|
||||
if _, err := d.Client().Get("https://tracker.test/issues"); err == nil {
|
||||
t.Fatal("transport error was swallowed")
|
||||
}
|
||||
requests := d.Requests()
|
||||
if len(requests) != 2 || string(requests[0].Body) != `{"title":"x"}` {
|
||||
t.Fatalf("requests=%+v", requests)
|
||||
}
|
||||
if _, err := d.Client().Get("https://tracker.test/underflow"); err == nil {
|
||||
t.Fatal("response queue underflow succeeded")
|
||||
}
|
||||
|
||||
d.Enqueue(Response{Status: http.StatusOK})
|
||||
req, err := http.NewRequest(http.MethodGet, "https://tracker.test/copied", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := d.RoundTrip(req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.URL.Path = "/mutated"
|
||||
if got := d.Requests()[3].URL.Path; got != "/copied" {
|
||||
t.Fatalf("recorded URL aliases request: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
type mockTracker struct{}
|
||||
|
||||
func (*mockTracker) Name() string { return "test" }
|
||||
func (*mockTracker) DisplayName() string { return "Test" }
|
||||
func (*mockTracker) ConfigPrefix() string { return "test" }
|
||||
func (*mockTracker) Init(context.Context, tracker.Store) error { return nil }
|
||||
func (*mockTracker) Validate() error { return nil }
|
||||
func (*mockTracker) Close() error { return nil }
|
||||
func (*mockTracker) FetchIssues(context.Context, tracker.FetchOptions) ([]tracker.TrackerIssue, error) {
|
||||
return []tracker.TrackerIssue{{ID: "EXT-1", Identifier: "EXT-1", URL: "https://tracker.test/EXT-1", Title: "remote", UpdatedAt: time.Date(2026, 9, 3, 1, 0, 0, 0, time.UTC), Labels: []string{"bug"}}}, nil
|
||||
}
|
||||
func (*mockTracker) FetchIssue(context.Context, string) (*tracker.TrackerIssue, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (*mockTracker) CreateIssue(context.Context, *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
return &tracker.TrackerIssue{ID: "EXT-3", Identifier: "EXT-3", URL: "https://tracker.test/EXT-3"}, nil
|
||||
}
|
||||
func (*mockTracker) UpdateIssue(context.Context, string, *types.Issue) (*tracker.TrackerIssue, error) {
|
||||
return &tracker.TrackerIssue{ID: "EXT-1", Identifier: "EXT-1", URL: "https://tracker.test/EXT-1"}, nil
|
||||
}
|
||||
func (*mockTracker) FieldMapper() tracker.FieldMapper { return mockMapper{} }
|
||||
func (*mockTracker) IsExternalRef(string) bool { return true }
|
||||
func (*mockTracker) ExtractIdentifier(ref string) string { return ref[strings.LastIndex(ref, "/")+1:] }
|
||||
func (*mockTracker) BuildExternalRef(issue *tracker.TrackerIssue) string { return issue.URL }
|
||||
|
||||
type mockMapper struct{}
|
||||
|
||||
func (mockMapper) PriorityToBeads(interface{}) int { return 2 }
|
||||
func (mockMapper) PriorityToTracker(int) interface{} { return 2 }
|
||||
func (mockMapper) StatusToBeads(interface{}) types.Status { return types.StatusOpen }
|
||||
func (mockMapper) StatusToTracker(types.Status) interface{} { return "open" }
|
||||
func (mockMapper) TypeToBeads(interface{}) types.IssueType { return types.TypeTask }
|
||||
func (mockMapper) TypeToTracker(types.IssueType) interface{} { return "task" }
|
||||
func (mockMapper) IssueToBeads(issue *tracker.TrackerIssue) *tracker.IssueConversion {
|
||||
return &tracker.IssueConversion{Issue: &types.Issue{ID: "bd-2", Title: issue.Title, Status: types.StatusOpen, IssueType: types.TypeTask, Priority: 2, Labels: issue.Labels}}
|
||||
}
|
||||
func (mockMapper) IssueToTracker(issue *types.Issue) map[string]interface{} {
|
||||
return map[string]interface{}{"title": issue.Title}
|
||||
}
|
||||
96
internal/tracker/conformance/http.go
Normal file
96
internal/tracker/conformance/http.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
// Package conformance contains reusable tracker adapter test fixtures.
|
||||
package conformance
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Request records one outbound tracker request.
|
||||
type Request struct {
|
||||
Method string
|
||||
URL *url.URL
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// Response is one deterministic HTTP response. Err simulates transport
|
||||
// failure; Status and Body are returned otherwise.
|
||||
type Response struct {
|
||||
Status int
|
||||
Body string
|
||||
Err error
|
||||
}
|
||||
|
||||
// HTTPDouble is a deterministic, FIFO HTTP RoundTripper. It records every
|
||||
// request and returns queued responses, making API mapping tests independent of
|
||||
// network services.
|
||||
type HTTPDouble struct {
|
||||
mu sync.Mutex
|
||||
requests []Request
|
||||
responses []Response
|
||||
}
|
||||
|
||||
// NewHTTPDouble returns an empty deterministic transport.
|
||||
func NewHTTPDouble() *HTTPDouble { return &HTTPDouble{} }
|
||||
|
||||
// Enqueue adds a response to the FIFO queue.
|
||||
func (d *HTTPDouble) Enqueue(response Response) {
|
||||
d.mu.Lock()
|
||||
d.responses = append(d.responses, response)
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
// Requests returns a copy of all observed requests.
|
||||
func (d *HTTPDouble) Requests() []Request {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
out := make([]Request, len(d.requests))
|
||||
copy(out, d.requests)
|
||||
return out
|
||||
}
|
||||
|
||||
// Client returns an HTTP client using this transport.
|
||||
func (d *HTTPDouble) Client() *http.Client { return &http.Client{Transport: d} }
|
||||
|
||||
// RoundTrip implements http.RoundTripper.
|
||||
func (d *HTTPDouble) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
var body []byte
|
||||
if req.Body != nil {
|
||||
var err error
|
||||
body, err = io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
d.mu.Lock()
|
||||
urlCopy := *req.URL
|
||||
d.requests = append(d.requests, Request{Method: req.Method, URL: &urlCopy, Body: body})
|
||||
var response Response
|
||||
queued := false
|
||||
if len(d.responses) > 0 {
|
||||
response, d.responses = d.responses[0], d.responses[1:]
|
||||
queued = true
|
||||
}
|
||||
d.mu.Unlock()
|
||||
if response.Err != nil {
|
||||
return nil, response.Err
|
||||
}
|
||||
if !queued {
|
||||
return nil, errors.New("conformance HTTPDouble: no queued response")
|
||||
}
|
||||
if response.Status == 0 {
|
||||
response.Status = http.StatusOK
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: response.Status,
|
||||
Status: fmt.Sprintf("%d", response.Status),
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(response.Body)),
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
202
internal/tracker/conformance/run.go
Normal file
202
internal/tracker/conformance/run.go
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
package conformance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// Snapshot is the normalized local state inspected by the shared suite.
|
||||
type Snapshot struct {
|
||||
Issues map[string]types.Issue
|
||||
Config map[string]string
|
||||
Metadata map[string]string
|
||||
LastSync string
|
||||
}
|
||||
|
||||
// Fixture contains deterministic HTTP and local-store dependencies for one
|
||||
// adapter test. StoreFactory counts opens so API-only operations can prove
|
||||
// that they do not initialize local persistence.
|
||||
type Fixture struct {
|
||||
HTTP *HTTPDouble
|
||||
StoreFactory *Factory
|
||||
}
|
||||
|
||||
// Factory is a fake UOW factory. Each Open returns the same map-backed store,
|
||||
// while OpenCount records initialization attempts.
|
||||
type Factory struct {
|
||||
Store *Store
|
||||
opens int
|
||||
}
|
||||
|
||||
// NewFactory returns a fixture factory with an empty store.
|
||||
func NewFactory() *Factory { return &Factory{Store: NewStore()} }
|
||||
|
||||
// Open returns the tracker store and records one UOW open.
|
||||
func (f *Factory) Open() tracker.Store { f.opens++; return f.Store }
|
||||
|
||||
// OpenCount reports the number of UOW opens.
|
||||
func (f *Factory) OpenCount() int { return f.opens }
|
||||
|
||||
// Setup binds an existing tracker.Engine to fixture assertions. Refusal and
|
||||
// APIOnly are adapter-specific front doors; keeping them callbacks avoids
|
||||
// inventing a backend-neutral command API.
|
||||
type Setup struct {
|
||||
Engine *tracker.Engine
|
||||
Store *Store
|
||||
Expected Expected
|
||||
Refusal func(context.Context) (*tracker.SyncResult, error)
|
||||
APIOnly func(context.Context, func() tracker.Store) error
|
||||
}
|
||||
|
||||
// Expected identifies adapter-specific values that the generic suite must
|
||||
// observe without baking a provider's naming conventions into the harness.
|
||||
type Expected struct {
|
||||
ExternalRef string
|
||||
ConfigKey string
|
||||
MetadataKey string
|
||||
}
|
||||
|
||||
// Run executes the common tracker Engine/UOW contract.
|
||||
func Run(t *testing.T, build func(*testing.T, *Fixture) Setup) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
newSetup := func(t *testing.T) Setup {
|
||||
fixture := &Fixture{HTTP: NewHTTPDouble(), StoreFactory: NewFactory()}
|
||||
setup := build(t, fixture)
|
||||
if setup.Engine == nil || setup.Store == nil || setup.Refusal == nil || setup.APIOnly == nil || setup.Expected.ExternalRef == "" || setup.Expected.ConfigKey == "" || setup.Expected.MetadataKey == "" {
|
||||
t.Fatal("setup must provide Engine, Store, Expected refs/config/metadata, Refusal, and APIOnly")
|
||||
}
|
||||
return setup
|
||||
}
|
||||
|
||||
t.Run("pull_persists_normalized_fields_and_last_sync", func(t *testing.T) {
|
||||
s := newSetup(t)
|
||||
result, err := s.Engine.Sync(ctx, tracker.SyncOptions{Pull: true})
|
||||
if err != nil {
|
||||
t.Fatalf("pull: %v", err)
|
||||
}
|
||||
if result == nil || !result.Success {
|
||||
t.Fatalf("pull result = %+v", result)
|
||||
}
|
||||
snapshot := s.Store.Snapshot()
|
||||
if len(snapshot.Issues) == 0 {
|
||||
t.Fatal("pull created no local issue")
|
||||
}
|
||||
for _, issue := range snapshot.Issues {
|
||||
if issue.ExternalRef == nil || *issue.ExternalRef != s.Expected.ExternalRef || len(issue.Labels) == 0 {
|
||||
t.Fatalf("pull lost external ref/labels: %+v", issue)
|
||||
}
|
||||
}
|
||||
if result.LastSync == "" || snapshot.LastSync != result.LastSync {
|
||||
t.Fatalf("pull last_sync result=%q snapshot=%q", result.LastSync, snapshot.LastSync)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("push_persists_config_and_metadata", func(t *testing.T) {
|
||||
s := newSetup(t)
|
||||
result, err := s.Engine.Sync(ctx, tracker.SyncOptions{Push: true})
|
||||
if err != nil {
|
||||
t.Fatalf("push: %v", err)
|
||||
}
|
||||
if result == nil || !result.Success {
|
||||
t.Fatalf("push result = %+v", result)
|
||||
}
|
||||
snapshot := s.Store.Snapshot()
|
||||
if snapshot.Config[s.Expected.ConfigKey] == "" || snapshot.Metadata[s.Expected.MetadataKey] == "" {
|
||||
t.Fatalf("push did not persist config/metadata: %+v", snapshot)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dry_run_does_not_mutate", func(t *testing.T) {
|
||||
s := newSetup(t)
|
||||
before, writes := s.Store.Snapshot(), s.Store.MutationCount()
|
||||
for name, opts := range map[string]tracker.SyncOptions{"pull": {Pull: true, DryRun: true}, "push": {Push: true, DryRun: true}} {
|
||||
result, err := s.Engine.Sync(ctx, opts)
|
||||
if err != nil {
|
||||
t.Fatalf("%s dry-run: %v", name, err)
|
||||
}
|
||||
if result == nil || !result.Success {
|
||||
t.Fatalf("%s dry-run result = %+v", name, result)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(before, s.Store.Snapshot()) || writes != s.Store.MutationCount() {
|
||||
t.Fatal("dry-run changed local state")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("refusal_is_explicit_and_does_not_mutate", func(t *testing.T) {
|
||||
s := newSetup(t)
|
||||
before, writes := s.Store.Snapshot(), s.Store.MutationCount()
|
||||
_, err := s.Refusal(ctx)
|
||||
if err == nil {
|
||||
t.Fatal("refused operation succeeded")
|
||||
}
|
||||
var refusal *storage.ErrUnsupported
|
||||
if !errors.As(err, &refusal) {
|
||||
t.Fatalf("refusal is not typed: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(before, s.Store.Snapshot()) || writes != s.Store.MutationCount() {
|
||||
t.Fatal("refusal changed local state")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("external_ref_resolution_prefers_issue_plane", func(t *testing.T) {
|
||||
s := newSetup(t)
|
||||
// The setup seeded Expected.ExternalRef on the issues plane. Put the
|
||||
// SAME ref on the wisps plane — the state a push-then-pull produces
|
||||
// once a wisp has been pushed under a ref the durable bead also
|
||||
// carries — and give the wisp the ID that sorts first, so a resolver
|
||||
// with no plane preference returns it.
|
||||
var want string
|
||||
for id, issue := range s.Store.Issues {
|
||||
if issue.ExternalRef != nil && *issue.ExternalRef == s.Expected.ExternalRef {
|
||||
want = id
|
||||
break
|
||||
}
|
||||
}
|
||||
if want == "" {
|
||||
t.Fatalf("setup seeded no issues-plane row for %q", s.Expected.ExternalRef)
|
||||
}
|
||||
ref := s.Expected.ExternalRef
|
||||
wispID := "aaa-wisp-" + want
|
||||
s.Store.Wisps[wispID] = &types.Issue{ID: wispID, Title: "pushed wisp", Status: types.StatusOpen, IssueType: types.TypeTask, Priority: 2, Ephemeral: true, ExternalRef: &ref}
|
||||
|
||||
got, err := s.Store.GetIssueByExternalRef(ctx, ref)
|
||||
if err != nil {
|
||||
t.Fatalf("resolve %q: %v", ref, err)
|
||||
}
|
||||
// Resolving to the wisp would make the pull dedup update the ephemeral
|
||||
// row instead of the durable bead — a silent write to the wrong issue.
|
||||
if got == nil || got.ID != want {
|
||||
t.Fatalf("external_ref %q resolved to %v, want issues-plane %q: the issues plane must win over the wisp plane", ref, got, want)
|
||||
}
|
||||
|
||||
// And the wisp plane is still reachable when the issues plane has no
|
||||
// match, so plane preference did not become plane exclusion.
|
||||
otherRef := ref + "-wisp-only"
|
||||
onlyID := "wisp-only"
|
||||
s.Store.Wisps[onlyID] = &types.Issue{ID: onlyID, Title: "wisp only", Status: types.StatusOpen, IssueType: types.TypeTask, Priority: 2, Ephemeral: true, ExternalRef: &otherRef}
|
||||
got, err = s.Store.GetIssueByExternalRef(ctx, otherRef)
|
||||
if err != nil || got == nil || got.ID != onlyID {
|
||||
t.Fatalf("wisp-only external_ref %q resolved to (%v, %v), want %q", otherRef, got, err, onlyID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("api_only_does_not_open_uow", func(t *testing.T) {
|
||||
fixture := &Fixture{HTTP: NewHTTPDouble(), StoreFactory: NewFactory()}
|
||||
s := build(t, fixture)
|
||||
before := fixture.StoreFactory.OpenCount()
|
||||
if err := s.APIOnly(ctx, fixture.StoreFactory.Open); err != nil {
|
||||
t.Fatalf("api-only: %v", err)
|
||||
}
|
||||
if got := fixture.StoreFactory.OpenCount(); got != before {
|
||||
t.Fatalf("api-only opened UOW: %d -> %d", before, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
213
internal/tracker/conformance/store.go
Normal file
213
internal/tracker/conformance/store.go
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
package conformance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/tracker"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// Store is a small tracker.Store implementation backed by maps. It is useful
|
||||
// for adapter contract tests that need to observe local mutations without
|
||||
// opening a real Dolt server.
|
||||
type Store struct {
|
||||
mu sync.Mutex
|
||||
Issues map[string]*types.Issue
|
||||
Config map[string]string
|
||||
Metadata map[string]string
|
||||
|
||||
// Wisps is the second storage plane. It exists so the shared suite can
|
||||
// state the external_ref resolution contract — issues win over wisps —
|
||||
// which is unrepresentable in a single-plane fixture and which a merged
|
||||
// search silently gets wrong. Empty unless a test seeds it, so every
|
||||
// other assertion is unaffected.
|
||||
Wisps map[string]*types.Issue
|
||||
|
||||
LastSync string
|
||||
Mutations int
|
||||
}
|
||||
|
||||
// Snapshot returns a copy of the normalized state.
|
||||
func (s *Store) Snapshot() Snapshot {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := Snapshot{Issues: make(map[string]types.Issue, len(s.Issues)), Config: cloneMap(s.Config), Metadata: cloneMap(s.Metadata), LastSync: s.LastSync}
|
||||
for id, issue := range s.Issues {
|
||||
out.Issues[id] = *cloneIssue(issue)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// MutationCount reports committed local writes.
|
||||
func (s *Store) MutationCount() int { s.mu.Lock(); defer s.mu.Unlock(); return s.Mutations }
|
||||
|
||||
var _ tracker.Store = (*Store)(nil)
|
||||
|
||||
// ApplyIssueUpdate applies an issue update and replaces labels atomically.
|
||||
func (s *Store) ApplyIssueUpdate(ctx context.Context, id string, updates map[string]interface{}, labels []string, actor string) error {
|
||||
if err := s.UpdateIssue(ctx, id, updates, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if issue := s.Issues[id]; issue != nil && labels != nil {
|
||||
issue.Labels = append([]string(nil), labels...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewStore returns an empty fake tracker store.
|
||||
func NewStore() *Store {
|
||||
return &Store{Issues: map[string]*types.Issue{}, Wisps: map[string]*types.Issue{}, Config: map[string]string{}, Metadata: map[string]string{}}
|
||||
}
|
||||
|
||||
// Open returns the store as a unit-of-work target. The call is intentionally
|
||||
// side-effect free; callers count UOW opens in their factory when needed.
|
||||
func (s *Store) Open() tracker.Store { return s }
|
||||
|
||||
// GetConfig reads one config value.
|
||||
func (s *Store) GetConfig(_ context.Context, key string) (string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.Config[key], nil
|
||||
}
|
||||
|
||||
// GetAllConfig returns a copy of config.
|
||||
func (s *Store) GetAllConfig(context.Context) (map[string]string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return cloneMap(s.Config), nil
|
||||
}
|
||||
|
||||
// GetLocalMetadata reads one local metadata value.
|
||||
func (s *Store) GetLocalMetadata(_ context.Context, key string) (string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.Metadata[key], nil
|
||||
}
|
||||
|
||||
// SetLocalMetadata stores one local metadata value.
|
||||
func (s *Store) SetLocalMetadata(_ context.Context, key, value string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.Metadata[key] = value
|
||||
if strings.HasSuffix(key, ".last_sync") {
|
||||
s.LastSync = value
|
||||
}
|
||||
s.Mutations++
|
||||
return nil
|
||||
}
|
||||
|
||||
// SearchIssues returns a copy of all local issues.
|
||||
func (s *Store) SearchIssues(_ context.Context, _ string, _ types.IssueFilter) ([]*types.Issue, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := make([]*types.Issue, 0, len(s.Issues))
|
||||
for _, issue := range s.Issues {
|
||||
out = append(out, cloneIssue(issue))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetIssueByExternalRef finds one local issue by external reference, issues
|
||||
// plane first and wisps only as a fallback. The order is the contract, not an
|
||||
// implementation detail: the direct backend resolves `issues` before `wisps`
|
||||
// so pull dedup updates the durable bead rather than a pushed ephemeral one,
|
||||
// and Run asserts it (see external_ref_resolution_prefers_issue_plane).
|
||||
func (s *Store) GetIssueByExternalRef(_ context.Context, ref string) (*types.Issue, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, plane := range []map[string]*types.Issue{s.Issues, s.Wisps} {
|
||||
if issue := matchExternalRef(plane, ref); issue != nil {
|
||||
return cloneIssue(issue), nil
|
||||
}
|
||||
}
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
|
||||
// matchExternalRef returns the lowest-ID row in one plane carrying ref, or nil.
|
||||
// Go map iteration is randomized, so picking by ID keeps a plane with several
|
||||
// matching rows from making the harness flaky.
|
||||
func matchExternalRef(plane map[string]*types.Issue, ref string) *types.Issue {
|
||||
var found *types.Issue
|
||||
for _, issue := range plane {
|
||||
if issue.ExternalRef == nil || *issue.ExternalRef != ref {
|
||||
continue
|
||||
}
|
||||
if found == nil || issue.ID < found.ID {
|
||||
found = issue
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
// GetDependentsWithMetadata returns no dependents in the minimal fixture.
|
||||
func (s *Store) GetDependentsWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetDependenciesWithMetadata returns no dependencies in the minimal fixture.
|
||||
func (s *Store) GetDependenciesWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// CreateIssue inserts one local issue.
|
||||
func (s *Store) CreateIssue(_ context.Context, issue *types.Issue, _ string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.Issues[issue.ID] = cloneIssue(issue)
|
||||
s.Mutations++
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateIssue applies the common tracker fields.
|
||||
func (s *Store) UpdateIssue(_ context.Context, id string, updates map[string]interface{}, _ string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
issue := s.Issues[id]
|
||||
if issue == nil {
|
||||
return storage.ErrNotFound
|
||||
}
|
||||
if v, ok := updates["title"].(string); ok {
|
||||
issue.Title = v
|
||||
}
|
||||
if v, ok := updates["description"].(string); ok {
|
||||
issue.Description = v
|
||||
}
|
||||
if v, ok := updates["external_ref"].(string); ok {
|
||||
issue.ExternalRef = &v
|
||||
}
|
||||
s.Mutations++
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddDependency is a no-op in the minimal fixture.
|
||||
func (s *Store) AddDependency(context.Context, *types.Dependency, string) error {
|
||||
s.mu.Lock()
|
||||
s.Mutations++
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func cloneMap(in map[string]string) map[string]string {
|
||||
out := make(map[string]string, len(in))
|
||||
for k, v := range in {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
func cloneIssue(in *types.Issue) *types.Issue {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := *in
|
||||
out.Labels = append([]string(nil), in.Labels...)
|
||||
if in.ExternalRef != nil {
|
||||
ref := *in.ExternalRef
|
||||
out.ExternalRef = &ref
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
|
@ -114,7 +114,7 @@ type PushHooks struct {
|
|||
// integrations follow, eliminating duplication between Linear, GitLab, etc.
|
||||
type Engine struct {
|
||||
Tracker IssueTracker
|
||||
Store lifecycleStorage
|
||||
Store Store
|
||||
Actor string
|
||||
PullHooks *PullHooks
|
||||
PushHooks *PushHooks
|
||||
|
|
@ -131,22 +131,25 @@ type Engine struct {
|
|||
warnings []string
|
||||
}
|
||||
|
||||
type lifecycleStorage interface {
|
||||
storage.Storage
|
||||
storage.IssueLifecycleStore
|
||||
}
|
||||
|
||||
// NewEngine creates a new sync engine for the given tracker and storage.
|
||||
func NewEngine(tracker IssueTracker, store lifecycleStorage, actor string) *Engine {
|
||||
//
|
||||
// store is typed rather than interface{} so the seam names its contract: a
|
||||
// storage.Storage satisfies Store and is adapted here, while a caller that
|
||||
// already adapted its store (trackerStoreForCommand) passes the adapter
|
||||
// straight through. NewStore is idempotent, so adapting twice is a no-op.
|
||||
func NewEngine(tracker IssueTracker, store Store, actor string) *Engine {
|
||||
return &Engine{
|
||||
Tracker: tracker,
|
||||
Store: store,
|
||||
Store: NewStore(store),
|
||||
Actor: actor,
|
||||
}
|
||||
}
|
||||
|
||||
// Sync performs a complete synchronization operation based on the given options.
|
||||
func (e *Engine) Sync(ctx context.Context, opts SyncOptions) (*SyncResult, error) {
|
||||
if e == nil || e.Store == nil {
|
||||
return nil, fmt.Errorf("tracker sync store is not initialized")
|
||||
}
|
||||
ctx, span := syncTracer.Start(ctx, "tracker.sync",
|
||||
trace.WithAttributes(
|
||||
attribute.String("sync.tracker", e.Tracker.DisplayName()),
|
||||
|
|
@ -528,9 +531,14 @@ func (e *Engine) doPull(ctx context.Context, opts SyncOptions, allowOverwriteIDs
|
|||
updates["metadata"] = raw
|
||||
}
|
||||
|
||||
if err := e.Store.RunInIssueLifecycleTransaction(ctx, fmt.Sprintf("bd: pull update %s", existing.ID), func(tx storage.IssueLifecycleTransaction) error {
|
||||
return applyPullIssueUpdate(ctx, tx, existing.ID, updates, conv.Issue.Labels, e.Actor)
|
||||
}); err != nil {
|
||||
markPullIssueFields(updates)
|
||||
updater, ok := e.Store.(IssueUpdater)
|
||||
if !ok {
|
||||
e.warn("tracker store does not support atomic issue updates")
|
||||
stats.Errors++
|
||||
continue
|
||||
}
|
||||
if err := updater.ApplyIssueUpdate(ctx, existing.ID, updates, conv.Issue.Labels, e.Actor); err != nil {
|
||||
e.warn("Failed to update %s: %v", existing.ID, err)
|
||||
stats.Errors++
|
||||
if pulledIDs != nil {
|
||||
|
|
@ -576,26 +584,15 @@ func (e *Engine) doPull(ctx context.Context, opts SyncOptions, allowOverwriteIDs
|
|||
return stats, nil
|
||||
}
|
||||
|
||||
// applyPullIssueUpdate keeps a pulled update atomic with its labels.
|
||||
func applyPullIssueUpdate(ctx context.Context, tx storage.IssueLifecycleTransaction, id string, updates map[string]interface{}, labels []string, actor string) error {
|
||||
if err := applyPullIssueFields(ctx, tx, id, updates, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
return syncIssueLabels(ctx, tx, id, labels, actor)
|
||||
func applyPullIssueFields(ctx context.Context, tx storage.IssueLifecycleTransaction, id string, updates map[string]interface{}, actor string) error {
|
||||
markPullIssueFields(updates)
|
||||
return tx.UpdateIssue(ctx, id, updates, actor)
|
||||
}
|
||||
|
||||
// applyPullIssueFields applies a pulled issue's fields while preserving the
|
||||
// caller's control over related collections such as labels.
|
||||
//
|
||||
// A pull always forces close policy. The remote tracker is authoritative for
|
||||
// the status it reports, and it knows nothing about local-only children or
|
||||
// local-only blockers — refusing an upstream close because of them would wedge
|
||||
// sync on state the remote cannot see and the operator did not create. Both the
|
||||
// pull and the conflict reimport route through here, so this is the one place
|
||||
// that decision lives.
|
||||
func applyPullIssueFields(ctx context.Context, tx storage.IssueLifecycleTransaction, id string, updates map[string]interface{}, actor string) error {
|
||||
// markPullIssueFields adds the external-authority close policy marker shared
|
||||
// by direct and proxied store adapters.
|
||||
func markPullIssueFields(updates map[string]interface{}) {
|
||||
updates[issueops.OpForceClosePolicy] = true
|
||||
return tx.UpdateIssue(ctx, id, updates, actor)
|
||||
}
|
||||
|
||||
func pullIssueEqual(local *types.Issue, remote *types.Issue, ref string) bool {
|
||||
|
|
@ -778,7 +775,21 @@ func (e *Engine) externalRefChangedAfter(ctx context.Context, local *types.Issue
|
|||
// never see this optional capability even when the concrete store underneath
|
||||
// implements it — the same reason cmd/bd type-asserts through
|
||||
// storage.UnwrapStore for RawDBAccessor, StoreLocator, and friends.
|
||||
func externalRefHistoryQuerier(store storage.Storage) (storage.ExternalRefHistoryQuerier, bool) {
|
||||
func externalRefHistoryQuerier(store Store) (ExternalRefHistoryStore, bool) {
|
||||
if q, ok := store.(ExternalRefHistoryStore); ok {
|
||||
return q, true
|
||||
}
|
||||
if direct, ok := store.(*directStore); ok {
|
||||
if q, ok := direct.Storage.(ExternalRefHistoryStore); ok {
|
||||
return q, true
|
||||
}
|
||||
if dolt, ok := direct.Storage.(storage.DoltStorage); ok {
|
||||
if q, ok := storage.UnwrapStore(dolt).(ExternalRefHistoryStore); ok {
|
||||
return q, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
if q, ok := store.(storage.ExternalRefHistoryQuerier); ok {
|
||||
return q, true
|
||||
}
|
||||
|
|
@ -1303,9 +1314,13 @@ func (e *Engine) reimportIssue(ctx context.Context, c Conflict) {
|
|||
}
|
||||
}
|
||||
|
||||
if err := e.Store.RunInIssueLifecycleTransaction(ctx, fmt.Sprintf("bd: reimport update %s", c.IssueID), func(tx storage.IssueLifecycleTransaction) error {
|
||||
return applyPullIssueFields(ctx, tx, c.IssueID, updates, e.Actor)
|
||||
}); err != nil {
|
||||
markPullIssueFields(updates)
|
||||
updater, ok := e.Store.(IssueUpdater)
|
||||
if !ok {
|
||||
e.warn("tracker store does not support atomic issue updates")
|
||||
return
|
||||
}
|
||||
if err := updater.ApplyIssueUpdate(ctx, c.IssueID, updates, nil, e.Actor); err != nil {
|
||||
e.warn("Failed to update %s during reimport: %v", c.IssueID, err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1475,7 +1490,7 @@ func (e *Engine) dependencyIssueResolver(ctx context.Context, extraIssues []*typ
|
|||
}, nil
|
||||
}
|
||||
|
||||
func dependencyExists(ctx context.Context, store storage.Storage, issueID, dependsOnID string, depType types.DependencyType) bool {
|
||||
func dependencyExists(ctx context.Context, store Store, issueID, dependsOnID string, depType types.DependencyType) bool {
|
||||
if strings.TrimSpace(issueID) == "" || strings.TrimSpace(dependsOnID) == "" {
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
103
internal/tracker/store.go
Normal file
103
internal/tracker/store.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// Store is the persistence contract required by the tracker synchronization
|
||||
// engine. It deliberately names only tracker operations; direct stores and
|
||||
// unit-of-work providers can each adapt to this seam without pretending to
|
||||
// implement the other backend's full storage surface.
|
||||
type Store interface {
|
||||
GetConfig(context.Context, string) (string, error)
|
||||
GetAllConfig(context.Context) (map[string]string, error)
|
||||
GetLocalMetadata(context.Context, string) (string, error)
|
||||
SetLocalMetadata(context.Context, string, string) error
|
||||
SearchIssues(context.Context, string, types.IssueFilter) ([]*types.Issue, error)
|
||||
GetIssueByExternalRef(context.Context, string) (*types.Issue, error)
|
||||
GetDependentsWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error)
|
||||
GetDependenciesWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error)
|
||||
CreateIssue(context.Context, *types.Issue, string) error
|
||||
UpdateIssue(context.Context, string, map[string]interface{}, string) error
|
||||
AddDependency(context.Context, *types.Dependency, string) error
|
||||
}
|
||||
|
||||
// IssueUpdater is the optional atomic lifecycle-plus-label update capability.
|
||||
type IssueUpdater interface {
|
||||
ApplyIssueUpdate(context.Context, string, map[string]interface{}, []string, string) error
|
||||
}
|
||||
|
||||
// ExternalRefHistoryStore is an optional capability used for precise
|
||||
// relink/conflict detection.
|
||||
type ExternalRefHistoryStore interface {
|
||||
PreviousExternalRef(context.Context, string, time.Time) (string, bool, error)
|
||||
}
|
||||
|
||||
// NewStore adapts a classic storage.Storage to the tracker contract. Values
|
||||
// already implementing Store are returned unchanged, so NewStore is idempotent
|
||||
// and safe to call on a value that a caller has already adapted.
|
||||
//
|
||||
// Branch order is load-bearing, and neither interface alone can discriminate:
|
||||
//
|
||||
// - storage.Storage is a strict superset of Store, so a raw direct store also
|
||||
// satisfies Store. Testing Store first would return it un-adapted and drop
|
||||
// ApplyIssueUpdate entirely.
|
||||
// - directStore embeds the storage.Storage interface, so *directStore
|
||||
// satisfies storage.Storage too. Testing storage.Storage first re-wraps an
|
||||
// already-adapted value as &directStore{Storage: &directStore{...}}, and
|
||||
// the inner *directStore can never satisfy storage.IssueLifecycleStore
|
||||
// (RunInIssueLifecycleTransaction is not part of storage.Storage). That
|
||||
// silently degrades ApplyIssueUpdate to UpdateIssue — no labels, no
|
||||
// lifecycle transaction.
|
||||
//
|
||||
// The tracker's own adapters are therefore matched by concrete type first: no
|
||||
// storage backend can accidentally satisfy that check.
|
||||
func NewStore(value interface{}) Store {
|
||||
switch store := value.(type) {
|
||||
case *directStore:
|
||||
return store
|
||||
case *uowStore:
|
||||
return store
|
||||
}
|
||||
if store, ok := value.(storage.Storage); ok {
|
||||
return &directStore{Storage: store}
|
||||
}
|
||||
if store, ok := value.(Store); ok {
|
||||
return store
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type directStore struct{ storage.Storage }
|
||||
|
||||
var _ Store = (*directStore)(nil)
|
||||
var _ IssueUpdater = (*directStore)(nil)
|
||||
|
||||
func (s *directStore) ApplyIssueUpdate(ctx context.Context, id string, updates map[string]interface{}, labels []string, actor string) error {
|
||||
dolt, ok := s.Storage.(storage.IssueLifecycleStore)
|
||||
if !ok {
|
||||
return s.Storage.UpdateIssue(ctx, id, updates, actor)
|
||||
}
|
||||
return dolt.RunInIssueLifecycleTransaction(ctx, "bd: tracker update "+id, func(tx storage.IssueLifecycleTransaction) error {
|
||||
if err := tx.UpdateIssue(ctx, id, updates, actor); err != nil {
|
||||
return err
|
||||
}
|
||||
if labels == nil {
|
||||
return nil
|
||||
}
|
||||
// syncIssueLabels, not a local copy: it normalizes both label sets
|
||||
// (trim + drop empty), which is the semantics the pull path had before
|
||||
// this seam moved behind ApplyIssueUpdate. Comparing raw strings makes
|
||||
// a whitespace-only difference churn remove+add on every sync and lets
|
||||
// an empty label reach AddLabel.
|
||||
return syncIssueLabels(ctx, tx, id, labels, actor)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *directStore) GetDependenciesWithMetadata(ctx context.Context, id string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return s.Storage.GetDependenciesWithMetadata(ctx, id)
|
||||
}
|
||||
206
internal/tracker/store_test.go
Normal file
206
internal/tracker/store_test.go
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
type contractStoreStub struct{}
|
||||
|
||||
func (contractStoreStub) GetConfig(context.Context, string) (string, error) { return "", nil }
|
||||
func (contractStoreStub) GetAllConfig(context.Context) (map[string]string, error) { return nil, nil }
|
||||
func (contractStoreStub) GetLocalMetadata(context.Context, string) (string, error) { return "", nil }
|
||||
func (contractStoreStub) SetLocalMetadata(context.Context, string, string) error { return nil }
|
||||
func (contractStoreStub) SearchIssues(context.Context, string, types.IssueFilter) ([]*types.Issue, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (contractStoreStub) GetIssueByExternalRef(context.Context, string) (*types.Issue, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (contractStoreStub) GetDependentsWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (contractStoreStub) GetDependenciesWithMetadata(context.Context, string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (contractStoreStub) CreateIssue(context.Context, *types.Issue, string) error { return nil }
|
||||
func (contractStoreStub) UpdateIssue(context.Context, string, map[string]interface{}, string) error {
|
||||
return nil
|
||||
}
|
||||
func (contractStoreStub) AddDependency(context.Context, *types.Dependency, string) error { return nil }
|
||||
|
||||
func TestNewStoreNilIsSafe(t *testing.T) {
|
||||
if got := NewStore(nil); got != nil {
|
||||
t.Fatalf("NewStore(nil) = %T, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStorePreservesNarrowStore(t *testing.T) {
|
||||
stub := contractStoreStub{}
|
||||
if got := NewStore(stub); got == nil {
|
||||
t.Fatal("NewStore returned nil for a Store implementation")
|
||||
}
|
||||
}
|
||||
|
||||
type failingUOWProvider struct{ err error }
|
||||
|
||||
func (p failingUOWProvider) NewUOW(context.Context) (uow.UnitOfWork, error) { return nil, p.err }
|
||||
func (p failingUOWProvider) Close(context.Context) error { return nil }
|
||||
|
||||
func TestUOWStorePropagatesProviderFailure(t *testing.T) {
|
||||
want := errors.New("provider unavailable")
|
||||
store := NewUOWStore(failingUOWProvider{err: want})
|
||||
_, err := store.GetConfig(context.Background(), "linear.team_id")
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("GetConfig error = %v, want %v", err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUOWStoreTypedNilIsSafe(t *testing.T) {
|
||||
var provider *failingUOWProvider
|
||||
if got := NewUOWStore(provider); got != nil {
|
||||
t.Fatalf("typed nil provider returned %T", got)
|
||||
}
|
||||
}
|
||||
|
||||
type lifecycleStoreFake struct {
|
||||
storage.Storage
|
||||
labels []string
|
||||
updateErr error
|
||||
// degraded records a direct UpdateIssue that bypassed the lifecycle
|
||||
// transaction. That is the label-dropping fallback ApplyIssueUpdate takes
|
||||
// when its inner value is not a storage.IssueLifecycleStore.
|
||||
degraded bool
|
||||
}
|
||||
|
||||
func (s *lifecycleStoreFake) RunInIssueLifecycleTransaction(ctx context.Context, _ string, fn func(storage.IssueLifecycleTransaction) error) error {
|
||||
return fn(&lifecycleTxFake{store: s})
|
||||
}
|
||||
|
||||
func (s *lifecycleStoreFake) UpdateIssue(context.Context, string, map[string]interface{}, string) error {
|
||||
s.degraded = true
|
||||
return s.updateErr
|
||||
}
|
||||
|
||||
type lifecycleTxFake struct {
|
||||
storage.Transaction
|
||||
store *lifecycleStoreFake
|
||||
}
|
||||
|
||||
func (tx *lifecycleTxFake) UpdateIssue(context.Context, string, map[string]interface{}, string) error {
|
||||
return tx.store.updateErr
|
||||
}
|
||||
func (tx *lifecycleTxFake) GetLabels(context.Context, string) ([]string, error) {
|
||||
return append([]string(nil), tx.store.labels...), nil
|
||||
}
|
||||
func (tx *lifecycleTxFake) AddLabel(_ context.Context, _, label, _ string) error {
|
||||
tx.store.labels = append(tx.store.labels, label)
|
||||
return nil
|
||||
}
|
||||
func (tx *lifecycleTxFake) RemoveLabel(_ context.Context, _, label, _ string) error {
|
||||
filtered := tx.store.labels[:0]
|
||||
for _, existing := range tx.store.labels {
|
||||
if existing != label {
|
||||
filtered = append(filtered, existing)
|
||||
}
|
||||
}
|
||||
tx.store.labels = filtered
|
||||
return nil
|
||||
}
|
||||
func (tx *lifecycleTxFake) ReopenIssueWithResult(context.Context, string, string, string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func TestDirectStoreApplyIssueUpdateDistinguishesOmittedAndEmptyLabels(t *testing.T) {
|
||||
underlying := &lifecycleStoreFake{labels: []string{"keep"}}
|
||||
store := NewStore(underlying).(*directStore)
|
||||
if err := store.ApplyIssueUpdate(context.Background(), "bd-1", nil, nil, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(underlying.labels) != 1 || underlying.labels[0] != "keep" {
|
||||
t.Fatalf("nil labels changed state: %v", underlying.labels)
|
||||
}
|
||||
if err := store.ApplyIssueUpdate(context.Background(), "bd-1", nil, []string{}, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(underlying.labels) != 0 {
|
||||
t.Fatalf("empty labels did not clear state: %v", underlying.labels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectStoreApplyIssueUpdateFailureDoesNotTouchLabels(t *testing.T) {
|
||||
underlying := &lifecycleStoreFake{labels: []string{"keep"}, updateErr: errors.New("update failed")}
|
||||
store := NewStore(underlying).(*directStore)
|
||||
if err := store.ApplyIssueUpdate(context.Background(), "bd-1", nil, []string{}, "actor"); !errors.Is(err, underlying.updateErr) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(underlying.labels) != 1 || underlying.labels[0] != "keep" {
|
||||
t.Fatalf("failed update changed labels: %v", underlying.labels)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewEngineKeepsDirectStoreAdaptationIntact pins the production wiring that
|
||||
// no other test covers: trackerStoreForCommand adapts the direct store itself
|
||||
// and hands the adapter to NewEngine, which adapts again. Re-wrapping produces
|
||||
// &directStore{Storage: &directStore{...}}, whose inner value can never satisfy
|
||||
// storage.IssueLifecycleStore, so ApplyIssueUpdate silently degrades to a
|
||||
// label-less, transaction-less UpdateIssue. The conformance harness cannot see
|
||||
// this: its store implements tracker.Store directly, which is the one shape
|
||||
// direct mode never produces.
|
||||
func TestNewEngineKeepsDirectStoreAdaptationIntact(t *testing.T) {
|
||||
underlying := &lifecycleStoreFake{labels: []string{"stale"}}
|
||||
adapted := NewStore(underlying) // what trackerStoreForCommand returns in direct mode
|
||||
|
||||
engine := NewEngine(nil, adapted, "actor")
|
||||
|
||||
if engine.Store != adapted {
|
||||
t.Fatalf("NewEngine re-adapted an already-adapted store: %T", engine.Store)
|
||||
}
|
||||
updater, ok := engine.Store.(IssueUpdater)
|
||||
if !ok {
|
||||
t.Fatalf("engine store %T lost the IssueUpdater capability", engine.Store)
|
||||
}
|
||||
if err := updater.ApplyIssueUpdate(context.Background(), "bd-1", nil, []string{"fresh"}, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if underlying.degraded {
|
||||
t.Fatal("ApplyIssueUpdate bypassed the lifecycle transaction")
|
||||
}
|
||||
if len(underlying.labels) != 1 || underlying.labels[0] != "fresh" {
|
||||
t.Fatalf("labels did not survive the pull: %v", underlying.labels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStoreIsIdempotent(t *testing.T) {
|
||||
direct := NewStore(&lifecycleStoreFake{})
|
||||
if got := NewStore(direct); got != direct {
|
||||
t.Fatalf("NewStore(directStore) = %T, want the same value", got)
|
||||
}
|
||||
uowBacked := NewUOWStore(failingUOWProvider{err: errors.New("unused")})
|
||||
if got := NewStore(uowBacked); got != uowBacked {
|
||||
t.Fatalf("NewStore(uowStore) = %T, want the same value", got)
|
||||
}
|
||||
stub := contractStoreStub{}
|
||||
if got := NewStore(NewStore(stub)); got != Store(stub) {
|
||||
t.Fatalf("NewStore(narrow Store) = %T, want the same value", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectStoreApplyIssueUpdateNormalizesLabels(t *testing.T) {
|
||||
underlying := &lifecycleStoreFake{labels: []string{"keep"}}
|
||||
store := NewStore(underlying).(*directStore)
|
||||
|
||||
// " keep " differs from "keep" only in whitespace and must not churn;
|
||||
// the empty label must be dropped rather than reaching AddLabel.
|
||||
if err := store.ApplyIssueUpdate(context.Background(), "bd-1", nil, []string{" keep ", ""}, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(underlying.labels) != 1 || underlying.labels[0] != "keep" {
|
||||
t.Fatalf("labels churned or admitted an empty value: %v", underlying.labels)
|
||||
}
|
||||
}
|
||||
|
|
@ -79,14 +79,14 @@ func (m *mockExternalRefTracker) BuildExternalRef(issue *TrackerIssue) string {
|
|||
return m.mockTracker.BuildExternalRef(issue)
|
||||
}
|
||||
|
||||
func (m *mockTracker) Name() string { return m.name }
|
||||
func (m *mockTracker) DisplayName() string { return m.name }
|
||||
func (m *mockTracker) ConfigPrefix() string { return m.name }
|
||||
func (m *mockTracker) Init(_ context.Context, _ storage.Storage) error { return nil }
|
||||
func (m *mockTracker) Validate() error { return nil }
|
||||
func (m *mockTracker) Close() error { return nil }
|
||||
func (m *mockTracker) FieldMapper() FieldMapper { return m.fieldMapper }
|
||||
func (m *mockTracker) IsExternalRef(ref string) bool { return len(ref) > 0 }
|
||||
func (m *mockTracker) Name() string { return m.name }
|
||||
func (m *mockTracker) DisplayName() string { return m.name }
|
||||
func (m *mockTracker) ConfigPrefix() string { return m.name }
|
||||
func (m *mockTracker) Init(_ context.Context, _ Store) error { return nil }
|
||||
func (m *mockTracker) Validate() error { return nil }
|
||||
func (m *mockTracker) Close() error { return nil }
|
||||
func (m *mockTracker) FieldMapper() FieldMapper { return m.fieldMapper }
|
||||
func (m *mockTracker) IsExternalRef(ref string) bool { return len(ref) > 0 }
|
||||
func (m *mockTracker) ExtractIdentifier(ref string) string {
|
||||
if i := strings.LastIndex(ref, "/"); i >= 0 {
|
||||
return ref[i+1:]
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package tracker
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -22,7 +21,7 @@ type IssueTracker interface {
|
|||
|
||||
// Init initializes the tracker with configuration from the beads config store.
|
||||
// Called once before any sync operations.
|
||||
Init(ctx context.Context, store storage.Storage) error
|
||||
Init(ctx context.Context, store Store) error
|
||||
|
||||
// Validate checks that the tracker is properly configured and can connect.
|
||||
Validate() error
|
||||
|
|
|
|||
174
internal/tracker/uow_store.go
Normal file
174
internal/tracker/uow_store.go
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// NewUOWStore adapts a proxied unit-of-work provider to the tracker Store
|
||||
// contract. Every operation uses a request-scoped unit of work; lifecycle
|
||||
// updates and label replacement remain one atomic transaction.
|
||||
func NewUOWStore(provider uow.UnitOfWorkProvider) Store {
|
||||
if provider == nil {
|
||||
return nil
|
||||
}
|
||||
v := reflect.ValueOf(provider)
|
||||
switch v.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
if v.IsNil() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if v.IsValid() == false {
|
||||
return nil
|
||||
}
|
||||
return &uowStore{provider: provider}
|
||||
}
|
||||
|
||||
type uowStore struct{ provider uow.UnitOfWorkProvider }
|
||||
|
||||
var _ Store = (*uowStore)(nil)
|
||||
var _ IssueUpdater = (*uowStore)(nil)
|
||||
|
||||
func (s *uowStore) GetConfig(ctx context.Context, key string) (string, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
|
||||
return uw.ConfigUseCase().GetConfig(ctx, key)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) GetAllConfig(ctx context.Context) (map[string]string, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (map[string]string, error) {
|
||||
return uw.ConfigUseCase().GetAllConfig(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) GetLocalMetadata(ctx context.Context, key string) (string, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
|
||||
return uw.ConfigUseCase().GetLocalMetadata(ctx, key)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) SetLocalMetadata(ctx context.Context, key, value string) error {
|
||||
_, err := uow.RunTxEphemeral(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (struct{}, error) {
|
||||
return struct{}{}, uw.ConfigUseCase().SetLocalMetadata(ctx, key, value)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *uowStore) SearchIssues(ctx context.Context, query string, filter types.IssueFilter) ([]*types.Issue, error) {
|
||||
page, err := uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (domain.SearchPage, error) {
|
||||
return uw.IssueUseCase().SearchIssues(ctx, query, filter)
|
||||
})
|
||||
return page.Items, err
|
||||
}
|
||||
|
||||
// GetIssueByExternalRef resolves the ISSUES PLANE FIRST and only then falls
|
||||
// back to the wisp plane, because that ordering is the direct backend's
|
||||
// deliberate contract, not an accident of its implementation:
|
||||
// issueops.GetIssueByExternalRefInTx queries `issues` and only falls through to
|
||||
// `wisps` on sql.ErrNoRows, and says why ("so that pushed ephemeral beads are
|
||||
// found during pull dedup").
|
||||
//
|
||||
// A single merged search cannot express that. SearchIssues with neither plane
|
||||
// flag set routes to searchUnion, a UNION ALL over the two legs ordered by
|
||||
// content and terminating in `id ASC` — no plane preference. So for an
|
||||
// external_ref present on BOTH planes, direct mode returns the issue and a
|
||||
// merged search returns whichever row happens to sort first. The consumer is
|
||||
// the pull dedup in engine.go, which then UPDATES the returned row with remote
|
||||
// content, making the divergence a silent write to the wrong local bead in
|
||||
// proxied mode only.
|
||||
//
|
||||
// Both plane reads share ONE unit of work. They must: uow.RunTxRead opens a
|
||||
// fresh UOW per call, so issuing them as two s.SearchIssues calls would split
|
||||
// one logical lookup across two read transactions, and the fallback's premise
|
||||
// ("the issues plane is already known empty") would no longer be
|
||||
// snapshot-guaranteed. A concurrent same-ref insert into the issues plane
|
||||
// landing between the two reads would let the merged fallback return the wisp,
|
||||
// re-creating exactly the wrong-row write this ordering exists to prevent.
|
||||
// The direct backend resolves both planes inside a single withReadTx, so the
|
||||
// shared snapshot is also the parity-preserving shape.
|
||||
func (s *uowStore) GetIssueByExternalRef(ctx context.Context, ref string) (*types.Issue, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (*types.Issue, error) {
|
||||
// Issues plane. SkipWisps selects the plane without contributing a WHERE
|
||||
// clause, so this is the exact analog of the direct backend's
|
||||
// `SELECT id FROM issues WHERE external_ref = ?`.
|
||||
page, err := uw.IssueUseCase().SearchIssues(ctx, "", types.IssueFilter{ExternalRef: &ref, Limit: 1, SkipWisps: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(page.Items) > 0 {
|
||||
return page.Items[0], nil
|
||||
}
|
||||
|
||||
// Wisp fallback, expressed as the merged search rather than Ephemeral=true.
|
||||
// Ephemeral is the only flag that routes to the wisps plane alone, but it
|
||||
// ALSO contributes `ephemeral = 1`, a predicate the direct backend's
|
||||
// `SELECT id FROM wisps WHERE external_ref = ?` does not have — it would
|
||||
// drop wisp-plane rows carrying ephemeral=0 (NoHistory beads, and typed
|
||||
// wisps minted without the flag; see types.IssueFilter.EphemeralTier),
|
||||
// trading this divergence for a narrower one. The merged search adds no
|
||||
// predicate, and its issues leg is already known empty from the search
|
||||
// above — in the same snapshot, now that both reads share this UOW — so a
|
||||
// hit here can only be a wisp, which is the fallback the direct backend
|
||||
// performs.
|
||||
page, err = uw.IssueUseCase().SearchIssues(ctx, "", types.IssueFilter{ExternalRef: &ref, Limit: 1})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(page.Items) == 0 {
|
||||
return nil, storage.ErrNotFound
|
||||
}
|
||||
return page.Items[0], nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) GetDependentsWithMetadata(ctx context.Context, id string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return uw.DependencyUseCase().ListWithIssueMetadata(ctx, id, domain.DepListFilter{Direction: domain.DepDirectionIn})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) GetDependenciesWithMetadata(ctx context.Context, id string) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return uow.RunTxRead(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return uw.DependencyUseCase().ListWithIssueMetadata(ctx, id, domain.DepListFilter{Direction: domain.DepDirectionOut})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) CreateIssue(ctx context.Context, issue *types.Issue, actor string) error {
|
||||
return uow.RunTx(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
|
||||
_, err := uw.IssueUseCase().CreateIssue(ctx, domain.CreateIssueParams{Issue: issue}, actor)
|
||||
return "bd: tracker create", err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error {
|
||||
return s.ApplyIssueUpdate(ctx, id, updates, nil, actor)
|
||||
}
|
||||
|
||||
func (s *uowStore) ApplyIssueUpdate(ctx context.Context, id string, updates map[string]interface{}, labels []string, actor string) error {
|
||||
fields := make(map[string]any, len(updates))
|
||||
for key, value := range updates {
|
||||
fields[key] = value
|
||||
}
|
||||
var setLabels *[]string
|
||||
if labels != nil {
|
||||
copy := append([]string(nil), labels...)
|
||||
setLabels = ©
|
||||
}
|
||||
return uow.RunTx(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
|
||||
_, err := uw.IssueUseCase().ApplyUpdate(ctx, id, domain.UpdateSpec{Fields: fields, SetLabels: setLabels}, actor)
|
||||
return "bd: tracker update", err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *uowStore) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error {
|
||||
return uow.RunTx(ctx, s.provider, func(ctx context.Context, uw uow.UnitOfWork) (string, error) {
|
||||
_, err := uw.DependencyUseCase().AddDependencies(ctx, []*types.Dependency{dep}, actor, domain.BulkAddDepsOpts{})
|
||||
return "bd: tracker dependency", err
|
||||
})
|
||||
}
|
||||
281
internal/tracker/uow_store_engine_test.go
Normal file
281
internal/tracker/uow_store_engine_test.go
Normal file
|
|
@ -0,0 +1,281 @@
|
|||
package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
type engineUOWState struct {
|
||||
issues map[string]*types.Issue
|
||||
wisps map[string]*types.Issue
|
||||
configs map[string]string
|
||||
commits int
|
||||
// uows counts units of work handed out by the provider, so a test can pin
|
||||
// how many read transactions one logical lookup is allowed to span.
|
||||
uows int
|
||||
}
|
||||
|
||||
type engineIssueUC struct {
|
||||
domain.IssueUseCase
|
||||
s *engineUOWState
|
||||
}
|
||||
|
||||
// searchPlane returns the rows in one plane matching filter, lowest ID first.
|
||||
// ephemeralOnly models the `ephemeral = 1` predicate that IssueFilter.Ephemeral
|
||||
// contributes on top of routing the search to the wisps plane.
|
||||
func searchPlane(plane map[string]*types.Issue, filter types.IssueFilter, ephemeralOnly bool) []*types.Issue {
|
||||
items := make([]*types.Issue, 0, len(plane))
|
||||
for _, issue := range plane {
|
||||
if filter.ExternalRef != nil && (issue.ExternalRef == nil || *issue.ExternalRef != *filter.ExternalRef) {
|
||||
continue
|
||||
}
|
||||
if ephemeralOnly && !issue.Ephemeral {
|
||||
continue
|
||||
}
|
||||
items = append(items, issue)
|
||||
}
|
||||
sort.Slice(items, func(i, j int) bool { return items[i].ID < items[j].ID })
|
||||
return items
|
||||
}
|
||||
|
||||
// SearchIssues models the repository's plane routing, which is the mechanism
|
||||
// GetIssueByExternalRef parity depends on: SkipWisps searches the issues plane
|
||||
// alone, Ephemeral routes to the wisps plane alone (and narrows it to
|
||||
// ephemeral=1 rows), and neither flag merges both with NO plane preference.
|
||||
//
|
||||
// The merged branch deliberately yields the wisp BEFORE the issue. The real
|
||||
// merged path is a UNION ALL ordered by content and terminating in `id ASC`, so
|
||||
// a wisp whose id sorts first is returned first — the adversarial ordering is
|
||||
// the whole reason the plane preference has to be explicit rather than implied.
|
||||
func (u *engineIssueUC) SearchIssues(_ context.Context, _ string, filter types.IssueFilter) (domain.SearchPage, error) {
|
||||
var items []*types.Issue
|
||||
switch {
|
||||
case filter.SkipWisps:
|
||||
items = searchPlane(u.s.issues, filter, false)
|
||||
case filter.Ephemeral != nil && *filter.Ephemeral:
|
||||
items = searchPlane(u.s.wisps, filter, true)
|
||||
default:
|
||||
items = append(searchPlane(u.s.wisps, filter, false), searchPlane(u.s.issues, filter, false)...)
|
||||
}
|
||||
if filter.Limit > 0 && len(items) > filter.Limit {
|
||||
items = items[:filter.Limit]
|
||||
}
|
||||
return domain.SearchPage{Items: items}, nil
|
||||
}
|
||||
func (u *engineIssueUC) GetIssue(_ context.Context, id string) (*types.Issue, error) {
|
||||
return u.s.issues[id], nil
|
||||
}
|
||||
func (u *engineIssueUC) CreateIssue(_ context.Context, p domain.CreateIssueParams, _ string) (domain.CreateIssueResult, error) {
|
||||
u.s.issues[p.Issue.ID] = p.Issue
|
||||
return domain.CreateIssueResult{Issue: p.Issue}, nil
|
||||
}
|
||||
func (u *engineIssueUC) UpdateIssue(_ context.Context, id string, fields map[string]any, _ string) error {
|
||||
if issue := u.s.issues[id]; issue != nil {
|
||||
if title, ok := fields["title"].(string); ok {
|
||||
issue.Title = title
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (u *engineIssueUC) ApplyUpdate(ctx context.Context, id string, spec domain.UpdateSpec, actor string) (*types.Issue, error) {
|
||||
if err := u.UpdateIssue(ctx, id, spec.Fields, actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return u.s.issues[id], nil
|
||||
}
|
||||
|
||||
type engineConfigUC struct {
|
||||
domain.ConfigUseCase
|
||||
s *engineUOWState
|
||||
}
|
||||
|
||||
func (u *engineConfigUC) GetConfig(_ context.Context, key string) (string, error) {
|
||||
return u.s.configs[key], nil
|
||||
}
|
||||
func (u *engineConfigUC) GetAllConfig(context.Context) (map[string]string, error) {
|
||||
return u.s.configs, nil
|
||||
}
|
||||
func (u *engineConfigUC) GetLocalMetadata(context.Context, string) (string, error) { return "", nil }
|
||||
func (u *engineConfigUC) SetLocalMetadata(context.Context, string, string) error { return nil }
|
||||
|
||||
type engineDepUC struct{ domain.DependencyUseCase }
|
||||
|
||||
func (engineDepUC) ListWithIssueMetadata(context.Context, string, domain.DepListFilter) ([]*types.IssueWithDependencyMetadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (engineDepUC) AddDependencies(context.Context, []*types.Dependency, string, domain.BulkAddDepsOpts) (domain.BulkAddDepsResult, error) {
|
||||
return domain.BulkAddDepsResult{}, nil
|
||||
}
|
||||
|
||||
type engineUOW struct{ state *engineUOWState }
|
||||
|
||||
func (u *engineUOW) Close(context.Context) {}
|
||||
func (u *engineUOW) Commit(context.Context, string) error { u.state.commits++; return nil }
|
||||
func (u *engineUOW) SwitchDatabase(context.Context, string) error { return nil }
|
||||
func (u *engineUOW) ConfigUseCase() domain.ConfigUseCase { return &engineConfigUC{s: u.state} }
|
||||
func (u *engineUOW) DoltRemoteUseCase() domain.DoltRemoteUseCase { return nil }
|
||||
func (u *engineUOW) IssueUseCase() domain.IssueUseCase { return &engineIssueUC{s: u.state} }
|
||||
func (u *engineUOW) DependencyUseCase() domain.DependencyUseCase { return engineDepUC{} }
|
||||
func (u *engineUOW) LabelUseCase() domain.LabelUseCase { return nil }
|
||||
func (u *engineUOW) CommentUseCase() domain.CommentUseCase { return nil }
|
||||
func (u *engineUOW) RawSQLUseCase() domain.RawSQLUseCase { return nil }
|
||||
func (u *engineUOW) EventsJournalUseCase() domain.EventsJournalUseCase { return nil }
|
||||
|
||||
type engineUOWProvider struct{ state *engineUOWState }
|
||||
|
||||
func (p *engineUOWProvider) NewUOW(context.Context) (uow.UnitOfWork, error) {
|
||||
p.state.uows++
|
||||
return &engineUOW{state: p.state}, nil
|
||||
}
|
||||
func (*engineUOWProvider) Close(context.Context) error { return nil }
|
||||
|
||||
func TestUOWStoreEngineMutationsCommitThroughProvider(t *testing.T) {
|
||||
state := &engineUOWState{issues: map[string]*types.Issue{}, configs: map[string]string{"issue_prefix": "bd"}}
|
||||
st := NewUOWStore(&engineUOWProvider{state: state})
|
||||
issue := &types.Issue{ID: "bd-new", Title: "new"}
|
||||
if err := st.CreateIssue(context.Background(), issue, "test"); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
if err := st.UpdateIssue(context.Background(), issue.ID, map[string]interface{}{"title": "updated"}, "test"); err != nil {
|
||||
t.Fatalf("update: %v", err)
|
||||
}
|
||||
got, err := st.GetIssueByExternalRef(context.Background(), "missing")
|
||||
if got != nil || err == nil {
|
||||
t.Fatalf("missing external ref: got=%v err=%v", got, err)
|
||||
}
|
||||
if state.issues[issue.ID].Title != "updated" || state.commits != 2 {
|
||||
t.Fatalf("state=%+v commits=%d", state.issues[issue.ID], state.commits)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUOWStoreGetIssueByExternalRefPrefersIssuePlane pins the proxied adapter to
|
||||
// the direct backend's resolution order. issueops.GetIssueByExternalRefInTx
|
||||
// queries `issues` and falls through to `wisps` only on no rows, so one
|
||||
// external_ref present on both planes must resolve to the ISSUE on both routes.
|
||||
// A merged search has no plane preference, and the consumer is the pull dedup,
|
||||
// which updates whatever row comes back — so a divergence here is a silent
|
||||
// write to the wrong local bead in proxied mode only.
|
||||
func TestUOWStoreGetIssueByExternalRefPrefersIssuePlane(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ref := "https://tracker.test/EXT-1"
|
||||
|
||||
// The wisp's ID sorts before the issue's, so the merged search returns it
|
||||
// first: resolving by "first merged hit" fails this test.
|
||||
issue := &types.Issue{ID: "bd-durable", Title: "durable", ExternalRef: &ref}
|
||||
wisp := &types.Issue{ID: "bd-aaa-wisp", Title: "pushed wisp", Ephemeral: true, ExternalRef: &ref}
|
||||
state := &engineUOWState{
|
||||
issues: map[string]*types.Issue{issue.ID: issue},
|
||||
wisps: map[string]*types.Issue{wisp.ID: wisp},
|
||||
configs: map[string]string{},
|
||||
}
|
||||
st := NewUOWStore(&engineUOWProvider{state: state})
|
||||
|
||||
got, err := st.GetIssueByExternalRef(ctx, ref)
|
||||
if err != nil {
|
||||
t.Fatalf("resolve collided external_ref: %v", err)
|
||||
}
|
||||
if got == nil || got.ID != issue.ID {
|
||||
t.Fatalf("external_ref on both planes resolved to %v, want issues-plane %q", got, issue.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUOWStoreGetIssueByExternalRefFallsBackToWispPlane is the other half of the
|
||||
// contract: preferring the issues plane must not stop resolving wisp-only refs,
|
||||
// which is the case the direct backend's fall-through exists to serve.
|
||||
//
|
||||
// The ephemeral=0 subtest is why the fallback is the merged search rather than
|
||||
// Ephemeral=true: that flag contributes an `ephemeral = 1` predicate the direct
|
||||
// backend's `SELECT id FROM wisps WHERE external_ref = ?` does not have, and
|
||||
// wisp-plane rows legitimately carry ephemeral=0 (NoHistory beads, and typed
|
||||
// wisps minted without the flag — see types.IssueFilter.EphemeralTier).
|
||||
func TestUOWStoreGetIssueByExternalRefFallsBackToWispPlane(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
ephemeral bool
|
||||
}{
|
||||
{name: "ephemeral_flag_set", ephemeral: true},
|
||||
{name: "ephemeral_flag_clear", ephemeral: false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ref := "https://tracker.test/EXT-WISP"
|
||||
wisp := &types.Issue{ID: "bd-wisp", Title: "wisp only", Ephemeral: tc.ephemeral, ExternalRef: &ref}
|
||||
state := &engineUOWState{
|
||||
issues: map[string]*types.Issue{},
|
||||
wisps: map[string]*types.Issue{wisp.ID: wisp},
|
||||
configs: map[string]string{},
|
||||
}
|
||||
st := NewUOWStore(&engineUOWProvider{state: state})
|
||||
|
||||
got, err := st.GetIssueByExternalRef(ctx, ref)
|
||||
if err != nil {
|
||||
t.Fatalf("resolve wisp-only external_ref: %v", err)
|
||||
}
|
||||
if got == nil || got.ID != wisp.ID {
|
||||
t.Fatalf("wisp-only external_ref resolved to %v, want %q", got, wisp.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUOWStoreGetIssueByExternalRefUsesOneReadTransaction pins both plane reads
|
||||
// to a SINGLE unit of work. uow.RunTxRead opens a fresh UOW per call, so
|
||||
// resolving through two s.SearchIssues calls would split one logical lookup
|
||||
// across two read transactions, and the wisp fallback's premise ("the issues
|
||||
// plane is already known empty") would stop being snapshot-guaranteed: a
|
||||
// concurrent same-ref insert into the issues plane landing between the reads
|
||||
// would let the merged fallback return the wisp, re-creating the wrong-row
|
||||
// write TestUOWStoreGetIssueByExternalRefPrefersIssuePlane exists to prevent.
|
||||
// The direct backend resolves both planes inside one withReadTx, so this is
|
||||
// also the parity-preserving shape.
|
||||
//
|
||||
// Only the paths that actually perform both reads can pin this, so the cases
|
||||
// are the wisp fallback and the miss; an issues-plane hit short-circuits after
|
||||
// one read and would pass either way.
|
||||
func TestUOWStoreGetIssueByExternalRefUsesOneReadTransaction(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
wispRef bool
|
||||
}{
|
||||
{name: "wisp_fallback", wispRef: true},
|
||||
{name: "no_match_on_either_plane", wispRef: false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ref := "https://tracker.test/EXT-SNAPSHOT"
|
||||
wisps := map[string]*types.Issue{}
|
||||
if tc.wispRef {
|
||||
wisps["bd-wisp"] = &types.Issue{ID: "bd-wisp", Title: "wisp only", Ephemeral: true, ExternalRef: &ref}
|
||||
}
|
||||
state := &engineUOWState{
|
||||
issues: map[string]*types.Issue{},
|
||||
wisps: wisps,
|
||||
configs: map[string]string{},
|
||||
}
|
||||
st := NewUOWStore(&engineUOWProvider{state: state})
|
||||
|
||||
got, err := st.GetIssueByExternalRef(ctx, ref)
|
||||
if tc.wispRef {
|
||||
if err != nil {
|
||||
t.Fatalf("resolve wisp-only external_ref: %v", err)
|
||||
}
|
||||
if got == nil || got.ID != "bd-wisp" {
|
||||
t.Fatalf("resolved to %v, want %q", got, "bd-wisp")
|
||||
}
|
||||
} else if got != nil || !errors.Is(err, storage.ErrNotFound) {
|
||||
t.Fatalf("unmatched external_ref: got=%v err=%v, want ErrNotFound", got, err)
|
||||
}
|
||||
|
||||
if state.uows != 1 {
|
||||
t.Fatalf("both plane reads must share one unit of work: opened %d, want 1", state.uows)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue