This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/aitimemachine/beads/internal/storage/dolt/dependencies_merge_test.go
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

172 lines
7.1 KiB
Go

package dolt
import (
"context"
"testing"
"github.com/steveyegge/beads/internal/storage/depid"
"github.com/steveyegge/beads/internal/types"
)
// TestAddDependencyUsesDeterministicID proves the production insert path derives
// the dependency primary key deterministically from the natural edge key
// (issue_id, target) rather than a random UUID — the heart of the #4259 fix.
func TestAddDependencyUsesDeterministicID(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
db := store.db
for _, id := range []string{"det-x", "det-y"} {
if _, err := db.ExecContext(ctx,
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES (?, ?, '', '', '', '', 'open', 2, 'task')",
id, id); err != nil {
t.Fatalf("seed issue %s: %v", id, err)
}
}
if err := store.AddDependency(ctx, &types.Dependency{IssueID: "det-x", DependsOnID: "det-y", Type: types.DepBlocks}, "alice"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
var gotID string
if err := db.QueryRowContext(ctx,
"SELECT id FROM dependencies WHERE issue_id = 'det-x' AND depends_on_issue_id = 'det-y'").Scan(&gotID); err != nil {
t.Fatalf("read edge id: %v", err)
}
if want := depid.New("det-x", "det-y"); gotID != want {
t.Errorf("AddDependency wrote id %q, want deterministic %q (a random UUID here is the #4259 bug)", gotID, want)
}
}
// TestDependencyDeterministicIDMergesAcrossBranches is the cross-clone
// merge-safety regression test for #4259. It merges two divergent branches that
// each create the same logical edge (issue_id, target), demonstrating both halves
// of why a deterministic id matters:
//
// - deterministic ids: both branches use the same primary key, so the same edge
// is one row. With identical edge content the merge is clean and yields
// exactly one dependency (no duplicate, no uk_dep_* collision).
// - random ids (negative control): the same edge under two different primary
// keys brings in two rows that violate uk_dep_issue_target, and the merge
// does not cleanly converge — exactly the unrecoverable `bd dolt pull` break
// this fix removes.
//
// The deterministic-id value used here is depid.New(...), the same value the
// production insert paths compute (see TestAddDependencyUsesDeterministicID).
func TestDependencyDeterministicIDMergesAcrossBranches(t *testing.T) {
t.Run("deterministic ids converge to one row", func(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
x, y := "conv-x", "conv-y"
id := depid.New(x, y)
// Identical edge content on both branches (same id, same audit columns):
// the byte-identical case, e.g. an edge synced before two clones migrated
// independently. It must merge cleanly to a single row.
insert := "INSERT INTO dependencies (id, issue_id, depends_on_issue_id, type, created_at, created_by) " +
"VALUES ('" + id + "', '" + x + "', '" + y + "', 'blocks', '2020-01-01 00:00:00', 'sync')"
mergeErr, rows := runTwoBranchEdgeMerge(t, ctx, store, x, y, insert, insert)
if mergeErr != nil {
t.Fatalf("merging identical deterministic-id edges should be clean, got: %v", mergeErr)
}
if rows != 1 {
t.Errorf("expected exactly 1 dependency row after merge, got %d", rows)
}
})
t.Run("random ids collide (negative control)", func(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
x, y := "rand-x", "rand-y"
// Same logical edge, two different (random) primary keys — the pre-fix
// behaviour. The merge must NOT cleanly converge: the two rows collide on
// uk_dep_issue_target.
insA := "INSERT INTO dependencies (id, issue_id, depends_on_issue_id, type, created_at, created_by) " +
"VALUES (UUID(), '" + x + "', '" + y + "', 'blocks', NOW(), 'a')"
insB := "INSERT INTO dependencies (id, issue_id, depends_on_issue_id, type, created_at, created_by) " +
"VALUES (UUID(), '" + x + "', '" + y + "', 'blocks', NOW(), 'b')"
mergeErr, rows := runTwoBranchEdgeMerge(t, ctx, store, x, y, insA, insB)
if mergeErr == nil && rows == 1 {
t.Error("expected divergent random ids to break the merge (uk_dep_issue_target collision), but it converged cleanly")
}
})
}
// runTwoBranchEdgeMerge seeds issues x and y on the shared ancestor, forks a peer
// branch from it, runs insA on the current branch and insB on the peer branch
// (each committed), then merges the peer back into the current branch. It returns
// the merge error (if any) and the resulting count of dependency rows for the
// edge x -> y on the current branch.
func runTwoBranchEdgeMerge(t *testing.T, ctx context.Context, store *DoltStore, x, y, insA, insB string) (error, int) {
t.Helper()
db := store.db
var currentBranch string
if err := db.QueryRowContext(ctx, "SELECT active_branch()").Scan(&currentBranch); err != nil {
t.Fatalf("get current branch: %v", err)
}
for _, id := range []string{x, y} {
if _, err := db.ExecContext(ctx,
"INSERT INTO issues (id, title, description, design, acceptance_criteria, notes, status, priority, issue_type) VALUES (?, ?, '', '', '', '', 'open', 2, 'task')",
id, id); err != nil {
t.Fatalf("seed issue %s: %v", id, err)
}
}
if _, err := db.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'seed issues')"); err != nil {
t.Fatalf("commit seed issues: %v", err)
}
peerBranch := currentBranch + "_peer"
if _, err := db.ExecContext(ctx, "CALL DOLT_BRANCH(?, 'HEAD')", peerBranch); err != nil {
t.Fatalf("create peer branch: %v", err)
}
t.Cleanup(func() {
db.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", currentBranch)
db.ExecContext(ctx, "CALL DOLT_BRANCH('-D', ?)", peerBranch)
})
// Current branch creates its edge.
if _, err := db.ExecContext(ctx, insA); err != nil {
t.Fatalf("insert edge on %s: %v", currentBranch, err)
}
if _, err := db.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'edge on current')"); err != nil {
t.Fatalf("commit edge on %s: %v", currentBranch, err)
}
// Peer branch independently creates the same logical edge.
if _, err := db.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", peerBranch); err != nil {
t.Fatalf("checkout peer branch: %v", err)
}
if _, err := db.ExecContext(ctx, insB); err != nil {
t.Fatalf("insert edge on peer: %v", err)
}
if _, err := db.ExecContext(ctx, "CALL DOLT_COMMIT('-Am', 'edge on peer')"); err != nil {
t.Fatalf("commit edge on peer: %v", err)
}
// Merge peer back into the current branch.
if _, err := db.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", currentBranch); err != nil {
t.Fatalf("checkout current branch: %v", err)
}
_, mergeErr := db.ExecContext(ctx, "CALL DOLT_MERGE(?)", peerBranch)
var rows int
if err := db.QueryRowContext(ctx,
"SELECT COUNT(*) FROM dependencies WHERE issue_id = ? AND depends_on_issue_id = ?", x, y).Scan(&rows); err != nil {
// A failed/aborted merge can leave the session mid-transaction; the row
// count is only meaningful for the clean path, so tolerate a read error
// here and let the caller judge on mergeErr.
return mergeErr, -1
}
return mergeErr, rows
}