perf(wiki): move ingest log to event table and index to on-demand API

Fixes O(n²) write amplification during wiki ingest on large KBs. Previously
every ingest/retract op re-wrote the single `slug='log'` row end-to-end and
every batch re-wrote the entire `slug='index'` directory markdown. On a 40k-
doc KB the log row grew to tens of MB and the index row to several MB, so
each batch triggered giant TOAST updates that dominated ingest wall time.

Log: new `wiki_log_entries` event table (`id DESC` indexed per KB) replaces
the single TEXT row. Batch ingest now collects entries and flushes them
once per batch via `AppendBatch`. Each entry stores `pages_affected` as
JSONB `[{slug,title}]` so the UI can render real titles; custom Scan falls
back to legacy `[]string` so older rows still deserialize.

Index: `wiki_pages[slug=index].content` keeps only the LLM-generated intro
(a few KB). The directory is now served by a structured paginated API
(`GetIndexView`) that reads `slug/title/summary` per type with cursor
pagination, so the agent and the frontend only pull the slice they need.
`RebuildIndexPage` degrades to a no-op; agent `wiki_read_page('index')`
synthesizes a small top-K overview and points callers at `wiki_search`.

Ingest resilience: LLM calls wrap with 3-attempt exponential backoff on
transient errors (5xx/408/429, transport resets/timeouts). Summary/extract
failures now bubble up so the batch's failed-op requeue path runs instead
of silently dropping the doc.

Frontend: sidebar Index/Log entries switch to dedicated views. Index view
streams intro → Summary → Entity → Concept → Synthesis → Comparison via
IntersectionObserver (with a nextTick re-check so small KBs still load
every section). Log view uses cursor pagination; pages_affected renders
titles with slug tooltip.

- new migration: migrations/versioned/000040_wiki_log_entries.{up,down}.sql
- tests: log repo pagination + legacy Scan, ListByTypeLight windowing,
  renderIndexOverviewForAgent output, isTransientLLMError classifier

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
wizardchen
2026-05-08 21:43:53 +08:00
committed by lyingbug
parent bf12868923
commit d0144f3586
25 changed files with 2353 additions and 272 deletions
@@ -392,14 +392,42 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
totalPagesAffected = len(allPagesAffected)
// Append log entries — one per operation for chronological traceability
// Collect log entries for this batch and flush them in a single INSERT.
// Historically each op triggered its own `GetLog + UpdatePage` round
// trip, which rewrote the entire log page TEXT column and caused O(n^2)
// write amplification as the log grew. AppendBatch writes one row per
// event into wiki_log_entries instead.
//
// slugsToRefs resolves each retract slug against the batch-start
// snapshot (batchCtx.SlugTitleMap) so the log feed carries titles for
// pages that existed when the batch began. Pages created or renamed
// during this batch fall through the map lookup and log as slug-only
// refs, which the frontend renders as the slug itself — a sensible
// fallback given retracts only touch pre-existing pages.
slugsToRefs := func(slugs []string) []types.WikiLogPageRef {
if len(slugs) == 0 {
return nil
}
out := make([]types.WikiLogPageRef, 0, len(slugs))
for _, slug := range slugs {
title := batchCtx.SlugTitleMap[slug]
out = append(out, types.WikiLogPageRef{Slug: slug, Title: title})
}
return out
}
logEntries := make([]*types.WikiLogEntry, 0, len(pendingOps)+len(docResults))
for _, op := range pendingOps {
if op.Op == WikiOpRetract {
s.appendLogEntry(ctx, payload.KnowledgeBaseID, "retract", op.KnowledgeID, op.DocTitle, op.DocSummary, op.PageSlugs)
logEntries = append(logEntries, s.buildLogEntry(payload.TenantID, payload.KnowledgeBaseID, "retract", op.KnowledgeID, op.DocTitle, op.DocSummary, slugsToRefs(op.PageSlugs)))
}
}
for _, r := range docResults {
s.appendLogEntry(ctx, payload.KnowledgeBaseID, "ingest", r.KnowledgeID, r.DocTitle, r.Summary, r.Pages)
logEntries = append(logEntries, s.buildLogEntry(payload.TenantID, payload.KnowledgeBaseID, "ingest", r.KnowledgeID, r.DocTitle, r.Summary, r.Pages))
}
if len(logEntries) > 0 && s.logEntrySvc != nil {
if err := s.logEntrySvc.AppendBatch(ctx, logEntries); err != nil {
logger.Warnf(ctx, "wiki ingest: failed to append %d log entries: %v", len(logEntries), err)
}
}
// Build change description for the Index Intro LLM prompt
@@ -625,9 +653,18 @@ func (s *wikiIngestService) mapOneDocument(
}
}
extractedPages := make([]string, 0, len(slugItems)+1)
for slug := range slugItems {
extractedPages = append(extractedPages, slug)
// extractedPages records every wiki page this document materialized
// (entities, concepts, plus the summary page appended below). The
// slug is used for link/retract bookkeeping; the title is captured
// for the log feed so the user sees "提供本学位在线验证报告查询…"
// rather than "entity/xue-xin-wang".
extractedPages := make([]types.WikiLogPageRef, 0, len(slugItems)+1)
for slug, item := range slugItems {
title := item.Name
if title == "" {
title = slug
}
extractedPages = append(extractedPages, types.WikiLogPageRef{Slug: slug, Title: title})
}
// Count total distinct chunks cited across all slugs for logging.
@@ -647,32 +684,44 @@ func (s *wikiIngestService) mapOneDocument(
var docSummary string
if summaryErr != nil {
logger.Errorf(ctx, "wiki ingest: generate summary failed for %s: %v", knowledgeID, summaryErr)
} else {
sumLine, sumBody := splitSummaryLine(summaryContent)
if sumBody == "" {
sumBody = summaryContent
}
if sumLine == "" {
sumLine = docTitle
}
docSummaryLine = sumLine
docSummary = sumBody
if strings.TrimSpace(docSummary) == "" {
docSummary = sumLine
}
updates = append(updates, SlugUpdate{
Slug: summarySlug,
Type: types.WikiPageTypeSummary,
DocTitle: docTitle,
KnowledgeID: knowledgeID,
SourceRef: sourceRef,
Language: lang,
SummaryLine: sumLine,
SummaryBody: sumBody,
})
extractedPages = append(extractedPages, summarySlug)
// Summary is the headline artifact of an ingested document — a
// document with no summary page is half-ingested and leaves the
// entity/concept updates hanging without a root to link back to
// from the index. Historically we just logged and moved on,
// which meant a single transient 504 permanently dropped the
// summary page for that document.
//
// Returning an error here sends the op to failedOps (see the
// map-phase loop in ProcessWikiIngest), which requeueFailedOps
// appends back onto the pending list so the next batch retries.
// The internal retries in generateWithTemplate already exhaust
// the LLM's own transient-error budget before we give up here.
logger.Errorf(ctx, "wiki ingest: generate summary failed for %s, will requeue: %v", knowledgeID, summaryErr)
return nil, nil, fmt.Errorf("generate summary: %w", summaryErr)
}
sumLine, sumBody := splitSummaryLine(summaryContent)
if sumBody == "" {
sumBody = summaryContent
}
if sumLine == "" {
sumLine = docTitle
}
docSummaryLine = sumLine
docSummary = sumBody
if strings.TrimSpace(docSummary) == "" {
docSummary = sumLine
}
updates = append(updates, SlugUpdate{
Slug: summarySlug,
Type: types.WikiPageTypeSummary,
DocTitle: docTitle,
KnowledgeID: knowledgeID,
SourceRef: sourceRef,
Language: lang,
SummaryLine: sumLine,
SummaryBody: sumBody,
})
extractedPages = append(extractedPages, types.WikiLogPageRef{Slug: summarySlug, Title: docTitle})
// Entities
for _, item := range extractedEntities {
@@ -740,7 +789,7 @@ func (s *wikiIngestService) mapOneDocument(
newSlugSet := make(map[string]bool, len(extractedPages))
for _, ns := range extractedPages {
newSlugSet[ns] = true
newSlugSet[ns.Slug] = true
}
var reparseOverlap, staleCount int