THE TWO ROUTES DISAGREED ABOUT WHAT --force MEANS on a destructive command.
On a local database `--force` deletes the named beads and orphans their
dependents; on a team server it deleted the whole dependent subtree, `--cascade`
was REFUSED outright ("not supported in proxied-server mode (delete always
cascades)") and Cascade was hardcoded true at both call sites. So there was no
way to delete one bead on a team server without taking everything under it, and
asking for the safer behaviour was an error.
Q1 ANSWERED IN THE ROLE. issueops.Deleter has one method, reached through
store.Deleter() and the provider's own accessor, and the three modes are request
fields: no flags refuses over a dependent the request did not name, Force
orphans it, Cascade deletes the closure. The guard is workapi's and the body's,
below every front door, for the reason Sweeper's require-a-filter gate is: a
second front door that inherited the capability without the guard would orphan a
workspace's graph by omission — which is exactly what this commit's HTTP
operation would otherwise have been.
IT IS A DIFFERENT ROLE FROM Sweeper, not a mode of it. Sweeper erases a set the
caller DESCRIBED; this erases rows the caller NAMED. That is what decides where
each one's safety lives: a description can be accidentally too wide, so Sweeper
carries a require-a-filter refusal; a list of ids cannot be, so this one's guard
is about the graph.
THE DEFECT. The direct route's batch path deleted the rows in one transaction
and then rewrote `[deleted:ID]` into the neighbours' text afterwards, discarding
every update error. A failure in the window left a workspace whose beads were
gone and whose descriptions still cited them by id. The existence probe, the
guard, the cascade expansion, the deletion and the rewrite are now one
transaction. WHAT THAT COSTS is written into the leaf beside the promise: the
transaction is as large as the deletion plus its neighbourhood, so a batch with
a very large neighbourhood fails whole. It is NOT Sweeper's trade (bd-hysee) —
the delete itself was already one transaction on every route — and the part that
is new is named rather than measured, which is recorded for sign-off.
TWO MORE DEFECTS the single call site removed. The one-id path held its own
transaction that removed each edge by hand, so it never reported labels, events
or orphans; and `domain.DeleteIssuesParams.EnforceCascadePolicy` and `.Force`
were declared, documented in fifteen lines describing exactly these three modes,
and READ BY NOTHING. They are deleted; the policy lives in the role now.
THE CONTRACT IS WHAT WAS DELETED, not what was reported.
backend/conformance/deleter_contract.go holds eleven cases, each citing the leaf
doc, run at all three backends with ZERO PARKS and zero divergences. Every case
that acts asserts row counts through QueryScalar rather than trusting the result
the delete reported about itself. They are TWO INDEPENDENT BODIES plus an engine
check: dolt and embeddeddolt share internal/storage/issueops.DeleteInTx and
differ only in how they reach a transaction, and the unit-of-work provider
reaches the same questions through the domain use cases. It does NOT duplicate
backend/conformance/audit_issue-lifecycle.go, which stays the right home for
`DeleteIssues` at the STORAGE seam: that suite's Factory hands back a bare
storage.DoltStorage, so the unit-of-work backend — where the guard did not exist
at all before this commit — can never run a case placed there.
`bd serve` gains POST /v0/beads/issues:delete, spec first. The dependents guard,
the all-or-nothing id resolution and the reference rewrite are the role's, so
the handler is argument validation and a projection. `cascade` and `force`
default false, which is the guarded mode; DeleteIssuesResult is not
x-go-type-pinned for the reason SweepResult is not, and a reflection test keeps
the projection honest. No page envelope, so nothing to pin in pinning.go.
USER-VISIBLE, and the CHANGELOG says so loudly: a team-server script relying on
the implicit cascade starts leaving orphans; `bd delete <one-id>` without
--force now refuses over an outside dependent, converging the direct route's
three paths onto the answer two of them already gave; the proxied `--json`
result gains `orphaned_issues`. Reasoning and the alternatives are in
/data/tmp/sd-server/AMBIGUITIES.md as A-del-1..6.
Verified: build, vet, gofmt clean; golangci-lint 0 issues; make api-check
(regenerate-and-diff plus the httpapi suite) green; TestDeleterContract 11/11 at
dolt, embeddeddolt and uow; the internal/workapi delete unit tests; the
internal/storage, internal/telemetry and root layering pins;
TestConformance/Audit/issue-lifecycle and the dolt TestDeleteIssues* family
(this commit refactors DeleteIssuesInTx's guard); TestEmbeddedDelete,
TestEmbeddedDeleteJSONDependencyErrorContract, TestBurnWisps,
TestWispGCProtectsActiveWisps and TestCLI_WispGCExcludeType under
BEADS_TEST_EMBEDDED_DOLT=1; TestProxiedServerDelete{,ForceOrphans,Wisp,
Concurrent}, TestProxiedServerMolBurn and TestProxiedServerWispGC under
BEADS_TEST_PROXIED_SERVER=1; and TestProxiedLookup*.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
350 lines
12 KiB
Go
350 lines
12 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
// bdDelete runs "bd delete" with the given args and returns stdout.
|
|
// Retries on flock contention.
|
|
func bdDelete(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"delete"}, args...)
|
|
out, err := bdRunWithFlockRetry(t, bd, dir, fullArgs...)
|
|
if err != nil {
|
|
t.Fatalf("bd delete %s failed: %v\n%s", strings.Join(args, " "), err, out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// bdDeleteFail runs "bd delete" expecting failure.
|
|
func bdDeleteFail(t *testing.T, bd, dir string, args ...string) string {
|
|
t.Helper()
|
|
fullArgs := append([]string{"delete"}, args...)
|
|
cmd := exec.Command(bd, fullArgs...)
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err == nil {
|
|
t.Fatalf("expected bd delete %s to fail, but it succeeded:\n%s", strings.Join(args, " "), out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// bdShowFail runs "bd show" expecting failure (e.g., deleted issue).
|
|
func bdShowFail(t *testing.T, bd, dir, id string) string {
|
|
t.Helper()
|
|
cmd := exec.Command(bd, "show", id, "--json")
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
out, err := cmd.CombinedOutput()
|
|
if err == nil {
|
|
t.Fatalf("expected bd show %s to fail (deleted), but succeeded:\n%s", id, out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
func TestEmbeddedDelete(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
dir, _, _ := bdInit(t, bd, "--prefix", "td")
|
|
|
|
t.Run("delete_single_issue", func(t *testing.T) {
|
|
issue := bdCreate(t, bd, dir, "Delete me", "--type", "task")
|
|
bdDelete(t, bd, dir, issue.ID, "--force")
|
|
bdShowFail(t, bd, dir, issue.ID)
|
|
})
|
|
|
|
t.Run("delete_cleans_up_dependencies", func(t *testing.T) {
|
|
parent := bdCreate(t, bd, dir, "Parent", "--type", "task")
|
|
child := bdCreate(t, bd, dir, "Child", "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
// Delete child; parent should survive.
|
|
bdDelete(t, bd, dir, child.ID, "--force")
|
|
bdShowFail(t, bd, dir, child.ID)
|
|
got := bdShow(t, bd, dir, parent.ID)
|
|
if got.Status == types.StatusClosed {
|
|
t.Error("expected parent to still be open")
|
|
}
|
|
})
|
|
|
|
t.Run("delete_without_force_shows_preview", func(t *testing.T) {
|
|
target := bdCreate(t, bd, dir, "Lonely", "--type", "task")
|
|
|
|
// Without --force, bd delete shows a preview (exits 0) but does not delete.
|
|
out := bdDelete(t, bd, dir, target.ID)
|
|
if !strings.Contains(out, "PREVIEW") && !strings.Contains(out, "preview") {
|
|
t.Logf("expected preview output: %s", out)
|
|
}
|
|
got := bdShow(t, bd, dir, target.ID)
|
|
if got.ID != target.ID {
|
|
t.Errorf("expected the target to still exist after preview")
|
|
}
|
|
})
|
|
|
|
// The one-id preview over a bead with an OUTSIDE dependent now refuses
|
|
// rather than exiting 0, which is a change and is the direct route
|
|
// converging with itself: the batch path and `--dry-run` have always
|
|
// refused here (see TestEmbeddedDeleteJSONDependencyErrorContract, which
|
|
// runs both), and only the unconfirmed single-id preview did not. The
|
|
// refusal is the role's, so both routes and all three of this command's
|
|
// paths now give the same answer to the same question.
|
|
t.Run("delete_without_force_refuses_over_an_outside_dependent", func(t *testing.T) {
|
|
parent := bdCreate(t, bd, dir, "Parent strict", "--type", "task")
|
|
child := bdCreate(t, bd, dir, "Child strict", "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
out := bdDeleteFail(t, bd, dir, parent.ID)
|
|
if !strings.Contains(out, "dependents not in deletion set") {
|
|
t.Errorf("refusal did not name the guard: %s", out)
|
|
}
|
|
if !strings.Contains(out, "--cascade") || !strings.Contains(out, "--force") {
|
|
t.Errorf("refusal did not say what to send instead: %s", out)
|
|
}
|
|
// Nothing was deleted, on either end of the edge.
|
|
if got := bdShow(t, bd, dir, parent.ID); got.ID != parent.ID {
|
|
t.Errorf("expected the parent to survive a refused delete")
|
|
}
|
|
if got := bdShow(t, bd, dir, child.ID); got.ID != child.ID {
|
|
t.Errorf("expected the dependent to survive a refused delete")
|
|
}
|
|
})
|
|
|
|
t.Run("delete_single_quiet_forced_dry_run_is_payload_blind", func(t *testing.T) {
|
|
titleMarker := "EMBEDDED_QUIET_TITLE_MARKER"
|
|
descriptionMarker := "EMBEDDED_QUIET_DESCRIPTION_MARKER"
|
|
notesMarker := "EMBEDDED_QUIET_NOTES_MARKER"
|
|
payloadMarker := "EMBEDDED_QUIET_PAYLOAD_MARKER"
|
|
parent := bdCreate(t, bd, dir, titleMarker,
|
|
"--type", "task",
|
|
"--description", descriptionMarker,
|
|
"--notes", notesMarker,
|
|
"--metadata", `{"marker":"`+payloadMarker+`"}`)
|
|
child := bdCreate(t, bd, dir, "Embedded quiet dependent", "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
cmd := exec.Command(bd, "delete", parent.ID, "--force", "--dry-run", "--quiet")
|
|
cmd.Dir = dir
|
|
cmd.Env = bdEnv(dir)
|
|
stdout, stderr, err := runCommandBuffers(t, cmd)
|
|
if err != nil {
|
|
t.Fatalf("single forced quiet dry-run failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
|
|
}
|
|
if stdout.Len() != 0 {
|
|
t.Fatalf("quiet dry-run produced stdout: %s", stdout.String())
|
|
}
|
|
combined := stdout.String() + stderr.String()
|
|
for _, marker := range []string{titleMarker, descriptionMarker, notesMarker, payloadMarker} {
|
|
if strings.Contains(combined, marker) {
|
|
t.Fatalf("quiet dry-run leaked %q: %s", marker, combined)
|
|
}
|
|
}
|
|
if got := bdShowDetails(t, bd, dir, parent.ID); got["id"] != parent.ID {
|
|
t.Fatalf("forced dry-run removed parent: got %v, want %q", got["id"], parent.ID)
|
|
}
|
|
if got := bdShowDetails(t, bd, dir, child.ID); got["id"] != child.ID {
|
|
t.Fatalf("forced dry-run removed dependent: got %v, want %q", got["id"], child.ID)
|
|
}
|
|
})
|
|
|
|
t.Run("delete_force_orphans_dependents", func(t *testing.T) {
|
|
parent := bdCreate(t, bd, dir, "Force parent", "--type", "task")
|
|
child := bdCreate(t, bd, dir, "Force child", "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
bdDelete(t, bd, dir, parent.ID, "--force")
|
|
bdShowFail(t, bd, dir, parent.ID)
|
|
// Child should still exist (orphaned).
|
|
got := bdShow(t, bd, dir, child.ID)
|
|
if got.ID != child.ID {
|
|
t.Errorf("expected orphaned child to survive, got %s", got.ID)
|
|
}
|
|
})
|
|
|
|
t.Run("delete_batch", func(t *testing.T) {
|
|
issue1 := bdCreate(t, bd, dir, "Batch 1", "--type", "task")
|
|
issue2 := bdCreate(t, bd, dir, "Batch 2", "--type", "task")
|
|
issue3 := bdCreate(t, bd, dir, "Batch 3", "--type", "task")
|
|
|
|
bdDelete(t, bd, dir, issue1.ID, issue2.ID, issue3.ID, "--force")
|
|
bdShowFail(t, bd, dir, issue1.ID)
|
|
bdShowFail(t, bd, dir, issue2.ID)
|
|
bdShowFail(t, bd, dir, issue3.ID)
|
|
})
|
|
|
|
t.Run("delete_nonexistent", func(t *testing.T) {
|
|
bdDeleteFail(t, bd, dir, "td-nonexistent999", "--force")
|
|
})
|
|
}
|
|
|
|
func TestEmbeddedDeleteJSONDependencyErrorContract(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "jde")
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
batch bool
|
|
}{
|
|
{name: "single"},
|
|
{name: "batch", batch: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
titleMarker := "EMBEDDED_JSON_ERROR_TITLE_" + tc.name
|
|
descriptionMarker := "EMBEDDED_JSON_ERROR_DESCRIPTION_" + tc.name
|
|
notesMarker := "EMBEDDED_JSON_ERROR_NOTES_" + tc.name
|
|
metadataMarker := "EMBEDDED_JSON_ERROR_METADATA_" + tc.name
|
|
parent := bdCreate(t, bd, dir, titleMarker,
|
|
"--type", "task",
|
|
"--description", descriptionMarker,
|
|
"--notes", notesMarker,
|
|
"--metadata", `{"marker":"`+metadataMarker+`"}`)
|
|
child := bdCreate(t, bd, dir, "JSON error dependent "+tc.name, "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
issueIDs := []string{parent.ID}
|
|
if tc.batch {
|
|
other := bdCreate(t, bd, dir, "JSON error batch companion", "--type", "task")
|
|
issueIDs = append(issueIDs, other.ID)
|
|
}
|
|
|
|
runDelete := func(flags ...string) (string, string, error) {
|
|
args := append([]string{"delete"}, issueIDs...)
|
|
args = append(args, flags...)
|
|
cmd := exec.Command(bd, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(bdEnv(dir), "BD_JSON_ENVELOPE=0")
|
|
stdout, stderr, err := runCommandBuffers(t, cmd)
|
|
return stdout.String(), stderr.String(), err
|
|
}
|
|
|
|
stdout, stderr, err := runDelete("--dry-run", "--json")
|
|
if err == nil {
|
|
t.Fatalf("unforced dependency-blocked JSON dry-run succeeded\nstdout:\n%s\nstderr:\n%s", stdout, stderr)
|
|
}
|
|
var preview map[string]interface{}
|
|
if err := json.Unmarshal([]byte(stdout), &preview); err != nil {
|
|
t.Fatalf("unforced preview stdout is not exactly one JSON object: %v\nstdout:\n%s", err, stdout)
|
|
}
|
|
if preview["preview"] != true || preview["dry_run"] != true {
|
|
t.Fatalf("unforced preview missing structural fields: %#v", preview)
|
|
}
|
|
if _, ok := preview["error"].(string); !ok {
|
|
t.Fatalf("unforced preview missing dependency error: %#v", preview)
|
|
}
|
|
var jsonErr map[string]interface{}
|
|
if err := json.Unmarshal([]byte(stderr), &jsonErr); err != nil {
|
|
t.Fatalf("unforced stderr is not exactly one JSON error object: %v\nstderr:\n%s", err, stderr)
|
|
}
|
|
if _, ok := jsonErr["error"].(string); !ok {
|
|
t.Fatalf("unforced stderr missing error field: %#v", jsonErr)
|
|
}
|
|
if strings.Contains(stderr, "Error:") {
|
|
t.Fatalf("unforced stderr contains plaintext error prefix: %s", stderr)
|
|
}
|
|
for _, marker := range []string{titleMarker, descriptionMarker, notesMarker, metadataMarker} {
|
|
if strings.Contains(stdout+stderr, marker) {
|
|
t.Fatalf("unforced JSON dry-run leaked %q", marker)
|
|
}
|
|
}
|
|
|
|
stdout, stderr, err = runDelete("--force", "--dry-run", "--quiet", "--json")
|
|
if err != nil {
|
|
t.Fatalf("forced quiet JSON dry-run failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
|
|
}
|
|
if strings.TrimSpace(stderr) != "" {
|
|
t.Fatalf("forced quiet JSON dry-run wrote stderr: %s", stderr)
|
|
}
|
|
var forcedPreview map[string]interface{}
|
|
if err := json.Unmarshal([]byte(stdout), &forcedPreview); err != nil {
|
|
t.Fatalf("forced quiet JSON stdout is not exactly one JSON object: %v\nstdout:\n%s", err, stdout)
|
|
}
|
|
for _, key := range []string{"preview", "dry_run", "would_delete", "would_orphan"} {
|
|
if _, ok := forcedPreview[key]; !ok {
|
|
t.Fatalf("forced quiet JSON preview missing %q: %#v", key, forcedPreview)
|
|
}
|
|
}
|
|
for _, marker := range []string{titleMarker, descriptionMarker, notesMarker, metadataMarker} {
|
|
if strings.Contains(stdout, marker) {
|
|
t.Fatalf("forced quiet JSON dry-run leaked %q", marker)
|
|
}
|
|
}
|
|
|
|
if got := bdShowDetails(t, bd, dir, parent.ID); got["id"] != parent.ID {
|
|
t.Fatalf("dry-run removed parent: got %v, want %q", got["id"], parent.ID)
|
|
}
|
|
if got := bdShowDetails(t, bd, dir, child.ID); got["id"] != child.ID {
|
|
t.Fatalf("dry-run removed dependent: got %v, want %q", got["id"], child.ID)
|
|
}
|
|
assertDepExists(t, beadsDir, "jde", child.ID, parent.ID)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEmbeddedGetDependencies(t *testing.T) {
|
|
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
|
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
|
|
}
|
|
t.Parallel()
|
|
|
|
bd := buildEmbeddedBD(t)
|
|
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "gd")
|
|
|
|
parent := bdCreate(t, bd, dir, "Parent", "--type", "task")
|
|
child := bdCreate(t, bd, dir, "Child", "--type", "task")
|
|
bdDepAdd(t, bd, dir, child.ID, parent.ID)
|
|
|
|
store := openStore(t, beadsDir, "gd")
|
|
|
|
t.Run("get_dependencies", func(t *testing.T) {
|
|
deps, err := store.GetDependencies(t.Context(), child.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetDependencies: %v", err)
|
|
}
|
|
if len(deps) != 1 {
|
|
t.Fatalf("expected 1 dependency, got %d", len(deps))
|
|
}
|
|
if deps[0].ID != parent.ID {
|
|
t.Errorf("expected dependency on %s, got %s", parent.ID, deps[0].ID)
|
|
}
|
|
})
|
|
|
|
t.Run("get_dependents", func(t *testing.T) {
|
|
deps, err := store.GetDependents(t.Context(), parent.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetDependents: %v", err)
|
|
}
|
|
if len(deps) != 1 {
|
|
t.Fatalf("expected 1 dependent, got %d", len(deps))
|
|
}
|
|
if deps[0].ID != child.ID {
|
|
t.Errorf("expected dependent %s, got %s", child.ID, deps[0].ID)
|
|
}
|
|
})
|
|
|
|
t.Run("get_dependencies_empty", func(t *testing.T) {
|
|
deps, err := store.GetDependencies(t.Context(), parent.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetDependencies: %v", err)
|
|
}
|
|
if len(deps) != 0 {
|
|
t.Errorf("expected 0 dependencies for parent, got %d", len(deps))
|
|
}
|
|
})
|
|
}
|