beads/internal/storage
Julian Knutsen dba70de281
fix(federation): actionable error when peer credentials cannot be decrypted (#6995)
* fix(embeddeddolt): use stored peer credentials for federation sync

bd federation add-peer promises stored credentials are used automatically
when syncing, but the embedded Fetch/PushTo/PullFrom paths read only the
DOLT_REMOTE_USER environment variable, so first sync against any
authenticated Dolt server presented the wrong identity. This is the
peer-path remainder of the class fixed for default remotes in #2543.

withPeerAuth mirrors server mode's withPeerCredentials: peer-store lookup
first, environment fallback for unknown remotes and credential-free peers
(federation sync iterates every dolt remote, so non-peer remotes must
keep their behavior). Stored credentials override the process-wide
DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair as a unit under a
federationEnvMutex, mirroring package dolt: the in-process Dolt engine
reads them from the process environment, so the override is serialized
and both variables are restored (or re-unset) afterward, including on
callback failure. A stored empty field unsets its variable rather than
inheriting an ambient value.

Fixes #5080

Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Michael Pursifull

* docs(embeddeddolt): clarify federationEnvMutex is separate from package dolt's lock

Review nit on #5085: the 'mirroring' phrasing read as if the two
packages shared one mutex. State explicitly that they are separate
locks and why per-package serialization suffices.

* fix(federation): actionable error when peer credentials cannot be decrypted

A database pulled to a second machine carries federation_peers rows
its local .beads-credential-key cannot decrypt, and every peer
operation died with a raw cipher error while bd federation status
blamed the network (Unreachable:). The failure semantics were right
(fail closed, matching server mode); the message said nothing about
the machine-local key or the fix.

Add storage.ErrCredentialKeyMismatch plus a shared wording helper
CredentialKeyMismatchError naming the key file as context and the
remediation (re-run bd federation add-peer on this machine). Both
backends wrap their decrypt failures with it: embeddeddolt at its
single decrypt funnel, dolt at its two read paths. GetFederationPeer
wraps now name the peer. bd federation status classifies the sentinel
with errors.Is and prints Local credential error instead of
Unreachable; the --json shape is unchanged and now pinned by test.

When the error fires is unchanged: same call sites, same fail-closed
semantics, no fallback to ambient credentials.

Tests pin the decrypt-failure branch in both backends (embeddeddolt
against a real store with a rotated key, dolt via sqlmock) and the
status classification end to end in the gated CLI harness.

Refs #5080

* fix(embeddeddolt): classify short ciphertext as credential-key mismatch

A stored peer password blob shorter than the GCM nonce returned a bare
'ciphertext too short' error, bypassing CredentialKeyMismatchError, so
federation status read that local corruption as an unreachable peer.
Route the branch through the same sentinel as a failed GCM open; the
remediation (re-add the peer on this machine) is identical.

Adds a short-ciphertext regression at the decrypt funnel.

Refs: gastownhall/beads#5214 (review, gap 1)

* fix(embeddeddolt): bind stored credentials to the remote URL; serialize env fallbacks

Two fixes to the shared withPeerAuth seam from the consolidation review:

- Stored peer credentials are now bound to the peer's canonical remote
  URL, not only to a matching remote name. AddRemoteIfNotExists keeps
  an existing same-name remote regardless of its URL, so a redirected
  remote could receive the stored password. withPeerAuth now verifies
  the live dolt_remotes URL against the peer row before any credential
  is installed, failing closed on divergence and on a missing remote.

- The plain-remote and empty-credential fallback callbacks now run
  under federationEnvMutex too. They previously called remoteAuthUser()
  and the operation itself outside the lock, so a fallback operation
  could observe a concurrent stored-peer operation's temporarily
  installed DOLT_REMOTE_USER/DOLT_REMOTE_PASSWORD pair.

Adds three regressions: diverged-URL fail-closed, missing-remote
fail-closed, and env-fallback-does-not-observe-peer-credentials.

Refs: gastownhall/beads#5085 (review)

* style(dolt): use US spelling in the decrypt-funnel comments

The repo's misspell linter rejects 'Labelling' and 'labelled'. Comment
text only; no behavior change.

Refs: gastownhall/beads#5214 (CI)

* review: name the resolved credential-key path in the mismatch error

Maintainer review fixup for PR #5214 (adopt-pr iteration 1). Addresses the one
minor and the one nit from the review synthesis; both reviewers found 0 blockers
and 0 majors and the quality scorecard is approve (898/850).

Minor (Debuggability & Operability). All three CredentialKeyMismatchError call
sites passed the bare const credentialKeyFile, so the message claimed "the key
file .beads-credential-key is machine-local" without saying where it is. On the
dolt backend that basename is genuinely ambiguous: initCredentialKey resolves it
against s.beadsDir and falls back to the legacy s.dbPath as a transparent
migration, so during the migration window two files with that name can exist
carrying different keys. Pass filepath.Join(s.beadsDir, credentialKeyFile)
instead, at dolt/credentials.go and both embeddeddolt/federation.go sites. No
signature change: the parameter was already named keyFile and its doc comment
already describes it as machine-local.

beadsDir is provably non-empty at all three sites in production, verified in
source rather than assumed: dolt only reaches the funnel with a non-nil key, and
initCredentialKey leaves the key nil when beadsDir is empty; embeddeddolt's
ensureCredentialKey errors out when it is empty.

This lengthens a message the author pre-declared as a judgment call. The
synthesis adjudicated that carve-out deliberately: it covers the message's
wording and its single-line form, not which file the text identifies.

Nit (Test Evidence Quality). dolt's withPeerCredentials is the seam that carries
the sentinel into `bd federation status` on the default backend and had no
fails-closed pin, unlike its embedded twin TestWithPeerAuth_KeyMismatchFailsClosed.
Added one subtest on the existing sqlmock harness asserting the callback does not
run and the sentinel survives the seam's own wrap.

Both pins were proven non-vacuous by mutation, not assumed:
- Reverting the three call sites to the bare const reds all three dolt subtests
  and all three embeddeddolt tests, with the original bare-basename text in the
  diff output.
- Making withPeerCredentials swallow the decrypt error reds only the new subtest,
  on exactly the two properties the nit named (callback ran; no failure).

The dolt test needed beadsDir set on its hand-built store: filepath.Join("",
credentialKeyFile) is the bare basename, so with beadsDir left empty the
resolved-path assertion would have passed vacuously. The embeddeddolt pin's
package-level const became a function of the store for the same reason — its
store has a real temp beadsDir, so the clause now pins the resolved path.

Validation: go vet rc=0 on all three packages; internal/storage, dolt
(3 tests incl. both negative/narrowing tests) and embeddeddolt (3 credential
tests) green; cmd/bd TestEmbeddedFederation 12/12 green including the PR's own
status_credential_key_mismatch e2e subtest. make bazel-sync-check rc=0 with zero
tracked drift — no new files and zero import changes, so no BUILD srcs or deps
can move.

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

---------

Co-authored-by: Michael Pursifull <mike@arcaven.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Eddie the Engineer <adopt-pr@gascity.com>
2026-09-30 20:29:57 -07:00
..
backendnames test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
backends test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
contextinfo test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
dberrors test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
dbproxy fix(dbproxy): retire the proxy when its Dolt backend exits cleanly (#6937) 2026-09-29 01:48:15 -07:00
depid test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
dolt fix(federation): actionable error when peer credentials cannot be decrypted (#6995) 2026-09-30 20:29:57 -07:00
doltutil test(bazel): fully green //..., tag taxonomy + docker lane, sharding, test sandbox ceiling (#6871) 2026-09-27 20:19:38 -07:00
domain perf(ready): recurse the --parent descendant walk off indexed base tables (#6130) 2026-09-30 20:22:43 -07:00
embeddeddolt fix(federation): actionable error when peer credentials cannot be decrypted (#6995) 2026-09-30 20:29:57 -07:00
externaldeps test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
fs test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
git test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
issueops perf(ready): recurse the --parent descendant walk off indexed base tables (#6130) 2026-09-30 20:22:43 -07:00
journalscan test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
kvkeys test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
memoryops test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
rowid test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
schema feat(schema): soft watchdog warning for long-running schema migrations (#5997) 2026-09-30 20:23:14 -07:00
sqlbuild perf(ready): recurse the --parent descendant walk off indexed base tables (#6130) 2026-09-30 20:22:43 -07:00
sqlclass fix(sql): classify bd sql statements with the SQL parser; never drop result sets (#6932) 2026-09-29 01:08:10 -07:00
storagecontract test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
uow feat(dep): detect cycles through tracks edges (#6148) 2026-09-30 20:20:17 -07:00
versioncontrolops feat(backup): proxied auto-backup on managed-local, serialized with bd backup sync (#6879) 2026-09-28 02:45:58 -07:00
advanced_queries.go /internal/storage: add interfaces for methods not covered by Storage 2026-03-06 15:30:10 -08:00
annotation_queries.go Update internal/storage/annotation_queries.go 2026-03-06 15:40:56 -08:00
batch.go refactor: delete the dead orphan-handling capability (bd-gwryr) (#5497) 2026-08-09 12:03:18 -07:00
batch_apply.go Add issueops.BatchApplier: one plan, one transaction (#5480) 2026-08-09 06:59:34 -07:00
batch_apply_test.go Add issueops.BatchApplier: one plan, one transaction (#5480) 2026-08-09 06:59:34 -07:00
BUILD.bazel fix(sql): classify bd sql statements with the SQL parser; never drop result sets (#6932) 2026-09-29 01:08:10 -07:00
bulk_issues.go feat(reclaim): scope filters --label/--label-any/--exclude-label/--assignee/--id (wy-jpd3.3) 2026-07-24 05:31:23 -07:00
child_counter_reservation.go fix(create): commit labels during initial issue creation (#4149) 2026-05-24 19:23:46 -07:00
compaction.go fix(compact): archive content before destructive compaction; make restore work (#4464) 2026-06-20 14:12:07 -07:00
config_metadata.go feat: custom status categories (active/wip/done/frozen) 2026-03-21 12:35:03 -07:00
contract_leg_registry_test.go test(storage): register the contract legs instead of discovering them (#5499) 2026-08-09 13:08:26 -07:00
dependency_queries.go feat(storage): events + dependents reads, batched blocked, public claim surface (#4926) 2026-07-22 07:41:47 -07:00
diff_store.go perf(export): incremental auto-export via dolt_diff (#5806) 2026-08-22 06:49:19 -07:00
directory_size.go fix(backup): report embedded database size (#4920) (#4928) 2026-07-25 16:14:07 -07:00
directory_size_test.go fix(backup): report embedded database size (#4920) (#4928) 2026-07-25 16:14:07 -07:00
errors_unsupported.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
event_queries.go feat(storage): events + dependents reads, batched blocked, public claim surface (#4926) 2026-07-22 07:41:47 -07:00
federation.go /internal/storage: add interfaces for methods not covered by Storage 2026-03-06 15:30:10 -08:00
history_presence.go fix(export): heal the delete wedge — prove deletions via store history instead of refusing forever (#5896) (#6059) 2026-08-29 21:24:29 -07:00
history_viewer.go feat(storage): add ExternalRefHistoryQuerier capability for external-ref history (#4733) 2026-07-12 13:43:27 -07:00
hook_batch_applier.go fix: a re-close that runs on_close, a silent cycle-gate walker, and a leaf that contradicts the wire (#5537) 2026-08-10 02:37:34 -07:00
hook_batch_applier_test.go fix: a re-close that runs on_close, a silent cycle-gate walker, and a leaf that contradicts the wire (#5537) 2026-08-10 02:37:34 -07:00
hook_batch_closer.go fix: a re-close that runs on_close, a silent cycle-gate walker, and a leaf that contradicts the wire (#5537) 2026-08-10 02:37:34 -07:00
hook_batch_closer_test.go fix: a re-close that runs on_close, a silent cycle-gate walker, and a leaf that contradicts the wire (#5537) 2026-08-10 02:37:34 -07:00
hook_batch_creator.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
hook_blockingannotator.go feat(list): put bd list behind Reader.List and the new BlockingAnnotator role (bd-5k21g) 2026-08-06 02:56:57 +00:00
hook_bootstrapper.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
hook_commenter.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_commenter_test.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_counter.go feat(issueops): put bd count behind the Counter role (bd-m8hn7) 2026-08-05 05:27:39 +00:00
hook_cycle_detector.go feat(issueops): put bd dep cycles behind the CycleDetector role (bd-wfkbv) 2026-08-05 09:36:14 +00:00
hook_decorator.go cmd/bd: bd label rename <old> <new> (stacked on #6007) (#6009) 2026-09-29 17:02:35 -07:00
hook_decorator_internal_test.go fix(export): promoted no-history wisps survive import round-trip (bd-r9uce) (#5368) 2026-08-05 12:48:44 -07:00
hook_decorator_test.go fix(telemetry): wire WrapStorage decorator into bd storage chain (#3858) 2026-07-06 22:27:32 -07:00
hook_deleter.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
hook_dependency_editor.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_dependency_editor_test.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_edgereader.go feat(issueops): put multi-id bd dep list behind the EdgeReader role (bd-nhsno) 2026-08-05 09:42:06 +00:00
hook_graph_counter.go Add issueops.GraphCounter: how many edges, in one named direction (ga-itny5) (#5485) 2026-08-09 11:29:32 -07:00
hook_init_verifier.go feat(init): put bd init's identity seam behind Bootstrapper and InitVerifier (bd-zl3u8) 2026-08-06 03:01:33 +00:00
hook_issue_claimer.go Add issueops.Claimer and route the HTTP claim through it (#5334) 2026-08-04 08:40:18 -07:00
hook_issue_claimer_test.go Add issueops.Claimer and route the HTTP claim through it (#5334) 2026-08-04 08:40:18 -07:00
hook_issue_operations.go fix: a re-close that runs on_close, a silent cycle-gate walker, and a leaf that contradicts the wire (#5537) 2026-08-10 02:37:34 -07:00
hook_issue_operations_test.go Add issueops.Releaser: give a claim back, optionally only a named holder's (#5482) 2026-08-09 07:48:56 -07:00
hook_issue_reader.go feat(issueops): add the Reader role and wire both front doors through it 2026-07-31 18:37:25 +00:00
hook_issue_reader_test.go test(telemetry,storage): pin the Reader decorators the way Lifecycle is pinned 2026-07-31 18:37:26 +00:00
hook_memories.go Put the memory plane behind memoryops, conformance-tested at three backends (#5387) 2026-08-07 03:57:04 -07:00
hook_metadata_cas.go Add issueops.MetadataCAS: compare-and-set over one metadata key (#5473) 2026-08-09 04:15:34 -07:00
hook_metadata_cas_test.go Add issueops.MetadataCAS: compare-and-set over one metadata key (#5473) 2026-08-09 04:15:34 -07:00
hook_querier.go feat(query): put bd query behind the Querier role and stop it dropping matches (bd-pohmh) 2026-08-05 16:36:05 +00:00
hook_ready_claimer.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_ready_claimer_test.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_ready_counter.go feat(ready): put bd ready's published total behind the ReadyCounter role (bd-s10oa) 2026-08-05 09:47:24 +00:00
hook_relations.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_relations_test.go feat(issueops): put every issue capability behind one library surface 2026-08-04 12:27:56 -07:00
hook_releaser.go Add issueops.Releaser: give a claim back, optionally only a named holder's (#5482) 2026-08-09 07:48:56 -07:00
hook_releaser_test.go Add issueops.Releaser: give a claim back, optionally only a named holder's (#5482) 2026-08-09 07:48:56 -07:00
hook_role_census_test.go Publish POST /v0/beads/issues/{id}/comments, the wire half of issueops.Commenter (ga-rslox) (#5594) 2026-08-10 18:59:57 -07:00
hook_stats_reporter.go feat(issueops): put bd status behind the StatsReporter role (bd-4ze25) 2026-08-05 09:27:16 +00:00
hook_sweeper.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
hook_tree_walker.go feat(issueops): put bd dep tree behind the TreeWalker role (bd-lx5iu) 2026-08-06 03:09:18 +00:00
hook_version_reconciler.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
hook_workspace_config.go Trim this series' comments toward the repo's own density 2026-08-06 08:06:59 +00:00
issue_create_dependencies.go feat(storage): one guarded issue-lifecycle surface for the CLI and linked consumers (#5191) 2026-07-31 11:26:38 -07:00
iter.go Reapply "Simplify the supported backends: roll back the direct PostgreSQL and MySQL adapters" (#4857) 2026-07-18 02:43:56 +00:00
journal_facade_test.go feat(journalops): the mutation journal becomes the third facade namespace (#5552) 2026-08-10 10:58:05 -07:00
leg_contract_wiring_test.go test(bazel): runfiles helpers + declared inputs for scan-guard tests (#6828) 2026-09-27 08:24:08 -07:00
merge_slot.go feat: add MergeSlot methods to Storage interface (fix-merge PR #2787) 2026-03-26 19:34:21 -07:00
metadata.go fix(contracts): JSON contract fixes before 1.3.0 freezes them — string revision tokens, --set-metadata scalar typing, create --json nano timestamps (#6053) 2026-08-29 05:20:27 +00:00
metadata_cas.go Add issueops.MetadataCAS: compare-and-set over one metadata key (#5473) 2026-08-09 04:15:34 -07:00
metadata_cas_test.go Add issueops.MetadataCAS: compare-and-set over one metadata key (#5473) 2026-08-09 04:15:34 -07:00
metadata_edit_value_test.go fix(contracts): JSON contract fixes before 1.3.0 freezes them — string revision tokens, --set-metadata scalar typing, create --json nano timestamps (#6053) 2026-08-29 05:20:27 +00:00
metadata_jsonpath_test.go fix(query): make slash and mixed-case metadata keys queryable (#4896) 2026-07-24 17:52:22 -07:00
metadata_schema_test.go feat: config-driven metadata schema enforcement (GH#1416 Phase 2) (#2027) 2026-02-24 22:24:56 -08:00
metadata_test.go feat: allow UpdateIssue metadata updates via json.RawMessage/[]byte (#1417) 2026-02-03 17:55:23 -08:00
remote.go feat(storage): carry the git data ref of a git-backed Dolt remote (#6521) 2026-09-27 15:56:18 -07:00
remote_ref.go feat(storage): carry the git data ref of a git-backed Dolt remote (#6521) 2026-09-27 15:56:18 -07:00
remote_ref_test.go feat(storage): carry the git data ref of a git-backed Dolt remote (#6521) 2026-09-27 15:56:18 -07:00
role_accessor_decorator_test.go docs: the ext-api-store documentation tranche — post-#5516 auth sweep, wave doc coherence, role checklist refresh (#5545) 2026-08-10 08:05:07 -07:00
storage.go fix(federation): actionable error when peer credentials cannot be decrypted (#6995) 2026-09-30 20:29:57 -07:00
sync.go /internal/storage: add interfaces for methods not covered by Storage 2026-03-06 15:30:10 -08:00
version_control.go fix(compact): replay cherry-picks in commit_order, not commit date (#6644) 2026-09-27 02:06:57 -07:00
versioned.go feat(storage): carry the git data ref of a git-backed Dolt remote (#6521) 2026-09-27 15:56:18 -07:00
wake_defers.go fix(deps): enforce external capability blockers across ready/claim/close (#4769) (#4753) 2026-09-02 16:56:23 -07:00