beads/internal/storage/issueops/dependency_queries.go
Julian Knutsen 34b9a132f8
feat(storage): events + dependents reads, batched blocked, public claim surface (#4926)
* feat(storage): engine reads — EventsSince, GetDependentRecords, EventClaimed

`bd activity`/`bd show` render event history, and the library's own callers
need a durable, resumable feed of what changed — but the storage surface had
only the time-granular GetAllEventsSince, no keyset cursor, and no raw
target-keyed dependents read. Add both as additive extension interfaces plus
backend methods; no wire, schema, CLI, or existing-signature changes. Each new
read delegates its SQL to the shared issueops helpers the backends already use.

- Extract the raw "claimed" event literal into a typed EventClaimed constant
  (types) and use it at the single write site (issueops/claim.go). The on-disk
  value is byte-identical.
- EventsSince(cursor{created_at,id}, limit) — a durable-only keyset read over
  the events table, for a change feed that pages forward without dropping or
  duplicating same-second ties. New EventQueryStore extension interface +
  EventCursor, composed into DoltStorage and implemented on the Dolt backends.
- GetDependentRecords(targetID, depType, limit, afterID) on
  DependencyQueryStore — a raw target-keyed dependents read (the inbound edges)
  that returns edges from dangling/cross-project/wisp sources rather than
  dropping them, so `bd dep list`-style dependents views see the whole set.

Real-Dolt tests cover direction correctness, type filter, keyset paging,
same-second id tie-break, cursor exclusivity, durable-only scope, and the
claimed-constant write path.

(cherry picked from commit 35ae4d186047d99bef6d492132f5767ef2db5d7d)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(storage): make EventsSince sargable + add a per-bead scope

The EventsSince cursor predicate `(created_at > ?) OR (created_at = ? AND id > ?)`
full-scans on Dolt (EXPLAIN: Filter over a bare Table + TopN). Rewrite it to the
logically-equivalent, SARGABLE form

	created_at >= ? AND ((created_at > ?) OR (id > ?))

The redundant `created_at >= ?` lower bound flips the plan to
IndexedTableAccess(events) on [events.created_at] (verified by a new
EXPLAIN FORMAT=TREE regression test). The composite (created_at, id) row-value
form is deliberately NOT used: the events table has only single-column
idx_events_created_at (no composite index) on every backend, so the >= lower
bound is the seeking form; the shared issueops query serves each backend.

Also give EventsSince an optional issueID filter ("" = all) implemented off the
same keyset with `issue_id = ?`, so `bd show`'s per-bead event history is one
scoped read of the same primitive rather than a second one. The signature is
changed while unmerged across the interface + dolt/embeddeddolt backends.

Coverage/docs: dolt suite gains issue-filter, limit-clamp (default 100 / cap
500), and the EXPLAIN sargability guard; embeddeddolt gains a mirrored feed
test; the interface doc names idx_events_created_at and states the
commit-visibility-lag caveat (a resuming consumer overlaps a ~45s slack window
and de-duplicates by id).

(cherry picked from commit 3b7903290fad76e15ece171dcf6bdfd5f0f584b8)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(storage): single-source the EventsSince sargability guard from production SQL

The EXPLAIN sargability guard asserted a hand-copied literal of the EventsSince
query, so it could pass while the production predicate silently drifted.
Single-source it: export issueops.EventsSinceQuery(issueID, limit) — the exact
SQL EventsSinceInTx runs — and have the durable-events plan guard EXPLAIN that
string (placeholders literalized) instead of a copy. A change to the SARGABLE
predicate now breaks the guard.

Adds a literalizeParams test helper that binds ? placeholders to literals in
order and panics on arity drift, so a drifted placeholder count fails loudly
rather than EXPLAINing malformed SQL.

(cherry picked from commit dda19e737ac17ae4387c6abc4a1acca287b85494)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(storage): dependents reads — total id cursor, sargable target, + CountDependentRecords

Three coupled fixes to the target-keyed dependents read behind `bd dep list`'s
dependents views and the library's own callers:

TOTAL KEYSET. The old cursor paged on source issue_id, which is not a total key
for a fixed target: a source can appear across the durable and wisp dependency
tables, so paging on it could drop or duplicate rows (the Type tie-break was
dead code and the "issue_id is unique" comment was false). Cursor instead on the
dependency row's own primary id (depid.New(issue_id, target), a UUIDv5 that is
stable and globally unique across both tables): ORDER BY id ASC, afterID = last
row's id. types.Dependency gains an additive ID field, scanned by a dedicated
scanDependentRow (the shared scanDependencyRow is left untouched so no
source-keyed read has to select id). The two per-table pages merge by id and
truncate — total, no drop, no dup, proven by a page-size-1 walk across a
boundary that spans both tables (durable + wisp sources).

SARGABLE TARGET. Replace COALESCE(target...)=? (an expression no index can
match) with an explicit per-column OR, depTargetEqualsOr. On an index-merging
planner it plans as a BitmapOr index-merge over the three idx_dep_*_target
indexes where COALESCE was a full scan; on Dolt it is at worst no worse than
COALESCE, and the type-filtered path seeks the (type, target) composite on both.

CountDependentRecords(ctx, targetID, depType): the same sargable predicate,
COUNT only, summed across both tables — callers need a true total membership
count without paging to exhaustion. Implemented once in issueops and wired onto
the DependencyQueryStore interface + the dolt/embeddeddolt backends.

Coverage: dolt + embeddeddolt tests for both reads (two-table span, type filter,
keyset boundary, limit clamp default 100). Method docs name the cursor key and
the two-table raw-read / policy-at-hydration contract.

(cherry picked from commit 16db364d94f8525b9800483dc5ddcdccf94593f8)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(storage): de-dup cross-table depid collisions in dependents reads

depid.New keys a dependency row's id on (issue_id, target) and omits the table,
so the SAME edge present in BOTH dependencies and wisp_dependencies — a wisp
promoted to durable, or two Dolt clones merged — carries one id in both tables.

GetDependentRecordsInTx appended both per-table pages and sorted by id, so a
colliding edge produced a duplicate id in a merged page (and, at a page
boundary, a dropped row on the next `id > afterID`). CountDependentRecordsInTx
summed two raw COUNT(*)s, over-counting the collision beyond the distinct keyset
row count.

De-dup the paged read by id, iterating durable first and keeping the durable
(authoritative, non-ephemeral) row. Make the count agree with that de-dup:
durable rows plus wisp rows whose depid is not already a durable row for the same
target/type (an uncorrelated, bounded NOT IN over the sargable per-column OR
target predicate), so the total is distinct-by-id.

Adds a dolt regression test that seeds the same edge in both tables (FK checks
relaxed to reproduce the post-merge state) and asserts one row per id in pages,
Count == distinct total (3, not the sum 4), and stable keyset paging across the
collision. Fails before this change (id appears twice), passes after.

(cherry picked from commit c6825f8513cd38967fb73856253bd99a5eb65fc2)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(storage): batched target-keyed dependents read (GetDependentRecordsForIssues)

Adds GetDependentRecordsForIssues(ctx, targetIDs) → map[targetID][]*Dependency:
for a SET of target ids in ONE batched read, each id's INCOMING dependency rows
(its dependents), spanning BOTH the durable and wisp dependency tables, ALL dep
types (the caller filters), de-duped by row id. It is the target-keyed mirror of
the source-keyed GetDependencyRecordsForIssues — the whole-page read that lets
`bd dep list`-style dependents views and the library's own callers render every
id's inbound blocking edges without a per-id fan-out, and unlike
GetBlockingInfoForIssues's blocksMap it keeps the full type set (waits-for /
conditional-blocks are not dropped) and each row's real dep_type.

Cross-table de-dup matches GetDependentRecordsInTx: a wisp promoted to durable
carries one depid in both tables, so the durable table is scanned first and a
repeat id is skipped. The target is matched by the coalesced target expression
(depTargetIn), the same predicate the batched blocks/counts reads use.

Wired through issueops + the DependencyQueryStore interface + the dolt /
embeddeddolt backends. (The public-root re-export via beads.DependentQuerier
lands with the narrow public query-interface work.)

Tests: dolt + embeddeddolt batched-read regressions (multi-target keying, both
tables, full type set incl conditional-blocks/waits-for, decoy exclusion, absent
leaf, empty input).

(cherry picked from commit 1f69cedf282471d474ac5b1be633c7a9b18990c8)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(storage): batch transitive is_blocked read on DependencyQueryStore

`bd`'s discovery/ready-list views and the library's own callers need the
denormalized transitive is_blocked value for a PAGE of up to ~200 issues; the
surface only had the single IsBlocked(ctx, id), so a page cost N per-row calls.
Add IsBlockedBatch(ctx, ids) (map[string]bool) to the DependencyQueryStore
interface and implement it once in issueops.IsBlockedBatchInTx: a batched
SELECT id, is_blocked FROM {issues,wisps} WHERE id IN (...) at queryBatchSize=200
— the same denormalized column IsBlocked reads, NOT a recompute — so it returns
the identical transitive value per id, reflecting inherited/ancestor blockedness
(a child of a blocked parent is blocked with no direct blocking edge). ids
present in neither table are absent from the map; cross-table dups prefer the
wisps row, matching loadStatusByIDInTx and the search wisp-merge.

Wired onto the dolt and embeddeddolt backends via the same pattern as
CountDependentRecords (each delegates through its read tx).

Test: a dolt parity suite seeding a direct blocker, an inherited parent-child
block (transitive is_blocked with an EMPTY direct-blocker set), and an unblocked
control, asserting IsBlockedBatch agrees with per-row IsBlocked for every id and
that a missing id is absent from the map.

(cherry picked from commit 1cd441231e2c5a2b45064db01a03bb06de9009b9)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(issueops): IsBlockedBatch issues-wins on cross-table id collision, matching IsBlocked

IsBlockedInTx (per-row) scans issues→wisps and breaks on the first table that
has the id, so ISSUES wins the shared denormalized is_blocked field on a
cross-table (issues vs wisps) id collision. IsBlockedBatchInTx preferred the
wisps row, so the two reads disagreed on the same stored flag for an id present
in BOTH tables.

Make the batch keep the first-seen (issues) value and skip any later (wisps)
duplicate, mirroring the single read's precedence exactly. Both the dolt and
embeddeddolt backends delegate to this shared function, so they stay consistent.

Add a dolt regression seeding the same id in issues (is_blocked=1) and wisps
(is_blocked=0) and asserting IsBlocked(id) == IsBlockedBatch([id])[id] == the
issues value. Verified it fails before the fix (true vs false).

(cherry picked from commit 7dbd4f849d589acb15f8168532fcf8874aa87c99)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(embeddeddolt): IsBlockedBatch parity regression on the embedded backend

Mirror the dolt IsBlockedBatch coverage on the embedded Dolt backend: assert the
batched transitive is_blocked read agrees with per-row IsBlocked for every id and
reflects an inherited parent-child block (is_blocked true with an empty
direct-blocker set). Complements the already-present embedded keyset regression.

(cherry picked from commit 414e7eaf8de43eb1786088c2851c18763f6724ba)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(beads): re-export claim surface, error taxonomy, and query interfaces on public root

Public root-package surface for the claim/error/query reads consumers reach by
type-assertion. Pure re-export + a string-coupled conflict shim; no engine
behavior changes.

- IssueClaimer interface (ClaimIssue/ClaimReadyIssue, which live on the
  storage.BulkIssueStore extension, not the base Storage) + AsIssueClaimer
  type-assertion helper. Callers assert once at startup and fail loud, per the
  decorator note.
- Re-export ErrCircuitOpen (storage/dolt) as an aliased root sentinel so
  errors.Is works across the boundary (the claim sentinels ErrAlreadyClaimed /
  ErrNotClaimable are already re-exported with the other sentinels); a
  wrap-preservation test pins this against errors wrapped the way the engine
  wraps them.
- Typed conflict detail: the conditional-UPDATE claim path returns no typed
  result on conflict — the current assignee/status remains embedded in the
  error string. Rather than change that internal signature, expose
  ClaimConflict + ParseClaimConflict (with its own test) to recover it.
- Re-export DependencyQueryStore, EventQueryStore, EventCursor aliases and the
  EventClaimed constant so consumers reach the new reads by type-assertion.

(cherry picked from commit 70f443ad941117a74f3143f8570f91a0f721ece4)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(beads): derive claim-conflict markers from a storage source of truth

ParseClaimConflict hardcoded the message literals "issue already claimed by "
and "issue not claimable: status ", coupling the root parser to the engine's
fmt.Errorf format three layers away with nothing to catch drift.

- Export the two format fragments next to the sentinels: storage.ClaimedByFragment
  (" by ") and storage.NotClaimableStatusFragment (": status "). issueops/claim.go
  now wraps with "%w%s%s" using them (on-disk/message bytes unchanged), and
  ParseClaimConflict reconstructs its markers as ErrAlreadyClaimed.Error()+fragment
  rather than hardcoding — producer and parser can no longer drift independently.
- Harden tailAfter: recover the assignee/status only as the trailing run; if the
  tail still contains a known marker (an appended wrap corrupted it) return "".
  Documents the repo convention that claim-error wraps PREPEND context.
- Producer-tied tripwires: a dolt-suite round-trip (real ClaimIssue → both
  conflict branches, recovered via the storage fragments) and a root external
  round-trip (real Dolt store via beads.Open → AsIssueClaimer → ParseClaimConflict
  recovers assignee/status). The latter also exercises AsIssueClaimer against a
  live Storage.

(cherry picked from commit aced93f3d93c3fb779702df0549fab58369af997)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(beads): bound the claim-conflict token so an appended wrap can't leak garbage

tailAfter took the entire trailing run after the fragment, guarding only against
a further embedded full marker. A plain appended wrap — fmt.Errorf("%w (ctx)",
claimErr) — sailed past that guard and returned "alice (ctx)" as the assignee.

Bound the token to a single whitespace-free run: a status never contains
whitespace and an assignee is a whitespace-free actor id by convention, so if the
tail carries trailing content (whitespace or an appended "(...)" wrap) we return
"" — the documented best-effort empty-field outcome, with ParseClaimConflict
still reporting ok=true on the errors.Is match. A prepended wrap (the repo
convention) and a clean conflict are unaffected.

Tests: a direct tailAfter table (clean, prepended, appended-paren, appended-space,
absent, empty) and a ParseClaimConflict appended-wrap case asserting empty field
with ok=true.

(cherry picked from commit 181f47c7b7b4bb397c5f16992d1e33b75982eb04)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor(beads): narrow public query interfaces + claim-surface drift guards

Public-surface hardening for the library's consumers, done while unmerged
(cheap now, expensive later).

Stop aliasing whole internal engine interfaces on the root. Replace the
`DependencyQueryStore = storage.DependencyQueryStore` / `EventQueryStore =
storage.EventQueryStore` aliases with NARROW, hand-declared root interfaces that
expose exactly what consumers use:
  - EventQuerier { EventsSince(...) }
  - DependentQuerier { GetDependentRecords(...); CountDependentRecords(...) }
plus AsEventQuerier / AsDependentQuerier helpers mirroring AsIssueClaimer. Every
As* helper now first tries the direct assertion, then storage.UnwrapStore, so a
HookFiringStore-decorated store keeps its capability (AsIssueClaimer gains the
same unwrap; previously it would return false for a decorated store).

Drift guards: compile-time assertions tie each narrow interface to the full
engine interface storage.DoltStorage (in beads.go) AND to both concrete stores —
*dolt.DoltStore and *embeddeddolt.EmbeddedDoltStore (in tests) — so a signature
change to the claim/event/dependents surface breaks the build instead of
silently making As* return false. A live-Dolt test exercises AsEventQuerier /
AsDependentQuerier and their methods end to end.

(cherry picked from commit ddf7b2ab8a2fa41e36f13ccf701983d0bcd01faa)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* refactor(beads): drop the dead UnwrapStore fallback from the As* accessors

The storage.UnwrapStore fallback in AsIssueClaimer/AsEventQuerier/
AsDependentQuerier was provably unreachable. ClaimIssue/ClaimReadyIssue,
EventsSince, and the dependents reads all live on the engine interface
storage.DoltStorage (via BulkIssueStore / EventQueryStore /
DependencyQueryStore), and the compile-time drift guards prove
storage.DoltStorage satisfies each narrow surface. So whenever the fallback's
`s.(storage.DoltStorage)` succeeds, `s` already satisfies the narrow surface and
the leading direct assertion has already returned — branch 2 can only run when
branch 1 failed, and branch 1 failing implies s is not a DoltStorage, so branch 2
fails too. The only decorator, HookFiringStore, embeds storage.DoltStorage and
overrides none of these methods, so it forwards them by promotion and resolves on
the direct assertion.

Reduce the three accessors to a single direct type-assertion and document the
decorator contract (a decorator MUST embed storage.DoltStorage — unlike the
cmd/bd optional interfaces such as StoreLocator/BackupStore, which are not part
of DoltStorage and so genuinely need UnwrapStore, which is unchanged and still
used there). Adds a test that a HookFiringStore-decorated live store resolves
through all three accessors — the guard that would catch a future decorator that
stops forwarding.

(cherry picked from commit 0d9d0500edd3c165f101bbb590dcea7147f3530b)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(beads): re-export the batch transitive-blocked surface on the public root

Add BlockedQuerier (IsBlocked + IsBlockedBatch) and AsBlockedQuerier to the public
beads package, following the established narrow-interface + As* accessor style
(EventQuerier/DependentQuerier): a hand-declared root interface exposing only the
transitive-blocked reads consumers use, reachable via a single direct type
assertion, with a compile-time drift guard (var _ BlockedQuerier =
(storage.DoltStorage)(nil)) so a signature change on the engine breaks the build
here instead of silently making the accessor return false.

The (created_at, id) keyset needs no new re-export: it is additive fields on the
already-re-exported beads.IssueFilter, honored by the already-exported
Storage.SearchIssues / SearchIssuesWithCounts.

query_interfaces tests gain the concrete-store conformance assertions (server +
embedded Dolt), a live-Dolt exercise of AsBlockedQuerier proving IsBlockedBatch
agrees with per-row IsBlocked through the public surface, and the decorator-
contract check that AsBlockedQuerier resolves through a HookFiringStore.

(cherry picked from commit e8b87b6acf601bc2b323d92fbe863c4be7a99386)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(beads): re-export GetDependentRecordsForIssues on the public DependentQuerier

Surface the batched target-keyed dependents read (GetDependentRecordsForIssues,
landed at the storage layer earlier in the dependents-reads work) on the public
root DependentQuerier interface, so a consumer reaching the dependents surface
via AsDependentQuerier can fetch a whole page of inbound edges for a SET of
target ids in one call — the batched mirror of GetDependencyRecordsForIssues —
without a per-id fan-out. A live-Dolt query_interfaces assertion exercises it
through the public surface.

(cherry picked from commit 1f69cedf282471d474ac5b1be633c7a9b18990c8)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(storage): transactional snapshot reads on storage.Transaction

Composite views (bd show-style assemblies of counts, relations, comments, and
history) previously issued each read as its own store call with no shared
snapshot, so related numbers could disagree. Widen storage.Transaction with
the read subset those views need — CountIssuesByGroup, GetIssueCommentsPage,
GetDependentRecords/GetDependentRecordsForIssues/CountDependentRecords,
IsBlocked/IsBlockedBatch, EventsSince — so a caller can assemble the whole
view inside one transaction. Pure surface addition: every method delegates to
the existing InTx implementation; no new SQL, no semantics changes.

On the server backend the transaction spans two sessions (durable + clone-
local). Single-tier reads route by wispness and see this transaction's own
uncommitted writes on both tiers (comments page, blocked checks — including
mixed batches via tier partitioning). Reads that union both tables in one
query (dependents family, grouped counts) run on the durable session and see
uncommitted durable writes plus committed clone-local state; that limitation
is documented per method on the interface and asserted per backend in the
tests. The embedded backend is single-handle and fully read-your-writes.

Conformance adds a snapshot test that mutates inside the transaction and
asserts exact deltas across all the new reads (a non-transactional delegation
fails it), a read-your-writes test covering every method, and wisp-bearing
fixtures; the server backend adds a clone-local-tier read-your-writes test
pinning both the routed reads and the documented spanning-read behavior.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(issueops): drop depTargetEqualsOr's dead alias parameter

Every caller on this branch passes "" — the aliased form's only consumer
lived in a backend package that does not exist here — so golangci-lint's
unparam check rejects the parameter. Inline the bare-column form.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(storage): wrap ErrAlreadyClaimed for open foreign-assignee; split tx conformance tests

Maintainer review fixes for PR #4926 (iteration 1).

Contract (major): issueops.ClaimIssueInTx's open-but-assigned refusal branch
returned a plain error, so the public IssueClaimer contract (wrapped conflict
sentinels, classified by errors.Is / ParseClaimConflict) was false for the
common "open issue pre-assigned to another actor" case. Wrap
storage.ErrAlreadyClaimed while preserving the holder-focused wording, matching
the already-correct domain-stack twin. Adds a conformance test
(ClaimOpenForeignAssignee) and a ParseClaimConflict subtest for the wrapped
open-assigned form.

Complexity (major): split testTransactionSnapshotReads and
testTransactionReadYourWrites into named read + assertion helpers
(readSnapView/assertSnapBaseline/assertSnapDelta and
seedReadYourWritesGraph/assertInTxDependentReads/assertInTxCountsAndEvents) with
equivalent coverage, dropping the worst changed function out of the major
cyclomatic/cognitive band (39/73 -> 11/17 and 23/38 -> 4/5).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(issueops): extract per-table read to cut IsBlockedBatchInTx complexity

Review fix for #4926. IsBlockedBatchInTx inlined the whole two-table batched
scan (gocognit 29, gocyclo 12), which the quality scorecard flagged as a major
readability finding. Extract the per-table batched read into
readIsBlockedIntoFromTable, mirroring the sibling
GetDependentRecordsForIssuesInTx / getDependentRecordsIntoFromTable split, so the
outer function only dispatches the issues+wisps read and nesting resets at the
function boundary.

Behavior-preserving: the issues-win cross-table de-dup and the optional-wisps
table-not-exist skip are unchanged -- same helper-wraps / outer-classifies idiom
as the sibling, and isTableNotExistError unwraps the %w wrap. The embedded
TestIsBlockedBatchEmbedded parity test still passes against a real Dolt backend.
gocognit 29 -> helper 15; gocyclo 12 -> 6.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Test User <test@test.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: CI Bot <ci@beads.test>
2026-07-22 07:41:47 -07:00

1076 lines
38 KiB
Go

package issueops
import (
"context"
"database/sql"
"errors"
"fmt"
"sort"
"strings"
"github.com/steveyegge/beads/internal/types"
)
// GetAllDependencyRecordsInTx returns all dependency records from permanent and
// wisp dependency tables.
func GetAllDependencyRecordsInTx(ctx context.Context, tx DBTX) (map[string][]*types.Dependency, error) {
result := make(map[string][]*types.Dependency)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
if err := getAllDependencyRecordsIntoFromTable(ctx, tx, depTable, result); err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, err
}
}
return result, nil
}
//nolint:gosec // G201: depTable is "dependencies" or "wisp_dependencies" (hardcoded by caller).
func getAllDependencyRecordsIntoFromTable(ctx context.Context, tx DBTX, depTable string, result map[string][]*types.Dependency) error {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s
ORDER BY issue_id
`, DepTargetExpr, depTable))
if err != nil {
return fmt.Errorf("get all dependency records from %s: %w", depTable, err)
}
defer rows.Close()
for rows.Next() {
dep, scanErr := scanDependencyRow(rows)
if scanErr != nil {
return fmt.Errorf("get all dependency records from %s: %w", depTable, scanErr)
}
result[dep.IssueID] = append(result[dep.IssueID], dep)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("get all dependency records from %s: %w", depTable, err)
}
return nil
}
// GetDependencyRecordsForIssuesInTx returns dependency records for specific issues,
// routing each ID to dependencies or wisp_dependencies based on wisp status.
// Uses a single batched wisp-partition query + batched IN clauses, so cost is
// O(1 + N/queryBatchSize) round-trips rather than O(N) — important on remote
// backends (see GH#3414).
func GetDependencyRecordsForIssuesInTx(ctx context.Context, tx DBTX, issueIDs []string) (map[string][]*types.Dependency, error) {
if len(issueIDs) == 0 {
return make(map[string][]*types.Dependency), nil
}
wispIDs, permIDs, err := PartitionWispIDsInTx(ctx, tx, issueIDs)
if err != nil {
return nil, err
}
result := make(map[string][]*types.Dependency)
if len(wispIDs) > 0 {
if err := getDependencyRecordsIntoFromTable(ctx, tx, "wisp_dependencies", wispIDs, result); err != nil {
return nil, err
}
}
if len(permIDs) > 0 {
if err := getDependencyRecordsIntoFromTable(ctx, tx, "dependencies", permIDs, result); err != nil {
return nil, err
}
}
return result, nil
}
// GetDependencyRecordsForIssuesFromTableInTx is a fast-path variant used by
// callers that already know every ID belongs to a single dep table (e.g.
// searchTableInTx). Skips the wisp-partition round-trip.
func GetDependencyRecordsForIssuesFromTableInTx(ctx context.Context, tx DBTX, depTable string, issueIDs []string) (map[string][]*types.Dependency, error) {
if len(issueIDs) == 0 {
return make(map[string][]*types.Dependency), nil
}
result := make(map[string][]*types.Dependency)
if err := getDependencyRecordsIntoFromTable(ctx, tx, depTable, issueIDs, result); err != nil {
return nil, err
}
return result, nil
}
//nolint:gosec // G201: depTable is "dependencies" or "wisp_dependencies" (hardcoded by callers).
func getDependencyRecordsIntoFromTable(ctx context.Context, tx DBTX, depTable string, ids []string, result map[string][]*types.Dependency) error {
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
end = len(ids)
}
batch := ids[start:end]
placeholders := make([]string, len(batch))
args := make([]any, len(batch))
for i, id := range batch {
placeholders[i] = "?"
args[i] = id
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s WHERE issue_id IN (%s) ORDER BY issue_id`,
DepTargetExpr, depTable, strings.Join(placeholders, ",")), args...)
if err != nil {
return fmt.Errorf("get dependency records from %s: %w", depTable, err)
}
for rows.Next() {
dep, scanErr := scanDependencyRow(rows)
if scanErr != nil {
_ = rows.Close()
return fmt.Errorf("get dependency records: scan: %w", scanErr)
}
result[dep.IssueID] = append(result[dep.IssueID], dep)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("get dependency records: rows: %w", err)
}
}
return nil
}
// GetDependentRecordsForIssuesInTx returns raw dependency rows keyed by TARGET
// id: for each id in targetIDs, the rows whose target is that id — its INCOMING
// edges, i.e. its dependents — spanning BOTH the durable and wisp dependency
// tables and applying NO type filter or visibility policy (the caller filters
// at hydration). It is the batched, target-keyed mirror of the source-keyed
// GetDependencyRecordsForIssuesInTx: one query per table per batch of
// queryBatchSize target ids, so cost is O(1 + N/queryBatchSize) round-trips per
// table rather than O(N) — the whole-page read that lets a caller render every
// id's inbound `blocks` edges without a per-id fan-out.
//
// A target is matched by the coalesced target expression (DepTargetExpr) — the
// same predicate the batched source-keyed blocks/counts reads use — so an id
// that appears in any of the three typed target columns resolves; each returned
// row's DependsOnID is that resolved target, which is the map key. Rows are
// de-duped by their primary id across the two tables exactly as
// GetDependentRecordsInTx does: a wisp promoted to durable carries ONE depid in
// both tables, so the durable table is scanned first and a repeat id from the
// wisp table is skipped — the edge is returned once, as its authoritative
// durable row.
func GetDependentRecordsForIssuesInTx(ctx context.Context, tx DBTX, targetIDs []string) (map[string][]*types.Dependency, error) {
result := make(map[string][]*types.Dependency)
if len(targetIDs) == 0 {
return result, nil
}
// De-dup by row id across the two tables, preferring the durable copy scanned
// first — same cross-table collision handling as GetDependentRecordsInTx.
seen := make(map[string]bool)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
if err := getDependentRecordsIntoFromTable(ctx, tx, depTable, targetIDs, seen, result); err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, err
}
}
return result, nil
}
//nolint:gosec // G201: depTable is "dependencies" or "wisp_dependencies" (hardcoded by caller); placeholders are ? only.
func getDependentRecordsIntoFromTable(ctx context.Context, tx DBTX, depTable string, targetIDs []string, seen map[string]bool, result map[string][]*types.Dependency) error {
for start := 0; start < len(targetIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(targetIDs) {
end = len(targetIDs)
}
batch := targetIDs[start:end]
placeholders := make([]string, len(batch))
args := make([]any, len(batch))
for i, id := range batch {
placeholders[i] = "?"
args[i] = id
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
`SELECT id, issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s WHERE %s ORDER BY %s`,
DepTargetExpr, depTable, depTargetIn("", strings.Join(placeholders, ",")), DepTargetExpr), args...)
if err != nil {
return fmt.Errorf("get dependent records from %s: %w", depTable, err)
}
for rows.Next() {
dep, scanErr := scanDependentRow(rows)
if scanErr != nil {
_ = rows.Close()
return fmt.Errorf("get dependent records: scan: %w", scanErr)
}
// De-dup by row id (depid): the wisp copy of a promoted edge carries
// the same id as the durable copy scanned first, so skip the repeat.
if seen[dep.ID] {
continue
}
seen[dep.ID] = true
result[dep.DependsOnID] = append(result[dep.DependsOnID], dep)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("get dependent records: rows: %w", err)
}
}
return nil
}
// Target-keyed dependents-read bounds. A raw read has no consumer to apply a
// page size, so it clamps its own (default when limit <= 0, hard cap otherwise).
const (
defaultDependentRecordsLimit = 100
maxDependentRecordsLimit = 500
)
// GetDependentRecordsInTx returns raw dependency rows whose TARGET is targetID
// — the edges pointing AT targetID — from both the permanent and wisp
// dependency tables. Unlike GetDependents/GetDependentsWithMetadata it does
// NOT join or hydrate the source issues, so edges from dangling, cross-project,
// or wisp sources are returned as raw rows rather than dropped. RAW READ: it
// spans BOTH the `dependencies` and `wisp_dependencies` tables and applies no
// visibility policy — filtering (e.g. a group-membership visibility rule) is
// the caller's job, applied at hydration.
//
// When depType is non-empty only rows of that dependency type are returned
// ("" = all types). Results are ordered by the dependency row's primary id ASC
// and bounded by limit (see the clamp constants). afterID is a keyset
// continuation over that id order: "" starts at the beginning, otherwise only
// rows with id > afterID are returned.
//
// CURSOR KEY: the dependency row's own id (depid.New(issue_id, target), a
// UUIDv5 that is stable and globally unique across both tables) — NOT the source
// issue_id. issue_id is not a total key for a fixed target: a source can appear
// across the two scanned tables, so paging on it could drop or duplicate rows.
// Paging on the unique row id is total, so each source's inbound edge is
// returned exactly once. The target match is a sargable per-column OR over the
// three typed columns (seeks idx_dep_*_target / the type composites) rather than
// a COALESCE wrapper.
func GetDependentRecordsInTx(ctx context.Context, tx DBTX, targetID, depType string, limit int, afterID string) ([]*types.Dependency, error) {
if limit <= 0 {
limit = defaultDependentRecordsLimit
}
if limit > maxDependentRecordsLimit {
limit = maxDependentRecordsLimit
}
var all []*types.Dependency
seen := make(map[string]bool)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := queryDependentRecordsFromTable(ctx, tx, depTable, targetID, depType, limit, afterID)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, err
}
// De-dup by row id across the two tables. depid.New keys the id on
// (issue_id, target) and deliberately omits the table, so the SAME edge
// that lands in BOTH tables — a wisp promoted to durable, or two clones
// merged — carries ONE id in both. Appending the wisp copy would put a
// duplicate id in the page and, at a page boundary, drop it on the next
// `id > afterID`. We iterate durable first and skip an already-seen id,
// so a colliding edge is returned exactly once as its DURABLE row (the
// authoritative, non-ephemeral copy). Within a single table id is a
// PRIMARY KEY, so intra-table rows never collide.
for _, r := range rows {
if seen[r.ID] {
continue
}
seen[r.ID] = true
all = append(all, r)
}
}
// Merge the de-duped per-table pages into one total order by row id. Each
// table returned its first `limit` rows with id > afterID, so the global
// first `limit` DISTINCT ids > afterID are all present; sorting by the
// globally unique id and truncating yields exactly that page — no drop, no
// dup, and stable across a cross-table id collision.
sort.Slice(all, func(i, j int) bool { return all[i].ID < all[j].ID })
if len(all) > limit {
all = all[:limit]
}
return all, nil
}
//nolint:gosec // G201: depTable is a hardcoded constant; targetID/depType/afterID are bound as parameters.
func queryDependentRecordsFromTable(ctx context.Context, tx DBTX, depTable, targetID, depType string, limit int, afterID string) ([]*types.Dependency, error) {
query := fmt.Sprintf(`
SELECT id, issue_id, %s AS depends_on_id, type, created_at, created_by, metadata, thread_id
FROM %s
WHERE %s`, DepTargetExpr, depTable, depTargetEqualsOr())
args := []any{targetID, targetID, targetID}
if depType != "" {
query += " AND type = ?"
args = append(args, depType)
}
if afterID != "" {
query += " AND id > ?"
args = append(args, afterID)
}
query += fmt.Sprintf(" ORDER BY id ASC LIMIT %d", limit)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("get dependent records from %s: %w", depTable, err)
}
defer rows.Close()
var deps []*types.Dependency
for rows.Next() {
dep, scanErr := scanDependentRow(rows)
if scanErr != nil {
return nil, fmt.Errorf("get dependent records: scan: %w", scanErr)
}
deps = append(deps, dep)
}
return deps, rows.Err()
}
// scanDependentRow scans a dependents row that INCLUDES the row id (the keyset
// cursor). The shared scanDependencyRow does not select id, and adding it there
// would ripple through every source-keyed read, so the target-keyed read owns
// this variant.
func scanDependentRow(rows *sql.Rows) (*types.Dependency, error) {
var dep types.Dependency
var createdAt sql.NullTime
var metadata, threadID sql.NullString
if err := rows.Scan(&dep.ID, &dep.IssueID, &dep.DependsOnID, &dep.Type, &createdAt, &dep.CreatedBy, &metadata, &threadID); err != nil {
return nil, fmt.Errorf("scan dependent: %w", err)
}
if createdAt.Valid {
dep.CreatedAt = createdAt.Time
}
if metadata.Valid {
dep.Metadata = metadata.String
}
if threadID.Valid {
dep.ThreadID = threadID.String
}
return &dep, nil
}
// CountDependentRecordsInTx returns the number of DISTINCT inbound edges of
// targetID, applying the same sargable target predicate and optional depType
// filter as GetDependentRecordsInTx but no keyset/limit. Callers that want a
// true total membership count need it without paging to exhaustion. Like the
// paged read it is a RAW count spanning both tables; the caller applies any
// visibility policy separately.
//
// It must agree with GetDependentRecordsInTx's durable-preferred de-dup: an edge
// present in BOTH tables (same depid) is ONE row in the page, so the count is
// every durable row PLUS every wisp row whose depid is not already a durable row
// for the same target/type. Summing two raw COUNT(*)s would over-count that edge
// and exceed the distinct keyset row count.
func CountDependentRecordsInTx(ctx context.Context, tx DBTX, targetID, depType string) (int, error) {
durable, err := countDependentRecordsFromTable(ctx, tx, "dependencies", targetID, depType)
if err != nil {
return 0, err
}
wispExtra, err := countWispDependentsNotInDurableInTx(ctx, tx, targetID, depType)
if err != nil {
// wisp_dependencies absent: the durable count is the whole answer.
if isTableNotExistError(err) {
return durable, nil
}
return 0, err
}
return durable + wispExtra, nil
}
// countWispDependentsNotInDurableInTx counts wisp_dependencies rows whose target
// is targetID (optional depType) but whose depid is NOT present in the durable
// dependencies table for the same target/type — the wisp-ONLY inbound edges.
// Colliding edges (present in both tables) are counted on the durable side, so
// this is the exact complement that makes the total distinct-by-id. The NOT IN
// subquery is uncorrelated and bounded by the target's durable inbound-edge
// count; both arms use the sargable per-column OR target predicate.
//
//nolint:gosec // G201: table names are hardcoded constants; targetID/depType are bound as parameters.
func countWispDependentsNotInDurableInTx(ctx context.Context, tx DBTX, targetID, depType string) (int, error) {
wispWhere := depTargetEqualsOr()
durableWhere := depTargetEqualsOr()
args := []any{targetID, targetID, targetID}
if depType != "" {
wispWhere += " AND type = ?"
args = append(args, depType)
}
args = append(args, targetID, targetID, targetID)
if depType != "" {
durableWhere += " AND type = ?"
args = append(args, depType)
}
query := fmt.Sprintf(
"SELECT COUNT(*) FROM wisp_dependencies WHERE %s AND id NOT IN (SELECT id FROM dependencies WHERE %s)",
wispWhere, durableWhere)
var n int
if err := tx.QueryRowContext(ctx, query, args...).Scan(&n); err != nil {
return 0, fmt.Errorf("count wisp-only dependent records: %w", err)
}
return n, nil
}
//nolint:gosec // G201: depTable is a hardcoded constant; targetID/depType are bound as parameters.
func countDependentRecordsFromTable(ctx context.Context, tx DBTX, depTable, targetID, depType string) (int, error) {
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE %s", depTable, depTargetEqualsOr())
args := []any{targetID, targetID, targetID}
if depType != "" {
query += " AND type = ?"
args = append(args, depType)
}
var n int
if err := tx.QueryRowContext(ctx, query, args...).Scan(&n); err != nil {
return 0, fmt.Errorf("count dependent records from %s: %w", depTable, err)
}
return n, nil
}
func GetDependencyCountsInTx(ctx context.Context, tx DBTX, issueIDs []string) (map[string]*types.DependencyCounts, error) {
if len(issueIDs) == 0 {
return make(map[string]*types.DependencyCounts), nil
}
result := make(map[string]*types.DependencyCounts)
for _, id := range issueIDs {
result[id] = &types.DependencyCounts{}
}
depTables := []string{"dependencies", "wisp_dependencies"}
if empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx); probeErr != nil {
return nil, fmt.Errorf("get dependency counts: probe: %w", probeErr)
} else if empty {
depTables = []string{"dependencies"}
}
for start := 0; start < len(issueIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(issueIDs) {
end = len(issueIDs)
}
batch := issueIDs[start:end]
placeholders := make([]string, len(batch))
args := make([]any, len(batch))
for i, id := range batch {
placeholders[i] = "?"
args[i] = id
}
inClause := strings.Join(placeholders, ",")
for _, depTable := range depTables {
//nolint:gosec // G201: depTable is hardcoded and inClause contains only ? placeholders.
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, COUNT(*) as cnt
FROM %s
WHERE issue_id IN (%s) AND type = 'blocks'
GROUP BY issue_id
`, depTable, inClause), args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, fmt.Errorf("get dependency counts (blockers from %s): %w", depTable, err)
}
for depRows.Next() {
var id string
var cnt int
if err := depRows.Scan(&id, &cnt); err != nil {
_ = depRows.Close()
return nil, fmt.Errorf("get dependency counts: scan blocker: %w", err)
}
if c, ok := result[id]; ok {
c.DependencyCount += cnt
}
}
_ = depRows.Close()
if err := depRows.Err(); err != nil {
return nil, fmt.Errorf("get dependency counts: blocker rows: %w", err)
}
//nolint:gosec // G201: depTable is hardcoded and inClause contains only ? placeholders.
blockingRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT %s AS depends_on_id, COUNT(*) as cnt
FROM %s
WHERE %s AND type = 'blocks'
GROUP BY %s
`, DepTargetExpr, depTable, depTargetIn("", inClause), DepTargetExpr), args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, fmt.Errorf("get dependency counts (dependents from %s): %w", depTable, err)
}
for blockingRows.Next() {
var id string
var cnt int
if err := blockingRows.Scan(&id, &cnt); err != nil {
_ = blockingRows.Close()
return nil, fmt.Errorf("get dependency counts: scan dependent: %w", err)
}
if c, ok := result[id]; ok {
c.DependentCount += cnt
}
}
_ = blockingRows.Close()
if err := blockingRows.Err(); err != nil {
return nil, fmt.Errorf("get dependency counts: dependent rows: %w", err)
}
}
}
return result, nil
}
// GetBlockingInfoForIssuesInTx returns blocking dependency records for a set of issue IDs.
// Returns three maps:
// - blockedByMap: issueID -> list of IDs blocking it
// - blocksMap: issueID -> list of IDs it blocks
// - parentMap: childID -> parentID (parent-child deps)
func GetBlockingInfoForIssuesInTx(ctx context.Context, tx DBTX, issueIDs []string) (
blockedByMap map[string][]string,
blocksMap map[string][]string,
parentMap map[string]string,
err error,
) {
blockedByMap = make(map[string][]string)
blocksMap = make(map[string][]string)
parentMap = make(map[string]string)
if len(issueIDs) == 0 {
return
}
// Partition into wisp and perm IDs for routing. Use the batched
// partitioner so we don't take a round-trip per ID on remote backends
// (GH#3414).
wispIDs, permIDs, partErr := PartitionWispIDsInTx(ctx, tx, issueIDs)
if partErr != nil {
return nil, nil, nil, partErr
}
// Process wisp IDs against wisp_dependencies.
if len(wispIDs) > 0 {
if err := queryBlockedByInfo(ctx, tx, wispIDs, "wisp_dependencies", blockedByMap, parentMap); err != nil {
return nil, nil, nil, err
}
}
// Process perm IDs against dependencies.
if len(permIDs) > 0 {
if err := queryBlockedByInfo(ctx, tx, permIDs, "dependencies", blockedByMap, parentMap); err != nil {
return nil, nil, nil, err
}
}
// "Blocks" is target-oriented, so scan both dependency tables regardless of
// the target issue's storage class.
if err := queryBlocksInfo(ctx, tx, issueIDs, []string{"dependencies", "wisp_dependencies"}, blocksMap); err != nil {
return nil, nil, nil, err
}
return blockedByMap, blocksMap, parentMap, nil
}
type blockingInfoRow struct {
issueID, blockerID, depType string
}
// queryBlockedByInfo queries outbound blocking info from a specific dependency
// table. Blocker status is resolved against both issue storage classes so
// cross-class closed blockers do not appear active.
// Uses batched IN clauses (queryBatchSize) to avoid query-planner spikes.
func queryBlockedByInfo(
ctx context.Context, tx DBTX,
issueIDs []string,
depTable string,
blockedByMap map[string][]string,
parentMap map[string]string,
) error {
for start := 0; start < len(issueIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(issueIDs) {
end = len(issueIDs)
}
batch := issueIDs[start:end]
placeholders := make([]string, len(batch))
args := make([]any, len(batch))
for i, id := range batch {
placeholders[i] = "?"
args[i] = id
}
inClause := strings.Join(placeholders, ",")
// Query: "blocked by" — deps where issue_id is in our set.
//nolint:gosec // G201: depTable is a caller-controlled constant.
blockedByQuery := fmt.Sprintf(`
SELECT d.issue_id, %s AS depends_on_id, d.type
FROM %s d
WHERE d.issue_id IN (%s) AND d.type IN ('blocks', 'parent-child')
`, depTargetExpr("d"), depTable, inClause)
rows, err := tx.QueryContext(ctx, blockedByQuery, args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return fmt.Errorf("get blocked-by info from %s: %w", depTable, err)
}
var depRows []blockingInfoRow
var blockerIDs []string
for rows.Next() {
var row blockingInfoRow
if scanErr := rows.Scan(&row.issueID, &row.blockerID, &row.depType); scanErr != nil {
_ = rows.Close()
return fmt.Errorf("get blocking info: scan blocked-by: %w", scanErr)
}
depRows = append(depRows, row)
blockerIDs = append(blockerIDs, row.blockerID)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("get blocking info: blocked-by rows: %w", err)
}
statusByID, err := loadStatusByIDInTx(ctx, tx, blockerIDs)
if err != nil {
return fmt.Errorf("get blocking info: blocker status: %w", err)
}
for _, row := range depRows {
if statusByID[row.blockerID] == types.StatusClosed {
continue
}
if row.depType == "parent-child" {
parentMap[row.issueID] = row.blockerID
} else {
blockedByMap[row.issueID] = append(blockedByMap[row.issueID], row.blockerID)
}
}
}
return nil
}
// queryBlocksInfo queries inbound blocking info across dependency tables.
func queryBlocksInfo(
ctx context.Context, tx DBTX,
issueIDs []string,
depTables []string,
blocksMap map[string][]string,
) error {
for start := 0; start < len(issueIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(issueIDs) {
end = len(issueIDs)
}
batch := issueIDs[start:end]
placeholders := make([]string, len(batch))
args := make([]any, len(batch))
for i, id := range batch {
placeholders[i] = "?"
args[i] = id
}
inClause := strings.Join(placeholders, ",")
statusByID, err := loadStatusByIDInTx(ctx, tx, batch)
if err != nil {
return fmt.Errorf("get blocking info: blocker status: %w", err)
}
for _, depTable := range depTables {
// Query: "blocks" — deps where depends_on_id is in our set.
//nolint:gosec // G201: depTable is a caller-controlled constant.
blocksQuery := fmt.Sprintf(`
SELECT %s AS depends_on_id, d.issue_id, d.type
FROM %s d
WHERE %s AND d.type IN ('blocks', 'parent-child')
`, depTargetExpr("d"), depTable, depTargetIn("d", inClause))
rows, err := tx.QueryContext(ctx, blocksQuery, args...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return fmt.Errorf("get blocks info from %s: %w", depTable, err)
}
for rows.Next() {
var blockerID, blockedID, depType string
if scanErr := rows.Scan(&blockerID, &blockedID, &depType); scanErr != nil {
_ = rows.Close()
return fmt.Errorf("get blocking info: scan blocks: %w", scanErr)
}
if statusByID[blockerID] == types.StatusClosed {
continue
}
if depType == "parent-child" {
continue
}
blocksMap[blockerID] = append(blocksMap[blockerID], blockedID)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("get blocking info: blocks rows: %w", err)
}
}
}
return nil
}
func loadStatusByIDInTx(ctx context.Context, tx DBTX, ids []string) (map[string]types.Status, error) {
statusByID := make(map[string]types.Status)
if len(ids) == 0 {
return statusByID, nil
}
sourceByID := make(map[string]string)
for _, issueTable := range []string{"issues", "wisps"} {
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
end = len(ids)
}
placeholders, args := buildSQLInClause(ids[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id, status FROM %s WHERE id IN (%s)
`, issueTable, placeholders), args...)
if err != nil {
if optionalBlockedTable(issueTable) && isTableNotExistError(err) {
break
}
return nil, fmt.Errorf("status from %s: %w", issueTable, err)
}
for rows.Next() {
var id string
var status types.Status
if err := rows.Scan(&id, &status); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("scan status: %w", err)
}
if _, exists := sourceByID[id]; exists {
// Prefer wisps-table status on cross-table dup (be-iabdi).
// Tables iterate issues→wisps so the second encounter is always wisps.
sourceByID[id] = issueTable
statusByID[id] = status
continue
}
sourceByID[id] = issueTable
statusByID[id] = status
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("status rows from %s: %w", issueTable, err)
}
}
}
return statusByID, nil
}
// GetNewlyUnblockedByCloseInTx finds issues that become unblocked when the
// given issue is closed. Works within an existing transaction.
// Returns full issue objects for the newly-unblocked issues.
//
//nolint:gosec // G201: table names come from hardcoded constants
func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx DBTX, closedIssueID string) ([]*types.Issue, error) {
candidateSet := make(map[string]bool)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE %s AND type = 'blocks'
`, depTable, depTargetEquals("")), closedIssueID)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, fmt.Errorf("find blocked candidates from %s: %w", depTable, err)
}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("scan candidate from %s: %w", depTable, err)
}
candidateSet[id] = true
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("candidate rows from %s: %w", depTable, err)
}
}
if len(candidateSet) == 0 {
return nil, nil
}
candidateIDs := make([]string, 0, len(candidateSet))
for id := range candidateSet {
candidateIDs = append(candidateIDs, id)
}
sort.Strings(candidateIDs)
candidateStatusByID, err := loadStatusByIDInTx(ctx, tx, candidateIDs)
if err != nil {
return nil, fmt.Errorf("check candidate status: %w", err)
}
activeCandidateIDs := candidateIDs[:0]
for _, id := range candidateIDs {
status, ok := candidateStatusByID[id]
if !ok || status == types.StatusClosed || status == types.StatusPinned {
continue
}
activeCandidateIDs = append(activeCandidateIDs, id)
}
candidateIDs = activeCandidateIDs
if len(candidateIDs) == 0 {
return nil, nil
}
stillBlocked := make(map[string]bool)
for start := 0; start < len(candidateIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(candidateIDs) {
end = len(candidateIDs)
}
batch := candidateIDs[start:end]
placeholders, batchArgs := buildSQLInClause(batch)
remainingByCandidate := make(map[string][]string, len(batch))
remainingBlockerSet := make(map[string]struct{})
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
//nolint:gosec // G201: depTable is hardcoded.
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE issue_id IN (%s) AND type = 'blocks' AND %s != ?
`, DepTargetExpr, depTable, placeholders, DepTargetExpr), append(batchArgs, closedIssueID)...)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return nil, fmt.Errorf("check remaining blockers from %s: %w", depTable, err)
}
for depRows.Next() {
var candidateID, blockerID string
if err := depRows.Scan(&candidateID, &blockerID); err != nil {
_ = depRows.Close()
return nil, fmt.Errorf("scan remaining blocker: %w", err)
}
remainingByCandidate[candidateID] = append(remainingByCandidate[candidateID], blockerID)
remainingBlockerSet[blockerID] = struct{}{}
}
_ = depRows.Close()
if err := depRows.Err(); err != nil {
return nil, fmt.Errorf("remaining blocker rows from %s: %w", depTable, err)
}
}
remainingBlockerIDs := make([]string, 0, len(remainingBlockerSet))
for blockerID := range remainingBlockerSet {
remainingBlockerIDs = append(remainingBlockerIDs, blockerID)
}
sort.Strings(remainingBlockerIDs)
statusByID, err := loadStatusByIDInTx(ctx, tx, remainingBlockerIDs)
if err != nil {
return nil, fmt.Errorf("check remaining blocker status: %w", err)
}
for candidateID, blockerIDs := range remainingByCandidate {
for _, blockerID := range blockerIDs {
status, ok := statusByID[blockerID]
if ok && status != types.StatusClosed && status != types.StatusPinned {
stillBlocked[candidateID] = true
break
}
}
}
}
var unblocked []*types.Issue
for _, id := range candidateIDs {
if stillBlocked[id] {
continue
}
issue, err := GetIssueInTx(ctx, tx, id)
if err != nil {
continue
}
unblocked = append(unblocked, issue)
}
return unblocked, nil
}
// IsBlockedInTx checks if an issue is blocked by active dependencies within
// an existing transaction. Returns whether the issue is blocked and, if so,
// a list of blocker descriptions for display.
//
//nolint:gosec // G201: table names are hardcoded constants.
func IsBlockedInTx(ctx context.Context, tx DBTX, issueID string) (bool, []string, error) {
var blocked bool
found := false
for _, table := range []string{"issues", "wisps"} {
var b int
//nolint:gosec // G201: table is a hardcoded "issues" or "wisps".
err := tx.QueryRowContext(ctx, "SELECT is_blocked FROM "+table+" WHERE id = ?", issueID).Scan(&b)
if err == nil {
blocked = b != 0
found = true
break
}
if !errors.Is(err, sql.ErrNoRows) {
if optionalBlockedTable(table) && isTableNotExistError(err) {
continue
}
return false, nil, fmt.Errorf("read is_blocked from %s: %w", table, err)
}
}
if !found || !blocked {
return false, nil, nil
}
type depEdge struct {
dependsOnID, depType string
}
var edges []depEdge
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT %s AS depends_on_id, type FROM %s
WHERE issue_id = ? AND type IN ('blocks', 'waits-for', 'conditional-blocks')
`, DepTargetExpr, depTable), issueID)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
continue
}
return false, nil, fmt.Errorf("check blockers from %s: %w", depTable, err)
}
for rows.Next() {
var e depEdge
if err := rows.Scan(&e.dependsOnID, &e.depType); err != nil {
_ = rows.Close()
return false, nil, fmt.Errorf("scan blocker edge: %w", err)
}
edges = append(edges, e)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return false, nil, fmt.Errorf("blocker edge rows from %s: %w", depTable, err)
}
}
if len(edges) == 0 {
return true, nil, nil
}
blockerIDs := make([]string, 0, len(edges))
for _, e := range edges {
blockerIDs = append(blockerIDs, e.dependsOnID)
}
statusByID, err := loadStatusByIDInTx(ctx, tx, blockerIDs)
if err != nil {
return false, nil, fmt.Errorf("check blocker status: %w", err)
}
var blockers []string
for _, e := range edges {
status, ok := statusByID[e.dependsOnID]
if !ok {
continue
}
if status == types.StatusClosed || status == types.StatusPinned {
continue
}
if e.depType != "blocks" {
blockers = append(blockers, e.dependsOnID+" ("+e.depType+")")
} else {
blockers = append(blockers, e.dependsOnID)
}
}
return true, blockers, nil
}
// IsBlockedBatchInTx returns the denormalized, TRANSITIVE is_blocked flag for
// each of ids in one batched read — the same value IsBlockedInTx returns per id,
// without the per-row blocker-list recompute. It reads the stored is_blocked
// column (SELECT id, is_blocked FROM {issues,wisps} WHERE id IN (...)), batched
// at queryBatchSize, so it reflects inherited/ancestor blockedness (a child of a
// blocked parent has is_blocked=1 with no direct blocking edge of its own) with
// no graph walk. ids missing from both tables are absent from the map (callers
// treat absent as not-blocked). On a cross-table id collision the ISSUES row wins,
// matching IsBlockedInTx exactly: the single read scans issues→wisps and breaks on
// the first table that has the id, so the batch keeps the first-seen (issues) value
// and skips any later (wisps) duplicate. The two reads share the is_blocked field,
// so they must resolve a collision identically — this is a data anomaly, but the
// single and batch reads would otherwise disagree on the same stored flag.
func IsBlockedBatchInTx(ctx context.Context, tx DBTX, ids []string) (map[string]bool, error) {
blocked := make(map[string]bool, len(ids))
if len(ids) == 0 {
return blocked, nil
}
// De-dup by id across the two tables, keeping the first-seen (issues) value so
// a cross-table collision resolves ISSUES-win, exactly as IsBlockedInTx does
// (see above). The wisps table is optional, so a missing one is skipped —
// same two-table read shape as GetDependentRecordsForIssuesInTx.
seen := make(map[string]bool, len(ids))
for _, table := range []string{"issues", "wisps"} {
if err := readIsBlockedIntoFromTable(ctx, tx, table, ids, seen, blocked); err != nil {
if optionalBlockedTable(table) && isTableNotExistError(err) {
continue
}
return nil, err
}
}
return blocked, nil
}
//nolint:gosec // G201: table is a hardcoded "issues" or "wisps"; placeholders are ? only.
func readIsBlockedIntoFromTable(ctx context.Context, tx DBTX, table string, ids []string, seen, blocked map[string]bool) error {
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
end = len(ids)
}
placeholders, args := buildSQLInClause(ids[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(
"SELECT id, is_blocked FROM %s WHERE id IN (%s)", table, placeholders), args...)
if err != nil {
return fmt.Errorf("read is_blocked from %s: %w", table, err)
}
for rows.Next() {
var id string
var b int
if err := rows.Scan(&id, &b); err != nil {
_ = rows.Close()
return fmt.Errorf("scan is_blocked from %s: %w", table, err)
}
// Keep the first-seen (issues) value and skip any later (wisps)
// duplicate, so the batch is_blocked matches per-row IsBlocked.
if seen[id] {
continue
}
seen[id] = true
blocked[id] = b != 0
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("is_blocked rows from %s: %w", table, err)
}
}
return nil
}
// scanDependencyRow scans a single dependency row from a *sql.Rows.
func scanDependencyRow(rows *sql.Rows) (*types.Dependency, error) {
var dep types.Dependency
var createdAt sql.NullTime
var metadata, threadID sql.NullString
if err := rows.Scan(&dep.IssueID, &dep.DependsOnID, &dep.Type, &createdAt, &dep.CreatedBy, &metadata, &threadID); err != nil {
return nil, fmt.Errorf("scan dependency: %w", err)
}
if createdAt.Valid {
dep.CreatedAt = createdAt.Time
}
if metadata.Valid {
dep.Metadata = metadata.String
}
if threadID.Valid {
dep.ThreadID = threadID.String
}
return &dep, nil
}