beads/cmd/bd/flatten.go
ecuthiell 8269a88cbf
fix(compact): target external GC at the active database (#6327)
* fix(compact): report active database size (#5619)

Measure legacy admin compact --dolt reporting through ActiveDatabaseSizer so sibling databases cannot distort reclaimed-space numbers. Omit unavailable advisory measurements without blocking GC.

Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell

* docs(compact): explain the legacy size field names

Keep the existing compact JSON schema distinct from the newer GC helper.
The active external-GC target is addressed by the separate issue6313 leaf.

* fix(compact): select the authoritative local active GC directory

Ask the active storage instance for its optional external-GC directory
capability. Reuse the authority captured at store open, and refuse stale,
unsupported or invalid local targets rather than guessing from a root,
general CLI path or advisory size. Preserve archive-level fallback behavior.

Add native owned-process target and no-launch regressions plus instance
authority tests; retain the parent active-size and legacy JSON contracts.

Fixes #6313

* fix(compact): resolve GC capability through storage decorators

Use the existing storage unwrapping boundary so external GC sees the captured
active database behind the root command's dependency, telemetry and hook
decorators. Cover owned, shared and unsupported stores through the real chain,
and describe the active database in compact help.

Refs gastownhall/beads#6313
Agent: unknown-model/unknown-reasoning

* fix(compact): preserve external GC refusal diagnostics (#6313)

Route all four new target refusals through the standard error/hint formatter
and assert their stderr schema, message, hint and exit status. Preserve the
relative-path refusal and unavailable dry-run target omission. Update the
requested compact and flatten help mirrors without regenerating other docs.

Addresses the diagnostics and documentation review on PR #6327. Existing
PR #5668 ancestry and mandatory parent-first sequencing remain unchanged.

Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* docs(compact): align flag help with active GC target (saa-cgh.94)

Authored by /root/cgh86_review; closes the independent root review help finding.

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* docs: retain release-pinned CLI references (saa-cgh.94)

Preserve corrected live compact/flatten help while generated references continue to match docs/cli-docs.pin v1.2.2. Fix the exact six-line hosted docs-policy failure without changing pin authority.

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* fix(compact): classify cancellation before the GC authority refusal

Maintainer review fixes for #6327, review iteration 1. Non-gating findings
only: the synthesis and the quality scorecard both returned approve with no
blocker and no major finding.

Taken:

- Error Handling & Resilience (minor, both lanes). runCompactDolt formatted a
  cancelled or expired context as the permanent "cannot select a local
  database" refusal plus its four-cause hint, and in --dry-run turned it into a
  hard exit-1. Classify context.Canceled/DeadlineExceeded ahead of the
  ErrUnsupported arm and report the interruption instead. The ctx probe stays
  in ExternalGCPath: it is the only cancellation checkpoint before the
  uninterruptible external `dolt gc`, so the finding's other arm (drop the
  probe) would trade a misleading message for launching a multi-minute GC on an
  interrupted command.

- Change Impact / Blast Radius (minor). Document why
  resolveLocalActiveDatabaseDir keeps the cfg.AutoStart conjunct, and make the
  CLI hint name the remedy (`bd config set dolt.auto-start true`) instead of
  only the condition. The offered code arm is declined: AutoStart rides
  resolveAutoStart's own exclusions (forced false for ServerModeExternal and
  under BEADS_TEST_MODE=1), so dropping it widens GC authority - a behavior
  change, not a cleanup.

- Release Safety (minor). Add the three new test files to the gazelle explicit
  go_test srcs lists so this PR's assertions are visible under `bazel test`.
  Verified with `bazel run //:gazelle -- --mode=diff`: gazelle requests none of
  the three, so the rows match its own output. The two rows it still wants are
  pre-existing base drift in unrelated files and are left alone.

- Test Evidence Quality (nit). The two "unsupported ... through decorators"
  rows were measured vacuous for the decorator claim. Pin the refusal's origin
  through ErrUnsupported.Backend ("external" = the active store refused,
  "active store" = the interface assertion missed) and pin the opposite
  polarity on the size-only/locator-only rows. Residual recorded on the test:
  the dry-run decorator row still cannot discriminate, because its benign
  preview is identical for both origins.

Declined, and carried to the human-approval summary:

- Debuggability & Operability (minor, gate impact none). Reusing the `bd gc`
  companions adds production output in an iteration no further review pass
  grades, and the alternative arm would assert the refs are left in place
  "deliberately", which nothing in the change establishes.

- Resource Lifecycle & Cleanup (nit, Required fix: none). exec.CommandContext
  would kill a mid-flight `dolt gc`; that is a data-safety question for the GC
  path rather than a cleanup.

Validation: `go build ./...` and `go vet` on cmd/bd + internal/storage{,/dolt}
clean; scripts/ci/pr-lint.sh against the pinned base green (gofmt,
golangci-lint 0 issues, windows cross-lint 0 issues), proved non-vacuous by a
misspell planted on an added line; 66 PASS / 0 FAIL / 0 SKIP over the covering
cmd/bd selectors. Both new pins are mutation-proved: removing the
classification arm reds exactly the three new cancellation rows and nothing
else, and dropping storage.UnwrapStore now reds three decorator rows where it
previously reddened two. internal/storage/dolt/store.go is comment-only.

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Gas City Merge Queue <merge-queue@gascity.local>
2026-09-29 08:27:43 -07:00

186 lines
5.6 KiB
Go

package main
import (
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
)
var (
flattenDryRun bool
flattenForce bool
)
var flattenCmd = &cobra.Command{
Use: "flatten",
GroupID: "maint",
Short: "Squash all Dolt history into a single commit",
Long: `Nuclear option: squash ALL Dolt commit history into a single commit.
This uses the Tim Sehn recipe:
1. Create a new branch from the current state
2. Soft-reset to the initial commit (preserving all data)
3. Commit everything as a single snapshot
4. Swap main branch to the new flattened branch
5. Prune remote-tracking refs (they would keep the old history alive;
the next push or fetch re-creates them at the new tip)
6. Run a full Dolt GC (all storage generations) to reclaim the old history
The GC pass is a full collection: Dolt storage is generational, and a default
GC never revisits data an earlier GC moved to the old generation. On any store
that has been GC'd before (bd gc, or a previous flatten or compact), only a
full collection reclaims the squashed history. A full GC can take minutes on
multi-gigabyte stores.
This is irreversible — all commit history is lost. The resulting database
has exactly one commit containing all current data.
Use this when:
- Your active database has grown very large
- You don't need commit-level history (time travel)
- You want to start fresh with minimal storage
Examples:
bd flatten --dry-run # Preview: show commit count and disk usage
bd flatten --force # Actually squash all history
bd flatten --force --json # JSON output`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(_ *cobra.Command, _ []string) error {
if usesProxiedServer() {
return HandleErrorRespectJSON("flatten is not supported in proxied-server mode")
}
evt := metrics.NewCommandEvent("flatten")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
if !flattenDryRun {
CheckReadonly("flatten")
}
ctx := rootCtx
start := time.Now()
flattener, ok := storage.UnwrapStore(store).(storage.Flattener)
if !ok {
return HandleErrorRespectJSON("storage backend does not support flatten")
}
logEntries, logErr := store.Log(ctx, 0)
if logErr != nil {
return HandleErrorRespectJSON("failed to read commit log: %v", logErr)
}
commitCount := len(logEntries)
var initialHash string
if commitCount > 0 {
initialHash = logEntries[commitCount-1].Hash
}
if flattenDryRun {
remoteRefs, tags := listRemoteRefsAndTags(ctx)
if jsonOutput {
result := map[string]interface{}{
"dry_run": true,
"commit_count": commitCount,
"initial_hash": initialHash,
"would_flatten": commitCount > 1,
"remote_refs": remoteRefs,
"tags": tags,
}
addGCSizeJSON(result, storeSizeBytes(ctx), -1)
return outputJSON(result)
}
fmt.Printf("DRY RUN — Flatten preview\n\n")
fmt.Printf(" Commits: %d\n", commitCount)
fmt.Printf(" Initial commit: %s\n", initialHash)
if len(remoteRefs) > 0 {
fmt.Printf(" Remote refs: %d (pruned before GC so old history is reclaimable)\n", len(remoteRefs))
}
if len(tags) > 0 {
fmt.Printf(" Tags: %d (anchor old history; GC cannot reclaim past them)\n", len(tags))
}
if commitCount <= 1 {
fmt.Printf("\n Already flat (1 commit). Nothing to do.\n")
} else {
fmt.Printf("\n Would squash %d commits into 1.\n", commitCount)
fmt.Printf(" Run with --force to proceed.\n")
}
return nil
}
if commitCount <= 1 {
if jsonOutput {
return outputJSON(map[string]interface{}{
"success": true,
"message": "already flat",
"commit_count": commitCount,
})
}
fmt.Println("Already flat (1 commit). Nothing to do.")
return nil
}
if !flattenForce {
return HandleErrorWithHintRespectJSON(
fmt.Sprintf("would squash %d commits into 1 (irreversible)", commitCount),
"Use --force to confirm or --dry-run to preview.")
}
if !jsonOutput {
fmt.Printf("Flattening %d commits...\n", commitCount)
}
if err := flattener.Flatten(ctx); err != nil {
return HandleErrorRespectJSON("flatten failed: %v", err)
}
// Prune remote-tracking refs before GC: they still anchor the entire
// pre-flatten chain, and with them in place GC reclaims nothing on any
// workspace that has ever pushed or fetched (bd-agctw).
sizeBefore := storeSizeBytes(ctx)
pruned, tags := pruneRemoteRefsForGC(ctx)
if !jsonOutput {
printPruneReport(pruned, tags)
}
gcMode := runPostRewriteGC(ctx, "flatten")
sizeAfter := storeSizeBytes(ctx)
elapsed := time.Since(start)
if jsonOutput {
result := map[string]interface{}{
"success": true,
"commits_before": commitCount,
"commits_after": 1,
"remote_refs_pruned": pruned,
"tags_anchoring": tags,
"elapsed_ms": elapsed.Milliseconds(),
}
if gcMode != "" {
result["gc_mode"] = gcMode
}
addGCSizeJSON(result, sizeBefore, sizeAfter)
return outputJSON(result)
}
fmt.Printf("✓ Flattened %d commits → 1\n", commitCount)
if line := gcSizeLine(sizeBefore, sizeAfter); line != "" {
fmt.Printf(" Store: %s\n", line)
}
fmt.Printf(" Time: %v\n", elapsed.Round(time.Millisecond))
return nil
},
}
func init() {
flattenCmd.Flags().BoolVar(&flattenDryRun, "dry-run", false, "Preview without making changes")
flattenCmd.Flags().BoolVarP(&flattenForce, "force", "f", false, "Confirm irreversible history squash")
rootCmd.AddCommand(flattenCmd)
}