diff --git a/.golangci.yml b/.golangci.yml index 462241126..a3795fafc 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -160,11 +160,18 @@ linters: # guard, finds one, and stops looking. # # What closes that route is the forbidigo `LabelUseCase` rule below, - # scoped to this same file. The two are a PAIR and neither is - # decorative: forbidigo names the call and depguard names the package, - # so a caller that reached the domain through some third spelling — - # a helper returning a use case directly, say — still trips the import. - # Both were verified by mutation, one bypass each. + # and since ga-2ltro.12 that rule is PACKAGE-WIDE across cmd/bd while + # this one still names one file. The asymmetry is the design, not a + # gap: forbidigo denies the CALL, which is what a front door has to + # stop doing, and every file may be held to that today. depguard denies + # the PACKAGE, which a file cannot stop importing until its whole + # command has moved — fifty-eight non-test files in cmd/bd open a unit + # of work for verbs no role serves yet. So the pair covers a caller who + # reached the domain through some third spelling (a helper returning a + # use case directly, say) in the one file that has fully migrated, and + # forbidigo alone covers the call everywhere else. Both were verified + # by mutation, one bypass each; ga-2ltro.12 re-verified the forbidigo + # half from a file the depguard half does not name. # # cmd/bd/label.go holds every label decision now — the arg split, the # reserved-prefix refusal, the patch, the report — and reaches storage @@ -176,17 +183,22 @@ linters: # # The list grows by one file per command whose front doors reach the # roles, the way cmd-bd-role-constructors grows by one package per - # shared body. + # shared body. cmd/bd/state.go is the second entry: ga-2ltro.12 moved + # `bd state`, `bd state list` and `bd set-state` onto Reader.Get and + # Lifecycle, deleted their proxied twin outright, and left the file + # importing neither package — so the rule pins a migration that has + # happened rather than asking for one that has not. cmd-bd-domain-boundary: list-mode: lax files: - "**/cmd/bd/label.go" + - "**/cmd/bd/state.go" - "!$test" deny: - pkg: github.com/steveyegge/beads/internal/storage/domain - desc: "bd label reaches storage through an issueops role accessor, never through the domain use cases - see openIssueLifecycle in cmd/bd/label.go" + desc: "bd label and bd state reach storage through an issueops role accessor, never through the domain use cases - see openIssueLifecycle in cmd/bd/label.go" - pkg: github.com/steveyegge/beads/internal/storage/uow - desc: "bd label does not open a unit of work: the route fork is the accessor (openIssueLifecycle/openIssueReader), and a unit of work here is how a use-case call gets back in - see cmd/bd/label.go" + desc: "these commands do not open a unit of work: the route fork is the accessor (openIssueLifecycle/openIssueReader), and a unit of work here is how a use-case call gets back in - see cmd/bd/label.go" # store.IssueReader() is the door to the reader role, and a store wears # decorators (telemetry spans, hook firing) that each add their layer in # their OWN accessor. A command holding the storage.DoltStorage @@ -322,29 +334,29 @@ linters: # # The previous version of this comment said there was exactly one rule and # that a second "would need this widened or replaced". That came true, and - # what it cost is the two entries below plus a `text:` on ONE of the - # per-path holes. Every hole from here down is written for the FILTER rule - # and predates the label one, so a hole that names only a path silently - # opens both. That is fine wherever the file never reaches a label use - # case, and it is not fine for cmd/bd/label.go, which is the one file the - # label rule exists for - see the `text:`-scoped entry at the label family - # below. + # what it cost is the entry below plus a `text:` on THREE of the per-path + # holes. Every hole from here down is written for the FILTER rule and + # predates the label one, so a hole that names only a path silently opens + # both. That is fine wherever the file never reaches a label use case, and + # it is not fine for the files that do - see the three `text:`-scoped + # entries below. - path-except: '^(internal/httpapi/|cmd/bd/)' linters: - forbidigo - # The label-plane rule is deny-by-default across cmd/bd like its sibling, - # and this narrows it to the ONE file whose front door has been migrated. - # Every other caller of a label use case in cmd/bd is a command this slice - # did not move - `bd create --labels`' dry-run preview, the mutate and - # show proxied routes, and label.go's own list-all and propagate twins in - # label_proxied_server.go - and holding them to a rule written for a - # migration they have not had would be a lint that has to be waived on - # sight. Each one drops off this exemption as its command reaches the - # role, which is the same shrink-by-migration the filter holes above do. - - path-except: '^cmd/bd/label\.go$' - text: 'reach the label plane through issueops' - linters: - - forbidigo + # + # THE LABEL-PLANE RULE IS NOW PACKAGE-WIDE ACROSS cmd/bd (ga-2ltro.12). + # It used to be narrowed by a `path-except` to cmd/bd/label.go, the one + # file ga-26w10 had migrated, on the argument that holding an unmigrated + # command to a rule written for a migration it had not had would be a lint + # waived on sight. What that reasoning missed is that the waiver was + # INVISIBLE: a file outside the exemption got no marker, no reason and no + # diff, so the three commands still hand-rolling the four-way + # (add|remove) x (wisp|durable) switch the rule exists to delete looked + # exactly like the ones that had nothing to declare. There is no + # `path-except` here any more. Every remaining caller carries its own + # //nolint:forbidigo with the reason that site is not a role's question, + # and a reviewer can count them. + # # Inside cmd/bd the rule is deny-by-default, so each entry below is a # HOLE in it and carries the reason that hole exists. 59 of the 389 # non-test files under cmd/bd are on this list; widening it is an edit @@ -364,6 +376,7 @@ linters: # file on it. The entry therefore stays and the count is unchanged — a # role commit that could not remove one says so rather than implying it. - path: '^cmd/bd/list_show_filter_modes\.go$' + text: 'do not write a filter here' linters: - forbidigo # `bd ready` hands its filter to the plain listing and to --claim, and @@ -384,6 +397,7 @@ linters: # ReadyCounter, both over the request rather than the filter, on both # routes - which is why this hole keeps shrinking rather than growing. - path: '^cmd/bd/ready(_input|_proxied_server)?\.go$' + text: 'do not write a filter here' linters: - forbidigo # Everything below this point is a command that is NOT one of the three @@ -408,25 +422,40 @@ linters: # provider's capability accessor. `gc` stays — it is a different command # that still selects its own closed beads, and it now shares only the pure # recheck (workapi.FilterSweepCandidates) with the role. - # `text:` HERE AND NOWHERE ELSE, and the asymmetry is deliberate rather - # than an oversight to tidy up later. This is the only hole in the list - # whose files include one the LABEL rule must bite in - cmd/bd/label.go, - # which still names types.IssueFilter for list-all and propagate and - # therefore still needs its filter exemption. Without the text scope this - # entry would exempt that file from every forbidigo rule there will ever - # be, including the one written to guard it. The other holes name no such - # file, so widening them now would be churn asserting nothing; the next - # rule that overlaps one of them adds its own text scope the same way. + # `text:` ON EVERY HOLE IN THIS FAMILY, this one included, and the + # completeness is the point rather than the tidiness. A hole naming only a + # path opens EVERY forbidigo rule for its files, so an unscoped one is a + # standing invitation: move the call into a file the hole already covers + # and the label guard never sees it. That is not hypothetical - it is how + # the guard spent its first slice unable to see mol_port.go's copy of the + # switch it was written for. + # + # The first version of this pass scoped only the three holes that covered + # a file with a LabelUseCase call TODAY, and reasoned that the others + # "name no file that reaches a label use case, so widening them now would + # be churn asserting nothing". That reasoning is wrong in the one way that + # matters for a guard: a rule is not sized by where the violations are, it + # is sized by where the next one may be WRITTEN. A review probe put a + # neutrally-named helper into the ado/gitlab/jira/linear hole and it + # passed. Every hole below is scoped now, and adding a file family to this + # list no longer silently costs the label boundary. + # + # The `_test\.go` exemption further down stays unscoped on purpose: `run: + # tests: false` means forbidigo never analyzes a test file at all, so that + # entry asserts nothing either way and scoping it would be the churn this + # comment just refused to write. - path: '^cmd/bd/(close|duplicates|find_duplicates|gate|gc|graph|info|label|orphans|purge|search|todo|wisp|human)(_proxied_server)?\.go$' text: 'do not write a filter here' linters: - forbidigo # Molecule reporting: each selects the members of one molecule. - path: '^cmd/bd/mol_(current|port|progress|ready_gated|stale)\.go$' + text: 'do not write a filter here' linters: - forbidigo # External-tracker sync: each selects the beads to push to its tracker. - path: '^cmd/bd/(ado|gitlab|jira|linear)\.go$' + text: 'do not write a filter here' linters: - forbidigo # Bulk movement of issues in, out and across prefixes. export_source is @@ -434,10 +463,12 @@ linters: # not build a filter, it carries the one export.go builds through to # whichever stack is live. - path: '^cmd/bd/(export|export_auto|export_source|import|migrate|migrate_issues|migrate_personal|rename|rename_prefix)\.go$' + text: 'do not write a filter here' linters: - forbidigo # doctor's diagnostics and the fixes that follow them. - path: '^cmd/bd/doctor_(conventions|pollution)\.go$|^cmd/bd/doctor/(maintenance|fix/maintenance|fix/migrate)\.go$' + text: 'do not write a filter here' linters: - forbidigo # One command each, no family to join. @@ -449,6 +480,7 @@ linters: # front door reached the role. - path: '^cmd/bd/(cleanup|completions|gate_discover|lint|ping|repo|swarm|template)\.go$' + text: 'do not write a filter here' linters: - forbidigo # Tests are exempt for the same reason the depguard twin exempts them: diff --git a/cmd/bd/close_proxied_server.go b/cmd/bd/close_proxied_server.go index 64b9dfb1c..5fc9d268c 100644 --- a/cmd/bd/close_proxied_server.go +++ b/cmd/bd/close_proxied_server.go @@ -425,7 +425,12 @@ func autoCloseProxiedCompletedMolecule(ctx context.Context, uw uow.UnitOfWork, c if err != nil || root == nil || root.Status == types.StatusClosed { return nil } - if labels, err := uw.LabelUseCase().GetLabels(ctx, moleculeID); err == nil { + // A READ, and one that has to see this transaction. The auto-close decision + // is made from labels written earlier in the same unit of work, and + // issueops.Reader opens a transaction of its own, so it would answer from + // the last committed state instead. The follow-up is a reader role bound to + // a caller's transaction; until one exists this stays (ga-2ltro.12). + if labels, err := uw.LabelUseCase().GetLabels(ctx, moleculeID); err == nil { //nolint:forbidigo // in-transaction read; issueops.Reader would open its own root.Labels = labels } if !shouldAutoCloseCompletedRoot(root) { diff --git a/cmd/bd/create_proxied_server.go b/cmd/bd/create_proxied_server.go index 5f8362664..d584f8a75 100644 --- a/cmd/bd/create_proxied_server.go +++ b/cmd/bd/create_proxied_server.go @@ -75,7 +75,14 @@ func runCreateProxiedSingle(_ *cobra.Command, ctx context.Context, in createInpu return HandleError("parent issue %s not found: %v", in.parentID, err) } if !in.noInheritLabels { - inherited, lerr := dryUW.LabelUseCase().GetLabels(ctx, in.parentID) + // A READ inside the DRY-RUN unit of work, which is opened only to + // be discarded: this previews what --parent would inherit without + // creating anything. The role that answers it for real is + // CreateRequest.InheritLabelsFromParent, which resolves the parent's + // labels inside the create it is part of — and a preview has no + // create to be part of. A dry-run mode on the create role is the + // follow-up (ga-2ltro.12). + inherited, lerr := dryUW.LabelUseCase().GetLabels(ctx, in.parentID) //nolint:forbidigo // dry-run preview; the role resolves this only inside a real create if lerr != nil { dryUW.Close(ctx) return HandleError("dry-run inherit labels: %v", lerr) diff --git a/cmd/bd/export_source.go b/cmd/bd/export_source.go index 5bd40d583..6eb0c08b9 100644 --- a/cmd/bd/export_source.go +++ b/cmd/bd/export_source.go @@ -237,7 +237,13 @@ func (s *uowExportSource) LoadExportRelations(ctx context.Context, issues []*typ return rel, nil } - labels, err := s.uw.LabelUseCase().GetLabelsForIssues(ctx, allIDs) + // BATCH READS, and the batch is what keeps them here. Export hydrates every + // relation of N issues at once — labels, comments, dependencies, counts, + // both planes — and no role answers "the labels of these N ids": Reader.Get + // is one detail view per call, so a role-routed export would be N round + // trips over a whole workspace. A bulk relation reader is the follow-up + // (ga-2ltro.12). Every LabelUseCase call in this file is one of these. + labels, err := s.uw.LabelUseCase().GetLabelsForIssues(ctx, allIDs) //nolint:forbidigo // bulk relation load; no role answers labels-for-N-ids if err != nil { return exportRelations{}, fmt.Errorf("load labels: %w", err) } @@ -253,7 +259,7 @@ func (s *uowExportSource) LoadExportRelations(ctx context.Context, issues []*typ } mergeExportMap(rel.commentCounts, counts) - wispLabels, err := s.uw.LabelUseCase().GetLabelsForWisps(ctx, allIDs) + wispLabels, err := s.uw.LabelUseCase().GetLabelsForWisps(ctx, allIDs) //nolint:forbidigo // bulk relation load; see GetLabelsForIssues above if err != nil { if !dberrors.IsTableNotExist(err) { return exportRelations{}, fmt.Errorf("load wisp labels: %w", err) diff --git a/cmd/bd/label_proxied_server.go b/cmd/bd/label_proxied_server.go index 104c0e179..bd5efb2f5 100644 --- a/cmd/bd/label_proxied_server.go +++ b/cmd/bd/label_proxied_server.go @@ -71,15 +71,21 @@ func runLabelListAllProxiedServer(ctx context.Context) error { } } } + // THE QUESTION HAS NO ROLE, which is the reason ga-26w10 left `list-all` + // behind and the reason it is still here: no issueops surface answers + // "every distinct label in this workspace, with a count". Reader.Get is one + // detail view per call, so asking it would be one round trip per bead to + // build a histogram. A label-vocabulary role is the follow-up + // (ga-2ltro.12). if len(permIDs) > 0 { - byIssue, err := uw.LabelUseCase().GetLabelsForIssues(ctx, permIDs) + byIssue, err := uw.LabelUseCase().GetLabelsForIssues(ctx, permIDs) //nolint:forbidigo // label histogram; no role answers the workspace's label vocabulary if err != nil { return HandleErrorRespectJSON("getting labels: %v", err) } accumulate(byIssue) } if len(wispIDs) > 0 { - byWisp, err := uw.LabelUseCase().GetLabelsForWisps(ctx, wispIDs) + byWisp, err := uw.LabelUseCase().GetLabelsForWisps(ctx, wispIDs) //nolint:forbidigo // label histogram; see GetLabelsForIssues above if err != nil { return HandleErrorRespectJSON("getting labels: %v", err) } @@ -160,12 +166,24 @@ func runLabelPropagateProxiedServer(ctx context.Context, args []string) error { if len(children) == 0 { return "", nil } + // THE ONE SURVIVING WISP SWITCH, and it is named rather than hidden. + // This is the same four-way shape ga-26w10 deleted from `bd label` and + // ga-2ltro.12 deleted from `bd tag`, `bd set-state` and the molecule + // port — the branch a front door can get backwards and put a wisp's + // label in the durable table. It survives because propagate is a search + // plus a fan-out that must land as ONE transaction over N children, and + // a per-child Lifecycle.Update is N transactions: the atomicity this + // command has today would be the price of the migration. The shape that + // keeps both is issueops.BatchApplier.ApplyBatch with one ItemUpdate per + // child carrying this same label patch, and it is blocked on a cmd/bd + // accessor for that role. That is the follow-up this waiver names + // (ga-2ltro.12); it is the last one on this list that WRITES. for _, child := range children { var e error if child.Ephemeral { - e = uw.LabelUseCase().AddWispLabel(ctx, child.ID, label, actor) + e = uw.LabelUseCase().AddWispLabel(ctx, child.ID, label, actor) //nolint:forbidigo // atomic N-child fan-out; awaits a BatchApplier accessor } else { - e = uw.LabelUseCase().AddLabel(ctx, child.ID, label, actor) + e = uw.LabelUseCase().AddLabel(ctx, child.ID, label, actor) //nolint:forbidigo // atomic N-child fan-out; awaits a BatchApplier accessor } if e != nil { return "", fmt.Errorf("add label '%s' on %s: %w", label, child.ID, e) diff --git a/cmd/bd/mol_bond.go b/cmd/bd/mol_bond.go index b3d584649..a19b68527 100644 --- a/cmd/bd/mol_bond.go +++ b/cmd/bd/mol_bond.go @@ -317,13 +317,19 @@ func bondProtoProtoInto(ctx context.Context, w molWriter, protoA, protoB *types. compoundTitle = customTitle } - // Create compound root issue + // Create compound root issue. The molecule label travels IN the create + // rather than in a write of its own: a create decides which table the row + // goes to, and carrying the labels with it means one decision places both. + // The separate write this replaces was made through molWriter.AddLabel, + // whose two implementations each resolved the plane again by a different + // predicate, so the row and its label were free to disagree. compound := &types.Issue{ Title: compoundTitle, Description: fmt.Sprintf("Compound proto bonding %s and %s", protoA.ID, protoB.ID), Status: types.StatusOpen, Priority: minPriority(protoA.Priority, protoB.Priority), IssueType: types.TypeEpic, + Labels: []string{MoleculeLabel}, BondedFrom: []types.BondRef{ {SourceID: protoA.ID, BondType: bondType, BondPoint: ""}, {SourceID: protoB.ID, BondType: bondType, BondPoint: ""}, @@ -334,11 +340,6 @@ func bondProtoProtoInto(ctx context.Context, w molWriter, protoA, protoB *types. } compoundID := compound.ID - // Add template label (labels are stored separately, not in issue table) - if err := w.AddLabel(ctx, compoundID, MoleculeLabel, actorName); err != nil { - return nil, fmt.Errorf("adding template label: %w", err) - } - // Add parent-child dependencies from compound to both proto roots depA := &types.Dependency{ IssueID: protoA.ID, diff --git a/cmd/bd/mol_bond_compound_label_test.go b/cmd/bd/mol_bond_compound_label_test.go new file mode 100644 index 000000000..1bdc94b3c --- /dev/null +++ b/cmd/bd/mol_bond_compound_label_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "context" + "testing" + + "github.com/steveyegge/beads/internal/types" +) + +// recordingMolWriter records the writes bondProtoProtoInto makes without a +// database. It embeds the molReader INTERFACE rather than implementing it: the +// bond path under test performs no reads, so a nil reader is the honest +// declaration that a read here would be a new dependency, and it panics loudly +// rather than quietly answering zero if one ever appears. +type recordingMolWriter struct { + molReader + created []*types.Issue + deps []*types.Dependency +} + +func (w *recordingMolWriter) CreateIssue(_ context.Context, issue *types.Issue, _ string) error { + if issue.ID == "" { + issue.ID = "cmp-1" + } + clone := *issue + clone.Labels = append([]string(nil), issue.Labels...) + w.created = append(w.created, &clone) + return nil +} + +func (w *recordingMolWriter) AddDependency(_ context.Context, dep *types.Dependency, _ string) error { + w.deps = append(w.deps, dep) + return nil +} + +func (w *recordingMolWriter) UpdateIssue(context.Context, string, map[string]interface{}, string) error { + panic("UpdateIssue: not part of the proto+proto bond") +} +func (w *recordingMolWriter) CloseIssue(context.Context, string, string, string) error { + panic("CloseIssue: not part of the proto+proto bond") +} +func (w *recordingMolWriter) DeleteIssue(context.Context, string, string) error { + panic("DeleteIssue: not part of the proto+proto bond") +} +func (w *recordingMolWriter) SetConfig(context.Context, string, string) error { + panic("SetConfig: not part of the proto+proto bond") +} +func (w *recordingMolWriter) ClaimStepIfOpen(context.Context, string, string) error { + panic("ClaimStepIfOpen: not part of the proto+proto bond") +} + +// TestBondProtoProtoLabelsTheCompoundAtCreation pins the property that makes +// the compound's molecule label unable to land in the wrong plane: it travels +// in the SAME create that decides which table the compound row itself goes to. +// +// The label used to be a second write — molWriter.AddLabel — and each +// implementation of that method picked the label's plane again, with its own +// predicate: storeMolWriter deferred to the storage layer's isActiveWisp, while +// uowMolWriter consulted a cache it filled from IssueUseCase().GetWisp. Two +// predicates for one fact is the shape a row and its labels come apart in. One +// create makes the disagreement unrepresentable rather than unlikely. +func TestBondProtoProtoLabelsTheCompoundAtCreation(t *testing.T) { + w := &recordingMolWriter{} + protoA := &types.Issue{ID: "bd-a", Title: "Proto A", Priority: 2} + protoB := &types.Issue{ID: "bd-b", Title: "Proto B", Priority: 1} + + result, err := bondProtoProtoInto(context.Background(), w, protoA, protoB, types.BondTypeSequential, "", "tester") + if err != nil { + t.Fatalf("bondProtoProtoInto: %v", err) + } + + if len(w.created) != 1 { + t.Fatalf("created %d issues, want exactly the compound", len(w.created)) + } + compound := w.created[0] + if compound.ID != result.ResultID { + t.Errorf("result names %s but the create made %s", result.ResultID, compound.ID) + } + if !containsLabel(compound.Labels, MoleculeLabel) { + t.Errorf("compound created with labels %v, want the create itself to carry %q", compound.Labels, MoleculeLabel) + } +} + +func containsLabel(labels []string, want string) bool { + for _, l := range labels { + if l == want { + return true + } + } + return false +} diff --git a/cmd/bd/mol_port.go b/cmd/bd/mol_port.go index 6e6d3cab7..1e314328b 100644 --- a/cmd/bd/mol_port.go +++ b/cmd/bd/mol_port.go @@ -40,9 +40,14 @@ var _ molReader = storage.DoltStorage(nil) type molWriter interface { molReader + // CreateIssue carries the issue's Labels. There is deliberately no AddLabel + // verb beside it: a label written separately makes the plane decision a + // second time, and the two implementations of this interface did not make + // it the same way — one asked the storage layer's isActiveWisp, the other a + // cache filled from a wisp probe. A caller that wants an issue labeled + // says so on the issue it creates. CreateIssue(ctx context.Context, issue *types.Issue, actor string) error AddDependency(ctx context.Context, dep *types.Dependency, actor string) error - AddLabel(ctx context.Context, issueID, label, actor string) error UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error CloseIssue(ctx context.Context, id, reason, actor string) error DeleteIssue(ctx context.Context, id, actor string) error @@ -63,10 +68,6 @@ func (w storeMolWriter) AddDependency(ctx context.Context, dep *types.Dependency return w.tx.AddDependency(ctx, dep, actor) } -func (w storeMolWriter) AddLabel(ctx context.Context, issueID, label, actor string) error { - return w.tx.AddLabel(ctx, issueID, label, actor) -} - func (w storeMolWriter) UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error { return w.tx.UpdateIssue(ctx, id, updates, actor) } @@ -128,12 +129,21 @@ func (r uowMolReader) GetIssue(ctx context.Context, id string) (*types.Issue, er if rerr != nil { return nil, fmt.Errorf("resolving %s: %w", id, rerr) } + // READS, ALL OF THEM, and they stay for the reason the writes did not. + // uowMolReader is a PORT: it adapts a caller's open unit of work to the + // molecule commands' reader interface, and every method here must answer + // from inside that transaction. issueops.Reader opens one of its own, so a + // role-routed port would show the molecule the last committed state while + // the command that owns the transaction is midway through changing it. + // A reader role bound to a caller's transaction is the follow-up + // (ga-2ltro.12). The wisp branch here is a read that follows the row + // GetIssueOrWisp already found, not a front door choosing where to write. var labels []string var err error if isWisp { - labels, err = r.uw.LabelUseCase().GetWispLabels(ctx, id) + labels, err = r.uw.LabelUseCase().GetWispLabels(ctx, id) //nolint:forbidigo // in-transaction port read; issueops.Reader would open its own } else { - labels, err = r.uw.LabelUseCase().GetLabels(ctx, id) + labels, err = r.uw.LabelUseCase().GetLabels(ctx, id) //nolint:forbidigo // in-transaction port read; issueops.Reader would open its own } if err == nil { issue.Labels = labels @@ -146,7 +156,7 @@ func (r uowMolReader) GetIssuesByIDs(ctx context.Context, ids []string) ([]*type if err != nil { return nil, err } - if labelMap, err := r.uw.LabelUseCase().GetLabelsForIssues(ctx, ids); err == nil { + if labelMap, err := r.uw.LabelUseCase().GetLabelsForIssues(ctx, ids); err == nil { //nolint:forbidigo // in-transaction port read; see GetIssue above for _, issue := range issues { issue.Labels = labelMap[issue.ID] } @@ -157,7 +167,7 @@ func (r uowMolReader) GetIssuesByIDs(ctx context.Context, ids []string) ([]*type wisps = nil //nolint:staticcheck // wisps table may not exist; issues result still valid } if len(wisps) > 0 { - if labelMap, err := r.uw.LabelUseCase().GetLabelsForWisps(ctx, ids); err == nil { + if labelMap, err := r.uw.LabelUseCase().GetLabelsForWisps(ctx, ids); err == nil { //nolint:forbidigo // in-transaction port read; see GetIssue above for _, wisp := range wisps { wisp.Labels = labelMap[wisp.ID] } @@ -234,7 +244,7 @@ func (r uowMolReader) GetEpicsEligibleForClosure(ctx context.Context) ([]*types. } func (r uowMolReader) GetLabels(ctx context.Context, issueID string) ([]string, error) { - return r.uw.LabelUseCase().GetLabels(ctx, issueID) + return r.uw.LabelUseCase().GetLabels(ctx, issueID) //nolint:forbidigo // in-transaction port read; see GetIssue above } func (r uowMolReader) GetConfig(ctx context.Context, key string) (string, error) { @@ -407,17 +417,6 @@ func (w *uowMolWriter) AddDependency(ctx context.Context, dep *types.Dependency, return w.uw.DependencyUseCase().AddDependency(ctx, dep, actor) } -func (w *uowMolWriter) AddLabel(ctx context.Context, issueID, label, actor string) error { - isWisp, err := w.isWisp(ctx, issueID) - if err != nil { - return err - } - if isWisp { - return w.uw.LabelUseCase().AddWispLabel(ctx, issueID, label, actor) - } - return w.uw.LabelUseCase().AddLabel(ctx, issueID, label, actor) -} - func (w *uowMolWriter) UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error { isWisp, err := w.isWisp(ctx, id) if err != nil { diff --git a/cmd/bd/mutate_proxied_server.go b/cmd/bd/mutate_proxied_server.go index 273828cdd..e3ceb6dc1 100644 --- a/cmd/bd/mutate_proxied_server.go +++ b/cmd/bd/mutate_proxied_server.go @@ -11,6 +11,7 @@ import ( "github.com/steveyegge/beads/internal/ui" "github.com/steveyegge/beads/internal/validation" "github.com/steveyegge/beads/internal/workapi" + "github.com/steveyegge/beads/issueops" ) func proxiedMutateIssue(ctx context.Context, id, commitMsg string, mutate func(ctx context.Context, uw uow.UnitOfWork, issue *types.Issue, isWisp bool) error) (*types.Issue, error) { @@ -133,24 +134,67 @@ func runNoteProxiedServer(ctx context.Context, id, noteText string) error { return nil } +// runTagProxiedServer is `bd tag` on the proxied-server route, and it is the +// one verb in this file that does NOT go through proxiedMutateIssue. +// +// It cannot, and that is the point. proxiedMutateIssue hands its callback a +// unit of work and an isWisp boolean, which is an invitation to pick a plane — +// and the tag callback accepted it, choosing between AddWispLabel and AddLabel +// exactly the way cmd/bd/label_proxied_server.go used to before ga-26w10. +// A label edit is a patch, and issueops.Lifecycle is what applies one: +// UpdateRequest.IssuePlaneOnly stays false, so the role resolves the plane +// inside its own transaction and there is no boolean here to get backwards. +// +// The two reads it still makes are front-door work rather than plumbing. +// issueops.Reader.Get supplies the issue the template guard needs — the roles +// have no opinion about templates, and the direct route refuses one — and the +// role takes an exact id by contract, which Get's issue-then-wisp lookup is. func runTagProxiedServer(ctx context.Context, args []string) error { id := args[0] label := args[1] - updated, err := proxiedMutateIssue(ctx, id, "bd: tag "+id, func(ctx context.Context, uw uow.UnitOfWork, issue *types.Issue, isWisp bool) error { - if isWisp { - return uw.LabelUseCase().AddWispLabel(ctx, issue.ID, label, actor) - } - return uw.LabelUseCase().AddLabel(ctx, issue.ID, label, actor) + + reader, err := openIssueReader() + if err != nil { + return HandleErrorRespectJSON("tag %s: %v", id, err) + } + details, err := reader.Get(ctx, issueops.GetRequest{ID: id}) + if errors.Is(err, storage.ErrNotFound) { + return HandleErrorRespectJSON("tag %s: issue %s not found", id, id) + } + if err != nil { + return HandleErrorRespectJSON("tag %s: resolving %s: %v", id, id, err) + } + if verr := validateIssueUpdatable(id, &details.Issue); verr != nil { + return HandleErrorRespectJSON("tag %s: %v", id, verr) + } + + lifecycle, err := openIssueLifecycle() + if err != nil { + return HandleErrorRespectJSON("tag %s: %v", id, err) + } + // The role commits its own Dolt version inside the storage layer, so + // `--dolt-auto-commit batch` is deferred on the context rather than by + // blanking a commit message — see applyLabelEdit in cmd/bd/label.go. + ctx, err = issueOpsContext(ctx) + if err != nil { + return HandleErrorRespectJSON("tag %s: %v", id, err) + } + result, err := lifecycle.Update(ctx, issueops.UpdateRequest{ + Actor: actor, + IssueID: details.ID, + Patch: issueops.IssuePatch{Labels: issueops.LabelPatch{Add: []string{label}}}, }) if err != nil { return HandleErrorRespectJSON("tag %s: %v", id, err) } + commandDidWrite.Store(true) + if jsonOutput { - if updated != nil { - return outputJSON(updated) + if result.Issue != nil { + return outputJSON(result.Issue) } return nil } - fmt.Printf("%s Added label %q to %s\n", ui.RenderPass("✓"), label, formatFeedbackID(id, issueTitleOrEmpty(updated))) + fmt.Printf("%s Added label %q to %s\n", ui.RenderPass("✓"), label, formatFeedbackID(id, issueTitleOrEmpty(result.Issue))) return nil } diff --git a/cmd/bd/proxied_lookup_failure_test.go b/cmd/bd/proxied_lookup_failure_test.go index e2469c34a..c0259a857 100644 --- a/cmd/bd/proxied_lookup_failure_test.go +++ b/cmd/bd/proxied_lookup_failure_test.go @@ -172,7 +172,7 @@ var proxiedLookupCommands = []struct { { name: "state", run: func(ctx context.Context) error { - return runStateProxiedServer(ctx, stubMissingID, "phase") + return inProxiedRoute(func() error { return runState(ctx, stubMissingID, "phase") }) }, wantNotFound: "Error: resolving bd-missing: not found", wantHardErr: "Error: resolving bd-missing: connection reset by peer", @@ -301,9 +301,14 @@ var proxiedLookupCommands = []struct { { name: "set state", run: func(ctx context.Context) error { - return runSetStateProxiedServer(ctx, stubMissingID, "phase", "done", "") + return inProxiedRoute(func() error { return runSetState(ctx, stubMissingID, "phase", "done", "") }) }, - wantNotFound: "Error: issue bd-missing not found", + // Both routes now resolve through resolveLabelTarget, so this is the + // direct route's message rather than the retired proxied twin's + // "issue bd-missing not found" — the same alignment `bd label`'s + // migration made, and for the same reason: resolution happens before + // the role is asked for anything, so the failure is not about state. + wantNotFound: "Error: resolving bd-missing: not found", wantHardErr: "Error: resolving bd-missing: connection reset by peer", }, { diff --git a/cmd/bd/set_state_proxied_integration_test.go b/cmd/bd/set_state_proxied_integration_test.go index ec8695c41..e3afd7548 100644 --- a/cmd/bd/set_state_proxied_integration_test.go +++ b/cmd/bd/set_state_proxied_integration_test.go @@ -165,6 +165,42 @@ func TestProxiedServerSetState(t *testing.T) { if strings.TrimSpace(val) != "muted" { t.Errorf("wisp state value = %q, want muted", strings.TrimSpace(val)) } + + // ASSERT AT THE TABLE, not only through the reader. The reader answers + // from whichever plane the write chose, so a write and a read that get + // the same boolean backwards together still agree with each other and + // the check above passes. This is the assertion that does not: a wisp's + // state label belongs in wisp_labels and the durable table stays empty. + db := openProxiedDB(t, p) + if n := countRows(t, db, "SELECT COUNT(*) FROM wisp_labels WHERE issue_id = ? AND label = ?", wisp.ID, "patrol:muted"); n != 1 { + t.Errorf("wisp_labels rows for patrol:muted = %d, want 1", n) + } + if n := countRows(t, db, "SELECT COUNT(*) FROM labels WHERE issue_id = ?", wisp.ID); n != 0 { + t.Errorf("durable labels rows for a wisp = %d, want 0", n) + } + }) + + t.Run("state_change_on_wisp_swaps_in_place", func(t *testing.T) { + wisp := bdProxiedCreate(t, bd, p.dir, "Wisp set-state swap", "--ephemeral") + + for _, spec := range []string{"phase=planning", "phase=running"} { + if out, err := bdProxiedRun(t, bd, p.dir, "set-state", wisp.ID, spec, "--json"); err != nil { + t.Fatalf("set-state %s on wisp: %v\n%s", spec, err, out) + } + } + + // The swap is one edit: the old label must be gone and the new one + // present, both in the ephemeral plane, with nothing left durable. + db := openProxiedDB(t, p) + if n := countRows(t, db, "SELECT COUNT(*) FROM wisp_labels WHERE issue_id = ? AND label LIKE 'phase:%'", wisp.ID); n != 1 { + t.Errorf("wisp_labels phase rows = %d, want exactly 1 after a swap", n) + } + if n := countRows(t, db, "SELECT COUNT(*) FROM wisp_labels WHERE issue_id = ? AND label = ?", wisp.ID, "phase:running"); n != 1 { + t.Errorf("wisp_labels rows for phase:running = %d, want 1", n) + } + if n := countRows(t, db, "SELECT COUNT(*) FROM labels WHERE issue_id = ?", wisp.ID); n != 0 { + t.Errorf("durable labels rows for a wisp = %d, want 0", n) + } }) } diff --git a/cmd/bd/show_proxied_server.go b/cmd/bd/show_proxied_server.go index 83885d5eb..75c734cd5 100644 --- a/cmd/bd/show_proxied_server.go +++ b/cmd/bd/show_proxied_server.go @@ -510,11 +510,19 @@ func proxiedRenderIssue(ctx context.Context, uw uow.UnitOfWork, issue *types.Iss fmt.Printf("\n%s\n%s\n", ui.RenderBold("ACCEPTANCE CRITERIA"), uimd.RenderMarkdown(issue.AcceptanceCriteria)) } + // A READ on an ALTERNATE view. `bd show`'s detail view is on + // issueops.Reader on both routes and gets its labels hydrated there; this + // renderer serves --refs, --children, --thread and --as-of, which answer + // with shapes the Reader contract does not describe, from a unit of work + // the caller already holds and has already read the issue from. Asking the + // role here would open a second transaction to re-fetch a row this function + // was handed. Alternate views reaching roles of their own is the follow-up + // (ga-2ltro.12). var labels []string if isWisp { - labels, _ = uw.LabelUseCase().GetWispLabels(ctx, issue.ID) + labels, _ = uw.LabelUseCase().GetWispLabels(ctx, issue.ID) //nolint:forbidigo // alternate view, caller-owned UOW; the detail view is on the role } else { - labels, _ = uw.LabelUseCase().GetLabels(ctx, issue.ID) + labels, _ = uw.LabelUseCase().GetLabels(ctx, issue.ID) //nolint:forbidigo // alternate view, caller-owned UOW; the detail view is on the role } if len(labels) > 0 { fmt.Printf("\n%s %s\n", ui.RenderBold("LABELS:"), strings.Join(labels, ", ")) diff --git a/cmd/bd/state.go b/cmd/bd/state.go index b24f0fdfc..6478c77b5 100644 --- a/cmd/bd/state.go +++ b/cmd/bd/state.go @@ -5,16 +5,51 @@ package main import ( "context" + "errors" "fmt" "strings" "github.com/spf13/cobra" "github.com/steveyegge/beads/internal/metrics" + "github.com/steveyegge/beads/internal/storage" "github.com/steveyegge/beads/internal/types" "github.com/steveyegge/beads/internal/ui" - "github.com/steveyegge/beads/internal/utils" + "github.com/steveyegge/beads/issueops" ) +// resolveStateTarget reads one issue's detail view for the state commands, on +// whichever route this invocation is on. +// +// STATE IS LABELS, so this is the same two moves `bd label list` makes after +// ga-26w10: resolve the positional argument to the exact id the roles take, +// then ask issueops.Reader for the detail view, whose Labels are already +// hydrated and already sorted. There is no label-shaped read here and no plane +// to choose — Reader.Get's issue-then-wisp lookup finds an ephemeral row on its +// own, which is how the DIRECT route acquires wisp support it never had: it +// used to call store.GetLabels, which only ever reads the durable table. +// +// Resolution stays a front-door job, and stays shared with `bd label`: +// GetRequest.ID is exact by contract, so the partial-id affordance the direct +// route offers cannot live below this line. +func resolveStateTarget(ctx context.Context, issueID string) (*issueops.IssueDetails, error) { + fullID, err := resolveLabelTarget(ctx, issueID) + if err != nil { + return nil, err + } + reader, err := openIssueReader() + if err != nil { + return nil, err + } + details, err := reader.Get(ctx, issueops.GetRequest{ID: fullID}) + if errors.Is(err, storage.ErrNotFound) { + return nil, errors.New("not found") + } + if err != nil { + return nil, err + } + return details, nil +} + var stateCmd = &cobra.Command{ Use: "state ", GroupID: "issues", @@ -43,50 +78,38 @@ Examples: } }() - if usesProxiedServer() { - return runStateProxiedServer(rootCtx, args[0], args[1]) - } - - ctx := rootCtx - issueID := args[0] - dimension := args[1] - - var fullID string - var err error - fullID, err = utils.ResolvePartialID(ctx, store, issueID) - if err != nil { - return HandleErrorRespectJSON("resolving %s: %v", issueID, err) - } - - var labels []string - labels, err = store.GetLabels(ctx, fullID) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - value, _ := findStateLabel(labels, dimension) - - if jsonOutput { - result := map[string]interface{}{ - "issue_id": fullID, - "dimension": dimension, - "value": value, - } - if value == "" { - result["value"] = nil - } - return outputJSON(result) - } - - if value == "" { - fmt.Printf("(no %s state set)\n", dimension) - } else { - fmt.Println(value) - } - return nil + return runState(rootCtx, args[0], args[1]) }, } +func runState(ctx context.Context, issueID, dimension string) error { + details, err := resolveStateTarget(ctx, issueID) + if err != nil { + return HandleErrorRespectJSON("resolving %s: %v", issueID, err) + } + + value, _ := findStateLabel(details.Labels, dimension) + + if jsonOutput { + result := map[string]interface{}{ + "issue_id": details.ID, + "dimension": dimension, + "value": value, + } + if value == "" { + result["value"] = nil + } + return outputJSON(result) + } + + if value == "" { + fmt.Printf("(no %s state set)\n", dimension) + } else { + fmt.Println(value) + } + return nil +} + var setStateCmd = &cobra.Command{ Use: "set-state =", GroupID: "issues", @@ -134,121 +157,131 @@ The --reason flag provides context for the event bead (recommended).`, CheckReadonly("set-state") - if usesProxiedServer() { - return runSetStateProxiedServer(rootCtx, issueID, dimension, newValue, reason) + return runSetState(rootCtx, issueID, dimension, newValue, reason) + }, +} + +func runSetState(ctx context.Context, issueID, dimension, newValue, reason string) error { + details, err := resolveStateTarget(ctx, issueID) + if err != nil { + return HandleErrorRespectJSON("resolving %s: %v", issueID, err) + } + fullID := details.ID + + oldValue, found := findStateLabel(details.Labels, dimension) + oldLabel := "" + if found { + oldLabel = dimension + ":" + oldValue + } + + newLabel := dimension + ":" + newValue + + if oldLabel == newLabel { + if jsonOutput { + return outputJSON(map[string]interface{}{ + "issue_id": fullID, + "dimension": dimension, + "value": newValue, + "changed": false, + }) } + fmt.Printf("(no change: %s already set to %s)\n", dimension, newValue) + return nil + } - ctx := rootCtx + eventTitle := fmt.Sprintf("State change: %s → %s", dimension, newValue) + eventDesc := "" + if oldValue != "" { + eventDesc = fmt.Sprintf("Changed %s from %s to %s", dimension, oldValue, newValue) + } else { + eventDesc = fmt.Sprintf("Set %s to %s", dimension, newValue) + } + if reason != "" { + eventDesc += "\n\nReason: " + reason + } - var fullID string - var err error - fullID, err = utils.ResolvePartialID(ctx, store, issueID) - if err != nil { - return HandleErrorRespectJSON("resolving %s: %v", issueID, err) - } + lifecycle, err := openIssueLifecycle() + if err != nil { + return HandleErrorRespectJSON("%v", err) + } + // The role commits its own Dolt version inside the storage layer, so + // `--dolt-auto-commit batch` is deferred on the CONTEXT rather than by + // blanking a commit message — see applyLabelEdit in cmd/bd/label.go. + ctx, err = issueOpsContext(ctx) + if err != nil { + return HandleErrorRespectJSON("%v", err) + } - var labels []string - labels, err = store.GetLabels(ctx, fullID) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - oldValue, found := findStateLabel(labels, dimension) - oldLabel := "" - if found { - oldLabel = dimension + ":" + oldValue - } - - newLabel := dimension + ":" + newValue - - if oldLabel == newLabel { - if jsonOutput { - return outputJSON(map[string]interface{}{ - "issue_id": fullID, - "dimension": dimension, - "value": newValue, - "changed": false, - }) - } - fmt.Printf("(no change: %s already set to %s)\n", dimension, newValue) - return nil - } - - eventTitle := fmt.Sprintf("State change: %s → %s", dimension, newValue) - eventDesc := "" - if oldValue != "" { - eventDesc = fmt.Sprintf("Changed %s from %s to %s", dimension, oldValue, newValue) - } else { - eventDesc = fmt.Sprintf("Set %s to %s", dimension, newValue) - } - if reason != "" { - eventDesc += "\n\nReason: " + reason - } - - var eventID string - childID, err := store.GetNextChildID(ctx, fullID) - if err != nil { - return HandleErrorRespectJSON("generating child ID: %v", err) - } - - event := &types.Issue{ - ID: childID, + // THE EVENT IS CREATED IN THE PLANE ITS SUBJECT LIVES IN, and the role + // is what makes that a copied fact instead of a chosen one. The + // retired proxied route branched here between IssueUseCase().CreateWisp + // and CreateIssue; the retired direct route branched not at all and put + // every event in the durable table, so a wisp's state history landed + // beside issues that outlive it. Carrying the subject's own retention + // mode onto the event says the SAME thing to both routes, and it is + // checked rather than trusted: a guarded create refuses a parent edge it + // could not write, so an event that disagreed with its subject's plane + // fails the command instead of silently landing in the wrong table. + created, err := lifecycle.Create(ctx, issueops.CreateRequest{ + Actor: actor, + Issue: &types.Issue{ Title: eventTitle, Description: eventDesc, Status: types.StatusClosed, Priority: 4, IssueType: types.TypeEvent, CreatedBy: getActorWithGit(), - } - if err := store.CreateIssue(ctx, event, actor); err != nil { - return HandleErrorRespectJSON("creating event: %v", err) - } + Ephemeral: details.Ephemeral, + NoHistory: details.NoHistory, + }, + ParentID: fullID, + }) + if err != nil { + return HandleErrorRespectJSON("creating event: %v", err) + } + commandDidWrite.Store(true) + eventID := created.Issue.ID - dep := &types.Dependency{ - IssueID: childID, - DependsOnID: fullID, - Type: types.DepParentChild, - } - if err := store.AddDependency(ctx, dep, actor); err != nil { - WarnError("failed to add parent-child dependency: %v", err) - } + // ONE PATCH SWAPS THE DIMENSION. Both retired routes removed the old + // label and added the new one as two writes, and both treated a failed + // removal as a warning — which leaves an issue carrying two values for + // one dimension, a state findStateLabel resolves by whichever the label + // read happens to return first. A LabelPatch applies Add and Remove in + // one guarded update, so the dimension has one value or the command + // failed. The plane needs no branch: IssuePlaneOnly stays false and the + // role resolves it inside its own transaction. + if _, err := lifecycle.Update(ctx, issueops.UpdateRequest{ + Actor: actor, + IssueID: fullID, + Patch: issueops.IssuePatch{Labels: issueops.LabelPatch{ + Add: []string{newLabel}, + Remove: removeStateLabel(oldLabel), + }}, + }); err != nil { + return HandleErrorRespectJSON("setting %s: %v", dimension, err) + } - eventID = childID - - if oldLabel != "" { - if err := store.RemoveLabel(ctx, fullID, oldLabel, actor); err != nil { - WarnError("failed to remove old label %s: %v", oldLabel, err) - } + if jsonOutput { + result := map[string]interface{}{ + "issue_id": fullID, + "dimension": dimension, + "old_value": oldValue, + "new_value": newValue, + "event_id": eventID, + "changed": true, } - - if err := store.AddLabel(ctx, fullID, newLabel, actor); err != nil { - return HandleErrorRespectJSON("adding label: %v", err) + if oldValue == "" { + result["old_value"] = nil } + return outputJSON(result) + } - commandDidWrite.Store(true) - - if jsonOutput { - result := map[string]interface{}{ - "issue_id": fullID, - "dimension": dimension, - "old_value": oldValue, - "new_value": newValue, - "event_id": eventID, - "changed": true, - } - if oldValue == "" { - result["old_value"] = nil - } - return outputJSON(result) - } - - fmt.Printf("%s Set %s = %s on %s\n", ui.RenderPass("✓"), dimension, newValue, fullID) - if oldValue != "" { - fmt.Printf(" Previous: %s\n", oldValue) - } - fmt.Printf(" Event: %s\n", eventID) - return nil - }, + fmt.Printf("%s Set %s = %s on %s\n", ui.RenderPass("✓"), dimension, newValue, fullID) + if oldValue != "" { + fmt.Printf(" Previous: %s\n", oldValue) + } + fmt.Printf(" Event: %s\n", eventID) + return nil } // stateListCmd lists all state dimensions on an issue @@ -276,49 +309,39 @@ Example: } }() - if usesProxiedServer() { - return runStateListProxiedServer(rootCtx, args[0]) - } - - ctx := rootCtx - issueID := args[0] - - var fullID string - var err error - fullID, err = utils.ResolvePartialID(ctx, store, issueID) - if err != nil { - return HandleErrorRespectJSON("resolving %s: %v", issueID, err) - } - - var labels []string - labels, err = store.GetLabels(ctx, fullID) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - states := collectStateLabels(labels) - - if jsonOutput { - return outputJSON(map[string]interface{}{ - "issue_id": fullID, - "states": states, - }) - } - - if len(states) == 0 { - fmt.Printf("\n%s has no state labels\n", fullID) - return nil - } - - fmt.Printf("\n%s State for %s:\n", ui.RenderAccent("📊"), fullID) - for dimension, value := range states { - fmt.Printf(" %s: %s\n", dimension, value) - } - fmt.Println() - return nil + return runStateList(rootCtx, args[0]) }, } +func runStateList(ctx context.Context, issueID string) error { + details, err := resolveStateTarget(ctx, issueID) + if err != nil { + return HandleErrorRespectJSON("resolving %s: %v", issueID, err) + } + fullID := details.ID + + states := collectStateLabels(details.Labels) + + if jsonOutput { + return outputJSON(map[string]interface{}{ + "issue_id": fullID, + "states": states, + }) + } + + if len(states) == 0 { + fmt.Printf("\n%s has no state labels\n", fullID) + return nil + } + + fmt.Printf("\n%s State for %s:\n", ui.RenderAccent("📊"), fullID) + for dimension, value := range states { + fmt.Printf(" %s: %s\n", dimension, value) + } + fmt.Println() + return nil +} + func init() { // set-state flags setStateCmd.Flags().String("reason", "", "Reason for the state change (recorded in event)") @@ -343,6 +366,18 @@ func findStateLabel(labels []string, dimension string) (string, bool) { return "", false } +// removeStateLabel is the Remove member of the dimension swap: the old label +// when there was one, and nothing when the dimension is being set for the +// first time. A nil slice rather than a one-entry slice holding "" matters — +// LabelPatch drops an empty entry, so both spell "remove nothing", but only +// one of them says so. +func removeStateLabel(oldLabel string) []string { + if oldLabel == "" { + return nil + } + return []string{oldLabel} +} + func collectStateLabels(labels []string) map[string]string { states := make(map[string]string) for _, label := range labels { diff --git a/cmd/bd/state_proxied_server.go b/cmd/bd/state_proxied_server.go deleted file mode 100644 index 9d724b6fd..000000000 --- a/cmd/bd/state_proxied_server.go +++ /dev/null @@ -1,268 +0,0 @@ -package main - -import ( - "context" - "errors" - "fmt" - "strings" - - "github.com/steveyegge/beads/internal/storage" - "github.com/steveyegge/beads/internal/storage/domain" - "github.com/steveyegge/beads/internal/storage/uow" - "github.com/steveyegge/beads/internal/types" - "github.com/steveyegge/beads/internal/ui" - "github.com/steveyegge/beads/internal/workapi" -) - -func proxiedStateLabels(ctx context.Context, uw uow.UnitOfWork, issueID string) (string, []string, error) { - issue, isWisp, err := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), issueID) - if errors.Is(err, storage.ErrNotFound) { - return "", nil, HandleErrorRespectJSON("resolving %s: not found", issueID) - } - if err != nil { - return "", nil, HandleErrorRespectJSON("resolving %s: %v", issueID, err) - } - var labels []string - if isWisp { - labels, err = uw.LabelUseCase().GetWispLabels(ctx, issue.ID) - } else { - labels, err = uw.LabelUseCase().GetLabels(ctx, issue.ID) - } - if err != nil { - return "", nil, HandleErrorRespectJSON("%v", err) - } - return issue.ID, labels, nil -} - -func runStateProxiedServer(ctx context.Context, issueID, dimension string) error { - uw, err := proxiedOpenReadUOW(ctx) - if err != nil { - return err - } - defer uw.Close(ctx) - - fullID, labels, err := proxiedStateLabels(ctx, uw, issueID) - if err != nil { - return err - } - - prefix := dimension + ":" - var value string - for _, label := range labels { - if strings.HasPrefix(label, prefix) { - value = strings.TrimPrefix(label, prefix) - break - } - } - - if jsonOutput { - result := map[string]interface{}{ - "issue_id": fullID, - "dimension": dimension, - "value": value, - } - if value == "" { - result["value"] = nil - } - return outputJSON(result) - } - - if value == "" { - fmt.Printf("(no %s state set)\n", dimension) - } else { - fmt.Println(value) - } - return nil -} - -type setStateResult struct { - fullID string - oldValue string - eventID string - changed bool - removeWarn string -} - -func runSetStateProxiedServer(ctx context.Context, issueID, dimension, newValue, reason string) error { - if uowProvider == nil { - return HandleErrorRespectJSON("proxied-server UOW provider not initialized") - } - - newLabel := dimension + ":" + newValue - - res, err := uow.RunTxResult(ctx, uowProvider, func(ctx context.Context, uw uow.UnitOfWork) (setStateResult, string, error) { - issue, isWisp, rerr := workapi.GetIssueOrWisp(ctx, workapi.NewUOWDetailSource(uw), issueID) - if errors.Is(rerr, storage.ErrNotFound) { - return setStateResult{}, "", fmt.Errorf("issue %s not found", issueID) - } - if rerr != nil { - return setStateResult{}, "", fmt.Errorf("resolving %s: %w", issueID, rerr) - } - fullID := issue.ID - - var labels []string - var lerr error - if isWisp { - labels, lerr = uw.LabelUseCase().GetWispLabels(ctx, fullID) - } else { - labels, lerr = uw.LabelUseCase().GetLabels(ctx, fullID) - } - if lerr != nil { - return setStateResult{}, "", lerr - } - - prefix := dimension + ":" - var oldLabel, oldValue string - for _, label := range labels { - if strings.HasPrefix(label, prefix) { - oldLabel = label - oldValue = strings.TrimPrefix(label, prefix) - break - } - } - - if oldLabel == newLabel { - return setStateResult{fullID: fullID, oldValue: oldValue, changed: false}, "", nil - } - - eventDesc := fmt.Sprintf("Set %s to %s", dimension, newValue) - if oldValue != "" { - eventDesc = fmt.Sprintf("Changed %s from %s to %s", dimension, oldValue, newValue) - } - if reason != "" { - eventDesc += "\n\nReason: " + reason - } - - event := &types.Issue{ - Title: fmt.Sprintf("State change: %s → %s", dimension, newValue), - Description: eventDesc, - Status: types.StatusClosed, - Priority: 4, - IssueType: types.TypeEvent, - CreatedBy: getActorWithGit(), - } - params := domain.CreateIssueParams{Issue: event, ParentID: fullID} - - var eventID string - if isWisp { - cr, cerr := uw.IssueUseCase().CreateWisp(ctx, params, actor) - if cerr != nil { - return setStateResult{}, "", fmt.Errorf("creating event: %w", cerr) - } - eventID = cr.Issue.ID - } else { - cr, cerr := uw.IssueUseCase().CreateIssue(ctx, params, actor) - if cerr != nil { - return setStateResult{}, "", fmt.Errorf("creating event: %w", cerr) - } - eventID = cr.Issue.ID - } - - var removeWarn string - if oldLabel != "" { - var rerr error - if isWisp { - rerr = uw.LabelUseCase().RemoveWispLabel(ctx, fullID, oldLabel, actor) - } else { - rerr = uw.LabelUseCase().RemoveLabel(ctx, fullID, oldLabel, actor) - } - if rerr != nil { - removeWarn = fmt.Sprintf("failed to remove old label %s: %v", oldLabel, rerr) - } - } - if isWisp { - if aerr := uw.LabelUseCase().AddWispLabel(ctx, fullID, newLabel, actor); aerr != nil { - return setStateResult{}, "", fmt.Errorf("adding label: %w", aerr) - } - } else { - if aerr := uw.LabelUseCase().AddLabel(ctx, fullID, newLabel, actor); aerr != nil { - return setStateResult{}, "", fmt.Errorf("adding label: %w", aerr) - } - } - - return setStateResult{fullID: fullID, oldValue: oldValue, eventID: eventID, changed: true, removeWarn: removeWarn}, "bd: set-state " + fullID, nil - }) - if err != nil { - return HandleErrorRespectJSON("%v", err) - } - - if res.removeWarn != "" { - WarnError("%s", res.removeWarn) - } - - if !res.changed { - if jsonOutput { - return outputJSON(map[string]interface{}{ - "issue_id": res.fullID, - "dimension": dimension, - "value": newValue, - "changed": false, - }) - } - fmt.Printf("(no change: %s already set to %s)\n", dimension, newValue) - return nil - } - - commandDidWrite.Store(true) - - if jsonOutput { - result := map[string]interface{}{ - "issue_id": res.fullID, - "dimension": dimension, - "old_value": res.oldValue, - "new_value": newValue, - "event_id": res.eventID, - "changed": true, - } - if res.oldValue == "" { - result["old_value"] = nil - } - return outputJSON(result) - } - - fmt.Printf("%s Set %s = %s on %s\n", ui.RenderPass("✓"), dimension, newValue, res.fullID) - if res.oldValue != "" { - fmt.Printf(" Previous: %s\n", res.oldValue) - } - fmt.Printf(" Event: %s\n", res.eventID) - return nil -} - -func runStateListProxiedServer(ctx context.Context, issueID string) error { - uw, err := proxiedOpenReadUOW(ctx) - if err != nil { - return err - } - defer uw.Close(ctx) - - fullID, labels, err := proxiedStateLabels(ctx, uw, issueID) - if err != nil { - return err - } - - states := make(map[string]string) - for _, label := range labels { - if idx := strings.Index(label, ":"); idx > 0 { - states[label[:idx]] = label[idx+1:] - } - } - - if jsonOutput { - return outputJSON(map[string]interface{}{ - "issue_id": fullID, - "states": states, - }) - } - - if len(states) == 0 { - fmt.Printf("\n%s has no state labels\n", fullID) - return nil - } - - fmt.Printf("\n%s State for %s:\n", ui.RenderAccent("📊"), fullID) - for dimension, value := range states { - fmt.Printf(" %s: %s\n", dimension, value) - } - fmt.Println() - return nil -} diff --git a/internal/storage/domain/db/issue_search.go b/internal/storage/domain/db/issue_search.go index 192702440..8c0dfc74e 100644 --- a/internal/storage/domain/db/issue_search.go +++ b/internal/storage/domain/db/issue_search.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "sort" "strings" "github.com/steveyegge/beads/internal/storage/dberrors" @@ -60,20 +61,25 @@ func (r *issueSQLRepositoryImpl) searchAcrossIssuesAndWisps(ctx context.Context, } func (r *issueSQLRepositoryImpl) searchUnion(ctx context.Context, query string, filter types.IssueFilter) (domain.SearchPage, error) { - iSub, iArgs, err := r.buildUnionSubquery(query, filter, issuesFilterTables, "i") + outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) + window := searchWindowForFilter(filter) + legWindow := legWindowSQL(outerOrderBy, window) + + iSub, iArgs, err := r.buildUnionSubquery(query, filter, issuesFilterTables, "i", legWindow) if err != nil { return domain.SearchPage{}, fmt.Errorf("search union (issues): %w", err) } - wSub, wArgs, err := r.buildUnionSubquery(query, filter, wispsFilterTables, "w") + wSub, wArgs, err := r.buildUnionSubquery(query, filter, wispsFilterTables, "w", legWindow) if err != nil { return domain.SearchPage{}, fmt.Errorf("search union (wisps): %w", err) } - outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) - window := searchWindowForFilter(filter) - + // EACH LEG IS PARENTHESIZED, and it is not decoration. A leg that carries + // its own ORDER BY and LIMIT (legWindowSQL) is a syntax error inside a bare + // UNION ALL — the engine reads the clause as belonging to the union — so the + // parentheses are what let the window be pushed down at all. //nolint:gosec // G201: subqueries built from hardcoded table names and ? placeholders. - unionSQL := fmt.Sprintf("SELECT id, src FROM (%s UNION ALL %s) merged %s %s", + unionSQL := fmt.Sprintf("SELECT id, src FROM ((%s) UNION ALL (%s)) merged %s %s", iSub, wSub, outerOrderBy, window.sql) args := make([]any, 0, len(iArgs)+len(wArgs)) @@ -88,6 +94,7 @@ func (r *issueSQLRepositoryImpl) searchUnion(ctx context.Context, query string, if err != nil { return domain.SearchPage{}, fmt.Errorf("search union: %w", err) } + page.sortGoSide(filter.SortBy, filter.SortDesc) hasMore, err := page.finishWindow(window) if err != nil { return domain.SearchPage{}, err @@ -106,7 +113,19 @@ func (r *issueSQLRepositoryImpl) searchUnion(ctx context.Context, query string, return domain.SearchPage{Items: out, HasMore: hasMore}, nil } -func (r *issueSQLRepositoryImpl) buildUnionSubquery(query string, filter types.IssueFilter, tables filterTables, srcTag string) (string, []any, error) { +// buildUnionSubquery renders one leg of the wisp-inclusive UNION ALL. +// +// legWindow is the ORDER BY and LIMIT the leg carries, from legWindowSQL. It is +// the leg's whole share of the caller's window, and pushing it down is sound +// for the reason a top-N is: under a TOTAL order, a row in the union's top-N is +// in its own leg's top-N too, because anything ahead of it in its leg is ahead +// of it globally. Every ORDER BY sqlbuild renders ends in `id ASC`, so the +// order is total and there are no ties for the bound to break arbitrarily. +// +// It is empty when the sort has no SQL ORDER BY, and that is not a missed case: +// a LIMIT on an UNORDERED leg is the bug this exists to remove, one level +// further down and harder to see. +func (r *issueSQLRepositoryImpl) buildUnionSubquery(query string, filter types.IssueFilter, tables filterTables, srcTag, legWindow string) (string, []any, error) { plan := buildLabelDrivenSearch(filter, tables) whereClauses, args, err := buildIssueFilterClauses(query, plan.Filter, tables) if err != nil { @@ -121,12 +140,33 @@ func (r *issueSQLRepositoryImpl) buildUnionSubquery(query string, filter types.I if plan.Distinct { selectKw = "SELECT DISTINCT" } - //nolint:gosec // G201: srcTag is a hardcoded 'i' or 'w'; fromSQL/whereSQL composed from fixed table names and ? placeholders. - sub := fmt.Sprintf("%s id, '%s' AS src, %s FROM %s %s", - selectKw, srcTag, unionSortColumnsSQL, plan.FromSQL, whereSQL) + //nolint:gosec // G201: srcTag is a hardcoded 'i' or 'w'; fromSQL/whereSQL/legWindow composed from fixed table names, sort aliases and ? placeholders. + sub := fmt.Sprintf("%s id, '%s' AS src, %s FROM %s %s %s", + selectKw, srcTag, unionSortColumnsSQL, plan.FromSQL, whereSQL, legWindow) return sub, args, nil } +// legWindowSQL is the window each UNION leg carries: the SAME ordering the +// outer query applies, plus the deepest row index that outer query can reach. +// +// THE ORDER CLAUSE IS REUSED VERBATIM rather than rebuilt, and that is what +// makes the bound safe. unionOrderBySQL names `id` and the `sort_*` aliases, +// both of which every leg projects, so one string is valid in both positions +// and the leg cannot come to rest in a different order than the one the outer +// query is about to cut against. +// +// legLimit is the outer bound PLUS any offset the engine carries, because those +// are rows the outer query reads and discards — a leg cut to the page size +// alone would starve an offset page of exactly the rows it was going to skip +// past. An offset this package keeps for itself (the capped case) is already +// inside the bound. +func legWindowSQL(outerOrderBy string, w searchWindow) string { + if outerOrderBy == "" || w.legLimit <= 0 { + return "" + } + return fmt.Sprintf("%s LIMIT %d", outerOrderBy, w.legLimit) +} + func (r *issueSQLRepositoryImpl) fetchIssuesByIDs(ctx context.Context, ids []string, tables filterTables, filter types.IssueFilter) (map[string]*types.Issue, error) { if len(ids) == 0 { return map[string]*types.Issue{}, nil @@ -445,11 +485,18 @@ type idSrcPage struct { // canonical. And its own check text names the hard failure as the damage: // "stale issues-table copies break every lookup for the affected IDs". // -// So the durable row is dropped wherever it sits and the wisp row is kept at -// its own position, which is the page the per-table seam produces for the same -// data. A page can come back one row short of its limit when this fires; that -// is a corrupt row being withheld, not a paging bug, and the repair is a doctor -// run. +// So the durable row is dropped and the wisp row is kept at its own position, +// which is the page the per-table seam produces for the same data. A page can +// come back one row short of its limit when this fires; that is a corrupt row +// being withheld, not a paging bug, and the repair is a doctor run. +// +// THE DROP REACHES ONLY THE SCANNED WINDOW, which is narrower than "wherever it +// sits" — the phrase this used to use. Both copies have to be inside the rows +// this query returned for the shadow to be seen at all, and under a bounded +// search they need not be: legs carry the outer bound now (legWindowSQL), so a +// durable copy inside the window whose wisp twin ranks past it survives the +// scan and is reported. That is a corrupt store answering approximately, not a +// second bug; the detector is `bd doctor`, not this loop. func scanIDSrcPage(rows *sql.Rows) (idSrcPage, error) { defer func() { _ = rows.Close() }() @@ -522,6 +569,49 @@ func reassembleBySrc[T comparable](ordered []idSrcRef, issues, wisps map[string] return out } +// sortGoSide establishes the order for a sort key SQL renders no ORDER BY for, +// BEFORE finishWindow's trim decides which rows the page keeps. +// +// It is the union seam's copy of issueops.sortMergedResults, and it exists for +// the identical reason that one gives: "a concatenation of two independently- +// ordered legs" trimmed without a re-sort "keeps an arbitrary prefix of the +// concatenation" rather than the top-n. Under a Go-side sort the legs are not +// merely independently ordered, they are UNORDERED — searchWindowFor pushes no +// bound in that case precisely so the whole matching set arrives here — and +// this is where the requested order first exists. +// +// The only such key is "id", and the id is on the ref already, so no row has to +// be hydrated to be placed. The comparison is the byte order SQL would apply +// and the byte order sqlbuild.Less applies for this key; the natural-numeric +// order a human reads (bd-9 before bd-10) is applied above, on the delivered +// page, by workapi.CompareIssuesBy. This decides MEMBERSHIP; that decides +// DISPLAY. +// +// It honors sortDesc, which sqlbuild.Less does not for this key — the per-table +// seam therefore answers a reversed id page with the byte-FIRST rows. That is a +// live sibling bug, named here rather than mirrored. +func (p *idSrcPage) sortGoSide(sortBy string, sortDesc bool) { + if !sqlbuild.IsGoSideSort(sortBy) || len(p.ordered) <= 1 { + return + } + sort.SliceStable(p.ordered, func(i, j int) bool { + if sortDesc { + return p.ordered[i].id > p.ordered[j].id + } + return p.ordered[i].id < p.ordered[j].id + }) + p.issueIDs = p.issueIDs[:0] + p.wispIDs = p.wispIDs[:0] + for _, r := range p.ordered { + switch r.src { + case "i": + p.issueIDs = append(p.issueIDs, r.id) + case "w": + p.wispIDs = append(p.wispIDs, r.id) + } + } +} + // finishWindow is finishWindow's shape for a merged id page: the same cap, // skip and trim, with the per-table id lists rebuilt once from whatever // survives. @@ -609,16 +699,48 @@ type searchWindow struct { // row moves it by one. rowCap int capSource string + // legLimit is the deepest row index the outer query can reach, and so the + // bound each UNION leg may carry. 0 means the legs stay unbounded, either + // because the search is unbounded or because its order is not one SQL can + // express — see legWindowSQL. + legLimit int } -func searchWindowFor(limit, offset, maxRows int, maxRowsSource string) searchWindow { +// searchWindowFor sizes one search's window. goSideSort reports a sort key SQL +// renders no ORDER BY for (sqlbuild.IsGoSideSort — "id", which needs the +// natural-numeric comparison that puts bd-9 before bd-10). +// +// A PAGE BOUND IS ONLY EVER PUSHED UNDER AN ORDER THE QUERY CAN EXPRESS, and +// that is the whole of what goSideSort changes here. A LIMIT with no ORDER BY +// does not return the first n rows; it returns n rows. Measured on a two-plane +// store with ids interleaved across the planes, `--sort id --limit 5` answered +// with five DURABLE rows and no wisp at all — a page whose order was right, and +// whose membership silently excluded an entire plane. +// +// So a Go-side sort takes the same window internal/workapi.SQLLimit gives the +// reader role for the same reason: none. The query returns the complete +// matching set, the comparator that can express the order runs over it, and +// FinishPageAt's cut is the only thing bounding the page — "this is where the +// requested order first exists", as its doc says. The CAP is not waived with +// it: maxRows still sizes a bound, so a Go-side sort over a set too large to +// order in memory reports ErrTooManyRows instead of scanning the workspace. +// +// The cost is stated rather than absorbed: a caller combining `--sort id` with +// a small limit and NO cap now reads every matching row where it used to read +// limit+1. It read the wrong ones, and the reader role has always paid this +// price on the same query. +func searchWindowFor(limit, offset, maxRows int, maxRowsSource string, goSideSort bool) searchWindow { // The window the cap is sized against is the one the query TOUCHES, so a // skip this package keeps has to be inside the bound. An unbounded limit // already carries every matching row and needs no widening — the same two // lines WithRowsBeforeThePage runs on the filter for the other seam. + pageLimit := limit + if goSideSort { + pageLimit = 0 + } skipHere := maxRows > 0 && offset > 0 - touched := limit - if skipHere && limit > 0 { + touched := pageLimit + if skipHere && pageLimit > 0 { touched += offset } bound, rowCap := issueops.SearchProbeLimit(touched, maxRows) @@ -628,20 +750,33 @@ func searchWindowFor(limit, offset, maxRows int, maxRowsSource string) searchWin w.skip = offset case skipHere: w.skip, w.sql = offset, fmt.Sprintf("LIMIT %d", bound) + w.legLimit = bound case offset > 0: w.sql = fmt.Sprintf("LIMIT %d OFFSET %d", bound, offset) + w.legLimit = bound + offset default: w.sql = fmt.Sprintf("LIMIT %d", bound) + w.legLimit = bound + } + if goSideSort { + // The bound that survives above is the CAP's, not the page's, and a cap + // is a defensive ceiling on an unordered scan rather than a top-n. It + // stays on the outer query, where trimming it is the caller's error; + // it must not become a leg's idea of which rows matter. + w.legLimit = 0 } return w } func searchWindowForFilter(filter types.IssueFilter) searchWindow { - return searchWindowFor(filter.Limit, filter.Offset, filter.MaxRows, filter.MaxRowsSource) + return searchWindowFor(filter.Limit, filter.Offset, filter.MaxRows, filter.MaxRowsSource, sqlbuild.IsGoSideSort(filter.SortBy)) } +// readyWindowForFilter sizes the ready query's window. types.WorkFilter carries +// no sort key — ready has one policy order, which SQL renders — so no Go-side +// sort is reachable here. func readyWindowForFilter(filter types.WorkFilter) searchWindow { - return searchWindowFor(filter.Limit, filter.Offset, filter.MaxRows, filter.MaxRowsSource) + return searchWindowFor(filter.Limit, filter.Offset, filter.MaxRows, filter.MaxRowsSource, false) } // finishWindow is the Go half of a window: the defensive cap against what the diff --git a/internal/storage/domain/db/issue_search_counts.go b/internal/storage/domain/db/issue_search_counts.go index 93dc00b86..98aac4798 100644 --- a/internal/storage/domain/db/issue_search_counts.go +++ b/internal/storage/domain/db/issue_search_counts.go @@ -59,20 +59,25 @@ func (r *issueSQLRepositoryImpl) searchAcrossIssuesAndWispsWithCounts(ctx contex } func (r *issueSQLRepositoryImpl) searchUnionWithCounts(ctx context.Context, query string, filter types.IssueFilter, wispDepsExist bool) (domain.SearchCountsPage, error) { - iSub, iArgs, err := r.buildUnionSubquery(query, filter, issuesFilterTables, "i") + outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) + window := searchWindowForFilter(filter) + legWindow := legWindowSQL(outerOrderBy, window) + + iSub, iArgs, err := r.buildUnionSubquery(query, filter, issuesFilterTables, "i", legWindow) if err != nil { return domain.SearchCountsPage{}, fmt.Errorf("search union with counts (issues): %w", err) } - wSub, wArgs, err := r.buildUnionSubquery(query, filter, wispsFilterTables, "w") + wSub, wArgs, err := r.buildUnionSubquery(query, filter, wispsFilterTables, "w", legWindow) if err != nil { return domain.SearchCountsPage{}, fmt.Errorf("search union with counts (wisps): %w", err) } - outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) - window := searchWindowForFilter(filter) - + // EACH LEG IS PARENTHESIZED, and it is not decoration. A leg that carries + // its own ORDER BY and LIMIT (legWindowSQL) is a syntax error inside a bare + // UNION ALL — the engine reads the clause as belonging to the union — so the + // parentheses are what let the window be pushed down at all. //nolint:gosec // G201: subqueries built from hardcoded table names and ? placeholders. - unionSQL := fmt.Sprintf("SELECT id, src FROM (%s UNION ALL %s) merged %s %s", + unionSQL := fmt.Sprintf("SELECT id, src FROM ((%s) UNION ALL (%s)) merged %s %s", iSub, wSub, outerOrderBy, window.sql) args := make([]any, 0, len(iArgs)+len(wArgs)) @@ -87,6 +92,7 @@ func (r *issueSQLRepositoryImpl) searchUnionWithCounts(ctx context.Context, quer if err != nil { return domain.SearchCountsPage{}, fmt.Errorf("search union with counts: %w", err) } + page.sortGoSide(filter.SortBy, filter.SortDesc) hasMore, err := page.finishWindow(window) if err != nil { return domain.SearchCountsPage{}, err diff --git a/internal/storage/domain/db/issue_search_union_window_test.go b/internal/storage/domain/db/issue_search_union_window_test.go new file mode 100644 index 000000000..4809ae167 --- /dev/null +++ b/internal/storage/domain/db/issue_search_union_window_test.go @@ -0,0 +1,194 @@ +package db + +import ( + "fmt" + + "github.com/steveyegge/beads/internal/storage/domain" + "github.com/steveyegge/beads/internal/types" +) + +func (s *testSuite) TestIssueSearchUnionWindow() { + s.Run("GoSideSortWithALimitAnswersTheGloballyCorrectPage", s.unionGoSideSortKeepsTheRightSubset) + s.Run("TheCountsTwinAnswersTheSamePage", s.unionCountsGoSideSortKeepsTheRightSubset) + s.Run("TheComposedQueryRunsWithBoundedLegs", s.unionComposedQueryWithBoundedLegs) + s.Run("LegsCarryTheWindowUnderASortSQLCanExpress", s.unionLegsCarryTheWindow) + s.Run("LegsStayUnboundedUnderAGoSideSort", s.unionLegsStayUnboundedUnderGoSideSort) +} + +// seedTwoPlanes writes n durable rows and n ephemeral rows under one id prefix, +// INTERLEAVED by id so that neither plane holds a prefix of the id order: the +// durable rows take the even suffixes and the wisps the odd ones. A page of the +// first k ids therefore has to come from both planes, which is what makes a +// per-leg subset observable — a leg that answered with an arbitrary k rows, or +// a union that took its k from whichever leg the engine emitted first, produces +// a visibly different set rather than the same one in a different order. +func (s *testSuite) seedTwoPlanes(prefix string, n int) []string { + r := s.issueRepo() + ids := make([]string, 0, 2*n) + for i := 0; i < 2*n; i++ { + id := fmt.Sprintf("%s-%03d", prefix, i) + issue := newTestIssue(id, fmt.Sprintf("row %d", i)) + opts := domain.InsertIssueOpts{} + if i%2 == 1 { + issue.Ephemeral = true + opts.UseWispsTable = true + } + s.Require().NoError(r.Insert(s.Ctx(), issue, "tester", opts)) + ids = append(ids, id) + } + return ids +} + +// unionGoSideSortKeepsTheRightSubset is the F5 case. SortBy "id" is the one key +// sqlbuild renders no ORDER BY for, so before this fix the union query was +// `SELECT id, src FROM (leg UNION ALL leg) merged LIMIT k` — a LIMIT with no +// ordering anywhere in the statement, cutting an arbitrary k rows out of a +// concatenation. The Go-side sort then put those arbitrary rows in order, which +// is why the failure reads as a correct-looking page: the ORDER is right and +// the MEMBERSHIP is wrong. +func (s *testSuite) unionGoSideSortKeepsTheRightSubset() { + const prefix = "bd-uw-gos" + ids := s.seedTwoPlanes(prefix, 12) + r := s.issueRepo() + + const limit = 5 + page, err := r.SearchAcrossIssuesAndWisps(s.Ctx(), "", + types.IssueFilter{IDPrefix: prefix + "-", SortBy: "id", Limit: limit}) + s.Require().NoError(err) + + // The ids are zero-padded, so byte order and natural order agree and the + // answer is unambiguous: the first `limit` of the seeded sequence, which + // spans both planes. + want := ids[:limit] + s.Equal(want, idsFrom(page), + "a limited page under a Go-side sort must be the globally first rows, not an arbitrary subset of the concatenation") + s.True(page.HasMore) +} + +// unionCountsGoSideSortKeepsTheRightSubset is the same case on the counts twin, +// and that is the twin the live callers reach. workapi.BuildQueryPlan pushes +// SortBy and Limit straight onto the filter for any filter-expressible query — +// no SQLLimit guard, and DefaultQueryLimit means a limit is always set — so +// `bd query '' --sort id` and GET /v0/beads/issues/query?sort=id both +// arrived here with SortBy "id" and a bound. Its own comment says the pushdown +// makes "the page the first N in the requested order on every backend", which +// was true of every key except the one IsGoSideSort names. +func (s *testSuite) unionCountsGoSideSortKeepsTheRightSubset() { + const prefix = "bd-uw-cgos" + ids := s.seedTwoPlanes(prefix, 12) + r := s.issueRepo() + + const limit = 5 + page, err := r.SearchAcrossIssuesAndWispsWithCounts(s.Ctx(), "", + types.IssueFilter{IDPrefix: prefix + "-", SortBy: "id", Limit: limit}) + s.Require().NoError(err) + + got := make([]string, 0, len(page.Items)) + for _, item := range page.Items { + got = append(got, item.Issue.ID) + } + s.Equal(ids[:limit], got, + "the counts twin bounds the same page the plain twin does") + s.True(page.HasMore) +} + +// unionComposedQueryWithBoundedLegs runs the WHOLE query — both twins, with and +// without a text term, ordered and unordered, bounded and not — rather than a +// leg on its own. +// +// It exists because the leg probe below does not cover this and cannot: it +// executes each subquery standalone, where `... ORDER BY x LIMIT n` is +// perfectly legal. Composed into a bare `leg UNION ALL leg` the same string is +// a SYNTAX ERROR, the engine reading the trailing clause as the union's own, +// and `bd search` failed outright until the legs were parenthesized. A probe +// that measures the part is not evidence about the whole. +func (s *testSuite) unionComposedQueryWithBoundedLegs() { + const prefix = "bd-uw-cmp" + s.seedTwoPlanes(prefix, 6) + r := s.issueRepo() + + for _, tc := range []struct { + what string + query string + filter types.IssueFilter + want int + }{ + {"ordered and bounded", "", types.IssueFilter{IDPrefix: prefix + "-", SortBy: "created", Limit: 4}, 4}, + {"ordered, bounded, with an offset", "", types.IssueFilter{IDPrefix: prefix + "-", SortBy: "priority", Limit: 3, Offset: 2}, 3}, + {"ordered and bounded with a text term", "row", types.IssueFilter{IDPrefix: prefix + "-", SortBy: "updated", Limit: 4}, 4}, + {"the default order, bounded", "", types.IssueFilter{IDPrefix: prefix + "-", Limit: 5}, 5}, + {"ordered and unbounded", "", types.IssueFilter{IDPrefix: prefix + "-", SortBy: "created"}, 12}, + {"a Go-side sort, bounded", "", types.IssueFilter{IDPrefix: prefix + "-", SortBy: "id", Limit: 4}, 4}, + } { + s.Run(tc.what, func() { + page, err := r.SearchAcrossIssuesAndWisps(s.Ctx(), tc.query, tc.filter) + s.Require().NoError(err) + s.Len(page.Items, tc.want) + + counts, err := r.SearchAcrossIssuesAndWispsWithCounts(s.Ctx(), tc.query, tc.filter) + s.Require().NoError(err) + s.Len(counts.Items, tc.want) + }) + } +} + +// unionLegsCarryTheWindow is the F4 measurement: a ROW-COUNT PROBE on the legs +// themselves. It builds the same subqueries searchUnion builds and executes +// each one on its own, so "the legs stop over-fetching" is a number rather than +// an assertion about SQL text. +func (s *testSuite) unionLegsCarryTheWindow() { + const prefix = "bd-uw-leg" + s.seedTwoPlanes(prefix, 20) // 20 durable + 20 wisps + r := NewIssueSQLRepository(s.Runner()).(*issueSQLRepositoryImpl) + + filter := types.IssueFilter{IDPrefix: prefix + "-", SortBy: "created", Limit: 4} + outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) + s.Require().NotEmpty(outerOrderBy, "this case needs a sort SQL can express") + window := searchWindowForFilter(filter) + + for _, leg := range []struct { + name string + tables filterTables + tag string + }{ + {"issues", issuesFilterTables, "i"}, + {"wisps", wispsFilterTables, "w"}, + } { + sub, args, err := r.buildUnionSubquery("", filter, leg.tables, leg.tag, legWindowSQL(outerOrderBy, window)) + s.Require().NoError(err) + + rows, err := s.db.QueryContext(s.Ctx(), sub, args...) + s.Require().NoError(err) + scanned := 0 + for rows.Next() { + scanned++ + } + s.Require().NoError(rows.Err()) + s.Require().NoError(rows.Close()) + + // 20 rows match in each plane. The outer query can consume at most + // window.legLimit of them, so a leg that returns more has done work no + // caller can observe. + s.Positive(window.legLimit, "a bounded search must bound its legs") + s.LessOrEqual(scanned, window.legLimit, + "%s leg returned %d rows for a window of %d", leg.name, scanned, window.legLimit) + s.Equal(window.legLimit, scanned, + "%s leg should fill the window exactly: 20 rows match", leg.name) + } +} + +// unionLegsStayUnboundedUnderGoSideSort pins the other half of the same rule. A +// LIMIT on a leg is only sound when the leg is ORDERED, so the sort SQL cannot +// express must not get one — a bounded leg with no ORDER BY is precisely the F5 +// bug, moved one level down and made harder to see. +func (s *testSuite) unionLegsStayUnboundedUnderGoSideSort() { + filter := types.IssueFilter{IDPrefix: "bd-uw-none-", SortBy: "id", Limit: 4} + outerOrderBy := unionOrderBySQL(filter.SortBy, filter.SortDesc) + s.Empty(outerOrderBy, "sortBy=id is the Go-side sort: SQL renders no ORDER BY for it") + + window := searchWindowForFilter(filter) + s.Empty(window.sql, "a Go-side sort takes no SQL window: the page is cut where the requested order first exists") + s.Zero(window.legLimit, "and with no outer window there is nothing to push into the legs") + s.Empty(legWindowSQL(outerOrderBy, window)) + s.Equal(4, window.limit, "the Go half still trims to the caller's page") +} diff --git a/internal/storage/domain/db/issue_search_window_test.go b/internal/storage/domain/db/issue_search_window_test.go index 5a4bf92c4..0f333140a 100644 --- a/internal/storage/domain/db/issue_search_window_test.go +++ b/internal/storage/domain/db/issue_search_window_test.go @@ -23,9 +23,11 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { limit int offset int maxRows int + goSide bool wantSQL string wantSkip int wantCap int + wantLeg int }{ { // The divergence this table exists for. `--limit 10 --offset 2 @@ -40,6 +42,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 4", wantSkip: 2, wantCap: 3, + wantLeg: 4, }, { // What issueops.Reader hands this seam for that same request, @@ -52,6 +55,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 4", wantSkip: 0, wantCap: 3, + wantLeg: 4, }, { // Limit+Offset == MaxRows. The probe row must not fire a cap the @@ -65,6 +69,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 4", wantSkip: 1, wantCap: 4, + wantLeg: 4, }, { what: "one more row of offset, and the same request is over the cap", @@ -74,6 +79,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 4", wantSkip: 2, wantCap: 3, + wantLeg: 4, }, { what: "under the cap the bound is the touched window plus the probe row", @@ -83,6 +89,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 3", wantSkip: 1, wantCap: 3, + wantLeg: 3, }, { // An unlimited page needs no widening: it already touches every @@ -94,6 +101,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 4", wantSkip: 2, wantCap: 3, + wantLeg: 4, }, { // NO CAP, so nothing counts rows and the engine does the skipping — @@ -106,6 +114,7 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSQL: "LIMIT 3 OFFSET 1", wantSkip: 0, wantCap: 0, + wantLeg: 4, }, { // No LIMIT to hang an OFFSET on. MySQL has none, and the sentinel @@ -118,9 +127,48 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { wantSkip: 1, wantCap: 0, }, + { + // THE GO-SIDE SORT, and the case F5 was. `--sort id` renders no + // ORDER BY, so a LIMIT here cuts n arbitrary rows out of a + // two-plane concatenation. The page bound comes off entirely and + // the comparator upstream does the cutting. + what: "a Go-side sort takes no page bound at all", + limit: 5, + offset: 0, + maxRows: 0, + goSide: true, + wantSQL: "", + wantSkip: 0, + wantCap: 0, + wantLeg: 0, + }, + { + // The CAP is not waived with the page bound: an unordered scan + // still answers ErrTooManyRows rather than reading a workspace. + what: "a Go-side sort keeps the cap's bound and pushes none of it down", + limit: 5, + offset: 0, + maxRows: 3, + goSide: true, + wantSQL: "LIMIT 4", + wantSkip: 0, + wantCap: 3, + wantLeg: 0, + }, + { + what: "a Go-side sort keeps its own offset, the way an unbounded scan does", + limit: 5, + offset: 2, + maxRows: 0, + goSide: true, + wantSQL: "", + wantSkip: 2, + wantCap: 0, + wantLeg: 0, + }, } { t.Run(tc.what, func(t *testing.T) { - w := searchWindowFor(tc.limit, tc.offset, tc.maxRows, "--max-rows") + w := searchWindowFor(tc.limit, tc.offset, tc.maxRows, "--max-rows", tc.goSide) where := fmt.Sprintf("Limit=%d Offset=%d MaxRows=%d", tc.limit, tc.offset, tc.maxRows) if w.sql != tc.wantSQL { t.Errorf("%s rendered %q, want %q", where, w.sql, tc.wantSQL) @@ -131,6 +179,9 @@ func TestSearchWindowForCountsTheOffsetIntoTheCap(t *testing.T) { if w.rowCap != tc.wantCap { t.Errorf("%s enforces rowCap=%d, want %d", where, w.rowCap, tc.wantCap) } + if w.legLimit != tc.wantLeg { + t.Errorf("%s pushes legLimit=%d into each union leg, want %d", where, w.legLimit, tc.wantLeg) + } if w.limit != tc.limit { t.Errorf("%s trims to %d, want the page the caller asked for", where, w.limit) } diff --git a/internal/storage/issueops/count.go b/internal/storage/issueops/count.go index d880f5ca1..460fb7e6b 100644 --- a/internal/storage/issueops/count.go +++ b/internal/storage/issueops/count.go @@ -56,8 +56,10 @@ func CountIssuesInTx(ctx context.Context, tx DBTX, query string, filter types.Is // it is not one — it collapses a cross-table duplicate to the canonical wisp // row and answers (be-iabdi), as the union seam now does too. So a store // holding one dual-resident id counts it TWICE here while the listing shows - // it once. `bd doctor --check=cross-table` is the detector, and - // `--check=validate --fix` the repair. + // it once. `bd doctor`'s Cross-Table Duplicates check is the detector, and + // `--check=validate --fix` the repair. (There is no `--check=cross-table` + // selector: the four the flag accepts are artifacts, conventions, pollution + // and validate, and the cross-table check runs in the default sweep.) wispCount, wispErr := countTableInTx(ctx, tx, query, filter, WispsFilterTables) if wispErr != nil && !isTableNotExistError(wispErr) { return 0, fmt.Errorf("count wisps (merge): %w", wispErr)