Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions graphify/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,24 @@ def _uninstall_codex_hook(project_dir: Path) -> None:
def _agents_install(project_dir: Path, platform: str) -> None:
"""Write the graphify section to the local AGENTS.md (Codex/OpenCode/OpenClaw)."""
target = (project_dir or Path(".")) / "AGENTS.md"
agents_already_configured = False

if target.exists():
content = target.read_text(encoding="utf-8")
if _AGENTS_MD_MARKER in content:
print(f"graphify already configured in AGENTS.md")
return
new_content = content.rstrip() + "\n\n" + _AGENTS_MD_SECTION
agents_already_configured = True
else:
new_content = content.rstrip() + "\n\n" + _AGENTS_MD_SECTION
else:
new_content = _AGENTS_MD_SECTION

target.write_text(new_content, encoding="utf-8")
print(f"graphify section written to {target.resolve()}")
if agents_already_configured:
print("graphify already configured in AGENTS.md")
if platform not in ("codex", "opencode"):
return
else:
target.write_text(new_content, encoding="utf-8")
print(f"graphify section written to {target.resolve()}")

if platform == "codex":
_install_codex_hook(project_dir or Path("."))
Expand Down
25 changes: 24 additions & 1 deletion tests/test_install.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for graphify install --platform routing."""
import json
from pathlib import Path
from unittest.mock import patch
import pytest
Expand Down Expand Up @@ -122,11 +123,17 @@ def _agents_uninstall(tmp_path):


def test_codex_agents_install_writes_agents_md(tmp_path):
from graphify.__main__ import _CODEX_HOOK

_agents_install(tmp_path, "codex")
agents_md = tmp_path / "AGENTS.md"
assert agents_md.exists()
assert "graphify" in agents_md.read_text()
assert "GRAPH_REPORT.md" in agents_md.read_text()
hooks_path = tmp_path / ".codex" / "hooks.json"
assert hooks_path.exists()
hooks = json.loads(hooks_path.read_text())
assert hooks["hooks"]["PreToolUse"] == _CODEX_HOOK["hooks"]["PreToolUse"]


def test_opencode_agents_install_writes_agents_md(tmp_path):
Expand All @@ -140,11 +147,27 @@ def test_claw_agents_install_writes_agents_md(tmp_path):


def test_agents_install_idempotent(tmp_path):
"""Installing twice does not duplicate the section."""
"""Installing twice does not duplicate the section or Codex hook."""
_agents_install(tmp_path, "codex")
_agents_install(tmp_path, "codex")
content = (tmp_path / "AGENTS.md").read_text()
assert content.count("## graphify") == 1
hooks = json.loads((tmp_path / ".codex" / "hooks.json").read_text())
assert len(hooks["hooks"]["PreToolUse"]) == 1


def test_codex_agents_install_repairs_missing_hook_after_partial_install(tmp_path):
"""A rerun heals a partial Codex install where only AGENTS.md exists."""
from graphify.__main__ import _AGENTS_MD_SECTION

(tmp_path / "AGENTS.md").write_text(_AGENTS_MD_SECTION, encoding="utf-8")

_agents_install(tmp_path, "codex")

hooks_path = tmp_path / ".codex" / "hooks.json"
assert hooks_path.exists()
hooks = json.loads(hooks_path.read_text())
assert len(hooks["hooks"]["PreToolUse"]) == 1


def test_agents_install_appends_to_existing(tmp_path):
Expand Down