perf(wiki-ingest): scale to 4w-doc KBs + generic task queue / dead-letter
The wiki ingest post-process pipeline OOM'd and ran for hours on KBs
with ~40k documents. The dominant tail was a per-batch ListAllPages
that pulled every page (multi-MB content blobs) into Go memory, plus a
24h-TTL Redis pending list whose data could be evicted before a long
serialized backlog drained. dedup did O(P × N) Jaccard scoring in Go
on every batch. Lint loaded the full graph. The index page kept the
entire wiki directory in its content column and rewrote the TOAST
chunk on every ingest. None of these survive at 4w docs.
This change reworks the write path end to end and pulls the durable-
queue + dead-letter primitives out of wiki and into shared
infrastructure that every asynq task type now benefits from.
- migration 000041: task_pending_ops + task_dead_letters tables, plus
three GIN indexes on wiki_pages (source_refs jsonb_path_ops,
source_refs text fallback, lower(title) trgm).
- TaskPendingOpsRepository + TaskDeadLetterRepository in
internal/application/repository/task_queue.go: cursor-paginated
list, atomic IncrFailCount via UPDATE…RETURNING, dedup-key scoped
delete that refuses empty keys so a buggy caller can't wipe a KB's
queue.
- internal/middleware/asynqdl: writes a task_dead_letters row when an
asynq task exhausts its retry budget. Payload-agnostic — a small
probe struct extracts TenantID + scope hints across every existing
payload type, so summary:generation / image:multimodal /
faq_import / kb:clone / etc. all dead-letter without per-handler
code. Best-effort: an insert failure never masks the underlying
task error. Installed in router/task.go before the langfuse mw so
it sees raw errors.
- Pending queue moved Redis → PG (no TTL, restart-durable). Redis
keeps just the active-batch lock and the delete tombstone.
- In-batch retry budget (wikiMaxFailRetries=5) tracked via
pendingRepo.IncrFailCount; over the cap the row is moved to
task_dead_letters with task_type=wiki:ingest, related_id=knowledge
id. Asynq retries (10) are handled by the new middleware.
- Removed the per-batch ListAllPages. WikiBatchContext now carries
lazy fetcher closures (SlugTitleMany, SummaryByKnowledgeID) with
mutex-protected caches; reduce reaches for titles / summaries on
demand instead of pre-loading the whole KB.
- getExistingPageSlugsForKnowledge now uses ListSlugsBySourceRef,
which the new GIN index on source_refs serves as a Bitmap Index
Scan instead of a sequential text LIKE.
- Dedup pre-filter uses pg_trgm via FindSimilarPages
(idx_wiki_pages_title_trgm). For each new entity/concept (and each
of its aliases) we ask the DB for the top-K trigram-similar
existing pages and union the results — bounded prompt size, no Go-
side O(P × M) loop. Small KBs (≤25 entities) bypass the pre-filter.
- cleanDeadLinks / injectCrossLinks are scoped to the batch's
affected slugs (a few dozen) instead of every page in the KB. Both
use the new lite ListBySlugs / ExistsSlugs repo methods so they
pull only slug + title + outlinks, not full content.
- Slug fuzzy resolve (slug_fuzzy.go): when the LLM emits
[[bad-slug|display]] the cleanup path now tries display-text
reverse lookup → hyphen/case normalized equality → char-bigram
Jaccard ≥ 0.8 before stripping. Recovers the common pinyin-word-
break drift case ("shang-hai-tower" vs "shanghai-tower") in place
instead of replacing the link with plain text.
- rebuildIndexPage uses ListByTypeRecent(200) for the first-time
intro and drops the full DocumentSummaries blob from the
incremental update prompt, so its context stays bounded regardless
of KB size.
- Concurrency tunables surfaced in WikiConfig: IngestBatchSize /
IngestMapParallel / IngestReduceParallel, with sensible defaults
via OrDefault helpers. scheduleFollowUp drops to ProcessIn(0) so
follow-ups don't waste asynq retry slots bouncing on the active
lock.
- RunLint walks pages via the new ListPagesCursor in 200-page
windows and computes the live-slug set with a one-column
ListAllSlugs Pluck instead of a Limit:0 GetGraph that materialized
every node + edge. Memory is now bounded; 40k pages walks in
constant ~4MB.
- 14 GORM tests for both repos against an in-memory SQLite mirror of
the production DDL (task_queue_test.go).
- 6 tests for the dead-letter middleware covering retry budget
detection, payload-agnostic scope inference, error truncation, and
repo-failure isolation.
- 7 tests for the slug fuzzy resolve helper covering the three
resolution stages, display-text priority over normalized equality,
bigram fallback acceptance, and rejection of unrelated slugs.
- Existing wiki_ingest / wiki_lint / wiki_page tests updated to the
new fetcher / cursor APIs.
This commit is contained in:
@@ -73,7 +73,24 @@ func NewWikiLintService(
|
||||
}
|
||||
}
|
||||
|
||||
// RunLint performs a comprehensive health check on a wiki knowledge base
|
||||
// lintCursorBatch is the per-batch limit for the streaming page walk.
|
||||
// Picked at 200 because wiki pages can carry multi-KB content blobs
|
||||
// and 200 rows × ~20KB ≈ 4MB resident at a time, which is well within
|
||||
// what we want to hold while running per-page checks.
|
||||
const lintCursorBatch = 200
|
||||
|
||||
// RunLint performs a comprehensive health check on a wiki knowledge base.
|
||||
//
|
||||
// At 4w-document scale the legacy "load every page in one shot"
|
||||
// approach was the dominant tail in this method (and intermittently
|
||||
// caused OOM in production). We now walk the page set via
|
||||
// ListPagesCursor in lintCursorBatch-sized windows, accumulating
|
||||
// issues incrementally — memory stays bounded regardless of KB size.
|
||||
//
|
||||
// We also drop the GetGraph(Limit:0) call that the legacy path used
|
||||
// to compute the live-slug set. ListAllSlugs is a one-column projection
|
||||
// over the same predicate (kbID + status<>archived), so it gives the
|
||||
// same answer at a fraction of the cost.
|
||||
func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintReport, error) {
|
||||
// Validate KB
|
||||
kb, err := s.kbService.GetKnowledgeBaseByIDOnly(ctx, kbID)
|
||||
@@ -90,159 +107,170 @@ func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintRe
|
||||
return nil, fmt.Errorf("get stats: %w", err)
|
||||
}
|
||||
|
||||
// Get graph for link analysis. Lint needs the FULL graph to detect
|
||||
// orphans and broken links across every page, so we pass Limit=0
|
||||
// which the service treats as "no cap".
|
||||
graph, err := s.wikiService.GetGraph(ctx, &types.WikiGraphRequest{
|
||||
KnowledgeBaseID: kbID,
|
||||
Mode: types.WikiGraphModeOverview,
|
||||
Limit: 0,
|
||||
})
|
||||
// Compute the live-slug set from the cheap one-column projection.
|
||||
// This replaces a full GetGraph call (which materialized every node
|
||||
// + edge) with a single Pluck("slug") query.
|
||||
liveSlugs, err := s.wikiService.ListAllSlugs(ctx, kbID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get graph: %w", err)
|
||||
return nil, fmt.Errorf("list all slugs: %w", err)
|
||||
}
|
||||
|
||||
// Get all pages for detailed analysis
|
||||
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||
KnowledgeBaseID: kbID,
|
||||
PageSize: 500,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list pages: %w", err)
|
||||
slugSet := make(map[string]bool, len(liveSlugs))
|
||||
for _, slug := range liveSlugs {
|
||||
slugSet[slug] = true
|
||||
}
|
||||
|
||||
var issues []WikiLintIssue
|
||||
healthScore := 100
|
||||
knowledgeLive := make(map[string]bool) // kid -> exists; cached across pages
|
||||
|
||||
// Build slug set for link validation
|
||||
slugSet := make(map[string]bool)
|
||||
for _, node := range graph.Nodes {
|
||||
slugSet[node.Slug] = true
|
||||
}
|
||||
|
||||
// Check 1: Orphan pages (no inbound links, excluding index/log)
|
||||
for _, page := range resp.Pages {
|
||||
if page.PageType == types.WikiPageTypeIndex || page.PageType == types.WikiPageTypeLog {
|
||||
continue
|
||||
}
|
||||
if len(page.InLinks) == 0 {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueOrphanPage,
|
||||
Severity: SeverityWarning,
|
||||
PageSlug: page.Slug,
|
||||
Description: fmt.Sprintf("Page '%s' has no inbound links — it's disconnected from the wiki", page.Title),
|
||||
AutoFixable: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check 2: Broken links
|
||||
for _, page := range resp.Pages {
|
||||
for _, outLink := range page.OutLinks {
|
||||
if !slugSet[outLink] {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueBrokenLink,
|
||||
Severity: SeverityError,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: outLink,
|
||||
Description: fmt.Sprintf("Page '%s' links to [[%s]] which does not exist", page.Title, outLink),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check 3: Empty content
|
||||
for _, page := range resp.Pages {
|
||||
content := strings.TrimSpace(page.Content)
|
||||
if len(content) < 50 {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueEmptyContent,
|
||||
Severity: SeverityWarning,
|
||||
PageSlug: page.Slug,
|
||||
Description: fmt.Sprintf("Page '%s' has very little content (%d chars)", page.Title, len(content)),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check 4: Stale source refs — source_refs pointing at soft-deleted
|
||||
// knowledge. This is the primary self-heal for the ingest/delete race
|
||||
// condition: if a wiki_ingest task managed to slip past the in-flight
|
||||
// guards and wrote a page for a knowledge that has since been deleted,
|
||||
// the page becomes a dead-end (wiki_read_source_doc returns "knowledge
|
||||
// not found"). Flag it so AutoFix can strip the ref, and delete the
|
||||
// page if no live refs remain.
|
||||
if s.knowledgeService != nil {
|
||||
knowledgeLive := make(map[string]bool) // kid -> exists
|
||||
for _, page := range resp.Pages {
|
||||
// Skip wiki-intrinsic pages: index/log never carry SourceRefs
|
||||
// anyway, and accidentally flagging them would risk AutoFix
|
||||
// deleting system pages.
|
||||
if page.PageType == types.WikiPageTypeIndex || page.PageType == types.WikiPageTypeLog {
|
||||
continue
|
||||
}
|
||||
for _, ref := range page.SourceRefs {
|
||||
kid := ref
|
||||
if i := strings.Index(ref, "|"); i > 0 {
|
||||
kid = ref[:i]
|
||||
}
|
||||
if kid == "" {
|
||||
continue
|
||||
}
|
||||
live, seen := knowledgeLive[kid]
|
||||
if !seen {
|
||||
kn, err := s.knowledgeService.GetKnowledgeByIDOnly(ctx, kid)
|
||||
live = err == nil && kn != nil
|
||||
knowledgeLive[kid] = live
|
||||
}
|
||||
if !live {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueStaleRef,
|
||||
Severity: SeverityError,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: kid,
|
||||
Description: fmt.Sprintf("Page '%s' references deleted knowledge %s", page.Title, kid),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check 5: Missing cross-references (entities mentioned in content but not linked)
|
||||
// First-pass walk: orphan / broken-link / empty / stale-ref
|
||||
// detection. Each check is independent of order; we accumulate
|
||||
// issues per-batch and the cursor walk keeps memory bounded.
|
||||
//
|
||||
// We collect entity / concept titles in this pass too so the
|
||||
// missing-cross-ref check (which is intrinsically O(N×M) in
|
||||
// distinct entities × pages) doesn't need a second walk to find
|
||||
// candidates. The check itself runs in a second walk because it
|
||||
// needs the full entity-title set to compare against any page.
|
||||
entitySlugs := make(map[string]string) // slug -> title
|
||||
for _, page := range resp.Pages {
|
||||
if page.PageType == types.WikiPageTypeEntity || page.PageType == types.WikiPageTypeConcept {
|
||||
entitySlugs[page.Slug] = page.Title
|
||||
|
||||
cursor := ""
|
||||
for {
|
||||
pages, next, err := s.wikiService.ListPagesCursor(ctx, kbID, cursor, lintCursorBatch)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list pages cursor: %w", err)
|
||||
}
|
||||
}
|
||||
for _, page := range resp.Pages {
|
||||
for slug, title := range entitySlugs {
|
||||
if slug == page.Slug {
|
||||
continue
|
||||
if len(pages) == 0 {
|
||||
break
|
||||
}
|
||||
for _, page := range pages {
|
||||
// Track entity/concept titles for the second pass.
|
||||
if page.PageType == types.WikiPageTypeEntity || page.PageType == types.WikiPageTypeConcept {
|
||||
entitySlugs[page.Slug] = page.Title
|
||||
}
|
||||
// Check if title is mentioned in content but not linked
|
||||
if strings.Contains(strings.ToLower(page.Content), strings.ToLower(title)) {
|
||||
linked := false
|
||||
for _, l := range page.OutLinks {
|
||||
if l == slug {
|
||||
linked = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !linked {
|
||||
|
||||
// Check 1: Orphan pages (no inbound links, excluding system pages).
|
||||
if page.PageType != types.WikiPageTypeIndex && page.PageType != types.WikiPageTypeLog {
|
||||
if len(page.InLinks) == 0 {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueMissingCrossRef,
|
||||
Severity: SeverityInfo,
|
||||
Type: LintIssueOrphanPage,
|
||||
Severity: SeverityWarning,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: slug,
|
||||
Description: fmt.Sprintf("Page '%s' mentions '%s' but doesn't link to [[%s]]", page.Title, title, slug),
|
||||
Description: fmt.Sprintf("Page '%s' has no inbound links — it's disconnected from the wiki", page.Title),
|
||||
AutoFixable: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check 2: Broken links — outlinks pointing at slugs that
|
||||
// don't exist in the live set.
|
||||
for _, outLink := range page.OutLinks {
|
||||
if !slugSet[outLink] {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueBrokenLink,
|
||||
Severity: SeverityError,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: outLink,
|
||||
Description: fmt.Sprintf("Page '%s' links to [[%s]] which does not exist", page.Title, outLink),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check 3: Empty content.
|
||||
content := strings.TrimSpace(page.Content)
|
||||
if len(content) < 50 {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueEmptyContent,
|
||||
Severity: SeverityWarning,
|
||||
PageSlug: page.Slug,
|
||||
Description: fmt.Sprintf("Page '%s' has very little content (%d chars)", page.Title, len(content)),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
|
||||
// Check 4: Stale source refs — source_refs pointing at
|
||||
// soft-deleted knowledge. Cached knowledgeLive lookup keeps
|
||||
// per-kid checks O(1) after the first batch encounters
|
||||
// each id.
|
||||
if s.knowledgeService != nil &&
|
||||
page.PageType != types.WikiPageTypeIndex &&
|
||||
page.PageType != types.WikiPageTypeLog {
|
||||
for _, ref := range page.SourceRefs {
|
||||
kid := ref
|
||||
if i := strings.Index(ref, "|"); i > 0 {
|
||||
kid = ref[:i]
|
||||
}
|
||||
if kid == "" {
|
||||
continue
|
||||
}
|
||||
live, seen := knowledgeLive[kid]
|
||||
if !seen {
|
||||
kn, err := s.knowledgeService.GetKnowledgeByIDOnly(ctx, kid)
|
||||
live = err == nil && kn != nil
|
||||
knowledgeLive[kid] = live
|
||||
}
|
||||
if !live {
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueStaleRef,
|
||||
Severity: SeverityError,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: kid,
|
||||
Description: fmt.Sprintf("Page '%s' references deleted knowledge %s", page.Title, kid),
|
||||
AutoFixable: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
cursor = next
|
||||
}
|
||||
|
||||
// Second-pass walk: missing-cross-ref check. This needs the full
|
||||
// entitySlugs map (built in pass 1), so it has to be a separate
|
||||
// pass — but it's still streaming.
|
||||
cursor = ""
|
||||
for {
|
||||
pages, next, err := s.wikiService.ListPagesCursor(ctx, kbID, cursor, lintCursorBatch)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list pages cursor (pass 2): %w", err)
|
||||
}
|
||||
if len(pages) == 0 {
|
||||
break
|
||||
}
|
||||
for _, page := range pages {
|
||||
lowerContent := strings.ToLower(page.Content)
|
||||
outLinkSet := make(map[string]struct{}, len(page.OutLinks))
|
||||
for _, l := range page.OutLinks {
|
||||
outLinkSet[l] = struct{}{}
|
||||
}
|
||||
for slug, title := range entitySlugs {
|
||||
if slug == page.Slug || title == "" {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(lowerContent, strings.ToLower(title)) {
|
||||
continue
|
||||
}
|
||||
if _, linked := outLinkSet[slug]; linked {
|
||||
continue
|
||||
}
|
||||
issues = append(issues, WikiLintIssue{
|
||||
Type: LintIssueMissingCrossRef,
|
||||
Severity: SeverityInfo,
|
||||
PageSlug: page.Slug,
|
||||
TargetSlug: slug,
|
||||
Description: fmt.Sprintf("Page '%s' mentions '%s' but doesn't link to [[%s]]", page.Title, title, slug),
|
||||
AutoFixable: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
if next == "" {
|
||||
break
|
||||
}
|
||||
cursor = next
|
||||
}
|
||||
|
||||
// Calculate health score
|
||||
|
||||
Reference in New Issue
Block a user