de92557f46
* chore(backend): add go.uber.org/goleak test dependency Promotes the previously-indirect goleak v1.3.0 to a direct require so per-package TestMain calls can use it for goroutine leak detection. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(backend/lifecycle): drain workspace-stream backoff on Manager.Stop StreamManager.connectWorkspaceStream slept the full retry backoff in time.Sleep between failed attempts. SessionTraceContext is deliberately uncancellable (it carries a long-lived span), so if the Manager began shutting down mid-backoff the goroutine outlived Stop() and stranded until the timer fired. Thread Manager.stopCh through the StreamManager constructor and add a sleepOrStop helper that selects between the timer and the shutdown signal. ReconnectAll's startup sleep gets the same treatment. TestConnectWorkspaceStream_BackoffDrainsOnStop closes stopCh while a failing-port retry is in flight and asserts the goroutine exits within 500ms — fails without the fix, passes with it. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(backend/github): close sqlite DB in newTestStore to drain connectionOpener newTestStore opened *sqlx.DB via db.OpenSQLite but never registered a Close on cleanup, leaving database/sql.(*DB).connectionOpener goroutines parked across test boundaries. Add t.Cleanup(sqlxDB.Close); goleak's per-package TestMain (added in a follow-up commit) is the regression check. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(backend): wire goleak.VerifyTestMain across leak-prone packages Per-package TestMain in: - internal/integrations/healthpoll (auth-health ticker loop) - internal/jira, internal/linear (auth + issue-watch loops) - internal/github (PR / review / issue poller loops) - internal/gateway/websocket (per-conn pumps + terminal bridge) - internal/orchestrator (detached event-handler goroutines) - internal/agent/runtime/lifecycle (remote-status loop, StreamManager helpers; ignores OTel batch-span processor and OTLP retry helper — initialised early in the process and only exit on shutdown) - internal/agentctl/server/process (extends the existing fixture-binary TestMain so subprocess + PTY pump leaks surface there) Catches regressions where a Start/Stop forgets to drain on WaitGroup, or a detached goroutine forgets to honour ctx/stopCh cancellation. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * docs(backend): document goroutine ownership and leak-testing convention New "Goroutine ownership and leak testing" subsection under Conventions in apps/backend/AGENTS.md (CLAUDE.md symlinks to it). Covers: - Lifecycle: Start/Stop + WaitGroup, both idempotent. - Cancellation: every long wait selects on ctx.Done() or stopCh; never time.Sleep in retry loops (reference lifecycle.StreamManager.sleepOrStop). - Detached helpers: must respect the owning type's shutdown signal. - Leak testing: new packages spawning goroutines add goleak.VerifyTestMain; IgnoreTopFunction is reserved for genuine third-party background goroutines and must carry a comment explaining the suppression. Lists the packages currently instrumented so future contributors can mirror the pattern. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore(backend/lifecycle): drop dead OTel IgnoreTopFunction from goleak setup Review follow-up. agentctl/tracing.initTracing only spawns the OTel SDK provider when OTEL_EXPORTER_OTLP_ENDPOINT is set; in tests it isn't, so the noop provider is used and no batchSpanProcessor / OTLP retry goroutines ever exist. The IgnoreTopFunction calls were dead suppressions and the comment ("called by NewManager via the agent execution trace path") misdescribed when the SDK init runs. Drop the ignores and correct AGENTS.md to stop citing OTel as the canonical suppression example. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(backend/lifecycle): drop unneeded 50ms sleep from backoff-drain test QA follow-up. The 50ms time.Sleep before close(stopCh) in TestConnectWorkspaceStream_BackoffDrainsOnStop was meant to ensure the goroutine had entered the backoff sleep before shutdown — but that ordering doesn't affect the test outcome: - With the fix: sleepOrStop sees stopCh already closed and returns false on the first call, so the goroutine drains within one attempt regardless of when stopCh closes. - Without the fix: time.Sleep ignores stopCh, the goroutine burns its full 5-retry backoff (~31s), and the 500ms timeout fires. Removing the sleep makes the test ~12x faster (15ms vs 200ms total) and removes the only time.Sleep-based wait from the new tests, aligning with the codebase's "channel-based sync, no sleep waits" convention. Verified by 50x -race and 5x revert-and-fail. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(backend/github): drain HTTP idle conns before goleak inspects CI surfaced a leaked goroutine on the Linux runner that local runs missed: a single net/http.(*http2ClientConn).readLoop parked on a TLS read, created by net/http.(*http2Transport).newClientConn. The package's anonymousHTTPClient and PATClient's rewriteTransport both route through http.DefaultTransport's connection pool, so an idle HTTP/2 connection in that pool keeps the reader goroutine alive past m.Run() — invisible on sandboxed local runs where outbound network is blocked. Replace github/goleak_test.go's plain VerifyTestMain with an expanded TestMain that drains http.DefaultTransport's idle connections before goleak.Find runs. This is the stdlib's documented teardown for the connection pool, keeps the assertion strict (no IgnoreTopFunction), and doesn't leak into production code. Other in-scope packages don't need this — they don't use http.DefaultTransport for HTTPS calls. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(backend): drain ws.Done on stopCh; always run goleak in github tests - lifecycle/streams.go: replace bare <-ws.Done() with a select that also watches sm.stopCh so a connected goroutine drains when Manager.Stop() fires before the remote WS closes. A nil stopCh (isolated tests) blocks the nil channel case forever, degrading to plain <-ws.Done() semantics. - github/goleak_test.go: remove the exitCode==0 guard around goleak.Find so leaks are always surfaced even when a test has already failed; original exit code is preserved (leaks only promote it to 1). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(backend/lifecycle): close ws on stopCh so agentctl WS loops drain cubic flagged that the previous shutdown path observed stopCh but never called ws.Close(), so the underlying WebSocket and its agentctl-side read/write goroutines (workspace_stream.go:67-68) kept running past Manager.Stop(). ClearWorkspaceStream only removes the WS from the execution's tracking — it doesn't close the connection. Call ws.Close() on the stopCh path. ws.Close is idempotent via closeOnce, so it's safe even if the remote end already closed. TestConnectWorkspaceStream_ClosesWSOnStop connects to a mock workspace server that stays open until the client closes its side; after stopCh fires, the test asserts ws.Done() closes within 500ms — which is only true if connectWorkspaceStream invoked ws.Close(). Verified to fail without the fix and pass with it. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(backend/lifecycle): use channel sync instead of sleep polling in ws-close test cubic flagged that TestConnectWorkspaceStream_ClosesWSOnStop polled execution.GetWorkspaceStream() in a 500ms sleep loop, which is brittle under CI load. connectWorkspaceStream already accepts a ready channel that closes after SetWorkspaceStream — pass it in and wait on the channel instead. synctest isn't viable here (real network I/O), so channel-based sync is the testing convention per AGENTS.md. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
335 lines
11 KiB
Go
335 lines
11 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/kandev/kandev/internal/db"
|
|
)
|
|
|
|
func newTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
tmp := t.TempDir()
|
|
dbPath := filepath.Join(tmp, "github.db")
|
|
dbConn, err := db.OpenSQLite(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("open db: %v", err)
|
|
}
|
|
sqlxDB := sqlx.NewDb(dbConn, "sqlite3")
|
|
t.Cleanup(func() { _ = sqlxDB.Close() })
|
|
if _, err := sqlxDB.Exec(`CREATE TABLE tasks (id TEXT PRIMARY KEY, archived_at DATETIME)`); err != nil {
|
|
t.Fatalf("create tasks table: %v", err)
|
|
}
|
|
store, err := NewStore(sqlxDB, sqlxDB)
|
|
if err != nil {
|
|
t.Fatalf("new store: %v", err)
|
|
}
|
|
return store
|
|
}
|
|
|
|
func TestTaskPR_PerRepoStorage(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
now := time.Now().UTC()
|
|
prFront := &TaskPR{
|
|
TaskID: "task-1", RepositoryID: "repo-front",
|
|
Owner: "kdlbs", Repo: "kandev", PRNumber: 100,
|
|
PRURL: "https://github.com/kdlbs/kandev/pull/100",
|
|
PRTitle: "frontend changes", HeadBranch: "feat/x", BaseBranch: "main",
|
|
State: "open", CreatedAt: now,
|
|
}
|
|
prBack := &TaskPR{
|
|
TaskID: "task-1", RepositoryID: "repo-back",
|
|
Owner: "kdlbs", Repo: "kandev-backend", PRNumber: 200,
|
|
PRURL: "https://github.com/kdlbs/kandev-backend/pull/200",
|
|
PRTitle: "backend changes", HeadBranch: "feat/x", BaseBranch: "main",
|
|
State: "open", CreatedAt: now,
|
|
}
|
|
|
|
if err := store.CreateTaskPR(ctx, prFront); err != nil {
|
|
t.Fatalf("create front PR: %v", err)
|
|
}
|
|
if err := store.CreateTaskPR(ctx, prBack); err != nil {
|
|
t.Fatalf("create back PR: %v", err)
|
|
}
|
|
|
|
gotFront, err := store.GetTaskPRByRepository(ctx, "task-1", "repo-front")
|
|
if err != nil {
|
|
t.Fatalf("get front: %v", err)
|
|
}
|
|
if gotFront == nil || gotFront.PRNumber != 100 {
|
|
t.Errorf("expected front PR #100, got %+v", gotFront)
|
|
}
|
|
|
|
gotBack, err := store.GetTaskPRByRepository(ctx, "task-1", "repo-back")
|
|
if err != nil {
|
|
t.Fatalf("get back: %v", err)
|
|
}
|
|
if gotBack == nil || gotBack.PRNumber != 200 {
|
|
t.Errorf("expected back PR #200, got %+v", gotBack)
|
|
}
|
|
|
|
all, err := store.ListTaskPRsByTask(ctx, "task-1")
|
|
if err != nil {
|
|
t.Fatalf("list: %v", err)
|
|
}
|
|
if len(all) != 2 {
|
|
t.Errorf("expected 2 PRs for task, got %d", len(all))
|
|
}
|
|
}
|
|
|
|
func TestTaskPR_ReplaceTaskPR_ScopedByRepository(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
now := time.Now().UTC()
|
|
if err := store.CreateTaskPR(ctx, &TaskPR{
|
|
TaskID: "task-2", RepositoryID: "repo-a",
|
|
Owner: "o", Repo: "r", PRNumber: 1, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create A: %v", err)
|
|
}
|
|
if err := store.CreateTaskPR(ctx, &TaskPR{
|
|
TaskID: "task-2", RepositoryID: "repo-b",
|
|
Owner: "o", Repo: "r2", PRNumber: 2, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create B: %v", err)
|
|
}
|
|
|
|
// Replace only repo-a's PR — repo-b must survive.
|
|
if err := store.ReplaceTaskPR(ctx, &TaskPR{
|
|
TaskID: "task-2", RepositoryID: "repo-a",
|
|
Owner: "o", Repo: "r", PRNumber: 99, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("replace A: %v", err)
|
|
}
|
|
|
|
all, _ := store.ListTaskPRsByTask(ctx, "task-2")
|
|
if len(all) != 2 {
|
|
t.Fatalf("expected 2 PRs after scoped replace, got %d", len(all))
|
|
}
|
|
bySpec := map[string]int{}
|
|
for _, p := range all {
|
|
bySpec[p.RepositoryID] = p.PRNumber
|
|
}
|
|
if bySpec["repo-a"] != 99 {
|
|
t.Errorf("expected repo-a updated to 99, got %d", bySpec["repo-a"])
|
|
}
|
|
if bySpec["repo-b"] != 2 {
|
|
t.Errorf("expected repo-b unchanged at 2, got %d", bySpec["repo-b"])
|
|
}
|
|
}
|
|
|
|
// Regression: a task that pre-dated the multi-repo schema kept a row with
|
|
// repository_id=”, then a later sync inserted a NEW row under the resolved
|
|
// repository_id — leaving two rows for the same PR and "+2" badges on the
|
|
// kanban card. backfillTaskPRsRepositoryID must dedup the legacy row.
|
|
func TestBackfillTaskPRsRepositoryID_DedupsLegacyEmptyRow(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
|
|
// Insert a legacy row (repository_id = '') and a per-repo row for the
|
|
// same (task, owner, repo, pr_number). The unique index is
|
|
// (task_id, repository_id, pr_number), so both rows can coexist.
|
|
if err := store.CreateTaskPR(ctx, &TaskPR{
|
|
ID: "legacy", TaskID: "task-x", RepositoryID: "",
|
|
Owner: "kdlbs", Repo: "kandev", PRNumber: 767, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create legacy: %v", err)
|
|
}
|
|
if err := store.CreateTaskPR(ctx, &TaskPR{
|
|
ID: "scoped", TaskID: "task-x", RepositoryID: "repo-1",
|
|
Owner: "kdlbs", Repo: "kandev", PRNumber: 767, CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create scoped: %v", err)
|
|
}
|
|
|
|
if err := store.backfillTaskPRsRepositoryID(); err != nil {
|
|
t.Fatalf("backfill: %v", err)
|
|
}
|
|
|
|
all, _ := store.ListTaskPRsByTask(ctx, "task-x")
|
|
if len(all) != 1 {
|
|
t.Fatalf("expected 1 PR after dedup, got %d", len(all))
|
|
}
|
|
if all[0].RepositoryID != "repo-1" {
|
|
t.Errorf("expected scoped row to win, got repository_id=%q", all[0].RepositoryID)
|
|
}
|
|
|
|
// Idempotent: second pass is a no-op.
|
|
if err := store.backfillTaskPRsRepositoryID(); err != nil {
|
|
t.Fatalf("second backfill: %v", err)
|
|
}
|
|
all2, _ := store.ListTaskPRsByTask(ctx, "task-x")
|
|
if len(all2) != 1 {
|
|
t.Errorf("idempotent expected 1 PR, got %d", len(all2))
|
|
}
|
|
}
|
|
|
|
// When task_repositories does not exist (e.g. github-store unit tests that
|
|
// init only this package's schema), backfill must skip the cross-package
|
|
// UPDATE rather than erroring out.
|
|
func TestBackfillTaskPRsRepositoryID_SkipsWhenTaskRepositoriesAbsent(t *testing.T) {
|
|
store := newTestStore(t)
|
|
if err := store.backfillTaskPRsRepositoryID(); err != nil {
|
|
t.Fatalf("backfill on store without task_repositories: %v", err)
|
|
}
|
|
}
|
|
|
|
// Regression: a single-repo task created before the multi-repo migration
|
|
// kept its github_pr_watches row with repository_id=”. The orchestrator's
|
|
// reconciler then created a SECOND watch row with the resolved
|
|
// repository_id (because (session_id, ”) != (session_id, 'X') in its dedup
|
|
// set). Both watches polled independently, so when the user opened a PR
|
|
// each one called AssociatePRWithTask with its own RepositoryID — leaving
|
|
// two github_task_prs rows for the same PR and a "+2" badge on the kanban
|
|
// card. backfillPRWatchesRepositoryID must dedup the legacy row so the
|
|
// reconciler's existence-check finds it under the resolved repository_id.
|
|
func TestBackfillPRWatchesRepositoryID_DedupsLegacyEmptyRow(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
|
|
// Legacy watch row (repository_id='') and the per-repo row that the
|
|
// reconciler inserted post-migration. Both reference the same session.
|
|
if err := store.CreatePRWatch(ctx, &PRWatch{
|
|
ID: "legacy-w", SessionID: "sess-1", TaskID: "task-w", RepositoryID: "",
|
|
Owner: "kdlbs", Repo: "kandev", PRNumber: 0, Branch: "feat/x",
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create legacy watch: %v", err)
|
|
}
|
|
if err := store.CreatePRWatch(ctx, &PRWatch{
|
|
ID: "scoped-w", SessionID: "sess-1", TaskID: "task-w", RepositoryID: "repo-1",
|
|
Owner: "kdlbs", Repo: "kandev", PRNumber: 0, Branch: "feat/x",
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create scoped watch: %v", err)
|
|
}
|
|
|
|
if err := store.backfillPRWatchesRepositoryID(); err != nil {
|
|
t.Fatalf("backfill: %v", err)
|
|
}
|
|
|
|
all, err := store.ListPRWatchesBySession(ctx, "sess-1")
|
|
if err != nil {
|
|
t.Fatalf("list: %v", err)
|
|
}
|
|
if len(all) != 1 {
|
|
t.Fatalf("expected 1 watch after dedup, got %d", len(all))
|
|
}
|
|
if all[0].RepositoryID != "repo-1" {
|
|
t.Errorf("expected scoped row to win, got repository_id=%q", all[0].RepositoryID)
|
|
}
|
|
|
|
// Idempotent: second pass is a no-op.
|
|
if err := store.backfillPRWatchesRepositoryID(); err != nil {
|
|
t.Fatalf("second backfill: %v", err)
|
|
}
|
|
all2, _ := store.ListPRWatchesBySession(ctx, "sess-1")
|
|
if len(all2) != 1 {
|
|
t.Errorf("idempotent expected 1 watch, got %d", len(all2))
|
|
}
|
|
}
|
|
|
|
// When task_repositories does not exist, the watch backfill must skip the
|
|
// cross-package UPDATE rather than erroring out — same contract as the
|
|
// task-PR backfill.
|
|
func TestBackfillPRWatchesRepositoryID_SkipsWhenTaskRepositoriesAbsent(t *testing.T) {
|
|
store := newTestStore(t)
|
|
if err := store.backfillPRWatchesRepositoryID(); err != nil {
|
|
t.Fatalf("backfill on store without task_repositories: %v", err)
|
|
}
|
|
}
|
|
|
|
// The orphan case — a single-repo task whose pre-migration watch never had a
|
|
// per-repo counterpart. The dedup pass leaves it alone; the UPDATE has to
|
|
// stamp its repository_id from task_repositories. This is what stops the
|
|
// reconciler from inserting a duplicate watch on the next poll tick.
|
|
func TestBackfillPRWatchesRepositoryID_BackfillsOrphanRow(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
if _, err := store.db.Exec(`CREATE TABLE task_repositories (
|
|
id TEXT PRIMARY KEY, task_id TEXT NOT NULL,
|
|
repository_id TEXT NOT NULL, position INTEGER DEFAULT 0
|
|
)`); err != nil {
|
|
t.Fatalf("create task_repositories: %v", err)
|
|
}
|
|
if _, err := store.db.Exec(
|
|
`INSERT INTO task_repositories VALUES ('r1','task-z','repo-abc',0)`,
|
|
); err != nil {
|
|
t.Fatalf("seed task_repositories: %v", err)
|
|
}
|
|
|
|
now := time.Now().UTC()
|
|
if err := store.CreatePRWatch(ctx, &PRWatch{
|
|
ID: "legacy-orphan", SessionID: "sess-2", TaskID: "task-z", RepositoryID: "",
|
|
Owner: "o", Repo: "r", PRNumber: 0, Branch: "feat/y",
|
|
CreatedAt: now, UpdatedAt: now,
|
|
}); err != nil {
|
|
t.Fatalf("create legacy: %v", err)
|
|
}
|
|
|
|
if err := store.backfillPRWatchesRepositoryID(); err != nil {
|
|
t.Fatalf("backfill: %v", err)
|
|
}
|
|
|
|
all, _ := store.ListPRWatchesBySession(ctx, "sess-2")
|
|
if len(all) != 1 {
|
|
t.Fatalf("expected 1 watch, got %d", len(all))
|
|
}
|
|
if all[0].RepositoryID != "repo-abc" {
|
|
t.Errorf("expected backfilled repository_id='repo-abc', got %q", all[0].RepositoryID)
|
|
}
|
|
}
|
|
|
|
// Covers the UPDATE path: a legacy row with repository_id=” and NO per-repo
|
|
// counterpart should get its repository_id stamped from task_repositories,
|
|
// not deleted. This is the most common case in real upgrades — a task that
|
|
// has only ever had one PR (so no duplicate to dedup) but pre-dates the
|
|
// per-repo schema.
|
|
func TestBackfillTaskPRsRepositoryID_BackfillsOrphanRow(t *testing.T) {
|
|
store := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
if _, err := store.db.Exec(`CREATE TABLE task_repositories (
|
|
id TEXT PRIMARY KEY, task_id TEXT NOT NULL,
|
|
repository_id TEXT NOT NULL, position INTEGER DEFAULT 0
|
|
)`); err != nil {
|
|
t.Fatalf("create task_repositories: %v", err)
|
|
}
|
|
if _, err := store.db.Exec(
|
|
`INSERT INTO task_repositories VALUES ('r1','task-y','repo-abc',0)`,
|
|
); err != nil {
|
|
t.Fatalf("seed task_repositories: %v", err)
|
|
}
|
|
|
|
// Orphan legacy row: no per-repo counterpart, so the dedup DELETE leaves
|
|
// it alone and the UPDATE has to do the healing.
|
|
if err := store.CreateTaskPR(ctx, &TaskPR{
|
|
ID: "legacy2", TaskID: "task-y", RepositoryID: "",
|
|
Owner: "o", Repo: "r", PRNumber: 1, CreatedAt: time.Now().UTC(),
|
|
}); err != nil {
|
|
t.Fatalf("create legacy: %v", err)
|
|
}
|
|
|
|
if err := store.backfillTaskPRsRepositoryID(); err != nil {
|
|
t.Fatalf("backfill: %v", err)
|
|
}
|
|
|
|
all, _ := store.ListTaskPRsByTask(ctx, "task-y")
|
|
if len(all) != 1 {
|
|
t.Fatalf("expected 1 row, got %d", len(all))
|
|
}
|
|
if all[0].RepositoryID != "repo-abc" {
|
|
t.Errorf("expected backfilled repository_id='repo-abc', got %q", all[0].RepositoryID)
|
|
}
|
|
}
|