Official Codebase of 3DCodeVerse
Python
agents: one clean_env builds a vendor CLI's scrubbed environment — hardened_env and eval's one-shot _clean_env had the same comprehension, and is_secret_env round-tripped each variable through scrub_secrets
gaoypeng0e832802d239
diff --git a/eval/bench/_oneshot.py b/eval/bench/_oneshot.pyindex 5c397727..c335469d 100644--- a/eval/bench/_oneshot.py+++ b/eval/bench/_oneshot.py@@ -20,13 +20,13 @@ Backends (``get_oneshot_backend``):Every call runs in a scratch directory *outside* the repository so the CLIscannot pick up CLAUDE.md / AGENTS.md context, and with secrets stripped from the-environment (``hardened_env``-style), keeping only the CLI's own auth variable.+environment (``cli_common.clean_env``: no git ceiling — it runs outside any repo),+keeping only the CLI's own auth variable."""from __future__ import annotationsimport json-import osimport reimport tempfileimport time@@ -43,7 +43,7 @@ from codeverse3d.agents.backends import (split_model_effort,usage_from_envelope,)-from codeverse3d.agents.cli_common import is_secret_env, run_with_watchdog, tail+from codeverse3d.agents.cli_common import clean_env, run_with_watchdog, tailfrom codeverse3d.config import get_settingsfrom codeverse3d.contracts.artifacts import BuildResult, GateReportfrom codeverse3d.contracts.chat import ChatMessage, ChatRequest@@ -332,13 +332,6 @@ def _scratch_cwd(label: str) -> Path:return Path(tempfile.mkdtemp(prefix=f"c3d_oneshot_{label}_"))-def _clean_env(keep: set[str]) -> dict[str, str]:- env = {k: v for k, v in os.environ.items() if k in keep or not is_secret_env(k)}- env["C3D_AGENT_CONTEXT"] = "1"- env["PYTHONUNBUFFERED"] = "1"- return env--def _dump(out_dir: Path, prompt: str, argv: list[str], stdout: str, stderr: str) -> None:out_dir.mkdir(parents=True, exist_ok=True)(out_dir / "prompt.md").write_text(prompt)@@ -368,7 +361,7 @@ class ClaudeOneShot:def generate(self, prompt: str, *, out_dir: Path, timeout_s: float = DEFAULT_TIMEOUT_S, label: str = "oneshot") -> OneShotResult:cwd = _scratch_cwd("claude")argv = self.argv(prompt)- proc = run_with_watchdog(argv, cwd=cwd, env=_clean_env({"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"}),+ proc = run_with_watchdog(argv, cwd=cwd, env=clean_env({"ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"}),soft_timeout_s=timeout_s, idle_grace_s=IDLE_GRACE_S, activity_dirs=[cwd])_dump(out_dir, prompt, argv, proc.stdout, proc.stderr)env = parse_claude_json(proc.stdout)@@ -426,7 +419,7 @@ class CodexOneShot:out_dir.mkdir(parents=True, exist_ok=True)last_msg = out_dir / "last_message.md"argv = self.argv(cwd, last_msg)- proc = run_with_watchdog(argv, cwd=cwd, env=_clean_env({"OPENAI_API_KEY", "CODEX_API_KEY"}), soft_timeout_s=timeout_s,+ proc = run_with_watchdog(argv, cwd=cwd, env=clean_env({"OPENAI_API_KEY", "CODEX_API_KEY"}), soft_timeout_s=timeout_s,idle_grace_s=IDLE_GRACE_S, stdin=prompt, activity_dirs=[cwd])_dump(out_dir, prompt, argv, proc.stdout, proc.stderr)events = parse_codex_jsonl(proc.stdout)diff --git a/harness/codeverse3d/agents/cli_common.py b/harness/codeverse3d/agents/cli_common.pyindex 1898b295..490a7a4a 100644--- a/harness/codeverse3d/agents/cli_common.py+++ b/harness/codeverse3d/agents/cli_common.py@@ -248,24 +248,19 @@ def _reject_control_chars(cmd: Sequence[str], cwd: object) -> None:log = logging.getLogger(__name__)-def is_secret_env(name: str) -> bool:- """True for env vars that look like credentials (stripped from agent children).-- One owner for the patterns: :mod:`codeverse3d.proc` (a stdlib-only leaf every layer- may import), which also scrubs the generated-code subprocesses."""- return name not in scrub_secrets({name: ""})+def clean_env(keep: set[str] | frozenset[str] = frozenset()) -> dict[str, str]:+ """``os.environ`` minus secrets (the one owner of the patterns: ``proc.scrub_secrets``) but+ for the ``keep`` names (a CLI's own auth var), plus the recursion guard — a vendor CLI's env."""+ env = scrub_secrets(dict(os.environ)) | {k: v for k, v in os.environ.items() if k in keep}+ env["C3D_AGENT_CONTEXT"] = "1"+ env["PYTHONUNBUFFERED"] = "1"+ return envdef hardened_env(ws: Workspace, job: AgentJob, *, keep: set[str] | None = None) -> dict[str, str]:- """Copy ``os.environ`` minus secrets, plus recursion guard + git ceiling + ``job.env``.-- ``keep`` names secrets that must survive (e.g. the CLI's own auth var).- """- keep = keep or set()- env = {k: v for k, v in os.environ.items() if k in keep or not is_secret_env(k)}- env["C3D_AGENT_CONTEXT"] = "1"+ """:func:`clean_env` plus the git ceiling and ``job.env``."""+ env = clean_env(keep or set())env["GIT_CEILING_DIRECTORIES"] = str(ws.root.parent)- env["PYTHONUNBUFFERED"] = "1"env.update(job.env)return envdiff --git a/harness/codeverse3d/proc.py b/harness/codeverse3d/proc.pyindex 66d91b2f..b205948b 100644--- a/harness/codeverse3d/proc.py+++ b/harness/codeverse3d/proc.py@@ -346,8 +346,8 @@ class ManagedProcess:# ------------------------------------------------------------------ env scrubbing-#: The ONE owner of the credential patterns: ``agents/cli_common.is_secret_env`` delegates-#: here (tests/core/test_proc.py pins the two together).+#: The ONE owner of the credential patterns: ``agents/cli_common.clean_env`` (the vendor CLIs'+#: env) calls :func:`scrub_secrets` too._SECRET_EXACT: frozenset[str] = frozenset({"GEMINI_API_KEYS", "GEMINI_API_KEY", "GOOGLE_API_KEY", "ANTHROPIC_API_KEY", "OPENAI_API_KEY","OPENAI_ORG_ID", "HF_TOKEN", "HUGGINGFACE_TOKEN", "GITHUB_TOKEN", "GH_TOKEN",diff --git a/harness/tests/agents/test_cli_common.py b/harness/tests/agents/test_cli_common.pyindex b72f1874..60c15fd8 100644--- a/harness/tests/agents/test_cli_common.py+++ b/harness/tests/agents/test_cli_common.py@@ -9,12 +9,12 @@ from codeverse3d.agents.cli_common import (Trajectory,attribute_changes,begin_session,+ clean_env,finish_session,hardened_env,invoke,is_quota_failure,is_rate_limited,- is_secret_env,is_transient_failure,provider_wait,)@@ -23,9 +23,14 @@ from codeverse3d.contracts.common import Usagefrom codeverse3d.workspace import Workspace-def test_secret_detection():- assert is_secret_env("OPENAI_API_KEY") and is_secret_env("FOO_TOKEN") and is_secret_env("GEMINI_API_KEYS")- assert not is_secret_env("PATH") and not is_secret_env("HOME") and not is_secret_env("NODE_OPTIONS")+def test_clean_env_drops_secrets_but_the_kept_ones(monkeypatch):+ for name in ("OPENAI_API_KEY", "FOO_TOKEN", "GEMINI_API_KEYS", "CODEX_API_KEY"):+ monkeypatch.setenv(name, "s")+ monkeypatch.setenv("NODE_OPTIONS", "--x")+ env = clean_env({"CODEX_API_KEY"})+ assert not {"OPENAI_API_KEY", "FOO_TOKEN", "GEMINI_API_KEYS"} & set(env)+ assert env["CODEX_API_KEY"] == "s" and env["NODE_OPTIONS"] == "--x" and "PATH" in env and "HOME" in env+ assert env["C3D_AGENT_CONTEXT"] == "1" and "GIT_CEILING_DIRECTORIES" not in envdef test_hardened_env_strips_secrets_and_adds_guards(tmp_ws: Workspace, monkeypatch):