feat: Enhance review prompt with detailed instructions and placeholders for empty sections
This commit is contained in:
@@ -5,18 +5,37 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from gitea_codex_bot.config import get_settings
|
||||
from gitea_codex_bot.services.gitea import PullRequestContext
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
from gitea_codex_bot.workers.container_runner import (
|
||||
CONTAINER_CODEX_HOME,
|
||||
RESULT_END_MARKER,
|
||||
RESULT_START_MARKER,
|
||||
_build_docker_command,
|
||||
_build_install_and_run_command,
|
||||
_extract_result_meta_from_codex_stdout,
|
||||
_load_codex_auth_json_b64,
|
||||
_parse_codex_exec_stdout,
|
||||
_load_repo_review_config_from_gitea,
|
||||
_parse_review_result_from_stdout_artifact,
|
||||
_resolve_codex_auth_json_path,
|
||||
run_review_ephemeral,
|
||||
)
|
||||
|
||||
|
||||
def _sample_pr() -> PullRequestContext:
|
||||
return PullRequestContext(
|
||||
repo="acme/repo",
|
||||
pr_number=1,
|
||||
base_ref="main",
|
||||
base_sha="b" * 40,
|
||||
head_ref="feature",
|
||||
head_sha="a" * 40,
|
||||
clone_url="https://gitea.test/acme/repo.git",
|
||||
html_url="https://gitea.test/acme/repo/pulls/1",
|
||||
is_fork=False,
|
||||
)
|
||||
|
||||
|
||||
def test_build_docker_command_api_key_mode_uses_openai_env() -> None:
|
||||
settings = get_settings()
|
||||
|
||||
@@ -25,6 +44,8 @@ def test_build_docker_command_api_key_mode_uses_openai_env() -> None:
|
||||
assert "OPENAI_API_KEY" in cmd
|
||||
assert "OPENAI_ORG_ID" in cmd
|
||||
assert "OPENAI_PROJECT_ID" in cmd
|
||||
assert "GITEA_TOKEN" in cmd
|
||||
assert "GITEA_GIT_USERNAME" in cmd
|
||||
assert "--mount" not in cmd
|
||||
|
||||
|
||||
@@ -45,37 +66,40 @@ def test_build_docker_command_chatgpt_mode_mounts_auth_json(
|
||||
assert "OPENAI_API_KEY" not in cmd
|
||||
assert f"CODEX_HOME={CONTAINER_CODEX_HOME}" in env_items
|
||||
assert "CODEX_AUTH_JSON_B64" in env_items
|
||||
assert "GITEA_TOKEN" in env_items
|
||||
assert "GITEA_GIT_USERNAME" in env_items
|
||||
|
||||
|
||||
def test_build_install_command_chatgpt_mode_copies_auth_json(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
||||
def test_build_install_command_chatgpt_mode_sets_git_checkout_and_review(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
||||
auth_file = tmp_path / "auth.json"
|
||||
auth_file.write_text("{}", encoding="utf-8")
|
||||
monkeypatch.setenv("CODEX_AUTH_MODE", "chatgpt")
|
||||
monkeypatch.setenv("CODEX_AUTH_JSON_PATH", str(auth_file))
|
||||
get_settings.cache_clear()
|
||||
settings = get_settings()
|
||||
pr = _sample_pr()
|
||||
|
||||
command = _build_install_and_run_command(settings)
|
||||
command = _build_install_and_run_command(settings, pr=pr)
|
||||
|
||||
assert 'printf "%s" "$CODEX_AUTH_JSON_B64" | base64 -d > /root/.codex/auth.json' in command
|
||||
assert "codex exec --skip-git-repo-check --json -m gpt-5.3-codex" in command
|
||||
assert f"--reasoning-effort {settings.openai_reasoning_effort}" in command
|
||||
|
||||
|
||||
def test_build_install_command_includes_configured_reasoning_effort(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("OPENAI_REASONING_EFFORT", "medium")
|
||||
get_settings.cache_clear()
|
||||
settings = get_settings()
|
||||
|
||||
command = _build_install_and_run_command(settings)
|
||||
|
||||
assert "--reasoning-effort medium" in command
|
||||
assert "git -c http.extraHeader=" in command
|
||||
assert f"clone --no-tags --depth 80 {pr.clone_url} /work/repo" in command
|
||||
assert f"fetch --no-tags origin {pr.base_ref} {pr.head_ref}" in command
|
||||
assert f"git checkout --detach {pr.head_sha}" in command
|
||||
assert "resolved_head=\"$(git rev-parse HEAD)\"" in command
|
||||
assert "unset GITEA_TOKEN auth_b64" in command
|
||||
assert f"codex exec review --base {pr.base_sha}" in command
|
||||
assert "--output-schema /tmp/codex-review-schema.json" in command
|
||||
assert "-o /tmp/codex-review-result.json" in command
|
||||
assert f'echo "{RESULT_START_MARKER}"' in command
|
||||
assert f'echo "{RESULT_END_MARKER}"' in command
|
||||
|
||||
|
||||
def test_build_install_command_can_disable_reasoning_effort_flag() -> None:
|
||||
settings = get_settings()
|
||||
pr = _sample_pr()
|
||||
|
||||
command = _build_install_and_run_command(settings, include_reasoning_effort=False)
|
||||
command = _build_install_and_run_command(settings, pr=pr, include_reasoning_effort=False)
|
||||
|
||||
assert "--reasoning-effort" not in command
|
||||
|
||||
@@ -104,6 +128,29 @@ def test_load_codex_auth_json_b64_roundtrip(monkeypatch: pytest.MonkeyPatch, tmp
|
||||
assert encoded
|
||||
|
||||
|
||||
def test_load_repo_review_config_from_gitea_when_missing() -> None:
|
||||
class _Gitea:
|
||||
def get_file_content(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
cfg = _load_repo_review_config_from_gitea(_Gitea(), "acme/repo", "a" * 40)
|
||||
|
||||
assert cfg.configured is False
|
||||
assert cfg.enabled is True
|
||||
|
||||
|
||||
def test_load_repo_review_config_from_gitea_when_present() -> None:
|
||||
class _Gitea:
|
||||
def get_file_content(self, *_args, **_kwargs):
|
||||
return "enabled: false\nreview:\n default_mode: tests\n"
|
||||
|
||||
cfg = _load_repo_review_config_from_gitea(_Gitea(), "acme/repo", "a" * 40)
|
||||
|
||||
assert cfg.configured is True
|
||||
assert cfg.enabled is False
|
||||
assert cfg.default_mode == "tests"
|
||||
|
||||
|
||||
def test_run_review_ephemeral_chatgpt_does_not_fallback_to_api_key_path(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Path,
|
||||
@@ -115,18 +162,22 @@ def test_run_review_ephemeral_chatgpt_does_not_fallback_to_api_key_path(
|
||||
get_settings.cache_clear()
|
||||
settings = get_settings()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"gitea_codex_bot.workers.container_runner.prepare_review_prompt",
|
||||
lambda *_args, **_kwargs: ("prompt", {"diff": ""}, object()),
|
||||
)
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", lambda _settings: object())
|
||||
class _FakeGiteaClient:
|
||||
def __init__(self, _settings) -> None:
|
||||
pass
|
||||
|
||||
def get_pull_request(self, *_args, **_kwargs):
|
||||
return _sample_pr()
|
||||
|
||||
def get_file_content(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", _FakeGiteaClient)
|
||||
monkeypatch.setattr(
|
||||
"gitea_codex_bot.workers.container_runner.subprocess.run",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("docker unavailable")),
|
||||
)
|
||||
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
|
||||
result, _repo_cfg = run_review_ephemeral(
|
||||
settings,
|
||||
repo="acme/repo",
|
||||
@@ -142,18 +193,22 @@ def test_run_review_ephemeral_api_key_mode_does_not_fallback_to_host(monkeypatch
|
||||
get_settings.cache_clear()
|
||||
settings = get_settings()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"gitea_codex_bot.workers.container_runner.prepare_review_prompt",
|
||||
lambda *_args, **_kwargs: ("prompt", {"diff": ""}, object()),
|
||||
)
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", lambda _settings: object())
|
||||
class _FakeGiteaClient:
|
||||
def __init__(self, _settings) -> None:
|
||||
pass
|
||||
|
||||
def get_pull_request(self, *_args, **_kwargs):
|
||||
return _sample_pr()
|
||||
|
||||
def get_file_content(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", _FakeGiteaClient)
|
||||
monkeypatch.setattr(
|
||||
"gitea_codex_bot.workers.container_runner.subprocess.run",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("docker unavailable")),
|
||||
)
|
||||
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
|
||||
result, _repo_cfg = run_review_ephemeral(
|
||||
settings,
|
||||
repo="acme/repo",
|
||||
@@ -169,12 +224,17 @@ def test_run_review_ephemeral_retries_without_reasoning_effort_when_unsupported(
|
||||
get_settings.cache_clear()
|
||||
settings = get_settings()
|
||||
|
||||
monkeypatch.setattr(
|
||||
"gitea_codex_bot.workers.container_runner.prepare_review_prompt",
|
||||
lambda *_args, **_kwargs: ("prompt", {"diff": ""}, object()),
|
||||
)
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", lambda _settings: object())
|
||||
class _FakeGiteaClient:
|
||||
def __init__(self, _settings) -> None:
|
||||
pass
|
||||
|
||||
def get_pull_request(self, *_args, **_kwargs):
|
||||
return _sample_pr()
|
||||
|
||||
def get_file_content(self, *_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.GiteaClient", _FakeGiteaClient)
|
||||
calls: list[list[str]] = []
|
||||
|
||||
def _fake_run(cmd, *args, **kwargs):
|
||||
@@ -194,15 +254,18 @@ def test_run_review_ephemeral_retries_without_reasoning_effort_when_unsupported(
|
||||
(),
|
||||
{
|
||||
"returncode": 0,
|
||||
"stdout": '{"verdict":"correct","confidence":0.9,"summary":"ok","findings":[]}\n',
|
||||
"stdout": (
|
||||
'{"type":"response.started","model":"gpt-5.3-codex"}\n'
|
||||
f"{RESULT_START_MARKER}\n"
|
||||
'{"verdict":"correct","confidence":0.9,"summary":"ok","findings":[],"markdown_comment":"ok"}\n'
|
||||
f"{RESULT_END_MARKER}\n"
|
||||
),
|
||||
"stderr": "",
|
||||
},
|
||||
)()
|
||||
|
||||
monkeypatch.setattr("gitea_codex_bot.workers.container_runner.subprocess.run", _fake_run)
|
||||
|
||||
from gitea_codex_bot.types import ParsedCommand
|
||||
|
||||
result, _repo_cfg = run_review_ephemeral(
|
||||
settings,
|
||||
repo="acme/repo",
|
||||
@@ -218,33 +281,26 @@ def test_run_review_ephemeral_retries_without_reasoning_effort_when_unsupported(
|
||||
assert "--reasoning-effort" not in second_shell
|
||||
|
||||
|
||||
def test_parse_codex_exec_stdout_from_stream_item_text_json() -> None:
|
||||
stdout = '\n'.join(
|
||||
[
|
||||
'{"type":"thread.started","thread_id":"abc"}',
|
||||
'{"type":"item.completed","item":{"type":"agent_message","text":"{\\"verdict\\":\\"correct\\",\\"confidence\\":0.9,\\"summary\\":\\"ok\\",\\"findings\\":[]}"}}',
|
||||
]
|
||||
def test_parse_review_result_from_stdout_artifact() -> None:
|
||||
stdout = (
|
||||
"noise\n"
|
||||
f"{RESULT_START_MARKER}\n"
|
||||
'{"verdict":"correct","confidence":0.9,"summary":"ok","findings":[],"markdown_comment":"ok"}\n'
|
||||
f"{RESULT_END_MARKER}\n"
|
||||
)
|
||||
parsed = _parse_codex_exec_stdout(stdout)
|
||||
parsed = _parse_review_result_from_stdout_artifact(stdout)
|
||||
assert parsed["verdict"] == "correct"
|
||||
assert parsed["summary"] == "ok"
|
||||
|
||||
|
||||
def test_parse_codex_exec_stdout_from_fenced_json_text() -> None:
|
||||
stdout = '\n'.join(
|
||||
[
|
||||
'{"type":"thread.started","thread_id":"abc"}',
|
||||
'{"type":"item.completed","item":{"type":"agent_message","text":"Here is the result:\\n```json\\n{\\"verdict\\":\\"has_issues\\",\\"confidence\\":0.8,\\"summary\\":\\"x\\",\\"findings\\":[]}\\n```"}}',
|
||||
]
|
||||
)
|
||||
parsed = _parse_codex_exec_stdout(stdout)
|
||||
assert parsed["verdict"] == "has_issues"
|
||||
assert parsed["summary"] == "x"
|
||||
def test_parse_review_result_from_stdout_artifact_fails_without_markers() -> None:
|
||||
with pytest.raises(RuntimeError):
|
||||
_parse_review_result_from_stdout_artifact("no markers here")
|
||||
|
||||
|
||||
def test_extract_result_meta_from_codex_stdout_collects_model_and_usage() -> None:
|
||||
settings = get_settings()
|
||||
stdout = '\n'.join(
|
||||
stdout = "\n".join(
|
||||
[
|
||||
'{"type":"response.started","model":"gpt-5.3-codex"}',
|
||||
'{"type":"response.completed","response":{"usage":{"input_tokens":101,"output_tokens":22,"total_tokens":123}}}',
|
||||
|
||||
@@ -57,6 +57,27 @@ def test_build_prompt_includes_trigger_message() -> None:
|
||||
assert "Trigger message: @codex review security\nPlease focus auth." in prompt
|
||||
|
||||
|
||||
def test_build_prompt_uses_empty_placeholders_and_no_network_instruction() -> None:
|
||||
pr = type("PR", (), {"html_url": "https://gitea.example/pr/1"})()
|
||||
command = ParsedCommand(name="review", raw="@codex review")
|
||||
diff_context = {"truncated": False, "changed_files": [], "diff": ""}
|
||||
repo_cfg = RepoReviewConfig()
|
||||
|
||||
prompt = _build_prompt(
|
||||
pr,
|
||||
command,
|
||||
diff_context,
|
||||
repo_cfg,
|
||||
changed_file_contents="",
|
||||
test_output=None,
|
||||
)
|
||||
|
||||
assert "You do not have internet/network access. Do not try to fetch URLs." in prompt
|
||||
assert "Never claim that PR content is inaccessible or missing if these sections are present." in prompt
|
||||
assert "Changed files:\n(none)" in prompt
|
||||
assert "Unified diff:\n(empty)" in prompt
|
||||
|
||||
|
||||
def test_prepare_review_prompt_applies_repo_default_mode_when_command_mode_not_explicit(monkeypatch, tmp_path) -> None:
|
||||
repo_dir = tmp_path / "repo"
|
||||
repo_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user