|
| 1 | +import json |
| 2 | +from datetime import datetime, timezone |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pygitguardian.models import AIDiscovery, MCPConfiguration, MCPServer, UserInfo |
| 8 | + |
| 9 | +from ggshield.verticals.ai.agents.codex import Codex |
| 10 | + |
| 11 | + |
| 12 | +def _function_call_entry( |
| 13 | + *, |
| 14 | + name: str = "get_issue", |
| 15 | + namespace: str = "mcp__linear__", |
| 16 | + arguments: str = '{"id": "NHI-1"}', |
| 17 | + timestamp: str = "2026-04-01T09:00:00.000Z", |
| 18 | +) -> dict: |
| 19 | + """Build a Codex JSONL response_item line carrying an MCP function_call.""" |
| 20 | + return { |
| 21 | + "timestamp": timestamp, |
| 22 | + "type": "response_item", |
| 23 | + "payload": { |
| 24 | + "type": "function_call", |
| 25 | + "name": name, |
| 26 | + "namespace": namespace, |
| 27 | + "arguments": arguments, |
| 28 | + "call_id": "call_AAA", |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +def _session_meta(cwd: str = "/home/u/repo") -> dict: |
| 34 | + return { |
| 35 | + "timestamp": "2026-04-01T08:55:00.000Z", |
| 36 | + "type": "session_meta", |
| 37 | + "payload": {"id": "sess-1", "cwd": cwd, "cli_version": "0.130.0"}, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def _turn_context(cwd: str = "/home/u/repo", model: str = "gpt-5.5") -> dict: |
| 42 | + return { |
| 43 | + "timestamp": "2026-04-01T08:56:00.000Z", |
| 44 | + "type": "turn_context", |
| 45 | + "payload": {"turn_id": "turn-1", "cwd": cwd, "model": model}, |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def _write_session(path: Path, entries: list[dict]) -> None: |
| 50 | + path.write_text("\n".join(json.dumps(e) for e in entries) + "\n") |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def empty_ai_config() -> AIDiscovery: |
| 55 | + return AIDiscovery( |
| 56 | + user=UserInfo(hostname="h", username="u", machine_id="m"), |
| 57 | + servers=[], |
| 58 | + discovery_duration=0.0, |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +class TestCodexParseSessionFile: |
| 63 | + def test_extracts_mcp_tool_use(self, tmp_path: Path, empty_ai_config) -> None: |
| 64 | + path = tmp_path / "rollout.jsonl" |
| 65 | + _write_session( |
| 66 | + path, |
| 67 | + [ |
| 68 | + _session_meta(), |
| 69 | + _turn_context(model="gpt-5.5"), |
| 70 | + _function_call_entry(), |
| 71 | + ], |
| 72 | + ) |
| 73 | + |
| 74 | + events = list(Codex()._parse_session_file(path, empty_ai_config)) |
| 75 | + |
| 76 | + assert len(events) == 1 |
| 77 | + ev = events[0] |
| 78 | + assert ev.tool == "get_issue" |
| 79 | + assert ev.server == "linear" |
| 80 | + assert ev.agent == "codex" |
| 81 | + assert ev.model == "gpt-5.5" |
| 82 | + assert ev.cwd == "/home/u/repo" |
| 83 | + assert ev.input == {"id": "NHI-1"} |
| 84 | + assert ev.timestamp == datetime(2026, 4, 1, 9, 0, tzinfo=timezone.utc) |
| 85 | + |
| 86 | + def test_ignores_non_mcp_function_calls( |
| 87 | + self, tmp_path: Path, empty_ai_config |
| 88 | + ) -> None: |
| 89 | + path = tmp_path / "rollout.jsonl" |
| 90 | + _write_session( |
| 91 | + path, |
| 92 | + [ |
| 93 | + _session_meta(), |
| 94 | + _turn_context(), |
| 95 | + # Built-in shell tool: no namespace. |
| 96 | + { |
| 97 | + "timestamp": "2026-04-01T09:00:01.000Z", |
| 98 | + "type": "response_item", |
| 99 | + "payload": { |
| 100 | + "type": "function_call", |
| 101 | + "name": "exec_command", |
| 102 | + "arguments": '{"cmd": "ls"}', |
| 103 | + "call_id": "x", |
| 104 | + }, |
| 105 | + }, |
| 106 | + # A non-function_call response_item. |
| 107 | + { |
| 108 | + "timestamp": "2026-04-01T09:00:02.000Z", |
| 109 | + "type": "response_item", |
| 110 | + "payload": {"type": "message", "role": "assistant"}, |
| 111 | + }, |
| 112 | + ], |
| 113 | + ) |
| 114 | + assert list(Codex()._parse_session_file(path, empty_ai_config)) == [] |
| 115 | + |
| 116 | + def test_tracks_cwd_and_model_across_turns( |
| 117 | + self, tmp_path: Path, empty_ai_config |
| 118 | + ) -> None: |
| 119 | + path = tmp_path / "rollout.jsonl" |
| 120 | + _write_session( |
| 121 | + path, |
| 122 | + [ |
| 123 | + _session_meta(cwd="/home/u/initial"), |
| 124 | + _turn_context(cwd="/home/u/turn1", model="gpt-5.5"), |
| 125 | + _function_call_entry(timestamp="2026-04-01T09:00:01.000Z"), |
| 126 | + _turn_context(cwd="/home/u/turn2", model="gpt-5.6"), |
| 127 | + _function_call_entry(timestamp="2026-04-01T09:00:02.000Z"), |
| 128 | + ], |
| 129 | + ) |
| 130 | + |
| 131 | + events = list(Codex()._parse_session_file(path, empty_ai_config)) |
| 132 | + |
| 133 | + assert [(e.cwd, e.model) for e in events] == [ |
| 134 | + ("/home/u/turn1", "gpt-5.5"), |
| 135 | + ("/home/u/turn2", "gpt-5.6"), |
| 136 | + ] |
| 137 | + |
| 138 | + def test_resolves_server_display_name_from_discovery(self, tmp_path: Path) -> None: |
| 139 | + config = AIDiscovery( |
| 140 | + user=UserInfo(hostname="h", username="u", machine_id="m"), |
| 141 | + servers=[ |
| 142 | + MCPServer( |
| 143 | + name="LinearDisplay", |
| 144 | + configurations=[ |
| 145 | + MCPConfiguration( |
| 146 | + name="linear", |
| 147 | + agent="codex", |
| 148 | + scope=MCPConfiguration.Scope.USER, |
| 149 | + transport=MCPConfiguration.Transport.HTTP, |
| 150 | + project=None, |
| 151 | + ) |
| 152 | + ], |
| 153 | + ), |
| 154 | + ], |
| 155 | + discovery_duration=0.0, |
| 156 | + ) |
| 157 | + path = tmp_path / "rollout.jsonl" |
| 158 | + _write_session(path, [_session_meta(), _turn_context(), _function_call_entry()]) |
| 159 | + |
| 160 | + events = list(Codex()._parse_session_file(path, config)) |
| 161 | + |
| 162 | + assert events[0].server == "LinearDisplay" |
| 163 | + |
| 164 | + |
| 165 | +class TestCodexHistoryFiles: |
| 166 | + def test_globs_rollouts_under_dated_dirs(self, tmp_path: Path) -> None: |
| 167 | + sessions = tmp_path / ".codex" / "sessions" |
| 168 | + (sessions / "2026" / "04" / "01").mkdir(parents=True) |
| 169 | + (sessions / "2026" / "04" / "01" / "rollout-1.jsonl").write_text("{}\n") |
| 170 | + (sessions / "2026" / "04" / "02").mkdir(parents=True) |
| 171 | + (sessions / "2026" / "04" / "02" / "rollout-2.jsonl").write_text("{}\n") |
| 172 | + # Wrong depth — should be ignored. |
| 173 | + (sessions / "loose.jsonl").write_text("{}\n") |
| 174 | + # Wrong prefix — should be ignored. |
| 175 | + (sessions / "2026" / "04" / "02" / "other.jsonl").write_text("{}\n") |
| 176 | + |
| 177 | + with patch( |
| 178 | + "ggshield.verticals.ai.agents.codex.get_user_home_dir", |
| 179 | + return_value=tmp_path, |
| 180 | + ): |
| 181 | + files = sorted(Codex()._history_files()) |
| 182 | + |
| 183 | + assert [f.name for f in files] == ["rollout-1.jsonl", "rollout-2.jsonl"] |
0 commit comments