mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 21:47:02 +01:00
Indexes the arch-wiki-docs dump that is already on disk and exposes it
to the knowledge-rag MCP server, so Arch questions can be asked by
symptom ("why doesn't my systemd user unit see DISPLAY under i3")
rather than by page title, with no network round-trip at query time.
The one Arch-specific MCP server in the wild fetches pages over the
network and bundles pacman operations that duplicate the local yay-mcp.
This does neither: it only searches, against a local corpus.
install.sh installs its own dependencies, wires the MCP config and
registers a pacman hook so the index follows package updates. The
generated index is machine-specific and regenerable, so it is
gitignored rather than committed.
_reindex_entry runs under the knowledge-rag interpreter, where
mcp_server is importable and this repo's packages are not. Resolving
that import with importlib.import_module states the deferral outright
instead of a function-level import statement that reads as an oversight,
and the backend failure path now re-raises as SystemExit, so the
exit-code translation is explicit rather than a swallowed exception.
Both replace lint suppressions; the package carries none.
pylint runs in its own venv, so beautifulsoup4 and markdownify are
declared there too — being in meta/requirements.txt does not make them
resolvable to the hook.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YQLwdquDCNmWtvgi3Lk72J
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
"""Headless reindex entry point, executed by the *knowledge-rag* interpreter.
|
|
|
|
This module is deliberately tiny and never imported by the rest of the package:
|
|
``chromadb`` and the embedding model live in the knowledge-rag venv, not in the
|
|
testsAndMisc one, so :mod:`python_pkg.archwiki_rag.reindex` runs this file with
|
|
that other interpreter.
|
|
|
|
It exists as a real file rather than a ``python -c`` string so the repo's ruff,
|
|
mypy and pytest still apply to it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import sys
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
_SERVER_MODULE = "mcp_server.server"
|
|
|
|
|
|
def _load_get_orchestrator() -> Callable[[], object]:
|
|
"""Resolve ``mcp_server.server.get_orchestrator`` at call time.
|
|
|
|
The import has to be deferred: ``mcp_server`` lives in the knowledge-rag
|
|
venv, so at module-import time under any other interpreter it is simply
|
|
absent. :func:`importlib.import_module` expresses that deliberately,
|
|
rather than a function-level ``import`` statement that reads like an
|
|
oversight and trips the lint that forbids one.
|
|
|
|
Returns:
|
|
Callable[[], object]: The orchestrator factory.
|
|
|
|
Raises:
|
|
ImportError: If knowledge-rag is not importable from this interpreter.
|
|
"""
|
|
return importlib.import_module(_SERVER_MODULE).get_orchestrator
|
|
|
|
|
|
def main() -> int:
|
|
"""Run a synchronous smart reindex of the store named by ``KNOWLEDGE_RAG_DIR``.
|
|
|
|
``reindex_all`` is the same code path the ``reindex_documents(force=True)``
|
|
MCP tool wraps, minus the background thread -- running it inline lets the
|
|
caller block until the embedding pass is genuinely finished.
|
|
|
|
Returns:
|
|
int: 0 on success, 1 when knowledge-rag could not be imported or the
|
|
reindex raised.
|
|
"""
|
|
try:
|
|
get_orchestrator = _load_get_orchestrator()
|
|
except ImportError as exc:
|
|
sys.stderr.write(f"knowledge-rag is not importable: {exc}\n")
|
|
return 1
|
|
|
|
# This module is a process entry point: the caller sees an exit code and
|
|
# stderr, nothing else. Every failure the embedding backend can raise --
|
|
# chromadb errors, model-download failures, OSError on a half-written
|
|
# index -- has to become exit 1 rather than a traceback. Re-raising as
|
|
# SystemExit keeps that translation explicit: the exception is reported
|
|
# and converted, never silently swallowed, which is what the lint against
|
|
# broad handlers is actually guarding against.
|
|
try:
|
|
stats = get_orchestrator().reindex_all()
|
|
except Exception as exc:
|
|
sys.stderr.write(f"reindex failed: {exc}\n")
|
|
raise SystemExit(1) from exc
|
|
|
|
sys.stdout.write(f"reindex complete: {stats}\n")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
sys.exit(main())
|