* feat(roles): BatchGetter role and issues:batchGet route (S7)
Adds BatchGetter.GetMany(ids) -> {Issues, Missing}, a one-snapshot,
order-preserving, deduped, 1000-id-capped batch read, wired through all
three library legs (dolt, embedded dolt, uow) behind the existing
hook/telemetry decorator chain, plus the POST /v0/beads/issues:batchGet
HTTP route (spec, apigen, wire-shape golden, issues.batchGet capability
token). Conformance contract (RunBatchGetter*, 12 cases) passes on all
three legs against real backends. No HTTP client leg yet (waits on the
generator / PR #7247); left as a documented seam on BatchGetter's doc
comment.
CLI GetIssuesByIDs call sites are left unmigrated this slice and marked
with TODO(batchgetter) comments: every call site reads an unbounded id
list today, and GetMany's 1000-id cap has no chunking/fallback in any
caller yet, so swapping them in now would add a new, unhandled failure
mode to already-shipped CLI paths (export, import, molecule port,
ready-explain, wisp sweep).
Verified via three live-mutate guards against
internal/storage/issueops/batchget.go (disabling the cap check, dropping
the Missing append, and replacing ordered iteration with map iteration):
each turns the embedded-dolt contract red, confirming the cap, Missing
reporting, and order-preservation assertions actually catch a break.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(roles): close S7 review gaps — revision on wire, BUILD sync, handler tests
Addresses the S7 Opus review's FIX-FIRST items on BatchGetter/issues:batchGet:
HIGH
- batchGetResponse now projects apigen.BatchGetIssue (types.BatchGetIssue +
NewBatchGetIssue), carrying `revision` per issue the way IssueDetails does;
spec, apigen and the wire-shape golden regenerated (additive, no
wire_revision bump).
- make bazel-sync: the 12 files missing from BUILD.bazel srcs are back in
sync across 9 BUILD.bazel files.
- New internal/httpapi/batch_get_test.go: success incl. revision, missing
ids, empty arrays not null, unknown member refused, missing/null ids
refused, query parameter refused, 1001 ids refused with the full request
reaching the role, 1000 ids accepted.
MED
- RunBatchGetterAcceptsExactlyTheCap pins the cap boundary (> vs >=).
- RunBatchGetterHydratesLabels pins Labels survive projection.
- RunBatchGetterReadsOneSnapshot renamed to
RunBatchGetterSharesOneReadStructurally with a doc that no longer
overclaims atomicity; a real interleaving test needs a hook on the frozen
roleFixtureKit, out of scope here.
- Doc comments across ExecuteGetMany, GetManyResult.Issues and the
BatchGetter interface now say hydration is labels only.
LOW
- failBatchGetErr's doc no longer claims blank entries are "already caught".
- PR #7247 references kept.
Re-ran build/vet/gofmt/lint, make api-check, make bazel-sync(-check), the
full internal/httpapi suite, and all three conformance legs (dolt via the
pinned local server, embeddeddolt, uow) plus targeted cmd/bd serve tests —
all green. Live-mutate-guard reverified: cap >=, dropping Missing from the
response, and clearing Labels each now fail the relevant test.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
* fix(roles): sync client vocabulary and repo gates with issues:batchGet
The S7 route landed on the server without the sibling tables that hold
the rest of the tree set-equal with the server's surface, so five
tests and two manifest gates went red on the PR head:
- httpclient/wire: mirror batchGetIssues into the client vocabulary,
CLASSIFIED, not wired, the way the wire wave and #7250's
issues.count.scope landed: OpBatchGetIssues, its issues.batchGet
token on opCapability, its identity row, and the first-slice skew
dimension 35 -> 36 (TestCapabilityTableMatchesTheServerRouteTable).
- httpclient/wire: BatchGetIssue is the first golden.json schema with
a `revision` member that needs no legacy-integer tolerance — the
route postdates #6053 and types.BatchGetIssue declares Revision a
string, and a pre-#6053 server never advertises issues.batchGet, so
Preflight refuses before any body is decoded. Exempt it with that
reason in a new revisionSchemaExempt table rather than adding an
issues[] rewrite no legacy server can reach; the gate refuses an
entry that is both covered and exempt, a stale exemption, and an
empty reason.
- httpclient/encode: give the oracle's role set a BatchGetter stub —
httpapi.Listen requires the whole set, so all three oracle tests
failed to bind.
- storage/uow: exempt IssueUseCase.GetMany from the notifying-parity
partition as a read, the reason hook_batch_getter.go gives.
- shard manifest: place TestBatchGetterContract in the 15-shard
embedded storage block (generator --write, incremental).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(roles): pin the BatchGetter pure tier and the spec ids caps
Review fixes, iteration 1. The review approved with no blocker or major
finding and three minors, and this commit takes all three. It changes
tests and one comment only, and every new assertion was mutation-probed
with go test -overlay.
- storage/issueops: add batchget_test.go, the database-free tier that
FinishGetMany's doc already claimed ("pinned without a database") but
that no test supplied. It pins:
- the cap is checked before the blank scan (an over-cap request with
a blank at position 0 answers *TooManyIDsError);
- the bound is inclusive;
- dedupe keeps the first mention, does not trim, and never writes
through the caller's slice;
- Issues and Missing both follow request order;
- both slices are never nil;
- ExecuteGetMany refuses one id repeated past the cap before any
query (a sqlmock with no expectations).
Ten mutants were each killed by exactly their intended test, with the
rest of the package green: last-mention, sort, trim, in-place dedupe,
blank-before-cap, >=, nil Missing, nil Issues, dedupe-before-validate
and validate-after-query.
- httpapi: TestSpecDefaultsMatchSharedConstants now checks ids.maxItems
against the enforced bound on the two request bodies that carry an
`ids` list: batchGetIssues against issueops.MaxGetManyIDs, and
deleteIssues against maxDeleteIDs. Each body is reached through its
operation's own requestBody $ref. Setting either constant to 999 turns
only this test red. The spec's other list caps (batch
create/close/apply items, dependency edge lists) stay ungated, as
before.
- cmd/bd/serve.go: the batchGetter field comment said its accessor
"does NOT recurse through the hook decorator". HookFiringStore.
BatchGetter does recurse to the inner getter; it just doesn't wrap
the result. The comment now says that.
Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
768 lines
22 KiB
Go
768 lines
22 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
"github.com/steveyegge/beads/internal/utils"
|
|
)
|
|
|
|
// LargeMoleculeThreshold is the step count above which we show summary instead of full list.
|
|
// This prevents overwhelming output and slow queries for mega-molecules.
|
|
const LargeMoleculeThreshold = 100
|
|
|
|
// MoleculeProgress holds the progress information for a molecule
|
|
type MoleculeProgress struct {
|
|
MoleculeID string `json:"molecule_id"`
|
|
MoleculeTitle string `json:"molecule_title"`
|
|
Assignee string `json:"assignee,omitempty"`
|
|
CurrentStep *types.Issue `json:"current_step,omitempty"`
|
|
NextStep *types.Issue `json:"next_step,omitempty"`
|
|
Steps []*StepStatus `json:"steps"`
|
|
Completed int `json:"completed"`
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
// StepStatus represents the status of a step in a molecule
|
|
type StepStatus struct {
|
|
Issue *types.Issue `json:"issue"`
|
|
Status string `json:"status"` // "done", "current", "ready", "blocked", "pending"
|
|
IsCurrent bool `json:"is_current"` // true if this is the in_progress step
|
|
}
|
|
|
|
var molCurrentCmd = &cobra.Command{
|
|
Use: "current [molecule-id]",
|
|
Short: "Show current position in molecule workflow",
|
|
Long: `Show where you are in a molecule workflow.
|
|
|
|
If molecule-id is given, show status for that molecule.
|
|
If not given, infer from in_progress issues assigned to current agent.
|
|
|
|
The output shows all steps with status indicators:
|
|
[done] - Step is complete (closed)
|
|
[current] - Step is in_progress (you are here)
|
|
[ready] - Step is ready to start (unblocked)
|
|
[blocked] - Step is blocked by dependencies
|
|
[pending] - Step is waiting
|
|
|
|
For large molecules (>100 steps), a summary is shown instead.
|
|
Use --limit or --range to view specific steps:
|
|
bd mol current <id> --limit 50 # Show first 50 steps
|
|
bd mol current <id> --range 100-150 # Show steps 100-150`,
|
|
Args: cobra.MaximumNArgs(1),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
evt := metrics.NewCommandEvent("mol-current")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
forAgent, _ := cmd.Flags().GetString("for")
|
|
limit, _ := cmd.Flags().GetInt("limit")
|
|
rangeStr, _ := cmd.Flags().GetString("range")
|
|
|
|
agent := forAgent
|
|
if agent == "" {
|
|
agent = actor
|
|
}
|
|
|
|
if usesProxiedServer() {
|
|
return runMolCurrentProxiedServer(rootCtx, args, agent, limit, rangeStr)
|
|
}
|
|
|
|
ctx := rootCtx
|
|
|
|
if store == nil {
|
|
return HandleErrorRespectJSON("no database connection")
|
|
}
|
|
|
|
var rangeStart, rangeEnd int
|
|
if rangeStr != "" {
|
|
var err error
|
|
rangeStart, rangeEnd, err = parseRange(rangeStr)
|
|
if err != nil {
|
|
return HandleErrorRespectJSON("invalid range '%s': %v", rangeStr, err)
|
|
}
|
|
}
|
|
|
|
explicitSteps := limit > 0 || rangeStr != ""
|
|
|
|
var molecules []*MoleculeProgress
|
|
|
|
if len(args) == 1 {
|
|
moleculeID, err := utils.ResolvePartialID(ctx, store, args[0])
|
|
if err != nil {
|
|
return HandleErrorRespectJSON("molecule '%s' not found", args[0])
|
|
}
|
|
|
|
stats, err := store.GetMoleculeProgress(ctx, moleculeID)
|
|
if err != nil {
|
|
return HandleErrorRespectJSON("loading molecule: %v", err)
|
|
}
|
|
|
|
if stats.Total > LargeMoleculeThreshold && !explicitSteps && !jsonOutput {
|
|
printLargeMoleculeSummary(stats)
|
|
return nil
|
|
}
|
|
|
|
progress, err := getMoleculeProgress(ctx, store, moleculeID)
|
|
if err != nil {
|
|
return HandleErrorRespectJSON("loading molecule: %v", err)
|
|
}
|
|
|
|
if rangeStr != "" {
|
|
progress.Steps = filterStepsByRange(progress.Steps, rangeStart, rangeEnd)
|
|
} else if limit > 0 && len(progress.Steps) > limit {
|
|
progress.Steps = progress.Steps[:limit]
|
|
}
|
|
|
|
molecules = append(molecules, progress)
|
|
} else {
|
|
molecules = findInProgressMolecules(ctx, store, agent)
|
|
|
|
if len(molecules) == 0 {
|
|
molecules = findHookedMolecules(ctx, store, agent)
|
|
}
|
|
|
|
if len(molecules) == 0 {
|
|
if jsonOutput {
|
|
return outputJSON([]interface{}{})
|
|
}
|
|
fmt.Printf("No molecules in progress")
|
|
if agent != "" {
|
|
fmt.Printf(" for %s", agent)
|
|
}
|
|
fmt.Println(".")
|
|
fmt.Println("\nTo start work on a molecule:")
|
|
fmt.Println(" bd mol wisp create <proto-id> # Instantiate as ephemeral wisp")
|
|
fmt.Println(" bd update <step-id> --claim # Claim a step")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
if jsonOutput {
|
|
return outputJSON(molecules)
|
|
}
|
|
|
|
for i, mol := range molecules {
|
|
if i > 0 {
|
|
fmt.Println()
|
|
}
|
|
printMoleculeProgress(mol)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
// getMoleculeProgress loads a molecule and computes progress
|
|
func getMoleculeProgress(ctx context.Context, s molReader, moleculeID string) (*MoleculeProgress, error) {
|
|
subgraph, err := loadTemplateSubgraph(ctx, s, moleculeID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
progress := &MoleculeProgress{
|
|
MoleculeID: subgraph.Root.ID,
|
|
MoleculeTitle: subgraph.Root.Title,
|
|
Assignee: subgraph.Root.Assignee,
|
|
Total: len(subgraph.Issues) - 1, // Exclude root
|
|
}
|
|
|
|
// Compute step readiness from within-molecule dependencies.
|
|
// Uses analyzeMoleculeParallel instead of GetReadyWork because GetReadyWork
|
|
// excludes ephemeral issues (wisp steps are ephemeral by definition).
|
|
// See: https://github.com/steveyegge/gastown/issues/1276 (historical reference)
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
readyIDs := make(map[string]bool)
|
|
for id, info := range analysis.Steps {
|
|
if info.IsReady {
|
|
readyIDs[id] = true
|
|
}
|
|
}
|
|
|
|
// Build step status list (exclude root)
|
|
var steps []*StepStatus
|
|
for _, issue := range subgraph.Issues {
|
|
if issue.ID == subgraph.Root.ID {
|
|
continue // Skip root
|
|
}
|
|
|
|
step := &StepStatus{
|
|
Issue: issue,
|
|
}
|
|
|
|
switch issue.Status {
|
|
case types.StatusClosed:
|
|
step.Status = "done"
|
|
progress.Completed++
|
|
case types.StatusInProgress:
|
|
step.Status = "current"
|
|
step.IsCurrent = true
|
|
progress.CurrentStep = issue
|
|
case types.StatusBlocked:
|
|
step.Status = "blocked"
|
|
default:
|
|
// Check if ready (unblocked)
|
|
if readyIDs[issue.ID] {
|
|
step.Status = "ready"
|
|
if progress.NextStep == nil {
|
|
progress.NextStep = issue
|
|
}
|
|
} else {
|
|
step.Status = "pending"
|
|
}
|
|
}
|
|
|
|
steps = append(steps, step)
|
|
}
|
|
|
|
// Sort steps by dependency order
|
|
sortStepsByDependencyOrder(steps, subgraph)
|
|
progress.Steps = steps
|
|
|
|
// If no current step but there's a ready step, set it as next
|
|
if progress.CurrentStep == nil && progress.NextStep == nil {
|
|
for _, step := range steps {
|
|
if step.Status == "ready" {
|
|
progress.NextStep = step.Issue
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return progress, nil
|
|
}
|
|
|
|
// findInProgressMolecules finds molecules with in_progress steps for an agent
|
|
func findInProgressMolecules(ctx context.Context, s molReader, agent string) []*MoleculeProgress {
|
|
var inProgressIssues []*types.Issue
|
|
|
|
status := types.StatusInProgress
|
|
filter := types.IssueFilter{Status: &status}
|
|
if agent != "" {
|
|
filter.Assignee = &agent
|
|
}
|
|
allIssues, err := s.SearchIssues(ctx, "", filter)
|
|
if err == nil {
|
|
inProgressIssues = allIssues
|
|
}
|
|
|
|
if len(inProgressIssues) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// Batch-find parent molecules for all in_progress issues (bd-hn4q)
|
|
issueIDs := make([]string, len(inProgressIssues))
|
|
for i, issue := range inProgressIssues {
|
|
issueIDs[i] = issue.ID
|
|
}
|
|
moleculeRoots := findParentMolecules(ctx, s, issueIDs)
|
|
|
|
moleculeMap := make(map[string]*MoleculeProgress)
|
|
for _, issue := range inProgressIssues {
|
|
moleculeID := moleculeRoots[issue.ID]
|
|
if moleculeID == "" {
|
|
continue
|
|
}
|
|
|
|
if _, exists := moleculeMap[moleculeID]; !exists {
|
|
progress, err := getMoleculeProgress(ctx, s, moleculeID)
|
|
if err == nil {
|
|
moleculeMap[moleculeID] = progress
|
|
}
|
|
}
|
|
}
|
|
|
|
// Convert to slice
|
|
var molecules []*MoleculeProgress
|
|
for _, mol := range moleculeMap {
|
|
molecules = append(molecules, mol)
|
|
}
|
|
|
|
// Sort by molecule ID for consistent output
|
|
sort.Slice(molecules, func(i, j int) bool {
|
|
return molecules[i].MoleculeID < molecules[j].MoleculeID
|
|
})
|
|
|
|
return molecules
|
|
}
|
|
|
|
// findHookedMolecules finds molecules bonded to hooked issues for an agent.
|
|
// This is a fallback when no in_progress steps exist but a molecule is attached
|
|
// to the agent's hooked work via a "blocks" dependency.
|
|
func findHookedMolecules(ctx context.Context, s molReader, agent string) []*MoleculeProgress {
|
|
// Query for hooked issues assigned to the agent
|
|
status := types.StatusHooked
|
|
filter := types.IssueFilter{Status: &status}
|
|
if agent != "" {
|
|
filter.Assignee = &agent
|
|
}
|
|
hookedIssues, err := s.SearchIssues(ctx, "", filter)
|
|
if err != nil || len(hookedIssues) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// For each hooked issue, check if it IS a molecule or has blocks deps on one
|
|
moleculeMap := make(map[string]*MoleculeProgress)
|
|
for _, issue := range hookedIssues {
|
|
// Check if the hooked issue itself is a molecule (e.g., patrol wisps
|
|
// are directly hooked without a separate handoff bead). hq-3paz0m
|
|
if issue.IssueType == types.TypeEpic {
|
|
if _, exists := moleculeMap[issue.ID]; !exists {
|
|
progress, err := getMoleculeProgress(ctx, s, issue.ID)
|
|
if err == nil {
|
|
moleculeMap[issue.ID] = progress
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
deps, err := s.GetDependencyRecords(ctx, issue.ID)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
// Look for a blocks dependency pointing to a molecule (epic or template)
|
|
for _, dep := range deps {
|
|
if dep.Type != types.DepBlocks {
|
|
continue
|
|
}
|
|
// The issue depends on (is blocked by) dep.DependsOnID
|
|
candidate, err := s.GetIssue(ctx, dep.DependsOnID)
|
|
if err != nil || candidate == nil {
|
|
continue
|
|
}
|
|
|
|
// Check if candidate is a molecule (epic or has template label)
|
|
isMolecule := candidate.IssueType == types.TypeEpic
|
|
if !isMolecule {
|
|
for _, label := range candidate.Labels {
|
|
if label == BeadsTemplateLabel {
|
|
isMolecule = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if isMolecule {
|
|
if _, exists := moleculeMap[candidate.ID]; !exists {
|
|
progress, err := getMoleculeProgress(ctx, s, candidate.ID)
|
|
if err == nil {
|
|
moleculeMap[candidate.ID] = progress
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Convert to slice
|
|
var molecules []*MoleculeProgress
|
|
for _, mol := range moleculeMap {
|
|
molecules = append(molecules, mol)
|
|
}
|
|
|
|
// Sort by molecule ID for consistent output
|
|
sort.Slice(molecules, func(i, j int) bool {
|
|
return molecules[i].MoleculeID < molecules[j].MoleculeID
|
|
})
|
|
|
|
return molecules
|
|
}
|
|
|
|
// findParentMolecules batch-finds the root molecule for multiple issue IDs.
|
|
// Returns a map of issueID → moleculeRootID for issues that belong to a molecule.
|
|
// Issues not part of a molecule are omitted from the result.
|
|
//
|
|
// This replaces the previous N+1 pattern where findParentMolecule was called
|
|
// in a loop, issuing GetDependencyRecords + GetIssue per level per issue.
|
|
// Instead, this walks parent-child chains level-by-level using batch queries,
|
|
// reducing O(N * depth) round-trips to O(depth). (bd-hn4q)
|
|
func findParentMolecules(ctx context.Context, s molReader, issueIDs []string) map[string]string {
|
|
if len(issueIDs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
// rootOf accumulates: startID -> rootID for chains that terminated
|
|
rootOf := make(map[string]string, len(issueIDs))
|
|
|
|
// current tracks: startID -> currentAncestorID (still walking up)
|
|
current := make(map[string]string, len(issueIDs))
|
|
for _, id := range issueIDs {
|
|
current[id] = id
|
|
}
|
|
|
|
// Walk up parent-child chains in batch, level by level
|
|
for depth := 0; depth < 50 && len(current) > 0; depth++ {
|
|
// Collect unique IDs at current level
|
|
seen := make(map[string]bool, len(current))
|
|
toCheck := make([]string, 0, len(current))
|
|
for _, curID := range current {
|
|
if !seen[curID] {
|
|
seen[curID] = true
|
|
toCheck = append(toCheck, curID)
|
|
}
|
|
}
|
|
|
|
// Batch fetch parent-child deps for all current ancestors
|
|
allDeps, err := s.GetDependencyRecordsForIssues(ctx, toCheck)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
// Build parent lookup: childID -> parentID
|
|
parentOf := make(map[string]string, len(allDeps))
|
|
for childID, deps := range allDeps {
|
|
for _, dep := range deps {
|
|
if dep.Type == types.DepParentChild {
|
|
parentOf[childID] = dep.DependsOnID
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Advance chains that have parents, finalize those that don't
|
|
nextCurrent := make(map[string]string)
|
|
for startID, curID := range current {
|
|
if parent, ok := parentOf[curID]; ok {
|
|
nextCurrent[startID] = parent
|
|
} else {
|
|
rootOf[startID] = curID
|
|
}
|
|
}
|
|
current = nextCurrent
|
|
}
|
|
|
|
// Anything still walking after max depth — treat as root
|
|
for startID, curID := range current {
|
|
rootOf[startID] = curID
|
|
}
|
|
|
|
// Batch fetch root issues to check if they're molecules
|
|
uniqueRoots := make(map[string]bool, len(rootOf))
|
|
for _, rootID := range rootOf {
|
|
uniqueRoots[rootID] = true
|
|
}
|
|
rootIDs := make([]string, 0, len(uniqueRoots))
|
|
for id := range uniqueRoots {
|
|
rootIDs = append(rootIDs, id)
|
|
}
|
|
|
|
// TODO(batchgetter): unbounded id list; see issueops.BatchGetter's doc and
|
|
// the importIssueLookup TODO in import_shared.go for why this is not yet
|
|
// routed through the role.
|
|
rootIssues, err := s.GetIssuesByIDs(ctx, rootIDs)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
isMolecule := make(map[string]bool, len(rootIssues))
|
|
for _, issue := range rootIssues {
|
|
// `bd mol pour` creates roots with TypeMolecule (no template label),
|
|
// while distilled-from-epic templates use TypeEpic + BeadsTemplateLabel.
|
|
// Both shapes must be recognized so --continue can advance through
|
|
// poured molecules. See gastownhall/beads#3719.
|
|
if issue.IssueType == types.TypeEpic || issue.IssueType == types.TypeMolecule {
|
|
isMolecule[issue.ID] = true
|
|
continue
|
|
}
|
|
for _, label := range issue.Labels {
|
|
if label == BeadsTemplateLabel {
|
|
isMolecule[issue.ID] = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Filter to only molecule roots
|
|
result := make(map[string]string, len(rootOf))
|
|
for startID, rootID := range rootOf {
|
|
if isMolecule[rootID] {
|
|
result[startID] = rootID
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// findParentMolecule walks up the parent-child chain to find the root molecule
|
|
// for a single issue. Returns "" if the issue is not part of a molecule.
|
|
func findParentMolecule(ctx context.Context, s molReader, issueID string) string {
|
|
roots := findParentMolecules(ctx, s, []string{issueID})
|
|
return roots[issueID]
|
|
}
|
|
|
|
// sortStepsByDependencyOrder sorts steps by their dependency order
|
|
func sortStepsByDependencyOrder(steps []*StepStatus, subgraph *TemplateSubgraph) {
|
|
// Build dependency graph
|
|
depCount := make(map[string]int) // issue ID -> number of deps
|
|
for _, step := range steps {
|
|
depCount[step.Issue.ID] = 0
|
|
}
|
|
|
|
// Count blocking dependencies within the step set
|
|
stepIDs := make(map[string]bool)
|
|
for _, step := range steps {
|
|
stepIDs[step.Issue.ID] = true
|
|
}
|
|
|
|
for _, dep := range subgraph.Dependencies {
|
|
if dep.Type == types.DepBlocks && stepIDs[dep.IssueID] && stepIDs[dep.DependsOnID] {
|
|
depCount[dep.IssueID]++
|
|
}
|
|
}
|
|
|
|
// Stable sort by dependency count (fewer deps first)
|
|
sort.SliceStable(steps, func(i, j int) bool {
|
|
return depCount[steps[i].Issue.ID] < depCount[steps[j].Issue.ID]
|
|
})
|
|
}
|
|
|
|
// printMoleculeProgress prints the progress in human-readable format
|
|
func printMoleculeProgress(mol *MoleculeProgress) {
|
|
fmt.Printf("You're working on molecule %s\n", ui.RenderAccent(mol.MoleculeID))
|
|
fmt.Printf(" %s\n", mol.MoleculeTitle)
|
|
if mol.Assignee != "" {
|
|
fmt.Printf(" Assigned to: %s\n", mol.Assignee)
|
|
}
|
|
fmt.Println()
|
|
|
|
for _, step := range mol.Steps {
|
|
statusIcon := getStatusIcon(step.Status)
|
|
marker := ""
|
|
if step.IsCurrent {
|
|
marker = " <- YOU ARE HERE"
|
|
}
|
|
fmt.Printf(" %s %s: %s%s\n", statusIcon, step.Issue.ID, step.Issue.Title, marker)
|
|
}
|
|
|
|
fmt.Println()
|
|
fmt.Printf("Progress: %d/%d steps complete\n", mol.Completed, mol.Total)
|
|
|
|
if mol.NextStep != nil && mol.CurrentStep == nil {
|
|
fmt.Printf("\nNext ready: %s - %s\n", mol.NextStep.ID, mol.NextStep.Title)
|
|
fmt.Printf(" Start with: bd update %s --claim\n", mol.NextStep.ID)
|
|
}
|
|
|
|
// Show hint about viewing step instructions
|
|
var hintStepID string
|
|
if mol.CurrentStep != nil {
|
|
hintStepID = mol.CurrentStep.ID
|
|
} else if mol.NextStep != nil {
|
|
hintStepID = mol.NextStep.ID
|
|
}
|
|
if hintStepID != "" {
|
|
fmt.Printf("\n%s Run `bd show %s` to see detailed instructions.\n", ui.RenderAccent("💡"), hintStepID)
|
|
}
|
|
}
|
|
|
|
// getStatusIcon returns the icon for a step status
|
|
func getStatusIcon(status string) string {
|
|
switch status {
|
|
case "done":
|
|
return ui.RenderPass("[done]")
|
|
case "current":
|
|
return ui.RenderWarn("[current]")
|
|
case "ready":
|
|
return ui.RenderAccent("[ready]")
|
|
case "blocked":
|
|
return ui.RenderFail("[blocked]")
|
|
default:
|
|
return "[pending]"
|
|
}
|
|
}
|
|
|
|
// ContinueResult holds the result of advancing to the next molecule step
|
|
type ContinueResult struct {
|
|
ClosedStep *types.Issue `json:"closed_step"`
|
|
NextStep *types.Issue `json:"next_step,omitempty"`
|
|
AutoAdvanced bool `json:"auto_advanced"`
|
|
MolComplete bool `json:"molecule_complete"`
|
|
MoleculeID string `json:"molecule_id,omitempty"`
|
|
}
|
|
|
|
// AdvanceToNextStep finds the next ready step in a molecule after closing a step.
|
|
// If autoClaim is true, it marks the next step as in_progress using optimistic
|
|
// concurrency control: the step's status is re-verified before claiming to
|
|
// guard against TOCTOU races where multiple agents identify and try to claim the
|
|
// same step concurrently.
|
|
// Returns nil if the issue is not part of a molecule.
|
|
func AdvanceToNextStep(ctx context.Context, s molWriter, closedStepID string, autoClaim bool, actorName string) (*ContinueResult, error) {
|
|
if s == nil {
|
|
return nil, fmt.Errorf("no database connection")
|
|
}
|
|
|
|
// Get the closed step
|
|
closedStep, err := s.GetIssue(ctx, closedStepID)
|
|
if err != nil || closedStep == nil {
|
|
return nil, fmt.Errorf("could not get closed step: %w", err)
|
|
}
|
|
|
|
result := &ContinueResult{
|
|
ClosedStep: closedStep,
|
|
}
|
|
|
|
// Find parent molecule
|
|
moleculeID := findParentMolecule(ctx, s, closedStepID)
|
|
if moleculeID == "" {
|
|
// Not part of a molecule - nothing to advance
|
|
return nil, nil
|
|
}
|
|
result.MoleculeID = moleculeID
|
|
|
|
// Load molecule progress
|
|
progress, err := getMoleculeProgress(ctx, s, moleculeID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not load molecule: %w", err)
|
|
}
|
|
|
|
// Check if molecule is complete
|
|
if progress.Completed >= progress.Total {
|
|
result.MolComplete = true
|
|
return result, nil
|
|
}
|
|
|
|
// Collect all ready steps (not just the first) so we can fall back
|
|
// if a concurrent agent claims one before us.
|
|
var readySteps []*types.Issue
|
|
for _, step := range progress.Steps {
|
|
if step.Status == "ready" {
|
|
readySteps = append(readySteps, step.Issue)
|
|
}
|
|
}
|
|
|
|
if len(readySteps) == 0 {
|
|
// No ready steps - might be blocked
|
|
return result, nil
|
|
}
|
|
|
|
result.NextStep = readySteps[0]
|
|
|
|
if autoClaim {
|
|
for _, candidate := range readySteps {
|
|
if err := s.ClaimStepIfOpen(ctx, candidate.ID, actorName); err == nil {
|
|
result.NextStep = candidate
|
|
result.AutoAdvanced = true
|
|
break
|
|
}
|
|
// This candidate was already claimed; try the next ready step
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// PrintContinueResult prints the result of advancing to the next step
|
|
func PrintContinueResult(result *ContinueResult) {
|
|
if result == nil {
|
|
return
|
|
}
|
|
|
|
if result.MolComplete {
|
|
fmt.Printf("\n%s Molecule %s complete! All steps closed.\n", ui.RenderPass("✓"), result.MoleculeID)
|
|
fmt.Println("Consider: bd mol squash " + result.MoleculeID + " --summary '...'")
|
|
return
|
|
}
|
|
|
|
if result.NextStep == nil {
|
|
fmt.Println("\nNo ready steps in molecule (may be blocked).")
|
|
return
|
|
}
|
|
|
|
fmt.Printf("\nNext ready in molecule:\n")
|
|
fmt.Printf(" %s: %s\n", result.NextStep.ID, result.NextStep.Title)
|
|
|
|
if result.AutoAdvanced {
|
|
fmt.Printf("\n%s Marked in_progress (use --no-auto to skip)\n", ui.RenderWarn("→"))
|
|
} else {
|
|
fmt.Printf("\nStart with: bd update %s --claim\n", result.NextStep.ID)
|
|
}
|
|
}
|
|
|
|
// parseRange parses a range string like "1-50" or "100-150" into start and end indices.
|
|
// Returns 1-based indices (start=1 means first step).
|
|
func parseRange(rangeStr string) (start, end int, err error) {
|
|
parts := strings.Split(rangeStr, "-")
|
|
if len(parts) != 2 {
|
|
return 0, 0, fmt.Errorf("expected format 'start-end' (e.g., '1-50')")
|
|
}
|
|
start, err = strconv.Atoi(strings.TrimSpace(parts[0]))
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("invalid start: %w", err)
|
|
}
|
|
end, err = strconv.Atoi(strings.TrimSpace(parts[1]))
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("invalid end: %w", err)
|
|
}
|
|
if start < 1 {
|
|
return 0, 0, fmt.Errorf("start must be >= 1")
|
|
}
|
|
if end < start {
|
|
return 0, 0, fmt.Errorf("end must be >= start")
|
|
}
|
|
return start, end, nil
|
|
}
|
|
|
|
// filterStepsByRange filters steps to a 1-based range [start, end].
|
|
func filterStepsByRange(steps []*StepStatus, start, end int) []*StepStatus {
|
|
// Convert to 0-based indices
|
|
startIdx := start - 1
|
|
endIdx := end
|
|
|
|
if startIdx >= len(steps) {
|
|
return nil
|
|
}
|
|
if endIdx > len(steps) {
|
|
endIdx = len(steps)
|
|
}
|
|
return steps[startIdx:endIdx]
|
|
}
|
|
|
|
// printLargeMoleculeSummary prints a summary for molecules with many steps.
|
|
func printLargeMoleculeSummary(stats *types.MoleculeProgressStats) {
|
|
fmt.Printf("Molecule: %s\n", ui.RenderAccent(stats.MoleculeID))
|
|
fmt.Printf(" %s\n", stats.MoleculeTitle)
|
|
fmt.Println()
|
|
|
|
// Progress summary
|
|
var percent float64
|
|
if stats.Total > 0 {
|
|
percent = float64(stats.Completed) * 100 / float64(stats.Total)
|
|
}
|
|
fmt.Printf("Progress: %d / %d steps (%.1f%%)\n", stats.Completed, stats.Total, percent)
|
|
|
|
if stats.CurrentStepID != "" {
|
|
fmt.Printf("Current step: %s\n", stats.CurrentStepID)
|
|
} else if stats.InProgress > 0 {
|
|
fmt.Printf("In progress: %d step(s)\n", stats.InProgress)
|
|
}
|
|
|
|
fmt.Println()
|
|
fmt.Printf("%s This molecule has %d steps (threshold: %d).\n",
|
|
ui.RenderWarn("Note:"), stats.Total, LargeMoleculeThreshold)
|
|
fmt.Println("To view steps, use one of:")
|
|
fmt.Printf(" bd mol current %s --limit 50 # First 50 steps\n", stats.MoleculeID)
|
|
fmt.Printf(" bd mol current %s --range 1-50 # Steps 1-50\n", stats.MoleculeID)
|
|
fmt.Printf(" bd mol progress %s # Efficient progress summary\n", stats.MoleculeID)
|
|
|
|
// Show hint about viewing step instructions
|
|
if stats.CurrentStepID != "" {
|
|
fmt.Printf("\n%s Run `bd show %s` to see detailed instructions.\n", ui.RenderAccent("💡"), stats.CurrentStepID)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
molCurrentCmd.Flags().String("for", "", "Show molecules for a specific agent/assignee")
|
|
molCurrentCmd.Flags().Int("limit", 0, "Maximum number of steps to display (0 = auto, use 'all' threshold)")
|
|
molCurrentCmd.Flags().String("range", "", "Display specific step range (e.g., '1-50', '100-150')")
|
|
molCmd.AddCommand(molCurrentCmd)
|
|
}
|