Skip to content

[bug] search-index 收缩保护导致已删除文档残留在检索结果中 #83

Closed

Description

@jeff-r2026

现象

buildIndex 有一个“防止把健康索引覆盖成过小索引”的守卫:当新索引条目数 < 旧索引的 50%(且旧索引 > 5 条)时,直接跳过写入:

// src/utils/search-index.ts:554-559
const existingIndex = await loadIndex(targetPath);
if (existingIndex && existingIndex.entries.length > 5 && entries.length < existingIndex.entries.length * 0.5) {
  log.warn(`Index rebuild skipped: new index (${entries.length}) much smaller than existing (${existingIndex.entries.length})`);
  return elapsed;
}

问题

这个守卫无法区分“源目录损坏/读取失败”与“合法的大量删除”。在以下合法场景会导致已删除的文档继续被 recall 命中,直到用户手动删除 search-index.json:

  • 团队一次性删除/归档了大量 learnings 或 docs;
  • scope 切换后源目录指向了一个更小的知识库;
  • teamai pull 重建索引时源已显著缩小。

建议

  • 增加 --force-reindex(或在 pull 显式重建时绕过该守卫);
  • 或将守卫判据从“仅条目数”改为“源目录文件列表/数量校验”,仅在源目录读取异常(0 文件但目录存在且非空)时才跳过;
  • 缩小索引时可写入一份 stale 黑名单以便审计。

严重程度:中高(导致检索返回已删除内容)。

Activity

  1. added
    bug
    on Jun 30, 2026
  2. jeff-r2026 commented on Jul 1, 2026

    @jeff-r2026
    CollaboratorAuthor

    Fixed by #91: the shrink-guard in `buildIndex` (`src/utils/search-index.ts`) now only skips the rebuild when the new index is completely empty, instead of when it's <50% of the existing size — so legitimate large-scale deletions (the scenario this issue reported) now correctly propagate to the index instead of being silently blocked.

    Note: a follow-up risk from this specific fix (the new guard no longer catches partial-failure scenarios that produce a small-but-nonzero index) is tracked separately in #94 — that's a new/different risk, not a reopening of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions