beads/cmd/bd/mol_bond_ref_attach_test.go
Harry Miller 11fa7f5852
fix(mol): a dispatch hint for a command that does not exist, and a --ref bond that deadlocks (#5759)
* fix(mol): print a real dispatch command in mol ready --gated

The gate-ready listing ended with "bd sling <agent> --mol <molecule-id>".
No bd binary has ever had a sling command: sling belongs to the gastown
orchestrator (gt sling), and the line was a hardcoded Println with no
implementation behind it. Following the hint gives "unknown command".

Print the bd-level action instead. The listing already knows each
molecule's ready step, so the hint names that step's ID and the assign
command that actually dispatches it; agents pick work up by claiming a
step, which is what puts a molecule in the hooked status this command
filters on.

The regression test resolves the printed command against the root command
tree, so a future hint cannot name a command that does not exist.

* fix(mol): stop a --ref arm from deadlocking the molecule it bonds into

bd mol bond <proto> <mol> --ref <name> gave the spawned arm two
relationships to mol at once: ParentID nested it under mol, and
AttachToID added a blocking dependency ON mol. Those cannot both be
satisfied. The arm waits for mol to close; mol cannot close while it
holds an open arm child. bd ready empties out with nothing naming the
cause, and the only escape is closing the parent by hand.

The documented Christmas Ornament example is exactly this shape --
"bd mol bond mol-worker-arm bd-patrol --ref arm-{{worker_name}}" takes
the default sequential bond type, so the primary use of --ref deadlocked.

Nesting already attaches the arm, and its dotted ID records it, so the
blocking edge is dropped for a nested arm rather than pointed somewhere
else. A bond without --ref is untouched: that arm is a sibling, mol
closes, and the arm then unblocks, which is satisfiable and stays. A
parallel bond never blocked and is likewise unchanged.

* fix(mol bond): refuse --ref with --type conditional instead of inverting it

Dropping the attachment edge for a nested --ref arm traded a deadlock for
lost ordering, which is defensible for a sequential bond but not for a
conditional one. DepConditionalBlocks means "B runs only if A fails"
(internal/types/types.go), so dropping it does not lose ordering - it
inverts the meaning, turning an arm that should almost never run into one
that always runs. That is a silent false dispatch, strictly worse than the
visible deadlock it replaced.

refuseConditionalRefArm now rejects the combination, consulted both from
gatherMolBondInput (so --dry-run and the proxied route refuse identically,
rather than previewing a bond the real command rejects) and from
buildAttachCloneOpts (so a caller that skips flag validation cannot
construct the arm either). The drop in buildAttachCloneOpts narrows to
DepBlocks, so a future bond type reaching that point is not silently
stripped of its edge as well.

The sequential case keeps the drop, and the loss is now stated rather than
implied: bond's long help says a --ref arm carries no ordering and is ready
as soon as it is spawned, and the CHANGELOG entry says the same. The
committed docs/cli-reference/mol.md is generated from the tag in
docs/cli-docs.pin, so it picks the wording up at the next release
regeneration - hand-editing it fails the drift gate.

Also from review: the parallel arm's redundant-but-satisfiable
DepParentChild edge is now explained rather than merely test-pinned, and
`bd mol ready --gated` says which molecule its dispatch example belongs to
when more than one is listed. registerMolBondFlags is extracted so a test
can build an isolated command instead of mutating the package-level one.

Reported-by: bee-ghosttrack
Agent-Signature: claude-code-opus-5-unknown-reasoning on behalf of Harry Miller

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* docs(changelog): move this PR's entry under [Unreleased]

The 1.3.0 cut renamed the old [Unreleased] block, so the entry this branch
added there now merges cleanly into the released [1.3.0] section (#6643).

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore(bazel): enumerate the PR's two new cmd/bd test files in go_test srcs

gazelle-generated. Both files are untagged (no //go:build), so the
bazel go_test target owes their enumeration; the in-package census showed
the gap was exactly these two and zero pre-existing untagged gaps.
Note make bazel-sync-check exits 0 on the un-synced head, so only the
literal make bazel-sync surfaces this.

* fix(mol): correct the --ref help, the refusal message and the hint header

Review iteration 1 on PR #5759. All four synthesis findings are minor or nit
with no gating impact (scorecard 967/850, Required Changes: None); taken
because a maintainer commit already exists on this branch (d66096104), so the
push is owed either way.

Finding 1 (minor) - the new --ref help paragraph asserted "B is nested inside
A", which is inverted for the proto+mol order that every --ref example in the
same help text uses: for aIsProto && !bIsProto the dispatch passes issueB as
mol, so ParentID is B. Rephrased by role, matching the CHANGELOG's own neutral
wording, and de-positionalized the parenthetical in the same paragraph, which
named A as the container too.

Finding 2 (minor) - the conditional + --ref refusal claimed "a nested arm
cannot be ordered against the molecule that contains it" from
gatherMolBondInput, which runs before operands are resolved and so also
refuses mol+mol and proto+proto, where --ref is inert and nothing nests.
Reworded to state the nesting rationale as what --ref does where it applies
rather than as a claim about the bond in front of the user.

Finding 3 (nit) - the ordering-trade comment claimed docs/cli-reference/mol.md
already documents the trade. It does not: that page is generated from the
release named in docs/cli-docs.pin (v1.2.2), whose source has zero hits for
"nested inside". Softened to name the long help as the documented place and
mol.md as where it will appear once the pin advances. mol.md is deliberately
untouched - hand-writing it would diverge from the pinned generator output and
red a currently-green required check.

Finding 4 (nit) - "for the first one above" could name a different molecule
than the step ID printed beneath it, whenever the first listed molecule has no
ready step and a later one does. Both offered arms miss that case (indexing
element 0 and dropping the fallback nil-derefs; suppressing the line only when
NO molecule has a step leaves the disagreement intact), so this implements the
stated intent instead: firstReadyStepID also returns the listing position and
the header names it.

Deviations, with the measurements behind them:

- Finding 2's "leave the nesting rationale on the attach-path error" arm was
  not taken as a message split. gatherMolBondInput runs at mol_bond.go:104,
  before the usesProxiedServer() branch, and buildAttachCloneOpts is reached
  only via bondProtoMolWithSubgraph/bondProtoMolAttachInto downstream of it, so
  the attach-path string is unreachable from every CLI route; split that way
  the corrected wording is all any user would see and the rationale would be
  dead text. One shared, shape-agnostic message keeps both true. Verified from
  the CLI: `mol bond no-such-a no-such-b --type conditional --ref arm-ace`
  refuses (rc 1), while the same nonexistent operands without --type
  conditional reach operand resolution and fail there.
- Finding 2's alternative arm (scope the gather-time refusal to the attach
  shapes) would falsify this PR's own CHANGELOG entry, which states the
  combination is "refused outright (including under --dry-run and on the
  proxied route)".
- No mol+mol refusal test was added: gatherMolBondInput never resolves
  operands, so mol+mol and proto+mol are the same input to it and such a test
  would be vacuous.
- Artifact sweep for the inverted claim came back empty: CHANGELOG.md and the
  PR body already describe the nesting by role ("nested inside its target"), so
  the A/B inversion existed only in the help paragraph.

Tests: the PR's 9 tests plus the new one pass by name (go test ./cmd/bd/ -run
'TestBuildAttachCloneOpts|TestGatherMolBondInput|TestGatedReadyHint' = 10 PASS
/ 0 FAIL; the full cmd/bd package does not finish). Mutation proof that the new
pin is not a duplicate: hardcoding the header position to 1 - the pre-fix
meaning - reds only
TestGatedReadyHintHeaderNamesTheEntryItPrinted/example_comes_from_a_later_molecule
while its sibling subtest and all 9 existing tests stay green. go build ./...
and go vet ./cmd/bd/ exit 0.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-09-29 17:05:18 -07:00

136 lines
5.1 KiB
Go

package main
import (
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/types"
)
func bondSubgraph() *TemplateSubgraph {
root := &types.Issue{ID: "proto-arm", Title: "arm"}
return &TemplateSubgraph{
Root: root,
Issues: []*types.Issue{root},
IssueMap: map[string]*types.Issue{root.ID: root},
}
}
// A --ref arm is nested under the molecule, so it must not also be blocked by
// it: the arm would wait for the molecule to close while the molecule waits for
// its own open arm, and bd ready goes empty with nothing to point at.
func TestBuildAttachCloneOptsRefArmIsNotBlockedByItsParent(t *testing.T) {
mol := &types.Issue{ID: "bd-patrol", Title: "patrol"}
opts, err := buildAttachCloneOpts(bondSubgraph(), mol, types.BondTypeSequential, nil, "arm-ace", "tester", false, false)
if err != nil {
t.Fatalf("buildAttachCloneOpts() error = %v", err)
}
if opts.AttachToID != "" {
t.Errorf("AttachToID = %q, want no blocking attachment for a nested arm", opts.AttachToID)
}
if opts.ParentID != mol.ID {
t.Errorf("ParentID = %q, want %q", opts.ParentID, mol.ID)
}
if opts.ChildRef != "arm-ace" {
t.Errorf("ChildRef = %q, want %q", opts.ChildRef, "arm-ace")
}
}
// A conditional --ref arm is refused rather than degraded. Dropping a
// "conditional-blocks" edge does not lose ordering, it inverts the meaning:
// the edge says "run only if the molecule fails", so an arm without it runs
// unconditionally. A silent false dispatch is worse than the deadlock the
// sequential case trades away, and there is no safe reading of the two flags
// together, so bd refuses the combination instead of guessing.
func TestBuildAttachCloneOptsConditionalRefArmIsRefused(t *testing.T) {
mol := &types.Issue{ID: "bd-patrol", Title: "patrol"}
_, err := buildAttachCloneOpts(bondSubgraph(), mol, types.BondTypeConditional, nil, "arm-ace", "tester", false, false)
if err == nil {
t.Fatal("buildAttachCloneOpts() error = nil, want --ref + --type conditional to be refused")
}
if !strings.Contains(err.Error(), "--ref cannot be combined with --type conditional") {
t.Errorf("error = %q, want it to name the refused flag combination", err.Error())
}
}
// A conditional bond WITHOUT --ref keeps its edge: a sibling arm can wait on
// the molecule, so there is nothing unsatisfiable to work around.
func TestBuildAttachCloneOptsConditionalSiblingKeepsItsEdge(t *testing.T) {
mol := &types.Issue{ID: "bd-patrol", Title: "patrol"}
opts, err := buildAttachCloneOpts(bondSubgraph(), mol, types.BondTypeConditional, nil, "", "tester", false, false)
if err != nil {
t.Fatalf("buildAttachCloneOpts() error = %v", err)
}
if opts.AttachToID != mol.ID {
t.Errorf("AttachToID = %q, want %q", opts.AttachToID, mol.ID)
}
if opts.AttachDepType != types.DepConditionalBlocks {
t.Errorf("AttachDepType = %q, want %q", opts.AttachDepType, types.DepConditionalBlocks)
}
}
// The refusal also has to fire before either bond route runs, so --dry-run
// and the proxied path refuse identically rather than previewing a bond the
// real command rejects.
func TestGatherMolBondInputRefusesConditionalRefArm(t *testing.T) {
cmd := &cobra.Command{Use: "bond"}
registerMolBondFlags(cmd)
if err := cmd.Flags().Set("type", types.BondTypeConditional); err != nil {
t.Fatalf("set --type: %v", err)
}
if err := cmd.Flags().Set("ref", "arm-ace"); err != nil {
t.Fatalf("set --ref: %v", err)
}
_, err := gatherMolBondInput(cmd, []string{"mol-arm", "bd-patrol"})
if err == nil {
t.Fatal("gatherMolBondInput() error = nil, want --ref + --type conditional to be refused")
}
if !strings.Contains(err.Error(), "--ref cannot be combined with --type conditional") {
t.Errorf("error = %q, want it to name the refused flag combination", err.Error())
}
}
// Without --ref the arm is a sibling, which is satisfiable: the molecule closes
// and then the arm unblocks. That attachment must survive.
func TestBuildAttachCloneOptsSiblingKeepsBlockingAttachment(t *testing.T) {
mol := &types.Issue{ID: "bd-patrol", Title: "patrol"}
opts, err := buildAttachCloneOpts(bondSubgraph(), mol, types.BondTypeSequential, nil, "", "tester", false, false)
if err != nil {
t.Fatalf("buildAttachCloneOpts() error = %v", err)
}
if opts.AttachToID != mol.ID {
t.Errorf("AttachToID = %q, want %q", opts.AttachToID, mol.ID)
}
if opts.AttachDepType != types.DepBlocks {
t.Errorf("AttachDepType = %q, want %q", opts.AttachDepType, types.DepBlocks)
}
if opts.ParentID != "" {
t.Errorf("ParentID = %q, want empty without --ref", opts.ParentID)
}
}
// A parallel bond never blocked, so nesting changes nothing about it.
func TestBuildAttachCloneOptsParallelRefArmKeepsAttachment(t *testing.T) {
mol := &types.Issue{ID: "bd-patrol", Title: "patrol"}
opts, err := buildAttachCloneOpts(bondSubgraph(), mol, types.BondTypeParallel, nil, "arm-ace", "tester", false, false)
if err != nil {
t.Fatalf("buildAttachCloneOpts() error = %v", err)
}
if opts.AttachToID != mol.ID {
t.Errorf("AttachToID = %q, want %q", opts.AttachToID, mol.ID)
}
if opts.AttachDepType != types.DepParentChild {
t.Errorf("AttachDepType = %q, want %q", opts.AttachDepType, types.DepParentChild)
}
}