diff --git a/docs/apps-module-migrations.json b/docs/apps-module-migrations.json index 9090a5956..854eb10db 100644 --- a/docs/apps-module-migrations.json +++ b/docs/apps-module-migrations.json @@ -321,5 +321,64 @@ "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" + ] } ] diff --git a/docs/architecture.md b/docs/architecture.md index 0ea517ba0..8ef9310db 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -215,6 +215,11 @@ 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 + 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, the worker launch and the recorded read context.py Export supplied MindIE reads into a caller-reviewed campaign trace diff --git a/docs/module-layout.md b/docs/module-layout.md index 3edb8ca37..c1e7b97ae 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 Cannbot 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. MindIE integration +and integrated acceptance remain subsequent #490 slices. ### 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 Cannbot integration migration + +`apps/integrations/cannbot/` owns the Cannbot channel, project grant and reader +worker. Its CLI commands import the canonical implementation on demand. The new +`integrations` initializer imports nothing. Cannbot retains its existing +`CannbotChannel` definition in the package initializer; importing it does not +read grants or start workers. MindIE remains in `apps/mindie/` until its separate +integration slice. + +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. + +The Cannbot project adapter resolves its sibling worker in the canonical +directory. Registered launchers use that file with the approved interpreter and +the existing `-I -B` flags. The worker loads without InferRSI installed or a +package context, and its top-level imports are standard-library-only. The legacy +worker path retains Python import compatibility; external interpreters use the +canonical file. Grant schemas, read-only access, limits, revocation and receipts +are unchanged. + ### Core and technical packages The four target modules are peers: `knowledge`, `harness`, `benchmark`, and 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..55d456532 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) @@ -309,7 +309,7 @@ def main(argv: list[str] | None = None) -> int: _print(result) return 0 if args.command == "cannbot": - from .. import cannbot + from ..integrations import cannbot action = args.cannbot_action if action == "enable": 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/tests/check_installed.py b/tests/check_installed.py index 58bb4e996..f6a9e384e 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,8 @@ 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"), ): old_name = f"inferrsi.apps.{name}" canonical_name = f"inferrsi.apps.{group}.{name}" @@ -629,6 +632,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 @@ -877,3 +884,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..47ba6768e 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"]) +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"] 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()