diff --git a/src/agent_env/agents/agents/claude-code/claude_bridge.py b/src/agent_env/agents/agents/claude-code/claude_bridge.py index 1d030f2da..3bffd332d 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -55,6 +55,10 @@ def project_dir(workspace: Path) -> Path: ) # What agent-env gives an agent for [model.headers]: a JSON object of header names to values. MODEL_HEADERS_ENV = "LITELLM_EXTRA_HEADERS" +# Settings every CLI run takes. The CLI deletes files older than cleanupPeriodDays by its own clock, so on an env clock +# ahead of real time it deleted the transcript it resumes. It refuses 0, and --no-session-persistence keeps none, so +# this is every day a JavaScript date can be from 1970: the cutoff is never after 1970, whatever the clock says. +CLI_SETTINGS = {"cleanupPeriodDays": 100_000_000} # The CLI version ClaudeCodeTool names the tools of; a test holds it to the Dockerfile's CLAUDE_CODE_VERSION. @@ -627,7 +631,7 @@ def _command(session_id: str, resume: bool, *, model: str | None, allowed_tools: the CLI's own tools: ``{"disable": "*"}`` offers none, ``{"only": [...]}`` just those, and ``{"disable": [...]}`` the CLI's defaults but those; with any selection, its MCP servers are only ``mcp_servers``.""" cmd = ["claude", "-p", "--output-format", "stream-json", "--verbose", "--dangerously-skip-permissions", - "--setting-sources", "user"] + "--setting-sources", "user", "--settings", json.dumps(CLI_SETTINGS)] cmd += ["--resume", session_id] if resume else ["--session-id", session_id] if model: cmd += ["--model", _with_1m_window(model)] diff --git a/tst/unit/agents/claude_code/test_env_clock_image.py b/tst/unit/agents/claude_code/test_env_clock_image.py index 64de53fe4..a611472d3 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -1,7 +1,8 @@ """Claude Code's bridge on the env's clock: it hands the shared egress rule (tst/unit/agents/test_agent_egress.py) the model endpoint's URL and every MCP server's, so on an armed clock an env MCP server over TLS goes through the egress -proxy and loopback and plain-HTTP hosts go direct; unarmed, the CLI goes out as before. What every clocked image shares -is checked in tst/unit/agents/test_agent_clock_images.py.""" +proxy and loopback and plain-HTTP hosts go direct; unarmed, the CLI goes out as before. Every run keeps its transcripts, +which the CLI's own cleanup would delete on a clock ahead of the files' real times. What every clocked image shares is +checked in tst/unit/agents/test_agent_clock_images.py.""" from __future__ import annotations @@ -16,6 +17,8 @@ from tst.util.build_context import sent PROXY = "http://127.0.0.1:3128" +# How many days a JavaScript date can be from 1970 either way (ECMAScript's time value range). +JS_DATE_RANGE_DAYS = 100_000_000 ARMED_PAGE = agent_egress.clockpage.encode(2, 1_900_000_000 * 10**9, 10**18, 1.0, 1, 3, 0, 0) @@ -27,6 +30,15 @@ def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy_and_to_loopback_di assert env["NODE_USE_ENV_PROXY"] == "1" +@pytest.mark.parametrize("resume", [False, True]) +def test_every_cli_run_keeps_transcripts_whatever_date_the_clock_shows(resume): + command = claude_bridge._command("sess", resume, model=None, allowed_tools=None, mcp_servers=None, + system_prompt=None, output_format=None, role=None, effort=None, max_turns=None) + + assert command[command.index("--setting-sources") + 1] == "user" + assert json.loads(command[command.index("--settings") + 1])["cleanupPeriodDays"] >= JS_DATE_RANGE_DAYS + + def _spawned_env(monkeypatch, tmp_path, armed: bool) -> dict[str, str]: """The environment _spawn_claude starts the CLI with, for an env MCP server over TLS, one over plain HTTP and the peer-agents one."""