beads/cmd/bd/assign_fence_proxied_test.go
Steve Yegge 13d2c8d6ba
fix(update): refuse overwriting a live foreign claim without --force (bd-98s5c) (#5066)
* fix(update): refuse overwriting a live foreign claim without --force (bd-98s5c)

Plain 'bd update -a <new>' and its shorthand 'bd assign' silently
overwrote another actor's live in_progress claim — rc=0, no warning, no
audit trail. It was the last unfenced cross-actor takeover path in the
CLI: --claim, unclaim, and close all already refuse without --force, so
an agent that had correctly learned not to steamroll still had one verb
that would do it without complaint (originating collisions: two Sol
cross-actor steamrolls, wyvern wy-8uaqw).

The fence is a new validation.AssigneeNotStolen validator wired at the
cmd layer (the exact sibling of the be-035 close fence), covering all
four surfaces: direct and proxied-server update -a, direct and proxied
assign. It refuses only the silent steamroll — every predicate clause is
load-bearing:

- assignee != ""          unassigned beads stay freely assignable
- assignee != actor       self-edits untouched
- assignee != newAssignee idempotent re-assert stays a success (replay)
- status == in_progress   open-bead dispatch stays frictionless
- holder not a claim.pools alias — the same carve-out --claim has, via a
  new shared issueops.ParseClaimPools, so the two verbs can never
  disagree on which holders are pools

Unassigning (-a "") a live foreign claim is fenced the same way — it is
unclaim in disguise. The fence SKIPS under an explicit --if-assignee
guard: that CAS names the holder (nothing silent), and it is how the
bd-wsqvw park transition works, since --force and --if-assignee are
mutually exclusive (mirroring unclaim). A refusal is a policy verdict:
exit 1, never 13 — coordinating is the fix, not retrying. The copy
points at coordination and names --force explicitly; it never teaches
the unclaim steamroller (wy-zs5s2 item 2).

Downstream: wyvern wh-take.sh's steal arm adopts --force (wy-er4ch);
its queue arm can register fable-crew in claim.pools or use
--if-assignee. bd batch remains a raw-transaction surface that bypasses
cmd-layer fences (pre-existing; noted on the bead).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EF8QbCbF6JnTuoCKvu9xiG

* fix(update): let the claim CAS keep its canonical conflict copy under --claim -a

CI caught TestProxiedServerUpdate/claim_then_other_user_conflicts: with
--claim and -a combined, the reassign fence fired on the pre-claim
snapshot and replaced the claim CAS's "already claimed" refusal with
the reassign copy. The claim CAS is itself the anti-steal gate — a
foreign live claim fails it before any field update runs, and an
assignee edit that rides a WON claim only ever touches the actor's own
fresh claim — so the fence now defers to it (skips when claimFlag is
set), on both the direct and proxied paths. Existing automation keys on
the canonical message; a new embedded subtest fences the contract.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EF8QbCbF6JnTuoCKvu9xiG

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 15:10:12 -07:00

87 lines
3.6 KiB
Go

//go:build cgo
package main
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestProxiedServerReassignFence proves the bd-98s5c live-claim reassign
// fence holds on the proxied-server path — the topology where cross-actor
// collisions actually happen, since every shared-dolt-server clone writes
// through it. A silent steamroll refuses with exit 1 (policy refusal, never
// ExitGuardMismatch), --force overrides, and the idempotent re-assert stays a
// success for retry/replay safety.
func TestProxiedServerReassignFence(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("update_refuses_then_force_overrides", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "rfa")
issue := bdProxiedCreate(t, bd, p.dir, "Held via proxy")
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--actor", "holder", "--assignee", "holder", "--status", "in_progress")
out, code := bdProxiedUpdateFailCode(t, bd, p.dir, issue.ID, "--actor", "thief", "--assignee", "thief")
if code != 1 {
t.Errorf("fence refusal exit code = %d, want 1 (policy refusal, not %d/guard-mismatch)\n%s", code, ExitGuardMismatch, out)
}
for _, frag := range []string{"held by", "holder", "--force"} {
if !strings.Contains(out, frag) {
t.Errorf("refusal should contain %q, got:\n%s", frag, out)
}
}
got := bdProxiedShow(t, bd, p.dir, issue.ID)
if got.Assignee != "holder" || got.Status != types.StatusInProgress {
t.Errorf("refused reassign clobbered the row: assignee=%q status=%s, want holder/in_progress", got.Assignee, got.Status)
}
// Idempotent re-assert of the current holder is a success (replay
// safety on the retrying proxied path).
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--actor", "replayer", "--assignee", "holder")
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--actor", "thief", "--assignee", "thief", "--force")
if got := bdProxiedShow(t, bd, p.dir, issue.ID); got.Assignee != "thief" {
t.Errorf("--force reassign did not apply: assignee=%q, want thief", got.Assignee)
}
})
t.Run("assign_refuses_then_force_overrides", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "rfb")
issue := bdProxiedCreate(t, bd, p.dir, "Assigned via proxy")
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--actor", "holder", "--assignee", "holder", "--status", "in_progress")
out, err := bdProxiedRun(t, bd, p.dir, "assign", issue.ID, "thief", "--actor", "thief")
if err == nil {
t.Fatalf("proxied assign over a live foreign claim should fail, got:\n%s", out)
}
if !strings.Contains(string(out), "holder") {
t.Errorf("assign refusal should name the holder, got:\n%s", out)
}
if got := bdProxiedShow(t, bd, p.dir, issue.ID); got.Assignee != "holder" {
t.Errorf("refused assign clobbered the row: assignee=%q, want holder", got.Assignee)
}
if out, err := bdProxiedRun(t, bd, p.dir, "assign", issue.ID, "thief", "--actor", "thief", "--force"); err != nil {
t.Fatalf("assign --force failed: %v\n%s", err, out)
}
if got := bdProxiedShow(t, bd, p.dir, issue.ID); got.Assignee != "thief" {
t.Errorf("assign --force did not apply: assignee=%q, want thief", got.Assignee)
}
})
t.Run("open_bead_dispatch_stays_frictionless", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "rfc")
issue := bdProxiedCreate(t, bd, p.dir, "Open dispatch via proxy", "--assignee", "alice")
bdProxiedUpdateOne(t, bd, p.dir, issue.ID, "--actor", "dispatcher", "--assignee", "bob")
if got := bdProxiedShow(t, bd, p.dir, issue.ID); got.Assignee != "bob" {
t.Errorf("open-bead reassign should not be fenced: assignee=%q, want bob", got.Assignee)
}
})
}