{"record":{"id":"4969ac79edb6c680","repo":"gastownhall/beads","slug":"scanning-open-beads-for-references-w","errorCode":null,"errorMessage":"scanning open beads for references: %w","messagePattern":"scanning open beads for references: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/issueops/sweep.go","lineNumber":100,"sourceCode":"// status would under-protect and delete a bead a live bead still cites\n// (issueops.SweepRequest.ProtectReferenced).\nfunc sweepReferencedInTx(ctx context.Context, tx *sql.Tx, candidates []*types.Issue) (map[string]bool, error) {\n\tif len(candidates) == 0 {\n\t\treturn nil, nil\n\t}\n\tcandidateIDs := make(map[string]bool, len(candidates))\n\tfor _, issue := range candidates {\n\t\tcandidateIDs[issue.ID] = true\n\t}\n\tmatcher := workapi.NewCandidateIDMatcher(candidateIDs)\n\n\tcustom, err := ResolveCustomStatusesDetailedInTx(ctx, tx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"reading custom statuses for reference scan: %w\", err)\n\t}\n\tnotDone, err := SearchIssuesInTx(ctx, tx, \"\", workapi.BuildSweepReferenceScanFilter(custom))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"scanning open beads for references: %w\", err)\n\t}\n\n\tnotDoneIDs := make([]string, 0, len(notDone))\n\tfor _, issue := range notDone {\n\t\tif issue != nil {\n\t\t\tnotDoneIDs = append(notDoneIDs, issue.ID)\n\t\t}\n\t}\n\tcomments, err := GetCommentsForIssuesInTx(ctx, tx, notDoneIDs)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"scanning open beads for references: %w\", err)\n\t}\n\n\treferenced := make(map[string]bool)\n\tfor _, issue := range notDone {\n\t\tif issue == nil {\n\t\t\tcontinue\n\t\t}","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/issueops/sweep.go#L82-L118","documentation":"After resolving custom statuses, sweepReferencedInTx searches all not-done issues (per the reference-scan filter) via SearchIssuesInTx to find beads that might cite the sweep candidates; a failure is wrapped as 'scanning open beads for references'. Note this same message also wraps the comment-fetch failure (error 3808), so the inner error must be read to tell which leg failed.","triggerScenarios":"SearchIssuesInTx fails while building the not-done set: missing issues table/columns, invalid SQL from BuildSweepReferenceScanFilter with unusual custom statuses, dropped connection, or cancelled context during a ProtectReferenced sweep.","commonSituations":"Sweeping a large workspace where the open-beads scan times out; Dolt server restart mid-sweep; custom statuses producing an unexpected filter clause; schema drift after an upgrade.","solutions":["Read the wrapped inner error to identify the failing leg (this search vs. the comments fetch).","Retry the sweep with a fresh transaction — both legs are read-only up to this point.","Narrow the sweep (fewer candidates, tighter closed-before) to reduce scan cost if timing out.","Verify schema/migrations are current, especially anything touched by the reference-scan filter."],"exampleFix":"// before\nresult, err := SweepInTx(ctx, tx, req) // reference scan fails mid-run\n// after\nctx, cancel := context.WithTimeout(ctx, 5*time.Minute)\ndefer cancel()\nresult, err = SweepInTx(ctx, tx, req) // generous timeout for large scans","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"result, err := issueops.SweepInTx(ctx, tx, req)\nif err != nil && strings.Contains(err.Error(), \"scanning open beads for references\") {\n    // read-only leg failed; reopen tx, back off, retry\n    return fmt.Errorf(\"reference scan failed, sweep aborted safely: %w\", err)\n}","preventionTips":["Use a generous context timeout for large workspaces","Retry with fresh transactions — the scan is read-only","Check inner error text to distinguish search vs comment failures","Dry-run first to size the scan cost"],"tags":["go","sql","sweep","reference-scan"],"backgroundTag":"sql-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}