beads/internal/workapi/ready.go
CI Bot f1d4b4b1ca fix(workapi): refuse an invalid metadata key where the builders can name it
The frozen document says an invalid `metadata_field` key is a 400. It was a
500: the only check lived in the SQL builder, so the failure arrived wrapped in
the storage method's name ("GetReadyWorkWithCounts: invalid metadata key ...")
— a shape nothing above storage can classify — and client typos landed in the
request_error alarm stream. Three doc comments already claimed otherwise,
including the leaf contract's "Keys are validated inside", which no Reader
implementation did.

Both builders now call one validator, so the claim is true of every
implementation of the role rather than of the CLI's flag parsing only. The CLI
keeps its own usage errors: it validates the same input at flag-parse time and
reaches the builder with the keys already checked, so its messages and exit
codes are untouched.

invalidFilterParam gains the two prefixes, and every row of that switch — the
three that were already there included — is now driven end to end by
TestABuilderRefusalIsTheDocumentedBadRequest. `invalid issue type ` was pinned
nowhere before: rewording it in list.go turns ?type=bogus into a 500 and now
fails that test.
2026-07-31 18:37:25 +00:00

105 lines
3.6 KiB
Go

package workapi
import (
"fmt"
"strings"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
"github.com/steveyegge/beads/issueops"
)
// DefaultReadyLimit is the default number of rows a ready-work query returns
// when the caller does not ask for a specific limit. Every frontend registers
// its limit knob from this constant so the surfaces cannot drift apart; 0
// still means unlimited.
const DefaultReadyLimit = 100
// LimitOr resolves a request's optional limit: nil takes the surface's shared
// default, and an explicit 0 stays 0, which means unlimited at both storage
// seams. The pointer is what keeps "unset" and "explicitly unlimited"
// distinguishable — collapsing them would make one constant unable to serve
// both surfaces.
func LimitOr(limit *int, fallback int) int {
if limit == nil {
return fallback
}
return *limit
}
// BuildReadyFilter turns a ready request into the storage-level work filter.
// It is the single definition of what `bd ready` means: open issues only (an
// empty WorkFilter would default to open plus in_progress), issue-type alias
// expansion, label and exclude-type normalization, limit defaulting, and the
// assignee rule that lets Unassigned win over a stale Assignee.
//
// Labels, LabelsAny and ExcludeLabels are normalized here, so a caller can
// pass raw user input. A caller that has to decide something from the label
// sets - the CLI's directory-label default is the one such case - reads them
// back off the returned filter, where they are already normalized, rather than
// normalizing its own copy: a value it then puts on the filter itself is its
// own to shape, and running it through here would change it.
func BuildReadyFilter(in issueops.ReadyRequest) (types.WorkFilter, error) {
filter := types.WorkFilter{
// Open only, not in_progress - the same set `bd list --ready` shows.
Status: types.StatusOpen,
Type: utils.NormalizeIssueType(in.IssueType),
Limit: LimitOr(in.Limit, DefaultReadyLimit),
Offset: in.Offset,
Unassigned: in.Unassigned,
SortPolicy: types.SortPolicy(in.Sort),
Labels: utils.NormalizeLabels(in.Labels),
LabelsAny: utils.NormalizeLabels(in.LabelsAny),
ExcludeLabels: utils.NormalizeLabels(in.ExcludeLabels),
LabelPattern: in.LabelPattern,
LabelRegex: in.LabelRegex,
IncludeDeferred: in.IncludeDeferred,
IncludeEphemeral: in.IncludeEphemeral,
ExcludeTypes: normalizeExcludeTypes(in.ExcludeTypes),
HasMetadataKey: in.HasMetadataKey,
}
if in.Priority != nil {
p := *in.Priority
filter.Priority = &p
}
if in.Assignee != "" && !in.Unassigned {
a := in.Assignee
filter.Assignee = &a
}
if in.ParentID != "" {
pid := in.ParentID
filter.ParentID = &pid
}
if in.MolType != nil {
filter.MolType = in.MolType
}
if len(in.MetadataFields) > 0 {
filter.MetadataFields = in.MetadataFields
}
if err := ValidateMetadataFilters(in.MetadataFields, in.HasMetadataKey); err != nil {
return filter, err
}
if !filter.SortPolicy.IsValid() {
return filter, fmt.Errorf("invalid sort policy '%s'. Valid values: hybrid, priority, oldest", in.Sort)
}
return filter, nil
}
// normalizeExcludeTypes splits comma-separated exclusions and expands type
// aliases, so --exclude-type=mr,epic and --exclude-type mr --exclude-type epic
// mean the same thing.
func normalizeExcludeTypes(raw []string) []types.IssueType {
var out []types.IssueType
for _, group := range raw {
for _, t := range strings.Split(group, ",") {
t = strings.TrimSpace(t)
if t != "" {
out = append(out, types.IssueType(utils.NormalizeIssueType(t)))
}
}
}
return out
}