feat: Improve wiki cleanup and ingest handling for deleted knowledge

- Enhanced the `cleanupWikiOnKnowledgeDelete` function to pass the full knowledge object, allowing for more accurate title and summary retrieval during wiki cleanup.
- Introduced a tombstone mechanism in Redis to mark recently deleted knowledge, enabling in-flight wiki ingest tasks to skip processing for deleted documents.
- Updated the `isKnowledgeGone` function to check for deleted knowledge using the tombstone, improving efficiency in handling concurrent delete and ingest operations.
- Added logic in the `wikiIngestService` to filter out updates for deleted knowledge, ensuring that no ghost references are created during the ingest process.
- Implemented lint checks for stale source references in the `WikiLintService`, allowing for automatic cleanup of pages referencing deleted knowledge.

These changes significantly enhance the robustness of the wiki system, ensuring consistency and accuracy in handling knowledge deletions and related wiki pages.
This commit is contained in:
wizardchen
2026-04-21 14:20:52 +08:00
parent 8461d5ce4d
commit fdccb68ee4
6 changed files with 387 additions and 56 deletions
+80 -5
View File
@@ -55,18 +55,21 @@ type WikiLintReport struct {
// WikiLintService provides wiki health checking capabilities
type WikiLintService struct {
wikiService interfaces.WikiPageService
kbService interfaces.KnowledgeBaseService
wikiService interfaces.WikiPageService
kbService interfaces.KnowledgeBaseService
knowledgeService interfaces.KnowledgeService
}
// NewWikiLintService creates a new wiki lint service
func NewWikiLintService(
wikiService interfaces.WikiPageService,
kbService interfaces.KnowledgeBaseService,
knowledgeService interfaces.KnowledgeService,
) *WikiLintService {
return &WikiLintService{
wikiService: wikiService,
kbService: kbService,
wikiService: wikiService,
kbService: kbService,
knowledgeService: knowledgeService,
}
}
@@ -157,7 +160,51 @@ func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintRe
}
}
// Check 4: Missing cross-references (entities mentioned in content but not linked)
// 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)
entitySlugs := make(map[string]string) // slug -> title
for _, page := range resp.Pages {
if page.PageType == types.WikiPageTypeEntity || page.PageType == types.WikiPageTypeConcept {
@@ -310,6 +357,34 @@ func (s *WikiLintService) AutoFix(ctx context.Context, kbID string) (int, error)
if _, err := s.wikiService.UpdatePage(ctx, page); err == nil {
fixed++
}
case LintIssueStaleRef:
// Strip source_refs that point at soft-deleted knowledge. If the
// page has no other live sources, delete it outright — leaving
// an orphan summary page is worse than removing it, because the
// model would still link to it from other pages and the
// wiki_read_source_doc drill-down would always fail.
if issue.TargetSlug == "" {
continue
}
page, err := s.wikiService.GetPageBySlug(ctx, kbID, issue.PageSlug)
if err != nil || page == nil {
continue
}
if page.PageType == types.WikiPageTypeIndex || page.PageType == types.WikiPageTypeLog {
continue
}
remaining := removeSourceRef(page.SourceRefs, issue.TargetSlug)
if len(remaining) == 0 {
if err := s.wikiService.DeletePage(ctx, kbID, page.Slug); err == nil {
fixed++
}
} else if len(remaining) != len(page.SourceRefs) {
page.SourceRefs = remaining
if err := s.wikiService.UpdatePageMeta(ctx, page); err == nil {
fixed++
}
}
}
}