{"record":{"id":"c4d00afebec1f111","repo":"gastownhall/beads","slug":"check-issue-existence-w","errorCode":null,"errorMessage":"check issue existence: %w","messagePattern":"check issue existence: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/issueops/commenter.go","lineNumber":77,"sourceCode":"}\n\n// resolveCommentPlaneInTx names the comment table the anchor's thread lives in,\n// refusing an id that names neither an issue nor a wisp.\n//\n// The existence probe is here rather than left to the insert's own so the\n// refusal is TYPED: AddIssueCommentInTx reports a missing anchor as prose, and\n// a caller of this role classifies with errors.Is. It resolves the plane in\n// the same transaction the insert runs in, so a comment cannot land on a row\n// an earlier read saw and this one did not.\n//\n//nolint:gosec // G201: issueTable comes from WispTableRouting (\"issues\" or \"wisps\")\nfunc resolveCommentPlaneInTx(ctx context.Context, tx *sql.Tx, issueID string) (string, error) {\n\tisWisp := IsActiveWispInTx(ctx, tx, issueID)\n\tissueTable, _, _, _ := WispTableRouting(isWisp)\n\tvar exists bool\n\tif err := tx.QueryRowContext(ctx,\n\t\tfmt.Sprintf(`SELECT EXISTS(SELECT 1 FROM %s WHERE id = ?)`, issueTable), issueID).Scan(&exists); err != nil {\n\t\treturn \"\", fmt.Errorf(\"check issue existence: %w\", err)\n\t}\n\tif !exists {\n\t\treturn \"\", fmt.Errorf(\"%w: issue %s\", storage.ErrNotFound, issueID)\n\t}\n\tif isWisp {\n\t\treturn \"wisp_comments\", nil\n\t}\n\treturn \"comments\", nil\n}\n","sourceCodeStart":59,"sourceCodeEnd":87,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/issueops/commenter.go#L59-L87","documentation":"resolveCommentPlaneInTx fails while probing whether the anchor issue exists: the EXISTS SELECT against the issues/wisps table returned a driver error (connection loss, context cancellation, lock timeout, schema problem). The error is wrapped as \"check issue existence: %w\" so the underlying database/sql error remains inspectable. This is an infrastructure failure, not a 'not found' — a missing issue yields ErrNotFound instead (error 3434).","triggerScenarios":"Calling AddComment/ExecuteAddComment when the Dolt/SQL connection has dropped mid-transaction; context deadline exceeded while the transaction is contended; the underlying table missing or locked by another writer.","commonSituations":"Long-running scripts hitting a server-side connection idle timeout; concurrent bd processes deadlocking on the issue row; deploying a schema migration while comments are being written; network blips against a remote Dolt server.","solutions":["Inspect the wrapped error with errors.Is/As (driver.ErrBadConn, context.DeadlineExceeded) to identify the infrastructure cause.","Retry the whole AddComment operation (a fresh transaction) on transient errors like ErrBadConn; do not retry on context cancellation.","Check database connectivity/server health and that the beads database schema is fully migrated."],"exampleFix":"// before\nerr := store.AddComment(ctx, req) // opaque driver failure\n\n// after\nif err := store.AddComment(ctx, req); err != nil {\n\tif errors.Is(err, context.DeadlineExceeded) {\n\t\tctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)\n\t\tdefer cancel()\n\t\tcontinue // retry with fresh context/tx\n\t}\n\treturn err\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"var dbErr *driverError\nif err := store.AddComment(ctx, req); err != nil {\n\tif errors.Is(err, driver.ErrBadConn) || errors.Is(err, context.DeadlineExceeded) {\n\t\t// recreate tx/connection and retry the whole operation\n\t} else if errors.As(err, &dbErr) {\n\t\tlog.Errorf(\"db failure during comment: %v\", err)\n\t}\n\treturn err\n}","preventionTips":["Set generous but finite context timeouts on comment operations.","Use connection pooling with health checks so dead connections are replaced.","Avoid holding long transactions that contend with the existence probe.","Monitor DB connectivity; alert on repeated 'check issue existence' failures."],"tags":["database","sql","transient"],"backgroundTag":"database-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}