beads/internal/storage/domain/issue.go
Julian Knutsen d801ec43dc
feat(issueops): put every issue capability behind one library surface
Beads had two implementations of most issue verbs: one for the direct route
(the CLI talking to a store it opened itself) and one for the proxied-server
route (the CLI talking through a unit of work over a running Dolt server).
They drifted — different commit messages, different transaction boundaries,
different refusals, occasionally different answers.

Seven role interfaces now live in the leaf package issueops: Lifecycle,
Reader, ReadyClaimer, BatchCloser, DependencyEditor, Commenter and Relations.
Each is reached only through a store accessor, never a constructor, because
the accessor is where the hook and telemetry decorators add their layers.
Three implementations back every role — dolt, embeddeddolt, and the
unit-of-work provider the proxied route uses. Both routes call the same role
for close, dep add/remove/list, comment, ready --claim and update.

A depguard rule (httpapi-domain-boundary) denies internal/storage/domain from
internal/httpapi, so a handler cannot reach uw.IssueUseCase() past the roles
and open a second front door one lint-clean commit at a time.

Behaviour users will notice, all in the CHANGELOG:
  - bd close a b c is one Dolt commit, not three, and --claim-next moved
    inside that transaction so it fires only when something landed
  - the open-children guard moved into the transaction and its hand-rolled
    cmd/bd copy is gone, removing a read-then-write window that made
    bd close <child> <parent> depend on argument order
  - bd dep add --file honours deferred version commit
  - embedded bd comment writes the role's message and author
  - dep verb commit messages are the role's on the direct route

Wisp-sourced dependency edges are restored. Moving bd dep add onto the role
had pinned an edge's SOURCE to the issues plane, and the two routes did not
even fail alike: direct refused with "issue <id> not found", proxied threw an
fk_dep_issue violation. The comment justifying the pin claimed the issues
plane was "the only one its unit-of-work sibling can reach" — that was false;
domain.DependencyUseCase declared a full set of wisp twins with no production
caller. All three implementations now route per edge by the edge's own
source. A mixed request stays one transaction: parent-child-first ordering
and the whole-graph cycle gate span both planes, and a refusal rolls back
both. Behind the role, three bulk add verbs collapsed into one source-routed
AddDependencies, so no plane-pinned variant is left to reach for by mistake.

Six Provenance fields with no production caller were deleted rather than
wired — CloseRequest, CloseBatchRequest, ClaimNextRequest, AddCommentRequest,
AddDependenciesRequest, RemoveDependencyRequest. Each role already composes an
id-bearing message, so a caller could only restate the default and make it
unreachable from production. Two are not caller-settable even in principle:
CloseBatchCommitMessage names what LANDED and ClaimNextCommitMessage names the
id the claim WON. UpdateRequest and ReopenRequest keep theirs; commit messages
users see are unchanged.

Verified: build, vet and gofmt clean; golangci-lint 0 issues; protocol corpus
byte-identical under BEADS_PROTOCOL_REQUIRE_DOLT=1; all twelve
DependencyEditor conformance wirings green (four contract functions against
each implementation); TestEmbeddedDepAddWispSource runs bd dep add <wisp-id>
at the front door and was demonstrated RED against the pinned routing first.
Three internal/storage/dolt failures reproduce on pristine origin/main.

Known gaps are enumerated in the PR description rather than left to be found:
comment listing is on no role, bd dep tree and bd dep cycles have no role,
bd create and bd reopen are on Lifecycle only on the direct route, per-edge
line attribution is lost in bd dep add --file, multi-store bd close
--claim-next rides only the first store's batch, and the is_blocked staging gap
(bd-2y9ke, predates this change). Seven of the eight role interfaces now have a
shared conformance contract in backend/conformance, run at all three
implementations; main's Claimer, added in parallel by #5334, is the eighth and
has none yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 12:27:56 -07:00

1809 lines
67 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"context"
"fmt"
"strings"
"time"
"github.com/steveyegge/beads/internal/idgen"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dberrors"
"github.com/steveyegge/beads/internal/types"
publicops "github.com/steveyegge/beads/issueops"
)
type InsertIssueOpts struct {
UseWispsTable bool
CreateOnly bool
}
type IssueTableOpts struct {
UseWispsTable bool
}
type ClaimRowResult struct {
Updated bool
// CurrentAssignee is the assignee found on the 0-row disambiguation
// read; CurrentAssigneeIsPool marks it as a claim.pools alias, so the
// caller reports a status conflict instead of a held-by-someone
// refusal (a pool alias never "holds" a claim).
CurrentAssignee string
CurrentAssigneeIsPool bool
CurrentStatus types.Status
StartedAtWasZero bool
OldIssue *types.Issue
}
type IssueSQLRepository interface {
Insert(ctx context.Context, issue *types.Issue, actor string, opts InsertIssueOpts) error
InsertBatch(ctx context.Context, issues []*types.Issue, actor string, opts InsertIssueOpts) error
MovePersistence(ctx context.Context, id string, mode types.PersistenceMode) (changed bool, err error)
Update(ctx context.Context, id string, updates map[string]any, actor string, opts IssueTableOpts) error
Claim(ctx context.Context, id, actor string, opts IssueTableOpts) (ClaimRowResult, error)
Get(ctx context.Context, id string, opts IssueTableOpts) (*types.Issue, error)
AsOf(ctx context.Context, id, ref string) (*types.Issue, error)
GetByIDs(ctx context.Context, ids []string, opts IssueTableOpts) ([]*types.Issue, error)
Exists(ctx context.Context, id string, opts IssueTableOpts) (bool, error)
CountForPrefix(ctx context.Context, prefix string, opts IssueTableOpts) (int, error)
NextCounterID(ctx context.Context, prefix string) (int, error)
SearchAcrossIssuesAndWisps(ctx context.Context, query string, filter types.IssueFilter) (SearchPage, error)
SearchAcrossIssuesAndWispsWithCounts(ctx context.Context, query string, filter types.IssueFilter) (SearchCountsPage, error)
SearchIssueIDs(ctx context.Context, query string, filter types.IssueFilter) ([]string, error)
GetReadyWork(ctx context.Context, filter types.WorkFilter) (SearchPage, error)
GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) (SearchCountsPage, error)
GetDescendants(ctx context.Context, rootID string, filter types.IssueFilter) ([]*types.Issue, error)
Delete(ctx context.Context, id string, opts IssueTableOpts) error
DeleteByIDs(ctx context.Context, ids []string, opts IssueTableOpts) (int, error)
PartitionWispIDs(ctx context.Context, ids []string) (wispIDs, regularIDs []string, err error)
FindAllDependents(ctx context.Context, ids []string) ([]string, error)
FindWispDependentsRecursive(ctx context.Context, ids []string) (map[string]bool, error)
AffectedByDeletion(ctx context.Context, issueIDs, wispIDs []string) (affectedIssues, affectedWisps []string, err error)
RecomputeIsBlocked(ctx context.Context, issueIDs, wispIDs []string) error
Close(ctx context.Context, id string, params CloseRowParams, actor string, opts IssueTableOpts) (CloseRowResult, error)
CloseChecked(ctx context.Context, id string, params CloseRowParams, actor string, force bool) (CloseRowResult, error)
Reopen(ctx context.Context, id string, params ReopenRowParams, actor string, opts IssueTableOpts) (ReopenRowResult, error)
GetNewlyUnblockedByClose(ctx context.Context, closedID string) ([]*types.Issue, error)
ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error)
ClaimReadyWisp(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error)
GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error)
GetStatistics(ctx context.Context) (*types.Statistics, error)
CountIssues(ctx context.Context, query string, filter types.IssueFilter) (int64, error)
CountIssuesByGroup(ctx context.Context, filter types.IssueFilter, groupBy string) (map[string]int, error)
History(ctx context.Context, id string) ([]*storage.HistoryEntry, error)
IterEvents(ctx context.Context, id string, limit int) (storage.Iter[types.Event], error)
GetStaleIssues(ctx context.Context, filter types.StaleFilter) ([]*types.Issue, error)
GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error)
UnclaimIssue(ctx context.Context, id, actor string, force bool) error
ReclaimExpiredLeases(ctx context.Context, olderThan time.Duration, filter types.ReclaimFilter, actor string) ([]types.ReclaimedLease, error)
}
type CloseRowParams struct {
Reason string
Session string
}
type CloseRowResult struct {
Updated bool
AlreadyClosed bool
IsWisp bool
OpenChildren int
}
type ReopenRowParams struct {
Reason string
}
type ReopenRowResult struct {
Updated bool
AlreadyOpen bool
IsWisp bool
}
type DeleteIssuesParams struct {
IDs []string
Cascade bool
DryRun bool
UpdateTextReferences bool
// EnforceCascadePolicy selects embedded-parity dependent handling
// (issueops.DeleteIssuesInTx). When false — the legacy default used by the
// proxied-server delete command, which always cascades — deletion expands to
// all transitive dependents regardless of Cascade/Force. When true,
// Cascade/Force choose the behavior:
// Cascade=true → delete all transitive dependents
// Cascade=false, Force=false → refuse if any external dependent exists
// Cascade=false, Force=true → orphan external dependents (delete only IDs)
EnforceCascadePolicy bool
Force bool
}
type DeleteIssuesResult struct {
DeletedCount int
DependenciesCount int
LabelsCount int
EventsCount int
ReferencesUpdated int
// OrphanedIssues lists external dependents left behind by a force delete
// (Cascade=false, Force=true), or the blocking dependents reported with the
// refusal error (Cascade=false, Force=false). Empty on the cascade path.
OrphanedIssues []string
}
type DeletePreview struct {
Issues map[string]*types.Issue
ConnectedIssues map[string]*types.Issue
DepRecords map[string][]*types.Dependency
NotFound []string
}
type SearchPage struct {
Items []*types.Issue
HasMore bool
}
type SearchCountsPage struct {
Items []*types.IssueWithCounts
HasMore bool
}
type CreateIssueParams struct {
Issue *types.Issue
ExplicitID string
ParentID string
Labels []string
InheritLabelsFromParent bool
Dependencies []DependencySpec
WaitsFor *WaitsForSpec
DiscoveredFromParent string
ForcePrefix bool
CreateOnly bool
Comments []*types.Comment
}
type DependencySpec struct {
Type types.DependencyType
TargetID string
SwapDirection bool
Metadata string
ThreadID string
}
type WaitsForSpec struct {
SpawnerID string
Gate string
}
type CreateIssueResult struct {
Issue *types.Issue
InheritedLabels []string
PostCreateWrites bool
}
type CreateIssuesResult struct {
Issues []*types.Issue
}
type GraphPlan struct {
Nodes []GraphNode
Edges []GraphEdge
}
type GraphNode struct {
Key string
Issue *types.Issue
ParentKey string
ParentID string
Assignee string
AssignAfterCreate bool
MetadataRefs map[string]string
Labels []string
Deps []GraphNodeDep
}
// GraphNodeDep is an inline dependency on a single graph node. Target is
// resolved as a plan-local key first, then treated as a literal issue ID.
type GraphNodeDep struct {
Type types.DependencyType
Target string
}
type GraphEdge struct {
FromKey string
FromID string
ToKey string
ToID string
Type types.DependencyType
// Gate and spawner describe fanout-gate metadata for waits-for edges;
// SpawnerKey references a plan-local node and is resolved after IDs mint.
Gate string
SpawnerKey string
SpawnerID string
// ThreadID threads conversation edges (replies-to).
ThreadID string
}
type GraphApplyResult struct {
IDs map[string]string
}
type ClaimResult struct {
AlreadyClaimed bool
PriorAssignee string
}
type ClaimReadyResult struct {
Issue *types.Issue
Claimed bool
}
type UpdateSpec struct {
Fields map[string]any
Claim bool
// ExpectedVersion requires the current row version to match before any
// claim or field writes.
ExpectedVersion *int64
Persistence *types.PersistenceMode
AddLabels []string
RemoveLabels []string
SetLabels *[]string
Reparent *string
// ExpectedAssignee and ExpectedStatus are the bd-wsqvw compare-and-set
// guards (`bd update --if-assignee/--if-status`): when non-nil, the whole
// update applies only if the issue's current assignee/status equals the
// expected value, else ApplyUpdate refuses with
// storage.ErrAssigneeMismatch/ErrStatusMismatch and nothing is written. A
// non-nil pointer to "" means "expected unassigned". The guard read shares
// the unit of work's transaction; a writer that commits mid-flight
// collides on the row_lock rewrite at commit time and the caller's
// whole-attempt retry re-checks the guards on the redo.
ExpectedAssignee *string
ExpectedStatus *string
}
type IssueUseCase interface {
GetIssue(ctx context.Context, id string) (*types.Issue, error)
GetIssuesByIDs(ctx context.Context, ids []string) ([]*types.Issue, error)
FindWispDependentsRecursive(ctx context.Context, ids []string) (map[string]bool, error)
SearchIssues(ctx context.Context, query string, filter types.IssueFilter) (SearchPage, error)
SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) (SearchCountsPage, error)
SearchIssueIDs(ctx context.Context, query string, filter types.IssueFilter) ([]string, error)
GetReadyWork(ctx context.Context, filter types.WorkFilter) (SearchPage, error)
GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) (SearchCountsPage, error)
GetDescendants(ctx context.Context, rootID string, filter types.IssueFilter) ([]*types.Issue, error)
ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (ClaimReadyResult, error)
GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error)
GetStatistics(ctx context.Context) (*types.Statistics, error)
CountIssues(ctx context.Context, query string, filter types.IssueFilter) (int64, error)
CountIssuesByGroup(ctx context.Context, filter types.IssueFilter, groupBy string) (map[string]int, error)
History(ctx context.Context, id string) ([]*storage.HistoryEntry, error)
IterEvents(ctx context.Context, id string, limit int) (storage.Iter[types.Event], error)
GetStaleIssues(ctx context.Context, filter types.StaleFilter) ([]*types.Issue, error)
GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error)
Unclaim(ctx context.Context, id, actor string, force bool) error
ReclaimExpiredLeases(ctx context.Context, olderThan time.Duration, filter types.ReclaimFilter, actor string) ([]types.ReclaimedLease, error)
CreateIssue(ctx context.Context, params CreateIssueParams, actor string) (CreateIssueResult, error)
CreateIssues(ctx context.Context, params []CreateIssueParams, actor string) (CreateIssuesResult, error)
UpdateIssue(ctx context.Context, id string, updates map[string]any, actor string) error
ClaimIssue(ctx context.Context, id, actor string) (ClaimResult, error)
ClaimIssueIfOpen(ctx context.Context, id, actor string) (ClaimResult, error)
CloseIssue(ctx context.Context, id string, params CloseIssueParams, actor string) (CloseIssueResult, error)
CloseIssueChecked(ctx context.Context, id string, params CloseIssueParams, actor string, force bool) (CloseIssueResult, error)
ReopenIssue(ctx context.Context, id string, params ReopenIssueParams, actor string) (ReopenIssueResult, error)
CountOpenChildren(ctx context.Context, id string) (int, error)
GetNewlyUnblockedByClose(ctx context.Context, closedID string) ([]*types.Issue, error)
ApplyUpdate(ctx context.Context, id string, spec UpdateSpec, actor string) (*types.Issue, error)
ApplyIssueGraph(ctx context.Context, plan GraphPlan, actor string) (GraphApplyResult, error)
AsOf(ctx context.Context, id, ref string) (*types.Issue, error)
DeleteIssue(ctx context.Context, id, actor string) (DeleteIssuesResult, error)
DeleteIssues(ctx context.Context, params DeleteIssuesParams, actor string) (DeleteIssuesResult, error)
PreviewDelete(ctx context.Context, ids []string) (DeletePreview, error)
DeleteWisp(ctx context.Context, id, actor string) (DeleteIssuesResult, error)
DeleteWisps(ctx context.Context, params DeleteIssuesParams, actor string) (DeleteIssuesResult, error)
PreviewDeleteWisp(ctx context.Context, ids []string) (DeletePreview, error)
GetWisp(ctx context.Context, id string) (*types.Issue, error)
GetWispsByIDs(ctx context.Context, ids []string) ([]*types.Issue, error)
CreateWisp(ctx context.Context, params CreateIssueParams, actor string) (CreateIssueResult, error)
CreateWisps(ctx context.Context, params []CreateIssueParams, actor string) (CreateIssuesResult, error)
UpdateWisp(ctx context.Context, id string, updates map[string]any, actor string) error
ClaimWisp(ctx context.Context, id, actor string) (ClaimResult, error)
ClaimWispIfOpen(ctx context.Context, id, actor string) (ClaimResult, error)
CloseWisp(ctx context.Context, id string, params CloseIssueParams, actor string) (CloseIssueResult, error)
CloseWispChecked(ctx context.Context, id string, params CloseIssueParams, actor string, force bool) (CloseIssueResult, error)
ReopenWisp(ctx context.Context, id string, params ReopenIssueParams, actor string) (ReopenIssueResult, error)
CountOpenWispChildren(ctx context.Context, id string) (int, error)
GetNewlyUnblockedByCloseWisp(ctx context.Context, closedID string) ([]*types.Issue, error)
ApplyWispGraph(ctx context.Context, plan GraphPlan, actor string) (GraphApplyResult, error)
ClaimReadyWisp(ctx context.Context, filter types.WorkFilter, actor string) (ClaimReadyResult, error)
}
type CloseIssueParams struct {
Reason string
Session string
}
type CloseIssueResult struct {
Issue *types.Issue
Closed bool
OpenChildren int
}
type ReopenIssueParams struct {
Reason string
}
type ReopenIssueResult struct {
Issue *types.Issue
Reopened bool
}
func NewIssueUseCase(
issueRepo IssueSQLRepository,
depRepo DependencySQLRepository,
labelRepo LabelSQLRepository,
counterRepo ChildCounterSQLRepository,
commentRepo CommentSQLRepository,
cfgRepo ConfigSQLRepository,
eventsRepo EventsSQLRepository,
labelUC LabelUseCase,
depUC DependencyUseCase,
) IssueUseCase {
return &issueUseCaseImpl{
issueRepo: issueRepo,
depRepo: depRepo,
labelRepo: labelRepo,
counterRepo: counterRepo,
commentRepo: commentRepo,
cfgRepo: cfgRepo,
eventsRepo: eventsRepo,
labelUC: labelUC,
depUC: depUC,
}
}
type issueUseCaseImpl struct {
issueRepo IssueSQLRepository
depRepo DependencySQLRepository
labelRepo LabelSQLRepository
counterRepo ChildCounterSQLRepository
commentRepo CommentSQLRepository
cfgRepo ConfigSQLRepository
eventsRepo EventsSQLRepository
labelUC LabelUseCase
depUC DependencyUseCase
}
var _ IssueUseCase = (*issueUseCaseImpl)(nil)
func (u *issueUseCaseImpl) GetIssue(ctx context.Context, id string) (*types.Issue, error) {
return u.get(ctx, id, false)
}
func (u *issueUseCaseImpl) AsOf(ctx context.Context, id, ref string) (*types.Issue, error) {
if id == "" {
return nil, fmt.Errorf("as of: id must not be empty")
}
issue, err := u.issueRepo.AsOf(ctx, id, ref)
if err != nil {
return nil, fmt.Errorf("as of %s @ %s: %w", id, ref, err)
}
return issue, nil
}
func (u *issueUseCaseImpl) GetWisp(ctx context.Context, id string) (*types.Issue, error) {
return u.get(ctx, id, true)
}
func (u *issueUseCaseImpl) get(ctx context.Context, id string, useWisp bool) (*types.Issue, error) {
if id == "" {
return nil, fmt.Errorf("get: id must not be empty")
}
issue, err := u.issueRepo.Get(ctx, id, IssueTableOpts{UseWispsTable: useWisp})
if err != nil {
return nil, fmt.Errorf("get %s: %w", id, err)
}
return issue, nil
}
func (u *issueUseCaseImpl) GetIssuesByIDs(ctx context.Context, ids []string) ([]*types.Issue, error) {
return u.getByIDs(ctx, ids, false)
}
func (u *issueUseCaseImpl) FindWispDependentsRecursive(ctx context.Context, ids []string) (map[string]bool, error) {
return u.issueRepo.FindWispDependentsRecursive(ctx, ids)
}
func (u *issueUseCaseImpl) GetWispsByIDs(ctx context.Context, ids []string) ([]*types.Issue, error) {
return u.getByIDs(ctx, ids, true)
}
func (u *issueUseCaseImpl) getByIDs(ctx context.Context, ids []string, useWisp bool) ([]*types.Issue, error) {
if len(ids) == 0 {
return nil, nil
}
out, err := u.issueRepo.GetByIDs(ctx, ids, IssueTableOpts{UseWispsTable: useWisp})
if err != nil {
return nil, fmt.Errorf("getByIDs: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) UpdateIssue(ctx context.Context, id string, updates map[string]any, actor string) error {
return u.update(ctx, id, updates, actor, false)
}
func (u *issueUseCaseImpl) UpdateWisp(ctx context.Context, id string, updates map[string]any, actor string) error {
return u.update(ctx, id, updates, actor, true)
}
func (u *issueUseCaseImpl) update(ctx context.Context, id string, updates map[string]any, actor string, useWisp bool) error {
if id == "" {
return fmt.Errorf("update: id must not be empty")
}
if len(updates) == 0 {
return nil
}
if err := validateCanonicalIssueUpdates(updates); err != nil {
return err
}
if err := u.validateIssueTypeUpdate(ctx, updates); err != nil {
return err
}
return u.issueRepo.Update(ctx, id, updates, actor, IssueTableOpts{UseWispsTable: useWisp})
}
// validateIssueTypeUpdate rejects an issue_type the configuration does not
// define. It reads the configured custom types, so unlike
// validateCanonicalIssueUpdates it needs the use case and a context.
func (u *issueUseCaseImpl) validateIssueTypeUpdate(ctx context.Context, updates map[string]any) error {
rawType, ok := updates["issue_type"]
if !ok {
return nil
}
issueType, ok := rawType.(string)
if !ok {
return nil
}
customTypes, err := u.cfgRepo.GetCustomTypes(ctx)
if err != nil {
return fmt.Errorf("update: read custom types: %w", err)
}
if !types.IssueType(issueType).IsValidWithCustom(customTypes) {
return fmt.Errorf("%w: invalid issue type: %s", storage.ErrValidation, issueType)
}
return nil
}
// validateCanonicalIssueUpdates rejects out-of-range values for the canonical
// scalar columns, and rejects values whose Go type the column cannot carry. An
// unsupported type used to fall through unvalidated and reach the SQL layer,
// which coerced it silently -- a title of 7 was stored as "7", and an int64
// priority or estimate skipped its range check entirely.
func validateCanonicalIssueUpdates(updates map[string]any) error {
if value, ok := updates["title"]; ok {
title, isString := value.(string)
if !isString {
return canonicalIssueUpdateTypeError("title", value, "string")
}
if err := types.ValidateIssueTitle(title); err != nil {
return canonicalIssueUpdateValidationError("title", err)
}
}
if value, ok := updates["priority"]; ok {
priority, isInt := value.(int)
if !isInt {
return canonicalIssueUpdateTypeError("priority", value, "int")
}
if err := types.ValidateIssuePriority(priority); err != nil {
return canonicalIssueUpdateValidationError("priority", err)
}
}
if value, ok := updates["estimated_minutes"]; ok {
var estimatedMinutes *int
switch value := value.(type) {
case int:
estimatedMinutes = &value
case *int:
estimatedMinutes = value
case nil:
// Clearing the estimate; a nil estimate validates.
default:
return canonicalIssueUpdateTypeError("estimated_minutes", value, "int or *int")
}
if err := types.ValidateIssueEstimatedMinutes(estimatedMinutes); err != nil {
return canonicalIssueUpdateValidationError("estimated_minutes", err)
}
}
return nil
}
func canonicalIssueUpdateValidationError(field string, err error) error {
return fmt.Errorf("%w: update field %q: %w", storage.ErrValidation, field, err)
}
func canonicalIssueUpdateTypeError(field string, value any, want string) error {
return fmt.Errorf("%w: update field %q: expected %s, got %T", storage.ErrValidation, field, want, value)
}
func (u *issueUseCaseImpl) ClaimIssue(ctx context.Context, id, actor string) (ClaimResult, error) {
return u.claim(ctx, id, actor, false)
}
func (u *issueUseCaseImpl) ClaimWisp(ctx context.Context, id, actor string) (ClaimResult, error) {
return u.claim(ctx, id, actor, true)
}
func (u *issueUseCaseImpl) claim(ctx context.Context, id, actor string, useWisp bool) (ClaimResult, error) {
if id == "" {
return ClaimResult{}, fmt.Errorf("claim: id must not be empty")
}
if actor == "" {
return ClaimResult{}, fmt.Errorf("claim: actor must not be empty")
}
row, err := u.issueRepo.Claim(ctx, id, actor, IssueTableOpts{UseWispsTable: useWisp})
if err != nil {
return ClaimResult{}, fmt.Errorf("claim %s: %w", id, err)
}
if row.Updated {
return ClaimResult{}, nil
}
if row.CurrentAssignee == actor && row.CurrentStatus == types.StatusInProgress {
return ClaimResult{AlreadyClaimed: true, PriorAssignee: actor}, nil
}
// The refusal carries the assignee and status the repository read back in
// THIS transaction, so a caller reports who won without parsing the
// message. The copy lives on the typed error, shared with the twin
// (issueops.ClaimIssueInTx) that used to restate it — that is what bd-at6rc
// asked for. The sentinel stays matchable through Unwrap, which the proxied
// batch exit code keys on.
//
// A pool-assigned issue only loses the CAS for a non-assignee reason
// (status changed underneath us), so it refuses on the status rather than
// with a misleading held-by refusal.
// Composed here rather than on the typed error, for the reason
// issueops.ClaimIssueInTx gives: ClaimConflictError passes its wrapped
// refusal through unchanged, so the fragments have to be in the wrapped
// error or beads.ParseClaimConflict recovers nothing. This is the
// domain-stack twin of that producer and must answer the same three ways
// (bd-at6rc).
refusal := fmt.Errorf("%w%s%s", storage.ErrNotClaimable, storage.NotClaimableStatusFragment, row.CurrentStatus)
if row.CurrentAssignee != "" && row.CurrentAssignee != actor {
switch {
// Pool aliases refuse on the STATUS, checked first so a pool never
// reaches the holder-steering copy below.
case row.CurrentAssigneeIsPool:
// refusal already names the status.
case row.CurrentStatus == types.StatusOpen:
// Never name a release command here (wy-yuclk): copy that names
// one gets pattern-matched by batch agents into an unclaim+claim
// steamroller of live claims. bd reclaim is safe to name because
// it only recovers claims whose lease has already expired. The
// parseable " by <assignee>" tail is deliberately absent, so the
// holder is recovered from the typed field instead.
refusal = fmt.Errorf("%w: already assigned to %q — coordinate with the holder; if their claim is abandoned (crashed agent), lease expiry will surface it for bd reclaim", storage.ErrAlreadyClaimed, row.CurrentAssignee)
default:
refusal = fmt.Errorf("%w%s%s", storage.ErrAlreadyClaimed, storage.ClaimedByFragment, row.CurrentAssignee)
}
}
return ClaimResult{}, &publicops.ClaimConflictError{
IssueID: id,
Assignee: row.CurrentAssignee,
Status: row.CurrentStatus,
Err: refusal,
}
}
func (u *issueUseCaseImpl) ApplyUpdate(ctx context.Context, id string, spec UpdateSpec, actor string) (*types.Issue, error) {
if id == "" {
return nil, fmt.Errorf("ApplyUpdate: id must not be empty")
}
useWisp, err := u.isWispID(ctx, id)
if err != nil {
return nil, fmt.Errorf("ApplyUpdate %s: %w", id, err)
}
if spec.ExpectedVersion != nil || spec.ExpectedAssignee != nil || spec.ExpectedStatus != nil {
var current *types.Issue
if useWisp {
current, err = u.GetWisp(ctx, id)
} else {
current, err = u.GetIssue(ctx, id)
}
if err != nil {
return nil, fmt.Errorf("ApplyUpdate: read %s for guards: %w", id, err)
}
if current == nil {
return nil, fmt.Errorf("%w: issue %s", storage.ErrNotFound, id)
}
if spec.ExpectedVersion != nil && current.RowVersion != *spec.ExpectedVersion {
return nil, fmt.Errorf("%w: expected %d, got %d", storage.ErrVersionMismatch, *spec.ExpectedVersion, current.RowVersion)
}
if spec.ExpectedAssignee != nil && current.Assignee != *spec.ExpectedAssignee {
return nil, fmt.Errorf("%w: %s is held by %q, expected %q",
storage.ErrAssigneeMismatch, id, current.Assignee, *spec.ExpectedAssignee)
}
if spec.ExpectedStatus != nil && string(current.Status) != *spec.ExpectedStatus {
return nil, fmt.Errorf("%w: %s has status %q, expected %q",
storage.ErrStatusMismatch, id, current.Status, *spec.ExpectedStatus)
}
}
// Validate the requested field values before anything mutates. The claim
// below writes assignee and status, so a spec that pairs Claim with an
// invalid field used to leave the issue claimed by an update that never
// applied.
if err := validateCanonicalIssueUpdates(spec.Fields); err != nil {
return nil, err
}
if err := u.validateIssueTypeUpdate(ctx, spec.Fields); err != nil {
return nil, err
}
if spec.Claim {
if useWisp {
if _, err := u.ClaimWisp(ctx, id, actor); err != nil {
return nil, err
}
} else {
if _, err := u.ClaimIssue(ctx, id, actor); err != nil {
return nil, err
}
}
}
if len(spec.Fields) > 0 {
if useWisp {
if err := u.UpdateWisp(ctx, id, spec.Fields, actor); err != nil {
return nil, err
}
} else {
if err := u.UpdateIssue(ctx, id, spec.Fields, actor); err != nil {
return nil, err
}
}
}
if spec.SetLabels != nil {
if useWisp {
if err := u.labelUC.SetWispLabels(ctx, id, *spec.SetLabels, actor); err != nil {
return nil, err
}
} else {
if err := u.labelUC.SetLabels(ctx, id, *spec.SetLabels, actor); err != nil {
return nil, err
}
}
}
if len(spec.AddLabels) > 0 {
if useWisp {
if err := u.labelUC.AddWispLabels(ctx, id, spec.AddLabels, actor); err != nil {
return nil, err
}
} else {
if err := u.labelUC.AddLabels(ctx, id, spec.AddLabels, actor); err != nil {
return nil, err
}
}
}
if len(spec.RemoveLabels) > 0 {
if useWisp {
if err := u.labelUC.RemoveWispLabels(ctx, id, spec.RemoveLabels, actor); err != nil {
return nil, err
}
} else {
if err := u.labelUC.RemoveLabels(ctx, id, spec.RemoveLabels, actor); err != nil {
return nil, err
}
}
}
if spec.Reparent != nil {
if useWisp {
if err := u.depUC.ReparentWisp(ctx, id, *spec.Reparent, actor); err != nil {
return nil, err
}
} else {
if err := u.depUC.Reparent(ctx, id, *spec.Reparent, actor); err != nil {
return nil, err
}
}
}
if spec.Persistence != nil {
if _, err := u.issueRepo.MovePersistence(ctx, id, *spec.Persistence); err != nil {
return nil, fmt.Errorf("ApplyUpdate: move persistence for %s: %w", id, err)
}
useWisp, err = u.isWispID(ctx, id)
if err != nil {
return nil, fmt.Errorf("ApplyUpdate: re-locate %s after persistence move: %w", id, err)
}
}
var issue *types.Issue
if useWisp {
issue, err = u.GetWisp(ctx, id)
} else {
issue, err = u.GetIssue(ctx, id)
}
if err != nil {
return nil, fmt.Errorf("ApplyUpdate: re-fetch %s: %w", id, err)
}
return issue, nil
}
func (u *issueUseCaseImpl) isWispID(ctx context.Context, id string) (bool, error) {
found, err := u.issueRepo.Exists(ctx, id, IssueTableOpts{UseWispsTable: true})
if err != nil {
if dberrors.IsTableNotExist(err) {
return false, nil
}
return false, fmt.Errorf("probe wisps table: %w", err)
}
return found, nil
}
func (u *issueUseCaseImpl) SearchIssues(ctx context.Context, query string, filter types.IssueFilter) (SearchPage, error) {
out, err := u.issueRepo.SearchAcrossIssuesAndWisps(ctx, query, filter)
if err != nil {
return SearchPage{}, fmt.Errorf("SearchIssues: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) SearchIssueIDs(ctx context.Context, query string, filter types.IssueFilter) ([]string, error) {
out, err := u.issueRepo.SearchIssueIDs(ctx, query, filter)
if err != nil {
return nil, fmt.Errorf("SearchIssueIDs: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) (SearchCountsPage, error) {
out, err := u.issueRepo.SearchAcrossIssuesAndWispsWithCounts(ctx, query, filter)
if err != nil {
return SearchCountsPage{}, fmt.Errorf("SearchIssuesWithCounts: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetReadyWork(ctx context.Context, filter types.WorkFilter) (SearchPage, error) {
out, err := u.issueRepo.GetReadyWork(ctx, filter)
if err != nil {
return SearchPage{}, fmt.Errorf("GetReadyWork: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) (SearchCountsPage, error) {
out, err := u.issueRepo.GetReadyWorkWithCounts(ctx, filter)
if err != nil {
return SearchCountsPage{}, fmt.Errorf("GetReadyWorkWithCounts: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetDescendants(ctx context.Context, rootID string, filter types.IssueFilter) ([]*types.Issue, error) {
if rootID == "" {
return nil, fmt.Errorf("GetDescendants: rootID must not be empty")
}
out, err := u.issueRepo.GetDescendants(ctx, rootID, filter)
if err != nil {
return nil, fmt.Errorf("GetDescendants: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) CreateIssue(ctx context.Context, params CreateIssueParams, actor string) (CreateIssueResult, error) {
return u.create(ctx, params, actor, false)
}
func (u *issueUseCaseImpl) CreateWisp(ctx context.Context, params CreateIssueParams, actor string) (CreateIssueResult, error) {
return u.create(ctx, params, actor, true)
}
func (u *issueUseCaseImpl) create(ctx context.Context, params CreateIssueParams, actor string, useWisp bool) (CreateIssueResult, error) {
if params.Issue == nil {
return CreateIssueResult{}, fmt.Errorf("create: Issue must not be nil")
}
issue := params.Issue
if issue.Status == "" {
issue.Status = types.StatusOpen
}
// Set CreatedAt before the mint path: GenerateHashID hashes timestamp,
// so it must be stable across the candidate loop and the eventual
// db.Insert (which would otherwise normalize a zero value to a later
// time and break candidate reproducibility on retry).
if issue.CreatedAt.IsZero() {
issue.CreatedAt = time.Now().UTC()
}
switch {
case params.ExplicitID != "":
issue.ID = params.ExplicitID
case params.ParentID != "":
childID, err := u.counterRepo.NextChildID(ctx, params.ParentID, ChildCounterOpts{UseWispsTable: useWisp})
if err != nil {
return CreateIssueResult{}, fmt.Errorf("create: next child ID for %s: %w", params.ParentID, err)
}
issue.ID = childID
case issue.ID == "":
minted, err := u.mintTopLevelID(ctx, issue, actor, useWisp)
if err != nil {
return CreateIssueResult{}, fmt.Errorf("create: mint top-level ID: %w", err)
}
issue.ID = minted
}
if params.CreateOnly && params.ExplicitID != "" && !params.ForcePrefix {
configuredPrefix, err := u.cfgRepo.GetConfig(ctx, "issue_prefix")
if err != nil {
return CreateIssueResult{}, fmt.Errorf("create: read issue prefix: %w", err)
}
allowedPrefixes, err := u.cfgRepo.GetConfig(ctx, "allowed_prefixes")
if err != nil {
return CreateIssueResult{}, fmt.Errorf("create: read allowed prefixes: %w", err)
}
if err := validateExplicitIDPrefix(issue.ID, strings.TrimSuffix(configuredPrefix, "-"), allowedPrefixes); err != nil {
return CreateIssueResult{}, fmt.Errorf("create: explicit ID prefix: %w", err)
}
}
if params.DiscoveredFromParent != "" {
if parent, err := u.GetIssue(ctx, params.DiscoveredFromParent); err == nil && parent.SourceRepo != "" {
issue.SourceRepo = parent.SourceRepo
}
}
insertOpts := InsertIssueOpts{UseWispsTable: useWisp, CreateOnly: params.CreateOnly}
if err := u.issueRepo.Insert(ctx, issue, actor, insertOpts); err != nil {
return CreateIssueResult{}, fmt.Errorf("create: insert: %w", err)
}
if len(params.Comments) > 0 {
issue.Comments = make([]*types.Comment, 0, len(params.Comments))
for _, comment := range params.Comments {
if comment == nil {
return CreateIssueResult{}, fmt.Errorf("create: comment must not be nil")
}
copy := *comment
copy.IssueID = issue.ID
if copy.Author == "" {
copy.Author = actor
}
inserted, err := u.commentRepo.InsertRecord(ctx, &copy, CommentOpts{UseWispsTable: useWisp})
if err != nil {
return CreateIssueResult{}, fmt.Errorf("create: insert comment: %w", err)
}
issue.Comments = append(issue.Comments, inserted)
}
}
result := CreateIssueResult{Issue: issue}
if params.ParentID != "" {
pcDep := &types.Dependency{
IssueID: issue.ID,
DependsOnID: params.ParentID,
Type: types.DepParentChild,
}
if err := u.depRepo.Insert(ctx, pcDep, actor, DepInsertOpts{UseWispsTable: useWisp}); err != nil {
return result, fmt.Errorf("create: add parent-child dep: %w", err)
}
result.PostCreateWrites = true
}
if params.InheritLabelsFromParent && params.ParentID != "" {
parentIsWisp, err := u.isWispID(ctx, params.ParentID)
if err != nil {
return result, fmt.Errorf("create: determine parent tier for label inheritance from %s: %w", params.ParentID, err)
}
parentLabels, err := u.labelRepo.List(ctx, params.ParentID, LabelOpts{UseWispsTable: parentIsWisp})
switch {
case dberrors.IsTableNotExist(err):
// Older schemas may lack the wisp label table; nothing to inherit.
case err != nil:
// Swallowing this silently created children missing their
// inherited labels (bd-6dnrw.44 P3); the create is transactional,
// so failing loud is safe.
return result, fmt.Errorf("create: read parent labels for inheritance from %s: %w", params.ParentID, err)
default:
existing := make(map[string]bool, len(params.Labels))
for _, l := range params.Labels {
existing[l] = true
}
for _, l := range parentLabels {
if !existing[l] {
result.InheritedLabels = append(result.InheritedLabels, l)
}
}
}
}
for _, label := range params.Labels {
if err := u.labelRepo.Insert(ctx, issue.ID, label, actor, LabelOpts{UseWispsTable: useWisp}); err != nil {
return result, fmt.Errorf("create: add label %s: %w", label, err)
}
result.PostCreateWrites = true
}
for _, label := range result.InheritedLabels {
if err := u.labelRepo.Insert(ctx, issue.ID, label, actor, LabelOpts{UseWispsTable: useWisp}); err != nil {
return result, fmt.Errorf("create: add inherited label %s: %w", label, err)
}
result.PostCreateWrites = true
}
for _, spec := range params.Dependencies {
dep := &types.Dependency{
IssueID: issue.ID,
DependsOnID: spec.TargetID,
Type: spec.Type,
Metadata: spec.Metadata,
ThreadID: spec.ThreadID,
}
if spec.SwapDirection {
dep.IssueID, dep.DependsOnID = dep.DependsOnID, dep.IssueID
}
depSourceIsWisp, err := u.isWispID(ctx, dep.IssueID)
if err != nil {
return result, fmt.Errorf("create: determine dep source tier for %s: %w", dep.IssueID, err)
}
if err := u.depRepo.Insert(ctx, dep, actor, DepInsertOpts{UseWispsTable: depSourceIsWisp}); err != nil {
return result, fmt.Errorf("create: add dep %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
}
result.PostCreateWrites = true
}
if params.WaitsFor != nil {
// Spawner identity is the depends_on_id; metadata carries the gate.
dep, err := types.NewWaitsForDependency(issue.ID, params.WaitsFor.SpawnerID, params.WaitsFor.Gate)
if err != nil {
return result, fmt.Errorf("create: marshal waits-for meta: %w", err)
}
if err := u.depRepo.Insert(ctx, dep, actor, DepInsertOpts{UseWispsTable: useWisp}); err != nil {
return result, fmt.Errorf("create: add waits-for: %w", err)
}
result.PostCreateWrites = true
}
return result, nil
}
func validateExplicitIDPrefix(id, prefix, allowedPrefixes string) error {
if strings.HasPrefix(id, prefix+"-") {
return nil
}
for _, allowed := range strings.Split(allowedPrefixes, ",") {
allowed = strings.TrimSpace(allowed)
if allowed != "" && strings.HasPrefix(id, allowed+"-") {
return nil
}
}
return fmt.Errorf("%w: issue ID %s does not match configured prefix %s", storage.ErrPrefixMismatch, id, prefix)
}
func (u *issueUseCaseImpl) CreateIssues(ctx context.Context, params []CreateIssueParams, actor string) (CreateIssuesResult, error) {
return u.createMany(ctx, params, actor, false)
}
func (u *issueUseCaseImpl) CreateWisps(ctx context.Context, params []CreateIssueParams, actor string) (CreateIssuesResult, error) {
return u.createMany(ctx, params, actor, true)
}
func (u *issueUseCaseImpl) createMany(ctx context.Context, params []CreateIssueParams, actor string, useWisp bool) (CreateIssuesResult, error) {
result := CreateIssuesResult{}
for i := range params {
r, err := u.create(ctx, params[i], actor, useWisp)
if err != nil {
return result, fmt.Errorf("createMany[%d]: %w", i, err)
}
result.Issues = append(result.Issues, r.Issue)
}
return result, nil
}
func (u *issueUseCaseImpl) ApplyIssueGraph(ctx context.Context, plan GraphPlan, actor string) (GraphApplyResult, error) {
return u.applyGraph(ctx, plan, actor, false)
}
func (u *issueUseCaseImpl) ApplyWispGraph(ctx context.Context, plan GraphPlan, actor string) (GraphApplyResult, error) {
return u.applyGraph(ctx, plan, actor, true)
}
func (u *issueUseCaseImpl) applyGraph(ctx context.Context, plan GraphPlan, actor string, useWisp bool) (GraphApplyResult, error) {
keyToID := make(map[string]string, len(plan.Nodes))
pendingAssignees := make(map[int]string, len(plan.Nodes))
// Pass 1 — create every node as a top-level issue. We deliberately do
// not pass ParentID to u.create: graph nodes with parent_key/parent_id
// receive top-level hash (or counter) IDs and have their parent linkage
// added as a separate parent-child dep below. This matches embedded
// executeGraphApply (cmd/bd/graph_apply.go) and lets children precede
// parents in plan order — keyToID is only consulted after every node
// has minted its ID.
for i, node := range plan.Nodes {
if node.Issue == nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %d (key=%q) has nil Issue", i, node.Key)
}
// The whole plan routes to one table, so every node's storage class
// must match. The CLI pre-validates this; guard here too so other
// callers cannot route wisp-flagged issues into the durable table.
if nodeWisp := node.Issue.Ephemeral || node.Issue.NoHistory; nodeWisp != useWisp {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q storage class (ephemeral=%t, no_history=%t) does not match plan routing (wisp=%t)", node.Key, node.Issue.Ephemeral, node.Issue.NoHistory, useWisp)
}
if node.AssignAfterCreate {
pendingAssignees[i] = node.Assignee
node.Issue.Assignee = ""
} else if node.Assignee != "" {
node.Issue.Assignee = node.Assignee
}
params := CreateIssueParams{
Issue: node.Issue,
Labels: node.Labels,
}
r, err := u.create(ctx, params, actor, useWisp)
if err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: %w", node.Key, err)
}
keyToID[node.Key] = r.Issue.ID
}
// Pass 2 — resolve MetadataRefs now that every node has a minted ID.
// Merges the resolved IDs into the issue's existing metadata JSON and
// writes the result back via Update. Kept inside applyGraph so the CLI
// cannot bypass this step; the proxied caller used to do it post-call.
for _, node := range plan.Nodes {
if len(node.MetadataRefs) == 0 {
continue
}
metaJSON, err := types.MergeMetadataRefs(node.Issue.Metadata, node.MetadataRefs, keyToID)
if err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: %w", node.Key, err)
}
updates := map[string]any{"metadata": metaJSON}
if err := u.issueRepo.Update(ctx, keyToID[node.Key], updates, actor, IssueTableOpts{UseWispsTable: useWisp}); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: updating metadata refs: %w", node.Key, err)
}
}
// Build the (childID, parentID) pair set and validate that any planned
// parent-child link does not close a cycle through planned edges or
// already-existing dependencies in the store. This must run before any
// dep inserts to catch the violation before we've written anything.
parentDepPairs := graphParentDepPairs(plan.Nodes, keyToID)
newSchedulingEdges := make([][2]string, 0, len(plan.Nodes)+len(plan.Edges))
if err := u.validatePlannedBlockingPaths(ctx, plan, keyToID, parentDepPairs); err != nil {
return GraphApplyResult{}, err
}
if err := u.validatePlannedBlockingCycles(ctx, plan, keyToID); err != nil {
return GraphApplyResult{}, err
}
// Preserve failure-before-write for explicit edges that conflict directly
// with an implicit node parent relationship. Parent-first mutation below is
// for transitive hierarchy visibility, not for deferring structural errors.
for i, edge := range plan.Edges {
fromID := resolveEdgeRef(edge.FromKey, edge.FromID, keyToID)
if fromID == "" {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d references undefined from_key %q", i, edge.FromKey)
}
toID := resolveEdgeRef(edge.ToKey, edge.ToID, keyToID)
if toID == "" {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d references undefined to_key %q", i, edge.ToKey)
}
depType := edge.Type
if depType == "" {
depType = types.DepBlocks
}
if parentDepPairs[depPairKey(fromID, toID)] && depType != types.DepParentChild {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d %s->%s duplicates a parent-child relationship with dependency type %q", i, fromID, toID, depType)
}
if parentDepPairs[depPairKey(toID, fromID)] && cycleRelevantDepType(depType) {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d %s->%s creates a blocking reverse of a parent-child relationship", i, fromID, toID)
}
}
// Pass 3 — insert node parent-child deps now that all IDs are known. These
// must be visible before any blocking edge in the same plan so the storage
// hierarchy guard evaluates existing + planned ancestry.
for _, node := range plan.Nodes {
parentID := node.ParentID
if node.ParentKey != "" {
parentID = keyToID[node.ParentKey]
}
if parentID == "" {
continue
}
childID := keyToID[node.Key]
dep := &types.Dependency{
IssueID: childID,
DependsOnID: parentID,
Type: types.DepParentChild,
}
if err := u.depRepo.Insert(ctx, dep, actor, DepInsertOpts{UseWispsTable: useWisp}); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: parent-child dep %s->%s: %w", node.Key, childID, parentID, err)
}
newSchedulingEdges = append(newSchedulingEdges, [2]string{childID, parentID})
}
// Pass 4 — insert explicit edge deps in two stable phases: all additional
// parent-child edges first, then every other type. Deduplicate against the
// node parent-child pairs:
// - Same pair, parent-child type → skip (pass 3 already inserted it).
// - Same pair, different type → error (conflicting edge over a parent-child link).
// - Reverse pair, blocking type → error (creates a parent → child blocking cycle).
//
// The blocking-only whole-graph preflight above gives early, edge-specific
// errors. Repository Insert remains the defensive authority for the broader
// blocks + conditional-blocks + parent-child scheduling graph.
for phase := 0; phase < 2; phase++ {
parentPhase := phase == 0
for i, edge := range plan.Edges {
fromID := resolveEdgeRef(edge.FromKey, edge.FromID, keyToID)
if fromID == "" {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d references undefined from_key %q", i, edge.FromKey)
}
toID := resolveEdgeRef(edge.ToKey, edge.ToID, keyToID)
if toID == "" {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d references undefined to_key %q", i, edge.ToKey)
}
depType := edge.Type
if depType == "" {
depType = types.DepBlocks
}
if (depType == types.DepParentChild) != parentPhase {
continue
}
if parentDepPairs[depPairKey(fromID, toID)] {
if depType == types.DepParentChild {
continue
}
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d %s->%s duplicates a parent-child relationship with dependency type %q", i, fromID, toID, depType)
}
if parentDepPairs[depPairKey(toID, fromID)] && cycleRelevantDepType(depType) {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d %s->%s creates a blocking reverse of a parent-child relationship", i, fromID, toID)
}
dep, err := types.NewGraphEdgeDependency(fromID, toID, depType, edge.Gate, edge.SpawnerKey, edge.SpawnerID, edge.ThreadID, keyToID)
if err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d: %w", i, err)
}
if err := u.depRepo.Insert(ctx, dep, actor, DepInsertOpts{UseWispsTable: useWisp}); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: edge %d (%s -> %s): %w", i, fromID, toID, err)
}
if isSchedulingDep(depType) {
newSchedulingEdges = append(newSchedulingEdges, [2]string{fromID, toID})
}
}
// Per-node inline deps in stable order for this phase, resolved by the
// same shared builder as the embedded executeGraphApply (cmd/bd/graph_apply.go).
for _, node := range plan.Nodes {
for _, nd := range node.Deps {
dep, err := types.NewGraphNodeDependency(keyToID[node.Key], nd.Type, nd.Target, keyToID)
if err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: %w", node.Key, err)
}
if (dep.Type == types.DepParentChild) != parentPhase {
continue
}
if err := u.depRepo.Insert(ctx, dep, actor, DepInsertOpts{UseWispsTable: useWisp}); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: adding dep to %q: %w", node.Key, nd.Target, err)
}
if isSchedulingDep(dep.Type) {
newSchedulingEdges = append(newSchedulingEdges, [2]string{dep.IssueID, dep.DependsOnID})
}
}
}
}
if cyclePath, err := u.depRepo.CycleThroughEdges(ctx, newSchedulingEdges); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: final cycle check: %w", err)
} else if cyclePath != "" {
return GraphApplyResult{}, fmt.Errorf("applyGraph: dependency cycle would be created: %s", cyclePath)
}
// Pass 5 — apply deferred assignees.
for i, assignee := range pendingAssignees {
if assignee == "" {
continue
}
id := keyToID[plan.Nodes[i].Key]
if err := u.issueRepo.Update(ctx, id, map[string]any{"assignee": assignee}, actor, IssueTableOpts{UseWispsTable: useWisp}); err != nil {
return GraphApplyResult{}, fmt.Errorf("applyGraph: node %q: defer assignee: %w", plan.Nodes[i].Key, err)
}
}
return GraphApplyResult{IDs: keyToID}, nil
}
// graphParentDepPairs encodes the (childID, parentID) parent-child pairs
// implied by the plan's node ParentKey/ParentID fields. Used by applyGraph
// to dedup explicit edges against implicit parent-child relationships and
// to seed the in-memory adjacency for live cycle detection.
func graphParentDepPairs(nodes []GraphNode, keyToID map[string]string) map[string]bool {
pairs := make(map[string]bool, len(nodes))
for _, n := range nodes {
parentID := n.ParentID
if n.ParentKey != "" {
parentID = keyToID[n.ParentKey]
}
childID := keyToID[n.Key]
if childID == "" || parentID == "" {
continue
}
pairs[depPairKey(childID, parentID)] = true
}
return pairs
}
// depPairKey encodes an ordered (issueID, dependsOnID) pair using a NUL
// separator so the two halves can be recovered unambiguously without ID
// characters colliding with the delimiter.
func depPairKey(issueID, dependsOnID string) string {
return issueID + "\x00" + dependsOnID
}
// depPairIDs decodes a key produced by depPairKey, returning (from, to, ok).
func depPairIDs(pair string) (string, string, bool) {
for i := 0; i < len(pair); i++ {
if pair[i] == 0 {
return pair[:i], pair[i+1:], true
}
}
return "", "", false
}
// cycleRelevantDepType returns true for dep types whose presence in the
// reverse direction of a parent-child link would form a cycle.
func cycleRelevantDepType(t types.DependencyType) bool {
return t == types.DepBlocks || t == types.DepConditionalBlocks
}
// readyPathDepType reports whether a dependency type affects ready-work. It is
// the broad predicate used when walking existing deps for parent→child
// blocking-path validation, in contrast to the blocking-only
// cycleRelevantDepType used by the early pure-blocking preflight. The two must
// stay distinct: narrowing the parent-path walk would miss real ready-work
// deadlocks, while it may additionally reject a return path through waits-for.
func readyPathDepType(t types.DependencyType) bool {
return t.AffectsReadyWork()
}
// resolveEdgeRef returns the ID for an edge endpoint: the explicit id when set
// (an ID override wins over a plan-local key, matching the CLI embedded path),
// else the keyToID lookup. Returns "" when neither resolves, which the caller
// should treat as a structural error.
func resolveEdgeRef(key, id string, keyToID map[string]string) string {
if id != "" {
return id
}
return keyToID[key]
}
// validatePlannedBlockingPaths rejects plans that would close a cycle
// once the parent-child deps are inserted. The adjacency it walks combines
// the planned parent-child pairs (child → parent), the cycle-relevant
// planned edges (excluding reverse-of-parent-child which is rejected by
// applyGraph's dedup pass), and live AffectsReadyWork dependencies pulled
// lazily from the store via depRepo. Mirrors embedded
// validateGraphApplyPlannedParentBlockingPaths.
func (u *issueUseCaseImpl) validatePlannedBlockingPaths(
ctx context.Context,
plan GraphPlan,
keyToID map[string]string,
parentDepPairs map[string]bool,
) error {
adj := make(map[string][]string)
for pair := range parentDepPairs {
fromID, toID, ok := depPairIDs(pair)
if ok {
adj[fromID] = append(adj[fromID], toID)
}
}
for _, edge := range plan.Edges {
depType := edge.Type
if depType == "" {
depType = types.DepBlocks
}
if !readyPathDepType(depType) {
continue
}
fromID := resolveEdgeRef(edge.FromKey, edge.FromID, keyToID)
toID := resolveEdgeRef(edge.ToKey, edge.ToID, keyToID)
if fromID == "" || toID == "" {
continue
}
// Skip the reverse-of-parent-child case for cycle-relevant types —
// applyGraph's edge dedup already errors on those with a clearer
// message, so we don't want them showing up here as ambiguous
// "blocking path" errors.
if cycleRelevantDepType(depType) && parentDepPairs[depPairKey(toID, fromID)] {
continue
}
adj[fromID] = append(adj[fromID], toID)
}
depCache := make(map[string][]*types.Dependency)
for _, node := range plan.Nodes {
parentID := node.ParentID
if node.ParentKey != "" {
parentID = keyToID[node.ParentKey]
}
childID := keyToID[node.Key]
if childID == "" || parentID == "" {
continue
}
hasPath, err := u.graphHasPath(ctx, adj, depCache, parentID, childID, readyPathDepType)
if err != nil {
return err
}
if hasPath {
return fmt.Errorf("applyGraph: node %q: planned blocking dependencies create a path from parent %q to child %q", node.Key, parentID, childID)
}
}
return nil
}
// validatePlannedBlockingCycles rejects planned blocking edges that would close
// a blocking-dependency cycle, evaluated whole-graph before any insert. It
// mirrors embedded validateGraphApplyPlannedBlockingCycles. This early
// preflight is intentionally restricted to blocking edges; repository Insert
// subsequently enforces the combined scheduling graph for every stored edge.
func (u *issueUseCaseImpl) validatePlannedBlockingCycles(
ctx context.Context,
plan GraphPlan,
keyToID map[string]string,
) error {
type plannedEdge struct {
index int
fromID string
toID string
}
adj := make(map[string][]string)
checks := make([]plannedEdge, 0, len(plan.Edges))
for i, edge := range plan.Edges {
depType := edge.Type
if depType == "" {
depType = types.DepBlocks
}
if !cycleRelevantDepType(depType) {
continue
}
fromID := resolveEdgeRef(edge.FromKey, edge.FromID, keyToID)
toID := resolveEdgeRef(edge.ToKey, edge.ToID, keyToID)
if fromID == "" || toID == "" {
continue
}
if fromID == toID {
return fmt.Errorf("applyGraph: edge %d %s->%s creates a blocking dependency cycle", i, fromID, toID)
}
adj[fromID] = append(adj[fromID], toID)
checks = append(checks, plannedEdge{index: i, fromID: fromID, toID: toID})
}
depCache := make(map[string][]*types.Dependency)
for _, edge := range checks {
hasPath, err := u.graphHasPath(ctx, adj, depCache, edge.toID, edge.fromID, cycleRelevantDepType)
if err != nil {
return fmt.Errorf("applyGraph: edge %d %s->%s: checking planned blocking cycle: %w", edge.index, edge.fromID, edge.toID, err)
}
if hasPath {
return fmt.Errorf("applyGraph: edge %d %s->%s creates a blocking dependency cycle", edge.index, edge.fromID, edge.toID)
}
}
return nil
}
// graphHasPath returns true if fromID can reach toID by following the
// in-memory adjacency (planned parent-child + planned blocking edges) and
// existing deps loaded lazily from the store. followExistingDep selects which
// existing dep types the walk traverses, so callers can mirror either the
// early blocking-only preflight or the broader ready-work graph. Per-node dep
// fetches are cached so each visited node hits the DB at most once.
//
// Existing deps are loaded from BOTH dependency tables. The per-edge
// depRepo.HasCycle probe this walk replaced traversed dependencies ∪
// wisp_dependencies (and the embedded path's GetDependencyRecords selects the
// table per node), so a blocking cycle that closes through the other table —
// e.g. an existing wisp edge reached during a regular graph-apply — must still
// be detected regardless of which table this graph-apply primarily writes.
func (u *issueUseCaseImpl) graphHasPath(
ctx context.Context,
adj map[string][]string,
depCache map[string][]*types.Dependency,
fromID, toID string,
followExistingDep func(types.DependencyType) bool,
) (bool, error) {
seen := make(map[string]bool)
var visit func(string) (bool, error)
visit = func(id string) (bool, error) {
if id == toID {
return true, nil
}
if seen[id] {
return false, nil
}
seen[id] = true
for _, next := range adj[id] {
found, err := visit(next)
if err != nil || found {
return found, err
}
}
deps, ok := depCache[id]
if !ok {
regular, err := u.depRepo.ListByIssueIDs(ctx, []string{id}, DepListOpts{
Direction: DepDirectionOut,
UseWispsTable: false,
})
if err != nil {
return false, fmt.Errorf("applyGraph: read existing deps for %s: %w", id, err)
}
wisp, err := u.depRepo.ListByIssueIDs(ctx, []string{id}, DepListOpts{
Direction: DepDirectionOut,
UseWispsTable: true,
})
if err != nil {
return false, fmt.Errorf("applyGraph: read existing wisp deps for %s: %w", id, err)
}
deps = append(regular.Outgoing[id], wisp.Outgoing[id]...)
depCache[id] = deps
}
for _, dep := range deps {
if !followExistingDep(dep.Type) {
continue
}
found, err := visit(dep.DependsOnID)
if err != nil || found {
return found, err
}
}
return false, nil
}
return visit(fromID)
}
// resolveTopLevelPrefix picks the prefix for a freshly-minted top-level ID,
// mirroring the embedded path's precedence (issueops/create.go:88-96 and
// dolt/wisps.go wispPrefix). Reads issue_prefix from config once and trims
// the trailing hyphen so a config value of "bd-" yields "bd-<hash>" rather
// than "bd--<hash>".
func (u *issueUseCaseImpl) resolveTopLevelPrefix(ctx context.Context, issue *types.Issue, useWisp bool) (string, error) {
if issue.PrefixOverride != "" {
return issue.PrefixOverride, nil
}
configPrefix, err := u.cfgRepo.GetConfig(ctx, "issue_prefix")
if err != nil {
return "", fmt.Errorf("read issue_prefix: %w", err)
}
configPrefix = strings.TrimSuffix(configPrefix, "-")
if configPrefix == "" {
return "", fmt.Errorf("issue_prefix config is missing")
}
switch {
case issue.IDPrefix != "":
return configPrefix + "-" + issue.IDPrefix, nil
case useWisp:
return configPrefix + "-wisp", nil
}
return configPrefix, nil
}
// mintTopLevelID generates a fresh top-level ID for an issue that has no
// ExplicitID and no ParentID. Honors counter mode for non-wisps (config key
// issue_id_mode=counter); otherwise uses adaptive hash-mode IDs that mirror
// issueops.GenerateIssueIDInTable. Reads issue.CreatedAt (caller must have
// stabilized it before this call so retries hash the same value).
func (u *issueUseCaseImpl) mintTopLevelID(ctx context.Context, issue *types.Issue, actor string, useWisp bool) (string, error) {
prefix, err := u.resolveTopLevelPrefix(ctx, issue, useWisp)
if err != nil {
return "", err
}
// Counter mode applies only to the issues table — wisps always hash-mint
// because there is no wisp_counter table and ephemeral churn would make
// a monotonic counter meaningless.
if !useWisp {
mode, err := u.cfgRepo.GetConfig(ctx, "issue_id_mode")
if err != nil {
return "", fmt.Errorf("read issue_id_mode: %w", err)
}
if mode == "counter" {
n, err := u.issueRepo.NextCounterID(ctx, prefix)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%d", prefix, n), nil
}
}
cfg, err := u.cfgRepo.GetAdaptiveIDConfig(ctx)
if err != nil {
return "", fmt.Errorf("read adaptive id config: %w", err)
}
tableOpts := IssueTableOpts{UseWispsTable: useWisp}
count, err := u.issueRepo.CountForPrefix(ctx, prefix, tableOpts)
if err != nil {
return "", err
}
baseLength := ComputeAdaptiveLength(count, cfg)
if baseLength > cfg.MaxLength {
baseLength = cfg.MaxLength
}
for length := baseLength; length <= cfg.MaxLength; length++ {
for nonce := 0; nonce < 10; nonce++ {
candidate := idgen.GenerateHashID(prefix, issue.Title, issue.Description, actor, issue.CreatedAt, length, nonce)
exists, err := u.issueRepo.Exists(ctx, candidate, tableOpts)
if err != nil {
return "", err
}
if !exists {
return candidate, nil
}
}
}
return "", fmt.Errorf("failed to generate unique ID for prefix %q after lengths %d..%d with 10 nonces each", prefix, baseLength, cfg.MaxLength)
}
func (u *issueUseCaseImpl) CloseIssue(ctx context.Context, id string, params CloseIssueParams, actor string) (CloseIssueResult, error) {
return u.close(ctx, id, params, actor, false)
}
func (u *issueUseCaseImpl) CloseWisp(ctx context.Context, id string, params CloseIssueParams, actor string) (CloseIssueResult, error) {
return u.close(ctx, id, params, actor, true)
}
// CloseIssueChecked closes an issue through the shared guarded close path.
func (u *issueUseCaseImpl) CloseIssueChecked(ctx context.Context, id string, params CloseIssueParams, actor string, force bool) (CloseIssueResult, error) {
return u.closeChecked(ctx, id, params, actor, force, false)
}
// CloseWispChecked is the wisp twin of CloseIssueChecked.
func (u *issueUseCaseImpl) CloseWispChecked(ctx context.Context, id string, params CloseIssueParams, actor string, force bool) (CloseIssueResult, error) {
return u.closeChecked(ctx, id, params, actor, force, true)
}
func (u *issueUseCaseImpl) closeChecked(ctx context.Context, id string, params CloseIssueParams, actor string, force, useWisp bool) (CloseIssueResult, error) {
if id == "" {
return CloseIssueResult{}, fmt.Errorf("close: id must not be empty")
}
if actor == "" {
return CloseIssueResult{}, fmt.Errorf("close: actor must not be empty")
}
row, err := u.issueRepo.CloseChecked(ctx, id, CloseRowParams{Reason: params.Reason, Session: params.Session}, actor, force)
if err != nil {
return CloseIssueResult{}, fmt.Errorf("close %s: %w", id, err)
}
issue, err := u.issueRepo.Get(ctx, id, IssueTableOpts{UseWispsTable: row.IsWisp || useWisp})
if err != nil {
return CloseIssueResult{}, fmt.Errorf("close %s: reload: %w", id, err)
}
return CloseIssueResult{Issue: issue, Closed: !row.AlreadyClosed, OpenChildren: row.OpenChildren}, nil
}
func (u *issueUseCaseImpl) close(ctx context.Context, id string, params CloseIssueParams, actor string, useWisp bool) (CloseIssueResult, error) {
if id == "" {
return CloseIssueResult{}, fmt.Errorf("close: id must not be empty")
}
if actor == "" {
return CloseIssueResult{}, fmt.Errorf("close: actor must not be empty")
}
row, err := u.issueRepo.Close(ctx, id, CloseRowParams{Reason: params.Reason, Session: params.Session}, actor, IssueTableOpts{UseWispsTable: useWisp})
if err != nil {
return CloseIssueResult{}, fmt.Errorf("close %s: %w", id, err)
}
issue, err := u.issueRepo.Get(ctx, id, IssueTableOpts{UseWispsTable: row.IsWisp})
if err != nil {
return CloseIssueResult{}, fmt.Errorf("close %s: reload: %w", id, err)
}
return CloseIssueResult{
Issue: issue,
Closed: !row.AlreadyClosed,
}, nil
}
func (u *issueUseCaseImpl) ReopenIssue(ctx context.Context, id string, params ReopenIssueParams, actor string) (ReopenIssueResult, error) {
return u.reopen(ctx, id, params, actor, false)
}
func (u *issueUseCaseImpl) ReopenWisp(ctx context.Context, id string, params ReopenIssueParams, actor string) (ReopenIssueResult, error) {
return u.reopen(ctx, id, params, actor, true)
}
func (u *issueUseCaseImpl) reopen(ctx context.Context, id string, params ReopenIssueParams, actor string, useWisp bool) (ReopenIssueResult, error) {
if id == "" {
return ReopenIssueResult{}, fmt.Errorf("reopen: id must not be empty")
}
if actor == "" {
return ReopenIssueResult{}, fmt.Errorf("reopen: actor must not be empty")
}
row, err := u.issueRepo.Reopen(ctx, id, ReopenRowParams{Reason: params.Reason}, actor, IssueTableOpts{UseWispsTable: useWisp})
if err != nil {
return ReopenIssueResult{}, fmt.Errorf("reopen %s: %w", id, err)
}
issue, err := u.issueRepo.Get(ctx, id, IssueTableOpts{UseWispsTable: row.IsWisp})
if err != nil {
return ReopenIssueResult{}, fmt.Errorf("reopen %s: reload: %w", id, err)
}
return ReopenIssueResult{
Issue: issue,
Reopened: row.Updated,
}, nil
}
func (u *issueUseCaseImpl) ClaimIssueIfOpen(ctx context.Context, id, actor string) (ClaimResult, error) {
return u.claim(ctx, id, actor, false)
}
func (u *issueUseCaseImpl) ClaimWispIfOpen(ctx context.Context, id, actor string) (ClaimResult, error) {
return u.claim(ctx, id, actor, true)
}
func (u *issueUseCaseImpl) CountOpenChildren(ctx context.Context, id string) (int, error) {
return u.countOpenChildren(ctx, id, false)
}
func (u *issueUseCaseImpl) CountOpenWispChildren(ctx context.Context, id string) (int, error) {
return u.countOpenChildren(ctx, id, true)
}
func (u *issueUseCaseImpl) countOpenChildren(ctx context.Context, id string, useWisp bool) (int, error) {
if id == "" {
return 0, fmt.Errorf("CountOpenChildren: id must not be empty")
}
children, err := u.depRepo.ListWithIssueMetadata(ctx, id, DepListOpts{
Types: []types.DependencyType{types.DepParentChild},
Direction: DepDirectionIn,
UseWispsTable: useWisp,
})
if err != nil {
return 0, fmt.Errorf("CountOpenChildren %s: %w", id, err)
}
open := 0
for _, child := range children {
if child.Status != types.StatusClosed {
open++
}
}
return open, nil
}
func (u *issueUseCaseImpl) GetNewlyUnblockedByClose(ctx context.Context, closedID string) ([]*types.Issue, error) {
return u.getNewlyUnblockedByClose(ctx, closedID)
}
func (u *issueUseCaseImpl) GetNewlyUnblockedByCloseWisp(ctx context.Context, closedID string) ([]*types.Issue, error) {
return u.getNewlyUnblockedByClose(ctx, closedID)
}
func (u *issueUseCaseImpl) getNewlyUnblockedByClose(ctx context.Context, closedID string) ([]*types.Issue, error) {
if closedID == "" {
return nil, fmt.Errorf("GetNewlyUnblockedByClose: closedID must not be empty")
}
out, err := u.issueRepo.GetNewlyUnblockedByClose(ctx, closedID)
if err != nil {
return nil, fmt.Errorf("GetNewlyUnblockedByClose %s: %w", closedID, err)
}
return out, nil
}
func (u *issueUseCaseImpl) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (ClaimReadyResult, error) {
return u.claimReady(ctx, filter, actor, false)
}
func (u *issueUseCaseImpl) ClaimReadyWisp(ctx context.Context, filter types.WorkFilter, actor string) (ClaimReadyResult, error) {
return u.claimReady(ctx, filter, actor, true)
}
func (u *issueUseCaseImpl) claimReady(ctx context.Context, filter types.WorkFilter, actor string, useWisp bool) (ClaimReadyResult, error) {
var (
issue *types.Issue
err error
)
if useWisp {
issue, err = u.issueRepo.ClaimReadyWisp(ctx, filter, actor)
} else {
issue, err = u.issueRepo.ClaimReadyIssue(ctx, filter, actor)
}
if err != nil {
if useWisp {
return ClaimReadyResult{}, fmt.Errorf("ClaimReadyWisp: %w", err)
}
return ClaimReadyResult{}, fmt.Errorf("ClaimReadyIssue: %w", err)
}
return ClaimReadyResult{Issue: issue, Claimed: issue != nil}, nil
}
func (u *issueUseCaseImpl) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) {
out, err := u.issueRepo.GetBlockedIssues(ctx, filter)
if err != nil {
return nil, fmt.Errorf("GetBlockedIssues: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetStatistics(ctx context.Context) (*types.Statistics, error) {
out, err := u.issueRepo.GetStatistics(ctx)
if err != nil {
return nil, fmt.Errorf("GetStatistics: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) CountIssues(ctx context.Context, query string, filter types.IssueFilter) (int64, error) {
out, err := u.issueRepo.CountIssues(ctx, query, filter)
if err != nil {
return 0, fmt.Errorf("CountIssues: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) CountIssuesByGroup(ctx context.Context, filter types.IssueFilter, groupBy string) (map[string]int, error) {
out, err := u.issueRepo.CountIssuesByGroup(ctx, filter, groupBy)
if err != nil {
return nil, fmt.Errorf("CountIssuesByGroup: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) History(ctx context.Context, id string) ([]*storage.HistoryEntry, error) {
out, err := u.issueRepo.History(ctx, id)
if err != nil {
return nil, fmt.Errorf("History: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) IterEvents(ctx context.Context, id string, limit int) (storage.Iter[types.Event], error) {
out, err := u.issueRepo.IterEvents(ctx, id, limit)
if err != nil {
return nil, fmt.Errorf("IterEvents: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetStaleIssues(ctx context.Context, filter types.StaleFilter) ([]*types.Issue, error) {
out, err := u.issueRepo.GetStaleIssues(ctx, filter)
if err != nil {
return nil, fmt.Errorf("GetStaleIssues: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error) {
out, err := u.issueRepo.GetEpicsEligibleForClosure(ctx)
if err != nil {
return nil, fmt.Errorf("GetEpicsEligibleForClosure: %w", err)
}
return out, nil
}
func (u *issueUseCaseImpl) Unclaim(ctx context.Context, id, actor string, force bool) error {
if id == "" {
return fmt.Errorf("Unclaim: id must not be empty")
}
if err := u.issueRepo.UnclaimIssue(ctx, id, actor, force); err != nil {
return fmt.Errorf("Unclaim: %w", err)
}
return nil
}
func (u *issueUseCaseImpl) ReclaimExpiredLeases(ctx context.Context, olderThan time.Duration, filter types.ReclaimFilter, actor string) ([]types.ReclaimedLease, error) {
out, err := u.issueRepo.ReclaimExpiredLeases(ctx, olderThan, filter, actor)
if err != nil {
return nil, fmt.Errorf("ReclaimExpiredLeases: %w", err)
}
return out, nil
}