From 97b026405a0bff152d8423b374a978931328b765 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Wed, 18 Nov 2026 23:50:57 +0800 Subject: [PATCH 1/2] refactor(apps): group external adapters under integrations Signed-off-by: zengchuang Agent-assisted-by: Codex (author) --- docs/apps-module-migrations.json | 138 +++++++ docs/architecture.md | 20 +- docs/external-channels.md | 2 +- docs/module-layout.md | 49 ++- src/inferrsi/apps/cannbot/__init__.py | 105 +---- src/inferrsi/apps/cannbot/project.py | 310 ++------------- src/inferrsi/apps/cannbot/read_worker.py | 370 +----------------- src/inferrsi/apps/cli/__init__.py | 8 +- src/inferrsi/apps/integrations/__init__.py | 1 + .../apps/integrations/cannbot/__init__.py | 107 +++++ .../apps/integrations/cannbot/project.py | 283 ++++++++++++++ .../apps/integrations/cannbot/read_worker.py | 369 +++++++++++++++++ .../apps/integrations/mindie/__init__.py | 5 + .../apps/integrations/mindie/context.py | 181 +++++++++ src/inferrsi/apps/integrations/mindie/mcp.py | 125 ++++++ .../apps/integrations/mindie/project.py | 249 ++++++++++++ .../apps/integrations/mindie/read_worker.py | 79 ++++ src/inferrsi/apps/mindie/__init__.py | 6 +- src/inferrsi/apps/mindie/context.py | 182 +-------- src/inferrsi/apps/mindie/mcp.py | 126 +----- src/inferrsi/apps/mindie/project.py | 262 +------------ src/inferrsi/apps/mindie/read_worker.py | 80 +--- tests/_gate_policy_support.py | 3 +- tests/check_installed.py | 96 +++-- tests/test_apps_module_migrations.py | 104 ++++- tests/test_cannbot_boundaries.py | 6 +- tests/test_contracts_package.py | 2 +- tests/test_first_wave_isolation_policy.py | 1 + tests/test_mindie_context.py | 2 +- tests/test_mindie_project.py | 4 +- tests/test_mindie_runtime.py | 6 +- tests/test_package_root_inventory.py | 10 +- tests/test_target_module_import_gate.py | 4 +- 33 files changed, 1824 insertions(+), 1471 deletions(-) create mode 100644 src/inferrsi/apps/integrations/__init__.py create mode 100644 src/inferrsi/apps/integrations/cannbot/__init__.py create mode 100644 src/inferrsi/apps/integrations/cannbot/project.py create mode 100644 src/inferrsi/apps/integrations/cannbot/read_worker.py create mode 100644 src/inferrsi/apps/integrations/mindie/__init__.py create mode 100644 src/inferrsi/apps/integrations/mindie/context.py create mode 100644 src/inferrsi/apps/integrations/mindie/mcp.py create mode 100644 src/inferrsi/apps/integrations/mindie/project.py create mode 100644 src/inferrsi/apps/integrations/mindie/read_worker.py diff --git a/docs/apps-module-migrations.json b/docs/apps-module-migrations.json index 9090a5956..961379b29 100644 --- a/docs/apps-module-migrations.json +++ b/docs/apps-module-migrations.json @@ -321,5 +321,143 @@ "tests/test_apps_module_migrations.py", "tests/test_benchmark_compare.py" ] + }, + { + "old": "inferrsi.apps.cannbot", + "canonical": "inferrsi.apps.integrations.cannbot", + "exports": [ + "CannbotChannel", + "project" + ], + "consumers": [ + "src/inferrsi/apps/cli/__init__.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py" + ], + "package": true, + "module_exports": { + "project": { + "old": "inferrsi.apps.cannbot.project", + "canonical": "inferrsi.apps.integrations.cannbot.project" + } + } + }, + { + "old": "inferrsi.apps.cannbot.project", + "canonical": "inferrsi.apps.integrations.cannbot.project", + "exports": [ + "MAX_BYTES", + "PLATFORMS", + "ChannelFailure", + "project_root", + "load_grant", + "describe", + "search", + "read", + "enable", + "disable" + ], + "consumers": [ + "src/inferrsi/apps/integrations/cannbot/__init__.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py", + "tests/test_cannbot_boundaries.py" + ] + }, + { + "old": "inferrsi.apps.cannbot.read_worker", + "canonical": "inferrsi.apps.integrations.cannbot.read_worker", + "exports": [ + "OutputLimitError", + "capture", + "discover", + "search", + "read" + ], + "consumers": [ + "src/inferrsi/apps/integrations/cannbot/project.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py", + "tests/test_cannbot_boundaries.py" + ] + }, + { + "old": "inferrsi.apps.mindie", + "canonical": "inferrsi.apps.integrations.mindie", + "exports": [], + "consumers": [ + "docs/architecture.md", + "docs/external-channels.md", + "docs/module-layout.md", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py" + ], + "package": true + }, + { + "old": "inferrsi.apps.mindie.context", + "canonical": "inferrsi.apps.integrations.mindie.context", + "exports": [ + "MAX_BYTES", + "export_context" + ], + "consumers": [ + "docs/module-layout.md", + "src/inferrsi/apps/cli/__init__.py", + "src/inferrsi/apps/integrations/mindie/mcp.py", + "src/inferrsi/apps/integrations/mindie/project.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py", + "tests/test_mindie_context.py", + "tests/test_mindie_runtime.py" + ] + }, + { + "old": "inferrsi.apps.mindie.project", + "canonical": "inferrsi.apps.integrations.mindie.project", + "exports": [ + "ReadFailure", + "project_root", + "enable", + "disable", + "describe", + "call", + "dispatch" + ], + "consumers": [ + "docs/module-layout.md", + "src/inferrsi/apps/cli/__init__.py", + "src/inferrsi/apps/integrations/mindie/mcp.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py", + "tests/test_mindie_project.py", + "tests/test_mindie_runtime.py" + ] + }, + { + "old": "inferrsi.apps.mindie.mcp", + "canonical": "inferrsi.apps.integrations.mindie.mcp", + "exports": [ + "serve" + ], + "consumers": [ + "docs/module-layout.md", + "src/inferrsi/apps/integrations/mindie/project.py", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py", + "tests/test_mindie_project.py" + ] + }, + { + "old": "inferrsi.apps.mindie.read_worker", + "canonical": "inferrsi.apps.integrations.mindie.read_worker", + "exports": [ + "read" + ], + "consumers": [ + "docs/module-layout.md", + "tests/check_installed.py", + "tests/test_apps_module_migrations.py" + ] } ] diff --git a/docs/architecture.md b/docs/architecture.md index 0ea517ba0..0c0be5e05 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -215,11 +215,16 @@ src/inferrsi/ skill_evaluation.py Composed bundle export, trial run and summary skill_eval_engineering.py Composed artifact check: task rules around sandbox replay improvement.py Composed assessment, selection, rerun and holdout acceptance - mindie/ - project.py Opt-in per-checkout grant, the worker launch and the recorded read - context.py Export supplied MindIE reads into a caller-reviewed campaign trace - mcp.py Stdio MCP for the two approved local MindIE reads - read_worker.py Standalone worker run by the approved MindIE interpreter + integrations/ + cannbot/ + __init__.py CannbotChannel implementation and channel result adaptation + project.py Opt-in checkout/interpreter grant and read-only operations + read_worker.py Standalone worker run by the approved Cannbot interpreter + mindie/ + project.py Opt-in per-checkout grant, worker launch and recorded read + context.py Export supplied MindIE reads into a reviewed campaign trace + mcp.py Stdio MCP for the two approved local MindIE reads + read_worker.py Standalone worker run by the approved MindIE interpreter views/ summary_chart.py Stdlib SVG gain chart (chain/branch) and report figure spec optimization_chart.py Managed optimization objective/token curves and final result @@ -290,8 +295,9 @@ tests/fixtures/legacy/ Frozen historical validation dependencies for regres ``` The package root holds exactly two tracked files: `__init__.py` and `__main__.py`. -#452 M1 moved the MindIE adapter to `apps/mindie/` (`project.py`, `context.py`, -`mcp.py` and the standalone `read_worker.py`) and deleted root `mindie_project.py`, +#452 M1 moved the MindIE adapter into Apps; #490 groups it at +`apps/integrations/mindie/` (`project.py`, `context.py`, `mcp.py` and the standalone +`read_worker.py`). M1 deleted root `mindie_project.py`, `mindie_context.py`, `mindie_mcp.py` and `_mindie_read_worker.py` with no forwarding shell or alias. #408 A2 moved the chart renderer to `apps/views/summary_chart.py` and the task launcher to `apps/workflows/benchmark_operations.py` diff --git a/docs/external-channels.md b/docs/external-channels.md index 6db248f58..9d5f13c30 100644 --- a/docs/external-channels.md +++ b/docs/external-channels.md @@ -213,7 +213,7 @@ conclusion. | --- | --- | | `cannbot enable / disable / describe / search / read` | implemented | | `find` integration with gating, budgets, per-channel states | implemented | -| MindIE as a second channel | not yet; `apps/mindie/` has the reader, not the adapter | +| MindIE as a second channel | not yet; `apps/integrations/mindie/` has the reader, not the adapter | | `cannbot serve` (stdio MCP proxying `discover` / `search` / `read_card`) | not yet | | `campaign import-cannbot` (receipts into evidence) | not yet | | Automatic clone, index build or deployment | deliberately absent | diff --git a/docs/module-layout.md b/docs/module-layout.md index 3edb8ca37..57519b8ca 100644 --- a/docs/module-layout.md +++ b/docs/module-layout.md @@ -13,7 +13,7 @@ its dashboard views and templates are in `apps/views/`, and the collected-context projection is in `knowledge/context_projection.py`. #371 owns the Apps composition. Its accepted directory follow-up [#490](https://github.com/InferMatrixLab/InferRSI/issues/490) groups the implemented -storage, handoff and workflows slices described below. +storage, handoff, workflows and integrations slices described below. The old `run` chain is split by owner. Its gates are in `benchmark/legacy_run_v0.py`, its execution facts stay in `harness/`, and @@ -41,8 +41,8 @@ through these exports; new pickles use the canonical definition. The retired root `inferrsi.evidence_paths` has no alias and remains unavailable. Shim removal requires a separate reviewed PR recording consumer migration, -wheel and plugin validation, and the first removal version. Integrations and -integrated acceptance remain subsequent #490 slices. +wheel and plugin validation, and the first removal version. Integrated acceptance +remains the final #490 slice. ### Apps handoff migration @@ -89,6 +89,32 @@ map remains incompatible with a new runtime, including for `status` and `recover Do not edit a frozen plan to bypass this check. Use the original pinned runtime to resume, following [release-boundary recovery](optimization-workflow-unification.md#release-boundary-recovery). +### Apps integrations migration + +`apps/integrations/cannbot/` owns the Cannbot channel, its project grant and its +reader worker. `apps/integrations/mindie/` owns the MindIE grant, context export, +stdio MCP and reader worker. CLI commands import these canonical packages on +demand. The new `integrations` initializer and the MindIE initializer import +nothing. Cannbot retains its existing `CannbotChannel` definition in its package +initializer; importing it does not read grants or start workers. + +The [migration inventory](apps-module-migrations.json) records packages with +`package: true`. Compatibility packages and modules use explicit static imports +and literal `__all__`. Cannbot historically exports a `project` module, so +`module_exports` records its two-level mapping: the old package exports its old +project shim, whose public functions and classes are the canonical objects. +The shim and implementation retain their own module identities; no module alias +is installed. Both parent-first and child-first imports preserve that binding. +Private helper patches use the canonical modules. + +Both project adapters resolve their sibling worker in the canonical directory. +The registered launchers use those files with the approved interpreter and the +existing `-I -B` flags. Each worker loads without InferRSI installed or a package +context, and its top-level imports are standard-library-only. Legacy worker +paths retain Python import compatibility; external interpreters are launched +against the canonical files. Grant schemas, read-only access, limits, revocation +and receipts are unchanged. MCP still starts through `inferrsi mindie serve`. + ### Core and technical packages The four target modules are peers: `knowledge`, `harness`, `benchmark`, and @@ -114,7 +140,8 @@ root to an exact file set: 23 tracked files after C1, 21 after C2, 10 after R1 The two are `__init__.py` and `__main__.py`. M1 moved root `mindie_project.py`, `mindie_context.py`, `mindie_mcp.py` and `_mindie_read_worker.py` to `apps/mindie/project.py`, `context.py`, `mcp.py` and `read_worker.py` and deleted -the four root files with no forwarding shell or alias. A2 moved root +the four root files with no forwarding shell or alias. The integrations slice +later moved that Apps package into `apps/integrations/`. A2 moved root `summary_chart.py` to `apps/views/summary_chart.py` and root `benchmark_operations.py` to `apps/benchmark_operations.py` and deleted both root files the same way. The workflows @@ -188,7 +215,7 @@ Several flat modules remain shared seams until their slice starts. | `knowledge/knowledge_operations.py`, `knowledge/knowledge_store.py`, `knowledge/catalog.py`, `knowledge/research.py`, `knowledge/skill_catalog.py`, `knowledge/_skill_selection.py`, `knowledge/_source_snapshot.py` | The seven Knowledge implementations, moved by K01 | Interfaces unchanged; the same-named root shells were deleted by R1 | | `harness/records/` | Campaign manifests, traces, logs, profile captures and delivered-context captures | Imports no Knowledge or Apps module; `apps/workflows/campaign.py` supplies the backend names, the selection default and the context projection | | `apps/views/`, `apps/workflows/campaign.py` | Dashboard views, templates and the Knowledge-to-records connection | Apps composition under #371; campaign composition is grouped under workflows by #490 | -| `apps/mindie/` (`project.py`, `context.py`, `mcp.py`, `read_worker.py`) | The local MindIE adapter: project grant, stdio MCP, context export and the read worker | M1 (#452) moved the four root MindIE files here; no Harness, Knowledge or Infra module names MindIE, and the initializer imports nothing | +| `apps/integrations/mindie/` (`project.py`, `context.py`, `mcp.py`, `read_worker.py`) | The local MindIE adapter: project grant, stdio MCP, context export and the read worker | M1 (#452) moved the four root MindIE files here; no Harness, Knowledge or Infra module names MindIE, and the initializer imports nothing | | `harness/` | Mixed legacy tree, not target isolation | Not redefined | | `harness/runner.py`, `harness/scenarios/external.py`, `harness/atomic_json.py` | Old run execution facts and Harness-owned JSON writes | Ports injected; no Model edge, and no Infra edge except the local process executor | | `benchmark/legacy_run_v0.py`, `benchmark/sampling.py` | Old run gates and metric validation; image/video sample collection | Imports only `benchmark/metrics.py`; owns its rule inputs and verdict labels | @@ -606,15 +633,15 @@ M1 (#452) deleted the four root MindIE files the same way: | Retired root module | Canonical module or file | | --- | --- | -| `inferrsi.mindie_project` | `inferrsi.apps.mindie.project` | -| `inferrsi.mindie_context` | `inferrsi.apps.mindie.context` | -| `inferrsi.mindie_mcp` | `inferrsi.apps.mindie.mcp` | -| `inferrsi._mindie_read_worker` | `src/inferrsi/apps/mindie/read_worker.py`, a file run by path and not imported | +| `inferrsi.mindie_project` | `inferrsi.apps.integrations.mindie.project` | +| `inferrsi.mindie_context` | `inferrsi.apps.integrations.mindie.context` | +| `inferrsi.mindie_mcp` | `inferrsi.apps.integrations.mindie.mcp` | +| `inferrsi._mindie_read_worker` | `src/inferrsi/apps/integrations/mindie/read_worker.py`, a file run by path and not imported | The four files moved with their code unchanged except the imports that name -their neighbours and the package version. `apps/mindie/__init__.py` imports +their neighbours and the package version. `apps/integrations/mindie/__init__.py` imports nothing and does not re-export `__version__`; `mcp.py` reads it with -`from ... import __version__`. The worker is still started as +`from .... import __version__` after the integrations move. The worker is still started as `python -I -B read_worker.py` with a private working directory, a 20 second limit and a 2 MiB cap, opens SQLite read-only, imports only the standard library at the top level and takes `mindie_knowledge` inside `read()`. It is not described diff --git a/src/inferrsi/apps/cannbot/__init__.py b/src/inferrsi/apps/cannbot/__init__.py index 28f61ace9..720b3084f 100644 --- a/src/inferrsi/apps/cannbot/__init__.py +++ b/src/inferrsi/apps/cannbot/__init__.py @@ -1,107 +1,6 @@ -"""The cannbot-knowledge channel: an external read-only corpus beside ours.""" +"""Compatibility exports for :mod:`inferrsi.apps.integrations.cannbot`.""" -from __future__ import annotations - -import time -from pathlib import Path - -from ...knowledge.channels import ( - ChannelHit, - ChannelRequest, - ChannelResult, - ChannelStatus, -) +from ..integrations.cannbot import CannbotChannel from . import project __all__ = ["CannbotChannel", "project"] - -SCORE_SCALE = "cannbot/knowledge-search.v5" - - -class CannbotChannel: - """Approved cannbot-knowledge checkout, queried through its own interpreter.""" - - def __init__(self, *, root: Path | None = None, timeout: float = 8.0): - from . import project - - self._project = project - self.root = root - self.timeout = timeout - # `hint` records why this channel cannot answer before any search runs. - # Keeping it lets `find` report "not enabled" instead of telling the - # caller to declare a backend for a source that is not approved yet. - self.hint: ChannelStatus | None = None - try: - grant = project.load_grant(root) - except project.ChannelFailure as exc: - grant = {} - self.hint = ChannelStatus(exc.state, detail=str(exc), fix=exc.fix) - except (OSError, ValueError) as exc: - grant = {} - self.hint = ChannelStatus("rejected", detail=str(exc)) - self.backend = "ascend" - self.declared_backend = grant.get("backend") - self.name = "cannbot-knowledge" - - def describe(self, *, request: ChannelRequest) -> ChannelStatus: - try: - info = self._project.describe(self.root, timeout=request.remaining(), request=request) - except self._project.ChannelFailure as exc: - return ChannelStatus(exc.state, detail=str(exc), fix=exc.fix) - state = info.get("state", "rejected") - if state != "ok": - return ChannelStatus( - state if state != "rejected" else "rejected", - detail=str(info.get("detail") or ""), fix=str(info.get("fix") or ""), - ) - return ChannelStatus("ok", revision=info.get("revision")) - - def search( - self, query: str, *, platform: str | None, limit: int, request: ChannelRequest - ) -> ChannelResult: - started = time.monotonic() - try: - payload = self._project.search( - query, platform=platform, limit=limit, root=self.root, - timeout=request.remaining(), request=request, - ) - except self._project.ChannelFailure as exc: - return ChannelResult( - name=self.name, status=ChannelStatus( - exc.state, detail=str(exc), fix=exc.fix, - ), - elapsed_ms=max(1, int((time.monotonic() - started) * 1000)), - ) - elapsed = int((time.monotonic() - started) * 1000) - items = tuple( - ChannelHit( - locator=str(row.get("locator") or ""), - title=str(row.get("title") or ""), - summary=str(row.get("summary") or ""), - score=float(row.get("score") or 0), - score_scale=str(payload.get("score_scale") or SCORE_SCALE), - status=str(row.get("status") or ""), - platforms=tuple(row.get("platforms") or ()), - card_sha256=str(row.get("card_sha256") or ""), - source_card_sha256=str(row.get("source_card_sha256") or ""), - ) - for row in payload.get("items") or [] - if row.get("locator") - ) - return ChannelResult( - name=self.name, - status=ChannelStatus("ok", revision=payload.get("revision")), - items=items, - elapsed_ms=elapsed, - score_scale=str(payload.get("score_scale") or SCORE_SCALE), - read_prefix="inferrsi cannbot read", - ) - - def read(self, locator: str, *, expected_sha256: str | None = None, - expected_revision: str | None = None) -> str: - payload = self._project.read( - locator, expected_sha256=expected_sha256, root=self.root, - expected_revision=expected_revision, - timeout=max(self.timeout, 15.0), - ) - return str(payload.get("text") or "") diff --git a/src/inferrsi/apps/cannbot/project.py b/src/inferrsi/apps/cannbot/project.py index 7386f0499..5afec478f 100644 --- a/src/inferrsi/apps/cannbot/project.py +++ b/src/inferrsi/apps/cannbot/project.py @@ -1,283 +1,27 @@ -"""An opt-in, per-checkout grant for an external cannbot-knowledge checkout. - -The upstream corpus is CANN-licensed and stays where it is. This module records -only which checkout, interpreter and default platform this project approved; it -never copies knowledge into the repository, the wheel or an evidence tree. -""" - -from __future__ import annotations - -import hashlib -import json -import os -import re -import subprocess -import tempfile -from pathlib import Path - -from .read_worker import OutputLimitError, _finite, capture - -_SCHEMA = "inferrsi-cannbot-project/1" -_WORKER = Path(__file__).with_name("read_worker.py") -MAX_BYTES = 2 * 1024 * 1024 -_BACKEND = re.compile(r"[a-z][a-z0-9]{0,31}") -# Upstream platform values this adapter will pass through. An unlisted value is -# refused rather than forwarded: the upstream CLI is fail-closed about platform -# and a typo there would silently change which cards answer. -PLATFORMS = ("a2", "a3", "950", "310p", "910", "kirin_x90", "kirin_9030", "agnostic") - - -class ChannelFailure(ValueError): - """A channel-level failure carrying the state the caller should report.""" - - def __init__(self, message, *, state="rejected", fix="", exit_code=None): - super().__init__(message) - self.state = state - self.fix = fix - self.exit_code = exit_code - - -def project_root() -> Path: - cwd = Path.cwd().resolve() - for folder in (cwd, *cwd.parents): - if (folder / ".git").exists(): - return folder - raise ChannelFailure( - "Open a Git project to use project-scoped cannbot knowledge", - state="not_enabled", fix="Open a Git project, then enable cannbot knowledge", - ) - - -def _grant_path(root: Path) -> Path: - project_id = hashlib.sha256(os.fsencode(root)).hexdigest() - return Path.home() / ".config" / "inferrsi" / "cannbot-projects" / (project_id + ".json") - - -def _checkout_identity(root: Path) -> dict: - marker = root / ".git" - info = marker.stat() - identity = {"device": info.st_dev, "inode": info.st_ino} - if hasattr(info, "st_birthtime"): - identity["created"] = info.st_birthtime - return identity - - -def _json(value) -> bytes: - return (json.dumps(value, ensure_ascii=True, allow_nan=False) + "\n").encode("utf-8") - - -def _parse(raw: bytes) -> dict: - def _object(pairs): - value = {} - for key, item in pairs: - if key in value: - raise ValueError("cannbot response contains duplicate JSON keys") - value[key] = item - return value - - def _invalid(_): - raise ValueError("cannbot response contains a non-finite JSON value") - - try: - value = json.loads(raw, object_pairs_hook=_object, parse_constant=_invalid, - parse_float=_finite) - except (ValueError, UnicodeError, RecursionError) as exc: - raise ValueError("cannbot did not return a JSON object") from exc - if not isinstance(value, dict): - raise ValueError("cannbot did not return a JSON object") - return value - - -def load_grant(root: Path | None = None) -> dict: - root = project_root() if root is None else root - path = _grant_path(root) - try: - info = path.stat() - if (path.is_symlink() or not path.is_file() - or os.name == "posix" and (info.st_uid != os.getuid() or info.st_mode & 0o077)): - raise ValueError("The cannbot project grant must be a private local file") - with path.open("rb") as source: - raw = source.read(MAX_BYTES + 1) - if len(raw) > MAX_BYTES: - raise ValueError("The cannbot project grant exceeds 2 MiB") - grant = _parse(raw) - checkout = _checkout_identity(root) - except FileNotFoundError: - raise ChannelFailure( - "cannbot knowledge is not enabled for this project", state="not_enabled", - fix="inferrsi cannbot enable --checkout --python ", - ) from None - except OSError: - raise ChannelFailure("The cannbot project grant could not be read", state="not_enabled") \ - from None - if (grant.get("schema") != _SCHEMA or grant.get("project_root") != str(root) - or grant.get("checkout_identity") != checkout - or not isinstance(grant.get("upstream"), str) - or not Path(grant["upstream"]).is_absolute() - or not isinstance(grant.get("python"), str) - or not Path(grant["python"]).is_absolute() - or not isinstance(grant.get("backend"), str) - or not _BACKEND.fullmatch(grant["backend"])): - raise ChannelFailure("The cannbot grant does not match this project", state="rejected") - platforms = grant.get("platforms", []) - if not isinstance(platforms, list) or any(item not in PLATFORMS for item in platforms): - raise ChannelFailure("The cannbot grant has an invalid platform list", state="rejected") - return grant - - -def _run(grant: dict, action: str, arguments: dict, *, timeout: float, request=None) -> dict: - environment = os.environ.copy() - for key in ("CANNBOT_MCP_TOKENS", "CANNBOT_KNOWLEDGE_PLATFORM"): - environment.pop(key, None) - try: - with tempfile.TemporaryDirectory(prefix="inferrsi-cannbot-read-") as temporary: - cwd = Path(temporary) / "reader" - cwd.mkdir(mode=0o700) - result = capture( - [grant["python"], "-I", "-B", str(_WORKER), - grant["upstream"], action, json.dumps(arguments), str(timeout)], - cwd=cwd, environment=environment, timeout=timeout, request=request, - own_group=True, - ) - except subprocess.TimeoutExpired: - raise ChannelFailure( - f"cannbot did not answer within {timeout:g}s", state="timeout" - ) from None - except OutputLimitError: - raise ChannelFailure("cannbot output exceeded 2 MiB", state="rejected") from None - except OSError: - raise ChannelFailure( - "The approved cannbot reader could not be started", state="unreachable" - ) from None - if len(result.stdout) > MAX_BYTES: - raise ChannelFailure("cannbot returned more than 2 MiB", state="rejected") - try: - payload = _parse(result.stdout) - except ValueError: - raise ChannelFailure("cannbot returned invalid JSON", state="rejected") from None - if result.returncode or payload.get("isError"): - raise ChannelFailure( - str(payload.get("error") or "The approved cannbot reader failed"), - state=str(payload.get("state") or "rejected"), - fix=str(payload.get("fix") or ""), - exit_code=result.returncode, - ) - return payload - - -def describe(root: Path | None = None, *, timeout: float = 15.0, request=None) -> dict: - try: - grant = load_grant(root) - except ChannelFailure as exc: - return {"state": exc.state, "detail": str(exc), "fix": exc.fix} - try: - payload = _run(grant, "discover", {}, timeout=timeout, request=request) - except ChannelFailure as exc: - return {"state": exc.state, "detail": str(exc), "fix": exc.fix} - return { - "state": "ok", - "revision": payload.get("revision"), - "cards": payload.get("cards"), - "index": payload.get("index", {}), - "platforms": list(grant.get("platforms", [])), - } - - -def search( - query: str, *, platform: str | None = None, limit: int = 100, - root: Path | None = None, timeout: float = 8.0, request=None, -) -> dict: - if not isinstance(query, str) or not query.strip(): - raise ChannelFailure("cannbot search needs a non-empty query") - if type(limit) is not int or not 1 <= limit <= 200: - raise ChannelFailure("cannbot limit must be an integer from 1 through 200") - grant = load_grant(root) - platforms = list(grant.get("platforms", [])) - if platform is None: - if not platforms: - raise ChannelFailure( - "cannbot search needs a platform", state="rejected", - fix="inferrsi find --backend ascend --platform ", - ) - platform = platforms[0] - if platform not in PLATFORMS: - raise ChannelFailure(f"cannbot does not know platform {platform!r}", state="rejected") - return _run(grant, "search", { - "query": query, "platform": platform, "limit": limit, - }, timeout=timeout, request=request) - - -def read(locator: str, *, page: int = 1, expected_sha256: str | None = None, - expected_revision: str | None = None, - root: Path | None = None, timeout: float = 15.0) -> dict: - if not isinstance(locator, str) or not locator.strip(): - raise ChannelFailure("cannbot read needs a locator from a search result") - if type(page) is not int or page < 1: - raise ChannelFailure("cannbot page must be a positive integer") - if expected_sha256 is not None and ( - not isinstance(expected_sha256, str) or not re.fullmatch(r"[0-9a-f]{64}", expected_sha256) - ): - raise ChannelFailure("cannbot expected_sha256 must be a lowercase 64-hex digest") - if expected_revision is not None and ( - not isinstance(expected_revision, str) - or not re.fullmatch(r"[0-9a-f]{40}", expected_revision) - ): - raise ChannelFailure("cannbot expected_revision must be a full lowercase Git revision") - arguments = {"locator": locator, "page": page} - if expected_sha256: - arguments["expected_sha256"] = expected_sha256 - if expected_revision: - arguments["expected_revision"] = expected_revision - return _run(load_grant(root), "read", arguments, timeout=timeout) - - -def enable(*, upstream: Path, python: Path, backend: str = "ascend", - platforms: list[str] | None = None) -> dict: - """Approve one checkout for this project after proving it answers. - - The probe is deliberate: a grant that records a checkout which cannot answer - would turn every later search into a failure the user has to debug. - """ - root = project_root() - upstream = Path(upstream).expanduser() - if not (upstream / ".git").exists(): - raise ValueError("--checkout must be a Git checkout of cannbot-knowledge") - if not (upstream / "knowledge").is_dir(): - raise ValueError("--checkout must contain the knowledge/ bundle root") - if not (upstream / ".agents/skills/knowledge-query/scripts/knowledge_query.py").is_file(): - raise ValueError("--checkout is missing the knowledge-query scripts") - interpreter = Path(python).expanduser() - if not interpreter.is_file() or not os.access(interpreter, os.X_OK): - raise ValueError("--python must be an executable interpreter with pyyaml installed") - if not _BACKEND.fullmatch(backend): - raise ValueError("--backend must be a lowercase backend name") - chosen = list(platforms or []) - if any(item not in PLATFORMS for item in chosen): - raise ValueError("--platform takes one of " + ", ".join(PLATFORMS)) - grant = { - "schema": _SCHEMA, "project_root": str(root), - "checkout_identity": _checkout_identity(root), - "upstream": str(upstream.resolve()), - # Record the interpreter path as given. Resolving the symlink in a venv - # bin/ lands on the base interpreter, which loses every installed - # dependency -- the probe would pass and every later search would fail. - "python": str(interpreter.absolute()), - "backend": backend, "platforms": chosen, - } - info = _run(grant, "discover", {}, timeout=30.0) - path = _grant_path(root) - path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) - with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as output: - pending = Path(output.name) - try: - output.write(_json(grant)) - output.close() - pending.replace(path) - finally: - pending.unlink(missing_ok=True) - return {"enabled": True, "revision": info.get("revision"), "cards": info.get("cards")} - - -def disable() -> dict: - _grant_path(project_root()).unlink(missing_ok=True) - return {"enabled": False, "reason": "cannbot knowledge is not enabled for this project"} +"""Compatibility exports for :mod:`inferrsi.apps.integrations.cannbot.project`.""" + +from ..integrations.cannbot.project import ( + MAX_BYTES, + PLATFORMS, + ChannelFailure, + describe, + disable, + enable, + load_grant, + project_root, + read, + search, +) + +__all__ = [ + "MAX_BYTES", + "PLATFORMS", + "ChannelFailure", + "project_root", + "load_grant", + "describe", + "search", + "read", + "enable", + "disable", +] diff --git a/src/inferrsi/apps/cannbot/read_worker.py b/src/inferrsi/apps/cannbot/read_worker.py index 0ff4055d4..a2a8bfa0d 100644 --- a/src/inferrsi/apps/cannbot/read_worker.py +++ b/src/inferrsi/apps/cannbot/read_worker.py @@ -1,369 +1,5 @@ -"""Call the approved cannbot-knowledge checkout with its own interpreter. +"""Compatibility exports for :mod:`inferrsi.apps.integrations.cannbot.read_worker`.""" -Run this worker with the interpreter recorded in the project grant: upstream -requires the MCP SDK constraints and PyYAML from its own environment, and this -program must not import the upstream package into the InferRSI process. Nothing -here writes to the upstream tree; the query CLI is read-only except for an -optional receipt, which is not requested. -""" +from ..integrations.cannbot.read_worker import OutputLimitError, capture, discover, read, search -import hashlib -import json -import math -import os -import queue -import re -import signal -import subprocess -import sys -import threading -import time -from pathlib import Path - -_SCRIPT = ".agents/skills/knowledge-query/scripts/knowledge_query.py" -_INDEX = ".agents/skills/knowledge-query/scripts/knowledge_index.py" -_STALE_MARKERS = ("stale", "missing", "rebuild", "index build", "inconsistent") -_FIX_BUILD = ( - "cd && python3 " + _INDEX + " build" -) -# Upstream output is untrusted input. Bound it while reading, not after the -# whole stream is already in memory. -_MAX_UPSTREAM = 2 * 1024 * 1024 - - -def _fail(message, *, state="rejected", fix=""): - print(json.dumps({"isError": True, "error": message, "state": state, "fix": fix})) - raise SystemExit(1) - - -def _object(pairs): - value = {} - for key, item in pairs: - if key in value: - raise ValueError("cannbot source JSON has duplicate keys") - value[key] = item - return value - - -def _invalid(_): - raise ValueError("cannbot source JSON has a non-finite number") - - -def _finite(value): - number = float(value) - if not math.isfinite(number): - _invalid(value) - return number - - -def _strict(raw): - """Reject duplicate keys and non-finite numbers at the source boundary. - - `json.loads` collapses a duplicate key before any caller can see it, so a - later re-serialisation cannot repair a document that was ambiguous on - arrival. The check has to run on the upstream bytes themselves. - """ - try: - value = json.loads( - raw, object_pairs_hook=_object, parse_constant=_invalid, parse_float=_finite, - ) - except (ValueError, UnicodeError, RecursionError): - _fail("cannbot returned malformed JSON", state="rejected") - if not isinstance(value, dict): - _fail("cannbot did not return a JSON object", state="rejected") - return value - - -class OutputLimitError(ValueError): - """A pipe exceeded the bounded capture size.""" - - -def _stop(process, *, own_group): - if own_group and os.name == "posix": - try: - os.killpg(process.pid, signal.SIGKILL) - except ProcessLookupError: - pass - elif own_group and os.name == "nt" and process.poll() is None: - subprocess.run( - ["taskkill", "/PID", str(process.pid), "/T", "/F"], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2, check=False, - ) - if process.poll() is None: - process.kill() - process.wait(timeout=2) - - -def capture(argv, *, cwd, timeout, environment=None, request=None, own_group=False): - """Capture at most 2 MiB + 1 per pipe, with no unbounded communicate(). - - The outer worker owns a process group; upstream queries inherit it. Both - the caller's cancellation and this function's finally block reap that group. - Each pipe is drained concurrently so a full stderr cannot deadlock stdout. - """ - deadline = time.monotonic() + timeout - - def start(): - return subprocess.Popen( - argv, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - cwd=cwd, env=environment, start_new_session=own_group and os.name == "posix", - creationflags=(subprocess.CREATE_NEW_PROCESS_GROUP - if own_group and os.name == "nt" else 0), - ) - - def stop(process): - _stop(process, own_group=own_group) - - process = request.start(start, stop) if request else start() - received = queue.Queue(maxsize=2) - - def drain(name, stream): - try: - with stream: - received.put((name, stream.read(_MAX_UPSTREAM + 1))) - except OSError: - received.put((name, b"")) - - for name, stream in (("stdout", process.stdout), ("stderr", process.stderr)): - threading.Thread(target=drain, args=(name, stream), daemon=True).start() - try: - output = {} - for _ in range(2): - try: - name, raw = received.get(timeout=max(0, deadline - time.monotonic())) - except queue.Empty: - raise subprocess.TimeoutExpired(argv, timeout) from None - if len(raw) > _MAX_UPSTREAM: - raise OutputLimitError("cannbot output exceeded 2 MiB") - output[name] = raw - code = process.wait(timeout=max(0, deadline - time.monotonic())) - return subprocess.CompletedProcess(argv, code, output["stdout"], output["stderr"]) - finally: - if request: - request.finish(process) - else: - stop(process) - - -def _run(upstream, script, argv, *, timeout): - try: - result = capture( - [sys.executable, "-I", "-B", str(upstream / script), *argv], - cwd=str(upstream), timeout=timeout, - ) - except OutputLimitError: - _fail("cannbot output exceeded 2 MiB") - if result.returncode: - lowered = result.stderr.decode("utf-8", "replace").casefold() - state = "index_stale" if any(m in lowered for m in _STALE_MARKERS) else "rejected" - # Never relay arbitrary upstream stderr: it may contain private paths. - _fail("cannbot index needs rebuilding" if state == "index_stale" - else "cannbot query failed", state=state, - fix=_FIX_BUILD if state == "index_stale" else "") - return _strict(result.stdout) - - -def _index(payload): - index = payload.get("index") - if not isinstance(index, dict): - _fail("cannbot response needs an index object") - state = index.get("built_repository_state") - if not isinstance(state, dict) or not isinstance(state.get("commit"), str): - _fail("cannbot response needs an index revision") - if not re.fullmatch(r"[0-9a-f]{40}", state["commit"]): - _fail("cannbot response has an invalid index revision") - return index, state - - -def discover(upstream, *, timeout=30): - payload = _run(upstream, _SCRIPT, ["discover", "--json"], timeout=timeout) - if payload.get("schema_version") != "knowledge-discovery.v1": - _fail("Unsupported cannbot discovery schema", state="version_mismatch") - if type(payload.get("cards")) is not int or payload["cards"] < 0: - _fail("cannbot discovery needs a non-negative card count") - index, state = _index(payload) - return { - "state": "ok", - "revision": state.get("commit"), - "cards": payload.get("cards"), - "domains": payload.get("domains", {}), - "index": { - "schema": index.get("schema_version"), - "version": index.get("version") or index.get("index_version"), - "content_fingerprint": index.get("content_fingerprint"), - }, - } - - -def _card_bytes(upstream, locator): - root = (upstream / "knowledge").resolve() - card = (root / (locator + ".md")).resolve() - if not card.is_relative_to(root) or not card.is_file(): - _fail("No such card in the approved checkout") - with card.open("rb") as source: - raw = source.read(_MAX_UPSTREAM + 1) - if len(raw) > _MAX_UPSTREAM: - _fail("cannbot card exceeded 2 MiB") - return raw - - -def _source_fingerprint(upstream): - # The query index hashes parsed metadata + body, unlike MCP read_card's - # raw-byte digest. Use the approved source's own implementation; never copy - # its parser or fingerprint algorithm into this package. - sys.path.insert(0, str((upstream / _SCRIPT).parent)) - try: - from retrieval.index import card_fingerprint, load_contract - - _, split_frontmatter = load_contract(upstream) - except (ImportError, AttributeError): - _fail("Unsupported cannbot card identity API", state="version_mismatch") - return card_fingerprint, split_frontmatter - - -def search(upstream, arguments, *, timeout=60): - payload = _run(upstream, _SCRIPT, [ - "search", "--json", "--query", arguments["query"], - "--platform", arguments["platform"], "--limit", str(arguments["limit"]), - ], timeout=timeout) - if payload.get("schema_version") != "knowledge-search.v5": - _fail("Unsupported cannbot search schema", state="version_mismatch") - rows = payload.get("results") - if not isinstance(rows, list): - _fail("cannbot search needs a results list") - # Bound card verification too, rather than trimming only the final output. - rows = rows[:arguments["limit"]] - hits = [] - if rows: - fingerprint, split_frontmatter = _source_fingerprint(upstream) - for row in rows: - if not isinstance(row, dict): - _fail("cannbot search result must be an object") - locator, digest = row.get("path"), row.get("card_sha256") - if (not isinstance(locator, str) or not locator or locator.startswith("/") - or "\\" in locator or any(p in ("", ".", "..") for p in locator.split("/"))): - _fail("cannbot search result has an invalid locator") - if not isinstance(digest, str) or not re.fullmatch(r"[0-9a-f]{64}", digest): - _fail("cannbot search result needs a card digest") - score = row.get("score") - if type(score) not in (int, float) or not math.isfinite(score): - _fail("cannbot search result needs a finite score") - if any(row.get(key) is not None and not isinstance(row[key], str) - for key in ("title", "description", "status")): - _fail("cannbot search result has invalid text fields") - platforms = row.get("platforms") - if platforms is not None and (not isinstance(platforms, list) - or any(not isinstance(p, str) for p in platforms)): - _fail("cannbot search result has an invalid platform list") - raw = _card_bytes(upstream, locator) - metadata, body = split_frontmatter(raw.decode("utf-8")) - if fingerprint(metadata, body) != digest: - _fail("cannbot card differs from its search index", state="index_stale", - fix=_FIX_BUILD) - hits.append({ - "locator": row.get("path"), - "title": row.get("title") or (row.get("path") or "").rsplit("/", 1)[-1], - "summary": row.get("description") or "", - "score": row.get("score") or 0, - "status": row.get("status") or "", - "platforms": row.get("platforms") or [], - "source_card_sha256": digest, - "card_sha256": hashlib.sha256(raw).hexdigest(), - }) - index, state = _index(payload) - # The pin lives on the index provenance, not at the top level: that is the - # revision the index was built from, which is what a citation must name. - return { - "state": "ok", - "revision": state.get("commit"), - "index": { - "schema": index.get("schema_version"), - "version": index.get("index_version"), - "content_fingerprint": index.get("content_fingerprint"), - }, - "score_scale": payload.get("schema_version") or "cannbot", - "items": hits, - } - - -def _revision(upstream, *, timeout): - # Inherited repository-selection variables must not redirect this check. - environment = {key: value for key, value in os.environ.items() - if not key.startswith("GIT_")} - try: - result = capture( - ["git", "-C", str(upstream), "rev-parse", "--verify", "HEAD"], - cwd=str(upstream), timeout=timeout, environment=environment, - ) - except OutputLimitError: - _fail("cannbot revision output exceeded 2 MiB") - revision = result.stdout.decode("ascii", "replace").strip() - if result.returncode or not re.fullmatch(r"[0-9a-f]{40}", revision): - _fail("Cannot determine the approved cannbot checkout revision", - state="version_mismatch") - return revision - - -def read(upstream, arguments, *, timeout=15): - deadline = time.monotonic() + timeout - revision = _revision(upstream, timeout=max(0, deadline - time.monotonic())) - expected_revision = arguments.get("expected_revision") - if expected_revision and revision != expected_revision: - _fail("The cannbot revision changed since the search; search again", - state="version_mismatch") - locator = arguments["locator"] - body = _card_bytes(upstream, locator) - if _revision(upstream, timeout=max(0, deadline - time.monotonic())) != revision: - _fail("The cannbot revision changed during the read; search again", - state="version_mismatch") - digest = hashlib.sha256(body).hexdigest() - # Search reports what it matched; read reports what it actually returned. - # Without both, a card edited between the two is served under the old - # locator and a citation names bytes nobody verified. A caller that passed - # an expected digest gets a refusal instead of silent drift. - expected = arguments.get("expected_sha256") - if expected and expected != digest: - _fail( - "The card changed since the search that found it " - f"(expected {expected[:12]}…, read {digest[:12]}…); search again", - state="rejected", - ) - lines = body.decode("utf-8").splitlines() - size, page = 200, arguments.get("page", 1) - start = (page - 1) * size - chunk = lines[start:start + size] - return { - "state": "ok", - "locator": locator, - "revision": revision, - "page": page, - "pages": max(1, (len(lines) + size - 1) // size), - "line_count": len(lines), - "card_sha256": digest, - "text": "\n".join(chunk), - "has_more": start + size < len(lines), - } - - -if __name__ == "__main__": - try: - upstream = Path(sys.argv[1]).resolve() - action, raw = sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else "{}" - arguments = _strict(raw or "{}") - timeout = float(sys.argv[4]) if len(sys.argv) > 4 else 30.0 - if action == "discover": - result = discover(upstream, timeout=timeout) - elif action == "search": - result = search(upstream, arguments, timeout=timeout) - elif action == "read": - result = read(upstream, arguments, timeout=timeout) - else: - _fail("Unknown cannbot read action") - except SystemExit: - raise - except subprocess.TimeoutExpired: - _fail("cannbot query timed out", state="timeout") - except (OSError, ValueError, KeyError, TypeError): - # Paths and upstream internals must not reach the caller. - _fail("The local cannbot checkout is unavailable", state="unreachable") - print(json.dumps(result, ensure_ascii=True, allow_nan=False)) +__all__ = ["OutputLimitError", "capture", "discover", "search", "read"] diff --git a/src/inferrsi/apps/cli/__init__.py b/src/inferrsi/apps/cli/__init__.py index 454f239eb..177acd163 100644 --- a/src/inferrsi/apps/cli/__init__.py +++ b/src/inferrsi/apps/cli/__init__.py @@ -154,7 +154,7 @@ def _channels(): dropping it from the list. """ from ...knowledge.channels import ChannelRegistry - from ..cannbot import CannbotChannel + from ..integrations.cannbot import CannbotChannel channel = CannbotChannel() return ChannelRegistry([channel], declared_backend=channel.declared_backend) @@ -302,14 +302,14 @@ def main(argv: list[str] | None = None) -> int: _print(delivery) return 0 if delivery["status"] == "published" else 2 if args.command == "mindie": - from ..mindie.project import dispatch + from ..integrations.mindie.project import dispatch result = dispatch(args) if result is not None: _print(result) return 0 if args.command == "cannbot": - from .. import cannbot + from ..integrations import cannbot action = args.cannbot_action if action == "enable": @@ -618,7 +618,7 @@ def main(argv: list[str] | None = None) -> int: if args.command == "campaign": if args.campaign_action == "import-mindie": - from ..mindie.context import export_context + from ..integrations.mindie.context import export_context path = export_context( args.directory, context_id=args.id, query_input=args.query_input, diff --git a/src/inferrsi/apps/integrations/__init__.py b/src/inferrsi/apps/integrations/__init__.py new file mode 100644 index 000000000..86e19b7da --- /dev/null +++ b/src/inferrsi/apps/integrations/__init__.py @@ -0,0 +1 @@ +"""Application adapters for explicitly authorized external systems.""" diff --git a/src/inferrsi/apps/integrations/cannbot/__init__.py b/src/inferrsi/apps/integrations/cannbot/__init__.py new file mode 100644 index 000000000..63224413a --- /dev/null +++ b/src/inferrsi/apps/integrations/cannbot/__init__.py @@ -0,0 +1,107 @@ +"""The cannbot-knowledge channel: an external read-only corpus beside ours.""" + +from __future__ import annotations + +import time +from pathlib import Path + +from ....knowledge.channels import ( + ChannelHit, + ChannelRequest, + ChannelResult, + ChannelStatus, +) +from . import project + +__all__ = ["CannbotChannel", "project"] + +SCORE_SCALE = "cannbot/knowledge-search.v5" + + +class CannbotChannel: + """Approved cannbot-knowledge checkout, queried through its own interpreter.""" + + def __init__(self, *, root: Path | None = None, timeout: float = 8.0): + from . import project + + self._project = project + self.root = root + self.timeout = timeout + # `hint` records why this channel cannot answer before any search runs. + # Keeping it lets `find` report "not enabled" instead of telling the + # caller to declare a backend for a source that is not approved yet. + self.hint: ChannelStatus | None = None + try: + grant = project.load_grant(root) + except project.ChannelFailure as exc: + grant = {} + self.hint = ChannelStatus(exc.state, detail=str(exc), fix=exc.fix) + except (OSError, ValueError) as exc: + grant = {} + self.hint = ChannelStatus("rejected", detail=str(exc)) + self.backend = "ascend" + self.declared_backend = grant.get("backend") + self.name = "cannbot-knowledge" + + def describe(self, *, request: ChannelRequest) -> ChannelStatus: + try: + info = self._project.describe(self.root, timeout=request.remaining(), request=request) + except self._project.ChannelFailure as exc: + return ChannelStatus(exc.state, detail=str(exc), fix=exc.fix) + state = info.get("state", "rejected") + if state != "ok": + return ChannelStatus( + state if state != "rejected" else "rejected", + detail=str(info.get("detail") or ""), fix=str(info.get("fix") or ""), + ) + return ChannelStatus("ok", revision=info.get("revision")) + + def search( + self, query: str, *, platform: str | None, limit: int, request: ChannelRequest + ) -> ChannelResult: + started = time.monotonic() + try: + payload = self._project.search( + query, platform=platform, limit=limit, root=self.root, + timeout=request.remaining(), request=request, + ) + except self._project.ChannelFailure as exc: + return ChannelResult( + name=self.name, status=ChannelStatus( + exc.state, detail=str(exc), fix=exc.fix, + ), + elapsed_ms=max(1, int((time.monotonic() - started) * 1000)), + ) + elapsed = int((time.monotonic() - started) * 1000) + items = tuple( + ChannelHit( + locator=str(row.get("locator") or ""), + title=str(row.get("title") or ""), + summary=str(row.get("summary") or ""), + score=float(row.get("score") or 0), + score_scale=str(payload.get("score_scale") or SCORE_SCALE), + status=str(row.get("status") or ""), + platforms=tuple(row.get("platforms") or ()), + card_sha256=str(row.get("card_sha256") or ""), + source_card_sha256=str(row.get("source_card_sha256") or ""), + ) + for row in payload.get("items") or [] + if row.get("locator") + ) + return ChannelResult( + name=self.name, + status=ChannelStatus("ok", revision=payload.get("revision")), + items=items, + elapsed_ms=elapsed, + score_scale=str(payload.get("score_scale") or SCORE_SCALE), + read_prefix="inferrsi cannbot read", + ) + + def read(self, locator: str, *, expected_sha256: str | None = None, + expected_revision: str | None = None) -> str: + payload = self._project.read( + locator, expected_sha256=expected_sha256, root=self.root, + expected_revision=expected_revision, + timeout=max(self.timeout, 15.0), + ) + return str(payload.get("text") or "") diff --git a/src/inferrsi/apps/integrations/cannbot/project.py b/src/inferrsi/apps/integrations/cannbot/project.py new file mode 100644 index 000000000..7386f0499 --- /dev/null +++ b/src/inferrsi/apps/integrations/cannbot/project.py @@ -0,0 +1,283 @@ +"""An opt-in, per-checkout grant for an external cannbot-knowledge checkout. + +The upstream corpus is CANN-licensed and stays where it is. This module records +only which checkout, interpreter and default platform this project approved; it +never copies knowledge into the repository, the wheel or an evidence tree. +""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import subprocess +import tempfile +from pathlib import Path + +from .read_worker import OutputLimitError, _finite, capture + +_SCHEMA = "inferrsi-cannbot-project/1" +_WORKER = Path(__file__).with_name("read_worker.py") +MAX_BYTES = 2 * 1024 * 1024 +_BACKEND = re.compile(r"[a-z][a-z0-9]{0,31}") +# Upstream platform values this adapter will pass through. An unlisted value is +# refused rather than forwarded: the upstream CLI is fail-closed about platform +# and a typo there would silently change which cards answer. +PLATFORMS = ("a2", "a3", "950", "310p", "910", "kirin_x90", "kirin_9030", "agnostic") + + +class ChannelFailure(ValueError): + """A channel-level failure carrying the state the caller should report.""" + + def __init__(self, message, *, state="rejected", fix="", exit_code=None): + super().__init__(message) + self.state = state + self.fix = fix + self.exit_code = exit_code + + +def project_root() -> Path: + cwd = Path.cwd().resolve() + for folder in (cwd, *cwd.parents): + if (folder / ".git").exists(): + return folder + raise ChannelFailure( + "Open a Git project to use project-scoped cannbot knowledge", + state="not_enabled", fix="Open a Git project, then enable cannbot knowledge", + ) + + +def _grant_path(root: Path) -> Path: + project_id = hashlib.sha256(os.fsencode(root)).hexdigest() + return Path.home() / ".config" / "inferrsi" / "cannbot-projects" / (project_id + ".json") + + +def _checkout_identity(root: Path) -> dict: + marker = root / ".git" + info = marker.stat() + identity = {"device": info.st_dev, "inode": info.st_ino} + if hasattr(info, "st_birthtime"): + identity["created"] = info.st_birthtime + return identity + + +def _json(value) -> bytes: + return (json.dumps(value, ensure_ascii=True, allow_nan=False) + "\n").encode("utf-8") + + +def _parse(raw: bytes) -> dict: + def _object(pairs): + value = {} + for key, item in pairs: + if key in value: + raise ValueError("cannbot response contains duplicate JSON keys") + value[key] = item + return value + + def _invalid(_): + raise ValueError("cannbot response contains a non-finite JSON value") + + try: + value = json.loads(raw, object_pairs_hook=_object, parse_constant=_invalid, + parse_float=_finite) + except (ValueError, UnicodeError, RecursionError) as exc: + raise ValueError("cannbot did not return a JSON object") from exc + if not isinstance(value, dict): + raise ValueError("cannbot did not return a JSON object") + return value + + +def load_grant(root: Path | None = None) -> dict: + root = project_root() if root is None else root + path = _grant_path(root) + try: + info = path.stat() + if (path.is_symlink() or not path.is_file() + or os.name == "posix" and (info.st_uid != os.getuid() or info.st_mode & 0o077)): + raise ValueError("The cannbot project grant must be a private local file") + with path.open("rb") as source: + raw = source.read(MAX_BYTES + 1) + if len(raw) > MAX_BYTES: + raise ValueError("The cannbot project grant exceeds 2 MiB") + grant = _parse(raw) + checkout = _checkout_identity(root) + except FileNotFoundError: + raise ChannelFailure( + "cannbot knowledge is not enabled for this project", state="not_enabled", + fix="inferrsi cannbot enable --checkout --python ", + ) from None + except OSError: + raise ChannelFailure("The cannbot project grant could not be read", state="not_enabled") \ + from None + if (grant.get("schema") != _SCHEMA or grant.get("project_root") != str(root) + or grant.get("checkout_identity") != checkout + or not isinstance(grant.get("upstream"), str) + or not Path(grant["upstream"]).is_absolute() + or not isinstance(grant.get("python"), str) + or not Path(grant["python"]).is_absolute() + or not isinstance(grant.get("backend"), str) + or not _BACKEND.fullmatch(grant["backend"])): + raise ChannelFailure("The cannbot grant does not match this project", state="rejected") + platforms = grant.get("platforms", []) + if not isinstance(platforms, list) or any(item not in PLATFORMS for item in platforms): + raise ChannelFailure("The cannbot grant has an invalid platform list", state="rejected") + return grant + + +def _run(grant: dict, action: str, arguments: dict, *, timeout: float, request=None) -> dict: + environment = os.environ.copy() + for key in ("CANNBOT_MCP_TOKENS", "CANNBOT_KNOWLEDGE_PLATFORM"): + environment.pop(key, None) + try: + with tempfile.TemporaryDirectory(prefix="inferrsi-cannbot-read-") as temporary: + cwd = Path(temporary) / "reader" + cwd.mkdir(mode=0o700) + result = capture( + [grant["python"], "-I", "-B", str(_WORKER), + grant["upstream"], action, json.dumps(arguments), str(timeout)], + cwd=cwd, environment=environment, timeout=timeout, request=request, + own_group=True, + ) + except subprocess.TimeoutExpired: + raise ChannelFailure( + f"cannbot did not answer within {timeout:g}s", state="timeout" + ) from None + except OutputLimitError: + raise ChannelFailure("cannbot output exceeded 2 MiB", state="rejected") from None + except OSError: + raise ChannelFailure( + "The approved cannbot reader could not be started", state="unreachable" + ) from None + if len(result.stdout) > MAX_BYTES: + raise ChannelFailure("cannbot returned more than 2 MiB", state="rejected") + try: + payload = _parse(result.stdout) + except ValueError: + raise ChannelFailure("cannbot returned invalid JSON", state="rejected") from None + if result.returncode or payload.get("isError"): + raise ChannelFailure( + str(payload.get("error") or "The approved cannbot reader failed"), + state=str(payload.get("state") or "rejected"), + fix=str(payload.get("fix") or ""), + exit_code=result.returncode, + ) + return payload + + +def describe(root: Path | None = None, *, timeout: float = 15.0, request=None) -> dict: + try: + grant = load_grant(root) + except ChannelFailure as exc: + return {"state": exc.state, "detail": str(exc), "fix": exc.fix} + try: + payload = _run(grant, "discover", {}, timeout=timeout, request=request) + except ChannelFailure as exc: + return {"state": exc.state, "detail": str(exc), "fix": exc.fix} + return { + "state": "ok", + "revision": payload.get("revision"), + "cards": payload.get("cards"), + "index": payload.get("index", {}), + "platforms": list(grant.get("platforms", [])), + } + + +def search( + query: str, *, platform: str | None = None, limit: int = 100, + root: Path | None = None, timeout: float = 8.0, request=None, +) -> dict: + if not isinstance(query, str) or not query.strip(): + raise ChannelFailure("cannbot search needs a non-empty query") + if type(limit) is not int or not 1 <= limit <= 200: + raise ChannelFailure("cannbot limit must be an integer from 1 through 200") + grant = load_grant(root) + platforms = list(grant.get("platforms", [])) + if platform is None: + if not platforms: + raise ChannelFailure( + "cannbot search needs a platform", state="rejected", + fix="inferrsi find --backend ascend --platform ", + ) + platform = platforms[0] + if platform not in PLATFORMS: + raise ChannelFailure(f"cannbot does not know platform {platform!r}", state="rejected") + return _run(grant, "search", { + "query": query, "platform": platform, "limit": limit, + }, timeout=timeout, request=request) + + +def read(locator: str, *, page: int = 1, expected_sha256: str | None = None, + expected_revision: str | None = None, + root: Path | None = None, timeout: float = 15.0) -> dict: + if not isinstance(locator, str) or not locator.strip(): + raise ChannelFailure("cannbot read needs a locator from a search result") + if type(page) is not int or page < 1: + raise ChannelFailure("cannbot page must be a positive integer") + if expected_sha256 is not None and ( + not isinstance(expected_sha256, str) or not re.fullmatch(r"[0-9a-f]{64}", expected_sha256) + ): + raise ChannelFailure("cannbot expected_sha256 must be a lowercase 64-hex digest") + if expected_revision is not None and ( + not isinstance(expected_revision, str) + or not re.fullmatch(r"[0-9a-f]{40}", expected_revision) + ): + raise ChannelFailure("cannbot expected_revision must be a full lowercase Git revision") + arguments = {"locator": locator, "page": page} + if expected_sha256: + arguments["expected_sha256"] = expected_sha256 + if expected_revision: + arguments["expected_revision"] = expected_revision + return _run(load_grant(root), "read", arguments, timeout=timeout) + + +def enable(*, upstream: Path, python: Path, backend: str = "ascend", + platforms: list[str] | None = None) -> dict: + """Approve one checkout for this project after proving it answers. + + The probe is deliberate: a grant that records a checkout which cannot answer + would turn every later search into a failure the user has to debug. + """ + root = project_root() + upstream = Path(upstream).expanduser() + if not (upstream / ".git").exists(): + raise ValueError("--checkout must be a Git checkout of cannbot-knowledge") + if not (upstream / "knowledge").is_dir(): + raise ValueError("--checkout must contain the knowledge/ bundle root") + if not (upstream / ".agents/skills/knowledge-query/scripts/knowledge_query.py").is_file(): + raise ValueError("--checkout is missing the knowledge-query scripts") + interpreter = Path(python).expanduser() + if not interpreter.is_file() or not os.access(interpreter, os.X_OK): + raise ValueError("--python must be an executable interpreter with pyyaml installed") + if not _BACKEND.fullmatch(backend): + raise ValueError("--backend must be a lowercase backend name") + chosen = list(platforms or []) + if any(item not in PLATFORMS for item in chosen): + raise ValueError("--platform takes one of " + ", ".join(PLATFORMS)) + grant = { + "schema": _SCHEMA, "project_root": str(root), + "checkout_identity": _checkout_identity(root), + "upstream": str(upstream.resolve()), + # Record the interpreter path as given. Resolving the symlink in a venv + # bin/ lands on the base interpreter, which loses every installed + # dependency -- the probe would pass and every later search would fail. + "python": str(interpreter.absolute()), + "backend": backend, "platforms": chosen, + } + info = _run(grant, "discover", {}, timeout=30.0) + path = _grant_path(root) + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as output: + pending = Path(output.name) + try: + output.write(_json(grant)) + output.close() + pending.replace(path) + finally: + pending.unlink(missing_ok=True) + return {"enabled": True, "revision": info.get("revision"), "cards": info.get("cards")} + + +def disable() -> dict: + _grant_path(project_root()).unlink(missing_ok=True) + return {"enabled": False, "reason": "cannbot knowledge is not enabled for this project"} diff --git a/src/inferrsi/apps/integrations/cannbot/read_worker.py b/src/inferrsi/apps/integrations/cannbot/read_worker.py new file mode 100644 index 000000000..0ff4055d4 --- /dev/null +++ b/src/inferrsi/apps/integrations/cannbot/read_worker.py @@ -0,0 +1,369 @@ +"""Call the approved cannbot-knowledge checkout with its own interpreter. + +Run this worker with the interpreter recorded in the project grant: upstream +requires the MCP SDK constraints and PyYAML from its own environment, and this +program must not import the upstream package into the InferRSI process. Nothing +here writes to the upstream tree; the query CLI is read-only except for an +optional receipt, which is not requested. +""" + +import hashlib +import json +import math +import os +import queue +import re +import signal +import subprocess +import sys +import threading +import time +from pathlib import Path + +_SCRIPT = ".agents/skills/knowledge-query/scripts/knowledge_query.py" +_INDEX = ".agents/skills/knowledge-query/scripts/knowledge_index.py" +_STALE_MARKERS = ("stale", "missing", "rebuild", "index build", "inconsistent") +_FIX_BUILD = ( + "cd && python3 " + _INDEX + " build" +) +# Upstream output is untrusted input. Bound it while reading, not after the +# whole stream is already in memory. +_MAX_UPSTREAM = 2 * 1024 * 1024 + + +def _fail(message, *, state="rejected", fix=""): + print(json.dumps({"isError": True, "error": message, "state": state, "fix": fix})) + raise SystemExit(1) + + +def _object(pairs): + value = {} + for key, item in pairs: + if key in value: + raise ValueError("cannbot source JSON has duplicate keys") + value[key] = item + return value + + +def _invalid(_): + raise ValueError("cannbot source JSON has a non-finite number") + + +def _finite(value): + number = float(value) + if not math.isfinite(number): + _invalid(value) + return number + + +def _strict(raw): + """Reject duplicate keys and non-finite numbers at the source boundary. + + `json.loads` collapses a duplicate key before any caller can see it, so a + later re-serialisation cannot repair a document that was ambiguous on + arrival. The check has to run on the upstream bytes themselves. + """ + try: + value = json.loads( + raw, object_pairs_hook=_object, parse_constant=_invalid, parse_float=_finite, + ) + except (ValueError, UnicodeError, RecursionError): + _fail("cannbot returned malformed JSON", state="rejected") + if not isinstance(value, dict): + _fail("cannbot did not return a JSON object", state="rejected") + return value + + +class OutputLimitError(ValueError): + """A pipe exceeded the bounded capture size.""" + + +def _stop(process, *, own_group): + if own_group and os.name == "posix": + try: + os.killpg(process.pid, signal.SIGKILL) + except ProcessLookupError: + pass + elif own_group and os.name == "nt" and process.poll() is None: + subprocess.run( + ["taskkill", "/PID", str(process.pid), "/T", "/F"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2, check=False, + ) + if process.poll() is None: + process.kill() + process.wait(timeout=2) + + +def capture(argv, *, cwd, timeout, environment=None, request=None, own_group=False): + """Capture at most 2 MiB + 1 per pipe, with no unbounded communicate(). + + The outer worker owns a process group; upstream queries inherit it. Both + the caller's cancellation and this function's finally block reap that group. + Each pipe is drained concurrently so a full stderr cannot deadlock stdout. + """ + deadline = time.monotonic() + timeout + + def start(): + return subprocess.Popen( + argv, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + cwd=cwd, env=environment, start_new_session=own_group and os.name == "posix", + creationflags=(subprocess.CREATE_NEW_PROCESS_GROUP + if own_group and os.name == "nt" else 0), + ) + + def stop(process): + _stop(process, own_group=own_group) + + process = request.start(start, stop) if request else start() + received = queue.Queue(maxsize=2) + + def drain(name, stream): + try: + with stream: + received.put((name, stream.read(_MAX_UPSTREAM + 1))) + except OSError: + received.put((name, b"")) + + for name, stream in (("stdout", process.stdout), ("stderr", process.stderr)): + threading.Thread(target=drain, args=(name, stream), daemon=True).start() + try: + output = {} + for _ in range(2): + try: + name, raw = received.get(timeout=max(0, deadline - time.monotonic())) + except queue.Empty: + raise subprocess.TimeoutExpired(argv, timeout) from None + if len(raw) > _MAX_UPSTREAM: + raise OutputLimitError("cannbot output exceeded 2 MiB") + output[name] = raw + code = process.wait(timeout=max(0, deadline - time.monotonic())) + return subprocess.CompletedProcess(argv, code, output["stdout"], output["stderr"]) + finally: + if request: + request.finish(process) + else: + stop(process) + + +def _run(upstream, script, argv, *, timeout): + try: + result = capture( + [sys.executable, "-I", "-B", str(upstream / script), *argv], + cwd=str(upstream), timeout=timeout, + ) + except OutputLimitError: + _fail("cannbot output exceeded 2 MiB") + if result.returncode: + lowered = result.stderr.decode("utf-8", "replace").casefold() + state = "index_stale" if any(m in lowered for m in _STALE_MARKERS) else "rejected" + # Never relay arbitrary upstream stderr: it may contain private paths. + _fail("cannbot index needs rebuilding" if state == "index_stale" + else "cannbot query failed", state=state, + fix=_FIX_BUILD if state == "index_stale" else "") + return _strict(result.stdout) + + +def _index(payload): + index = payload.get("index") + if not isinstance(index, dict): + _fail("cannbot response needs an index object") + state = index.get("built_repository_state") + if not isinstance(state, dict) or not isinstance(state.get("commit"), str): + _fail("cannbot response needs an index revision") + if not re.fullmatch(r"[0-9a-f]{40}", state["commit"]): + _fail("cannbot response has an invalid index revision") + return index, state + + +def discover(upstream, *, timeout=30): + payload = _run(upstream, _SCRIPT, ["discover", "--json"], timeout=timeout) + if payload.get("schema_version") != "knowledge-discovery.v1": + _fail("Unsupported cannbot discovery schema", state="version_mismatch") + if type(payload.get("cards")) is not int or payload["cards"] < 0: + _fail("cannbot discovery needs a non-negative card count") + index, state = _index(payload) + return { + "state": "ok", + "revision": state.get("commit"), + "cards": payload.get("cards"), + "domains": payload.get("domains", {}), + "index": { + "schema": index.get("schema_version"), + "version": index.get("version") or index.get("index_version"), + "content_fingerprint": index.get("content_fingerprint"), + }, + } + + +def _card_bytes(upstream, locator): + root = (upstream / "knowledge").resolve() + card = (root / (locator + ".md")).resolve() + if not card.is_relative_to(root) or not card.is_file(): + _fail("No such card in the approved checkout") + with card.open("rb") as source: + raw = source.read(_MAX_UPSTREAM + 1) + if len(raw) > _MAX_UPSTREAM: + _fail("cannbot card exceeded 2 MiB") + return raw + + +def _source_fingerprint(upstream): + # The query index hashes parsed metadata + body, unlike MCP read_card's + # raw-byte digest. Use the approved source's own implementation; never copy + # its parser or fingerprint algorithm into this package. + sys.path.insert(0, str((upstream / _SCRIPT).parent)) + try: + from retrieval.index import card_fingerprint, load_contract + + _, split_frontmatter = load_contract(upstream) + except (ImportError, AttributeError): + _fail("Unsupported cannbot card identity API", state="version_mismatch") + return card_fingerprint, split_frontmatter + + +def search(upstream, arguments, *, timeout=60): + payload = _run(upstream, _SCRIPT, [ + "search", "--json", "--query", arguments["query"], + "--platform", arguments["platform"], "--limit", str(arguments["limit"]), + ], timeout=timeout) + if payload.get("schema_version") != "knowledge-search.v5": + _fail("Unsupported cannbot search schema", state="version_mismatch") + rows = payload.get("results") + if not isinstance(rows, list): + _fail("cannbot search needs a results list") + # Bound card verification too, rather than trimming only the final output. + rows = rows[:arguments["limit"]] + hits = [] + if rows: + fingerprint, split_frontmatter = _source_fingerprint(upstream) + for row in rows: + if not isinstance(row, dict): + _fail("cannbot search result must be an object") + locator, digest = row.get("path"), row.get("card_sha256") + if (not isinstance(locator, str) or not locator or locator.startswith("/") + or "\\" in locator or any(p in ("", ".", "..") for p in locator.split("/"))): + _fail("cannbot search result has an invalid locator") + if not isinstance(digest, str) or not re.fullmatch(r"[0-9a-f]{64}", digest): + _fail("cannbot search result needs a card digest") + score = row.get("score") + if type(score) not in (int, float) or not math.isfinite(score): + _fail("cannbot search result needs a finite score") + if any(row.get(key) is not None and not isinstance(row[key], str) + for key in ("title", "description", "status")): + _fail("cannbot search result has invalid text fields") + platforms = row.get("platforms") + if platforms is not None and (not isinstance(platforms, list) + or any(not isinstance(p, str) for p in platforms)): + _fail("cannbot search result has an invalid platform list") + raw = _card_bytes(upstream, locator) + metadata, body = split_frontmatter(raw.decode("utf-8")) + if fingerprint(metadata, body) != digest: + _fail("cannbot card differs from its search index", state="index_stale", + fix=_FIX_BUILD) + hits.append({ + "locator": row.get("path"), + "title": row.get("title") or (row.get("path") or "").rsplit("/", 1)[-1], + "summary": row.get("description") or "", + "score": row.get("score") or 0, + "status": row.get("status") or "", + "platforms": row.get("platforms") or [], + "source_card_sha256": digest, + "card_sha256": hashlib.sha256(raw).hexdigest(), + }) + index, state = _index(payload) + # The pin lives on the index provenance, not at the top level: that is the + # revision the index was built from, which is what a citation must name. + return { + "state": "ok", + "revision": state.get("commit"), + "index": { + "schema": index.get("schema_version"), + "version": index.get("index_version"), + "content_fingerprint": index.get("content_fingerprint"), + }, + "score_scale": payload.get("schema_version") or "cannbot", + "items": hits, + } + + +def _revision(upstream, *, timeout): + # Inherited repository-selection variables must not redirect this check. + environment = {key: value for key, value in os.environ.items() + if not key.startswith("GIT_")} + try: + result = capture( + ["git", "-C", str(upstream), "rev-parse", "--verify", "HEAD"], + cwd=str(upstream), timeout=timeout, environment=environment, + ) + except OutputLimitError: + _fail("cannbot revision output exceeded 2 MiB") + revision = result.stdout.decode("ascii", "replace").strip() + if result.returncode or not re.fullmatch(r"[0-9a-f]{40}", revision): + _fail("Cannot determine the approved cannbot checkout revision", + state="version_mismatch") + return revision + + +def read(upstream, arguments, *, timeout=15): + deadline = time.monotonic() + timeout + revision = _revision(upstream, timeout=max(0, deadline - time.monotonic())) + expected_revision = arguments.get("expected_revision") + if expected_revision and revision != expected_revision: + _fail("The cannbot revision changed since the search; search again", + state="version_mismatch") + locator = arguments["locator"] + body = _card_bytes(upstream, locator) + if _revision(upstream, timeout=max(0, deadline - time.monotonic())) != revision: + _fail("The cannbot revision changed during the read; search again", + state="version_mismatch") + digest = hashlib.sha256(body).hexdigest() + # Search reports what it matched; read reports what it actually returned. + # Without both, a card edited between the two is served under the old + # locator and a citation names bytes nobody verified. A caller that passed + # an expected digest gets a refusal instead of silent drift. + expected = arguments.get("expected_sha256") + if expected and expected != digest: + _fail( + "The card changed since the search that found it " + f"(expected {expected[:12]}…, read {digest[:12]}…); search again", + state="rejected", + ) + lines = body.decode("utf-8").splitlines() + size, page = 200, arguments.get("page", 1) + start = (page - 1) * size + chunk = lines[start:start + size] + return { + "state": "ok", + "locator": locator, + "revision": revision, + "page": page, + "pages": max(1, (len(lines) + size - 1) // size), + "line_count": len(lines), + "card_sha256": digest, + "text": "\n".join(chunk), + "has_more": start + size < len(lines), + } + + +if __name__ == "__main__": + try: + upstream = Path(sys.argv[1]).resolve() + action, raw = sys.argv[2], sys.argv[3] if len(sys.argv) > 3 else "{}" + arguments = _strict(raw or "{}") + timeout = float(sys.argv[4]) if len(sys.argv) > 4 else 30.0 + if action == "discover": + result = discover(upstream, timeout=timeout) + elif action == "search": + result = search(upstream, arguments, timeout=timeout) + elif action == "read": + result = read(upstream, arguments, timeout=timeout) + else: + _fail("Unknown cannbot read action") + except SystemExit: + raise + except subprocess.TimeoutExpired: + _fail("cannbot query timed out", state="timeout") + except (OSError, ValueError, KeyError, TypeError): + # Paths and upstream internals must not reach the caller. + _fail("The local cannbot checkout is unavailable", state="unreachable") + print(json.dumps(result, ensure_ascii=True, allow_nan=False)) diff --git a/src/inferrsi/apps/integrations/mindie/__init__.py b/src/inferrsi/apps/integrations/mindie/__init__.py new file mode 100644 index 000000000..d4511e2ae --- /dev/null +++ b/src/inferrsi/apps/integrations/mindie/__init__.py @@ -0,0 +1,5 @@ +"""The local MindIE adapter: project grant, stdio MCP, context export and the read worker. + +This initializer imports nothing. The worker file runs under the approved MindIE +interpreter and must stay loadable without this package. +""" diff --git a/src/inferrsi/apps/integrations/mindie/context.py b/src/inferrsi/apps/integrations/mindie/context.py new file mode 100644 index 000000000..7f5377f5c --- /dev/null +++ b/src/inferrsi/apps/integrations/mindie/context.py @@ -0,0 +1,181 @@ +"""Export explicitly supplied MindIE reads for a caller-reviewed campaign trace. + +This CLI host adapter never activates MindIE, reads a session or calls a model. +Retained responses establish supplied bytes, not their authenticity or consumption. +It lives in the Apps package with the rest of the MindIE adapter. +""" + +from __future__ import annotations + +import hashlib +import json +import re +from pathlib import Path + +MAX_BYTES = 2 * 1024 * 1024 +_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}") +_REF = re.compile(r"mindie://([A-Za-z0-9_.-]+)/units/([A-Za-z0-9_.-]+)@([1-9][0-9]*)") + + +def _object(pairs): + value = {} + for key, item in pairs: + if key in value: + raise ValueError("MindIE evidence contains duplicate JSON keys") + value[key] = item + return value + + +def _invalid_constant(_): + raise ValueError("MindIE evidence contains a non-finite JSON value") + + +def _parse(raw): + try: + value = json.loads(raw, object_pairs_hook=_object, parse_constant=_invalid_constant) + except (ValueError, UnicodeError, RecursionError) as exc: + raise ValueError("MindIE evidence is not valid UTF-8 JSON") from exc + if not isinstance(value, dict): + raise ValueError("MindIE evidence must contain a JSON object") + return value + + +def _payload(value, required, locator=""): + status = value.get("status") + if (value.get("isError") is True or value.get("error") + or isinstance(status, str) and status in {"error", "failed", "inactive", "denied"}): + raise ValueError("MindIE call failed or knowledge was not enabled") + if required <= value.keys(): + return value, locator + structured = value.get("structuredContent") + if isinstance(structured, dict): + return _payload(structured, required, locator + "/structuredContent") + blocks = value.get("content") + if isinstance(blocks, list) and len(blocks) == 1 and isinstance(blocks[0], dict): + block = blocks[0] + if block.get("type") == "text" and isinstance(block.get("text"), str): + return _payload(_parse(block["text"]), required, + locator + "/content/0/text (decoded JSON)") + raise ValueError("Unsupported or missing MindIE business payload") + + +def export_context(root: Path, *, context_id: str, query_input: Path, query_result: Path, + explain_input: Path, explain_result: Path, query_exit_code: int, + explain_exit_code: int) -> Path: + """Keep a new read receipt and a trace context fragment without marking review.""" + if not isinstance(context_id, str) or not _ID.fullmatch(context_id): + raise ValueError("MindIE context id must be a safe identifier") + if query_exit_code != 0 or explain_exit_code != 0: + raise ValueError("MindIE calls must both exit successfully") + root = Path(root).resolve() + campaign = _parse((root / "campaign.json").read_bytes()) + if not isinstance(campaign.get("campaign_id"), str): + raise ValueError("campaign.json is missing an identity") + originals, documents = {}, {} + for name, path in (("query-input", query_input), ("query-result", query_result), + ("explain-input", explain_input), ("explain-result", explain_result)): + with Path(path).open("rb") as stream: + raw = stream.read(MAX_BYTES + 1) + if len(raw) > MAX_BYTES: + raise ValueError("MindIE evidence exceeds 2 MiB") + originals[name + ".json"] = raw + documents[name] = _parse(raw) + query = documents["query-input"].get("query") + if not isinstance(query, str) or not query.strip(): + raise ValueError("MindIE query input has no query") + found, query_pointer = _payload(documents["query-result"], {"results"}) + if not isinstance(found["results"], list): + raise ValueError("MindIE query results must be a list") + relative = f"mindie/{context_id}" + hits = {} + for key in ("results", "cards", "foundation_cards", "unverified_cards"): + rows = found.get(key, []) + if not isinstance(rows, list): + raise ValueError("MindIE navigation must contain lists") + for index, row in enumerate(rows): + if (not isinstance(row, dict) or not isinstance(row.get("ref"), str) + or not _REF.fullmatch(row["ref"])): + continue + navigation = { + "list": key, + "locator": f"{relative}/query-result.json#{query_pointer}/{key}/{index}", + **{field: row[field] for field in + ("match_status", "missing_conditions", "ready_to_apply") if field in row}, + } + hits.setdefault(row["ref"], []).append(navigation) + requested = documents["explain-input"].get("refs") + if requested is None: + requested = [documents["explain-input"].get("ref")] + if (not isinstance(requested, list) or not requested + or any(not isinstance(ref, str) or ref not in hits for ref in requested)): + raise ValueError("MindIE explain refs must be exact revision-bound query hits") + domains = {_REF.fullmatch(ref).group(1) for ref in requested} + if len(domains) != 1: + raise ValueError("MindIE explain refs must share one domain") + domain = next(iter(domains)) + delivered, pointer = _payload(documents["explain-result"], {"units", "requires", "unresolved"}) + if any(not isinstance(delivered[key], list) for key in ("units", "requires", "unresolved")): + raise ValueError("MindIE delivery must contain units, requires and unresolved lists") + sources, sections, seen = [], [], set() + response_hash = hashlib.sha256(originals["explain-result.json"]).hexdigest() + for key in ("requires", "units"): + for index, row in enumerate(delivered[key]): + if not isinstance(row, dict): + raise ValueError("Invalid MindIE delivered unit") + identity, revision, content = row.get("id"), row.get("revision"), row.get("content") + if (not isinstance(identity, str) or not _ID.fullmatch(identity) + or type(revision) is not int or revision < 1 + or not isinstance(content, str) or not content.strip()): + raise ValueError("MindIE delivered unit needs id, revision and full content") + ref = f"mindie://{domain}/units/{identity}@{revision}" + if row.get("ref", ref) != ref or ref in seen: + raise ValueError("MindIE delivered unit reference is inconsistent or duplicated") + if key == "units" and ref not in requested: + raise ValueError("MindIE delivered unit does not match the explain request") + seen.add(ref) + locator = (f"{relative}/explain-result.json (sha256:{response_hash})" + f"#{pointer}/{key}/{index}/content") + # Keep query-time provenance visible through the existing strict + # source schema and text-only consumers, without judging applicability. + note = ( + "Query-time navigation (not a final applicability assessment): " + + json.dumps(hits.get(ref, [])) + + "\nExplain request conditions (caller supplied): " + + json.dumps(documents["explain-input"].get("conditions", {})) + ) + locator = f"{note}\nBody: {locator}" + sources.append({"ref": ref, "revision": str(revision), "locator": locator}) + sections.append(f"## {ref} ({key})\n\n{note}\n\n{content}\n") + if not sources: + raise ValueError("MindIE explain delivered no full bodies") + omitted = bool(delivered["unresolved"]) or not set(requested) <= seen + text = "\n".join(sections) + if len(text.encode("utf-8")) > MAX_BYTES: + raise ValueError("MindIE context exceeds 2 MiB") + fragment = {"state": "partial" if omitted else "complete", "text": "", + "queries": [query], "sources": sources, + "missing_reason": ("Requested material was not delivered; inspect the retained " + "explain request and unresolved list." if omitted else "")} + receipt = {"schema_version": 1, "campaign_id": campaign["campaign_id"], + "context_id": context_id, "query_exit_code": query_exit_code, + "explain_exit_code": explain_exit_code, "requested_refs": requested, + "delivered_refs": [source["ref"] for source in sources], + "unresolved": delivered["unresolved"], + "attribution": "caller_supplied_tool_responses_not_authenticated", + "sha256": {name: hashlib.sha256(raw).hexdigest() + for name, raw in originals.items()}} + originals["reviewed-context.txt"] = text.encode("utf-8") + receipt["sha256"]["reviewed-context.txt"] = hashlib.sha256(originals[ + "reviewed-context.txt"]).hexdigest() + for name, value in (("context.json", fragment), ("receipt.json", receipt)): + originals[name] = (json.dumps(value, ensure_ascii=False, indent=2, + allow_nan=False) + "\n").encode("utf-8") + target = root / "mindie" / context_id + if not target.resolve().is_relative_to(root): + raise ValueError("MindIE context directory escapes the campaign") + target.parent.mkdir(parents=True, exist_ok=True) + target.mkdir(exist_ok=False) + # Keep partial writes on I/O failure for inspection, never silently replace them. + for name, raw in originals.items(): + (target / name).write_bytes(raw) + return target diff --git a/src/inferrsi/apps/integrations/mindie/mcp.py b/src/inferrsi/apps/integrations/mindie/mcp.py new file mode 100644 index 000000000..bfec4ae94 --- /dev/null +++ b/src/inferrsi/apps/integrations/mindie/mcp.py @@ -0,0 +1,125 @@ +"""Stdio MCP for the two local MindIE reads approved for this project.""" + +import json +import re +import sys + +from .... import __version__ +from .context import MAX_BYTES, _invalid_constant, _object +from .project import call, describe + +_PROTOCOL = "2025-11-25" +_SUPPORTED = {"2024-11-05", "2025-03-26", "2025-06-18", _PROTOCOL} +_NATIVE_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._:-]{0,255}") +_REFS = {"type": "array", "maxItems": 20, "items": {"type": "string"}} +_COMMON = { + "conditions": {"type": "object", "description": "Observed device, dtype, shape and software."}, + "token_budget": {"type": "integer", "minimum": 1, "maximum": 64000}, +} +_TOOLS = { + "knowledge_query": ( + "Search the approved local MindIE domain for relevant versioned knowledge cards. " + "Choose search terms from the task's concrete knowledge gap.", + "query", + { + "query": {"type": "string", "minLength": 1, "maxLength": 4096}, + "limit": {"type": "integer", "minimum": 1, "maximum": 20}, + "include_unknown": { + "type": "boolean", "description": "Include cards with unknown applicability.", + }, + }, + ), + "knowledge_explain": ( + "Read the complete bodies and prerequisites for selected exact MindIE refs from the query.", + "refs", + {"refs": {**_REFS, "minItems": 1}, "known_refs": _REFS}, + ), +} + + +def _catalog(): + return [ + { + "name": name, "description": description, + "inputSchema": { + "type": "object", "properties": {**_COMMON, **properties}, + "required": [required], "additionalProperties": False, + }, + "annotations": {"readOnlyHint": True, "destructiveHint": False, "openWorldHint": False}, + } + for name, (description, required, properties) in _TOOLS.items() + ] + + +def _thread_id(params): + meta = params.get("_meta") + if isinstance(meta, dict): + turn = meta.get("x-codex-turn-metadata") + thread = meta.get("threadId") + if (isinstance(turn, dict) and isinstance(thread, str) and _NATIVE_ID.fullmatch(thread) + and thread == turn.get("thread_id")): + return thread + return None + + +def _reply(method, params): + if method == "initialize": + requested = params.get("protocolVersion") + return { + "protocolVersion": requested if requested in _SUPPORTED else _PROTOCOL, + "serverInfo": {"name": "inferrsi-mindie", "version": __version__}, + "capabilities": {"tools": {}}, + } + if method == "ping": + return {} + if method == "tools/list": + return {"tools": _catalog() if describe()["available"] else []} + if method == "tools/call": + name = params.get("name") + try: + if not isinstance(name, str) or name not in _TOOLS: + raise ValueError("Unknown MindIE read tool") + result = call(name.removeprefix("knowledge_"), params.get("arguments", {}), + session=_thread_id(params)) + except (OSError, ValueError) as exc: + return {"isError": True, "content": [{"type": "text", "text": str(exc)}]} + return { + "isError": False, "structuredContent": result, + "content": [{"type": "text", "text": json.dumps(result, ensure_ascii=True)}], + } + raise LookupError(method) + + +def serve(): + while line := sys.stdin.buffer.readline(MAX_BYTES + 1): + identifier = None + try: + if len(line) > MAX_BYTES: + # A rejected physical line is one message. Discard its tail + # in bounded chunks so it cannot become a second request. + while line and not line.endswith(b"\n"): + line = sys.stdin.buffer.readline(MAX_BYTES + 1) + raise ValueError("MCP message exceeds 2 MiB") + try: + message = json.loads(line, object_pairs_hook=_object, + parse_constant=_invalid_constant) + except (ValueError, UnicodeError, RecursionError): + print(json.dumps({"jsonrpc": "2.0", "id": None, "error": { + "code": -32700, "message": "Parse error", + }}), flush=True) + continue + if not isinstance(message, dict): + raise ValueError("Invalid MCP request") + identifier = message.get("id") + method, params = message.get("method"), message.get("params", {}) + if (message.get("jsonrpc") != "2.0" or not isinstance(method, str) + or not isinstance(params, dict)): + raise ValueError("Invalid MCP request") + if "id" not in message: + continue + payload = {"result": _reply(method, params)} + except LookupError: + payload = {"error": {"code": -32601, "message": "Unknown MCP method"}} + except (ValueError, TypeError): + payload = {"error": {"code": -32600, "message": "Invalid MCP request"}} + print(json.dumps({"jsonrpc": "2.0", "id": identifier, **payload}), flush=True) diff --git a/src/inferrsi/apps/integrations/mindie/project.py b/src/inferrsi/apps/integrations/mindie/project.py new file mode 100644 index 000000000..8a7fb091a --- /dev/null +++ b/src/inferrsi/apps/integrations/mindie/project.py @@ -0,0 +1,249 @@ +"""An opt-in, per-checkout grant for the local MindIE knowledge reader.""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import subprocess +import tempfile +from pathlib import Path + +from .context import MAX_BYTES, _parse + +_SCHEMA = "inferrsi-mindie-project/1" +_DOMAIN = re.compile(r"[a-z][a-z0-9-]{0,63}") +_WORKER = Path(__file__).with_name("read_worker.py") +_REF_ID = r"[A-Za-z0-9_.-]+@[1-9][0-9]*" +_SEARCH_FIELDS = {"query", "conditions", "limit", "token_budget", "include_unknown"} +_EXPLAIN_FIELDS = {"refs", "conditions", "known_refs", "token_budget"} + + +class ReadFailure(ValueError): + def __init__(self, message, *, exit_code=None, response=None): + super().__init__(message) + self.exit_code = exit_code + self.response = response + + +def project_root() -> Path: + cwd = Path.cwd().resolve() + for folder in (cwd, *cwd.parents): + if (folder / ".git").exists(): + return folder + raise ValueError("Open a Git project to use project-scoped MindIE knowledge") + + +def _grant_path(root: Path) -> Path: + project_id = hashlib.sha256(os.fsencode(root)).hexdigest() + return Path.home() / ".config" / "inferrsi" / "mindie-projects" / (project_id + ".json") + + +def _checkout_identity(root: Path) -> dict: + marker = root / ".git" + info = marker.stat() + identity = {"device": info.st_dev, "inode": info.st_ino} + if hasattr(info, "st_birthtime"): + identity["created"] = info.st_birthtime + if marker.is_file(): + with marker.open("rb") as pointer: + raw = pointer.read(4097) + if len(raw) > 4096 or not raw.startswith(b"gitdir: "): + raise ValueError("The checkout has an invalid Git worktree pointer") + identity["worktree_pointer"] = hashlib.sha256(raw).hexdigest() + gitdir = (root / os.fsdecode(raw[8:].rstrip(b"\r\n"))).stat() + identity["worktree_gitdir"] = [gitdir.st_dev, gitdir.st_ino] + return identity + + +def _json(value) -> bytes: + return (json.dumps(value, ensure_ascii=True, allow_nan=False) + "\n").encode("utf-8") + + +def _read_object(path: Path) -> dict: + with path.open("rb") as source: + raw = source.read(MAX_BYTES + 1) + if len(raw) > MAX_BYTES: + raise ValueError("MindIE JSON exceeds 2 MiB") + return _parse(raw) + + +def _load_grant(root: Path) -> dict: + path = _grant_path(root) + try: + info = path.stat() + if (path.is_symlink() or not path.is_file() + or os.name == "posix" and (info.st_uid != os.getuid() or info.st_mode & 0o077)): + raise ValueError("The MindIE project grant must be a private local file") + grant = _read_object(path) + checkout = _checkout_identity(root) + except FileNotFoundError: + raise ValueError("MindIE is not enabled for this project") from None + except OSError: + raise ValueError("The MindIE project grant could not be read") from None + if (grant.get("schema") != _SCHEMA or grant.get("project_root") != str(root) + or grant.get("checkout") != checkout + or not isinstance(grant.get("domain"), str) or not _DOMAIN.fullmatch(grant["domain"]) + or any(not isinstance(grant.get(key), str) or not Path(grant[key]).is_absolute() + for key in ("python", "engine"))): + raise ValueError("The MindIE grant does not match this project") + return grant + + +def _worker(grant: dict, action: str, arguments: bytes) -> bytes: + environment = os.environ.copy() + environment.pop("MINDIE_BENCH_WORKSPACE", None) + try: + with tempfile.TemporaryDirectory(prefix="inferrsi-mindie-read-") as temporary: + # Upstream also probes cwd.parent/workspace and cwd.parent/work. + # Keep that parent private and empty, not the shared temp directory. + cwd = Path(temporary) / "reader" + cwd.mkdir(mode=0o700) + result = subprocess.run( + [grant["python"], "-I", "-B", str(_WORKER), + grant["engine"], grant["domain"], action], + input=arguments, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, + cwd=cwd, env=environment, timeout=20, check=False, + ) + except (OSError, subprocess.TimeoutExpired): + raise ReadFailure("The approved MindIE reader could not be reached") from None + if len(result.stdout) > MAX_BYTES: + raise ReadFailure("MindIE returned more than 2 MiB", exit_code=result.returncode) + try: + payload = _parse(result.stdout) + except ValueError: + raise ReadFailure( + "MindIE did not return a JSON object", exit_code=result.returncode, + ) from None + if result.returncode or payload.get("isError"): + raise ReadFailure(payload.get("error", "The approved MindIE reader failed"), + exit_code=result.returncode, response=result.stdout) + return result.stdout + + +def enable(adapter: Path, domain: str) -> dict: + """Approve this checkout for one installed, already synced MindIE domain.""" + root = project_root() + if not _DOMAIN.fullmatch(domain): + raise ValueError("Invalid MindIE domain") + config = _read_object(adapter.expanduser()) + domains = config.get("domain_configs", {}) + engine = domains.get(domain) if isinstance(domains, dict) else None + engine = engine or config.get("engine_config") + python = config.get("python") + if any(not isinstance(value, str) or not Path(value).is_absolute() + for value in (python, engine)): + raise ValueError("The MindIE adapter has no installed runtime or engine for this domain") + grant = dict( + schema=_SCHEMA, project_root=str(root), checkout=_checkout_identity(root), + python=python, engine=str(Path(engine).resolve(strict=True)), domain=domain, + ) + info = _parse(_worker(grant, "describe", b"{}")) + path = _grant_path(root) + path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as output: + pending = Path(output.name) + try: + output.write(_json(grant)) + output.close() + pending.replace(path) + finally: + pending.unlink(missing_ok=True) + return {"available": True, **info} + + +def disable() -> dict: + _grant_path(project_root()).unlink(missing_ok=True) + return {"available": False, "reason": "MindIE is not enabled for this project"} + + +def describe() -> dict: + try: + grant = _load_grant(project_root()) + info = _parse(_worker(grant, "describe", b"{}")) + except ValueError as exc: + return {"available": False, "reason": str(exc)} + return {"available": True, "tools": ["knowledge_query", "knowledge_explain"], **info} + + +def _check_arguments(action: str, arguments: dict, domain: str) -> None: + fields = {"query": _SEARCH_FIELDS, "explain": _EXPLAIN_FIELDS}.get(action) + if fields is None or not isinstance(arguments, dict) or set(arguments) - fields: + raise ValueError("Use the public query or explain arguments") + if not isinstance(arguments.get("conditions", {}), dict): + raise ValueError("MindIE conditions must be a JSON object") + budget = arguments.get("token_budget", 16000) + if type(budget) is not int or not 1 <= budget <= 64000: + raise ValueError("MindIE token_budget must be an integer from 1 through 64000") + if action == "query": + query, limit = arguments.get("query"), arguments.get("limit", 5) + if not isinstance(query, str) or not query.strip() or len(query) > 4096: + raise ValueError("MindIE query needs a search string of 1 through 4096 characters") + if type(limit) is not int or not 1 <= limit <= 20: + raise ValueError("MindIE limit must be an integer from 1 through 20") + if type(arguments.get("include_unknown", False)) is not bool: + raise ValueError("MindIE include_unknown must be a boolean") + return + pattern = re.compile(rf"mindie://{re.escape(domain)}/units/{_REF_ID}") + for key in ("refs", "known_refs"): + refs = arguments.get(key, []) + if (not isinstance(refs, list) or len(refs) > 20 or key == "refs" and not refs + or any(not isinstance(ref, str) or not pattern.fullmatch(ref) for ref in refs)): + raise ValueError("Explain requires exact revision-bound refs in the approved domain") + + +def _record_read(root, action, domain, request, response, session, *, exit_code, error=None): + folder = _grant_path(root) + reads = folder.parent / "reads" / folder.stem + reads.mkdir(mode=0o700, parents=True, exist_ok=True) + receipt = Path(tempfile.mkdtemp(prefix=action + "-", dir=reads)) + files = {"input.json": request} + if response is not None: + files["result.json"] = response + for name, content in files.items(): + (receipt / name).write_bytes(content) + record = { + "authorization": "project", "operation": "knowledge_" + action, "exit_code": exit_code, + "domain": domain, + "session_sha256": hashlib.sha256(session.encode()).hexdigest() if session else None, + "sha256": {name: hashlib.sha256(content).hexdigest() for name, content in files.items()}, + } + if error is not None: + record["error"] = error + (receipt / "receipt.json").write_bytes(_json(record)) + return receipt + + +def call(action: str, arguments: dict, *, session: str | None = None) -> dict: + root = project_root() + grant = _load_grant(root) + domain = grant["domain"] + _check_arguments(action, arguments, domain) + request = _json(arguments) + if len(request) > MAX_BYTES: + raise ValueError("MindIE JSON exceeds 2 MiB") + try: + response = _worker(grant, action, request) + except ReadFailure as exc: + receipt = _record_read(root, action, domain, request, exc.response, session, + exit_code=exc.exit_code, error=str(exc)) + raise ValueError(f"{exc}. Local attempt receipt is in {receipt}") from None + receipt = _record_read(root, action, domain, request, response, session, exit_code=0) + return {"receipt_directory": str(receipt), "result": _parse(response)} + + +def dispatch(args) -> dict | None: + if args.mindie_action == "enable": + return enable(args.adapter_config, args.domain) + if args.mindie_action == "disable": + return disable() + if args.mindie_action == "describe": + return describe() + if args.mindie_action == "serve": + from .mcp import serve + + serve() + return None + return call(args.mindie_action, _read_object(args.input), + session=os.environ.get("CODEX_THREAD_ID")) diff --git a/src/inferrsi/apps/integrations/mindie/read_worker.py b/src/inferrsi/apps/integrations/mindie/read_worker.py new file mode 100644 index 000000000..54b1adfa3 --- /dev/null +++ b/src/inferrsi/apps/integrations/mindie/read_worker.py @@ -0,0 +1,79 @@ +"""Read the installed MindIE graded catalog using its own retrieval engine. + +Run this worker with the approved MindIE interpreter. Its SQLite connection is +read-only, and delivery accounting belongs to the caller's local receipts. +""" + +import json +import sqlite3 +import sys +from contextlib import closing +from importlib.metadata import version +from pathlib import Path +from threading import RLock +from time import perf_counter +from types import SimpleNamespace + + +def read(engine_path, domain, action, arguments): + component = version("mindie-knowledge") + if component != "0.13.13": + raise ValueError("The project reader supports mindie-knowledge 0.13.13") + from mindie_knowledge.graded.index import RoutingIndex + from mindie_knowledge.graded.repository import GradedRepository + from mindie_knowledge.state_layout import state_root + + class ReadOnlyRepository(GradedRepository): + def observe_delivered(self, index, payload, *, delivery, run_id=None): + """Leave MindIE's delivery counters and feedback state untouched.""" + + def _rebuild_index(self, catalog, started): + # The caller's read transaction already holds one consistent + # generation. A missing derived index can be rebuilt in memory. + index = RoutingIndex(self._read(item) for item in catalog["units"]) + if index.generation != catalog["generation"]: + raise ValueError("MindIE catalog generation mismatch") + index.build_elapsed_ms = (perf_counter() - started) * 1000 + return index + + engine = json.loads(Path(engine_path).read_text(encoding="utf-8")) + if engine.get("domain") != domain: + raise ValueError("The installed MindIE domain differs from the project grant") + root = state_root(engine["root"], domain) + database = (root / "state-v4.sqlite3").as_uri() + "?mode=ro" + with closing(sqlite3.connect(database, uri=True, timeout=3)) as db: + db.row_factory = sqlite3.Row + db.execute("BEGIN") + store = SimpleNamespace(db=db, root=root, domain=domain, lock=RLock()) + repository = ReadOnlyRepository(store) + if action == "describe": + if not repository.available(): + raise ValueError("The approved MindIE domain has no synced graded catalog") + repository._load() + return {"domain": domain, "component_version": component} + if action == "query": + return repository.query(**arguments, navigation_only=True) + if action == "explain": + return repository.explain(**arguments) + raise ValueError("Unknown MindIE read action") + + +if __name__ == "__main__": + try: + result = read(*sys.argv[1:], json.load(sys.stdin)) + except ValueError as exc: + # Only emit the detail for our fixed errors. Corpus and library errors + # can contain private paths or material. + detail = str(exc) + if detail not in { + "The project reader supports mindie-knowledge 0.13.13", + "The installed MindIE domain differs from the project grant", + "The approved MindIE domain has no synced graded catalog", + }: + detail = "MindIE rejected the read" + print(json.dumps({"isError": True, "error": detail})) + raise SystemExit(1) from None + except (OSError, RuntimeError, ImportError, KeyError, TypeError, sqlite3.Error): + print(json.dumps({"isError": True, "error": "The local MindIE catalog is unavailable"})) + raise SystemExit(1) from None + print(json.dumps(result, ensure_ascii=True, allow_nan=False)) diff --git a/src/inferrsi/apps/mindie/__init__.py b/src/inferrsi/apps/mindie/__init__.py index d4511e2ae..58b42d599 100644 --- a/src/inferrsi/apps/mindie/__init__.py +++ b/src/inferrsi/apps/mindie/__init__.py @@ -1,5 +1,3 @@ -"""The local MindIE adapter: project grant, stdio MCP, context export and the read worker. +"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie`.""" -This initializer imports nothing. The worker file runs under the approved MindIE -interpreter and must stay loadable without this package. -""" +__all__ = [] diff --git a/src/inferrsi/apps/mindie/context.py b/src/inferrsi/apps/mindie/context.py index 7f5377f5c..10c8b7d1d 100644 --- a/src/inferrsi/apps/mindie/context.py +++ b/src/inferrsi/apps/mindie/context.py @@ -1,181 +1,5 @@ -"""Export explicitly supplied MindIE reads for a caller-reviewed campaign trace. +"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie.context`.""" -This CLI host adapter never activates MindIE, reads a session or calls a model. -Retained responses establish supplied bytes, not their authenticity or consumption. -It lives in the Apps package with the rest of the MindIE adapter. -""" +from ..integrations.mindie.context import MAX_BYTES, export_context -from __future__ import annotations - -import hashlib -import json -import re -from pathlib import Path - -MAX_BYTES = 2 * 1024 * 1024 -_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}") -_REF = re.compile(r"mindie://([A-Za-z0-9_.-]+)/units/([A-Za-z0-9_.-]+)@([1-9][0-9]*)") - - -def _object(pairs): - value = {} - for key, item in pairs: - if key in value: - raise ValueError("MindIE evidence contains duplicate JSON keys") - value[key] = item - return value - - -def _invalid_constant(_): - raise ValueError("MindIE evidence contains a non-finite JSON value") - - -def _parse(raw): - try: - value = json.loads(raw, object_pairs_hook=_object, parse_constant=_invalid_constant) - except (ValueError, UnicodeError, RecursionError) as exc: - raise ValueError("MindIE evidence is not valid UTF-8 JSON") from exc - if not isinstance(value, dict): - raise ValueError("MindIE evidence must contain a JSON object") - return value - - -def _payload(value, required, locator=""): - status = value.get("status") - if (value.get("isError") is True or value.get("error") - or isinstance(status, str) and status in {"error", "failed", "inactive", "denied"}): - raise ValueError("MindIE call failed or knowledge was not enabled") - if required <= value.keys(): - return value, locator - structured = value.get("structuredContent") - if isinstance(structured, dict): - return _payload(structured, required, locator + "/structuredContent") - blocks = value.get("content") - if isinstance(blocks, list) and len(blocks) == 1 and isinstance(blocks[0], dict): - block = blocks[0] - if block.get("type") == "text" and isinstance(block.get("text"), str): - return _payload(_parse(block["text"]), required, - locator + "/content/0/text (decoded JSON)") - raise ValueError("Unsupported or missing MindIE business payload") - - -def export_context(root: Path, *, context_id: str, query_input: Path, query_result: Path, - explain_input: Path, explain_result: Path, query_exit_code: int, - explain_exit_code: int) -> Path: - """Keep a new read receipt and a trace context fragment without marking review.""" - if not isinstance(context_id, str) or not _ID.fullmatch(context_id): - raise ValueError("MindIE context id must be a safe identifier") - if query_exit_code != 0 or explain_exit_code != 0: - raise ValueError("MindIE calls must both exit successfully") - root = Path(root).resolve() - campaign = _parse((root / "campaign.json").read_bytes()) - if not isinstance(campaign.get("campaign_id"), str): - raise ValueError("campaign.json is missing an identity") - originals, documents = {}, {} - for name, path in (("query-input", query_input), ("query-result", query_result), - ("explain-input", explain_input), ("explain-result", explain_result)): - with Path(path).open("rb") as stream: - raw = stream.read(MAX_BYTES + 1) - if len(raw) > MAX_BYTES: - raise ValueError("MindIE evidence exceeds 2 MiB") - originals[name + ".json"] = raw - documents[name] = _parse(raw) - query = documents["query-input"].get("query") - if not isinstance(query, str) or not query.strip(): - raise ValueError("MindIE query input has no query") - found, query_pointer = _payload(documents["query-result"], {"results"}) - if not isinstance(found["results"], list): - raise ValueError("MindIE query results must be a list") - relative = f"mindie/{context_id}" - hits = {} - for key in ("results", "cards", "foundation_cards", "unverified_cards"): - rows = found.get(key, []) - if not isinstance(rows, list): - raise ValueError("MindIE navigation must contain lists") - for index, row in enumerate(rows): - if (not isinstance(row, dict) or not isinstance(row.get("ref"), str) - or not _REF.fullmatch(row["ref"])): - continue - navigation = { - "list": key, - "locator": f"{relative}/query-result.json#{query_pointer}/{key}/{index}", - **{field: row[field] for field in - ("match_status", "missing_conditions", "ready_to_apply") if field in row}, - } - hits.setdefault(row["ref"], []).append(navigation) - requested = documents["explain-input"].get("refs") - if requested is None: - requested = [documents["explain-input"].get("ref")] - if (not isinstance(requested, list) or not requested - or any(not isinstance(ref, str) or ref not in hits for ref in requested)): - raise ValueError("MindIE explain refs must be exact revision-bound query hits") - domains = {_REF.fullmatch(ref).group(1) for ref in requested} - if len(domains) != 1: - raise ValueError("MindIE explain refs must share one domain") - domain = next(iter(domains)) - delivered, pointer = _payload(documents["explain-result"], {"units", "requires", "unresolved"}) - if any(not isinstance(delivered[key], list) for key in ("units", "requires", "unresolved")): - raise ValueError("MindIE delivery must contain units, requires and unresolved lists") - sources, sections, seen = [], [], set() - response_hash = hashlib.sha256(originals["explain-result.json"]).hexdigest() - for key in ("requires", "units"): - for index, row in enumerate(delivered[key]): - if not isinstance(row, dict): - raise ValueError("Invalid MindIE delivered unit") - identity, revision, content = row.get("id"), row.get("revision"), row.get("content") - if (not isinstance(identity, str) or not _ID.fullmatch(identity) - or type(revision) is not int or revision < 1 - or not isinstance(content, str) or not content.strip()): - raise ValueError("MindIE delivered unit needs id, revision and full content") - ref = f"mindie://{domain}/units/{identity}@{revision}" - if row.get("ref", ref) != ref or ref in seen: - raise ValueError("MindIE delivered unit reference is inconsistent or duplicated") - if key == "units" and ref not in requested: - raise ValueError("MindIE delivered unit does not match the explain request") - seen.add(ref) - locator = (f"{relative}/explain-result.json (sha256:{response_hash})" - f"#{pointer}/{key}/{index}/content") - # Keep query-time provenance visible through the existing strict - # source schema and text-only consumers, without judging applicability. - note = ( - "Query-time navigation (not a final applicability assessment): " - + json.dumps(hits.get(ref, [])) - + "\nExplain request conditions (caller supplied): " - + json.dumps(documents["explain-input"].get("conditions", {})) - ) - locator = f"{note}\nBody: {locator}" - sources.append({"ref": ref, "revision": str(revision), "locator": locator}) - sections.append(f"## {ref} ({key})\n\n{note}\n\n{content}\n") - if not sources: - raise ValueError("MindIE explain delivered no full bodies") - omitted = bool(delivered["unresolved"]) or not set(requested) <= seen - text = "\n".join(sections) - if len(text.encode("utf-8")) > MAX_BYTES: - raise ValueError("MindIE context exceeds 2 MiB") - fragment = {"state": "partial" if omitted else "complete", "text": "", - "queries": [query], "sources": sources, - "missing_reason": ("Requested material was not delivered; inspect the retained " - "explain request and unresolved list." if omitted else "")} - receipt = {"schema_version": 1, "campaign_id": campaign["campaign_id"], - "context_id": context_id, "query_exit_code": query_exit_code, - "explain_exit_code": explain_exit_code, "requested_refs": requested, - "delivered_refs": [source["ref"] for source in sources], - "unresolved": delivered["unresolved"], - "attribution": "caller_supplied_tool_responses_not_authenticated", - "sha256": {name: hashlib.sha256(raw).hexdigest() - for name, raw in originals.items()}} - originals["reviewed-context.txt"] = text.encode("utf-8") - receipt["sha256"]["reviewed-context.txt"] = hashlib.sha256(originals[ - "reviewed-context.txt"]).hexdigest() - for name, value in (("context.json", fragment), ("receipt.json", receipt)): - originals[name] = (json.dumps(value, ensure_ascii=False, indent=2, - allow_nan=False) + "\n").encode("utf-8") - target = root / "mindie" / context_id - if not target.resolve().is_relative_to(root): - raise ValueError("MindIE context directory escapes the campaign") - target.parent.mkdir(parents=True, exist_ok=True) - target.mkdir(exist_ok=False) - # Keep partial writes on I/O failure for inspection, never silently replace them. - for name, raw in originals.items(): - (target / name).write_bytes(raw) - return target +__all__ = ["MAX_BYTES", "export_context"] diff --git a/src/inferrsi/apps/mindie/mcp.py b/src/inferrsi/apps/mindie/mcp.py index 681a3055a..9dbb389ba 100644 --- a/src/inferrsi/apps/mindie/mcp.py +++ b/src/inferrsi/apps/mindie/mcp.py @@ -1,125 +1,5 @@ -"""Stdio MCP for the two local MindIE reads approved for this project.""" +"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie.mcp`.""" -import json -import re -import sys +from ..integrations.mindie.mcp import serve -from ... import __version__ -from .context import MAX_BYTES, _invalid_constant, _object -from .project import call, describe - -_PROTOCOL = "2025-11-25" -_SUPPORTED = {"2024-11-05", "2025-03-26", "2025-06-18", _PROTOCOL} -_NATIVE_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._:-]{0,255}") -_REFS = {"type": "array", "maxItems": 20, "items": {"type": "string"}} -_COMMON = { - "conditions": {"type": "object", "description": "Observed device, dtype, shape and software."}, - "token_budget": {"type": "integer", "minimum": 1, "maximum": 64000}, -} -_TOOLS = { - "knowledge_query": ( - "Search the approved local MindIE domain for relevant versioned knowledge cards. " - "Choose search terms from the task's concrete knowledge gap.", - "query", - { - "query": {"type": "string", "minLength": 1, "maxLength": 4096}, - "limit": {"type": "integer", "minimum": 1, "maximum": 20}, - "include_unknown": { - "type": "boolean", "description": "Include cards with unknown applicability.", - }, - }, - ), - "knowledge_explain": ( - "Read the complete bodies and prerequisites for selected exact MindIE refs from the query.", - "refs", - {"refs": {**_REFS, "minItems": 1}, "known_refs": _REFS}, - ), -} - - -def _catalog(): - return [ - { - "name": name, "description": description, - "inputSchema": { - "type": "object", "properties": {**_COMMON, **properties}, - "required": [required], "additionalProperties": False, - }, - "annotations": {"readOnlyHint": True, "destructiveHint": False, "openWorldHint": False}, - } - for name, (description, required, properties) in _TOOLS.items() - ] - - -def _thread_id(params): - meta = params.get("_meta") - if isinstance(meta, dict): - turn = meta.get("x-codex-turn-metadata") - thread = meta.get("threadId") - if (isinstance(turn, dict) and isinstance(thread, str) and _NATIVE_ID.fullmatch(thread) - and thread == turn.get("thread_id")): - return thread - return None - - -def _reply(method, params): - if method == "initialize": - requested = params.get("protocolVersion") - return { - "protocolVersion": requested if requested in _SUPPORTED else _PROTOCOL, - "serverInfo": {"name": "inferrsi-mindie", "version": __version__}, - "capabilities": {"tools": {}}, - } - if method == "ping": - return {} - if method == "tools/list": - return {"tools": _catalog() if describe()["available"] else []} - if method == "tools/call": - name = params.get("name") - try: - if not isinstance(name, str) or name not in _TOOLS: - raise ValueError("Unknown MindIE read tool") - result = call(name.removeprefix("knowledge_"), params.get("arguments", {}), - session=_thread_id(params)) - except (OSError, ValueError) as exc: - return {"isError": True, "content": [{"type": "text", "text": str(exc)}]} - return { - "isError": False, "structuredContent": result, - "content": [{"type": "text", "text": json.dumps(result, ensure_ascii=True)}], - } - raise LookupError(method) - - -def serve(): - while line := sys.stdin.buffer.readline(MAX_BYTES + 1): - identifier = None - try: - if len(line) > MAX_BYTES: - # A rejected physical line is one message. Discard its tail - # in bounded chunks so it cannot become a second request. - while line and not line.endswith(b"\n"): - line = sys.stdin.buffer.readline(MAX_BYTES + 1) - raise ValueError("MCP message exceeds 2 MiB") - try: - message = json.loads(line, object_pairs_hook=_object, - parse_constant=_invalid_constant) - except (ValueError, UnicodeError, RecursionError): - print(json.dumps({"jsonrpc": "2.0", "id": None, "error": { - "code": -32700, "message": "Parse error", - }}), flush=True) - continue - if not isinstance(message, dict): - raise ValueError("Invalid MCP request") - identifier = message.get("id") - method, params = message.get("method"), message.get("params", {}) - if (message.get("jsonrpc") != "2.0" or not isinstance(method, str) - or not isinstance(params, dict)): - raise ValueError("Invalid MCP request") - if "id" not in message: - continue - payload = {"result": _reply(method, params)} - except LookupError: - payload = {"error": {"code": -32601, "message": "Unknown MCP method"}} - except (ValueError, TypeError): - payload = {"error": {"code": -32600, "message": "Invalid MCP request"}} - print(json.dumps({"jsonrpc": "2.0", "id": identifier, **payload}), flush=True) +__all__ = ["serve"] diff --git a/src/inferrsi/apps/mindie/project.py b/src/inferrsi/apps/mindie/project.py index 8a7fb091a..71b3e6dd4 100644 --- a/src/inferrsi/apps/mindie/project.py +++ b/src/inferrsi/apps/mindie/project.py @@ -1,249 +1,13 @@ -"""An opt-in, per-checkout grant for the local MindIE knowledge reader.""" - -from __future__ import annotations - -import hashlib -import json -import os -import re -import subprocess -import tempfile -from pathlib import Path - -from .context import MAX_BYTES, _parse - -_SCHEMA = "inferrsi-mindie-project/1" -_DOMAIN = re.compile(r"[a-z][a-z0-9-]{0,63}") -_WORKER = Path(__file__).with_name("read_worker.py") -_REF_ID = r"[A-Za-z0-9_.-]+@[1-9][0-9]*" -_SEARCH_FIELDS = {"query", "conditions", "limit", "token_budget", "include_unknown"} -_EXPLAIN_FIELDS = {"refs", "conditions", "known_refs", "token_budget"} - - -class ReadFailure(ValueError): - def __init__(self, message, *, exit_code=None, response=None): - super().__init__(message) - self.exit_code = exit_code - self.response = response - - -def project_root() -> Path: - cwd = Path.cwd().resolve() - for folder in (cwd, *cwd.parents): - if (folder / ".git").exists(): - return folder - raise ValueError("Open a Git project to use project-scoped MindIE knowledge") - - -def _grant_path(root: Path) -> Path: - project_id = hashlib.sha256(os.fsencode(root)).hexdigest() - return Path.home() / ".config" / "inferrsi" / "mindie-projects" / (project_id + ".json") - - -def _checkout_identity(root: Path) -> dict: - marker = root / ".git" - info = marker.stat() - identity = {"device": info.st_dev, "inode": info.st_ino} - if hasattr(info, "st_birthtime"): - identity["created"] = info.st_birthtime - if marker.is_file(): - with marker.open("rb") as pointer: - raw = pointer.read(4097) - if len(raw) > 4096 or not raw.startswith(b"gitdir: "): - raise ValueError("The checkout has an invalid Git worktree pointer") - identity["worktree_pointer"] = hashlib.sha256(raw).hexdigest() - gitdir = (root / os.fsdecode(raw[8:].rstrip(b"\r\n"))).stat() - identity["worktree_gitdir"] = [gitdir.st_dev, gitdir.st_ino] - return identity - - -def _json(value) -> bytes: - return (json.dumps(value, ensure_ascii=True, allow_nan=False) + "\n").encode("utf-8") - - -def _read_object(path: Path) -> dict: - with path.open("rb") as source: - raw = source.read(MAX_BYTES + 1) - if len(raw) > MAX_BYTES: - raise ValueError("MindIE JSON exceeds 2 MiB") - return _parse(raw) - - -def _load_grant(root: Path) -> dict: - path = _grant_path(root) - try: - info = path.stat() - if (path.is_symlink() or not path.is_file() - or os.name == "posix" and (info.st_uid != os.getuid() or info.st_mode & 0o077)): - raise ValueError("The MindIE project grant must be a private local file") - grant = _read_object(path) - checkout = _checkout_identity(root) - except FileNotFoundError: - raise ValueError("MindIE is not enabled for this project") from None - except OSError: - raise ValueError("The MindIE project grant could not be read") from None - if (grant.get("schema") != _SCHEMA or grant.get("project_root") != str(root) - or grant.get("checkout") != checkout - or not isinstance(grant.get("domain"), str) or not _DOMAIN.fullmatch(grant["domain"]) - or any(not isinstance(grant.get(key), str) or not Path(grant[key]).is_absolute() - for key in ("python", "engine"))): - raise ValueError("The MindIE grant does not match this project") - return grant - - -def _worker(grant: dict, action: str, arguments: bytes) -> bytes: - environment = os.environ.copy() - environment.pop("MINDIE_BENCH_WORKSPACE", None) - try: - with tempfile.TemporaryDirectory(prefix="inferrsi-mindie-read-") as temporary: - # Upstream also probes cwd.parent/workspace and cwd.parent/work. - # Keep that parent private and empty, not the shared temp directory. - cwd = Path(temporary) / "reader" - cwd.mkdir(mode=0o700) - result = subprocess.run( - [grant["python"], "-I", "-B", str(_WORKER), - grant["engine"], grant["domain"], action], - input=arguments, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, - cwd=cwd, env=environment, timeout=20, check=False, - ) - except (OSError, subprocess.TimeoutExpired): - raise ReadFailure("The approved MindIE reader could not be reached") from None - if len(result.stdout) > MAX_BYTES: - raise ReadFailure("MindIE returned more than 2 MiB", exit_code=result.returncode) - try: - payload = _parse(result.stdout) - except ValueError: - raise ReadFailure( - "MindIE did not return a JSON object", exit_code=result.returncode, - ) from None - if result.returncode or payload.get("isError"): - raise ReadFailure(payload.get("error", "The approved MindIE reader failed"), - exit_code=result.returncode, response=result.stdout) - return result.stdout - - -def enable(adapter: Path, domain: str) -> dict: - """Approve this checkout for one installed, already synced MindIE domain.""" - root = project_root() - if not _DOMAIN.fullmatch(domain): - raise ValueError("Invalid MindIE domain") - config = _read_object(adapter.expanduser()) - domains = config.get("domain_configs", {}) - engine = domains.get(domain) if isinstance(domains, dict) else None - engine = engine or config.get("engine_config") - python = config.get("python") - if any(not isinstance(value, str) or not Path(value).is_absolute() - for value in (python, engine)): - raise ValueError("The MindIE adapter has no installed runtime or engine for this domain") - grant = dict( - schema=_SCHEMA, project_root=str(root), checkout=_checkout_identity(root), - python=python, engine=str(Path(engine).resolve(strict=True)), domain=domain, - ) - info = _parse(_worker(grant, "describe", b"{}")) - path = _grant_path(root) - path.parent.mkdir(mode=0o700, parents=True, exist_ok=True) - with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as output: - pending = Path(output.name) - try: - output.write(_json(grant)) - output.close() - pending.replace(path) - finally: - pending.unlink(missing_ok=True) - return {"available": True, **info} - - -def disable() -> dict: - _grant_path(project_root()).unlink(missing_ok=True) - return {"available": False, "reason": "MindIE is not enabled for this project"} - - -def describe() -> dict: - try: - grant = _load_grant(project_root()) - info = _parse(_worker(grant, "describe", b"{}")) - except ValueError as exc: - return {"available": False, "reason": str(exc)} - return {"available": True, "tools": ["knowledge_query", "knowledge_explain"], **info} - - -def _check_arguments(action: str, arguments: dict, domain: str) -> None: - fields = {"query": _SEARCH_FIELDS, "explain": _EXPLAIN_FIELDS}.get(action) - if fields is None or not isinstance(arguments, dict) or set(arguments) - fields: - raise ValueError("Use the public query or explain arguments") - if not isinstance(arguments.get("conditions", {}), dict): - raise ValueError("MindIE conditions must be a JSON object") - budget = arguments.get("token_budget", 16000) - if type(budget) is not int or not 1 <= budget <= 64000: - raise ValueError("MindIE token_budget must be an integer from 1 through 64000") - if action == "query": - query, limit = arguments.get("query"), arguments.get("limit", 5) - if not isinstance(query, str) or not query.strip() or len(query) > 4096: - raise ValueError("MindIE query needs a search string of 1 through 4096 characters") - if type(limit) is not int or not 1 <= limit <= 20: - raise ValueError("MindIE limit must be an integer from 1 through 20") - if type(arguments.get("include_unknown", False)) is not bool: - raise ValueError("MindIE include_unknown must be a boolean") - return - pattern = re.compile(rf"mindie://{re.escape(domain)}/units/{_REF_ID}") - for key in ("refs", "known_refs"): - refs = arguments.get(key, []) - if (not isinstance(refs, list) or len(refs) > 20 or key == "refs" and not refs - or any(not isinstance(ref, str) or not pattern.fullmatch(ref) for ref in refs)): - raise ValueError("Explain requires exact revision-bound refs in the approved domain") - - -def _record_read(root, action, domain, request, response, session, *, exit_code, error=None): - folder = _grant_path(root) - reads = folder.parent / "reads" / folder.stem - reads.mkdir(mode=0o700, parents=True, exist_ok=True) - receipt = Path(tempfile.mkdtemp(prefix=action + "-", dir=reads)) - files = {"input.json": request} - if response is not None: - files["result.json"] = response - for name, content in files.items(): - (receipt / name).write_bytes(content) - record = { - "authorization": "project", "operation": "knowledge_" + action, "exit_code": exit_code, - "domain": domain, - "session_sha256": hashlib.sha256(session.encode()).hexdigest() if session else None, - "sha256": {name: hashlib.sha256(content).hexdigest() for name, content in files.items()}, - } - if error is not None: - record["error"] = error - (receipt / "receipt.json").write_bytes(_json(record)) - return receipt - - -def call(action: str, arguments: dict, *, session: str | None = None) -> dict: - root = project_root() - grant = _load_grant(root) - domain = grant["domain"] - _check_arguments(action, arguments, domain) - request = _json(arguments) - if len(request) > MAX_BYTES: - raise ValueError("MindIE JSON exceeds 2 MiB") - try: - response = _worker(grant, action, request) - except ReadFailure as exc: - receipt = _record_read(root, action, domain, request, exc.response, session, - exit_code=exc.exit_code, error=str(exc)) - raise ValueError(f"{exc}. Local attempt receipt is in {receipt}") from None - receipt = _record_read(root, action, domain, request, response, session, exit_code=0) - return {"receipt_directory": str(receipt), "result": _parse(response)} - - -def dispatch(args) -> dict | None: - if args.mindie_action == "enable": - return enable(args.adapter_config, args.domain) - if args.mindie_action == "disable": - return disable() - if args.mindie_action == "describe": - return describe() - if args.mindie_action == "serve": - from .mcp import serve - - serve() - return None - return call(args.mindie_action, _read_object(args.input), - session=os.environ.get("CODEX_THREAD_ID")) +"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie.project`.""" + +from ..integrations.mindie.project import ( + ReadFailure, + call, + describe, + disable, + dispatch, + enable, + project_root, +) + +__all__ = ["ReadFailure", "project_root", "enable", "disable", "describe", "call", "dispatch"] diff --git a/src/inferrsi/apps/mindie/read_worker.py b/src/inferrsi/apps/mindie/read_worker.py index 54b1adfa3..a121883ab 100644 --- a/src/inferrsi/apps/mindie/read_worker.py +++ b/src/inferrsi/apps/mindie/read_worker.py @@ -1,79 +1,5 @@ -"""Read the installed MindIE graded catalog using its own retrieval engine. +"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie.read_worker`.""" -Run this worker with the approved MindIE interpreter. Its SQLite connection is -read-only, and delivery accounting belongs to the caller's local receipts. -""" +from ..integrations.mindie.read_worker import read -import json -import sqlite3 -import sys -from contextlib import closing -from importlib.metadata import version -from pathlib import Path -from threading import RLock -from time import perf_counter -from types import SimpleNamespace - - -def read(engine_path, domain, action, arguments): - component = version("mindie-knowledge") - if component != "0.13.13": - raise ValueError("The project reader supports mindie-knowledge 0.13.13") - from mindie_knowledge.graded.index import RoutingIndex - from mindie_knowledge.graded.repository import GradedRepository - from mindie_knowledge.state_layout import state_root - - class ReadOnlyRepository(GradedRepository): - def observe_delivered(self, index, payload, *, delivery, run_id=None): - """Leave MindIE's delivery counters and feedback state untouched.""" - - def _rebuild_index(self, catalog, started): - # The caller's read transaction already holds one consistent - # generation. A missing derived index can be rebuilt in memory. - index = RoutingIndex(self._read(item) for item in catalog["units"]) - if index.generation != catalog["generation"]: - raise ValueError("MindIE catalog generation mismatch") - index.build_elapsed_ms = (perf_counter() - started) * 1000 - return index - - engine = json.loads(Path(engine_path).read_text(encoding="utf-8")) - if engine.get("domain") != domain: - raise ValueError("The installed MindIE domain differs from the project grant") - root = state_root(engine["root"], domain) - database = (root / "state-v4.sqlite3").as_uri() + "?mode=ro" - with closing(sqlite3.connect(database, uri=True, timeout=3)) as db: - db.row_factory = sqlite3.Row - db.execute("BEGIN") - store = SimpleNamespace(db=db, root=root, domain=domain, lock=RLock()) - repository = ReadOnlyRepository(store) - if action == "describe": - if not repository.available(): - raise ValueError("The approved MindIE domain has no synced graded catalog") - repository._load() - return {"domain": domain, "component_version": component} - if action == "query": - return repository.query(**arguments, navigation_only=True) - if action == "explain": - return repository.explain(**arguments) - raise ValueError("Unknown MindIE read action") - - -if __name__ == "__main__": - try: - result = read(*sys.argv[1:], json.load(sys.stdin)) - except ValueError as exc: - # Only emit the detail for our fixed errors. Corpus and library errors - # can contain private paths or material. - detail = str(exc) - if detail not in { - "The project reader supports mindie-knowledge 0.13.13", - "The installed MindIE domain differs from the project grant", - "The approved MindIE domain has no synced graded catalog", - }: - detail = "MindIE rejected the read" - print(json.dumps({"isError": True, "error": detail})) - raise SystemExit(1) from None - except (OSError, RuntimeError, ImportError, KeyError, TypeError, sqlite3.Error): - print(json.dumps({"isError": True, "error": "The local MindIE catalog is unavailable"})) - raise SystemExit(1) from None - print(json.dumps(result, ensure_ascii=True, allow_nan=False)) +__all__ = ["read"] diff --git a/tests/_gate_policy_support.py b/tests/_gate_policy_support.py index d82ab2c0c..5abdfb5f4 100644 --- a/tests/_gate_policy_support.py +++ b/tests/_gate_policy_support.py @@ -80,7 +80,8 @@ # ``inferrsi.apps.workflows.benchmark_operations``. RETIRED_BY_A2 = frozenset({"inferrsi.summary_chart", "inferrsi.benchmark_operations"}) # #452 M1 deleted the four root MindIE files the same way. The adapter is the -# ``inferrsi.apps.mindie`` package: ``project``, ``context``, ``mcp`` and ``read_worker``. +# ``inferrsi.apps.integrations.mindie`` package: ``project``, ``context``, ``mcp`` +# and ``read_worker``. RETIRED_BY_M1 = frozenset( { "inferrsi.mindie_project", diff --git a/tests/check_installed.py b/tests/check_installed.py index 58bb4e996..d44fb4adf 100644 --- a/tests/check_installed.py +++ b/tests/check_installed.py @@ -11,6 +11,7 @@ from importlib.resources import as_file, files from importlib.util import find_spec from pathlib import Path +from types import ModuleType import inferrsi import inferrsi.harness @@ -622,6 +623,10 @@ def _inventory(root, prefix): ("campaign", "workflows"), ("benchmark_operations", "workflows"), ("improvement", "workflows"), ("skill_evaluation", "workflows"), ("skill_eval_engineering", "workflows"), ("benchmark_compare", "workflows"), + ("cannbot", "integrations"), ("cannbot.project", "integrations"), + ("cannbot.read_worker", "integrations"), ("mindie", "integrations"), + ("mindie.project", "integrations"), ("mindie.context", "integrations"), + ("mindie.mcp", "integrations"), ("mindie.read_worker", "integrations"), ): old_name = f"inferrsi.apps.{name}" canonical_name = f"inferrsi.apps.{group}.{name}" @@ -629,6 +634,10 @@ def _inventory(root, prefix): assert find_spec(old_name) is not None and find_spec(canonical_name) is not None for exported in old.__all__: value = getattr(canonical, exported) + if isinstance(value, ModuleType): + assert getattr(old, exported) is import_module(old_name + "." + exported) + assert value is import_module(canonical_name + "." + exported) + continue assert getattr(old, exported) is value, (old_name, exported) if getattr(value, "__module__", None) == canonical_name: assert pickle.loads(f"c{old_name}\n{exported}\n.".encode("ascii")) is value @@ -701,7 +710,7 @@ def _inventory(root, prefix): assert installed_root - {"_version.py"} == {"__init__.py", "__main__.py"}, sorted(installed_root) # #408 A1: the CLI and its parser ship only as inferrsi.apps.cli; neither root name imports. # #408 A2: the chart renderer and the task launcher ship only under inferrsi.apps. -# #452 M1: the MindIE adapter ships only as inferrsi.apps.mindie. +# #452 M1: the MindIE adapter ships only as inferrsi.apps.integrations.mindie. for a1_retired in ("cli", "arguments", "summary_chart", "benchmark_operations", "mindie_project", "mindie_context", "mindie_mcp", "_mindie_read_worker"): assert find_spec(f"inferrsi.{a1_retired}") is None, a1_retired @@ -715,8 +724,10 @@ def _inventory(root, prefix): assert main.__module__ == "inferrsi.apps.cli" for canonical in ("inferrsi.apps.views.summary_chart", "inferrsi.apps.workflows.benchmark_operations", - "inferrsi.apps.mindie.project", "inferrsi.apps.mindie.context", - "inferrsi.apps.mindie.mcp", "inferrsi.apps.mindie.read_worker"): + "inferrsi.apps.integrations.mindie.project", + "inferrsi.apps.integrations.mindie.context", + "inferrsi.apps.integrations.mindie.mcp", + "inferrsi.apps.integrations.mindie.read_worker"): assert find_spec(canonical) is not None, canonical assert import_module("inferrsi.apps.views.summary_chart").main.__module__ == ( "inferrsi.apps.views.summary_chart" @@ -747,12 +758,13 @@ def _inventory(root, prefix): # the stdio MCP module through its absolute package path. mindie_probe = ( "import sys\n" - "import inferrsi.apps.mindie as adapter\n" + "import inferrsi.apps.integrations.mindie as adapter\n" "loaded = {name for name in sys.modules if name.split('.')[0] == 'inferrsi'}\n" - "allowed = {'inferrsi', 'inferrsi._version', 'inferrsi.apps', 'inferrsi.apps.mindie'}\n" + "allowed = {'inferrsi', 'inferrsi._version', 'inferrsi.apps', 'inferrsi.apps.integrations',\n" + " 'inferrsi.apps.integrations.mindie'}\n" "assert loaded <= allowed, sorted(loaded - allowed)\n" "assert not hasattr(adapter, '__version__')\n" - "from inferrsi.apps.mindie import mcp\n" + "from inferrsi.apps.integrations.mindie import mcp\n" "import inferrsi\n" "reply = mcp._reply('initialize', {'protocolVersion': '2025-11-25'})\n" "assert reply['serverInfo']['version'] == inferrsi.__version__, reply\n" @@ -764,27 +776,29 @@ def _inventory(root, prefix): assert mindie.returncode == 0, mindie.stderr # The worker ships as a plain file beside the adapter. It is started by file path under # ``-I -B`` with no package context, so it must load without a package-relative import. -worker_file = files("inferrsi").joinpath("apps", "mindie", "read_worker.py") -assert worker_file.is_file() -with as_file(worker_file) as worker_path: - worker_probe = ( - "import ast, runpy, sys\n" - f"path = {str(worker_path)!r}\n" - "tree = ast.parse(open(path, encoding='utf-8').read())\n" - "top = [node for node in tree.body if isinstance(node, (ast.Import, ast.ImportFrom))]\n" - "assert all(not (isinstance(node, ast.ImportFrom) and node.level) for node in top)\n" - "assert all((node.module if isinstance(node, ast.ImportFrom) else node.names[0].name)\n" - " .split('.')[0] in sys.stdlib_module_names for node in top)\n" - "namespace = runpy.run_path(path, run_name='not_main')\n" - "assert callable(namespace['read'])\n" - "assert not [name for name in sys.modules if name.split('.')[0] in\n" - " ('inferrsi', 'mindie_knowledge')]\n" - ) - worker = subprocess.run( - [sys.executable, "-I", "-B", "-c", worker_probe], capture_output=True, text=True, - check=False, - ) -assert worker.returncode == 0, worker.stderr +for worker_system in ("cannbot", "mindie"): + worker_file = files("inferrsi").joinpath( + "apps", "integrations", worker_system, "read_worker.py") + assert worker_file.is_file() + with as_file(worker_file) as worker_path: + worker_probe = ( + "import ast, runpy, sys\n" + f"path = {str(worker_path)!r}\n" + "tree = ast.parse(open(path, encoding='utf-8').read())\n" + "top = [node for node in tree.body if isinstance(node, (ast.Import, ast.ImportFrom))]\n" + "assert all(not (isinstance(node, ast.ImportFrom) and node.level) for node in top)\n" + "assert all((node.module if isinstance(node, ast.ImportFrom) else node.names[0].name)\n" + " .split('.')[0] in sys.stdlib_module_names for node in top)\n" + "namespace = runpy.run_path(path, run_name='not_main')\n" + "assert callable(namespace['read'])\n" + "assert not [name for name in sys.modules if name.split('.')[0] in\n" + " ('inferrsi', 'mindie_knowledge')]\n" + ) + worker = subprocess.run( + [sys.executable, "-I", "-B", "-c", worker_probe], capture_output=True, text=True, + check=False, + ) + assert worker.returncode == 0, worker.stderr # The installed adapter launches that worker through its own ``_WORKER`` lookup and the # real ``python -I -B `` entry. From a temporary cwd, with a synthetic grant whose # engine file does not exist and no MindIE library, the read must end in the worker's @@ -792,8 +806,8 @@ def _inventory(root, prefix): launch_probe = ( "import json, sys\n" "from pathlib import Path\n" - "import inferrsi.apps.mindie as adapter\n" - "from inferrsi.apps.mindie import project\n" + "import inferrsi.apps.integrations.mindie as adapter\n" + "from inferrsi.apps.integrations.mindie import project\n" "assert project._WORKER == Path(adapter.__path__[0]) / 'read_worker.py', project._WORKER\n" "assert project._WORKER.is_file()\n" "grant = {'python': sys.executable, 'engine': str(Path.cwd() / 'missing-engine.json'),\n" @@ -877,3 +891,27 @@ def _inventory(root, prefix): check=False, cwd=serving_cwd, ) assert serving.returncode == 0, serving.stderr + +launch_probe = ( + "import sys\n" + "from pathlib import Path\n" + "import inferrsi.apps.integrations.cannbot as adapter\n" + "from inferrsi.apps.integrations.cannbot import project\n" + "assert project._WORKER == Path(adapter.__path__[0]) / 'read_worker.py'\n" + "assert project._WORKER.is_file()\n" + "grant = {'python': sys.executable, 'upstream': str(Path.cwd() / 'missing-cannbot')}\n" + "try:\n" + " project._run(grant, 'discover', {}, timeout=5)\n" + "except project.ChannelFailure as exc:\n" + " failure = exc\n" + "else:\n" + " raise AssertionError('the synthetic read must be rejected')\n" + "assert str(failure) == 'The local cannbot checkout is unavailable', str(failure)\n" + "assert failure.state == 'unreachable' and failure.exit_code == 1\n" +) +with tempfile.TemporaryDirectory(prefix="inferrsi-cannbot-launch-") as launch_cwd: + launch = subprocess.run( + [sys.executable, "-I", "-B", "-c", launch_probe], capture_output=True, text=True, + check=False, cwd=launch_cwd, + ) +assert launch.returncode == 0, launch.stderr diff --git a/tests/test_apps_module_migrations.py b/tests/test_apps_module_migrations.py index 4582c3ca5..85b5c20a8 100644 --- a/tests/test_apps_module_migrations.py +++ b/tests/test_apps_module_migrations.py @@ -5,7 +5,9 @@ import json import pickle from importlib import import_module +from importlib.util import resolve_name from pathlib import Path +from types import ModuleType from typing import get_origin import pytest @@ -20,34 +22,52 @@ @pytest.mark.parametrize("entry", MIGRATIONS, ids=lambda entry: entry["old"]) def test_shim_is_an_explicit_export_of_the_same_objects(entry): - path = REPO / "src" / (entry["old"].replace(".", "/") + ".py") + relative = entry["old"].replace(".", "/") + path = REPO / "src" / (relative + ("/__init__.py" if entry.get("package") else ".py")) tree = ast.parse(path.read_text(encoding="utf-8")) + statements = list(tree.body) if entry.get("script_entry"): - docstring, imported, declared, guard = tree.body + guard = statements.pop() assert ast.dump(guard) == ast.dump(ast.parse( f'if __name__ == "__main__":\n {entry["script_entry"]}()\n' ).body[0]) - else: - docstring, imported, declared = tree.body + docstring, *imports, declared = statements assert isinstance(docstring, ast.Expr) and isinstance(docstring.value, ast.Constant) - assert isinstance(imported, ast.ImportFrom) + package = entry["old"] if entry.get("package") else entry["old"].rpartition(".")[0] + imported_names = [] + for imported in imports: + assert isinstance(imported, ast.ImportFrom) + target = resolve_name("." * imported.level + (imported.module or ""), package) + for name in imported.names: + assert name.asname is None and name.name != "*" + module_export = entry.get("module_exports", {}).get(name.name) + if module_export: + assert target + "." + name.name == module_export["old"] + else: + assert target == entry["canonical"] + imported_names.append(name.name) + assert sorted(imported_names) == sorted(entry["exports"]) if entry.get("script_entry"): - # This former file entry must also work without a package context. - assert imported.level == 0 and imported.module == entry["canonical"] - else: - assert imported.level == 1 - assert "inferrsi.apps." + imported.module == entry["canonical"] - assert sorted((name.name, name.asname) for name in imported.names) == sorted( - (name, None) for name in entry["exports"] - ) + assert len(imports) == 1 and imports[0].level == 0 assert isinstance(declared, ast.Assign) assert [target.id for target in declared.targets] == ["__all__"] assert ast.literal_eval(declared.value) == entry["exports"] old, canonical = import_module(entry["old"]), import_module(entry["canonical"]) assert old.__all__ == entry["exports"] - assert sorted(name for name in vars(old) if not name.startswith("_")) == sorted(old.__all__) + old_modules = {item["old"] for item in MIGRATIONS} + public = [name for name, value in vars(old).items() if not name.startswith("_") + and not (entry.get("package") and name not in old.__all__ + and isinstance(value, ModuleType) and value.__name__ in old_modules)] + assert sorted(public) == sorted(old.__all__) for name in entry["exports"]: value = getattr(canonical, name) + if name in entry.get("module_exports", {}): + binding = entry["module_exports"][name] + assert any(item["old"] == binding["old"] and item["canonical"] == binding["canonical"] + for item in MIGRATIONS) + assert getattr(old, name) is import_module(binding["old"]) + assert value is import_module(binding["canonical"]) + continue assert getattr(old, name) is value # Generic aliases retain stdlib metadata; only definitions move modules. if get_origin(value) is None and (inspect.isfunction(value) or inspect.isclass(value)): @@ -70,6 +90,53 @@ def test_generic_alias_identity_does_not_depend_on_inspect_isclass(monkeypatch): test_shim_is_an_explicit_export_of_the_same_objects(entry) +@pytest.mark.parametrize("child_first", [False, True]) +def test_legacy_cannbot_package_keeps_its_project_module_in_either_import_order(child_first): + script = """ +import importlib, json, sys +parent = 'inferrsi.apps.cannbot' +if sys.argv[1] == 'True': + importlib.import_module(parent + '.project') +old = importlib.import_module(parent) +child = importlib.import_module(parent + '.project') +canonical = importlib.import_module('inferrsi.apps.integrations.cannbot') +importlib.import_module(parent + '.read_worker') +assert old.project is child +assert child is not canonical.project +assert old.CannbotChannel is canonical.CannbotChannel +assert all(getattr(child, name) is getattr(canonical.project, name) for name in child.__all__) +print(json.dumps({'compatible': True})) +""" + assert _fresh(script, str(child_first)) == {"compatible": True} + + +@pytest.mark.parametrize("system", ["cannbot", "mindie"]) +def test_project_launchers_reference_the_canonical_standalone_worker(system): + project = import_module(f"inferrsi.apps.integrations.{system}.project") + legacy = import_module(f"inferrsi.apps.{system}.project") + worker = ROOT / "apps/integrations" / system / "read_worker.py" + assert project._WORKER == worker + assert Path(project.__file__).with_name("read_worker.py") == worker + operation = "read" if system == "cannbot" else "call" + assert getattr(legacy, operation) is getattr(project, operation) + script = """ +import ast, json, runpy, sys +from pathlib import Path +path = Path(sys.argv[1]) +tree = ast.parse(path.read_text(encoding='utf-8')) +top = [node for node in tree.body if isinstance(node, (ast.Import, ast.ImportFrom))] +for node in top: + assert not (isinstance(node, ast.ImportFrom) and node.level) + names = [node.module] if isinstance(node, ast.ImportFrom) else [n.name for n in node.names] + assert all(name.split('.')[0] in sys.stdlib_module_names for name in names) +namespace = runpy.run_path(str(path), run_name='not_main') +assert callable(namespace['read']) +assert not any(n.split('.')[0] in ('inferrsi', 'mindie_knowledge') for n in sys.modules) +print(json.dumps({'standalone': True})) +""" + assert _fresh(script, str(worker)) == {"standalone": True} + + def test_old_run_output_instance_pickle_loads_and_preserves_its_data(): # Construct the legacy bytes using the original definition from the old module. source = """from dataclasses import dataclass @@ -99,7 +166,9 @@ def test_active_consumers_use_canonical_paths_and_are_registered(): modules = support._module_index(ROOT) old_names = {entry["old"] for entry in MIGRATIONS} entries = {entry["canonical"]: entry for entry in MIGRATIONS} - shims = {name.replace(".", "/") + ".py" for name in old_names} + shims = {entry["old"].replace(".", "/") + ( + "/__init__.py" if entry.get("package") else ".py" + ) for entry in MIGRATIONS} for entry in MIGRATIONS: assert entry["consumers"] and len(entry["consumers"]) == len(set(entry["consumers"])) assert all((REPO / path).is_file() for path in entry["consumers"]) @@ -159,7 +228,8 @@ def test_storage_dependency_check_rejects_a_handoff_back_edge(tmp_path, indirect def test_migrated_initializers_import_nothing_and_module_bodies_do_no_io(): - packages = ["inferrsi.apps.storage", "inferrsi.apps.handoff", "inferrsi.apps.workflows"] + packages = ["inferrsi.apps.storage", "inferrsi.apps.handoff", "inferrsi.apps.workflows", + "inferrsi.apps.integrations", "inferrsi.apps.integrations.mindie"] for package in packages: path = REPO / "src" / package.replace(".", "/") / "__init__.py" tree = ast.parse(path.read_text(encoding="utf-8")) @@ -223,7 +293,7 @@ def test_delivered_context_draft_loads_a_legacy_instance_pickle(): assert type(restored) is DeliveredContextDraft and restored == value -@pytest.mark.parametrize("group", ["handoff", "workflows"]) +@pytest.mark.parametrize("group", ["handoff", "workflows", "integrations"]) def test_cold_handoff_and_workflow_imports_do_not_read_data_probe_or_launch_services(group): script = """ import builtins, importlib, importlib.abc, io, json, socket, sqlite3, subprocess, sys, warnings diff --git a/tests/test_cannbot_boundaries.py b/tests/test_cannbot_boundaries.py index 15a7ef87e..0db06500e 100644 --- a/tests/test_cannbot_boundaries.py +++ b/tests/test_cannbot_boundaries.py @@ -11,8 +11,8 @@ import pytest -from inferrsi.apps.cannbot import project -from inferrsi.apps.cannbot.read_worker import _MAX_UPSTREAM +from inferrsi.apps.integrations.cannbot import project +from inferrsi.apps.integrations.cannbot.read_worker import _MAX_UPSTREAM REPO = Path(__file__).resolve().parents[1] REVISION = "ab" * 20 @@ -218,7 +218,7 @@ def test_read_rejects_revision_drift_with_unchanged_card(upstream, page): def test_read_rejects_revision_change_during_read(upstream, monkeypatch, capsys): - from inferrsi.apps.cannbot import read_worker + from inferrsi.apps.integrations.cannbot import read_worker source, _, _ = upstream original = read_worker._card_bytes diff --git a/tests/test_contracts_package.py b/tests/test_contracts_package.py index b8644389e..7ac566f68 100644 --- a/tests/test_contracts_package.py +++ b/tests/test_contracts_package.py @@ -71,7 +71,7 @@ def hook(event, args): sys.addaudithook(hook) defined = {} for name, package, code in bodies: - namespace = {"__name__": name, "__package__": package} + namespace = {"__name__": name, "__package__": package, "__file__": code.co_filename} active[:] = [name] exec(code, namespace) active.clear() diff --git a/tests/test_first_wave_isolation_policy.py b/tests/test_first_wave_isolation_policy.py index 23e93be8a..4828db4cf 100644 --- a/tests/test_first_wave_isolation_policy.py +++ b/tests/test_first_wave_isolation_policy.py @@ -427,6 +427,7 @@ def test_another_importer_fails(self) -> None: "benchmark_operations.py", "apps/benchmark_operations.py", "summary_chart.py", "apps/views/summary_chart.py", "mindie_project.py", "apps/mindie/project.py", "_mindie_read_worker.py", "apps/mindie/read_worker.py", "helper.py", "catalog.py", + "apps/integrations/mindie/project.py", "apps/integrations/mindie/read_worker.py", "_repo_pack_data.py", ) for path in paths: diff --git a/tests/test_mindie_context.py b/tests/test_mindie_context.py index 5b8d67bd8..786d703df 100644 --- a/tests/test_mindie_context.py +++ b/tests/test_mindie_context.py @@ -9,7 +9,7 @@ import pytest from inferrsi.apps.cli import main -from inferrsi.apps.mindie.context import export_context +from inferrsi.apps.integrations.mindie.context import export_context from inferrsi.apps.views.dashboard_data import load_dashboard from inferrsi.harness.records.campaign_records import create_campaign diff --git a/tests/test_mindie_project.py b/tests/test_mindie_project.py index 05d3b99e2..5b94b79d5 100644 --- a/tests/test_mindie_project.py +++ b/tests/test_mindie_project.py @@ -3,8 +3,8 @@ from pathlib import Path from inferrsi.apps.cli import main -from inferrsi.apps.mindie import mcp as mindie_mcp -from inferrsi.apps.mindie import project as mindie_project +from inferrsi.apps.integrations.mindie import mcp as mindie_mcp +from inferrsi.apps.integrations.mindie import project as mindie_project def test_project_enable_discover_read_and_revoke(tmp_path, monkeypatch, capsys): diff --git a/tests/test_mindie_runtime.py b/tests/test_mindie_runtime.py index 1e8560766..5eb9ec854 100644 --- a/tests/test_mindie_runtime.py +++ b/tests/test_mindie_runtime.py @@ -11,8 +11,8 @@ import pytest -from inferrsi.apps.mindie import project as mindie_project -from inferrsi.apps.mindie.context import MAX_BYTES +from inferrsi.apps.integrations.mindie import project as mindie_project +from inferrsi.apps.integrations.mindie.context import MAX_BYTES @pytest.fixture @@ -236,7 +236,7 @@ def test_mcp_stdio_round_trip(reader, tmp_path, monkeypatch, oversized_line): wire += "\n" if oversized_line != "eof": wire += '{"jsonrpc":"2.0","id":7,"method":"ping"}\n' - source = str(Path(mindie_project.__file__).resolve().parents[3]) + source = str(Path(__file__).resolve().parents[1] / "src") process = subprocess.run( [sys.executable, "-I", "-B", "-c", f"import sys; sys.path.insert(0, {source!r}); " "from inferrsi.apps.cli import main; raise SystemExit(main(['mindie', 'serve']))"], diff --git a/tests/test_package_root_inventory.py b/tests/test_package_root_inventory.py index a2bc065df..8d9e3dff1 100644 --- a/tests/test_package_root_inventory.py +++ b/tests/test_package_root_inventory.py @@ -20,7 +20,7 @@ # retired: seven Knowledge shells, three old-run forwarders and the root inspection # binding, whose implementations are in knowledge/ and apps/, the two that A1 moved # into apps/cli/, the two that A2 moved into apps/ and the four that M1 moved into -# apps/mindie/. +# apps/integrations/mindie/. RETIRED_BY_C1 = frozenset({"_json_v1.py", "claim.py", "contracts.py", "runspec.py"}) RETIRED_BY_C2 = frozenset({"_repo_pack_data.py", "evidence_paths.py"}) RETIRED_BY_R1 = frozenset( @@ -43,7 +43,7 @@ # A2 (#408) moved the chart renderer to apps/views/ and the task launcher to apps/, and # deleted both root files. RETIRED_BY_A2 = frozenset({"benchmark_operations.py", "summary_chart.py"}) -# M1 (#452) moved the MindIE adapter to apps/mindie/ and deleted the four root files. +# M1 (#452) moved the MindIE adapter to apps/integrations/mindie/ and deleted the four root files. RETIRED_BY_M1 = frozenset( {"_mindie_read_worker.py", "mindie_context.py", "mindie_mcp.py", "mindie_project.py"} ) @@ -109,12 +109,12 @@ def test_inventory_is_the_exact_final_two_file_root() -> None: assert len(ROOT_FILES | RETIRED_BY_M1) == 6 assert GENERATED not in ROOT_FILES assert ROOT_FILES == {"__init__.py", "__main__.py"} - # M1 moved its four files; the implementation is the apps/mindie package. + # M1 moved its four files into Apps; #490 groups the implementation in integrations. for name in ("project.py", "context.py", "mcp.py", "read_worker.py"): - assert (REPO / PACKAGE / "apps" / "mindie" / name).is_file(), name + assert (REPO / PACKAGE / "apps" / "integrations" / "mindie" / name).is_file(), name # The package initializer stays import-free, so importing it loads no core and # does not re-export the package version. - initializer = ast.parse((REPO / PACKAGE / "apps" / "mindie" / "__init__.py").read_text( + initializer = ast.parse((REPO / PACKAGE / "apps/integrations/mindie/__init__.py").read_text( encoding="utf-8")) assert not [node for node in ast.walk(initializer) if isinstance(node, (ast.Import, ast.ImportFrom))] diff --git a/tests/test_target_module_import_gate.py b/tests/test_target_module_import_gate.py index 9d0864a16..7827ad4ff 100644 --- a/tests/test_target_module_import_gate.py +++ b/tests/test_target_module_import_gate.py @@ -272,7 +272,9 @@ def test_no_new_cross_core_imports(self) -> None: for moved in ("cli/__init__.py", "cli/arguments.py", "benchmark_operations.py", "workflows/benchmark_operations.py", "views/summary_chart.py", "mindie/project.py", "mindie/context.py", - "mindie/mcp.py", "mindie/read_worker.py"): + "mindie/mcp.py", "mindie/read_worker.py", + "integrations/mindie/project.py", "integrations/mindie/context.py", + "integrations/mindie/mcp.py", "integrations/mindie/read_worker.py"): self.assertTrue((ROOT / "apps" / moved).is_file(), moved) def test_first_wave_policy_holds_on_current_tree(self) -> None: From 99c23738ecb5ea35b456cc1608c7eb624f680a11 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Fri, 20 Nov 2026 00:29:41 +0800 Subject: [PATCH 2/2] docs(apps): clarify legacy MindIE package marker Agent-assisted-by: Codex (author) Signed-off-by: zengchuang --- src/inferrsi/apps/mindie/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inferrsi/apps/mindie/__init__.py b/src/inferrsi/apps/mindie/__init__.py index 58b42d599..b7a5d082b 100644 --- a/src/inferrsi/apps/mindie/__init__.py +++ b/src/inferrsi/apps/mindie/__init__.py @@ -1,3 +1,3 @@ -"""Compatibility exports for :mod:`inferrsi.apps.integrations.mindie`.""" +"""Compatibility package marker for legacy MindIE submodule imports.""" __all__ = []