Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(providers): make codex_cli subprocess concurrency configurable (#2334)
Nehal kanak kuniyil thottathil052e1305325d
diff --git a/docs/agent/CODEX.md b/docs/agent/CODEX.mdindex 117410b0f..1c443ec80 100644--- a/docs/agent/CODEX.md+++ b/docs/agent/CODEX.md@@ -85,6 +85,14 @@ codex exec --ephemeral --sandbox read-only --json --cd /absolute/path/to/repo -Repowise sends the prompt on stdin, parses Codex JSONL output, records token usage from `turn.completed.usage`, and treats `codex_cli/*` cost as `$0.00` because subscription billing happens outside Repowise API pricing. `--model` is passed to Codex only when you explicitly configure a model. `--reasoning minimal` maps to Codex `model_reasoning_effort="minimal"` when the selected model advertises a `minimal` level, and falls back to `"low"` when it does not; `low`, `medium`, `high`, and `xhigh` pass through when the model advertises those levels. `off`/`none` maps to `model_reasoning_effort="none"`. `auto` sends no effort at all and lets Codex pick.+Each page is a full `codex exec` process, and subscription limits are per account, so the provider defaults to **4** concurrent subprocesses. Override with:++```bash+REPOWISE_CODEX_CLI_CONCURRENCY=2 repowise generate --unwritten+```++That variable raises as well as lowers the limit, so 4 is a default rather than an enforced ceiling. Higher is reasonable on a plan with a larger allowance, and is also the fastest way to trip the account limit mid-run.+Smoke check:```bashdiff --git a/packages/core/src/repowise/core/providers/llm/codex_cli.py b/packages/core/src/repowise/core/providers/llm/codex_cli.pyindex 531f4b2bc..89ab3850e 100644--- a/packages/core/src/repowise/core/providers/llm/codex_cli.py+++ b/packages/core/src/repowise/core/providers/llm/codex_cli.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport asyncioimport contextlibimport json+import osimport shutilimport subprocessfrom dataclasses import dataclass@@ -35,6 +36,18 @@ _DEFAULT_MODEL_LABEL = "codex_cli/default"_EXEC_TIMEOUT_SECONDS = 600_CATALOG_TIMEOUT_SECONDS = 5+# Subscription seats are rate limited per account and each call is a full CLI+# process. Serializing turns a 104-page generate into about 95 minutes; too+# much concurrency trips the account limit and fails the run. 4 matches the+# ceiling init applies to the other CLI-backed providers.+#+# The env override is a true override, not a clamp: it can raise the fan-out+# above 4 as well as lower it. Deliberate -- a higher-tier plan can take more+# than a lower one, and only the operator knows which they have -- but it does+# mean 4 is a default rather than an enforced cap.+_DEFAULT_CONCURRENCY = 4+_CONCURRENCY_ENV = "REPOWISE_CODEX_CLI_CONCURRENCY"+async def _close_subprocess_transport(proc: asyncio.subprocess.Process) -> None:"""Close asyncio's subprocess transport before the event loop shuts down."""@@ -81,6 +94,18 @@ def _model_label(model: str | None) -> str:return f"codex_cli/{native}" if native else _DEFAULT_MODEL_LABEL+def _resolve_concurrency() -> int:+ raw = os.environ.get(_CONCURRENCY_ENV, "").strip()+ if not raw:+ return _DEFAULT_CONCURRENCY+ try:+ value = int(raw)+ except ValueError:+ log.warning("codex_cli.concurrency.invalid", value=raw, using=_DEFAULT_CONCURRENCY)+ return _DEFAULT_CONCURRENCY+ return max(1, value)++def _extract_codex_model_catalog(raw: object) -> dict[str, CodexModelReasoning]:if not isinstance(raw, dict):return {}@@ -347,8 +372,8 @@ class CodexCliProvider(BaseProvider):chooses the model. Persisted labels like ``codex_cli/gpt-5.5`` areaccepted and normalized before calling the CLI.repo_path: Working directory passed to ``codex exec --cd``.- rate_limiter: Accepted for interface consistency, but the provider- serializes subprocess calls by default.+ rate_limiter: Accepted for interface consistency; the provider also+ bounds its own subprocess fan-out."""# A process spawn plus a full Codex agent turn. The floor is tens of@@ -396,7 +421,7 @@ class CodexCliProvider(BaseProvider):def _get_semaphore(self) -> asyncio.Semaphore:loop = asyncio.get_running_loop()if self._semaphore_loop is not loop:- self._subprocess_semaphore = asyncio.Semaphore(1)+ self._subprocess_semaphore = asyncio.Semaphore(_resolve_concurrency())self._semaphore_loop = loopreturn self._subprocess_semaphore # type: ignore[return-value]diff --git a/tests/unit/test_providers/test_codex_cli_provider.py b/tests/unit/test_providers/test_codex_cli_provider.pyindex 68fce1594..a71cc765f 100644--- a/tests/unit/test_providers/test_codex_cli_provider.py+++ b/tests/unit/test_providers/test_codex_cli_provider.py@@ -16,6 +16,7 @@ from repowise.core.providers.llm.codex_cli import (CodexCliProvider,CodexModelReasoning,_extract_codex_model_catalog,+ _resolve_concurrency,)@@ -410,8 +411,10 @@ async def test_generate_raises_when_jsonl_has_no_agent_message(monkeypatch, tmp_await CodexCliProvider(repo_path=tmp_path).generate("sys", "user")-async def test_generate_serializes_subprocess_calls(monkeypatch, tmp_path):+async def test_generate_respects_configured_concurrency(monkeypatch, tmp_path):+ """The semaphore is the fan-out knob: 2 in flight, never more."""monkeypatch.setattr("shutil.which", lambda cmd: "codex" if cmd == "codex" else None)+ monkeypatch.setenv("REPOWISE_CODEX_CLI_CONCURRENCY", "2")active = 0max_active = 0@@ -431,9 +434,56 @@ async def test_generate_serializes_subprocess_calls(monkeypatch, tmp_path):await asyncio.gather(provider.generate("sys", "user 1"),provider.generate("sys", "user 2"),+ provider.generate("sys", "user 3"),)- assert max_active == 1+ assert max_active == 2+++async def test_generate_fans_out_to_the_default_concurrency(monkeypatch, tmp_path):+ """Serializing made a 104-page generate take about 95 minutes."""+ monkeypatch.setattr("shutil.which", lambda cmd: "codex" if cmd == "codex" else None)+ monkeypatch.delenv("REPOWISE_CODEX_CLI_CONCURRENCY", raising=False)+ active = 0+ max_active = 0++ async def on_communicate() -> None:+ nonlocal active, max_active+ active += 1+ max_active = max(max_active, active)+ await asyncio.sleep(0.01)+ active -= 1++ async def fake_exec(*_args: str, **_kwargs: Any) -> FakeProcess:+ return FakeProcess(stdout=_success_jsonl("OK"), on_communicate=on_communicate)++ monkeypatch.setattr("asyncio.create_subprocess_exec", fake_exec)+ provider = CodexCliProvider(repo_path=tmp_path)++ await asyncio.gather(*[provider.generate("sys", f"user {i}") for i in range(4)])++ assert max_active == 4+++def test_concurrency_env_overrides_the_default(monkeypatch):+ monkeypatch.setenv("REPOWISE_CODEX_CLI_CONCURRENCY", "6")+ assert _resolve_concurrency() == 6+++def test_concurrency_default_is_four(monkeypatch):+ monkeypatch.delenv("REPOWISE_CODEX_CLI_CONCURRENCY", raising=False)+ assert _resolve_concurrency() == 4+++def test_concurrency_rejects_invalid_and_clamps_to_one(monkeypatch):+ monkeypatch.setenv("REPOWISE_CODEX_CLI_CONCURRENCY", "not-a-number")+ assert _resolve_concurrency() == 4++ monkeypatch.setenv("REPOWISE_CODEX_CLI_CONCURRENCY", "0")+ assert _resolve_concurrency() == 1++ monkeypatch.setenv("REPOWISE_CODEX_CLI_CONCURRENCY", " 2 ")+ assert _resolve_concurrency() == 2async def test_generate_times_out_and_kills_codex_exec(monkeypatch, tmp_path):diff --git a/website/codex.md b/website/codex.mdindex 67be3218e..26741ca5e 100644--- a/website/codex.md+++ b/website/codex.md@@ -84,6 +84,12 @@ codex exec --ephemeral --sandbox read-only --json --cd <repo> -Repowise records token usage from Codex JSONL and treats `codex_cli/*` cost as `$0.00`. `--model` is passed to Codex only when explicitly configured.+Concurrency defaults to 4 processes. The variable below overrides it in either direction -- lower it if you are hitting limits, and note that raising it past 4 is allowed but is the quickest way to hit them:++```bash+REPOWISE_CODEX_CLI_CONCURRENCY=2 repowise generate --unwritten+```+## Plugin and skillsThe Repowise repository includes a local Codex plugin at `plugins/codex` and a marketplace entry at `.agents/plugins/marketplace.json`.