caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

157 lines
6.0 KiB
Go

package dolt
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
// depTargetExpr resolves a dependency row's target id from the split physical
// columns instead of the STORED generated `depends_on_id` column. Both
// `dependencies` and `wisp_dependencies` define depends_on_id as
// GENERATED ALWAYS AS (COALESCE(depends_on_issue_id, depends_on_wisp_id,
// depends_on_external)). Count queries must filter on the base columns: inside
// a count(*) (which projects no real columns) the pure-Go GMS analyzer can
// prune the base columns the generated column derives from and then fail with
// "column depends_on_id could not be found in any table in scope". The slice
// path projects real columns, so it can use depends_on_id directly. This
// matches issueops.DepTargetExpr on main.
const depTargetExpr = "COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external)"
// CountIssues returns the number of issues matching query and filter.
// Filter.Limit and Filter.Offset are ignored; all other fields apply.
// Wisps-merge semantics follow SearchIssues: SkipWisps=true counts the
// durable issues table only, otherwise the wisps tier is merged in (GH#4387).
func (s *DoltStore) CountIssues(ctx context.Context, query string, filter types.IssueFilter) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
count, err := issueops.CountIssuesInTx(ctx, tx, query, filter)
if err != nil {
return err
}
n = int64(count)
return nil
})
return n, err
}
// CountIssuesByGroup returns per-group issue counts. groupBy is one of:
// status, priority, type, assignee, label.
func (s *DoltStore) CountIssuesByGroup(ctx context.Context, filter types.IssueFilter, groupBy string) (map[string]int, error) {
var result map[string]int
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.CountIssuesByGroupInTx(ctx, tx, filter, groupBy)
return err
})
return result, err
}
// CountDependents returns the number of issues that depend on issueID.
// Counts both dependency tables so the total matches GetDependentsWithMetadata:
// a dependent may be a permanent issue (edge in `dependencies`) or a wisp
// (edge in `wisp_dependencies`, routed there by WispTableRouting on the source).
//
// Both tables' targets are resolved via depTargetExpr (the split physical
// columns) rather than the STORED generated depends_on_id, which a count(*)
// can fail to resolve under the pure-Go GMS analyzer.
func (s *DoltStore) CountDependents(ctx context.Context, issueID string) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var perm, wisp int64
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM dependencies WHERE `+depTargetExpr+` = ?`, issueID).Scan(&perm); err != nil {
return err
}
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM wisp_dependencies WHERE `+depTargetExpr+` = ?`, issueID).Scan(&wisp); err != nil {
return err
}
n = perm + wisp
return nil
})
return n, err
}
// CountDependencies returns the number of issues that issueID depends on.
// Counts both dependency tables so the total matches GetDependenciesWithMetadata:
// a wisp's outgoing edges live in `wisp_dependencies`, a permanent issue's in
// `dependencies`. Counted as two separate queries summed in Go (see
// CountDependents for why a single combined query is avoided).
func (s *DoltStore) CountDependencies(ctx context.Context, issueID string) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var perm, wisp int64
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM dependencies WHERE issue_id = ?`, issueID).Scan(&perm); err != nil {
return err
}
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM wisp_dependencies WHERE issue_id = ?`, issueID).Scan(&wisp); err != nil {
return err
}
n = perm + wisp
return nil
})
return n, err
}
// CountIssueComments returns the number of comments on an issue.
func (s *DoltStore) CountIssueComments(ctx context.Context, issueID string) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
return tx.QueryRowContext(ctx,
`SELECT count(*) FROM comments WHERE issue_id = ?`, issueID).Scan(&n)
})
return n, err
}
// CountEvents returns the number of audit events for an issue, capped at limit
// (or unbounded if limit == 0).
func (s *DoltStore) CountEvents(ctx context.Context, issueID string, limit int) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
return tx.QueryRowContext(ctx,
`SELECT count(*) FROM events WHERE issue_id = ?`, issueID).Scan(&n)
})
if err != nil {
return 0, err
}
if limit > 0 && n > int64(limit) {
n = int64(limit)
}
return n, nil
}
// CountDependentsByStatus returns the number of issues that depend on issueID
// and are in the given status. Counts both dependency tables, joining each to
// its home issue table (dependencies→issues, wisp_dependencies→wisps), so wisp
// dependents are included the same way GetDependentsWithMetadata includes them.
// Counted as two separate queries summed in Go (see CountDependents for why a
// single combined query is avoided).
func (s *DoltStore) CountDependentsByStatus(ctx context.Context, issueID string, status types.Status) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var perm, wisp int64
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM dependencies d
JOIN issues i ON i.id = d.issue_id
WHERE COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) = ? AND i.status = ?`,
issueID, string(status)).Scan(&perm); err != nil {
return err
}
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM wisp_dependencies d
JOIN wisps w ON w.id = d.issue_id
WHERE COALESCE(d.depends_on_issue_id, d.depends_on_wisp_id, d.depends_on_external) = ? AND w.status = ?`,
issueID, string(status)).Scan(&wisp); err != nil {
return err
}
n = perm + wisp
return nil
})
return n, err
}