beads/cmd/bd/init_safety_help.go
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

86 lines
2.9 KiB
Go

package main
import (
"fmt"
"github.com/spf13/cobra"
)
// initSafetyHelpCmd documents the init flag surface and the destroy-token
// format. Its contents are the authoritative runtime reference for the
// error-message-no-echo invariant: refusal texts point here rather than
// constructing copy-pasteable destructive invocations. See
// docs/adr/0002-init-safety-invariants.md.
var initSafetyHelpCmd = &cobra.Command{
Use: "init-safety",
Short: "Explain bd init flag semantics and the destroy-token format",
Long: `bd init flag safety contract.
Every bd init invocation resolves project_id from exactly one explicitly
named source (local reinit, remote adoption, or a fresh mint). When the
source is ambiguous, bd init refuses.
FLAG SURFACE
bd init Mint a new identity. Bootstraps from
origin if it has refs/dolt/data.
bd init --reinit-local Re-initialize local .beads/ over existing
local data. Does NOT authorize discarding
remote history. If origin has Dolt data
this will refuse — pair with
--discard-remote to override.
bd init --reinit-local \ Discard the remote's Dolt history and
--discard-remote replace it with the local reinit. First
bd dolt push after this will be a
history-replacing force-push.
bd init --force Deprecated alias for --reinit-local.
Kept working for ≥2 releases.
ADOPTING A REMOTE
If you want to use the remote's existing history, use:
bd bootstrap
bd init will automatically suggest this when a remote is detected.
DESTROY-TOKEN (non-interactive only)
When running with no TTY (CI, agents, piped input), --discard-remote
requires an explicit --destroy-token value. The token format is:
DESTROY-<issue-prefix>
For example, if your issue prefix is "bd", the token is "DESTROY-bd":
bd init --reinit-local --discard-remote --destroy-token=DESTROY-bd
In interactive (TTY) mode you confirm via a typed prompt instead. The
token is not echoed by bd's runtime error messages — this is a
deliberate guard against pattern-matched one-liners (see
docs/adr/0002-init-safety-invariants.md).
EXIT CODES
10 refused: remote has Dolt history and you passed --force/--reinit-local
without --discard-remote
11 refused: existing local data and you declined the destroy confirm
12 refused: --discard-remote passed without a valid --destroy-token
(non-interactive mode)
RECOVERY
If you hit a refusal, see docs/RECOVERY.md for step-by-step recovery
playbooks for each exit code.
`,
Run: func(cmd *cobra.Command, _ []string) {
fmt.Print(cmd.Long)
},
}
func init() {
rootCmd.AddCommand(initSafetyHelpCmd)
}