diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 817472ac9..e50689a4b 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -129,3 +129,12 @@ When normal ID normalization removes an entire binding name, its UTF-8 bytes provide a deterministic operator suffix within the same lexical owner. Its double underscore separators cannot collide with an ordinary normalized binding ID. Local call resolution stops if malformed scope metadata cycles. + +## Agent Integrations + +Graphify integrates with 20+ AI coding assistants (Claude Code, Google Antigravity, Xiaomi MiMo Code, OpenCode, Codex, Cursor, etc.) via `graphify/install.py`: + +- **Skill discovery**: installs `SKILL.md` along with progressive reference sidecars (`references/`) into platform-native skill directories (`.mimocode/skills/graphify/` for MiMo Code, `.agents/skills/graphify/` for Antigravity, `.claude/skills/graphify/` for Claude Code). +- **Runtime context retrieval**: platforms with native plugin/hook systems (such as MiMo Code's `chat.message` and `tool.execute.before` hooks, or Claude Code's `PreToolUse` hook) automatically execute bounded, non-shell `graphify query` searches against `graphify-out/graph.json` before model responses. +- **Persistent instructions**: written to root instruction files (`AGENTS.md`, `CLAUDE.md`, `.cursor/rules/`) enforcing graph-first exploration before raw file inspection. +- **Scope management**: all integrations support user-global installs and project-scoped installs (`--project`), with idempotent registration and safe, surgical uninstallation that preserves user modifications. diff --git a/README.md b/README.md index ed4b09f4d..7a8128926 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,7 @@ for example `graphify claude install --project` or `graphify codex install --pro | Cursor | `graphify cursor install` | | Devin CLI | `graphify devin install` | | Google Antigravity | `graphify antigravity install` | +| Xiaomi MiMo Code | `graphify mimo install` | Codex users also need `multi_agent = true` under `[features]` in `~/.codex/config.toml` for parallel extraction. CodeBuddy uses the same Agent tool and PreToolUse hook mechanism as Claude Code. Factory Droid uses the `Task` tool for parallel subagent dispatch. OpenClaw and Aider use sequential extraction (parallel agent support is still early on those platforms). Trae uses the Agent tool for parallel subagent dispatch and does **not** support `PreToolUse` hooks, so AGENTS.md is the always-on mechanism. @@ -308,7 +309,7 @@ Run `graphify install` once in your project, for example `graphify cl This writes a small config file that tells your assistant to consult the knowledge graph for codebase questions, preferring scoped queries like `graphify query ""` over reading the full report or grepping raw files. -- **Hook platforms** (Claude Code, Gemini CLI): a hook fires automatically before search-style tool calls (and, on Claude Code, before reading source files one by one via the Read/Glob tools) and nudges your assistant toward the graph path. +- **Hook platforms** (Claude Code, Gemini CLI, Xiaomi MiMo Code): a hook or plugin fires automatically before search-style tool calls or message responses, retrieving focused knowledge graph context. - **Instruction-file platforms** (Codex, OpenCode, Cursor, etc.): persistent instruction files (`AGENTS.md`, `.cursor/rules/`, etc.) provide the same query-first guidance. `GRAPH_REPORT.md` is still available for broad architecture review. @@ -321,6 +322,8 @@ This writes a small config file that tells your assistant to consult the knowled **Cursor** writes `.cursor/rules/graphify.mdc` with `alwaysApply: true`, so Cursor includes it in every conversation automatically, no hook needed. +**Xiaomi MiMo Code** (`graphify mimo install`): installs the Graphify skill (global `~/.config/mimocode/skills/` or project `.mimocode/skills/`), registers native `chat.message` and `tool.execute.before` plugin hooks in `mimocode.json` to automatically retrieve scoped graph context before answering user questions, and writes persistent instructions to `AGENTS.md`. Supports `graphify mimo install [--project]`, `graphify mimo uninstall [--project]`, and `graphify mimo status [--project]`. + To remove graphify from all platforms at once: `graphify uninstall` (add `--purge` to also delete `graphify-out/`). Or use the per-platform command (e.g. `graphify claude uninstall`). ### CLI skills or MCP instructions? diff --git a/graphify/__main__.py b/graphify/__main__.py index 579087662..2bc421071 100644 --- a/graphify/__main__.py +++ b/graphify/__main__.py @@ -55,6 +55,7 @@ _install_codex_hook, _install_gemini_hook, _install_kilo_plugin, + _install_mimo_plugin, _install_opencode_plugin, _install_skill_references, _kilo_config_path, @@ -65,6 +66,9 @@ _kiro_install, _kiro_uninstall, _load_json_like, + _mimo_install, + _mimo_status, + _mimo_uninstall, _packaged_skill_refs_dir, _platform_skill_destination, _print_banner, @@ -87,6 +91,7 @@ _uninstall_codex_hook, _uninstall_gemini_hook, _uninstall_kilo_plugin, + _uninstall_mimo_plugin, _uninstall_opencode_plugin, _vscode_skill_destination, claude_install, @@ -116,6 +121,7 @@ _KILO_PLUGIN_PATH, _KILO_CONFIG_JSON_PATH, _KILO_CONFIG_JSONC_PATH, + _MIMO_PLUGIN_JS, _OPENCODE_PLUGIN_JS, _OPENCODE_PLUGIN_PATH, _OPENCODE_CONFIG_PATH, @@ -1049,6 +1055,15 @@ def _run_cli() -> None: print( " antigravity uninstall remove .agents/rules, .agents/workflows, and skill" ) + print( + " mimo install configure Xiaomi MiMo Code (skill + plugin + AGENTS.md)" + ) + print( + " mimo uninstall remove Xiaomi MiMo Code configuration" + ) + print( + " mimo status check Xiaomi MiMo Code integration status" + ) print( " hermes install write skill to ~/.hermes/skills/graphify/ (Hermes)" ) diff --git a/graphify/detect.py b/graphify/detect.py index 3a17041f8..cd655253e 100644 --- a/graphify/detect.py +++ b/graphify/detect.py @@ -990,6 +990,7 @@ def _is_regular_file(path: Path) -> bool: (".cursor", "rules", "graphify.mdc"), # cursor (".kilo", "plugins", "graphify.js"), # kilo (".kiro", "steering", "graphify.md"), # kiro + (".mimocode", "plugins", "graphify.js"), # mimo (".opencode", "plugins", "graphify.js"), # opencode (".windsurf", "rules", "graphify.md"), # devin }) diff --git a/graphify/install.py b/graphify/install.py index 5291056c7..070ae7cc5 100644 --- a/graphify/install.py +++ b/graphify/install.py @@ -47,32 +47,19 @@ def _skill_lock_path(skill_dir: Path) -> Path: - """The lock file for ``skill_dir``, kept out of the skill directory. - - Inside it, a project-scope install would leave the file in the user's - repository, and uninstall would have to delete a file another process may - still hold - letting that process and a new one both own the lock. - """ + """The lock file for ``skill_dir``, kept out of the skill directory.""" try: real = skill_dir.resolve() - except (OSError, RuntimeError): # symlink loop + except (OSError, RuntimeError): real = skill_dir.absolute() digest = hashlib.sha256(os.path.normcase(str(real)).encode("utf-8")).hexdigest()[:16] return Path.home() / ".cache" / "graphify" / "skill-locks" / f"{digest}.lock" -# Lock files this thread holds: the CLI's refresh takes the lock and then calls -# _copy_skill_file, which takes it again. Per thread, so another thread of the -# same process still waits its turn. _held_skill_locks = threading.local() def _try_skill_lock(fh) -> bool: - """Take the lock on ``fh`` without waiting; False if another process holds it. - - Raises OSError when the file system cannot lock at all (e.g. some network - mounts). - """ try: if fcntl is not None: fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) @@ -97,23 +84,6 @@ def _release_skill_lock(fh) -> None: @contextlib.contextmanager def _skill_lock(skill_dir: Path, *, wait: bool = True): - """Serialize every process that writes ``skill_dir``. - - Install, uninstall and the CLI's stale-skill refresh (#1805) all write the - same files and stage through fixed ``SKILL.md.tmp`` / ``references.tmp`` - names, so two of them at once would write through each other - and an - uninstall racing a refresh could see the refresh put the skill back. - - Yields True while this process holds the lock. With ``wait`` off (the - refresh) it yields False instead of waiting when another process holds it. - It yields None when locking is unavailable here (no writable cache - directory, a file system that refuses to lock): install and uninstall then - go ahead unlocked, as they did before the lock existed; the refresh skips. - - An OS lock (flock / msvcrt.locking): the OS releases it however the holder - exits, so a crash or a kill never leaves the directory locked and there is - no staleness timeout to guess. The lock file is never deleted. - """ path = _skill_lock_path(skill_dir) held = _held_skill_locks.__dict__.setdefault("paths", set()) if path in held: @@ -147,14 +117,6 @@ def _skill_lock(skill_dir: Path, *, wait: bool = True): def _write_version_stamp(skill_dst: Path, version: str) -> None: - """Atomically write ``.graphify_version`` beside ``skill_dst``. - - Matches the SKILL.md install path: temp file in the same directory, then - ``os.replace``. Unlike ``Path.write_text`` (and unlike - ``paths.write_text_atomic``, which resolves through symlinks), ``os.replace`` - replaces a managed symlink in place — consistent with SKILL.md / - ``references/`` and crash-safe against a half-written stamp (#3286). - """ version_file = skill_dst.parent / ".graphify_version" tmp = version_file.with_name(".graphify_version.tmp") try: @@ -170,29 +132,25 @@ def _write_version_stamp(skill_dst: Path, version: str) -> None: @functools.lru_cache(maxsize=None) def _always_on(basename: str) -> str: - """Read a packaged always-on instruction block from graphify/always_on/. - - The six always-on blocks (CLAUDE.md / AGENTS.md / GEMINI.md / VS Code - Copilot instructions / Antigravity rules / Kiro steering) live as committed - markdown next to this module, generated by tools/skillgen from a single - human-edited fragment and guarded against drift by ``skillgen --check``. The - installer injects them verbatim via ``_replace_or_append_section``, so the - bytes here must match the former triple-quoted constant exactly — the - always-on-roundtrip validator proves that. - """ path = Path(__file__).parent / "always_on" / f"{basename}.md" try: return path.read_text(encoding="utf-8") except OSError as exc: - # Defer to use-time so a missing/corrupt packaged block can't crash module - # import (which would brick every CLI command, not just install). Reached - # only by an install/integration path that actually needs this block. raise RuntimeError( f"graphify install is incomplete: missing always-on block '{basename}' " f"at {path}. Reinstall graphifyy (e.g. `uv tool install --reinstall graphifyy`)." ) from exc + + +def _mimo_global_config_dir() -> Path: + if os.environ.get("MIMOCODE_HOME"): + return Path(os.environ["MIMOCODE_HOME"]) / "config" + if os.environ.get("XDG_CONFIG_HOME"): + return Path(os.environ["XDG_CONFIG_HOME"]) / "mimocode" + return Path.home() / ".config" / "mimocode" + + def _platform_skill_destination(platform_name: str, *, project: bool = False, project_dir: Path | None = None) -> Path: - """Return the skill destination for a platform and scope.""" if platform_name == "gemini": if project: return (project_dir or Path(".")) / ".gemini" / "skills" / "graphify" / "SKILL.md" @@ -208,7 +166,6 @@ def _platform_skill_destination(platform_name: str, *, project: bool = False, pr if platform_name == "hermes": if project: return (project_dir or Path(".")) / ".hermes" / "skills" / "graphify" / "SKILL.md" - # On Windows, Hermes scans %LOCALAPPDATA%\hermes\skills, not ~/.hermes (#1403). if platform.system() == "Windows": local_appdata = Path(os.environ.get("LOCALAPPDATA") or (Path.home() / "AppData" / "Local")) return local_appdata / "hermes" / "skills" / "graphify" / "SKILL.md" @@ -225,9 +182,6 @@ def _platform_skill_destination(platform_name: str, *, project: bool = False, pr return Path.home() / ".config" / "agents" / "skills" / "graphify" / "SKILL.md" if platform_name == "agents": - # The generic Agent-Skills target: project ./.agents/skills, global the - # spec's user-global ~/.agents/skills (read by `npx skills` and compliant - # frameworks), NOT amp's ~/.config/agents/skills. if project: return (project_dir or Path(".")) / ".agents" / "skills" / "graphify" / "SKILL.md" return Path.home() / ".agents" / "skills" / "graphify" / "SKILL.md" @@ -235,9 +189,13 @@ def _platform_skill_destination(platform_name: str, *, project: bool = False, pr if platform_name in ("antigravity", "antigravity-windows"): if project: return (project_dir or Path(".")) / ".agents" / "skills" / "graphify" / "SKILL.md" - # Global Antigravity skill dir (all workspaces): ~/.gemini/config/skills/ return Path.home() / ".gemini" / "config" / "skills" / "graphify" / "SKILL.md" + if platform_name in ("mimo", "mimo-windows"): + if project: + return (project_dir or Path(".")) / ".mimocode" / "skills" / "graphify" / "SKILL.md" + return _mimo_global_config_dir() / "skills" / "graphify" / "SKILL.md" + cfg = _PLATFORM_CONFIG[platform_name] if project: return (project_dir or Path(".")) / cfg["skill_dst"] @@ -245,28 +203,9 @@ def _platform_skill_destination(platform_name: str, *, project: bool = False, pr if platform_name in ("claude", "windows") and os.environ.get("CLAUDE_CONFIG_DIR"): return Path(os.environ["CLAUDE_CONFIG_DIR"]) / "skills" / "graphify" / "SKILL.md" return Path.home() / cfg["skill_dst"] + + def _packaged_skill_refs_dir(platform_name: str) -> Path | None: - """Return the packaged references source dir for a progressive platform, else None. - - A platform opts into progressive disclosure by setting ``skill_refs`` in its - ``_PLATFORM_CONFIG`` entry. The value names a bundle under - ``graphify/skills//references/``. Reuse keys (e.g. trae-cn) point at - their twin's bundle. - - ``gemini`` has no ``_PLATFORM_CONFIG`` entry: it installs claude's - ``skill.md`` body verbatim (see ``_copy_skill_file``). Since that body is the - lean progressive core that links to ``references/``, gemini needs claude's - references/ sidecar too, or its SKILL.md ships with dead pointers. So gemini - resolves to the claude bundle rather than opting out. - - Bundles ship one platform-group at a time. A host whose bundle directory - ``graphify/skills//`` is not in this build has not gone progressive - yet, so this returns None and the host installs today's monolithic SKILL.md - with no references/ sidecar. Only when the bundle directory IS present does - this return the references path; if that directory then lacks its - ``references/`` subdir, ``_copy_skill_file`` hard-fails (a malformed bundle, - the empty-sidecar regression the wheel-content test also guards). - """ if platform_name == "gemini": bundle = "claude" else: @@ -277,25 +216,15 @@ def _packaged_skill_refs_dir(platform_name: str) -> Path | None: if not bundle_dir.is_dir(): return None return bundle_dir / "references" -def _install_skill_references(skill_dst: Path, refs_src: Path) -> None: - """Atomically install a packaged references/ sidecar next to SKILL.md. - Stages the packaged dir into ``references.tmp`` (copytree), drops any stale - ``references/`` already on disk, then ``os.replace``-renames the staged dir - into place. The rename is atomic on the same filesystem, so an interrupted - install never leaves a half-written references/ visible to the agent. - """ + +def _install_skill_references(skill_dst: Path, refs_src: Path) -> None: refs_dst = skill_dst.parent / "references" refs_staged = skill_dst.parent / "references.tmp" if refs_staged.exists(): shutil.rmtree(refs_staged) try: shutil.copytree(refs_src, refs_staged) - # copytree preserves the source's mode bits, and a packaged bundle can - # be read-only: a Nix store path, a root-owned site-packages, a - # container image layer. Renaming a directory needs write permission on - # the directory itself, to update its ".." entry, so the os.replace - # below would fail with EACCES. Restore owner-write on the staged copy. for path in (refs_staged, *refs_staged.rglob("*")): path.chmod(path.stat().st_mode | stat.S_IWUSR) if refs_dst.exists(): @@ -305,15 +234,9 @@ def _install_skill_references(skill_dst: Path, refs_src: Path) -> None: if refs_staged.exists(): shutil.rmtree(refs_staged, ignore_errors=True) raise -def _copy_skill_file(platform_name: str, *, project: bool = False, project_dir: Path | None = None) -> Path: - """Copy a packaged skill file and write its version stamp. - For progressive platforms (those with ``skill_refs`` set), the packaged - ``references/`` sidecar is installed alongside SKILL.md and the single - ``.graphify_version`` stamp covers both. For monolith platforms (no - ``skill_refs``), any orphan ``references/`` left by a prior progressive - install is removed so the on-disk layout matches the package. - """ + +def _copy_skill_file(platform_name: str, *, project: bool = False, project_dir: Path | None = None) -> Path: skill_file = "skill.md" if platform_name == "gemini" else _PLATFORM_CONFIG[platform_name]["skill_file"] skill_src = Path(__file__).parent / skill_file if not skill_src.exists(): @@ -322,8 +245,6 @@ def _copy_skill_file(platform_name: str, *, project: bool = False, project_dir: refs_src = _packaged_skill_refs_dir(platform_name) if refs_src is not None and not refs_src.exists(): - # Progressive platform declared a references bundle that is missing from - # the package. Fail loud rather than silently shipping an empty sidecar. print( f"error: references for '{platform_name}' not found in package " f"({refs_src}) - reinstall graphify", @@ -334,34 +255,22 @@ def _copy_skill_file(platform_name: str, *, project: bool = False, project_dir: skill_dst = _platform_skill_destination(platform_name, project=project, project_dir=project_dir) with _skill_lock(skill_dst.parent): skill_dst.parent.mkdir(parents=True, exist_ok=True) - - # Install the references/ sidecar (or clear an orphan one) BEFORE writing - # SKILL.md, so SKILL.md is the last artifact laid down. An install that is - # interrupted partway then leaves no SKILL.md rather than a SKILL.md that - # points at an absent references/ dir. if refs_src is not None: _install_skill_references(skill_dst, refs_src) print(f" references -> {skill_dst.parent / 'references'}") else: - # Monolith (or progressive-with-no-refs): clear any orphan references/. orphan_refs = skill_dst.parent / "references" if orphan_refs.exists(): shutil.rmtree(orphan_refs) - # A SKILL.md that differs from what is about to be written may carry the - # user's local edits (a tuned description:, extra guidance); replacing it - # wholesale with only "skill installed ->" for output read like a no-op - # while the edits were gone (#3144). Keep one .bak beside it and say so. - # Every upgrade differs too - the .bak is overwritten each install, so it - # always holds exactly the previous copy. try: if skill_dst.exists() and skill_dst.read_bytes() != skill_src.read_bytes(): backup = skill_dst.with_suffix(skill_dst.suffix + ".bak") shutil.copy2(skill_dst, backup) print(f" previous copy -> {backup} (differed from the packaged skill)") except OSError: - pass # a failed backup must not block the install - # SKILL.md last (crash-safety), via an atomic temp + rename. + pass + tmp_dst = skill_dst.with_suffix(skill_dst.suffix + ".tmp") try: shutil.copy(skill_src, tmp_dst) @@ -376,8 +285,9 @@ def _copy_skill_file(platform_name: str, *, project: bool = False, project_dir: _write_version_stamp(skill_dst, __version__) print(f" skill installed -> {skill_dst}") return skill_dst + + def _remove_skill_file(platform_name: str, *, project: bool = False, project_dir: Path | None = None) -> bool: - """Remove a platform skill file and its version stamp without touching other scopes.""" skill_dst = _platform_skill_destination(platform_name, project=project, project_dir=project_dir) removed = False with _skill_lock(skill_dst.parent): @@ -399,36 +309,32 @@ def _remove_skill_file(platform_name: str, *, project: bool = False, project_dir except OSError: break return removed + + def _project_scope_root(path: Path, project_dir: Path) -> Path: - """Return the top-level project artifact for a project-scoped skill path.""" try: rel = path.relative_to(project_dir) except ValueError: return path return project_dir / rel.parts[0] if rel.parts else path + + def _remove_claude_skill_registration(project_dir: Path) -> None: - """Remove the project-scoped Claude skill registration file/section.""" claude_md = project_dir / ".claude" / "CLAUDE.md" if not claude_md.exists(): return content, newline = _read_keep_newline(claude_md) - # Match the exact H1 `# graphify` registration heading, never a substring of a - # user's `## graphify`/`### graphify` (#2062). Section runs to the next H1. - # _SKILL_REGISTRATION_MARKER is the single source of truth for this heading, - # shared with _register_always_on_block so an insert and its removal always - # agree on what they're matching (#3668). cleaned = _remove_marker_section(content, _SKILL_REGISTRATION_MARKER, boundary_prefix="# ") if cleaned is None: return - # A symlink stays: writing through it cleans the file it points to. if cleaned or claude_md.is_symlink(): - # The file's own line ending is restored, so the rest of its content is - # never re-translated on write (#3668/#4360). _write_keep_newline(claude_md, cleaned + "\n", newline) print(f" CLAUDE.md -> graphify skill registration removed from {_shown_path(claude_md)}") else: claude_md.unlink() print(f" CLAUDE.md -> deleted {claude_md}") + + def _print_project_git_add_hint(paths: list[Path]) -> None: unique: list[str] = [] for path in paths: @@ -442,59 +348,25 @@ def _print_project_git_add_hint(paths: list[Path]) -> None: print() print("Project-scoped install. Add to version control:") print(f" git add {' '.join(unique)}") -def _claude_pretooluse_hooks(strict: bool = False, project: bool = False) -> "list[dict]": - """graphify's Claude/Codebuddy PreToolUse hooks, resolved at install time. - - The command invokes `graphify hook-guard ` via the absolute exe - path (`_resolve_graphify_exe`) — or, for a project-scoped install, via the - bare `graphify` command, since that config gets committed (#3129). Either - form parses under sh, cmd.exe and PowerShell alike — this is the #522 fix, - and mirrors the codex hook. Matchers are - "Bash|Grep" and "Read|Glob" and the command always contains "graphify", so the - existing install/uninstall filters find and replace both old bash hooks and - these. "Grep" is in the search matcher because current Claude Code routes - content search through its dedicated Grep tool, not Bash (#1986) — a - Bash-only matcher never fired on the agent's primary search path. - - When ``strict`` is set, the read hook carries ``--strict`` so it blocks the - first raw read per session (Claude Code only). The ``GRAPHIFY_HOOK_STRICT`` env - var can force it on or off at runtime without a reinstall. - - Each entry carries a ``timeout`` (#3314): unset, Claude Code defaults a - command hook to 600s, so a single wedged guard (a stuck filesystem, a - hung subprocess) stalls the surrounding tool call for ten minutes on - every Bash/Grep/Read/Glob call -- the four highest-frequency tools an - agent uses. The guard itself measures ~170ms warm; 10s is generous - headroom over that while still two orders of magnitude below the - unset default. - """ + + +def _claude_pretooluse_hooks(strict: bool = False, project: bool = False) -> list[dict]: exe = _resolve_graphify_exe(project=project) if " " in exe and not exe.startswith('"'): exe = f'"{exe}"' read_cmd = f"{exe} hook-guard read" + (" --strict" if strict else "") return [ - {"matcher": "Bash|Grep", - "hooks": [{"type": "command", "command": f"{exe} hook-guard search", "timeout": 10}]}, - {"matcher": "Read|Glob", - "hooks": [{"type": "command", "command": read_cmd, "timeout": 10}]}, + {"matcher": "Bash|Grep", "hooks": [{"type": "command", "command": f"{exe} hook-guard search", "timeout": 10}]}, + {"matcher": "Read|Glob", "hooks": [{"type": "command", "command": read_cmd, "timeout": 10}]}, ] -# Tokens that only ever appear in a command graphify itself writes: its -# `hook-guard ` and `hook-check` subcommands. Legacy -# (pre-#522) installs shipped an inline POSIX bash guard instead, whose command -# always gated on graphify's own output dir, so that marker identifies it too. + + _GRAPHIFY_HOOK_SUBCOMMANDS = ("hook-guard", "hook-check") _GRAPHIFY_LEGACY_HOOK_MARKER = "graphify-out/" def _is_graphify_hook_command(command: str) -> bool: - """True when a hook ``command`` string is one graphify installed. - - Matches graphify's own guard/check subcommands (current installs) or the - ``graphify-out/`` path its pre-#522 inline bash guard gated on (legacy - installs). Deliberately command-scoped: the old ``"graphify" in str(group)`` - match also caught an unrelated group whose command path merely contained the - word "graphify" (#4359). - """ + """True when a hook ``command`` string is one graphify installed.""" lowered = command.lower() if "graphify" not in lowered: return False @@ -504,30 +376,16 @@ def _is_graphify_hook_command(command: str) -> bool: def _is_graphify_hook_entry(entry: object) -> bool: - """True when a single ``hooks[]`` element is a graphify command entry. - - Non-dict and non-command entries are never graphify's (the old uninstall - filter called ``.get`` on every entry and raised ``AttributeError`` on a - plain string, #4359). - """ + """True when a single ``hooks[]`` element is a graphify command entry.""" if not isinstance(entry, dict) or entry.get("type") != "command": return False command = entry.get("command") return isinstance(command, str) and _is_graphify_hook_command(command) -def _strip_graphify_hooks(groups: "list") -> "list": - """Drop graphify's own hook entries from *groups*, keeping everything else. - - Only the individual entries graphify installed are removed, so a user - command sharing graphify's group survives and a group is dropped only once - removal leaves it with no hooks. Non-dict groups and non-list ``hooks`` are - passed through untouched, and a group without a graphify entry is never - rewritten. This replaces the old group-level ``"graphify" in str(group)`` - filter, which removed whole groups (and other tools' commands in them) and - any unrelated group whose command path happened to contain the word (#4359). - """ - kept: "list" = [] +def _strip_graphify_hooks(groups: list) -> list: + """Drop graphify's own hook entries from *groups*, keeping everything else.""" + kept: list = [] for group in groups: if not isinstance(group, dict): kept.append(group) @@ -538,14 +396,13 @@ def _strip_graphify_hooks(groups: "list") -> "list": continue remaining = [e for e in entries if not _is_graphify_hook_entry(e)] if len(remaining) == len(entries): - kept.append(group) # nothing of ours in this group + kept.append(group) elif remaining: kept.append({**group, "hooks": remaining}) - # else: the group held only graphify commands -- drop it entirely. return kept + + def _skill_registration(skill_path: str = "~/.claude/skills/graphify/SKILL.md") -> str: - # Heading is "# graphify" (H1) to match _SKILL_REGISTRATION_MARKER, which - # _register_always_on_block anchors its idempotent replace-or-append on. return ( "# graphify\n" f"- **graphify** (`{skill_path}`) " @@ -553,34 +410,17 @@ def _skill_registration(skill_path: str = "~/.claude/skills/graphify/SKILL.md") "When the user types `/graphify`, use the installed graphify skill " "or instructions before doing anything else.\n" ) + + def _shown_path(path: Path) -> str: - """Name *path* for install/uninstall output, plus the file it points to when it - is a symlink. Writing through a symlinked CLAUDE.md edits the target, so - printing only the link hid which file was changed (#3805).""" if path.is_symlink(): return f"{path} -> {path.resolve()}" return str(path) + + def _register_always_on_block(target: Path, prefix: str, registration: str) -> None: """Idempotently add or refresh an always-on registration in *target*, degrading instead of raising. - - Uses _replace_or_append_section (the same marker-anchored helper claude_install - and gemini_install already use, here with the H1 boundary_prefix so it stays - paired with _remove_claude_skill_registration's own H1 match) rather than a - bare append, so a stale or edited block gets refreshed on re-install instead - of the bare "graphify" substring check silently treating any unrelated - mention of the word as already-registered and skipping every re-run (#3668). - - Written through the shared _read_keep_newline/_write_keep_newline pair, so an - existing file's own line endings survive the read-modify-write: a plain - read_text/write_text round-trip turns a bare-LF file into CRLF on Windows (and - a CRLF file into LF elsewhere) just to append a few lines (#3668/#4360). - - The skill files are copied before this runs, so a *target* that cannot be - read or written must not abort an otherwise-complete install (#3474). That - happens whenever the dotfile is managed declaratively -- nix/home-manager - symlinks ``~/.claude/CLAUDE.md`` into a read-only /nix/store, and chezmoi or - stow with read-only sources leave the same shape. """ try: existed = target.exists() @@ -604,182 +444,44 @@ def _register_always_on_block(target: Path, prefix: str, registration: str) -> N f"{target} manually to finish always-on registration", file=sys.stderr, ) + + _PLATFORM_CONFIG: dict[str, dict] = { - "claude": { - "skill_file": "skill.md", - "skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md", - "claude_md": True, - "skill_refs": "claude", - }, - "codex": { - "skill_file": "skill-codex.md", - "skill_dst": Path(".codex") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "codex", - }, - "opencode": { - "skill_file": "skill-opencode.md", - "skill_dst": Path(".config") / "opencode" / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "opencode", - }, - "kilo": { - "skill_file": "skill-kilo.md", - "skill_dst": Path(".config") / "kilo" / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "kilo", - }, - "aider": { - # Monolith: aider ships the full SKILL.md inline, no references/ sidecar. - "skill_file": "skill-aider.md", - "skill_dst": Path(".aider") / "graphify" / "SKILL.md", - "claude_md": False, - }, - "copilot": { - "skill_file": "skill-copilot.md", - "skill_dst": Path(".copilot") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "copilot", - }, - "claw": { - "skill_file": "skill-claw.md", - "skill_dst": Path(".openclaw") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "claw", - }, - "droid": { - "skill_file": "skill-droid.md", - "skill_dst": Path(".factory") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "droid", - }, - "trae": { - "skill_file": "skill-trae.md", - "skill_dst": Path(".trae") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "trae", - }, - "trae-cn": { - # Reuses trae's split bundle (same skill body + references). - "skill_file": "skill-trae.md", - "skill_dst": Path(".trae-cn") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "trae", - }, - "hermes": { - # Reuses claw's split bundle. - "skill_file": "skill-claw.md", - "skill_dst": Path(".hermes") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "claw", - }, - "kiro": { - "skill_file": "skill-kiro.md", - "skill_dst": Path(".kiro") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "kiro", - }, - "pi": { - "skill_file": "skill-pi.md", - "skill_dst": Path(".pi") / "agent" / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "pi", - }, - "codebuddy": { - # Reuses claude's split bundle (shares skill.md). - "skill_file": "skill.md", - "skill_dst": Path(".codebuddy") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "claude", - }, - "antigravity": { - # Rides claude's split bundle (shares skill.md). - "skill_file": "skill.md", - "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "claude", - }, - "antigravity-windows": { - # Rides windows' split bundle. - "skill_file": "skill-windows.md", - "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "windows", - }, - "windows": { - "skill_file": "skill-windows.md", - "skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md", - "claude_md": True, - "skill_refs": "windows", - }, - "kimi": { - # Reuses claude's split bundle (shares skill.md). - "skill_file": "skill.md", - "skill_dst": Path(".kimi") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "claude", - }, - "amp": { - # Amp searches .agents/skills (project) and ~/.config/agents/skills (user), - # not .amp/skills. The user-scope path is set in _platform_skill_destination. - "skill_file": "skill-amp.md", - "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "amp", - }, - "agents": { - # The generic cross-framework Agent-Skills target. Global: ~/.agents/skills - # (the spec's user-global location, read by `npx skills` and compliant - # frameworks); project: ./.agents/skills. The CLI accepts `skills` as an - # alias (see _canonical_platform). Ships its own rendered bundle. - "skill_file": "skill-agents.md", - "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - "skill_refs": "agents", - }, - "devin": { - # Monolith: devin ships the full SKILL.md inline, no references/ sidecar. - "skill_file": "skill-devin.md", - # User scope: ~/.config/devin/skills/graphify/SKILL.md - # Project scope: .devin/skills/graphify/SKILL.md (overridden in _platform_skill_destination) - "skill_dst": Path(".config") / "devin" / "skills" / "graphify" / "SKILL.md", - "claude_md": False, - }, + "claude": {"skill_file": "skill.md", "skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md", "claude_md": True, "skill_refs": "claude"}, + "codex": {"skill_file": "skill-codex.md", "skill_dst": Path(".codex") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "codex"}, + "opencode": {"skill_file": "skill-opencode.md", "skill_dst": Path(".config") / "opencode" / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "opencode"}, + "kilo": {"skill_file": "skill-kilo.md", "skill_dst": Path(".config") / "kilo" / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "kilo"}, + "aider": {"skill_file": "skill-aider.md", "skill_dst": Path(".aider") / "graphify" / "SKILL.md", "claude_md": False}, + "copilot": {"skill_file": "skill-copilot.md", "skill_dst": Path(".copilot") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "copilot"}, + "claw": {"skill_file": "skill-claw.md", "skill_dst": Path(".openclaw") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "claw"}, + "droid": {"skill_file": "skill-droid.md", "skill_dst": Path(".factory") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "droid"}, + "trae": {"skill_file": "skill-trae.md", "skill_dst": Path(".trae") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "trae"}, + "trae-cn": {"skill_file": "skill-trae.md", "skill_dst": Path(".trae-cn") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "trae"}, + "hermes": {"skill_file": "skill-claw.md", "skill_dst": Path(".hermes") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "claw"}, + "kiro": {"skill_file": "skill-kiro.md", "skill_dst": Path(".kiro") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "kiro"}, + "pi": {"skill_file": "skill-pi.md", "skill_dst": Path(".pi") / "agent" / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "pi"}, + "codebuddy": {"skill_file": "skill.md", "skill_dst": Path(".codebuddy") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "claude"}, + "antigravity": {"skill_file": "skill.md", "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "claude"}, + "antigravity-windows": {"skill_file": "skill-windows.md", "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "windows"}, + "mimo": {"skill_file": "skill-opencode.md", "skill_dst": Path(".config") / "mimocode" / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "opencode"}, + "mimo-windows": {"skill_file": "skill-windows.md", "skill_dst": Path(".config") / "mimocode" / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "windows"}, + "windows": {"skill_file": "skill-windows.md", "skill_dst": Path(".claude") / "skills" / "graphify" / "SKILL.md", "claude_md": True, "skill_refs": "windows"}, + "kimi": {"skill_file": "skill.md", "skill_dst": Path(".kimi") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "claude"}, + "amp": {"skill_file": "skill-amp.md", "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "amp"}, + "agents": {"skill_file": "skill-agents.md", "skill_dst": Path(".agents") / "skills" / "graphify" / "SKILL.md", "claude_md": False, "skill_refs": "agents"}, + "devin": {"skill_file": "skill-devin.md", "skill_dst": Path(".config") / "devin" / "skills" / "graphify" / "SKILL.md", "claude_md": False}, } -# CLI-only platform aliases, resolved to a real _PLATFORM_CONFIG key before -# dispatch. `skills` is the friendly alias for the generic `agents` platform -# (the Agent-Skills ecosystem calls them "skills"). + _PLATFORM_ALIASES: dict[str, str] = {"skills": "agents"} + + def _canonical_platform(platform_name: str) -> str: - """Resolve a CLI platform alias to its real _PLATFORM_CONFIG key.""" return _PLATFORM_ALIASES.get(platform_name, platform_name) + + def _replace_or_append_section( content: str, marker: str, new_section: str, boundary_prefix: str = "## " ) -> str: - """Idempotently update or append a graphify-owned section in shared files. - - If no line is exactly ``marker`` (the heading, at column 0), append - ``new_section`` to the end (with a blank-line separator if there's existing - content). - - If a real ``marker`` heading exists, replace the existing section in place. - The section runs from that heading to the line before the next - ``boundary_prefix`` heading (default the next H2), or to EOF if none - follows. This lets older installs receive the updated copy without users - having to uninstall and reinstall (issue #580). - - ``boundary_prefix`` must match whatever level ``marker`` itself is (``"# "`` - for an H1 marker, the default ``"## "`` for an H2 one) — mirrors - ``_remove_marker_section``'s own ``boundary_prefix`` so an insert and its - matching removal agree on where a section ends (#3668). - - The heading is matched only when a line *is* exactly ``marker`` (after - stripping surrounding whitespace), never as a substring. Matching ``## - graphify`` inside a bullet or an inline reference used to anchor the replace - on that mention and delete every line from there to the next heading, - silently destroying hand-curated content (#1688). When several exact - headings exist, the last one is used, since graphify's section is appended. - """ lines = content.split("\n") starts = [i for i, line in enumerate(lines) if line.strip() == marker] if not starts: @@ -810,22 +512,7 @@ def _replace_or_append_section( return out -def _remove_marker_section(content: str, marker: str, boundary_prefix: str = "## ") -> "str | None": - """Remove every section whose heading line is exactly ``marker``. - - The heading is matched only when a line *is* exactly ``marker`` (after - stripping surrounding whitespace), never as a substring. The old uninstall - regex ``## graphify`` was unanchored, so it matched inside a user's - ``### graphify`` heading and deleted hand-written content (#2062) — the same - class of bug the install side hardened against in #1688. Each section runs to - the line before the next ``boundary_prefix`` heading (default the next H2) or - EOF, mirroring ``_replace_or_append_section``. All exact-heading sections are - removed (pre-#1688 installs could leave duplicates). - - Returns None when no exact ``marker`` line exists — the caller must then leave - the file untouched. This doubles as the guard: a substring mention (a bullet, - an inline reference, a deeper ``###`` heading) never triggers a strip. - """ +def _remove_marker_section(content: str, marker: str, boundary_prefix: str = "## ") -> str | None: lines = content.split("\n") removed = False while True: @@ -848,35 +535,15 @@ class of bug the install side hardened against in #1688. Each section runs to return "\n".join(lines).rstrip() -def _read_keep_newline(path: Path) -> "tuple[str, str]": - """Read *path* as UTF-8 and return ``(text, newline)``. - - ``text`` uses bare ``\n`` internally, so the section helpers - (``_replace_or_append_section`` / ``_remove_marker_section``) work on it - unchanged, and ``newline`` is the file's own dominant line ending (``"\r\n"`` - or ``"\n"``) so a later write can put it back. - - ``Path.read_text`` opens in universal-newline mode and folds every ``\r\n`` - to ``\n``, so the file's convention is lost before the write side ever sees - it; a plain ``write_text`` then re-emits the platform default and rewrites - the WHOLE file's endings, turning an LF file into CRLF on Windows (or a CRLF - file into LF on Linux) just to merge in a few lines (#4360). Reading the bytes - and normalising here keeps the original ending to restore. - """ +def _read_keep_newline(path: Path) -> tuple[str, str]: + """Read *path* as UTF-8 and return ``(text, newline)``.""" raw = path.read_bytes().decode("utf-8") newline = "\r\n" if "\r\n" in raw else "\n" return raw.replace("\r\n", "\n").replace("\r", "\n"), newline def _write_keep_newline(path: Path, text: str, newline: str = "\n") -> None: - """Write bare-``\n`` *text* using *newline* endings, without translation. - - ``newline=""`` stops ``write_text`` from translating on the way out; the one - conversion from bare ``\n`` to the file's own ending happens here, so every - install/uninstall writer shares a single line-ending contract (#4360). A file - with no prior convention (a fresh install) is written with plain LF, matching - what the other install writers already emit. - """ + """Write bare-``\n`` *text* using *newline* endings, without translation.""" if newline != "\n": text = text.replace("\n", newline) path.write_text(text, encoding="utf-8", newline="") @@ -885,18 +552,17 @@ def _write_keep_newline(path: Path, text: str, newline: str = "\n") -> None: _BANNER_LOGO = """\ ▄▄▀█▀▄▄ ▄▄▀▀ ▄█▄ ▀▀▄▄ -██▀▀▀▀▀███▀▀▀▀▀▀▀ ▄█████▄ ██ ██ ▄████ -█ ▀▄ ▄▀ ██▀ ▀▀▀ ██▄███ ██████▄ ██▄████▄ ██▄████▄ ▄▄ █████ ██ ██ +██▀▀▀▀▀███▀▀▀▀▀▀▀ ▄█████▄ ██ ██ ▄████ +█ ▀▄ ▄▀ ██▀ ▀▀▀ ██▄███ ██████▄ ██▄████▄ ██▄████▄ ▄▄ █████ ██ ██ █ ███ ███▀▀█ ██ █████ ██▀ ▄▄▄▄▄██ ██▀ ▀██ ██▀ ▀██ ██ ██ ▀██ ██▀ █ ▀▀█▄ ▀▀▀▄ █ ██▄ ▄██ ██ ██▀▀▀▀██ ██▄ ▄██ ██ ██ ██ ██ ▀█▄▄█▀ █ ███ ▀█ ▀████▀██ ██ ▀██████▀ ██▀████▀ ██ ██ ██ ██ ▀██▀ -▀██▀▀▀▀▀█▀▀▀▀▀██▀ ██ ▄██ - ▀▀▄▄ █ ▄▄▀▀ ██ ██▀ +▀██▀▀▀▀▀█▀▀▀▀▀██▀ ██ ▄██ + ▀▀▄▄ █ ▄▄▀▀ ██ ██▀ ▀▀▀""" def _print_banner() -> None: - """Green graphify logo banner on graphify install. TTY-only, never raises.""" if not sys.stdout.isatty(): return try: @@ -909,11 +575,12 @@ def _print_banner() -> None: D = "\033[2m" R = "\033[0m" lines = [f" {G}{line}{R}" for line in _BANNER_LOGO.splitlines()] - # Version sits after the wordmark baseline, like the old banner. lines[6] += f" {D}{__version__}{R}" print("\n" + "\n".join(lines) + "\n") except Exception: pass + + def install(platform: str = "claude", *, project: bool = False, project_dir: Path | None = None) -> None: _print_banner() platform = _canonical_platform(platform) @@ -923,9 +590,13 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat if platform == "cursor": _cursor_install(Path(".")) return - # On Windows, antigravity needs the PowerShell skill, not the bash one if platform == "antigravity" and sys.platform == "win32": platform = "antigravity-windows" + if platform == "mimo" and sys.platform == "win32": + platform = "mimo-windows" + if platform in ("mimo", "mimo-windows"): + _mimo_install((project_dir or Path(".")) if project else Path("."), project=project) + return if platform not in _PLATFORM_CONFIG: print( f"error: unknown platform '{platform}'. Choose from: {', '.join(_PLATFORM_CONFIG)}, gemini, cursor", @@ -938,13 +609,9 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat skill_dst = _copy_skill_file(platform, project=project, project_dir=project_dir) if platform == "kilo": - # Kilo Code also supports a native /graphify command file. command_src = Path(__file__).parent / "command-kilo.md" if not command_src.exists(): - print( - f"error: command-kilo.md not found in package - reinstall graphify", - file=sys.stderr, - ) + print("error: command-kilo.md not found in package - reinstall graphify", file=sys.stderr) sys.exit(1) command_dst = Path.home() / ".config" / "kilo" / "command" / "graphify.md" command_dst.parent.mkdir(parents=True, exist_ok=True) @@ -952,10 +619,6 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat print(f" command installed -> {command_dst}") if cfg["claude_md"]: - # Register in the matching Claude Code scope. Honor CLAUDE_CONFIG_DIR - # for the global (non-project) case, same as _platform_skill_destination - # does for the skill copy path (#527) -- this always-on registration - # path was missed by that fix (#2694). if project: claude_md = project_dir / ".claude" / "CLAUDE.md" skill_ref = ".claude/skills/graphify/SKILL.md" @@ -971,7 +634,6 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat ) if platform == "codebuddy": - # Register in ~/.codebuddy/CODEBUDDY.md (CodeBuddy only) _register_always_on_block( Path.home() / ".codebuddy" / "CODEBUDDY.md", " CODEBUDDY.md -> ", @@ -992,30 +654,24 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat print("Prefer a hosted version? Try the graphify platform free for 14 days:") print("https://app.graphify.com") print() + + def _print_install_usage() -> None: platforms = ", ".join([*_PLATFORM_CONFIG, "gemini", "cursor"]) print("Usage: graphify install [--project] [--strict] [--platform P|P]") print(f"Platforms: {platforms}") print(" --strict block the first raw file read per session until one " "`graphify query` runs (Claude Code project hook only; needs --project)") + + _CLAUDE_MD_MARKER = "## graphify" _CODEBUDDY_MD_MARKER = "## graphify" _AGENTS_MD_MARKER = "## graphify" _GEMINI_MD_MARKER = "## graphify" -# Deliberately H1, not H2 like the markers above: this one anchors the SKILL -# registration block _register_always_on_block writes into .claude/CLAUDE.md -# (or CODEBUDDY.md), a different section from the "always-on instructions" -# block the H2 markers above anchor. Kept at H1 specifically so it can never -# collide with a genuine user-authored "## graphify" heading elsewhere in the -# same file (#2062) — _remove_claude_skill_registration matches on this same -# constant so the two stay in sync. _SKILL_REGISTRATION_MARKER = "# graphify" -def _gemini_hook(project: bool = False) -> dict: - """Gemini CLI BeforeTool hook, resolved to a shell-agnostic `graphify` call. - A project-scoped install emits the bare command, since .gemini/settings.json - is then committed and an installing machine's path is wrong there (#3129). - """ + +def _gemini_hook(project: bool = False) -> dict: exe = _resolve_graphify_exe(project=project) if " " in exe and not exe.startswith('"'): exe = f'"{exe}"' @@ -1023,11 +679,11 @@ def _gemini_hook(project: bool = False) -> dict: "matcher": "read_file|list_directory", "hooks": [{"type": "command", "command": f"{exe} hook-guard gemini"}], } + + def gemini_install(project_dir: Path | None = None, *, project: bool = False) -> None: - """Copy skill file, write GEMINI.md section, and install BeforeTool hook.""" project_dir = project_dir or Path(".") skill_dst = _copy_skill_file("gemini", project=project, project_dir=project_dir) - target = project_dir / "GEMINI.md" if target.exists(): @@ -1042,37 +698,27 @@ def gemini_install(project_dir: Path | None = None, *, project: bool = False) -> if target.exists() and new_content == content: print(f"graphify already configured in {target.resolve()} (no change)") else: - # The file's own line ending is restored on write, so its existing - # content is never re-translated (#3668/#4360). _write_keep_newline(target, new_content, newline) print(f"graphify section written to {target.resolve()}") - # Always re-install the Gemini hook so an older payload (e.g. pre-issue-#580 - # wording) is replaced on upgrade. _install_gemini_hook(project_dir, project=project) if project: _print_project_git_add_hint([_project_scope_root(skill_dst, project_dir), project_dir / "GEMINI.md", project_dir / ".gemini"]) print() print("Gemini CLI will now check the knowledge graph before answering") print("codebase questions and rebuild it after code changes.") -def _refuse_to_modify(settings_path: Path) -> "NoReturn": - """Abort a hook install rather than clobber a config file we can't parse (#2167).""" + + +def _refuse_to_modify(settings_path: Path) -> NoReturn: print( f"[graphify] refusing to modify {settings_path}: not valid JSON " "(fix or move it and re-run)", file=sys.stderr, ) sys.exit(1) + + def _read_settings_for_merge(settings_path: Path) -> dict: - """Load an existing settings/hooks JSON file for a read-modify-write merge. - - A missing file yields a fresh ``{}`` (first install). An existing file that - cannot be parsed as a JSON object aborts via ``_refuse_to_modify`` instead of - silently falling back to ``{}`` — the old fallback rewrote the whole file and - destroyed every setting the user had (#2167). Reads with ``utf-8-sig`` so a - UTF-8 BOM (the most likely parse-error trigger, same class as #2163) is - tolerated rather than fatal. - """ if not settings_path.exists(): return {} try: @@ -1082,14 +728,9 @@ def _read_settings_for_merge(settings_path: Path) -> dict: if not isinstance(settings, dict): _refuse_to_modify(settings_path) return settings -def _write_settings_with_backup(settings_path: Path, settings: dict) -> None: - """Serialize ``settings`` to ``settings_path``, backing up the previous file. - Skips the write entirely when the output is identical to what is on disk - (idempotent re-install: no backup churn, no mtime churn). Otherwise copies - the existing file to ``.graphify-bak`` (single rolling backup) before - overwriting, so one bad merge can never destroy the user's config (#2167). - """ + +def _write_settings_with_backup(settings_path: Path, settings: dict) -> None: output = json.dumps(settings, indent=2) if settings_path.exists(): if settings_path.read_text(encoding="utf-8") == output: @@ -1097,6 +738,8 @@ def _write_settings_with_backup(settings_path: Path, settings: dict) -> None: backup = settings_path.with_name(settings_path.name + ".graphify-bak") shutil.copy2(settings_path, backup) settings_path.write_text(output, encoding="utf-8") + + def _install_gemini_hook(project_dir: Path, project: bool = False) -> None: settings_path = project_dir / ".gemini" / "settings.json" settings_path.parent.mkdir(parents=True, exist_ok=True) @@ -1111,6 +754,8 @@ def _install_gemini_hook(project_dir: Path, project: bool = False) -> None: hooks["BeforeTool"].append(_gemini_hook(project=project)) _write_settings_with_backup(settings_path, settings) print(" .gemini/settings.json -> BeforeTool hook registered") + + def _uninstall_gemini_hook(project_dir: Path) -> None: settings_path = project_dir / ".gemini" / "settings.json" if not settings_path.exists(): @@ -1128,14 +773,9 @@ def _uninstall_gemini_hook(project_dir: Path) -> None: settings["hooks"]["BeforeTool"] = filtered _write_settings_with_backup(settings_path, settings) print(" .gemini/settings.json -> BeforeTool hook removed") -def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: - """Remove the graphify section from GEMINI.md, uninstall hook, and remove skill file. - Scope rules (#2215): a bare call removes the user-global skill; passing - ``project_dir`` (or ``project=True``) scopes skill removal to that project - and leaves the global tree untouched, unless ``remove_user_skill=True`` - explicitly opts back into the global delete (as ``uninstall_all`` does). - """ + +def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: explicit_dir = project_dir is not None project_dir = project_dir or Path(".") if remove_user_skill is None: @@ -1154,7 +794,6 @@ def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False, if cleaned is None: print("graphify section not found in GEMINI.md - nothing to do") return - # A symlink stays: writing through it cleans the file it points to. if cleaned or target.is_symlink(): _write_keep_newline(target, cleaned + "\n", newline) print(f"graphify section removed from {target.resolve()}") @@ -1162,17 +801,16 @@ def gemini_uninstall(project_dir: Path | None = None, *, project: bool = False, target.unlink() print(f"GEMINI.md was empty after removal - deleted {target.resolve()}") _uninstall_gemini_hook(project_dir) + + _VSCODE_INSTRUCTIONS_MARKER = "## graphify" -def _vscode_skill_destination() -> Path: - """Skill path written by ``graphify vscode install``. - The same directory as the ``copilot`` platform's user-scope skill, but with - the VS Code variant of SKILL.md, so the version stamp there cannot tell which - of the two is installed. - """ + +def _vscode_skill_destination() -> Path: return Path.home() / ".copilot" / "skills" / "graphify" / "SKILL.md" + + def vscode_install(project_dir: Path | None = None) -> None: - """Install graphify skill for VS Code Copilot Chat + write .github/copilot-instructions.md.""" skill_src = Path(__file__).parent / "skill-vscode.md" refs_bundle = "vscode" if not skill_src.exists(): @@ -1191,7 +829,6 @@ def vscode_install(project_dir: Path | None = None) -> None: except OSError: pass raise - # Progressive-capable: install the packaged references/ sidecar when present. refs_src = Path(__file__).parent / "skills" / refs_bundle / "references" if refs_src.exists(): _install_skill_references(skill_dst, refs_src) @@ -1224,8 +861,9 @@ def vscode_install(project_dir: Path | None = None) -> None: "VS Code Copilot Chat configured. Type /graphify in the chat panel to build the graph." ) print("Note: for GitHub Copilot CLI (terminal), use: graphify copilot install") + + def vscode_uninstall(project_dir: Path | None = None) -> None: - """Remove graphify VS Code Copilot Chat skill and .github/copilot-instructions.md section.""" skill_dst = _vscode_skill_destination() with _skill_lock(skill_dst.parent): if skill_dst.exists(): @@ -1237,11 +875,7 @@ def vscode_uninstall(project_dir: Path | None = None) -> None: refs_dir = skill_dst.parent / "references" if refs_dir.exists(): shutil.rmtree(refs_dir) - for d in ( - skill_dst.parent, - skill_dst.parent.parent, - skill_dst.parent.parent.parent, - ): + for d in (skill_dst.parent, skill_dst.parent.parent, skill_dst.parent.parent.parent): try: d.rmdir() except OSError: @@ -1254,18 +888,16 @@ def vscode_uninstall(project_dir: Path | None = None) -> None: cleaned = _remove_marker_section(content, _VSCODE_INSTRUCTIONS_MARKER) if cleaned is None: return - # A symlink stays: writing through it cleans the file it points to. if cleaned or instructions.is_symlink(): _write_keep_newline(instructions, cleaned + "\n", newline) print(f" graphify section removed from {instructions}") else: instructions.unlink() print(f" {instructions} -> deleted (was empty after removal)") + + _ANTIGRAVITY_RULES_PATH = Path(".agents") / "rules" / "graphify.md" _ANTIGRAVITY_WORKFLOW_PATH = Path(".agents") / "workflows" / "graphify.md" -# Names no SKILL.md location on purpose: this constant is shared by the global and -# project-scoped installs, which put the skill in different places, so any hardcoded -# path dangles for the other scope. Antigravity resolves the skill by frontmatter name. _ANTIGRAVITY_WORKFLOW = """\ --- name: graphify @@ -1278,25 +910,17 @@ def vscode_uninstall(project_dir: Path | None = None) -> None: If no path argument is given, use `.` (current directory). """ + + def _kiro_install(project_dir: Path) -> None: - """Write graphify skill + steering file for Kiro IDE/CLI.""" project_dir = project_dir or Path(".") - - # Skill file + references/ sidecar + .graphify_version stamp via the shared - # progressive-disclosure helper. Previously this used a bare write_text that - # bypassed _copy_skill_file, so the references/ dir and version stamp were - # never written even though kiro declares skill_refs: "kiro" (#1142). _copy_skill_file("kiro", project=True, project_dir=project_dir) - - # Steering file → .kiro/steering/graphify.md (always-on) steering_dir = project_dir / ".kiro" / "steering" steering_dir.mkdir(parents=True, exist_ok=True) steering_dst = steering_dir / "graphify.md" if steering_dst.exists() and steering_dst.read_text(encoding="utf-8") == _always_on("kiro-steering"): print(f" .kiro/steering/graphify.md -> already configured (no change)") else: - # File is wholly graphify-owned. Overwrite on upgrade so older - # report-first wording does not silently linger (issue #580). action = "updated" if steering_dst.exists() else "written" steering_dst.write_text(_always_on("kiro-steering"), encoding="utf-8") print(f" .kiro/steering/graphify.md -> always-on steering {action}") @@ -1304,39 +928,28 @@ def _kiro_install(project_dir: Path) -> None: print() print("Kiro will now read the knowledge graph before every conversation.") print("Use /graphify to build or update the graph.") + + def _kiro_uninstall(project_dir: Path) -> None: - """Remove graphify skill + steering file for Kiro.""" project_dir = project_dir or Path(".") removed = [] - - # Skill + .graphify_version + references/ sidecar + empty-dir walk. skill_dst = _platform_skill_destination("kiro", project=True, project_dir=project_dir) if _remove_skill_file("kiro", project=True, project_dir=project_dir): removed.append(str(skill_dst.relative_to(project_dir))) - steering_dst = project_dir / ".kiro" / "steering" / "graphify.md" if steering_dst.exists(): steering_dst.unlink() removed.append(str(steering_dst.relative_to(project_dir))) - print("Removed: " + (", ".join(removed) if removed else "nothing to remove")) -def _antigravity_finalize(skill_dst: Path, project_dir: Path) -> None: - """Write Antigravity's always-on layer next to an installed skill. - Injects the native tool-discovery YAML frontmatter into *skill_dst*, then - writes ``.agents/rules/graphify.md`` and ``.agents/workflows/graphify.md`` - under *project_dir*. Shared by the global ``antigravity install`` and the - project-scoped ``install --project --platform antigravity`` paths, so both lay - down the rules/workflows that the uninstall path already expects to remove. - """ - # Inject YAML frontmatter for native Antigravity tool discovery. + +def _antigravity_finalize(skill_dst: Path, project_dir: Path) -> None: if skill_dst.exists(): content = skill_dst.read_text(encoding="utf-8") if not content.startswith("---\n"): frontmatter = "---\nname: graphify-manager\ndescription: Rebuild the code graph or perform manual CLI queries when MCP server is offline.\n---\n\n" skill_dst.write_text(frontmatter + content, encoding="utf-8") - # .agents/rules/graphify.md rules_path = project_dir / _ANTIGRAVITY_RULES_PATH rules_path.parent.mkdir(parents=True, exist_ok=True) if rules_path.exists(): @@ -1350,7 +963,6 @@ def _antigravity_finalize(skill_dst: Path, project_dir: Path) -> None: rules_path.write_text(_always_on("antigravity-rules"), encoding="utf-8") print(f"graphify rule written to {rules_path.resolve()}") - # .agents/workflows/graphify.md wf_path = project_dir / _ANTIGRAVITY_WORKFLOW_PATH wf_path.parent.mkdir(parents=True, exist_ok=True) if wf_path.exists(): @@ -1363,29 +975,17 @@ def _antigravity_finalize(skill_dst: Path, project_dir: Path) -> None: else: wf_path.write_text(_ANTIGRAVITY_WORKFLOW, encoding="utf-8") print(f"graphify workflow written to {wf_path.resolve()}") + + def _antigravity_install(project_dir: Path) -> None: - """Install graphify for Google Antigravity (global skill + .agents/rules + .agents/workflows).""" - # Copy the skill to ~/.gemini/config/skills/graphify/SKILL.md (global), then - # lay down the always-on rules/workflows under the project dir. install(platform="antigravity") _antigravity_finalize(_platform_skill_destination("antigravity"), project_dir) - print() print("Antigravity will now check the knowledge graph before answering") print("codebase questions. Run /graphify first to build the graph.") - print() - print( - "To enable full MCP architecture navigation, add this to ~/.gemini/antigravity/mcp_config.json:" - ) - print(' "graphify": {') - print(' "command": "uv",') - print( - ' "args": ["run", "--with", "graphifyy", "--with", "mcp", "-m", "graphify.serve", "${workspace.path}/graphify-out/graph.json"]' - ) - print(" }") + + def _antigravity_uninstall(project_dir: Path, *, project: bool = False) -> None: - """Remove graphify Antigravity rules, workflow, and skill files.""" - # Remove rules file rules_path = project_dir / _ANTIGRAVITY_RULES_PATH if rules_path.exists(): rules_path.unlink() @@ -1393,13 +993,11 @@ def _antigravity_uninstall(project_dir: Path, *, project: bool = False) -> None: else: print("No graphify Antigravity rule found - nothing to do") - # Remove workflow file wf_path = project_dir / _ANTIGRAVITY_WORKFLOW_PATH if wf_path.exists(): wf_path.unlink() print(f"graphify workflow removed from {wf_path.resolve()}") - # Remove skill file skill_dst = _platform_skill_destination("antigravity", project=project, project_dir=project_dir) with _skill_lock(skill_dst.parent): if skill_dst.exists(): @@ -1411,15 +1009,13 @@ def _antigravity_uninstall(project_dir: Path, *, project: bool = False) -> None: refs_dir = skill_dst.parent / "references" if refs_dir.exists(): shutil.rmtree(refs_dir) - for d in ( - skill_dst.parent, - skill_dst.parent.parent, - skill_dst.parent.parent.parent, - ): + for d in (skill_dst.parent, skill_dst.parent.parent, skill_dst.parent.parent.parent): try: d.rmdir() except OSError: break + + _CURSOR_RULE_PATH = Path(".cursor") / "rules" / "graphify.mdc" _CURSOR_RULE = """\ --- @@ -1431,44 +1027,51 @@ def _antigravity_uninstall(project_dir: Path, *, project: bool = False) -> None: **MANDATORY: Before using Read, Grep, Glob, or Bash to explore the codebase, you MUST run graphify first:** - `graphify query ""` — scoped subgraph for any codebase or architecture question + - `graphify path "" ""` — dependency path between two symbols + - `graphify explain ""` — all nodes related to a concept + This applies to YOU and to every subagent you spawn. Include this rule explicitly in every subagent prompt that involves code exploration. Do not skip graphify because files are "already known" or because you are executing a plan — the graph surfaces cross-file dependencies and INFERRED edges that grep and Read cannot find. Only use Read/Grep/Glob directly when: 1. graphify has already oriented you and you need to modify or debug specific lines + 2. `graphify-out/graph.json` does not exist yet - If `graphify-out/wiki/index.md` exists, navigate it instead of reading raw files + - Read `graphify-out/GRAPH_REPORT.md` only for broad architecture review when query/path/explain do not surface enough context + - After modifying code files, run `graphify update .` to keep the graph current (AST-only, no API cost) + """ + + def _cursor_install(project_dir: Path) -> None: - """Write .cursor/rules/graphify.mdc with alwaysApply: true.""" rule_path = (project_dir or Path(".")) / _CURSOR_RULE_PATH rule_path.parent.mkdir(parents=True, exist_ok=True) if rule_path.exists() and rule_path.read_text(encoding="utf-8") == _CURSOR_RULE: print(f"graphify rule at {rule_path} already configured (no change)") return - # File is wholly graphify-owned. Overwrite on upgrade so older - # report-first wording does not silently linger (issue #580). action = "updated" if rule_path.exists() else "written" rule_path.write_text(_CURSOR_RULE, encoding="utf-8") print(f"graphify rule {action} at {rule_path.resolve()}") print() print("Cursor will now always include the knowledge graph context.") print("Run /graphify . first to build the graph if you haven't already.") + + def _cursor_uninstall(project_dir: Path) -> None: - """Remove .cursor/rules/graphify.mdc.""" rule_path = (project_dir or Path(".")) / _CURSOR_RULE_PATH if not rule_path.exists(): print("No graphify Cursor rule found - nothing to do") return rule_path.unlink() print(f"graphify Cursor rule removed from {rule_path.resolve()}") -# Devin CLI — .windsurf/rules/graphify.md (always-on context) -# Devin reads .windsurf/rules/*.md files the same way Windsurf IDE does. + + _DEVIN_RULES_PATH = Path(".windsurf") / "rules" / "graphify.md" _DEVIN_RULES = """\ ## graphify @@ -1477,12 +1080,17 @@ def _cursor_uninstall(project_dir: Path) -> None: Rules: - For codebase or architecture questions, when `graphify-out/graph.json` exists, first run `graphify query ""` (or `graphify path "" ""` / `graphify explain ""`). These return a scoped subgraph, usually much smaller than `GRAPH_REPORT.md` or raw grep output. + - If graphify-out/wiki/index.md exists, navigate it instead of reading raw files + - Read graphify-out/GRAPH_REPORT.md only for broad architecture review or when query/path/explain do not surface enough context + - After modifying code files in this session, run `graphify update .` to keep the graph current (AST-only, no API cost) + """ + + def _devin_rules_install(project_dir: Path) -> None: - """Write .windsurf/rules/graphify.md for always-on Devin context.""" rules_path = (project_dir or Path(".")) / _DEVIN_RULES_PATH rules_path.parent.mkdir(parents=True, exist_ok=True) if rules_path.exists() and rules_path.read_text(encoding="utf-8") == _DEVIN_RULES: @@ -1491,13 +1099,16 @@ def _devin_rules_install(project_dir: Path) -> None: action = "updated" if rules_path.exists() else "written" rules_path.write_text(_DEVIN_RULES, encoding="utf-8") print(f" rules {action} -> {rules_path}") + + def _devin_rules_uninstall(project_dir: Path) -> None: - """Remove .windsurf/rules/graphify.md.""" rules_path = (project_dir or Path(".")) / _DEVIN_RULES_PATH if not rules_path.exists(): return rules_path.unlink() print(f" rules removed -> {rules_path}") + + _KILO_PLUGIN_JS = """\ // graphify Kilo plugin // Injects a knowledge graph reminder before bash tool calls when the graph exists. @@ -1513,10 +1124,6 @@ def _devin_rules_uninstall(project_dir: Path) -> None: if (!existsSync(join(directory, "graphify-out", "graph.json"))) return; if (input.tool === "bash") { - // Separate with ';' not '&&' — Windows PowerShell 5.1 rejects '&&' as a - // statement separator ("not a valid statement separator"), which broke - // the first bash command in every OpenCode session on Windows (#1646). - // ';' works in PowerShell 5.1, Bash, and POSIX shells alike. output.args.command = 'echo "[graphify] Knowledge graph available. Read graphify-out/GRAPH_REPORT.md for god nodes and architecture context before searching files." ; ' + output.args.command; @@ -1529,26 +1136,26 @@ def _devin_rules_uninstall(project_dir: Path) -> None: _KILO_PLUGIN_PATH = Path(".kilo") / "plugins" / "graphify.js" _KILO_CONFIG_JSON_PATH = Path(".kilo") / "kilo.json" _KILO_CONFIG_JSONC_PATH = Path(".kilo") / "kilo.jsonc" + + def _strip_json_comments(raw: str) -> str: - """Remove JSONC-style comments while leaving string content intact.""" result: list[str] = [] in_string = False escaped = False line_comment = False block_comment = False i = 0 + n = len(raw) - while i < len(raw): + while i < n: ch = raw[i] - nxt = raw[i + 1] if i + 1 < len(raw) else "" - + nxt = raw[i + 1] if i + 1 < n else "" if line_comment: if ch == "\n": line_comment = False result.append(ch) i += 1 continue - if block_comment: if ch == "*" and nxt == "/": block_comment = False @@ -1556,7 +1163,6 @@ def _strip_json_comments(raw: str) -> str: else: i += 1 continue - if in_string: result.append(ch) if escaped: @@ -1567,7 +1173,6 @@ def _strip_json_comments(raw: str) -> str: in_string = False i += 1 continue - if ch == "/" and nxt == "/": line_comment = True i += 2 @@ -1576,13 +1181,25 @@ def _strip_json_comments(raw: str) -> str: block_comment = True i += 2 continue - result.append(ch) if ch == '"': in_string = True i += 1 - return re.sub(r",(\s*[}\]])", r"\1", "".join(result)) + no_comments = "".join(result) + parts: list[str] = [] + last_end = 0 + for match in re.finditer(r'"(?:\\.|[^"\\])*"', no_comments, re.DOTALL): + non_string = no_comments[last_end : match.start()] + parts.append(re.sub(r",(\s*[}\]])", r"\1", non_string)) + parts.append(match.group(0)) + last_end = match.end() + + non_string = no_comments[last_end:] + parts.append(re.sub(r",(\s*[}\]])", r"\1", non_string)) + return "".join(parts) + + def _load_json_like(config_file: Path) -> dict: if not config_file.exists(): return {} @@ -1594,6 +1211,8 @@ def _load_json_like(config_file: Path) -> dict: except (OSError, json.JSONDecodeError): return {} return loaded if isinstance(loaded, dict) else {} + + def _kilo_config_path(project_dir: Path) -> Path: kilo_dir = (project_dir or Path(".")) / ".kilo" json_path = kilo_dir / _KILO_CONFIG_JSON_PATH.name @@ -1603,12 +1222,14 @@ def _kilo_config_path(project_dir: Path) -> Path: if jsonc_path.exists(): return jsonc_path return json_path + + def _kilo_config_write_path(project_dir: Path) -> Path: - """Write automated Kilo edits to kilo.json so existing JSONC stays untouched.""" kilo_dir = (project_dir or Path(".")) / ".kilo" return kilo_dir / _KILO_CONFIG_JSON_PATH.name + + def _install_kilo_plugin(project_dir: Path) -> None: - """Write graphify.js plugin and register it without rewriting user JSONC.""" plugin_file = project_dir / _KILO_PLUGIN_PATH plugin_file.parent.mkdir(parents=True, exist_ok=True) plugin_file.write_text(_KILO_PLUGIN_JS, encoding="utf-8") @@ -1628,11 +1249,10 @@ def _install_kilo_plugin(project_dir: Path) -> None: write_config_file.write_text(json.dumps(config, indent=2), encoding="utf-8") print(f" {write_config_file.relative_to(project_dir)} -> plugin registered") else: - print( - f" {config_file.relative_to(project_dir)} -> plugin already registered (no change)" - ) + print(f" {config_file.relative_to(project_dir)} -> plugin already registered (no change)") + + def _uninstall_kilo_plugin(project_dir: Path) -> None: - """Remove graphify.js plugin and deregister it without rewriting user JSONC.""" plugin_file = project_dir / _KILO_PLUGIN_PATH if plugin_file.exists(): plugin_file.unlink() @@ -1653,11 +1273,9 @@ def _uninstall_kilo_plugin(project_dir: Path) -> None: config.pop("plugin") write_config_file.parent.mkdir(parents=True, exist_ok=True) write_config_file.write_text(json.dumps(config, indent=2), encoding="utf-8") - print( - f" {write_config_file.relative_to(project_dir)} -> plugin deregistered" - ) -# OpenCode tool.execute.before plugin — fires before every tool call. -# Injects a graph reminder into shell command output when graph.json exists. + print(f" {write_config_file.relative_to(project_dir)} -> plugin deregistered") + + _OPENCODE_PLUGIN_JS = """\ // graphify OpenCode plugin // Injects a knowledge graph reminder before the first shell command of a @@ -1724,31 +1342,22 @@ def _uninstall_kilo_plugin(project_dir: Path) -> None: """ _OPENCODE_PLUGIN_PATH = Path(".opencode") / "plugins" / "graphify.js" _OPENCODE_CONFIG_PATH = Path(".opencode") / "opencode.json" -# First line of every plugin graphify has written, back to the first one (#71). _OPENCODE_PLUGIN_HEADER = "// graphify OpenCode plugin" -def _opencode_plugin_is_v1_only(body: str) -> bool: - """True for a graphify plugin written before the OpenCode 2 entrypoint. - Releases up to 0.9.74 exported a named ``GraphifyPlugin`` function and - nothing else. OpenCode 2 needs a default export, so it rejects the file - with "Plugin must export a default definition" (#3554, #3732). - """ + +def _opencode_plugin_is_v1_only(body: str) -> bool: return body.startswith(_OPENCODE_PLUGIN_HEADER) and "export default" not in body + + def _opencode_config_names_plugin(project_dir: Path) -> bool: - """True when opencode.json still mentions the bare plugin path.""" try: raw = (project_dir / _OPENCODE_CONFIG_PATH).read_text(encoding="utf-8") except (OSError, UnicodeDecodeError): return False return f'"{_OPENCODE_PLUGIN_PATH.as_posix()}"' in raw -def _drop_opencode_config_entry(project_dir: Path) -> bool: - """Remove the plugin entry older releases wrote into opencode.json. - The entry was the bare path ``.opencode/plugins/graphify.js``. OpenCode - loads every file in ``.opencode/plugins/`` on its own, so the entry was - never needed, and OpenCode 2 reads a bare path as an npm package name and - fails to install it (#3732). Returns True when the config changed. - """ + +def _drop_opencode_config_entry(project_dir: Path) -> bool: config_file = project_dir / _OPENCODE_CONFIG_PATH entry = _OPENCODE_PLUGIN_PATH.as_posix() try: @@ -1758,8 +1367,6 @@ def _drop_opencode_config_entry(project_dir: Path) -> bool: try: config = json.loads(raw) except ValueError: - # OpenCode accepts comments in this file, json does not. Rewriting it - # would drop them, so say what to remove instead. if f'"{entry}"' in raw: print( f" {_OPENCODE_CONFIG_PATH} -> not plain JSON, left as is; " @@ -1777,23 +1384,20 @@ def _drop_opencode_config_entry(project_dir: Path) -> bool: config.pop(key) changed = True if changed: - # ensure_ascii=False: keep the user's non-ASCII values readable. _write_text_atomic(config_file, json.dumps(config, indent=2, ensure_ascii=False)) return changed -def _install_opencode_plugin(project_dir: Path) -> None: - """Write graphify.js into .opencode/plugins/, where OpenCode discovers it. - Both files are replaced atomically. The CLI refresh can run this while - OpenCode is watching them, and it must never see half a plugin. - """ + +def _install_opencode_plugin(project_dir: Path) -> None: plugin_file = project_dir / _OPENCODE_PLUGIN_PATH _write_text_atomic(plugin_file, _OPENCODE_PLUGIN_JS) print(f" {_OPENCODE_PLUGIN_PATH} -> tool.execute.before hook written") if _drop_opencode_config_entry(project_dir): print(f" {_OPENCODE_CONFIG_PATH} -> stale plugin entry removed") + + def _uninstall_opencode_plugin(project_dir: Path) -> None: - """Remove graphify.js plugin and deregister from opencode.json.""" plugin_file = project_dir / _OPENCODE_PLUGIN_PATH if plugin_file.exists(): plugin_file.unlink() @@ -1801,35 +1405,598 @@ def _uninstall_opencode_plugin(project_dir: Path) -> None: if _drop_opencode_config_entry(project_dir): print(f" {_OPENCODE_CONFIG_PATH} -> plugin deregistered") + + +# Xiaomi MiMo Code chat.message + tool.execute.before plugin +_MIMO_PLUGIN_JS = """\ +// graphify Xiaomi MiMo Code plugin +import { existsSync } from "fs"; +import { join, resolve, isAbsolute, sep } from "path"; +import { spawn } from "child_process"; + +export const GraphifyPlugin = async (ctx = {}) => { + const workspaceRoot = ctx?.directory || process.cwd(); + const graphPath = join(workspaceRoot, "graphify-out", "graph.json"); + const seenMessageIds = new Set(); + let lastQueriedPrompt = ""; + let isQuerying = false; + let toolReminded = false; + + const resolveGraphifyBin = () => { + const isWindows = process.platform === "win32"; + const normWorkspace = resolve(workspaceRoot).toLowerCase(); + + if (process.env.GRAPHIFY_EXE && process.env.GRAPHIFY_EXE.trim()) { + const custom = resolve(process.env.GRAPHIFY_EXE.trim()); + const customNorm = custom.toLowerCase(); + if (isAbsolute(custom) && customNorm !== normWorkspace && !customNorm.startsWith(normWorkspace + sep)) { + try { + if (existsSync(custom)) return custom; + } catch {} + } + return null; + } + + const pathSep = isWindows ? ";" : ":"; + const pathStr = process.env.PATH || process.env.Path || ""; + const dirs = pathStr.split(pathSep).filter((d) => d && d.trim()); + const extensions = isWindows + ? (process.env.PATHEXT || ".EXE;.CMD;.BAT;.COM").split(";").filter(Boolean) + : [""]; + + for (const rawDir of dirs) { + const trimmedDir = rawDir.trim(); + if (!isAbsolute(trimmedDir)) continue; + + const resolvedDir = resolve(trimmedDir); + const resolvedNorm = resolvedDir.toLowerCase(); + if (resolvedNorm === normWorkspace || resolvedNorm.startsWith(normWorkspace + sep)) { + continue; + } + + for (const ext of extensions) { + const candidate = join(resolvedDir, isWindows ? `graphify${ext.toLowerCase()}` : "graphify"); + const candidateUpper = isWindows ? join(resolvedDir, `graphify${ext.toUpperCase()}`) : candidate; + try { + if (existsSync(candidate)) return candidate; + if (isWindows && existsSync(candidateUpper)) return candidateUpper; + } catch {} + } + } + return null; + }; + + const runGraphifyQuery = (question) => { + return new Promise((res) => { + if (!existsSync(graphPath) || !question || isQuerying) return res(null); + isQuerying = true; + try { + const trimmedQuestion = question.trim().slice(0, 500); + if (!trimmedQuestion) { + isQuerying = false; + return res(null); + } + + const bin = resolveGraphifyBin(); + if (!bin || !isAbsolute(bin)) { + isQuerying = false; + return res(null); + } + + try { + if (!existsSync(bin)) { + isQuerying = false; + return res(null); + } + } catch { + isQuerying = false; + return res(null); + } + + const args = ["query", trimmedQuestion, "--graph", graphPath, "--budget", "1500"]; + let stdout = ""; + let timer = null; + let killed = false; + + const child = spawn(bin, args, { + cwd: workspaceRoot, + shell: false, + windowsHide: true, + }); + + timer = setTimeout(() => { + killed = true; + try { child.kill("SIGKILL"); } catch {} + isQuerying = false; + res(null); + }, 4000); + + child.stdout?.on("data", (chunk) => { + stdout += chunk.toString("utf-8"); + if (stdout.length > 16000) { + try { child.kill(); } catch {} + } + }); + + child.on("error", () => { + if (timer) clearTimeout(timer); + isQuerying = false; + res(null); + }); + + child.on("close", (code) => { + if (timer) clearTimeout(timer); + isQuerying = false; + if (!killed && code === 0 && stdout && stdout.trim()) { + const text = stdout.trim(); + return res(text.length > 8000 ? text.slice(0, 8000) + "\\n...[truncated]" : text); + } + res(null); + }); + } catch { + isQuerying = false; + res(null); + } + }); + }; + + return { + "chat.message": async (input, output) => { + if (!existsSync(graphPath)) return; + const msgId = input?.message?.id || input?.messageID; + if (msgId && seenMessageIds.has(msgId)) return; + const parts = output?.parts; + if (!Array.isArray(parts)) return; + + const textParts = parts.filter((p) => p && p.type === "text" && typeof p.text === "string"); + const userText = textParts.map((p) => p.text).join("\\n").trim(); + if (!userText || userText === lastQueriedPrompt) return; + + if (msgId) seenMessageIds.add(msgId); + lastQueriedPrompt = userText; + + const graphContext = await runGraphifyQuery(userText); + if (graphContext) { + const injected = `\\n\\n\\n${graphContext}\\n`; + if (textParts.length > 0) { + textParts[textParts.length - 1].text += injected; + } else { + parts.push({ type: "text", text: injected }); + } + } + }, + "tool.execute.before": async (input, output) => { + if (toolReminded || !existsSync(graphPath)) return; + if (input && input.tool === "bash" && output && output.args && typeof output.args.command === "string") { + output.args.command = + 'echo "[graphify] Knowledge graph active at graphify-out/. Use \\'graphify query\\' for targeted questions." ; ' + + output.args.command; + toolReminded = true; + } + }, + }; +}; + +export default GraphifyPlugin; +""" + + +def _tokenize_jsonc(raw: str) -> list[tuple[str, int, int, str]]: + tokens = [] + i = 0 + n = len(raw) + while i < n: + ch = raw[i] + nxt = raw[i + 1] if i + 1 < n else "" + if ch == "/" and nxt == "/": + start = i + i += 2 + while i < n and raw[i] != "\n": + i += 1 + tokens.append(("LINE_COMMENT", start, i, raw[start:i])) + elif ch == "/" and nxt == "*": + start = i + i += 2 + while i + 1 < n and not (raw[i] == "*" and raw[i + 1] == "/"): + i += 1 + i = i + 2 if i + 1 < n else n + tokens.append(("BLOCK_COMMENT", start, i, raw[start:i])) + elif ch == '"': + start = i + i += 1 + escaped = False + while i < n: + c = raw[i] + if escaped: + escaped = False + elif c == "\\": + escaped = True + elif c == '"': + i += 1 + break + i += 1 + tokens.append(("STRING", start, i, raw[start:i])) + elif ch in "{}[],:": + tokens.append(("PUNCT", i, i + 1, ch)) + i += 1 + elif ch.isspace(): + start = i + while i < n and raw[i].isspace(): + i += 1 + tokens.append(("WHITESPACE", start, i, raw[start:i])) + else: + start = i + while i < n and not (raw[i].isspace() or raw[i] in '{}[],:"' or (raw[i] == "/" and i + 1 < n and raw[i + 1] in "/*")): + i += 1 + tokens.append(("OTHER", start, i, raw[start:i])) + return tokens + + +def _is_mimo_plugin_entry(candidate: object, possible_entries: set[str] | None = None) -> bool: + if not isinstance(candidate, str) or not possible_entries: + return False + if candidate in possible_entries: + return True + if sys.platform == "win32": + cand_norm = candidate.replace("/", "\\").lower() + for p in possible_entries: + if p.replace("/", "\\").lower() == cand_norm or candidate.lower() == p.lower(): + return True + return False + + +def _verify_mimo_plugins_val(data: dict, config_path: Path | None = None) -> None: + for key in ("plugin", "plugins"): + val = data.get(key) + if val is not None and not isinstance(val, (str, list)): + target = config_path or Path("mimocode.json") + print( + f"[graphify] refusing to modify {target}: '{key}' must be a string or array (found {type(val).__name__})", + file=sys.stderr, + ) + sys.exit(1) + + +def _preserve_jsonc_plugin_add(raw: str, entry: str) -> str: + try: + stripped = _strip_json_comments(raw) + data = json.loads(stripped) + except Exception as exc: + raise ValueError(f"Invalid JSON/JSONC: {exc}") from exc + + if not isinstance(data, dict): + raise ValueError("JSONC root must be an object") + + _verify_mimo_plugins_val(data) + existing_plugins = data.get("plugin") + if isinstance(existing_plugins, list) and entry in existing_plugins: + return raw + if isinstance(existing_plugins, str) and existing_plugins == entry: + return raw + + tokens = _tokenize_jsonc(raw) + root_open_idx = next((i for i, t in enumerate(tokens) if t[0] == "PUNCT" and t[3] == "{"), None) + root_close_idx = next((i for i in range(len(tokens) - 1, -1, -1) if tokens[i][0] == "PUNCT" and tokens[i][3] == "}"), None) + if root_open_idx is None or root_close_idx is None: + raise ValueError("Could not find root object braces in JSONC") + + plugin_key_tok_idx = None + depth = 0 + for idx in range(root_open_idx + 1, root_close_idx): + tok = tokens[idx] + if tok[0] == "PUNCT": + if tok[3] in "{[": + depth += 1 + elif tok[3] in "}]": + depth -= 1 + elif depth == 0 and tok[0] == "STRING" and tok[3] == '"plugin"': + plugin_key_tok_idx = idx + break + + entry_str = json.dumps(entry) + + if plugin_key_tok_idx is not None: + colon_idx = next(i for i in range(plugin_key_tok_idx + 1, root_close_idx) if tokens[i][0] == "PUNCT" and tokens[i][3] == ":") + val_start_idx = next(i for i in range(colon_idx + 1, root_close_idx) if tokens[i][0] not in ("WHITESPACE", "LINE_COMMENT", "BLOCK_COMMENT")) + val_tok = tokens[val_start_idx] + + if val_tok[0] == "PUNCT" and val_tok[3] == "[": + arr_open_idx = val_start_idx + arr_depth = 0 + arr_close_idx = None + for j in range(arr_open_idx, len(tokens)): + if tokens[j][0] == "PUNCT": + if tokens[j][3] == "[": + arr_depth += 1 + elif tokens[j][3] == "]": + arr_depth -= 1 + if arr_depth == 0: + arr_close_idx = j + break + if arr_close_idx is None: + raise ValueError("Unmatched array bracket") + + inner_items = [t for t in tokens[arr_open_idx + 1 : arr_close_idx] if t[0] in ("STRING", "OTHER") or (t[0] == "PUNCT" and t[3] in "{[")] + if not inner_items: + close_pos = tokens[arr_close_idx][1] + return raw[:close_pos] + f"\n {entry_str}\n " + raw[close_pos:] + + last_item = inner_items[-1] + last_item_end = last_item[2] + comma_exists = any(tokens[k][0] == "PUNCT" and tokens[k][3] == "," for k in range(tokens.index(last_item) + 1, arr_close_idx)) + + raw_mid = raw[:last_item_end] + "," + raw[last_item_end:] if not comma_exists else raw + arr_close_pos = tokens[arr_close_idx][1] + (1 if not comma_exists else 0) + pre = raw_mid[:arr_close_pos] + post = raw_mid[arr_close_pos:] + ws_match = re.search(r"(\n[ \t]*)$", pre) + if ws_match: + return pre[:ws_match.start(1)] + f"\n {entry_str}" + pre[ws_match.start(1):] + post + return pre + f"\n {entry_str}\n " + post + + if val_tok[0] == "STRING": + replacement = f"[\n {val_tok[3]},\n {entry_str}\n ]" + return raw[:val_tok[1]] + replacement + raw[val_tok[2]:] + raise ValueError("'plugin' value is neither string nor array") + + bracket_close_pos = tokens[root_close_idx][1] + indent = " " + plugin_block = f'{indent}"plugin": [\n{indent} {entry_str}\n{indent}]' + + if len(data) > 0: + last_prop_tok = next(tokens[j] for j in range(root_close_idx - 1, root_open_idx, -1) if tokens[j][0] not in ("WHITESPACE", "LINE_COMMENT", "BLOCK_COMMENT")) + comma_needed = not (last_prop_tok[0] == "PUNCT" and last_prop_tok[3] == ",") + + if comma_needed: + insertion_point = last_prop_tok[2] + raw_comma = raw[:insertion_point] + "," + raw[insertion_point:] + bracket_close_pos += 1 + else: + raw_comma = raw + + pre = raw_comma[:bracket_close_pos] + post = raw_comma[bracket_close_pos:] + ws_match = re.search(r"(\n[ \t]*)$", pre) + if ws_match: + return pre[:ws_match.start(1)] + f"\n{plugin_block}" + pre[ws_match.start(1):] + post + return pre + f"\n{plugin_block}\n" + post + + return raw[:bracket_close_pos] + f"\n{plugin_block}\n" + raw[bracket_close_pos:] + + +def _preserve_jsonc_plugin_remove(raw: str, possible_entries: set[str] | None = None) -> str: + try: + data = json.loads(_strip_json_comments(raw)) + except Exception: + return raw + + if not isinstance(data, dict) or "plugin" not in data: + return raw + + tokens = _tokenize_jsonc(raw) + plugin_key_idx = next((i for i, t in enumerate(tokens) if t[0] == "STRING" and t[3] == '"plugin"'), None) + if plugin_key_idx is None: + return raw + + colon_idx = next((i for i in range(plugin_key_idx + 1, len(tokens)) if tokens[i][0] == "PUNCT" and tokens[i][3] == ":"), None) + if colon_idx is None: + return raw + + val_idx = next((i for i in range(colon_idx + 1, len(tokens)) if tokens[i][0] not in ("WHITESPACE", "LINE_COMMENT", "BLOCK_COMMENT")), None) + if val_idx is None: + return raw + + val_tok = tokens[val_idx] + if val_tok[0] == "STRING": + if _is_mimo_plugin_entry(json.loads(val_tok[3]), possible_entries): + start_pos = tokens[plugin_key_idx][1] + next_comma = next((i for i in range(val_idx + 1, len(tokens)) if tokens[i][0] == "PUNCT" and tokens[i][3] == ","), None) + end_pos = tokens[next_comma][2] if next_comma else val_tok[2] + return (raw[:start_pos] + raw[end_pos:]).strip() + "\n" + + elif val_tok[0] == "PUNCT" and val_tok[3] == "[": + for idx in range(val_idx + 1, len(tokens)): + if tokens[idx][0] == "STRING" and _is_mimo_plugin_entry(json.loads(tokens[idx][3]), possible_entries): + match_tok = tokens[idx] + next_c = next((k for k in range(idx + 1, len(tokens)) if tokens[k][0] == "PUNCT" and tokens[k][3] == ","), None) + if next_c: + return raw[:match_tok[1]] + raw[tokens[next_c][2]:] + prev_c = next((k for k in range(idx - 1, val_idx, -1) if tokens[k][0] == "PUNCT" and tokens[k][3] == ","), None) + if prev_c: + return raw[:tokens[prev_c][1]] + raw[match_tok[2]:] + return raw[:match_tok[1]] + raw[match_tok[2]:] + return raw + + +def _prepare_mimo_config(project_dir: Path, *, project: bool = False) -> tuple[Path, str, bool]: + mimo_dir = (project_dir / ".mimocode") if project else _mimo_global_config_dir() + jsonc_file = mimo_dir / "mimocode.jsonc" + json_file = mimo_dir / "mimocode.json" + config_file = jsonc_file if jsonc_file.exists() else json_file + plugin_file = mimo_dir / "plugins" / "graphify.js" + entry = "./plugins/graphify.js" if project else plugin_file.resolve().as_uri() + + if not config_file.exists(): + return config_file, json.dumps({"plugin": [entry]}, indent=2) + "\n", False + + raw = config_file.read_text(encoding="utf-8-sig") + is_jsonc = (config_file.suffix == ".jsonc") or ("//" in raw) or ("/*" in raw) + + try: + data = json.loads(_strip_json_comments(raw) if is_jsonc else raw) + except Exception: + _refuse_to_modify(config_file) + if not isinstance(data, dict): + _refuse_to_modify(config_file) + + _verify_mimo_plugins_val(data, config_file) + + existing = data.get("plugin") + if (isinstance(existing, list) and entry in existing) or (isinstance(existing, str) and existing == entry): + return config_file, raw, True + + if is_jsonc: + new_content = _preserve_jsonc_plugin_add(raw, entry) + return config_file, new_content, False + + if isinstance(existing, list): + existing.append(entry) + elif isinstance(existing, str): + data["plugin"] = [existing, entry] + else: + data["plugin"] = [entry] + + return config_file, json.dumps(data, indent=2) + "\n", False + + +def _install_mimo_plugin_files( + project_dir: Path, + config_file: Path, + config_content: str, + already_registered: bool, + *, + project: bool = False, +) -> None: + plugin_file = ((project_dir / ".mimocode") if project else _mimo_global_config_dir()) / "plugins" / "graphify.js" + plugin_file.parent.mkdir(parents=True, exist_ok=True) + _write_text_atomic(plugin_file, _MIMO_PLUGIN_JS) + shown_plugin = plugin_file.relative_to(project_dir) if project else plugin_file + print(f" {shown_plugin} -> chat.message + tool.execute.before hook written") + + shown_config = config_file.relative_to(project_dir) if project else config_file + if not already_registered: + config_file.parent.mkdir(parents=True, exist_ok=True) + if config_file.exists(): + backup = config_file.with_name(config_file.name + ".graphify-bak") + shutil.copy2(config_file, backup) + _write_text_atomic(config_file, config_content) + print(f" {shown_config} -> plugin registered") + else: + print(f" {shown_config} -> plugin already registered (no change)") + + +def _install_mimo_plugin(project_dir: Path, *, project: bool = False) -> None: + config_file, config_content, already_registered = _prepare_mimo_config(project_dir, project=project) + _install_mimo_plugin_files(project_dir, config_file, config_content, already_registered, project=project) + + +def _prepare_mimo_uninstall(project_dir: Path, *, project: bool = False) -> tuple[Path, list[tuple[Path, str]]]: + mimo_dir = (project_dir / ".mimocode") if project else _mimo_global_config_dir() + plugin_file = mimo_dir / "plugins" / "graphify.js" + possible = {plugin_file.resolve().as_uri(), "./plugins/graphify.js", str(plugin_file)} + planned: list[tuple[Path, str]] = [] + + for cfg in (mimo_dir / "mimocode.json", mimo_dir / "mimocode.jsonc"): + if not cfg.exists(): + continue + try: + raw = cfg.read_text(encoding="utf-8-sig") + updated = _preserve_jsonc_plugin_remove(raw, possible) + if updated != raw: + planned.append((cfg, updated)) + except Exception: + pass + return plugin_file, planned + + +def _uninstall_mimo_plugin(project_dir: Path, *, project: bool = False) -> None: + plugin_file, planned_writes = _prepare_mimo_uninstall(project_dir, project=project) + shown_plugin = plugin_file.relative_to(project_dir) if project else plugin_file + if plugin_file.exists(): + plugin_file.unlink() + print(f" {shown_plugin} -> removed") + try: + plugin_file.parent.rmdir() + except OSError: + pass + + for config_file, new_content in planned_writes: + shown_config = config_file.relative_to(project_dir) if project else config_file + _write_text_atomic(config_file, new_content) + print(f" {shown_config} -> plugin deregistered") + + +def _write_mimo_agents_instruction(target: Path) -> None: + target.parent.mkdir(parents=True, exist_ok=True) + existed = target.exists() + content, newline = _read_keep_newline(target) if existed else ("", "\n") + new_content = _replace_or_append_section(content, _AGENTS_MD_MARKER, _always_on("agents-md")) + if not (existed and new_content == content): + _write_keep_newline(target, new_content, newline) + print(f" {target.resolve()} -> instructions written") + + +def _mimo_install(project_dir: Path, *, project: bool = False) -> None: + project_dir = project_dir or Path(".") + config_file, config_content, already_registered = _prepare_mimo_config(project_dir, project=project) + platform_name = "mimo-windows" if sys.platform == "win32" else "mimo" + + skill_dst = _copy_skill_file(platform_name, project=project, project_dir=project_dir) + _install_mimo_plugin_files(project_dir, config_file, config_content, already_registered, project=project) + + agents_file = (project_dir / "AGENTS.md") if project else (_mimo_global_config_dir() / "AGENTS.md") + _write_mimo_agents_instruction(agents_file) + + if project: + _print_project_git_add_hint([_project_scope_root(skill_dst, project_dir), project_dir / ".mimocode", project_dir / "AGENTS.md"]) + + print() + print("Xiaomi MiMo Code will now automatically check the knowledge graph before") + print("answering codebase questions. Run /graphify first to build the graph.") + + +def _mimo_uninstall(project_dir: Path, *, project: bool = False) -> None: + project_dir = project_dir or Path(".") + platform_name = "mimo-windows" if sys.platform == "win32" else "mimo" + _uninstall_mimo_plugin(project_dir, project=project) + _remove_skill_file(platform_name, project=project, project_dir=project_dir) + + target = (project_dir / "AGENTS.md") if project else (_mimo_global_config_dir() / "AGENTS.md") + if target.exists(): + content, newline = _read_keep_newline(target) + cleaned = _remove_marker_section(content, _AGENTS_MD_MARKER) + if cleaned is not None: + if cleaned: + _write_keep_newline(target, cleaned + "\n", newline) + else: + target.unlink() + + +def _read_graph_stats(graph_file: Path) -> str: + if not graph_file.exists(): + return f"No graph at {graph_file}." + try: + raw = json.loads(graph_file.read_text(encoding="utf-8")) + n = len(raw.get("nodes", [])) + e = len(raw.get("links", raw.get("edges", []))) + return f"Active graph with {n} nodes, {e} edges" + except Exception: + return "Graph exists (unparsed)" + + +def _mimo_status(project_dir: Path, *, project: bool = False) -> None: + project_dir = project_dir or Path(".") + mimo_dir = (project_dir / ".mimocode") if project else _mimo_global_config_dir() + platform_name = "mimo-windows" if sys.platform == "win32" else "mimo" + skill = _platform_skill_destination(platform_name, project=project, project_dir=project_dir) + + print(f"Xiaomi MiMo Code Status ({'Project' if project else 'Global'}):\n") + print(f" Skill: {'Installed' if skill.exists() else 'Not installed'} ({skill})") + + plugin = mimo_dir / "plugins" / "graphify.js" + print(f" Plugin: {'Installed' if plugin.exists() else 'Not installed'}") + + agents = (project_dir / "AGENTS.md") if project else (_mimo_global_config_dir() / "AGENTS.md") + has_agents = agents.exists() and _AGENTS_MD_MARKER in agents.read_text(encoding="utf-8") + print(f" Instructions: {'Configured' if has_agents else 'Missing'}") + print(f" Knowledge: {_read_graph_stats(project_dir / 'graphify-out' / 'graph.json')}") + + def _resolve_graphify_exe(project: bool = False) -> str: - """Return the absolute path to the graphify executable, with forward slashes. - - With *project* set, return the bare ``graphify`` command instead. A - project-scoped install writes hook config the installer then tells the user - to commit, so an absolute path resolved from the installing machine is wrong - for every other clone: it names a directory that does not exist there, and - the drive letter and ``.EXE`` casing do not even survive between two Windows - checkouts. A committed hook refers to ``graphify`` the way it would refer to - ``git`` or ``node``, and PATH resolves it per machine (#3129). - - Falls back to bare 'graphify' if resolution fails. Using an absolute path - ensures the hook works in environments where the venv Scripts/ directory is - not on PATH (e.g. VS Code Codex extension on Windows). - - The path is normalized to forward slashes so it survives every shell that - runs the hook command. On Windows, Claude Code runs command-type hooks - through Git Bash by default, where an unquoted backslash is an escape - character: a raw ``C:\\Users\\me\\graphify.EXE`` collapses to - ``C:Usersmegraphify.EXE: command not found`` and the guard silently fails. - Forward slashes are accepted by Git Bash, cmd.exe, and PowerShell alike, and - ``.replace`` is a no-op on POSIX where paths already use forward slashes. - """ - import shutil if project: return "graphify" found = shutil.which("graphify") if not found: - # Derive from sys.executable: same Scripts/ (Windows) or bin/ (Unix) dir scripts_dir = Path(sys.executable).parent for name in ("graphify.exe", "graphify"): candidate = scripts_dir / name @@ -1837,18 +2004,15 @@ def _resolve_graphify_exe(project: bool = False) -> str: found = str(candidate) break return (found or "graphify").replace("\\", "/") -def _install_codex_hook(project_dir: Path, project: bool = False) -> None: - """Add graphify PreToolUse hook to .codex/hooks.json. - A project-scoped install emits the bare command, since .codex/hooks.json is - then committed and an installing machine's path is wrong there (#3129). - """ + +def _install_codex_hook(project_dir: Path, project: bool = False) -> None: hooks_path = project_dir / ".codex" / "hooks.json" hooks_path.parent.mkdir(parents=True, exist_ok=True) - existing = _read_settings_for_merge(hooks_path) - graphify_exe = _resolve_graphify_exe(project=project) + if " " in graphify_exe and not graphify_exe.startswith('"'): + graphify_exe = f'"{graphify_exe}"' hook_entry = { "hooks": { "PreToolUse": [ @@ -1859,7 +2023,6 @@ def _install_codex_hook(project_dir: Path, project: bool = False) -> None: ] } } - hooks = existing.setdefault("hooks", {}) if not isinstance(hooks, dict): _refuse_to_modify(hooks_path) @@ -1877,7 +2040,6 @@ def _install_codex_hook(project_dir: Path, project: bool = False) -> None: def _uninstall_codex_hook(project_dir: Path) -> None: - """Remove graphify PreToolUse hook from .codex/hooks.json.""" hooks_path = project_dir / ".codex" / "hooks.json" if not hooks_path.exists(): return @@ -1894,10 +2056,10 @@ def _uninstall_codex_hook(project_dir: Path) -> None: existing["hooks"]["PreToolUse"] = filtered _write_settings_with_backup(hooks_path, existing) print(f" .codex/hooks.json -> PreToolUse hook removed") + + def _agents_install(project_dir: Path, platform: str, project: bool = False) -> None: - """Write the graphify section to the local AGENTS.md for always-on platforms.""" target = (project_dir or Path(".")) / "AGENTS.md" - if target.exists(): content, newline = _read_keep_newline(target) new_content = _replace_or_append_section( @@ -1919,61 +2081,56 @@ def _agents_install(project_dir: Path, platform: str, project: bool = False) -> _install_opencode_plugin(project_dir or Path(".")) elif platform == "kilo": _install_kilo_plugin(project_dir or Path(".")) + elif platform in ("mimo", "mimo-windows"): + _install_mimo_plugin(project_dir or Path("."), project=project) print() print( f"{platform.capitalize()} will now check the knowledge graph before answering" ) print("codebase questions and rebuild it after code changes.") - if platform not in ("codex", "opencode", "kilo"): + if platform not in ("codex", "opencode", "kilo", "mimo"): print() print("Note: unlike Claude Code, there is no PreToolUse hook equivalent for") print( f"{platform.capitalize()} — the AGENTS.md rules are the always-on mechanism." ) -def _amp_legacy_cleanup() -> None: - """Best-effort removal of the pre-fix ~/.amp/skills/graphify install dir. - Older graphify versions wrote the Amp skill to ~/.amp/skills, which Amp does - not search. Clean it up on install so a stale, never-loaded copy does not - linger. Failures are ignored (the new path is what matters). - """ + +def _amp_legacy_cleanup() -> None: legacy = Path.home() / ".amp" / "skills" / "graphify" if legacy.exists(): shutil.rmtree(legacy, ignore_errors=True) if not legacy.exists(): print(f" legacy removed -> {legacy}") + + def _amp_install(project_dir: Path | None = None) -> None: - """User-scope Amp install: skill into ~/.config/agents/skills + AGENTS.md.""" _amp_legacy_cleanup() _copy_skill_file("amp") _agents_install(project_dir or Path("."), "amp") + + def _amp_uninstall(project_dir: Path | None = None) -> None: - """User-scope Amp uninstall: remove the skill and the AGENTS.md section.""" removed = _remove_skill_file("amp") if removed: print("skill removed") _agents_uninstall(project_dir or Path("."), platform="amp") + + def _agents_platform_install(project_dir: Path | None = None) -> None: - """`graphify agents install`: skill into ~/.agents/skills + AGENTS.md. - - The amp-twin of the generic Agent-Skills target. Mirrors _amp_install but - lands the skill at the spec's user-global ~/.agents/skills (set in - _platform_skill_destination). Wiring AGENTS.md keeps it honest with the - rendered hooks reference, which points at `graphify agents install`. The bare - `graphify install --platform agents` path stays skill-only (via install()), - exactly as amp's `--platform amp` does. - """ _copy_skill_file("agents") _agents_install(project_dir or Path("."), "agents") + + def _agents_platform_uninstall(project_dir: Path | None = None) -> None: - """`graphify agents uninstall`: remove the skill and the AGENTS.md section.""" removed = _remove_skill_file("agents") if removed: print("skill removed") _agents_uninstall(project_dir or Path("."), platform="agents") + + def _project_install(platform_name: str, project_dir: Path | None = None, strict: bool = False) -> None: - """Install platform skill/config files in the current project.""" project_dir = project_dir or Path(".") platform_name = _canonical_platform(platform_name) if platform_name in ("claude", "windows"): @@ -2002,21 +2159,19 @@ def _project_install(platform_name: str, project_dir: Path | None = None, strict _devin_rules_install(project_dir) _print_project_git_add_hint([_project_scope_root(skill_dst, project_dir), project_dir / ".windsurf"]) elif platform_name == "antigravity": - # Project-scoped: skill in .agents/skills/ PLUS the .agents/rules + - # .agents/workflows always-on layer (previously this path wrote only the - # skill, leaving the rules/workflows the uninstall path removes unset). skill_dst = _copy_skill_file("antigravity", project=True, project_dir=project_dir) _antigravity_finalize(skill_dst, project_dir) _print_project_git_add_hint([_project_scope_root(skill_dst, project_dir), project_dir / ".agents"]) + elif platform_name in ("mimo", "mimo-windows"): + _mimo_install(project_dir, project=True) elif platform_name in ("copilot", "pi", "kimi", "agents"): - # Skill-only project install: drop SKILL.md (+ references) at the scope - # root. `agents` -> ./.agents/skills/graphify/SKILL.md. skill_dst = _copy_skill_file(platform_name, project=True, project_dir=project_dir) _print_project_git_add_hint([_project_scope_root(skill_dst, project_dir)]) else: install(platform=platform_name, project=True, project_dir=project_dir) + + def _project_uninstall(platform_name: str, project_dir: Path | None = None) -> None: - """Remove project-scoped platform skill/config files only.""" project_dir = project_dir or Path(".") platform_name = _canonical_platform(platform_name) if platform_name in ("claude", "windows"): @@ -2036,6 +2191,8 @@ def _project_uninstall(platform_name: str, project_dir: Path | None = None) -> N _uninstall_codex_hook(project_dir) elif platform_name == "antigravity": _antigravity_uninstall(project_dir, project=True) + elif platform_name in ("mimo", "mimo-windows"): + _mimo_uninstall(project_dir, project=True) elif platform_name == "devin": removed = _remove_skill_file("devin", project=True, project_dir=project_dir) _devin_rules_uninstall(project_dir) @@ -2046,13 +2203,12 @@ def _project_uninstall(platform_name: str, project_dir: Path | None = None) -> N if not removed: print("nothing to remove") elif platform_name == "codebuddy": - # project=True keeps `uninstall --project` project-scoped; previously - # this deleted the user-global codebuddy skill (#2215). codebuddy_uninstall(project_dir, project=True) else: _remove_skill_file(platform_name, project=True, project_dir=project_dir) + + def _project_uninstall_all(project_dir: Path | None = None) -> None: - """Remove project-scoped install files without touching user-scope installs.""" project_dir = project_dir or Path(".") print("Uninstalling project-scoped graphify files...\n") for platform_name in _PLATFORM_CONFIG: @@ -2060,8 +2216,9 @@ def _project_uninstall_all(project_dir: Path | None = None) -> None: for platform_name in ("gemini", "cursor"): _project_uninstall(platform_name, project_dir) print("\nDone.") + + def _agents_uninstall(project_dir: Path, platform: str = "") -> None: - """Remove the graphify section from the local AGENTS.md.""" target = (project_dir or Path(".")) / "AGENTS.md" if not target.exists(): @@ -2070,6 +2227,8 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None: _uninstall_opencode_plugin(project_dir or Path(".")) elif platform == "kilo": _uninstall_kilo_plugin(project_dir or Path(".")) + elif platform in ("mimo", "mimo-windows"): + _uninstall_mimo_plugin(project_dir or Path("."), project=True) return content, newline = _read_keep_newline(target) @@ -2080,9 +2239,10 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None: _uninstall_opencode_plugin(project_dir or Path(".")) elif platform == "kilo": _uninstall_kilo_plugin(project_dir or Path(".")) + elif platform in ("mimo", "mimo-windows"): + _uninstall_mimo_plugin(project_dir or Path("."), project=True) return - # A symlink stays: writing through it cleans the file it points to. if cleaned or target.is_symlink(): _write_keep_newline(target, cleaned + "\n", newline) print(f"graphify section removed from {target.resolve()}") @@ -2094,6 +2254,10 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None: _uninstall_opencode_plugin(project_dir or Path(".")) elif platform == "kilo": _uninstall_kilo_plugin(project_dir or Path(".")) + elif platform in ("mimo", "mimo-windows"): + _uninstall_mimo_plugin(project_dir or Path("."), project=True) + + def _kilo_uninstall_global() -> list[str]: removed = [] command_dst = Path.home() / ".config" / "kilo" / "command" / "graphify.md" @@ -2124,17 +2288,20 @@ def _kilo_uninstall_global() -> list[str]: break return removed + + def _kilo_install(project_dir: Path) -> None: - """Install native Kilo skill + command globally and always-on project wiring locally.""" install(platform="kilo") _agents_install(project_dir or Path("."), "kilo") + + def _kilo_uninstall(project_dir: Path) -> None: - """Remove Kilo always-on project wiring and global skill/command files.""" _agents_uninstall(project_dir or Path("."), platform="kilo") removed = _kilo_uninstall_global() print("; ".join(removed) if removed else "nothing to remove") + + def claude_install(project_dir: Path | None = None, strict: bool = False, project: bool = False) -> None: - """Write the graphify section to the local CLAUDE.md.""" target = (project_dir or Path(".")) / "CLAUDE.md" if target.exists(): @@ -2152,8 +2319,6 @@ def claude_install(project_dir: Path | None = None, strict: bool = False, projec _write_keep_newline(target, new_content, newline) print(f"graphify section written to {target.resolve()}") - # Always re-install the Claude Code PreToolUse hook so an old hook - # payload (e.g. pre-issue-#580 wording) is replaced on upgrade. _install_claude_hook(project_dir or Path("."), strict=strict, project=project) print() @@ -2162,12 +2327,9 @@ def claude_install(project_dir: Path | None = None, strict: bool = False, projec if strict: print("Strict mode: the first raw file read per session is blocked until") print("one `graphify query` runs (toggle with GRAPHIFY_HOOK_STRICT=0).") -def _install_claude_hook(project_dir: Path, strict: bool = False, project: bool = False) -> None: - """Add graphify PreToolUse hook to .claude/settings.json. - A project-scoped install emits the bare command, since .claude/settings.json - is then committed and an installing machine's path is wrong there (#3129). - """ + +def _install_claude_hook(project_dir: Path, strict: bool = False, project: bool = False) -> None: settings_path = project_dir / ".claude" / "settings.json" settings_path.parent.mkdir(parents=True, exist_ok=True) @@ -2185,18 +2347,15 @@ def _install_claude_hook(project_dir: Path, strict: bool = False, project: bool _write_settings_with_backup(settings_path, settings) _mode = " (strict)" if strict else "" print(f" .claude/settings.json -> PreToolUse hooks registered (Bash|Grep search + Read/Glob){_mode}") -def _uninstall_claude_hook(project_dir: Path) -> None: - """Remove the graphify PreToolUse hook from .claude/settings.json and its - local-only sibling .claude/settings.local.json. - A user may relocate the hook into settings.local.json so it is not committed - to a shared repo, so uninstall has to clean whichever file holds it (#1731). - """ + +def _uninstall_claude_hook(project_dir: Path) -> None: claude_dir = project_dir / ".claude" for name in ("settings.json", "settings.local.json"): _strip_graphify_hook(claude_dir / name) + + def _strip_graphify_hook(settings_path: Path) -> None: - """Drop graphify PreToolUse hooks from a single Claude settings file, if present.""" if not settings_path.exists(): return try: @@ -2215,12 +2374,7 @@ def _strip_graphify_hook(settings_path: Path) -> None: def _unsafe_purge_reason(out: Path, project_dir: Path) -> str | None: - """Why ``rmtree(out)`` must not run for ``--purge``, or None when it is safe. - - The output dir may legitimately live outside the project (an absolute - ``GRAPHIFY_OUT``), so this only rejects targets whose deletion would take - the project, the home directory, or a filesystem root with it. - """ + """Why ``rmtree(out)`` must not run for ``--purge``, or None when it is safe.""" target = out.resolve() project = project_dir.resolve() home = Path.home().resolve() @@ -2234,11 +2388,9 @@ def _unsafe_purge_reason(out: Path, project_dir: Path) -> str | None: def uninstall_all(project_dir: Path | None = None, purge: bool = False) -> None: - """Remove graphify from every platform detected in the current project.""" pd = project_dir or Path(".") out = pd / _GRAPHIFY_OUT if purge: - # Checked before any uninstaller runs, so a refused purge changes nothing. reason = _unsafe_purge_reason(out, pd) if reason: print( @@ -2249,9 +2401,6 @@ def uninstall_all(project_dir: Path | None = None, purge: bool = False) -> None: sys.exit(1) print("Uninstalling graphify from all detected platforms...\n") - # Skill-file / config-section uninstallers. remove_user_skill=True keeps the - # historical `graphify uninstall` behavior: global skill delete plus md/hook - # cleanup at the project dir (#2215). claude_uninstall(pd, remove_user_skill=True) codebuddy_uninstall(pd, remove_user_skill=True) gemini_uninstall(pd, remove_user_skill=True) @@ -2259,18 +2408,13 @@ def uninstall_all(project_dir: Path | None = None, purge: bool = False) -> None: _cursor_uninstall(pd) _kiro_uninstall(pd) _antigravity_uninstall(pd) - # AGENTS.md covers: codex, aider, opencode, claw, droid, trae, trae-cn, hermes, copilot _agents_uninstall(pd) - # Amp also drops a user-scope skill at ~/.config/agents/skills, which the - # AGENTS.md cleanup above does not touch. _remove_skill_file("amp") - # The generic agents platform's user-scope skill lives at ~/.agents/skills, - # which neither the AGENTS.md cleanup nor amp's removal reaches. _remove_skill_file("agents") _uninstall_opencode_plugin(pd) _uninstall_codex_hook(pd) + _mimo_uninstall(pd) - # Git hook try: from graphify.hooks import uninstall as hook_uninstall result = hook_uninstall(pd) @@ -2288,23 +2432,9 @@ def uninstall_all(project_dir: Path | None = None, purge: bool = False) -> None: print(f"\n {_GRAPHIFY_OUT}/ -> not found (nothing to purge)") print("\nDone. Run 'pip uninstall graphifyy' to remove the package itself.") -def claude_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: - """Remove the graphify skill tree (SKILL.md + references/) and the graphify - section from CLAUDE.md and its local-only variants, plus the PreToolUse hook. - - Mirrors gemini_uninstall: the bare `graphify uninstall` and `graphify claude - uninstall` must remove the installed skill, not just strip CLAUDE.md, or the - progressive-disclosure tree (SKILL.md + references/) is orphaned (#1121). - A user may relocate the section/hook into the local-only files Claude Code - supports so they are not committed to a shared repo, so uninstall also cleans - CLAUDE.local.md, .claude/CLAUDE.local.md and .claude/settings.local.json (#1731). - Scope rules (#2215): a bare call removes the user-global skill; passing - ``project_dir`` (or ``project=True``) scopes skill removal to that project - and leaves the global tree untouched, unless ``remove_user_skill=True`` - explicitly opts back into the global delete (as ``uninstall_all`` does). - """ +def claude_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: explicit_dir = project_dir is not None project_dir = project_dir or Path(".") if remove_user_skill is None: @@ -2322,7 +2452,6 @@ def claude_uninstall(project_dir: Path | None = None, *, project: bool = False, existing = [t for t in md_targets if t.exists()] removed_any = False for target in existing: - # Not short-circuited: every present file must be cleaned, not just the first. if _strip_graphify_md_section(target): removed_any = True @@ -2332,24 +2461,16 @@ def claude_uninstall(project_dir: Path | None = None, *, project: bool = False, print("graphify section not found in CLAUDE.md - nothing to do") _uninstall_claude_hook(project_dir) -def _strip_graphify_md_section(target: Path) -> bool: - """Strip the ## graphify section from one CLAUDE.md-style file. - Returns True if a section was removed. Deletes the file if nothing else - remains after removal. - """ + +def _strip_graphify_md_section(target: Path) -> bool: try: content, newline = _read_keep_newline(target) except (OSError, UnicodeDecodeError): - # An unreadable/undecodable CLAUDE.md-style file (e.g. non-UTF-8, or a - # directory of that name) must not abort uninstall - nothing to strip. return False - # Remove graphify's ## graphify section (heading matched exactly, never as a - # substring of a user's ### graphify) from the marker to the next H2 or EOF. cleaned = _remove_marker_section(content, _CLAUDE_MD_MARKER) if cleaned is None: return False - # A symlink stays: writing through it cleans the file it points to. if cleaned or target.is_symlink(): _write_keep_newline(target, cleaned + "\n", newline) print(f"graphify section removed from {target.resolve()}") @@ -2357,8 +2478,9 @@ def _strip_graphify_md_section(target: Path) -> bool: target.unlink() print(f"{target.name} was empty after removal - deleted {target.resolve()}") return True + + def codebuddy_install(project_dir: Path | None = None) -> None: - """Install the graphify skill and CODEBUDDY.md section for CodeBuddy.""" _copy_skill_file("codebuddy", project=bool(project_dir), project_dir=project_dir) target = (project_dir or Path(".")) / "CODEBUDDY.md" @@ -2377,14 +2499,14 @@ def codebuddy_install(project_dir: Path | None = None) -> None: _write_keep_newline(target, new_content, newline) print(f"graphify section written to {target.resolve()}") - # Also write CodeBuddy PreToolUse hook to .codebuddy/settings.json _install_codebuddy_hook(project_dir or Path(".")) print() print("CodeBuddy will now check the knowledge graph before answering") print("codebase questions and rebuild it after code changes.") + + def _install_codebuddy_hook(project_dir: Path) -> None: - """Add graphify PreToolUse hook to .codebuddy/settings.json.""" settings_path = project_dir / ".codebuddy" / "settings.json" settings_path.parent.mkdir(parents=True, exist_ok=True) @@ -2401,8 +2523,9 @@ def _install_codebuddy_hook(project_dir: Path) -> None: hooks["PreToolUse"].extend(_claude_pretooluse_hooks()) _write_settings_with_backup(settings_path, settings) print(f" .codebuddy/settings.json -> PreToolUse hooks registered") + + def _uninstall_codebuddy_hook(project_dir: Path) -> None: - """Remove graphify PreToolUse hook from .codebuddy/settings.json.""" settings_path = project_dir / ".codebuddy" / "settings.json" if not settings_path.exists(): return @@ -2419,14 +2542,9 @@ def _uninstall_codebuddy_hook(project_dir: Path) -> None: settings["hooks"]["PreToolUse"] = filtered _write_settings_with_backup(settings_path, settings) print(f" .codebuddy/settings.json -> PreToolUse hook removed") -def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: - """Remove the graphify skill tree (SKILL.md + references/) and the CODEBUDDY.md section. - Scope rules (#2215): a bare call removes the user-global skill; passing - ``project_dir`` (or ``project=True``) scopes skill removal to that project - and leaves the global tree untouched, unless ``remove_user_skill=True`` - explicitly opts back into the global delete (as ``uninstall_all`` does). - """ + +def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = False, remove_user_skill: bool | None = None) -> None: explicit_dir = project_dir is not None project_dir = project_dir or Path(".") if remove_user_skill is None: @@ -2447,7 +2565,6 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals print("graphify section not found in CODEBUDDY.md - nothing to do") return - # A symlink stays: writing through it cleans the file it points to. if cleaned or target.is_symlink(): _write_keep_newline(target, cleaned + "\n", newline) print(f"graphify section removed from {target.resolve()}") @@ -2459,43 +2576,17 @@ def codebuddy_uninstall(project_dir: Path | None = None, *, project: bool = Fals _CLI_INSTALL_COMMANDS = frozenset({ - "agents", - "aider", - "amp", - "antigravity", - "claude", - "claw", - "codebuddy", - "codex", - "copilot", - "cursor", - "devin", - "droid", - "gemini", - "hermes", - "install", - "kilo", - "kiro", - "opencode", - "pi", - "skills", - "trae", - "trae-cn", - "uninstall", - "vscode", + "agents", "aider", "amp", "antigravity", "claude", "claw", "codebuddy", + "codex", "copilot", "cursor", "devin", "droid", "gemini", "hermes", + "install", "kilo", "kiro", "mimo", "opencode", "pi", "skills", "trae", + "trae-cn", "uninstall", "vscode", }) def dispatch_install_cli(cmd: str) -> bool: - """Handle `graphify ` dispatch (install/uninstall + every - per-platform install target). Returns True if cmd was an install command and - was handled, False otherwise so the caller can continue its own dispatch. - Moved verbatim from __main__.main(). - """ if cmd not in _CLI_INSTALL_COMMANDS: return False if cmd == "install": - # Default to windows platform on Windows, claude elsewhere default_platform = "windows" if platform.system() == "Windows" else "claude" selected_platform: str | None = None project_scope = False @@ -2550,7 +2641,9 @@ def dispatch_install_cli(cmd: str) -> bool: file=sys.stderr, ) install(platform=chosen_platform) - elif cmd == "uninstall": + return True + + if cmd == "uninstall": args = sys.argv[2:] purge = "--purge" in args project_scope = "--project" in args @@ -2582,7 +2675,9 @@ def dispatch_install_cli(cmd: str) -> bool: _project_uninstall_all(Path(".")) else: uninstall_all(purge=purge) - elif cmd == "claude": + return True + + if cmd == "claude": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": _strict = "--strict" in sys.argv[3:] @@ -2598,7 +2693,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify claude [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "codebuddy": + return True + + if cmd == "codebuddy": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": codebuddy_install() @@ -2607,7 +2704,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify codebuddy [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "gemini": + return True + + if cmd == "gemini": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": gemini_install(project=("--project" in sys.argv[3:])) @@ -2616,7 +2715,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify gemini [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "cursor": + return True + + if cmd == "cursor": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": _cursor_install(Path(".")) @@ -2625,7 +2726,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify cursor [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "vscode": + return True + + if cmd == "vscode": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": vscode_install() @@ -2634,7 +2737,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify vscode [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "copilot": + return True + + if cmd == "copilot": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2650,7 +2755,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify copilot [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "kilo": + return True + + if cmd == "kilo": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": _kilo_install(Path(".")) @@ -2659,7 +2766,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify kilo [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "kiro": + return True + + if cmd == "kiro": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": _kiro_install(Path(".")) @@ -2668,7 +2777,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify kiro [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "devin": + return True + + if cmd == "devin": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2684,7 +2795,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify devin [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "pi": + return True + + if cmd == "pi": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2697,7 +2810,6 @@ def dispatch_install_cli(cmd: str) -> bool: else: _remove_skill_file("pi") elif subcmd == "guard" and sys.argv[3:] in (["install"], ["uninstall"], ["path"]): - # The native guard package ships inside the wheel; Pi owns its registration. package_path = Path(__file__).resolve().parent / "pi" if sys.argv[3] == "path": print(package_path) @@ -2714,7 +2826,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify pi [install|uninstall] | graphify pi guard [install|uninstall|path]", file=sys.stderr) sys.exit(1) - elif cmd == "amp": + return True + + if cmd == "amp": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2729,7 +2843,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify amp [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd in ("agents", "skills"): + return True + + if cmd in ("agents", "skills"): subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2744,7 +2860,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print(f"Usage: graphify {cmd} [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd in ("aider", "codex", "opencode", "claw", "droid", "trae", "trae-cn", "hermes"): + return True + + if cmd in ("aider", "codex", "opencode", "claw", "droid", "trae", "trae-cn", "hermes"): subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2761,7 +2879,9 @@ def dispatch_install_cli(cmd: str) -> bool: else: print(f"Usage: graphify {cmd} [install|uninstall]", file=sys.stderr) sys.exit(1) - elif cmd == "antigravity": + return True + + if cmd == "antigravity": subcmd = sys.argv[2] if len(sys.argv) > 2 else "" if subcmd == "install": if "--project" in sys.argv[3:]: @@ -2776,4 +2896,59 @@ def dispatch_install_cli(cmd: str) -> bool: else: print("Usage: graphify antigravity [install|uninstall]", file=sys.stderr) sys.exit(1) - return True + return True + + if cmd == "mimo": + subcmd = sys.argv[2] if len(sys.argv) > 2 else "" + args = sys.argv[3:] + project_scope = False + i = 0 + while i < len(args): + arg = args[i] + if arg in ("-h", "--help"): + print("Usage: graphify mimo [install|uninstall|status] [--project] [--scope project|global]", file=sys.stderr) + return True + elif arg in ("--project", "--scope=project"): + project_scope = True + i += 1 + elif arg == "--scope=global": + project_scope = False + i += 1 + elif arg == "--scope": + if i + 1 >= len(args): + print("error: --scope requires a value (project|global)", file=sys.stderr) + sys.exit(1) + val = args[i + 1] + if val == "project": + project_scope = True + elif val == "global": + project_scope = False + else: + print(f"error: invalid scope '{val}'. Choose from: project, global", file=sys.stderr) + sys.exit(1) + i += 2 + elif arg.startswith("-"): + print(f"error: unknown mimo option '{arg}'", file=sys.stderr) + sys.exit(1) + else: + print(f"error: unexpected argument '{arg}'", file=sys.stderr) + sys.exit(1) + + if subcmd == "install": + if project_scope: + _project_install("mimo", Path(".")) + else: + _mimo_install(Path(".")) + elif subcmd == "uninstall": + if project_scope: + _project_uninstall("mimo", Path(".")) + else: + _mimo_uninstall(Path(".")) + elif subcmd == "status": + _mimo_status(Path("."), project=project_scope) + else: + print("Usage: graphify mimo [install|uninstall|status] [--project]", file=sys.stderr) + sys.exit(1) + return True + + return False \ No newline at end of file diff --git a/tests/test_detect.py b/tests/test_detect.py index 6b47ea265..6a26b26a5 100644 --- a/tests/test_detect.py +++ b/tests/test_detect.py @@ -2487,6 +2487,7 @@ def test_detect_keeps_skills_graphify_source_layouts(tmp_path): ".github/copilot-instructions.md", ".claude/settings.json", ".codebuddy/settings.json", ".codex/hooks.json", ".gemini/settings.json", ".kilo/kilo.json", ".opencode/opencode.json", + ".mimocode/mimocode.json", } diff --git a/tests/test_install.py b/tests/test_install.py index 6ecd893f3..f18941a68 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -20,6 +20,7 @@ "trae": (".trae/skills/graphify/SKILL.md",), "trae-cn": (".trae-cn/skills/graphify/SKILL.md",), "windows": (".claude/skills/graphify/SKILL.md",), + "mimo": (".config/mimocode/skills/graphify/SKILL.md",), } @@ -1684,9 +1685,11 @@ def test_codex_hook_command_is_a_real_cli_subcommand(tmp_path): dispatched = _cli_dispatched_commands() assert "hook-check" in dispatched, "sanity: parser must find known commands" + import shlex + for entry in entries: # command is " [args...]" - parts = entry["command"].split() + parts = shlex.split(entry["command"]) subcommand = parts[1] if len(parts) > 1 else "" assert subcommand in dispatched, ( f"codex hook registers {subcommand!r}, which the CLI does not dispatch " diff --git a/tests/test_install_roundtrip.py b/tests/test_install_roundtrip.py index 42ced648f..8af2d2025 100644 --- a/tests/test_install_roundtrip.py +++ b/tests/test_install_roundtrip.py @@ -59,6 +59,8 @@ def test_skill_roundtrip_at_real_destination(platform, project, tmp_path, monkey monkeypatch.chdir(project_dir) # hermes user scope lands under %LOCALAPPDATA% on Windows; keep it inside tmp monkeypatch.setenv("LOCALAPPDATA", str(home / "AppData" / "Local")) + monkeypatch.delenv("MIMOCODE_HOME", raising=False) + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) with patch("graphify.__main__.Path.home", return_value=home): dst = mainmod._platform_skill_destination( diff --git a/tests/test_mimo_install.py b/tests/test_mimo_install.py new file mode 100644 index 000000000..47f509b97 --- /dev/null +++ b/tests/test_mimo_install.py @@ -0,0 +1,61 @@ +import json +import pytest +from pathlib import Path +from graphify.install import ( + _platform_skill_destination, + _preserve_jsonc_plugin_add, + _preserve_jsonc_plugin_remove, + _mimo_install, + _mimo_uninstall, + _mimo_status, + dispatch_install_cli, +) + +def test_platform_skill_destination_mimo(tmp_path): + # چک کردن مسیر مقصد اسکیل + dest_proj = _platform_skill_destination("mimo", project=True, project_dir=tmp_path) + assert dest_proj == tmp_path / ".mimocode" / "skills" / "graphify" / "SKILL.md" + +def test_jsonc_trailing_comment_comma(): + # ایراد دوم ربات: کامنت تک‌خطی و درج اشتباه کاما + raw = '{\n "theme": "dark" // my trailing comment\n}' + result = _preserve_jsonc_plugin_add(raw, "./plugins/graphify.js") + + # کاما باید بعد از "dark" باشد نه بعد از کامنت + assert '"dark",' in result + assert not result.strip().endswith("// my trailing comment,") + + # تست اعتبار خروجی جی‌سان + clean = "\n".join(line.split("//")[0] for line in result.splitlines()) + parsed = json.loads(clean) + assert parsed["plugin"] == ["./plugins/graphify.js"] + +def test_invalid_plugin_type_rejected(): + # ایراد سوم ربات: رد کردن مقادیر نامعتبر به جای بازنویسی بی‌صدا + raw = '{\n "plugin": 12345\n}' + with pytest.raises(SystemExit): + _preserve_jsonc_plugin_add(raw, "./plugins/graphify.js") + +def test_mimo_install_and_uninstall_lifecycle(tmp_path, monkeypatch): + # اجرای کامل install و uninstall جهت پوشش Formal Verification + monkeypatch.chdir(tmp_path) + + # ۱. تست Install + _mimo_install(tmp_path, project=True) + + skill_file = tmp_path / ".mimocode" / "skills" / "graphify" / "SKILL.md" + plugin_file = tmp_path / ".mimocode" / "plugins" / "graphify.js" + config_file = tmp_path / ".mimocode" / "mimocode.json" + agents_file = tmp_path / "AGENTS.md" + + assert skill_file.exists() + assert plugin_file.exists() + assert config_file.exists() + assert agents_file.exists() + + # ۲. تست Status + _mimo_status(tmp_path, project=True) + + # ۳. تست Uninstall + _mimo_uninstall(tmp_path, project=True) + assert not plugin_file.exists() \ No newline at end of file diff --git a/tests/test_uninstall_scope.py b/tests/test_uninstall_scope.py index 989534b0d..7dbd17dbe 100644 --- a/tests/test_uninstall_scope.py +++ b/tests/test_uninstall_scope.py @@ -12,6 +12,7 @@ """ from __future__ import annotations +import json from pathlib import Path import pytest @@ -40,10 +41,19 @@ def _plant_skill_tree(root: Path, dot_dir: str) -> Path: return skill_dir +def _global_dot_dir(platform: str, default_dot_dir: str) -> str: + from graphify.install import _platform_skill_destination + dst = _platform_skill_destination(platform, project=False) + try: + return dst.relative_to(Path.home()).parts[0] + except ValueError: + return default_dot_dir + + @pytest.mark.parametrize("uninstall_fn,platform,dot_dir", PLATFORMS) def test_project_dir_call_never_touches_global(uninstall_fn, platform, dot_dir, tmp_path): """fn(project_dir) removes only the project skill tree (#2215 trap closed).""" - global_tree = _plant_skill_tree(Path.home(), dot_dir) + global_tree = _plant_skill_tree(Path.home(), _global_dot_dir(platform, dot_dir)) proj_dir = tmp_path / "proj" project_tree = _plant_skill_tree(proj_dir, dot_dir) @@ -59,7 +69,7 @@ def test_project_dir_call_never_touches_global(uninstall_fn, platform, dot_dir, @pytest.mark.parametrize("uninstall_fn,platform,dot_dir", PLATFORMS) def test_bare_call_still_removes_global(uninstall_fn, platform, dot_dir, tmp_path, monkeypatch): """fn() with no args keeps the historical CLI behavior: global skill removed.""" - global_tree = _plant_skill_tree(Path.home(), dot_dir) + global_tree = _plant_skill_tree(Path.home(), _global_dot_dir(platform, dot_dir)) cwd = tmp_path / "empty-cwd" cwd.mkdir() monkeypatch.chdir(cwd) @@ -73,7 +83,7 @@ def test_bare_call_still_removes_global(uninstall_fn, platform, dot_dir, tmp_pat @pytest.mark.parametrize("uninstall_fn,platform,dot_dir", PLATFORMS) def test_remove_user_skill_opt_in_with_project_dir(uninstall_fn, platform, dot_dir, tmp_path): """fn(pd, remove_user_skill=True) removes the global skill, leaves the project tree.""" - global_tree = _plant_skill_tree(Path.home(), dot_dir) + global_tree = _plant_skill_tree(Path.home(), _global_dot_dir(platform, dot_dir)) proj_dir = tmp_path / "proj" project_tree = _plant_skill_tree(proj_dir, dot_dir) @@ -88,7 +98,7 @@ def test_remove_user_skill_opt_in_with_project_dir(uninstall_fn, platform, dot_d @pytest.mark.parametrize("uninstall_fn,platform,dot_dir", PLATFORMS) def test_project_true_removes_only_project_tree(uninstall_fn, platform, dot_dir, tmp_path): """fn(pd, project=True) removes only the project skill tree.""" - global_tree = _plant_skill_tree(Path.home(), dot_dir) + global_tree = _plant_skill_tree(Path.home(), _global_dot_dir(platform, dot_dir)) proj_dir = tmp_path / "proj" project_tree = _plant_skill_tree(proj_dir, dot_dir) @@ -113,6 +123,36 @@ def test_project_uninstall_codebuddy_spares_global(tmp_path): assert not project_tree.exists() +def test_project_uninstall_mimo_spares_global(tmp_path, monkeypatch): + """`graphify uninstall --project` (mimo branch) must not delete ~/.config/mimocode.""" + home = tmp_path / "home" + monkeypatch.setattr(Path, "home", lambda: home) + global_dir = home / ".config" / "mimocode" + global_tree = _plant_skill_tree(global_dir, "") + global_plugin = global_dir / "plugins" / "graphify.js" + global_plugin.parent.mkdir(parents=True, exist_ok=True) + global_plugin.write_text("// global plugin\n", encoding="utf-8") + global_cfg = global_dir / "mimocode.json" + global_cfg.write_text(json.dumps({"plugin": [global_plugin.as_posix()]}), encoding="utf-8") + + proj_dir = tmp_path / "proj" + project_tree = _plant_skill_tree(proj_dir, ".mimocode") + proj_plugin = proj_dir / ".mimocode" / "plugins" / "graphify.js" + proj_plugin.parent.mkdir(parents=True, exist_ok=True) + proj_plugin.write_text("// proj plugin\n", encoding="utf-8") + proj_cfg = proj_dir / ".mimocode" / "mimocode.json" + proj_cfg.write_text(json.dumps({"plugin": ["./plugins/graphify.js"]}), encoding="utf-8") + + _project_uninstall("mimo", proj_dir) + + assert (global_tree / "SKILL.md").exists(), "CLI --project uninstall deleted the global mimo skill" + assert (global_tree / ".graphify_version").exists() + assert global_plugin.exists(), "CLI --project uninstall deleted the global mimo plugin" + assert global_cfg.exists(), "CLI --project uninstall deleted the global mimo config" + assert not (project_tree / "SKILL.md").exists() + assert not proj_plugin.exists() + + import pytest as _pytest_posix