Commit graph

18 commits

Author SHA1 Message Date
Bee
3571331b50
test: fixtures commit with maintenance.auto=false before every git worktree add (wy-7ocweu) (#7358)
Fixes #7349. The same race #7314 closed in internal/beads and
internal/config, in the fixtures it did not cover: a fixture's plain
`git commit` spawns the detached `git maintenance run --auto` child, whose
worktree-prune races the fixture's next `git worktree add` and deletes
.git/worktrees/<name>/locked under it (seen on PR #7312 run 37585997441,
cmd/bd Dolt-server tier shard 4/16, TestBootstrap_WorktreeFallbackDir).

One noAutoMaintenance + gitCommand(args...) helper per test package, each
in an existing untagged shared test file (cmd/bd/helpers_test.go,
doctor/git_test_helpers_test.go, doctor/fix/common_test.go,
formula/parser_test.go, git/gitdir_test.go, storage/domain/git/git_test.go;
cmd/bd/test_helpers_test.go is //go:build cgo so unusable for the pure
lane), and every inline exec.Command("git", ...) in those six packages
swapped for it. The flags ride the command line, never env config: the
routing-key scrub drops env config. No new files, so no BUILD.bazel rows.

Proof: GIT_TRACE on `go test ./cmd/bd -run TestBootstrap_WorktreeFallbackDir`
counts 3 `maintenance run --auto` spawns at d8d8bf587b and 0 with this
change.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-08 08:01:46 +00:00
Julian Knutsen
cba2037589
fix(bootstrap): probe git remotes for dolt data instead of rejecting; exit non-zero when bootstrap declines (#5743, #5663) (#6037)
* fix(bootstrap): probe git remotes for dolt data instead of rejecting them

`bd init` derives sync.remote from the git origin and persists it to the
git-tracked .beads/config.yaml — by design, because the first `bd dolt push`
creates refs/dolt/data on that same git remote. `bd bootstrap` then rejected
exactly that value: isGitCodeRepoURL classified any .git-suffixed or
known-forge URL as "not a Dolt remote" and returned Action=none *before* the
refs/dolt/data origin auto-detect that would have hydrated the clone. Every
fresh clone of such a repo got no database, and deleting the sync.remote line
by hand was the only way out (#5743, #5663).

The #4421 CPU storm this guard was added for is not a property of "the URL
points at a forge". Dolt's dbfactory routes by scheme: a raw http(s):// URL
goes to the remotesapi client, which speaks its wire protocol at github.com
and retries forever; a git+https/git+ssh URL goes to the git remote factory,
which shells out to git and fails cleanly. So the classifier is a good router
and a bad rejector.

Route on it instead: when sync.remote classifies as a forge URL, normalize it
to its git+ form and probe refs/dolt/data with the existing 10s-bounded
`git ls-remote` helper, then

  - data present: Action=sync from the git+ URL, never the raw form
  - definitively absent: note it and fall through to origin auto-detect,
    backup, issues.jsonl and fresh init, matching `bd init`'s own "remote has
    no Dolt data yet" behavior
  - probe error (UNKNOWN): Action=none with a new Blocked flag; never clone
    something we could not verify

Non-forge remotes (dolthub://, s3://, remotesapi http(s) endpoints,
self-hosted git+ssh) keep the trusted-as-is path untouched, so GH#3339 still
holds: normalization now happens only inside the forge branch, where a
remotesapi endpoint cannot appear.

Blocked/BlockedRemote are additive JSON fields; the exit-code half of the fix
follows in the next commit.

Refs #5743, #5663

* fix(bootstrap): exit non-zero when bootstrap declines to set up a database

Action=="none" was two different outcomes wearing one name: "a database
already exists, nothing to do" and "bootstrap refused and left you with no
database". printBootstrapPlan printed "✓ Database already exists" for both,
and RunE returned nil for both — so a rejected sync.remote reported success,
exited 0, and left the user with `bd list` saying "no beads database found"
(#5743, #5663).

The split was already encoded in the plan: every genuine no-op sets
HasExisting. Use it.

  - printBootstrapPlan reserves the success tick for HasExisting plans and
    otherwise reports the reason on stderr. Blocked plans additionally get a
    runnable `git ls-remote <url> refs/dolt/data` hint (the git+ prefix is
    stripped, as the probe itself strips it) and a pointer at the sync.remote
    line in config.yaml.
  - bootstrapPlanOutcome maps the plan to an exit status in one place, so
    text, --json and --dry-run all agree. JSON mode returns SilentExit, since
    the plan (carrying "blocked": true) is already on stdout.
  - The "Could not verify existing server database" branch is marked Blocked
    for the same reason: it also declined without confirming a database, and
    it also false-ticked.

Refs #5743, #5663

* fix(init): normalize git-forge sync.remote URLs before cloning

Init never had bootstrap's forge guard: a configured
`sync.remote: https://github.com/org/repo.git` was passed to DOLT_CLONE
verbatim, which Dolt routes to the remotesapi client — the same retry storm
(#4421) the bootstrap guard exists to prevent. Route it the same way
bootstrap now does: forge URLs are converted to their git+ form, which
selects the git remote factory, and everything else is left byte-identical so
a user-configured remotesapi endpoint (http://myserver:7007/mydb) is never
rewritten (GH#3339).

Also documents, at the two places someone would go looking after a bootstrap
failure, why init deliberately persists a plain git origin as sync.remote:
bootstrap probes refs/dolt/data to resolve it (#5743), so un-persisting here
would be the wrong fix and would strand every already-committed
.beads/config.yaml.

This commit is independent of the bootstrap fix and can be dropped from a
release cherry-pick if the minimum diff is wanted.

Refs #5743, #5663, #4421

* fix(sync): thread git's stderr into the refs/dolt/data probe error

The probe wrapped only the exec error, and exec.ExitError.Error() is just
"exit status 128" — git's actual complaint sits unread in ExitError.Stderr,
which cmd.Output() had already captured. A context timeout was worse still:
"signal: killed". Since bootstrap's blocked message is built from this error,
the fix's primary failure path told users nothing that distinguished bad
credentials from DNS from a deleted repository.

Now the error carries git's own first stderr line ("Permission denied
(publickey)", "Repository not found", "Invalid username or token") and names a
timeout as a timeout.

Also drops the remote and ref name from the wrapper. Every caller already
names the remote it probed, so wrapping here too produced messages that said
both twice: `... on sync.remote "X": probe refs/dolt/data on X: exit status
128`.

Refs #5743

* fix(bootstrap): derive the blocked verdict once; keep local recovery reachable

Review round on #6037. Every change below fixes a confirmed defect in this
PR's own new code.

State leak (the #5743 bug, reintroduced). existingBootstrapDBPlan's "none"
verdict was copied into the working plan, so every later branch inherited its
HasExisting/Blocked/Reason. A probe error on top of a seeded HasExisting=true
produced action=none + blocked=true + has_existing=true, which printed the
success tick and exited 0; a seeded server-verify error leaked blocked=true
into a *successful* sync plan's JSON, violating the field's own contract that
nothing clones from a blocked plan. The verdict is now held aside as an
explicit fallback instead of merged in, and Blocked is derived in one place
from the finished plan rather than hand-set by any branch.

Local recovery. A probe error used to return immediately, so an offline laptop
or credential-less CI runner was denied the backup/JSONL restore that the same
machine would have got if the probe had merely answered "no data". Restoring a
local backup touches no remote: the fail-closed rule now applies only to
remote actions, and the block is emitted at the tail, after every local source
has been tried.

Origin auto-detect is skipped once sync.remote is configured. sync.remote is
the authoritative remote, so hydrating from whatever git origin happens to be
— a fork, an old experiment, the code repo under the dedicated-data-repo
pattern — is not recovery, and finalizeSyncedBootstrap would then have
rewritten the team's committed sync.remote to point at it. This also removes
the second identical ls-remote in the flagship pre-first-push case (init
persists the git origin as sync.remote, so the two legs were probing the same
repository for the same ref), and makes the fall-through tests hermetic: the
origin leg is not behind the injectable probe seam.

Remote wiring. The fall-through made executeInitAction reachable with a
configured sync.remote for the first time, and it never registered the remote
as Dolt "origin". The promise that "the first bd dolt push creates
refs/dolt/data on that same git remote" then breaks: push either refuses to
adopt a remote without consent or adopts the git origin — a different
repository — and rewrites the committed sync.remote to match. Two machines
bootstrapping in the pre-push window would mint independent Dolt identities
and diverge. The provisioning actions now wire the routed remote, as the sync
action and bd init already did. bd init gets the same routing on its own
wiring call: its no-Dolt-data-yet path never clones, so a raw https forge URL
was stored verbatim in dolt_remotes and the *first* push took it to the
remotesapi client — #4421, one leg further down.

--dry-run always exits 0. bd doctor documents `bd bootstrap --dry-run` as the
safe inspection step, and a preview that aborts a set -e provisioning script
is a worse contract than a preview that merely reports. The blocked state is
still printed and still in the JSON, so the preview does predict what a real
run would refuse to do.

Credentials. sync.remote is commonly an https URL with an embedded CI token.
The reason, the hint and the blocked_remote JSON field all echoed it verbatim
into logs and agent transcripts; so did the sync plan's "Remote:" line. All
display paths now go through redactRemoteURL, which drops http(s) userinfo
whole and keeps only the account selector for ssh so the hint stays runnable.

Hints. The old remediation could name a config file that does not hold the key
(resolveSyncRemote reads a merged scope and a deprecated alias), and following
it was actively harmful: deleting sync.remote and re-running hits the same
auth failure on the git-origin probe, which swallows its error, so bootstrap
would "succeed" by creating a fresh database diverged from the team's history.
The hints now reproduce the probe, point at access, and offer `bd dolt remote
add` — never deletion.

One owner for the routing rule. doltRemoteURL (sync_remote.go) is now the
single definition of "classify, then normalize forge URLs only". It replaces
initCloneURL and the copy this PR had inlined into detectBootstrapAction: if
bd init and bd bootstrap ever disagree about the URL derived from one
committed sync.remote, that is the #5743 class of skew all over again.

Tests: blocked-with-HasExisting and blocked-leaking-into-sync (neither was
covered), probe-error-still-restores/imports, credential scrubbing on both the
plan and the printed output, dry-run exit status, the real probe's stderr
thread-through, and redactRemoteURL itself. The probe-error fixture now uses
the error shape production actually produces instead of a fabricated one, the
printer tests use the shared mutexed capture helpers, and the "#4421 pin"
comment is rescoped: it pins the routing, not the classifier, which still has
an unlisted-forge gap tracked as a follow-up.

Refs #5743, #5663, #4421

* fix(bootstrap): redact --json credentials, scrub git stderr, cut detect complexity

Maintainer fixup addressing PR #6037 review findings.

- Add BootstrapPlan.MarshalJSON so `bd bootstrap --json` (and
  `--dry-run --json`) redacts the credential-bearing sync_remote on a copy,
  matching the human print path. Closes the deterministic token leak to stdout
  while leaving the raw plan executeBootstrapPlan clones from untouched.
- Scrub credential-bearing URLs from the folded `git ls-remote` stderr at the
  source (scrubURLCredentials in gitLsRemoteProbeError) so a tokenized URL git
  echoes cannot reach plan.Reason, which flows to both stderr and JSON.
- Reduce detectBootstrapPlan cyclomatic 22->12 and cognitive 27->13 by
  extracting planConfiguredSyncRemote, gitOriginSyncPlan, and
  localFileRecoveryPlan. Detection order and plan-mutation semantics are
  preserved (a forge remote with no Dolt data still records SyncRemote so a
  local-recovery workspace stays wired to it).
- Drop the dead `plan.Blocked = true` in existingBootstrapDBPlan; the wrapper
  derives Blocked once from Action=="none" && !HasExisting.

Tests: assert the token is absent from the plan JSON (and MarshalJSON does not
mutate the source plan), and that tokenized git stderr is scrubbed while
credential-free diagnostics pass through unchanged.

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

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-29 18:23:57 -07:00
guyua9
7f7708e1b1 fix: preserve Windows drive remotes for dolt sync 2026-04-29 02:20:38 +08:00
Steven Syrek
c6d0cc2fd9
fix(init): refuse --force when remote has Dolt history (#3455)
* fix(init): refuse --force when remote has Dolt history

bd init --force in a repo whose origin has refs/dolt/data silently
skipped the bootstrap-from-remote path and then wired origin as a Dolt
remote anyway. The next write auto-pushed an orphan history, failing
with "no common ancestor". Recovery options were all destructive.

This change:

- Adds --reinit-local (named-intent replacement for --force) and
  --discard-remote (explicit cross-boundary authorization).
- Routes --force to --reinit-local with a DeprecationWarning. --force
  continues to work for ≥2 releases.
- Refuses bd init --force / --reinit-local when origin has
  refs/dolt/data, unless --discard-remote is also passed. Exit code 10
  (ExitRemoteDivergenceRefused).
- Adds stable numeric exit codes for init refusals (10/11/12).
- Adds bd init-safety documenting the full flag surface and the
  destroy-token format.
- Adds an internal race-safety re-check: if remote state changes
  during the TTY confirmation window, abort rather than overwrite.
- Retrofits the existing destroy-token echo at init.go:231 to not
  print a copy-pasteable destructive invocation, matching the new
  error-text-no-echo ADR invariant.

Adds:
- cmd/bd/init_safety.go — CheckRemoteSafety chokepoint (pure fn).
- cmd/bd/init_safety_test.go — guard matrix (13 cases) + subprocess
  regression test with a synthetic refs/dolt/data fixture.
- docs/adr/0002-init-safety-invariants.md — ADR encoding the five
  invariants (single-source identity, scope-bound --force, central
  chokepoint, error-text-no-echo, race-safety).
- docs/RECOVERY.md — named recovery playbooks per refusal exit code.
- .github/CODEOWNERS — review ownership for init*.go pointing at the
  ADR so future flags must extend the guard matrix.

Historical pattern (git log for cmd/bd/init.go): 8+ prior commits
patched individual surfaces of this class without encoding the
invariant. The guard matrix test is the structural hedge — adding a
new flag or data source is a signal to extend the table, not to add
another `&& !someFlag` at the call site.

(bd-q83)

* fix(init): satisfy lint for origin safety helpers (bd-main-nwo)

---------

Co-authored-by: matt wilkie <maphew@gmail.com>
2026-04-24 09:01:35 -07:00
cwalv
4d14fb8ce1 feat: auto-detect beads database on git origin during bootstrap
When running `bd bootstrap` in a fresh clone, probe origin for
refs/dolt/data and automatically clone the existing beads database
instead of creating unrelated state. `bd init` warns if the ref
exists and suggests using `bd bootstrap` instead.

Fixes #2580
2026-03-14 00:18:13 -07:00
Osamu Okano
24836dd25b
fix(init): skip AGENTS.md generation in bare repositories 2026-03-02 20:25:52 +09:00
beads/crew/emma
76b2f1e456 refactor: gut JSONL sync engine, keep simple manual import/export (bd-h0zvj.2)
Delete 11 production files and 19 test files (~12,900 lines):
- 3-way merge engine (sync_merge, sync_conflict, sync_manual)
- Sync mode infrastructure (sync_mode, sync_mode_cmd)
- Sync state tracking and backoff (sync_state)
- Sync branch/worktree operations (sync_branch, migrate_sync)
- Integrity checks (sync_check, sync_status)
- Bridge functions (sync_bridge)

Rewrite sync.go from 805 to 159 lines:
- Default: Dolt commit + push
- --import: JSONL → Dolt
- --export/--flush-only: Dolt → JSONL
- --pull: Pull from Dolt remote

Keep sync_helpers.go with core export/import functions.
Keep sync_git.go (used by init.go, restore.go).

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

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-02-15 16:42:44 -08:00
quartz
d582e00ffc perf(tests): replace ~60 git init subprocess calls with cached template copy
Use sync.Once to create a single git repo template at test startup, then
copy the .git directory for each test instead of spawning git init. Reduces
subprocess overhead across cmd/bd, cmd/bd/doctor, and cmd/bd/doctor/fix
packages (-283 lines net).

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

Executed-By: beads/polecats/quartz
Rig: beads
Role: polecats
2026-02-14 17:49:53 -08:00
darcy
4480faf737 fix: remove/fix 14 broken tests after JSONL/daemon deletion (-2,782 lines)
Tests were calling stubbed JSONL functions (writeJSONLAtomic, autoImportIfNewer,
validateJSONLIntegrity, etc.) that now return nil/empty after the JSONL sync
removal. Also fixes: --no-daemon flag re-registered as hidden no-op for backward
compat, getProjectRoot uses runtime.Caller to avoid CWD sensitivity, removed
--no-daemon from init test args, added v0.49.6 changelog entries.

Deleted: direct_mode_test.go, jsonl_integrity_test.go, jsonl_lock_test.go,
rename_prefix_jsonl_update_test.go. Trimmed: main_test.go, nodb_test.go,
show_watch_test.go, staleness_test.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 12:10:35 -08:00
beads/crew/lizzy
2bb827facb style: run gofmt across entire codebase (bd-nmk12)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 19:35:20 -08:00
Peter Chanthamynavong
5d93aaf722
fix(sync): remove orphaned restoreBeadsDirFromBranch function (#1121)
Remove dead code that was inadvertently orphaned when PR #918 refactored
the sync flow. The function was never called since v0.47.0.

The function caused GH#1100 by running `git checkout HEAD -- .beads/`
which restored the entire .beads/ directory, overwriting uncommitted
config.yaml changes.

Add regression test (TestConfigPreservedDuringSync) to prevent similar
restoration logic from being reintroduced.

Fixes #1100
2026-01-15 19:22:52 -08:00
Peter Chanthamynavong
0a48519561
feat(context): centralize RepoContext API for git operations (#1102)
Centralizes repository context resolution via RepoContext API, fixing bugs where git commands run in the wrong repo when BEADS_DIR points elsewhere or in worktree scenarios.
2026-01-15 07:55:08 -08:00
fang
2332e6f5bd fix: update tests for getCurrentBranchOrHEAD and getPIDFileForSocket
- sync_git_test.go: getCurrentBranchOrHEAD now returns 1 value (not 2)
- daemon_basics_test.go: getPIDFileForSocket uses dbPath not socketPath
- daemon_autostart.go: mark unused socketPath param with underscore

These tests were broken by recent refactors that changed function signatures
but didn't update the corresponding test files.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-12 23:51:56 -08:00
Phredrick Phool
228b7195b1 fix(sync): handle detached HEAD in bd sync --status
Add getCurrentBranchOrHEAD() which returns "HEAD" when in detached HEAD
state instead of failing. This fixes bd sync --status for jj/jujutsu users.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-12 07:09:25 -06:00
Phredrick Phool
465f39b366 Merge remote-tracking branch 'origin/main' into fix-jj-detached-head-upstream-check
# Conflicts:
#	cmd/bd/sync_git_test.go
2026-01-12 06:49:51 -06:00
Phredrick Phool
78fec4bc45 fix(daemon): check sync-branch upstream for jj/jujutsu compatibility
When sync-branch is configured, check that branch's upstream instead of
current HEAD's upstream. This fixes --auto-push with jj/jujutsu which
always operates in detached HEAD mode.

Adds gitBranchHasUpstream(branch) to check specific branch's upstream
tracking, independent of current HEAD state.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-12 06:44:31 -06:00
Steve Yegge
d74a14d92a test(sync): add tests for redirect handling in git status checks
Add tests for gitHasBeadsChanges and gitHasUncommittedBeadsChanges
functions when .beads directory is redirected to another repo:

- Test non-redirected case (baseline functionality)
- Test redirected case with no changes (clean target repo)
- Test redirected case with changes (modified file in target)

These tests cover the redirect detection logic added in a61cbde6
that uses `git -C <targetRepoDir>` when beadsDir is redirected.

Closes: bd-hxcf

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:31:24 -08:00
ruby
32aea46ca6 test(sync): add unit tests for gitHasUncommittedBeadsChanges (bd-p7i2)
Extract parseGitStatusForBeadsChanges helper function from
gitHasUncommittedBeadsChanges to enable unit testing without git setup.

Add 14 test cases covering:
- Empty/no changes
- Modified files (staged, unstaged, both)
- Added files (staged, with modifications)
- Untracked files (should return false)
- Deleted files (should return false)
- Renamed/copied files
- Edge cases (short status line)

Part of GH#885 follow-up.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 16:22:20 -08:00