[feat]. Add username and custom bot mention tags #2
@@ -31,21 +31,26 @@ def run_review_ephemeral(
|
|||||||
gitea = GiteaClient(settings)
|
gitea = GiteaClient(settings)
|
||||||
prompt, _diff_context, repo_cfg = prepare_review_prompt(settings, gitea, repo, pr_number, command)
|
prompt, _diff_context, repo_cfg = prepare_review_prompt(settings, gitea, repo, pr_number, command)
|
||||||
container_name = f"codex-review-{uuid.uuid4().hex[:12]}"
|
container_name = f"codex-review-{uuid.uuid4().hex[:12]}"
|
||||||
install_and_run = _build_install_and_run_command(settings)
|
|
||||||
extra_env: dict[str, str] = {}
|
extra_env: dict[str, str] = {}
|
||||||
if settings.codex_auth_mode == "chatgpt":
|
if settings.codex_auth_mode == "chatgpt":
|
||||||
extra_env["CODEX_AUTH_JSON_B64"] = _load_codex_auth_json_b64(settings)
|
extra_env["CODEX_AUTH_JSON_B64"] = _load_codex_auth_json_b64(settings)
|
||||||
cmd = _build_docker_command(settings, container_name=container_name, install_and_run=install_and_run)
|
|
||||||
try:
|
try:
|
||||||
completed = subprocess.run(
|
completed = _run_ephemeral_container(
|
||||||
cmd,
|
settings,
|
||||||
input=prompt,
|
container_name=container_name,
|
||||||
text=True,
|
prompt=prompt,
|
||||||
check=False,
|
extra_env=extra_env,
|
||||||
capture_output=True,
|
include_reasoning_effort=True,
|
||||||
timeout=settings.max_review_minutes * 60,
|
|
||||||
env={**os.environ, **extra_env},
|
|
||||||
)
|
)
|
||||||
|
if _needs_reasoning_effort_compat_retry(completed):
|
||||||
|
logger.info("Ephemeral runner does not support --reasoning-effort; retrying without it.")
|
||||||
|
completed = _run_ephemeral_container(
|
||||||
|
settings,
|
||||||
|
container_name=container_name,
|
||||||
|
prompt=prompt,
|
||||||
|
extra_env=extra_env,
|
||||||
|
include_reasoning_effort=False,
|
||||||
|
)
|
||||||
if completed.returncode != 0:
|
if completed.returncode != 0:
|
||||||
raise RuntimeError(_format_runner_failure(completed))
|
raise RuntimeError(_format_runner_failure(completed))
|
||||||
parsed = _parse_codex_exec_stdout(completed.stdout)
|
parsed = _parse_codex_exec_stdout(completed.stdout)
|
||||||
@@ -56,7 +61,28 @@ def run_review_ephemeral(
|
|||||||
return _ephemeral_runner_failure_result(exc, settings.codex_auth_mode), repo_cfg
|
return _ephemeral_runner_failure_result(exc, settings.codex_auth_mode), repo_cfg
|
||||||
|
|
||||||
|
|
||||||
def _build_install_and_run_command(settings: Settings) -> str:
|
def _run_ephemeral_container(
|
||||||
|
settings: Settings,
|
||||||
|
*,
|
||||||
|
container_name: str,
|
||||||
|
prompt: str,
|
||||||
|
extra_env: dict[str, str],
|
||||||
|
include_reasoning_effort: bool,
|
||||||
|
) -> subprocess.CompletedProcess[str]:
|
||||||
|
install_and_run = _build_install_and_run_command(settings, include_reasoning_effort=include_reasoning_effort)
|
||||||
|
cmd = _build_docker_command(settings, container_name=container_name, install_and_run=install_and_run)
|
||||||
|
return subprocess.run(
|
||||||
|
cmd,
|
||||||
|
input=prompt,
|
||||||
|
text=True,
|
||||||
|
check=False,
|
||||||
|
capture_output=True,
|
||||||
|
timeout=settings.max_review_minutes * 60,
|
||||||
|
env={**os.environ, **extra_env},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _build_install_and_run_command(settings: Settings, *, include_reasoning_effort: bool = True) -> str:
|
||||||
steps = ["set -euo pipefail"]
|
steps = ["set -euo pipefail"]
|
||||||
if settings.codex_auth_mode == "chatgpt":
|
if settings.codex_auth_mode == "chatgpt":
|
||||||
steps.extend(
|
steps.extend(
|
||||||
@@ -77,12 +103,19 @@ def _build_install_and_run_command(settings: Settings) -> str:
|
|||||||
codex_exec_parts = ["codex exec --skip-git-repo-check --json"]
|
codex_exec_parts = ["codex exec --skip-git-repo-check --json"]
|
||||||
if model:
|
if model:
|
||||||
codex_exec_parts.append(f"-m {shlex.quote(model)}")
|
codex_exec_parts.append(f"-m {shlex.quote(model)}")
|
||||||
if reasoning_effort:
|
if include_reasoning_effort and reasoning_effort:
|
||||||
codex_exec_parts.append(f"--reasoning-effort {shlex.quote(reasoning_effort)}")
|
codex_exec_parts.append(f"--reasoning-effort {shlex.quote(reasoning_effort)}")
|
||||||
steps.append(" ".join(codex_exec_parts))
|
steps.append(" ".join(codex_exec_parts))
|
||||||
return "; ".join(steps)
|
return "; ".join(steps)
|
||||||
|
|
||||||
|
|
||||||
|
def _needs_reasoning_effort_compat_retry(completed: subprocess.CompletedProcess[str]) -> bool:
|
||||||
|
if completed.returncode == 0:
|
||||||
|
return False
|
||||||
|
stderr_text = completed.stderr or ""
|
||||||
|
return "unexpected argument '--reasoning-effort' found" in stderr_text
|
||||||
|
|
||||||
|
|
||||||
def _build_docker_command(settings: Settings, *, container_name: str, install_and_run: str) -> list[str]:
|
def _build_docker_command(settings: Settings, *, container_name: str, install_and_run: str) -> list[str]:
|
||||||
cmd = [
|
cmd = [
|
||||||
"docker",
|
"docker",
|
||||||
|
|||||||
@@ -72,6 +72,14 @@ def test_build_install_command_includes_configured_reasoning_effort(monkeypatch:
|
|||||||
assert "--reasoning-effort medium" in command
|
assert "--reasoning-effort medium" in command
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_install_command_can_disable_reasoning_effort_flag() -> None:
|
||||||
|
settings = get_settings()
|
||||||
|
|
||||||
|
command = _build_install_and_run_command(settings, include_reasoning_effort=False)
|
||||||
|
|
||||||
|
assert "--reasoning-effort" not in command
|
||||||
|
|
||||||
|
|
||||||
def test_chatgpt_mode_requires_existing_auth_json(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
def test_chatgpt_mode_requires_existing_auth_json(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
||||||
missing = tmp_path / "missing-auth.json"
|
missing = tmp_path / "missing-auth.json"
|
||||||
monkeypatch.setenv("CODEX_AUTH_MODE", "chatgpt")
|
monkeypatch.setenv("CODEX_AUTH_MODE", "chatgpt")
|
||||||
@@ -157,6 +165,59 @@ def test_run_review_ephemeral_api_key_mode_does_not_fallback_to_host(monkeypatch
|
|||||||
assert "API-key auth runner failed" in result["summary"]
|
assert "API-key auth runner failed" in result["summary"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_run_review_ephemeral_retries_without_reasoning_effort_when_unsupported(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
|
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())
|
||||||
|
|
||||||
|
calls: list[list[str]] = []
|
||||||
|
|
||||||
|
def _fake_run(cmd, *args, **kwargs):
|
||||||
|
calls.append(cmd)
|
||||||
|
if len(calls) == 1:
|
||||||
|
return type(
|
||||||
|
"Completed",
|
||||||
|
(),
|
||||||
|
{
|
||||||
|
"returncode": 2,
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": "error: unexpected argument '--reasoning-effort' found",
|
||||||
|
},
|
||||||
|
)()
|
||||||
|
return type(
|
||||||
|
"Completed",
|
||||||
|
(),
|
||||||
|
{
|
||||||
|
"returncode": 0,
|
||||||
|
"stdout": '{"verdict":"correct","confidence":0.9,"summary":"ok","findings":[]}\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",
|
||||||
|
pr_number=1,
|
||||||
|
command=ParsedCommand(name="review", raw="@codex review"),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["verdict"] == "correct"
|
||||||
|
assert len(calls) == 2
|
||||||
|
first_shell = calls[0][-1]
|
||||||
|
second_shell = calls[1][-1]
|
||||||
|
assert "--reasoning-effort" in first_shell
|
||||||
|
assert "--reasoning-effort" not in second_shell
|
||||||
|
|
||||||
|
|
||||||
def test_parse_codex_exec_stdout_from_stream_item_text_json() -> None:
|
def test_parse_codex_exec_stdout_from_stream_item_text_json() -> None:
|
||||||
stdout = '\n'.join(
|
stdout = '\n'.join(
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user