fix(cmd/bd): only a missing data dir skips the identity check (bd-wlygc)
isBootstrapPreviewErr decides whether the workspace identity check's preview open failed because no local database exists yet (a first run, so the check is skipped) or for any other reason (refuse with "could not verify workspace identity"). It was errors.Is(err, os.ErrNotExist), which also matches ENOENTs that are not a first run: a dial to a configured unix socket nothing listens on, and an embedded data dir missing an internal file. Those skipped the check silently instead of refusing loudly as be-3bt2e requires. The real open fails in every such state too, so what was lost is the refusal, not a guard against a mutating open. The embedded read-only open now wraps a new exported sentinel, embeddeddolt.ErrNoEmbeddedDatabase, alongside the failed data-dir stat's own error; the message is unchanged. The classifier requires both the sentinel and ENOENT. TestIsBootstrapPreviewErr gains real-ENOENT negatives (an absent unix socket dial, a file missing inside the data dir), each checked to be an ENOENT first so the table can tell a narrow classifier from a broad one, plus a sentinel-wrapping-EACCES negative that pins the ENOENT conjunct. Bare os.ErrNotExist is no longer a first run. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3862b03ff5
commit
31fb150ced
4 changed files with 49 additions and 5 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
@ -14,6 +15,7 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
||||
)
|
||||
|
||||
// TestIsBootstrapPreviewErr pins the classifier that decides whether the
|
||||
|
|
@ -22,17 +24,42 @@ import (
|
|||
// shape embeddeddolt produces for "no database on disk yet" and nothing else,
|
||||
// including an error that merely happens to also be wrapped.
|
||||
func TestIsBootstrapPreviewErr(t *testing.T) {
|
||||
wrappedNotExist := fmt.Errorf("embeddeddolt: no embedded database at %s: %w", "/tmp/x/embeddeddolt", os.ErrNotExist)
|
||||
// The bootstrap shape, built the way embeddeddolt's read-only open builds
|
||||
// it around its failed stat of the data dir.
|
||||
dataDir := filepath.Join(t.TempDir(), "embeddeddolt")
|
||||
_, statErr := os.Stat(dataDir)
|
||||
noEmbeddedDB := fmt.Errorf("%w at %s: %w", embeddeddolt.ErrNoEmbeddedDatabase, dataDir, statErr)
|
||||
unreadableDataDir := fmt.Errorf("%w at %s: %w", embeddeddolt.ErrNoEmbeddedDatabase, dataDir, os.ErrPermission)
|
||||
wrappedOther := fmt.Errorf("dial tcp 127.0.0.1:3306: %w", errors.New("connect: connection refused"))
|
||||
|
||||
// Real ENOENTs from sources that are not "no local database yet" (bd-wlygc).
|
||||
// A hand-built errors.New can never satisfy errors.Is(err, os.ErrNotExist),
|
||||
// so only a real syscall error tells a narrow classifier from one that
|
||||
// matches every ENOENT. The dial is what dolt server mode wraps for a
|
||||
// configured socket nothing listens on; it is relative to a fresh cwd so
|
||||
// the path stays under the unix socket path limit wherever t.TempDir lives.
|
||||
t.Chdir(t.TempDir())
|
||||
_, dialErr := net.Dial("unix", "absent.sock")
|
||||
absentSocket := fmt.Errorf("Dolt server unreachable at %s: %w", "absent.sock", dialErr)
|
||||
_, manifestErr := os.Stat(filepath.Join(t.TempDir(), "beads", ".dolt", "noms", "manifest"))
|
||||
guttedDataDir := fmt.Errorf("embeddeddolt: open db: %w", manifestErr)
|
||||
for _, err := range []error{absentSocket, guttedDataDir} {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("precondition: %v is not an ENOENT, so it cannot tell a narrow classifier from a broad one", err)
|
||||
}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{"nil error", nil, false},
|
||||
{"direct os.ErrNotExist", os.ErrNotExist, true},
|
||||
{"wrapped os.ErrNotExist (embedded store shape)", wrappedNotExist, true},
|
||||
{"embedded store's no-database error", noEmbeddedDB, true},
|
||||
{"bare os.ErrNotExist", os.ErrNotExist, false},
|
||||
{"embedded store's data dir unreadable", unreadableDataDir, false},
|
||||
{"real ENOENT dialing an absent unix socket", absentSocket, false},
|
||||
{"real ENOENT from a file missing inside the data dir", guttedDataDir, false},
|
||||
{"generic non-ENOENT error", errors.New("connection refused"), false},
|
||||
{"wrapped non-ENOENT error", wrappedOther, false},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2749,8 +2749,14 @@ func flushBatchCommitOnShutdown() {
|
|||
// run, not a workspace problem. It must stay narrow: any other previewErr
|
||||
// (unreachable server, bad config, ...) has to refuse rather than silently
|
||||
// wave the real, mutating open through (be-3bt2e).
|
||||
//
|
||||
// So it matches the embedded store's own no-database error, and only when the
|
||||
// cause is a missing data dir — not any ENOENT. A dial to a configured unix
|
||||
// socket whose server is down satisfies errors.Is(err, os.ErrNotExist) too,
|
||||
// as does a data dir missing an internal file, and neither is a first run
|
||||
// (bd-wlygc).
|
||||
func isBootstrapPreviewErr(err error) bool {
|
||||
return errors.Is(err, os.ErrNotExist)
|
||||
return errors.Is(err, embeddeddolt.ErrNoEmbeddedDatabase) && errors.Is(err, os.ErrNotExist)
|
||||
}
|
||||
|
||||
// refuseUnverifiablePreviewOpen renders the identity gate's refusal when the
|
||||
|
|
|
|||
|
|
@ -15,3 +15,14 @@ import "errors"
|
|||
// refuses it must not turn an otherwise successful command into a non-zero
|
||||
// exit after the fact.
|
||||
var ErrReadOnly = errors.New("embeddeddolt: store is read-only")
|
||||
|
||||
// ErrNoEmbeddedDatabase is wrapped, together with the failed stat's own error,
|
||||
// by OpenReadOnly and OpenForPreviewCommand when beadsDir has no embedded data
|
||||
// directory.
|
||||
//
|
||||
// Exported, and declared here without a cgo build tag, for the same reason as
|
||||
// ErrReadOnly: the CLI's pre-open workspace identity check has to tell "no
|
||||
// local database yet" — a first run, which it skips — apart from every other
|
||||
// failed preview open, which it refuses. errors.Is(err, os.ErrNotExist) alone
|
||||
// cannot: a dial to a unix socket nothing listens on satisfies it too.
|
||||
var ErrNoEmbeddedDatabase = errors.New("embeddeddolt: no embedded database")
|
||||
|
|
|
|||
|
|
@ -356,7 +356,7 @@ func openReadOnly(ctx context.Context, beadsDir, database, branch string, checkB
|
|||
}
|
||||
dataDir := filepath.Join(absBeadsDir, "embeddeddolt")
|
||||
if _, err := os.Stat(dataDir); err != nil {
|
||||
return nil, fmt.Errorf("embeddeddolt: no embedded database at %s: %w", dataDir, err)
|
||||
return nil, fmt.Errorf("%w at %s: %w", ErrNoEmbeddedDatabase, dataDir, err)
|
||||
}
|
||||
|
||||
s := &EmbeddedDoltStore{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue