ReMe/reme/steps/index/reindex.py
北沐城歌 037cd06e8b
fix(file_store): keep tag rebuild failures from aborting the whole reindex (#780)
The tag index is an optional derived index. Incremental updates already
handle a failure that way: _upsert_tag_nodes and _delete_tag_paths swallow
it, log it, and mark the index unhealthy, deliberately "without allowing
optional-index failures to block other indexes".

The explicit rebuild path did the opposite: it turned the same False into a
RuntimeError, which failed every reindex(scope="all") call and discarded the
keyword and embedding rebuilds that had already completed.

The rebuild is now wrapped rather than inspected only through its return
value. require_tag_index() runs before _rebuild_tag_index reaches its own
try, so an unavailable tag-index dependency raises from there and a bare
return check would let it escape; the wrapper keeps that case inside the
optional index as well.

On failure the store returns a "failed" marker instead, and the step
summary surfaces it, so a degraded tag index stays visible without hiding
the indexes that succeeded.

That marker has to be rendered for flat single-index reports too. A
scope="tag" rebuild returns {"indexed": 0, "scope": "tag", "failed": true}
with no nested entries, so a summary built only from nested dictionaries
drops it and answers "Reindexed scope=tag: 0 indexed" -- indistinguishable
from a successful empty rebuild. MCP callers read only the answer and the
CLI hides metadata, so they had no way to tell a failed repair from an
empty one.

The embedding rebuild keeps raising: it is not an optional index, and an
incomplete vector index must not be reported as success.

Refs agentscope-ai/QwenPaw#8863
Refs agentscope-ai/QwenPaw#8580
2026-11-26 11:13:12 +08:00

41 lines
1.6 KiB
Python

"""Explicit scoped rebuild of search indexes from already-ingested chunks."""
from ..base_step import BaseStep
from ...core.registry import R
def _render_entry(name: str, entry: dict) -> str:
"""Render one index report, flagging a degraded optional index."""
rendered = f"{name}={entry.get('indexed', 0)}"
return f"{rendered} (failed)" if entry.get("failed") else rendered
def _render_reindex(details: dict) -> str:
"""Render the store's reindex report as one readable summary line."""
scope = str(details.get("scope") or "all")
counts = ", ".join(_render_entry(name, entry) for name, entry in details.items() if isinstance(entry, dict))
if counts:
summary = counts
else:
# Flat single-index report: the failure marker sits at the top level, so
# it has to be rendered here or it disappears from ``answer`` -- which is
# all that MCP callers and the default CLI output ever look at.
summary = f"{details.get('indexed', 0)} indexed"
if details.get("failed"):
summary += " (failed)"
return f"Reindexed scope={scope}: {summary}"
@R.register("reindex_step")
class ReindexStep(BaseStep):
"""Rebuild BM25, embeddings, and/or tags without scanning workspace files."""
async def execute(self):
assert self.context is not None
scope = str(self.context.get("scope", "all"))
details = await self.file_store.reindex(scope)
self.context.response.answer = _render_reindex(details)
self.context.response.metadata.update(details)
self.context.response.metadata["scope"] = scope
return self.context.response