beads/cmd/bd/dolt_clean_databases_proxied_server.go
matt wilkie f8c84d510a
fix(uow): verify project identity on every team-server open, not just at init (#5183)
The proxied/team-server path short-circuits in cmd/bd/main.go before
validateWorkspaceIdentity and never constructs a DoltStore, so
DoltStore.verifyProjectIdentity — which guards every non-CreateIfMissing
gateway open — is unreachable there. `bd init --team-server` ADOPTS whatever
identity the shared database carries, and before this change nothing
re-asserted it on any later open.

This predates #5159; what #5159 changed is exposure. Proxied-server used to
mean a per-workspace database bd created itself, so "the server is serving a
DIFFERENT project's database" barely applied. --team-server deliberately
points bd at a long-lived, operator-managed (bts) database, selectable per
invocation via --database — exactly the case the gateway guard exists to
catch. This is the proxied-path sibling of the shared-server gap #5143
closes; that PR does not cover this path.

Scope: this closes the TEAM-SERVER half. Plain proxied-server mode also
never reaches verifyProjectIdentity, but asserting there would break
`bd sql --database <arbitrary db>`, which the proxied multistatement tests
establish as intentional. Follow-up tracked separately.

checkTeamServerIdentity runs immediately after checkTeamServerSchema, so the
metadata table is known to exist at this binary's version. A mismatch is
wrapped in backoff.Permanent; isSerializationError only matches typed MySQL
1213/1205, so a plain mismatch can never be misclassified as retryable,
while a genuine deadlock during the metadata read still retries.

Identity is required on BOTH sides in team-server mode rather than
soft-skipped as verifyProjectIdentity does for legacy databases:
adoptTeamServerIdentity already refuses to attach to a bts database with no
metadata._project_id, so an absent identity is an invalid state, not a
legacy one — and tolerating it would mean deleting one row from the shared
database silently disables this guard for every client. A team-server
workspace with no local ProjectID is likewise refused, since an empty
expectation is indistinguishable from the adoption path. --team-server is
unreleased, so neither tightening costs compatibility.

The two callers that legitimately have nothing to assert — `bd init`, which
adopts, and server-wide database maintenance — go through
newProxiedServerUOWProviderAdopting. Asserting the locally-minted
placeholder at init would reject every correct init. identityPosture's zero
value is the asserting one, so a missed posture fails safe.

Tests: unit coverage of match/mismatch/adoption/absent-identity/read-error,
plus an integration negative control that provisions a real identity and
confirms a foreign workspace is refused. The mismatch test was
mutation-checked — it fails when the comparison is disabled.

Agent-Signature: claude-opus-5-medium on behalf of maphew
2026-07-31 01:41:46 -07:00

25 lines
683 B
Go

package main
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/storage/uow"
)
func runDoltCleanDatabasesProxied(ctx context.Context, beadsDir string, opts cleanDatabasesOptions) error {
provider, err := newProxiedServerUOWProviderAdopting(ctx, beadsDir, "")
if err != nil {
return HandleError("failed to open uow provider: %v", err)
}
defer func() { _ = provider.Close(ctx) }()
mp, ok := provider.(uow.MaintenanceProvider)
if !ok {
return HandleError("proxied-server provider does not support maintenance operations")
}
return mp.RunNonTx(ctx, func(ctx context.Context, conn *sql.Conn) error {
return cleanDatabases(ctx, conn, opts)
})
}