beads/cmd/bd/context_cmd.go
Julian Knutsen 92ad0295f1
Guard and qualify historical upgrades to v1.2 (#4907)
* Add sealed-copy SQLite migration bridge (bd-mvh)

Create a fresh current workspace from a fingerprinted copy of an authenticated historical SQLite workspace. Keep every historical executable inside an isolated destination and reject malformed or semantically lossy cutovers without touching the source.

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Test User <test@test.com>
Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Refuse implicit historical workspace upgrades (bd-mvh)

Detect historical SQLite and legacy Dolt layouts before config migration, version tracking, init, bootstrap, doctor, or store construction. Require the sealed-copy bridge while preserving current shared-server admission against the explicitly selected workspace.

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Test User <test@test.com>
Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Qualify authenticated historical upgrades (bd-mvh)

Replace generic recipe discovery with a fixed, checksum-pinned corpus spanning the reviewed SQLite, server-Dolt, and embedded-Dolt eras. Assert source isolation, representative data fidelity, schema idempotence, and rollback retention while keeping historical executables inside bounded temporary environments.

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Test User <test@test.com>
Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Add authenticated legacy SQLite reader (bd-tku)

Let the current binary read the exact v0.49.6/v0.50.3 SQLite contract from a sealed private DB/WAL copy and emit canonical JSONL for the normal fresh-Dolt import path. Reject schema drift and values that current import would lose, default, truncate, or deduplicate.

Qualify both authentic releases end to end, including current DATETIME(0) canonicalization and reader-to-fresh-import timestamp equality.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Fix empty embedded-root upgrade bypass (bd-e6m)

Require an actual embedded Dolt repository marker before current layout precedence can suppress historical SQLite or legacy Dolt refusal.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* fix(migrate): maintainer review fixups for legacy SQLite upgrade PR

- decompose the legacy-workspace guard and extract the dependency-row loader
  (behavior-preserving), and align the legacy SQLite reader projection with
  issueops.ScanIssueFrom arity after the 0060/0061 rebase, guarded by a
  projection arity test.

- assert the historical-upgrade harness schema no-op by shape
  ("Schema already at v<N>") instead of a hardcoded v59, so the migration-test
  matrix stops reddening on every schema bump (candidate now prints v61).

* Fix historical upgrade review blockers (bd-mvh)

Route direct SQLite lanes through the current authenticated reader, guard no-store commands before legacy workspace mutation, and move coarse embedded repository detection behind the storage adapter. Restore the legacy reader projection contract and verify source-preserving behavior across the repaired paths.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Fix historical upgrade edge cases (bd-mvh)

Refuse ambiguous local server-Dolt roots before storage opens and normalize nullable legacy SQLite sentinel fields during authenticated export.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

* Fix final historical upgrade review blockers (bd-mvh)

Make explicit server selection outrank stale embedded repository markers, so ambiguous local server roots still fail closed.

Permit zero-record cutovers only through the authenticated direct SQLite reader, verify the empty round trip against a fresh current export, and align the upgrade guidance with the enforced admission matrix.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of CI Bot

---------

Co-authored-by: CI Bot <ci@beads.test>
Co-authored-by: Test User <test@test.com>
2026-08-06 18:45:04 -07:00

215 lines
6.9 KiB
Go

package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/doltserver"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage/domain"
)
// ContextInfo contains the effective backend identity and repository context.
type ContextInfo struct {
BeadsDir string `json:"beads_dir"`
RepoRoot string `json:"repo_root"`
CWDRepoRoot string `json:"cwd_repo_root,omitempty"`
IsRedirected bool `json:"is_redirected"`
IsWorktree bool `json:"is_worktree"`
Backend string `json:"backend"`
DoltMode string `json:"dolt_mode"`
ServerHost string `json:"server_host,omitempty"`
ServerPort int `json:"server_port,omitempty"`
ProxiedDir string `json:"proxied_dir,omitempty"`
Database string `json:"database"`
DataDir string `json:"data_dir,omitempty"`
ProjectID string `json:"project_id,omitempty"`
SyncRemote string `json:"sync_remote,omitempty"`
SyncGitRemote string `json:"sync_git_remote,omitempty"` // Deprecated: use sync_remote
Role string `json:"role,omitempty"`
BdVersion string `json:"bd_version"`
}
var contextCmd = &cobra.Command{
Use: "context",
GroupID: "setup",
Short: "Show effective backend identity and repository context",
Long: `Show the effective backend identity information including repository paths,
backend configuration, and sync settings.
This command reads directly from config files and does not require the
database to be open, making it useful for diagnostics in degraded states.
Examples:
bd context # Show context information
bd context --json # Output in JSON format
`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
evt := metrics.NewCommandEvent("context")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
if usesProxiedServer() {
return runContextProxiedServer(cmd, rootCtx)
}
// The direct route reads config files itself rather than through the
// contextinfo provider — it must answer in degraded states where no
// database can be opened — but it assembles the SAME snapshot the
// proxied route gets from the provider, and hands it to the same
// view. That is what keeps `bd context` one answer across two routes,
// and what puts both of them on the projection GET /v0/beads/context
// serves. TestContextRoutesNameOneWorkspaceTheSameWay holds them to it;
// until it existed both routes carried their own `Backend: dolt` and
// agreed by telling the same lie.
snapshot := domain.ContextInfo{BdVersion: Version}
if selected := selectedNoDBBeadsDir(cmd); selected != "" {
prepareSelectedNoDBContext(selected)
}
rc, err := beads.GetRepoContext()
if err != nil {
if jsonOutput {
if jerr := outputJSON(map[string]string{"error": fmt.Sprintf("cannot resolve repo context: %v", err)}); jerr != nil {
return jerr
}
return SilentExit()
}
return HandleError("cannot resolve repo context: %v", err)
}
snapshot.BeadsDir = rc.BeadsDir
snapshot.RepoRoot = rc.RepoRoot
snapshot.CWDRepoRoot = rc.CWDRepoRoot
snapshot.IsRedirected = rc.IsRedirected
snapshot.IsWorktree = rc.IsWorktree
if role, ok := rc.Role(); ok {
snapshot.Role = string(role)
}
cfg, err := configfile.LoadForDiscovery(rc.BeadsDir)
if err != nil {
cfg = configfile.DefaultConfig()
}
if cfg == nil {
cfg = configfile.DefaultConfig()
}
if err := applyContextBackend(&snapshot, rc.BeadsDir, cfg); err != nil {
return HandleError("%v", err)
}
snapshot.SyncRemote = resolveSyncRemoteFromDir(rc.BeadsDir)
info := contextInfoView(snapshot)
if jsonOutput {
return outputJSON(info)
}
printContextText(info)
return nil
},
}
// applyContextBackend records the backend half of the direct route's snapshot,
// off the config files it just read.
//
// It is a named function rather than a run of assignments inside the command so
// that the claim above it — that both `bd context` routes assemble the SAME
// snapshot — is something a test can drive rather than something a reader has
// to check by eye. TestContextRoutesNameOneWorkspaceTheSameWay does exactly
// that, comparing this against the contextinfo provider the proxied route and
// GET /v0/beads/context both go through.
//
// The identity itself goes through domain.SetBackendIdentity, which is the one
// policy both routes share: it is what stops a non-Dolt workspace from being
// described as embedded Dolt on database "beads", which is what both routes did
// while each held its own copy of `Backend: configfile.BackendDolt`.
func applyContextBackend(snapshot *domain.ContextInfo, beadsDir string, cfg *configfile.Config) error {
snapshot.SetBackendIdentity(cfg.GetBackend(), cfg.GetDoltMode(), cfg.GetDoltDatabase())
snapshot.ProjectID = cfg.ProjectID
if cfg.IsDoltServerMode() {
snapshot.ServerHost = cfg.GetDoltServerHost()
snapshot.ServerPort = doltserver.DefaultConfig(beadsDir).Port
}
if cfg.IsDoltProxiedServerMode() {
p, err := resolveProxiedServerRootPath(beadsDir)
if err != nil {
return fmt.Errorf("resolve proxied server root: %w", err)
}
snapshot.ProxiedDir = p
}
if dataDir := cfg.GetDoltDataDir(); dataDir != "" {
snapshot.DataDir = dataDir
}
return nil
}
func printContextText(info ContextInfo) {
fmt.Printf("bd version: %s\n", info.BdVersion)
fmt.Println()
// Repository
fmt.Println("Repository:")
fmt.Printf(" beads dir: %s\n", info.BeadsDir)
fmt.Printf(" repo root: %s\n", info.RepoRoot)
if info.CWDRepoRoot != "" && info.CWDRepoRoot != info.RepoRoot {
fmt.Printf(" cwd repo: %s\n", info.CWDRepoRoot)
}
if info.IsRedirected {
fmt.Printf(" redirected: yes\n")
}
if info.IsWorktree {
fmt.Printf(" worktree: yes\n")
}
if info.Role != "" {
fmt.Printf(" role: %s\n", info.Role)
}
fmt.Println()
// Backend
fmt.Println("Backend:")
fmt.Printf(" type: %s\n", info.Backend)
// Dolt-only identity. A registered backend reports neither, and a bare
// "mode:" with nothing after it reads as a failure to determine rather than
// as not-applicable — so omit them, like every other optional field here.
if info.DoltMode != "" {
fmt.Printf(" mode: %s\n", info.DoltMode)
}
if info.Database != "" {
fmt.Printf(" database: %s\n", info.Database)
}
if info.ServerHost != "" {
fmt.Printf(" server: %s:%d\n", info.ServerHost, info.ServerPort)
}
if info.ProxiedDir != "" {
fmt.Printf(" proxied dir: %s\n", info.ProxiedDir)
}
if info.DataDir != "" {
fmt.Printf(" data dir: %s\n", info.DataDir)
}
if info.ProjectID != "" {
fmt.Printf(" project id: %s\n", info.ProjectID)
}
// Sync
if info.SyncRemote != "" {
fmt.Println()
fmt.Println("Sync:")
fmt.Printf(" remote: %s\n", info.SyncRemote)
}
}
func init() {
rootCmd.AddCommand(contextCmd)
readOnlyCommands["context"] = true
}