From aba9b10c9395d22aa0cbc446a0f592843040dbc5 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Fri, 20 Nov 2026 00:50:59 +0800 Subject: [PATCH] refactor(apps): group MindIE adapter under integrations Agent-assisted-by: Codex (author) Signed-off-by: zengchuang --- docs/apps-module-migrations.json | 79 ++++++ docs/architecture.md | 15 +- docs/external-channels.md | 2 +- docs/module-layout.md | 51 ++-- src/inferrsi/apps/cli/__init__.py | 4 +- .../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 | 67 +++-- tests/test_apps_module_migrations.py | 4 +- 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 +- 24 files changed, 835 insertions(+), 712 deletions(-) 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 b35a150dc..7d6942a37 100644 --- a/docs/apps-module-migrations.json +++ b/docs/apps-module-migrations.json @@ -382,5 +382,84 @@ "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 1ed3273bc..492631b2a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -220,11 +220,11 @@ src/inferrsi/ __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 - mcp.py Stdio MCP for the two approved local MindIE reads - read_worker.py Standalone worker run by the approved MindIE 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 @@ -293,8 +293,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 c1e7b97ae..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, workflows and Cannbot integrations 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. MindIE integration -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,14 +89,14 @@ 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 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. +`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 @@ -107,13 +107,13 @@ 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. +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 @@ -140,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 @@ -214,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 | @@ -632,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/cli/__init__.py b/src/inferrsi/apps/cli/__init__.py index e6dc3d900..efe9074ca 100644 --- a/src/inferrsi/apps/cli/__init__.py +++ b/src/inferrsi/apps/cli/__init__.py @@ -297,7 +297,7 @@ 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: @@ -617,7 +617,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/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..b7a5d082b 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 package marker for legacy MindIE submodule imports.""" -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 49ea01e31..6aabc5f5e 100644 --- a/tests/check_installed.py +++ b/tests/check_installed.py @@ -661,7 +661,9 @@ def _inventory(root, prefix): ("improvement", "workflows"), ("skill_evaluation", "workflows"), ("skill_eval_engineering", "workflows"), ("benchmark_compare", "workflows"), ("cannbot", "integrations"), ("cannbot.project", "integrations"), - ("cannbot.read_worker", "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}" @@ -745,7 +747,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 @@ -759,8 +761,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" @@ -791,12 +795,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" @@ -808,27 +813,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 @@ -836,8 +843,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" diff --git a/tests/test_apps_module_migrations.py b/tests/test_apps_module_migrations.py index 47ba6768e..85b5c20a8 100644 --- a/tests/test_apps_module_migrations.py +++ b/tests/test_apps_module_migrations.py @@ -110,7 +110,7 @@ def test_legacy_cannbot_package_keeps_its_project_module_in_either_import_order( assert _fresh(script, str(child_first)) == {"compatible": True} -@pytest.mark.parametrize("system", ["cannbot"]) +@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") @@ -229,7 +229,7 @@ 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", - "inferrsi.apps.integrations"] + "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")) 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: