From ae1d4be4a99c1042044999bf1f9a00e5fc2d39ca Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 11:29:28 -0800 Subject: [PATCH 01/29] feat(agents): agents follow the env's clock the way computers do (FD-5916) Prototype of the Agents on the Env Clock design, to size the change. The gateway takes followers declared after it starts; every AgentEnvAgent in an image that runs the clock follower serves clock/v1 sync_time; sync_env_clock and deploy_agent declare each agent as a follower and hand it its config; the Claude Code image runs the computer base's follower, egress proxy and libfaketime, copied in until FD-5700; get_time no longer calls the machine's clock unsynced. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agentenv_protocol/a2a_agent/__init__.py | 2 + .../src/agentenv_protocol/a2a_agent/clock.py | 74 + .../agentenv_protocol/a2a_agent/extensions.py | 32 + .../agentenv_protocol/a2a_agent/framework.py | 21 + .../src/agentenv_protocol/computer/clock.py | 16 +- .../tests/test_a2a_agent_clock.py | 115 ++ src/agent_env/agents/README.md | 13 + .../agents/agents/claude-code/Dockerfile | 44 +- .../agents/claude-code/claude_bridge.py | 29 +- .../agents/agents/claude-code/clock/COPYING | 339 ++++ .../claude-code/clock/agentenv_clock_page.c | 320 ++++ .../claude-code/clock/agentenv_clock_waits.c | 1175 +++++++++++++ .../agents/agents/claude-code/clock/build.sh | 22 + .../agents/claude-code/clock/clock-follower | 204 +++ .../agents/claude-code/clock/clock.conf | 11 + .../agents/claude-code/clock/clockpage.py | 157 ++ .../claude-code/clock/libfaketime.patch | 1527 +++++++++++++++++ .../agents/claude-code/clock/tls_realtime.c | 115 ++ .../agents/agents/claude-code/egress/boot.go | 294 ++++ .../agents/claude-code/egress/boot_test.go | 99 ++ .../agents/agents/claude-code/egress/clock.go | 151 ++ .../agents/claude-code/egress/clock_test.go | 112 ++ .../agents/agents/claude-code/egress/go.mod | 12 + .../agents/agents/claude-code/egress/go.sum | 10 + .../agents/agents/claude-code/egress/hello.go | 164 ++ .../agents/claude-code/egress/hello_test.go | 82 + .../agents/claude-code/egress/leaves.go | 188 ++ .../agents/agents/claude-code/egress/main.go | 92 + .../agents/agents/claude-code/egress/proxy.go | 598 +++++++ .../agents/claude-code/egress/proxy_test.go | 564 ++++++ .../egress/testdata/page-armed.hex | 1 + .../egress/testdata/page-unarmed.hex | 1 + .../agents/claude-code/egress/upstream.go | 268 +++ .../agents/agents/claude-code/root.conf | 62 + .../agents/agents/claude-code/start-agent | 19 + .../artifacts/ubuntu-24.04/egress/boot.go | 11 +- .../ubuntu-24.04/egress/boot_test.go | 11 + src/agent_env/env/gateway/clock_followers.py | 12 +- src/agent_env/env/gateway/constants.py | 5 +- src/agent_env/env/gateway/gateway.py | 15 + src/agent_env/env/gateway/get_time.py | 5 +- .../task_step/task_steps/agent_clock.py | 110 ++ .../task_step/task_steps/deploy_agent.py | 3 + .../task_step/task_steps/sync_env_clock.py | 29 +- .../claude_code/test_env_clock_image.py | 66 + .../agents/claude_code/test_image_contract.py | 5 +- tst/unit/env/gateway/clock_watch_test.py | 58 +- tst/unit/env/gateway/control_token_test.py | 1 + tst/unit/task_step/agent_clock_test.py | 154 ++ 49 files changed, 7395 insertions(+), 23 deletions(-) create mode 100644 packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py create mode 100644 packages/agentenv-protocol/tests/test_a2a_agent_clock.py create mode 100644 src/agent_env/agents/agents/claude-code/clock/COPYING create mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c create mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c create mode 100755 src/agent_env/agents/agents/claude-code/clock/build.sh create mode 100755 src/agent_env/agents/agents/claude-code/clock/clock-follower create mode 100644 src/agent_env/agents/agents/claude-code/clock/clock.conf create mode 100644 src/agent_env/agents/agents/claude-code/clock/clockpage.py create mode 100644 src/agent_env/agents/agents/claude-code/clock/libfaketime.patch create mode 100644 src/agent_env/agents/agents/claude-code/clock/tls_realtime.c create mode 100644 src/agent_env/agents/agents/claude-code/egress/boot.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/boot_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/clock.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/clock_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/go.mod create mode 100644 src/agent_env/agents/agents/claude-code/egress/go.sum create mode 100644 src/agent_env/agents/agents/claude-code/egress/hello.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/hello_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/leaves.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/main.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex create mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex create mode 100644 src/agent_env/agents/agents/claude-code/egress/upstream.go create mode 100644 src/agent_env/agents/agents/claude-code/root.conf create mode 100755 src/agent_env/agents/agents/claude-code/start-agent create mode 100644 src/agent_env/task_step/task_steps/agent_clock.py create mode 100644 tst/unit/agents/claude_code/test_env_clock_image.py create mode 100644 tst/unit/task_step/agent_clock_test.py diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/__init__.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/__init__.py index 5f17fed1e..174561232 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/__init__.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/__init__.py @@ -15,6 +15,7 @@ STANDARD_EXTENSIONS, TRAJECTORY_V1, TRIGGERS_V1, + CLOCK_V1, AttributionProbeResponse, BundleSkillRequest, CdpAddRequest, @@ -152,6 +153,7 @@ "STANDARD_EXTENSIONS", "TRAJECTORY_V1", "TRIGGERS_V1", + "CLOCK_V1", "A2AAgentApplication", "AgentCapabilities", "AgentConfig", diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py new file mode 100644 index 000000000..938f4f16c --- /dev/null +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py @@ -0,0 +1,74 @@ +"""An agent's side of clock/v1. An agent image that follows the env's clock runs the computer base's follower, and +agent-env's libfaketime is preloaded for the agent's user, so every program the A2A server starts reads universe time +from the follower's page; the server itself starts with the real-time marker and stays on real time. + +The framework serves clock/v1's ``sync_time``, the method every server and computer that follows the clock serves, on +the card of an agent in such an image (``follows``), so the card is what tells core the agent follows. Unlike a +computer's, whose config core writes through the sandbox's root channel, an agent's request carries the follower's +config itself (``watch_url``, ``name``, ``read_token``), and this server, running as the agent's user, writes it: the +model's own commands could rewrite it and fool their own view of the time, though never the env's clock, and the token +only reads. It answers once the follower shows the gateway's current version, or says why it doesn't.""" +from __future__ import annotations + +import json +import os +import time +import uuid +from pathlib import Path +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError + +from ..computer import clock as follower +from ..computer.errors import InvalidArgument, TargetUnavailable + + +def follows() -> bool: + """Whether this agent's image runs the clock follower.""" + return follower.follows() + + +def write_config(watch_url: str, name: str, read_token: str) -> None: + """The follower's config, written beside its place and renamed into it, so the follower, which reads it every + quarter second, never reads half of one; readable by this user and the follower's group alone.""" + path = Path(follower.CONFIG) + staged = path.with_name(f"{path.name}.{uuid.uuid4().hex}") + fd = os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640) + try: + with os.fdopen(fd, "w") as f: + json.dump({"watch_url": watch_url, "name": name, "token": read_token}, f) + os.chmod(staged, 0o640) + os.replace(staged, path) + except BaseException: + staged.unlink(missing_ok=True) + raise + + +def set_zone(timezone: str) -> str: + """The task's zone for every program this server starts from now on: ``TZ`` in its environment, which a started + program inherits; this server's own records keep UTC timestamps either way.""" + zone = timezone.strip() + try: + ZoneInfo(zone) + except (ZoneInfoNotFoundError, ValueError) as e: + raise InvalidArgument(f"timezone must be an IANA name such as 'America/Chicago', got {timezone!r}") from e + os.environ["TZ"] = zone + time.tzset() + return zone + + +async def sync_time(env_get_time_url: str, timezone: str | None = None, watch_url: str | None = None, + name: str | None = None, read_token: str | None = None) -> dict: + """``clock/v1``'s ``sync_time`` on an agent: the follower configured to follow the gateway at ``watch_url`` as + ``name`` with ``read_token``, the task's ``timezone`` set for what this server starts, and an answer once the + follower shows the gateway's version. ``env_get_time_url`` is taken as every server takes it and not used: the + follower reads the gateway's watch, not its time.""" + if not follows(): + raise TargetUnavailable("this agent's image doesn't run the clock follower") + if not (watch_url and name and read_token): + raise InvalidArgument("an agent follows the clock with its watch_url, name and read_token, which " + "sync_env_clock sends; this request names " + + (", ".join(k for k, v in (("watch_url", watch_url), ("name", name), + ("read_token", read_token)) if v) or "none of them")) + zone = set_zone(timezone) if timezone is not None else os.environ.get("TZ") or "UTC" + write_config(watch_url, name, read_token) + shown = await follower.following("this agent's clock follower") + return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, "follower": shown} diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/extensions.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/extensions.py index d48ff283b..e6200211d 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/extensions.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/extensions.py @@ -134,6 +134,18 @@ class BundleSkillRequest(ExtensionRequest): skill_bundle: SkillBundle +class ClockSyncTimeRequest(ExtensionRequest): + """clock/v1's ``sync_time`` on an agent: what every server's takes, ``env_get_time_url`` and ``timezone``, and the + config of the follower an agent runs itself, the gateway's watch URL as its sandbox reaches it, the name the + gateway knows it by and its read token.""" + + env_get_time_url: str + timezone: str | None = None + watch_url: str | None = None + name: str | None = None + read_token: str | None = None + + class SkillAddResponse(ExtensionResponse): name: str @@ -1345,6 +1357,25 @@ def _configure_trajectory(*, live: bool = False) -> ExtensionConfiguration: }, ) +CLOCK_V1 = ExtensionDefinition( + uri="urn:agentenv:clock/v1", + description=( + "Follow the env's clock: the agent's image runs the clock follower, so every program the agent runs reads " + "the env's time. Served where the image has the follower." + ), + endpoint="/ext/clock", + core_operations={ + "sync_time": _op( + "sync_time", + "POST", + "/ext/clock/sync-time", + SDK, + request_model=ClockSyncTimeRequest, + response_required=("synced", "timezone", "follower"), + ), + }, +) + INSTALL_V1 = ExtensionDefinition( uri="urn:agentenv:install/v1", description="Declare commands for installing the agent into an existing target.", @@ -1392,6 +1423,7 @@ def _configure_trajectory(*, live: bool = False) -> ExtensionConfiguration: PEER_AGENTS_V1, ATTRIBUTION_PROBE_V1, TRIGGERS_V1, + CLOCK_V1, INSTALL_V1, PROXY_V1, ) diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py index ce344d08a..648c47872 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import dataclasses import inspect import json import logging @@ -39,6 +40,8 @@ SKILL_CONFIG_V1, TRAJECTORY_V1, TRIGGERS_V1, + CLOCK_V1, + ClockSyncTimeRequest, ContextEventsTrajectoryRequest, ContextObjectTrajectoryRequest, ContextTrajectoryRequest, @@ -59,6 +62,8 @@ TriggerRegisterRequest, enable, ) +from ..computer.errors import ComputerError +from . import clock as agent_clock from .peer_agents import PEER_MCP_PATH, PEER_MCP_SERVER_NAME, PeerAgents from .registry import RegisteredOperation, build_registry from .staging import STAGING_ENDPOINT, StagingStore, staging_routes @@ -631,6 +636,7 @@ def handlers(self) -> dict[tuple[str, str], Callable]: (TRIGGERS_V1.uri, "register"): self.triggers_register, (TRIGGERS_V1.uri, "decide"): self.triggers_decide, (TRIGGERS_V1.uri, "state"): self.triggers_state, + (CLOCK_V1.uri, "sync_time"): self.clock_sync_time, } async def agent_config_set(self, request: AgentConfig) -> dict[str, Any]: @@ -876,6 +882,20 @@ async def triggers_decide(self, request: TriggerDecideRequest) -> dict[str, Any] async def triggers_state(self) -> dict[str, Any]: return self.trigger_engine.state() + async def clock_sync_time(self, request: ClockSyncTimeRequest) -> dict[str, Any]: + try: + return await agent_clock.sync_time(**request.model_dump()) + except ComputerError as exc: + raise HTTPException(status_code=exc.STATUS, detail=f"{exc.CODE}: {exc}") from exc + + +def _with_clock(definition: _AgentDefinition) -> _AgentDefinition: + """``definition`` serving clock/v1 where the agent's image runs the clock follower, so every agent in such an + image follows the env's clock with no code of its own; unchanged elsewhere, or where the agent declares it.""" + if not agent_clock.follows() or any(a.definition.uri == CLOCK_V1.uri for a in definition.extensions): + return definition + return dataclasses.replace(definition, extensions=(*definition.extensions, ExtensionActivation(CLOCK_V1))) + class DefaultExtensionHandlers: """Stable access to default implementations of SDK-owned operations.""" @@ -968,6 +988,7 @@ def __init__(self, agent: Any, definition: _AgentDefinition | None = None) -> No "agent instance is already bound to an application; create a new " "agent instance for each application" ) + definition = _with_clock(definition) self.agent = agent self.definition = definition self.rpc_path = _rpc_path(definition.identity.url) diff --git a/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py b/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py index 19667b72d..d8f19c92d 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py @@ -82,7 +82,15 @@ async def sync_time(env_get_time_url: str, timezone: Optional[str] = None) -> di if timezone is not None and timezone.strip() != zone: raise InvalidArgument(f"the task's clock is in {timezone.strip()!r} and this computer's session in {zone!r}; " f"deploy it with computer_settings.timezone = {timezone.strip()!r}") - deadline = time.monotonic() + CONNECT_WAIT_S + follower = await following("this computer's clock follower") + return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, "follower": follower} + + +async def following(whose: str, wait_s: float = CONNECT_WAIT_S) -> dict: + """The follower's status once it shows the gateway's current version; TargetUnavailable saying why ``whose`` + doesn't within ``wait_s``. A computer's ``sync_time`` and an agent's (``agentenv_protocol.a2a_agent.clock``) both answer + with it.""" + deadline = time.monotonic() + wait_s while True: status, current, unread = follower_status(), None, None if status.get("connected") and status.get("url"): @@ -92,9 +100,7 @@ async def sync_time(env_get_time_url: str, timezone: Optional[str] = None) -> di unread = f"{type(e).__name__}: {e}"[:300] status = follower_status() if current is not None and status.get("connected") and status.get("version") == current: - return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, - "follower": {key: status.get(key) for key in ("name", "url", "version", "armed")}} + return {key: status.get(key) for key in ("name", "url", "version", "armed")} if time.monotonic() >= deadline: - raise TargetUnavailable("this computer's clock follower isn't following the gateway: " - + _why(status, current, unread)) + raise TargetUnavailable(f"{whose} isn't following the gateway: " + _why(status, current, unread)) await asyncio.sleep(0.1) diff --git a/packages/agentenv-protocol/tests/test_a2a_agent_clock.py b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py new file mode 100644 index 000000000..3a09b3a42 --- /dev/null +++ b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py @@ -0,0 +1,115 @@ +"""An agent's side of clock/v1: in an image that runs the clock follower, every AgentEnvAgent serves sync_time with no +code of its own; called by sync_env_clock, it writes the follower's config from the request, sets the task's zone for +what the server starts, and answers once the follower shows the gateway's current version, or says why it doesn't.""" +from __future__ import annotations + +import json +import os +import stat +import time + +import pytest +from starlette.testclient import TestClient + +from agentenv_protocol.a2a_agent import ( + CLOCK_V1, + AgentEnvAgent, + AgentIdentity, + TaskRequest, + TaskResult, + a2a_agent, +) +from agentenv_protocol.computer import clock as follower + +REQUEST = {"env_get_time_url": "http://gw/clock/time", "watch_url": "http://gw/clock/watch", "name": "agent-solver", + "read_token": "token"} + + +@pytest.fixture +def image(tmp_path, monkeypatch): + """An image that runs the follower: its config and status under ``tmp_path``, the gateway at version b:3.""" + (tmp_path / "clock-follower").write_text("") + monkeypatch.setattr(follower, "FOLLOWER", str(tmp_path / "clock-follower")) + monkeypatch.setattr(follower, "DISK_GUEST", str(tmp_path / "image.env")) + monkeypatch.setattr(follower, "CONFIG", str(tmp_path / "follower.json")) + monkeypatch.setattr(follower, "STATUS", str(tmp_path / "status.json")) + monkeypatch.setattr(follower, "CONNECT_WAIT_S", 0.3) + monkeypatch.setattr(follower, "gateway_version", lambda url: "b:3") + monkeypatch.setenv("TZ", "UTC") + yield tmp_path + time.tzset() + + +def _status(image, **status) -> None: + (image / "status.json").write_text(json.dumps(status)) + + +def _app(**declared): + @a2a_agent(identity=AgentIdentity(name="clocked", description="test", version="1"), **declared) + class Agent(AgentEnvAgent): + async def run(self, request: TaskRequest) -> TaskResult: + return TaskResult.text("ok") + + return TestClient(Agent().create_app()) + + +def _clock(client) -> dict | None: + card = client.get("/.well-known/agent.json").json() + return next((e for e in card["capabilities"]["extensions"] if e["uri"] == CLOCK_V1.uri), None) + + +def test_every_agent_in_an_image_with_the_follower_serves_sync_time(image): + with _app() as client: + served = _clock(client) + + assert served is not None + request = served["params"]["methods"]["sync_time"]["request"] + assert request == {"required": ["env_get_time_url"], "optional": ["timezone", "watch_url", "name", "read_token"]} + + +def test_an_agent_in_an_image_without_the_follower_serves_no_clock(image, monkeypatch): + monkeypatch.setattr(follower, "FOLLOWER", str(image / "absent")) + with _app() as client: + assert _clock(client) is None + + +def test_sync_time_writes_the_followers_config_sets_the_zone_and_answers_once_it_follows(image): + _status(image, connected=True, url="http://gw/clock/watch", name="agent-solver", version="b:3", armed=True) + with _app() as client: + answer = client.post("/ext/clock/sync-time", json={**REQUEST, "timezone": "America/Chicago"}) + + assert answer.status_code == 200, answer.text + assert answer.json() == {"synced": True, "timezone": "America/Chicago", "env_get_time_url": "http://gw/clock/time", + "follower": {"name": "agent-solver", "url": "http://gw/clock/watch", "version": "b:3", + "armed": True}} + config = image / "follower.json" + assert json.loads(config.read_text()) == {"watch_url": "http://gw/clock/watch", "name": "agent-solver", + "token": "token"} + assert stat.S_IMODE(config.stat().st_mode) == 0o640 + assert os.environ["TZ"] == "America/Chicago" and time.strftime("%Z") in {"CST", "CDT"} + assert [p.name for p in image.iterdir() if p.name.startswith("follower.json.")] == [] + + +def test_a_follower_behind_the_gateway_is_named(image): + _status(image, connected=True, url="http://gw/clock/watch", version="b:2") + with _app() as client: + answer = client.post("/ext/clock/sync-time", json=REQUEST) + + assert answer.status_code == 404 + assert "this agent's clock follower isn't following the gateway: it shows the gateway's version 'b:2'" in answer.text + + +@pytest.mark.parametrize("missing", ["watch_url", "name", "read_token"]) +def test_a_request_without_the_followers_config_is_refused_and_nothing_is_written(image, missing): + with _app() as client: + answer = client.post("/ext/clock/sync-time", json={k: v for k, v in REQUEST.items() if k != missing}) + + assert answer.status_code == 400 and "watch_url, name and read_token" in answer.text + assert not (image / "follower.json").exists() + + +def test_a_zone_that_isnt_one_is_refused(image): + with _app() as client: + answer = client.post("/ext/clock/sync-time", json={**REQUEST, "timezone": "Mars/Olympus"}) + + assert answer.status_code == 400 and "IANA" in answer.text and os.environ["TZ"] == "UTC" diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index f4041d557..60e1221bf 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -34,6 +34,19 @@ turn off becomes `--disallowedTools`, which keeps the CLI's defaults for the res `claude_bridge.py`: every tool the pinned CLI has, though which it offers by default depends on the model. A test fails when the Dockerfile's `CLAUDE_CODE_VERSION` moves, until `ClaudeCodeTool` and `TOOLS_CLI_VERSION` move with it. +The image follows a task env's clock the way a computer does (FD-5916): it runs the computer base's clock follower +and egress proxy, copied into `clock/` and `egress/` until the clock library image (FD-5700) ships them, each as its own +user under root's supervisord (`start-agent`, `root.conf`), with agent-env's libfaketime preloaded for the agent's user. +The A2A server starts with the real-time marker, so it and its records stay on real time, and serves clock/v1's +`sync_time`, which the protocol's framework serves on the card of any agent in an image with the follower +(`agentenv_protocol.a2a_agent.clock`). Once `sync_env_clock` has armed the clock, it declares the agent at the gateway +as a follower with a read token of its own and calls that `sync_time` with the follower's config, which the server +writes; `deploy_agent` does the same for an agent it deploys while the clock is armed. From then on the CLI, its own +"Today's date is" line included, and every command the model runs read the env's time, and a clock write that waits for +followers waits for the agent. The CLI goes out through the egress proxy, which keeps a live site's TLS working at any +date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time and the proxy relays TLS +untouched. Started as a user other than root, the image runs the A2A server alone, on real time, as before. + `install_agent` installs it too, into a container a task already runs or onto a `vm` sandbox's Linux host, with `install.sh` from its folder: Claude Code's native build for the target's CPU and libc, uv, and the agent's requirements in a Python of its own, so the target's Python is left alone. The CLI runs its commands in bash or zsh, so a target with diff --git a/src/agent_env/agents/agents/claude-code/Dockerfile b/src/agent_env/agents/agents/claude-code/Dockerfile index cbe95d6e6..789bfd983 100644 --- a/src/agent_env/agents/agents/claude-code/Dockerfile +++ b/src/agent_env/agents/agents/claude-code/Dockerfile @@ -1,6 +1,20 @@ # Node for the scripts the model runs, without npm or npx; Claude Code itself is a native build. FROM public.ecr.aws/docker/library/node:22-slim@sha256:c3de60bf2f9dd0ac6370e6117950ff62d6e339527e7472301c9c78a017978392 AS node +# The env's clock (FD-5916): the computer base's pieces, copied in (clock/, egress/) until the clock library image +# (FD-5700) ships them for each libc. The egress proxy, one static Go binary, and agent-env's libfaketime and its TLS +# shim, built for this image's libc (Debian bookworm) in stages of their own so no toolchain is left in the image. +FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress +COPY egress/ /egress/ +RUN cd /egress && go vet ./... && go test -count=1 ./... \ + && CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags=-s -o /out/egress . + +FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS clock +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + build-essential patch curl ca-certificates +COPY clock/ /clock/ +RUN sh /clock/build.sh /out + FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 # poppler-utils: the CLI's Read tool runs pdftoppm for a PDF's pages. RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl poppler-utils \ @@ -24,13 +38,35 @@ COPY requirements.txt model-requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -r model-requirements.txt # install.sh and run.sh are install/v1's: in the image, so they stay in its build context. COPY a2a_server.py claude_bridge.py kit.py tar_changelog.py entrypoint.sh install.sh run.sh ./ -# The CLI's cwd, /app, and its state, fixed so neither follows the uid: Modal runs the image as root. +# The CLI's cwd, /app, and its state, fixed so neither follows the uid. RUN useradd --create-home --uid 10001 agent && mkdir -p /app /home/agent/.claude \ && chown agent:agent /app /home/agent/.claude && chmod 0755 entrypoint.sh +# The env's clock: the follower and the egress proxy, each as its own user, under root's supervisord (root.conf), and +# agent-env's libfaketime and its TLS shim preloaded into every process, faking only the agent's (clock.conf). The +# follower's config is the agent's user's to write (agentenv_protocol.a2a_agent.clock) and the follower's to read. +RUN pip install --no-cache-dir supervisor==4.2.5 \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress \ + && mkdir -p /usr/local/lib/agentenv-clock /usr/local/lib/agentenv-egress /etc/agentenv \ + && install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock \ + && touch /etc/agentenv/ca.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem +COPY --from=clock /out/libfaketimeMT.so.1 /out/libtls_realtime.so /usr/local/lib/agentenv-clock/ +COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress +COPY clock/clockpage.py clock/clock-follower /usr/local/lib/agentenv-clock/ +COPY clock/clock.conf /etc/agentenv/clock.conf +COPY root.conf /etc/agentenv/root.conf +COPY start-agent /usr/local/bin/start-agent +RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/" \ + -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf \ + && chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/bin/start-agent \ + && printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ + > /etc/ld.so.preload ENV A2A_PORT=8000 PYTHONPATH=/opt/agent CLAUDE_CONFIG_DIR=/home/agent/.claude \ DISABLE_AUTOUPDATER=1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 -USER agent WORKDIR /app EXPOSE 8000 -# CMD rather than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. -CMD ["/opt/agent/entrypoint.sh"] +# Starts as root, which start-agent needs for the clock; the A2A server and the CLI run as the agent's user. CMD rather +# than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. +CMD ["/usr/local/bin/start-agent"] 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 e8fcfc651..aabc2ec9b 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -12,11 +12,12 @@ import signal import stat import time -from collections.abc import Callable, Mapping +from collections.abc import Callable, Iterable, Mapping from dataclasses import dataclass, field, replace from functools import partial from pathlib import Path from typing import Literal +from urllib.parse import urlsplit from pydantic import BaseModel, ConfigDict, model_validator @@ -173,8 +174,27 @@ def _model_headers(raw: str | None) -> dict[str, str]: return {str(name): str(value) for name, value in headers.items()} +#: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it. +EGRESS_PROXY_ENV = "AGENTENV_EGRESS_PROXY" +_DIRECT_HOSTS = ("localhost", "127.0.0.1", "::1", "host.docker.internal") + + +def egress_env(environ: Mapping[str, str], direct: Iterable[str] = ()) -> dict[str, str]: + """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916): they read the env's time, + so a client checking a live site's certificate against it would refuse the site once the clock is months off, and + the proxy, on real time, hands it a certificate that holds then. The env's MCP servers (``direct``) are reached + directly: the proxy refuses an address that isn't global, as a local or private gateway's is.""" + proxy = environ.get(EGRESS_PROXY_ENV) + if not proxy: + return {} + hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, direct)])) + return {**{name: proxy for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY") for name in (base, base.lower())}, + "NO_PROXY": hosts, "no_proxy": hosts, "NODE_USE_ENV_PROXY": "1"} + + def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: str | None = None, - task_id: str | None = None, max_thinking_tokens: int | None = None) -> dict[str, str]: + task_id: str | None = None, max_thinking_tokens: int | None = None, + direct_hosts: Iterable[str] = ()) -> dict[str, str]: """The CLI's environment: this process's, pointed at the endpoint agent-env configured (its key as a bearer token, so an API key too would conflict), with the endpoint's headers and the run's attribution, and the model's aliases pinned to ``model`` on that endpoint. A variable already set is kept, LITELLM_* too, so a script the model @@ -203,6 +223,7 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" + env.update(egress_env(environ, direct_hosts)) return env @@ -682,7 +703,9 @@ async def _spawn_claude( stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=cli_env(os.environ, model=model, project_id=project_id, task_id=task_id, - max_thinking_tokens=max_thinking_tokens), + max_thinking_tokens=max_thinking_tokens, + direct_hosts=[urlsplit(str(entry.get("url") or "")).hostname or "" + for entry in (mcp_servers or {}).values()]), cwd=WORKSPACE_DIR, start_new_session=True, )) diff --git a/src/agent_env/agents/agents/claude-code/clock/COPYING b/src/agent_env/agents/agents/claude-code/clock/COPYING new file mode 100644 index 000000000..d159169d1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c new file mode 100644 index 000000000..9167f7ede --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c @@ -0,0 +1,320 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * agent-env's additions to libfaketime 0.9.12, part 1, included by libfaketime.c (libfaketime.patch) before its + * time functions: the process's settings, read once at load from /etc/agentenv/clock.conf, or from the environment + * where there is no such file, so no getenv() runs after main() starts; who is faked (the session user, unless started with AGENTENV_REAL_TIME=1, a marker + * taken out of the environment block in place); and the clock page the follower writes, read without a cache. + * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). + */ +/* + * configuration is read once, when the library loads, from AGENTENV_CLOCK_CONF (NAME=VALUE lines) when that + * file exists, else from the environment, and never with getenv() afterwards: a getenv() racing a program's setenv() + * reads a freed environ array, which is how libfaketime's pthread_cond_timedwait crashed Chromium. + */ +#ifndef AGENTENV_CLOCK_CONF +#define AGENTENV_CLOCK_CONF "/etc/agentenv/clock.conf" +#endif +#define FT_CONF_MAX 64 +static char ft_conf_text[8192]; +static const char *ft_conf_name[FT_CONF_MAX]; +static const char *ft_conf_value[FT_CONF_MAX]; +static int ft_conf_count = 0; +static bool ft_conf_loaded = false; +/* this process stays on real time: another user's than AGENTENV_SESSION_UID, or started with + AGENTENV_REAL_TIME=1. */ +static bool ft_disabled = false; +/* bumped with each new time specification parsed, so a wait sees the clock change under it. */ +static volatile unsigned long ft_spec_gen = 0; +static __thread unsigned long ft_tl_gen = 0; +/* real seconds per fake second for what is left of a wait, as of this thread's last clock read: 1 / rate, + or 1 for a frozen clock. */ +static __thread double ft_tl_scale = 1.0; +/* how long a wait that can be cut sleeps before it looks at the clock again (AGENTENV_WAIT_SLICE_MS). */ +static long ft_wait_slice_ns = 100000000L; + +static void ft_conf_add(char *name, char *value) +{ + if (ft_conf_count < FT_CONF_MAX) + { + ft_conf_name[ft_conf_count] = name; + ft_conf_value[ft_conf_count] = value; + ft_conf_count++; + } +} + +static void ft_conf_load(void) +{ + extern char **environ; + size_t used = 0; + int fd; + if (ft_conf_loaded) return; + ft_conf_loaded = true; + fd = open(AGENTENV_CLOCK_CONF, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + { + ssize_t n; + char *line; + while (used < sizeof(ft_conf_text) - 1 && + (n = read(fd, ft_conf_text + used, sizeof(ft_conf_text) - 1 - used)) > 0) + used += n; + close(fd); + ft_conf_text[used] = 0; + line = ft_conf_text; + while (line != NULL && *line != 0) + { + char *next = strchr(line, '\n'); + char *eq; + if (next != NULL) *next++ = 0; + eq = strchr(line, '='); + if (line[0] != '#' && eq != NULL && eq != line) + { + *eq = 0; + ft_conf_add(line, eq + 1); + } + line = next; + } + return; + } + for (char **e = environ; e != NULL && *e != NULL; e++) + { + const char *s = *e; + size_t len; + char *copy, *eq; + if (strncmp(s, "FAKE", 4) && strncmp(s, "NO_FAKE", 7) && strncmp(s, "DONT_FAKE", 9) && strncmp(s, "HOME=", 5) + && strncmp(s, "AGENTENV_", 9)) + continue; + len = strlen(s); + if (used + len + 1 > sizeof(ft_conf_text)) break; + copy = ft_conf_text + used; + memcpy(copy, s, len + 1); + used += len + 1; + eq = strchr(copy, '='); + if (eq == NULL) continue; + *eq = 0; + ft_conf_add(copy, eq + 1); + } +} + +static char *ft_getenv(const char *name) +{ + ft_conf_load(); + for (int i = 0; i < ft_conf_count; i++) + { + if (strcmp(ft_conf_name[i], name) == 0) return (char *) ft_conf_value[i]; + } + return NULL; +} + +/* + * AGENTENV_REAL_TIME=1 taken out of the original environment block in place: its pointer out of the array + * (which is main()'s envp too) and the string itself changed to "=0", so no copy of it is the marker. unsetenv() isn't + * used: a program may define its own (bash does, and before its main() it does nothing). + */ +static void ft_scrub_marker(void) +{ + extern char **environ; + char **e = environ; + if (e == NULL) return; + while (*e != NULL) + { + if (strcmp(*e, "AGENTENV_REAL_TIME=1") == 0) + { + char **d = e; + (*e)[sizeof("AGENTENV_REAL_TIME=") - 1] = '0'; + do d[0] = d[1]; while (*d++ != NULL); + } + else + e++; + } +} + +/* whether a wait's deadline is one to translate: a valid one, in a faked process, outside libfaketime. */ +static bool ft_translates_wait(const struct timespec *abstime) +{ + return abstime != NULL && !ft_disabled && !dont_fake && abstime->tv_nsec >= 0 && abstime->tv_nsec < SEC_TO_nSEC; +} + +/* + * where a CLOCK_REALTIME deadline in fake time falls on a real clock (CLOCK_MONOTONIC or CLOCK_REALTIME): + * now, plus what is left of the fake interval times ft_tl_scale; so a frozen clock counts what is left as real time and + * nothing waits forever, and a deadline already past is now. + */ +static void ft_deadline_from_fake(clockid_t clock, const struct timespec *fake_abs, struct timespec *out) +{ + struct timespec now, real_now, fake_now, left, scaled; + DONT_FAKE_TIME((*real_clock_gettime)(clock, &now)); + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &real_now)); + fake_now = real_now; + (void) fake_clock_gettime(CLOCK_REALTIME, &fake_now); + *out = now; + timespecsub(fake_abs, &fake_now, &left); + if (left.tv_sec < 0) return; + if (left.tv_sec > 100L * 365 * 86400) left.tv_sec = 100L * 365 * 86400; + if (ft_tl_scale != 1.0) + { + timespecmul(&left, ft_tl_scale, &scaled); + } + else + { + scaled = left; + } + timespecadd(&now, &scaled, out); +} + +/* the end of a wait's next slice on CLOCK_MONOTONIC; true when that is the deadline itself. */ +static bool ft_slice_end(const struct timespec *mono_abs, struct timespec *slice) +{ + struct timespec now, step = {ft_wait_slice_ns / SEC_TO_nSEC, ft_wait_slice_ns % SEC_TO_nSEC}; + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &now)); + timespecadd(&now, &step, slice); + if (timespeccmp(slice, mono_abs, <)) return false; + *slice = *mono_abs; + return true; +} + +/* after a slice, the clock read again (the file, with no cache); a new specification since ``gen`` moves the + deadline. True when it moved. */ +static bool ft_deadline_moved(const struct timespec *fake_abs, struct timespec *mono_abs, unsigned long *gen) +{ + struct timespec now; + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &now)); + (void) fake_clock_gettime(CLOCK_REALTIME, &now); + if (ft_tl_gen == *gen) return false; + *gen = ft_tl_gen; + ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, mono_abs); + return true; +} + +/* + * the clock page. 64 bytes, little-endian, rewritten in place by the follower: seq odd while it writes, + * then even; check is the xor of the other words and a constant. A realtime read in a faked process copies it + * lock-free and computes universe + (now - anchor) * rate (rate 0: universe), so every process agrees with no shared + * start; the first thread to see a new generation also hands it to libfaketime's own state (rates of sleeps, the + * time an absolute spec gives) under the MT lock. The page must be owned by AGENTENV_CLOCK_PAGE_UID (0 by default) and + * writable by no one else, so the session user can't plant one. + */ +struct ft_clock_page +{ + uint32_t magic, version; + uint64_t seq; + int64_t universe_ns, anchor_ns; + double rate; + uint32_t armed, reserved; + uint64_t generation, check; + int64_t mono_universe_ns, mono_anchor_ns; +}; +#define FT_PAGE_MAGIC 0x4B434541u +#define FT_PAGE_VERSION 2u +static const volatile struct ft_clock_page *ft_page = NULL; +static uint64_t ft_page_applied = 0; +static time_t ft_page_last_try = 0; +static void parse_ft_string(const char *user_faked_time); + +static uint64_t ft_page_check(const struct ft_clock_page *p) +{ + uint64_t rate_bits; + memcpy(&rate_bits, &p->rate, sizeof rate_bits); + return p->seq ^ (uint64_t) p->universe_ns ^ (uint64_t) p->anchor_ns ^ rate_bits ^ p->armed ^ p->generation + ^ (uint64_t) p->mono_universe_ns ^ (uint64_t) p->mono_anchor_ns ^ 0x9E3779B97F4A7C15ull; +} + +static bool ft_page_snapshot(struct ft_clock_page *out) +{ + for (int i = 0; i < 1000; i++) + { + uint64_t s1 = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); + if (s1 & 1) continue; + out->magic = ft_page->magic; + out->seq = s1; + out->universe_ns = ft_page->universe_ns; + out->anchor_ns = ft_page->anchor_ns; + out->rate = ft_page->rate; + out->armed = ft_page->armed; + out->version = ft_page->version; + out->generation = ft_page->generation; + out->check = ft_page->check; + out->mono_universe_ns = ft_page->mono_universe_ns; + out->mono_anchor_ns = ft_page->mono_anchor_ns; + __atomic_thread_fence(__ATOMIC_ACQUIRE); + if (__atomic_load_n(&ft_page->seq, __ATOMIC_RELAXED) == s1 && out->magic == FT_PAGE_MAGIC + && out->version == FT_PAGE_VERSION + && ft_page_check(out) == out->check) + return true; + } + return false; +} + +static void ft_page_open(void) +{ + const char *path = ft_getenv("AGENTENV_CLOCK_PAGE"); + const char *owner = ft_getenv("AGENTENV_CLOCK_PAGE_UID"); + struct stat st; + void *map; + int fd; + if (path == NULL || *path == 0) return; + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return; + if (fstat(fd, &st) != 0 || st.st_size < (off_t) sizeof(struct ft_clock_page) + || st.st_uid != (uid_t) (owner ? strtoul(owner, NULL, 10) : 0) || (st.st_mode & 022) != 0) + { + close(fd); + return; + } + map = mmap(NULL, sizeof(struct ft_clock_page), PROT_READ, MAP_SHARED, fd, 0); + close(fd); + if (map == MAP_FAILED) return; + if (((const struct ft_clock_page *) map)->magic != FT_PAGE_MAGIC + || ((const struct ft_clock_page *) map)->version != FT_PAGE_VERSION) + { + munmap(map, sizeof(struct ft_clock_page)); + return; + } + ft_page = map; +} + +/* libfaketime's own state set to the page's clock; called with the MT lock held. */ +static void ft_page_apply_locked(const struct ft_clock_page *s) +{ + char spec[160]; + if (s->generation == ft_page_applied) return; + if (!s->armed) + snprintf(spec, sizeof spec, "+0 x1"); + else + snprintf(spec, sizeof spec, "@%lld.%09lld x%.17g ^%lld.%09lld", + (long long) (s->universe_ns / 1000000000LL), (long long) (s->universe_ns % 1000000000LL), s->rate, + (long long) (s->anchor_ns / 1000000000LL), (long long) (s->anchor_ns % 1000000000LL)); + parse_ft_string(spec); + __atomic_store_n(&ft_page_applied, s->generation, __ATOMIC_RELEASE); +} + +/* "%s" times are read as a number, without strptime() and mktime(), which reads TZ with getenv(). */ +static const char *ft_parse_epoch(const char *s, struct timespec *out) +{ + char *end; + long long sec; + long nsec = 0, scale = 100000000L; + bool negative; + while (*s == ' ') s++; + negative = (*s == '-'); + sec = strtoll(s, &end, 10); + if (end == s) return NULL; + if (*end == '.') + { + end++; + while (*end >= '0' && *end <= '9') + { + nsec += (*end - '0') * scale; + scale /= 10; + end++; + } + } + if (negative && nsec > 0) + { + sec -= 1; + nsec = SEC_TO_nSEC - nsec; + } + out->tv_sec = (time_t) sec; + out->tv_nsec = nsec; + return end; +} + diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c new file mode 100644 index 000000000..5d8237705 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c @@ -0,0 +1,1175 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * agent-env's additions to libfaketime 0.9.12, part 2, included by libfaketime.c (libfaketime.patch) after its time + * functions: clock reads and waits for a set realtime clock time on universe time, re-planned whenever the page + * changes, and everything else (monotonic and boot clocks, relative sleeps and timeouts) the kernel's. + * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). + */ +/* + * Elapsed time is real; clock reads and waits for a realtime clock time follow the universe. + */ +#include +#include +#include +#include + +#if !defined(FAKE_PTHREAD) || !defined(FAKE_TIMERS) || !defined(FAKE_SLEEP) || defined(INTERCEPT_SYSCALL) +#error agent-env waits need FAKE_PTHREAD, FAKE_TIMERS and FAKE_SLEEP, and their own syscall(), not the INTERCEPT_SYSCALL one +#endif +#ifndef SYS_futex_waitv +#define SYS_futex_waitv 449 +#endif + +/* + * the system call itself, with no libc and no libfaketime state, so syscall() below works before the + * library is initialised (Chromium's allocator calls syscall() from inside the dynamic loader). + */ +static long ft_raw_syscall6(long n, long a, long b, long c, long d, long e, long f) +{ + long ret; +#if defined(__x86_64__) + register long r10 __asm__("r10") = d; + register long r8 __asm__("r8") = e; + register long r9 __asm__("r9") = f; + __asm__ volatile("syscall" : "=a"(ret) : "0"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9) + : "rcx", "r11", "memory"); +#elif defined(__aarch64__) + register long x8 __asm__("x8") = n; + register long x0 __asm__("x0") = a; + register long x1 __asm__("x1") = b; + register long x2 __asm__("x2") = c; + register long x3 __asm__("x3") = d; + register long x4 __asm__("x4") = e; + register long x5 __asm__("x5") = f; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); + ret = x0; +#else +#error the agent-env syscall() needs x86_64 or aarch64 +#endif + if (ret < 0 && ret > -4096) + { + errno = (int) -ret; + return -1; + } + return ret; +} + +#define FT_NS 1000000000LL +#define FT_NEVER INT64_MAX +static __thread struct ft_clock_page ft_tl_snap; +static __thread bool ft_tl_snap_ok = false; + +/* a consistent copy of the page. While the follower holds it odd (preempted mid-write), wait for it on seq, + which it wakes when done, up to a second: a copy older than the page being written would be extrapolated past the + new page's anchor, and the clock would step back when the write lands. Only a follower that died mid-write (seq odd + for over a second, remembered) leaves this thread on its last good copy. */ +static uint64_t ft_dead_seq = 0; +static bool ft_snap(struct ft_clock_page *s) +{ + for (int i = 0; i < 64; i++) + { + if (ft_page_snapshot(s)) + { + ft_tl_snap = *s; + ft_tl_snap_ok = true; + return true; + } + sched_yield(); + } + for (int i = 0; i < 100; i++) + { + uint64_t seq = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); + if ((seq & 1) && seq == __atomic_load_n(&ft_dead_seq, __ATOMIC_RELAXED)) break; + if (seq & 1) + { + struct timespec ts = {0, 10000000}; + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) (uint32_t) seq, (long) &ts, 0, 0); + } + if (ft_page_snapshot(s)) + { + ft_tl_snap = *s; + ft_tl_snap_ok = true; + return true; + } + if (i == 99) __atomic_store_n(&ft_dead_seq, seq, __ATOMIC_RELAXED); + } + if (!ft_tl_snap_ok) return false; + *s = ft_tl_snap; + return true; +} + +/* the real time just read, in hand before the loads after it: a load can be carried out before an earlier read of the + CPU's counter (aarch64 and x86_64 both allow it). On aarch64 a load whose address depends on the time, then a load + barrier, as the kernel orders its own counter reads; on x86_64 an lfence. */ +static inline void ft_after_real_read(const struct timespec *tp) +{ +#if defined(__aarch64__) + uint64_t tmp; + __asm__ volatile("eor %0, %1, %1\n\tadd %0, sp, %0\n\tldr xzr, [%0]\n\tdmb ishld" + : "=&r"(tmp) : "r"((uint64_t) tp->tv_nsec) : "memory"); +#elif defined(__x86_64__) + (void) tp; + __asm__ volatile("lfence" ::: "memory"); +#else + (void) tp; + __atomic_thread_fence(__ATOMIC_SEQ_CST); +#endif +} + +/* a consistent copy of the page and the real time read while it was the page: the time is read after the copy and kept + only if the page is still the one copied. A time from before a write paired with the page it wrote (a reader that + waited out the write, or was descheduled between its time and its copy) could put the universe behind what that + reader had shown, since a write is continuous with the page it replaces only at its own real time. */ +static bool ft_snap_at(clockid_t clock, struct ft_clock_page *s, struct timespec *tp) +{ + for (int i = 0; i < 8; i++) + { + int r; + if (!ft_snap(s)) return false; + DONT_FAKE_TIME(r = (*real_clock_gettime)(clock, tp)); + if (r != 0) return false; + ft_after_real_read(tp); + if (__atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE) == s->seq) return true; + } + return true; +} + +static inline int64_t ft_ns(const struct timespec *t) +{ + if (t->tv_sec > 9000000000LL) return FT_NEVER / 2; + if (t->tv_sec < -9000000000LL) return -(FT_NEVER / 2); + return (int64_t) t->tv_sec * FT_NS + t->tv_nsec; +} +static inline struct timespec ft_ts(int64_t ns) +{ + struct timespec t; + if (ns < 0) ns = 0; + t.tv_sec = (time_t) (ns / FT_NS); + t.tv_nsec = (long) (ns % FT_NS); + return t; +} +static inline int64_t ft_real_rt(void) +{ + struct timespec t; + (*real_clock_gettime)(CLOCK_REALTIME, &t); + return ft_ns(&t); +} +static inline bool ft_uw_active(void) +{ + return ft_page != NULL && !ft_disabled && !dont_fake; +} +static inline bool ft_faked(void) +{ + return !ft_disabled && !dont_fake; +} +static inline bool ft_valid_ts(const struct timespec *t) +{ + return t != NULL && t->tv_nsec >= 0 && t->tv_nsec < FT_NS && t->tv_sec >= 0; +} +static inline bool ft_rt_clock(clockid_t c) +{ + return c == CLOCK_REALTIME || c == CLOCK_REALTIME_ALARM; +} +/* the clocks the universe has; every other one (monotonic, boot, raw, CPU time) is real. */ +static inline bool ft_universe_clock(clockid_t c) +{ + return c == CLOCK_REALTIME || c == CLOCK_REALTIME_COARSE || c == CLOCK_REALTIME_ALARM || c == CLOCK_TAI; +} + +/* the universe's realtime at real realtime t; before the anchor, the anchor's. */ +static int64_t ft_page_rt(const struct ft_clock_page *s, int64_t t) +{ + if (!s->armed) return t; + if (s->rate == 0 || t <= s->anchor_ns) return s->universe_ns; + if (s->rate == 1.0) return s->universe_ns + (t - s->anchor_ns); + return s->universe_ns + (int64_t) ((double) (t - s->anchor_ns) * s->rate); +} +/* the real realtime at which the universe reaches ``due``; 0 (the past) once it has, FT_NEVER while frozen + short of it. */ +static int64_t ft_real_of(const struct ft_clock_page *s, int64_t due) +{ + double r; + if (!s->armed) return due < 1 ? 1 : due; + if (due <= s->universe_ns) return 1; + if (s->rate <= 0) return FT_NEVER; + r = (double) (due - s->universe_ns) / s->rate; + if (r > 4e18) return FT_NEVER; + return s->anchor_ns + (int64_t) r + 1; +} +static inline bool ft_reached(const struct ft_clock_page *s, int64_t due) +{ + return ft_page_rt(s, ft_real_rt()) >= due; +} +static inline uint32_t ft_seq32(void) +{ + return (uint32_t) __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); +} + +/* ---- a lock for the tables below, with signals blocked so a handler can't deadlock on it ---- */ +static volatile int ft_tab_lock_word = 0; +static void ft_tab_lock(sigset_t *old) +{ + sigset_t all; + sigfillset(&all); + pthread_sigmask(SIG_SETMASK, &all, old); + while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); +} +static void ft_tab_unlock(const sigset_t *old) +{ + __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); + pthread_sigmask(SIG_SETMASK, old, NULL); +} + +/* + * ---- timers for a realtime clock time: timerfds and POSIX timers armed at TIMER_ABSTIME on a realtime clock. The + * kernel timer is armed at the real realtime the universe reaches the deadline at (in the past once it has, so it fires + * now), its interval at interval / rate, and disarmed while frozen short of it; the helper arms it again on each change. + * A one-shot timer that has fired is left alone. ---- + */ +#define FT_TIMER_FDS 8 +struct ft_timer +{ + int kind; /* 0 free, 1 timerfd, 2 POSIX timer */ + int tracked; /* armed for a realtime clock time */ + int nfds, fds[FT_TIMER_FDS]; /* a timerfd's descriptors: the one it was armed through and its duplicates */ + timer_t id; + int clock; + int flags; /* the caller's flags, TFD_TIMER_CANCEL_ON_SET kept */ + int real_set; /* the kernel timer is armed */ + int64_t due, interval; /* universe ns */ + struct ft_clock_page pg; /* the page it was last armed from */ +}; +#define FT_TIMERS_MAX 1024 +static struct ft_timer ft_timers[FT_TIMERS_MAX]; +static int ft_timers_top = 0; +static volatile int ft_timers_any = 0; +static int (*real_timer_create_new)(clockid_t, struct sigevent *, timer_t *); +static int (*real_timer_delete_new)(timer_t); +static int (*real_timer_settime_new)(timer_t, int, const struct itimerspec *, struct itimerspec *); +static int (*real_timer_gettime_new)(timer_t, struct itimerspec *); +#define FT_TFD_CLOCKS 65536 +static int ft_tfd_clock[FT_TFD_CLOCKS]; /* clock + 1 as timerfd_create was told; 0 unknown */ + +static struct ft_timer *ft_timer_find_fd(int fd) +{ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 1) + for (int k = 0; k < ft_timers[i].nfds; k++) + if (ft_timers[i].fds[k] == fd) return &ft_timers[i]; + return NULL; +} +static struct ft_timer *ft_timer_find_id(timer_t id) +{ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 2 && ft_timers[i].id == id) return &ft_timers[i]; + return NULL; +} +static struct ft_timer *ft_timer_new(void) +{ + for (int i = 0; i < FT_TIMERS_MAX; i++) + if (ft_timers[i].kind == 0) + { + memset(&ft_timers[i], 0, sizeof ft_timers[i]); + if (i >= ft_timers_top) ft_timers_top = i + 1; + return &ft_timers[i]; + } + return NULL; +} +static int ft_timer_real_get(struct ft_timer *t, struct itimerspec *cur) +{ + return t->kind == 1 ? (*real_timerfd_gettime)(t->fds[0], cur) : (*real_timer_gettime_new)(t->id, cur); +} +/* the next expiration at or after ``after``. */ +static int64_t ft_timer_next(const struct ft_timer *t, int64_t after) +{ + int64_t next = t->due; + if (t->interval > 0 && next < after) next += ((after - next + t->interval - 1) / t->interval) * t->interval; + return next; +} +/* the kernel timer set from page ``s``. A periodic timer goes on from its first expiration past the + universe's time on the page it was armed from, so a jump delivers what it passed at once. Lock held. */ +static int ft_timer_arm(struct ft_timer *t, const struct ft_clock_page *s, bool first) +{ + struct itimerspec real, cur; + double rate = s->armed ? s->rate : 1.0; + int64_t next = t->due, at; + int r; + memset(&real, 0, sizeof real); + if (!first && t->real_set && t->interval == 0 && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 + && cur.it_value.tv_nsec == 0) + { + t->tracked = 0; /* fired: the expiration is the kernel's to hand over */ + return 0; + } + if (!first && t->interval > 0) next = ft_timer_next(t, ft_page_rt(&t->pg, ft_real_rt()) + 1); + at = ft_real_of(s, next); + if (at != FT_NEVER) + { + real.it_value = ft_ts(at); + if (real.it_value.tv_sec == 0 && real.it_value.tv_nsec == 0) real.it_value.tv_nsec = 1; + if (t->interval > 0) + { + int64_t iv = (int64_t) ((double) t->interval / rate); + real.it_interval = ft_ts(iv < 1 ? 1 : iv); + } + } + t->due = next; + t->pg = *s; + t->real_set = (at != FT_NEVER); + if (t->kind == 1) + r = (*real_timerfd_settime)(t->fds[0], TFD_TIMER_ABSTIME | (t->flags & TFD_TIMER_CANCEL_ON_SET), &real, NULL); + else + r = (*real_timer_settime_new)(t->id, TIMER_ABSTIME, &real, NULL); + if (r != 0) t->tracked = 0; + return r; +} +/* the timer's value in universe time. Lock held. */ +static void ft_timer_value(struct ft_timer *t, struct itimerspec *out) +{ + struct ft_clock_page s; + struct itimerspec cur; + memset(out, 0, sizeof *out); + if (!t->tracked) return; + if (t->interval == 0 && t->real_set && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 + && cur.it_value.tv_nsec == 0) + { + t->tracked = 0; + return; + } + out->it_interval = ft_ts(t->interval); + if (ft_snap(&s)) + { + int64_t now = ft_page_rt(&s, ft_real_rt()), next = ft_timer_next(t, now); + out->it_value = ft_ts(next - now < 1 ? 1 : next - now); + } +} + +/* + * ---- realtime condvar waits in progress: each waits to the real time its deadline falls at; on a page change that + * moves that time, the helper broadcasts its condvar, once, and the wait returns 0 (a spurious wakeup the caller + * rechecks), so the caller waits again on the new page. A broadcast that comes before the waiter is inside glibc's wait + * is sent again until the waiter has seen the change. ---- + */ +struct ft_cw +{ + pthread_cond_t *cond; + int used, woken; + int64_t due, real_due; + uint64_t gen; /* the page generation its real_due came from; FT_CW_UNPLANNED before */ +}; +#define FT_CW_MAX 512 +#define FT_CW_UNPLANNED UINT64_MAX +static struct ft_cw ft_cws[FT_CW_MAX]; +static int ft_cws_top = 0; + +/* ---- the helper thread: one per process, started with the first such timer or wait, blocked on the page's seq ---- */ +static volatile pid_t ft_helper_pid = 0; +static volatile int ft_atfork_set = 0; +static void ft_helper_child(void) +{ + /* POSIX timers and condvar waiters belong to the parent; timerfds are shared with it and stay */ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 2) ft_timers[i].kind = 0; + memset(ft_cws, 0, sizeof ft_cws); + ft_cws_top = 0; + ft_tab_lock_word = 0; + ft_helper_pid = 0; +} + +/* on a page change, every timer armed again and every condvar waiter whose real deadline moved woken; true + while a waiter is still to see it (not planned yet, or broadcast to before it was waiting). */ +static bool ft_on_change(const struct ft_clock_page *s, bool rebroadcast) +{ + sigset_t old; + bool pending = false; + ft_tab_lock(&old); + if (!rebroadcast) + for (int i = 0; i < ft_timers_top; i++) + { + struct ft_timer *t = &ft_timers[i]; + if (t->kind != 0 && t->tracked && t->pg.generation != s->generation) ft_timer_arm(t, s, false); + } + for (int i = 0; i < ft_cws_top; i++) + { + struct ft_cw *w = &ft_cws[i]; + if (!w->used || w->gen == s->generation) continue; + if (w->gen == FT_CW_UNPLANNED) + { + pending = true; + continue; + } + if (!w->woken) + { + int64_t nd = ft_real_of(s, w->due), diff; + diff = (nd == FT_NEVER || w->real_due == FT_NEVER) ? (nd == w->real_due ? 0 : FT_NEVER) : nd - w->real_due; + if (diff >= -1000000 && diff <= 1000000) + { + w->gen = s->generation; /* its real end didn't move */ + continue; + } + } + if (!w->woken || rebroadcast) pthread_cond_broadcast(w->cond); + w->woken = 1; + pending = true; + } + ft_tab_unlock(&old); + return pending; +} + +static void *ft_helper_main(void *arg) +{ + struct ft_clock_page s; + uint64_t gen = UINT64_MAX; /* the first pass catches a change made while it started */ + int backoff_ms = 0; + (void) arg; + for (;;) + { + uint32_t seq32 = ft_seq32(); + bool pending = false; + if (ft_snap(&s)) + { + if (s.generation != gen) + { + gen = s.generation; + pending = ft_on_change(&s, false); + backoff_ms = pending ? 1 : 0; + } + else if (backoff_ms > 0) + { + pending = ft_on_change(&s, true); + backoff_ms = pending ? (backoff_ms < 64 ? backoff_ms * 2 : 100) : 0; + } + } + if (backoff_ms > 0) + { + struct timespec ts = {0, (long) backoff_ms * 1000000L}; + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, (long) &ts, 0, 0); + } + else + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, 0, 0, 0); + } + return NULL; +} + +static void ft_helper_start(void) +{ + pid_t pid = getpid(); + sigset_t all, old; + pthread_t thread; + pthread_attr_t attr; + if (__atomic_load_n(&ft_helper_pid, __ATOMIC_ACQUIRE) == pid) return; + sigfillset(&all); + pthread_sigmask(SIG_SETMASK, &all, &old); + while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); + if (ft_helper_pid != pid) + { + if (!ft_atfork_set) + { + pthread_atfork(NULL, NULL, ft_helper_child); + ft_atfork_set = 1; + } + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&attr, 128 * 1024); + if (pthread_create(&thread, &attr, ft_helper_main, NULL) == 0) /* inherits the all-blocked signal mask */ + { + __atomic_store_n(&ft_helper_pid, pid, __ATOMIC_RELEASE); + pthread_setname_np(thread, "agentenv-clock"); + } + pthread_attr_destroy(&attr); + } + __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); + pthread_sigmask(SIG_SETMASK, &old, NULL); +} + +/* ---- the waits ---- */ + +/* the raw system calls below as the cancellation points their callers are (clock_nanosleep, sem_timedwait): + cancellation acted on at once for the call alone, as glibc's own waits were before its cancellable system call + wrapper. */ +static inline int ft_async_cancel(void) +{ + int old; + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); + return old; +} +static inline void ft_deferred_cancel(int old) +{ + int e = errno, ignored; + pthread_setcanceltype(old, &ignored); + errno = e; +} + +/* a sleep until the universe reaches ``due``, on the page's seq. 0, or EINTR. */ +static int ft_rt_sleep(const struct ft_clock_page *first, int64_t due) +{ + struct ft_clock_page s = *first; + pthread_testcancel(); + for (;;) + { + uint32_t seq32 = (uint32_t) s.seq; + int64_t until; + struct timespec ts; + long r; + int ct; + if (ft_reached(&s, due)) return 0; + until = ft_real_of(&s, due); + ts = ft_ts(until); + ct = ft_async_cancel(); + r = ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, (long) seq32, + until == FT_NEVER ? 0 : (long) &ts, 0, FUTEX_BITSET_MATCH_ANY); + ft_deferred_cancel(ct); + if (r == -1 && errno == EINTR) return EINTR; + if (!ft_snap(&s)) return 0; + } +} + +struct ft_futex_waitv { uint64_t val, uaddr; uint32_t flags, reserved; }; +static volatile int ft_waitv_missing = 0; +enum { FT_W_WORD, FT_W_PAGE, FT_W_TIMEOUT, FT_W_INTR, FT_W_ERR }; +/* without futex_waitv (before Linux 5.16, under gVisor, or refused by a seccomp filter): the word alone, until the real + deadline or a slice (AGENTENV_WAIT_SLICE_MS) at most, and the caller reads the page again between slices. */ +static int ft_wait1(uint32_t *word, uint32_t val, bool private_word, int64_t until) +{ + int64_t end = ft_real_rt() + ft_wait_slice_ns; + struct timespec ts; + if (until < end) end = until; + ts = ft_ts(end); + if (ft_raw_syscall6(SYS_futex, (long) word, + FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME | (private_word ? FUTEX_PRIVATE_FLAG : 0), (long) val, + (long) &ts, 0, FUTEX_BITSET_MATCH_ANY) == 0 || errno == EAGAIN) + return FT_W_WORD; + if (errno == ETIMEDOUT) return FT_W_PAGE; /* a slice's end, or the deadline: the caller reads the page */ + return errno == EINTR ? FT_W_INTR : FT_W_ERR; +} +/* until *word isn't val, the page changes from seq32, or real realtime ``until``; FT_W_ERR with errno the kernel's + (an unaligned or unmapped word). */ +static int ft_wait2(uint32_t *word, uint32_t val, bool private_word, uint32_t seq32, int64_t until) +{ + struct ft_futex_waitv w[2]; + struct timespec ts = ft_ts(until); + long r; + if (ft_waitv_missing) return ft_wait1(word, val, private_word, until); + /* the page first: futex_waitv returns the last of the futexes woken, so a caller's wake is never taken for the page's */ + w[0].val = seq32; + w[0].uaddr = (uint64_t) (uintptr_t) &ft_page->seq; + w[0].flags = 0x02; /* FUTEX2_SIZE_U32, shared */ + w[0].reserved = 0; + w[1].val = val; + w[1].uaddr = (uint64_t) (uintptr_t) word; + w[1].flags = 0x02 | (private_word ? 128 : 0); + w[1].reserved = 0; + r = ft_raw_syscall6(SYS_futex_waitv, (long) w, 2, 0, until == FT_NEVER ? 0 : (long) &ts, CLOCK_REALTIME, 0); + if (r == 1) return FT_W_WORD; + if (r == 0) return FT_W_PAGE; + if (errno == EAGAIN) return __atomic_load_n(word, __ATOMIC_RELAXED) != val ? FT_W_WORD : FT_W_PAGE; + if (errno == ETIMEDOUT) return FT_W_TIMEOUT; + if (errno == EINTR) return FT_W_INTR; + if (errno != ENOSYS && errno != EPERM) return FT_W_ERR; /* EPERM: a seccomp filter that refuses it so */ + ft_waitv_missing = 1; + return ft_wait1(word, val, private_word, until); +} + +/* futex(2) FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME through syscall(): until the universe reaches the deadline, + beside the page; *handled false leaves the call to the kernel as it is. */ +static long ft_u_futex_rt(uint32_t *uaddr, int op, uint32_t val, const struct timespec *abs, bool *handled) +{ + struct ft_clock_page s; + int64_t due = ft_ns(abs); + *handled = false; + if (!ft_snap(&s)) return 0; + *handled = true; + for (;;) + { + uint32_t seq32 = (uint32_t) s.seq; + int k; + if (ft_reached(&s, due)) + { + errno = ETIMEDOUT; + return -1; + } + k = ft_wait2(uaddr, val, (op & FUTEX_PRIVATE_FLAG) != 0, seq32, ft_real_of(&s, due)); + if (k == FT_W_WORD) + { + if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) + { + errno = EAGAIN; + return -1; + } + return 0; + } + if (k == FT_W_INTR) + { + errno = EINTR; + return -1; + } + if (k == FT_W_ERR) return -1; + if (!ft_snap(&s)) return 0; + } +} + +/* sem_timedwait / sem_clockwait(CLOCK_REALTIME) on glibc's semaphore (its layout is part of the ABI, since + process-shared semaphores outlive glibc versions): the value in the low word of data, the waiters above it, the + futex the value word, which sem_post wakes when there are waiters; ``private`` is glibc's futex flag the other way + round (0 for a process-private semaphore, FUTEX_PRIVATE_FLAG for a shared one). */ +struct ft_new_sem { uint64_t data; int private_flag; int pad; }; +static void ft_sem_unwait(void *sem) +{ + __atomic_fetch_sub(&((struct ft_new_sem *) sem)->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); +} +/* as one of the semaphore's waiters (counted in data's high word): 0 with a token taken, or the error. A cancelled wait + leaves the waiters it joined, as glibc's does. */ +static int ft_sem_wait_slow(struct ft_new_sem *sem, struct ft_clock_page *s, int64_t due) +{ + volatile int err = 0; + pthread_cleanup_push(ft_sem_unwait, sem); + uint64_t d = __atomic_fetch_add(&sem->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); + for (;;) + { + if ((d & 0xffffffffu) == 0) + { + int k, ct; + if (ft_reached(s, due)) + { + err = ETIMEDOUT; + break; + } + ct = ft_async_cancel(); + k = ft_wait2((uint32_t *) &sem->data, 0, sem->private_flag == 0, (uint32_t) s->seq, ft_real_of(s, due)); + ft_deferred_cancel(ct); + if (k == FT_W_INTR || k == FT_W_ERR) + { + err = k == FT_W_INTR ? EINTR : errno; + break; + } + if (!ft_snap(s)) + { + err = ETIMEDOUT; + break; + } + d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); + } + else if (__atomic_compare_exchange_n(&sem->data, &d, d - 1 - ((uint64_t) 1 << 32), true, __ATOMIC_ACQUIRE, + __ATOMIC_RELAXED)) + break; + } + pthread_cleanup_pop(err != 0); + return err; +} +static int ft_u_sem_wait(sem_t *sem_, int64_t due, bool *handled) +{ + struct ft_new_sem *sem = (struct ft_new_sem *) sem_; + struct ft_clock_page s; + uint64_t d; + int err; + *handled = false; + if (sizeof(sem_t) < sizeof(struct ft_new_sem) || !ft_snap(&s)) return 0; + *handled = true; + pthread_testcancel(); + d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); + while ((d & 0xffffffffu) > 0) + if (__atomic_compare_exchange_n(&sem->data, &d, d - 1, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) return 0; + err = ft_sem_wait_slow(sem, &s, due); + if (err == 0) return 0; + errno = err; + return -1; +} + +static void ft_cw_release(void *w) +{ + sigset_t old; + ft_tab_lock(&old); + ((struct ft_cw *) w)->used = 0; + ft_tab_unlock(&old); +} +/* glibc's wait, a cancellation point: a cancelled wait gives up its entry, so the helper never wakes a condvar that + may be gone. */ +static int ft_cw_clockwait(struct ft_cw *w, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *end) +{ + volatile int r; + pthread_cleanup_push(ft_cw_release, w); + r = real_pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, end); + pthread_cleanup_pop(0); + return r; +} + +/* a realtime condvar wait (see ft_cw). -1 leaves it to the caller's own path (no page, table full). */ +static int ft_u_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) +{ + struct ft_clock_page s; + struct ft_cw *w = NULL; + struct timespec end; + sigset_t old; + int64_t due = ft_ns(abstime), real_due; + int r, woken; + if (!ft_snap(&s)) return -1; + if (ft_reached(&s, due)) return ETIMEDOUT; + ft_helper_start(); + ft_tab_lock(&old); + for (int i = 0; i < FT_CW_MAX; i++) + if (!ft_cws[i].used) + { + w = &ft_cws[i]; + if (i >= ft_cws_top) ft_cws_top = i + 1; + break; + } + if (w != NULL) + { + w->cond = cond; + w->due = due; + w->woken = 0; + w->gen = FT_CW_UNPLANNED; + w->used = 1; + } + ft_tab_unlock(&old); + if (w == NULL) return -1; + if (!ft_snap(&s)) s.armed = 0; + real_due = ft_real_of(&s, due); + ft_tab_lock(&old); + w->real_due = real_due; + w->gen = s.generation; + ft_tab_unlock(&old); + end = ft_ts(real_due == FT_NEVER ? ft_real_rt() + 86400 * FT_NS : real_due); + r = ft_cw_clockwait(w, cond, mutex, &end); + ft_tab_lock(&old); + woken = w->woken; + w->used = 0; + ft_tab_unlock(&old); + /* a real timeout before the universe's deadline (the page moved it later) is a spurious wakeup; the helper's wake for + a jump that passed the deadline is the timeout */ + if (r == ETIMEDOUT) return (ft_snap(&s) && !ft_reached(&s, due)) ? 0 : ETIMEDOUT; + if (r == 0 && woken && ft_snap(&s) && ft_reached(&s, due)) return ETIMEDOUT; + return r; +} + +/* a realtime mutex wait: glibc's mutex has no way in, so slices (AGENTENV_WAIT_SLICE_MS) of a real + CLOCK_MONOTONIC wait, which loses nothing when it times out, with the page read between them. */ +static int ft_u_mutex_lock(pthread_mutex_t *mutex, int64_t due, bool *handled) +{ + struct ft_clock_page s; + *handled = false; + if (!ft_snap(&s)) return 0; + *handled = true; + for (;;) + { + struct timespec now, end; + int64_t at, sl; + int r; + if (ft_reached(&s, due)) return ETIMEDOUT; + at = ft_real_of(&s, due); + sl = (at == FT_NEVER) ? ft_wait_slice_ns : at - ft_real_rt(); + if (sl > ft_wait_slice_ns) sl = ft_wait_slice_ns; + if (sl < 1) sl = 1; + (*real_clock_gettime)(CLOCK_MONOTONIC, &now); + end = ft_ts(ft_ns(&now) + sl); + r = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &end); + if (r == EINVAL) /* a PI mutex on a kernel without FUTEX_LOCK_PI2 */ + { + struct timespec real = ft_ts(ft_real_rt() + sl); + r = real_pthread_mutex_timedlock(mutex, &real); + } + if (r != ETIMEDOUT) return r; + if (!ft_snap(&s)) return ETIMEDOUT; + } +} + +/* syscall(): futex waits for a realtime clock time follow the universe (raw futex users such as Rust's std + and GLib call it), clock_gettime reads it once the library is up; everything else goes straight to the kernel. */ +long syscall(long number, ...) +{ + va_list ap; + long a[6]; + va_start(ap, number); + for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); + va_end(ap); + if (number == SYS_futex && ft_uw_active()) + { + int op = (int) a[1]; + const struct timespec *ts = (const struct timespec *) a[3]; + if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET && (op & FUTEX_CLOCK_REALTIME) && ft_valid_ts(ts) + && (uint32_t) a[5] == FUTEX_BITSET_MATCH_ANY) + { + bool handled; + long r = ft_u_futex_rt((uint32_t *) a[0], op, (uint32_t) a[2], ts, &handled); + if (handled) return r; + } + } + else if (number == SYS_clock_gettime && ft_page != NULL && !ft_disabled && !dont_fake) + return clock_gettime((clockid_t) a[0], (struct timespec *) a[1]); + return ft_raw_syscall6(number, a[0], a[1], a[2], a[3], a[4], a[5]); +} + +/* ---- timerfds ---- */ +static int ft_u_timerfd_settime(int fd, int flags, const struct itimerspec *nv, struct itimerspec *ov, bool *handled) +{ + struct ft_clock_page s; + struct ft_timer *t = NULL; + struct itimerspec prev; + sigset_t old; + int clock, r; + bool was = false; + *handled = false; + if (fd < 0 || fd >= FT_TFD_CLOCKS || nv == NULL) return 0; + clock = ft_tfd_clock[fd] - 1; + *handled = true; + if (ft_timers_any) + { + ft_tab_lock(&old); + t = ft_timer_find_fd(fd); + if (t != NULL && t->tracked) + { + ft_timer_value(t, &prev); + was = true; + t->tracked = 0; /* the record stays, with the timer's descriptors, while any is open */ + } + ft_tab_unlock(&old); + } + if (!ft_rt_clock(clock) || !(flags & TFD_TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) + || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) + { + r = (*real_timerfd_settime)(fd, flags, nv, ov); + if (r == 0 && ov != NULL && was) *ov = prev; + return r; + } + ft_tab_lock(&old); + if ((t = ft_timer_find_fd(fd)) == NULL && (t = ft_timer_new()) != NULL) + { + t->kind = 1; + t->nfds = 1; + t->fds[0] = fd; + } + if (t == NULL) + { + ft_tab_unlock(&old); + *handled = false; /* table full: the caller's own (translated once) path */ + return 0; + } + t->clock = clock; + t->flags = flags; + t->due = ft_ns(&nv->it_value); + t->interval = ft_ns(&nv->it_interval); + t->tracked = 1; + if (ov != NULL) + { + if (was) + *ov = prev; + else + (*real_timerfd_gettime)(fd, ov); + } + r = ft_timer_arm(t, &s, true); + if (r != 0) t->tracked = 0; + ft_timers_any = 1; + ft_tab_unlock(&old); + if (r == 0) ft_helper_start(); + return r; +} +static int ft_u_timerfd_gettime(int fd, struct itimerspec *cur, bool *handled) +{ + struct ft_timer *t; + sigset_t old; + *handled = false; + if (!ft_timers_any || cur == NULL) return 0; + ft_tab_lock(&old); + t = ft_timer_find_fd(fd); + if (t != NULL && t->tracked) + { + ft_timer_value(t, cur); + *handled = true; + } + ft_tab_unlock(&old); + return 0; +} + +/* + * ---- descriptors: a timerfd's record follows its file, not a number. close() takes a descriptor off it (the record + * goes with the last), dup() and its kin add the new one, so the helper never arms a number the program has closed or + * that now names another file. A number closed where this can't see (closefrom, a raw system call) is dropped when + * timerfd_create hands it out again; a timer with more duplicates than a record holds is left armed as it was. ---- + */ +static int (*ft_real_close)(int); +static int (*ft_real_dup)(int); +static int (*ft_real_dup2)(int, int); +static int (*ft_real_dup3)(int, int, int); +static int (*ft_real_fcntl)(int, int, ...); +static int (*ft_real_fcntl64)(int, int, ...); +static int (*ft_real_close_range)(unsigned int, unsigned int, int); +#define FT_RESOLVE(name) do { if (ft_real_##name == NULL) ft_real_##name = dlsym(RTLD_NEXT, #name); } while (0) + +static inline bool ft_tfd_known(int fd) +{ + return fd >= 0 && fd < FT_TFD_CLOCKS && __atomic_load_n(&ft_tfd_clock[fd], __ATOMIC_RELAXED) != 0; +} +/* the process whose descriptors these tables describe: set at load and in the child of each glibc fork. A child that + shares this memory without fork (vfork, as Python's subprocess uses, or a CLONE_VM clone) has a descriptor table of + its own, so what it closes or duplicates before exec changes nothing here. A fork's child also takes the tables' + lock free: another thread may have held it at the fork, and no thread of the child would ever release it. */ +static volatile pid_t ft_fd_owner = 0; +static void ft_fd_forked(void) +{ + ft_fd_owner = getpid(); + ft_tab_lock_word = 0; +} +static inline bool ft_fd_mine(void) +{ + return getpid() == ft_fd_owner; +} +/* ``fd`` closed, or about to name another file. Lock held. */ +static void ft_fd_gone_locked(int fd) +{ + struct ft_timer *t; + ft_tfd_clock[fd] = 0; + while ((t = ft_timer_find_fd(fd)) != NULL) + { + int k = 0; + while (t->fds[k] != fd) k++; + t->fds[k] = t->fds[--t->nfds]; + if (t->nfds == 0) t->kind = 0; + } +} +static void ft_fd_gone(int fd) +{ + sigset_t old; + if (!ft_tfd_known(fd) || !ft_fd_mine()) return; + ft_tab_lock(&old); + ft_fd_gone_locked(fd); + ft_tab_unlock(&old); +} +/* ``to`` now names ``from``'s file, a known timerfd. Lock held. */ +static void ft_fd_duped_locked(int from, int to) +{ + struct ft_timer *t; + if (to < 0 || to >= FT_TFD_CLOCKS) return; + ft_tfd_clock[to] = ft_tfd_clock[from]; + if ((t = ft_timer_find_fd(from)) != NULL) + { + if (t->nfds < FT_TIMER_FDS) + t->fds[t->nfds++] = to; + else + t->kind = 0; + } +} +static void ft_fd_duped(int from, int to) +{ + sigset_t old; + if (!ft_tfd_known(from) || !ft_fd_mine()) return; + ft_tab_lock(&old); + ft_fd_duped_locked(from, to); + ft_tab_unlock(&old); +} +/* dup2 or dup3 onto ``fd2``. One that replaces a timerfd (whose close doesn't block) makes the change and its + bookkeeping under the lock, so a failed call keeps the record and the helper never arms the number in between. */ +static int ft_dup_onto(int fd, int fd2, int flags, bool three) +{ + sigset_t old; + int r; + FT_RESOLVE(dup2); + FT_RESOLVE(dup3); + if (fd == fd2 || !ft_tfd_known(fd2) || !ft_fd_mine()) + { + r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); + if (r >= 0 && fd != fd2) ft_fd_duped(fd, r); + return r; + } + ft_tab_lock(&old); + r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); + if (r >= 0) + { + ft_fd_gone_locked(fd2); + if (ft_tfd_known(fd)) ft_fd_duped_locked(fd, fd2); + } + ft_tab_unlock(&old); + return r; +} + +int close(int fd) +{ + ft_fd_gone(fd); + FT_RESOLVE(close); + return (*ft_real_close)(fd); +} +int dup(int fd) +{ + int r; + FT_RESOLVE(dup); + r = (*ft_real_dup)(fd); + if (r >= 0) ft_fd_duped(fd, r); + return r; +} +int dup2(int fd, int fd2) +{ + return ft_dup_onto(fd, fd2, 0, false); +} +int dup3(int fd, int fd2, int flags) +{ + return ft_dup_onto(fd, fd2, flags, true); +} +/* the argument passed on as glibc's own fcntl reads it, a pointer-sized word whatever the command */ +int fcntl(int fd, int cmd, ...) +{ + va_list ap; + void *arg; + int r; + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + FT_RESOLVE(fcntl); + r = (*ft_real_fcntl)(fd, cmd, arg); + if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); + return r; +} +int fcntl64(int fd, int cmd, ...) +{ + va_list ap; + void *arg; + int r; + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + FT_RESOLVE(fcntl64); + r = (*ft_real_fcntl64)(fd, cmd, arg); + if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); + return r; +} +/* one that closes a timerfd runs under the lock, as dup2 does, so a failed call keeps the records. In practice it runs + in a child of fork before exec, where no other thread wants the lock. */ +int close_range(unsigned int first, unsigned int last, int flags) +{ + sigset_t old; + bool timerfd = false; + int r; + FT_RESOLVE(close_range); + if (!(flags & CLOSE_RANGE_CLOEXEC) && ft_fd_mine()) + for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS && !timerfd; fd++) timerfd = ft_tfd_known((int) fd); + if (!timerfd) return (*ft_real_close_range)(first, last, flags); + ft_tab_lock(&old); + r = (*ft_real_close_range)(first, last, flags); + if (r == 0) + for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS; fd++) + if (ft_tfd_known((int) fd)) ft_fd_gone_locked((int) fd); + ft_tab_unlock(&old); + return r; +} +/* the owner, and the real calls resolved at load, so a first close() in a signal handler doesn't call dlsym */ +static void __attribute__((constructor)) ft_fd_init(void) +{ + ft_fd_owner = getpid(); + pthread_atfork(NULL, NULL, ft_fd_forked); + FT_RESOLVE(close); + FT_RESOLVE(dup); + FT_RESOLVE(dup2); + FT_RESOLVE(dup3); + FT_RESOLVE(fcntl); + FT_RESOLVE(fcntl64); + FT_RESOLVE(close_range); +} + +/* ---- POSIX timers (unversioned: upstream's GLIBC_2.2 / 2.3.3 versions don't bind glibc 2.34's timer_settime) ---- */ +int timer_create(clockid_t clock, struct sigevent *sev, timer_t *id) +{ + int r; + ftpl_init(); + if (real_timer_create_new == NULL) real_timer_create_new = dlsym(RTLD_NEXT, "timer_create"); + if (real_timer_create_new == NULL) + { + errno = ENOSYS; + return -1; + } + r = (*real_timer_create_new)(clock, sev, id); + if (r == 0 && id != NULL && ft_rt_clock(clock) && ft_faked()) + { + sigset_t old; + struct ft_timer *t; + ft_tab_lock(&old); + t = ft_timer_find_id(*id); + if (t == NULL) t = ft_timer_new(); + if (t != NULL) + { + memset(t, 0, sizeof *t); + t->kind = 2; + t->id = *id; + t->clock = clock; + } + ft_timers_any = 1; + ft_tab_unlock(&old); + } + return r; +} +int timer_delete(timer_t id) +{ + ftpl_init(); + if (real_timer_delete_new == NULL) real_timer_delete_new = dlsym(RTLD_NEXT, "timer_delete"); + if (ft_timers_any) + { + sigset_t old; + struct ft_timer *t; + ft_tab_lock(&old); + if ((t = ft_timer_find_id(id)) != NULL) t->kind = 0; + ft_tab_unlock(&old); + } + return (*real_timer_delete_new)(id); +} +int timer_settime(timer_t id, int flags, const struct itimerspec *nv, struct itimerspec *ov) +{ + struct ft_clock_page s; + struct ft_timer *t; + struct itimerspec prev; + sigset_t old; + bool was = false; + int r; + ftpl_init(); + if (real_timer_settime_new == NULL) real_timer_settime_new = dlsym(RTLD_NEXT, "timer_settime"); + if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); + if (!ft_timers_any || nv == NULL) return (*real_timer_settime_new)(id, flags, nv, ov); + ft_tab_lock(&old); + t = ft_timer_find_id(id); + if (t != NULL && t->tracked) + { + ft_timer_value(t, &prev); + was = true; + t->tracked = 0; + } + ft_tab_unlock(&old); + if (t == NULL || !ft_uw_active() || !(flags & TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) + || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) + { + r = (*real_timer_settime_new)(id, flags, nv, ov); + if (r == 0 && ov != NULL && was) *ov = prev; + return r; + } + ft_tab_lock(&old); + if (ov != NULL) + { + if (was) + *ov = prev; + else + (*real_timer_gettime_new)(id, ov); + } + t->due = ft_ns(&nv->it_value); + t->interval = ft_ns(&nv->it_interval); + t->tracked = 1; + r = ft_timer_arm(t, &s, true); + ft_tab_unlock(&old); + if (r == 0) ft_helper_start(); + return r; +} +int timer_gettime(timer_t id, struct itimerspec *cur) +{ + sigset_t old; + struct ft_timer *t; + ftpl_init(); + if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); + if (ft_timers_any && cur != NULL) + { + ft_tab_lock(&old); + t = ft_timer_find_id(id); + if (t != NULL && t->tracked) + { + ft_timer_value(t, cur); + ft_tab_unlock(&old); + return 0; + } + ft_tab_unlock(&old); + } + return (*real_timer_gettime_new)(id, cur); +} + diff --git a/src/agent_env/agents/agents/claude-code/clock/build.sh b/src/agent_env/agents/agents/claude-code/clock/build.sh new file mode 100755 index 000000000..8d7227284 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/build.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Builds agent-env's libfaketime: upstream 0.9.12 from its sha256-pinned tarball, libfaketime.patch applied and the two +# engine files beside it copied in, the multithreaded library with stat and utime faking compiled out (file times stay +# real); and the TLS shim beside it (tls_realtime.c). Writes OUT/libfaketimeMT.so.1 and OUT/libtls_realtime.so, which +# /etc/ld.so.preload lists in that order. Needs a C toolchain, make, patch and curl; builds on x86_64 and aarch64. +# build.sh OUT [TARBALL] (TARBALL: a local copy of upstream's v0.9.12 tarball, else it is downloaded) +set -eu +out=$1 +here=$(cd "$(dirname "$0")" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +tarball=${2:-$work/libfaketime.tar.gz} +[ -f "$tarball" ] || curl -fsSL -o "$tarball" https://github.com/wolfcw/libfaketime/archive/refs/tags/v0.9.12.tar.gz +echo "4fc32218697c052adcdc5ee395581f2554ca56d086ac817ced2be0d6f1f8a9fa $tarball" | sha256sum -c - >/dev/null +tar xzf "$tarball" -C "$work" +src=$work/libfaketime-0.9.12 +patch -s -d "$src" -p1 < "$here/libfaketime.patch" +cp "$here/agentenv_clock_page.c" "$here/agentenv_clock_waits.c" "$src/src/" +make -s -C "$src/src" FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME" libfaketimeMT.so.1 +mkdir -p "$out" +cp "$src/src/libfaketimeMT.so.1" "$out/" +${CC:-cc} -std=gnu99 -O2 -fPIC -shared -Wall -Wextra -Werror -o "$out/libtls_realtime.so" "$here/tls_realtime.c" -ldl diff --git a/src/agent_env/agents/agents/claude-code/clock/clock-follower b/src/agent_env/agents/agents/claude-code/clock/clock-follower new file mode 100755 index 000000000..652431687 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clock-follower @@ -0,0 +1,204 @@ +#!/usr/bin/python3 -I +"""The computer's clock follower: holds a long poll on the gateway's ``/clock/watch`` and writes each state it learns +to the clock page (``clockpage.py``), which agent-env's libfaketime reads in every session program, so the session reads +the universe's time. It runs as its own user, under root's supervisord, so the session user can read the clock but +neither write it nor stop it. + +Core writes its config, the gateway's watch URL as this computer reaches it, the computer's name and its read token, +through the sandbox's root channel (``CONFIG``, readable by root and this user alone); never through the driver, which +runs as the session user. It reads the config every quarter second, during a watch too, and follows a changed one at +once. Until there is one, the clock stays as root made it at boot: real time. It reports how it's doing in ``STATUS``, +which anyone may read and which holds no token: the gateway it follows, whether it's connected, the version it shows, +or its last error. + +The gateway's clock is read against this machine's: its offset is four-stamp, ``((received - t1) + (sent - t4)) / 2``, +from the newest of the last 8 answers whose round trip, less the time the gateway held the watch, isn't an outlier. A +state is written anchored, universe V at this machine's real time A (the gateway's ``sent``) at rate R, so every reader +computes V + (now - A) * R. Each watch names the version it shows, which is its acknowledgement of that version.""" +import json +import os +import queue +import random +import sys +import threading +import time +import urllib.error +import urllib.parse +import urllib.request +from datetime import datetime + +sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) +import clockpage # noqa: E402 + +CONFIG = os.environ.get("AGENTENV_CLOCK_CONFIG", "/etc/agentenv/clock/follower.json") +STATUS = os.environ.get("AGENTENV_CLOCK_STATUS", "/run/agentenv-clock/status.json") +#: How long a watch may take: the gateway holds one up to 25 s (its card's ``hold_s``), plus a margin. +WATCH_TIMEOUT_S = 35.0 +#: How often the config is read, during a watch too. +CONFIG_POLL_S = 0.25 +#: A re-anchoring (the offset moved) is written only past this much, so the page doesn't churn on noise. +ADJUST_S = 0.001 +SAMPLES = 8 +#: After a failed watch it waits before asking again: RETRY_FIRST_S at first, doubled after each failure up to +#: RETRY_MAX_S, each wait a random fraction (RETRY_JITTER) of that, so computers that lost a gateway don't all return at +#: once. +RETRY_FIRST_S = 0.05 +RETRY_MAX_S = 2.0 +RETRY_JITTER = (0.5, 1.0) +OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) + + +def log(event: str, **fields) -> None: + sys.stdout.write(json.dumps({"event": event, "real": time.time(), **fields}) + "\n") + sys.stdout.flush() + + +def atomic_write(path: str, text: str) -> None: + tmp = f"{path}.{os.getpid()}.tmp" + with open(tmp, "w") as f: + f.write(text) + os.chmod(tmp, 0o644) + os.replace(tmp, path) + + +def read_config() -> tuple[bytes | None, dict | None]: + """The config's bytes and what they say, (None, None) when there is none, (bytes, None) when it isn't one.""" + try: + with open(CONFIG, "rb") as f: + raw = f.read() + except OSError: + return None, None + try: + config = json.loads(raw) + return raw, config if isinstance(config, dict) and all( + isinstance(config.get(key), str) and config[key] for key in ("watch_url", "name", "token")) else None + except ValueError: + return raw, None + + +def parse_time(text: str) -> float: + return datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() + + +def offset(samples: list[dict]) -> tuple[float, int]: + """The offset from the newest sample whose delay isn't an outlier (over twice the median and 2 ms), and how many + were dropped.""" + delays = sorted(sample["delay"] for sample in samples) + median = delays[len(delays) // 2] + kept = [sample for sample in samples if sample["delay"] <= max(2 * median, median + 0.002)] + return (kept or samples)[-1]["theta"], len(samples) - len(kept) + + +def state_of(body: dict, theta: float) -> tuple[int, float, float, float]: + """(armed, universe at the anchor, rate, anchor on this machine's clock) for a watch's answer ``body``.""" + if not body.get("armed"): + return 0, 0.0, 1.0, time.time() + return (1, parse_time(body["virtual_time"]), float(body["virtual_seconds_per_real_second"]), + body["sent"] - theta) + + +def watch(url: str, name: str, token: str, after: str, out: queue.Queue) -> None: + """One watch, its answer (or what failed) put on ``out`` with the stamps around it.""" + t1 = time.time() + try: + request = urllib.request.Request( + f"{url}?{urllib.parse.urlencode({'after': after, 'follower': name})}", + headers={"Authorization": f"Bearer {token}"}) + with OPENER.open(request, timeout=WATCH_TIMEOUT_S) as answer: + body = json.loads(answer.read()) + out.put((t1, time.time(), body, None)) + except Exception as e: # noqa: BLE001 - away, restarting or refusing: the follower says so and asks again + out.put((t1, time.time(), None, e)) + + +class Follower: + """Follows one config until it changes.""" + + def __init__(self, raw: bytes, config: dict): + self.raw, self.url, self.name, self.token = raw, config["watch_url"], config["name"], config["token"] + self.after, self.samples, self.written = "", [], None + self.errors, self.backoff = 0, RETRY_FIRST_S + self.status = {"url": self.url, "name": self.name, "connected": False, "version": None, "armed": False, + "error": None} + + def report(self, **fields) -> None: + self.status.update(fields) + atomic_write(STATUS, json.dumps(self.status)) + + def run(self) -> None: + log("following", url=self.url, name=self.name) + self.report() + while True: + answers: queue.Queue = queue.Queue() + threading.Thread(target=watch, args=(self.url, self.name, self.token, self.after, answers), + daemon=True).start() + while True: + try: + t1, t4, body, error = answers.get(timeout=CONFIG_POLL_S) + break + except queue.Empty: + if read_config()[0] != self.raw: + return # its watch is left to end on its own; nothing it answers is applied + if error is not None: + self.failed(error) + continue + if read_config()[0] != self.raw: + return + try: + self.apply(t1, t4, body) + except (KeyError, TypeError, ValueError, AttributeError) as e: + self.failed(ValueError(f"not a clock watch's answer ({type(e).__name__}: {e})")) + + def failed(self, error: Exception) -> None: + self.errors += 1 + code = error.code if isinstance(error, urllib.error.HTTPError) else None + self.report(connected=False, error=f"{type(error).__name__}: {error}"[:300], status=code, errors=self.errors, + error_at=time.time()) + if self.errors <= 20 or self.errors % 100 == 0: + log("error", errors=self.errors, status=code, error=repr(error)[:200], backoff=self.backoff) + deadline = time.monotonic() + self.backoff * random.uniform(*RETRY_JITTER) + while time.monotonic() < deadline and read_config()[0] == self.raw: + time.sleep(min(CONFIG_POLL_S, max(0.0, deadline - time.monotonic()))) + self.backoff = min(self.backoff * 2, RETRY_MAX_S) + + def apply(self, t1: float, t4: float, body: dict) -> None: + if self.errors: + log("reconnected", after_errors=self.errors) + self.errors, self.backoff = 0, RETRY_FIRST_S + held = body["sent"] - body["received"] + self.samples = (self.samples + [{"delay": max(0.0, (t4 - t1) - held), + "theta": ((body["received"] - t1) + (body["sent"] - t4)) / 2}])[-SAMPLES:] + theta, dropped = offset(self.samples) + armed, universe, rate, anchor = state_of(body, theta) + meaning = (armed, rate, universe - rate * anchor if armed else 0.0) + changed = body["version"] != self.after + moved = (self.written is None or meaning[:2] != self.written[:2] + or abs(meaning[2] - self.written[2]) > ADJUST_S) + if changed or moved: + written = clockpage.write(armed, universe, rate, anchor) + self.written = meaning + log("applied" if changed else "adjusted", version=body["version"], armed=bool(armed), + virtual_time=body.get("virtual_time"), spec=written["spec"], theta=theta, held=held, dropped=dropped) + self.after = body["version"] + self.report(connected=True, version=self.after, armed=bool(armed), virtual_time=body.get("virtual_time"), + rate=body.get("virtual_seconds_per_real_second"), theta=theta, applied_at=time.time(), error=None, + status=None, errors=0) + + +def main() -> None: + log("start", pid=os.getpid(), uid=os.getuid()) + atomic_write(STATUS, json.dumps({"connected": False, "error": None, "waiting": "no config yet"})) + while True: + raw, config = read_config() + if config is None: + if raw is not None: + atomic_write(STATUS, json.dumps({"connected": False, "error": "its config isn't one: it needs " + "watch_url, name and token"})) + while read_config()[0] == raw: + time.sleep(CONFIG_POLL_S) + continue + Follower(raw, config).run() + + +if __name__ == "__main__": + main() diff --git a/src/agent_env/agents/agents/claude-code/clock/clock.conf b/src/agent_env/agents/agents/claude-code/clock/clock.conf new file mode 100644 index 000000000..715987d43 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clock.conf @@ -0,0 +1,11 @@ +# Read once by agent-env's libfaketime as each process loads it. While this file exists it is the only source of these +# settings: the environment changes none of them (AGENTENV_REAL_TIME=1 is the one switch a program's environment has). +# Only the session user's processes are faked; the clock comes from the page the follower writes, never from a cache. +AGENTENV_SESSION_UID=1000 +FAKETIME_TIMESTAMP_FILE=/run/agentenv-clock/clock +FAKETIME_NO_CACHE=1 +FAKETIME_DONT_FAKE_MONOTONIC=1 +FAKETIME_FMT=%s +FAKETIME_DISABLE_SHM=1 +AGENTENV_CLOCK_PAGE=/dev/shm/agentenv-clock +AGENTENV_CLOCK_PAGE_UID=0 diff --git a/src/agent_env/agents/agents/claude-code/clock/clockpage.py b/src/agent_env/agents/agents/claude-code/clock/clockpage.py new file mode 100644 index 000000000..4002bb6c2 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clockpage.py @@ -0,0 +1,157 @@ +"""The follower's side of the clock page (version 2) and of the clock file: writes the universe's realtime +clock to both. Elapsed time is real, so the page's monotonic fields are written as the real monotonic clock (identity) +and no library reads them; they stay so the page keeps its 80-byte layout. + + clockpage.py set UNIVERSE RATE [ANCHOR] armed: UNIVERSE (epoch s) at real time ANCHOR (default now), at RATE + clockpage.py clear real time + clockpage.py create OWNER as root at boot: the page, OWNER's from its first byte, unarmed + clockpage.py show + +The page (default /dev/shm/agentenv-clock, a tmpfs, so readers that mapped it see every write) is created once, 0644, +owned by whoever writes it, and rewritten in place: seq odd, the fields and their check, seq even, then FUTEX_WAKE on +seq, which wakes every wait agent-env's libfaketime has for a clock time, and its helper threads, so they plan again. +Readers never see a torn value: they retry (waiting on seq) while seq is odd or changed, or the check doesn't match. +A page a writer died writing (seq left odd) is taken up by the next write, which then writes the universe as given. + +A write that would put the universe less than CONTINUITY_NS behind where the page has it now (a rate change computed +from a universe read a moment before the write) continues it from where it is instead: the write's real time is taken +last, while seq is odd, and the universe at it read from the page being replaced, so no reader of either page ever sees +the clock step back. A larger step back is a set_time and is written as given.""" +import ctypes +import mmap +import os +import platform +import pwd +import stat +import struct +import sys +import time +from decimal import Decimal + +PAGE = os.environ.get("AGENTENV_CLOCK_PAGE", "/dev/shm/agentenv-clock") +FILE = os.environ.get("AGENTENV_CLOCK_FILE", "/run/agentenv-clock/clock") +MAGIC, VERSION = 0x4B434541, 2 +# magic version seq universe_ns anchor_ns rate armed reserved generation check mono_universe_ns mono_anchor_ns +LAYOUT = struct.Struct(" int: + rate_bits = struct.unpack(" bytes: + """The page's bytes for these fields, its check among them: what every write writes and every reader checks, + agent-env's libfaketime and the egress proxy (its egress/testdata pins them).""" + return LAYOUT.pack(MAGIC, VERSION, seq, universe_ns, anchor_ns, rate, armed, 0, generation, + check(seq, universe_ns, anchor_ns, rate, armed, generation, mono_universe_ns, mono_anchor_ns), + mono_universe_ns, mono_anchor_ns) + + +def open_page() -> mmap.mmap: + try: + fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o644) + os.ftruncate(fd, LAYOUT.size) + os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) + except FileExistsError: + fd = os.open(PAGE, os.O_RDWR) + if os.fstat(fd).st_uid != os.getuid(): + raise SystemExit(f"{PAGE} isn't ours") + if os.fstat(fd).st_size < LAYOUT.size: + os.close(fd) + raise SystemExit(f"{PAGE} is an older page") + m = mmap.mmap(fd, LAYOUT.size) + os.close(fd) + return m + + +def _wake(m: mmap.mmap) -> None: + """FUTEX_WAKE every waiter on seq's low word (a shared futex: other processes' read-only mappings are woken too).""" + if _SYS_FUTEX is None: + return + buf = (ctypes.c_char * LAYOUT.size).from_buffer(m) + try: + _libc.syscall(ctypes.c_long(_SYS_FUTEX), ctypes.c_void_p(ctypes.addressof(buf) + 8), ctypes.c_int(_FUTEX_WAKE), + ctypes.c_int(0x7FFFFFFF), None, None, ctypes.c_int(0)) + finally: + del buf + + +def _universe_at(armed, universe_ns, anchor_ns, rate, now_ns) -> int: + if not armed: + return now_ns + if rate == 0 or now_ns <= anchor_ns: + return universe_ns + return universe_ns + int((now_ns - anchor_ns) * rate) + + +def write(armed: int, universe: float, rate: float, anchor: float) -> dict: + m = open_page() + _, _, seq, o_u, o_a, o_rate, o_armed, _, generation, _, _, _ = LAYOUT.unpack(m[:LAYOUT.size]) + universe_ns, anchor_ns = int(Decimal(str(universe)) * 10**9), int(Decimal(str(anchor)) * 10**9) + if not armed: + rate = 1.0 + generation += 1 + dead = seq & 1 # a writer died mid-write: its fields may be torn, so this write goes on from the next even seq + seq += dead + m[8:16] = struct.pack(" 1 and not dead and 0 < old_now - new_now < CONTINUITY_NS + if continued: + universe_ns, anchor_ns = old_now, now_rt + body = encode(seq + 2, universe_ns, anchor_ns, rate, armed, generation, now_mono, now_mono) + m[16:] = body[16:] + m[8:16] = struct.pack(" 1 else 0, + "continued": bool(continued), "universe_ns": universe_ns, "anchor_ns": anchor_ns} + + +def create(owner: str) -> None: + """As root at boot: the page, ``owner``'s from its first byte (unarmed), replacing one anyone else made.""" + user = pwd.getpwnam(owner) + try: + st = os.lstat(PAGE) + if stat.S_ISREG(st.st_mode) and st.st_uid == user.pw_uid and st.st_size == LAYOUT.size: + return + os.unlink(PAGE) + except FileNotFoundError: + pass + fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + try: + os.fchown(fd, user.pw_uid, user.pw_gid) + os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) + os.fchmod(fd, 0o644) + finally: + os.close(fd) + + +if __name__ == "__main__": + cmd = sys.argv[1] + if cmd == "set": + anchor = float(sys.argv[4]) if len(sys.argv) > 4 else time.time() + print(write(1, float(sys.argv[2]), float(sys.argv[3]), anchor)) + elif cmd == "create": + create(sys.argv[2]) + elif cmd == "clear": + print(write(0, 0.0, 1.0, time.time())) + else: + with open(PAGE, "rb") as f: + print(LAYOUT.unpack(f.read(LAYOUT.size))) diff --git a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch new file mode 100644 index 000000000..d26148eed --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch @@ -0,0 +1,1527 @@ +SPDX-License-Identifier: GPL-2.0-only +agent-env's changes to libfaketime 0.9.12 (src/), applied from the tarball's root with patch -p1 by build.sh, which +also copies in agentenv_clock_page.c and agentenv_clock_waits.c, included where marked below. A change to libfaketime, +so licensed as libfaketime is, under the GNU General Public License version 2 (COPYING). + +diff -ruN a/src/libfaketime.c b/src/libfaketime.c +--- a/src/libfaketime.c ++++ b/src/libfaketime.c +@@ -231,11 +231,14 @@ + static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp); + #endif + #ifdef FAKE_PTHREAD +-static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); +-static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); +-static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict); +-static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); +-static pthread_rwlock_t monotonic_conds_lock; ++static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); ++static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); ++static int (*real_pthread_cond_clockwait) (pthread_cond_t *, pthread_mutex_t*, clockid_t, const struct timespec *); ++static int (*real_pthread_mutex_timedlock) (pthread_mutex_t *, const struct timespec *); ++static int (*real_pthread_mutex_clocklock) (pthread_mutex_t *, clockid_t, const struct timespec *); ++#endif ++#ifdef FAKE_TIMERS ++static int (*real_timerfd_create) (int, int); + #endif + + #ifndef __APPLE__ +@@ -334,6 +337,7 @@ + bool str_array_contains(const char *haystack, const char *needle); + void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym); + ++#include "agentenv_clock_page.c" /* agent-env: settings, who is faked, the clock page */ + /** Semaphore protecting shared data */ + static sem_t *shared_sem = NULL; + +@@ -420,6 +424,29 @@ + static bool parse_config_file = true; + + static void ft_cleanup (void) __attribute__ ((destructor)); ++ ++#ifdef PTHREAD_SINGLETHREADED_TIME ++/* agentenv: the lock fake_clock_gettime holds, at file scope so a fork() can't hand the child a held lock. */ ++static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; ++static sigset_t ft_fork_mask; ++static void ft_atfork_prepare(void) ++{ ++ sigset_t all; ++ sigfillset(&all); ++ pthread_sigmask(SIG_SETMASK, &all, &ft_fork_mask); ++ pthread_mutex_lock(&time_mutex); ++} ++static void ft_atfork_parent(void) ++{ ++ pthread_mutex_unlock(&time_mutex); ++ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); ++} ++static void ft_atfork_child(void) ++{ ++ pthread_mutex_init(&time_mutex, NULL); ++ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); ++} ++#endif + static void ftpl_init (void) __attribute__ ((constructor)); + + +@@ -519,7 +546,7 @@ + + static void ft_shm_destroy(void) + { +- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); ++ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); + + if (ft_shared_env != NULL) + { +@@ -553,7 +580,7 @@ + static void ft_shm_really_init (void) + { + int ticks_shm_fd; +- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); ++ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); + sem_t *shared_semR = NULL; + static int nt=1; + +@@ -561,7 +588,7 @@ + if (ft_shared_env == NULL) + { + ft_shm_create(); +- ft_shared_env = getenv("FAKETIME_SHARED"); ++ ft_shared_env = ft_getenv("FAKETIME_SHARED"); + } + + /* check for stale semaphore / shared memory information */ +@@ -575,7 +602,7 @@ + if (SEM_FAILED == (shared_semR = sem_open(sem_name, 0))) /* gone stale? */ + { + ft_shm_create(); +- ft_shared_env = getenv("FAKETIME_SHARED"); ++ ft_shared_env = ft_getenv("FAKETIME_SHARED"); + } + else + { +@@ -631,7 +658,7 @@ + exit(1); + } + } +- if (getenv("FAKETIME_FLSHM") != NULL) ++ if (ft_getenv("FAKETIME_FLSHM") != NULL) + { /* force the deletion of the shm sync env variable */ + unsetenv("FAKETIME_SHARED"); + } +@@ -653,12 +680,6 @@ + sem_close(shared_sem); + shared_sem = NULL; + } +-#ifdef FAKE_PTHREAD +- if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) { +- fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n"); +- exit(-1); +- } +-#endif + if (shmCreator == true) ft_shm_destroy(); + } + +@@ -672,8 +693,8 @@ + static void get_fake_monotonic_setting(int* current_value) + { + char *tmp_env; +- if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL +- || (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL ++ || (tmp_env = ft_getenv("DONT_FAKE_MONOTONIC")) != NULL) + { + if (0 == strcmp(tmp_env, "1")) + { +@@ -1140,7 +1161,7 @@ + #endif /* ifndef __APPLE__ */ + #endif /* ifdef FAKE_STAT */ + +-#ifdef STATX_TYPE ++#if defined STATX_TYPE && defined FAKE_STAT + static inline void fake_statx_timestamp(struct statx_timestamp* p) + { + struct timespec t = {p->tv_sec,p->tv_nsec}; +@@ -1329,6 +1350,7 @@ + * Contributed by Balint Reczey in v0.9.5 + */ + ++#include "agentenv_clock_waits.c" /* agent-env: universe-time reads and waits */ + #ifdef FAKE_SLEEP + /* + * Faked nanosleep() +@@ -1343,6 +1365,7 @@ + struct timespec real_req; + + ftpl_init(); ++ if (real_nanosleep != NULL) return (*real_nanosleep)(req, rem); + if (real_nanosleep == NULL) + { + return -1; +@@ -1395,13 +1418,39 @@ + struct timespec real_req; + + ftpl_init(); ++ if (real_clock_nanosleep != NULL && (!(flags & TIMER_ABSTIME) || !ft_rt_clock(clock_id) || !ft_faked())) ++ return (*real_clock_nanosleep)(clock_id, flags, req, rem); ++ if (ft_uw_active() && ft_valid_ts(req)) ++ { ++ struct ft_clock_page s; ++ if (ft_snap(&s)) return ft_rt_sleep(&s, ft_ns(req)); ++ } + if (real_clock_nanosleep == NULL) + { + return -1; + } + if (req != NULL) + { +- if (flags & TIMER_ABSTIME) /* sleep until absolute time */ ++ if ((flags & TIMER_ABSTIME) && clock_id == CLOCK_REALTIME && ft_translates_wait(req)) ++ { ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, req, &mono); ++ gen = ft_tl_gen; ++ for (;;) ++ { ++ last = ft_slice_end(&mono, &slice); ++ DONT_FAKE_TIME(result = (*real_clock_nanosleep)(CLOCK_MONOTONIC, TIMER_ABSTIME, &slice, NULL)); ++ if (result != 0) return result; ++ if (!ft_deadline_moved(req, &mono, &gen) && last) return 0; ++ } ++ } ++ if ((flags & TIMER_ABSTIME) && (ft_disabled || dont_fake || clock_id == CLOCK_REALTIME)) ++ { ++ real_req = *req; ++ } ++ else if (flags & TIMER_ABSTIME) /* sleep until absolute time */ + { + struct timespec tdiff, timeadj; + timespecsub(req, &user_faked_time_timespec, &timeadj); +@@ -1482,6 +1531,7 @@ + int result; + + ftpl_init(); ++ if (real_usleep != NULL) return (*real_usleep)(usec); + if (user_rate_set && !dont_fake) + { + struct timespec real_req; +@@ -1531,6 +1581,7 @@ + #endif + { + ftpl_init(); ++ if (real_sleep != NULL) return (*real_sleep)(seconds); + if (user_rate_set && !dont_fake) + { + if (real_nanosleep == NULL) +@@ -1551,7 +1602,7 @@ + else + { + int result; +- struct timespec real_req = {seconds, 0}, rem; ++ struct timespec real_req = {seconds, 0}, rem = {0, 0}; + timespecmul(&real_req, 1.0 / user_rate, &real_req); + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem)); +@@ -1604,6 +1655,8 @@ + { + return -1; + } ++ (void) seconds_real; ++ return (*real_alarm)(seconds); + + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(ret = (*alarm)(seconds_real)); +@@ -1627,6 +1680,7 @@ + { + return -1; + } ++ return (*real_ppoll)(fds, nfds, timeout_ts, sigmask); + if (timeout_ts != NULL) + { + if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0))) +@@ -1662,6 +1716,7 @@ + { + return -1; + } ++ return (*real_epoll_wait)(epfd, events, maxevents, timeout); + if (user_rate_set && !dont_fake && timeout > 0) + { + real_timeout = (int) timeout * 1.0/user_rate; +@@ -1686,6 +1741,7 @@ + { + return -1; + } ++ return (*real_epoll_pwait)(epfd, events, maxevents, timeout, sigmask); + if (user_rate_set && !dont_fake && timeout > 0) + { + real_timeout = (int) timeout * 1.0/user_rate; +@@ -1715,6 +1771,8 @@ + { + return -1; + } ++ (void) timeout_real; ++ return (*real_poll)(fds, nfds, timeout); + + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real)); +@@ -1748,6 +1806,7 @@ + { + return -1; + } ++ return (*real_select)(nfds, readfds, writefds, errorfds, timeout); + + if (timeout != NULL) + { +@@ -1812,6 +1871,7 @@ + { + return -1; + } ++ return (*real_pselect)(nfds, readfds, writefds, errorfds, timeout, sigmask); + + if (timeout != NULL) + { +@@ -1831,98 +1891,63 @@ + } + #endif + +-int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) ++static int ft_sem_wait_until(sem_t *sem, const struct timespec *fake_abs) + { +- int result; +- struct timespec real_abs_timeout, *real_abs_timeout_pt; +- +- /* sanity check */ +- if (abs_timeout == NULL) +- { +- return -1; +- } +- +- if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; +- +- if (!dont_fake) +- { +- struct timespec tdiff, timeadj; +- +- timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff); +- +- if (user_rate_set) ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ int result; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); ++ gen = ft_tl_gen; ++ for (;;) ++ { ++ last = ft_slice_end(&mono, &slice); ++ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, CLOCK_MONOTONIC, &slice)); ++ if (result == 0 || errno != ETIMEDOUT) return result; ++ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) + { +- timespecmul(&tdiff, user_rate, &timeadj); ++ errno = ETIMEDOUT; ++ return -1; + } +- else +- { +- timeadj = tdiff; +- } +- timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout); +- real_abs_timeout_pt = &real_abs_timeout; +- } +- else +- { +- /* cast away constness */ +- real_abs_timeout_pt = (struct timespec *)abs_timeout; + } +- +- DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt)); +- return result; + } + +-/* EXPERIMENTAL */ +-int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) ++int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) + { + int result; +- struct timespec real_abstime, *real_abstime_pt; +- +- if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC)) ++ ftpl_init(); ++ if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; ++ if (ft_uw_active() && ft_translates_wait(abs_timeout)) + { +- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); +- return result; ++ bool handled; ++ result = ft_u_sem_wait(sem, ft_ns(abs_timeout), &handled); ++ if (handled) return result; + } +- +- /* sanity check */ +- if (abstime == NULL) ++ if (!ft_translates_wait(abs_timeout) || real_sem_clockwait == NULL) + { +- return -1; ++ DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, abs_timeout)); ++ return result; + } ++ return ft_sem_wait_until(sem, abs_timeout); ++} + ++int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) ++{ ++ int result; ++ ftpl_init(); + if (!CHECK_MISSING_REAL(sem_clockwait)) return -1; +- +- if (!dont_fake) ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) + { +- struct timespec tdiff, timeadj; +- +- timespecsub(abstime, &user_faked_time_timespec, &tdiff); +- +- if (user_rate_set) +- { +- timespecmul(&tdiff, 1.0 / user_rate, &timeadj); +- } +- else +- { +- timeadj = tdiff; +- } +- if (clockid == CLOCK_REALTIME) +- { +- timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime); +- } +- if (clockid == CLOCK_MONOTONIC) +- { +- timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime); +- } +- real_abstime_pt = &real_abstime; ++ bool handled; ++ result = ft_u_sem_wait(sem, ft_ns(abstime), &handled); ++ if (handled) return result; + } +- else ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) + { +- /* cast away constness */ +- real_abstime_pt = (struct timespec *)abstime; ++ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); ++ return result; + } +- +- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt)); +- return result; ++ return ft_sem_wait_until(sem, abstime); + } + #endif + +@@ -1949,6 +1974,23 @@ + * Faked timer_settime() + * Does not affect timer speed when stepping clock with each time() call. + */ ++#ifdef __linux__ ++/* agentenv: the clock each timerfd was made on (ft_tfd_clock); a record still naming the number is stale. */ ++int timerfd_create(int clockid, int flags) ++{ ++ int fd; ++ ftpl_init(); ++ if (real_timerfd_create == NULL) return -1; ++ fd = real_timerfd_create(clockid, flags); ++ if (fd >= 0 && fd < FT_TFD_CLOCKS && ft_fd_mine()) ++ { ++ ft_fd_gone(fd); ++ ft_tfd_clock[fd] = clockid + 1; ++ } ++ return fd; ++} ++#endif ++ + static int + timer_settime_common(timer_t_or_int timerid, int flags, + const struct itimerspec *new_value, +@@ -1977,18 +2019,11 @@ + { + if (flags & abstime_flag) + { +- struct timespec tdiff, timeadj; +- timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj); +- if (user_rate_set) +- { +- timespecmul(&timeadj, 1.0/user_rate, &tdiff); +- } ++ if (ft_disabled || (compat == FT_FD && ft_tfd_known(timerid.int_member) ++ && !ft_rt_clock(ft_tfd_clock[timerid.int_member] - 1))) ++ new_real.it_value = new_value->it_value; + else +- { +- tdiff = timeadj; +- } +- /* only CLOCK_REALTIME is handled */ +- timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value); ++ ft_deadline_from_fake(CLOCK_REALTIME, &new_value->it_value, &new_real.it_value); + } + else + { +@@ -2061,47 +2096,6 @@ + return result; + } + +-/* +- * Faked timer_settime() compatible with implementation in GLIBC 2.2 +- */ +-int timer_settime_22(int timerid, int flags, +- const struct itimerspec *new_value, +- struct itimerspec *old_value) +-{ +- ftpl_init(); +- if (real_timer_settime_22 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.int_member = timerid; +- return (timer_settime_common(temp, flags, new_value, old_value, +- FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME)); +- } +-} +- +-/* +- * Faked timer_settime() compatible with implementation in GLIBC 2.3.3 +- */ +-int timer_settime_233(timer_t timerid, int flags, +- const struct itimerspec *new_value, +- struct itimerspec *old_value) +-{ +- ftpl_init(); +- if (real_timer_settime_233 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_settime_common(temp, flags, new_value, old_value, +- FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME)); +- } +-} + + /* + * Faked timer_gettime() +@@ -2151,48 +2145,6 @@ + return result; + } + +-/* +- * Faked timer_gettime() compatible with implementation in GLIBC 2.2 +- */ +-int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value) +-{ +- ftpl_init(); +- if (real_timer_gettime_22 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_gettime_common(temp, curr_value, +- FT_COMPAT_GLIBC_2_2)); +- } +-} +- +-/* +- * Faked timer_gettime() compatible with implementation in GLIBC 2.3.3 +- */ +-int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value) +-{ +- ftpl_init(); +- if (real_timer_gettime_233 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_gettime_common(temp, curr_value, +- FT_COMPAT_GLIBC_2_3_3)); +- } +-} +- +-__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2"); +-__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3"); +-__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2"); +-__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3"); + + #ifdef __linux__ + /* +@@ -2203,6 +2155,14 @@ + struct itimerspec *old_value) + { + ftpl_init(); ++ if (real_timerfd_settime != NULL && ft_faked()) ++ { ++ bool handled = false; ++ int r = ft_uw_active() ? ft_u_timerfd_settime(fd, flags, new_value, old_value, &handled) : 0; ++ if (handled) return r; ++ if (!(flags & TFD_TIMER_ABSTIME) || fd < 0 || fd >= FT_TFD_CLOCKS || !ft_rt_clock(ft_tfd_clock[fd] - 1)) ++ return (*real_timerfd_settime)(fd, flags, new_value, old_value); ++ } + if (real_timerfd_settime == NULL) + { + return -1; +@@ -2222,6 +2182,13 @@ + int timerfd_gettime(int fd, struct itimerspec *curr_value) + { + ftpl_init(); ++ if (real_timerfd_gettime != NULL && ft_faked()) ++ { ++ bool handled = false; ++ int r = ft_u_timerfd_gettime(fd, curr_value, &handled); ++ if (handled) return r; ++ return (*real_timerfd_gettime)(fd, curr_value); ++ } + if (real_timerfd_gettime == NULL) + { + return -1; +@@ -2379,6 +2346,8 @@ + #endif + if (result == -1) return result; /* original function failed */ + ++ if (!ft_universe_clock(clk_id)) return result; ++ + /* pass the real current time to our faking version, overwriting it */ + if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW + #ifdef CLOCK_MONOTONIC_COARSE +@@ -2495,12 +2464,25 @@ + return; + } + ++ /* agentenv: a specification without "x" is at rate 1, not at the rate of the one before it */ ++ double prev_rate = user_rate; ++ bool prev_rate_set = user_rate_set; ++ if (user_faked_time[0] == 0) return; ++ user_rate = 1.0; ++ user_rate_set = false; ++ + /* check whether the user gave us an absolute time to fake */ + switch (user_faked_time[0]) + { + + default: /* Try and interpret this as a specified time */ + if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE; ++ if (strcmp(user_faked_time_fmt, "%s") == 0) ++ { ++ if (ft_parse_epoch(user_faked_time, &user_faked_time_timespec) == NULL) goto parse_failed; ++ user_faked_time_set = true; ++ goto parse_modifiers; ++ } + user_faked_time_tm.tm_isdst = -1; + nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); + +@@ -2518,9 +2500,7 @@ + } + else + { +- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n" +- "Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt); +- exit(EXIT_FAILURE); ++ goto parse_failed; + } + goto parse_modifiers; + break; +@@ -2548,6 +2528,11 @@ + /* Contributed by David North, TDI in version 0.7 */ + case '@': /* Specific time, but clock along relative to that starttime */ + ft_mode = FT_START_AT; ++ if (strcmp(user_faked_time_fmt, "%s") == 0) ++ { ++ if (ft_parse_epoch(&user_faked_time[1], &user_faked_time_timespec) == NULL) goto parse_failed; ++ goto parse_anchor; ++ } + user_faked_time_tm.tm_isdst = -1; + nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm); + if (NULL != nstime_str) +@@ -2563,12 +2548,17 @@ + } + else + { +- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"); +- exit(EXIT_FAILURE); ++ goto parse_failed; + } + +- /* Reset starttime */ +- if (NULL == getenv("FAKETIME_DONT_RESET")) ++parse_anchor: ++ /* agentenv: "^" is the real time at which the clock read the time given, so every process reads the ++ same time whenever it started, with no shared memory; without it, the process's own start, as before. */ ++ if (strchr(user_faked_time, '^') != NULL) ++ { ++ if (ft_parse_epoch(strchr(user_faked_time, '^') + 1, &ftpl_starttime.real) == NULL) goto parse_failed; ++ } ++ else if (NULL == ft_getenv("FAKETIME_DONT_RESET")) + reset_time(); + goto parse_modifiers; + break; +@@ -2577,17 +2567,17 @@ + ft_mode = FT_START_AT; + struct stat master_file_stats; + int ret; +- if (NULL == getenv("FAKETIME_FOLLOW_FILE")) ++ if (NULL == ft_getenv("FAKETIME_FOLLOW_FILE")) + { + fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n"); + exit(1); + } + else + { +- DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); ++ DONT_FAKE_TIME(ret = stat(ft_getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); + if (ret == -1) + { +- fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE")); ++ fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", ft_getenv("FAKETIME_FOLLOW_FILE")); + exit(1); + } + else +@@ -2596,7 +2586,7 @@ + user_faked_time_timespec.tv_nsec = 0; + } + } +- if (NULL == getenv("FAKETIME_DONT_RESET")) ++ if (NULL == ft_getenv("FAKETIME_DONT_RESET")) + reset_time(); + goto parse_modifiers; + break; +@@ -2609,7 +2599,15 @@ + { + user_rate = atof(strchr(user_faked_time, 'x')+1); + user_rate_set = true; +- if (NULL != getenv("FAKETIME_XRESET")) { ++ if (user_rate <= 0.0) ++ { ++ /* agentenv: rate 0 is the time given, frozen; sleeps and waits then take the real time they ask for */ ++ ft_mode = FT_FREEZE; ++ user_faked_time_set = true; ++ user_rate = 1.0; ++ user_rate_set = false; ++ } ++ if (NULL != ft_getenv("FAKETIME_XRESET")) { + if (ftpl_timecache.real.tv_nsec >= 0) { + user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec; + user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec; +@@ -2639,9 +2637,15 @@ + + strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1); + user_faked_time_saved[BUFFERLEN-1] = 0; ++ ft_spec_gen++; + #ifdef DEBUG + fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved); + #endif ++ return; ++ ++parse_failed: ++ user_rate = prev_rate; ++ user_rate_set = prev_rate_set; + } + + +@@ -2666,8 +2670,8 @@ + const char *progname = __progname; + #endif + +- char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS"); +- bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM"); ++ char *ignore_list = ft_getenv("FAKETIME_IGNORE_SYMBOLS"); ++ bool should_debug_dlsym = ft_getenv("FAKETIME_DEBUG_DLSYM"); + #define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym) + #define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym) + +@@ -2749,32 +2753,25 @@ + #endif + + #ifdef FAKE_PTHREAD +- + #ifdef __GLIBC__ + real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5"); +- + real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2"); +- real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2"); +- real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2"); ++ real_pthread_cond_clockwait = dlvsym(RTLD_NEXT, "pthread_cond_clockwait", "GLIBC_2.30"); ++ real_pthread_mutex_clocklock = dlvsym(RTLD_NEXT, "pthread_mutex_clocklock", "GLIBC_2.30"); + #endif +- + if (NULL == real_pthread_cond_timedwait_232) + { +- real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); ++ real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); + } +- if (NULL == real_pthread_cond_init_232) ++ if (NULL == real_pthread_cond_clockwait) + { +- real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init"); ++ real_pthread_cond_clockwait = dlsym(RTLD_NEXT, "pthread_cond_clockwait"); + } +- if (NULL == real_pthread_cond_destroy_232) ++ if (NULL == real_pthread_mutex_clocklock) + { +- real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy"); +- } +- +- if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) { +- fprintf(stderr,"monotonic_conds_lock init failed\n"); +- exit(-1); ++ real_pthread_mutex_clocklock = dlsym(RTLD_NEXT, "pthread_mutex_clocklock"); + } ++ real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); + #endif + #ifdef __APPLE__ + real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); +@@ -2814,6 +2811,7 @@ + #endif + #ifdef __linux__ + real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime"); ++ real_timerfd_create = dlsym(RTLD_NEXT, "timerfd_create"); + real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime"); + #endif + #endif +@@ -2826,16 +2824,42 @@ + #undef dlsym + #undef dlvsym + ++ /* agentenv: a process of another user than the session's, or one started with AGENTENV_REAL_TIME=1, stays on real ++ time; the marker is taken out of its environment, so what it starts is faked. */ ++ { ++ const char *uid = ft_getenv("AGENTENV_SESSION_UID"); ++ const char *marker = getenv("AGENTENV_REAL_TIME"); ++ if (uid != NULL && *uid != 0 && (uid_t) strtoul(uid, NULL, 10) != getuid()) ++ ft_disabled = true; ++ if (marker != NULL && strcmp(marker, "1") == 0) ++ { ++ ft_disabled = true; ++ ft_scrub_marker(); ++ } ++ if ((tmp_env = ft_getenv("AGENTENV_WAIT_SLICE_MS")) != NULL && atol(tmp_env) > 0) ++ ft_wait_slice_ns = atol(tmp_env) * 1000000L; ++ } ++ if (ft_disabled) ++ { ++ ft_mode = FT_NOOP; ++ dont_fake = dont_fake_final; ++ return; ++ } ++#ifdef PTHREAD_SINGLETHREADED_TIME ++ pthread_atfork(ft_atfork_prepare, ft_atfork_parent, ft_atfork_child); ++#endif ++ ft_page_open(); ++ + #ifdef FAKE_STATELESS + if (0) ft_shm_init(); + #else +- tmp_env = getenv("FAKETIME_DISABLE_SHM"); ++ tmp_env = ft_getenv("FAKETIME_DISABLE_SHM"); + if (!tmp_env || tmp_env[0] == '0') { + ft_shm_init(); + } + #endif + #ifdef FAKE_STAT +- if (getenv("NO_FAKE_STAT")!=NULL) ++ if (ft_getenv("NO_FAKE_STAT")!=NULL) + { + fake_stat_disabled = 1; //Note that this is NOT re-checked + } +@@ -2843,7 +2867,7 @@ + #if defined FAKE_FILE_TIMESTAMPS + #ifdef FAKE_UTIME + // fake_utime_disabled is 0 by default +- if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked ++ if ((tmp_env = ft_getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked + { + if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T') + { /* an empty string or a yes/true value turns off disabling */ +@@ -2860,11 +2884,11 @@ + #endif + #endif + +- if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_CACHE_DURATION")) != NULL) + { + cache_duration = atoi(tmp_env); + } +- if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_NO_CACHE")) != NULL) + { + if (0 == strcmp(tmp_env, "1")) + { +@@ -2875,7 +2899,7 @@ + /* Check whether we actually should be faking the returned timestamp. */ + + /* We can prevent faking time for specified commands */ +- if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SKIP_CMDS")) != NULL) + { + char *skip_cmd, *saveptr, *tmpvar; + /* Don't mess with the env variable directly. */ +@@ -2904,12 +2928,12 @@ + } + + /* We can limit faking time to specified commands */ +- if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_ONLY_CMDS")) != NULL) + { + char *only_cmd, *saveptr, *tmpvar; + bool cmd_matched = false; + +- if (getenv("FAKETIME_SKIP_CMDS") != NULL) ++ if (ft_getenv("FAKETIME_SKIP_CMDS") != NULL) + { + fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n"); + exit(EXIT_FAILURE); +@@ -2941,44 +2965,44 @@ + } + } + +- if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) + { + ft_start_after_secs = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) + { + ft_stop_after_secs = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) + { + ft_start_after_ncalls = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) + { + ft_stop_after_ncalls = atol(tmp_env); + limited_faking = true; + } + + /* check whether we should spawn an external command */ +- if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_TARGET")) != NULL) + { + spawnsupport = true; +- (void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); ++ (void) strncpy(ft_spawn_target, ft_getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); + ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0; +- if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_SECONDS")) != NULL) + { + ft_spawn_secs = atol(tmp_env); + } +- if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) + { + ft_spawn_ncalls = atol(tmp_env); + } + } + +- if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SAVE_FILE")) != NULL) + { + if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT, + S_IWUSR | S_IRUSR))) +@@ -2989,7 +3013,7 @@ + } + + /* load file only if reading timestamps from it is not finished yet */ +- if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_LOAD_FILE")) != NULL) + { + int infile = -1; + struct stat sb; +@@ -3021,7 +3045,7 @@ + infile_set = true; + } + +- tmp_env = getenv("FAKETIME_FMT"); ++ tmp_env = ft_getenv("FAKETIME_FMT"); + if (tmp_env == NULL) + { + strcpy(user_faked_time_fmt, "%Y-%m-%d %T"); +@@ -3061,7 +3085,7 @@ + system_time_from_system(&ftpl_starttime); + } + /* fake time supplied as environment variable? */ +- if (NULL != (tmp_env = getenv("FAKETIME"))) ++ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) + { + parse_config_file = false; + parse_ft_string(tmp_env); +@@ -3070,6 +3094,11 @@ + { + read_config_file(); + } ++ if (ft_page != NULL) ++ { ++ struct ft_clock_page s; ++ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); ++ } + + dont_fake = dont_fake_final; + } +@@ -3209,8 +3238,8 @@ + * a system-wide /etc/faketimerc present. + * The /etc/faketimerc handling has been contributed by David Burley, + * Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */ +- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); +- (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME")); ++ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); ++ (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", ft_getenv("HOME")); + if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 || + (faketimerc = open(filename, O_RDONLY)) != -1 || + (faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1) +@@ -3248,6 +3277,48 @@ + tp_save.tv_nsec = tp->tv_nsec; + + if (dont_fake) return 0; ++ if (ft_disabled) return 0; ++ if (ft_page != NULL && (clk_id == CLOCK_REALTIME || clk_id == CLOCK_REALTIME_COARSE)) ++ { ++ struct ft_clock_page s; ++ if (ft_snap_at(clk_id, &s, tp)) ++ { ++ if (s.generation != __atomic_load_n(&ft_page_applied, __ATOMIC_ACQUIRE)) ++ { ++#ifdef PTHREAD_SINGLETHREADED_TIME ++ sigset_t all, old; ++ sigfillset(&all); ++ pthread_sigmask(SIG_SETMASK, &all, &old); ++ pthread_mutex_lock(&time_mutex); ++ ft_page_apply_locked(&s); ++ pthread_mutex_unlock(&time_mutex); ++ pthread_sigmask(SIG_SETMASK, &old, NULL); ++#else ++ ft_page_apply_locked(&s); ++#endif ++ } ++ ft_tl_gen = s.generation; ++ ft_tl_scale = (s.armed && s.rate > 0) ? 1.0 / s.rate : 1.0; ++ if (s.armed) ++ { ++ int64_t now = (int64_t) tp->tv_sec * 1000000000LL + tp->tv_nsec, fake; ++ if (s.rate == 0 || now <= s.anchor_ns) ++ fake = s.universe_ns; ++ else if (s.rate == 1.0) ++ fake = s.universe_ns + (now - s.anchor_ns); ++ else ++ fake = s.universe_ns + (int64_t) ((double) (now - s.anchor_ns) * s.rate); ++ tp->tv_sec = fake / 1000000000LL; ++ tp->tv_nsec = fake % 1000000000LL; ++ if (tp->tv_nsec < 0) ++ { ++ tp->tv_sec -= 1; ++ tp->tv_nsec += 1000000000LL; ++ } ++ } ++ return 0; ++ } ++ } + /* Per process timers are only sped up or slowed down */ + if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID)) + { +@@ -3262,7 +3333,6 @@ + volatile int ret = INT_MAX; + + #ifdef PTHREAD_SINGLETHREADED_TIME +- static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; + + // block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation. + sigset_t all_signals, original_mask; +@@ -3367,6 +3437,19 @@ + force_cache_expiration = 0; + } + ++ /* agentenv: with the clock page mapped, the file isn't read; until it is, look for it once a second */ ++ if (ft_page == NULL && ft_getenv("AGENTENV_CLOCK_PAGE") != NULL && current_ts.tv_sec != ft_page_last_try) ++ { ++ ft_page_last_try = current_ts.tv_sec; ++ ft_page_open(); ++ if (ft_page != NULL) ++ { ++ struct ft_clock_page s; ++ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); ++ } ++ } ++ if (ft_page != NULL) cache_expired = 0; ++ + if (cache_expired == 1) + { + static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */ +@@ -3377,7 +3460,7 @@ + fprintf(stderr, "***************++ Cache expired ++**************\n"); + */ + +- if (NULL != (tmp_env = getenv("FAKETIME"))) ++ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) + { + strncpy(user_faked_time, tmp_env, BUFFERLEN - 1); + user_faked_time[BUFFERLEN - 1] = 0; +@@ -3401,6 +3484,9 @@ + get_fake_monotonic_setting(&fake_monotonic_clock); + } /* cache had expired */ + ++ ft_tl_gen = ft_spec_gen; ++ ft_tl_scale = (ft_mode == FT_START_AT && user_rate_set && user_rate > 0) ? 1.0 / user_rate : 1.0; ++ + if (infile_set) + { + if (load_time(tp)) +@@ -3520,6 +3606,7 @@ + { + struct timespec ts; + int ret; ++ if (ft_disabled) return 0; + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000; + +@@ -3627,6 +3714,8 @@ + DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp)); + if (result == -1) return result; /* original function failed */ + ++ if (!ft_universe_clock(clk_id)) return result; ++ + /* pass the real current time to our faking version, overwriting it */ + if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW + #ifdef CLOCK_MONOTONIC_COARSE +@@ -3723,238 +3812,124 @@ + + #ifdef FAKE_PTHREAD + +-typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread; +- +-struct pthread_cond_monotonic { +- pthread_cond_t *ptr; +- UT_hash_handle hh; +-}; +- +-static struct pthread_cond_monotonic *monotonic_conds = NULL; ++/* ++ * agentenv: a wait with a CLOCK_REALTIME deadline waits on CLOCK_MONOTONIC instead, until ft_deadline_from_fake's ++ * deadline. A condvar's clock is read from glibc's own condvar word (bit 1 of __wrefs, which glibc's waits read, since ++ * glibc 2.25) instead of a table kept by intercepting pthread_cond_init and pthread_cond_destroy; nothing here calls ++ * getenv() or malloc() or takes a lock. A condvar wait isn't cut into slices, since a slice ending would be a wakeup ++ * its caller must recheck, so a jump or a change of rate reaches it only when it ends; a mutex wait is cut. ++ */ ++#define FT_COND_CLOCK_MONOTONIC 2 + +-int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) ++int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) + { +- clockid_t clock_id; +- int result; +- ++ struct timespec mono; + ftpl_init(); +- if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1; +- result = real_pthread_cond_init_232(cond, attr); +- +- if (result != 0 || attr == NULL) +- return result; +- +- pthread_condattr_getclock(attr, &clock_id); +- +- if (clock_id == CLOCK_MONOTONIC) { +- struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic)); +- e->ptr = cond; +- +- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_ADD_PTR(monotonic_conds, ptr, e); +- pthread_rwlock_unlock(&monotonic_conds_lock); ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL ++ && !(__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) ++ { ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- +- return result; ++ if (!ft_translates_wait(abstime) || real_pthread_cond_clockwait == NULL ++ || (__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) ++ return real_pthread_cond_timedwait_232(cond, mutex, abstime); ++ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); ++ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); + } + +-int pthread_cond_destroy_232(pthread_cond_t *cond) ++int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) + { +- struct pthread_cond_monotonic* e; +- ++ struct timespec real; + ftpl_init(); +- +- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_FIND_PTR(monotonic_conds, &cond, e); +- if (e) { +- HASH_DEL(monotonic_conds, e); +- free(e); ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL) ++ { ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- pthread_rwlock_unlock(&monotonic_conds_lock); +- +- return real_pthread_cond_destroy_232(cond); ++ if (!ft_translates_wait(abstime)) ++ return real_pthread_cond_timedwait_225(cond, mutex, abstime); ++ ft_deadline_from_fake(CLOCK_REALTIME, abstime, &real); ++ return real_pthread_cond_timedwait_225(cond, mutex, &real); + } + +-/* +- * Check whether we need a run-time activation of the +- * forced monotonic fix to avoid faked / unfaked timestamp +- * confusion between the application and glibc internals. +- */ +-bool needs_forced_monotonic_fix(char *function_name) +-{ +- bool result = false; +- char *env_var; +-#ifdef __GLIBC__ +- const char *glibc_version_string = gnu_get_libc_version(); ++__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); ++#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER ++__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); ++#else ++__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); + #endif + +- if (function_name == NULL) return false; +- +- /* The forced monotonic fix can be activated by setting an +- * environment variable to 1, or disabled by setting it to 0 */ +- if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL) ++int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, ++ const struct timespec *abstime) ++{ ++ struct timespec mono; ++ ftpl_init(); ++ if (real_pthread_cond_clockwait == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) + { +- if (env_var[0] == '0') +- result = false; +- else +- result = true; ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- else +- { +-#ifdef __GLIBC__ +- /* Here we try to derive the necessity for a forced monotonic fix * +- * based on glibc version. What could possibly go wrong? */ +- +- int glibc_major, glibc_minor; +- sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor); +- +- /* The following decision logic is yet purely based on experiences +- * with pthread_cond_timedwait(). The used boundaries may still be +- * imprecise. */ +- if ( (glibc_major == 2) && +- ((glibc_minor <= 24) || (glibc_minor >= 30)) ) +- { +- result = true; +- } +- else +-#endif +- result = false; // avoid forced monotonic fixes unless really necessary +- } +- +- if (getenv("FAKETIME_DEBUG") != NULL) +-#ifdef __GLIBC__ +- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n", +- function_name, result ? "yes":"no", glibc_version_string); +-#else +- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n", +- function_name, result ? "yes":"no"); +-#endif +- +- return result; ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) ++ return real_pthread_cond_clockwait(cond, mutex, clockid, abstime); ++ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); ++ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); + } + +-int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat) ++static int ft_mutex_lock_until(pthread_mutex_t *mutex, const struct timespec *fake_abs) + { +- struct timespec tp, tdiff_actual, realtime, faketime; +- struct timespec *tf = NULL; +- struct pthread_cond_monotonic* e; +- char *tmp_env; +- int wait_ms; +- clockid_t clk_id; +- int result = 0; +- +- ftpl_init(); +- +- if (abstime != NULL) ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ int result; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); ++ gen = ft_tl_gen; ++ for (;;) + { +- if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire read monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_FIND_PTR(monotonic_conds, &cond, e); +- pthread_rwlock_unlock(&monotonic_conds_lock); +- if (e != NULL) +- clk_id = CLOCK_MONOTONIC; +- else +- clk_id = CLOCK_REALTIME; +- +- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); +- if (result == -1) +- { +- return EINVAL; +- } +- faketime = realtime; +- (void)fake_clock_gettime(clk_id, &faketime); +- +- if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL) +- { +- wait_ms = atol(tmp_env); +- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); +- if (result == -1) +- { +- return EINVAL; +- } +- +- tdiff_actual.tv_sec = wait_ms / 1000; +- tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000; +- timespecadd(&realtime, &tdiff_actual, &tp); +- +- tf = &tp; ++ last = ft_slice_end(&mono, &slice); ++ result = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &slice); ++ if (result == EINVAL && real_pthread_mutex_timedlock != NULL) ++ { /* a PI mutex on a kernel without FUTEX_LOCK_PI2 takes only CLOCK_REALTIME */ ++ struct timespec real; ++ ft_deadline_from_fake(CLOCK_REALTIME, fake_abs, &real); ++ return real_pthread_mutex_timedlock(mutex, &real); + } +- else +- { +- timespecsub(abstime, &faketime, &tp); +- if (user_rate_set) +- { +- timespecmul(&tp, 1.0 / user_rate, &tdiff_actual); +- } +- else +- { +- tdiff_actual = tp; +- } +- } +- +- /* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime +- internally to calculate the appropriate duration for the +- waiting time. This already uses the faked functions, hence, the +- fake time needs to be passed to pthread_cond_timedwait for +- CLOCK_MONOTONIC. */ +-#ifndef __ARM_ARCH +-#ifndef FORCE_MONOTONIC_FIX +- if (clk_id == CLOCK_MONOTONIC) { +- if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) { +- timespecadd(&realtime, &tdiff_actual, &tp); +- } +- else { +- timespecadd(&faketime, &tdiff_actual, &tp); +- } +- } +- else +-#endif +-#endif +- timespecadd(&realtime, &tdiff_actual, &tp); +- +- tf = &tp; +- } +- +- switch (compat) { +- case FT_COMPAT_GLIBC_2_3_2: +- result = real_pthread_cond_timedwait_232(cond, mutex, tf); +- break; +- case FT_COMPAT_GLIBC_2_2_5: +- result = real_pthread_cond_timedwait_225(cond, mutex, tf); +- break; ++ if (result != ETIMEDOUT) return result; ++ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) return ETIMEDOUT; + } +- return result; + } + +-int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) ++int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) + { +- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5); ++ ftpl_init(); ++ if (real_pthread_mutex_timedlock == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_mutex_clocklock != NULL) ++ { ++ bool handled; ++ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); ++ if (handled) return r; ++ } ++ if (!ft_translates_wait(abstime) || real_pthread_mutex_clocklock == NULL) ++ return real_pthread_mutex_timedlock(mutex, abstime); ++ return ft_mutex_lock_until(mutex, abstime); + } + +-int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) ++int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) + { +- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2); ++ ftpl_init(); ++ if (real_pthread_mutex_clocklock == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) ++ { ++ bool handled; ++ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); ++ if (handled) return r; ++ } ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) ++ return real_pthread_mutex_clocklock(mutex, clockid, abstime); ++ return ft_mutex_lock_until(mutex, abstime); + } + +-__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); +-#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER +-__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); +-__asm__(".symver pthread_cond_init_232, pthread_cond_init@@"); +-__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@"); +-#else +-__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); +-__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2"); +-__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2"); +-#endif +- + #endif + + /* +@@ -4005,15 +3980,15 @@ + /* If FAKETIME_TIMESTAMP_FILE was given in environment, + * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it. + * This allows other process instances to share the same time. */ +- if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL) +- && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0') +- && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) +- && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) ++ if ( (ft_getenv("FAKETIME_TIMESTAMP_FILE") != NULL) ++ && (*ft_getenv("FAKETIME_TIMESTAMP_FILE") != '\0') ++ && (ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) ++ && (strcmp(ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) + { + const char *error = NULL; + FILE *envfile; + static char custom_filename[BUFSIZ]; +- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); ++ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); + + if ((envfile = fopen(custom_filename, "wt")) != NULL) + { +@@ -4034,7 +4009,7 @@ + { + fprintf(stderr, "libfaketime: In clock_settime(), failed to " + "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n", +- error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); ++ error, ft_getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); + } + } + +@@ -4125,7 +4100,7 @@ + + /* return 0 if no FAKERANDOM_SEED was seen */ + static int bypass_randomness(void* buf, size_t buflen) { +- char *seedstring = getenv("FAKERANDOM_SEED"); ++ char *seedstring = ft_getenv("FAKERANDOM_SEED"); + char *b = buf; + + if (seedstring != NULL) { +@@ -4169,7 +4144,7 @@ + #else + pid_t getpid() { + #endif +- const char *pidstring = getenv("FAKETIME_FAKEPID"); ++ const char *pidstring = ft_getenv("FAKETIME_FAKEPID"); + if (pidstring != NULL) { + long int pid = strtol(pidstring, NULL, 0); + return (pid_t)(pid); +@@ -4270,7 +4245,7 @@ + va_list ap; + va_start(ap, number); + #ifdef FAKE_RANDOM +- if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) { ++ if (number == __NR_getrandom && ft_getenv("FAKERANDOM_SEED")) { + void *buf; + size_t buflen; + unsigned int flags; +@@ -4282,7 +4257,7 @@ + } + #endif + // static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp); +- if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) { ++ if (number == __NR_clock_gettime && (ft_getenv("FAKETIME") || ft_getenv("FAKETIME_TIMESTAMP_FILE"))) { + clockid_t clk_id; + struct timespec *tp; + clk_id = va_arg(ap, clockid_t); +diff -ruN a/src/libfaketime.map b/src/libfaketime.map +--- a/src/libfaketime.map ++++ b/src/libfaketime.map +@@ -1,21 +1,9 @@ +-GLIBC_2.2 { +- global: +- +- timer_gettime; timer_settime; +- local: timer_settime_*; timer_gettime_*; +-}; +- +-GLIBC_2.3.3 { +- # Changed timer_t. +- timer_gettime; timer_settime; +-} GLIBC_2.2; +- + GLIBC_2.2.5 { + global: pthread_cond_timedwait; + local: pthread_cond_timedwait_*; pthread_cond_init_*; pthread_cond_destroy*; + }; + + GLIBC_2.3.2 { +- pthread_cond_timedwait; pthread_cond_init; pthread_cond_destroy; ++ pthread_cond_timedwait; + } GLIBC_2.2.5; + diff --git a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c new file mode 100644 index 000000000..0a9a86688 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * agent-env's TLS shim: OpenSSL checks a peer's certificates against real time, whatever the universe's clock says. + * + * Preloaded after the clock library (/etc/ld.so.preload). A program on universe time months from real time would find + * every live site's certificate not yet valid or expired. Most clients reach live sites through the computer's egress + * proxy, which hands them certificates valid at the universe date; this is for the ones that bypass it. OpenSSL's + * libssl verifies a chain with X509_verify_cert, which this defines first in the global scope: it sets the + * verification's check time to real time, read with a raw system call the clock library doesn't intercept, unless the + * caller set one, then calls OpenSSL's own, the one in the caller's own libcrypto, so a process holding two copies of + * OpenSSL (a bundled one beside the system's) verifies each with its own. + * + * Not covered: TLS stacks of their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +typedef struct x509_store_ctx_st X509_STORE_CTX; +typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; + +/* OpenSSL's X509_V_FLAG_USE_CHECK_TIME (x509_vfy.h), the same in 1.1 and 3 */ +#define TLS_USE_CHECK_TIME 0x2 +#define TLS_SYSTEM_LIBCRYPTO "libcrypto.so.3" + +/* OpenSSL's functions as one copy of it has them */ +struct tls_openssl +{ + int (*verify)(X509_STORE_CTX *); + void (*set_time)(X509_STORE_CTX *, unsigned long, time_t); + X509_VERIFY_PARAM *(*get0_param)(X509_STORE_CTX *); + unsigned long (*get_flags)(const X509_VERIFY_PARAM *); +}; + +int X509_verify_cert(X509_STORE_CTX *ctx); + +/* the real time in seconds, read with the system call itself (the clock library intercepts libc's), 0 if it fails */ +static long tls_real_time(void) +{ + struct timespec ts = {0, 0}; +#if defined(__x86_64__) + long ret; + __asm__ volatile("syscall" : "=a"(ret) : "0"(228L), "D"((long) CLOCK_REALTIME), "S"(&ts) : "rcx", "r11", "memory"); + if (ret != 0) return 0; +#elif defined(__aarch64__) + register long x8 __asm__("x8") = 113; + register long x0 __asm__("x0") = CLOCK_REALTIME; + register long x1 __asm__("x1") = (long) &ts; + __asm__ volatile("svc 0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory"); + if (x0 != 0) return 0; +#else +#error the TLS shim reads real time with a raw system call on x86_64 or aarch64 +#endif + return ts.tv_sec; +} + +/* ``name`` in ``scope`` (a handle, or RTLD_NEXT), never this file's own definition */ +static void *tls_lookup(void *scope, const char *name) +{ + void *found = dlsym(scope, name); + return found == (void *) X509_verify_cert ? NULL : found; +} + +static void tls_take(void *scope, struct tls_openssl *out) +{ + out->verify = (int (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_verify_cert"); + out->set_time = (void (*)(X509_STORE_CTX *, unsigned long, time_t)) tls_lookup(scope, "X509_STORE_CTX_set_time"); + out->get0_param = (X509_VERIFY_PARAM * (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_STORE_CTX_get0_param"); + out->get_flags = (unsigned long (*)(const X509_VERIFY_PARAM *)) tls_lookup(scope, "X509_VERIFY_PARAM_get_flags"); +} + +/* OpenSSL as the object at ``caller`` sees it: from that object's own dependencies (libssl's libcrypto, whichever copy + it is); for a caller that isn't a shared library depending on one (a program calling it itself), the next definition + in the global scope; else the system's libcrypto where it was loaded privately (a Python extension's). Looked up on + every call, once a handshake: a copy of OpenSSL unloaded and another loaded at its address must not be handed the + first one's functions. */ +static void tls_resolve(void *caller, struct tls_openssl *out) +{ + Dl_info info; + void *handle; + memset(out, 0, sizeof *out); + if (dladdr(caller, &info)) + { + if (info.dli_fname != NULL && info.dli_fname[0] != 0 + && (handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD)) != NULL) + { + tls_take(handle, out); + dlclose(handle); + if (out->verify != NULL) return; + } + } + tls_take(RTLD_NEXT, out); + if (out->verify != NULL) return; + if ((handle = dlopen(TLS_SYSTEM_LIBCRYPTO, RTLD_NOW | RTLD_NOLOAD)) != NULL) + { + tls_take(handle, out); + dlclose(handle); + } +} + +int X509_verify_cert(X509_STORE_CTX *ctx) +{ + struct tls_openssl ssl; + tls_resolve(__builtin_return_address(0), &ssl); + if (ssl.verify == NULL) return -1; + if (ctx != NULL && ssl.set_time != NULL && ssl.get0_param != NULL && ssl.get_flags != NULL) + { + X509_VERIFY_PARAM *param = ssl.get0_param(ctx); + long now = tls_real_time(); + if (param != NULL && !(ssl.get_flags(param) & TLS_USE_CHECK_TIME) && now > 0) ssl.set_time(ctx, 0, (time_t) now); + } + return ssl.verify(ctx); +} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot.go b/src/agent_env/agents/agents/claude-code/egress/boot.go new file mode 100644 index 000000000..031555627 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/boot.go @@ -0,0 +1,294 @@ +package main + +import ( + "crypto/x509" + "encoding/pem" + "errors" + "fmt" + "io/fs" + "os" + "os/exec" + "os/user" + "path/filepath" + "sort" + "strconv" + "strings" + "time" +) + +// bootPaths are where boot reads and writes; tests point them into a folder of their own. +type bootPaths struct { + Folder, PublicCA, Anchor string + // Bundles are the PEM bundles the CA is appended to when they exist: agent-env's trust files, which + // SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS name. + Bundles []string + // UpdateTrust rebuilds the system store from its anchors, and its hooks Java's. + UpdateTrust []string + // CertifiRoots and JavaRoots are searched for certifi's cacert.pem and JDKs' lib/security/cacerts. + CertifiRoots, JavaRoots []string + EgressUser, SessionUser string + // NSS adds the CA to the session user's NSS database, which Chromium and Firefox read; nil leaves it. + NSS func(paths bootPaths) error +} + +// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in +// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. +func defaultBootPaths() bootPaths { + p := bootPaths{ + Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", + Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, + UpdateTrust: []string{"update-ca-certificates"}, + CertifiRoots: []string{"/usr/lib/python3", "/usr/local/lib", "/opt"}, + JavaRoots: []string{"/usr/lib/jvm", "/opt"}, + EgressUser: EgressUser, SessionUser: SessionUser, + NSS: sessionNSS, + } + if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { + p.SessionUser = name + } + if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { + p.NSS = nil + } + return p +} + +// boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and +// trusted in the system store, agent-env's trust files, every certifi bundle, every JDK's store and the session +// user's NSS database. A step that fails fails the boot, naming it, but for a certifi bundle or a JDK's store, which +// an app brings: those are named among the report's warnings. +func boot(p bootPaths) (map[string]any, error) { + start := time.Now() + report := map[string]any{} + egress, err := user.Lookup(p.EgressUser) + if err != nil { + return report, err + } + uid, _ := strconv.Atoi(egress.Uid) + gid, _ := strconv.Atoi(egress.Gid) + if err := os.MkdirAll(p.Folder, 0o700); err != nil { + return report, err + } + caPath, keyPath := filepath.Join(p.Folder, "ca.pem"), filepath.Join(p.Folder, "ca.key") + if _, err := os.Stat(caPath); errors.Is(err, fs.ErrNotExist) { + if err := writeCA(caPath, keyPath); err != nil { + return report, fmt.Errorf("minting the CA: %w", err) + } + report["minted"] = true + } + for _, path := range []string{p.Folder, caPath, keyPath} { + if err := os.Chown(path, uid, gid); err != nil { + return report, err + } + } + if err := os.Chmod(p.Folder, 0o700); err != nil { + return report, err + } + caPEM, err := os.ReadFile(caPath) + if err != nil { + return report, err + } + for _, path := range []string{p.PublicCA, p.Anchor} { + if err := writeRootFile(path, caPEM); err != nil { + return report, err + } + } + if len(p.UpdateTrust) > 0 { + if out, err := exec.Command(p.UpdateTrust[0], p.UpdateTrust[1:]...).CombinedOutput(); err != nil { + return report, fmt.Errorf("%s: %w: %s", strings.Join(p.UpdateTrust, " "), err, truncate(string(out), ErrorLimit)) + } + } + appended := map[string]bool{} + for _, path := range p.Bundles { + added, err := appendOnce(path, caPEM) + if errors.Is(err, fs.ErrNotExist) { + continue + } + if err != nil { + return report, fmt.Errorf("trusting the CA in %s: %w", path, err) + } + appended[path] = added + } + // An app's own stores, certifi's and JDKs': one that can't take the CA is named, and the computer boots, its + // app then refusing intercepted sites as it would on a store of its own. + var warnings []string + for _, path := range certifiBundles(p.CertifiRoots) { + added, err := appendOnce(path, caPEM) + if err != nil { + warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", path, err)) + continue + } + appended[path] = added + } + report["bundles"] = appended + java := map[string]string{} + for _, store := range javaStores(p.JavaRoots) { + if err := trustJava(store, p.PublicCA); err != nil { + warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", store, err)) + continue + } + java[store] = "trusted" + } + report["java"] = java + if len(warnings) > 0 { + report["warnings"] = warnings + } + if p.NSS != nil { + if err := p.NSS(p); err != nil { + return report, fmt.Errorf("the session's NSS database: %w", err) + } + report["nss"] = "trusted" + } + report["seconds"] = time.Since(start).Seconds() + return report, nil +} + +func writeCA(caPath, keyPath string) error { + ca, err := mintCA(CAName) + if err != nil { + return err + } + key, err := x509.MarshalPKCS8PrivateKey(ca.key) + if err != nil { + return err + } + if err := writeExclusive(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}), 0o600); err != nil { + return err + } + return writeExclusive(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.cert.Raw}), 0o644) +} + +func writeExclusive(path string, data []byte, mode os.FileMode) error { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) + if err != nil { + return err + } + if _, err := f.Write(data); err != nil { + f.Close() + return err + } + return f.Close() +} + +// writeRootFile writes path whole, 0644, replacing it in one rename. +func writeRootFile(path string, data []byte) error { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + part := path + ".part" + if err := os.WriteFile(part, data, 0o644); err != nil { + return err + } + if err := os.Chmod(part, 0o644); err != nil { + return err + } + return os.Rename(part, path) +} + +// appendOnce appends pem to the bundle at path (through a symlink) unless it's there; whether it did. +func appendOnce(path string, pemData []byte) (bool, error) { + real, err := filepath.EvalSymlinks(path) + if err != nil { + return false, err + } + text, err := os.ReadFile(real) + if err != nil { + return false, err + } + block := strings.TrimSpace(string(pemData)) + if strings.Contains(string(text), block) { + return false, nil + } + f, err := os.OpenFile(real, os.O_WRONLY|os.O_APPEND, 0) + if err != nil { + return false, err + } + defer f.Close() + sep := "" + if len(text) > 0 && !strings.HasSuffix(string(text), "\n") { + sep = "\n" + } + _, err = f.WriteString(sep + block + "\n") + return err == nil, err +} + +func certifiBundles(roots []string) []string { + seen := map[string]bool{} + var out []string + for _, root := range roots { + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() || d.Name() != "cacert.pem" || filepath.Base(filepath.Dir(path)) != "certifi" { + return nil + } + real, err := filepath.EvalSymlinks(path) + if err == nil && !seen[real] { + seen[real] = true + out = append(out, path) + } + return nil + }) + } + sort.Strings(out) + return out +} + +// javaStores are the JDKs' stores, lib/security/cacerts, under roots; the system store's hook builds Debian's. +func javaStores(roots []string) []string { + var out []string + for _, root := range roots { + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() || d.Name() != "cacerts" { + return nil + } + if filepath.Base(filepath.Dir(path)) == "security" && filepath.Base(filepath.Dir(filepath.Dir(path))) == "lib" { + if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() { + out = append(out, path) + } + } + return nil + }) + } + sort.Strings(out) + return out +} + +// trustJava adds the CA to a JDK's store with that JDK's own keytool, else any on the PATH. +func trustJava(store, ca string) error { + keytool := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(store))), "bin", "keytool") + if _, err := os.Stat(keytool); err != nil { + if keytool, err = exec.LookPath("keytool"); err != nil { + return errors.New("there's no keytool to add the CA with") + } + } + opened := []string{"-keystore", store, "-storepass", "changeit"} + exec.Command(keytool, append([]string{"-delete", "-alias", "agentenv-egress"}, opened...)...).Run() + out, err := exec.Command(keytool, append([]string{"-importcert", "-noprompt", "-trustcacerts", "-alias", + "agentenv-egress", "-file", ca}, opened...)...).CombinedOutput() + if err != nil { + return fmt.Errorf("keytool: %w: %s", err, truncate(string(out), ErrorLimit)) + } + return nil +} + +// sessionNSS adds the CA to the session user's NSS database, as that user, making the database if there's none. +func sessionNSS(p bootPaths) error { + session, err := user.Lookup(p.SessionUser) + if err != nil { + return err + } + db := filepath.Join(session.HomeDir, ".pki", "nssdb") + as := []string{"setpriv", "--reuid=" + p.SessionUser, "--regid=" + p.SessionUser, "--init-groups", "env", + "HOME=" + session.HomeDir} + steps := [][]string{{"mkdir", "-p", db}} + if _, err := os.Stat(filepath.Join(db, "cert9.db")); err != nil { + steps = append(steps, []string{"certutil", "-N", "-d", "sql:" + db, "--empty-password"}) + } + steps = append(steps, []string{"certutil", "-A", "-d", "sql:" + db, "-n", NSSNickname, "-t", "C,,", "-i", + p.PublicCA}) + for _, step := range steps { + argv := append(append([]string{}, as...), step...) + if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil { + return fmt.Errorf("%s: %w: %s", strings.Join(step[:2], " "), err, truncate(string(out), ErrorLimit)) + } + } + return nil +} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot_test.go b/src/agent_env/agents/agents/claude-code/egress/boot_test.go new file mode 100644 index 000000000..c5f59ad0a --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/boot_test.go @@ -0,0 +1,99 @@ +package main + +import ( + "os" + "os/user" + "path/filepath" + "strings" + "testing" +) + +func TestBootMintsTheCAOnceItsKeyPrivateAndTrustsItInEveryBundleOnce(t *testing.T) { + dir := t.TempDir() + me, _ := user.Current() + bundle := filepath.Join(dir, "ca-bundle.pem") + os.WriteFile(bundle, []byte("-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----"), 0o644) + certifi := filepath.Join(dir, "opt", "venv", "lib", "certifi", "cacert.pem") + os.MkdirAll(filepath.Dir(certifi), 0o755) + os.WriteFile(certifi, []byte(""), 0o644) + p := bootPaths{ + Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), + Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), + Bundles: []string{bundle, filepath.Join(dir, "missing.pem")}, + CertifiRoots: []string{filepath.Join(dir, "opt")}, + EgressUser: me.Username, + } + report, err := boot(p) + if err != nil { + t.Fatal(err, report) + } + if report["minted"] != true { + t.Fatalf("%v", report) + } + info, _ := os.Stat(filepath.Join(p.Folder, "ca.key")) + if info.Mode().Perm() != 0o600 { + t.Fatalf("the CA key is %v", info.Mode().Perm()) + } + if info, _ := os.Stat(p.Folder); info.Mode().Perm() != 0o700 { + t.Fatalf("the folder is %v", info.Mode().Perm()) + } + ca, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) + public, _ := os.ReadFile(p.PublicCA) + if string(ca) != string(public) { + t.Fatal("the public CA isn't the CA") + } + if _, err := boot(p); err != nil { + t.Fatal(err) + } + again, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) + if string(again) != string(ca) { + t.Fatal("a second boot minted another CA") + } + for _, path := range []string{bundle, certifi} { + text, _ := os.ReadFile(path) + if n := strings.Count(string(text), strings.TrimSpace(string(ca))); n != 1 { + t.Fatalf("%s has the CA %d times", path, n) + } + } + leaves, err := LoadLeaves(p.Folder) + if err != nil || leaves.ca.cert.NotBefore != CAFrom || leaves.ca.cert.NotAfter != CAUntil { + t.Fatalf("%v", err) + } +} + +func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { + dir := t.TempDir() + me, _ := user.Current() + broken := filepath.Join(dir, "opt", "venv", "lib", "certifi") + os.MkdirAll(filepath.Join(broken, "elsewhere"), 0o755) + os.Symlink(filepath.Join(broken, "elsewhere"), filepath.Join(broken, "cacert.pem")) // a folder, not a bundle + jdk := filepath.Join(dir, "jdk", "lib", "security") + os.MkdirAll(jdk, 0o755) + os.WriteFile(filepath.Join(jdk, "cacerts"), []byte("not a keystore"), 0o644) + t.Setenv("PATH", dir) // and no keytool to add the CA with + p := bootPaths{ + Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), + Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), + CertifiRoots: []string{filepath.Join(dir, "opt")}, JavaRoots: []string{filepath.Join(dir, "jdk")}, + EgressUser: me.Username, + } + report, err := boot(p) + if err != nil { + t.Fatalf("the boot failed: %v", err) + } + warnings, _ := report["warnings"].([]string) + if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, " "), "keytool") { + t.Fatalf("%v", report) + } +} + +func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { + if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { + t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) + } + t.Setenv("AGENTENV_SESSION_USER", "agent") + t.Setenv("AGENTENV_EGRESS_NSS", "off") + if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { + t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock.go b/src/agent_env/agents/agents/claude-code/egress/clock.go new file mode 100644 index 000000000..30cb880c1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/clock.go @@ -0,0 +1,151 @@ +package main + +import ( + "encoding/binary" + "math" + "os" + "sync" + "syscall" + "time" +) + +// The clock page the follower writes (clockpage.py, version 2): magic version seq universe_ns anchor_ns rate armed +// reserved generation check mono_universe_ns mono_anchor_ns, little-endian, 80 bytes, seq odd while it's written. +const ( + pageSize = 80 + pageMagic = 0x4B434541 + checkSalt = 0x9E3779B97F4A7C15 +) + +// state is the universe as the session reads it: armed, the universe at the anchor (seconds), the anchor (real +// seconds) and the rate. +type state struct { + Armed bool + V float64 + Anchor float64 + Rate float64 +} + +var unarmed = state{Rate: 1} + +func (s state) at(t float64) float64 { + if !s.Armed { + return t + } + if s.Rate == 0 || t <= s.Anchor { + return s.V + } + return s.V + (t-s.Anchor)*s.Rate +} + +func pageCheck(seq uint64, u, a int64, rate float64, armed uint32, generation uint64, mu, ma int64) uint64 { + return seq ^ uint64(u) ^ uint64(a) ^ math.Float64bits(rate) ^ uint64(armed) ^ generation ^ uint64(mu) ^ + uint64(ma) ^ checkSalt +} + +// readPage reads a consistent state from the page's bytes, or false when it's being written, torn or not a page. +func readPage(b []byte) (state, bool) { + if len(b) < pageSize || binary.LittleEndian.Uint32(b[0:]) != pageMagic { + return state{}, false + } + seq := binary.LittleEndian.Uint64(b[8:]) + if seq&1 == 1 { + return state{}, false + } + u := int64(binary.LittleEndian.Uint64(b[16:])) + a := int64(binary.LittleEndian.Uint64(b[24:])) + rate := math.Float64frombits(binary.LittleEndian.Uint64(b[32:])) + armed := binary.LittleEndian.Uint32(b[40:]) + generation := binary.LittleEndian.Uint64(b[48:]) + check := binary.LittleEndian.Uint64(b[56:]) + mu := int64(binary.LittleEndian.Uint64(b[64:])) + ma := int64(binary.LittleEndian.Uint64(b[72:])) + if check != pageCheck(seq, u, a, rate, armed, generation, mu, ma) || binary.LittleEndian.Uint64(b[8:]) != seq { + return state{}, false + } + if armed == 0 { + return unarmed, true + } + return state{Armed: true, V: float64(u) / 1e9, Anchor: float64(a) / 1e9, Rate: rate}, true +} + +// Universe follows the clock page, read every PagePoll, so a change is seen and the universe it left is held for Hold. +type Universe struct { + path string + mu sync.Mutex + page []byte + current state + known bool + previous state + leftAt time.Time +} + +func NewUniverse(path string) *Universe { + u := &Universe{path: path, current: unarmed} + u.refresh(time.Now()) + return u +} + +func (u *Universe) Poll() { + for { + time.Sleep(PagePoll) + u.refresh(time.Now()) + } +} + +func (u *Universe) read() state { + if u.page == nil { + f, err := os.Open(u.path) + if err != nil { + return unarmed + } + mapped, err := syscall.Mmap(int(f.Fd()), 0, pageSize, syscall.PROT_READ, syscall.MAP_SHARED) + f.Close() + if err != nil { + return unarmed + } + u.page = mapped + } + for i := 0; i < PageReads; i++ { + if s, ok := readPage(u.page); ok { + return s + } + } + return u.current +} + +func (u *Universe) refresh(now time.Time) state { + u.mu.Lock() + defer u.mu.Unlock() + s := u.read() + if u.known && s != u.current { + u.previous, u.leftAt = u.current, now + } + u.current, u.known = s, true + return s +} + +// View is false when the session is on real time and was armed nowhere within Hold; else true, with the universes a +// client may check against: now, the universe at the anchor, and the one left within Hold when it was armed. +func (u *Universe) View(now time.Time) (bool, []float64) { + u.refresh(now) + u.mu.Lock() + defer u.mu.Unlock() + return view(u.current, u.previous, u.leftAt, now) +} + +func view(current, previous state, leftAt time.Time, now time.Time) (bool, []float64) { + held := !leftAt.IsZero() && now.Sub(leftAt) <= Hold && previous.Armed + if !current.Armed && !held { + return false, nil + } + t := float64(now.UnixNano()) / 1e9 + out := []float64{current.at(t)} + if current.Armed { + out = append(out, current.V) + } + if held { + out = append(out, previous.at(t)) + } + return true, out +} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock_test.go b/src/agent_env/agents/agents/claude-code/egress/clock_test.go new file mode 100644 index 000000000..22bb41503 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/clock_test.go @@ -0,0 +1,112 @@ +package main + +import ( + "encoding/binary" + "encoding/hex" + "math" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func page(seq uint64, universe, anchor time.Time, rate float64, armed bool) []byte { + b := make([]byte, pageSize) + u, a := universe.UnixNano(), anchor.UnixNano() + var on uint32 + if armed { + on = 1 + } + binary.LittleEndian.PutUint32(b[0:], pageMagic) + binary.LittleEndian.PutUint32(b[4:], 2) + binary.LittleEndian.PutUint64(b[8:], seq) + binary.LittleEndian.PutUint64(b[16:], uint64(u)) + binary.LittleEndian.PutUint64(b[24:], uint64(a)) + binary.LittleEndian.PutUint64(b[32:], math.Float64bits(rate)) + binary.LittleEndian.PutUint32(b[40:], on) + binary.LittleEndian.PutUint64(b[48:], 7) + binary.LittleEndian.PutUint64(b[56:], pageCheck(seq, u, a, rate, on, 7, a, a)) + binary.LittleEndian.PutUint64(b[64:], uint64(a)) + binary.LittleEndian.PutUint64(b[72:], uint64(a)) + return b +} + +func TestAPageReadsAsTheUniverseItHoldsAndATornOneNot(t *testing.T) { + universe, anchor := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Unix(time.Now().Unix(), 0) + s, ok := readPage(page(4, universe, anchor, 2, true)) + if !ok || !s.Armed || s.Rate != 2 || int64(s.V) != universe.Unix() { + t.Fatalf("read %+v %v", s, ok) + } + if got := s.at(float64(anchor.Unix()) + 10); int64(got) != universe.Unix()+20 { + t.Fatalf("10 s on at rate 2: %v", got) + } + if _, ok := readPage(page(5, universe, anchor, 2, true)); ok { + t.Fatal("a page being written (odd seq) read as whole") + } + torn := page(4, universe, anchor, 2, true) + torn[20] ^= 1 + if _, ok := readPage(torn); ok { + t.Fatal("a torn page read as whole") + } + if s, ok := readPage(page(2, time.Time{}, time.Time{}, 1, false)); !ok || s.Armed { + t.Fatalf("an unarmed page: %+v %v", s, ok) + } +} + +func TestTheUniverseFollowsThePageFileAndHoldsTheOneItLeft(t *testing.T) { + path := filepath.Join(t.TempDir(), "page") + if err := os.WriteFile(path, page(2, time.Time{}, time.Time{}, 1, false), 0o644); err != nil { + t.Fatal(err) + } + u := NewUniverse(path) + if armed, _ := u.View(time.Now()); armed { + t.Fatal("unarmed page viewed as armed") + } + past := time.Now().Add(-180 * 24 * time.Hour) + f, _ := os.OpenFile(path, os.O_WRONLY, 0) + f.WriteAt(page(4, past, time.Now(), 1, true), 0) + f.Close() + armed, universes := u.View(time.Now()) + if !armed || len(universes) != 2 || int64(universes[1]) != past.Unix() { + t.Fatalf("armed page: %v %v", armed, universes) + } +} + +func TestAClientMayCheckAgainstTheUniverseItLeftForHoldAfterAChange(t *testing.T) { + now := time.Now() + past := state{Armed: true, V: float64(now.Add(-100 * 24 * time.Hour).Unix()), Anchor: float64(now.Unix()), Rate: 1} + if armed, _ := view(unarmed, state{}, time.Time{}, now); armed { + t.Fatal("real time viewed as armed") + } + armed, universes := view(unarmed, past, now.Add(-Hold/2), now) + if !armed || len(universes) != 2 { + t.Fatalf("within Hold of leaving an armed universe: %v %v", armed, universes) + } + if armed, _ := view(unarmed, past, now.Add(-2*Hold), now); armed { + t.Fatal("past Hold, the universe left still counts") + } +} + +// golden is a page the follower's writer wrote (clockpage.encode), as testdata keeps it. +func golden(t *testing.T, name string) []byte { + text, err := os.ReadFile(filepath.Join("testdata", "page-"+name+".hex")) + if err != nil { + t.Fatal(err) + } + b, err := hex.DecodeString(strings.TrimSpace(string(text))) + if err != nil { + t.Fatal(err) + } + return b +} + +func TestThePagesTheFollowersWriterWritesReadAsTheUniverseTheyHold(t *testing.T) { + s, ok := readPage(golden(t, "armed")) + if !ok || !s.Armed || s.V != 1577836800 || s.Rate != 2 || math.Abs(s.Anchor-1794000000.123456789) > 1e-6 { + t.Fatalf("armed: %+v %v", s, ok) + } + if s, ok := readPage(golden(t, "unarmed")); !ok || s != unarmed { + t.Fatalf("unarmed: %+v %v", s, ok) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/go.mod b/src/agent_env/agents/agents/claude-code/egress/go.mod new file mode 100644 index 000000000..7b1b97e80 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/go.mod @@ -0,0 +1,12 @@ +module agentenv/egress + +go 1.26.11 + +require github.com/refraction-networking/utls v1.8.2 + +require ( + github.com/andybalholm/brotli v1.0.6 // indirect + github.com/klauspost/compress v1.17.4 // indirect + golang.org/x/crypto v0.36.0 // indirect + golang.org/x/sys v0.31.0 // indirect +) diff --git a/src/agent_env/agents/agents/claude-code/egress/go.sum b/src/agent_env/agents/agents/claude-code/egress/go.sum new file mode 100644 index 000000000..3e8a6d1ef --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/go.sum @@ -0,0 +1,10 @@ +github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= +github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= +github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= +github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/src/agent_env/agents/agents/claude-code/egress/hello.go b/src/agent_env/agents/agents/claude-code/egress/hello.go new file mode 100644 index 000000000..381ea2993 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/hello.go @@ -0,0 +1,164 @@ +package main + +import ( + "bufio" + "encoding/binary" + "errors" + "strings" + "time" +) + +// A TLS record's content type for handshake messages, a ClientHello's handshake type, and the most plaintext one +// record carries. +const ( + recordHandshake = 22 + clientHelloType = 1 + maxRecord = 16384 +) + +// clientHello is the ClientHello handshake message the client has sent, peeked from r so the handshake stays +// untouched for whoever reads it next; it may span several records. +func clientHello(r *bufio.Reader, deadline time.Time) ([]byte, error) { + need := 5 + for { + if time.Now().After(deadline) { + return nil, errors.New("ClientHello incomplete") + } + data, err := r.Peek(need) + if err != nil { + if errors.Is(err, bufio.ErrBufferFull) { + return nil, errors.New("ClientHello larger than the read buffer") + } + return nil, err + } + if data[0] != recordHandshake { + return nil, errors.New("not TLS") + } + var body []byte + for p := 0; ; { + if p+5 > len(data) { + need = p + 5 + break + } + if data[p] != recordHandshake { + return nil, errors.New("ClientHello interrupted by another record") + } + size := int(binary.BigEndian.Uint16(data[p+3:])) + if p+5+size > len(data) { + need = p + 5 + size + break + } + body = append(body, data[p+5:p+5+size]...) + p += 5 + size + if len(body) >= 4 { + if whole := 4 + (int(body[1])<<16 | int(body[2])<<8 | int(body[3])); len(body) >= whole { + return body[:whole], nil + } + } + } + } +} + +// parseHello is the server name and the ALPN protocols a ClientHello message offers. +func parseHello(hello []byte) (string, []string, error) { + p, err := helloExtensions(hello) + if err != nil { + return "", nil, err + } + if len(hello) < p+2 { + return "", nil, nil + } + end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) + if end > len(hello) { + return "", nil, errors.New("extensions overrun the ClientHello") + } + var sni string + var alpn []string + for q := p + 2; q+4 <= end; { + kind := binary.BigEndian.Uint16(hello[q:]) + size := int(binary.BigEndian.Uint16(hello[q+2:])) + body := hello[q+4 : min(q+4+size, end)] + q += 4 + size + switch { + case kind == 0 && len(body) > 5 && body[2] == 0: + n := int(binary.BigEndian.Uint16(body[3:])) + if 5+n <= len(body) { + sni = strings.ToLower(string(body[5 : 5+n])) + } + case kind == 16 && len(body) > 2: + for r := 2; r < len(body); { + n := int(body[r]) + if r+1+n > len(body) { + break + } + alpn = append(alpn, string(body[r+1:r+1+n])) + r += 1 + n + } + } + } + return sni, alpn, nil +} + +// resumption: what only the proxy's own session with the client could mean, never the site's. +var dropped = map[uint16]bool{41: true, 42: true} + +// helloRecord is the ClientHello as one handshake record, without the extensions only a resumption with the proxy put +// there, and the extensions it left out. +func helloRecord(hello []byte) ([]byte, []uint16, error) { + p, err := helloExtensions(hello) + if err != nil { + return nil, nil, err + } + body := append([]byte{}, hello[4:p]...) + var gone []uint16 + if len(hello) >= p+2 { + end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) + if end > len(hello) { + return nil, nil, errors.New("extensions overrun the ClientHello") + } + var exts []byte + for q := p + 2; q+4 <= end; { + kind := binary.BigEndian.Uint16(hello[q:]) + size := int(binary.BigEndian.Uint16(hello[q+2:])) + if q+4+size > end { + return nil, nil, errors.New("an extension overruns the ClientHello") + } + if dropped[kind] { + gone = append(gone, kind) + } else { + exts = append(exts, hello[q:q+4+size]...) + } + q += 4 + size + } + body = binary.BigEndian.AppendUint16(body, uint16(len(exts))) + body = append(body, exts...) + } + if len(body)+4 > maxRecord { + return nil, nil, errors.New("ClientHello longer than one record") + } + msg := append([]byte{clientHelloType, byte(len(body) >> 16), byte(len(body) >> 8), byte(len(body))}, body...) + return append([]byte{recordHandshake, 3, 1, byte(len(msg) >> 8), byte(len(msg))}, msg...), gone, nil +} + +// helloExtensions is where a ClientHello message's extensions start, past its version, random, session id, cipher +// suites and compression methods, each length checked against the message. +func helloExtensions(hello []byte) (int, error) { + if len(hello) < 4 || hello[0] != clientHelloType { + return 0, errors.New("not a ClientHello") + } + p := 4 + 2 + 32 + for _, width := range []int{1, 2, 1} { // session id, cipher suites, compression methods + if len(hello) < p+width { + return 0, errors.New("short ClientHello") + } + n := int(hello[p]) + if width == 2 { + n = int(binary.BigEndian.Uint16(hello[p:])) + } + p += width + n + if p > len(hello) { + return 0, errors.New("a ClientHello field overruns the message") + } + } + return p, nil +} diff --git a/src/agent_env/agents/agents/claude-code/egress/hello_test.go b/src/agent_env/agents/agents/claude-code/egress/hello_test.go new file mode 100644 index 000000000..a3b30b2f1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/hello_test.go @@ -0,0 +1,82 @@ +package main + +import ( + "bufio" + "bytes" + "crypto/tls" + "encoding/binary" + "net" + "reflect" + "testing" + "time" +) + +// goHello is the ClientHello message Go's TLS client sends for name, offering alpn. +func goHello(t *testing.T, name string, alpn []string) []byte { + t.Helper() + client, server := net.Pipe() + go tls.Client(client, &tls.Config{ServerName: name, NextProtos: alpn, InsecureSkipVerify: true}).Handshake() + defer client.Close() + defer server.Close() + hello, err := clientHello(bufio.NewReader(server), time.Now().Add(5*time.Second)) + if err != nil { + t.Fatal(err) + } + return hello +} + +func records(message []byte, size int) []byte { + var out []byte + for len(message) > 0 { + n := min(size, len(message)) + out = append(out, 22, 3, 1, byte(n>>8), byte(n)) + out = append(out, message[:n]...) + message = message[n:] + } + return out +} + +func TestAClientHelloIsReadWholeAcrossRecordsAndNamesItsSiteAndProtocols(t *testing.T) { + hello := goHello(t, "Example.COM", []string{"h2", "http/1.1"}) + sni, alpn, err := parseHello(hello) + if err != nil || sni != "example.com" || !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) { + t.Fatalf("parsed %q %v %v", sni, alpn, err) + } + split := records(hello, 100) + again, err := clientHello(bufio.NewReader(bytes.NewReader(split)), time.Now().Add(time.Second)) + if err != nil || !bytes.Equal(again, hello) { + t.Fatalf("across records: %v", err) + } + if _, err := clientHello(bufio.NewReader(bytes.NewReader([]byte("GET / HTTP/1.1\r\n\r\n"))), + time.Now().Add(time.Second)); err == nil { + t.Fatal("plain HTTP read as TLS") + } +} + +func withExtension(hello []byte, kind uint16, body []byte) []byte { + p := 4 + 2 + 32 + p += 1 + int(hello[p]) + p += 2 + int(binary.BigEndian.Uint16(hello[p:])) + p += 1 + int(hello[p]) + exts := append(append([]byte{}, hello[p+2:]...), byte(kind>>8), byte(kind), byte(len(body)>>8), byte(len(body))) + exts = append(exts, body...) + out := append(append([]byte{}, hello[:p]...), byte(len(exts)>>8), byte(len(exts))) + out = append(out, exts...) + n := len(out) - 4 + out[1], out[2], out[3] = byte(n>>16), byte(n>>8), byte(n) + return out +} + +func TestTheUpstreamRecordLeavesOutWhatOnlyAResumptionWithTheProxyMeans(t *testing.T) { + hello := withExtension(goHello(t, "example.com", nil), 41, []byte{0, 1, 2}) + record, gone, err := helloRecord(hello) + if err != nil || !reflect.DeepEqual(gone, []uint16{41}) { + t.Fatalf("dropped %v %v", gone, err) + } + if record[0] != 22 || int(binary.BigEndian.Uint16(record[3:])) != len(record)-5 { + t.Fatal("not one handshake record") + } + if _, again, _ := helloRecord(record[5:]); len(again) != 0 { + t.Fatalf("the record still has %v", again) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/leaves.go b/src/agent_env/agents/agents/claude-code/egress/leaves.go new file mode 100644 index 000000000..027ecac92 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/leaves.go @@ -0,0 +1,188 @@ +package main + +import ( + "crypto" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "errors" + "math/big" + "net" + "os" + "path/filepath" + "sort" + "strings" + "sync" +) + +// The longest common name X.509 allows (ub-common-name). +const commonNameLimit = 64 + +// Leaves signs the leaves an intercepted client is handed: by this computer's CA when the site verified, else by a CA +// trusted nowhere, so the client refuses it as it would the site. Each is valid over all of its CA's window, so it +// holds wherever the universe goes, and is minted once per name and its alternative names. +type Leaves struct { + ca, untrusted signer + key *ecdsa.PrivateKey + mu sync.Mutex + cache map[string]*tls.Certificate + // order is the cache's keys, oldest first, so it drops the oldest past cacheLimit; a connection keeps its own. + order []string +} + +type signer struct { + cert *x509.Certificate + key crypto.Signer +} + +func LoadLeaves(folder string) (*Leaves, error) { + certPEM, err := os.ReadFile(filepath.Join(folder, "ca.pem")) + if err != nil { + return nil, err + } + keyPEM, err := os.ReadFile(filepath.Join(folder, "ca.key")) + if err != nil { + return nil, err + } + ca, err := parseSigner(certPEM, keyPEM) + if err != nil { + return nil, err + } + return NewLeaves(ca) +} + +func NewLeaves(ca signer) (*Leaves, error) { + untrusted, err := mintCA("Untrusted upstream") + if err != nil { + return nil, err + } + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return nil, err + } + return &Leaves{ca: ca, untrusted: untrusted, key: key, cache: map[string]*tls.Certificate{}}, nil +} + +// For is the leaf for host and the names sans ("DNS:..." or "IP:..."), by this computer's CA when trusted, and +// whether it was minted now. +func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate, bool, error) { + sorted := append([]string{}, sans...) + sort.Strings(sorted) + key := host + "|" + strings.Join(sorted, ",") + by := l.ca + if !trusted { + key, by = "untrusted|"+key, l.untrusted + } + l.mu.Lock() + defer l.mu.Unlock() + if cert, ok := l.cache[key]; ok { + return cert, false, nil + } + template := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, + NotBefore: by.cert.NotBefore.Add(LeafInset), + NotAfter: by.cert.NotAfter.Add(-LeafInset), + KeyUsage: x509.KeyUsageDigitalSignature, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + BasicConstraintsValid: true, + } + for _, name := range append([]string{host}, sans...) { + name = strings.TrimPrefix(strings.TrimPrefix(name, "DNS:"), "IP:") + if ip := net.ParseIP(strings.Trim(name, "[]")); ip != nil { + template.IPAddresses = appendIP(template.IPAddresses, ip) + } else if name != "" && !contains(template.DNSNames, name) { + template.DNSNames = append(template.DNSNames, name) + } + } + der, err := x509.CreateCertificate(rand.Reader, template, by.cert, &l.key.PublicKey, by.key) + if err != nil { + return nil, false, err + } + cert := &tls.Certificate{Certificate: [][]byte{der, by.cert.Raw}, PrivateKey: l.key} + l.cache[key] = cert + l.order = append(l.order, key) + for len(l.order) > cacheLimit { + delete(l.cache, l.order[0]) + l.order = l.order[1:] + } + return cert, true, nil +} + +// mintCA is a self-signed CA over the window, its key in memory. +func mintCA(name string) (signer, error) { + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return signer{}, err + } + template := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: name}, + NotBefore: CAFrom, + NotAfter: CAUntil, + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, + BasicConstraintsValid: true, + IsCA: true, + MaxPathLenZero: true, + } + der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) + if err != nil { + return signer{}, err + } + cert, err := x509.ParseCertificate(der) + if err != nil { + return signer{}, err + } + return signer{cert: cert, key: key}, nil +} + +func parseSigner(certPEM, keyPEM []byte) (signer, error) { + block, _ := pem.Decode(certPEM) + if block == nil { + return signer{}, errors.New("no certificate in the CA's PEM") + } + cert, err := x509.ParseCertificate(block.Bytes) + if err != nil { + return signer{}, err + } + block, _ = pem.Decode(keyPEM) + if block == nil { + return signer{}, errors.New("no key in the CA key's PEM") + } + key, err := x509.ParsePKCS8PrivateKey(block.Bytes) + if err != nil { + return signer{}, err + } + s, ok := key.(crypto.Signer) + if !ok { + return signer{}, errors.New("the CA key can't sign") + } + return signer{cert: cert, key: s}, nil +} + +func serial() *big.Int { + n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) + return n +} + +func appendIP(ips []net.IP, ip net.IP) []net.IP { + for _, have := range ips { + if have.Equal(ip) { + return ips + } + } + return append(ips, ip) +} + +func contains(list []string, s string) bool { + for _, have := range list { + if strings.EqualFold(have, s) { + return true + } + } + return false +} diff --git a/src/agent_env/agents/agents/claude-code/egress/main.go b/src/agent_env/agents/agents/claude-code/egress/main.go new file mode 100644 index 000000000..0bd5ae5c5 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/main.go @@ -0,0 +1,92 @@ +// egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. +// +// It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and +// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128. Session programs reach it through HTTP(S)_PROXY and +// ALL_PROXY; nothing redirects or filters, so it needs no NET_ADMIN and works on any provider. +// +// A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe +// a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is +// intercepted: the site is verified against real time and the public roots, and the client is handed a leaf from this +// computer's CA, valid over every date the clock can show. The upstream handshake is uTLS's, built from the client's +// own ClientHello, so the site sees the client's fingerprint and ALPN offer either way. +// +// egress boot as root at the computer's start: the CA, minted once, its key this user's alone, trusted everywhere +// egress serve as agentenv-egress: serve, one JSON line per connection on stdout +package main + +import ( + "encoding/json" + "fmt" + "os" + "time" +) + +const ( + Listen = "127.0.0.1:3128" + EgressUser = "agentenv-egress" + SessionUser = "user" + Folder = "/etc/agentenv/egress" + PublicCA = "/etc/agentenv/egress-ca.pem" + PublicRoots = "/usr/local/lib/agentenv-egress/roots.pem" + ClockPage = "/dev/shm/agentenv-clock" + Hosts = "/etc/hosts" + HostsBegin = "# agentenv sites" + HostsEnd = "# end agentenv sites" + CAName = "agentenv egress CA" + NSSNickname = "agentenv egress" + Hold = 30 * time.Second + Margin = 8 * 24 * time.Hour + ProbeTTL = time.Hour + ProbeFailedTTL = time.Minute + FirstBytes = 300 * time.Millisecond + HelloTimeout = 10 * time.Second + DialTimeout = 10 * time.Second + + // How long a client has to send its request's head, and how long either handshake of an interception, the + // client's with the proxy and the proxy's with the site, has to finish. + HeadTimeout = 15 * time.Second + HandshakeTimeout = 15 * time.Second + + // The longest request head a client may send, and the most of an error a log row keeps, in bytes. + HeadLimit = 64 << 10 + ErrorLimit = 300 + + // How far inside the CA's dates each leaf it signs is, so no leaf outlasts its CA. + LeafInset = 2 * time.Hour + + // How often the clock page is read, and how many reads may find it mid-write before the last universe stands. + PagePoll = 20 * time.Millisecond + PageReads = 1000 +) + +// The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' +// (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. +var ( + CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) + CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) +) + +func main() { + command := "" + if len(os.Args) > 1 { + command = os.Args[1] + } + switch command { + case "boot": + report, err := boot(defaultBootPaths()) + out, _ := json.Marshal(report) + fmt.Println(string(out)) + if err != nil { + fmt.Fprintln(os.Stderr, "egress boot:", err) + os.Exit(1) + } + case "serve": + if err := serveForever(); err != nil { + fmt.Fprintln(os.Stderr, "egress serve:", err) + os.Exit(1) + } + default: + fmt.Fprintf(os.Stderr, "usage: egress boot|serve, not %q\n", command) + os.Exit(2) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy.go b/src/agent_env/agents/agents/claude-code/egress/proxy.go new file mode 100644 index 000000000..c575fe5e6 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/proxy.go @@ -0,0 +1,598 @@ +package main + +import ( + "bufio" + "context" + "crypto/tls" + "crypto/x509" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/netip" + "net/url" + "os" + "strconv" + "strings" + "sync" + "time" +) + +type Proxy struct { + Leaves *Leaves + Universe interface { + View(time.Time) (bool, []float64) + } + // Roots are the public roots a site is verified against, as the image had them before any agentenv CA joined. + Roots *x509.CertPool + // Resolve, Allowed, Routed and Dial default to the system's resolver, a global address, the hosts file's agentenv + // sites block and a TCP dial; tests replace them. + Resolve func(ctx context.Context, host string) ([]netip.Addr, error) + Allowed func(netip.Addr) bool + Routed func() map[string]string + Dial func(dst string) (net.Conn, error) + Log func(row map[string]any) + probes probes +} + +func serveForever() error { + leaves, err := LoadLeaves(Folder) + if err != nil { + return fmt.Errorf("the CA (has boot run?): %w", err) + } + roots, err := os.ReadFile(PublicRoots) + if err != nil { + return err + } + pool := x509.NewCertPool() + if !pool.AppendCertsFromPEM(roots) { + return fmt.Errorf("no certificates in %s", PublicRoots) + } + universe := NewUniverse(ClockPage) + go universe.Poll() + ln, err := net.Listen("tcp", Listen) + if err != nil { + return err + } + p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} + p.Log(map[string]any{"event": "serving", "listen": Listen, "uid": os.Getuid(), "pid": os.Getpid()}) + return p.Serve(ln) +} + +func jsonLines(w io.Writer) func(map[string]any) { + var mu sync.Mutex + encoder := json.NewEncoder(w) + return func(row map[string]any) { + row["t"] = float64(time.Now().UnixMilli()) / 1e3 + mu.Lock() + defer mu.Unlock() + encoder.Encode(row) + } +} + +func (p *Proxy) Serve(ln net.Listener) error { + for { + conn, err := ln.Accept() + if err != nil { + if errors.Is(err, net.ErrClosed) { + return nil + } + return err + } + go p.handle(conn) + } +} + +func (p *Proxy) log(row map[string]any) { + if p.Log != nil { + p.Log(row) + } +} + +func (p *Proxy) handle(conn net.Conn) { + start := time.Now() + row := map[string]any{"client": conn.RemoteAddr().String()} + defer func() { + if r := recover(); r != nil { + row["error"] = fmt.Sprint("panic: ", r) + p.log(row) + } + conn.Close() + }() + br := bufio.NewReaderSize(conn, HeadLimit) + conn.SetReadDeadline(start.Add(HeadTimeout)) + method, target, version, headers, err := readHead(br) + if err != nil { + row["error"] = truncate("request: "+err.Error(), ErrorLimit) + p.log(row) + return + } + row["method"] = method + if method == http.MethodConnect { + host, port := splitAuthority(target, 443) + row["host"], row["port"] = host, port + dsts, why, err := p.destination(host, port) + if err != nil { + row["mode"], row["error"] = "refused", err.Error() + reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") + p.log(row) + return + } + row["allowed"] = why + if _, err := io.WriteString(conn, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { + return + } + p.tunnel(conn, br, host, port, dsts, why, row, start) + return + } + u, err := url.Parse(target) + if err != nil || u.Scheme != "http" || u.Host == "" { + row["mode"], row["error"] = "refused", "not an absolute http URL" + reply(conn, "400 Bad Request", "egress: CONNECT, or an absolute http:// URL\n") + p.log(row) + return + } + host, port := splitAuthority(u.Host, 80) + row["host"], row["port"], row["mode"] = host, port, "http" + dsts, why, err := p.destination(host, port) + if err != nil { + row["mode"], row["error"] = "refused", err.Error() + reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") + p.log(row) + return + } + row["allowed"] = why + up, dst, err := p.dialAny(dsts) + if err != nil { + row["error"] = truncate(err.Error(), ErrorLimit) + reply(conn, "502 Bad Gateway", "egress: "+err.Error()+"\n") + p.log(row) + return + } + defer up.Close() + row["dst"] = dst + upgrade := false + for _, h := range headers { + if headerName(h) == "upgrade" { + upgrade = true + } + } + // The client's own head, but for its request line, which names the path alone, and the headers meant for this + // proxy; a request that isn't an upgrade closes once answered, so its connection can't carry one for another site. + var head strings.Builder + head.WriteString(method + " " + u.RequestURI() + " " + version + "\r\n") + for _, h := range headers { + switch headerName(h) { + case "proxy-connection", "proxy-authorization": + continue + case "connection", "keep-alive": + if !upgrade { + continue + } + } + head.WriteString(h + "\r\n") + } + if upgrade { + row["mode"] = "upgrade" + } else { + head.WriteString("Connection: close\r\n") + } + head.WriteString("\r\n") + conn.SetReadDeadline(time.Time{}) + if _, err := io.WriteString(up, head.String()); err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + p.log(row) + return + } + p.log(row) + // Both ways at once: a body the client sends once told to continue, or an upgraded connection's frames. + go func() { + io.Copy(up, br) + closeWrite(up) + }() + io.Copy(conn, up) +} + +// readHead is a request's head, read through its blank line: its request line's parts and its header lines. +func readHead(r *bufio.Reader) (string, string, string, []string, error) { + var lines []string + size := 0 + for { + line, err := r.ReadString('\n') + size += len(line) + if size > HeadLimit { + return "", "", "", nil, errors.New("request head too long") + } + if err != nil { + return "", "", "", nil, err + } + line = strings.TrimRight(line, "\r\n") + if line == "" { + if len(lines) == 0 { + continue + } + break + } + lines = append(lines, line) + } + parts := strings.SplitN(lines[0], " ", 3) + if len(parts) != 3 { + return "", "", "", nil, fmt.Errorf("malformed request line %q", truncate(lines[0], 100)) + } + return parts[0], parts[1], parts[2], lines[1:], nil +} + +func headerName(line string) string { + name, _, _ := strings.Cut(line, ":") + return strings.ToLower(strings.TrimSpace(name)) +} + +// destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for +// a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), +// "global" for a name whose every address is global, IPv4 first. Anything else is refused. +func (p *Proxy) destination(host string, port int) ([]string, string, error) { + host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") + routed := p.Routed + if routed == nil { + routed = routedSites + } + if address, ok := routed()[host]; ok { + return []string{net.JoinHostPort(address, strconv.Itoa(port))}, "routed", nil + } + resolve := p.Resolve + if resolve == nil { + resolve = func(ctx context.Context, host string) ([]netip.Addr, error) { + return net.DefaultResolver.LookupNetIP(ctx, "ip", host) + } + } + allowed := p.Allowed + if allowed == nil { + allowed = isGlobal + } + ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) + defer cancel() + addresses, err := resolve(ctx, host) + if err != nil { + return nil, "", fmt.Errorf("%s doesn't resolve: %w", host, err) + } + if len(addresses) == 0 { + return nil, "", fmt.Errorf("%s has no address", host) + } + var private []string + var v4, v6 []string + for _, a := range addresses { + a = a.Unmap() + dst := netip.AddrPortFrom(a, uint16(port)).String() + switch { + case !allowed(a): + private = append(private, a.String()) + case a.Is4(): + v4 = append(v4, dst) + default: + v6 = append(v6, dst) + } + } + if len(private) > 0 { + return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) + } + return append(v4, v6...), "global", nil +} + +// notGlobal are the special-purpose blocks (IANA's registries) no global address is in. +var notGlobal = func() []netip.Prefix { + var out []netip.Prefix + for _, s := range []string{ + "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", + "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", + "224.0.0.0/4", "240.0.0.0/4", + "::/128", "::1/128", "64:ff9b:1::/48", "100::/64", "2001::/23", "2001:db8::/32", "fc00::/7", "fe80::/10", + "ff00::/8", + } { + out = append(out, netip.MustParsePrefix(s)) + } + return out +}() + +func isGlobal(a netip.Addr) bool { + a = a.Unmap() + if !a.IsValid() { + return false + } + for _, block := range notGlobal { + if block.Contains(a) { + return false + } + } + return true +} + +// routedSites is the hosts file's agentenv sites block, name to address: root's, so the session user can't add to it. +func routedSites() map[string]string { + out := map[string]string{} + data, err := os.ReadFile(Hosts) + if err != nil { + return out + } + inside := false + for _, line := range strings.Split(string(data), "\n") { + text := strings.TrimSpace(line) + switch { + case text == HostsBegin: + inside = true + case text == HostsEnd: + inside = false + case inside && text != "" && !strings.HasPrefix(text, "#"): + fields := strings.Fields(text) + for _, name := range fields[1:] { + out[strings.ToLower(name)] = fields[0] + } + } + } + return out +} + +func (p *Proxy) tunnel(conn net.Conn, br *bufio.Reader, host string, port int, dsts []string, why string, + row map[string]any, start time.Time) { + wait := FirstBytes + if port == 443 { + wait = HelloTimeout + } + conn.SetReadDeadline(time.Now().Add(wait)) + first, err := br.Peek(1) + if err != nil && !isTimeout(err) { + row["error"] = "closed before sending" + p.log(row) + return + } + if len(first) == 0 || first[0] != recordHandshake || why == "routed" { + row["mode"], row["decision"] = "pass", "not TLS" + if len(first) > 0 && first[0] == recordHandshake { + row["mode"], row["decision"] = "splice", "routed" + } + if !p.splice(conn, br, dsts, row, start) { + p.log(row) + } + return + } + conn.SetReadDeadline(time.Now().Add(HelloTimeout)) + hello, err := clientHello(br, time.Now().Add(HelloTimeout)) + if err != nil { + row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) + p.log(row) + return + } + sni, offered, err := parseHello(hello) + if err != nil { + row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) + p.log(row) + return + } + row["sni"], row["alpn_offered"] = sni, offered + verifyName := sni + if verifyName == "" { + verifyName = strings.Trim(host, "[]") + } + armed, universes := p.Universe.View(time.Now()) + if !armed { + row["decision"], row["mode"] = "real", "splice" + if !p.splice(conn, br, dsts, row, start) { + p.log(row) + } + return + } + row["universes"] = rounded(universes) + // Each address in turn, by its own probe: one that doesn't answer is passed over, so the connection goes to a + // server whose own certificate decided it. + var unreachable []string + for _, dst := range p.preferChecked(dsts, sni) { + delete(row, "probe_error") + delete(row, "upstream_valid") + seen, probed := p.probeAt(dst, sni, verifyName, hello) + if seen.unreachable { + unreachable = append(unreachable, seen.problem) + continue + } + row["probed"] = probed + decision := "outside" + switch { + case seen.failed: + // a handshake only the client can finish, as one asking for its certificate: the client decides + decision, row["probe_error"] = "probe_failed", seen.problem + case !seen.verified: + decision, row["probe_error"] = "unverified", seen.problem + case inside(seen.from, seen.until, universes): + decision = "inside" + } + if seen.verified { + row["upstream_valid"] = []int64{seen.from.Unix(), seen.until.Unix()} + } + row["decision"] = decision + var dialled bool + if decision == "inside" || decision == "probe_failed" { + row["mode"] = "splice" + dialled = p.splice(conn, br, []string{dst}, row, start) + } else { + row["mode"] = "intercept" + dialled = p.intercept(conn, br, dst, sni, verifyName, hello, row, start) + } + if dialled { + return + } + // it stopped answering since its probe: forget it, and go on to the next + p.forget(dst, sni) + unreachable = append(unreachable, fmt.Sprint(row["error"])) + delete(row, "error") + } + row["error"] = truncate("upstream: no address answered: "+strings.Join(unreachable, "; "), ErrorLimit) + p.log(row) +} + +// preferChecked is dsts with those whose fresh probe answered first, each group in its own order, so an address +// already found working isn't kept waiting behind one whose probe has to be made again. +func (p *Proxy) preferChecked(dsts []string, sni string) []string { + p.probes.mu.Lock() + defer p.probes.mu.Unlock() + var checked, rest []string + for _, dst := range dsts { + if hit, ok := p.probes.cache[dst+"|"+sni]; ok && hit.fresh() && !hit.unreachable { + checked = append(checked, dst) + } else { + rest = append(rest, dst) + } + } + return append(checked, rest...) +} + +// splice relays the connection to the first of dsts that answers, untouched; false, with the error in row, when none +// does. +func (p *Proxy) splice(conn net.Conn, br *bufio.Reader, dsts []string, row map[string]any, start time.Time) bool { + up, dst, err := p.dialAny(dsts) + if err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + return false + } + defer up.Close() + row["dst"] = dst + conn.SetReadDeadline(time.Time{}) + row["upstream_ms"] = time.Since(start).Milliseconds() + p.log(row) + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + io.Copy(up, br) + closeWrite(up) + }() + io.Copy(conn, up) + closeWrite(conn) + wg.Wait() + return true +} + +// intercept hands the client a leaf of this computer's CA and relays the plaintext to dst over a handshake of the +// proxy's own; false, with the error in row, when dst doesn't answer. +func (p *Proxy) intercept(conn net.Conn, br *bufio.Reader, dst, sni, verifyName string, hello []byte, + row map[string]any, start time.Time) bool { + uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) + var unanswered dialError + if errors.As(err, &unanswered) { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + return false + } + if err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + defer uc.NetConn().Close() + row["dst"] = dst + row["alpn"], row["verified"], row["upstream_tls"] = found.ALPN, found.Verified, []string{found.Version, found.Cipher} + row["upstream_ms"] = time.Since(start).Milliseconds() + if len(found.Dropped) > 0 { + row["dropped_extensions"] = found.Dropped + } + var sans []string + if found.Verified { + // the site's own names, as its leaf lists them, so the client pools connections as it would with the site's + // certificate + for _, name := range found.SANs { + if sni == "" || strings.HasPrefix(name, "DNS:") { + sans = append(sans, name) + } + } + if sni == "" { + sans = append(sans, "IP:"+strings.Trim(hostOf(dst), "[]")) + } + } else { + row["upstream_verify"] = truncate(found.VerifyErr, 200) + } + cert, minted, err := p.Leaves.For(verifyName, sans, found.Verified) + if err != nil { + row["error"] = truncate("leaf: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + row["minted"] = minted + config := &tls.Config{Certificates: []tls.Certificate{*cert}, SessionTicketsDisabled: true} + if found.ALPN != "" { + config.NextProtos = []string{found.ALPN} + } + client := tls.Server(&peeked{Conn: conn, r: br}, config) + conn.SetDeadline(time.Now().Add(HandshakeTimeout)) + if err := client.Handshake(); err != nil { + row["error"] = truncate("client handshake: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + conn.SetDeadline(time.Time{}) + row["handshake_ms"] = time.Since(start).Milliseconds() + p.log(row) + done := make(chan struct{}, 2) + go func() { + io.Copy(uc, client) + done <- struct{}{} + }() + go func() { + io.Copy(client, uc) + done <- struct{}{} + }() + <-done + client.Close() + uc.NetConn().Close() + <-done + return true +} + +// peeked is a connection whose reads come through the reader that peeked its first bytes. +type peeked struct { + net.Conn + r *bufio.Reader +} + +func (c *peeked) Read(b []byte) (int, error) { return c.r.Read(b) } + +func closeWrite(c net.Conn) { + if cw, ok := c.(interface{ CloseWrite() error }); ok { + cw.CloseWrite() + } +} + +func isTimeout(err error) bool { + var ne net.Error + return errors.As(err, &ne) && ne.Timeout() +} + +func reply(conn net.Conn, status, body string) { + fmt.Fprintf(conn, "HTTP/1.1 %s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", + status, len(body), body) +} + +func splitAuthority(authority string, port int) (string, int) { + host, p, err := net.SplitHostPort(authority) + if err != nil { + return strings.Trim(authority, "[]"), port + } + n, err := strconv.Atoi(p) + if err != nil { + return host, port + } + return host, n +} + +func hostOf(hostport string) string { + host, _, err := net.SplitHostPort(hostport) + if err != nil { + return hostport + } + return host +} + +func rounded(values []float64) []int64 { + out := make([]int64, len(values)) + for i, v := range values { + out[i] = int64(v) + } + return out +} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go new file mode 100644 index 000000000..c8e57b3e7 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go @@ -0,0 +1,564 @@ +package main + +import ( + "bufio" + "bytes" + "context" + "crypto/tls" + "crypto/x509" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "net/netip" + "net/url" + "strings" + "sync" + "testing" + "time" +) + +type fixedUniverse struct { + armed bool + universes []float64 +} + +func (f fixedUniverse) View(time.Time) (bool, []float64) { return f.armed, f.universes } + +func TestOnlyDestinationsWhoseEveryAddressIsGlobalAreDialled(t *testing.T) { + refused := []string{"10.0.0.1", "10.255.255.254", "172.16.0.1", "172.31.255.1", "192.168.1.1", "127.0.0.1", + "127.1.2.3", "0.0.0.0", "169.254.169.254", "169.254.1.1", "100.64.0.1", "100.127.255.254", "192.0.2.1", + "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::", "fd00::1", "fe80::1", "::ffff:10.0.0.1", + "2001:db8::1"} + if len(refused) != 22 { + t.Fatal(len(refused)) + } + p := &Proxy{Routed: func() map[string]string { return nil }} + for _, address := range refused { + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr(address)}, nil + } + if _, _, err := p.destination("somewhere.example", 443); err == nil { + t.Errorf("%s dialled", address) + } + } + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("2606:4700::1111"), netip.MustParseAddr("1.1.1.1")}, nil + } + dst, why, err := p.destination("one.one.one.one", 443) + if err != nil || strings.Join(dst, " ") != "1.1.1.1:443 [2606:4700::1111]:443" || why != "global" { + t.Fatalf("a public name, IPv4 first: %q %q %v", dst, why, err) + } + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("10.0.0.1")}, nil + } + if _, _, err := p.destination("split.example", 443); err == nil || !strings.Contains(err.Error(), "10.0.0.1") { + t.Fatalf("a name with one private address: %v", err) + } + p.Routed = func() map[string]string { return map[string]string{"notes.test": "127.0.0.1"} } + if dst, why, err := p.destination("Notes.Test.", 443); err != nil || strings.Join(dst, " ") != "127.0.0.1:443" || + why != "routed" { + t.Fatalf("a routed site: %q %q %v", dst, why, err) + } +} + +// harness is a proxy in front of the site server, which it reaches as example.com, and a client whose clock is at +// clientTime. +type harness struct { + t *testing.T + site *httptest.Server + proxy *Proxy + url *url.URL + ca signer + clientCert *tls.Certificate + rows []map[string]any + rowsMu sync.Mutex +} + +func newHarness(t *testing.T, site *httptest.Server, universe fixedUniverse, trustSite bool) *harness { + t.Helper() + ca, err := mintCA(CAName) + if err != nil { + t.Fatal(err) + } + leaves, err := NewLeaves(ca) + if err != nil { + t.Fatal(err) + } + roots := x509.NewCertPool() + if trustSite && site.Certificate() != nil { + roots.AddCert(site.Certificate()) + } + h := &harness{t: t, site: site, ca: ca} + h.proxy = &Proxy{ + Leaves: leaves, Universe: universe, Roots: roots, + Resolve: func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil + }, + Allowed: func(netip.Addr) bool { return true }, + Routed: func() map[string]string { return nil }, + Log: func(row map[string]any) { + h.rowsMu.Lock() + h.rows = append(h.rows, row) + h.rowsMu.Unlock() + }, + } + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { ln.Close() }) + go h.proxy.Serve(ln) + h.url, _ = url.Parse("http://" + ln.Addr().String()) + return h +} + +func (h *harness) get(scheme string, clientTime time.Time, http2 bool) (*http.Response, string, error) { + roots := x509.NewCertPool() + if h.site.Certificate() != nil { + roots.AddCert(h.site.Certificate()) + } + roots.AddCert(h.ca.cert) + config := &tls.Config{RootCAs: roots, Time: func() time.Time { return clientTime }} + if h.clientCert != nil { + config.Certificates = []tls.Certificate{*h.clientCert} + } + transport := &http.Transport{Proxy: http.ProxyURL(h.url), ForceAttemptHTTP2: http2, TLSClientConfig: config} + defer transport.CloseIdleConnections() + _, port, _ := net.SplitHostPort(h.site.Listener.Addr().String()) + resp, err := (&http.Client{Transport: transport, Timeout: 10 * time.Second}).Get( + fmt.Sprintf("%s://example.com:%s/hello", scheme, port)) + if err != nil { + return nil, "", err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + return resp, string(body), err +} + +func (h *harness) lastRow() map[string]any { + deadline := time.Now().Add(2 * time.Second) + for time.Now().Before(deadline) { + h.rowsMu.Lock() + n := len(h.rows) + h.rowsMu.Unlock() + if n > 0 { + break + } + time.Sleep(10 * time.Millisecond) + } + h.rowsMu.Lock() + defer h.rowsMu.Unlock() + if len(h.rows) == 0 { + h.t.Fatal("no row logged") + } + return h.rows[len(h.rows)-1] +} + +func site(t *testing.T, http2 bool) *httptest.Server { + s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello over %s", r.Proto) + })) + s.EnableHTTP2 = http2 + s.StartTLS() + t.Cleanup(s.Close) + return s +} + +func TestOnRealTimeTheClientsBytesReachTheSiteUntouched(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + resp, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello over HTTP/1.1" { + t.Fatalf("%q %v", body, err) + } + if !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { + t.Fatal("the client didn't see the site's own certificate") + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "real" { + t.Fatalf("%v", row) + } +} + +func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t *testing.T) { + s := site(t, true) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now, now}}, true) + resp, _, err := h.get("https", time.Now(), true) + if err != nil || !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { + t.Fatalf("inside: %v", err) + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "inside" { + t.Fatalf("%v", row) + } + + // httptest's certificate ends in 2084: a universe in 2090 is outside it. + later := time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC) + u := float64(later.Unix()) + h = newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u, u}}, true) + resp, body, err := h.get("https", later, true) + if err != nil || body != "hello over HTTP/2.0" { + t.Fatalf("outside: %q %v", body, err) + } + leaf := resp.TLS.PeerCertificates[0] + if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { + t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) + } + row := h.lastRow() + if row["mode"] != "intercept" || row["decision"] != "outside" || row["alpn"] != "h2" || row["verified"] != true { + t.Fatalf("%v", row) + } +} + +func TestASiteThatDoesntVerifyIsHandedOnUnderACATrustedNowhere(t *testing.T) { + s := site(t, false) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, false) + _, _, err := h.get("https", time.Now(), false) + if err == nil || !strings.Contains(err.Error(), "certificate") { + t.Fatalf("the client took it: %v", err) + } + if row := h.lastRow(); row["decision"] != "unverified" { + t.Fatalf("%v", row) + } +} + +func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "plain %s %s ua=%q", r.Method, r.URL.Path, r.UserAgent()) + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, body, err := h.get("http", time.Now(), false) + if err != nil || !strings.HasPrefix(body, "plain GET /hello") { + t.Fatalf("%q %v", body, err) + } +} + +func TestARefusedDestinationIsAnswered403(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + h.proxy.Allowed = isGlobal + _, _, err := h.get("https", time.Now(), false) + if err == nil || !strings.Contains(err.Error(), "Forbidden") { + t.Fatalf("%v", err) + } + if row := h.lastRow(); row["mode"] != "refused" { + t.Fatalf("%v", row) + } +} + +func TestEveryCheckedAddressIsTriedInTurn(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil + } + _, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello over HTTP/1.1" { + t.Fatalf("%q %v", body, err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "127.0.0.1:") { + t.Fatalf("%v", row) + } +} + +func TestAnUpgradeOverPlainHTTPRelaysBothWays(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Upgrade") != "echo" || !strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") { + w.WriteHeader(http.StatusBadRequest) + return + } + conn, rw, _ := w.(http.Hijacker).Hijack() + defer conn.Close() + rw.WriteString("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: echo\r\n\r\n") + rw.Flush() + line, _ := rw.ReadString('\n') + rw.WriteString(line) + rw.Flush() + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) + conn, err := net.Dial("tcp", h.url.Host) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + conn.SetDeadline(time.Now().Add(5 * time.Second)) + fmt.Fprintf(conn, "GET http://example.com:%s/ws HTTP/1.1\r\nHost: example.com:%s\r\nConnection: Upgrade\r\n"+ + "Upgrade: echo\r\n\r\n", port, port) + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, nil) + if err != nil || resp.StatusCode != http.StatusSwitchingProtocols { + t.Fatalf("%v %v", resp, err) + } + fmt.Fprint(conn, "ping\n") + if line, err := br.ReadString('\n'); err != nil || line != "ping\n" { + t.Fatalf("%q %v", line, err) + } +} + +func TestAnUploadThatWaitsFor100ContinueGoesThroughAtOnce(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + fmt.Fprint(w, len(body)) + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) + req, _ := http.NewRequest("POST", "http://example.com:"+port+"/up", bytes.NewReader(make([]byte, 100000))) + req.Header.Set("Expect", "100-continue") + transport := &http.Transport{Proxy: http.ProxyURL(h.url), ExpectContinueTimeout: 10 * time.Second} + defer transport.CloseIdleConnections() + started := time.Now() + resp, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(req) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + if string(body) != "100000" || time.Since(started) > 5*time.Second { + t.Fatalf("%q after %s", body, time.Since(started)) + } +} + +func TestEveryCertificateOfTheVerifiedChainBoundsPassingThrough(t *testing.T) { + year := func(y int) time.Time { return time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) } + leaf := &x509.Certificate{NotBefore: year(2020), NotAfter: year(2030)} + intermediate := &x509.Certificate{NotBefore: year(2019), NotAfter: year(2025)} + root := &x509.Certificate{NotBefore: year(2000), NotAfter: year(2040)} + from, until := chainWindow([][]*x509.Certificate{{leaf, intermediate, root}}) + if !from.Equal(year(2020)) || !until.Equal(year(2025)) { + t.Fatalf("%s to %s", from, until) + } + if inside(from, until, []float64{float64(year(2027).Unix())}) { + t.Fatal("a universe past the intermediate's end passed through") + } +} + +func TestASiteAskingForTheClientsCertificateIsPassedThroughForTheClientToAnswer(t *testing.T) { + s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello %d certificate", len(r.TLS.PeerCertificates)) + })) + s.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, MaxVersion: tls.VersionTLS12} + s.StartTLS() + t.Cleanup(s.Close) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) + client, err := mintCA("client") + if err != nil { + t.Fatal(err) + } + h.clientCert = &tls.Certificate{Certificate: [][]byte{client.cert.Raw}, PrivateKey: client.key} + _, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello 1 certificate" { + t.Fatalf("%q %v", body, err) + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "probe_failed" { + t.Fatalf("%v", row) + } +} + +func TestTheProbeAndLeafCachesStayWithinTheirLimit(t *testing.T) { + limit := cacheLimit + cacheLimit = 3 + defer func() { cacheLimit = limit }() + ca, _ := mintCA(CAName) + leaves, _ := NewLeaves(ca) + for i := 0; i < 10; i++ { + if _, _, err := leaves.For(fmt.Sprintf("site%d.test", i), nil, true); err != nil { + t.Fatal(err) + } + } + if len(leaves.cache) != 3 || leaves.cache["site9.test|"] == nil { + t.Fatalf("%d leaves kept", len(leaves.cache)) + } + p := &Proxy{Dial: func(string) (net.Conn, error) { return nil, fmt.Errorf("unreachable") }} + for i := 0; i < 10; i++ { + p.probeAt(fmt.Sprintf("192.0.2.%d:443", i), "", "x", goHello(t, "x", nil)) + } + if len(p.probes.cache) != 3 { + t.Fatalf("%d probes kept", len(p.probes.cache)) + } +} + +func TestAConnectionTheProbeLetsThroughGoesToTheServerItProbed(t *testing.T) { + s := site(t, false) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil + } + var mu sync.Mutex + var dialled []string + h.proxy.Dial = func(dst string) (net.Conn, error) { + mu.Lock() + dialled = append(dialled, dst) + mu.Unlock() + return net.DialTimeout("tcp", dst, DialTimeout) + } + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + row := h.lastRow() + mu.Lock() + defer mu.Unlock() + // the probe tried 127.0.0.2, then reached 127.0.0.1; the connection went straight to 127.0.0.1 + if row["decision"] != "inside" || len(dialled) != 3 || !strings.HasPrefix(dialled[2], "127.0.0.1:") { + t.Fatalf("%v %v", row, dialled) + } + if hit := h.proxy.probes.cache[dialled[0]+"|example.com"]; !hit.unreachable || hit.verified { + t.Fatalf("the address that didn't answer is cached as %+v", hit) + } +} + +func TestASlowProbeHoldsUpNoOtherSite(t *testing.T) { + release := make(chan struct{}) + p := &Proxy{Dial: func(dst string) (net.Conn, error) { + if strings.HasPrefix(dst, "192.0.2.1:") { + <-release + } + return nil, fmt.Errorf("unreachable") + }} + hello := goHello(t, "x", nil) + p.probeAt("192.0.2.2:443", "", "x", hello) + go p.probeAt("192.0.2.1:443", "", "x", hello) + time.Sleep(50 * time.Millisecond) + started := time.Now() + if _, probed := p.probeAt("192.0.2.2:443", "", "x", hello); probed || time.Since(started) > 100*time.Millisecond { + t.Fatalf("a cached site waited %s behind a slow one (probed again: %v)", time.Since(started), probed) + } + close(release) +} + +// standIns resolves example.com to its addresses and dials each to the site unless it's down; both may change +// between requests. +type standIns struct { + mu sync.Mutex + addresses []string + down map[string]bool +} + +func (s *standIns) set(down []string, addresses ...string) { + s.mu.Lock() + defer s.mu.Unlock() + s.addresses, s.down = addresses, map[string]bool{} + for _, a := range down { + s.down[a] = true + } +} + +func newStandIns(h *harness, addresses ...string) *standIns { + s := &standIns{addresses: addresses, down: map[string]bool{}} + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + s.mu.Lock() + defer s.mu.Unlock() + var out []netip.Addr + for _, a := range s.addresses { + out = append(out, netip.MustParseAddr(a)) + } + return out, nil + } + h.proxy.Dial = func(dst string) (net.Conn, error) { + host, _, _ := net.SplitHostPort(dst) + s.mu.Lock() + down := s.down[host] + s.mu.Unlock() + if down { + return nil, fmt.Errorf("%s is down", host) + } + return net.DialTimeout("tcp", h.site.Listener.Addr().String(), DialTimeout) + } + return s +} + +func TestAnAddressThatStopsAnsweringAfterItsProbeGivesWayToTheNext(t *testing.T) { + s := site(t, false) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) + addresses := newStandIns(h, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.1:") { + t.Fatalf("%v", row) + } + addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["decision"] != "inside" { + t.Fatalf("%v", row) + } +} + +func TestAnAddressThatDidntAnswerLeavesANewAddressToItsOwnProbe(t *testing.T) { + s := site(t, false) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) + addresses := newStandIns(h) + addresses.set([]string{"198.51.100.1"}, "198.51.100.1") + if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err == nil { + t.Fatal("reached a site none of whose addresses answer") + } + addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err != nil { + t.Fatal(err) + } + row := h.lastRow() + if !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["mode"] != "intercept" || row["decision"] != "outside" { + t.Fatalf("the new address wasn't judged by its own certificate: %v", row) + } +} + +func TestAClientHelloWhoseFieldsOverrunItIsRefusedNotPanickedOn(t *testing.T) { + hello := goHello(t, "example.com", nil) + p := 4 + 2 + 32 + p += 1 + int(hello[p]) + p += 2 + int(hello[p])<<8 | int(hello[p+1]) + bad := append([]byte{}, hello...) + bad[p] = 0xff // compression methods' length, past the message's end + bad = bad[:p+3] + if _, _, err := parseHello(bad); err == nil { + t.Fatal("parseHello took it") + } + if _, _, err := helloRecord(bad); err == nil { + t.Fatal("helloRecord took it") + } +} + +func TestAProbeThatPanicsLeavesNoOneWaitingOnIt(t *testing.T) { + p := &Proxy{Dial: func(string) (net.Conn, error) { panic("dial") }} + hello := goHello(t, "x", nil) + probe := func() (panicked bool) { + defer func() { panicked = recover() != nil }() + p.probeAt("192.0.2.9:443", "", "x", hello) + return false + } + if !probe() { + t.Fatal("the probe didn't panic") + } + done := make(chan bool) + go func() { done <- probe() }() + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("a second probe waited on the one that panicked") + } +} + +func TestAnAddressAlreadyFoundWorkingIsTriedBeforeOnesNotYetProbed(t *testing.T) { + p := &Proxy{} + p.probes.cache = map[string]probe{ + "[2001:db8::1]:443|x": {verified: true, at: time.Now()}, + "192.0.2.1:443|x": {unreachable: true, at: time.Now().Add(-2 * ProbeFailedTTL)}, + "192.0.2.3:443|x": {unreachable: true, at: time.Now()}, + } + got := p.preferChecked([]string{"192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443", "[2001:db8::1]:443"}, "x") + want := []string{"[2001:db8::1]:443", "192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443"} + if strings.Join(got, " ") != strings.Join(want, " ") { + t.Fatalf("%v", got) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex new file mode 100644 index 000000000..2aecf27e8 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex @@ -0,0 +1 @@ +4145434b02000000040000000000000000008ab9359ae51515cd68be7e91e51800000000000000400100000000000000070000000000000002b1a878f27237d300f2052a0100000000f2052a01000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex new file mode 100644 index 000000000..7f6a95f54 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex @@ -0,0 +1 @@ +4145434b02000000020000000000000000000000000000000000000000000000000000000000f03f00000000000000000300000000000000147c4a7fb979c7a100000000000000000000000000000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/upstream.go b/src/agent_env/agents/agents/claude-code/egress/upstream.go new file mode 100644 index 000000000..ccf32704f --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/upstream.go @@ -0,0 +1,268 @@ +package main + +import ( + "crypto/x509" + "errors" + "fmt" + "net" + "strings" + "sync" + "time" + + utls "github.com/refraction-networking/utls" +) + +// upstream is what a handshake with a site found: the protocol it picked, its leaf's validity and names, and whether +// that leaf verifies against real time and the public roots. +type upstream struct { + // Dst is the address the handshake reached. + Dst string + ALPN string + Verified bool + VerifyErr string + NotBefore time.Time + NotAfter time.Time + SANs []string + Version string + Cipher string + Dropped []uint16 +} + +// handshake is uTLS's handshake with the site at dst, built from the client's own ClientHello, so the site sees the +// client's fingerprint and ALPN offer. Records go full-size from the start and the connection closes without a +// close_notify, as Chromium's BoringSSL does. +func (p *Proxy) handshake(dsts []string, sni, verifyName string, hello []byte) (*utls.UConn, upstream, error) { + var found upstream + record, gone, err := helloRecord(hello) + if err != nil { + return nil, found, err + } + found.Dropped = gone + spec, err := (&utls.Fingerprinter{AllowBluntMimicry: true}).FingerprintClientHello(record) + if err != nil { + return nil, found, fmt.Errorf("fingerprint: %w", err) + } + raw, dst, err := p.dialAny(dsts) + if err != nil { + return nil, found, dialError{err} + } + found.Dst = dst + uc := utls.UClient(raw, &utls.Config{ServerName: sni, InsecureSkipVerify: true, OmitEmptyPsk: true, + DynamicRecordSizingDisabled: true}, utls.HelloCustom) + if err := uc.ApplyPreset(spec); err != nil { + raw.Close() + return nil, found, fmt.Errorf("apply: %w", err) + } + raw.SetDeadline(time.Now().Add(HandshakeTimeout)) + if err := uc.Handshake(); err != nil { + raw.Close() + return nil, found, fmt.Errorf("handshake: %w", err) + } + raw.SetDeadline(time.Time{}) + st := uc.ConnectionState() + found.ALPN, found.Version, found.Cipher = st.NegotiatedProtocol, fmt.Sprintf("%#04x", st.Version), + utls.CipherSuiteName(st.CipherSuite) + if len(st.PeerCertificates) == 0 { + found.VerifyErr = "no certificate" + return uc, found, nil + } + leaf := st.PeerCertificates[0] + found.NotBefore, found.NotAfter = leaf.NotBefore, leaf.NotAfter + for _, name := range leaf.DNSNames { + found.SANs = append(found.SANs, "DNS:"+name) + } + for _, ip := range leaf.IPAddresses { + found.SANs = append(found.SANs, "IP:"+ip.String()) + } + intermediates := x509.NewCertPool() + for _, c := range st.PeerCertificates[1:] { + intermediates.AddCert(c) + } + chains, err := leaf.Verify(x509.VerifyOptions{Roots: p.Roots, Intermediates: intermediates, DNSName: verifyName, + CurrentTime: time.Now()}) + if err != nil { + found.VerifyErr = err.Error() + } else { + found.Verified = true + found.NotBefore, found.NotAfter = chainWindow(chains) + } + return uc, found, nil +} + +// chainWindow is the dates every certificate of every verified chain is valid between: a client building any of +// those chains takes the site only then, so passing it through is safe only inside them. +func chainWindow(chains [][]*x509.Certificate) (time.Time, time.Time) { + var from, until time.Time + for _, chain := range chains { + for _, cert := range chain { + if from.IsZero() || cert.NotBefore.After(from) { + from = cert.NotBefore + } + if until.IsZero() || cert.NotAfter.Before(until) { + until = cert.NotAfter + } + } + } + return from, until +} + +// probe is what a handshake of the proxy's own found of one address of a site: the dates its verified chain shares, +// or that it didn't answer, or that its handshake didn't finish. It's cached per (address, SNI): a finished one for +// ProbeTTL, the others for ProbeFailedTTL, since they may be a moment's trouble. +type probe struct { + from, until time.Time + verified bool + // failed is a handshake that didn't finish; unreachable, an address that didn't answer. + failed, unreachable bool + at time.Time + problem string +} + +func (hit probe) fresh() bool { + if hit.failed || hit.unreachable { + return time.Since(hit.at) < ProbeFailedTTL + } + return time.Since(hit.at) < ProbeTTL +} + +// probes caches each address's probe, up to cacheLimit entries, the stale ones dropped first once it's full. An +// address's concurrent probes wait on the one under way; no lock is held while one is. +type probes struct { + mu sync.Mutex + cache map[string]probe + underway map[string]*underway +} + +type underway struct { + done chan struct{} + hit probe + ok bool +} + +// cacheLimit bounds the probe and leaf caches, so a computer that visits many sites keeps a bounded memory. +var cacheLimit = 4096 + +// dialError is a handshake's that never reached the site. +type dialError struct{ error } + +// probeAt is the probe of dst for sni, cached or new, and whether it probed now. +func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) { + key := dst + "|" + sni + p.probes.mu.Lock() + if p.probes.cache == nil { + p.probes.cache, p.probes.underway = map[string]probe{}, map[string]*underway{} + } + if hit, ok := p.probes.cache[key]; ok && hit.fresh() { + p.probes.mu.Unlock() + return hit, false + } + if running, ok := p.probes.underway[key]; ok { + p.probes.mu.Unlock() + <-running.done + if !running.ok { + return probe{at: time.Now(), failed: true, problem: "the probe it waited on failed"}, false + } + return running.hit, false + } + running := &underway{done: make(chan struct{})} + p.probes.underway[key] = running + p.probes.mu.Unlock() + defer func() { + p.probes.mu.Lock() + if running.ok { + p.store(key, running.hit) + } + delete(p.probes.underway, key) + p.probes.mu.Unlock() + close(running.done) + }() + + hit := probe{at: time.Now()} + uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) + var unanswered dialError + switch { + case errors.As(err, &unanswered): + hit.unreachable, hit.problem = true, truncate(err.Error(), 200) + case err != nil: + hit.failed, hit.problem = true, truncate(err.Error(), 200) + case !found.Verified: + hit.problem = truncate(found.VerifyErr, 200) + default: + hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true + } + if uc != nil { + uc.NetConn().Close() + } + running.hit, running.ok = hit, true + return hit, true +} + +// store keeps hit for key, dropping the stale entries, then any others, once the cache is past cacheLimit. +func (p *Proxy) store(key string, hit probe) { + p.probes.cache[key] = hit + if len(p.probes.cache) <= cacheLimit { + return + } + for k, v := range p.probes.cache { + if !v.fresh() { + delete(p.probes.cache, k) + } + } + for k := range p.probes.cache { + if len(p.probes.cache) <= cacheLimit { + break + } + if k != key { + delete(p.probes.cache, k) + } + } +} + +// forget drops dst's probe for sni, for an address that stopped answering since. +func (p *Proxy) forget(dst, sni string) { + p.probes.mu.Lock() + delete(p.probes.cache, dst+"|"+sni) + p.probes.mu.Unlock() +} + +// inside is whether every universe lies inside [from + Margin, until - Margin]. +func inside(from, until time.Time, universes []float64) bool { + lo := float64(from.Add(Margin).UnixNano()) / 1e9 + hi := float64(until.Add(-Margin).UnixNano()) / 1e9 + for _, u := range universes { + if u < lo || u > hi { + return false + } + } + return true +} + +// dialAny dials each address in turn until one answers: which, or every one's error. +func (p *Proxy) dialAny(dsts []string) (net.Conn, string, error) { + var problems []string + for _, dst := range dsts { + conn, err := p.dial(dst) + if err == nil { + return conn, dst, nil + } + problems = append(problems, err.Error()) + } + if len(problems) == 0 { + return nil, "", errors.New("no address to dial") + } + return nil, "", errors.New(strings.Join(problems, "; ")) +} + +func (p *Proxy) dial(dst string) (net.Conn, error) { + if p.Dial != nil { + return p.Dial(dst) + } + return net.DialTimeout("tcp", dst, DialTimeout) +} + +func truncate(s string, n int) string { + if len(s) > n { + return s[:n] + } + return s +} diff --git a/src/agent_env/agents/agents/claude-code/root.conf b/src/agent_env/agents/agents/claude-code/root.conf new file mode 100644 index 000000000..cfa21e733 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/root.conf @@ -0,0 +1,62 @@ +; Root's supervisord (start-agent, FD-5916): the clock follower and the egress proxy, each as its own user, on real +; time, and the A2A server as the agent's user. The server starts with the real-time marker, which agent-env's +; libfaketime takes out of its environment, so the server's own records keep real timestamps and everything it starts, +; the CLI and the model's commands, reads the env's time. Its socket and logs are root's alone, so the agent's user can +; neither stop nor restart any of them. The follower follows nothing until the server writes its config, which +; sync_env_clock has it do, then picks it up within a quarter second. +[supervisord] +nodaemon=true +user=root +logfile=/var/log/agentenv-root/supervisord.log +pidfile=/run/agentenv-root/supervisord.pid +childlogdir=/var/log/agentenv-root + +[unix_http_server] +file=/run/agentenv-root/supervisor.sock +chmod=0700 + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///run/agentenv-root/supervisor.sock + +[program:clock] +command=/usr/local/bin/python3 -I /usr/local/lib/agentenv-clock/clock-follower +user=agentenv-clock +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/clock.log +environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" + +[program:egress] +command=/usr/local/lib/agentenv-egress/egress serve +user=agentenv-egress +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/egress.log +environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" + +; uvicorn itself, not entrypoint.sh: the marker holds for the process it's given to alone, and a shell that execs +; uvicorn would hand uvicorn an environment without it. +[program:agent] +command=/usr/local/bin/python3 -m uvicorn a2a_server:app --host 0.0.0.0 --port %(ENV_A2A_PORT)s +user=agent +directory=/app +priority=10 +autorestart=true +stopasgroup=true +killasgroup=true +stopwaitsecs=30 +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +environment=HOME="/home/agent",USER="agent",LOGNAME="agent",AGENTENV_REAL_TIME="1",AGENTENV_EGRESS_PROXY="http://127.0.0.1:3128",NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem",SSL_CERT_FILE="/etc/agentenv/ca-bundle.pem",REQUESTS_CA_BUNDLE="/etc/agentenv/ca-bundle.pem" diff --git a/src/agent_env/agents/agents/claude-code/start-agent b/src/agent_env/agents/agents/claude-code/start-agent new file mode 100755 index 000000000..1f170978d --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/start-agent @@ -0,0 +1,19 @@ +#!/bin/sh +# The image's start (FD-5916). As root, it makes what the agent's user must not touch, the clock page and file (the +# clock user's from their first byte) and the egress proxy's CA (its key the egress user's alone, trusted in the +# system store, agent-env's trust files and every certifi bundle), then starts root's supervisord (root.conf), which +# runs the clock follower and the egress proxy, each as its own user on real time, and the A2A server as the agent's +# user on real time, everything it starts reading the env's time from the page. Until sync_env_clock has the agent +# follow a clock, the page holds real time and the proxy relays TLS untouched. Started as another user, the image runs +# the A2A server alone, as before, on real time. +set -eu +if [ "$(id -u)" != 0 ]; then + exec /opt/agent/entrypoint.sh +fi +install -d -o root -g root -m 0700 /var/log/agentenv-root /run/agentenv-root +install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock +python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock +AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off \ + /usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \ + || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; } +exec supervisord -c /etc/agentenv/root.conf diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go index e1ea67894..031555627 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go @@ -31,8 +31,10 @@ type bootPaths struct { NSS func(paths bootPaths) error } +// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in +// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. func defaultBootPaths() bootPaths { - return bootPaths{ + p := bootPaths{ Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, UpdateTrust: []string{"update-ca-certificates"}, @@ -41,6 +43,13 @@ func defaultBootPaths() bootPaths { EgressUser: EgressUser, SessionUser: SessionUser, NSS: sessionNSS, } + if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { + p.SessionUser = name + } + if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { + p.NSS = nil + } + return p } // boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go index 239d8db0a..c5f59ad0a 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go @@ -86,3 +86,14 @@ func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { t.Fatalf("%v", report) } } + +func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { + if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { + t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) + } + t.Setenv("AGENTENV_SESSION_USER", "agent") + t.Setenv("AGENTENV_EGRESS_NSS", "off") + if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { + t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) + } +} diff --git a/src/agent_env/env/gateway/clock_followers.py b/src/agent_env/env/gateway/clock_followers.py index 5b89a351f..0f9e4cd66 100644 --- a/src/agent_env/env/gateway/clock_followers.py +++ b/src/agent_env/env/gateway/clock_followers.py @@ -1,4 +1,5 @@ -"""The followers of the gateway's clock (the computers on it): each long-polls ``/clock/watch`` with the version it +"""The followers of the gateway's clock (the computers on it, declared at its start, and the agents a run declares +after it, ``declare``): each long-polls ``/clock/watch`` with the version it shows, which is its acknowledgement, and a clock write can wait until every one shows the write's version.""" from __future__ import annotations @@ -44,6 +45,15 @@ def __init__(self, clock: Callable[[], Clock], tokens: Mapping[str, str]): self._changed = asyncio.Event() self._acks = asyncio.Condition() + def declare(self, name: str, token: str) -> None: + """Declare ``name`` a follower, with the read token its watch carries, after the gateway started: an agent the + run deploys. Declared again, as a redeploy does, it takes the new token and is waited for afresh.""" + if not name or not token: + raise ClockError("a follower takes a name and a read token") + self._tokens[name] = token + self._shown[name] = None + self._seen.pop(name, None) + def changed(self) -> None: """Answer every held watch: the clock has a new version.""" self._changed.set() diff --git a/src/agent_env/env/gateway/constants.py b/src/agent_env/env/gateway/constants.py index 31264e87f..4bdb1555a 100644 --- a/src/agent_env/env/gateway/constants.py +++ b/src/agent_env/env/gateway/constants.py @@ -76,6 +76,7 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int: CONTROL_TOKEN_CHILDREN_ENV = "CONTROL_TOKEN_CHILDREN" # The route a clock follower long-polls, with its read token. CLOCK_WATCH_PATH = "/clock/watch" +CLOCK_FOLLOWERS_PATH = "/clock/followers" # How long /clock/watch holds a watch naming the current version before answering it unchanged; a follower's request # timeout must be longer. The card's watch method advertises it as hold_s. CLOCK_WATCH_HOLD_S = 25.0 @@ -187,6 +188,7 @@ def sources(names: list[str]) -> str: f"/tools/{TOOL_DISABLE_ACTION}", f"/tools/{TOOL_ENABLE_ACTION}", "/triggers/register", "/triggers/remove", "/triggers/clear", "/notifications/config", "/notifications/take", "/notifications/clear", + CLOCK_FOLLOWERS_PATH, }) @@ -249,7 +251,7 @@ def random_mcp_server_name() -> str: }, { "uri": EXT_CLOCK_URI, - "description": "Virtual clock (urn:agentenv:clock/v1): read the gateway's virtual time (wall-clock-derived, rate-scaled) via get_time; set_time/set_rate/advance/clear are the harness control path, each changing the clock's opaque version and, given wait_followers_s, answering 504 naming the followers that don't show it in time (set_rate keeps t0, as advance does, so time triggers keep their marks; rate 0 freezes the clock); a follower follows it through watch, a long poll naming the version it shows and held up to hold_s; state's followers block is diagnostic, not part of the contract.", + "description": "Virtual clock (urn:agentenv:clock/v1): read the gateway's virtual time (wall-clock-derived, rate-scaled) via get_time; set_time/set_rate/advance/clear are the harness control path, each changing the clock's opaque version and, given wait_followers_s, answering 504 naming the followers that don't show it in time (set_rate keeps t0, as advance does, so time triggers keep their marks; rate 0 freezes the clock); declare_follower adds a follower after the gateway started (an agent the run deploys), with the read token its watches carry; a follower follows it through watch, a long poll naming the version it shows and held up to hold_s; state's followers block is diagnostic, not part of the contract.", "params": {"endpoint": "/clock/time", "methods": { "get_time": {"method": "GET", "endpoint": "/clock/time"}, "set_time": {"method": "PUT", "endpoint": "/clock/set-time", "request": {"type": "object", "properties": {"virtual_time": {"type": "string"}, "virtual_seconds_per_real_second": {"type": "number"}, "time_zone": {"type": "string"}, **_WAIT_FOLLOWERS}, "required": ["virtual_time"]}}, @@ -257,6 +259,7 @@ def random_mcp_server_name() -> str: "advance": {"method": "PUT", "endpoint": "/clock/advance", "request": {"type": "object", "properties": {"virtual_time": {"type": "string"}, **_WAIT_FOLLOWERS}, "required": ["virtual_time"]}}, "clear": {"method": "POST", "endpoint": "/clock/clear", "request": {"type": "object", "properties": {**_WAIT_FOLLOWERS}}}, "state": {"method": "GET", "endpoint": "/clock/state", "request": {"type": "object", "properties": {}}}, + "declare_follower": {"method": "PUT", "endpoint": CLOCK_FOLLOWERS_PATH, "request": {"type": "object", "properties": {"name": {"type": "string"}, "read_token": {"type": "string"}}, "required": ["name", "read_token"]}}, "watch": {"method": "GET", "endpoint": CLOCK_WATCH_PATH, "hold_s": CLOCK_WATCH_HOLD_S, "request": {"type": "object", "properties": {"after": {"type": "string"}, "follower": {"type": "string"}}}}}}, "required": False, }, diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index cd8e180fe..2101400ec 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -46,6 +46,7 @@ AGENT_ENV_SESSION_META_KEY, # noqa: F401 CARD_FETCH_TIMEOUT_S, CHANGES_PAGE_DEFAULT, + CLOCK_FOLLOWERS_PATH, CLOCK_WATCH_PATH, CHANGES_PAGE_MAX, CHILD_DISCOVERY_TIMEOUT_S, @@ -386,6 +387,20 @@ async def clock_watch(request: Request) -> Response: await self._clock_followers.watch(request.query_params.get("after", ""), follower) return self._json_response({**self._clock.state(), "received": received, "sent": time.time()}, 200) + @self._mcp.custom_route(CLOCK_FOLLOWERS_PATH, methods=["PUT"]) + async def clock_declare_follower(request: Request) -> Response: + try: + body = await request.json() + except Exception: + return self._json_response({"ok": False, "error": "Invalid JSON body"}, 400) + if not isinstance(body, dict): + return self._json_response({"ok": False, "error": "body must be a JSON object"}, 400) + try: + self._clock_followers.declare(str(body.get("name") or ""), str(body.get("read_token") or "")) + except ClockError as e: + return self._json_response({"ok": False, "error": str(e)}, 400) + return self._json_response({"ok": True, "followers": self._clock_followers.state()}, 200) + @self._mcp.custom_route("/clock/time", methods=["GET"]) async def clock_time(request: Request) -> Response: try: diff --git a/src/agent_env/env/gateway/get_time.py b/src/agent_env/env/gateway/get_time.py index 24767249f..9d4172223 100644 --- a/src/agent_env/env/gateway/get_time.py +++ b/src/agent_env/env/gateway/get_time.py @@ -17,9 +17,8 @@ GET_TIME_TOOL_NAME = "get_time" GET_TIME_TOOL_DESCRIPTION = ( - "Returns the current date and time for this environment. The clock on the machine you are " - "running on is not synchronized with this environment, so do not use it and do not rely on " - "your own assumption about today's date. Call this tool and derive from its value every " + "Returns the current date and time for this environment. Do not rely on your own assumption " + "about today's date. Call this tool and derive from its value every " "absolute date, time or timestamp you pass to another tool, and every relative phrase you " 'resolve ("today", "this week", "overdue", "the last 30 days"). current_time is in UTC, as ' "are the dates and times the other tools here take, so derive those from current_time. The " diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py new file mode 100644 index 000000000..a722f9101 --- /dev/null +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -0,0 +1,110 @@ +"""Agents on the env's clock (FD-5916): an agent whose image runs the clock follower serves clock/v1's ``sync_time`` +(``agentenv_protocol.a2a_agent.clock``), and follows the gateway's clock the way a computer does, so every program it +runs reads the env's time. ``follow`` declares it at the gateway as a follower with a read token of its own, then calls +its ``sync_time`` with the follower's config: the gateway's watch URL as the agent's sandbox reaches it, the name the +gateway knows it by and that token. From then on a clock write that waits for followers (``wait_followers_s``) waits +for the agent too. ``sync_env_clock`` has every agent registered with the env follow once it has armed the clock, and +``deploy_agent`` an agent it deploys while the clock is armed.""" +from __future__ import annotations + +import logging +import secrets +from typing import TYPE_CHECKING, Optional + +import httpx + +from agent_env.env.env import EnvCapabilityUnsupported, gateway_url_of +from agent_env.env.gateway.constants import CLOCK_WATCH_PATH, EXT_CLOCK_URI +from agent_env.providers.sandbox_providers.local_docker.provider import host_url_for +from agent_env.providers.sandbox_providers.sandbox_provider import reachable_url, sandbox_request_headers_for_url +from agent_env.task_step.context import StepContextError + +if TYPE_CHECKING: + from agent_env.env.env import DeployedEnv + from agent_env.task_step.context import DeployedAgent, TaskStepContext + +logger = logging.getLogger(__name__) + +#: What an agent's ``sync_time`` must take to run a follower of its own. +FOLLOWER_FIELDS = ("watch_url", "name", "read_token") + + +def follower_name(agent: DeployedAgent) -> str: + """The name the gateway knows ``agent``'s follower by, apart from the computers' names.""" + return f"agent-{agent.agent_name}" + + +def _sync_time(agent: DeployedAgent) -> tuple[Optional[dict], Optional[str]]: + from agent_env.a2a_agent import A2AAgent + return A2AAgent.operation(A2AAgent.find_extension(agent.a2a_card or {}, EXT_CLOCK_URI), "sync_time") + + +def _fields(method: dict) -> set[str]: + request = method.get("request") or {} + return {*request.get("required", ()), *request.get("optional", ()), *(request.get("properties") or {})} + + +def watch_url_for(agent: DeployedAgent, env: DeployedEnv) -> str: + """The gateway's watch URL as ``agent``'s sandbox reaches it, as its MCP URL is (``deploy_agent``).""" + url = gateway_url_of(env).rstrip("/") + CLOCK_WATCH_PATH + if not getattr(env, "sandbox_id", None): + return host_url_for(url, agent.sandbox_type) + return reachable_url(url, from_sandbox_type=env.sandbox_type, to_sandbox_type=agent.sandbox_type) + + +async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Optional[str], + timezone: Optional[str], timeout: float) -> dict: + """Have ``agent`` follow ``env``'s gateway clock. ``{"synced": False, "reason": ...}`` for an agent that can't: + its card serves no ``sync_time`` (``not_advertised``) or one without the follower's fields + (``no_follower_fields``), or the gateway takes no followers after it starts (``gateway_takes_no_followers``); raises + for one that serves it and fails, or can't take the task's zone, as for a server.""" + method, path = _sync_time(agent) + if method is None: + return {"synced": False, "reason": "not_advertised"} + fields = _fields(method) + if not set(FOLLOWER_FIELDS) <= fields: + return {"synced": False, "reason": "no_follower_fields"} + if timezone is not None and "timezone" not in fields: + raise StepContextError(f"agent '{agent.agent_name}' serves {EXT_CLOCK_URI} sync_time without a timezone; " + f"rebuild its image or drop timezone={timezone!r} from the task") + name, token = follower_name(agent), secrets.token_urlsafe(32) + try: + await env.invoke(EXT_CLOCK_URI, "declare_follower", {"name": name, "read_token": token}, timeout=timeout) + except EnvCapabilityUnsupported: + return {"synced": False, "reason": "gateway_takes_no_followers"} + body = {"env_get_time_url": env_get_time_url or "", "watch_url": watch_url_for(agent, env), "name": name, + "read_token": token, **({"timezone": timezone} if timezone is not None else {})} + url = (agent.a2a_url or agent.api_url).rstrip("/") + path + async with httpx.AsyncClient() as client: + answer = await client.post(url, json=body, timeout=timeout, headers=sandbox_request_headers_for_url(url)) + if answer.status_code >= 400: + raise RuntimeError(f"agent '{agent.agent_name}' doesn't follow the clock of env '{env.env_id}' " + f"(HTTP {answer.status_code}): {answer.text[:600]}") + return {"synced": True, "follower": name, "result": answer.json()} + + +def _armed(context: TaskStepContext, env_id: str) -> Optional[dict]: + """The configuration of the last ``sync_env_clock`` that armed ``env_id``'s clock in this run, if any.""" + return next((c for c in reversed(context.metadata.get("clock_configurations") or []) + if c.get("env_id") == env_id and "env_get_time_url" in c), None) + + +async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, timeout: float) -> list[dict]: + """Have an agent ``deploy_agent`` just deployed follow the clock of each env registered with it that this run has + armed, as ``sync_env_clock`` has every agent then: one that can't is reported and skipped, or fails the step under + that step's ``tolerate_missing_sync_time = False``.""" + outcomes = [] + for env_id in agent.env_ids: + if (armed := _armed(context, env_id)) is None: + continue + env = next((d for d in context.deployed_envs if d.env_id == env_id), None) + if env is None or gateway_url_of(env) is None: + continue + outcome = await follow(agent, env, env_get_time_url=armed.get("env_get_time_url"), + timezone=armed.get("timezone"), timeout=timeout) + if not outcome["synced"] and not armed.get("tolerate_missing_sync_time", True): + raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env '{env_id}': " + f"{outcome['reason']}") + logger.info(f"agent '{agent.agent_name}' on env {env_id}'s clock: {outcome}") + outcomes.append({"env_id": env_id, "agent": agent.agent_name, **outcome}) + return outcomes diff --git a/src/agent_env/task_step/task_steps/deploy_agent.py b/src/agent_env/task_step/task_steps/deploy_agent.py index fb24dc111..443b97447 100644 --- a/src/agent_env/task_step/task_steps/deploy_agent.py +++ b/src/agent_env/task_step/task_steps/deploy_agent.py @@ -546,6 +546,9 @@ async def _execute(self, context: TaskStepContext, deployed_agents: list) -> Tas network_policy=deployed.network_policy, env_ids=list(self.env_ids), )) + # An agent deployed while a clock is armed follows it, as sync_env_clock had every agent then. + from agent_env.task_step.task_steps.agent_clock import follow_armed_clocks + await follow_armed_clocks(context, context.deployed_agents[-1], timeout=60) return context def _model_endpoint(self, context: TaskStepContext) -> dict[str, str]: diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index 8f574b058..4e3d1a823 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -5,6 +5,8 @@ env card lists (``legacy_protocol.backing_servers``) whose own card advertises it, at the route that card gives. An env whose card offers no set_time has no gateway clock to arm, as a computer deployed alone, and is refused saying to deploy it in a multi env; a record that kept no card is refused saying so. +Then has every agent registered with the env whose card serves clock/v1 sync_time follow the clock as a computer does +(``agent_clock``), so every program it runs reads the env's time and later clock writes can wait for it. Non-advertisers are skipped when tolerate_missing_sync_time (default True), and websites always. Place right before prompt_agent so the virtual timeline starts at agent-start; when setup needs the clock armed, arm it at rate 0 and start it with set_env_clock_rate right before prompt_agent. @@ -160,22 +162,45 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: entry = {"service": service, "environment": service, **({} if server.mcp else {"website": True}), **outcome} (synced if outcome.get("synced") else skipped).append(entry) + agents = await self._follow_agents(context, deployed, env_get_time_url) configuration = { "step_id": self.id, "env_id": self.env_id, "virtual_time": self.virtual_time, "virtual_seconds_per_real_second": self.virtual_seconds_per_real_second, - "env_get_time_url": env_get_time_url, "synced": synced, "skipped": skipped, + "env_get_time_url": env_get_time_url, "synced": synced, "skipped": skipped, "agents": agents, + "tolerate_missing_sync_time": self.tolerate_missing_sync_time, } if self.timezone is not None: configuration.update(timezone=self.timezone, armed_at=self.armed_virtual_time, get_time_zone=state.get("time_zone")) context.metadata.setdefault("clock_configurations", []).append(configuration) logger.info(f"sync_env_clock env={self.env_id}: armed rate={self.virtual_seconds_per_real_second}; " - f"synced={[s['service'] for s in synced]} skipped={[s['service'] for s in skipped]}") + f"synced={[s['service'] for s in synced]} skipped={[s['service'] for s in skipped]} " + f"agents={[(a['agent'], a['synced']) for a in agents]}") if not synced: logger.warning(f"sync_env_clock env={self.env_id}: clock armed but NO server synced " f"({[(s['service'], s.get('reason')) for s in skipped]}) — every tool still answers wall time") return context + async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: + """Have every agent registered with the env follow its clock (``agent_clock.follow``): one that can't is + reported and skipped, or fails the step when ``tolerate_missing_sync_time`` is false, as a server is.""" + from agent_env.task_step.task_steps.agent_clock import follow + + outcomes = [] + for agent in context.deployed_agents: + if self.env_id not in (agent.env_ids or []): + continue + outcome = await follow(agent, deployed, env_get_time_url=env_get_time_url, timezone=self.timezone, + timeout=self.timeout_seconds) + if not outcome["synced"]: + if not self.tolerate_missing_sync_time: + raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env " + f"'{self.env_id}': {outcome['reason']}") + logger.info(f"sync_env_clock: agent '{agent.agent_name}' doesn't follow the clock " + f"({outcome['reason']}); skipping") + outcomes.append({"agent": agent.agent_name, **outcome}) + return outcomes + async def _sync_one(self, deployed, gateway_url: str, service: str, env_get_time_url: Optional[str], mcp: bool = True, tolerate: Optional[bool] = None, server: Optional[BackingServer] = None) -> dict: """Invoke sync_time on one server, an MCP server or with ``mcp=False`` a website, if its card advertises diff --git a/tst/unit/agents/claude_code/test_env_clock_image.py b/tst/unit/agents/claude_code/test_env_clock_image.py new file mode 100644 index 000000000..8bef2b3c1 --- /dev/null +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -0,0 +1,66 @@ +"""Claude Code on the env's clock (FD-5916): the image runs the computer base's clock follower and egress proxy, each +as its own user, and agent-env's libfaketime for the agent's user, so the CLI and every command the model runs read +the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. The CLI goes out through +the egress proxy, and to the env's MCP servers directly.""" + +from __future__ import annotations + +import re + +import pytest + +from tst.unit.agents.claude_code.agent import AGENT_DIR, claude_bridge + +DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() +ROOT_CONF = (AGENT_DIR / "root.conf").read_text() +BASE = AGENT_DIR.parents[2] / "computers" / "artifacts" / "ubuntu-24.04" +PROXY = "http://127.0.0.1:3128" + + +@pytest.mark.parametrize("folder", ["clock", "egress"]) +def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(folder): + copied = sorted(p.relative_to(AGENT_DIR / folder) for p in (AGENT_DIR / folder).rglob("*") if p.is_file()) + + assert copied + assert [str(p) for p in copied if (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] + + +def _program(name: str) -> dict[str, str]: + section = re.search(rf"^\[program:{name}\]\n(.*?)(?=^\[|\Z)", ROOT_CONF, re.MULTILINE | re.DOTALL).group(1) + return dict(line.split("=", 1) for line in section.splitlines() if "=" in line and not line.startswith(";")) + + +def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as_the_agents_on_real_time(): + assert (_program("clock")["user"], _program("egress")["user"]) == ("agentenv-clock", "agentenv-egress") + agent = _program("agent") + assert agent["user"] == "agent" + # uvicorn itself, so the marker reaches the server and not a shell that drops it on exec. + assert agent["command"].startswith("/usr/local/bin/python3 -m uvicorn a2a_server:app") + assert 'AGENTENV_REAL_TIME="1"' in agent["environment"] + assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in agent["environment"] + assert 'NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem"' in agent["environment"] + + +def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(): + assert "> /etc/ld.so.preload" in DOCKERFILE and "libfaketimeMT.so.1" in DOCKERFILE + assert 's/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/' in DOCKERFILE + assert "install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock" in DOCKERFILE + assert not re.search(r"^USER ", DOCKERFILE, re.MULTILINE) + + +def test_started_as_another_user_the_image_runs_the_server_alone_as_before(): + start = (AGENT_DIR / "start-agent").read_text() + assert 'if [ "$(id -u)" != 0 ]; then\n exec /opt/agent/entrypoint.sh' in start + assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start + + +def test_where_the_image_runs_the_proxy_the_cli_goes_out_through_it_and_to_the_envs_servers_directly(): + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"]) + + assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY} + assert env["NO_PROXY"] == env["no_proxy"] == "localhost,127.0.0.1,::1,host.docker.internal,gateway" + assert env["NODE_USE_ENV_PROXY"] == "1" + + +def test_without_the_proxy_the_cli_is_run_as_before(): + assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env({})) diff --git a/tst/unit/agents/claude_code/test_image_contract.py b/tst/unit/agents/claude_code/test_image_contract.py index c8e528df3..3dc316cc5 100644 --- a/tst/unit/agents/claude_code/test_image_contract.py +++ b/tst/unit/agents/claude_code/test_image_contract.py @@ -17,7 +17,8 @@ def test_every_file_the_image_copies_is_in_the_folder(): copied = [name for line in re.findall(r"^COPY (?!--from=)(.+) \S+$", DOCKERFILE, re.MULTILINE) for name in line.split()] - assert copied and all((AGENT_DIR / name).is_file() for name in copied), copied + assert copied and all((AGENT_DIR / name).is_file() or (name.endswith("/") and (AGENT_DIR / name).is_dir()) + for name in copied), copied # install.sh and run.sh are install/v1's; a build context pruned to what the Dockerfile reads keeps them so. assert {"a2a_server.py", "claude_bridge.py", "kit.py", "tar_changelog.py", "entrypoint.sh", "install.sh", "run.sh"} <= set(copied) @@ -54,5 +55,5 @@ def test_the_image_pins_its_cli_checks_its_checksum_and_keeps_tls_on(): def test_the_image_starts_through_cmd_which_modal_passes_its_env_to(): - assert 'CMD ["/opt/agent/entrypoint.sh"]' in DOCKERFILE + assert 'CMD ["/usr/local/bin/start-agent"]' in DOCKERFILE assert not re.search(r"^ENTRYPOINT", DOCKERFILE, re.MULTILINE) diff --git a/tst/unit/env/gateway/clock_watch_test.py b/tst/unit/env/gateway/clock_watch_test.py index 552a74f31..3a848cd08 100644 --- a/tst/unit/env/gateway/clock_watch_test.py +++ b/tst/unit/env/gateway/clock_watch_test.py @@ -79,12 +79,13 @@ def test_the_card_offers_every_clock_method_with_the_waits_bound_and_the_watchs_ assert {name: (m["method"], m["endpoint"]) for name, m in methods.items()} == { "get_time": ("GET", "/clock/time"), "set_time": ("PUT", "/clock/set-time"), "set_rate": ("PUT", "/clock/set-rate"), "advance": ("PUT", "/clock/advance"), "clear": ("POST", "/clock/clear"), "state": ("GET", "/clock/state"), - "watch": ("GET", "/clock/watch")} + "declare_follower": ("PUT", "/clock/followers"), "watch": ("GET", "/clock/watch")} assert all(methods[name]["request"]["properties"]["wait_followers_s"] == wait for name in ("set_time", "set_rate", "advance", "clear")) assert methods["set_rate"]["request"]["required"] == ["virtual_seconds_per_real_second"] assert methods["set_time"]["request"]["required"] == ["virtual_time"] assert set(methods["watch"]["request"]["properties"]) == {"after", "follower"} + assert methods["declare_follower"]["request"]["required"] == ["name", "read_token"] assert methods["watch"]["hold_s"] == CLOCK_WATCH_HOLD_S == ClockFollowers.WATCH_S == 25.0 @@ -432,3 +433,58 @@ async def test_a_write_stops_waiting_for_a_computer_when_it_goes_quiet_during_th waited = time.monotonic() - started assert answer.status_code == 504 and answer.json()["stale"] == ["laptop"], answer.text assert 0.5 <= waited < 3, waited + + +@pytest.mark.asyncio +async def test_an_agent_declared_after_the_gateway_started_is_waited_for_by_its_own_token(gateway): + gw = gateway(followers={}) + async with _client(gw) as client: + declared = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "token-agent"}) + assert declared.status_code == 200 and declared.json()["followers"] == { + "agent-solver": {"version": None, "seen_s_ago": None}} + follower = asyncio.create_task(_follow(client, "agent-solver", "token-agent", applies_after_s=0.05)) + try: + await _until_shown(gw, "agent-solver") + written = await client.put("/clock/set-time", json={"virtual_time": T0, "wait_followers_s": 2}) + finally: + follower.cancel() + assert written.status_code == 200 + assert gw._clock_followers.state()["agent-solver"]["version"] == gw._clock.version + + +@pytest.mark.asyncio +async def test_an_agent_declared_again_takes_its_new_token_and_its_old_one_acknowledges_nothing(gateway): + gw = gateway(followers={}) + async with _client(gw) as client: + await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "first"}) + await client.put("/clock/set-time", json={"virtual_time": T0, "virtual_seconds_per_real_second": 0}) + await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "second"}) + old = asyncio.create_task(_watch(client, gw._clock.version, "agent-solver", "first")) + await asyncio.sleep(0.2) + by_the_old_token = gw._clock_followers.state()["agent-solver"]["version"] + new = asyncio.create_task(_watch(client, gw._clock.version, "agent-solver", "second")) + try: + await _until_shown(gw, "agent-solver") + finally: + old.cancel() + new.cancel() + assert by_the_old_token is None + + +@pytest.mark.parametrize("body", [{"name": "agent-solver"}, {"read_token": "t"}, {"name": "", "read_token": "t"}, [1]]) +@pytest.mark.asyncio +async def test_declaring_a_follower_takes_a_name_and_a_read_token(gateway, body): + gw = gateway(followers={}) + async with _client(gw) as client: + answer = await client.put("/clock/followers", json=body) + assert answer.status_code == 400 and gw._clock_followers.state() == {} + + +@pytest.mark.asyncio +async def test_only_the_envs_control_token_declares_a_follower(gateway): + gw = gateway(followers={}, control_token="control") + async with _client(gw) as client: + refused = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "t"}) + taken = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "t"}, + headers={"Authorization": "Bearer control"}) + assert (refused.status_code, taken.status_code) == (401, 200) diff --git a/tst/unit/env/gateway/control_token_test.py b/tst/unit/env/gateway/control_token_test.py index 5b5458b0c..6a9a22771 100644 --- a/tst/unit/env/gateway/control_token_test.py +++ b/tst/unit/env/gateway/control_token_test.py @@ -20,6 +20,7 @@ "/tools/disable": "POST", "/tools/enable": "POST", "/triggers/register": "POST", "/triggers/remove": "POST", "/triggers/clear": "POST", "/notifications/config": "POST", "/notifications/take": "POST", "/notifications/clear": "POST", + "/clock/followers": "PUT", } OPEN = {"/state": "GET", "/step": "POST", "/clock/time": "GET", "/clock/state": "GET", "/triggers/state": "GET", "/notifications/state": "GET", "/trajectory": "GET", "/changes": "GET", "/.well-known/agent-env.json": "GET", diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py new file mode 100644 index 000000000..78b765b8e --- /dev/null +++ b/tst/unit/task_step/agent_clock_test.py @@ -0,0 +1,154 @@ +"""Agents on the env's clock: once sync_env_clock has armed it, every agent registered with the env whose card serves +clock/v1 sync_time is declared at the gateway as a follower with a token of its own and handed the follower's config; +one that doesn't is reported and skipped, or fails the step when tolerate_missing_sync_time is false; and an agent +deploy_agent deploys while the clock is armed follows the same way.""" +import json +from types import SimpleNamespace + +import pytest + +from agent_env.env.gateway.constants import EXT_CLOCK_URI +from agent_env.task_step.context import DeployedAgent, StepContextError, TaskStepContext +from agent_env.task_step.task_steps import agent_clock +from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep +from tst.unit.task_step.sync_env_clock_test import (CLOCK_URL, T0, _CARD, _CLOCK_EXT, _ZONE_EXT, _carded, _composed_card, _mock_http, + _step) + +AGENT_URL = "http://solver:8000" +SYNC_TIME = {"method": "POST", "endpoint": "/ext/clock/sync-time", + "request": {"required": ["env_get_time_url"], "optional": ["timezone", "watch_url", "name", "read_token"]}} + + +def _agent(name="solver", sync_time=SYNC_TIME, env_ids=("e1",)) -> DeployedAgent: + extensions = [{"uri": EXT_CLOCK_URI, "params": {"endpoint": "/ext/clock", "methods": {"sync_time": sync_time}}}] + return DeployedAgent(agent_name=name, api_url=AGENT_URL, a2a_url=AGENT_URL, env_ids=list(env_ids), + a2a_card={"capabilities": {"extensions": extensions if sync_time else []}}) + + +async def _context(*agents) -> TaskStepContext: + """An env whose one MCP server follows the clock, and ``agents``.""" + return TaskStepContext(deployed_envs=[_carded(await _composed_card({"mcp-email": [_CLOCK_EXT]}))], + deployed_agents=list(agents)) + + +def _no_servers(monkeypatch): + monkeypatch.setattr("agent_env.env.env.Env.get", lambda *a, **k: SimpleNamespace(environment_name="email")) + + +@pytest.mark.asyncio +async def test_an_agent_is_declared_a_follower_then_handed_its_config_with_the_same_token(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent()) + context.deployed_envs = [_carded(await _composed_card({"mcp-email": [_ZONE_EXT]}))] + + await SyncEnvClockTaskStep(id="sync", version=1, env_id="e1", virtual_time="2026-01-01T06:00:00", + timezone="America/Chicago").execute(context) + + declared, synced = sent[3], sent[4] + assert (declared.method, str(declared.url)) == ("PUT", f"{_CARD}/clock/followers") + assert (synced.method, str(synced.url)) == ("POST", f"{AGENT_URL}/ext/clock/sync-time") + declaration, config = json.loads(declared.content), json.loads(synced.content) + assert declaration == {"name": "agent-solver", "read_token": config["read_token"]} and len(config["read_token"]) > 30 + assert config == {"env_get_time_url": CLOCK_URL, "watch_url": "http://gw/clock/watch", "name": "agent-solver", + "read_token": declaration["read_token"], "timezone": "America/Chicago"} + [agent] = context.metadata["clock_configurations"][0]["agents"] + assert (agent["agent"], agent["synced"], agent["follower"]) == ("solver", True, "agent-solver") + + +@pytest.mark.asyncio +async def test_only_the_agents_registered_with_the_env_follow_its_clock(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + + await _step().execute(await _context(_agent("elsewhere", env_ids=("e2",)))) + + assert [r.url.path for r in sent][3:] == [] + + +@pytest.mark.parametrize("sync_time, reason", [ + (None, "not_advertised"), + ({**SYNC_TIME, "request": {"required": ["env_get_time_url"], "optional": ["timezone"]}}, "no_follower_fields"), +]) +@pytest.mark.asyncio +async def test_an_agent_that_cant_follow_is_reported_and_skipped_or_fails_a_strict_step(monkeypatch, sync_time, reason): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent(sync_time=sync_time)) + + await _step().execute(context) + assert context.metadata["clock_configurations"][0]["agents"] == [{"agent": "solver", "synced": False, + "reason": reason}] + assert [r.url.path for r in sent][3:] == [] + with pytest.raises(StepContextError, match=f"agent 'solver' can't follow the clock of env 'e1': {reason}"): + await _step(tolerate_missing_sync_time=False).execute(await _context(_agent(sync_time=sync_time))) + + +@pytest.mark.asyncio +async def test_a_gateway_from_before_followers_after_start_is_reported_and_the_agent_skipped(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent()) + methods = next(e for e in context.deployed_envs[0].environment_card["capabilities"]["extensions"] + if e["uri"] == EXT_CLOCK_URI)["params"]["methods"] + del methods["declare_follower"] + + await _step().execute(context) + + assert context.metadata["clock_configurations"][0]["agents"][0]["reason"] == "gateway_takes_no_followers" + assert [r.url.path for r in sent][3:] == [] + + +@pytest.mark.asyncio +async def test_an_agent_that_serves_sync_time_and_fails_fails_the_step_saying_why(monkeypatch): + import httpx + + real = httpx.AsyncClient + + def handle(request: httpx.Request) -> httpx.Response: + if request.url.path == "/ext/clock/sync-time": + return httpx.Response(404, json={"detail": "target_unavailable: this agent's clock follower isn't following"}) + return httpx.Response(200, json={"env_get_time_url": CLOCK_URL} if request.url.path.endswith("/state") else {}) + + monkeypatch.setattr(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))) + _no_servers(monkeypatch) + + with pytest.raises(RuntimeError, match="agent 'solver' doesn't follow the clock of env 'e1' \\(HTTP 404\\): .*" + "isn't following"): + await _step().execute(await _context(_agent())) + + +@pytest.mark.asyncio +async def test_an_agent_deployed_while_the_clock_is_armed_follows_it(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + context = await _context() + context.metadata["clock_configurations"] = [ + {"step_id": "sync", "env_id": "e1", "virtual_time": T0, "env_get_time_url": CLOCK_URL, "timezone": "Asia/Tokyo", + "tolerate_missing_sync_time": True}, + {"step_id": "rate", "env_id": "e1", "virtual_seconds_per_real_second": 60, "virtual_time": T0}, + ] + late = _agent("late") + + outcomes = await agent_clock.follow_armed_clocks(context, late, timeout=5) + + assert [(r.method, str(r.url)) for r in sent] == [("PUT", f"{_CARD}/clock/followers"), + ("POST", f"{AGENT_URL}/ext/clock/sync-time")] + assert json.loads(sent[1].content)["timezone"] == "Asia/Tokyo" + assert [(o["env_id"], o["agent"], o["synced"]) for o in outcomes] == [("e1", "late", True)] + + +@pytest.mark.asyncio +async def test_an_agent_deployed_before_any_clock_is_armed_is_left_alone(monkeypatch): + sent = _mock_http(monkeypatch) + assert await agent_clock.follow_armed_clocks(await _context(), _agent(), timeout=5) == [] + assert sent == [] + + +@pytest.mark.asyncio +async def test_a_late_agent_that_cant_follow_fails_a_deploy_under_a_strict_sync(monkeypatch): + _mock_http(monkeypatch) + context = await _context() + context.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL, + "tolerate_missing_sync_time": False}] + with pytest.raises(StepContextError, match="agent 'late' can't follow the clock of env 'e1': not_advertised"): + await agent_clock.follow_armed_clocks(context, _agent("late", sync_time=None), timeout=5) From eea933c0ec2866bb1800c9e67285365f7cd77fd7 Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:08:23 -0800 Subject: [PATCH 02/29] refactor(agents): leave the Claude Code image to its own PR; agents skip unless require_agent_clock The image's changes move to wloo/claude-code-clock-image, along with the computer base's egress options only it uses: on CI the image's Claude Code timed out reaching the test's model endpoint. What stays serves sync_time for an agent whose image runs the follower and is otherwise inert. An agent that can't follow is now skipped even when tolerate_missing_sync_time is false, which stays about the env's servers; require_agent_clock fails the step instead. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/agent_env/agents/README.md | 13 - .../agents/agents/claude-code/Dockerfile | 44 +- .../agents/claude-code/claude_bridge.py | 29 +- .../agents/agents/claude-code/clock/COPYING | 339 ---- .../claude-code/clock/agentenv_clock_page.c | 320 ---- .../claude-code/clock/agentenv_clock_waits.c | 1175 ------------- .../agents/agents/claude-code/clock/build.sh | 22 - .../agents/claude-code/clock/clock-follower | 204 --- .../agents/claude-code/clock/clock.conf | 11 - .../agents/claude-code/clock/clockpage.py | 157 -- .../claude-code/clock/libfaketime.patch | 1527 ----------------- .../agents/claude-code/clock/tls_realtime.c | 115 -- .../agents/agents/claude-code/egress/boot.go | 294 ---- .../agents/claude-code/egress/boot_test.go | 99 -- .../agents/agents/claude-code/egress/clock.go | 151 -- .../agents/claude-code/egress/clock_test.go | 112 -- .../agents/agents/claude-code/egress/go.mod | 12 - .../agents/agents/claude-code/egress/go.sum | 10 - .../agents/agents/claude-code/egress/hello.go | 164 -- .../agents/claude-code/egress/hello_test.go | 82 - .../agents/claude-code/egress/leaves.go | 188 -- .../agents/agents/claude-code/egress/main.go | 92 - .../agents/agents/claude-code/egress/proxy.go | 598 ------- .../agents/claude-code/egress/proxy_test.go | 564 ------ .../egress/testdata/page-armed.hex | 1 - .../egress/testdata/page-unarmed.hex | 1 - .../agents/claude-code/egress/upstream.go | 268 --- .../agents/agents/claude-code/root.conf | 62 - .../agents/agents/claude-code/start-agent | 19 - .../artifacts/ubuntu-24.04/egress/boot.go | 11 +- .../ubuntu-24.04/egress/boot_test.go | 11 - .../task_step/task_steps/agent_clock.py | 4 +- .../task_step/task_steps/sync_env_clock.py | 19 +- .../claude_code/test_env_clock_image.py | 66 - .../agents/claude_code/test_image_contract.py | 5 +- tst/unit/task_step/agent_clock_test.py | 25 +- 36 files changed, 45 insertions(+), 6769 deletions(-) delete mode 100644 src/agent_env/agents/agents/claude-code/clock/COPYING delete mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c delete mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c delete mode 100755 src/agent_env/agents/agents/claude-code/clock/build.sh delete mode 100755 src/agent_env/agents/agents/claude-code/clock/clock-follower delete mode 100644 src/agent_env/agents/agents/claude-code/clock/clock.conf delete mode 100644 src/agent_env/agents/agents/claude-code/clock/clockpage.py delete mode 100644 src/agent_env/agents/agents/claude-code/clock/libfaketime.patch delete mode 100644 src/agent_env/agents/agents/claude-code/clock/tls_realtime.c delete mode 100644 src/agent_env/agents/agents/claude-code/egress/boot.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/boot_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/clock.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/clock_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/go.mod delete mode 100644 src/agent_env/agents/agents/claude-code/egress/go.sum delete mode 100644 src/agent_env/agents/agents/claude-code/egress/hello.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/hello_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/leaves.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/main.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex delete mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex delete mode 100644 src/agent_env/agents/agents/claude-code/egress/upstream.go delete mode 100644 src/agent_env/agents/agents/claude-code/root.conf delete mode 100755 src/agent_env/agents/agents/claude-code/start-agent delete mode 100644 tst/unit/agents/claude_code/test_env_clock_image.py diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index 60e1221bf..f4041d557 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -34,19 +34,6 @@ turn off becomes `--disallowedTools`, which keeps the CLI's defaults for the res `claude_bridge.py`: every tool the pinned CLI has, though which it offers by default depends on the model. A test fails when the Dockerfile's `CLAUDE_CODE_VERSION` moves, until `ClaudeCodeTool` and `TOOLS_CLI_VERSION` move with it. -The image follows a task env's clock the way a computer does (FD-5916): it runs the computer base's clock follower -and egress proxy, copied into `clock/` and `egress/` until the clock library image (FD-5700) ships them, each as its own -user under root's supervisord (`start-agent`, `root.conf`), with agent-env's libfaketime preloaded for the agent's user. -The A2A server starts with the real-time marker, so it and its records stay on real time, and serves clock/v1's -`sync_time`, which the protocol's framework serves on the card of any agent in an image with the follower -(`agentenv_protocol.a2a_agent.clock`). Once `sync_env_clock` has armed the clock, it declares the agent at the gateway -as a follower with a read token of its own and calls that `sync_time` with the follower's config, which the server -writes; `deploy_agent` does the same for an agent it deploys while the clock is armed. From then on the CLI, its own -"Today's date is" line included, and every command the model runs read the env's time, and a clock write that waits for -followers waits for the agent. The CLI goes out through the egress proxy, which keeps a live site's TLS working at any -date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time and the proxy relays TLS -untouched. Started as a user other than root, the image runs the A2A server alone, on real time, as before. - `install_agent` installs it too, into a container a task already runs or onto a `vm` sandbox's Linux host, with `install.sh` from its folder: Claude Code's native build for the target's CPU and libc, uv, and the agent's requirements in a Python of its own, so the target's Python is left alone. The CLI runs its commands in bash or zsh, so a target with diff --git a/src/agent_env/agents/agents/claude-code/Dockerfile b/src/agent_env/agents/agents/claude-code/Dockerfile index 789bfd983..cbe95d6e6 100644 --- a/src/agent_env/agents/agents/claude-code/Dockerfile +++ b/src/agent_env/agents/agents/claude-code/Dockerfile @@ -1,20 +1,6 @@ # Node for the scripts the model runs, without npm or npx; Claude Code itself is a native build. FROM public.ecr.aws/docker/library/node:22-slim@sha256:c3de60bf2f9dd0ac6370e6117950ff62d6e339527e7472301c9c78a017978392 AS node -# The env's clock (FD-5916): the computer base's pieces, copied in (clock/, egress/) until the clock library image -# (FD-5700) ships them for each libc. The egress proxy, one static Go binary, and agent-env's libfaketime and its TLS -# shim, built for this image's libc (Debian bookworm) in stages of their own so no toolchain is left in the image. -FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress -COPY egress/ /egress/ -RUN cd /egress && go vet ./... && go test -count=1 ./... \ - && CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags=-s -o /out/egress . - -FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS clock -RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ - build-essential patch curl ca-certificates -COPY clock/ /clock/ -RUN sh /clock/build.sh /out - FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 # poppler-utils: the CLI's Read tool runs pdftoppm for a PDF's pages. RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl poppler-utils \ @@ -38,35 +24,13 @@ COPY requirements.txt model-requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -r model-requirements.txt # install.sh and run.sh are install/v1's: in the image, so they stay in its build context. COPY a2a_server.py claude_bridge.py kit.py tar_changelog.py entrypoint.sh install.sh run.sh ./ -# The CLI's cwd, /app, and its state, fixed so neither follows the uid. +# The CLI's cwd, /app, and its state, fixed so neither follows the uid: Modal runs the image as root. RUN useradd --create-home --uid 10001 agent && mkdir -p /app /home/agent/.claude \ && chown agent:agent /app /home/agent/.claude && chmod 0755 entrypoint.sh -# The env's clock: the follower and the egress proxy, each as its own user, under root's supervisord (root.conf), and -# agent-env's libfaketime and its TLS shim preloaded into every process, faking only the agent's (clock.conf). The -# follower's config is the agent's user's to write (agentenv_protocol.a2a_agent.clock) and the follower's to read. -RUN pip install --no-cache-dir supervisor==4.2.5 \ - && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock \ - && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress \ - && mkdir -p /usr/local/lib/agentenv-clock /usr/local/lib/agentenv-egress /etc/agentenv \ - && install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock \ - && touch /etc/agentenv/ca.pem \ - && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ - && cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem -COPY --from=clock /out/libfaketimeMT.so.1 /out/libtls_realtime.so /usr/local/lib/agentenv-clock/ -COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress -COPY clock/clockpage.py clock/clock-follower /usr/local/lib/agentenv-clock/ -COPY clock/clock.conf /etc/agentenv/clock.conf -COPY root.conf /etc/agentenv/root.conf -COPY start-agent /usr/local/bin/start-agent -RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/" \ - -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf \ - && chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/bin/start-agent \ - && printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ - > /etc/ld.so.preload ENV A2A_PORT=8000 PYTHONPATH=/opt/agent CLAUDE_CONFIG_DIR=/home/agent/.claude \ DISABLE_AUTOUPDATER=1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 +USER agent WORKDIR /app EXPOSE 8000 -# Starts as root, which start-agent needs for the clock; the A2A server and the CLI run as the agent's user. CMD rather -# than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. -CMD ["/usr/local/bin/start-agent"] +# CMD rather than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. +CMD ["/opt/agent/entrypoint.sh"] 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 aabc2ec9b..e8fcfc651 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -12,12 +12,11 @@ import signal import stat import time -from collections.abc import Callable, Iterable, Mapping +from collections.abc import Callable, Mapping from dataclasses import dataclass, field, replace from functools import partial from pathlib import Path from typing import Literal -from urllib.parse import urlsplit from pydantic import BaseModel, ConfigDict, model_validator @@ -174,27 +173,8 @@ def _model_headers(raw: str | None) -> dict[str, str]: return {str(name): str(value) for name, value in headers.items()} -#: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it. -EGRESS_PROXY_ENV = "AGENTENV_EGRESS_PROXY" -_DIRECT_HOSTS = ("localhost", "127.0.0.1", "::1", "host.docker.internal") - - -def egress_env(environ: Mapping[str, str], direct: Iterable[str] = ()) -> dict[str, str]: - """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916): they read the env's time, - so a client checking a live site's certificate against it would refuse the site once the clock is months off, and - the proxy, on real time, hands it a certificate that holds then. The env's MCP servers (``direct``) are reached - directly: the proxy refuses an address that isn't global, as a local or private gateway's is.""" - proxy = environ.get(EGRESS_PROXY_ENV) - if not proxy: - return {} - hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, direct)])) - return {**{name: proxy for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY") for name in (base, base.lower())}, - "NO_PROXY": hosts, "no_proxy": hosts, "NODE_USE_ENV_PROXY": "1"} - - def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: str | None = None, - task_id: str | None = None, max_thinking_tokens: int | None = None, - direct_hosts: Iterable[str] = ()) -> dict[str, str]: + task_id: str | None = None, max_thinking_tokens: int | None = None) -> dict[str, str]: """The CLI's environment: this process's, pointed at the endpoint agent-env configured (its key as a bearer token, so an API key too would conflict), with the endpoint's headers and the run's attribution, and the model's aliases pinned to ``model`` on that endpoint. A variable already set is kept, LITELLM_* too, so a script the model @@ -223,7 +203,6 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" - env.update(egress_env(environ, direct_hosts)) return env @@ -703,9 +682,7 @@ async def _spawn_claude( stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=cli_env(os.environ, model=model, project_id=project_id, task_id=task_id, - max_thinking_tokens=max_thinking_tokens, - direct_hosts=[urlsplit(str(entry.get("url") or "")).hostname or "" - for entry in (mcp_servers or {}).values()]), + max_thinking_tokens=max_thinking_tokens), cwd=WORKSPACE_DIR, start_new_session=True, )) diff --git a/src/agent_env/agents/agents/claude-code/clock/COPYING b/src/agent_env/agents/agents/claude-code/clock/COPYING deleted file mode 100644 index d159169d1..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/COPYING +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c deleted file mode 100644 index 9167f7ede..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c +++ /dev/null @@ -1,320 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 1, included by libfaketime.c (libfaketime.patch) before its - * time functions: the process's settings, read once at load from /etc/agentenv/clock.conf, or from the environment - * where there is no such file, so no getenv() runs after main() starts; who is faked (the session user, unless started with AGENTENV_REAL_TIME=1, a marker - * taken out of the environment block in place); and the clock page the follower writes, read without a cache. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * configuration is read once, when the library loads, from AGENTENV_CLOCK_CONF (NAME=VALUE lines) when that - * file exists, else from the environment, and never with getenv() afterwards: a getenv() racing a program's setenv() - * reads a freed environ array, which is how libfaketime's pthread_cond_timedwait crashed Chromium. - */ -#ifndef AGENTENV_CLOCK_CONF -#define AGENTENV_CLOCK_CONF "/etc/agentenv/clock.conf" -#endif -#define FT_CONF_MAX 64 -static char ft_conf_text[8192]; -static const char *ft_conf_name[FT_CONF_MAX]; -static const char *ft_conf_value[FT_CONF_MAX]; -static int ft_conf_count = 0; -static bool ft_conf_loaded = false; -/* this process stays on real time: another user's than AGENTENV_SESSION_UID, or started with - AGENTENV_REAL_TIME=1. */ -static bool ft_disabled = false; -/* bumped with each new time specification parsed, so a wait sees the clock change under it. */ -static volatile unsigned long ft_spec_gen = 0; -static __thread unsigned long ft_tl_gen = 0; -/* real seconds per fake second for what is left of a wait, as of this thread's last clock read: 1 / rate, - or 1 for a frozen clock. */ -static __thread double ft_tl_scale = 1.0; -/* how long a wait that can be cut sleeps before it looks at the clock again (AGENTENV_WAIT_SLICE_MS). */ -static long ft_wait_slice_ns = 100000000L; - -static void ft_conf_add(char *name, char *value) -{ - if (ft_conf_count < FT_CONF_MAX) - { - ft_conf_name[ft_conf_count] = name; - ft_conf_value[ft_conf_count] = value; - ft_conf_count++; - } -} - -static void ft_conf_load(void) -{ - extern char **environ; - size_t used = 0; - int fd; - if (ft_conf_loaded) return; - ft_conf_loaded = true; - fd = open(AGENTENV_CLOCK_CONF, O_RDONLY | O_CLOEXEC); - if (fd >= 0) - { - ssize_t n; - char *line; - while (used < sizeof(ft_conf_text) - 1 && - (n = read(fd, ft_conf_text + used, sizeof(ft_conf_text) - 1 - used)) > 0) - used += n; - close(fd); - ft_conf_text[used] = 0; - line = ft_conf_text; - while (line != NULL && *line != 0) - { - char *next = strchr(line, '\n'); - char *eq; - if (next != NULL) *next++ = 0; - eq = strchr(line, '='); - if (line[0] != '#' && eq != NULL && eq != line) - { - *eq = 0; - ft_conf_add(line, eq + 1); - } - line = next; - } - return; - } - for (char **e = environ; e != NULL && *e != NULL; e++) - { - const char *s = *e; - size_t len; - char *copy, *eq; - if (strncmp(s, "FAKE", 4) && strncmp(s, "NO_FAKE", 7) && strncmp(s, "DONT_FAKE", 9) && strncmp(s, "HOME=", 5) - && strncmp(s, "AGENTENV_", 9)) - continue; - len = strlen(s); - if (used + len + 1 > sizeof(ft_conf_text)) break; - copy = ft_conf_text + used; - memcpy(copy, s, len + 1); - used += len + 1; - eq = strchr(copy, '='); - if (eq == NULL) continue; - *eq = 0; - ft_conf_add(copy, eq + 1); - } -} - -static char *ft_getenv(const char *name) -{ - ft_conf_load(); - for (int i = 0; i < ft_conf_count; i++) - { - if (strcmp(ft_conf_name[i], name) == 0) return (char *) ft_conf_value[i]; - } - return NULL; -} - -/* - * AGENTENV_REAL_TIME=1 taken out of the original environment block in place: its pointer out of the array - * (which is main()'s envp too) and the string itself changed to "=0", so no copy of it is the marker. unsetenv() isn't - * used: a program may define its own (bash does, and before its main() it does nothing). - */ -static void ft_scrub_marker(void) -{ - extern char **environ; - char **e = environ; - if (e == NULL) return; - while (*e != NULL) - { - if (strcmp(*e, "AGENTENV_REAL_TIME=1") == 0) - { - char **d = e; - (*e)[sizeof("AGENTENV_REAL_TIME=") - 1] = '0'; - do d[0] = d[1]; while (*d++ != NULL); - } - else - e++; - } -} - -/* whether a wait's deadline is one to translate: a valid one, in a faked process, outside libfaketime. */ -static bool ft_translates_wait(const struct timespec *abstime) -{ - return abstime != NULL && !ft_disabled && !dont_fake && abstime->tv_nsec >= 0 && abstime->tv_nsec < SEC_TO_nSEC; -} - -/* - * where a CLOCK_REALTIME deadline in fake time falls on a real clock (CLOCK_MONOTONIC or CLOCK_REALTIME): - * now, plus what is left of the fake interval times ft_tl_scale; so a frozen clock counts what is left as real time and - * nothing waits forever, and a deadline already past is now. - */ -static void ft_deadline_from_fake(clockid_t clock, const struct timespec *fake_abs, struct timespec *out) -{ - struct timespec now, real_now, fake_now, left, scaled; - DONT_FAKE_TIME((*real_clock_gettime)(clock, &now)); - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &real_now)); - fake_now = real_now; - (void) fake_clock_gettime(CLOCK_REALTIME, &fake_now); - *out = now; - timespecsub(fake_abs, &fake_now, &left); - if (left.tv_sec < 0) return; - if (left.tv_sec > 100L * 365 * 86400) left.tv_sec = 100L * 365 * 86400; - if (ft_tl_scale != 1.0) - { - timespecmul(&left, ft_tl_scale, &scaled); - } - else - { - scaled = left; - } - timespecadd(&now, &scaled, out); -} - -/* the end of a wait's next slice on CLOCK_MONOTONIC; true when that is the deadline itself. */ -static bool ft_slice_end(const struct timespec *mono_abs, struct timespec *slice) -{ - struct timespec now, step = {ft_wait_slice_ns / SEC_TO_nSEC, ft_wait_slice_ns % SEC_TO_nSEC}; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &now)); - timespecadd(&now, &step, slice); - if (timespeccmp(slice, mono_abs, <)) return false; - *slice = *mono_abs; - return true; -} - -/* after a slice, the clock read again (the file, with no cache); a new specification since ``gen`` moves the - deadline. True when it moved. */ -static bool ft_deadline_moved(const struct timespec *fake_abs, struct timespec *mono_abs, unsigned long *gen) -{ - struct timespec now; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &now)); - (void) fake_clock_gettime(CLOCK_REALTIME, &now); - if (ft_tl_gen == *gen) return false; - *gen = ft_tl_gen; - ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, mono_abs); - return true; -} - -/* - * the clock page. 64 bytes, little-endian, rewritten in place by the follower: seq odd while it writes, - * then even; check is the xor of the other words and a constant. A realtime read in a faked process copies it - * lock-free and computes universe + (now - anchor) * rate (rate 0: universe), so every process agrees with no shared - * start; the first thread to see a new generation also hands it to libfaketime's own state (rates of sleeps, the - * time an absolute spec gives) under the MT lock. The page must be owned by AGENTENV_CLOCK_PAGE_UID (0 by default) and - * writable by no one else, so the session user can't plant one. - */ -struct ft_clock_page -{ - uint32_t magic, version; - uint64_t seq; - int64_t universe_ns, anchor_ns; - double rate; - uint32_t armed, reserved; - uint64_t generation, check; - int64_t mono_universe_ns, mono_anchor_ns; -}; -#define FT_PAGE_MAGIC 0x4B434541u -#define FT_PAGE_VERSION 2u -static const volatile struct ft_clock_page *ft_page = NULL; -static uint64_t ft_page_applied = 0; -static time_t ft_page_last_try = 0; -static void parse_ft_string(const char *user_faked_time); - -static uint64_t ft_page_check(const struct ft_clock_page *p) -{ - uint64_t rate_bits; - memcpy(&rate_bits, &p->rate, sizeof rate_bits); - return p->seq ^ (uint64_t) p->universe_ns ^ (uint64_t) p->anchor_ns ^ rate_bits ^ p->armed ^ p->generation - ^ (uint64_t) p->mono_universe_ns ^ (uint64_t) p->mono_anchor_ns ^ 0x9E3779B97F4A7C15ull; -} - -static bool ft_page_snapshot(struct ft_clock_page *out) -{ - for (int i = 0; i < 1000; i++) - { - uint64_t s1 = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if (s1 & 1) continue; - out->magic = ft_page->magic; - out->seq = s1; - out->universe_ns = ft_page->universe_ns; - out->anchor_ns = ft_page->anchor_ns; - out->rate = ft_page->rate; - out->armed = ft_page->armed; - out->version = ft_page->version; - out->generation = ft_page->generation; - out->check = ft_page->check; - out->mono_universe_ns = ft_page->mono_universe_ns; - out->mono_anchor_ns = ft_page->mono_anchor_ns; - __atomic_thread_fence(__ATOMIC_ACQUIRE); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_RELAXED) == s1 && out->magic == FT_PAGE_MAGIC - && out->version == FT_PAGE_VERSION - && ft_page_check(out) == out->check) - return true; - } - return false; -} - -static void ft_page_open(void) -{ - const char *path = ft_getenv("AGENTENV_CLOCK_PAGE"); - const char *owner = ft_getenv("AGENTENV_CLOCK_PAGE_UID"); - struct stat st; - void *map; - int fd; - if (path == NULL || *path == 0) return; - fd = open(path, O_RDONLY | O_CLOEXEC); - if (fd < 0) return; - if (fstat(fd, &st) != 0 || st.st_size < (off_t) sizeof(struct ft_clock_page) - || st.st_uid != (uid_t) (owner ? strtoul(owner, NULL, 10) : 0) || (st.st_mode & 022) != 0) - { - close(fd); - return; - } - map = mmap(NULL, sizeof(struct ft_clock_page), PROT_READ, MAP_SHARED, fd, 0); - close(fd); - if (map == MAP_FAILED) return; - if (((const struct ft_clock_page *) map)->magic != FT_PAGE_MAGIC - || ((const struct ft_clock_page *) map)->version != FT_PAGE_VERSION) - { - munmap(map, sizeof(struct ft_clock_page)); - return; - } - ft_page = map; -} - -/* libfaketime's own state set to the page's clock; called with the MT lock held. */ -static void ft_page_apply_locked(const struct ft_clock_page *s) -{ - char spec[160]; - if (s->generation == ft_page_applied) return; - if (!s->armed) - snprintf(spec, sizeof spec, "+0 x1"); - else - snprintf(spec, sizeof spec, "@%lld.%09lld x%.17g ^%lld.%09lld", - (long long) (s->universe_ns / 1000000000LL), (long long) (s->universe_ns % 1000000000LL), s->rate, - (long long) (s->anchor_ns / 1000000000LL), (long long) (s->anchor_ns % 1000000000LL)); - parse_ft_string(spec); - __atomic_store_n(&ft_page_applied, s->generation, __ATOMIC_RELEASE); -} - -/* "%s" times are read as a number, without strptime() and mktime(), which reads TZ with getenv(). */ -static const char *ft_parse_epoch(const char *s, struct timespec *out) -{ - char *end; - long long sec; - long nsec = 0, scale = 100000000L; - bool negative; - while (*s == ' ') s++; - negative = (*s == '-'); - sec = strtoll(s, &end, 10); - if (end == s) return NULL; - if (*end == '.') - { - end++; - while (*end >= '0' && *end <= '9') - { - nsec += (*end - '0') * scale; - scale /= 10; - end++; - } - } - if (negative && nsec > 0) - { - sec -= 1; - nsec = SEC_TO_nSEC - nsec; - } - out->tv_sec = (time_t) sec; - out->tv_nsec = nsec; - return end; -} - diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c deleted file mode 100644 index 5d8237705..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c +++ /dev/null @@ -1,1175 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 2, included by libfaketime.c (libfaketime.patch) after its time - * functions: clock reads and waits for a set realtime clock time on universe time, re-planned whenever the page - * changes, and everything else (monotonic and boot clocks, relative sleeps and timeouts) the kernel's. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * Elapsed time is real; clock reads and waits for a realtime clock time follow the universe. - */ -#include -#include -#include -#include - -#if !defined(FAKE_PTHREAD) || !defined(FAKE_TIMERS) || !defined(FAKE_SLEEP) || defined(INTERCEPT_SYSCALL) -#error agent-env waits need FAKE_PTHREAD, FAKE_TIMERS and FAKE_SLEEP, and their own syscall(), not the INTERCEPT_SYSCALL one -#endif -#ifndef SYS_futex_waitv -#define SYS_futex_waitv 449 -#endif - -/* - * the system call itself, with no libc and no libfaketime state, so syscall() below works before the - * library is initialised (Chromium's allocator calls syscall() from inside the dynamic loader). - */ -static long ft_raw_syscall6(long n, long a, long b, long c, long d, long e, long f) -{ - long ret; -#if defined(__x86_64__) - register long r10 __asm__("r10") = d; - register long r8 __asm__("r8") = e; - register long r9 __asm__("r9") = f; - __asm__ volatile("syscall" : "=a"(ret) : "0"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9) - : "rcx", "r11", "memory"); -#elif defined(__aarch64__) - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - register long x4 __asm__("x4") = e; - register long x5 __asm__("x5") = f; - __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); - ret = x0; -#else -#error the agent-env syscall() needs x86_64 or aarch64 -#endif - if (ret < 0 && ret > -4096) - { - errno = (int) -ret; - return -1; - } - return ret; -} - -#define FT_NS 1000000000LL -#define FT_NEVER INT64_MAX -static __thread struct ft_clock_page ft_tl_snap; -static __thread bool ft_tl_snap_ok = false; - -/* a consistent copy of the page. While the follower holds it odd (preempted mid-write), wait for it on seq, - which it wakes when done, up to a second: a copy older than the page being written would be extrapolated past the - new page's anchor, and the clock would step back when the write lands. Only a follower that died mid-write (seq odd - for over a second, remembered) leaves this thread on its last good copy. */ -static uint64_t ft_dead_seq = 0; -static bool ft_snap(struct ft_clock_page *s) -{ - for (int i = 0; i < 64; i++) - { - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - sched_yield(); - } - for (int i = 0; i < 100; i++) - { - uint64_t seq = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if ((seq & 1) && seq == __atomic_load_n(&ft_dead_seq, __ATOMIC_RELAXED)) break; - if (seq & 1) - { - struct timespec ts = {0, 10000000}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) (uint32_t) seq, (long) &ts, 0, 0); - } - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - if (i == 99) __atomic_store_n(&ft_dead_seq, seq, __ATOMIC_RELAXED); - } - if (!ft_tl_snap_ok) return false; - *s = ft_tl_snap; - return true; -} - -/* the real time just read, in hand before the loads after it: a load can be carried out before an earlier read of the - CPU's counter (aarch64 and x86_64 both allow it). On aarch64 a load whose address depends on the time, then a load - barrier, as the kernel orders its own counter reads; on x86_64 an lfence. */ -static inline void ft_after_real_read(const struct timespec *tp) -{ -#if defined(__aarch64__) - uint64_t tmp; - __asm__ volatile("eor %0, %1, %1\n\tadd %0, sp, %0\n\tldr xzr, [%0]\n\tdmb ishld" - : "=&r"(tmp) : "r"((uint64_t) tp->tv_nsec) : "memory"); -#elif defined(__x86_64__) - (void) tp; - __asm__ volatile("lfence" ::: "memory"); -#else - (void) tp; - __atomic_thread_fence(__ATOMIC_SEQ_CST); -#endif -} - -/* a consistent copy of the page and the real time read while it was the page: the time is read after the copy and kept - only if the page is still the one copied. A time from before a write paired with the page it wrote (a reader that - waited out the write, or was descheduled between its time and its copy) could put the universe behind what that - reader had shown, since a write is continuous with the page it replaces only at its own real time. */ -static bool ft_snap_at(clockid_t clock, struct ft_clock_page *s, struct timespec *tp) -{ - for (int i = 0; i < 8; i++) - { - int r; - if (!ft_snap(s)) return false; - DONT_FAKE_TIME(r = (*real_clock_gettime)(clock, tp)); - if (r != 0) return false; - ft_after_real_read(tp); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE) == s->seq) return true; - } - return true; -} - -static inline int64_t ft_ns(const struct timespec *t) -{ - if (t->tv_sec > 9000000000LL) return FT_NEVER / 2; - if (t->tv_sec < -9000000000LL) return -(FT_NEVER / 2); - return (int64_t) t->tv_sec * FT_NS + t->tv_nsec; -} -static inline struct timespec ft_ts(int64_t ns) -{ - struct timespec t; - if (ns < 0) ns = 0; - t.tv_sec = (time_t) (ns / FT_NS); - t.tv_nsec = (long) (ns % FT_NS); - return t; -} -static inline int64_t ft_real_rt(void) -{ - struct timespec t; - (*real_clock_gettime)(CLOCK_REALTIME, &t); - return ft_ns(&t); -} -static inline bool ft_uw_active(void) -{ - return ft_page != NULL && !ft_disabled && !dont_fake; -} -static inline bool ft_faked(void) -{ - return !ft_disabled && !dont_fake; -} -static inline bool ft_valid_ts(const struct timespec *t) -{ - return t != NULL && t->tv_nsec >= 0 && t->tv_nsec < FT_NS && t->tv_sec >= 0; -} -static inline bool ft_rt_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_ALARM; -} -/* the clocks the universe has; every other one (monotonic, boot, raw, CPU time) is real. */ -static inline bool ft_universe_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_COARSE || c == CLOCK_REALTIME_ALARM || c == CLOCK_TAI; -} - -/* the universe's realtime at real realtime t; before the anchor, the anchor's. */ -static int64_t ft_page_rt(const struct ft_clock_page *s, int64_t t) -{ - if (!s->armed) return t; - if (s->rate == 0 || t <= s->anchor_ns) return s->universe_ns; - if (s->rate == 1.0) return s->universe_ns + (t - s->anchor_ns); - return s->universe_ns + (int64_t) ((double) (t - s->anchor_ns) * s->rate); -} -/* the real realtime at which the universe reaches ``due``; 0 (the past) once it has, FT_NEVER while frozen - short of it. */ -static int64_t ft_real_of(const struct ft_clock_page *s, int64_t due) -{ - double r; - if (!s->armed) return due < 1 ? 1 : due; - if (due <= s->universe_ns) return 1; - if (s->rate <= 0) return FT_NEVER; - r = (double) (due - s->universe_ns) / s->rate; - if (r > 4e18) return FT_NEVER; - return s->anchor_ns + (int64_t) r + 1; -} -static inline bool ft_reached(const struct ft_clock_page *s, int64_t due) -{ - return ft_page_rt(s, ft_real_rt()) >= due; -} -static inline uint32_t ft_seq32(void) -{ - return (uint32_t) __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); -} - -/* ---- a lock for the tables below, with signals blocked so a handler can't deadlock on it ---- */ -static volatile int ft_tab_lock_word = 0; -static void ft_tab_lock(sigset_t *old) -{ - sigset_t all; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); -} -static void ft_tab_unlock(const sigset_t *old) -{ - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, old, NULL); -} - -/* - * ---- timers for a realtime clock time: timerfds and POSIX timers armed at TIMER_ABSTIME on a realtime clock. The - * kernel timer is armed at the real realtime the universe reaches the deadline at (in the past once it has, so it fires - * now), its interval at interval / rate, and disarmed while frozen short of it; the helper arms it again on each change. - * A one-shot timer that has fired is left alone. ---- - */ -#define FT_TIMER_FDS 8 -struct ft_timer -{ - int kind; /* 0 free, 1 timerfd, 2 POSIX timer */ - int tracked; /* armed for a realtime clock time */ - int nfds, fds[FT_TIMER_FDS]; /* a timerfd's descriptors: the one it was armed through and its duplicates */ - timer_t id; - int clock; - int flags; /* the caller's flags, TFD_TIMER_CANCEL_ON_SET kept */ - int real_set; /* the kernel timer is armed */ - int64_t due, interval; /* universe ns */ - struct ft_clock_page pg; /* the page it was last armed from */ -}; -#define FT_TIMERS_MAX 1024 -static struct ft_timer ft_timers[FT_TIMERS_MAX]; -static int ft_timers_top = 0; -static volatile int ft_timers_any = 0; -static int (*real_timer_create_new)(clockid_t, struct sigevent *, timer_t *); -static int (*real_timer_delete_new)(timer_t); -static int (*real_timer_settime_new)(timer_t, int, const struct itimerspec *, struct itimerspec *); -static int (*real_timer_gettime_new)(timer_t, struct itimerspec *); -#define FT_TFD_CLOCKS 65536 -static int ft_tfd_clock[FT_TFD_CLOCKS]; /* clock + 1 as timerfd_create was told; 0 unknown */ - -static struct ft_timer *ft_timer_find_fd(int fd) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 1) - for (int k = 0; k < ft_timers[i].nfds; k++) - if (ft_timers[i].fds[k] == fd) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_find_id(timer_t id) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2 && ft_timers[i].id == id) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_new(void) -{ - for (int i = 0; i < FT_TIMERS_MAX; i++) - if (ft_timers[i].kind == 0) - { - memset(&ft_timers[i], 0, sizeof ft_timers[i]); - if (i >= ft_timers_top) ft_timers_top = i + 1; - return &ft_timers[i]; - } - return NULL; -} -static int ft_timer_real_get(struct ft_timer *t, struct itimerspec *cur) -{ - return t->kind == 1 ? (*real_timerfd_gettime)(t->fds[0], cur) : (*real_timer_gettime_new)(t->id, cur); -} -/* the next expiration at or after ``after``. */ -static int64_t ft_timer_next(const struct ft_timer *t, int64_t after) -{ - int64_t next = t->due; - if (t->interval > 0 && next < after) next += ((after - next + t->interval - 1) / t->interval) * t->interval; - return next; -} -/* the kernel timer set from page ``s``. A periodic timer goes on from its first expiration past the - universe's time on the page it was armed from, so a jump delivers what it passed at once. Lock held. */ -static int ft_timer_arm(struct ft_timer *t, const struct ft_clock_page *s, bool first) -{ - struct itimerspec real, cur; - double rate = s->armed ? s->rate : 1.0; - int64_t next = t->due, at; - int r; - memset(&real, 0, sizeof real); - if (!first && t->real_set && t->interval == 0 && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; /* fired: the expiration is the kernel's to hand over */ - return 0; - } - if (!first && t->interval > 0) next = ft_timer_next(t, ft_page_rt(&t->pg, ft_real_rt()) + 1); - at = ft_real_of(s, next); - if (at != FT_NEVER) - { - real.it_value = ft_ts(at); - if (real.it_value.tv_sec == 0 && real.it_value.tv_nsec == 0) real.it_value.tv_nsec = 1; - if (t->interval > 0) - { - int64_t iv = (int64_t) ((double) t->interval / rate); - real.it_interval = ft_ts(iv < 1 ? 1 : iv); - } - } - t->due = next; - t->pg = *s; - t->real_set = (at != FT_NEVER); - if (t->kind == 1) - r = (*real_timerfd_settime)(t->fds[0], TFD_TIMER_ABSTIME | (t->flags & TFD_TIMER_CANCEL_ON_SET), &real, NULL); - else - r = (*real_timer_settime_new)(t->id, TIMER_ABSTIME, &real, NULL); - if (r != 0) t->tracked = 0; - return r; -} -/* the timer's value in universe time. Lock held. */ -static void ft_timer_value(struct ft_timer *t, struct itimerspec *out) -{ - struct ft_clock_page s; - struct itimerspec cur; - memset(out, 0, sizeof *out); - if (!t->tracked) return; - if (t->interval == 0 && t->real_set && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; - return; - } - out->it_interval = ft_ts(t->interval); - if (ft_snap(&s)) - { - int64_t now = ft_page_rt(&s, ft_real_rt()), next = ft_timer_next(t, now); - out->it_value = ft_ts(next - now < 1 ? 1 : next - now); - } -} - -/* - * ---- realtime condvar waits in progress: each waits to the real time its deadline falls at; on a page change that - * moves that time, the helper broadcasts its condvar, once, and the wait returns 0 (a spurious wakeup the caller - * rechecks), so the caller waits again on the new page. A broadcast that comes before the waiter is inside glibc's wait - * is sent again until the waiter has seen the change. ---- - */ -struct ft_cw -{ - pthread_cond_t *cond; - int used, woken; - int64_t due, real_due; - uint64_t gen; /* the page generation its real_due came from; FT_CW_UNPLANNED before */ -}; -#define FT_CW_MAX 512 -#define FT_CW_UNPLANNED UINT64_MAX -static struct ft_cw ft_cws[FT_CW_MAX]; -static int ft_cws_top = 0; - -/* ---- the helper thread: one per process, started with the first such timer or wait, blocked on the page's seq ---- */ -static volatile pid_t ft_helper_pid = 0; -static volatile int ft_atfork_set = 0; -static void ft_helper_child(void) -{ - /* POSIX timers and condvar waiters belong to the parent; timerfds are shared with it and stay */ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2) ft_timers[i].kind = 0; - memset(ft_cws, 0, sizeof ft_cws); - ft_cws_top = 0; - ft_tab_lock_word = 0; - ft_helper_pid = 0; -} - -/* on a page change, every timer armed again and every condvar waiter whose real deadline moved woken; true - while a waiter is still to see it (not planned yet, or broadcast to before it was waiting). */ -static bool ft_on_change(const struct ft_clock_page *s, bool rebroadcast) -{ - sigset_t old; - bool pending = false; - ft_tab_lock(&old); - if (!rebroadcast) - for (int i = 0; i < ft_timers_top; i++) - { - struct ft_timer *t = &ft_timers[i]; - if (t->kind != 0 && t->tracked && t->pg.generation != s->generation) ft_timer_arm(t, s, false); - } - for (int i = 0; i < ft_cws_top; i++) - { - struct ft_cw *w = &ft_cws[i]; - if (!w->used || w->gen == s->generation) continue; - if (w->gen == FT_CW_UNPLANNED) - { - pending = true; - continue; - } - if (!w->woken) - { - int64_t nd = ft_real_of(s, w->due), diff; - diff = (nd == FT_NEVER || w->real_due == FT_NEVER) ? (nd == w->real_due ? 0 : FT_NEVER) : nd - w->real_due; - if (diff >= -1000000 && diff <= 1000000) - { - w->gen = s->generation; /* its real end didn't move */ - continue; - } - } - if (!w->woken || rebroadcast) pthread_cond_broadcast(w->cond); - w->woken = 1; - pending = true; - } - ft_tab_unlock(&old); - return pending; -} - -static void *ft_helper_main(void *arg) -{ - struct ft_clock_page s; - uint64_t gen = UINT64_MAX; /* the first pass catches a change made while it started */ - int backoff_ms = 0; - (void) arg; - for (;;) - { - uint32_t seq32 = ft_seq32(); - bool pending = false; - if (ft_snap(&s)) - { - if (s.generation != gen) - { - gen = s.generation; - pending = ft_on_change(&s, false); - backoff_ms = pending ? 1 : 0; - } - else if (backoff_ms > 0) - { - pending = ft_on_change(&s, true); - backoff_ms = pending ? (backoff_ms < 64 ? backoff_ms * 2 : 100) : 0; - } - } - if (backoff_ms > 0) - { - struct timespec ts = {0, (long) backoff_ms * 1000000L}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, (long) &ts, 0, 0); - } - else - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, 0, 0, 0); - } - return NULL; -} - -static void ft_helper_start(void) -{ - pid_t pid = getpid(); - sigset_t all, old; - pthread_t thread; - pthread_attr_t attr; - if (__atomic_load_n(&ft_helper_pid, __ATOMIC_ACQUIRE) == pid) return; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, &old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); - if (ft_helper_pid != pid) - { - if (!ft_atfork_set) - { - pthread_atfork(NULL, NULL, ft_helper_child); - ft_atfork_set = 1; - } - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_setstacksize(&attr, 128 * 1024); - if (pthread_create(&thread, &attr, ft_helper_main, NULL) == 0) /* inherits the all-blocked signal mask */ - { - __atomic_store_n(&ft_helper_pid, pid, __ATOMIC_RELEASE); - pthread_setname_np(thread, "agentenv-clock"); - } - pthread_attr_destroy(&attr); - } - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, &old, NULL); -} - -/* ---- the waits ---- */ - -/* the raw system calls below as the cancellation points their callers are (clock_nanosleep, sem_timedwait): - cancellation acted on at once for the call alone, as glibc's own waits were before its cancellable system call - wrapper. */ -static inline int ft_async_cancel(void) -{ - int old; - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); - return old; -} -static inline void ft_deferred_cancel(int old) -{ - int e = errno, ignored; - pthread_setcanceltype(old, &ignored); - errno = e; -} - -/* a sleep until the universe reaches ``due``, on the page's seq. 0, or EINTR. */ -static int ft_rt_sleep(const struct ft_clock_page *first, int64_t due) -{ - struct ft_clock_page s = *first; - pthread_testcancel(); - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int64_t until; - struct timespec ts; - long r; - int ct; - if (ft_reached(&s, due)) return 0; - until = ft_real_of(&s, due); - ts = ft_ts(until); - ct = ft_async_cancel(); - r = ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, (long) seq32, - until == FT_NEVER ? 0 : (long) &ts, 0, FUTEX_BITSET_MATCH_ANY); - ft_deferred_cancel(ct); - if (r == -1 && errno == EINTR) return EINTR; - if (!ft_snap(&s)) return 0; - } -} - -struct ft_futex_waitv { uint64_t val, uaddr; uint32_t flags, reserved; }; -static volatile int ft_waitv_missing = 0; -enum { FT_W_WORD, FT_W_PAGE, FT_W_TIMEOUT, FT_W_INTR, FT_W_ERR }; -/* without futex_waitv (before Linux 5.16, under gVisor, or refused by a seccomp filter): the word alone, until the real - deadline or a slice (AGENTENV_WAIT_SLICE_MS) at most, and the caller reads the page again between slices. */ -static int ft_wait1(uint32_t *word, uint32_t val, bool private_word, int64_t until) -{ - int64_t end = ft_real_rt() + ft_wait_slice_ns; - struct timespec ts; - if (until < end) end = until; - ts = ft_ts(end); - if (ft_raw_syscall6(SYS_futex, (long) word, - FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME | (private_word ? FUTEX_PRIVATE_FLAG : 0), (long) val, - (long) &ts, 0, FUTEX_BITSET_MATCH_ANY) == 0 || errno == EAGAIN) - return FT_W_WORD; - if (errno == ETIMEDOUT) return FT_W_PAGE; /* a slice's end, or the deadline: the caller reads the page */ - return errno == EINTR ? FT_W_INTR : FT_W_ERR; -} -/* until *word isn't val, the page changes from seq32, or real realtime ``until``; FT_W_ERR with errno the kernel's - (an unaligned or unmapped word). */ -static int ft_wait2(uint32_t *word, uint32_t val, bool private_word, uint32_t seq32, int64_t until) -{ - struct ft_futex_waitv w[2]; - struct timespec ts = ft_ts(until); - long r; - if (ft_waitv_missing) return ft_wait1(word, val, private_word, until); - /* the page first: futex_waitv returns the last of the futexes woken, so a caller's wake is never taken for the page's */ - w[0].val = seq32; - w[0].uaddr = (uint64_t) (uintptr_t) &ft_page->seq; - w[0].flags = 0x02; /* FUTEX2_SIZE_U32, shared */ - w[0].reserved = 0; - w[1].val = val; - w[1].uaddr = (uint64_t) (uintptr_t) word; - w[1].flags = 0x02 | (private_word ? 128 : 0); - w[1].reserved = 0; - r = ft_raw_syscall6(SYS_futex_waitv, (long) w, 2, 0, until == FT_NEVER ? 0 : (long) &ts, CLOCK_REALTIME, 0); - if (r == 1) return FT_W_WORD; - if (r == 0) return FT_W_PAGE; - if (errno == EAGAIN) return __atomic_load_n(word, __ATOMIC_RELAXED) != val ? FT_W_WORD : FT_W_PAGE; - if (errno == ETIMEDOUT) return FT_W_TIMEOUT; - if (errno == EINTR) return FT_W_INTR; - if (errno != ENOSYS && errno != EPERM) return FT_W_ERR; /* EPERM: a seccomp filter that refuses it so */ - ft_waitv_missing = 1; - return ft_wait1(word, val, private_word, until); -} - -/* futex(2) FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME through syscall(): until the universe reaches the deadline, - beside the page; *handled false leaves the call to the kernel as it is. */ -static long ft_u_futex_rt(uint32_t *uaddr, int op, uint32_t val, const struct timespec *abs, bool *handled) -{ - struct ft_clock_page s; - int64_t due = ft_ns(abs); - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int k; - if (ft_reached(&s, due)) - { - errno = ETIMEDOUT; - return -1; - } - k = ft_wait2(uaddr, val, (op & FUTEX_PRIVATE_FLAG) != 0, seq32, ft_real_of(&s, due)); - if (k == FT_W_WORD) - { - if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) - { - errno = EAGAIN; - return -1; - } - return 0; - } - if (k == FT_W_INTR) - { - errno = EINTR; - return -1; - } - if (k == FT_W_ERR) return -1; - if (!ft_snap(&s)) return 0; - } -} - -/* sem_timedwait / sem_clockwait(CLOCK_REALTIME) on glibc's semaphore (its layout is part of the ABI, since - process-shared semaphores outlive glibc versions): the value in the low word of data, the waiters above it, the - futex the value word, which sem_post wakes when there are waiters; ``private`` is glibc's futex flag the other way - round (0 for a process-private semaphore, FUTEX_PRIVATE_FLAG for a shared one). */ -struct ft_new_sem { uint64_t data; int private_flag; int pad; }; -static void ft_sem_unwait(void *sem) -{ - __atomic_fetch_sub(&((struct ft_new_sem *) sem)->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); -} -/* as one of the semaphore's waiters (counted in data's high word): 0 with a token taken, or the error. A cancelled wait - leaves the waiters it joined, as glibc's does. */ -static int ft_sem_wait_slow(struct ft_new_sem *sem, struct ft_clock_page *s, int64_t due) -{ - volatile int err = 0; - pthread_cleanup_push(ft_sem_unwait, sem); - uint64_t d = __atomic_fetch_add(&sem->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); - for (;;) - { - if ((d & 0xffffffffu) == 0) - { - int k, ct; - if (ft_reached(s, due)) - { - err = ETIMEDOUT; - break; - } - ct = ft_async_cancel(); - k = ft_wait2((uint32_t *) &sem->data, 0, sem->private_flag == 0, (uint32_t) s->seq, ft_real_of(s, due)); - ft_deferred_cancel(ct); - if (k == FT_W_INTR || k == FT_W_ERR) - { - err = k == FT_W_INTR ? EINTR : errno; - break; - } - if (!ft_snap(s)) - { - err = ETIMEDOUT; - break; - } - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - } - else if (__atomic_compare_exchange_n(&sem->data, &d, d - 1 - ((uint64_t) 1 << 32), true, __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED)) - break; - } - pthread_cleanup_pop(err != 0); - return err; -} -static int ft_u_sem_wait(sem_t *sem_, int64_t due, bool *handled) -{ - struct ft_new_sem *sem = (struct ft_new_sem *) sem_; - struct ft_clock_page s; - uint64_t d; - int err; - *handled = false; - if (sizeof(sem_t) < sizeof(struct ft_new_sem) || !ft_snap(&s)) return 0; - *handled = true; - pthread_testcancel(); - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - while ((d & 0xffffffffu) > 0) - if (__atomic_compare_exchange_n(&sem->data, &d, d - 1, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) return 0; - err = ft_sem_wait_slow(sem, &s, due); - if (err == 0) return 0; - errno = err; - return -1; -} - -static void ft_cw_release(void *w) -{ - sigset_t old; - ft_tab_lock(&old); - ((struct ft_cw *) w)->used = 0; - ft_tab_unlock(&old); -} -/* glibc's wait, a cancellation point: a cancelled wait gives up its entry, so the helper never wakes a condvar that - may be gone. */ -static int ft_cw_clockwait(struct ft_cw *w, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *end) -{ - volatile int r; - pthread_cleanup_push(ft_cw_release, w); - r = real_pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, end); - pthread_cleanup_pop(0); - return r; -} - -/* a realtime condvar wait (see ft_cw). -1 leaves it to the caller's own path (no page, table full). */ -static int ft_u_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -{ - struct ft_clock_page s; - struct ft_cw *w = NULL; - struct timespec end; - sigset_t old; - int64_t due = ft_ns(abstime), real_due; - int r, woken; - if (!ft_snap(&s)) return -1; - if (ft_reached(&s, due)) return ETIMEDOUT; - ft_helper_start(); - ft_tab_lock(&old); - for (int i = 0; i < FT_CW_MAX; i++) - if (!ft_cws[i].used) - { - w = &ft_cws[i]; - if (i >= ft_cws_top) ft_cws_top = i + 1; - break; - } - if (w != NULL) - { - w->cond = cond; - w->due = due; - w->woken = 0; - w->gen = FT_CW_UNPLANNED; - w->used = 1; - } - ft_tab_unlock(&old); - if (w == NULL) return -1; - if (!ft_snap(&s)) s.armed = 0; - real_due = ft_real_of(&s, due); - ft_tab_lock(&old); - w->real_due = real_due; - w->gen = s.generation; - ft_tab_unlock(&old); - end = ft_ts(real_due == FT_NEVER ? ft_real_rt() + 86400 * FT_NS : real_due); - r = ft_cw_clockwait(w, cond, mutex, &end); - ft_tab_lock(&old); - woken = w->woken; - w->used = 0; - ft_tab_unlock(&old); - /* a real timeout before the universe's deadline (the page moved it later) is a spurious wakeup; the helper's wake for - a jump that passed the deadline is the timeout */ - if (r == ETIMEDOUT) return (ft_snap(&s) && !ft_reached(&s, due)) ? 0 : ETIMEDOUT; - if (r == 0 && woken && ft_snap(&s) && ft_reached(&s, due)) return ETIMEDOUT; - return r; -} - -/* a realtime mutex wait: glibc's mutex has no way in, so slices (AGENTENV_WAIT_SLICE_MS) of a real - CLOCK_MONOTONIC wait, which loses nothing when it times out, with the page read between them. */ -static int ft_u_mutex_lock(pthread_mutex_t *mutex, int64_t due, bool *handled) -{ - struct ft_clock_page s; - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - struct timespec now, end; - int64_t at, sl; - int r; - if (ft_reached(&s, due)) return ETIMEDOUT; - at = ft_real_of(&s, due); - sl = (at == FT_NEVER) ? ft_wait_slice_ns : at - ft_real_rt(); - if (sl > ft_wait_slice_ns) sl = ft_wait_slice_ns; - if (sl < 1) sl = 1; - (*real_clock_gettime)(CLOCK_MONOTONIC, &now); - end = ft_ts(ft_ns(&now) + sl); - r = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &end); - if (r == EINVAL) /* a PI mutex on a kernel without FUTEX_LOCK_PI2 */ - { - struct timespec real = ft_ts(ft_real_rt() + sl); - r = real_pthread_mutex_timedlock(mutex, &real); - } - if (r != ETIMEDOUT) return r; - if (!ft_snap(&s)) return ETIMEDOUT; - } -} - -/* syscall(): futex waits for a realtime clock time follow the universe (raw futex users such as Rust's std - and GLib call it), clock_gettime reads it once the library is up; everything else goes straight to the kernel. */ -long syscall(long number, ...) -{ - va_list ap; - long a[6]; - va_start(ap, number); - for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); - va_end(ap); - if (number == SYS_futex && ft_uw_active()) - { - int op = (int) a[1]; - const struct timespec *ts = (const struct timespec *) a[3]; - if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET && (op & FUTEX_CLOCK_REALTIME) && ft_valid_ts(ts) - && (uint32_t) a[5] == FUTEX_BITSET_MATCH_ANY) - { - bool handled; - long r = ft_u_futex_rt((uint32_t *) a[0], op, (uint32_t) a[2], ts, &handled); - if (handled) return r; - } - } - else if (number == SYS_clock_gettime && ft_page != NULL && !ft_disabled && !dont_fake) - return clock_gettime((clockid_t) a[0], (struct timespec *) a[1]); - return ft_raw_syscall6(number, a[0], a[1], a[2], a[3], a[4], a[5]); -} - -/* ---- timerfds ---- */ -static int ft_u_timerfd_settime(int fd, int flags, const struct itimerspec *nv, struct itimerspec *ov, bool *handled) -{ - struct ft_clock_page s; - struct ft_timer *t = NULL; - struct itimerspec prev; - sigset_t old; - int clock, r; - bool was = false; - *handled = false; - if (fd < 0 || fd >= FT_TFD_CLOCKS || nv == NULL) return 0; - clock = ft_tfd_clock[fd] - 1; - *handled = true; - if (ft_timers_any) - { - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; /* the record stays, with the timer's descriptors, while any is open */ - } - ft_tab_unlock(&old); - } - if (!ft_rt_clock(clock) || !(flags & TFD_TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timerfd_settime)(fd, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if ((t = ft_timer_find_fd(fd)) == NULL && (t = ft_timer_new()) != NULL) - { - t->kind = 1; - t->nfds = 1; - t->fds[0] = fd; - } - if (t == NULL) - { - ft_tab_unlock(&old); - *handled = false; /* table full: the caller's own (translated once) path */ - return 0; - } - t->clock = clock; - t->flags = flags; - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timerfd_gettime)(fd, ov); - } - r = ft_timer_arm(t, &s, true); - if (r != 0) t->tracked = 0; - ft_timers_any = 1; - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -static int ft_u_timerfd_gettime(int fd, struct itimerspec *cur, bool *handled) -{ - struct ft_timer *t; - sigset_t old; - *handled = false; - if (!ft_timers_any || cur == NULL) return 0; - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - *handled = true; - } - ft_tab_unlock(&old); - return 0; -} - -/* - * ---- descriptors: a timerfd's record follows its file, not a number. close() takes a descriptor off it (the record - * goes with the last), dup() and its kin add the new one, so the helper never arms a number the program has closed or - * that now names another file. A number closed where this can't see (closefrom, a raw system call) is dropped when - * timerfd_create hands it out again; a timer with more duplicates than a record holds is left armed as it was. ---- - */ -static int (*ft_real_close)(int); -static int (*ft_real_dup)(int); -static int (*ft_real_dup2)(int, int); -static int (*ft_real_dup3)(int, int, int); -static int (*ft_real_fcntl)(int, int, ...); -static int (*ft_real_fcntl64)(int, int, ...); -static int (*ft_real_close_range)(unsigned int, unsigned int, int); -#define FT_RESOLVE(name) do { if (ft_real_##name == NULL) ft_real_##name = dlsym(RTLD_NEXT, #name); } while (0) - -static inline bool ft_tfd_known(int fd) -{ - return fd >= 0 && fd < FT_TFD_CLOCKS && __atomic_load_n(&ft_tfd_clock[fd], __ATOMIC_RELAXED) != 0; -} -/* the process whose descriptors these tables describe: set at load and in the child of each glibc fork. A child that - shares this memory without fork (vfork, as Python's subprocess uses, or a CLONE_VM clone) has a descriptor table of - its own, so what it closes or duplicates before exec changes nothing here. A fork's child also takes the tables' - lock free: another thread may have held it at the fork, and no thread of the child would ever release it. */ -static volatile pid_t ft_fd_owner = 0; -static void ft_fd_forked(void) -{ - ft_fd_owner = getpid(); - ft_tab_lock_word = 0; -} -static inline bool ft_fd_mine(void) -{ - return getpid() == ft_fd_owner; -} -/* ``fd`` closed, or about to name another file. Lock held. */ -static void ft_fd_gone_locked(int fd) -{ - struct ft_timer *t; - ft_tfd_clock[fd] = 0; - while ((t = ft_timer_find_fd(fd)) != NULL) - { - int k = 0; - while (t->fds[k] != fd) k++; - t->fds[k] = t->fds[--t->nfds]; - if (t->nfds == 0) t->kind = 0; - } -} -static void ft_fd_gone(int fd) -{ - sigset_t old; - if (!ft_tfd_known(fd) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_gone_locked(fd); - ft_tab_unlock(&old); -} -/* ``to`` now names ``from``'s file, a known timerfd. Lock held. */ -static void ft_fd_duped_locked(int from, int to) -{ - struct ft_timer *t; - if (to < 0 || to >= FT_TFD_CLOCKS) return; - ft_tfd_clock[to] = ft_tfd_clock[from]; - if ((t = ft_timer_find_fd(from)) != NULL) - { - if (t->nfds < FT_TIMER_FDS) - t->fds[t->nfds++] = to; - else - t->kind = 0; - } -} -static void ft_fd_duped(int from, int to) -{ - sigset_t old; - if (!ft_tfd_known(from) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_duped_locked(from, to); - ft_tab_unlock(&old); -} -/* dup2 or dup3 onto ``fd2``. One that replaces a timerfd (whose close doesn't block) makes the change and its - bookkeeping under the lock, so a failed call keeps the record and the helper never arms the number in between. */ -static int ft_dup_onto(int fd, int fd2, int flags, bool three) -{ - sigset_t old; - int r; - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - if (fd == fd2 || !ft_tfd_known(fd2) || !ft_fd_mine()) - { - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0 && fd != fd2) ft_fd_duped(fd, r); - return r; - } - ft_tab_lock(&old); - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0) - { - ft_fd_gone_locked(fd2); - if (ft_tfd_known(fd)) ft_fd_duped_locked(fd, fd2); - } - ft_tab_unlock(&old); - return r; -} - -int close(int fd) -{ - ft_fd_gone(fd); - FT_RESOLVE(close); - return (*ft_real_close)(fd); -} -int dup(int fd) -{ - int r; - FT_RESOLVE(dup); - r = (*ft_real_dup)(fd); - if (r >= 0) ft_fd_duped(fd, r); - return r; -} -int dup2(int fd, int fd2) -{ - return ft_dup_onto(fd, fd2, 0, false); -} -int dup3(int fd, int fd2, int flags) -{ - return ft_dup_onto(fd, fd2, flags, true); -} -/* the argument passed on as glibc's own fcntl reads it, a pointer-sized word whatever the command */ -int fcntl(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl); - r = (*ft_real_fcntl)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -int fcntl64(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl64); - r = (*ft_real_fcntl64)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -/* one that closes a timerfd runs under the lock, as dup2 does, so a failed call keeps the records. In practice it runs - in a child of fork before exec, where no other thread wants the lock. */ -int close_range(unsigned int first, unsigned int last, int flags) -{ - sigset_t old; - bool timerfd = false; - int r; - FT_RESOLVE(close_range); - if (!(flags & CLOSE_RANGE_CLOEXEC) && ft_fd_mine()) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS && !timerfd; fd++) timerfd = ft_tfd_known((int) fd); - if (!timerfd) return (*ft_real_close_range)(first, last, flags); - ft_tab_lock(&old); - r = (*ft_real_close_range)(first, last, flags); - if (r == 0) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS; fd++) - if (ft_tfd_known((int) fd)) ft_fd_gone_locked((int) fd); - ft_tab_unlock(&old); - return r; -} -/* the owner, and the real calls resolved at load, so a first close() in a signal handler doesn't call dlsym */ -static void __attribute__((constructor)) ft_fd_init(void) -{ - ft_fd_owner = getpid(); - pthread_atfork(NULL, NULL, ft_fd_forked); - FT_RESOLVE(close); - FT_RESOLVE(dup); - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - FT_RESOLVE(fcntl); - FT_RESOLVE(fcntl64); - FT_RESOLVE(close_range); -} - -/* ---- POSIX timers (unversioned: upstream's GLIBC_2.2 / 2.3.3 versions don't bind glibc 2.34's timer_settime) ---- */ -int timer_create(clockid_t clock, struct sigevent *sev, timer_t *id) -{ - int r; - ftpl_init(); - if (real_timer_create_new == NULL) real_timer_create_new = dlsym(RTLD_NEXT, "timer_create"); - if (real_timer_create_new == NULL) - { - errno = ENOSYS; - return -1; - } - r = (*real_timer_create_new)(clock, sev, id); - if (r == 0 && id != NULL && ft_rt_clock(clock) && ft_faked()) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - t = ft_timer_find_id(*id); - if (t == NULL) t = ft_timer_new(); - if (t != NULL) - { - memset(t, 0, sizeof *t); - t->kind = 2; - t->id = *id; - t->clock = clock; - } - ft_timers_any = 1; - ft_tab_unlock(&old); - } - return r; -} -int timer_delete(timer_t id) -{ - ftpl_init(); - if (real_timer_delete_new == NULL) real_timer_delete_new = dlsym(RTLD_NEXT, "timer_delete"); - if (ft_timers_any) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - if ((t = ft_timer_find_id(id)) != NULL) t->kind = 0; - ft_tab_unlock(&old); - } - return (*real_timer_delete_new)(id); -} -int timer_settime(timer_t id, int flags, const struct itimerspec *nv, struct itimerspec *ov) -{ - struct ft_clock_page s; - struct ft_timer *t; - struct itimerspec prev; - sigset_t old; - bool was = false; - int r; - ftpl_init(); - if (real_timer_settime_new == NULL) real_timer_settime_new = dlsym(RTLD_NEXT, "timer_settime"); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (!ft_timers_any || nv == NULL) return (*real_timer_settime_new)(id, flags, nv, ov); - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; - } - ft_tab_unlock(&old); - if (t == NULL || !ft_uw_active() || !(flags & TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timer_settime_new)(id, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timer_gettime_new)(id, ov); - } - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - r = ft_timer_arm(t, &s, true); - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -int timer_gettime(timer_t id, struct itimerspec *cur) -{ - sigset_t old; - struct ft_timer *t; - ftpl_init(); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (ft_timers_any && cur != NULL) - { - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - ft_tab_unlock(&old); - return 0; - } - ft_tab_unlock(&old); - } - return (*real_timer_gettime_new)(id, cur); -} - diff --git a/src/agent_env/agents/agents/claude-code/clock/build.sh b/src/agent_env/agents/agents/claude-code/clock/build.sh deleted file mode 100755 index 8d7227284..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/build.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -# Builds agent-env's libfaketime: upstream 0.9.12 from its sha256-pinned tarball, libfaketime.patch applied and the two -# engine files beside it copied in, the multithreaded library with stat and utime faking compiled out (file times stay -# real); and the TLS shim beside it (tls_realtime.c). Writes OUT/libfaketimeMT.so.1 and OUT/libtls_realtime.so, which -# /etc/ld.so.preload lists in that order. Needs a C toolchain, make, patch and curl; builds on x86_64 and aarch64. -# build.sh OUT [TARBALL] (TARBALL: a local copy of upstream's v0.9.12 tarball, else it is downloaded) -set -eu -out=$1 -here=$(cd "$(dirname "$0")" && pwd) -work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT -tarball=${2:-$work/libfaketime.tar.gz} -[ -f "$tarball" ] || curl -fsSL -o "$tarball" https://github.com/wolfcw/libfaketime/archive/refs/tags/v0.9.12.tar.gz -echo "4fc32218697c052adcdc5ee395581f2554ca56d086ac817ced2be0d6f1f8a9fa $tarball" | sha256sum -c - >/dev/null -tar xzf "$tarball" -C "$work" -src=$work/libfaketime-0.9.12 -patch -s -d "$src" -p1 < "$here/libfaketime.patch" -cp "$here/agentenv_clock_page.c" "$here/agentenv_clock_waits.c" "$src/src/" -make -s -C "$src/src" FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME" libfaketimeMT.so.1 -mkdir -p "$out" -cp "$src/src/libfaketimeMT.so.1" "$out/" -${CC:-cc} -std=gnu99 -O2 -fPIC -shared -Wall -Wextra -Werror -o "$out/libtls_realtime.so" "$here/tls_realtime.c" -ldl diff --git a/src/agent_env/agents/agents/claude-code/clock/clock-follower b/src/agent_env/agents/agents/claude-code/clock/clock-follower deleted file mode 100755 index 652431687..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clock-follower +++ /dev/null @@ -1,204 +0,0 @@ -#!/usr/bin/python3 -I -"""The computer's clock follower: holds a long poll on the gateway's ``/clock/watch`` and writes each state it learns -to the clock page (``clockpage.py``), which agent-env's libfaketime reads in every session program, so the session reads -the universe's time. It runs as its own user, under root's supervisord, so the session user can read the clock but -neither write it nor stop it. - -Core writes its config, the gateway's watch URL as this computer reaches it, the computer's name and its read token, -through the sandbox's root channel (``CONFIG``, readable by root and this user alone); never through the driver, which -runs as the session user. It reads the config every quarter second, during a watch too, and follows a changed one at -once. Until there is one, the clock stays as root made it at boot: real time. It reports how it's doing in ``STATUS``, -which anyone may read and which holds no token: the gateway it follows, whether it's connected, the version it shows, -or its last error. - -The gateway's clock is read against this machine's: its offset is four-stamp, ``((received - t1) + (sent - t4)) / 2``, -from the newest of the last 8 answers whose round trip, less the time the gateway held the watch, isn't an outlier. A -state is written anchored, universe V at this machine's real time A (the gateway's ``sent``) at rate R, so every reader -computes V + (now - A) * R. Each watch names the version it shows, which is its acknowledgement of that version.""" -import json -import os -import queue -import random -import sys -import threading -import time -import urllib.error -import urllib.parse -import urllib.request -from datetime import datetime - -sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) -import clockpage # noqa: E402 - -CONFIG = os.environ.get("AGENTENV_CLOCK_CONFIG", "/etc/agentenv/clock/follower.json") -STATUS = os.environ.get("AGENTENV_CLOCK_STATUS", "/run/agentenv-clock/status.json") -#: How long a watch may take: the gateway holds one up to 25 s (its card's ``hold_s``), plus a margin. -WATCH_TIMEOUT_S = 35.0 -#: How often the config is read, during a watch too. -CONFIG_POLL_S = 0.25 -#: A re-anchoring (the offset moved) is written only past this much, so the page doesn't churn on noise. -ADJUST_S = 0.001 -SAMPLES = 8 -#: After a failed watch it waits before asking again: RETRY_FIRST_S at first, doubled after each failure up to -#: RETRY_MAX_S, each wait a random fraction (RETRY_JITTER) of that, so computers that lost a gateway don't all return at -#: once. -RETRY_FIRST_S = 0.05 -RETRY_MAX_S = 2.0 -RETRY_JITTER = (0.5, 1.0) -OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) - - -def log(event: str, **fields) -> None: - sys.stdout.write(json.dumps({"event": event, "real": time.time(), **fields}) + "\n") - sys.stdout.flush() - - -def atomic_write(path: str, text: str) -> None: - tmp = f"{path}.{os.getpid()}.tmp" - with open(tmp, "w") as f: - f.write(text) - os.chmod(tmp, 0o644) - os.replace(tmp, path) - - -def read_config() -> tuple[bytes | None, dict | None]: - """The config's bytes and what they say, (None, None) when there is none, (bytes, None) when it isn't one.""" - try: - with open(CONFIG, "rb") as f: - raw = f.read() - except OSError: - return None, None - try: - config = json.loads(raw) - return raw, config if isinstance(config, dict) and all( - isinstance(config.get(key), str) and config[key] for key in ("watch_url", "name", "token")) else None - except ValueError: - return raw, None - - -def parse_time(text: str) -> float: - return datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() - - -def offset(samples: list[dict]) -> tuple[float, int]: - """The offset from the newest sample whose delay isn't an outlier (over twice the median and 2 ms), and how many - were dropped.""" - delays = sorted(sample["delay"] for sample in samples) - median = delays[len(delays) // 2] - kept = [sample for sample in samples if sample["delay"] <= max(2 * median, median + 0.002)] - return (kept or samples)[-1]["theta"], len(samples) - len(kept) - - -def state_of(body: dict, theta: float) -> tuple[int, float, float, float]: - """(armed, universe at the anchor, rate, anchor on this machine's clock) for a watch's answer ``body``.""" - if not body.get("armed"): - return 0, 0.0, 1.0, time.time() - return (1, parse_time(body["virtual_time"]), float(body["virtual_seconds_per_real_second"]), - body["sent"] - theta) - - -def watch(url: str, name: str, token: str, after: str, out: queue.Queue) -> None: - """One watch, its answer (or what failed) put on ``out`` with the stamps around it.""" - t1 = time.time() - try: - request = urllib.request.Request( - f"{url}?{urllib.parse.urlencode({'after': after, 'follower': name})}", - headers={"Authorization": f"Bearer {token}"}) - with OPENER.open(request, timeout=WATCH_TIMEOUT_S) as answer: - body = json.loads(answer.read()) - out.put((t1, time.time(), body, None)) - except Exception as e: # noqa: BLE001 - away, restarting or refusing: the follower says so and asks again - out.put((t1, time.time(), None, e)) - - -class Follower: - """Follows one config until it changes.""" - - def __init__(self, raw: bytes, config: dict): - self.raw, self.url, self.name, self.token = raw, config["watch_url"], config["name"], config["token"] - self.after, self.samples, self.written = "", [], None - self.errors, self.backoff = 0, RETRY_FIRST_S - self.status = {"url": self.url, "name": self.name, "connected": False, "version": None, "armed": False, - "error": None} - - def report(self, **fields) -> None: - self.status.update(fields) - atomic_write(STATUS, json.dumps(self.status)) - - def run(self) -> None: - log("following", url=self.url, name=self.name) - self.report() - while True: - answers: queue.Queue = queue.Queue() - threading.Thread(target=watch, args=(self.url, self.name, self.token, self.after, answers), - daemon=True).start() - while True: - try: - t1, t4, body, error = answers.get(timeout=CONFIG_POLL_S) - break - except queue.Empty: - if read_config()[0] != self.raw: - return # its watch is left to end on its own; nothing it answers is applied - if error is not None: - self.failed(error) - continue - if read_config()[0] != self.raw: - return - try: - self.apply(t1, t4, body) - except (KeyError, TypeError, ValueError, AttributeError) as e: - self.failed(ValueError(f"not a clock watch's answer ({type(e).__name__}: {e})")) - - def failed(self, error: Exception) -> None: - self.errors += 1 - code = error.code if isinstance(error, urllib.error.HTTPError) else None - self.report(connected=False, error=f"{type(error).__name__}: {error}"[:300], status=code, errors=self.errors, - error_at=time.time()) - if self.errors <= 20 or self.errors % 100 == 0: - log("error", errors=self.errors, status=code, error=repr(error)[:200], backoff=self.backoff) - deadline = time.monotonic() + self.backoff * random.uniform(*RETRY_JITTER) - while time.monotonic() < deadline and read_config()[0] == self.raw: - time.sleep(min(CONFIG_POLL_S, max(0.0, deadline - time.monotonic()))) - self.backoff = min(self.backoff * 2, RETRY_MAX_S) - - def apply(self, t1: float, t4: float, body: dict) -> None: - if self.errors: - log("reconnected", after_errors=self.errors) - self.errors, self.backoff = 0, RETRY_FIRST_S - held = body["sent"] - body["received"] - self.samples = (self.samples + [{"delay": max(0.0, (t4 - t1) - held), - "theta": ((body["received"] - t1) + (body["sent"] - t4)) / 2}])[-SAMPLES:] - theta, dropped = offset(self.samples) - armed, universe, rate, anchor = state_of(body, theta) - meaning = (armed, rate, universe - rate * anchor if armed else 0.0) - changed = body["version"] != self.after - moved = (self.written is None or meaning[:2] != self.written[:2] - or abs(meaning[2] - self.written[2]) > ADJUST_S) - if changed or moved: - written = clockpage.write(armed, universe, rate, anchor) - self.written = meaning - log("applied" if changed else "adjusted", version=body["version"], armed=bool(armed), - virtual_time=body.get("virtual_time"), spec=written["spec"], theta=theta, held=held, dropped=dropped) - self.after = body["version"] - self.report(connected=True, version=self.after, armed=bool(armed), virtual_time=body.get("virtual_time"), - rate=body.get("virtual_seconds_per_real_second"), theta=theta, applied_at=time.time(), error=None, - status=None, errors=0) - - -def main() -> None: - log("start", pid=os.getpid(), uid=os.getuid()) - atomic_write(STATUS, json.dumps({"connected": False, "error": None, "waiting": "no config yet"})) - while True: - raw, config = read_config() - if config is None: - if raw is not None: - atomic_write(STATUS, json.dumps({"connected": False, "error": "its config isn't one: it needs " - "watch_url, name and token"})) - while read_config()[0] == raw: - time.sleep(CONFIG_POLL_S) - continue - Follower(raw, config).run() - - -if __name__ == "__main__": - main() diff --git a/src/agent_env/agents/agents/claude-code/clock/clock.conf b/src/agent_env/agents/agents/claude-code/clock/clock.conf deleted file mode 100644 index 715987d43..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clock.conf +++ /dev/null @@ -1,11 +0,0 @@ -# Read once by agent-env's libfaketime as each process loads it. While this file exists it is the only source of these -# settings: the environment changes none of them (AGENTENV_REAL_TIME=1 is the one switch a program's environment has). -# Only the session user's processes are faked; the clock comes from the page the follower writes, never from a cache. -AGENTENV_SESSION_UID=1000 -FAKETIME_TIMESTAMP_FILE=/run/agentenv-clock/clock -FAKETIME_NO_CACHE=1 -FAKETIME_DONT_FAKE_MONOTONIC=1 -FAKETIME_FMT=%s -FAKETIME_DISABLE_SHM=1 -AGENTENV_CLOCK_PAGE=/dev/shm/agentenv-clock -AGENTENV_CLOCK_PAGE_UID=0 diff --git a/src/agent_env/agents/agents/claude-code/clock/clockpage.py b/src/agent_env/agents/agents/claude-code/clock/clockpage.py deleted file mode 100644 index 4002bb6c2..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clockpage.py +++ /dev/null @@ -1,157 +0,0 @@ -"""The follower's side of the clock page (version 2) and of the clock file: writes the universe's realtime -clock to both. Elapsed time is real, so the page's monotonic fields are written as the real monotonic clock (identity) -and no library reads them; they stay so the page keeps its 80-byte layout. - - clockpage.py set UNIVERSE RATE [ANCHOR] armed: UNIVERSE (epoch s) at real time ANCHOR (default now), at RATE - clockpage.py clear real time - clockpage.py create OWNER as root at boot: the page, OWNER's from its first byte, unarmed - clockpage.py show - -The page (default /dev/shm/agentenv-clock, a tmpfs, so readers that mapped it see every write) is created once, 0644, -owned by whoever writes it, and rewritten in place: seq odd, the fields and their check, seq even, then FUTEX_WAKE on -seq, which wakes every wait agent-env's libfaketime has for a clock time, and its helper threads, so they plan again. -Readers never see a torn value: they retry (waiting on seq) while seq is odd or changed, or the check doesn't match. -A page a writer died writing (seq left odd) is taken up by the next write, which then writes the universe as given. - -A write that would put the universe less than CONTINUITY_NS behind where the page has it now (a rate change computed -from a universe read a moment before the write) continues it from where it is instead: the write's real time is taken -last, while seq is odd, and the universe at it read from the page being replaced, so no reader of either page ever sees -the clock step back. A larger step back is a set_time and is written as given.""" -import ctypes -import mmap -import os -import platform -import pwd -import stat -import struct -import sys -import time -from decimal import Decimal - -PAGE = os.environ.get("AGENTENV_CLOCK_PAGE", "/dev/shm/agentenv-clock") -FILE = os.environ.get("AGENTENV_CLOCK_FILE", "/run/agentenv-clock/clock") -MAGIC, VERSION = 0x4B434541, 2 -# magic version seq universe_ns anchor_ns rate armed reserved generation check mono_universe_ns mono_anchor_ns -LAYOUT = struct.Struct(" int: - rate_bits = struct.unpack(" bytes: - """The page's bytes for these fields, its check among them: what every write writes and every reader checks, - agent-env's libfaketime and the egress proxy (its egress/testdata pins them).""" - return LAYOUT.pack(MAGIC, VERSION, seq, universe_ns, anchor_ns, rate, armed, 0, generation, - check(seq, universe_ns, anchor_ns, rate, armed, generation, mono_universe_ns, mono_anchor_ns), - mono_universe_ns, mono_anchor_ns) - - -def open_page() -> mmap.mmap: - try: - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o644) - os.ftruncate(fd, LAYOUT.size) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - except FileExistsError: - fd = os.open(PAGE, os.O_RDWR) - if os.fstat(fd).st_uid != os.getuid(): - raise SystemExit(f"{PAGE} isn't ours") - if os.fstat(fd).st_size < LAYOUT.size: - os.close(fd) - raise SystemExit(f"{PAGE} is an older page") - m = mmap.mmap(fd, LAYOUT.size) - os.close(fd) - return m - - -def _wake(m: mmap.mmap) -> None: - """FUTEX_WAKE every waiter on seq's low word (a shared futex: other processes' read-only mappings are woken too).""" - if _SYS_FUTEX is None: - return - buf = (ctypes.c_char * LAYOUT.size).from_buffer(m) - try: - _libc.syscall(ctypes.c_long(_SYS_FUTEX), ctypes.c_void_p(ctypes.addressof(buf) + 8), ctypes.c_int(_FUTEX_WAKE), - ctypes.c_int(0x7FFFFFFF), None, None, ctypes.c_int(0)) - finally: - del buf - - -def _universe_at(armed, universe_ns, anchor_ns, rate, now_ns) -> int: - if not armed: - return now_ns - if rate == 0 or now_ns <= anchor_ns: - return universe_ns - return universe_ns + int((now_ns - anchor_ns) * rate) - - -def write(armed: int, universe: float, rate: float, anchor: float) -> dict: - m = open_page() - _, _, seq, o_u, o_a, o_rate, o_armed, _, generation, _, _, _ = LAYOUT.unpack(m[:LAYOUT.size]) - universe_ns, anchor_ns = int(Decimal(str(universe)) * 10**9), int(Decimal(str(anchor)) * 10**9) - if not armed: - rate = 1.0 - generation += 1 - dead = seq & 1 # a writer died mid-write: its fields may be torn, so this write goes on from the next even seq - seq += dead - m[8:16] = struct.pack(" 1 and not dead and 0 < old_now - new_now < CONTINUITY_NS - if continued: - universe_ns, anchor_ns = old_now, now_rt - body = encode(seq + 2, universe_ns, anchor_ns, rate, armed, generation, now_mono, now_mono) - m[16:] = body[16:] - m[8:16] = struct.pack(" 1 else 0, - "continued": bool(continued), "universe_ns": universe_ns, "anchor_ns": anchor_ns} - - -def create(owner: str) -> None: - """As root at boot: the page, ``owner``'s from its first byte (unarmed), replacing one anyone else made.""" - user = pwd.getpwnam(owner) - try: - st = os.lstat(PAGE) - if stat.S_ISREG(st.st_mode) and st.st_uid == user.pw_uid and st.st_size == LAYOUT.size: - return - os.unlink(PAGE) - except FileNotFoundError: - pass - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) - try: - os.fchown(fd, user.pw_uid, user.pw_gid) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - os.fchmod(fd, 0o644) - finally: - os.close(fd) - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "set": - anchor = float(sys.argv[4]) if len(sys.argv) > 4 else time.time() - print(write(1, float(sys.argv[2]), float(sys.argv[3]), anchor)) - elif cmd == "create": - create(sys.argv[2]) - elif cmd == "clear": - print(write(0, 0.0, 1.0, time.time())) - else: - with open(PAGE, "rb") as f: - print(LAYOUT.unpack(f.read(LAYOUT.size))) diff --git a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch deleted file mode 100644 index d26148eed..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch +++ /dev/null @@ -1,1527 +0,0 @@ -SPDX-License-Identifier: GPL-2.0-only -agent-env's changes to libfaketime 0.9.12 (src/), applied from the tarball's root with patch -p1 by build.sh, which -also copies in agentenv_clock_page.c and agentenv_clock_waits.c, included where marked below. A change to libfaketime, -so licensed as libfaketime is, under the GNU General Public License version 2 (COPYING). - -diff -ruN a/src/libfaketime.c b/src/libfaketime.c ---- a/src/libfaketime.c -+++ b/src/libfaketime.c -@@ -231,11 +231,14 @@ - static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp); - #endif - #ifdef FAKE_PTHREAD --static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict); --static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); --static pthread_rwlock_t monotonic_conds_lock; -+static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_clockwait) (pthread_cond_t *, pthread_mutex_t*, clockid_t, const struct timespec *); -+static int (*real_pthread_mutex_timedlock) (pthread_mutex_t *, const struct timespec *); -+static int (*real_pthread_mutex_clocklock) (pthread_mutex_t *, clockid_t, const struct timespec *); -+#endif -+#ifdef FAKE_TIMERS -+static int (*real_timerfd_create) (int, int); - #endif - - #ifndef __APPLE__ -@@ -334,6 +337,7 @@ - bool str_array_contains(const char *haystack, const char *needle); - void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym); - -+#include "agentenv_clock_page.c" /* agent-env: settings, who is faked, the clock page */ - /** Semaphore protecting shared data */ - static sem_t *shared_sem = NULL; - -@@ -420,6 +424,29 @@ - static bool parse_config_file = true; - - static void ft_cleanup (void) __attribute__ ((destructor)); -+ -+#ifdef PTHREAD_SINGLETHREADED_TIME -+/* agentenv: the lock fake_clock_gettime holds, at file scope so a fork() can't hand the child a held lock. */ -+static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; -+static sigset_t ft_fork_mask; -+static void ft_atfork_prepare(void) -+{ -+ sigset_t all; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &ft_fork_mask); -+ pthread_mutex_lock(&time_mutex); -+} -+static void ft_atfork_parent(void) -+{ -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+static void ft_atfork_child(void) -+{ -+ pthread_mutex_init(&time_mutex, NULL); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+#endif - static void ftpl_init (void) __attribute__ ((constructor)); - - -@@ -519,7 +546,7 @@ - - static void ft_shm_destroy(void) - { -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - - if (ft_shared_env != NULL) - { -@@ -553,7 +580,7 @@ - static void ft_shm_really_init (void) - { - int ticks_shm_fd; -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - sem_t *shared_semR = NULL; - static int nt=1; - -@@ -561,7 +588,7 @@ - if (ft_shared_env == NULL) - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - - /* check for stale semaphore / shared memory information */ -@@ -575,7 +602,7 @@ - if (SEM_FAILED == (shared_semR = sem_open(sem_name, 0))) /* gone stale? */ - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - else - { -@@ -631,7 +658,7 @@ - exit(1); - } - } -- if (getenv("FAKETIME_FLSHM") != NULL) -+ if (ft_getenv("FAKETIME_FLSHM") != NULL) - { /* force the deletion of the shm sync env variable */ - unsetenv("FAKETIME_SHARED"); - } -@@ -653,12 +680,6 @@ - sem_close(shared_sem); - shared_sem = NULL; - } --#ifdef FAKE_PTHREAD -- if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) { -- fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n"); -- exit(-1); -- } --#endif - if (shmCreator == true) ft_shm_destroy(); - } - -@@ -672,8 +693,8 @@ - static void get_fake_monotonic_setting(int* current_value) - { - char *tmp_env; -- if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -- || (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -+ || (tmp_env = ft_getenv("DONT_FAKE_MONOTONIC")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -1140,7 +1161,7 @@ - #endif /* ifndef __APPLE__ */ - #endif /* ifdef FAKE_STAT */ - --#ifdef STATX_TYPE -+#if defined STATX_TYPE && defined FAKE_STAT - static inline void fake_statx_timestamp(struct statx_timestamp* p) - { - struct timespec t = {p->tv_sec,p->tv_nsec}; -@@ -1329,6 +1350,7 @@ - * Contributed by Balint Reczey in v0.9.5 - */ - -+#include "agentenv_clock_waits.c" /* agent-env: universe-time reads and waits */ - #ifdef FAKE_SLEEP - /* - * Faked nanosleep() -@@ -1343,6 +1365,7 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_nanosleep != NULL) return (*real_nanosleep)(req, rem); - if (real_nanosleep == NULL) - { - return -1; -@@ -1395,13 +1418,39 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_clock_nanosleep != NULL && (!(flags & TIMER_ABSTIME) || !ft_rt_clock(clock_id) || !ft_faked())) -+ return (*real_clock_nanosleep)(clock_id, flags, req, rem); -+ if (ft_uw_active() && ft_valid_ts(req)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap(&s)) return ft_rt_sleep(&s, ft_ns(req)); -+ } - if (real_clock_nanosleep == NULL) - { - return -1; - } - if (req != NULL) - { -- if (flags & TIMER_ABSTIME) /* sleep until absolute time */ -+ if ((flags & TIMER_ABSTIME) && clock_id == CLOCK_REALTIME && ft_translates_wait(req)) -+ { -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, req, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_clock_nanosleep)(CLOCK_MONOTONIC, TIMER_ABSTIME, &slice, NULL)); -+ if (result != 0) return result; -+ if (!ft_deadline_moved(req, &mono, &gen) && last) return 0; -+ } -+ } -+ if ((flags & TIMER_ABSTIME) && (ft_disabled || dont_fake || clock_id == CLOCK_REALTIME)) -+ { -+ real_req = *req; -+ } -+ else if (flags & TIMER_ABSTIME) /* sleep until absolute time */ - { - struct timespec tdiff, timeadj; - timespecsub(req, &user_faked_time_timespec, &timeadj); -@@ -1482,6 +1531,7 @@ - int result; - - ftpl_init(); -+ if (real_usleep != NULL) return (*real_usleep)(usec); - if (user_rate_set && !dont_fake) - { - struct timespec real_req; -@@ -1531,6 +1581,7 @@ - #endif - { - ftpl_init(); -+ if (real_sleep != NULL) return (*real_sleep)(seconds); - if (user_rate_set && !dont_fake) - { - if (real_nanosleep == NULL) -@@ -1551,7 +1602,7 @@ - else - { - int result; -- struct timespec real_req = {seconds, 0}, rem; -+ struct timespec real_req = {seconds, 0}, rem = {0, 0}; - timespecmul(&real_req, 1.0 / user_rate, &real_req); - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem)); -@@ -1604,6 +1655,8 @@ - { - return -1; - } -+ (void) seconds_real; -+ return (*real_alarm)(seconds); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*alarm)(seconds_real)); -@@ -1627,6 +1680,7 @@ - { - return -1; - } -+ return (*real_ppoll)(fds, nfds, timeout_ts, sigmask); - if (timeout_ts != NULL) - { - if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0))) -@@ -1662,6 +1716,7 @@ - { - return -1; - } -+ return (*real_epoll_wait)(epfd, events, maxevents, timeout); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1686,6 +1741,7 @@ - { - return -1; - } -+ return (*real_epoll_pwait)(epfd, events, maxevents, timeout, sigmask); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1715,6 +1771,8 @@ - { - return -1; - } -+ (void) timeout_real; -+ return (*real_poll)(fds, nfds, timeout); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real)); -@@ -1748,6 +1806,7 @@ - { - return -1; - } -+ return (*real_select)(nfds, readfds, writefds, errorfds, timeout); - - if (timeout != NULL) - { -@@ -1812,6 +1871,7 @@ - { - return -1; - } -+ return (*real_pselect)(nfds, readfds, writefds, errorfds, timeout, sigmask); - - if (timeout != NULL) - { -@@ -1831,98 +1891,63 @@ - } - #endif - --int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) -+static int ft_sem_wait_until(sem_t *sem, const struct timespec *fake_abs) - { -- int result; -- struct timespec real_abs_timeout, *real_abs_timeout_pt; -- -- /* sanity check */ -- if (abs_timeout == NULL) -- { -- return -1; -- } -- -- if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -- -- if (!dont_fake) -- { -- struct timespec tdiff, timeadj; -- -- timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, CLOCK_MONOTONIC, &slice)); -+ if (result == 0 || errno != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) - { -- timespecmul(&tdiff, user_rate, &timeadj); -+ errno = ETIMEDOUT; -+ return -1; - } -- else -- { -- timeadj = tdiff; -- } -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout); -- real_abs_timeout_pt = &real_abs_timeout; -- } -- else -- { -- /* cast away constness */ -- real_abs_timeout_pt = (struct timespec *)abs_timeout; - } -- -- DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt)); -- return result; - } - --/* EXPERIMENTAL */ --int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) - { - int result; -- struct timespec real_abstime, *real_abstime_pt; -- -- if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC)) -+ ftpl_init(); -+ if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -+ if (ft_uw_active() && ft_translates_wait(abs_timeout)) - { -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -- return result; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abs_timeout), &handled); -+ if (handled) return result; - } -- -- /* sanity check */ -- if (abstime == NULL) -+ if (!ft_translates_wait(abs_timeout) || real_sem_clockwait == NULL) - { -- return -1; -+ DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, abs_timeout)); -+ return result; - } -+ return ft_sem_wait_until(sem, abs_timeout); -+} - -+int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+{ -+ int result; -+ ftpl_init(); - if (!CHECK_MISSING_REAL(sem_clockwait)) return -1; -- -- if (!dont_fake) -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- struct timespec tdiff, timeadj; -- -- timespecsub(abstime, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -- { -- timespecmul(&tdiff, 1.0 / user_rate, &timeadj); -- } -- else -- { -- timeadj = tdiff; -- } -- if (clockid == CLOCK_REALTIME) -- { -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime); -- } -- if (clockid == CLOCK_MONOTONIC) -- { -- timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime); -- } -- real_abstime_pt = &real_abstime; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abstime), &handled); -+ if (handled) return result; - } -- else -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) - { -- /* cast away constness */ -- real_abstime_pt = (struct timespec *)abstime; -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -+ return result; - } -- -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt)); -- return result; -+ return ft_sem_wait_until(sem, abstime); - } - #endif - -@@ -1949,6 +1974,23 @@ - * Faked timer_settime() - * Does not affect timer speed when stepping clock with each time() call. - */ -+#ifdef __linux__ -+/* agentenv: the clock each timerfd was made on (ft_tfd_clock); a record still naming the number is stale. */ -+int timerfd_create(int clockid, int flags) -+{ -+ int fd; -+ ftpl_init(); -+ if (real_timerfd_create == NULL) return -1; -+ fd = real_timerfd_create(clockid, flags); -+ if (fd >= 0 && fd < FT_TFD_CLOCKS && ft_fd_mine()) -+ { -+ ft_fd_gone(fd); -+ ft_tfd_clock[fd] = clockid + 1; -+ } -+ return fd; -+} -+#endif -+ - static int - timer_settime_common(timer_t_or_int timerid, int flags, - const struct itimerspec *new_value, -@@ -1977,18 +2019,11 @@ - { - if (flags & abstime_flag) - { -- struct timespec tdiff, timeadj; -- timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj); -- if (user_rate_set) -- { -- timespecmul(&timeadj, 1.0/user_rate, &tdiff); -- } -+ if (ft_disabled || (compat == FT_FD && ft_tfd_known(timerid.int_member) -+ && !ft_rt_clock(ft_tfd_clock[timerid.int_member] - 1))) -+ new_real.it_value = new_value->it_value; - else -- { -- tdiff = timeadj; -- } -- /* only CLOCK_REALTIME is handled */ -- timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value); -+ ft_deadline_from_fake(CLOCK_REALTIME, &new_value->it_value, &new_real.it_value); - } - else - { -@@ -2061,47 +2096,6 @@ - return result; - } - --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.2 -- */ --int timer_settime_22(int timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.int_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME)); -- } --} -- --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_settime_233(timer_t timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME)); -- } --} - - /* - * Faked timer_gettime() -@@ -2151,48 +2145,6 @@ - return result; - } - --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.2 -- */ --int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_2)); -- } --} -- --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_3_3)); -- } --} -- --__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2"); --__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3"); --__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2"); --__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3"); - - #ifdef __linux__ - /* -@@ -2203,6 +2155,14 @@ - struct itimerspec *old_value) - { - ftpl_init(); -+ if (real_timerfd_settime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_uw_active() ? ft_u_timerfd_settime(fd, flags, new_value, old_value, &handled) : 0; -+ if (handled) return r; -+ if (!(flags & TFD_TIMER_ABSTIME) || fd < 0 || fd >= FT_TFD_CLOCKS || !ft_rt_clock(ft_tfd_clock[fd] - 1)) -+ return (*real_timerfd_settime)(fd, flags, new_value, old_value); -+ } - if (real_timerfd_settime == NULL) - { - return -1; -@@ -2222,6 +2182,13 @@ - int timerfd_gettime(int fd, struct itimerspec *curr_value) - { - ftpl_init(); -+ if (real_timerfd_gettime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_u_timerfd_gettime(fd, curr_value, &handled); -+ if (handled) return r; -+ return (*real_timerfd_gettime)(fd, curr_value); -+ } - if (real_timerfd_gettime == NULL) - { - return -1; -@@ -2379,6 +2346,8 @@ - #endif - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -2495,12 +2464,25 @@ - return; - } - -+ /* agentenv: a specification without "x" is at rate 1, not at the rate of the one before it */ -+ double prev_rate = user_rate; -+ bool prev_rate_set = user_rate_set; -+ if (user_faked_time[0] == 0) return; -+ user_rate = 1.0; -+ user_rate_set = false; -+ - /* check whether the user gave us an absolute time to fake */ - switch (user_faked_time[0]) - { - - default: /* Try and interpret this as a specified time */ - if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(user_faked_time, &user_faked_time_timespec) == NULL) goto parse_failed; -+ user_faked_time_set = true; -+ goto parse_modifiers; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); - -@@ -2518,9 +2500,7 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n" -- "Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - goto parse_modifiers; - break; -@@ -2548,6 +2528,11 @@ - /* Contributed by David North, TDI in version 0.7 */ - case '@': /* Specific time, but clock along relative to that starttime */ - ft_mode = FT_START_AT; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(&user_faked_time[1], &user_faked_time_timespec) == NULL) goto parse_failed; -+ goto parse_anchor; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm); - if (NULL != nstime_str) -@@ -2563,12 +2548,17 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - -- /* Reset starttime */ -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+parse_anchor: -+ /* agentenv: "^" is the real time at which the clock read the time given, so every process reads the -+ same time whenever it started, with no shared memory; without it, the process's own start, as before. */ -+ if (strchr(user_faked_time, '^') != NULL) -+ { -+ if (ft_parse_epoch(strchr(user_faked_time, '^') + 1, &ftpl_starttime.real) == NULL) goto parse_failed; -+ } -+ else if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2577,17 +2567,17 @@ - ft_mode = FT_START_AT; - struct stat master_file_stats; - int ret; -- if (NULL == getenv("FAKETIME_FOLLOW_FILE")) -+ if (NULL == ft_getenv("FAKETIME_FOLLOW_FILE")) - { - fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n"); - exit(1); - } - else - { -- DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); -+ DONT_FAKE_TIME(ret = stat(ft_getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); - if (ret == -1) - { -- fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE")); -+ fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", ft_getenv("FAKETIME_FOLLOW_FILE")); - exit(1); - } - else -@@ -2596,7 +2586,7 @@ - user_faked_time_timespec.tv_nsec = 0; - } - } -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+ if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2609,7 +2599,15 @@ - { - user_rate = atof(strchr(user_faked_time, 'x')+1); - user_rate_set = true; -- if (NULL != getenv("FAKETIME_XRESET")) { -+ if (user_rate <= 0.0) -+ { -+ /* agentenv: rate 0 is the time given, frozen; sleeps and waits then take the real time they ask for */ -+ ft_mode = FT_FREEZE; -+ user_faked_time_set = true; -+ user_rate = 1.0; -+ user_rate_set = false; -+ } -+ if (NULL != ft_getenv("FAKETIME_XRESET")) { - if (ftpl_timecache.real.tv_nsec >= 0) { - user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec; - user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec; -@@ -2639,9 +2637,15 @@ - - strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1); - user_faked_time_saved[BUFFERLEN-1] = 0; -+ ft_spec_gen++; - #ifdef DEBUG - fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved); - #endif -+ return; -+ -+parse_failed: -+ user_rate = prev_rate; -+ user_rate_set = prev_rate_set; - } - - -@@ -2666,8 +2670,8 @@ - const char *progname = __progname; - #endif - -- char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS"); -- bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM"); -+ char *ignore_list = ft_getenv("FAKETIME_IGNORE_SYMBOLS"); -+ bool should_debug_dlsym = ft_getenv("FAKETIME_DEBUG_DLSYM"); - #define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym) - #define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym) - -@@ -2749,32 +2753,25 @@ - #endif - - #ifdef FAKE_PTHREAD -- - #ifdef __GLIBC__ - real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5"); -- - real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2"); -- real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2"); -- real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2"); -+ real_pthread_cond_clockwait = dlvsym(RTLD_NEXT, "pthread_cond_clockwait", "GLIBC_2.30"); -+ real_pthread_mutex_clocklock = dlvsym(RTLD_NEXT, "pthread_mutex_clocklock", "GLIBC_2.30"); - #endif -- - if (NULL == real_pthread_cond_timedwait_232) - { -- real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); -+ real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); - } -- if (NULL == real_pthread_cond_init_232) -+ if (NULL == real_pthread_cond_clockwait) - { -- real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init"); -+ real_pthread_cond_clockwait = dlsym(RTLD_NEXT, "pthread_cond_clockwait"); - } -- if (NULL == real_pthread_cond_destroy_232) -+ if (NULL == real_pthread_mutex_clocklock) - { -- real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy"); -- } -- -- if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) { -- fprintf(stderr,"monotonic_conds_lock init failed\n"); -- exit(-1); -+ real_pthread_mutex_clocklock = dlsym(RTLD_NEXT, "pthread_mutex_clocklock"); - } -+ real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); - #endif - #ifdef __APPLE__ - real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); -@@ -2814,6 +2811,7 @@ - #endif - #ifdef __linux__ - real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime"); -+ real_timerfd_create = dlsym(RTLD_NEXT, "timerfd_create"); - real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime"); - #endif - #endif -@@ -2826,16 +2824,42 @@ - #undef dlsym - #undef dlvsym - -+ /* agentenv: a process of another user than the session's, or one started with AGENTENV_REAL_TIME=1, stays on real -+ time; the marker is taken out of its environment, so what it starts is faked. */ -+ { -+ const char *uid = ft_getenv("AGENTENV_SESSION_UID"); -+ const char *marker = getenv("AGENTENV_REAL_TIME"); -+ if (uid != NULL && *uid != 0 && (uid_t) strtoul(uid, NULL, 10) != getuid()) -+ ft_disabled = true; -+ if (marker != NULL && strcmp(marker, "1") == 0) -+ { -+ ft_disabled = true; -+ ft_scrub_marker(); -+ } -+ if ((tmp_env = ft_getenv("AGENTENV_WAIT_SLICE_MS")) != NULL && atol(tmp_env) > 0) -+ ft_wait_slice_ns = atol(tmp_env) * 1000000L; -+ } -+ if (ft_disabled) -+ { -+ ft_mode = FT_NOOP; -+ dont_fake = dont_fake_final; -+ return; -+ } -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ pthread_atfork(ft_atfork_prepare, ft_atfork_parent, ft_atfork_child); -+#endif -+ ft_page_open(); -+ - #ifdef FAKE_STATELESS - if (0) ft_shm_init(); - #else -- tmp_env = getenv("FAKETIME_DISABLE_SHM"); -+ tmp_env = ft_getenv("FAKETIME_DISABLE_SHM"); - if (!tmp_env || tmp_env[0] == '0') { - ft_shm_init(); - } - #endif - #ifdef FAKE_STAT -- if (getenv("NO_FAKE_STAT")!=NULL) -+ if (ft_getenv("NO_FAKE_STAT")!=NULL) - { - fake_stat_disabled = 1; //Note that this is NOT re-checked - } -@@ -2843,7 +2867,7 @@ - #if defined FAKE_FILE_TIMESTAMPS - #ifdef FAKE_UTIME - // fake_utime_disabled is 0 by default -- if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked -+ if ((tmp_env = ft_getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked - { - if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T') - { /* an empty string or a yes/true value turns off disabling */ -@@ -2860,11 +2884,11 @@ - #endif - #endif - -- if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_CACHE_DURATION")) != NULL) - { - cache_duration = atoi(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_NO_CACHE")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -2875,7 +2899,7 @@ - /* Check whether we actually should be faking the returned timestamp. */ - - /* We can prevent faking time for specified commands */ -- if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SKIP_CMDS")) != NULL) - { - char *skip_cmd, *saveptr, *tmpvar; - /* Don't mess with the env variable directly. */ -@@ -2904,12 +2928,12 @@ - } - - /* We can limit faking time to specified commands */ -- if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_ONLY_CMDS")) != NULL) - { - char *only_cmd, *saveptr, *tmpvar; - bool cmd_matched = false; - -- if (getenv("FAKETIME_SKIP_CMDS") != NULL) -+ if (ft_getenv("FAKETIME_SKIP_CMDS") != NULL) - { - fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n"); - exit(EXIT_FAILURE); -@@ -2941,44 +2965,44 @@ - } - } - -- if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) - { - ft_start_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) - { - ft_stop_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) - { - ft_start_after_ncalls = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) - { - ft_stop_after_ncalls = atol(tmp_env); - limited_faking = true; - } - - /* check whether we should spawn an external command */ -- if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_TARGET")) != NULL) - { - spawnsupport = true; -- (void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); -+ (void) strncpy(ft_spawn_target, ft_getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); - ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0; -- if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_SECONDS")) != NULL) - { - ft_spawn_secs = atol(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) - { - ft_spawn_ncalls = atol(tmp_env); - } - } - -- if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SAVE_FILE")) != NULL) - { - if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT, - S_IWUSR | S_IRUSR))) -@@ -2989,7 +3013,7 @@ - } - - /* load file only if reading timestamps from it is not finished yet */ -- if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_LOAD_FILE")) != NULL) - { - int infile = -1; - struct stat sb; -@@ -3021,7 +3045,7 @@ - infile_set = true; - } - -- tmp_env = getenv("FAKETIME_FMT"); -+ tmp_env = ft_getenv("FAKETIME_FMT"); - if (tmp_env == NULL) - { - strcpy(user_faked_time_fmt, "%Y-%m-%d %T"); -@@ -3061,7 +3085,7 @@ - system_time_from_system(&ftpl_starttime); - } - /* fake time supplied as environment variable? */ -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - parse_config_file = false; - parse_ft_string(tmp_env); -@@ -3070,6 +3094,11 @@ - { - read_config_file(); - } -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } - - dont_fake = dont_fake_final; - } -@@ -3209,8 +3238,8 @@ - * a system-wide /etc/faketimerc present. - * The /etc/faketimerc handling has been contributed by David Burley, - * Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */ -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -- (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", ft_getenv("HOME")); - if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 || - (faketimerc = open(filename, O_RDONLY)) != -1 || - (faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1) -@@ -3248,6 +3277,48 @@ - tp_save.tv_nsec = tp->tv_nsec; - - if (dont_fake) return 0; -+ if (ft_disabled) return 0; -+ if (ft_page != NULL && (clk_id == CLOCK_REALTIME || clk_id == CLOCK_REALTIME_COARSE)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap_at(clk_id, &s, tp)) -+ { -+ if (s.generation != __atomic_load_n(&ft_page_applied, __ATOMIC_ACQUIRE)) -+ { -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ sigset_t all, old; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &old); -+ pthread_mutex_lock(&time_mutex); -+ ft_page_apply_locked(&s); -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &old, NULL); -+#else -+ ft_page_apply_locked(&s); -+#endif -+ } -+ ft_tl_gen = s.generation; -+ ft_tl_scale = (s.armed && s.rate > 0) ? 1.0 / s.rate : 1.0; -+ if (s.armed) -+ { -+ int64_t now = (int64_t) tp->tv_sec * 1000000000LL + tp->tv_nsec, fake; -+ if (s.rate == 0 || now <= s.anchor_ns) -+ fake = s.universe_ns; -+ else if (s.rate == 1.0) -+ fake = s.universe_ns + (now - s.anchor_ns); -+ else -+ fake = s.universe_ns + (int64_t) ((double) (now - s.anchor_ns) * s.rate); -+ tp->tv_sec = fake / 1000000000LL; -+ tp->tv_nsec = fake % 1000000000LL; -+ if (tp->tv_nsec < 0) -+ { -+ tp->tv_sec -= 1; -+ tp->tv_nsec += 1000000000LL; -+ } -+ } -+ return 0; -+ } -+ } - /* Per process timers are only sped up or slowed down */ - if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID)) - { -@@ -3262,7 +3333,6 @@ - volatile int ret = INT_MAX; - - #ifdef PTHREAD_SINGLETHREADED_TIME -- static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; - - // block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation. - sigset_t all_signals, original_mask; -@@ -3367,6 +3437,19 @@ - force_cache_expiration = 0; - } - -+ /* agentenv: with the clock page mapped, the file isn't read; until it is, look for it once a second */ -+ if (ft_page == NULL && ft_getenv("AGENTENV_CLOCK_PAGE") != NULL && current_ts.tv_sec != ft_page_last_try) -+ { -+ ft_page_last_try = current_ts.tv_sec; -+ ft_page_open(); -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } -+ } -+ if (ft_page != NULL) cache_expired = 0; -+ - if (cache_expired == 1) - { - static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */ -@@ -3377,7 +3460,7 @@ - fprintf(stderr, "***************++ Cache expired ++**************\n"); - */ - -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - strncpy(user_faked_time, tmp_env, BUFFERLEN - 1); - user_faked_time[BUFFERLEN - 1] = 0; -@@ -3401,6 +3484,9 @@ - get_fake_monotonic_setting(&fake_monotonic_clock); - } /* cache had expired */ - -+ ft_tl_gen = ft_spec_gen; -+ ft_tl_scale = (ft_mode == FT_START_AT && user_rate_set && user_rate > 0) ? 1.0 / user_rate : 1.0; -+ - if (infile_set) - { - if (load_time(tp)) -@@ -3520,6 +3606,7 @@ - { - struct timespec ts; - int ret; -+ if (ft_disabled) return 0; - ts.tv_sec = tv->tv_sec; - ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000; - -@@ -3627,6 +3714,8 @@ - DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp)); - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -3723,238 +3812,124 @@ - - #ifdef FAKE_PTHREAD - --typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread; -- --struct pthread_cond_monotonic { -- pthread_cond_t *ptr; -- UT_hash_handle hh; --}; -- --static struct pthread_cond_monotonic *monotonic_conds = NULL; -+/* -+ * agentenv: a wait with a CLOCK_REALTIME deadline waits on CLOCK_MONOTONIC instead, until ft_deadline_from_fake's -+ * deadline. A condvar's clock is read from glibc's own condvar word (bit 1 of __wrefs, which glibc's waits read, since -+ * glibc 2.25) instead of a table kept by intercepting pthread_cond_init and pthread_cond_destroy; nothing here calls -+ * getenv() or malloc() or takes a lock. A condvar wait isn't cut into slices, since a slice ending would be a wakeup -+ * its caller must recheck, so a jump or a change of rate reaches it only when it ends; a mutex wait is cut. -+ */ -+#define FT_COND_CLOCK_MONOTONIC 2 - --int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) -+int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- clockid_t clock_id; -- int result; -- -+ struct timespec mono; - ftpl_init(); -- if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1; -- result = real_pthread_cond_init_232(cond, attr); -- -- if (result != 0 || attr == NULL) -- return result; -- -- pthread_condattr_getclock(attr, &clock_id); -- -- if (clock_id == CLOCK_MONOTONIC) { -- struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic)); -- e->ptr = cond; -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_ADD_PTR(monotonic_conds, ptr, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL -+ && !(__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- -- return result; -+ if (!ft_translates_wait(abstime) || real_pthread_cond_clockwait == NULL -+ || (__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ return real_pthread_cond_timedwait_232(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_destroy_232(pthread_cond_t *cond) -+int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- struct pthread_cond_monotonic* e; -- -+ struct timespec real; - ftpl_init(); -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- if (e) { -- HASH_DEL(monotonic_conds, e); -- free(e); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- pthread_rwlock_unlock(&monotonic_conds_lock); -- -- return real_pthread_cond_destroy_232(cond); -+ if (!ft_translates_wait(abstime)) -+ return real_pthread_cond_timedwait_225(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_REALTIME, abstime, &real); -+ return real_pthread_cond_timedwait_225(cond, mutex, &real); - } - --/* -- * Check whether we need a run-time activation of the -- * forced monotonic fix to avoid faked / unfaked timestamp -- * confusion between the application and glibc internals. -- */ --bool needs_forced_monotonic_fix(char *function_name) --{ -- bool result = false; -- char *env_var; --#ifdef __GLIBC__ -- const char *glibc_version_string = gnu_get_libc_version(); -+__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); -+#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); -+#else -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); - #endif - -- if (function_name == NULL) return false; -- -- /* The forced monotonic fix can be activated by setting an -- * environment variable to 1, or disabled by setting it to 0 */ -- if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL) -+int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, -+ const struct timespec *abstime) -+{ -+ struct timespec mono; -+ ftpl_init(); -+ if (real_pthread_cond_clockwait == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- if (env_var[0] == '0') -- result = false; -- else -- result = true; -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- else -- { --#ifdef __GLIBC__ -- /* Here we try to derive the necessity for a forced monotonic fix * -- * based on glibc version. What could possibly go wrong? */ -- -- int glibc_major, glibc_minor; -- sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor); -- -- /* The following decision logic is yet purely based on experiences -- * with pthread_cond_timedwait(). The used boundaries may still be -- * imprecise. */ -- if ( (glibc_major == 2) && -- ((glibc_minor <= 24) || (glibc_minor >= 30)) ) -- { -- result = true; -- } -- else --#endif -- result = false; // avoid forced monotonic fixes unless really necessary -- } -- -- if (getenv("FAKETIME_DEBUG") != NULL) --#ifdef __GLIBC__ -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n", -- function_name, result ? "yes":"no", glibc_version_string); --#else -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n", -- function_name, result ? "yes":"no"); --#endif -- -- return result; -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_cond_clockwait(cond, mutex, clockid, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat) -+static int ft_mutex_lock_until(pthread_mutex_t *mutex, const struct timespec *fake_abs) - { -- struct timespec tp, tdiff_actual, realtime, faketime; -- struct timespec *tf = NULL; -- struct pthread_cond_monotonic* e; -- char *tmp_env; -- int wait_ms; -- clockid_t clk_id; -- int result = 0; -- -- ftpl_init(); -- -- if (abstime != NULL) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) - { -- if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire read monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -- if (e != NULL) -- clk_id = CLOCK_MONOTONIC; -- else -- clk_id = CLOCK_REALTIME; -- -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- faketime = realtime; -- (void)fake_clock_gettime(clk_id, &faketime); -- -- if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL) -- { -- wait_ms = atol(tmp_env); -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- -- tdiff_actual.tv_sec = wait_ms / 1000; -- tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000; -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -+ last = ft_slice_end(&mono, &slice); -+ result = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &slice); -+ if (result == EINVAL && real_pthread_mutex_timedlock != NULL) -+ { /* a PI mutex on a kernel without FUTEX_LOCK_PI2 takes only CLOCK_REALTIME */ -+ struct timespec real; -+ ft_deadline_from_fake(CLOCK_REALTIME, fake_abs, &real); -+ return real_pthread_mutex_timedlock(mutex, &real); - } -- else -- { -- timespecsub(abstime, &faketime, &tp); -- if (user_rate_set) -- { -- timespecmul(&tp, 1.0 / user_rate, &tdiff_actual); -- } -- else -- { -- tdiff_actual = tp; -- } -- } -- -- /* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime -- internally to calculate the appropriate duration for the -- waiting time. This already uses the faked functions, hence, the -- fake time needs to be passed to pthread_cond_timedwait for -- CLOCK_MONOTONIC. */ --#ifndef __ARM_ARCH --#ifndef FORCE_MONOTONIC_FIX -- if (clk_id == CLOCK_MONOTONIC) { -- if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) { -- timespecadd(&realtime, &tdiff_actual, &tp); -- } -- else { -- timespecadd(&faketime, &tdiff_actual, &tp); -- } -- } -- else --#endif --#endif -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -- } -- -- switch (compat) { -- case FT_COMPAT_GLIBC_2_3_2: -- result = real_pthread_cond_timedwait_232(cond, mutex, tf); -- break; -- case FT_COMPAT_GLIBC_2_2_5: -- result = real_pthread_cond_timedwait_225(cond, mutex, tf); -- break; -+ if (result != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) return ETIMEDOUT; - } -- return result; - } - --int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5); -+ ftpl_init(); -+ if (real_pthread_mutex_timedlock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_mutex_clocklock != NULL) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (!ft_translates_wait(abstime) || real_pthread_mutex_clocklock == NULL) -+ return real_pthread_mutex_timedlock(mutex, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2); -+ ftpl_init(); -+ if (real_pthread_mutex_clocklock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_mutex_clocklock(mutex, clockid, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); --#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@"); --#else --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2"); --#endif -- - #endif - - /* -@@ -4005,15 +3980,15 @@ - /* If FAKETIME_TIMESTAMP_FILE was given in environment, - * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it. - * This allows other process instances to share the same time. */ -- if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -- && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -- && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -- && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) -+ if ( (ft_getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -+ && (*ft_getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -+ && (ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -+ && (strcmp(ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) - { - const char *error = NULL; - FILE *envfile; - static char custom_filename[BUFSIZ]; -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); - - if ((envfile = fopen(custom_filename, "wt")) != NULL) - { -@@ -4034,7 +4009,7 @@ - { - fprintf(stderr, "libfaketime: In clock_settime(), failed to " - "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n", -- error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); -+ error, ft_getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); - } - } - -@@ -4125,7 +4100,7 @@ - - /* return 0 if no FAKERANDOM_SEED was seen */ - static int bypass_randomness(void* buf, size_t buflen) { -- char *seedstring = getenv("FAKERANDOM_SEED"); -+ char *seedstring = ft_getenv("FAKERANDOM_SEED"); - char *b = buf; - - if (seedstring != NULL) { -@@ -4169,7 +4144,7 @@ - #else - pid_t getpid() { - #endif -- const char *pidstring = getenv("FAKETIME_FAKEPID"); -+ const char *pidstring = ft_getenv("FAKETIME_FAKEPID"); - if (pidstring != NULL) { - long int pid = strtol(pidstring, NULL, 0); - return (pid_t)(pid); -@@ -4270,7 +4245,7 @@ - va_list ap; - va_start(ap, number); - #ifdef FAKE_RANDOM -- if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) { -+ if (number == __NR_getrandom && ft_getenv("FAKERANDOM_SEED")) { - void *buf; - size_t buflen; - unsigned int flags; -@@ -4282,7 +4257,7 @@ - } - #endif - // static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp); -- if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) { -+ if (number == __NR_clock_gettime && (ft_getenv("FAKETIME") || ft_getenv("FAKETIME_TIMESTAMP_FILE"))) { - clockid_t clk_id; - struct timespec *tp; - clk_id = va_arg(ap, clockid_t); -diff -ruN a/src/libfaketime.map b/src/libfaketime.map ---- a/src/libfaketime.map -+++ b/src/libfaketime.map -@@ -1,21 +1,9 @@ --GLIBC_2.2 { -- global: -- -- timer_gettime; timer_settime; -- local: timer_settime_*; timer_gettime_*; --}; -- --GLIBC_2.3.3 { -- # Changed timer_t. -- timer_gettime; timer_settime; --} GLIBC_2.2; -- - GLIBC_2.2.5 { - global: pthread_cond_timedwait; - local: pthread_cond_timedwait_*; pthread_cond_init_*; pthread_cond_destroy*; - }; - - GLIBC_2.3.2 { -- pthread_cond_timedwait; pthread_cond_init; pthread_cond_destroy; -+ pthread_cond_timedwait; - } GLIBC_2.2.5; - diff --git a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c deleted file mode 100644 index 0a9a86688..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c +++ /dev/null @@ -1,115 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ -/* - * agent-env's TLS shim: OpenSSL checks a peer's certificates against real time, whatever the universe's clock says. - * - * Preloaded after the clock library (/etc/ld.so.preload). A program on universe time months from real time would find - * every live site's certificate not yet valid or expired. Most clients reach live sites through the computer's egress - * proxy, which hands them certificates valid at the universe date; this is for the ones that bypass it. OpenSSL's - * libssl verifies a chain with X509_verify_cert, which this defines first in the global scope: it sets the - * verification's check time to real time, read with a raw system call the clock library doesn't intercept, unless the - * caller set one, then calls OpenSSL's own, the one in the caller's own libcrypto, so a process holding two copies of - * OpenSSL (a bundled one beside the system's) verifies each with its own. - * - * Not covered: TLS stacks of their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries. - */ -#define _GNU_SOURCE -#include -#include -#include -#include - -typedef struct x509_store_ctx_st X509_STORE_CTX; -typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; - -/* OpenSSL's X509_V_FLAG_USE_CHECK_TIME (x509_vfy.h), the same in 1.1 and 3 */ -#define TLS_USE_CHECK_TIME 0x2 -#define TLS_SYSTEM_LIBCRYPTO "libcrypto.so.3" - -/* OpenSSL's functions as one copy of it has them */ -struct tls_openssl -{ - int (*verify)(X509_STORE_CTX *); - void (*set_time)(X509_STORE_CTX *, unsigned long, time_t); - X509_VERIFY_PARAM *(*get0_param)(X509_STORE_CTX *); - unsigned long (*get_flags)(const X509_VERIFY_PARAM *); -}; - -int X509_verify_cert(X509_STORE_CTX *ctx); - -/* the real time in seconds, read with the system call itself (the clock library intercepts libc's), 0 if it fails */ -static long tls_real_time(void) -{ - struct timespec ts = {0, 0}; -#if defined(__x86_64__) - long ret; - __asm__ volatile("syscall" : "=a"(ret) : "0"(228L), "D"((long) CLOCK_REALTIME), "S"(&ts) : "rcx", "r11", "memory"); - if (ret != 0) return 0; -#elif defined(__aarch64__) - register long x8 __asm__("x8") = 113; - register long x0 __asm__("x0") = CLOCK_REALTIME; - register long x1 __asm__("x1") = (long) &ts; - __asm__ volatile("svc 0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory"); - if (x0 != 0) return 0; -#else -#error the TLS shim reads real time with a raw system call on x86_64 or aarch64 -#endif - return ts.tv_sec; -} - -/* ``name`` in ``scope`` (a handle, or RTLD_NEXT), never this file's own definition */ -static void *tls_lookup(void *scope, const char *name) -{ - void *found = dlsym(scope, name); - return found == (void *) X509_verify_cert ? NULL : found; -} - -static void tls_take(void *scope, struct tls_openssl *out) -{ - out->verify = (int (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_verify_cert"); - out->set_time = (void (*)(X509_STORE_CTX *, unsigned long, time_t)) tls_lookup(scope, "X509_STORE_CTX_set_time"); - out->get0_param = (X509_VERIFY_PARAM * (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_STORE_CTX_get0_param"); - out->get_flags = (unsigned long (*)(const X509_VERIFY_PARAM *)) tls_lookup(scope, "X509_VERIFY_PARAM_get_flags"); -} - -/* OpenSSL as the object at ``caller`` sees it: from that object's own dependencies (libssl's libcrypto, whichever copy - it is); for a caller that isn't a shared library depending on one (a program calling it itself), the next definition - in the global scope; else the system's libcrypto where it was loaded privately (a Python extension's). Looked up on - every call, once a handshake: a copy of OpenSSL unloaded and another loaded at its address must not be handed the - first one's functions. */ -static void tls_resolve(void *caller, struct tls_openssl *out) -{ - Dl_info info; - void *handle; - memset(out, 0, sizeof *out); - if (dladdr(caller, &info)) - { - if (info.dli_fname != NULL && info.dli_fname[0] != 0 - && (handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - if (out->verify != NULL) return; - } - } - tls_take(RTLD_NEXT, out); - if (out->verify != NULL) return; - if ((handle = dlopen(TLS_SYSTEM_LIBCRYPTO, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - } -} - -int X509_verify_cert(X509_STORE_CTX *ctx) -{ - struct tls_openssl ssl; - tls_resolve(__builtin_return_address(0), &ssl); - if (ssl.verify == NULL) return -1; - if (ctx != NULL && ssl.set_time != NULL && ssl.get0_param != NULL && ssl.get_flags != NULL) - { - X509_VERIFY_PARAM *param = ssl.get0_param(ctx); - long now = tls_real_time(); - if (param != NULL && !(ssl.get_flags(param) & TLS_USE_CHECK_TIME) && now > 0) ssl.set_time(ctx, 0, (time_t) now); - } - return ssl.verify(ctx); -} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot.go b/src/agent_env/agents/agents/claude-code/egress/boot.go deleted file mode 100644 index 031555627..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/boot.go +++ /dev/null @@ -1,294 +0,0 @@ -package main - -import ( - "crypto/x509" - "encoding/pem" - "errors" - "fmt" - "io/fs" - "os" - "os/exec" - "os/user" - "path/filepath" - "sort" - "strconv" - "strings" - "time" -) - -// bootPaths are where boot reads and writes; tests point them into a folder of their own. -type bootPaths struct { - Folder, PublicCA, Anchor string - // Bundles are the PEM bundles the CA is appended to when they exist: agent-env's trust files, which - // SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS name. - Bundles []string - // UpdateTrust rebuilds the system store from its anchors, and its hooks Java's. - UpdateTrust []string - // CertifiRoots and JavaRoots are searched for certifi's cacert.pem and JDKs' lib/security/cacerts. - CertifiRoots, JavaRoots []string - EgressUser, SessionUser string - // NSS adds the CA to the session user's NSS database, which Chromium and Firefox read; nil leaves it. - NSS func(paths bootPaths) error -} - -// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in -// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. -func defaultBootPaths() bootPaths { - p := bootPaths{ - Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", - Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, - UpdateTrust: []string{"update-ca-certificates"}, - CertifiRoots: []string{"/usr/lib/python3", "/usr/local/lib", "/opt"}, - JavaRoots: []string{"/usr/lib/jvm", "/opt"}, - EgressUser: EgressUser, SessionUser: SessionUser, - NSS: sessionNSS, - } - if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { - p.SessionUser = name - } - if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { - p.NSS = nil - } - return p -} - -// boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and -// trusted in the system store, agent-env's trust files, every certifi bundle, every JDK's store and the session -// user's NSS database. A step that fails fails the boot, naming it, but for a certifi bundle or a JDK's store, which -// an app brings: those are named among the report's warnings. -func boot(p bootPaths) (map[string]any, error) { - start := time.Now() - report := map[string]any{} - egress, err := user.Lookup(p.EgressUser) - if err != nil { - return report, err - } - uid, _ := strconv.Atoi(egress.Uid) - gid, _ := strconv.Atoi(egress.Gid) - if err := os.MkdirAll(p.Folder, 0o700); err != nil { - return report, err - } - caPath, keyPath := filepath.Join(p.Folder, "ca.pem"), filepath.Join(p.Folder, "ca.key") - if _, err := os.Stat(caPath); errors.Is(err, fs.ErrNotExist) { - if err := writeCA(caPath, keyPath); err != nil { - return report, fmt.Errorf("minting the CA: %w", err) - } - report["minted"] = true - } - for _, path := range []string{p.Folder, caPath, keyPath} { - if err := os.Chown(path, uid, gid); err != nil { - return report, err - } - } - if err := os.Chmod(p.Folder, 0o700); err != nil { - return report, err - } - caPEM, err := os.ReadFile(caPath) - if err != nil { - return report, err - } - for _, path := range []string{p.PublicCA, p.Anchor} { - if err := writeRootFile(path, caPEM); err != nil { - return report, err - } - } - if len(p.UpdateTrust) > 0 { - if out, err := exec.Command(p.UpdateTrust[0], p.UpdateTrust[1:]...).CombinedOutput(); err != nil { - return report, fmt.Errorf("%s: %w: %s", strings.Join(p.UpdateTrust, " "), err, truncate(string(out), ErrorLimit)) - } - } - appended := map[string]bool{} - for _, path := range p.Bundles { - added, err := appendOnce(path, caPEM) - if errors.Is(err, fs.ErrNotExist) { - continue - } - if err != nil { - return report, fmt.Errorf("trusting the CA in %s: %w", path, err) - } - appended[path] = added - } - // An app's own stores, certifi's and JDKs': one that can't take the CA is named, and the computer boots, its - // app then refusing intercepted sites as it would on a store of its own. - var warnings []string - for _, path := range certifiBundles(p.CertifiRoots) { - added, err := appendOnce(path, caPEM) - if err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", path, err)) - continue - } - appended[path] = added - } - report["bundles"] = appended - java := map[string]string{} - for _, store := range javaStores(p.JavaRoots) { - if err := trustJava(store, p.PublicCA); err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", store, err)) - continue - } - java[store] = "trusted" - } - report["java"] = java - if len(warnings) > 0 { - report["warnings"] = warnings - } - if p.NSS != nil { - if err := p.NSS(p); err != nil { - return report, fmt.Errorf("the session's NSS database: %w", err) - } - report["nss"] = "trusted" - } - report["seconds"] = time.Since(start).Seconds() - return report, nil -} - -func writeCA(caPath, keyPath string) error { - ca, err := mintCA(CAName) - if err != nil { - return err - } - key, err := x509.MarshalPKCS8PrivateKey(ca.key) - if err != nil { - return err - } - if err := writeExclusive(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}), 0o600); err != nil { - return err - } - return writeExclusive(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.cert.Raw}), 0o644) -} - -func writeExclusive(path string, data []byte, mode os.FileMode) error { - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) - if err != nil { - return err - } - if _, err := f.Write(data); err != nil { - f.Close() - return err - } - return f.Close() -} - -// writeRootFile writes path whole, 0644, replacing it in one rename. -func writeRootFile(path string, data []byte) error { - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - return err - } - part := path + ".part" - if err := os.WriteFile(part, data, 0o644); err != nil { - return err - } - if err := os.Chmod(part, 0o644); err != nil { - return err - } - return os.Rename(part, path) -} - -// appendOnce appends pem to the bundle at path (through a symlink) unless it's there; whether it did. -func appendOnce(path string, pemData []byte) (bool, error) { - real, err := filepath.EvalSymlinks(path) - if err != nil { - return false, err - } - text, err := os.ReadFile(real) - if err != nil { - return false, err - } - block := strings.TrimSpace(string(pemData)) - if strings.Contains(string(text), block) { - return false, nil - } - f, err := os.OpenFile(real, os.O_WRONLY|os.O_APPEND, 0) - if err != nil { - return false, err - } - defer f.Close() - sep := "" - if len(text) > 0 && !strings.HasSuffix(string(text), "\n") { - sep = "\n" - } - _, err = f.WriteString(sep + block + "\n") - return err == nil, err -} - -func certifiBundles(roots []string) []string { - seen := map[string]bool{} - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacert.pem" || filepath.Base(filepath.Dir(path)) != "certifi" { - return nil - } - real, err := filepath.EvalSymlinks(path) - if err == nil && !seen[real] { - seen[real] = true - out = append(out, path) - } - return nil - }) - } - sort.Strings(out) - return out -} - -// javaStores are the JDKs' stores, lib/security/cacerts, under roots; the system store's hook builds Debian's. -func javaStores(roots []string) []string { - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacerts" { - return nil - } - if filepath.Base(filepath.Dir(path)) == "security" && filepath.Base(filepath.Dir(filepath.Dir(path))) == "lib" { - if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() { - out = append(out, path) - } - } - return nil - }) - } - sort.Strings(out) - return out -} - -// trustJava adds the CA to a JDK's store with that JDK's own keytool, else any on the PATH. -func trustJava(store, ca string) error { - keytool := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(store))), "bin", "keytool") - if _, err := os.Stat(keytool); err != nil { - if keytool, err = exec.LookPath("keytool"); err != nil { - return errors.New("there's no keytool to add the CA with") - } - } - opened := []string{"-keystore", store, "-storepass", "changeit"} - exec.Command(keytool, append([]string{"-delete", "-alias", "agentenv-egress"}, opened...)...).Run() - out, err := exec.Command(keytool, append([]string{"-importcert", "-noprompt", "-trustcacerts", "-alias", - "agentenv-egress", "-file", ca}, opened...)...).CombinedOutput() - if err != nil { - return fmt.Errorf("keytool: %w: %s", err, truncate(string(out), ErrorLimit)) - } - return nil -} - -// sessionNSS adds the CA to the session user's NSS database, as that user, making the database if there's none. -func sessionNSS(p bootPaths) error { - session, err := user.Lookup(p.SessionUser) - if err != nil { - return err - } - db := filepath.Join(session.HomeDir, ".pki", "nssdb") - as := []string{"setpriv", "--reuid=" + p.SessionUser, "--regid=" + p.SessionUser, "--init-groups", "env", - "HOME=" + session.HomeDir} - steps := [][]string{{"mkdir", "-p", db}} - if _, err := os.Stat(filepath.Join(db, "cert9.db")); err != nil { - steps = append(steps, []string{"certutil", "-N", "-d", "sql:" + db, "--empty-password"}) - } - steps = append(steps, []string{"certutil", "-A", "-d", "sql:" + db, "-n", NSSNickname, "-t", "C,,", "-i", - p.PublicCA}) - for _, step := range steps { - argv := append(append([]string{}, as...), step...) - if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil { - return fmt.Errorf("%s: %w: %s", strings.Join(step[:2], " "), err, truncate(string(out), ErrorLimit)) - } - } - return nil -} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot_test.go b/src/agent_env/agents/agents/claude-code/egress/boot_test.go deleted file mode 100644 index c5f59ad0a..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/boot_test.go +++ /dev/null @@ -1,99 +0,0 @@ -package main - -import ( - "os" - "os/user" - "path/filepath" - "strings" - "testing" -) - -func TestBootMintsTheCAOnceItsKeyPrivateAndTrustsItInEveryBundleOnce(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - bundle := filepath.Join(dir, "ca-bundle.pem") - os.WriteFile(bundle, []byte("-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----"), 0o644) - certifi := filepath.Join(dir, "opt", "venv", "lib", "certifi", "cacert.pem") - os.MkdirAll(filepath.Dir(certifi), 0o755) - os.WriteFile(certifi, []byte(""), 0o644) - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - Bundles: []string{bundle, filepath.Join(dir, "missing.pem")}, - CertifiRoots: []string{filepath.Join(dir, "opt")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatal(err, report) - } - if report["minted"] != true { - t.Fatalf("%v", report) - } - info, _ := os.Stat(filepath.Join(p.Folder, "ca.key")) - if info.Mode().Perm() != 0o600 { - t.Fatalf("the CA key is %v", info.Mode().Perm()) - } - if info, _ := os.Stat(p.Folder); info.Mode().Perm() != 0o700 { - t.Fatalf("the folder is %v", info.Mode().Perm()) - } - ca, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - public, _ := os.ReadFile(p.PublicCA) - if string(ca) != string(public) { - t.Fatal("the public CA isn't the CA") - } - if _, err := boot(p); err != nil { - t.Fatal(err) - } - again, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - if string(again) != string(ca) { - t.Fatal("a second boot minted another CA") - } - for _, path := range []string{bundle, certifi} { - text, _ := os.ReadFile(path) - if n := strings.Count(string(text), strings.TrimSpace(string(ca))); n != 1 { - t.Fatalf("%s has the CA %d times", path, n) - } - } - leaves, err := LoadLeaves(p.Folder) - if err != nil || leaves.ca.cert.NotBefore != CAFrom || leaves.ca.cert.NotAfter != CAUntil { - t.Fatalf("%v", err) - } -} - -func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - broken := filepath.Join(dir, "opt", "venv", "lib", "certifi") - os.MkdirAll(filepath.Join(broken, "elsewhere"), 0o755) - os.Symlink(filepath.Join(broken, "elsewhere"), filepath.Join(broken, "cacert.pem")) // a folder, not a bundle - jdk := filepath.Join(dir, "jdk", "lib", "security") - os.MkdirAll(jdk, 0o755) - os.WriteFile(filepath.Join(jdk, "cacerts"), []byte("not a keystore"), 0o644) - t.Setenv("PATH", dir) // and no keytool to add the CA with - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - CertifiRoots: []string{filepath.Join(dir, "opt")}, JavaRoots: []string{filepath.Join(dir, "jdk")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatalf("the boot failed: %v", err) - } - warnings, _ := report["warnings"].([]string) - if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, " "), "keytool") { - t.Fatalf("%v", report) - } -} - -func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { - if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { - t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) - } - t.Setenv("AGENTENV_SESSION_USER", "agent") - t.Setenv("AGENTENV_EGRESS_NSS", "off") - if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { - t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock.go b/src/agent_env/agents/agents/claude-code/egress/clock.go deleted file mode 100644 index 30cb880c1..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/clock.go +++ /dev/null @@ -1,151 +0,0 @@ -package main - -import ( - "encoding/binary" - "math" - "os" - "sync" - "syscall" - "time" -) - -// The clock page the follower writes (clockpage.py, version 2): magic version seq universe_ns anchor_ns rate armed -// reserved generation check mono_universe_ns mono_anchor_ns, little-endian, 80 bytes, seq odd while it's written. -const ( - pageSize = 80 - pageMagic = 0x4B434541 - checkSalt = 0x9E3779B97F4A7C15 -) - -// state is the universe as the session reads it: armed, the universe at the anchor (seconds), the anchor (real -// seconds) and the rate. -type state struct { - Armed bool - V float64 - Anchor float64 - Rate float64 -} - -var unarmed = state{Rate: 1} - -func (s state) at(t float64) float64 { - if !s.Armed { - return t - } - if s.Rate == 0 || t <= s.Anchor { - return s.V - } - return s.V + (t-s.Anchor)*s.Rate -} - -func pageCheck(seq uint64, u, a int64, rate float64, armed uint32, generation uint64, mu, ma int64) uint64 { - return seq ^ uint64(u) ^ uint64(a) ^ math.Float64bits(rate) ^ uint64(armed) ^ generation ^ uint64(mu) ^ - uint64(ma) ^ checkSalt -} - -// readPage reads a consistent state from the page's bytes, or false when it's being written, torn or not a page. -func readPage(b []byte) (state, bool) { - if len(b) < pageSize || binary.LittleEndian.Uint32(b[0:]) != pageMagic { - return state{}, false - } - seq := binary.LittleEndian.Uint64(b[8:]) - if seq&1 == 1 { - return state{}, false - } - u := int64(binary.LittleEndian.Uint64(b[16:])) - a := int64(binary.LittleEndian.Uint64(b[24:])) - rate := math.Float64frombits(binary.LittleEndian.Uint64(b[32:])) - armed := binary.LittleEndian.Uint32(b[40:]) - generation := binary.LittleEndian.Uint64(b[48:]) - check := binary.LittleEndian.Uint64(b[56:]) - mu := int64(binary.LittleEndian.Uint64(b[64:])) - ma := int64(binary.LittleEndian.Uint64(b[72:])) - if check != pageCheck(seq, u, a, rate, armed, generation, mu, ma) || binary.LittleEndian.Uint64(b[8:]) != seq { - return state{}, false - } - if armed == 0 { - return unarmed, true - } - return state{Armed: true, V: float64(u) / 1e9, Anchor: float64(a) / 1e9, Rate: rate}, true -} - -// Universe follows the clock page, read every PagePoll, so a change is seen and the universe it left is held for Hold. -type Universe struct { - path string - mu sync.Mutex - page []byte - current state - known bool - previous state - leftAt time.Time -} - -func NewUniverse(path string) *Universe { - u := &Universe{path: path, current: unarmed} - u.refresh(time.Now()) - return u -} - -func (u *Universe) Poll() { - for { - time.Sleep(PagePoll) - u.refresh(time.Now()) - } -} - -func (u *Universe) read() state { - if u.page == nil { - f, err := os.Open(u.path) - if err != nil { - return unarmed - } - mapped, err := syscall.Mmap(int(f.Fd()), 0, pageSize, syscall.PROT_READ, syscall.MAP_SHARED) - f.Close() - if err != nil { - return unarmed - } - u.page = mapped - } - for i := 0; i < PageReads; i++ { - if s, ok := readPage(u.page); ok { - return s - } - } - return u.current -} - -func (u *Universe) refresh(now time.Time) state { - u.mu.Lock() - defer u.mu.Unlock() - s := u.read() - if u.known && s != u.current { - u.previous, u.leftAt = u.current, now - } - u.current, u.known = s, true - return s -} - -// View is false when the session is on real time and was armed nowhere within Hold; else true, with the universes a -// client may check against: now, the universe at the anchor, and the one left within Hold when it was armed. -func (u *Universe) View(now time.Time) (bool, []float64) { - u.refresh(now) - u.mu.Lock() - defer u.mu.Unlock() - return view(u.current, u.previous, u.leftAt, now) -} - -func view(current, previous state, leftAt time.Time, now time.Time) (bool, []float64) { - held := !leftAt.IsZero() && now.Sub(leftAt) <= Hold && previous.Armed - if !current.Armed && !held { - return false, nil - } - t := float64(now.UnixNano()) / 1e9 - out := []float64{current.at(t)} - if current.Armed { - out = append(out, current.V) - } - if held { - out = append(out, previous.at(t)) - } - return true, out -} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock_test.go b/src/agent_env/agents/agents/claude-code/egress/clock_test.go deleted file mode 100644 index 22bb41503..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/clock_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package main - -import ( - "encoding/binary" - "encoding/hex" - "math" - "os" - "path/filepath" - "strings" - "testing" - "time" -) - -func page(seq uint64, universe, anchor time.Time, rate float64, armed bool) []byte { - b := make([]byte, pageSize) - u, a := universe.UnixNano(), anchor.UnixNano() - var on uint32 - if armed { - on = 1 - } - binary.LittleEndian.PutUint32(b[0:], pageMagic) - binary.LittleEndian.PutUint32(b[4:], 2) - binary.LittleEndian.PutUint64(b[8:], seq) - binary.LittleEndian.PutUint64(b[16:], uint64(u)) - binary.LittleEndian.PutUint64(b[24:], uint64(a)) - binary.LittleEndian.PutUint64(b[32:], math.Float64bits(rate)) - binary.LittleEndian.PutUint32(b[40:], on) - binary.LittleEndian.PutUint64(b[48:], 7) - binary.LittleEndian.PutUint64(b[56:], pageCheck(seq, u, a, rate, on, 7, a, a)) - binary.LittleEndian.PutUint64(b[64:], uint64(a)) - binary.LittleEndian.PutUint64(b[72:], uint64(a)) - return b -} - -func TestAPageReadsAsTheUniverseItHoldsAndATornOneNot(t *testing.T) { - universe, anchor := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Unix(time.Now().Unix(), 0) - s, ok := readPage(page(4, universe, anchor, 2, true)) - if !ok || !s.Armed || s.Rate != 2 || int64(s.V) != universe.Unix() { - t.Fatalf("read %+v %v", s, ok) - } - if got := s.at(float64(anchor.Unix()) + 10); int64(got) != universe.Unix()+20 { - t.Fatalf("10 s on at rate 2: %v", got) - } - if _, ok := readPage(page(5, universe, anchor, 2, true)); ok { - t.Fatal("a page being written (odd seq) read as whole") - } - torn := page(4, universe, anchor, 2, true) - torn[20] ^= 1 - if _, ok := readPage(torn); ok { - t.Fatal("a torn page read as whole") - } - if s, ok := readPage(page(2, time.Time{}, time.Time{}, 1, false)); !ok || s.Armed { - t.Fatalf("an unarmed page: %+v %v", s, ok) - } -} - -func TestTheUniverseFollowsThePageFileAndHoldsTheOneItLeft(t *testing.T) { - path := filepath.Join(t.TempDir(), "page") - if err := os.WriteFile(path, page(2, time.Time{}, time.Time{}, 1, false), 0o644); err != nil { - t.Fatal(err) - } - u := NewUniverse(path) - if armed, _ := u.View(time.Now()); armed { - t.Fatal("unarmed page viewed as armed") - } - past := time.Now().Add(-180 * 24 * time.Hour) - f, _ := os.OpenFile(path, os.O_WRONLY, 0) - f.WriteAt(page(4, past, time.Now(), 1, true), 0) - f.Close() - armed, universes := u.View(time.Now()) - if !armed || len(universes) != 2 || int64(universes[1]) != past.Unix() { - t.Fatalf("armed page: %v %v", armed, universes) - } -} - -func TestAClientMayCheckAgainstTheUniverseItLeftForHoldAfterAChange(t *testing.T) { - now := time.Now() - past := state{Armed: true, V: float64(now.Add(-100 * 24 * time.Hour).Unix()), Anchor: float64(now.Unix()), Rate: 1} - if armed, _ := view(unarmed, state{}, time.Time{}, now); armed { - t.Fatal("real time viewed as armed") - } - armed, universes := view(unarmed, past, now.Add(-Hold/2), now) - if !armed || len(universes) != 2 { - t.Fatalf("within Hold of leaving an armed universe: %v %v", armed, universes) - } - if armed, _ := view(unarmed, past, now.Add(-2*Hold), now); armed { - t.Fatal("past Hold, the universe left still counts") - } -} - -// golden is a page the follower's writer wrote (clockpage.encode), as testdata keeps it. -func golden(t *testing.T, name string) []byte { - text, err := os.ReadFile(filepath.Join("testdata", "page-"+name+".hex")) - if err != nil { - t.Fatal(err) - } - b, err := hex.DecodeString(strings.TrimSpace(string(text))) - if err != nil { - t.Fatal(err) - } - return b -} - -func TestThePagesTheFollowersWriterWritesReadAsTheUniverseTheyHold(t *testing.T) { - s, ok := readPage(golden(t, "armed")) - if !ok || !s.Armed || s.V != 1577836800 || s.Rate != 2 || math.Abs(s.Anchor-1794000000.123456789) > 1e-6 { - t.Fatalf("armed: %+v %v", s, ok) - } - if s, ok := readPage(golden(t, "unarmed")); !ok || s != unarmed { - t.Fatalf("unarmed: %+v %v", s, ok) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/go.mod b/src/agent_env/agents/agents/claude-code/egress/go.mod deleted file mode 100644 index 7b1b97e80..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/go.mod +++ /dev/null @@ -1,12 +0,0 @@ -module agentenv/egress - -go 1.26.11 - -require github.com/refraction-networking/utls v1.8.2 - -require ( - github.com/andybalholm/brotli v1.0.6 // indirect - github.com/klauspost/compress v1.17.4 // indirect - golang.org/x/crypto v0.36.0 // indirect - golang.org/x/sys v0.31.0 // indirect -) diff --git a/src/agent_env/agents/agents/claude-code/egress/go.sum b/src/agent_env/agents/agents/claude-code/egress/go.sum deleted file mode 100644 index 3e8a6d1ef..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= -github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= -github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= -golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= -golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/src/agent_env/agents/agents/claude-code/egress/hello.go b/src/agent_env/agents/agents/claude-code/egress/hello.go deleted file mode 100644 index 381ea2993..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/hello.go +++ /dev/null @@ -1,164 +0,0 @@ -package main - -import ( - "bufio" - "encoding/binary" - "errors" - "strings" - "time" -) - -// A TLS record's content type for handshake messages, a ClientHello's handshake type, and the most plaintext one -// record carries. -const ( - recordHandshake = 22 - clientHelloType = 1 - maxRecord = 16384 -) - -// clientHello is the ClientHello handshake message the client has sent, peeked from r so the handshake stays -// untouched for whoever reads it next; it may span several records. -func clientHello(r *bufio.Reader, deadline time.Time) ([]byte, error) { - need := 5 - for { - if time.Now().After(deadline) { - return nil, errors.New("ClientHello incomplete") - } - data, err := r.Peek(need) - if err != nil { - if errors.Is(err, bufio.ErrBufferFull) { - return nil, errors.New("ClientHello larger than the read buffer") - } - return nil, err - } - if data[0] != recordHandshake { - return nil, errors.New("not TLS") - } - var body []byte - for p := 0; ; { - if p+5 > len(data) { - need = p + 5 - break - } - if data[p] != recordHandshake { - return nil, errors.New("ClientHello interrupted by another record") - } - size := int(binary.BigEndian.Uint16(data[p+3:])) - if p+5+size > len(data) { - need = p + 5 + size - break - } - body = append(body, data[p+5:p+5+size]...) - p += 5 + size - if len(body) >= 4 { - if whole := 4 + (int(body[1])<<16 | int(body[2])<<8 | int(body[3])); len(body) >= whole { - return body[:whole], nil - } - } - } - } -} - -// parseHello is the server name and the ALPN protocols a ClientHello message offers. -func parseHello(hello []byte) (string, []string, error) { - p, err := helloExtensions(hello) - if err != nil { - return "", nil, err - } - if len(hello) < p+2 { - return "", nil, nil - } - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return "", nil, errors.New("extensions overrun the ClientHello") - } - var sni string - var alpn []string - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - body := hello[q+4 : min(q+4+size, end)] - q += 4 + size - switch { - case kind == 0 && len(body) > 5 && body[2] == 0: - n := int(binary.BigEndian.Uint16(body[3:])) - if 5+n <= len(body) { - sni = strings.ToLower(string(body[5 : 5+n])) - } - case kind == 16 && len(body) > 2: - for r := 2; r < len(body); { - n := int(body[r]) - if r+1+n > len(body) { - break - } - alpn = append(alpn, string(body[r+1:r+1+n])) - r += 1 + n - } - } - } - return sni, alpn, nil -} - -// resumption: what only the proxy's own session with the client could mean, never the site's. -var dropped = map[uint16]bool{41: true, 42: true} - -// helloRecord is the ClientHello as one handshake record, without the extensions only a resumption with the proxy put -// there, and the extensions it left out. -func helloRecord(hello []byte) ([]byte, []uint16, error) { - p, err := helloExtensions(hello) - if err != nil { - return nil, nil, err - } - body := append([]byte{}, hello[4:p]...) - var gone []uint16 - if len(hello) >= p+2 { - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return nil, nil, errors.New("extensions overrun the ClientHello") - } - var exts []byte - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - if q+4+size > end { - return nil, nil, errors.New("an extension overruns the ClientHello") - } - if dropped[kind] { - gone = append(gone, kind) - } else { - exts = append(exts, hello[q:q+4+size]...) - } - q += 4 + size - } - body = binary.BigEndian.AppendUint16(body, uint16(len(exts))) - body = append(body, exts...) - } - if len(body)+4 > maxRecord { - return nil, nil, errors.New("ClientHello longer than one record") - } - msg := append([]byte{clientHelloType, byte(len(body) >> 16), byte(len(body) >> 8), byte(len(body))}, body...) - return append([]byte{recordHandshake, 3, 1, byte(len(msg) >> 8), byte(len(msg))}, msg...), gone, nil -} - -// helloExtensions is where a ClientHello message's extensions start, past its version, random, session id, cipher -// suites and compression methods, each length checked against the message. -func helloExtensions(hello []byte) (int, error) { - if len(hello) < 4 || hello[0] != clientHelloType { - return 0, errors.New("not a ClientHello") - } - p := 4 + 2 + 32 - for _, width := range []int{1, 2, 1} { // session id, cipher suites, compression methods - if len(hello) < p+width { - return 0, errors.New("short ClientHello") - } - n := int(hello[p]) - if width == 2 { - n = int(binary.BigEndian.Uint16(hello[p:])) - } - p += width + n - if p > len(hello) { - return 0, errors.New("a ClientHello field overruns the message") - } - } - return p, nil -} diff --git a/src/agent_env/agents/agents/claude-code/egress/hello_test.go b/src/agent_env/agents/agents/claude-code/egress/hello_test.go deleted file mode 100644 index a3b30b2f1..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/hello_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "crypto/tls" - "encoding/binary" - "net" - "reflect" - "testing" - "time" -) - -// goHello is the ClientHello message Go's TLS client sends for name, offering alpn. -func goHello(t *testing.T, name string, alpn []string) []byte { - t.Helper() - client, server := net.Pipe() - go tls.Client(client, &tls.Config{ServerName: name, NextProtos: alpn, InsecureSkipVerify: true}).Handshake() - defer client.Close() - defer server.Close() - hello, err := clientHello(bufio.NewReader(server), time.Now().Add(5*time.Second)) - if err != nil { - t.Fatal(err) - } - return hello -} - -func records(message []byte, size int) []byte { - var out []byte - for len(message) > 0 { - n := min(size, len(message)) - out = append(out, 22, 3, 1, byte(n>>8), byte(n)) - out = append(out, message[:n]...) - message = message[n:] - } - return out -} - -func TestAClientHelloIsReadWholeAcrossRecordsAndNamesItsSiteAndProtocols(t *testing.T) { - hello := goHello(t, "Example.COM", []string{"h2", "http/1.1"}) - sni, alpn, err := parseHello(hello) - if err != nil || sni != "example.com" || !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) { - t.Fatalf("parsed %q %v %v", sni, alpn, err) - } - split := records(hello, 100) - again, err := clientHello(bufio.NewReader(bytes.NewReader(split)), time.Now().Add(time.Second)) - if err != nil || !bytes.Equal(again, hello) { - t.Fatalf("across records: %v", err) - } - if _, err := clientHello(bufio.NewReader(bytes.NewReader([]byte("GET / HTTP/1.1\r\n\r\n"))), - time.Now().Add(time.Second)); err == nil { - t.Fatal("plain HTTP read as TLS") - } -} - -func withExtension(hello []byte, kind uint16, body []byte) []byte { - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(binary.BigEndian.Uint16(hello[p:])) - p += 1 + int(hello[p]) - exts := append(append([]byte{}, hello[p+2:]...), byte(kind>>8), byte(kind), byte(len(body)>>8), byte(len(body))) - exts = append(exts, body...) - out := append(append([]byte{}, hello[:p]...), byte(len(exts)>>8), byte(len(exts))) - out = append(out, exts...) - n := len(out) - 4 - out[1], out[2], out[3] = byte(n>>16), byte(n>>8), byte(n) - return out -} - -func TestTheUpstreamRecordLeavesOutWhatOnlyAResumptionWithTheProxyMeans(t *testing.T) { - hello := withExtension(goHello(t, "example.com", nil), 41, []byte{0, 1, 2}) - record, gone, err := helloRecord(hello) - if err != nil || !reflect.DeepEqual(gone, []uint16{41}) { - t.Fatalf("dropped %v %v", gone, err) - } - if record[0] != 22 || int(binary.BigEndian.Uint16(record[3:])) != len(record)-5 { - t.Fatal("not one handshake record") - } - if _, again, _ := helloRecord(record[5:]); len(again) != 0 { - t.Fatalf("the record still has %v", again) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/leaves.go b/src/agent_env/agents/agents/claude-code/egress/leaves.go deleted file mode 100644 index 027ecac92..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/leaves.go +++ /dev/null @@ -1,188 +0,0 @@ -package main - -import ( - "crypto" - "crypto/ecdsa" - "crypto/elliptic" - "crypto/rand" - "crypto/tls" - "crypto/x509" - "crypto/x509/pkix" - "encoding/pem" - "errors" - "math/big" - "net" - "os" - "path/filepath" - "sort" - "strings" - "sync" -) - -// The longest common name X.509 allows (ub-common-name). -const commonNameLimit = 64 - -// Leaves signs the leaves an intercepted client is handed: by this computer's CA when the site verified, else by a CA -// trusted nowhere, so the client refuses it as it would the site. Each is valid over all of its CA's window, so it -// holds wherever the universe goes, and is minted once per name and its alternative names. -type Leaves struct { - ca, untrusted signer - key *ecdsa.PrivateKey - mu sync.Mutex - cache map[string]*tls.Certificate - // order is the cache's keys, oldest first, so it drops the oldest past cacheLimit; a connection keeps its own. - order []string -} - -type signer struct { - cert *x509.Certificate - key crypto.Signer -} - -func LoadLeaves(folder string) (*Leaves, error) { - certPEM, err := os.ReadFile(filepath.Join(folder, "ca.pem")) - if err != nil { - return nil, err - } - keyPEM, err := os.ReadFile(filepath.Join(folder, "ca.key")) - if err != nil { - return nil, err - } - ca, err := parseSigner(certPEM, keyPEM) - if err != nil { - return nil, err - } - return NewLeaves(ca) -} - -func NewLeaves(ca signer) (*Leaves, error) { - untrusted, err := mintCA("Untrusted upstream") - if err != nil { - return nil, err - } - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return nil, err - } - return &Leaves{ca: ca, untrusted: untrusted, key: key, cache: map[string]*tls.Certificate{}}, nil -} - -// For is the leaf for host and the names sans ("DNS:..." or "IP:..."), by this computer's CA when trusted, and -// whether it was minted now. -func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate, bool, error) { - sorted := append([]string{}, sans...) - sort.Strings(sorted) - key := host + "|" + strings.Join(sorted, ",") - by := l.ca - if !trusted { - key, by = "untrusted|"+key, l.untrusted - } - l.mu.Lock() - defer l.mu.Unlock() - if cert, ok := l.cache[key]; ok { - return cert, false, nil - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, - NotBefore: by.cert.NotBefore.Add(LeafInset), - NotAfter: by.cert.NotAfter.Add(-LeafInset), - KeyUsage: x509.KeyUsageDigitalSignature, - ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, - BasicConstraintsValid: true, - } - for _, name := range append([]string{host}, sans...) { - name = strings.TrimPrefix(strings.TrimPrefix(name, "DNS:"), "IP:") - if ip := net.ParseIP(strings.Trim(name, "[]")); ip != nil { - template.IPAddresses = appendIP(template.IPAddresses, ip) - } else if name != "" && !contains(template.DNSNames, name) { - template.DNSNames = append(template.DNSNames, name) - } - } - der, err := x509.CreateCertificate(rand.Reader, template, by.cert, &l.key.PublicKey, by.key) - if err != nil { - return nil, false, err - } - cert := &tls.Certificate{Certificate: [][]byte{der, by.cert.Raw}, PrivateKey: l.key} - l.cache[key] = cert - l.order = append(l.order, key) - for len(l.order) > cacheLimit { - delete(l.cache, l.order[0]) - l.order = l.order[1:] - } - return cert, true, nil -} - -// mintCA is a self-signed CA over the window, its key in memory. -func mintCA(name string) (signer, error) { - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return signer{}, err - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: name}, - NotBefore: CAFrom, - NotAfter: CAUntil, - KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, - BasicConstraintsValid: true, - IsCA: true, - MaxPathLenZero: true, - } - der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) - if err != nil { - return signer{}, err - } - cert, err := x509.ParseCertificate(der) - if err != nil { - return signer{}, err - } - return signer{cert: cert, key: key}, nil -} - -func parseSigner(certPEM, keyPEM []byte) (signer, error) { - block, _ := pem.Decode(certPEM) - if block == nil { - return signer{}, errors.New("no certificate in the CA's PEM") - } - cert, err := x509.ParseCertificate(block.Bytes) - if err != nil { - return signer{}, err - } - block, _ = pem.Decode(keyPEM) - if block == nil { - return signer{}, errors.New("no key in the CA key's PEM") - } - key, err := x509.ParsePKCS8PrivateKey(block.Bytes) - if err != nil { - return signer{}, err - } - s, ok := key.(crypto.Signer) - if !ok { - return signer{}, errors.New("the CA key can't sign") - } - return signer{cert: cert, key: s}, nil -} - -func serial() *big.Int { - n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) - return n -} - -func appendIP(ips []net.IP, ip net.IP) []net.IP { - for _, have := range ips { - if have.Equal(ip) { - return ips - } - } - return append(ips, ip) -} - -func contains(list []string, s string) bool { - for _, have := range list { - if strings.EqualFold(have, s) { - return true - } - } - return false -} diff --git a/src/agent_env/agents/agents/claude-code/egress/main.go b/src/agent_env/agents/agents/claude-code/egress/main.go deleted file mode 100644 index 0bd5ae5c5..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/main.go +++ /dev/null @@ -1,92 +0,0 @@ -// egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. -// -// It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and -// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128. Session programs reach it through HTTP(S)_PROXY and -// ALL_PROXY; nothing redirects or filters, so it needs no NET_ADMIN and works on any provider. -// -// A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe -// a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is -// intercepted: the site is verified against real time and the public roots, and the client is handed a leaf from this -// computer's CA, valid over every date the clock can show. The upstream handshake is uTLS's, built from the client's -// own ClientHello, so the site sees the client's fingerprint and ALPN offer either way. -// -// egress boot as root at the computer's start: the CA, minted once, its key this user's alone, trusted everywhere -// egress serve as agentenv-egress: serve, one JSON line per connection on stdout -package main - -import ( - "encoding/json" - "fmt" - "os" - "time" -) - -const ( - Listen = "127.0.0.1:3128" - EgressUser = "agentenv-egress" - SessionUser = "user" - Folder = "/etc/agentenv/egress" - PublicCA = "/etc/agentenv/egress-ca.pem" - PublicRoots = "/usr/local/lib/agentenv-egress/roots.pem" - ClockPage = "/dev/shm/agentenv-clock" - Hosts = "/etc/hosts" - HostsBegin = "# agentenv sites" - HostsEnd = "# end agentenv sites" - CAName = "agentenv egress CA" - NSSNickname = "agentenv egress" - Hold = 30 * time.Second - Margin = 8 * 24 * time.Hour - ProbeTTL = time.Hour - ProbeFailedTTL = time.Minute - FirstBytes = 300 * time.Millisecond - HelloTimeout = 10 * time.Second - DialTimeout = 10 * time.Second - - // How long a client has to send its request's head, and how long either handshake of an interception, the - // client's with the proxy and the proxy's with the site, has to finish. - HeadTimeout = 15 * time.Second - HandshakeTimeout = 15 * time.Second - - // The longest request head a client may send, and the most of an error a log row keeps, in bytes. - HeadLimit = 64 << 10 - ErrorLimit = 300 - - // How far inside the CA's dates each leaf it signs is, so no leaf outlasts its CA. - LeafInset = 2 * time.Hour - - // How often the clock page is read, and how many reads may find it mid-write before the last universe stands. - PagePoll = 20 * time.Millisecond - PageReads = 1000 -) - -// The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' -// (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. -var ( - CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) - CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) -) - -func main() { - command := "" - if len(os.Args) > 1 { - command = os.Args[1] - } - switch command { - case "boot": - report, err := boot(defaultBootPaths()) - out, _ := json.Marshal(report) - fmt.Println(string(out)) - if err != nil { - fmt.Fprintln(os.Stderr, "egress boot:", err) - os.Exit(1) - } - case "serve": - if err := serveForever(); err != nil { - fmt.Fprintln(os.Stderr, "egress serve:", err) - os.Exit(1) - } - default: - fmt.Fprintf(os.Stderr, "usage: egress boot|serve, not %q\n", command) - os.Exit(2) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy.go b/src/agent_env/agents/agents/claude-code/egress/proxy.go deleted file mode 100644 index c575fe5e6..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/proxy.go +++ /dev/null @@ -1,598 +0,0 @@ -package main - -import ( - "bufio" - "context" - "crypto/tls" - "crypto/x509" - "encoding/json" - "errors" - "fmt" - "io" - "net" - "net/http" - "net/netip" - "net/url" - "os" - "strconv" - "strings" - "sync" - "time" -) - -type Proxy struct { - Leaves *Leaves - Universe interface { - View(time.Time) (bool, []float64) - } - // Roots are the public roots a site is verified against, as the image had them before any agentenv CA joined. - Roots *x509.CertPool - // Resolve, Allowed, Routed and Dial default to the system's resolver, a global address, the hosts file's agentenv - // sites block and a TCP dial; tests replace them. - Resolve func(ctx context.Context, host string) ([]netip.Addr, error) - Allowed func(netip.Addr) bool - Routed func() map[string]string - Dial func(dst string) (net.Conn, error) - Log func(row map[string]any) - probes probes -} - -func serveForever() error { - leaves, err := LoadLeaves(Folder) - if err != nil { - return fmt.Errorf("the CA (has boot run?): %w", err) - } - roots, err := os.ReadFile(PublicRoots) - if err != nil { - return err - } - pool := x509.NewCertPool() - if !pool.AppendCertsFromPEM(roots) { - return fmt.Errorf("no certificates in %s", PublicRoots) - } - universe := NewUniverse(ClockPage) - go universe.Poll() - ln, err := net.Listen("tcp", Listen) - if err != nil { - return err - } - p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} - p.Log(map[string]any{"event": "serving", "listen": Listen, "uid": os.Getuid(), "pid": os.Getpid()}) - return p.Serve(ln) -} - -func jsonLines(w io.Writer) func(map[string]any) { - var mu sync.Mutex - encoder := json.NewEncoder(w) - return func(row map[string]any) { - row["t"] = float64(time.Now().UnixMilli()) / 1e3 - mu.Lock() - defer mu.Unlock() - encoder.Encode(row) - } -} - -func (p *Proxy) Serve(ln net.Listener) error { - for { - conn, err := ln.Accept() - if err != nil { - if errors.Is(err, net.ErrClosed) { - return nil - } - return err - } - go p.handle(conn) - } -} - -func (p *Proxy) log(row map[string]any) { - if p.Log != nil { - p.Log(row) - } -} - -func (p *Proxy) handle(conn net.Conn) { - start := time.Now() - row := map[string]any{"client": conn.RemoteAddr().String()} - defer func() { - if r := recover(); r != nil { - row["error"] = fmt.Sprint("panic: ", r) - p.log(row) - } - conn.Close() - }() - br := bufio.NewReaderSize(conn, HeadLimit) - conn.SetReadDeadline(start.Add(HeadTimeout)) - method, target, version, headers, err := readHead(br) - if err != nil { - row["error"] = truncate("request: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["method"] = method - if method == http.MethodConnect { - host, port := splitAuthority(target, 443) - row["host"], row["port"] = host, port - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - if _, err := io.WriteString(conn, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { - return - } - p.tunnel(conn, br, host, port, dsts, why, row, start) - return - } - u, err := url.Parse(target) - if err != nil || u.Scheme != "http" || u.Host == "" { - row["mode"], row["error"] = "refused", "not an absolute http URL" - reply(conn, "400 Bad Request", "egress: CONNECT, or an absolute http:// URL\n") - p.log(row) - return - } - host, port := splitAuthority(u.Host, 80) - row["host"], row["port"], row["mode"] = host, port, "http" - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate(err.Error(), ErrorLimit) - reply(conn, "502 Bad Gateway", "egress: "+err.Error()+"\n") - p.log(row) - return - } - defer up.Close() - row["dst"] = dst - upgrade := false - for _, h := range headers { - if headerName(h) == "upgrade" { - upgrade = true - } - } - // The client's own head, but for its request line, which names the path alone, and the headers meant for this - // proxy; a request that isn't an upgrade closes once answered, so its connection can't carry one for another site. - var head strings.Builder - head.WriteString(method + " " + u.RequestURI() + " " + version + "\r\n") - for _, h := range headers { - switch headerName(h) { - case "proxy-connection", "proxy-authorization": - continue - case "connection", "keep-alive": - if !upgrade { - continue - } - } - head.WriteString(h + "\r\n") - } - if upgrade { - row["mode"] = "upgrade" - } else { - head.WriteString("Connection: close\r\n") - } - head.WriteString("\r\n") - conn.SetReadDeadline(time.Time{}) - if _, err := io.WriteString(up, head.String()); err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return - } - p.log(row) - // Both ways at once: a body the client sends once told to continue, or an upgraded connection's frames. - go func() { - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) -} - -// readHead is a request's head, read through its blank line: its request line's parts and its header lines. -func readHead(r *bufio.Reader) (string, string, string, []string, error) { - var lines []string - size := 0 - for { - line, err := r.ReadString('\n') - size += len(line) - if size > HeadLimit { - return "", "", "", nil, errors.New("request head too long") - } - if err != nil { - return "", "", "", nil, err - } - line = strings.TrimRight(line, "\r\n") - if line == "" { - if len(lines) == 0 { - continue - } - break - } - lines = append(lines, line) - } - parts := strings.SplitN(lines[0], " ", 3) - if len(parts) != 3 { - return "", "", "", nil, fmt.Errorf("malformed request line %q", truncate(lines[0], 100)) - } - return parts[0], parts[1], parts[2], lines[1:], nil -} - -func headerName(line string) string { - name, _, _ := strings.Cut(line, ":") - return strings.ToLower(strings.TrimSpace(name)) -} - -// destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for -// a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), -// "global" for a name whose every address is global, IPv4 first. Anything else is refused. -func (p *Proxy) destination(host string, port int) ([]string, string, error) { - host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") - routed := p.Routed - if routed == nil { - routed = routedSites - } - if address, ok := routed()[host]; ok { - return []string{net.JoinHostPort(address, strconv.Itoa(port))}, "routed", nil - } - resolve := p.Resolve - if resolve == nil { - resolve = func(ctx context.Context, host string) ([]netip.Addr, error) { - return net.DefaultResolver.LookupNetIP(ctx, "ip", host) - } - } - allowed := p.Allowed - if allowed == nil { - allowed = isGlobal - } - ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) - defer cancel() - addresses, err := resolve(ctx, host) - if err != nil { - return nil, "", fmt.Errorf("%s doesn't resolve: %w", host, err) - } - if len(addresses) == 0 { - return nil, "", fmt.Errorf("%s has no address", host) - } - var private []string - var v4, v6 []string - for _, a := range addresses { - a = a.Unmap() - dst := netip.AddrPortFrom(a, uint16(port)).String() - switch { - case !allowed(a): - private = append(private, a.String()) - case a.Is4(): - v4 = append(v4, dst) - default: - v6 = append(v6, dst) - } - } - if len(private) > 0 { - return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) - } - return append(v4, v6...), "global", nil -} - -// notGlobal are the special-purpose blocks (IANA's registries) no global address is in. -var notGlobal = func() []netip.Prefix { - var out []netip.Prefix - for _, s := range []string{ - "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", - "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", - "224.0.0.0/4", "240.0.0.0/4", - "::/128", "::1/128", "64:ff9b:1::/48", "100::/64", "2001::/23", "2001:db8::/32", "fc00::/7", "fe80::/10", - "ff00::/8", - } { - out = append(out, netip.MustParsePrefix(s)) - } - return out -}() - -func isGlobal(a netip.Addr) bool { - a = a.Unmap() - if !a.IsValid() { - return false - } - for _, block := range notGlobal { - if block.Contains(a) { - return false - } - } - return true -} - -// routedSites is the hosts file's agentenv sites block, name to address: root's, so the session user can't add to it. -func routedSites() map[string]string { - out := map[string]string{} - data, err := os.ReadFile(Hosts) - if err != nil { - return out - } - inside := false - for _, line := range strings.Split(string(data), "\n") { - text := strings.TrimSpace(line) - switch { - case text == HostsBegin: - inside = true - case text == HostsEnd: - inside = false - case inside && text != "" && !strings.HasPrefix(text, "#"): - fields := strings.Fields(text) - for _, name := range fields[1:] { - out[strings.ToLower(name)] = fields[0] - } - } - } - return out -} - -func (p *Proxy) tunnel(conn net.Conn, br *bufio.Reader, host string, port int, dsts []string, why string, - row map[string]any, start time.Time) { - wait := FirstBytes - if port == 443 { - wait = HelloTimeout - } - conn.SetReadDeadline(time.Now().Add(wait)) - first, err := br.Peek(1) - if err != nil && !isTimeout(err) { - row["error"] = "closed before sending" - p.log(row) - return - } - if len(first) == 0 || first[0] != recordHandshake || why == "routed" { - row["mode"], row["decision"] = "pass", "not TLS" - if len(first) > 0 && first[0] == recordHandshake { - row["mode"], row["decision"] = "splice", "routed" - } - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - conn.SetReadDeadline(time.Now().Add(HelloTimeout)) - hello, err := clientHello(br, time.Now().Add(HelloTimeout)) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - sni, offered, err := parseHello(hello) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["sni"], row["alpn_offered"] = sni, offered - verifyName := sni - if verifyName == "" { - verifyName = strings.Trim(host, "[]") - } - armed, universes := p.Universe.View(time.Now()) - if !armed { - row["decision"], row["mode"] = "real", "splice" - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - row["universes"] = rounded(universes) - // Each address in turn, by its own probe: one that doesn't answer is passed over, so the connection goes to a - // server whose own certificate decided it. - var unreachable []string - for _, dst := range p.preferChecked(dsts, sni) { - delete(row, "probe_error") - delete(row, "upstream_valid") - seen, probed := p.probeAt(dst, sni, verifyName, hello) - if seen.unreachable { - unreachable = append(unreachable, seen.problem) - continue - } - row["probed"] = probed - decision := "outside" - switch { - case seen.failed: - // a handshake only the client can finish, as one asking for its certificate: the client decides - decision, row["probe_error"] = "probe_failed", seen.problem - case !seen.verified: - decision, row["probe_error"] = "unverified", seen.problem - case inside(seen.from, seen.until, universes): - decision = "inside" - } - if seen.verified { - row["upstream_valid"] = []int64{seen.from.Unix(), seen.until.Unix()} - } - row["decision"] = decision - var dialled bool - if decision == "inside" || decision == "probe_failed" { - row["mode"] = "splice" - dialled = p.splice(conn, br, []string{dst}, row, start) - } else { - row["mode"] = "intercept" - dialled = p.intercept(conn, br, dst, sni, verifyName, hello, row, start) - } - if dialled { - return - } - // it stopped answering since its probe: forget it, and go on to the next - p.forget(dst, sni) - unreachable = append(unreachable, fmt.Sprint(row["error"])) - delete(row, "error") - } - row["error"] = truncate("upstream: no address answered: "+strings.Join(unreachable, "; "), ErrorLimit) - p.log(row) -} - -// preferChecked is dsts with those whose fresh probe answered first, each group in its own order, so an address -// already found working isn't kept waiting behind one whose probe has to be made again. -func (p *Proxy) preferChecked(dsts []string, sni string) []string { - p.probes.mu.Lock() - defer p.probes.mu.Unlock() - var checked, rest []string - for _, dst := range dsts { - if hit, ok := p.probes.cache[dst+"|"+sni]; ok && hit.fresh() && !hit.unreachable { - checked = append(checked, dst) - } else { - rest = append(rest, dst) - } - } - return append(checked, rest...) -} - -// splice relays the connection to the first of dsts that answers, untouched; false, with the error in row, when none -// does. -func (p *Proxy) splice(conn net.Conn, br *bufio.Reader, dsts []string, row map[string]any, start time.Time) bool { - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - defer up.Close() - row["dst"] = dst - conn.SetReadDeadline(time.Time{}) - row["upstream_ms"] = time.Since(start).Milliseconds() - p.log(row) - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) - closeWrite(conn) - wg.Wait() - return true -} - -// intercept hands the client a leaf of this computer's CA and relays the plaintext to dst over a handshake of the -// proxy's own; false, with the error in row, when dst doesn't answer. -func (p *Proxy) intercept(conn net.Conn, br *bufio.Reader, dst, sni, verifyName string, hello []byte, - row map[string]any, start time.Time) bool { - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - if errors.As(err, &unanswered) { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - defer uc.NetConn().Close() - row["dst"] = dst - row["alpn"], row["verified"], row["upstream_tls"] = found.ALPN, found.Verified, []string{found.Version, found.Cipher} - row["upstream_ms"] = time.Since(start).Milliseconds() - if len(found.Dropped) > 0 { - row["dropped_extensions"] = found.Dropped - } - var sans []string - if found.Verified { - // the site's own names, as its leaf lists them, so the client pools connections as it would with the site's - // certificate - for _, name := range found.SANs { - if sni == "" || strings.HasPrefix(name, "DNS:") { - sans = append(sans, name) - } - } - if sni == "" { - sans = append(sans, "IP:"+strings.Trim(hostOf(dst), "[]")) - } - } else { - row["upstream_verify"] = truncate(found.VerifyErr, 200) - } - cert, minted, err := p.Leaves.For(verifyName, sans, found.Verified) - if err != nil { - row["error"] = truncate("leaf: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - row["minted"] = minted - config := &tls.Config{Certificates: []tls.Certificate{*cert}, SessionTicketsDisabled: true} - if found.ALPN != "" { - config.NextProtos = []string{found.ALPN} - } - client := tls.Server(&peeked{Conn: conn, r: br}, config) - conn.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := client.Handshake(); err != nil { - row["error"] = truncate("client handshake: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - conn.SetDeadline(time.Time{}) - row["handshake_ms"] = time.Since(start).Milliseconds() - p.log(row) - done := make(chan struct{}, 2) - go func() { - io.Copy(uc, client) - done <- struct{}{} - }() - go func() { - io.Copy(client, uc) - done <- struct{}{} - }() - <-done - client.Close() - uc.NetConn().Close() - <-done - return true -} - -// peeked is a connection whose reads come through the reader that peeked its first bytes. -type peeked struct { - net.Conn - r *bufio.Reader -} - -func (c *peeked) Read(b []byte) (int, error) { return c.r.Read(b) } - -func closeWrite(c net.Conn) { - if cw, ok := c.(interface{ CloseWrite() error }); ok { - cw.CloseWrite() - } -} - -func isTimeout(err error) bool { - var ne net.Error - return errors.As(err, &ne) && ne.Timeout() -} - -func reply(conn net.Conn, status, body string) { - fmt.Fprintf(conn, "HTTP/1.1 %s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", - status, len(body), body) -} - -func splitAuthority(authority string, port int) (string, int) { - host, p, err := net.SplitHostPort(authority) - if err != nil { - return strings.Trim(authority, "[]"), port - } - n, err := strconv.Atoi(p) - if err != nil { - return host, port - } - return host, n -} - -func hostOf(hostport string) string { - host, _, err := net.SplitHostPort(hostport) - if err != nil { - return hostport - } - return host -} - -func rounded(values []float64) []int64 { - out := make([]int64, len(values)) - for i, v := range values { - out[i] = int64(v) - } - return out -} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go deleted file mode 100644 index c8e57b3e7..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go +++ /dev/null @@ -1,564 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "context" - "crypto/tls" - "crypto/x509" - "fmt" - "io" - "net" - "net/http" - "net/http/httptest" - "net/netip" - "net/url" - "strings" - "sync" - "testing" - "time" -) - -type fixedUniverse struct { - armed bool - universes []float64 -} - -func (f fixedUniverse) View(time.Time) (bool, []float64) { return f.armed, f.universes } - -func TestOnlyDestinationsWhoseEveryAddressIsGlobalAreDialled(t *testing.T) { - refused := []string{"10.0.0.1", "10.255.255.254", "172.16.0.1", "172.31.255.1", "192.168.1.1", "127.0.0.1", - "127.1.2.3", "0.0.0.0", "169.254.169.254", "169.254.1.1", "100.64.0.1", "100.127.255.254", "192.0.2.1", - "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::", "fd00::1", "fe80::1", "::ffff:10.0.0.1", - "2001:db8::1"} - if len(refused) != 22 { - t.Fatal(len(refused)) - } - p := &Proxy{Routed: func() map[string]string { return nil }} - for _, address := range refused { - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr(address)}, nil - } - if _, _, err := p.destination("somewhere.example", 443); err == nil { - t.Errorf("%s dialled", address) - } - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("2606:4700::1111"), netip.MustParseAddr("1.1.1.1")}, nil - } - dst, why, err := p.destination("one.one.one.one", 443) - if err != nil || strings.Join(dst, " ") != "1.1.1.1:443 [2606:4700::1111]:443" || why != "global" { - t.Fatalf("a public name, IPv4 first: %q %q %v", dst, why, err) - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("10.0.0.1")}, nil - } - if _, _, err := p.destination("split.example", 443); err == nil || !strings.Contains(err.Error(), "10.0.0.1") { - t.Fatalf("a name with one private address: %v", err) - } - p.Routed = func() map[string]string { return map[string]string{"notes.test": "127.0.0.1"} } - if dst, why, err := p.destination("Notes.Test.", 443); err != nil || strings.Join(dst, " ") != "127.0.0.1:443" || - why != "routed" { - t.Fatalf("a routed site: %q %q %v", dst, why, err) - } -} - -// harness is a proxy in front of the site server, which it reaches as example.com, and a client whose clock is at -// clientTime. -type harness struct { - t *testing.T - site *httptest.Server - proxy *Proxy - url *url.URL - ca signer - clientCert *tls.Certificate - rows []map[string]any - rowsMu sync.Mutex -} - -func newHarness(t *testing.T, site *httptest.Server, universe fixedUniverse, trustSite bool) *harness { - t.Helper() - ca, err := mintCA(CAName) - if err != nil { - t.Fatal(err) - } - leaves, err := NewLeaves(ca) - if err != nil { - t.Fatal(err) - } - roots := x509.NewCertPool() - if trustSite && site.Certificate() != nil { - roots.AddCert(site.Certificate()) - } - h := &harness{t: t, site: site, ca: ca} - h.proxy = &Proxy{ - Leaves: leaves, Universe: universe, Roots: roots, - Resolve: func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil - }, - Allowed: func(netip.Addr) bool { return true }, - Routed: func() map[string]string { return nil }, - Log: func(row map[string]any) { - h.rowsMu.Lock() - h.rows = append(h.rows, row) - h.rowsMu.Unlock() - }, - } - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { ln.Close() }) - go h.proxy.Serve(ln) - h.url, _ = url.Parse("http://" + ln.Addr().String()) - return h -} - -func (h *harness) get(scheme string, clientTime time.Time, http2 bool) (*http.Response, string, error) { - roots := x509.NewCertPool() - if h.site.Certificate() != nil { - roots.AddCert(h.site.Certificate()) - } - roots.AddCert(h.ca.cert) - config := &tls.Config{RootCAs: roots, Time: func() time.Time { return clientTime }} - if h.clientCert != nil { - config.Certificates = []tls.Certificate{*h.clientCert} - } - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ForceAttemptHTTP2: http2, TLSClientConfig: config} - defer transport.CloseIdleConnections() - _, port, _ := net.SplitHostPort(h.site.Listener.Addr().String()) - resp, err := (&http.Client{Transport: transport, Timeout: 10 * time.Second}).Get( - fmt.Sprintf("%s://example.com:%s/hello", scheme, port)) - if err != nil { - return nil, "", err - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - return resp, string(body), err -} - -func (h *harness) lastRow() map[string]any { - deadline := time.Now().Add(2 * time.Second) - for time.Now().Before(deadline) { - h.rowsMu.Lock() - n := len(h.rows) - h.rowsMu.Unlock() - if n > 0 { - break - } - time.Sleep(10 * time.Millisecond) - } - h.rowsMu.Lock() - defer h.rowsMu.Unlock() - if len(h.rows) == 0 { - h.t.Fatal("no row logged") - } - return h.rows[len(h.rows)-1] -} - -func site(t *testing.T, http2 bool) *httptest.Server { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello over %s", r.Proto) - })) - s.EnableHTTP2 = http2 - s.StartTLS() - t.Cleanup(s.Close) - return s -} - -func TestOnRealTimeTheClientsBytesReachTheSiteUntouched(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - resp, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatal("the client didn't see the site's own certificate") - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "real" { - t.Fatalf("%v", row) - } -} - -func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t *testing.T) { - s := site(t, true) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now, now}}, true) - resp, _, err := h.get("https", time.Now(), true) - if err != nil || !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatalf("inside: %v", err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "inside" { - t.Fatalf("%v", row) - } - - // httptest's certificate ends in 2084: a universe in 2090 is outside it. - later := time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC) - u := float64(later.Unix()) - h = newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u, u}}, true) - resp, body, err := h.get("https", later, true) - if err != nil || body != "hello over HTTP/2.0" { - t.Fatalf("outside: %q %v", body, err) - } - leaf := resp.TLS.PeerCertificates[0] - if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { - t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) - } - row := h.lastRow() - if row["mode"] != "intercept" || row["decision"] != "outside" || row["alpn"] != "h2" || row["verified"] != true { - t.Fatalf("%v", row) - } -} - -func TestASiteThatDoesntVerifyIsHandedOnUnderACATrustedNowhere(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, false) - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "certificate") { - t.Fatalf("the client took it: %v", err) - } - if row := h.lastRow(); row["decision"] != "unverified" { - t.Fatalf("%v", row) - } -} - -func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "plain %s %s ua=%q", r.Method, r.URL.Path, r.UserAgent()) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, body, err := h.get("http", time.Now(), false) - if err != nil || !strings.HasPrefix(body, "plain GET /hello") { - t.Fatalf("%q %v", body, err) - } -} - -func TestARefusedDestinationIsAnswered403(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Allowed = isGlobal - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "Forbidden") { - t.Fatalf("%v", err) - } - if row := h.lastRow(); row["mode"] != "refused" { - t.Fatalf("%v", row) - } -} - -func TestEveryCheckedAddressIsTriedInTurn(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "127.0.0.1:") { - t.Fatalf("%v", row) - } -} - -func TestAnUpgradeOverPlainHTTPRelaysBothWays(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Upgrade") != "echo" || !strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") { - w.WriteHeader(http.StatusBadRequest) - return - } - conn, rw, _ := w.(http.Hijacker).Hijack() - defer conn.Close() - rw.WriteString("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: echo\r\n\r\n") - rw.Flush() - line, _ := rw.ReadString('\n') - rw.WriteString(line) - rw.Flush() - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - conn, err := net.Dial("tcp", h.url.Host) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - conn.SetDeadline(time.Now().Add(5 * time.Second)) - fmt.Fprintf(conn, "GET http://example.com:%s/ws HTTP/1.1\r\nHost: example.com:%s\r\nConnection: Upgrade\r\n"+ - "Upgrade: echo\r\n\r\n", port, port) - br := bufio.NewReader(conn) - resp, err := http.ReadResponse(br, nil) - if err != nil || resp.StatusCode != http.StatusSwitchingProtocols { - t.Fatalf("%v %v", resp, err) - } - fmt.Fprint(conn, "ping\n") - if line, err := br.ReadString('\n'); err != nil || line != "ping\n" { - t.Fatalf("%q %v", line, err) - } -} - -func TestAnUploadThatWaitsFor100ContinueGoesThroughAtOnce(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - body, _ := io.ReadAll(r.Body) - fmt.Fprint(w, len(body)) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - req, _ := http.NewRequest("POST", "http://example.com:"+port+"/up", bytes.NewReader(make([]byte, 100000))) - req.Header.Set("Expect", "100-continue") - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ExpectContinueTimeout: 10 * time.Second} - defer transport.CloseIdleConnections() - started := time.Now() - resp, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(req) - if err != nil { - t.Fatal(err) - } - body, _ := io.ReadAll(resp.Body) - resp.Body.Close() - if string(body) != "100000" || time.Since(started) > 5*time.Second { - t.Fatalf("%q after %s", body, time.Since(started)) - } -} - -func TestEveryCertificateOfTheVerifiedChainBoundsPassingThrough(t *testing.T) { - year := func(y int) time.Time { return time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) } - leaf := &x509.Certificate{NotBefore: year(2020), NotAfter: year(2030)} - intermediate := &x509.Certificate{NotBefore: year(2019), NotAfter: year(2025)} - root := &x509.Certificate{NotBefore: year(2000), NotAfter: year(2040)} - from, until := chainWindow([][]*x509.Certificate{{leaf, intermediate, root}}) - if !from.Equal(year(2020)) || !until.Equal(year(2025)) { - t.Fatalf("%s to %s", from, until) - } - if inside(from, until, []float64{float64(year(2027).Unix())}) { - t.Fatal("a universe past the intermediate's end passed through") - } -} - -func TestASiteAskingForTheClientsCertificateIsPassedThroughForTheClientToAnswer(t *testing.T) { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello %d certificate", len(r.TLS.PeerCertificates)) - })) - s.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, MaxVersion: tls.VersionTLS12} - s.StartTLS() - t.Cleanup(s.Close) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - client, err := mintCA("client") - if err != nil { - t.Fatal(err) - } - h.clientCert = &tls.Certificate{Certificate: [][]byte{client.cert.Raw}, PrivateKey: client.key} - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello 1 certificate" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "probe_failed" { - t.Fatalf("%v", row) - } -} - -func TestTheProbeAndLeafCachesStayWithinTheirLimit(t *testing.T) { - limit := cacheLimit - cacheLimit = 3 - defer func() { cacheLimit = limit }() - ca, _ := mintCA(CAName) - leaves, _ := NewLeaves(ca) - for i := 0; i < 10; i++ { - if _, _, err := leaves.For(fmt.Sprintf("site%d.test", i), nil, true); err != nil { - t.Fatal(err) - } - } - if len(leaves.cache) != 3 || leaves.cache["site9.test|"] == nil { - t.Fatalf("%d leaves kept", len(leaves.cache)) - } - p := &Proxy{Dial: func(string) (net.Conn, error) { return nil, fmt.Errorf("unreachable") }} - for i := 0; i < 10; i++ { - p.probeAt(fmt.Sprintf("192.0.2.%d:443", i), "", "x", goHello(t, "x", nil)) - } - if len(p.probes.cache) != 3 { - t.Fatalf("%d probes kept", len(p.probes.cache)) - } -} - -func TestAConnectionTheProbeLetsThroughGoesToTheServerItProbed(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - var mu sync.Mutex - var dialled []string - h.proxy.Dial = func(dst string) (net.Conn, error) { - mu.Lock() - dialled = append(dialled, dst) - mu.Unlock() - return net.DialTimeout("tcp", dst, DialTimeout) - } - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - mu.Lock() - defer mu.Unlock() - // the probe tried 127.0.0.2, then reached 127.0.0.1; the connection went straight to 127.0.0.1 - if row["decision"] != "inside" || len(dialled) != 3 || !strings.HasPrefix(dialled[2], "127.0.0.1:") { - t.Fatalf("%v %v", row, dialled) - } - if hit := h.proxy.probes.cache[dialled[0]+"|example.com"]; !hit.unreachable || hit.verified { - t.Fatalf("the address that didn't answer is cached as %+v", hit) - } -} - -func TestASlowProbeHoldsUpNoOtherSite(t *testing.T) { - release := make(chan struct{}) - p := &Proxy{Dial: func(dst string) (net.Conn, error) { - if strings.HasPrefix(dst, "192.0.2.1:") { - <-release - } - return nil, fmt.Errorf("unreachable") - }} - hello := goHello(t, "x", nil) - p.probeAt("192.0.2.2:443", "", "x", hello) - go p.probeAt("192.0.2.1:443", "", "x", hello) - time.Sleep(50 * time.Millisecond) - started := time.Now() - if _, probed := p.probeAt("192.0.2.2:443", "", "x", hello); probed || time.Since(started) > 100*time.Millisecond { - t.Fatalf("a cached site waited %s behind a slow one (probed again: %v)", time.Since(started), probed) - } - close(release) -} - -// standIns resolves example.com to its addresses and dials each to the site unless it's down; both may change -// between requests. -type standIns struct { - mu sync.Mutex - addresses []string - down map[string]bool -} - -func (s *standIns) set(down []string, addresses ...string) { - s.mu.Lock() - defer s.mu.Unlock() - s.addresses, s.down = addresses, map[string]bool{} - for _, a := range down { - s.down[a] = true - } -} - -func newStandIns(h *harness, addresses ...string) *standIns { - s := &standIns{addresses: addresses, down: map[string]bool{}} - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - s.mu.Lock() - defer s.mu.Unlock() - var out []netip.Addr - for _, a := range s.addresses { - out = append(out, netip.MustParseAddr(a)) - } - return out, nil - } - h.proxy.Dial = func(dst string) (net.Conn, error) { - host, _, _ := net.SplitHostPort(dst) - s.mu.Lock() - down := s.down[host] - s.mu.Unlock() - if down { - return nil, fmt.Errorf("%s is down", host) - } - return net.DialTimeout("tcp", h.site.Listener.Addr().String(), DialTimeout) - } - return s -} - -func TestAnAddressThatStopsAnsweringAfterItsProbeGivesWayToTheNext(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - addresses := newStandIns(h, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.1:") { - t.Fatalf("%v", row) - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["decision"] != "inside" { - t.Fatalf("%v", row) - } -} - -func TestAnAddressThatDidntAnswerLeavesANewAddressToItsOwnProbe(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - addresses := newStandIns(h) - addresses.set([]string{"198.51.100.1"}, "198.51.100.1") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err == nil { - t.Fatal("reached a site none of whose addresses answer") - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - if !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["mode"] != "intercept" || row["decision"] != "outside" { - t.Fatalf("the new address wasn't judged by its own certificate: %v", row) - } -} - -func TestAClientHelloWhoseFieldsOverrunItIsRefusedNotPanickedOn(t *testing.T) { - hello := goHello(t, "example.com", nil) - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(hello[p])<<8 | int(hello[p+1]) - bad := append([]byte{}, hello...) - bad[p] = 0xff // compression methods' length, past the message's end - bad = bad[:p+3] - if _, _, err := parseHello(bad); err == nil { - t.Fatal("parseHello took it") - } - if _, _, err := helloRecord(bad); err == nil { - t.Fatal("helloRecord took it") - } -} - -func TestAProbeThatPanicsLeavesNoOneWaitingOnIt(t *testing.T) { - p := &Proxy{Dial: func(string) (net.Conn, error) { panic("dial") }} - hello := goHello(t, "x", nil) - probe := func() (panicked bool) { - defer func() { panicked = recover() != nil }() - p.probeAt("192.0.2.9:443", "", "x", hello) - return false - } - if !probe() { - t.Fatal("the probe didn't panic") - } - done := make(chan bool) - go func() { done <- probe() }() - select { - case <-done: - case <-time.After(2 * time.Second): - t.Fatal("a second probe waited on the one that panicked") - } -} - -func TestAnAddressAlreadyFoundWorkingIsTriedBeforeOnesNotYetProbed(t *testing.T) { - p := &Proxy{} - p.probes.cache = map[string]probe{ - "[2001:db8::1]:443|x": {verified: true, at: time.Now()}, - "192.0.2.1:443|x": {unreachable: true, at: time.Now().Add(-2 * ProbeFailedTTL)}, - "192.0.2.3:443|x": {unreachable: true, at: time.Now()}, - } - got := p.preferChecked([]string{"192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443", "[2001:db8::1]:443"}, "x") - want := []string{"[2001:db8::1]:443", "192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443"} - if strings.Join(got, " ") != strings.Join(want, " ") { - t.Fatalf("%v", got) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex deleted file mode 100644 index 2aecf27e8..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000040000000000000000008ab9359ae51515cd68be7e91e51800000000000000400100000000000000070000000000000002b1a878f27237d300f2052a0100000000f2052a01000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex deleted file mode 100644 index 7f6a95f54..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000020000000000000000000000000000000000000000000000000000000000f03f00000000000000000300000000000000147c4a7fb979c7a100000000000000000000000000000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/upstream.go b/src/agent_env/agents/agents/claude-code/egress/upstream.go deleted file mode 100644 index ccf32704f..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/upstream.go +++ /dev/null @@ -1,268 +0,0 @@ -package main - -import ( - "crypto/x509" - "errors" - "fmt" - "net" - "strings" - "sync" - "time" - - utls "github.com/refraction-networking/utls" -) - -// upstream is what a handshake with a site found: the protocol it picked, its leaf's validity and names, and whether -// that leaf verifies against real time and the public roots. -type upstream struct { - // Dst is the address the handshake reached. - Dst string - ALPN string - Verified bool - VerifyErr string - NotBefore time.Time - NotAfter time.Time - SANs []string - Version string - Cipher string - Dropped []uint16 -} - -// handshake is uTLS's handshake with the site at dst, built from the client's own ClientHello, so the site sees the -// client's fingerprint and ALPN offer. Records go full-size from the start and the connection closes without a -// close_notify, as Chromium's BoringSSL does. -func (p *Proxy) handshake(dsts []string, sni, verifyName string, hello []byte) (*utls.UConn, upstream, error) { - var found upstream - record, gone, err := helloRecord(hello) - if err != nil { - return nil, found, err - } - found.Dropped = gone - spec, err := (&utls.Fingerprinter{AllowBluntMimicry: true}).FingerprintClientHello(record) - if err != nil { - return nil, found, fmt.Errorf("fingerprint: %w", err) - } - raw, dst, err := p.dialAny(dsts) - if err != nil { - return nil, found, dialError{err} - } - found.Dst = dst - uc := utls.UClient(raw, &utls.Config{ServerName: sni, InsecureSkipVerify: true, OmitEmptyPsk: true, - DynamicRecordSizingDisabled: true}, utls.HelloCustom) - if err := uc.ApplyPreset(spec); err != nil { - raw.Close() - return nil, found, fmt.Errorf("apply: %w", err) - } - raw.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := uc.Handshake(); err != nil { - raw.Close() - return nil, found, fmt.Errorf("handshake: %w", err) - } - raw.SetDeadline(time.Time{}) - st := uc.ConnectionState() - found.ALPN, found.Version, found.Cipher = st.NegotiatedProtocol, fmt.Sprintf("%#04x", st.Version), - utls.CipherSuiteName(st.CipherSuite) - if len(st.PeerCertificates) == 0 { - found.VerifyErr = "no certificate" - return uc, found, nil - } - leaf := st.PeerCertificates[0] - found.NotBefore, found.NotAfter = leaf.NotBefore, leaf.NotAfter - for _, name := range leaf.DNSNames { - found.SANs = append(found.SANs, "DNS:"+name) - } - for _, ip := range leaf.IPAddresses { - found.SANs = append(found.SANs, "IP:"+ip.String()) - } - intermediates := x509.NewCertPool() - for _, c := range st.PeerCertificates[1:] { - intermediates.AddCert(c) - } - chains, err := leaf.Verify(x509.VerifyOptions{Roots: p.Roots, Intermediates: intermediates, DNSName: verifyName, - CurrentTime: time.Now()}) - if err != nil { - found.VerifyErr = err.Error() - } else { - found.Verified = true - found.NotBefore, found.NotAfter = chainWindow(chains) - } - return uc, found, nil -} - -// chainWindow is the dates every certificate of every verified chain is valid between: a client building any of -// those chains takes the site only then, so passing it through is safe only inside them. -func chainWindow(chains [][]*x509.Certificate) (time.Time, time.Time) { - var from, until time.Time - for _, chain := range chains { - for _, cert := range chain { - if from.IsZero() || cert.NotBefore.After(from) { - from = cert.NotBefore - } - if until.IsZero() || cert.NotAfter.Before(until) { - until = cert.NotAfter - } - } - } - return from, until -} - -// probe is what a handshake of the proxy's own found of one address of a site: the dates its verified chain shares, -// or that it didn't answer, or that its handshake didn't finish. It's cached per (address, SNI): a finished one for -// ProbeTTL, the others for ProbeFailedTTL, since they may be a moment's trouble. -type probe struct { - from, until time.Time - verified bool - // failed is a handshake that didn't finish; unreachable, an address that didn't answer. - failed, unreachable bool - at time.Time - problem string -} - -func (hit probe) fresh() bool { - if hit.failed || hit.unreachable { - return time.Since(hit.at) < ProbeFailedTTL - } - return time.Since(hit.at) < ProbeTTL -} - -// probes caches each address's probe, up to cacheLimit entries, the stale ones dropped first once it's full. An -// address's concurrent probes wait on the one under way; no lock is held while one is. -type probes struct { - mu sync.Mutex - cache map[string]probe - underway map[string]*underway -} - -type underway struct { - done chan struct{} - hit probe - ok bool -} - -// cacheLimit bounds the probe and leaf caches, so a computer that visits many sites keeps a bounded memory. -var cacheLimit = 4096 - -// dialError is a handshake's that never reached the site. -type dialError struct{ error } - -// probeAt is the probe of dst for sni, cached or new, and whether it probed now. -func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) { - key := dst + "|" + sni - p.probes.mu.Lock() - if p.probes.cache == nil { - p.probes.cache, p.probes.underway = map[string]probe{}, map[string]*underway{} - } - if hit, ok := p.probes.cache[key]; ok && hit.fresh() { - p.probes.mu.Unlock() - return hit, false - } - if running, ok := p.probes.underway[key]; ok { - p.probes.mu.Unlock() - <-running.done - if !running.ok { - return probe{at: time.Now(), failed: true, problem: "the probe it waited on failed"}, false - } - return running.hit, false - } - running := &underway{done: make(chan struct{})} - p.probes.underway[key] = running - p.probes.mu.Unlock() - defer func() { - p.probes.mu.Lock() - if running.ok { - p.store(key, running.hit) - } - delete(p.probes.underway, key) - p.probes.mu.Unlock() - close(running.done) - }() - - hit := probe{at: time.Now()} - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - switch { - case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), 200) - case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), 200) - case !found.Verified: - hit.problem = truncate(found.VerifyErr, 200) - default: - hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true - } - if uc != nil { - uc.NetConn().Close() - } - running.hit, running.ok = hit, true - return hit, true -} - -// store keeps hit for key, dropping the stale entries, then any others, once the cache is past cacheLimit. -func (p *Proxy) store(key string, hit probe) { - p.probes.cache[key] = hit - if len(p.probes.cache) <= cacheLimit { - return - } - for k, v := range p.probes.cache { - if !v.fresh() { - delete(p.probes.cache, k) - } - } - for k := range p.probes.cache { - if len(p.probes.cache) <= cacheLimit { - break - } - if k != key { - delete(p.probes.cache, k) - } - } -} - -// forget drops dst's probe for sni, for an address that stopped answering since. -func (p *Proxy) forget(dst, sni string) { - p.probes.mu.Lock() - delete(p.probes.cache, dst+"|"+sni) - p.probes.mu.Unlock() -} - -// inside is whether every universe lies inside [from + Margin, until - Margin]. -func inside(from, until time.Time, universes []float64) bool { - lo := float64(from.Add(Margin).UnixNano()) / 1e9 - hi := float64(until.Add(-Margin).UnixNano()) / 1e9 - for _, u := range universes { - if u < lo || u > hi { - return false - } - } - return true -} - -// dialAny dials each address in turn until one answers: which, or every one's error. -func (p *Proxy) dialAny(dsts []string) (net.Conn, string, error) { - var problems []string - for _, dst := range dsts { - conn, err := p.dial(dst) - if err == nil { - return conn, dst, nil - } - problems = append(problems, err.Error()) - } - if len(problems) == 0 { - return nil, "", errors.New("no address to dial") - } - return nil, "", errors.New(strings.Join(problems, "; ")) -} - -func (p *Proxy) dial(dst string) (net.Conn, error) { - if p.Dial != nil { - return p.Dial(dst) - } - return net.DialTimeout("tcp", dst, DialTimeout) -} - -func truncate(s string, n int) string { - if len(s) > n { - return s[:n] - } - return s -} diff --git a/src/agent_env/agents/agents/claude-code/root.conf b/src/agent_env/agents/agents/claude-code/root.conf deleted file mode 100644 index cfa21e733..000000000 --- a/src/agent_env/agents/agents/claude-code/root.conf +++ /dev/null @@ -1,62 +0,0 @@ -; Root's supervisord (start-agent, FD-5916): the clock follower and the egress proxy, each as its own user, on real -; time, and the A2A server as the agent's user. The server starts with the real-time marker, which agent-env's -; libfaketime takes out of its environment, so the server's own records keep real timestamps and everything it starts, -; the CLI and the model's commands, reads the env's time. Its socket and logs are root's alone, so the agent's user can -; neither stop nor restart any of them. The follower follows nothing until the server writes its config, which -; sync_env_clock has it do, then picks it up within a quarter second. -[supervisord] -nodaemon=true -user=root -logfile=/var/log/agentenv-root/supervisord.log -pidfile=/run/agentenv-root/supervisord.pid -childlogdir=/var/log/agentenv-root - -[unix_http_server] -file=/run/agentenv-root/supervisor.sock -chmod=0700 - -[rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[supervisorctl] -serverurl=unix:///run/agentenv-root/supervisor.sock - -[program:clock] -command=/usr/local/bin/python3 -I /usr/local/lib/agentenv-clock/clock-follower -user=agentenv-clock -directory=/ -priority=3 -autorestart=true -startsecs=1 -startretries=1000 -redirect_stderr=true -stdout_logfile=/var/log/agentenv-root/clock.log -environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" - -[program:egress] -command=/usr/local/lib/agentenv-egress/egress serve -user=agentenv-egress -directory=/ -priority=3 -autorestart=true -startsecs=1 -startretries=1000 -redirect_stderr=true -stdout_logfile=/var/log/agentenv-root/egress.log -environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" - -; uvicorn itself, not entrypoint.sh: the marker holds for the process it's given to alone, and a shell that execs -; uvicorn would hand uvicorn an environment without it. -[program:agent] -command=/usr/local/bin/python3 -m uvicorn a2a_server:app --host 0.0.0.0 --port %(ENV_A2A_PORT)s -user=agent -directory=/app -priority=10 -autorestart=true -stopasgroup=true -killasgroup=true -stopwaitsecs=30 -redirect_stderr=true -stdout_logfile=/dev/stdout -stdout_logfile_maxbytes=0 -environment=HOME="/home/agent",USER="agent",LOGNAME="agent",AGENTENV_REAL_TIME="1",AGENTENV_EGRESS_PROXY="http://127.0.0.1:3128",NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem",SSL_CERT_FILE="/etc/agentenv/ca-bundle.pem",REQUESTS_CA_BUNDLE="/etc/agentenv/ca-bundle.pem" diff --git a/src/agent_env/agents/agents/claude-code/start-agent b/src/agent_env/agents/agents/claude-code/start-agent deleted file mode 100755 index 1f170978d..000000000 --- a/src/agent_env/agents/agents/claude-code/start-agent +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh -# The image's start (FD-5916). As root, it makes what the agent's user must not touch, the clock page and file (the -# clock user's from their first byte) and the egress proxy's CA (its key the egress user's alone, trusted in the -# system store, agent-env's trust files and every certifi bundle), then starts root's supervisord (root.conf), which -# runs the clock follower and the egress proxy, each as its own user on real time, and the A2A server as the agent's -# user on real time, everything it starts reading the env's time from the page. Until sync_env_clock has the agent -# follow a clock, the page holds real time and the proxy relays TLS untouched. Started as another user, the image runs -# the A2A server alone, as before, on real time. -set -eu -if [ "$(id -u)" != 0 ]; then - exec /opt/agent/entrypoint.sh -fi -install -d -o root -g root -m 0700 /var/log/agentenv-root /run/agentenv-root -install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock -python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock -AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off \ - /usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \ - || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; } -exec supervisord -c /etc/agentenv/root.conf diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go index 031555627..e1ea67894 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go @@ -31,10 +31,8 @@ type bootPaths struct { NSS func(paths bootPaths) error } -// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in -// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. func defaultBootPaths() bootPaths { - p := bootPaths{ + return bootPaths{ Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, UpdateTrust: []string{"update-ca-certificates"}, @@ -43,13 +41,6 @@ func defaultBootPaths() bootPaths { EgressUser: EgressUser, SessionUser: SessionUser, NSS: sessionNSS, } - if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { - p.SessionUser = name - } - if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { - p.NSS = nil - } - return p } // boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go index c5f59ad0a..239d8db0a 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go @@ -86,14 +86,3 @@ func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { t.Fatalf("%v", report) } } - -func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { - if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { - t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) - } - t.Setenv("AGENTENV_SESSION_USER", "agent") - t.Setenv("AGENTENV_EGRESS_NSS", "off") - if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { - t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) - } -} diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index a722f9101..6c61f0c3b 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -92,7 +92,7 @@ def _armed(context: TaskStepContext, env_id: str) -> Optional[dict]: async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, timeout: float) -> list[dict]: """Have an agent ``deploy_agent`` just deployed follow the clock of each env registered with it that this run has armed, as ``sync_env_clock`` has every agent then: one that can't is reported and skipped, or fails the step under - that step's ``tolerate_missing_sync_time = False``.""" + that step's ``require_agent_clock``.""" outcomes = [] for env_id in agent.env_ids: if (armed := _armed(context, env_id)) is None: @@ -102,7 +102,7 @@ async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, continue outcome = await follow(agent, env, env_get_time_url=armed.get("env_get_time_url"), timezone=armed.get("timezone"), timeout=timeout) - if not outcome["synced"] and not armed.get("tolerate_missing_sync_time", True): + if not outcome["synced"] and armed.get("require_agent_clock", False): raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env '{env_id}': " f"{outcome['reason']}") logger.info(f"agent '{agent.agent_name}' on env {env_id}'s clock: {outcome}") diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index 4e3d1a823..f9b783972 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -6,7 +6,8 @@ An env whose card offers no set_time has no gateway clock to arm, as a computer deployed alone, and is refused saying to deploy it in a multi env; a record that kept no card is refused saying so. Then has every agent registered with the env whose card serves clock/v1 sync_time follow the clock as a computer does -(``agent_clock``), so every program it runs reads the env's time and later clock writes can wait for it. +(``agent_clock``), so every program it runs reads the env's time and later clock writes can wait for it. An agent that +can't is skipped, as every agent is until its image runs the follower, or fails the step with require_agent_clock. Non-advertisers are skipped when tolerate_missing_sync_time (default True), and websites always. Place right before prompt_agent so the virtual timeline starts at agent-start; when setup needs the clock armed, arm it at rate 0 and start it with set_env_clock_rate right before prompt_agent. @@ -77,13 +78,17 @@ def __init__( fail_task_on_error: bool = True, timeout_seconds: int = 30, timezone: Optional[str] = None, + require_agent_clock: bool = False, ): """virtual_seconds_per_real_second = clock speed (1.0 real time, 0 frozen, 86400 cap = 1 real s -> 1 virtual day). For ~V virtual seconds over an ~R-real-second task, pass V/R. timezone = optional IANA name. Unset keeps today's behaviour. Set, a wall-clock virtual_time is local time in that zone, the gateway's clock is armed in it, so its get_time answers in it too, and every synced server is - told the zone (a server that cannot take it fails the step).""" + told the zone (a server that cannot take it fails the step). + + require_agent_clock = fail the step when an agent registered with the env can't follow its clock, rather than + skip it; apart from tolerate_missing_sync_time, which is about the env's servers.""" super().__init__(id, version, depends_on=depends_on, fail_task_on_error=fail_task_on_error) if not isinstance(virtual_time, str): raise ValueError("virtual_time must be an RFC3339 string") @@ -91,6 +96,7 @@ def __init__( self.virtual_time = virtual_time self.virtual_seconds_per_real_second = virtual_seconds_per_real_second self.tolerate_missing_sync_time = tolerate_missing_sync_time + self.require_agent_clock = require_agent_clock self.timeout_seconds = timeout_seconds self.timezone = timezone.strip() if isinstance(timezone, str) else timezone self.armed_virtual_time = resolve_virtual_time(virtual_time, self.timezone) @@ -104,6 +110,8 @@ def to_dict(self) -> dict: base["timeout_seconds"] = self.timeout_seconds if self.timezone is not None: base["timezone"] = self.timezone + if self.require_agent_clock: + base["require_agent_clock"] = True return base @classmethod @@ -116,6 +124,7 @@ def from_dict(cls, data: dict) -> SyncEnvClockTaskStep: tolerate_missing_sync_time=data.get("tolerate_missing_sync_time", True), timeout_seconds=data.get("timeout_seconds", 30), timezone=data.get("timezone"), + require_agent_clock=data.get("require_agent_clock", False), ) async def _backing_servers(self, deployed) -> list[BackingServer]: @@ -167,7 +176,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: "step_id": self.id, "env_id": self.env_id, "virtual_time": self.virtual_time, "virtual_seconds_per_real_second": self.virtual_seconds_per_real_second, "env_get_time_url": env_get_time_url, "synced": synced, "skipped": skipped, "agents": agents, - "tolerate_missing_sync_time": self.tolerate_missing_sync_time, + "require_agent_clock": self.require_agent_clock, } if self.timezone is not None: configuration.update(timezone=self.timezone, armed_at=self.armed_virtual_time, @@ -183,7 +192,7 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: """Have every agent registered with the env follow its clock (``agent_clock.follow``): one that can't is - reported and skipped, or fails the step when ``tolerate_missing_sync_time`` is false, as a server is.""" + reported and skipped, or fails the step under ``require_agent_clock``.""" from agent_env.task_step.task_steps.agent_clock import follow outcomes = [] @@ -193,7 +202,7 @@ async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_ outcome = await follow(agent, deployed, env_get_time_url=env_get_time_url, timezone=self.timezone, timeout=self.timeout_seconds) if not outcome["synced"]: - if not self.tolerate_missing_sync_time: + if self.require_agent_clock: raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env " f"'{self.env_id}': {outcome['reason']}") logger.info(f"sync_env_clock: agent '{agent.agent_name}' doesn't follow the clock " diff --git a/tst/unit/agents/claude_code/test_env_clock_image.py b/tst/unit/agents/claude_code/test_env_clock_image.py deleted file mode 100644 index 8bef2b3c1..000000000 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ /dev/null @@ -1,66 +0,0 @@ -"""Claude Code on the env's clock (FD-5916): the image runs the computer base's clock follower and egress proxy, each -as its own user, and agent-env's libfaketime for the agent's user, so the CLI and every command the model runs read -the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. The CLI goes out through -the egress proxy, and to the env's MCP servers directly.""" - -from __future__ import annotations - -import re - -import pytest - -from tst.unit.agents.claude_code.agent import AGENT_DIR, claude_bridge - -DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() -ROOT_CONF = (AGENT_DIR / "root.conf").read_text() -BASE = AGENT_DIR.parents[2] / "computers" / "artifacts" / "ubuntu-24.04" -PROXY = "http://127.0.0.1:3128" - - -@pytest.mark.parametrize("folder", ["clock", "egress"]) -def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(folder): - copied = sorted(p.relative_to(AGENT_DIR / folder) for p in (AGENT_DIR / folder).rglob("*") if p.is_file()) - - assert copied - assert [str(p) for p in copied if (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] - - -def _program(name: str) -> dict[str, str]: - section = re.search(rf"^\[program:{name}\]\n(.*?)(?=^\[|\Z)", ROOT_CONF, re.MULTILINE | re.DOTALL).group(1) - return dict(line.split("=", 1) for line in section.splitlines() if "=" in line and not line.startswith(";")) - - -def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as_the_agents_on_real_time(): - assert (_program("clock")["user"], _program("egress")["user"]) == ("agentenv-clock", "agentenv-egress") - agent = _program("agent") - assert agent["user"] == "agent" - # uvicorn itself, so the marker reaches the server and not a shell that drops it on exec. - assert agent["command"].startswith("/usr/local/bin/python3 -m uvicorn a2a_server:app") - assert 'AGENTENV_REAL_TIME="1"' in agent["environment"] - assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in agent["environment"] - assert 'NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem"' in agent["environment"] - - -def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(): - assert "> /etc/ld.so.preload" in DOCKERFILE and "libfaketimeMT.so.1" in DOCKERFILE - assert 's/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/' in DOCKERFILE - assert "install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock" in DOCKERFILE - assert not re.search(r"^USER ", DOCKERFILE, re.MULTILINE) - - -def test_started_as_another_user_the_image_runs_the_server_alone_as_before(): - start = (AGENT_DIR / "start-agent").read_text() - assert 'if [ "$(id -u)" != 0 ]; then\n exec /opt/agent/entrypoint.sh' in start - assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start - - -def test_where_the_image_runs_the_proxy_the_cli_goes_out_through_it_and_to_the_envs_servers_directly(): - env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"]) - - assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY} - assert env["NO_PROXY"] == env["no_proxy"] == "localhost,127.0.0.1,::1,host.docker.internal,gateway" - assert env["NODE_USE_ENV_PROXY"] == "1" - - -def test_without_the_proxy_the_cli_is_run_as_before(): - assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env({})) diff --git a/tst/unit/agents/claude_code/test_image_contract.py b/tst/unit/agents/claude_code/test_image_contract.py index 3dc316cc5..c8e528df3 100644 --- a/tst/unit/agents/claude_code/test_image_contract.py +++ b/tst/unit/agents/claude_code/test_image_contract.py @@ -17,8 +17,7 @@ def test_every_file_the_image_copies_is_in_the_folder(): copied = [name for line in re.findall(r"^COPY (?!--from=)(.+) \S+$", DOCKERFILE, re.MULTILINE) for name in line.split()] - assert copied and all((AGENT_DIR / name).is_file() or (name.endswith("/") and (AGENT_DIR / name).is_dir()) - for name in copied), copied + assert copied and all((AGENT_DIR / name).is_file() for name in copied), copied # install.sh and run.sh are install/v1's; a build context pruned to what the Dockerfile reads keeps them so. assert {"a2a_server.py", "claude_bridge.py", "kit.py", "tar_changelog.py", "entrypoint.sh", "install.sh", "run.sh"} <= set(copied) @@ -55,5 +54,5 @@ def test_the_image_pins_its_cli_checks_its_checksum_and_keeps_tls_on(): def test_the_image_starts_through_cmd_which_modal_passes_its_env_to(): - assert 'CMD ["/usr/local/bin/start-agent"]' in DOCKERFILE + assert 'CMD ["/opt/agent/entrypoint.sh"]' in DOCKERFILE assert not re.search(r"^ENTRYPOINT", DOCKERFILE, re.MULTILINE) diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 78b765b8e..340de33d3 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -1,6 +1,7 @@ """Agents on the env's clock: once sync_env_clock has armed it, every agent registered with the env whose card serves clock/v1 sync_time is declared at the gateway as a follower with a token of its own and handed the follower's config; -one that doesn't is reported and skipped, or fails the step when tolerate_missing_sync_time is false; and an agent +one that doesn't is reported and skipped, even when the env's servers must follow (tolerate_missing_sync_time false), +or fails the step under require_agent_clock; and an agent deploy_agent deploys while the clock is armed follows the same way.""" import json from types import SimpleNamespace @@ -80,8 +81,11 @@ async def test_an_agent_that_cant_follow_is_reported_and_skipped_or_fails_a_stri assert context.metadata["clock_configurations"][0]["agents"] == [{"agent": "solver", "synced": False, "reason": reason}] assert [r.url.path for r in sent][3:] == [] + strict = await _context(_agent(sync_time=sync_time)) + await _step(tolerate_missing_sync_time=False).execute(strict) + assert strict.metadata["clock_configurations"][0]["agents"][0]["synced"] is False with pytest.raises(StepContextError, match=f"agent 'solver' can't follow the clock of env 'e1': {reason}"): - await _step(tolerate_missing_sync_time=False).execute(await _context(_agent(sync_time=sync_time))) + await _step(require_agent_clock=True).execute(await _context(_agent(sync_time=sync_time))) @pytest.mark.asyncio @@ -124,7 +128,7 @@ async def test_an_agent_deployed_while_the_clock_is_armed_follows_it(monkeypatch context = await _context() context.metadata["clock_configurations"] = [ {"step_id": "sync", "env_id": "e1", "virtual_time": T0, "env_get_time_url": CLOCK_URL, "timezone": "Asia/Tokyo", - "tolerate_missing_sync_time": True}, + "require_agent_clock": False}, {"step_id": "rate", "env_id": "e1", "virtual_seconds_per_real_second": 60, "virtual_time": T0}, ] late = _agent("late") @@ -145,10 +149,19 @@ async def test_an_agent_deployed_before_any_clock_is_armed_is_left_alone(monkeyp @pytest.mark.asyncio -async def test_a_late_agent_that_cant_follow_fails_a_deploy_under_a_strict_sync(monkeypatch): +async def test_a_late_agent_that_cant_follow_fails_a_deploy_only_under_require_agent_clock(monkeypatch): _mock_http(monkeypatch) context = await _context() - context.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL, - "tolerate_missing_sync_time": False}] + context.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL}] + [skipped] = await agent_clock.follow_armed_clocks(context, _agent("late", sync_time=None), timeout=5) + assert (skipped["synced"], skipped["reason"]) == (False, "not_advertised") + context.metadata["clock_configurations"][0]["require_agent_clock"] = True with pytest.raises(StepContextError, match="agent 'late' can't follow the clock of env 'e1': not_advertised"): await agent_clock.follow_armed_clocks(context, _agent("late", sync_time=None), timeout=5) + + +def test_require_agent_clock_round_trips_and_is_left_out_when_off(): + assert "require_agent_clock" not in _step().to_dict() + strict = _step(require_agent_clock=True).to_dict() + assert strict["require_agent_clock"] is True + assert SyncEnvClockTaskStep.from_dict(strict).require_agent_clock is True From 182cef0ff3c54be05cd51ff7cad68a777da6ff06 Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:14:39 -0800 Subject: [PATCH 03/29] feat(agents): the Claude Code image follows the env's clock (FD-5916) The image half of the agent clock: the computer base's clock follower and egress proxy, copied in until FD-5700, libfaketime preloaded for the agent's user, root's supervisord starting them and the A2A server, and the CLI sent through the proxy. Needs #714 and an agentenv-protocol release with CLOCK_V1. Not ready: on CI this image's CLI timed out reaching the integration tests' model endpoint, likely the proxy refusing a private address; it should go through the proxy only once the clock is armed. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/agent_env/agents/README.md | 13 + .../agents/agents/claude-code/Dockerfile | 44 +- .../agents/claude-code/claude_bridge.py | 29 +- .../agents/agents/claude-code/clock/COPYING | 339 ++++ .../claude-code/clock/agentenv_clock_page.c | 320 ++++ .../claude-code/clock/agentenv_clock_waits.c | 1175 +++++++++++++ .../agents/agents/claude-code/clock/build.sh | 22 + .../agents/claude-code/clock/clock-follower | 204 +++ .../agents/claude-code/clock/clock.conf | 11 + .../agents/claude-code/clock/clockpage.py | 157 ++ .../claude-code/clock/libfaketime.patch | 1527 +++++++++++++++++ .../agents/claude-code/clock/tls_realtime.c | 115 ++ .../agents/agents/claude-code/egress/boot.go | 294 ++++ .../agents/claude-code/egress/boot_test.go | 99 ++ .../agents/agents/claude-code/egress/clock.go | 151 ++ .../agents/claude-code/egress/clock_test.go | 112 ++ .../agents/agents/claude-code/egress/go.mod | 12 + .../agents/agents/claude-code/egress/go.sum | 10 + .../agents/agents/claude-code/egress/hello.go | 164 ++ .../agents/claude-code/egress/hello_test.go | 82 + .../agents/claude-code/egress/leaves.go | 188 ++ .../agents/agents/claude-code/egress/main.go | 92 + .../agents/agents/claude-code/egress/proxy.go | 598 +++++++ .../agents/claude-code/egress/proxy_test.go | 564 ++++++ .../egress/testdata/page-armed.hex | 1 + .../egress/testdata/page-unarmed.hex | 1 + .../agents/claude-code/egress/upstream.go | 268 +++ .../agents/agents/claude-code/root.conf | 62 + .../agents/agents/claude-code/start-agent | 19 + .../artifacts/ubuntu-24.04/egress/boot.go | 11 +- .../ubuntu-24.04/egress/boot_test.go | 11 + .../claude_code/test_env_clock_image.py | 66 + .../agents/claude_code/test_image_contract.py | 5 +- 33 files changed, 6756 insertions(+), 10 deletions(-) create mode 100644 src/agent_env/agents/agents/claude-code/clock/COPYING create mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c create mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c create mode 100755 src/agent_env/agents/agents/claude-code/clock/build.sh create mode 100755 src/agent_env/agents/agents/claude-code/clock/clock-follower create mode 100644 src/agent_env/agents/agents/claude-code/clock/clock.conf create mode 100644 src/agent_env/agents/agents/claude-code/clock/clockpage.py create mode 100644 src/agent_env/agents/agents/claude-code/clock/libfaketime.patch create mode 100644 src/agent_env/agents/agents/claude-code/clock/tls_realtime.c create mode 100644 src/agent_env/agents/agents/claude-code/egress/boot.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/boot_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/clock.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/clock_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/go.mod create mode 100644 src/agent_env/agents/agents/claude-code/egress/go.sum create mode 100644 src/agent_env/agents/agents/claude-code/egress/hello.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/hello_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/leaves.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/main.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy_test.go create mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex create mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex create mode 100644 src/agent_env/agents/agents/claude-code/egress/upstream.go create mode 100644 src/agent_env/agents/agents/claude-code/root.conf create mode 100755 src/agent_env/agents/agents/claude-code/start-agent create mode 100644 tst/unit/agents/claude_code/test_env_clock_image.py diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index f4041d557..60e1221bf 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -34,6 +34,19 @@ turn off becomes `--disallowedTools`, which keeps the CLI's defaults for the res `claude_bridge.py`: every tool the pinned CLI has, though which it offers by default depends on the model. A test fails when the Dockerfile's `CLAUDE_CODE_VERSION` moves, until `ClaudeCodeTool` and `TOOLS_CLI_VERSION` move with it. +The image follows a task env's clock the way a computer does (FD-5916): it runs the computer base's clock follower +and egress proxy, copied into `clock/` and `egress/` until the clock library image (FD-5700) ships them, each as its own +user under root's supervisord (`start-agent`, `root.conf`), with agent-env's libfaketime preloaded for the agent's user. +The A2A server starts with the real-time marker, so it and its records stay on real time, and serves clock/v1's +`sync_time`, which the protocol's framework serves on the card of any agent in an image with the follower +(`agentenv_protocol.a2a_agent.clock`). Once `sync_env_clock` has armed the clock, it declares the agent at the gateway +as a follower with a read token of its own and calls that `sync_time` with the follower's config, which the server +writes; `deploy_agent` does the same for an agent it deploys while the clock is armed. From then on the CLI, its own +"Today's date is" line included, and every command the model runs read the env's time, and a clock write that waits for +followers waits for the agent. The CLI goes out through the egress proxy, which keeps a live site's TLS working at any +date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time and the proxy relays TLS +untouched. Started as a user other than root, the image runs the A2A server alone, on real time, as before. + `install_agent` installs it too, into a container a task already runs or onto a `vm` sandbox's Linux host, with `install.sh` from its folder: Claude Code's native build for the target's CPU and libc, uv, and the agent's requirements in a Python of its own, so the target's Python is left alone. The CLI runs its commands in bash or zsh, so a target with diff --git a/src/agent_env/agents/agents/claude-code/Dockerfile b/src/agent_env/agents/agents/claude-code/Dockerfile index cbe95d6e6..789bfd983 100644 --- a/src/agent_env/agents/agents/claude-code/Dockerfile +++ b/src/agent_env/agents/agents/claude-code/Dockerfile @@ -1,6 +1,20 @@ # Node for the scripts the model runs, without npm or npx; Claude Code itself is a native build. FROM public.ecr.aws/docker/library/node:22-slim@sha256:c3de60bf2f9dd0ac6370e6117950ff62d6e339527e7472301c9c78a017978392 AS node +# The env's clock (FD-5916): the computer base's pieces, copied in (clock/, egress/) until the clock library image +# (FD-5700) ships them for each libc. The egress proxy, one static Go binary, and agent-env's libfaketime and its TLS +# shim, built for this image's libc (Debian bookworm) in stages of their own so no toolchain is left in the image. +FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress +COPY egress/ /egress/ +RUN cd /egress && go vet ./... && go test -count=1 ./... \ + && CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags=-s -o /out/egress . + +FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS clock +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + build-essential patch curl ca-certificates +COPY clock/ /clock/ +RUN sh /clock/build.sh /out + FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 # poppler-utils: the CLI's Read tool runs pdftoppm for a PDF's pages. RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl poppler-utils \ @@ -24,13 +38,35 @@ COPY requirements.txt model-requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -r model-requirements.txt # install.sh and run.sh are install/v1's: in the image, so they stay in its build context. COPY a2a_server.py claude_bridge.py kit.py tar_changelog.py entrypoint.sh install.sh run.sh ./ -# The CLI's cwd, /app, and its state, fixed so neither follows the uid: Modal runs the image as root. +# The CLI's cwd, /app, and its state, fixed so neither follows the uid. RUN useradd --create-home --uid 10001 agent && mkdir -p /app /home/agent/.claude \ && chown agent:agent /app /home/agent/.claude && chmod 0755 entrypoint.sh +# The env's clock: the follower and the egress proxy, each as its own user, under root's supervisord (root.conf), and +# agent-env's libfaketime and its TLS shim preloaded into every process, faking only the agent's (clock.conf). The +# follower's config is the agent's user's to write (agentenv_protocol.a2a_agent.clock) and the follower's to read. +RUN pip install --no-cache-dir supervisor==4.2.5 \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock \ + && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress \ + && mkdir -p /usr/local/lib/agentenv-clock /usr/local/lib/agentenv-egress /etc/agentenv \ + && install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock \ + && touch /etc/agentenv/ca.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \ + && cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem +COPY --from=clock /out/libfaketimeMT.so.1 /out/libtls_realtime.so /usr/local/lib/agentenv-clock/ +COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress +COPY clock/clockpage.py clock/clock-follower /usr/local/lib/agentenv-clock/ +COPY clock/clock.conf /etc/agentenv/clock.conf +COPY root.conf /etc/agentenv/root.conf +COPY start-agent /usr/local/bin/start-agent +RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/" \ + -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf \ + && chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/bin/start-agent \ + && printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ + > /etc/ld.so.preload ENV A2A_PORT=8000 PYTHONPATH=/opt/agent CLAUDE_CONFIG_DIR=/home/agent/.claude \ DISABLE_AUTOUPDATER=1 CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 -USER agent WORKDIR /app EXPOSE 8000 -# CMD rather than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. -CMD ["/opt/agent/entrypoint.sh"] +# Starts as root, which start-agent needs for the clock; the A2A server and the CLI run as the agent's user. CMD rather +# than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only. +CMD ["/usr/local/bin/start-agent"] 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 e8fcfc651..aabc2ec9b 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -12,11 +12,12 @@ import signal import stat import time -from collections.abc import Callable, Mapping +from collections.abc import Callable, Iterable, Mapping from dataclasses import dataclass, field, replace from functools import partial from pathlib import Path from typing import Literal +from urllib.parse import urlsplit from pydantic import BaseModel, ConfigDict, model_validator @@ -173,8 +174,27 @@ def _model_headers(raw: str | None) -> dict[str, str]: return {str(name): str(value) for name, value in headers.items()} +#: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it. +EGRESS_PROXY_ENV = "AGENTENV_EGRESS_PROXY" +_DIRECT_HOSTS = ("localhost", "127.0.0.1", "::1", "host.docker.internal") + + +def egress_env(environ: Mapping[str, str], direct: Iterable[str] = ()) -> dict[str, str]: + """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916): they read the env's time, + so a client checking a live site's certificate against it would refuse the site once the clock is months off, and + the proxy, on real time, hands it a certificate that holds then. The env's MCP servers (``direct``) are reached + directly: the proxy refuses an address that isn't global, as a local or private gateway's is.""" + proxy = environ.get(EGRESS_PROXY_ENV) + if not proxy: + return {} + hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, direct)])) + return {**{name: proxy for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY") for name in (base, base.lower())}, + "NO_PROXY": hosts, "no_proxy": hosts, "NODE_USE_ENV_PROXY": "1"} + + def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: str | None = None, - task_id: str | None = None, max_thinking_tokens: int | None = None) -> dict[str, str]: + task_id: str | None = None, max_thinking_tokens: int | None = None, + direct_hosts: Iterable[str] = ()) -> dict[str, str]: """The CLI's environment: this process's, pointed at the endpoint agent-env configured (its key as a bearer token, so an API key too would conflict), with the endpoint's headers and the run's attribution, and the model's aliases pinned to ``model`` on that endpoint. A variable already set is kept, LITELLM_* too, so a script the model @@ -203,6 +223,7 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" + env.update(egress_env(environ, direct_hosts)) return env @@ -682,7 +703,9 @@ async def _spawn_claude( stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=cli_env(os.environ, model=model, project_id=project_id, task_id=task_id, - max_thinking_tokens=max_thinking_tokens), + max_thinking_tokens=max_thinking_tokens, + direct_hosts=[urlsplit(str(entry.get("url") or "")).hostname or "" + for entry in (mcp_servers or {}).values()]), cwd=WORKSPACE_DIR, start_new_session=True, )) diff --git a/src/agent_env/agents/agents/claude-code/clock/COPYING b/src/agent_env/agents/agents/claude-code/clock/COPYING new file mode 100644 index 000000000..d159169d1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c new file mode 100644 index 000000000..9167f7ede --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c @@ -0,0 +1,320 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * agent-env's additions to libfaketime 0.9.12, part 1, included by libfaketime.c (libfaketime.patch) before its + * time functions: the process's settings, read once at load from /etc/agentenv/clock.conf, or from the environment + * where there is no such file, so no getenv() runs after main() starts; who is faked (the session user, unless started with AGENTENV_REAL_TIME=1, a marker + * taken out of the environment block in place); and the clock page the follower writes, read without a cache. + * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). + */ +/* + * configuration is read once, when the library loads, from AGENTENV_CLOCK_CONF (NAME=VALUE lines) when that + * file exists, else from the environment, and never with getenv() afterwards: a getenv() racing a program's setenv() + * reads a freed environ array, which is how libfaketime's pthread_cond_timedwait crashed Chromium. + */ +#ifndef AGENTENV_CLOCK_CONF +#define AGENTENV_CLOCK_CONF "/etc/agentenv/clock.conf" +#endif +#define FT_CONF_MAX 64 +static char ft_conf_text[8192]; +static const char *ft_conf_name[FT_CONF_MAX]; +static const char *ft_conf_value[FT_CONF_MAX]; +static int ft_conf_count = 0; +static bool ft_conf_loaded = false; +/* this process stays on real time: another user's than AGENTENV_SESSION_UID, or started with + AGENTENV_REAL_TIME=1. */ +static bool ft_disabled = false; +/* bumped with each new time specification parsed, so a wait sees the clock change under it. */ +static volatile unsigned long ft_spec_gen = 0; +static __thread unsigned long ft_tl_gen = 0; +/* real seconds per fake second for what is left of a wait, as of this thread's last clock read: 1 / rate, + or 1 for a frozen clock. */ +static __thread double ft_tl_scale = 1.0; +/* how long a wait that can be cut sleeps before it looks at the clock again (AGENTENV_WAIT_SLICE_MS). */ +static long ft_wait_slice_ns = 100000000L; + +static void ft_conf_add(char *name, char *value) +{ + if (ft_conf_count < FT_CONF_MAX) + { + ft_conf_name[ft_conf_count] = name; + ft_conf_value[ft_conf_count] = value; + ft_conf_count++; + } +} + +static void ft_conf_load(void) +{ + extern char **environ; + size_t used = 0; + int fd; + if (ft_conf_loaded) return; + ft_conf_loaded = true; + fd = open(AGENTENV_CLOCK_CONF, O_RDONLY | O_CLOEXEC); + if (fd >= 0) + { + ssize_t n; + char *line; + while (used < sizeof(ft_conf_text) - 1 && + (n = read(fd, ft_conf_text + used, sizeof(ft_conf_text) - 1 - used)) > 0) + used += n; + close(fd); + ft_conf_text[used] = 0; + line = ft_conf_text; + while (line != NULL && *line != 0) + { + char *next = strchr(line, '\n'); + char *eq; + if (next != NULL) *next++ = 0; + eq = strchr(line, '='); + if (line[0] != '#' && eq != NULL && eq != line) + { + *eq = 0; + ft_conf_add(line, eq + 1); + } + line = next; + } + return; + } + for (char **e = environ; e != NULL && *e != NULL; e++) + { + const char *s = *e; + size_t len; + char *copy, *eq; + if (strncmp(s, "FAKE", 4) && strncmp(s, "NO_FAKE", 7) && strncmp(s, "DONT_FAKE", 9) && strncmp(s, "HOME=", 5) + && strncmp(s, "AGENTENV_", 9)) + continue; + len = strlen(s); + if (used + len + 1 > sizeof(ft_conf_text)) break; + copy = ft_conf_text + used; + memcpy(copy, s, len + 1); + used += len + 1; + eq = strchr(copy, '='); + if (eq == NULL) continue; + *eq = 0; + ft_conf_add(copy, eq + 1); + } +} + +static char *ft_getenv(const char *name) +{ + ft_conf_load(); + for (int i = 0; i < ft_conf_count; i++) + { + if (strcmp(ft_conf_name[i], name) == 0) return (char *) ft_conf_value[i]; + } + return NULL; +} + +/* + * AGENTENV_REAL_TIME=1 taken out of the original environment block in place: its pointer out of the array + * (which is main()'s envp too) and the string itself changed to "=0", so no copy of it is the marker. unsetenv() isn't + * used: a program may define its own (bash does, and before its main() it does nothing). + */ +static void ft_scrub_marker(void) +{ + extern char **environ; + char **e = environ; + if (e == NULL) return; + while (*e != NULL) + { + if (strcmp(*e, "AGENTENV_REAL_TIME=1") == 0) + { + char **d = e; + (*e)[sizeof("AGENTENV_REAL_TIME=") - 1] = '0'; + do d[0] = d[1]; while (*d++ != NULL); + } + else + e++; + } +} + +/* whether a wait's deadline is one to translate: a valid one, in a faked process, outside libfaketime. */ +static bool ft_translates_wait(const struct timespec *abstime) +{ + return abstime != NULL && !ft_disabled && !dont_fake && abstime->tv_nsec >= 0 && abstime->tv_nsec < SEC_TO_nSEC; +} + +/* + * where a CLOCK_REALTIME deadline in fake time falls on a real clock (CLOCK_MONOTONIC or CLOCK_REALTIME): + * now, plus what is left of the fake interval times ft_tl_scale; so a frozen clock counts what is left as real time and + * nothing waits forever, and a deadline already past is now. + */ +static void ft_deadline_from_fake(clockid_t clock, const struct timespec *fake_abs, struct timespec *out) +{ + struct timespec now, real_now, fake_now, left, scaled; + DONT_FAKE_TIME((*real_clock_gettime)(clock, &now)); + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &real_now)); + fake_now = real_now; + (void) fake_clock_gettime(CLOCK_REALTIME, &fake_now); + *out = now; + timespecsub(fake_abs, &fake_now, &left); + if (left.tv_sec < 0) return; + if (left.tv_sec > 100L * 365 * 86400) left.tv_sec = 100L * 365 * 86400; + if (ft_tl_scale != 1.0) + { + timespecmul(&left, ft_tl_scale, &scaled); + } + else + { + scaled = left; + } + timespecadd(&now, &scaled, out); +} + +/* the end of a wait's next slice on CLOCK_MONOTONIC; true when that is the deadline itself. */ +static bool ft_slice_end(const struct timespec *mono_abs, struct timespec *slice) +{ + struct timespec now, step = {ft_wait_slice_ns / SEC_TO_nSEC, ft_wait_slice_ns % SEC_TO_nSEC}; + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &now)); + timespecadd(&now, &step, slice); + if (timespeccmp(slice, mono_abs, <)) return false; + *slice = *mono_abs; + return true; +} + +/* after a slice, the clock read again (the file, with no cache); a new specification since ``gen`` moves the + deadline. True when it moved. */ +static bool ft_deadline_moved(const struct timespec *fake_abs, struct timespec *mono_abs, unsigned long *gen) +{ + struct timespec now; + DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &now)); + (void) fake_clock_gettime(CLOCK_REALTIME, &now); + if (ft_tl_gen == *gen) return false; + *gen = ft_tl_gen; + ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, mono_abs); + return true; +} + +/* + * the clock page. 64 bytes, little-endian, rewritten in place by the follower: seq odd while it writes, + * then even; check is the xor of the other words and a constant. A realtime read in a faked process copies it + * lock-free and computes universe + (now - anchor) * rate (rate 0: universe), so every process agrees with no shared + * start; the first thread to see a new generation also hands it to libfaketime's own state (rates of sleeps, the + * time an absolute spec gives) under the MT lock. The page must be owned by AGENTENV_CLOCK_PAGE_UID (0 by default) and + * writable by no one else, so the session user can't plant one. + */ +struct ft_clock_page +{ + uint32_t magic, version; + uint64_t seq; + int64_t universe_ns, anchor_ns; + double rate; + uint32_t armed, reserved; + uint64_t generation, check; + int64_t mono_universe_ns, mono_anchor_ns; +}; +#define FT_PAGE_MAGIC 0x4B434541u +#define FT_PAGE_VERSION 2u +static const volatile struct ft_clock_page *ft_page = NULL; +static uint64_t ft_page_applied = 0; +static time_t ft_page_last_try = 0; +static void parse_ft_string(const char *user_faked_time); + +static uint64_t ft_page_check(const struct ft_clock_page *p) +{ + uint64_t rate_bits; + memcpy(&rate_bits, &p->rate, sizeof rate_bits); + return p->seq ^ (uint64_t) p->universe_ns ^ (uint64_t) p->anchor_ns ^ rate_bits ^ p->armed ^ p->generation + ^ (uint64_t) p->mono_universe_ns ^ (uint64_t) p->mono_anchor_ns ^ 0x9E3779B97F4A7C15ull; +} + +static bool ft_page_snapshot(struct ft_clock_page *out) +{ + for (int i = 0; i < 1000; i++) + { + uint64_t s1 = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); + if (s1 & 1) continue; + out->magic = ft_page->magic; + out->seq = s1; + out->universe_ns = ft_page->universe_ns; + out->anchor_ns = ft_page->anchor_ns; + out->rate = ft_page->rate; + out->armed = ft_page->armed; + out->version = ft_page->version; + out->generation = ft_page->generation; + out->check = ft_page->check; + out->mono_universe_ns = ft_page->mono_universe_ns; + out->mono_anchor_ns = ft_page->mono_anchor_ns; + __atomic_thread_fence(__ATOMIC_ACQUIRE); + if (__atomic_load_n(&ft_page->seq, __ATOMIC_RELAXED) == s1 && out->magic == FT_PAGE_MAGIC + && out->version == FT_PAGE_VERSION + && ft_page_check(out) == out->check) + return true; + } + return false; +} + +static void ft_page_open(void) +{ + const char *path = ft_getenv("AGENTENV_CLOCK_PAGE"); + const char *owner = ft_getenv("AGENTENV_CLOCK_PAGE_UID"); + struct stat st; + void *map; + int fd; + if (path == NULL || *path == 0) return; + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return; + if (fstat(fd, &st) != 0 || st.st_size < (off_t) sizeof(struct ft_clock_page) + || st.st_uid != (uid_t) (owner ? strtoul(owner, NULL, 10) : 0) || (st.st_mode & 022) != 0) + { + close(fd); + return; + } + map = mmap(NULL, sizeof(struct ft_clock_page), PROT_READ, MAP_SHARED, fd, 0); + close(fd); + if (map == MAP_FAILED) return; + if (((const struct ft_clock_page *) map)->magic != FT_PAGE_MAGIC + || ((const struct ft_clock_page *) map)->version != FT_PAGE_VERSION) + { + munmap(map, sizeof(struct ft_clock_page)); + return; + } + ft_page = map; +} + +/* libfaketime's own state set to the page's clock; called with the MT lock held. */ +static void ft_page_apply_locked(const struct ft_clock_page *s) +{ + char spec[160]; + if (s->generation == ft_page_applied) return; + if (!s->armed) + snprintf(spec, sizeof spec, "+0 x1"); + else + snprintf(spec, sizeof spec, "@%lld.%09lld x%.17g ^%lld.%09lld", + (long long) (s->universe_ns / 1000000000LL), (long long) (s->universe_ns % 1000000000LL), s->rate, + (long long) (s->anchor_ns / 1000000000LL), (long long) (s->anchor_ns % 1000000000LL)); + parse_ft_string(spec); + __atomic_store_n(&ft_page_applied, s->generation, __ATOMIC_RELEASE); +} + +/* "%s" times are read as a number, without strptime() and mktime(), which reads TZ with getenv(). */ +static const char *ft_parse_epoch(const char *s, struct timespec *out) +{ + char *end; + long long sec; + long nsec = 0, scale = 100000000L; + bool negative; + while (*s == ' ') s++; + negative = (*s == '-'); + sec = strtoll(s, &end, 10); + if (end == s) return NULL; + if (*end == '.') + { + end++; + while (*end >= '0' && *end <= '9') + { + nsec += (*end - '0') * scale; + scale /= 10; + end++; + } + } + if (negative && nsec > 0) + { + sec -= 1; + nsec = SEC_TO_nSEC - nsec; + } + out->tv_sec = (time_t) sec; + out->tv_nsec = nsec; + return end; +} + diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c new file mode 100644 index 000000000..5d8237705 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c @@ -0,0 +1,1175 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * agent-env's additions to libfaketime 0.9.12, part 2, included by libfaketime.c (libfaketime.patch) after its time + * functions: clock reads and waits for a set realtime clock time on universe time, re-planned whenever the page + * changes, and everything else (monotonic and boot clocks, relative sleeps and timeouts) the kernel's. + * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). + */ +/* + * Elapsed time is real; clock reads and waits for a realtime clock time follow the universe. + */ +#include +#include +#include +#include + +#if !defined(FAKE_PTHREAD) || !defined(FAKE_TIMERS) || !defined(FAKE_SLEEP) || defined(INTERCEPT_SYSCALL) +#error agent-env waits need FAKE_PTHREAD, FAKE_TIMERS and FAKE_SLEEP, and their own syscall(), not the INTERCEPT_SYSCALL one +#endif +#ifndef SYS_futex_waitv +#define SYS_futex_waitv 449 +#endif + +/* + * the system call itself, with no libc and no libfaketime state, so syscall() below works before the + * library is initialised (Chromium's allocator calls syscall() from inside the dynamic loader). + */ +static long ft_raw_syscall6(long n, long a, long b, long c, long d, long e, long f) +{ + long ret; +#if defined(__x86_64__) + register long r10 __asm__("r10") = d; + register long r8 __asm__("r8") = e; + register long r9 __asm__("r9") = f; + __asm__ volatile("syscall" : "=a"(ret) : "0"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9) + : "rcx", "r11", "memory"); +#elif defined(__aarch64__) + register long x8 __asm__("x8") = n; + register long x0 __asm__("x0") = a; + register long x1 __asm__("x1") = b; + register long x2 __asm__("x2") = c; + register long x3 __asm__("x3") = d; + register long x4 __asm__("x4") = e; + register long x5 __asm__("x5") = f; + __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); + ret = x0; +#else +#error the agent-env syscall() needs x86_64 or aarch64 +#endif + if (ret < 0 && ret > -4096) + { + errno = (int) -ret; + return -1; + } + return ret; +} + +#define FT_NS 1000000000LL +#define FT_NEVER INT64_MAX +static __thread struct ft_clock_page ft_tl_snap; +static __thread bool ft_tl_snap_ok = false; + +/* a consistent copy of the page. While the follower holds it odd (preempted mid-write), wait for it on seq, + which it wakes when done, up to a second: a copy older than the page being written would be extrapolated past the + new page's anchor, and the clock would step back when the write lands. Only a follower that died mid-write (seq odd + for over a second, remembered) leaves this thread on its last good copy. */ +static uint64_t ft_dead_seq = 0; +static bool ft_snap(struct ft_clock_page *s) +{ + for (int i = 0; i < 64; i++) + { + if (ft_page_snapshot(s)) + { + ft_tl_snap = *s; + ft_tl_snap_ok = true; + return true; + } + sched_yield(); + } + for (int i = 0; i < 100; i++) + { + uint64_t seq = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); + if ((seq & 1) && seq == __atomic_load_n(&ft_dead_seq, __ATOMIC_RELAXED)) break; + if (seq & 1) + { + struct timespec ts = {0, 10000000}; + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) (uint32_t) seq, (long) &ts, 0, 0); + } + if (ft_page_snapshot(s)) + { + ft_tl_snap = *s; + ft_tl_snap_ok = true; + return true; + } + if (i == 99) __atomic_store_n(&ft_dead_seq, seq, __ATOMIC_RELAXED); + } + if (!ft_tl_snap_ok) return false; + *s = ft_tl_snap; + return true; +} + +/* the real time just read, in hand before the loads after it: a load can be carried out before an earlier read of the + CPU's counter (aarch64 and x86_64 both allow it). On aarch64 a load whose address depends on the time, then a load + barrier, as the kernel orders its own counter reads; on x86_64 an lfence. */ +static inline void ft_after_real_read(const struct timespec *tp) +{ +#if defined(__aarch64__) + uint64_t tmp; + __asm__ volatile("eor %0, %1, %1\n\tadd %0, sp, %0\n\tldr xzr, [%0]\n\tdmb ishld" + : "=&r"(tmp) : "r"((uint64_t) tp->tv_nsec) : "memory"); +#elif defined(__x86_64__) + (void) tp; + __asm__ volatile("lfence" ::: "memory"); +#else + (void) tp; + __atomic_thread_fence(__ATOMIC_SEQ_CST); +#endif +} + +/* a consistent copy of the page and the real time read while it was the page: the time is read after the copy and kept + only if the page is still the one copied. A time from before a write paired with the page it wrote (a reader that + waited out the write, or was descheduled between its time and its copy) could put the universe behind what that + reader had shown, since a write is continuous with the page it replaces only at its own real time. */ +static bool ft_snap_at(clockid_t clock, struct ft_clock_page *s, struct timespec *tp) +{ + for (int i = 0; i < 8; i++) + { + int r; + if (!ft_snap(s)) return false; + DONT_FAKE_TIME(r = (*real_clock_gettime)(clock, tp)); + if (r != 0) return false; + ft_after_real_read(tp); + if (__atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE) == s->seq) return true; + } + return true; +} + +static inline int64_t ft_ns(const struct timespec *t) +{ + if (t->tv_sec > 9000000000LL) return FT_NEVER / 2; + if (t->tv_sec < -9000000000LL) return -(FT_NEVER / 2); + return (int64_t) t->tv_sec * FT_NS + t->tv_nsec; +} +static inline struct timespec ft_ts(int64_t ns) +{ + struct timespec t; + if (ns < 0) ns = 0; + t.tv_sec = (time_t) (ns / FT_NS); + t.tv_nsec = (long) (ns % FT_NS); + return t; +} +static inline int64_t ft_real_rt(void) +{ + struct timespec t; + (*real_clock_gettime)(CLOCK_REALTIME, &t); + return ft_ns(&t); +} +static inline bool ft_uw_active(void) +{ + return ft_page != NULL && !ft_disabled && !dont_fake; +} +static inline bool ft_faked(void) +{ + return !ft_disabled && !dont_fake; +} +static inline bool ft_valid_ts(const struct timespec *t) +{ + return t != NULL && t->tv_nsec >= 0 && t->tv_nsec < FT_NS && t->tv_sec >= 0; +} +static inline bool ft_rt_clock(clockid_t c) +{ + return c == CLOCK_REALTIME || c == CLOCK_REALTIME_ALARM; +} +/* the clocks the universe has; every other one (monotonic, boot, raw, CPU time) is real. */ +static inline bool ft_universe_clock(clockid_t c) +{ + return c == CLOCK_REALTIME || c == CLOCK_REALTIME_COARSE || c == CLOCK_REALTIME_ALARM || c == CLOCK_TAI; +} + +/* the universe's realtime at real realtime t; before the anchor, the anchor's. */ +static int64_t ft_page_rt(const struct ft_clock_page *s, int64_t t) +{ + if (!s->armed) return t; + if (s->rate == 0 || t <= s->anchor_ns) return s->universe_ns; + if (s->rate == 1.0) return s->universe_ns + (t - s->anchor_ns); + return s->universe_ns + (int64_t) ((double) (t - s->anchor_ns) * s->rate); +} +/* the real realtime at which the universe reaches ``due``; 0 (the past) once it has, FT_NEVER while frozen + short of it. */ +static int64_t ft_real_of(const struct ft_clock_page *s, int64_t due) +{ + double r; + if (!s->armed) return due < 1 ? 1 : due; + if (due <= s->universe_ns) return 1; + if (s->rate <= 0) return FT_NEVER; + r = (double) (due - s->universe_ns) / s->rate; + if (r > 4e18) return FT_NEVER; + return s->anchor_ns + (int64_t) r + 1; +} +static inline bool ft_reached(const struct ft_clock_page *s, int64_t due) +{ + return ft_page_rt(s, ft_real_rt()) >= due; +} +static inline uint32_t ft_seq32(void) +{ + return (uint32_t) __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); +} + +/* ---- a lock for the tables below, with signals blocked so a handler can't deadlock on it ---- */ +static volatile int ft_tab_lock_word = 0; +static void ft_tab_lock(sigset_t *old) +{ + sigset_t all; + sigfillset(&all); + pthread_sigmask(SIG_SETMASK, &all, old); + while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); +} +static void ft_tab_unlock(const sigset_t *old) +{ + __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); + pthread_sigmask(SIG_SETMASK, old, NULL); +} + +/* + * ---- timers for a realtime clock time: timerfds and POSIX timers armed at TIMER_ABSTIME on a realtime clock. The + * kernel timer is armed at the real realtime the universe reaches the deadline at (in the past once it has, so it fires + * now), its interval at interval / rate, and disarmed while frozen short of it; the helper arms it again on each change. + * A one-shot timer that has fired is left alone. ---- + */ +#define FT_TIMER_FDS 8 +struct ft_timer +{ + int kind; /* 0 free, 1 timerfd, 2 POSIX timer */ + int tracked; /* armed for a realtime clock time */ + int nfds, fds[FT_TIMER_FDS]; /* a timerfd's descriptors: the one it was armed through and its duplicates */ + timer_t id; + int clock; + int flags; /* the caller's flags, TFD_TIMER_CANCEL_ON_SET kept */ + int real_set; /* the kernel timer is armed */ + int64_t due, interval; /* universe ns */ + struct ft_clock_page pg; /* the page it was last armed from */ +}; +#define FT_TIMERS_MAX 1024 +static struct ft_timer ft_timers[FT_TIMERS_MAX]; +static int ft_timers_top = 0; +static volatile int ft_timers_any = 0; +static int (*real_timer_create_new)(clockid_t, struct sigevent *, timer_t *); +static int (*real_timer_delete_new)(timer_t); +static int (*real_timer_settime_new)(timer_t, int, const struct itimerspec *, struct itimerspec *); +static int (*real_timer_gettime_new)(timer_t, struct itimerspec *); +#define FT_TFD_CLOCKS 65536 +static int ft_tfd_clock[FT_TFD_CLOCKS]; /* clock + 1 as timerfd_create was told; 0 unknown */ + +static struct ft_timer *ft_timer_find_fd(int fd) +{ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 1) + for (int k = 0; k < ft_timers[i].nfds; k++) + if (ft_timers[i].fds[k] == fd) return &ft_timers[i]; + return NULL; +} +static struct ft_timer *ft_timer_find_id(timer_t id) +{ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 2 && ft_timers[i].id == id) return &ft_timers[i]; + return NULL; +} +static struct ft_timer *ft_timer_new(void) +{ + for (int i = 0; i < FT_TIMERS_MAX; i++) + if (ft_timers[i].kind == 0) + { + memset(&ft_timers[i], 0, sizeof ft_timers[i]); + if (i >= ft_timers_top) ft_timers_top = i + 1; + return &ft_timers[i]; + } + return NULL; +} +static int ft_timer_real_get(struct ft_timer *t, struct itimerspec *cur) +{ + return t->kind == 1 ? (*real_timerfd_gettime)(t->fds[0], cur) : (*real_timer_gettime_new)(t->id, cur); +} +/* the next expiration at or after ``after``. */ +static int64_t ft_timer_next(const struct ft_timer *t, int64_t after) +{ + int64_t next = t->due; + if (t->interval > 0 && next < after) next += ((after - next + t->interval - 1) / t->interval) * t->interval; + return next; +} +/* the kernel timer set from page ``s``. A periodic timer goes on from its first expiration past the + universe's time on the page it was armed from, so a jump delivers what it passed at once. Lock held. */ +static int ft_timer_arm(struct ft_timer *t, const struct ft_clock_page *s, bool first) +{ + struct itimerspec real, cur; + double rate = s->armed ? s->rate : 1.0; + int64_t next = t->due, at; + int r; + memset(&real, 0, sizeof real); + if (!first && t->real_set && t->interval == 0 && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 + && cur.it_value.tv_nsec == 0) + { + t->tracked = 0; /* fired: the expiration is the kernel's to hand over */ + return 0; + } + if (!first && t->interval > 0) next = ft_timer_next(t, ft_page_rt(&t->pg, ft_real_rt()) + 1); + at = ft_real_of(s, next); + if (at != FT_NEVER) + { + real.it_value = ft_ts(at); + if (real.it_value.tv_sec == 0 && real.it_value.tv_nsec == 0) real.it_value.tv_nsec = 1; + if (t->interval > 0) + { + int64_t iv = (int64_t) ((double) t->interval / rate); + real.it_interval = ft_ts(iv < 1 ? 1 : iv); + } + } + t->due = next; + t->pg = *s; + t->real_set = (at != FT_NEVER); + if (t->kind == 1) + r = (*real_timerfd_settime)(t->fds[0], TFD_TIMER_ABSTIME | (t->flags & TFD_TIMER_CANCEL_ON_SET), &real, NULL); + else + r = (*real_timer_settime_new)(t->id, TIMER_ABSTIME, &real, NULL); + if (r != 0) t->tracked = 0; + return r; +} +/* the timer's value in universe time. Lock held. */ +static void ft_timer_value(struct ft_timer *t, struct itimerspec *out) +{ + struct ft_clock_page s; + struct itimerspec cur; + memset(out, 0, sizeof *out); + if (!t->tracked) return; + if (t->interval == 0 && t->real_set && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 + && cur.it_value.tv_nsec == 0) + { + t->tracked = 0; + return; + } + out->it_interval = ft_ts(t->interval); + if (ft_snap(&s)) + { + int64_t now = ft_page_rt(&s, ft_real_rt()), next = ft_timer_next(t, now); + out->it_value = ft_ts(next - now < 1 ? 1 : next - now); + } +} + +/* + * ---- realtime condvar waits in progress: each waits to the real time its deadline falls at; on a page change that + * moves that time, the helper broadcasts its condvar, once, and the wait returns 0 (a spurious wakeup the caller + * rechecks), so the caller waits again on the new page. A broadcast that comes before the waiter is inside glibc's wait + * is sent again until the waiter has seen the change. ---- + */ +struct ft_cw +{ + pthread_cond_t *cond; + int used, woken; + int64_t due, real_due; + uint64_t gen; /* the page generation its real_due came from; FT_CW_UNPLANNED before */ +}; +#define FT_CW_MAX 512 +#define FT_CW_UNPLANNED UINT64_MAX +static struct ft_cw ft_cws[FT_CW_MAX]; +static int ft_cws_top = 0; + +/* ---- the helper thread: one per process, started with the first such timer or wait, blocked on the page's seq ---- */ +static volatile pid_t ft_helper_pid = 0; +static volatile int ft_atfork_set = 0; +static void ft_helper_child(void) +{ + /* POSIX timers and condvar waiters belong to the parent; timerfds are shared with it and stay */ + for (int i = 0; i < ft_timers_top; i++) + if (ft_timers[i].kind == 2) ft_timers[i].kind = 0; + memset(ft_cws, 0, sizeof ft_cws); + ft_cws_top = 0; + ft_tab_lock_word = 0; + ft_helper_pid = 0; +} + +/* on a page change, every timer armed again and every condvar waiter whose real deadline moved woken; true + while a waiter is still to see it (not planned yet, or broadcast to before it was waiting). */ +static bool ft_on_change(const struct ft_clock_page *s, bool rebroadcast) +{ + sigset_t old; + bool pending = false; + ft_tab_lock(&old); + if (!rebroadcast) + for (int i = 0; i < ft_timers_top; i++) + { + struct ft_timer *t = &ft_timers[i]; + if (t->kind != 0 && t->tracked && t->pg.generation != s->generation) ft_timer_arm(t, s, false); + } + for (int i = 0; i < ft_cws_top; i++) + { + struct ft_cw *w = &ft_cws[i]; + if (!w->used || w->gen == s->generation) continue; + if (w->gen == FT_CW_UNPLANNED) + { + pending = true; + continue; + } + if (!w->woken) + { + int64_t nd = ft_real_of(s, w->due), diff; + diff = (nd == FT_NEVER || w->real_due == FT_NEVER) ? (nd == w->real_due ? 0 : FT_NEVER) : nd - w->real_due; + if (diff >= -1000000 && diff <= 1000000) + { + w->gen = s->generation; /* its real end didn't move */ + continue; + } + } + if (!w->woken || rebroadcast) pthread_cond_broadcast(w->cond); + w->woken = 1; + pending = true; + } + ft_tab_unlock(&old); + return pending; +} + +static void *ft_helper_main(void *arg) +{ + struct ft_clock_page s; + uint64_t gen = UINT64_MAX; /* the first pass catches a change made while it started */ + int backoff_ms = 0; + (void) arg; + for (;;) + { + uint32_t seq32 = ft_seq32(); + bool pending = false; + if (ft_snap(&s)) + { + if (s.generation != gen) + { + gen = s.generation; + pending = ft_on_change(&s, false); + backoff_ms = pending ? 1 : 0; + } + else if (backoff_ms > 0) + { + pending = ft_on_change(&s, true); + backoff_ms = pending ? (backoff_ms < 64 ? backoff_ms * 2 : 100) : 0; + } + } + if (backoff_ms > 0) + { + struct timespec ts = {0, (long) backoff_ms * 1000000L}; + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, (long) &ts, 0, 0); + } + else + ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, 0, 0, 0); + } + return NULL; +} + +static void ft_helper_start(void) +{ + pid_t pid = getpid(); + sigset_t all, old; + pthread_t thread; + pthread_attr_t attr; + if (__atomic_load_n(&ft_helper_pid, __ATOMIC_ACQUIRE) == pid) return; + sigfillset(&all); + pthread_sigmask(SIG_SETMASK, &all, &old); + while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); + if (ft_helper_pid != pid) + { + if (!ft_atfork_set) + { + pthread_atfork(NULL, NULL, ft_helper_child); + ft_atfork_set = 1; + } + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&attr, 128 * 1024); + if (pthread_create(&thread, &attr, ft_helper_main, NULL) == 0) /* inherits the all-blocked signal mask */ + { + __atomic_store_n(&ft_helper_pid, pid, __ATOMIC_RELEASE); + pthread_setname_np(thread, "agentenv-clock"); + } + pthread_attr_destroy(&attr); + } + __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); + pthread_sigmask(SIG_SETMASK, &old, NULL); +} + +/* ---- the waits ---- */ + +/* the raw system calls below as the cancellation points their callers are (clock_nanosleep, sem_timedwait): + cancellation acted on at once for the call alone, as glibc's own waits were before its cancellable system call + wrapper. */ +static inline int ft_async_cancel(void) +{ + int old; + pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); + return old; +} +static inline void ft_deferred_cancel(int old) +{ + int e = errno, ignored; + pthread_setcanceltype(old, &ignored); + errno = e; +} + +/* a sleep until the universe reaches ``due``, on the page's seq. 0, or EINTR. */ +static int ft_rt_sleep(const struct ft_clock_page *first, int64_t due) +{ + struct ft_clock_page s = *first; + pthread_testcancel(); + for (;;) + { + uint32_t seq32 = (uint32_t) s.seq; + int64_t until; + struct timespec ts; + long r; + int ct; + if (ft_reached(&s, due)) return 0; + until = ft_real_of(&s, due); + ts = ft_ts(until); + ct = ft_async_cancel(); + r = ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, (long) seq32, + until == FT_NEVER ? 0 : (long) &ts, 0, FUTEX_BITSET_MATCH_ANY); + ft_deferred_cancel(ct); + if (r == -1 && errno == EINTR) return EINTR; + if (!ft_snap(&s)) return 0; + } +} + +struct ft_futex_waitv { uint64_t val, uaddr; uint32_t flags, reserved; }; +static volatile int ft_waitv_missing = 0; +enum { FT_W_WORD, FT_W_PAGE, FT_W_TIMEOUT, FT_W_INTR, FT_W_ERR }; +/* without futex_waitv (before Linux 5.16, under gVisor, or refused by a seccomp filter): the word alone, until the real + deadline or a slice (AGENTENV_WAIT_SLICE_MS) at most, and the caller reads the page again between slices. */ +static int ft_wait1(uint32_t *word, uint32_t val, bool private_word, int64_t until) +{ + int64_t end = ft_real_rt() + ft_wait_slice_ns; + struct timespec ts; + if (until < end) end = until; + ts = ft_ts(end); + if (ft_raw_syscall6(SYS_futex, (long) word, + FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME | (private_word ? FUTEX_PRIVATE_FLAG : 0), (long) val, + (long) &ts, 0, FUTEX_BITSET_MATCH_ANY) == 0 || errno == EAGAIN) + return FT_W_WORD; + if (errno == ETIMEDOUT) return FT_W_PAGE; /* a slice's end, or the deadline: the caller reads the page */ + return errno == EINTR ? FT_W_INTR : FT_W_ERR; +} +/* until *word isn't val, the page changes from seq32, or real realtime ``until``; FT_W_ERR with errno the kernel's + (an unaligned or unmapped word). */ +static int ft_wait2(uint32_t *word, uint32_t val, bool private_word, uint32_t seq32, int64_t until) +{ + struct ft_futex_waitv w[2]; + struct timespec ts = ft_ts(until); + long r; + if (ft_waitv_missing) return ft_wait1(word, val, private_word, until); + /* the page first: futex_waitv returns the last of the futexes woken, so a caller's wake is never taken for the page's */ + w[0].val = seq32; + w[0].uaddr = (uint64_t) (uintptr_t) &ft_page->seq; + w[0].flags = 0x02; /* FUTEX2_SIZE_U32, shared */ + w[0].reserved = 0; + w[1].val = val; + w[1].uaddr = (uint64_t) (uintptr_t) word; + w[1].flags = 0x02 | (private_word ? 128 : 0); + w[1].reserved = 0; + r = ft_raw_syscall6(SYS_futex_waitv, (long) w, 2, 0, until == FT_NEVER ? 0 : (long) &ts, CLOCK_REALTIME, 0); + if (r == 1) return FT_W_WORD; + if (r == 0) return FT_W_PAGE; + if (errno == EAGAIN) return __atomic_load_n(word, __ATOMIC_RELAXED) != val ? FT_W_WORD : FT_W_PAGE; + if (errno == ETIMEDOUT) return FT_W_TIMEOUT; + if (errno == EINTR) return FT_W_INTR; + if (errno != ENOSYS && errno != EPERM) return FT_W_ERR; /* EPERM: a seccomp filter that refuses it so */ + ft_waitv_missing = 1; + return ft_wait1(word, val, private_word, until); +} + +/* futex(2) FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME through syscall(): until the universe reaches the deadline, + beside the page; *handled false leaves the call to the kernel as it is. */ +static long ft_u_futex_rt(uint32_t *uaddr, int op, uint32_t val, const struct timespec *abs, bool *handled) +{ + struct ft_clock_page s; + int64_t due = ft_ns(abs); + *handled = false; + if (!ft_snap(&s)) return 0; + *handled = true; + for (;;) + { + uint32_t seq32 = (uint32_t) s.seq; + int k; + if (ft_reached(&s, due)) + { + errno = ETIMEDOUT; + return -1; + } + k = ft_wait2(uaddr, val, (op & FUTEX_PRIVATE_FLAG) != 0, seq32, ft_real_of(&s, due)); + if (k == FT_W_WORD) + { + if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) + { + errno = EAGAIN; + return -1; + } + return 0; + } + if (k == FT_W_INTR) + { + errno = EINTR; + return -1; + } + if (k == FT_W_ERR) return -1; + if (!ft_snap(&s)) return 0; + } +} + +/* sem_timedwait / sem_clockwait(CLOCK_REALTIME) on glibc's semaphore (its layout is part of the ABI, since + process-shared semaphores outlive glibc versions): the value in the low word of data, the waiters above it, the + futex the value word, which sem_post wakes when there are waiters; ``private`` is glibc's futex flag the other way + round (0 for a process-private semaphore, FUTEX_PRIVATE_FLAG for a shared one). */ +struct ft_new_sem { uint64_t data; int private_flag; int pad; }; +static void ft_sem_unwait(void *sem) +{ + __atomic_fetch_sub(&((struct ft_new_sem *) sem)->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); +} +/* as one of the semaphore's waiters (counted in data's high word): 0 with a token taken, or the error. A cancelled wait + leaves the waiters it joined, as glibc's does. */ +static int ft_sem_wait_slow(struct ft_new_sem *sem, struct ft_clock_page *s, int64_t due) +{ + volatile int err = 0; + pthread_cleanup_push(ft_sem_unwait, sem); + uint64_t d = __atomic_fetch_add(&sem->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); + for (;;) + { + if ((d & 0xffffffffu) == 0) + { + int k, ct; + if (ft_reached(s, due)) + { + err = ETIMEDOUT; + break; + } + ct = ft_async_cancel(); + k = ft_wait2((uint32_t *) &sem->data, 0, sem->private_flag == 0, (uint32_t) s->seq, ft_real_of(s, due)); + ft_deferred_cancel(ct); + if (k == FT_W_INTR || k == FT_W_ERR) + { + err = k == FT_W_INTR ? EINTR : errno; + break; + } + if (!ft_snap(s)) + { + err = ETIMEDOUT; + break; + } + d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); + } + else if (__atomic_compare_exchange_n(&sem->data, &d, d - 1 - ((uint64_t) 1 << 32), true, __ATOMIC_ACQUIRE, + __ATOMIC_RELAXED)) + break; + } + pthread_cleanup_pop(err != 0); + return err; +} +static int ft_u_sem_wait(sem_t *sem_, int64_t due, bool *handled) +{ + struct ft_new_sem *sem = (struct ft_new_sem *) sem_; + struct ft_clock_page s; + uint64_t d; + int err; + *handled = false; + if (sizeof(sem_t) < sizeof(struct ft_new_sem) || !ft_snap(&s)) return 0; + *handled = true; + pthread_testcancel(); + d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); + while ((d & 0xffffffffu) > 0) + if (__atomic_compare_exchange_n(&sem->data, &d, d - 1, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) return 0; + err = ft_sem_wait_slow(sem, &s, due); + if (err == 0) return 0; + errno = err; + return -1; +} + +static void ft_cw_release(void *w) +{ + sigset_t old; + ft_tab_lock(&old); + ((struct ft_cw *) w)->used = 0; + ft_tab_unlock(&old); +} +/* glibc's wait, a cancellation point: a cancelled wait gives up its entry, so the helper never wakes a condvar that + may be gone. */ +static int ft_cw_clockwait(struct ft_cw *w, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *end) +{ + volatile int r; + pthread_cleanup_push(ft_cw_release, w); + r = real_pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, end); + pthread_cleanup_pop(0); + return r; +} + +/* a realtime condvar wait (see ft_cw). -1 leaves it to the caller's own path (no page, table full). */ +static int ft_u_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) +{ + struct ft_clock_page s; + struct ft_cw *w = NULL; + struct timespec end; + sigset_t old; + int64_t due = ft_ns(abstime), real_due; + int r, woken; + if (!ft_snap(&s)) return -1; + if (ft_reached(&s, due)) return ETIMEDOUT; + ft_helper_start(); + ft_tab_lock(&old); + for (int i = 0; i < FT_CW_MAX; i++) + if (!ft_cws[i].used) + { + w = &ft_cws[i]; + if (i >= ft_cws_top) ft_cws_top = i + 1; + break; + } + if (w != NULL) + { + w->cond = cond; + w->due = due; + w->woken = 0; + w->gen = FT_CW_UNPLANNED; + w->used = 1; + } + ft_tab_unlock(&old); + if (w == NULL) return -1; + if (!ft_snap(&s)) s.armed = 0; + real_due = ft_real_of(&s, due); + ft_tab_lock(&old); + w->real_due = real_due; + w->gen = s.generation; + ft_tab_unlock(&old); + end = ft_ts(real_due == FT_NEVER ? ft_real_rt() + 86400 * FT_NS : real_due); + r = ft_cw_clockwait(w, cond, mutex, &end); + ft_tab_lock(&old); + woken = w->woken; + w->used = 0; + ft_tab_unlock(&old); + /* a real timeout before the universe's deadline (the page moved it later) is a spurious wakeup; the helper's wake for + a jump that passed the deadline is the timeout */ + if (r == ETIMEDOUT) return (ft_snap(&s) && !ft_reached(&s, due)) ? 0 : ETIMEDOUT; + if (r == 0 && woken && ft_snap(&s) && ft_reached(&s, due)) return ETIMEDOUT; + return r; +} + +/* a realtime mutex wait: glibc's mutex has no way in, so slices (AGENTENV_WAIT_SLICE_MS) of a real + CLOCK_MONOTONIC wait, which loses nothing when it times out, with the page read between them. */ +static int ft_u_mutex_lock(pthread_mutex_t *mutex, int64_t due, bool *handled) +{ + struct ft_clock_page s; + *handled = false; + if (!ft_snap(&s)) return 0; + *handled = true; + for (;;) + { + struct timespec now, end; + int64_t at, sl; + int r; + if (ft_reached(&s, due)) return ETIMEDOUT; + at = ft_real_of(&s, due); + sl = (at == FT_NEVER) ? ft_wait_slice_ns : at - ft_real_rt(); + if (sl > ft_wait_slice_ns) sl = ft_wait_slice_ns; + if (sl < 1) sl = 1; + (*real_clock_gettime)(CLOCK_MONOTONIC, &now); + end = ft_ts(ft_ns(&now) + sl); + r = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &end); + if (r == EINVAL) /* a PI mutex on a kernel without FUTEX_LOCK_PI2 */ + { + struct timespec real = ft_ts(ft_real_rt() + sl); + r = real_pthread_mutex_timedlock(mutex, &real); + } + if (r != ETIMEDOUT) return r; + if (!ft_snap(&s)) return ETIMEDOUT; + } +} + +/* syscall(): futex waits for a realtime clock time follow the universe (raw futex users such as Rust's std + and GLib call it), clock_gettime reads it once the library is up; everything else goes straight to the kernel. */ +long syscall(long number, ...) +{ + va_list ap; + long a[6]; + va_start(ap, number); + for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); + va_end(ap); + if (number == SYS_futex && ft_uw_active()) + { + int op = (int) a[1]; + const struct timespec *ts = (const struct timespec *) a[3]; + if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET && (op & FUTEX_CLOCK_REALTIME) && ft_valid_ts(ts) + && (uint32_t) a[5] == FUTEX_BITSET_MATCH_ANY) + { + bool handled; + long r = ft_u_futex_rt((uint32_t *) a[0], op, (uint32_t) a[2], ts, &handled); + if (handled) return r; + } + } + else if (number == SYS_clock_gettime && ft_page != NULL && !ft_disabled && !dont_fake) + return clock_gettime((clockid_t) a[0], (struct timespec *) a[1]); + return ft_raw_syscall6(number, a[0], a[1], a[2], a[3], a[4], a[5]); +} + +/* ---- timerfds ---- */ +static int ft_u_timerfd_settime(int fd, int flags, const struct itimerspec *nv, struct itimerspec *ov, bool *handled) +{ + struct ft_clock_page s; + struct ft_timer *t = NULL; + struct itimerspec prev; + sigset_t old; + int clock, r; + bool was = false; + *handled = false; + if (fd < 0 || fd >= FT_TFD_CLOCKS || nv == NULL) return 0; + clock = ft_tfd_clock[fd] - 1; + *handled = true; + if (ft_timers_any) + { + ft_tab_lock(&old); + t = ft_timer_find_fd(fd); + if (t != NULL && t->tracked) + { + ft_timer_value(t, &prev); + was = true; + t->tracked = 0; /* the record stays, with the timer's descriptors, while any is open */ + } + ft_tab_unlock(&old); + } + if (!ft_rt_clock(clock) || !(flags & TFD_TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) + || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) + { + r = (*real_timerfd_settime)(fd, flags, nv, ov); + if (r == 0 && ov != NULL && was) *ov = prev; + return r; + } + ft_tab_lock(&old); + if ((t = ft_timer_find_fd(fd)) == NULL && (t = ft_timer_new()) != NULL) + { + t->kind = 1; + t->nfds = 1; + t->fds[0] = fd; + } + if (t == NULL) + { + ft_tab_unlock(&old); + *handled = false; /* table full: the caller's own (translated once) path */ + return 0; + } + t->clock = clock; + t->flags = flags; + t->due = ft_ns(&nv->it_value); + t->interval = ft_ns(&nv->it_interval); + t->tracked = 1; + if (ov != NULL) + { + if (was) + *ov = prev; + else + (*real_timerfd_gettime)(fd, ov); + } + r = ft_timer_arm(t, &s, true); + if (r != 0) t->tracked = 0; + ft_timers_any = 1; + ft_tab_unlock(&old); + if (r == 0) ft_helper_start(); + return r; +} +static int ft_u_timerfd_gettime(int fd, struct itimerspec *cur, bool *handled) +{ + struct ft_timer *t; + sigset_t old; + *handled = false; + if (!ft_timers_any || cur == NULL) return 0; + ft_tab_lock(&old); + t = ft_timer_find_fd(fd); + if (t != NULL && t->tracked) + { + ft_timer_value(t, cur); + *handled = true; + } + ft_tab_unlock(&old); + return 0; +} + +/* + * ---- descriptors: a timerfd's record follows its file, not a number. close() takes a descriptor off it (the record + * goes with the last), dup() and its kin add the new one, so the helper never arms a number the program has closed or + * that now names another file. A number closed where this can't see (closefrom, a raw system call) is dropped when + * timerfd_create hands it out again; a timer with more duplicates than a record holds is left armed as it was. ---- + */ +static int (*ft_real_close)(int); +static int (*ft_real_dup)(int); +static int (*ft_real_dup2)(int, int); +static int (*ft_real_dup3)(int, int, int); +static int (*ft_real_fcntl)(int, int, ...); +static int (*ft_real_fcntl64)(int, int, ...); +static int (*ft_real_close_range)(unsigned int, unsigned int, int); +#define FT_RESOLVE(name) do { if (ft_real_##name == NULL) ft_real_##name = dlsym(RTLD_NEXT, #name); } while (0) + +static inline bool ft_tfd_known(int fd) +{ + return fd >= 0 && fd < FT_TFD_CLOCKS && __atomic_load_n(&ft_tfd_clock[fd], __ATOMIC_RELAXED) != 0; +} +/* the process whose descriptors these tables describe: set at load and in the child of each glibc fork. A child that + shares this memory without fork (vfork, as Python's subprocess uses, or a CLONE_VM clone) has a descriptor table of + its own, so what it closes or duplicates before exec changes nothing here. A fork's child also takes the tables' + lock free: another thread may have held it at the fork, and no thread of the child would ever release it. */ +static volatile pid_t ft_fd_owner = 0; +static void ft_fd_forked(void) +{ + ft_fd_owner = getpid(); + ft_tab_lock_word = 0; +} +static inline bool ft_fd_mine(void) +{ + return getpid() == ft_fd_owner; +} +/* ``fd`` closed, or about to name another file. Lock held. */ +static void ft_fd_gone_locked(int fd) +{ + struct ft_timer *t; + ft_tfd_clock[fd] = 0; + while ((t = ft_timer_find_fd(fd)) != NULL) + { + int k = 0; + while (t->fds[k] != fd) k++; + t->fds[k] = t->fds[--t->nfds]; + if (t->nfds == 0) t->kind = 0; + } +} +static void ft_fd_gone(int fd) +{ + sigset_t old; + if (!ft_tfd_known(fd) || !ft_fd_mine()) return; + ft_tab_lock(&old); + ft_fd_gone_locked(fd); + ft_tab_unlock(&old); +} +/* ``to`` now names ``from``'s file, a known timerfd. Lock held. */ +static void ft_fd_duped_locked(int from, int to) +{ + struct ft_timer *t; + if (to < 0 || to >= FT_TFD_CLOCKS) return; + ft_tfd_clock[to] = ft_tfd_clock[from]; + if ((t = ft_timer_find_fd(from)) != NULL) + { + if (t->nfds < FT_TIMER_FDS) + t->fds[t->nfds++] = to; + else + t->kind = 0; + } +} +static void ft_fd_duped(int from, int to) +{ + sigset_t old; + if (!ft_tfd_known(from) || !ft_fd_mine()) return; + ft_tab_lock(&old); + ft_fd_duped_locked(from, to); + ft_tab_unlock(&old); +} +/* dup2 or dup3 onto ``fd2``. One that replaces a timerfd (whose close doesn't block) makes the change and its + bookkeeping under the lock, so a failed call keeps the record and the helper never arms the number in between. */ +static int ft_dup_onto(int fd, int fd2, int flags, bool three) +{ + sigset_t old; + int r; + FT_RESOLVE(dup2); + FT_RESOLVE(dup3); + if (fd == fd2 || !ft_tfd_known(fd2) || !ft_fd_mine()) + { + r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); + if (r >= 0 && fd != fd2) ft_fd_duped(fd, r); + return r; + } + ft_tab_lock(&old); + r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); + if (r >= 0) + { + ft_fd_gone_locked(fd2); + if (ft_tfd_known(fd)) ft_fd_duped_locked(fd, fd2); + } + ft_tab_unlock(&old); + return r; +} + +int close(int fd) +{ + ft_fd_gone(fd); + FT_RESOLVE(close); + return (*ft_real_close)(fd); +} +int dup(int fd) +{ + int r; + FT_RESOLVE(dup); + r = (*ft_real_dup)(fd); + if (r >= 0) ft_fd_duped(fd, r); + return r; +} +int dup2(int fd, int fd2) +{ + return ft_dup_onto(fd, fd2, 0, false); +} +int dup3(int fd, int fd2, int flags) +{ + return ft_dup_onto(fd, fd2, flags, true); +} +/* the argument passed on as glibc's own fcntl reads it, a pointer-sized word whatever the command */ +int fcntl(int fd, int cmd, ...) +{ + va_list ap; + void *arg; + int r; + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + FT_RESOLVE(fcntl); + r = (*ft_real_fcntl)(fd, cmd, arg); + if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); + return r; +} +int fcntl64(int fd, int cmd, ...) +{ + va_list ap; + void *arg; + int r; + va_start(ap, cmd); + arg = va_arg(ap, void *); + va_end(ap); + FT_RESOLVE(fcntl64); + r = (*ft_real_fcntl64)(fd, cmd, arg); + if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); + return r; +} +/* one that closes a timerfd runs under the lock, as dup2 does, so a failed call keeps the records. In practice it runs + in a child of fork before exec, where no other thread wants the lock. */ +int close_range(unsigned int first, unsigned int last, int flags) +{ + sigset_t old; + bool timerfd = false; + int r; + FT_RESOLVE(close_range); + if (!(flags & CLOSE_RANGE_CLOEXEC) && ft_fd_mine()) + for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS && !timerfd; fd++) timerfd = ft_tfd_known((int) fd); + if (!timerfd) return (*ft_real_close_range)(first, last, flags); + ft_tab_lock(&old); + r = (*ft_real_close_range)(first, last, flags); + if (r == 0) + for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS; fd++) + if (ft_tfd_known((int) fd)) ft_fd_gone_locked((int) fd); + ft_tab_unlock(&old); + return r; +} +/* the owner, and the real calls resolved at load, so a first close() in a signal handler doesn't call dlsym */ +static void __attribute__((constructor)) ft_fd_init(void) +{ + ft_fd_owner = getpid(); + pthread_atfork(NULL, NULL, ft_fd_forked); + FT_RESOLVE(close); + FT_RESOLVE(dup); + FT_RESOLVE(dup2); + FT_RESOLVE(dup3); + FT_RESOLVE(fcntl); + FT_RESOLVE(fcntl64); + FT_RESOLVE(close_range); +} + +/* ---- POSIX timers (unversioned: upstream's GLIBC_2.2 / 2.3.3 versions don't bind glibc 2.34's timer_settime) ---- */ +int timer_create(clockid_t clock, struct sigevent *sev, timer_t *id) +{ + int r; + ftpl_init(); + if (real_timer_create_new == NULL) real_timer_create_new = dlsym(RTLD_NEXT, "timer_create"); + if (real_timer_create_new == NULL) + { + errno = ENOSYS; + return -1; + } + r = (*real_timer_create_new)(clock, sev, id); + if (r == 0 && id != NULL && ft_rt_clock(clock) && ft_faked()) + { + sigset_t old; + struct ft_timer *t; + ft_tab_lock(&old); + t = ft_timer_find_id(*id); + if (t == NULL) t = ft_timer_new(); + if (t != NULL) + { + memset(t, 0, sizeof *t); + t->kind = 2; + t->id = *id; + t->clock = clock; + } + ft_timers_any = 1; + ft_tab_unlock(&old); + } + return r; +} +int timer_delete(timer_t id) +{ + ftpl_init(); + if (real_timer_delete_new == NULL) real_timer_delete_new = dlsym(RTLD_NEXT, "timer_delete"); + if (ft_timers_any) + { + sigset_t old; + struct ft_timer *t; + ft_tab_lock(&old); + if ((t = ft_timer_find_id(id)) != NULL) t->kind = 0; + ft_tab_unlock(&old); + } + return (*real_timer_delete_new)(id); +} +int timer_settime(timer_t id, int flags, const struct itimerspec *nv, struct itimerspec *ov) +{ + struct ft_clock_page s; + struct ft_timer *t; + struct itimerspec prev; + sigset_t old; + bool was = false; + int r; + ftpl_init(); + if (real_timer_settime_new == NULL) real_timer_settime_new = dlsym(RTLD_NEXT, "timer_settime"); + if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); + if (!ft_timers_any || nv == NULL) return (*real_timer_settime_new)(id, flags, nv, ov); + ft_tab_lock(&old); + t = ft_timer_find_id(id); + if (t != NULL && t->tracked) + { + ft_timer_value(t, &prev); + was = true; + t->tracked = 0; + } + ft_tab_unlock(&old); + if (t == NULL || !ft_uw_active() || !(flags & TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) + || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) + { + r = (*real_timer_settime_new)(id, flags, nv, ov); + if (r == 0 && ov != NULL && was) *ov = prev; + return r; + } + ft_tab_lock(&old); + if (ov != NULL) + { + if (was) + *ov = prev; + else + (*real_timer_gettime_new)(id, ov); + } + t->due = ft_ns(&nv->it_value); + t->interval = ft_ns(&nv->it_interval); + t->tracked = 1; + r = ft_timer_arm(t, &s, true); + ft_tab_unlock(&old); + if (r == 0) ft_helper_start(); + return r; +} +int timer_gettime(timer_t id, struct itimerspec *cur) +{ + sigset_t old; + struct ft_timer *t; + ftpl_init(); + if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); + if (ft_timers_any && cur != NULL) + { + ft_tab_lock(&old); + t = ft_timer_find_id(id); + if (t != NULL && t->tracked) + { + ft_timer_value(t, cur); + ft_tab_unlock(&old); + return 0; + } + ft_tab_unlock(&old); + } + return (*real_timer_gettime_new)(id, cur); +} + diff --git a/src/agent_env/agents/agents/claude-code/clock/build.sh b/src/agent_env/agents/agents/claude-code/clock/build.sh new file mode 100755 index 000000000..8d7227284 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/build.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Builds agent-env's libfaketime: upstream 0.9.12 from its sha256-pinned tarball, libfaketime.patch applied and the two +# engine files beside it copied in, the multithreaded library with stat and utime faking compiled out (file times stay +# real); and the TLS shim beside it (tls_realtime.c). Writes OUT/libfaketimeMT.so.1 and OUT/libtls_realtime.so, which +# /etc/ld.so.preload lists in that order. Needs a C toolchain, make, patch and curl; builds on x86_64 and aarch64. +# build.sh OUT [TARBALL] (TARBALL: a local copy of upstream's v0.9.12 tarball, else it is downloaded) +set -eu +out=$1 +here=$(cd "$(dirname "$0")" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +tarball=${2:-$work/libfaketime.tar.gz} +[ -f "$tarball" ] || curl -fsSL -o "$tarball" https://github.com/wolfcw/libfaketime/archive/refs/tags/v0.9.12.tar.gz +echo "4fc32218697c052adcdc5ee395581f2554ca56d086ac817ced2be0d6f1f8a9fa $tarball" | sha256sum -c - >/dev/null +tar xzf "$tarball" -C "$work" +src=$work/libfaketime-0.9.12 +patch -s -d "$src" -p1 < "$here/libfaketime.patch" +cp "$here/agentenv_clock_page.c" "$here/agentenv_clock_waits.c" "$src/src/" +make -s -C "$src/src" FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME" libfaketimeMT.so.1 +mkdir -p "$out" +cp "$src/src/libfaketimeMT.so.1" "$out/" +${CC:-cc} -std=gnu99 -O2 -fPIC -shared -Wall -Wextra -Werror -o "$out/libtls_realtime.so" "$here/tls_realtime.c" -ldl diff --git a/src/agent_env/agents/agents/claude-code/clock/clock-follower b/src/agent_env/agents/agents/claude-code/clock/clock-follower new file mode 100755 index 000000000..652431687 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clock-follower @@ -0,0 +1,204 @@ +#!/usr/bin/python3 -I +"""The computer's clock follower: holds a long poll on the gateway's ``/clock/watch`` and writes each state it learns +to the clock page (``clockpage.py``), which agent-env's libfaketime reads in every session program, so the session reads +the universe's time. It runs as its own user, under root's supervisord, so the session user can read the clock but +neither write it nor stop it. + +Core writes its config, the gateway's watch URL as this computer reaches it, the computer's name and its read token, +through the sandbox's root channel (``CONFIG``, readable by root and this user alone); never through the driver, which +runs as the session user. It reads the config every quarter second, during a watch too, and follows a changed one at +once. Until there is one, the clock stays as root made it at boot: real time. It reports how it's doing in ``STATUS``, +which anyone may read and which holds no token: the gateway it follows, whether it's connected, the version it shows, +or its last error. + +The gateway's clock is read against this machine's: its offset is four-stamp, ``((received - t1) + (sent - t4)) / 2``, +from the newest of the last 8 answers whose round trip, less the time the gateway held the watch, isn't an outlier. A +state is written anchored, universe V at this machine's real time A (the gateway's ``sent``) at rate R, so every reader +computes V + (now - A) * R. Each watch names the version it shows, which is its acknowledgement of that version.""" +import json +import os +import queue +import random +import sys +import threading +import time +import urllib.error +import urllib.parse +import urllib.request +from datetime import datetime + +sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) +import clockpage # noqa: E402 + +CONFIG = os.environ.get("AGENTENV_CLOCK_CONFIG", "/etc/agentenv/clock/follower.json") +STATUS = os.environ.get("AGENTENV_CLOCK_STATUS", "/run/agentenv-clock/status.json") +#: How long a watch may take: the gateway holds one up to 25 s (its card's ``hold_s``), plus a margin. +WATCH_TIMEOUT_S = 35.0 +#: How often the config is read, during a watch too. +CONFIG_POLL_S = 0.25 +#: A re-anchoring (the offset moved) is written only past this much, so the page doesn't churn on noise. +ADJUST_S = 0.001 +SAMPLES = 8 +#: After a failed watch it waits before asking again: RETRY_FIRST_S at first, doubled after each failure up to +#: RETRY_MAX_S, each wait a random fraction (RETRY_JITTER) of that, so computers that lost a gateway don't all return at +#: once. +RETRY_FIRST_S = 0.05 +RETRY_MAX_S = 2.0 +RETRY_JITTER = (0.5, 1.0) +OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) + + +def log(event: str, **fields) -> None: + sys.stdout.write(json.dumps({"event": event, "real": time.time(), **fields}) + "\n") + sys.stdout.flush() + + +def atomic_write(path: str, text: str) -> None: + tmp = f"{path}.{os.getpid()}.tmp" + with open(tmp, "w") as f: + f.write(text) + os.chmod(tmp, 0o644) + os.replace(tmp, path) + + +def read_config() -> tuple[bytes | None, dict | None]: + """The config's bytes and what they say, (None, None) when there is none, (bytes, None) when it isn't one.""" + try: + with open(CONFIG, "rb") as f: + raw = f.read() + except OSError: + return None, None + try: + config = json.loads(raw) + return raw, config if isinstance(config, dict) and all( + isinstance(config.get(key), str) and config[key] for key in ("watch_url", "name", "token")) else None + except ValueError: + return raw, None + + +def parse_time(text: str) -> float: + return datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() + + +def offset(samples: list[dict]) -> tuple[float, int]: + """The offset from the newest sample whose delay isn't an outlier (over twice the median and 2 ms), and how many + were dropped.""" + delays = sorted(sample["delay"] for sample in samples) + median = delays[len(delays) // 2] + kept = [sample for sample in samples if sample["delay"] <= max(2 * median, median + 0.002)] + return (kept or samples)[-1]["theta"], len(samples) - len(kept) + + +def state_of(body: dict, theta: float) -> tuple[int, float, float, float]: + """(armed, universe at the anchor, rate, anchor on this machine's clock) for a watch's answer ``body``.""" + if not body.get("armed"): + return 0, 0.0, 1.0, time.time() + return (1, parse_time(body["virtual_time"]), float(body["virtual_seconds_per_real_second"]), + body["sent"] - theta) + + +def watch(url: str, name: str, token: str, after: str, out: queue.Queue) -> None: + """One watch, its answer (or what failed) put on ``out`` with the stamps around it.""" + t1 = time.time() + try: + request = urllib.request.Request( + f"{url}?{urllib.parse.urlencode({'after': after, 'follower': name})}", + headers={"Authorization": f"Bearer {token}"}) + with OPENER.open(request, timeout=WATCH_TIMEOUT_S) as answer: + body = json.loads(answer.read()) + out.put((t1, time.time(), body, None)) + except Exception as e: # noqa: BLE001 - away, restarting or refusing: the follower says so and asks again + out.put((t1, time.time(), None, e)) + + +class Follower: + """Follows one config until it changes.""" + + def __init__(self, raw: bytes, config: dict): + self.raw, self.url, self.name, self.token = raw, config["watch_url"], config["name"], config["token"] + self.after, self.samples, self.written = "", [], None + self.errors, self.backoff = 0, RETRY_FIRST_S + self.status = {"url": self.url, "name": self.name, "connected": False, "version": None, "armed": False, + "error": None} + + def report(self, **fields) -> None: + self.status.update(fields) + atomic_write(STATUS, json.dumps(self.status)) + + def run(self) -> None: + log("following", url=self.url, name=self.name) + self.report() + while True: + answers: queue.Queue = queue.Queue() + threading.Thread(target=watch, args=(self.url, self.name, self.token, self.after, answers), + daemon=True).start() + while True: + try: + t1, t4, body, error = answers.get(timeout=CONFIG_POLL_S) + break + except queue.Empty: + if read_config()[0] != self.raw: + return # its watch is left to end on its own; nothing it answers is applied + if error is not None: + self.failed(error) + continue + if read_config()[0] != self.raw: + return + try: + self.apply(t1, t4, body) + except (KeyError, TypeError, ValueError, AttributeError) as e: + self.failed(ValueError(f"not a clock watch's answer ({type(e).__name__}: {e})")) + + def failed(self, error: Exception) -> None: + self.errors += 1 + code = error.code if isinstance(error, urllib.error.HTTPError) else None + self.report(connected=False, error=f"{type(error).__name__}: {error}"[:300], status=code, errors=self.errors, + error_at=time.time()) + if self.errors <= 20 or self.errors % 100 == 0: + log("error", errors=self.errors, status=code, error=repr(error)[:200], backoff=self.backoff) + deadline = time.monotonic() + self.backoff * random.uniform(*RETRY_JITTER) + while time.monotonic() < deadline and read_config()[0] == self.raw: + time.sleep(min(CONFIG_POLL_S, max(0.0, deadline - time.monotonic()))) + self.backoff = min(self.backoff * 2, RETRY_MAX_S) + + def apply(self, t1: float, t4: float, body: dict) -> None: + if self.errors: + log("reconnected", after_errors=self.errors) + self.errors, self.backoff = 0, RETRY_FIRST_S + held = body["sent"] - body["received"] + self.samples = (self.samples + [{"delay": max(0.0, (t4 - t1) - held), + "theta": ((body["received"] - t1) + (body["sent"] - t4)) / 2}])[-SAMPLES:] + theta, dropped = offset(self.samples) + armed, universe, rate, anchor = state_of(body, theta) + meaning = (armed, rate, universe - rate * anchor if armed else 0.0) + changed = body["version"] != self.after + moved = (self.written is None or meaning[:2] != self.written[:2] + or abs(meaning[2] - self.written[2]) > ADJUST_S) + if changed or moved: + written = clockpage.write(armed, universe, rate, anchor) + self.written = meaning + log("applied" if changed else "adjusted", version=body["version"], armed=bool(armed), + virtual_time=body.get("virtual_time"), spec=written["spec"], theta=theta, held=held, dropped=dropped) + self.after = body["version"] + self.report(connected=True, version=self.after, armed=bool(armed), virtual_time=body.get("virtual_time"), + rate=body.get("virtual_seconds_per_real_second"), theta=theta, applied_at=time.time(), error=None, + status=None, errors=0) + + +def main() -> None: + log("start", pid=os.getpid(), uid=os.getuid()) + atomic_write(STATUS, json.dumps({"connected": False, "error": None, "waiting": "no config yet"})) + while True: + raw, config = read_config() + if config is None: + if raw is not None: + atomic_write(STATUS, json.dumps({"connected": False, "error": "its config isn't one: it needs " + "watch_url, name and token"})) + while read_config()[0] == raw: + time.sleep(CONFIG_POLL_S) + continue + Follower(raw, config).run() + + +if __name__ == "__main__": + main() diff --git a/src/agent_env/agents/agents/claude-code/clock/clock.conf b/src/agent_env/agents/agents/claude-code/clock/clock.conf new file mode 100644 index 000000000..715987d43 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clock.conf @@ -0,0 +1,11 @@ +# Read once by agent-env's libfaketime as each process loads it. While this file exists it is the only source of these +# settings: the environment changes none of them (AGENTENV_REAL_TIME=1 is the one switch a program's environment has). +# Only the session user's processes are faked; the clock comes from the page the follower writes, never from a cache. +AGENTENV_SESSION_UID=1000 +FAKETIME_TIMESTAMP_FILE=/run/agentenv-clock/clock +FAKETIME_NO_CACHE=1 +FAKETIME_DONT_FAKE_MONOTONIC=1 +FAKETIME_FMT=%s +FAKETIME_DISABLE_SHM=1 +AGENTENV_CLOCK_PAGE=/dev/shm/agentenv-clock +AGENTENV_CLOCK_PAGE_UID=0 diff --git a/src/agent_env/agents/agents/claude-code/clock/clockpage.py b/src/agent_env/agents/agents/claude-code/clock/clockpage.py new file mode 100644 index 000000000..4002bb6c2 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/clockpage.py @@ -0,0 +1,157 @@ +"""The follower's side of the clock page (version 2) and of the clock file: writes the universe's realtime +clock to both. Elapsed time is real, so the page's monotonic fields are written as the real monotonic clock (identity) +and no library reads them; they stay so the page keeps its 80-byte layout. + + clockpage.py set UNIVERSE RATE [ANCHOR] armed: UNIVERSE (epoch s) at real time ANCHOR (default now), at RATE + clockpage.py clear real time + clockpage.py create OWNER as root at boot: the page, OWNER's from its first byte, unarmed + clockpage.py show + +The page (default /dev/shm/agentenv-clock, a tmpfs, so readers that mapped it see every write) is created once, 0644, +owned by whoever writes it, and rewritten in place: seq odd, the fields and their check, seq even, then FUTEX_WAKE on +seq, which wakes every wait agent-env's libfaketime has for a clock time, and its helper threads, so they plan again. +Readers never see a torn value: they retry (waiting on seq) while seq is odd or changed, or the check doesn't match. +A page a writer died writing (seq left odd) is taken up by the next write, which then writes the universe as given. + +A write that would put the universe less than CONTINUITY_NS behind where the page has it now (a rate change computed +from a universe read a moment before the write) continues it from where it is instead: the write's real time is taken +last, while seq is odd, and the universe at it read from the page being replaced, so no reader of either page ever sees +the clock step back. A larger step back is a set_time and is written as given.""" +import ctypes +import mmap +import os +import platform +import pwd +import stat +import struct +import sys +import time +from decimal import Decimal + +PAGE = os.environ.get("AGENTENV_CLOCK_PAGE", "/dev/shm/agentenv-clock") +FILE = os.environ.get("AGENTENV_CLOCK_FILE", "/run/agentenv-clock/clock") +MAGIC, VERSION = 0x4B434541, 2 +# magic version seq universe_ns anchor_ns rate armed reserved generation check mono_universe_ns mono_anchor_ns +LAYOUT = struct.Struct(" int: + rate_bits = struct.unpack(" bytes: + """The page's bytes for these fields, its check among them: what every write writes and every reader checks, + agent-env's libfaketime and the egress proxy (its egress/testdata pins them).""" + return LAYOUT.pack(MAGIC, VERSION, seq, universe_ns, anchor_ns, rate, armed, 0, generation, + check(seq, universe_ns, anchor_ns, rate, armed, generation, mono_universe_ns, mono_anchor_ns), + mono_universe_ns, mono_anchor_ns) + + +def open_page() -> mmap.mmap: + try: + fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o644) + os.ftruncate(fd, LAYOUT.size) + os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) + except FileExistsError: + fd = os.open(PAGE, os.O_RDWR) + if os.fstat(fd).st_uid != os.getuid(): + raise SystemExit(f"{PAGE} isn't ours") + if os.fstat(fd).st_size < LAYOUT.size: + os.close(fd) + raise SystemExit(f"{PAGE} is an older page") + m = mmap.mmap(fd, LAYOUT.size) + os.close(fd) + return m + + +def _wake(m: mmap.mmap) -> None: + """FUTEX_WAKE every waiter on seq's low word (a shared futex: other processes' read-only mappings are woken too).""" + if _SYS_FUTEX is None: + return + buf = (ctypes.c_char * LAYOUT.size).from_buffer(m) + try: + _libc.syscall(ctypes.c_long(_SYS_FUTEX), ctypes.c_void_p(ctypes.addressof(buf) + 8), ctypes.c_int(_FUTEX_WAKE), + ctypes.c_int(0x7FFFFFFF), None, None, ctypes.c_int(0)) + finally: + del buf + + +def _universe_at(armed, universe_ns, anchor_ns, rate, now_ns) -> int: + if not armed: + return now_ns + if rate == 0 or now_ns <= anchor_ns: + return universe_ns + return universe_ns + int((now_ns - anchor_ns) * rate) + + +def write(armed: int, universe: float, rate: float, anchor: float) -> dict: + m = open_page() + _, _, seq, o_u, o_a, o_rate, o_armed, _, generation, _, _, _ = LAYOUT.unpack(m[:LAYOUT.size]) + universe_ns, anchor_ns = int(Decimal(str(universe)) * 10**9), int(Decimal(str(anchor)) * 10**9) + if not armed: + rate = 1.0 + generation += 1 + dead = seq & 1 # a writer died mid-write: its fields may be torn, so this write goes on from the next even seq + seq += dead + m[8:16] = struct.pack(" 1 and not dead and 0 < old_now - new_now < CONTINUITY_NS + if continued: + universe_ns, anchor_ns = old_now, now_rt + body = encode(seq + 2, universe_ns, anchor_ns, rate, armed, generation, now_mono, now_mono) + m[16:] = body[16:] + m[8:16] = struct.pack(" 1 else 0, + "continued": bool(continued), "universe_ns": universe_ns, "anchor_ns": anchor_ns} + + +def create(owner: str) -> None: + """As root at boot: the page, ``owner``'s from its first byte (unarmed), replacing one anyone else made.""" + user = pwd.getpwnam(owner) + try: + st = os.lstat(PAGE) + if stat.S_ISREG(st.st_mode) and st.st_uid == user.pw_uid and st.st_size == LAYOUT.size: + return + os.unlink(PAGE) + except FileNotFoundError: + pass + fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + try: + os.fchown(fd, user.pw_uid, user.pw_gid) + os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) + os.fchmod(fd, 0o644) + finally: + os.close(fd) + + +if __name__ == "__main__": + cmd = sys.argv[1] + if cmd == "set": + anchor = float(sys.argv[4]) if len(sys.argv) > 4 else time.time() + print(write(1, float(sys.argv[2]), float(sys.argv[3]), anchor)) + elif cmd == "create": + create(sys.argv[2]) + elif cmd == "clear": + print(write(0, 0.0, 1.0, time.time())) + else: + with open(PAGE, "rb") as f: + print(LAYOUT.unpack(f.read(LAYOUT.size))) diff --git a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch new file mode 100644 index 000000000..d26148eed --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch @@ -0,0 +1,1527 @@ +SPDX-License-Identifier: GPL-2.0-only +agent-env's changes to libfaketime 0.9.12 (src/), applied from the tarball's root with patch -p1 by build.sh, which +also copies in agentenv_clock_page.c and agentenv_clock_waits.c, included where marked below. A change to libfaketime, +so licensed as libfaketime is, under the GNU General Public License version 2 (COPYING). + +diff -ruN a/src/libfaketime.c b/src/libfaketime.c +--- a/src/libfaketime.c ++++ b/src/libfaketime.c +@@ -231,11 +231,14 @@ + static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp); + #endif + #ifdef FAKE_PTHREAD +-static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); +-static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); +-static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict); +-static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); +-static pthread_rwlock_t monotonic_conds_lock; ++static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); ++static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); ++static int (*real_pthread_cond_clockwait) (pthread_cond_t *, pthread_mutex_t*, clockid_t, const struct timespec *); ++static int (*real_pthread_mutex_timedlock) (pthread_mutex_t *, const struct timespec *); ++static int (*real_pthread_mutex_clocklock) (pthread_mutex_t *, clockid_t, const struct timespec *); ++#endif ++#ifdef FAKE_TIMERS ++static int (*real_timerfd_create) (int, int); + #endif + + #ifndef __APPLE__ +@@ -334,6 +337,7 @@ + bool str_array_contains(const char *haystack, const char *needle); + void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym); + ++#include "agentenv_clock_page.c" /* agent-env: settings, who is faked, the clock page */ + /** Semaphore protecting shared data */ + static sem_t *shared_sem = NULL; + +@@ -420,6 +424,29 @@ + static bool parse_config_file = true; + + static void ft_cleanup (void) __attribute__ ((destructor)); ++ ++#ifdef PTHREAD_SINGLETHREADED_TIME ++/* agentenv: the lock fake_clock_gettime holds, at file scope so a fork() can't hand the child a held lock. */ ++static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; ++static sigset_t ft_fork_mask; ++static void ft_atfork_prepare(void) ++{ ++ sigset_t all; ++ sigfillset(&all); ++ pthread_sigmask(SIG_SETMASK, &all, &ft_fork_mask); ++ pthread_mutex_lock(&time_mutex); ++} ++static void ft_atfork_parent(void) ++{ ++ pthread_mutex_unlock(&time_mutex); ++ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); ++} ++static void ft_atfork_child(void) ++{ ++ pthread_mutex_init(&time_mutex, NULL); ++ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); ++} ++#endif + static void ftpl_init (void) __attribute__ ((constructor)); + + +@@ -519,7 +546,7 @@ + + static void ft_shm_destroy(void) + { +- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); ++ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); + + if (ft_shared_env != NULL) + { +@@ -553,7 +580,7 @@ + static void ft_shm_really_init (void) + { + int ticks_shm_fd; +- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); ++ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); + sem_t *shared_semR = NULL; + static int nt=1; + +@@ -561,7 +588,7 @@ + if (ft_shared_env == NULL) + { + ft_shm_create(); +- ft_shared_env = getenv("FAKETIME_SHARED"); ++ ft_shared_env = ft_getenv("FAKETIME_SHARED"); + } + + /* check for stale semaphore / shared memory information */ +@@ -575,7 +602,7 @@ + if (SEM_FAILED == (shared_semR = sem_open(sem_name, 0))) /* gone stale? */ + { + ft_shm_create(); +- ft_shared_env = getenv("FAKETIME_SHARED"); ++ ft_shared_env = ft_getenv("FAKETIME_SHARED"); + } + else + { +@@ -631,7 +658,7 @@ + exit(1); + } + } +- if (getenv("FAKETIME_FLSHM") != NULL) ++ if (ft_getenv("FAKETIME_FLSHM") != NULL) + { /* force the deletion of the shm sync env variable */ + unsetenv("FAKETIME_SHARED"); + } +@@ -653,12 +680,6 @@ + sem_close(shared_sem); + shared_sem = NULL; + } +-#ifdef FAKE_PTHREAD +- if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) { +- fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n"); +- exit(-1); +- } +-#endif + if (shmCreator == true) ft_shm_destroy(); + } + +@@ -672,8 +693,8 @@ + static void get_fake_monotonic_setting(int* current_value) + { + char *tmp_env; +- if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL +- || (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL ++ || (tmp_env = ft_getenv("DONT_FAKE_MONOTONIC")) != NULL) + { + if (0 == strcmp(tmp_env, "1")) + { +@@ -1140,7 +1161,7 @@ + #endif /* ifndef __APPLE__ */ + #endif /* ifdef FAKE_STAT */ + +-#ifdef STATX_TYPE ++#if defined STATX_TYPE && defined FAKE_STAT + static inline void fake_statx_timestamp(struct statx_timestamp* p) + { + struct timespec t = {p->tv_sec,p->tv_nsec}; +@@ -1329,6 +1350,7 @@ + * Contributed by Balint Reczey in v0.9.5 + */ + ++#include "agentenv_clock_waits.c" /* agent-env: universe-time reads and waits */ + #ifdef FAKE_SLEEP + /* + * Faked nanosleep() +@@ -1343,6 +1365,7 @@ + struct timespec real_req; + + ftpl_init(); ++ if (real_nanosleep != NULL) return (*real_nanosleep)(req, rem); + if (real_nanosleep == NULL) + { + return -1; +@@ -1395,13 +1418,39 @@ + struct timespec real_req; + + ftpl_init(); ++ if (real_clock_nanosleep != NULL && (!(flags & TIMER_ABSTIME) || !ft_rt_clock(clock_id) || !ft_faked())) ++ return (*real_clock_nanosleep)(clock_id, flags, req, rem); ++ if (ft_uw_active() && ft_valid_ts(req)) ++ { ++ struct ft_clock_page s; ++ if (ft_snap(&s)) return ft_rt_sleep(&s, ft_ns(req)); ++ } + if (real_clock_nanosleep == NULL) + { + return -1; + } + if (req != NULL) + { +- if (flags & TIMER_ABSTIME) /* sleep until absolute time */ ++ if ((flags & TIMER_ABSTIME) && clock_id == CLOCK_REALTIME && ft_translates_wait(req)) ++ { ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, req, &mono); ++ gen = ft_tl_gen; ++ for (;;) ++ { ++ last = ft_slice_end(&mono, &slice); ++ DONT_FAKE_TIME(result = (*real_clock_nanosleep)(CLOCK_MONOTONIC, TIMER_ABSTIME, &slice, NULL)); ++ if (result != 0) return result; ++ if (!ft_deadline_moved(req, &mono, &gen) && last) return 0; ++ } ++ } ++ if ((flags & TIMER_ABSTIME) && (ft_disabled || dont_fake || clock_id == CLOCK_REALTIME)) ++ { ++ real_req = *req; ++ } ++ else if (flags & TIMER_ABSTIME) /* sleep until absolute time */ + { + struct timespec tdiff, timeadj; + timespecsub(req, &user_faked_time_timespec, &timeadj); +@@ -1482,6 +1531,7 @@ + int result; + + ftpl_init(); ++ if (real_usleep != NULL) return (*real_usleep)(usec); + if (user_rate_set && !dont_fake) + { + struct timespec real_req; +@@ -1531,6 +1581,7 @@ + #endif + { + ftpl_init(); ++ if (real_sleep != NULL) return (*real_sleep)(seconds); + if (user_rate_set && !dont_fake) + { + if (real_nanosleep == NULL) +@@ -1551,7 +1602,7 @@ + else + { + int result; +- struct timespec real_req = {seconds, 0}, rem; ++ struct timespec real_req = {seconds, 0}, rem = {0, 0}; + timespecmul(&real_req, 1.0 / user_rate, &real_req); + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem)); +@@ -1604,6 +1655,8 @@ + { + return -1; + } ++ (void) seconds_real; ++ return (*real_alarm)(seconds); + + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(ret = (*alarm)(seconds_real)); +@@ -1627,6 +1680,7 @@ + { + return -1; + } ++ return (*real_ppoll)(fds, nfds, timeout_ts, sigmask); + if (timeout_ts != NULL) + { + if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0))) +@@ -1662,6 +1716,7 @@ + { + return -1; + } ++ return (*real_epoll_wait)(epfd, events, maxevents, timeout); + if (user_rate_set && !dont_fake && timeout > 0) + { + real_timeout = (int) timeout * 1.0/user_rate; +@@ -1686,6 +1741,7 @@ + { + return -1; + } ++ return (*real_epoll_pwait)(epfd, events, maxevents, timeout, sigmask); + if (user_rate_set && !dont_fake && timeout > 0) + { + real_timeout = (int) timeout * 1.0/user_rate; +@@ -1715,6 +1771,8 @@ + { + return -1; + } ++ (void) timeout_real; ++ return (*real_poll)(fds, nfds, timeout); + + #ifdef MACOS_DYLD_INTERPOSE + DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real)); +@@ -1748,6 +1806,7 @@ + { + return -1; + } ++ return (*real_select)(nfds, readfds, writefds, errorfds, timeout); + + if (timeout != NULL) + { +@@ -1812,6 +1871,7 @@ + { + return -1; + } ++ return (*real_pselect)(nfds, readfds, writefds, errorfds, timeout, sigmask); + + if (timeout != NULL) + { +@@ -1831,98 +1891,63 @@ + } + #endif + +-int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) ++static int ft_sem_wait_until(sem_t *sem, const struct timespec *fake_abs) + { +- int result; +- struct timespec real_abs_timeout, *real_abs_timeout_pt; +- +- /* sanity check */ +- if (abs_timeout == NULL) +- { +- return -1; +- } +- +- if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; +- +- if (!dont_fake) +- { +- struct timespec tdiff, timeadj; +- +- timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff); +- +- if (user_rate_set) ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ int result; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); ++ gen = ft_tl_gen; ++ for (;;) ++ { ++ last = ft_slice_end(&mono, &slice); ++ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, CLOCK_MONOTONIC, &slice)); ++ if (result == 0 || errno != ETIMEDOUT) return result; ++ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) + { +- timespecmul(&tdiff, user_rate, &timeadj); ++ errno = ETIMEDOUT; ++ return -1; + } +- else +- { +- timeadj = tdiff; +- } +- timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout); +- real_abs_timeout_pt = &real_abs_timeout; +- } +- else +- { +- /* cast away constness */ +- real_abs_timeout_pt = (struct timespec *)abs_timeout; + } +- +- DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt)); +- return result; + } + +-/* EXPERIMENTAL */ +-int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) ++int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) + { + int result; +- struct timespec real_abstime, *real_abstime_pt; +- +- if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC)) ++ ftpl_init(); ++ if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; ++ if (ft_uw_active() && ft_translates_wait(abs_timeout)) + { +- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); +- return result; ++ bool handled; ++ result = ft_u_sem_wait(sem, ft_ns(abs_timeout), &handled); ++ if (handled) return result; + } +- +- /* sanity check */ +- if (abstime == NULL) ++ if (!ft_translates_wait(abs_timeout) || real_sem_clockwait == NULL) + { +- return -1; ++ DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, abs_timeout)); ++ return result; + } ++ return ft_sem_wait_until(sem, abs_timeout); ++} + ++int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) ++{ ++ int result; ++ ftpl_init(); + if (!CHECK_MISSING_REAL(sem_clockwait)) return -1; +- +- if (!dont_fake) ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) + { +- struct timespec tdiff, timeadj; +- +- timespecsub(abstime, &user_faked_time_timespec, &tdiff); +- +- if (user_rate_set) +- { +- timespecmul(&tdiff, 1.0 / user_rate, &timeadj); +- } +- else +- { +- timeadj = tdiff; +- } +- if (clockid == CLOCK_REALTIME) +- { +- timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime); +- } +- if (clockid == CLOCK_MONOTONIC) +- { +- timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime); +- } +- real_abstime_pt = &real_abstime; ++ bool handled; ++ result = ft_u_sem_wait(sem, ft_ns(abstime), &handled); ++ if (handled) return result; + } +- else ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) + { +- /* cast away constness */ +- real_abstime_pt = (struct timespec *)abstime; ++ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); ++ return result; + } +- +- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt)); +- return result; ++ return ft_sem_wait_until(sem, abstime); + } + #endif + +@@ -1949,6 +1974,23 @@ + * Faked timer_settime() + * Does not affect timer speed when stepping clock with each time() call. + */ ++#ifdef __linux__ ++/* agentenv: the clock each timerfd was made on (ft_tfd_clock); a record still naming the number is stale. */ ++int timerfd_create(int clockid, int flags) ++{ ++ int fd; ++ ftpl_init(); ++ if (real_timerfd_create == NULL) return -1; ++ fd = real_timerfd_create(clockid, flags); ++ if (fd >= 0 && fd < FT_TFD_CLOCKS && ft_fd_mine()) ++ { ++ ft_fd_gone(fd); ++ ft_tfd_clock[fd] = clockid + 1; ++ } ++ return fd; ++} ++#endif ++ + static int + timer_settime_common(timer_t_or_int timerid, int flags, + const struct itimerspec *new_value, +@@ -1977,18 +2019,11 @@ + { + if (flags & abstime_flag) + { +- struct timespec tdiff, timeadj; +- timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj); +- if (user_rate_set) +- { +- timespecmul(&timeadj, 1.0/user_rate, &tdiff); +- } ++ if (ft_disabled || (compat == FT_FD && ft_tfd_known(timerid.int_member) ++ && !ft_rt_clock(ft_tfd_clock[timerid.int_member] - 1))) ++ new_real.it_value = new_value->it_value; + else +- { +- tdiff = timeadj; +- } +- /* only CLOCK_REALTIME is handled */ +- timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value); ++ ft_deadline_from_fake(CLOCK_REALTIME, &new_value->it_value, &new_real.it_value); + } + else + { +@@ -2061,47 +2096,6 @@ + return result; + } + +-/* +- * Faked timer_settime() compatible with implementation in GLIBC 2.2 +- */ +-int timer_settime_22(int timerid, int flags, +- const struct itimerspec *new_value, +- struct itimerspec *old_value) +-{ +- ftpl_init(); +- if (real_timer_settime_22 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.int_member = timerid; +- return (timer_settime_common(temp, flags, new_value, old_value, +- FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME)); +- } +-} +- +-/* +- * Faked timer_settime() compatible with implementation in GLIBC 2.3.3 +- */ +-int timer_settime_233(timer_t timerid, int flags, +- const struct itimerspec *new_value, +- struct itimerspec *old_value) +-{ +- ftpl_init(); +- if (real_timer_settime_233 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_settime_common(temp, flags, new_value, old_value, +- FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME)); +- } +-} + + /* + * Faked timer_gettime() +@@ -2151,48 +2145,6 @@ + return result; + } + +-/* +- * Faked timer_gettime() compatible with implementation in GLIBC 2.2 +- */ +-int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value) +-{ +- ftpl_init(); +- if (real_timer_gettime_22 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_gettime_common(temp, curr_value, +- FT_COMPAT_GLIBC_2_2)); +- } +-} +- +-/* +- * Faked timer_gettime() compatible with implementation in GLIBC 2.3.3 +- */ +-int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value) +-{ +- ftpl_init(); +- if (real_timer_gettime_233 == NULL) +- { +- return -1; +- } +- else +- { +- timer_t_or_int temp; +- temp.timer_t_member = timerid; +- return (timer_gettime_common(temp, curr_value, +- FT_COMPAT_GLIBC_2_3_3)); +- } +-} +- +-__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2"); +-__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3"); +-__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2"); +-__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3"); + + #ifdef __linux__ + /* +@@ -2203,6 +2155,14 @@ + struct itimerspec *old_value) + { + ftpl_init(); ++ if (real_timerfd_settime != NULL && ft_faked()) ++ { ++ bool handled = false; ++ int r = ft_uw_active() ? ft_u_timerfd_settime(fd, flags, new_value, old_value, &handled) : 0; ++ if (handled) return r; ++ if (!(flags & TFD_TIMER_ABSTIME) || fd < 0 || fd >= FT_TFD_CLOCKS || !ft_rt_clock(ft_tfd_clock[fd] - 1)) ++ return (*real_timerfd_settime)(fd, flags, new_value, old_value); ++ } + if (real_timerfd_settime == NULL) + { + return -1; +@@ -2222,6 +2182,13 @@ + int timerfd_gettime(int fd, struct itimerspec *curr_value) + { + ftpl_init(); ++ if (real_timerfd_gettime != NULL && ft_faked()) ++ { ++ bool handled = false; ++ int r = ft_u_timerfd_gettime(fd, curr_value, &handled); ++ if (handled) return r; ++ return (*real_timerfd_gettime)(fd, curr_value); ++ } + if (real_timerfd_gettime == NULL) + { + return -1; +@@ -2379,6 +2346,8 @@ + #endif + if (result == -1) return result; /* original function failed */ + ++ if (!ft_universe_clock(clk_id)) return result; ++ + /* pass the real current time to our faking version, overwriting it */ + if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW + #ifdef CLOCK_MONOTONIC_COARSE +@@ -2495,12 +2464,25 @@ + return; + } + ++ /* agentenv: a specification without "x" is at rate 1, not at the rate of the one before it */ ++ double prev_rate = user_rate; ++ bool prev_rate_set = user_rate_set; ++ if (user_faked_time[0] == 0) return; ++ user_rate = 1.0; ++ user_rate_set = false; ++ + /* check whether the user gave us an absolute time to fake */ + switch (user_faked_time[0]) + { + + default: /* Try and interpret this as a specified time */ + if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE; ++ if (strcmp(user_faked_time_fmt, "%s") == 0) ++ { ++ if (ft_parse_epoch(user_faked_time, &user_faked_time_timespec) == NULL) goto parse_failed; ++ user_faked_time_set = true; ++ goto parse_modifiers; ++ } + user_faked_time_tm.tm_isdst = -1; + nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); + +@@ -2518,9 +2500,7 @@ + } + else + { +- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n" +- "Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt); +- exit(EXIT_FAILURE); ++ goto parse_failed; + } + goto parse_modifiers; + break; +@@ -2548,6 +2528,11 @@ + /* Contributed by David North, TDI in version 0.7 */ + case '@': /* Specific time, but clock along relative to that starttime */ + ft_mode = FT_START_AT; ++ if (strcmp(user_faked_time_fmt, "%s") == 0) ++ { ++ if (ft_parse_epoch(&user_faked_time[1], &user_faked_time_timespec) == NULL) goto parse_failed; ++ goto parse_anchor; ++ } + user_faked_time_tm.tm_isdst = -1; + nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm); + if (NULL != nstime_str) +@@ -2563,12 +2548,17 @@ + } + else + { +- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"); +- exit(EXIT_FAILURE); ++ goto parse_failed; + } + +- /* Reset starttime */ +- if (NULL == getenv("FAKETIME_DONT_RESET")) ++parse_anchor: ++ /* agentenv: "^" is the real time at which the clock read the time given, so every process reads the ++ same time whenever it started, with no shared memory; without it, the process's own start, as before. */ ++ if (strchr(user_faked_time, '^') != NULL) ++ { ++ if (ft_parse_epoch(strchr(user_faked_time, '^') + 1, &ftpl_starttime.real) == NULL) goto parse_failed; ++ } ++ else if (NULL == ft_getenv("FAKETIME_DONT_RESET")) + reset_time(); + goto parse_modifiers; + break; +@@ -2577,17 +2567,17 @@ + ft_mode = FT_START_AT; + struct stat master_file_stats; + int ret; +- if (NULL == getenv("FAKETIME_FOLLOW_FILE")) ++ if (NULL == ft_getenv("FAKETIME_FOLLOW_FILE")) + { + fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n"); + exit(1); + } + else + { +- DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); ++ DONT_FAKE_TIME(ret = stat(ft_getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); + if (ret == -1) + { +- fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE")); ++ fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", ft_getenv("FAKETIME_FOLLOW_FILE")); + exit(1); + } + else +@@ -2596,7 +2586,7 @@ + user_faked_time_timespec.tv_nsec = 0; + } + } +- if (NULL == getenv("FAKETIME_DONT_RESET")) ++ if (NULL == ft_getenv("FAKETIME_DONT_RESET")) + reset_time(); + goto parse_modifiers; + break; +@@ -2609,7 +2599,15 @@ + { + user_rate = atof(strchr(user_faked_time, 'x')+1); + user_rate_set = true; +- if (NULL != getenv("FAKETIME_XRESET")) { ++ if (user_rate <= 0.0) ++ { ++ /* agentenv: rate 0 is the time given, frozen; sleeps and waits then take the real time they ask for */ ++ ft_mode = FT_FREEZE; ++ user_faked_time_set = true; ++ user_rate = 1.0; ++ user_rate_set = false; ++ } ++ if (NULL != ft_getenv("FAKETIME_XRESET")) { + if (ftpl_timecache.real.tv_nsec >= 0) { + user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec; + user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec; +@@ -2639,9 +2637,15 @@ + + strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1); + user_faked_time_saved[BUFFERLEN-1] = 0; ++ ft_spec_gen++; + #ifdef DEBUG + fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved); + #endif ++ return; ++ ++parse_failed: ++ user_rate = prev_rate; ++ user_rate_set = prev_rate_set; + } + + +@@ -2666,8 +2670,8 @@ + const char *progname = __progname; + #endif + +- char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS"); +- bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM"); ++ char *ignore_list = ft_getenv("FAKETIME_IGNORE_SYMBOLS"); ++ bool should_debug_dlsym = ft_getenv("FAKETIME_DEBUG_DLSYM"); + #define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym) + #define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym) + +@@ -2749,32 +2753,25 @@ + #endif + + #ifdef FAKE_PTHREAD +- + #ifdef __GLIBC__ + real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5"); +- + real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2"); +- real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2"); +- real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2"); ++ real_pthread_cond_clockwait = dlvsym(RTLD_NEXT, "pthread_cond_clockwait", "GLIBC_2.30"); ++ real_pthread_mutex_clocklock = dlvsym(RTLD_NEXT, "pthread_mutex_clocklock", "GLIBC_2.30"); + #endif +- + if (NULL == real_pthread_cond_timedwait_232) + { +- real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); ++ real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); + } +- if (NULL == real_pthread_cond_init_232) ++ if (NULL == real_pthread_cond_clockwait) + { +- real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init"); ++ real_pthread_cond_clockwait = dlsym(RTLD_NEXT, "pthread_cond_clockwait"); + } +- if (NULL == real_pthread_cond_destroy_232) ++ if (NULL == real_pthread_mutex_clocklock) + { +- real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy"); +- } +- +- if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) { +- fprintf(stderr,"monotonic_conds_lock init failed\n"); +- exit(-1); ++ real_pthread_mutex_clocklock = dlsym(RTLD_NEXT, "pthread_mutex_clocklock"); + } ++ real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); + #endif + #ifdef __APPLE__ + real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); +@@ -2814,6 +2811,7 @@ + #endif + #ifdef __linux__ + real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime"); ++ real_timerfd_create = dlsym(RTLD_NEXT, "timerfd_create"); + real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime"); + #endif + #endif +@@ -2826,16 +2824,42 @@ + #undef dlsym + #undef dlvsym + ++ /* agentenv: a process of another user than the session's, or one started with AGENTENV_REAL_TIME=1, stays on real ++ time; the marker is taken out of its environment, so what it starts is faked. */ ++ { ++ const char *uid = ft_getenv("AGENTENV_SESSION_UID"); ++ const char *marker = getenv("AGENTENV_REAL_TIME"); ++ if (uid != NULL && *uid != 0 && (uid_t) strtoul(uid, NULL, 10) != getuid()) ++ ft_disabled = true; ++ if (marker != NULL && strcmp(marker, "1") == 0) ++ { ++ ft_disabled = true; ++ ft_scrub_marker(); ++ } ++ if ((tmp_env = ft_getenv("AGENTENV_WAIT_SLICE_MS")) != NULL && atol(tmp_env) > 0) ++ ft_wait_slice_ns = atol(tmp_env) * 1000000L; ++ } ++ if (ft_disabled) ++ { ++ ft_mode = FT_NOOP; ++ dont_fake = dont_fake_final; ++ return; ++ } ++#ifdef PTHREAD_SINGLETHREADED_TIME ++ pthread_atfork(ft_atfork_prepare, ft_atfork_parent, ft_atfork_child); ++#endif ++ ft_page_open(); ++ + #ifdef FAKE_STATELESS + if (0) ft_shm_init(); + #else +- tmp_env = getenv("FAKETIME_DISABLE_SHM"); ++ tmp_env = ft_getenv("FAKETIME_DISABLE_SHM"); + if (!tmp_env || tmp_env[0] == '0') { + ft_shm_init(); + } + #endif + #ifdef FAKE_STAT +- if (getenv("NO_FAKE_STAT")!=NULL) ++ if (ft_getenv("NO_FAKE_STAT")!=NULL) + { + fake_stat_disabled = 1; //Note that this is NOT re-checked + } +@@ -2843,7 +2867,7 @@ + #if defined FAKE_FILE_TIMESTAMPS + #ifdef FAKE_UTIME + // fake_utime_disabled is 0 by default +- if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked ++ if ((tmp_env = ft_getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked + { + if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T') + { /* an empty string or a yes/true value turns off disabling */ +@@ -2860,11 +2884,11 @@ + #endif + #endif + +- if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_CACHE_DURATION")) != NULL) + { + cache_duration = atoi(tmp_env); + } +- if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_NO_CACHE")) != NULL) + { + if (0 == strcmp(tmp_env, "1")) + { +@@ -2875,7 +2899,7 @@ + /* Check whether we actually should be faking the returned timestamp. */ + + /* We can prevent faking time for specified commands */ +- if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SKIP_CMDS")) != NULL) + { + char *skip_cmd, *saveptr, *tmpvar; + /* Don't mess with the env variable directly. */ +@@ -2904,12 +2928,12 @@ + } + + /* We can limit faking time to specified commands */ +- if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_ONLY_CMDS")) != NULL) + { + char *only_cmd, *saveptr, *tmpvar; + bool cmd_matched = false; + +- if (getenv("FAKETIME_SKIP_CMDS") != NULL) ++ if (ft_getenv("FAKETIME_SKIP_CMDS") != NULL) + { + fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n"); + exit(EXIT_FAILURE); +@@ -2941,44 +2965,44 @@ + } + } + +- if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) + { + ft_start_after_secs = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) + { + ft_stop_after_secs = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) + { + ft_start_after_ncalls = atol(tmp_env); + limited_faking = true; + } +- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) + { + ft_stop_after_ncalls = atol(tmp_env); + limited_faking = true; + } + + /* check whether we should spawn an external command */ +- if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_TARGET")) != NULL) + { + spawnsupport = true; +- (void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); ++ (void) strncpy(ft_spawn_target, ft_getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); + ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0; +- if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_SECONDS")) != NULL) + { + ft_spawn_secs = atol(tmp_env); + } +- if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) + { + ft_spawn_ncalls = atol(tmp_env); + } + } + +- if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_SAVE_FILE")) != NULL) + { + if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT, + S_IWUSR | S_IRUSR))) +@@ -2989,7 +3013,7 @@ + } + + /* load file only if reading timestamps from it is not finished yet */ +- if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL) ++ if ((tmp_env = ft_getenv("FAKETIME_LOAD_FILE")) != NULL) + { + int infile = -1; + struct stat sb; +@@ -3021,7 +3045,7 @@ + infile_set = true; + } + +- tmp_env = getenv("FAKETIME_FMT"); ++ tmp_env = ft_getenv("FAKETIME_FMT"); + if (tmp_env == NULL) + { + strcpy(user_faked_time_fmt, "%Y-%m-%d %T"); +@@ -3061,7 +3085,7 @@ + system_time_from_system(&ftpl_starttime); + } + /* fake time supplied as environment variable? */ +- if (NULL != (tmp_env = getenv("FAKETIME"))) ++ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) + { + parse_config_file = false; + parse_ft_string(tmp_env); +@@ -3070,6 +3094,11 @@ + { + read_config_file(); + } ++ if (ft_page != NULL) ++ { ++ struct ft_clock_page s; ++ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); ++ } + + dont_fake = dont_fake_final; + } +@@ -3209,8 +3238,8 @@ + * a system-wide /etc/faketimerc present. + * The /etc/faketimerc handling has been contributed by David Burley, + * Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */ +- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); +- (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME")); ++ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); ++ (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", ft_getenv("HOME")); + if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 || + (faketimerc = open(filename, O_RDONLY)) != -1 || + (faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1) +@@ -3248,6 +3277,48 @@ + tp_save.tv_nsec = tp->tv_nsec; + + if (dont_fake) return 0; ++ if (ft_disabled) return 0; ++ if (ft_page != NULL && (clk_id == CLOCK_REALTIME || clk_id == CLOCK_REALTIME_COARSE)) ++ { ++ struct ft_clock_page s; ++ if (ft_snap_at(clk_id, &s, tp)) ++ { ++ if (s.generation != __atomic_load_n(&ft_page_applied, __ATOMIC_ACQUIRE)) ++ { ++#ifdef PTHREAD_SINGLETHREADED_TIME ++ sigset_t all, old; ++ sigfillset(&all); ++ pthread_sigmask(SIG_SETMASK, &all, &old); ++ pthread_mutex_lock(&time_mutex); ++ ft_page_apply_locked(&s); ++ pthread_mutex_unlock(&time_mutex); ++ pthread_sigmask(SIG_SETMASK, &old, NULL); ++#else ++ ft_page_apply_locked(&s); ++#endif ++ } ++ ft_tl_gen = s.generation; ++ ft_tl_scale = (s.armed && s.rate > 0) ? 1.0 / s.rate : 1.0; ++ if (s.armed) ++ { ++ int64_t now = (int64_t) tp->tv_sec * 1000000000LL + tp->tv_nsec, fake; ++ if (s.rate == 0 || now <= s.anchor_ns) ++ fake = s.universe_ns; ++ else if (s.rate == 1.0) ++ fake = s.universe_ns + (now - s.anchor_ns); ++ else ++ fake = s.universe_ns + (int64_t) ((double) (now - s.anchor_ns) * s.rate); ++ tp->tv_sec = fake / 1000000000LL; ++ tp->tv_nsec = fake % 1000000000LL; ++ if (tp->tv_nsec < 0) ++ { ++ tp->tv_sec -= 1; ++ tp->tv_nsec += 1000000000LL; ++ } ++ } ++ return 0; ++ } ++ } + /* Per process timers are only sped up or slowed down */ + if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID)) + { +@@ -3262,7 +3333,6 @@ + volatile int ret = INT_MAX; + + #ifdef PTHREAD_SINGLETHREADED_TIME +- static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; + + // block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation. + sigset_t all_signals, original_mask; +@@ -3367,6 +3437,19 @@ + force_cache_expiration = 0; + } + ++ /* agentenv: with the clock page mapped, the file isn't read; until it is, look for it once a second */ ++ if (ft_page == NULL && ft_getenv("AGENTENV_CLOCK_PAGE") != NULL && current_ts.tv_sec != ft_page_last_try) ++ { ++ ft_page_last_try = current_ts.tv_sec; ++ ft_page_open(); ++ if (ft_page != NULL) ++ { ++ struct ft_clock_page s; ++ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); ++ } ++ } ++ if (ft_page != NULL) cache_expired = 0; ++ + if (cache_expired == 1) + { + static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */ +@@ -3377,7 +3460,7 @@ + fprintf(stderr, "***************++ Cache expired ++**************\n"); + */ + +- if (NULL != (tmp_env = getenv("FAKETIME"))) ++ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) + { + strncpy(user_faked_time, tmp_env, BUFFERLEN - 1); + user_faked_time[BUFFERLEN - 1] = 0; +@@ -3401,6 +3484,9 @@ + get_fake_monotonic_setting(&fake_monotonic_clock); + } /* cache had expired */ + ++ ft_tl_gen = ft_spec_gen; ++ ft_tl_scale = (ft_mode == FT_START_AT && user_rate_set && user_rate > 0) ? 1.0 / user_rate : 1.0; ++ + if (infile_set) + { + if (load_time(tp)) +@@ -3520,6 +3606,7 @@ + { + struct timespec ts; + int ret; ++ if (ft_disabled) return 0; + ts.tv_sec = tv->tv_sec; + ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000; + +@@ -3627,6 +3714,8 @@ + DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp)); + if (result == -1) return result; /* original function failed */ + ++ if (!ft_universe_clock(clk_id)) return result; ++ + /* pass the real current time to our faking version, overwriting it */ + if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW + #ifdef CLOCK_MONOTONIC_COARSE +@@ -3723,238 +3812,124 @@ + + #ifdef FAKE_PTHREAD + +-typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread; +- +-struct pthread_cond_monotonic { +- pthread_cond_t *ptr; +- UT_hash_handle hh; +-}; +- +-static struct pthread_cond_monotonic *monotonic_conds = NULL; ++/* ++ * agentenv: a wait with a CLOCK_REALTIME deadline waits on CLOCK_MONOTONIC instead, until ft_deadline_from_fake's ++ * deadline. A condvar's clock is read from glibc's own condvar word (bit 1 of __wrefs, which glibc's waits read, since ++ * glibc 2.25) instead of a table kept by intercepting pthread_cond_init and pthread_cond_destroy; nothing here calls ++ * getenv() or malloc() or takes a lock. A condvar wait isn't cut into slices, since a slice ending would be a wakeup ++ * its caller must recheck, so a jump or a change of rate reaches it only when it ends; a mutex wait is cut. ++ */ ++#define FT_COND_CLOCK_MONOTONIC 2 + +-int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) ++int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) + { +- clockid_t clock_id; +- int result; +- ++ struct timespec mono; + ftpl_init(); +- if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1; +- result = real_pthread_cond_init_232(cond, attr); +- +- if (result != 0 || attr == NULL) +- return result; +- +- pthread_condattr_getclock(attr, &clock_id); +- +- if (clock_id == CLOCK_MONOTONIC) { +- struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic)); +- e->ptr = cond; +- +- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_ADD_PTR(monotonic_conds, ptr, e); +- pthread_rwlock_unlock(&monotonic_conds_lock); ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL ++ && !(__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) ++ { ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- +- return result; ++ if (!ft_translates_wait(abstime) || real_pthread_cond_clockwait == NULL ++ || (__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) ++ return real_pthread_cond_timedwait_232(cond, mutex, abstime); ++ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); ++ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); + } + +-int pthread_cond_destroy_232(pthread_cond_t *cond) ++int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) + { +- struct pthread_cond_monotonic* e; +- ++ struct timespec real; + ftpl_init(); +- +- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_FIND_PTR(monotonic_conds, &cond, e); +- if (e) { +- HASH_DEL(monotonic_conds, e); +- free(e); ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL) ++ { ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- pthread_rwlock_unlock(&monotonic_conds_lock); +- +- return real_pthread_cond_destroy_232(cond); ++ if (!ft_translates_wait(abstime)) ++ return real_pthread_cond_timedwait_225(cond, mutex, abstime); ++ ft_deadline_from_fake(CLOCK_REALTIME, abstime, &real); ++ return real_pthread_cond_timedwait_225(cond, mutex, &real); + } + +-/* +- * Check whether we need a run-time activation of the +- * forced monotonic fix to avoid faked / unfaked timestamp +- * confusion between the application and glibc internals. +- */ +-bool needs_forced_monotonic_fix(char *function_name) +-{ +- bool result = false; +- char *env_var; +-#ifdef __GLIBC__ +- const char *glibc_version_string = gnu_get_libc_version(); ++__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); ++#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER ++__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); ++#else ++__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); + #endif + +- if (function_name == NULL) return false; +- +- /* The forced monotonic fix can be activated by setting an +- * environment variable to 1, or disabled by setting it to 0 */ +- if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL) ++int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, ++ const struct timespec *abstime) ++{ ++ struct timespec mono; ++ ftpl_init(); ++ if (real_pthread_cond_clockwait == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) + { +- if (env_var[0] == '0') +- result = false; +- else +- result = true; ++ int r = ft_u_cond_wait(cond, mutex, abstime); ++ if (r != -1) return r; + } +- else +- { +-#ifdef __GLIBC__ +- /* Here we try to derive the necessity for a forced monotonic fix * +- * based on glibc version. What could possibly go wrong? */ +- +- int glibc_major, glibc_minor; +- sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor); +- +- /* The following decision logic is yet purely based on experiences +- * with pthread_cond_timedwait(). The used boundaries may still be +- * imprecise. */ +- if ( (glibc_major == 2) && +- ((glibc_minor <= 24) || (glibc_minor >= 30)) ) +- { +- result = true; +- } +- else +-#endif +- result = false; // avoid forced monotonic fixes unless really necessary +- } +- +- if (getenv("FAKETIME_DEBUG") != NULL) +-#ifdef __GLIBC__ +- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n", +- function_name, result ? "yes":"no", glibc_version_string); +-#else +- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n", +- function_name, result ? "yes":"no"); +-#endif +- +- return result; ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) ++ return real_pthread_cond_clockwait(cond, mutex, clockid, abstime); ++ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); ++ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); + } + +-int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat) ++static int ft_mutex_lock_until(pthread_mutex_t *mutex, const struct timespec *fake_abs) + { +- struct timespec tp, tdiff_actual, realtime, faketime; +- struct timespec *tf = NULL; +- struct pthread_cond_monotonic* e; +- char *tmp_env; +- int wait_ms; +- clockid_t clk_id; +- int result = 0; +- +- ftpl_init(); +- +- if (abstime != NULL) ++ struct timespec mono, slice; ++ unsigned long gen; ++ bool last; ++ int result; ++ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); ++ gen = ft_tl_gen; ++ for (;;) + { +- if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) { +- fprintf(stderr,"can't acquire read monotonic_conds_lock\n"); +- exit(-1); +- } +- HASH_FIND_PTR(monotonic_conds, &cond, e); +- pthread_rwlock_unlock(&monotonic_conds_lock); +- if (e != NULL) +- clk_id = CLOCK_MONOTONIC; +- else +- clk_id = CLOCK_REALTIME; +- +- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); +- if (result == -1) +- { +- return EINVAL; +- } +- faketime = realtime; +- (void)fake_clock_gettime(clk_id, &faketime); +- +- if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL) +- { +- wait_ms = atol(tmp_env); +- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); +- if (result == -1) +- { +- return EINVAL; +- } +- +- tdiff_actual.tv_sec = wait_ms / 1000; +- tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000; +- timespecadd(&realtime, &tdiff_actual, &tp); +- +- tf = &tp; ++ last = ft_slice_end(&mono, &slice); ++ result = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &slice); ++ if (result == EINVAL && real_pthread_mutex_timedlock != NULL) ++ { /* a PI mutex on a kernel without FUTEX_LOCK_PI2 takes only CLOCK_REALTIME */ ++ struct timespec real; ++ ft_deadline_from_fake(CLOCK_REALTIME, fake_abs, &real); ++ return real_pthread_mutex_timedlock(mutex, &real); + } +- else +- { +- timespecsub(abstime, &faketime, &tp); +- if (user_rate_set) +- { +- timespecmul(&tp, 1.0 / user_rate, &tdiff_actual); +- } +- else +- { +- tdiff_actual = tp; +- } +- } +- +- /* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime +- internally to calculate the appropriate duration for the +- waiting time. This already uses the faked functions, hence, the +- fake time needs to be passed to pthread_cond_timedwait for +- CLOCK_MONOTONIC. */ +-#ifndef __ARM_ARCH +-#ifndef FORCE_MONOTONIC_FIX +- if (clk_id == CLOCK_MONOTONIC) { +- if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) { +- timespecadd(&realtime, &tdiff_actual, &tp); +- } +- else { +- timespecadd(&faketime, &tdiff_actual, &tp); +- } +- } +- else +-#endif +-#endif +- timespecadd(&realtime, &tdiff_actual, &tp); +- +- tf = &tp; +- } +- +- switch (compat) { +- case FT_COMPAT_GLIBC_2_3_2: +- result = real_pthread_cond_timedwait_232(cond, mutex, tf); +- break; +- case FT_COMPAT_GLIBC_2_2_5: +- result = real_pthread_cond_timedwait_225(cond, mutex, tf); +- break; ++ if (result != ETIMEDOUT) return result; ++ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) return ETIMEDOUT; + } +- return result; + } + +-int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) ++int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) + { +- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5); ++ ftpl_init(); ++ if (real_pthread_mutex_timedlock == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_mutex_clocklock != NULL) ++ { ++ bool handled; ++ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); ++ if (handled) return r; ++ } ++ if (!ft_translates_wait(abstime) || real_pthread_mutex_clocklock == NULL) ++ return real_pthread_mutex_timedlock(mutex, abstime); ++ return ft_mutex_lock_until(mutex, abstime); + } + +-int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) ++int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) + { +- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2); ++ ftpl_init(); ++ if (real_pthread_mutex_clocklock == NULL) return EINVAL; ++ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) ++ { ++ bool handled; ++ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); ++ if (handled) return r; ++ } ++ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) ++ return real_pthread_mutex_clocklock(mutex, clockid, abstime); ++ return ft_mutex_lock_until(mutex, abstime); + } + +-__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); +-#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER +-__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); +-__asm__(".symver pthread_cond_init_232, pthread_cond_init@@"); +-__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@"); +-#else +-__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); +-__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2"); +-__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2"); +-#endif +- + #endif + + /* +@@ -4005,15 +3980,15 @@ + /* If FAKETIME_TIMESTAMP_FILE was given in environment, + * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it. + * This allows other process instances to share the same time. */ +- if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL) +- && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0') +- && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) +- && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) ++ if ( (ft_getenv("FAKETIME_TIMESTAMP_FILE") != NULL) ++ && (*ft_getenv("FAKETIME_TIMESTAMP_FILE") != '\0') ++ && (ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) ++ && (strcmp(ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) + { + const char *error = NULL; + FILE *envfile; + static char custom_filename[BUFSIZ]; +- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); ++ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); + + if ((envfile = fopen(custom_filename, "wt")) != NULL) + { +@@ -4034,7 +4009,7 @@ + { + fprintf(stderr, "libfaketime: In clock_settime(), failed to " + "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n", +- error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); ++ error, ft_getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); + } + } + +@@ -4125,7 +4100,7 @@ + + /* return 0 if no FAKERANDOM_SEED was seen */ + static int bypass_randomness(void* buf, size_t buflen) { +- char *seedstring = getenv("FAKERANDOM_SEED"); ++ char *seedstring = ft_getenv("FAKERANDOM_SEED"); + char *b = buf; + + if (seedstring != NULL) { +@@ -4169,7 +4144,7 @@ + #else + pid_t getpid() { + #endif +- const char *pidstring = getenv("FAKETIME_FAKEPID"); ++ const char *pidstring = ft_getenv("FAKETIME_FAKEPID"); + if (pidstring != NULL) { + long int pid = strtol(pidstring, NULL, 0); + return (pid_t)(pid); +@@ -4270,7 +4245,7 @@ + va_list ap; + va_start(ap, number); + #ifdef FAKE_RANDOM +- if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) { ++ if (number == __NR_getrandom && ft_getenv("FAKERANDOM_SEED")) { + void *buf; + size_t buflen; + unsigned int flags; +@@ -4282,7 +4257,7 @@ + } + #endif + // static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp); +- if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) { ++ if (number == __NR_clock_gettime && (ft_getenv("FAKETIME") || ft_getenv("FAKETIME_TIMESTAMP_FILE"))) { + clockid_t clk_id; + struct timespec *tp; + clk_id = va_arg(ap, clockid_t); +diff -ruN a/src/libfaketime.map b/src/libfaketime.map +--- a/src/libfaketime.map ++++ b/src/libfaketime.map +@@ -1,21 +1,9 @@ +-GLIBC_2.2 { +- global: +- +- timer_gettime; timer_settime; +- local: timer_settime_*; timer_gettime_*; +-}; +- +-GLIBC_2.3.3 { +- # Changed timer_t. +- timer_gettime; timer_settime; +-} GLIBC_2.2; +- + GLIBC_2.2.5 { + global: pthread_cond_timedwait; + local: pthread_cond_timedwait_*; pthread_cond_init_*; pthread_cond_destroy*; + }; + + GLIBC_2.3.2 { +- pthread_cond_timedwait; pthread_cond_init; pthread_cond_destroy; ++ pthread_cond_timedwait; + } GLIBC_2.2.5; + diff --git a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c new file mode 100644 index 000000000..0a9a86688 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * agent-env's TLS shim: OpenSSL checks a peer's certificates against real time, whatever the universe's clock says. + * + * Preloaded after the clock library (/etc/ld.so.preload). A program on universe time months from real time would find + * every live site's certificate not yet valid or expired. Most clients reach live sites through the computer's egress + * proxy, which hands them certificates valid at the universe date; this is for the ones that bypass it. OpenSSL's + * libssl verifies a chain with X509_verify_cert, which this defines first in the global scope: it sets the + * verification's check time to real time, read with a raw system call the clock library doesn't intercept, unless the + * caller set one, then calls OpenSSL's own, the one in the caller's own libcrypto, so a process holding two copies of + * OpenSSL (a bundled one beside the system's) verifies each with its own. + * + * Not covered: TLS stacks of their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +typedef struct x509_store_ctx_st X509_STORE_CTX; +typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; + +/* OpenSSL's X509_V_FLAG_USE_CHECK_TIME (x509_vfy.h), the same in 1.1 and 3 */ +#define TLS_USE_CHECK_TIME 0x2 +#define TLS_SYSTEM_LIBCRYPTO "libcrypto.so.3" + +/* OpenSSL's functions as one copy of it has them */ +struct tls_openssl +{ + int (*verify)(X509_STORE_CTX *); + void (*set_time)(X509_STORE_CTX *, unsigned long, time_t); + X509_VERIFY_PARAM *(*get0_param)(X509_STORE_CTX *); + unsigned long (*get_flags)(const X509_VERIFY_PARAM *); +}; + +int X509_verify_cert(X509_STORE_CTX *ctx); + +/* the real time in seconds, read with the system call itself (the clock library intercepts libc's), 0 if it fails */ +static long tls_real_time(void) +{ + struct timespec ts = {0, 0}; +#if defined(__x86_64__) + long ret; + __asm__ volatile("syscall" : "=a"(ret) : "0"(228L), "D"((long) CLOCK_REALTIME), "S"(&ts) : "rcx", "r11", "memory"); + if (ret != 0) return 0; +#elif defined(__aarch64__) + register long x8 __asm__("x8") = 113; + register long x0 __asm__("x0") = CLOCK_REALTIME; + register long x1 __asm__("x1") = (long) &ts; + __asm__ volatile("svc 0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory"); + if (x0 != 0) return 0; +#else +#error the TLS shim reads real time with a raw system call on x86_64 or aarch64 +#endif + return ts.tv_sec; +} + +/* ``name`` in ``scope`` (a handle, or RTLD_NEXT), never this file's own definition */ +static void *tls_lookup(void *scope, const char *name) +{ + void *found = dlsym(scope, name); + return found == (void *) X509_verify_cert ? NULL : found; +} + +static void tls_take(void *scope, struct tls_openssl *out) +{ + out->verify = (int (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_verify_cert"); + out->set_time = (void (*)(X509_STORE_CTX *, unsigned long, time_t)) tls_lookup(scope, "X509_STORE_CTX_set_time"); + out->get0_param = (X509_VERIFY_PARAM * (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_STORE_CTX_get0_param"); + out->get_flags = (unsigned long (*)(const X509_VERIFY_PARAM *)) tls_lookup(scope, "X509_VERIFY_PARAM_get_flags"); +} + +/* OpenSSL as the object at ``caller`` sees it: from that object's own dependencies (libssl's libcrypto, whichever copy + it is); for a caller that isn't a shared library depending on one (a program calling it itself), the next definition + in the global scope; else the system's libcrypto where it was loaded privately (a Python extension's). Looked up on + every call, once a handshake: a copy of OpenSSL unloaded and another loaded at its address must not be handed the + first one's functions. */ +static void tls_resolve(void *caller, struct tls_openssl *out) +{ + Dl_info info; + void *handle; + memset(out, 0, sizeof *out); + if (dladdr(caller, &info)) + { + if (info.dli_fname != NULL && info.dli_fname[0] != 0 + && (handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD)) != NULL) + { + tls_take(handle, out); + dlclose(handle); + if (out->verify != NULL) return; + } + } + tls_take(RTLD_NEXT, out); + if (out->verify != NULL) return; + if ((handle = dlopen(TLS_SYSTEM_LIBCRYPTO, RTLD_NOW | RTLD_NOLOAD)) != NULL) + { + tls_take(handle, out); + dlclose(handle); + } +} + +int X509_verify_cert(X509_STORE_CTX *ctx) +{ + struct tls_openssl ssl; + tls_resolve(__builtin_return_address(0), &ssl); + if (ssl.verify == NULL) return -1; + if (ctx != NULL && ssl.set_time != NULL && ssl.get0_param != NULL && ssl.get_flags != NULL) + { + X509_VERIFY_PARAM *param = ssl.get0_param(ctx); + long now = tls_real_time(); + if (param != NULL && !(ssl.get_flags(param) & TLS_USE_CHECK_TIME) && now > 0) ssl.set_time(ctx, 0, (time_t) now); + } + return ssl.verify(ctx); +} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot.go b/src/agent_env/agents/agents/claude-code/egress/boot.go new file mode 100644 index 000000000..031555627 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/boot.go @@ -0,0 +1,294 @@ +package main + +import ( + "crypto/x509" + "encoding/pem" + "errors" + "fmt" + "io/fs" + "os" + "os/exec" + "os/user" + "path/filepath" + "sort" + "strconv" + "strings" + "time" +) + +// bootPaths are where boot reads and writes; tests point them into a folder of their own. +type bootPaths struct { + Folder, PublicCA, Anchor string + // Bundles are the PEM bundles the CA is appended to when they exist: agent-env's trust files, which + // SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS name. + Bundles []string + // UpdateTrust rebuilds the system store from its anchors, and its hooks Java's. + UpdateTrust []string + // CertifiRoots and JavaRoots are searched for certifi's cacert.pem and JDKs' lib/security/cacerts. + CertifiRoots, JavaRoots []string + EgressUser, SessionUser string + // NSS adds the CA to the session user's NSS database, which Chromium and Firefox read; nil leaves it. + NSS func(paths bootPaths) error +} + +// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in +// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. +func defaultBootPaths() bootPaths { + p := bootPaths{ + Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", + Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, + UpdateTrust: []string{"update-ca-certificates"}, + CertifiRoots: []string{"/usr/lib/python3", "/usr/local/lib", "/opt"}, + JavaRoots: []string{"/usr/lib/jvm", "/opt"}, + EgressUser: EgressUser, SessionUser: SessionUser, + NSS: sessionNSS, + } + if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { + p.SessionUser = name + } + if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { + p.NSS = nil + } + return p +} + +// boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and +// trusted in the system store, agent-env's trust files, every certifi bundle, every JDK's store and the session +// user's NSS database. A step that fails fails the boot, naming it, but for a certifi bundle or a JDK's store, which +// an app brings: those are named among the report's warnings. +func boot(p bootPaths) (map[string]any, error) { + start := time.Now() + report := map[string]any{} + egress, err := user.Lookup(p.EgressUser) + if err != nil { + return report, err + } + uid, _ := strconv.Atoi(egress.Uid) + gid, _ := strconv.Atoi(egress.Gid) + if err := os.MkdirAll(p.Folder, 0o700); err != nil { + return report, err + } + caPath, keyPath := filepath.Join(p.Folder, "ca.pem"), filepath.Join(p.Folder, "ca.key") + if _, err := os.Stat(caPath); errors.Is(err, fs.ErrNotExist) { + if err := writeCA(caPath, keyPath); err != nil { + return report, fmt.Errorf("minting the CA: %w", err) + } + report["minted"] = true + } + for _, path := range []string{p.Folder, caPath, keyPath} { + if err := os.Chown(path, uid, gid); err != nil { + return report, err + } + } + if err := os.Chmod(p.Folder, 0o700); err != nil { + return report, err + } + caPEM, err := os.ReadFile(caPath) + if err != nil { + return report, err + } + for _, path := range []string{p.PublicCA, p.Anchor} { + if err := writeRootFile(path, caPEM); err != nil { + return report, err + } + } + if len(p.UpdateTrust) > 0 { + if out, err := exec.Command(p.UpdateTrust[0], p.UpdateTrust[1:]...).CombinedOutput(); err != nil { + return report, fmt.Errorf("%s: %w: %s", strings.Join(p.UpdateTrust, " "), err, truncate(string(out), ErrorLimit)) + } + } + appended := map[string]bool{} + for _, path := range p.Bundles { + added, err := appendOnce(path, caPEM) + if errors.Is(err, fs.ErrNotExist) { + continue + } + if err != nil { + return report, fmt.Errorf("trusting the CA in %s: %w", path, err) + } + appended[path] = added + } + // An app's own stores, certifi's and JDKs': one that can't take the CA is named, and the computer boots, its + // app then refusing intercepted sites as it would on a store of its own. + var warnings []string + for _, path := range certifiBundles(p.CertifiRoots) { + added, err := appendOnce(path, caPEM) + if err != nil { + warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", path, err)) + continue + } + appended[path] = added + } + report["bundles"] = appended + java := map[string]string{} + for _, store := range javaStores(p.JavaRoots) { + if err := trustJava(store, p.PublicCA); err != nil { + warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", store, err)) + continue + } + java[store] = "trusted" + } + report["java"] = java + if len(warnings) > 0 { + report["warnings"] = warnings + } + if p.NSS != nil { + if err := p.NSS(p); err != nil { + return report, fmt.Errorf("the session's NSS database: %w", err) + } + report["nss"] = "trusted" + } + report["seconds"] = time.Since(start).Seconds() + return report, nil +} + +func writeCA(caPath, keyPath string) error { + ca, err := mintCA(CAName) + if err != nil { + return err + } + key, err := x509.MarshalPKCS8PrivateKey(ca.key) + if err != nil { + return err + } + if err := writeExclusive(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}), 0o600); err != nil { + return err + } + return writeExclusive(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.cert.Raw}), 0o644) +} + +func writeExclusive(path string, data []byte, mode os.FileMode) error { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) + if err != nil { + return err + } + if _, err := f.Write(data); err != nil { + f.Close() + return err + } + return f.Close() +} + +// writeRootFile writes path whole, 0644, replacing it in one rename. +func writeRootFile(path string, data []byte) error { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + part := path + ".part" + if err := os.WriteFile(part, data, 0o644); err != nil { + return err + } + if err := os.Chmod(part, 0o644); err != nil { + return err + } + return os.Rename(part, path) +} + +// appendOnce appends pem to the bundle at path (through a symlink) unless it's there; whether it did. +func appendOnce(path string, pemData []byte) (bool, error) { + real, err := filepath.EvalSymlinks(path) + if err != nil { + return false, err + } + text, err := os.ReadFile(real) + if err != nil { + return false, err + } + block := strings.TrimSpace(string(pemData)) + if strings.Contains(string(text), block) { + return false, nil + } + f, err := os.OpenFile(real, os.O_WRONLY|os.O_APPEND, 0) + if err != nil { + return false, err + } + defer f.Close() + sep := "" + if len(text) > 0 && !strings.HasSuffix(string(text), "\n") { + sep = "\n" + } + _, err = f.WriteString(sep + block + "\n") + return err == nil, err +} + +func certifiBundles(roots []string) []string { + seen := map[string]bool{} + var out []string + for _, root := range roots { + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() || d.Name() != "cacert.pem" || filepath.Base(filepath.Dir(path)) != "certifi" { + return nil + } + real, err := filepath.EvalSymlinks(path) + if err == nil && !seen[real] { + seen[real] = true + out = append(out, path) + } + return nil + }) + } + sort.Strings(out) + return out +} + +// javaStores are the JDKs' stores, lib/security/cacerts, under roots; the system store's hook builds Debian's. +func javaStores(roots []string) []string { + var out []string + for _, root := range roots { + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() || d.Name() != "cacerts" { + return nil + } + if filepath.Base(filepath.Dir(path)) == "security" && filepath.Base(filepath.Dir(filepath.Dir(path))) == "lib" { + if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() { + out = append(out, path) + } + } + return nil + }) + } + sort.Strings(out) + return out +} + +// trustJava adds the CA to a JDK's store with that JDK's own keytool, else any on the PATH. +func trustJava(store, ca string) error { + keytool := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(store))), "bin", "keytool") + if _, err := os.Stat(keytool); err != nil { + if keytool, err = exec.LookPath("keytool"); err != nil { + return errors.New("there's no keytool to add the CA with") + } + } + opened := []string{"-keystore", store, "-storepass", "changeit"} + exec.Command(keytool, append([]string{"-delete", "-alias", "agentenv-egress"}, opened...)...).Run() + out, err := exec.Command(keytool, append([]string{"-importcert", "-noprompt", "-trustcacerts", "-alias", + "agentenv-egress", "-file", ca}, opened...)...).CombinedOutput() + if err != nil { + return fmt.Errorf("keytool: %w: %s", err, truncate(string(out), ErrorLimit)) + } + return nil +} + +// sessionNSS adds the CA to the session user's NSS database, as that user, making the database if there's none. +func sessionNSS(p bootPaths) error { + session, err := user.Lookup(p.SessionUser) + if err != nil { + return err + } + db := filepath.Join(session.HomeDir, ".pki", "nssdb") + as := []string{"setpriv", "--reuid=" + p.SessionUser, "--regid=" + p.SessionUser, "--init-groups", "env", + "HOME=" + session.HomeDir} + steps := [][]string{{"mkdir", "-p", db}} + if _, err := os.Stat(filepath.Join(db, "cert9.db")); err != nil { + steps = append(steps, []string{"certutil", "-N", "-d", "sql:" + db, "--empty-password"}) + } + steps = append(steps, []string{"certutil", "-A", "-d", "sql:" + db, "-n", NSSNickname, "-t", "C,,", "-i", + p.PublicCA}) + for _, step := range steps { + argv := append(append([]string{}, as...), step...) + if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil { + return fmt.Errorf("%s: %w: %s", strings.Join(step[:2], " "), err, truncate(string(out), ErrorLimit)) + } + } + return nil +} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot_test.go b/src/agent_env/agents/agents/claude-code/egress/boot_test.go new file mode 100644 index 000000000..c5f59ad0a --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/boot_test.go @@ -0,0 +1,99 @@ +package main + +import ( + "os" + "os/user" + "path/filepath" + "strings" + "testing" +) + +func TestBootMintsTheCAOnceItsKeyPrivateAndTrustsItInEveryBundleOnce(t *testing.T) { + dir := t.TempDir() + me, _ := user.Current() + bundle := filepath.Join(dir, "ca-bundle.pem") + os.WriteFile(bundle, []byte("-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----"), 0o644) + certifi := filepath.Join(dir, "opt", "venv", "lib", "certifi", "cacert.pem") + os.MkdirAll(filepath.Dir(certifi), 0o755) + os.WriteFile(certifi, []byte(""), 0o644) + p := bootPaths{ + Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), + Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), + Bundles: []string{bundle, filepath.Join(dir, "missing.pem")}, + CertifiRoots: []string{filepath.Join(dir, "opt")}, + EgressUser: me.Username, + } + report, err := boot(p) + if err != nil { + t.Fatal(err, report) + } + if report["minted"] != true { + t.Fatalf("%v", report) + } + info, _ := os.Stat(filepath.Join(p.Folder, "ca.key")) + if info.Mode().Perm() != 0o600 { + t.Fatalf("the CA key is %v", info.Mode().Perm()) + } + if info, _ := os.Stat(p.Folder); info.Mode().Perm() != 0o700 { + t.Fatalf("the folder is %v", info.Mode().Perm()) + } + ca, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) + public, _ := os.ReadFile(p.PublicCA) + if string(ca) != string(public) { + t.Fatal("the public CA isn't the CA") + } + if _, err := boot(p); err != nil { + t.Fatal(err) + } + again, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) + if string(again) != string(ca) { + t.Fatal("a second boot minted another CA") + } + for _, path := range []string{bundle, certifi} { + text, _ := os.ReadFile(path) + if n := strings.Count(string(text), strings.TrimSpace(string(ca))); n != 1 { + t.Fatalf("%s has the CA %d times", path, n) + } + } + leaves, err := LoadLeaves(p.Folder) + if err != nil || leaves.ca.cert.NotBefore != CAFrom || leaves.ca.cert.NotAfter != CAUntil { + t.Fatalf("%v", err) + } +} + +func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { + dir := t.TempDir() + me, _ := user.Current() + broken := filepath.Join(dir, "opt", "venv", "lib", "certifi") + os.MkdirAll(filepath.Join(broken, "elsewhere"), 0o755) + os.Symlink(filepath.Join(broken, "elsewhere"), filepath.Join(broken, "cacert.pem")) // a folder, not a bundle + jdk := filepath.Join(dir, "jdk", "lib", "security") + os.MkdirAll(jdk, 0o755) + os.WriteFile(filepath.Join(jdk, "cacerts"), []byte("not a keystore"), 0o644) + t.Setenv("PATH", dir) // and no keytool to add the CA with + p := bootPaths{ + Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), + Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), + CertifiRoots: []string{filepath.Join(dir, "opt")}, JavaRoots: []string{filepath.Join(dir, "jdk")}, + EgressUser: me.Username, + } + report, err := boot(p) + if err != nil { + t.Fatalf("the boot failed: %v", err) + } + warnings, _ := report["warnings"].([]string) + if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, " "), "keytool") { + t.Fatalf("%v", report) + } +} + +func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { + if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { + t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) + } + t.Setenv("AGENTENV_SESSION_USER", "agent") + t.Setenv("AGENTENV_EGRESS_NSS", "off") + if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { + t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock.go b/src/agent_env/agents/agents/claude-code/egress/clock.go new file mode 100644 index 000000000..30cb880c1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/clock.go @@ -0,0 +1,151 @@ +package main + +import ( + "encoding/binary" + "math" + "os" + "sync" + "syscall" + "time" +) + +// The clock page the follower writes (clockpage.py, version 2): magic version seq universe_ns anchor_ns rate armed +// reserved generation check mono_universe_ns mono_anchor_ns, little-endian, 80 bytes, seq odd while it's written. +const ( + pageSize = 80 + pageMagic = 0x4B434541 + checkSalt = 0x9E3779B97F4A7C15 +) + +// state is the universe as the session reads it: armed, the universe at the anchor (seconds), the anchor (real +// seconds) and the rate. +type state struct { + Armed bool + V float64 + Anchor float64 + Rate float64 +} + +var unarmed = state{Rate: 1} + +func (s state) at(t float64) float64 { + if !s.Armed { + return t + } + if s.Rate == 0 || t <= s.Anchor { + return s.V + } + return s.V + (t-s.Anchor)*s.Rate +} + +func pageCheck(seq uint64, u, a int64, rate float64, armed uint32, generation uint64, mu, ma int64) uint64 { + return seq ^ uint64(u) ^ uint64(a) ^ math.Float64bits(rate) ^ uint64(armed) ^ generation ^ uint64(mu) ^ + uint64(ma) ^ checkSalt +} + +// readPage reads a consistent state from the page's bytes, or false when it's being written, torn or not a page. +func readPage(b []byte) (state, bool) { + if len(b) < pageSize || binary.LittleEndian.Uint32(b[0:]) != pageMagic { + return state{}, false + } + seq := binary.LittleEndian.Uint64(b[8:]) + if seq&1 == 1 { + return state{}, false + } + u := int64(binary.LittleEndian.Uint64(b[16:])) + a := int64(binary.LittleEndian.Uint64(b[24:])) + rate := math.Float64frombits(binary.LittleEndian.Uint64(b[32:])) + armed := binary.LittleEndian.Uint32(b[40:]) + generation := binary.LittleEndian.Uint64(b[48:]) + check := binary.LittleEndian.Uint64(b[56:]) + mu := int64(binary.LittleEndian.Uint64(b[64:])) + ma := int64(binary.LittleEndian.Uint64(b[72:])) + if check != pageCheck(seq, u, a, rate, armed, generation, mu, ma) || binary.LittleEndian.Uint64(b[8:]) != seq { + return state{}, false + } + if armed == 0 { + return unarmed, true + } + return state{Armed: true, V: float64(u) / 1e9, Anchor: float64(a) / 1e9, Rate: rate}, true +} + +// Universe follows the clock page, read every PagePoll, so a change is seen and the universe it left is held for Hold. +type Universe struct { + path string + mu sync.Mutex + page []byte + current state + known bool + previous state + leftAt time.Time +} + +func NewUniverse(path string) *Universe { + u := &Universe{path: path, current: unarmed} + u.refresh(time.Now()) + return u +} + +func (u *Universe) Poll() { + for { + time.Sleep(PagePoll) + u.refresh(time.Now()) + } +} + +func (u *Universe) read() state { + if u.page == nil { + f, err := os.Open(u.path) + if err != nil { + return unarmed + } + mapped, err := syscall.Mmap(int(f.Fd()), 0, pageSize, syscall.PROT_READ, syscall.MAP_SHARED) + f.Close() + if err != nil { + return unarmed + } + u.page = mapped + } + for i := 0; i < PageReads; i++ { + if s, ok := readPage(u.page); ok { + return s + } + } + return u.current +} + +func (u *Universe) refresh(now time.Time) state { + u.mu.Lock() + defer u.mu.Unlock() + s := u.read() + if u.known && s != u.current { + u.previous, u.leftAt = u.current, now + } + u.current, u.known = s, true + return s +} + +// View is false when the session is on real time and was armed nowhere within Hold; else true, with the universes a +// client may check against: now, the universe at the anchor, and the one left within Hold when it was armed. +func (u *Universe) View(now time.Time) (bool, []float64) { + u.refresh(now) + u.mu.Lock() + defer u.mu.Unlock() + return view(u.current, u.previous, u.leftAt, now) +} + +func view(current, previous state, leftAt time.Time, now time.Time) (bool, []float64) { + held := !leftAt.IsZero() && now.Sub(leftAt) <= Hold && previous.Armed + if !current.Armed && !held { + return false, nil + } + t := float64(now.UnixNano()) / 1e9 + out := []float64{current.at(t)} + if current.Armed { + out = append(out, current.V) + } + if held { + out = append(out, previous.at(t)) + } + return true, out +} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock_test.go b/src/agent_env/agents/agents/claude-code/egress/clock_test.go new file mode 100644 index 000000000..22bb41503 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/clock_test.go @@ -0,0 +1,112 @@ +package main + +import ( + "encoding/binary" + "encoding/hex" + "math" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func page(seq uint64, universe, anchor time.Time, rate float64, armed bool) []byte { + b := make([]byte, pageSize) + u, a := universe.UnixNano(), anchor.UnixNano() + var on uint32 + if armed { + on = 1 + } + binary.LittleEndian.PutUint32(b[0:], pageMagic) + binary.LittleEndian.PutUint32(b[4:], 2) + binary.LittleEndian.PutUint64(b[8:], seq) + binary.LittleEndian.PutUint64(b[16:], uint64(u)) + binary.LittleEndian.PutUint64(b[24:], uint64(a)) + binary.LittleEndian.PutUint64(b[32:], math.Float64bits(rate)) + binary.LittleEndian.PutUint32(b[40:], on) + binary.LittleEndian.PutUint64(b[48:], 7) + binary.LittleEndian.PutUint64(b[56:], pageCheck(seq, u, a, rate, on, 7, a, a)) + binary.LittleEndian.PutUint64(b[64:], uint64(a)) + binary.LittleEndian.PutUint64(b[72:], uint64(a)) + return b +} + +func TestAPageReadsAsTheUniverseItHoldsAndATornOneNot(t *testing.T) { + universe, anchor := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Unix(time.Now().Unix(), 0) + s, ok := readPage(page(4, universe, anchor, 2, true)) + if !ok || !s.Armed || s.Rate != 2 || int64(s.V) != universe.Unix() { + t.Fatalf("read %+v %v", s, ok) + } + if got := s.at(float64(anchor.Unix()) + 10); int64(got) != universe.Unix()+20 { + t.Fatalf("10 s on at rate 2: %v", got) + } + if _, ok := readPage(page(5, universe, anchor, 2, true)); ok { + t.Fatal("a page being written (odd seq) read as whole") + } + torn := page(4, universe, anchor, 2, true) + torn[20] ^= 1 + if _, ok := readPage(torn); ok { + t.Fatal("a torn page read as whole") + } + if s, ok := readPage(page(2, time.Time{}, time.Time{}, 1, false)); !ok || s.Armed { + t.Fatalf("an unarmed page: %+v %v", s, ok) + } +} + +func TestTheUniverseFollowsThePageFileAndHoldsTheOneItLeft(t *testing.T) { + path := filepath.Join(t.TempDir(), "page") + if err := os.WriteFile(path, page(2, time.Time{}, time.Time{}, 1, false), 0o644); err != nil { + t.Fatal(err) + } + u := NewUniverse(path) + if armed, _ := u.View(time.Now()); armed { + t.Fatal("unarmed page viewed as armed") + } + past := time.Now().Add(-180 * 24 * time.Hour) + f, _ := os.OpenFile(path, os.O_WRONLY, 0) + f.WriteAt(page(4, past, time.Now(), 1, true), 0) + f.Close() + armed, universes := u.View(time.Now()) + if !armed || len(universes) != 2 || int64(universes[1]) != past.Unix() { + t.Fatalf("armed page: %v %v", armed, universes) + } +} + +func TestAClientMayCheckAgainstTheUniverseItLeftForHoldAfterAChange(t *testing.T) { + now := time.Now() + past := state{Armed: true, V: float64(now.Add(-100 * 24 * time.Hour).Unix()), Anchor: float64(now.Unix()), Rate: 1} + if armed, _ := view(unarmed, state{}, time.Time{}, now); armed { + t.Fatal("real time viewed as armed") + } + armed, universes := view(unarmed, past, now.Add(-Hold/2), now) + if !armed || len(universes) != 2 { + t.Fatalf("within Hold of leaving an armed universe: %v %v", armed, universes) + } + if armed, _ := view(unarmed, past, now.Add(-2*Hold), now); armed { + t.Fatal("past Hold, the universe left still counts") + } +} + +// golden is a page the follower's writer wrote (clockpage.encode), as testdata keeps it. +func golden(t *testing.T, name string) []byte { + text, err := os.ReadFile(filepath.Join("testdata", "page-"+name+".hex")) + if err != nil { + t.Fatal(err) + } + b, err := hex.DecodeString(strings.TrimSpace(string(text))) + if err != nil { + t.Fatal(err) + } + return b +} + +func TestThePagesTheFollowersWriterWritesReadAsTheUniverseTheyHold(t *testing.T) { + s, ok := readPage(golden(t, "armed")) + if !ok || !s.Armed || s.V != 1577836800 || s.Rate != 2 || math.Abs(s.Anchor-1794000000.123456789) > 1e-6 { + t.Fatalf("armed: %+v %v", s, ok) + } + if s, ok := readPage(golden(t, "unarmed")); !ok || s != unarmed { + t.Fatalf("unarmed: %+v %v", s, ok) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/go.mod b/src/agent_env/agents/agents/claude-code/egress/go.mod new file mode 100644 index 000000000..7b1b97e80 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/go.mod @@ -0,0 +1,12 @@ +module agentenv/egress + +go 1.26.11 + +require github.com/refraction-networking/utls v1.8.2 + +require ( + github.com/andybalholm/brotli v1.0.6 // indirect + github.com/klauspost/compress v1.17.4 // indirect + golang.org/x/crypto v0.36.0 // indirect + golang.org/x/sys v0.31.0 // indirect +) diff --git a/src/agent_env/agents/agents/claude-code/egress/go.sum b/src/agent_env/agents/agents/claude-code/egress/go.sum new file mode 100644 index 000000000..3e8a6d1ef --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/go.sum @@ -0,0 +1,10 @@ +github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= +github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= +github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= +github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/src/agent_env/agents/agents/claude-code/egress/hello.go b/src/agent_env/agents/agents/claude-code/egress/hello.go new file mode 100644 index 000000000..381ea2993 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/hello.go @@ -0,0 +1,164 @@ +package main + +import ( + "bufio" + "encoding/binary" + "errors" + "strings" + "time" +) + +// A TLS record's content type for handshake messages, a ClientHello's handshake type, and the most plaintext one +// record carries. +const ( + recordHandshake = 22 + clientHelloType = 1 + maxRecord = 16384 +) + +// clientHello is the ClientHello handshake message the client has sent, peeked from r so the handshake stays +// untouched for whoever reads it next; it may span several records. +func clientHello(r *bufio.Reader, deadline time.Time) ([]byte, error) { + need := 5 + for { + if time.Now().After(deadline) { + return nil, errors.New("ClientHello incomplete") + } + data, err := r.Peek(need) + if err != nil { + if errors.Is(err, bufio.ErrBufferFull) { + return nil, errors.New("ClientHello larger than the read buffer") + } + return nil, err + } + if data[0] != recordHandshake { + return nil, errors.New("not TLS") + } + var body []byte + for p := 0; ; { + if p+5 > len(data) { + need = p + 5 + break + } + if data[p] != recordHandshake { + return nil, errors.New("ClientHello interrupted by another record") + } + size := int(binary.BigEndian.Uint16(data[p+3:])) + if p+5+size > len(data) { + need = p + 5 + size + break + } + body = append(body, data[p+5:p+5+size]...) + p += 5 + size + if len(body) >= 4 { + if whole := 4 + (int(body[1])<<16 | int(body[2])<<8 | int(body[3])); len(body) >= whole { + return body[:whole], nil + } + } + } + } +} + +// parseHello is the server name and the ALPN protocols a ClientHello message offers. +func parseHello(hello []byte) (string, []string, error) { + p, err := helloExtensions(hello) + if err != nil { + return "", nil, err + } + if len(hello) < p+2 { + return "", nil, nil + } + end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) + if end > len(hello) { + return "", nil, errors.New("extensions overrun the ClientHello") + } + var sni string + var alpn []string + for q := p + 2; q+4 <= end; { + kind := binary.BigEndian.Uint16(hello[q:]) + size := int(binary.BigEndian.Uint16(hello[q+2:])) + body := hello[q+4 : min(q+4+size, end)] + q += 4 + size + switch { + case kind == 0 && len(body) > 5 && body[2] == 0: + n := int(binary.BigEndian.Uint16(body[3:])) + if 5+n <= len(body) { + sni = strings.ToLower(string(body[5 : 5+n])) + } + case kind == 16 && len(body) > 2: + for r := 2; r < len(body); { + n := int(body[r]) + if r+1+n > len(body) { + break + } + alpn = append(alpn, string(body[r+1:r+1+n])) + r += 1 + n + } + } + } + return sni, alpn, nil +} + +// resumption: what only the proxy's own session with the client could mean, never the site's. +var dropped = map[uint16]bool{41: true, 42: true} + +// helloRecord is the ClientHello as one handshake record, without the extensions only a resumption with the proxy put +// there, and the extensions it left out. +func helloRecord(hello []byte) ([]byte, []uint16, error) { + p, err := helloExtensions(hello) + if err != nil { + return nil, nil, err + } + body := append([]byte{}, hello[4:p]...) + var gone []uint16 + if len(hello) >= p+2 { + end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) + if end > len(hello) { + return nil, nil, errors.New("extensions overrun the ClientHello") + } + var exts []byte + for q := p + 2; q+4 <= end; { + kind := binary.BigEndian.Uint16(hello[q:]) + size := int(binary.BigEndian.Uint16(hello[q+2:])) + if q+4+size > end { + return nil, nil, errors.New("an extension overruns the ClientHello") + } + if dropped[kind] { + gone = append(gone, kind) + } else { + exts = append(exts, hello[q:q+4+size]...) + } + q += 4 + size + } + body = binary.BigEndian.AppendUint16(body, uint16(len(exts))) + body = append(body, exts...) + } + if len(body)+4 > maxRecord { + return nil, nil, errors.New("ClientHello longer than one record") + } + msg := append([]byte{clientHelloType, byte(len(body) >> 16), byte(len(body) >> 8), byte(len(body))}, body...) + return append([]byte{recordHandshake, 3, 1, byte(len(msg) >> 8), byte(len(msg))}, msg...), gone, nil +} + +// helloExtensions is where a ClientHello message's extensions start, past its version, random, session id, cipher +// suites and compression methods, each length checked against the message. +func helloExtensions(hello []byte) (int, error) { + if len(hello) < 4 || hello[0] != clientHelloType { + return 0, errors.New("not a ClientHello") + } + p := 4 + 2 + 32 + for _, width := range []int{1, 2, 1} { // session id, cipher suites, compression methods + if len(hello) < p+width { + return 0, errors.New("short ClientHello") + } + n := int(hello[p]) + if width == 2 { + n = int(binary.BigEndian.Uint16(hello[p:])) + } + p += width + n + if p > len(hello) { + return 0, errors.New("a ClientHello field overruns the message") + } + } + return p, nil +} diff --git a/src/agent_env/agents/agents/claude-code/egress/hello_test.go b/src/agent_env/agents/agents/claude-code/egress/hello_test.go new file mode 100644 index 000000000..a3b30b2f1 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/hello_test.go @@ -0,0 +1,82 @@ +package main + +import ( + "bufio" + "bytes" + "crypto/tls" + "encoding/binary" + "net" + "reflect" + "testing" + "time" +) + +// goHello is the ClientHello message Go's TLS client sends for name, offering alpn. +func goHello(t *testing.T, name string, alpn []string) []byte { + t.Helper() + client, server := net.Pipe() + go tls.Client(client, &tls.Config{ServerName: name, NextProtos: alpn, InsecureSkipVerify: true}).Handshake() + defer client.Close() + defer server.Close() + hello, err := clientHello(bufio.NewReader(server), time.Now().Add(5*time.Second)) + if err != nil { + t.Fatal(err) + } + return hello +} + +func records(message []byte, size int) []byte { + var out []byte + for len(message) > 0 { + n := min(size, len(message)) + out = append(out, 22, 3, 1, byte(n>>8), byte(n)) + out = append(out, message[:n]...) + message = message[n:] + } + return out +} + +func TestAClientHelloIsReadWholeAcrossRecordsAndNamesItsSiteAndProtocols(t *testing.T) { + hello := goHello(t, "Example.COM", []string{"h2", "http/1.1"}) + sni, alpn, err := parseHello(hello) + if err != nil || sni != "example.com" || !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) { + t.Fatalf("parsed %q %v %v", sni, alpn, err) + } + split := records(hello, 100) + again, err := clientHello(bufio.NewReader(bytes.NewReader(split)), time.Now().Add(time.Second)) + if err != nil || !bytes.Equal(again, hello) { + t.Fatalf("across records: %v", err) + } + if _, err := clientHello(bufio.NewReader(bytes.NewReader([]byte("GET / HTTP/1.1\r\n\r\n"))), + time.Now().Add(time.Second)); err == nil { + t.Fatal("plain HTTP read as TLS") + } +} + +func withExtension(hello []byte, kind uint16, body []byte) []byte { + p := 4 + 2 + 32 + p += 1 + int(hello[p]) + p += 2 + int(binary.BigEndian.Uint16(hello[p:])) + p += 1 + int(hello[p]) + exts := append(append([]byte{}, hello[p+2:]...), byte(kind>>8), byte(kind), byte(len(body)>>8), byte(len(body))) + exts = append(exts, body...) + out := append(append([]byte{}, hello[:p]...), byte(len(exts)>>8), byte(len(exts))) + out = append(out, exts...) + n := len(out) - 4 + out[1], out[2], out[3] = byte(n>>16), byte(n>>8), byte(n) + return out +} + +func TestTheUpstreamRecordLeavesOutWhatOnlyAResumptionWithTheProxyMeans(t *testing.T) { + hello := withExtension(goHello(t, "example.com", nil), 41, []byte{0, 1, 2}) + record, gone, err := helloRecord(hello) + if err != nil || !reflect.DeepEqual(gone, []uint16{41}) { + t.Fatalf("dropped %v %v", gone, err) + } + if record[0] != 22 || int(binary.BigEndian.Uint16(record[3:])) != len(record)-5 { + t.Fatal("not one handshake record") + } + if _, again, _ := helloRecord(record[5:]); len(again) != 0 { + t.Fatalf("the record still has %v", again) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/leaves.go b/src/agent_env/agents/agents/claude-code/egress/leaves.go new file mode 100644 index 000000000..027ecac92 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/leaves.go @@ -0,0 +1,188 @@ +package main + +import ( + "crypto" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "errors" + "math/big" + "net" + "os" + "path/filepath" + "sort" + "strings" + "sync" +) + +// The longest common name X.509 allows (ub-common-name). +const commonNameLimit = 64 + +// Leaves signs the leaves an intercepted client is handed: by this computer's CA when the site verified, else by a CA +// trusted nowhere, so the client refuses it as it would the site. Each is valid over all of its CA's window, so it +// holds wherever the universe goes, and is minted once per name and its alternative names. +type Leaves struct { + ca, untrusted signer + key *ecdsa.PrivateKey + mu sync.Mutex + cache map[string]*tls.Certificate + // order is the cache's keys, oldest first, so it drops the oldest past cacheLimit; a connection keeps its own. + order []string +} + +type signer struct { + cert *x509.Certificate + key crypto.Signer +} + +func LoadLeaves(folder string) (*Leaves, error) { + certPEM, err := os.ReadFile(filepath.Join(folder, "ca.pem")) + if err != nil { + return nil, err + } + keyPEM, err := os.ReadFile(filepath.Join(folder, "ca.key")) + if err != nil { + return nil, err + } + ca, err := parseSigner(certPEM, keyPEM) + if err != nil { + return nil, err + } + return NewLeaves(ca) +} + +func NewLeaves(ca signer) (*Leaves, error) { + untrusted, err := mintCA("Untrusted upstream") + if err != nil { + return nil, err + } + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return nil, err + } + return &Leaves{ca: ca, untrusted: untrusted, key: key, cache: map[string]*tls.Certificate{}}, nil +} + +// For is the leaf for host and the names sans ("DNS:..." or "IP:..."), by this computer's CA when trusted, and +// whether it was minted now. +func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate, bool, error) { + sorted := append([]string{}, sans...) + sort.Strings(sorted) + key := host + "|" + strings.Join(sorted, ",") + by := l.ca + if !trusted { + key, by = "untrusted|"+key, l.untrusted + } + l.mu.Lock() + defer l.mu.Unlock() + if cert, ok := l.cache[key]; ok { + return cert, false, nil + } + template := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, + NotBefore: by.cert.NotBefore.Add(LeafInset), + NotAfter: by.cert.NotAfter.Add(-LeafInset), + KeyUsage: x509.KeyUsageDigitalSignature, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + BasicConstraintsValid: true, + } + for _, name := range append([]string{host}, sans...) { + name = strings.TrimPrefix(strings.TrimPrefix(name, "DNS:"), "IP:") + if ip := net.ParseIP(strings.Trim(name, "[]")); ip != nil { + template.IPAddresses = appendIP(template.IPAddresses, ip) + } else if name != "" && !contains(template.DNSNames, name) { + template.DNSNames = append(template.DNSNames, name) + } + } + der, err := x509.CreateCertificate(rand.Reader, template, by.cert, &l.key.PublicKey, by.key) + if err != nil { + return nil, false, err + } + cert := &tls.Certificate{Certificate: [][]byte{der, by.cert.Raw}, PrivateKey: l.key} + l.cache[key] = cert + l.order = append(l.order, key) + for len(l.order) > cacheLimit { + delete(l.cache, l.order[0]) + l.order = l.order[1:] + } + return cert, true, nil +} + +// mintCA is a self-signed CA over the window, its key in memory. +func mintCA(name string) (signer, error) { + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return signer{}, err + } + template := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: name}, + NotBefore: CAFrom, + NotAfter: CAUntil, + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, + BasicConstraintsValid: true, + IsCA: true, + MaxPathLenZero: true, + } + der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) + if err != nil { + return signer{}, err + } + cert, err := x509.ParseCertificate(der) + if err != nil { + return signer{}, err + } + return signer{cert: cert, key: key}, nil +} + +func parseSigner(certPEM, keyPEM []byte) (signer, error) { + block, _ := pem.Decode(certPEM) + if block == nil { + return signer{}, errors.New("no certificate in the CA's PEM") + } + cert, err := x509.ParseCertificate(block.Bytes) + if err != nil { + return signer{}, err + } + block, _ = pem.Decode(keyPEM) + if block == nil { + return signer{}, errors.New("no key in the CA key's PEM") + } + key, err := x509.ParsePKCS8PrivateKey(block.Bytes) + if err != nil { + return signer{}, err + } + s, ok := key.(crypto.Signer) + if !ok { + return signer{}, errors.New("the CA key can't sign") + } + return signer{cert: cert, key: s}, nil +} + +func serial() *big.Int { + n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) + return n +} + +func appendIP(ips []net.IP, ip net.IP) []net.IP { + for _, have := range ips { + if have.Equal(ip) { + return ips + } + } + return append(ips, ip) +} + +func contains(list []string, s string) bool { + for _, have := range list { + if strings.EqualFold(have, s) { + return true + } + } + return false +} diff --git a/src/agent_env/agents/agents/claude-code/egress/main.go b/src/agent_env/agents/agents/claude-code/egress/main.go new file mode 100644 index 000000000..0bd5ae5c5 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/main.go @@ -0,0 +1,92 @@ +// egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. +// +// It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and +// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128. Session programs reach it through HTTP(S)_PROXY and +// ALL_PROXY; nothing redirects or filters, so it needs no NET_ADMIN and works on any provider. +// +// A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe +// a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is +// intercepted: the site is verified against real time and the public roots, and the client is handed a leaf from this +// computer's CA, valid over every date the clock can show. The upstream handshake is uTLS's, built from the client's +// own ClientHello, so the site sees the client's fingerprint and ALPN offer either way. +// +// egress boot as root at the computer's start: the CA, minted once, its key this user's alone, trusted everywhere +// egress serve as agentenv-egress: serve, one JSON line per connection on stdout +package main + +import ( + "encoding/json" + "fmt" + "os" + "time" +) + +const ( + Listen = "127.0.0.1:3128" + EgressUser = "agentenv-egress" + SessionUser = "user" + Folder = "/etc/agentenv/egress" + PublicCA = "/etc/agentenv/egress-ca.pem" + PublicRoots = "/usr/local/lib/agentenv-egress/roots.pem" + ClockPage = "/dev/shm/agentenv-clock" + Hosts = "/etc/hosts" + HostsBegin = "# agentenv sites" + HostsEnd = "# end agentenv sites" + CAName = "agentenv egress CA" + NSSNickname = "agentenv egress" + Hold = 30 * time.Second + Margin = 8 * 24 * time.Hour + ProbeTTL = time.Hour + ProbeFailedTTL = time.Minute + FirstBytes = 300 * time.Millisecond + HelloTimeout = 10 * time.Second + DialTimeout = 10 * time.Second + + // How long a client has to send its request's head, and how long either handshake of an interception, the + // client's with the proxy and the proxy's with the site, has to finish. + HeadTimeout = 15 * time.Second + HandshakeTimeout = 15 * time.Second + + // The longest request head a client may send, and the most of an error a log row keeps, in bytes. + HeadLimit = 64 << 10 + ErrorLimit = 300 + + // How far inside the CA's dates each leaf it signs is, so no leaf outlasts its CA. + LeafInset = 2 * time.Hour + + // How often the clock page is read, and how many reads may find it mid-write before the last universe stands. + PagePoll = 20 * time.Millisecond + PageReads = 1000 +) + +// The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' +// (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. +var ( + CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) + CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) +) + +func main() { + command := "" + if len(os.Args) > 1 { + command = os.Args[1] + } + switch command { + case "boot": + report, err := boot(defaultBootPaths()) + out, _ := json.Marshal(report) + fmt.Println(string(out)) + if err != nil { + fmt.Fprintln(os.Stderr, "egress boot:", err) + os.Exit(1) + } + case "serve": + if err := serveForever(); err != nil { + fmt.Fprintln(os.Stderr, "egress serve:", err) + os.Exit(1) + } + default: + fmt.Fprintf(os.Stderr, "usage: egress boot|serve, not %q\n", command) + os.Exit(2) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy.go b/src/agent_env/agents/agents/claude-code/egress/proxy.go new file mode 100644 index 000000000..c575fe5e6 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/proxy.go @@ -0,0 +1,598 @@ +package main + +import ( + "bufio" + "context" + "crypto/tls" + "crypto/x509" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/netip" + "net/url" + "os" + "strconv" + "strings" + "sync" + "time" +) + +type Proxy struct { + Leaves *Leaves + Universe interface { + View(time.Time) (bool, []float64) + } + // Roots are the public roots a site is verified against, as the image had them before any agentenv CA joined. + Roots *x509.CertPool + // Resolve, Allowed, Routed and Dial default to the system's resolver, a global address, the hosts file's agentenv + // sites block and a TCP dial; tests replace them. + Resolve func(ctx context.Context, host string) ([]netip.Addr, error) + Allowed func(netip.Addr) bool + Routed func() map[string]string + Dial func(dst string) (net.Conn, error) + Log func(row map[string]any) + probes probes +} + +func serveForever() error { + leaves, err := LoadLeaves(Folder) + if err != nil { + return fmt.Errorf("the CA (has boot run?): %w", err) + } + roots, err := os.ReadFile(PublicRoots) + if err != nil { + return err + } + pool := x509.NewCertPool() + if !pool.AppendCertsFromPEM(roots) { + return fmt.Errorf("no certificates in %s", PublicRoots) + } + universe := NewUniverse(ClockPage) + go universe.Poll() + ln, err := net.Listen("tcp", Listen) + if err != nil { + return err + } + p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} + p.Log(map[string]any{"event": "serving", "listen": Listen, "uid": os.Getuid(), "pid": os.Getpid()}) + return p.Serve(ln) +} + +func jsonLines(w io.Writer) func(map[string]any) { + var mu sync.Mutex + encoder := json.NewEncoder(w) + return func(row map[string]any) { + row["t"] = float64(time.Now().UnixMilli()) / 1e3 + mu.Lock() + defer mu.Unlock() + encoder.Encode(row) + } +} + +func (p *Proxy) Serve(ln net.Listener) error { + for { + conn, err := ln.Accept() + if err != nil { + if errors.Is(err, net.ErrClosed) { + return nil + } + return err + } + go p.handle(conn) + } +} + +func (p *Proxy) log(row map[string]any) { + if p.Log != nil { + p.Log(row) + } +} + +func (p *Proxy) handle(conn net.Conn) { + start := time.Now() + row := map[string]any{"client": conn.RemoteAddr().String()} + defer func() { + if r := recover(); r != nil { + row["error"] = fmt.Sprint("panic: ", r) + p.log(row) + } + conn.Close() + }() + br := bufio.NewReaderSize(conn, HeadLimit) + conn.SetReadDeadline(start.Add(HeadTimeout)) + method, target, version, headers, err := readHead(br) + if err != nil { + row["error"] = truncate("request: "+err.Error(), ErrorLimit) + p.log(row) + return + } + row["method"] = method + if method == http.MethodConnect { + host, port := splitAuthority(target, 443) + row["host"], row["port"] = host, port + dsts, why, err := p.destination(host, port) + if err != nil { + row["mode"], row["error"] = "refused", err.Error() + reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") + p.log(row) + return + } + row["allowed"] = why + if _, err := io.WriteString(conn, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { + return + } + p.tunnel(conn, br, host, port, dsts, why, row, start) + return + } + u, err := url.Parse(target) + if err != nil || u.Scheme != "http" || u.Host == "" { + row["mode"], row["error"] = "refused", "not an absolute http URL" + reply(conn, "400 Bad Request", "egress: CONNECT, or an absolute http:// URL\n") + p.log(row) + return + } + host, port := splitAuthority(u.Host, 80) + row["host"], row["port"], row["mode"] = host, port, "http" + dsts, why, err := p.destination(host, port) + if err != nil { + row["mode"], row["error"] = "refused", err.Error() + reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") + p.log(row) + return + } + row["allowed"] = why + up, dst, err := p.dialAny(dsts) + if err != nil { + row["error"] = truncate(err.Error(), ErrorLimit) + reply(conn, "502 Bad Gateway", "egress: "+err.Error()+"\n") + p.log(row) + return + } + defer up.Close() + row["dst"] = dst + upgrade := false + for _, h := range headers { + if headerName(h) == "upgrade" { + upgrade = true + } + } + // The client's own head, but for its request line, which names the path alone, and the headers meant for this + // proxy; a request that isn't an upgrade closes once answered, so its connection can't carry one for another site. + var head strings.Builder + head.WriteString(method + " " + u.RequestURI() + " " + version + "\r\n") + for _, h := range headers { + switch headerName(h) { + case "proxy-connection", "proxy-authorization": + continue + case "connection", "keep-alive": + if !upgrade { + continue + } + } + head.WriteString(h + "\r\n") + } + if upgrade { + row["mode"] = "upgrade" + } else { + head.WriteString("Connection: close\r\n") + } + head.WriteString("\r\n") + conn.SetReadDeadline(time.Time{}) + if _, err := io.WriteString(up, head.String()); err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + p.log(row) + return + } + p.log(row) + // Both ways at once: a body the client sends once told to continue, or an upgraded connection's frames. + go func() { + io.Copy(up, br) + closeWrite(up) + }() + io.Copy(conn, up) +} + +// readHead is a request's head, read through its blank line: its request line's parts and its header lines. +func readHead(r *bufio.Reader) (string, string, string, []string, error) { + var lines []string + size := 0 + for { + line, err := r.ReadString('\n') + size += len(line) + if size > HeadLimit { + return "", "", "", nil, errors.New("request head too long") + } + if err != nil { + return "", "", "", nil, err + } + line = strings.TrimRight(line, "\r\n") + if line == "" { + if len(lines) == 0 { + continue + } + break + } + lines = append(lines, line) + } + parts := strings.SplitN(lines[0], " ", 3) + if len(parts) != 3 { + return "", "", "", nil, fmt.Errorf("malformed request line %q", truncate(lines[0], 100)) + } + return parts[0], parts[1], parts[2], lines[1:], nil +} + +func headerName(line string) string { + name, _, _ := strings.Cut(line, ":") + return strings.ToLower(strings.TrimSpace(name)) +} + +// destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for +// a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), +// "global" for a name whose every address is global, IPv4 first. Anything else is refused. +func (p *Proxy) destination(host string, port int) ([]string, string, error) { + host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") + routed := p.Routed + if routed == nil { + routed = routedSites + } + if address, ok := routed()[host]; ok { + return []string{net.JoinHostPort(address, strconv.Itoa(port))}, "routed", nil + } + resolve := p.Resolve + if resolve == nil { + resolve = func(ctx context.Context, host string) ([]netip.Addr, error) { + return net.DefaultResolver.LookupNetIP(ctx, "ip", host) + } + } + allowed := p.Allowed + if allowed == nil { + allowed = isGlobal + } + ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) + defer cancel() + addresses, err := resolve(ctx, host) + if err != nil { + return nil, "", fmt.Errorf("%s doesn't resolve: %w", host, err) + } + if len(addresses) == 0 { + return nil, "", fmt.Errorf("%s has no address", host) + } + var private []string + var v4, v6 []string + for _, a := range addresses { + a = a.Unmap() + dst := netip.AddrPortFrom(a, uint16(port)).String() + switch { + case !allowed(a): + private = append(private, a.String()) + case a.Is4(): + v4 = append(v4, dst) + default: + v6 = append(v6, dst) + } + } + if len(private) > 0 { + return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) + } + return append(v4, v6...), "global", nil +} + +// notGlobal are the special-purpose blocks (IANA's registries) no global address is in. +var notGlobal = func() []netip.Prefix { + var out []netip.Prefix + for _, s := range []string{ + "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", + "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", + "224.0.0.0/4", "240.0.0.0/4", + "::/128", "::1/128", "64:ff9b:1::/48", "100::/64", "2001::/23", "2001:db8::/32", "fc00::/7", "fe80::/10", + "ff00::/8", + } { + out = append(out, netip.MustParsePrefix(s)) + } + return out +}() + +func isGlobal(a netip.Addr) bool { + a = a.Unmap() + if !a.IsValid() { + return false + } + for _, block := range notGlobal { + if block.Contains(a) { + return false + } + } + return true +} + +// routedSites is the hosts file's agentenv sites block, name to address: root's, so the session user can't add to it. +func routedSites() map[string]string { + out := map[string]string{} + data, err := os.ReadFile(Hosts) + if err != nil { + return out + } + inside := false + for _, line := range strings.Split(string(data), "\n") { + text := strings.TrimSpace(line) + switch { + case text == HostsBegin: + inside = true + case text == HostsEnd: + inside = false + case inside && text != "" && !strings.HasPrefix(text, "#"): + fields := strings.Fields(text) + for _, name := range fields[1:] { + out[strings.ToLower(name)] = fields[0] + } + } + } + return out +} + +func (p *Proxy) tunnel(conn net.Conn, br *bufio.Reader, host string, port int, dsts []string, why string, + row map[string]any, start time.Time) { + wait := FirstBytes + if port == 443 { + wait = HelloTimeout + } + conn.SetReadDeadline(time.Now().Add(wait)) + first, err := br.Peek(1) + if err != nil && !isTimeout(err) { + row["error"] = "closed before sending" + p.log(row) + return + } + if len(first) == 0 || first[0] != recordHandshake || why == "routed" { + row["mode"], row["decision"] = "pass", "not TLS" + if len(first) > 0 && first[0] == recordHandshake { + row["mode"], row["decision"] = "splice", "routed" + } + if !p.splice(conn, br, dsts, row, start) { + p.log(row) + } + return + } + conn.SetReadDeadline(time.Now().Add(HelloTimeout)) + hello, err := clientHello(br, time.Now().Add(HelloTimeout)) + if err != nil { + row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) + p.log(row) + return + } + sni, offered, err := parseHello(hello) + if err != nil { + row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) + p.log(row) + return + } + row["sni"], row["alpn_offered"] = sni, offered + verifyName := sni + if verifyName == "" { + verifyName = strings.Trim(host, "[]") + } + armed, universes := p.Universe.View(time.Now()) + if !armed { + row["decision"], row["mode"] = "real", "splice" + if !p.splice(conn, br, dsts, row, start) { + p.log(row) + } + return + } + row["universes"] = rounded(universes) + // Each address in turn, by its own probe: one that doesn't answer is passed over, so the connection goes to a + // server whose own certificate decided it. + var unreachable []string + for _, dst := range p.preferChecked(dsts, sni) { + delete(row, "probe_error") + delete(row, "upstream_valid") + seen, probed := p.probeAt(dst, sni, verifyName, hello) + if seen.unreachable { + unreachable = append(unreachable, seen.problem) + continue + } + row["probed"] = probed + decision := "outside" + switch { + case seen.failed: + // a handshake only the client can finish, as one asking for its certificate: the client decides + decision, row["probe_error"] = "probe_failed", seen.problem + case !seen.verified: + decision, row["probe_error"] = "unverified", seen.problem + case inside(seen.from, seen.until, universes): + decision = "inside" + } + if seen.verified { + row["upstream_valid"] = []int64{seen.from.Unix(), seen.until.Unix()} + } + row["decision"] = decision + var dialled bool + if decision == "inside" || decision == "probe_failed" { + row["mode"] = "splice" + dialled = p.splice(conn, br, []string{dst}, row, start) + } else { + row["mode"] = "intercept" + dialled = p.intercept(conn, br, dst, sni, verifyName, hello, row, start) + } + if dialled { + return + } + // it stopped answering since its probe: forget it, and go on to the next + p.forget(dst, sni) + unreachable = append(unreachable, fmt.Sprint(row["error"])) + delete(row, "error") + } + row["error"] = truncate("upstream: no address answered: "+strings.Join(unreachable, "; "), ErrorLimit) + p.log(row) +} + +// preferChecked is dsts with those whose fresh probe answered first, each group in its own order, so an address +// already found working isn't kept waiting behind one whose probe has to be made again. +func (p *Proxy) preferChecked(dsts []string, sni string) []string { + p.probes.mu.Lock() + defer p.probes.mu.Unlock() + var checked, rest []string + for _, dst := range dsts { + if hit, ok := p.probes.cache[dst+"|"+sni]; ok && hit.fresh() && !hit.unreachable { + checked = append(checked, dst) + } else { + rest = append(rest, dst) + } + } + return append(checked, rest...) +} + +// splice relays the connection to the first of dsts that answers, untouched; false, with the error in row, when none +// does. +func (p *Proxy) splice(conn net.Conn, br *bufio.Reader, dsts []string, row map[string]any, start time.Time) bool { + up, dst, err := p.dialAny(dsts) + if err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + return false + } + defer up.Close() + row["dst"] = dst + conn.SetReadDeadline(time.Time{}) + row["upstream_ms"] = time.Since(start).Milliseconds() + p.log(row) + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + io.Copy(up, br) + closeWrite(up) + }() + io.Copy(conn, up) + closeWrite(conn) + wg.Wait() + return true +} + +// intercept hands the client a leaf of this computer's CA and relays the plaintext to dst over a handshake of the +// proxy's own; false, with the error in row, when dst doesn't answer. +func (p *Proxy) intercept(conn net.Conn, br *bufio.Reader, dst, sni, verifyName string, hello []byte, + row map[string]any, start time.Time) bool { + uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) + var unanswered dialError + if errors.As(err, &unanswered) { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + return false + } + if err != nil { + row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + defer uc.NetConn().Close() + row["dst"] = dst + row["alpn"], row["verified"], row["upstream_tls"] = found.ALPN, found.Verified, []string{found.Version, found.Cipher} + row["upstream_ms"] = time.Since(start).Milliseconds() + if len(found.Dropped) > 0 { + row["dropped_extensions"] = found.Dropped + } + var sans []string + if found.Verified { + // the site's own names, as its leaf lists them, so the client pools connections as it would with the site's + // certificate + for _, name := range found.SANs { + if sni == "" || strings.HasPrefix(name, "DNS:") { + sans = append(sans, name) + } + } + if sni == "" { + sans = append(sans, "IP:"+strings.Trim(hostOf(dst), "[]")) + } + } else { + row["upstream_verify"] = truncate(found.VerifyErr, 200) + } + cert, minted, err := p.Leaves.For(verifyName, sans, found.Verified) + if err != nil { + row["error"] = truncate("leaf: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + row["minted"] = minted + config := &tls.Config{Certificates: []tls.Certificate{*cert}, SessionTicketsDisabled: true} + if found.ALPN != "" { + config.NextProtos = []string{found.ALPN} + } + client := tls.Server(&peeked{Conn: conn, r: br}, config) + conn.SetDeadline(time.Now().Add(HandshakeTimeout)) + if err := client.Handshake(); err != nil { + row["error"] = truncate("client handshake: "+err.Error(), ErrorLimit) + p.log(row) + return true + } + conn.SetDeadline(time.Time{}) + row["handshake_ms"] = time.Since(start).Milliseconds() + p.log(row) + done := make(chan struct{}, 2) + go func() { + io.Copy(uc, client) + done <- struct{}{} + }() + go func() { + io.Copy(client, uc) + done <- struct{}{} + }() + <-done + client.Close() + uc.NetConn().Close() + <-done + return true +} + +// peeked is a connection whose reads come through the reader that peeked its first bytes. +type peeked struct { + net.Conn + r *bufio.Reader +} + +func (c *peeked) Read(b []byte) (int, error) { return c.r.Read(b) } + +func closeWrite(c net.Conn) { + if cw, ok := c.(interface{ CloseWrite() error }); ok { + cw.CloseWrite() + } +} + +func isTimeout(err error) bool { + var ne net.Error + return errors.As(err, &ne) && ne.Timeout() +} + +func reply(conn net.Conn, status, body string) { + fmt.Fprintf(conn, "HTTP/1.1 %s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", + status, len(body), body) +} + +func splitAuthority(authority string, port int) (string, int) { + host, p, err := net.SplitHostPort(authority) + if err != nil { + return strings.Trim(authority, "[]"), port + } + n, err := strconv.Atoi(p) + if err != nil { + return host, port + } + return host, n +} + +func hostOf(hostport string) string { + host, _, err := net.SplitHostPort(hostport) + if err != nil { + return hostport + } + return host +} + +func rounded(values []float64) []int64 { + out := make([]int64, len(values)) + for i, v := range values { + out[i] = int64(v) + } + return out +} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go new file mode 100644 index 000000000..c8e57b3e7 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go @@ -0,0 +1,564 @@ +package main + +import ( + "bufio" + "bytes" + "context" + "crypto/tls" + "crypto/x509" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "net/netip" + "net/url" + "strings" + "sync" + "testing" + "time" +) + +type fixedUniverse struct { + armed bool + universes []float64 +} + +func (f fixedUniverse) View(time.Time) (bool, []float64) { return f.armed, f.universes } + +func TestOnlyDestinationsWhoseEveryAddressIsGlobalAreDialled(t *testing.T) { + refused := []string{"10.0.0.1", "10.255.255.254", "172.16.0.1", "172.31.255.1", "192.168.1.1", "127.0.0.1", + "127.1.2.3", "0.0.0.0", "169.254.169.254", "169.254.1.1", "100.64.0.1", "100.127.255.254", "192.0.2.1", + "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::", "fd00::1", "fe80::1", "::ffff:10.0.0.1", + "2001:db8::1"} + if len(refused) != 22 { + t.Fatal(len(refused)) + } + p := &Proxy{Routed: func() map[string]string { return nil }} + for _, address := range refused { + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr(address)}, nil + } + if _, _, err := p.destination("somewhere.example", 443); err == nil { + t.Errorf("%s dialled", address) + } + } + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("2606:4700::1111"), netip.MustParseAddr("1.1.1.1")}, nil + } + dst, why, err := p.destination("one.one.one.one", 443) + if err != nil || strings.Join(dst, " ") != "1.1.1.1:443 [2606:4700::1111]:443" || why != "global" { + t.Fatalf("a public name, IPv4 first: %q %q %v", dst, why, err) + } + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("10.0.0.1")}, nil + } + if _, _, err := p.destination("split.example", 443); err == nil || !strings.Contains(err.Error(), "10.0.0.1") { + t.Fatalf("a name with one private address: %v", err) + } + p.Routed = func() map[string]string { return map[string]string{"notes.test": "127.0.0.1"} } + if dst, why, err := p.destination("Notes.Test.", 443); err != nil || strings.Join(dst, " ") != "127.0.0.1:443" || + why != "routed" { + t.Fatalf("a routed site: %q %q %v", dst, why, err) + } +} + +// harness is a proxy in front of the site server, which it reaches as example.com, and a client whose clock is at +// clientTime. +type harness struct { + t *testing.T + site *httptest.Server + proxy *Proxy + url *url.URL + ca signer + clientCert *tls.Certificate + rows []map[string]any + rowsMu sync.Mutex +} + +func newHarness(t *testing.T, site *httptest.Server, universe fixedUniverse, trustSite bool) *harness { + t.Helper() + ca, err := mintCA(CAName) + if err != nil { + t.Fatal(err) + } + leaves, err := NewLeaves(ca) + if err != nil { + t.Fatal(err) + } + roots := x509.NewCertPool() + if trustSite && site.Certificate() != nil { + roots.AddCert(site.Certificate()) + } + h := &harness{t: t, site: site, ca: ca} + h.proxy = &Proxy{ + Leaves: leaves, Universe: universe, Roots: roots, + Resolve: func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil + }, + Allowed: func(netip.Addr) bool { return true }, + Routed: func() map[string]string { return nil }, + Log: func(row map[string]any) { + h.rowsMu.Lock() + h.rows = append(h.rows, row) + h.rowsMu.Unlock() + }, + } + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { ln.Close() }) + go h.proxy.Serve(ln) + h.url, _ = url.Parse("http://" + ln.Addr().String()) + return h +} + +func (h *harness) get(scheme string, clientTime time.Time, http2 bool) (*http.Response, string, error) { + roots := x509.NewCertPool() + if h.site.Certificate() != nil { + roots.AddCert(h.site.Certificate()) + } + roots.AddCert(h.ca.cert) + config := &tls.Config{RootCAs: roots, Time: func() time.Time { return clientTime }} + if h.clientCert != nil { + config.Certificates = []tls.Certificate{*h.clientCert} + } + transport := &http.Transport{Proxy: http.ProxyURL(h.url), ForceAttemptHTTP2: http2, TLSClientConfig: config} + defer transport.CloseIdleConnections() + _, port, _ := net.SplitHostPort(h.site.Listener.Addr().String()) + resp, err := (&http.Client{Transport: transport, Timeout: 10 * time.Second}).Get( + fmt.Sprintf("%s://example.com:%s/hello", scheme, port)) + if err != nil { + return nil, "", err + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + return resp, string(body), err +} + +func (h *harness) lastRow() map[string]any { + deadline := time.Now().Add(2 * time.Second) + for time.Now().Before(deadline) { + h.rowsMu.Lock() + n := len(h.rows) + h.rowsMu.Unlock() + if n > 0 { + break + } + time.Sleep(10 * time.Millisecond) + } + h.rowsMu.Lock() + defer h.rowsMu.Unlock() + if len(h.rows) == 0 { + h.t.Fatal("no row logged") + } + return h.rows[len(h.rows)-1] +} + +func site(t *testing.T, http2 bool) *httptest.Server { + s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello over %s", r.Proto) + })) + s.EnableHTTP2 = http2 + s.StartTLS() + t.Cleanup(s.Close) + return s +} + +func TestOnRealTimeTheClientsBytesReachTheSiteUntouched(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + resp, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello over HTTP/1.1" { + t.Fatalf("%q %v", body, err) + } + if !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { + t.Fatal("the client didn't see the site's own certificate") + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "real" { + t.Fatalf("%v", row) + } +} + +func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t *testing.T) { + s := site(t, true) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now, now}}, true) + resp, _, err := h.get("https", time.Now(), true) + if err != nil || !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { + t.Fatalf("inside: %v", err) + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "inside" { + t.Fatalf("%v", row) + } + + // httptest's certificate ends in 2084: a universe in 2090 is outside it. + later := time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC) + u := float64(later.Unix()) + h = newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u, u}}, true) + resp, body, err := h.get("https", later, true) + if err != nil || body != "hello over HTTP/2.0" { + t.Fatalf("outside: %q %v", body, err) + } + leaf := resp.TLS.PeerCertificates[0] + if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { + t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) + } + row := h.lastRow() + if row["mode"] != "intercept" || row["decision"] != "outside" || row["alpn"] != "h2" || row["verified"] != true { + t.Fatalf("%v", row) + } +} + +func TestASiteThatDoesntVerifyIsHandedOnUnderACATrustedNowhere(t *testing.T) { + s := site(t, false) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, false) + _, _, err := h.get("https", time.Now(), false) + if err == nil || !strings.Contains(err.Error(), "certificate") { + t.Fatalf("the client took it: %v", err) + } + if row := h.lastRow(); row["decision"] != "unverified" { + t.Fatalf("%v", row) + } +} + +func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "plain %s %s ua=%q", r.Method, r.URL.Path, r.UserAgent()) + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, body, err := h.get("http", time.Now(), false) + if err != nil || !strings.HasPrefix(body, "plain GET /hello") { + t.Fatalf("%q %v", body, err) + } +} + +func TestARefusedDestinationIsAnswered403(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + h.proxy.Allowed = isGlobal + _, _, err := h.get("https", time.Now(), false) + if err == nil || !strings.Contains(err.Error(), "Forbidden") { + t.Fatalf("%v", err) + } + if row := h.lastRow(); row["mode"] != "refused" { + t.Fatalf("%v", row) + } +} + +func TestEveryCheckedAddressIsTriedInTurn(t *testing.T) { + s := site(t, false) + h := newHarness(t, s, fixedUniverse{}, true) + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil + } + _, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello over HTTP/1.1" { + t.Fatalf("%q %v", body, err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "127.0.0.1:") { + t.Fatalf("%v", row) + } +} + +func TestAnUpgradeOverPlainHTTPRelaysBothWays(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Upgrade") != "echo" || !strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") { + w.WriteHeader(http.StatusBadRequest) + return + } + conn, rw, _ := w.(http.Hijacker).Hijack() + defer conn.Close() + rw.WriteString("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: echo\r\n\r\n") + rw.Flush() + line, _ := rw.ReadString('\n') + rw.WriteString(line) + rw.Flush() + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) + conn, err := net.Dial("tcp", h.url.Host) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + conn.SetDeadline(time.Now().Add(5 * time.Second)) + fmt.Fprintf(conn, "GET http://example.com:%s/ws HTTP/1.1\r\nHost: example.com:%s\r\nConnection: Upgrade\r\n"+ + "Upgrade: echo\r\n\r\n", port, port) + br := bufio.NewReader(conn) + resp, err := http.ReadResponse(br, nil) + if err != nil || resp.StatusCode != http.StatusSwitchingProtocols { + t.Fatalf("%v %v", resp, err) + } + fmt.Fprint(conn, "ping\n") + if line, err := br.ReadString('\n'); err != nil || line != "ping\n" { + t.Fatalf("%q %v", line, err) + } +} + +func TestAnUploadThatWaitsFor100ContinueGoesThroughAtOnce(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + fmt.Fprint(w, len(body)) + })) + t.Cleanup(s.Close) + h := newHarness(t, s, fixedUniverse{}, false) + _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) + req, _ := http.NewRequest("POST", "http://example.com:"+port+"/up", bytes.NewReader(make([]byte, 100000))) + req.Header.Set("Expect", "100-continue") + transport := &http.Transport{Proxy: http.ProxyURL(h.url), ExpectContinueTimeout: 10 * time.Second} + defer transport.CloseIdleConnections() + started := time.Now() + resp, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(req) + if err != nil { + t.Fatal(err) + } + body, _ := io.ReadAll(resp.Body) + resp.Body.Close() + if string(body) != "100000" || time.Since(started) > 5*time.Second { + t.Fatalf("%q after %s", body, time.Since(started)) + } +} + +func TestEveryCertificateOfTheVerifiedChainBoundsPassingThrough(t *testing.T) { + year := func(y int) time.Time { return time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) } + leaf := &x509.Certificate{NotBefore: year(2020), NotAfter: year(2030)} + intermediate := &x509.Certificate{NotBefore: year(2019), NotAfter: year(2025)} + root := &x509.Certificate{NotBefore: year(2000), NotAfter: year(2040)} + from, until := chainWindow([][]*x509.Certificate{{leaf, intermediate, root}}) + if !from.Equal(year(2020)) || !until.Equal(year(2025)) { + t.Fatalf("%s to %s", from, until) + } + if inside(from, until, []float64{float64(year(2027).Unix())}) { + t.Fatal("a universe past the intermediate's end passed through") + } +} + +func TestASiteAskingForTheClientsCertificateIsPassedThroughForTheClientToAnswer(t *testing.T) { + s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "hello %d certificate", len(r.TLS.PeerCertificates)) + })) + s.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, MaxVersion: tls.VersionTLS12} + s.StartTLS() + t.Cleanup(s.Close) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) + client, err := mintCA("client") + if err != nil { + t.Fatal(err) + } + h.clientCert = &tls.Certificate{Certificate: [][]byte{client.cert.Raw}, PrivateKey: client.key} + _, body, err := h.get("https", time.Now(), false) + if err != nil || body != "hello 1 certificate" { + t.Fatalf("%q %v", body, err) + } + if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "probe_failed" { + t.Fatalf("%v", row) + } +} + +func TestTheProbeAndLeafCachesStayWithinTheirLimit(t *testing.T) { + limit := cacheLimit + cacheLimit = 3 + defer func() { cacheLimit = limit }() + ca, _ := mintCA(CAName) + leaves, _ := NewLeaves(ca) + for i := 0; i < 10; i++ { + if _, _, err := leaves.For(fmt.Sprintf("site%d.test", i), nil, true); err != nil { + t.Fatal(err) + } + } + if len(leaves.cache) != 3 || leaves.cache["site9.test|"] == nil { + t.Fatalf("%d leaves kept", len(leaves.cache)) + } + p := &Proxy{Dial: func(string) (net.Conn, error) { return nil, fmt.Errorf("unreachable") }} + for i := 0; i < 10; i++ { + p.probeAt(fmt.Sprintf("192.0.2.%d:443", i), "", "x", goHello(t, "x", nil)) + } + if len(p.probes.cache) != 3 { + t.Fatalf("%d probes kept", len(p.probes.cache)) + } +} + +func TestAConnectionTheProbeLetsThroughGoesToTheServerItProbed(t *testing.T) { + s := site(t, false) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil + } + var mu sync.Mutex + var dialled []string + h.proxy.Dial = func(dst string) (net.Conn, error) { + mu.Lock() + dialled = append(dialled, dst) + mu.Unlock() + return net.DialTimeout("tcp", dst, DialTimeout) + } + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + row := h.lastRow() + mu.Lock() + defer mu.Unlock() + // the probe tried 127.0.0.2, then reached 127.0.0.1; the connection went straight to 127.0.0.1 + if row["decision"] != "inside" || len(dialled) != 3 || !strings.HasPrefix(dialled[2], "127.0.0.1:") { + t.Fatalf("%v %v", row, dialled) + } + if hit := h.proxy.probes.cache[dialled[0]+"|example.com"]; !hit.unreachable || hit.verified { + t.Fatalf("the address that didn't answer is cached as %+v", hit) + } +} + +func TestASlowProbeHoldsUpNoOtherSite(t *testing.T) { + release := make(chan struct{}) + p := &Proxy{Dial: func(dst string) (net.Conn, error) { + if strings.HasPrefix(dst, "192.0.2.1:") { + <-release + } + return nil, fmt.Errorf("unreachable") + }} + hello := goHello(t, "x", nil) + p.probeAt("192.0.2.2:443", "", "x", hello) + go p.probeAt("192.0.2.1:443", "", "x", hello) + time.Sleep(50 * time.Millisecond) + started := time.Now() + if _, probed := p.probeAt("192.0.2.2:443", "", "x", hello); probed || time.Since(started) > 100*time.Millisecond { + t.Fatalf("a cached site waited %s behind a slow one (probed again: %v)", time.Since(started), probed) + } + close(release) +} + +// standIns resolves example.com to its addresses and dials each to the site unless it's down; both may change +// between requests. +type standIns struct { + mu sync.Mutex + addresses []string + down map[string]bool +} + +func (s *standIns) set(down []string, addresses ...string) { + s.mu.Lock() + defer s.mu.Unlock() + s.addresses, s.down = addresses, map[string]bool{} + for _, a := range down { + s.down[a] = true + } +} + +func newStandIns(h *harness, addresses ...string) *standIns { + s := &standIns{addresses: addresses, down: map[string]bool{}} + h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { + s.mu.Lock() + defer s.mu.Unlock() + var out []netip.Addr + for _, a := range s.addresses { + out = append(out, netip.MustParseAddr(a)) + } + return out, nil + } + h.proxy.Dial = func(dst string) (net.Conn, error) { + host, _, _ := net.SplitHostPort(dst) + s.mu.Lock() + down := s.down[host] + s.mu.Unlock() + if down { + return nil, fmt.Errorf("%s is down", host) + } + return net.DialTimeout("tcp", h.site.Listener.Addr().String(), DialTimeout) + } + return s +} + +func TestAnAddressThatStopsAnsweringAfterItsProbeGivesWayToTheNext(t *testing.T) { + s := site(t, false) + now := float64(time.Now().Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) + addresses := newStandIns(h, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.1:") { + t.Fatalf("%v", row) + } + addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Now(), false); err != nil { + t.Fatal(err) + } + if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["decision"] != "inside" { + t.Fatalf("%v", row) + } +} + +func TestAnAddressThatDidntAnswerLeavesANewAddressToItsOwnProbe(t *testing.T) { + s := site(t, false) + u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) + h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) + addresses := newStandIns(h) + addresses.set([]string{"198.51.100.1"}, "198.51.100.1") + if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err == nil { + t.Fatal("reached a site none of whose addresses answer") + } + addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") + if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err != nil { + t.Fatal(err) + } + row := h.lastRow() + if !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["mode"] != "intercept" || row["decision"] != "outside" { + t.Fatalf("the new address wasn't judged by its own certificate: %v", row) + } +} + +func TestAClientHelloWhoseFieldsOverrunItIsRefusedNotPanickedOn(t *testing.T) { + hello := goHello(t, "example.com", nil) + p := 4 + 2 + 32 + p += 1 + int(hello[p]) + p += 2 + int(hello[p])<<8 | int(hello[p+1]) + bad := append([]byte{}, hello...) + bad[p] = 0xff // compression methods' length, past the message's end + bad = bad[:p+3] + if _, _, err := parseHello(bad); err == nil { + t.Fatal("parseHello took it") + } + if _, _, err := helloRecord(bad); err == nil { + t.Fatal("helloRecord took it") + } +} + +func TestAProbeThatPanicsLeavesNoOneWaitingOnIt(t *testing.T) { + p := &Proxy{Dial: func(string) (net.Conn, error) { panic("dial") }} + hello := goHello(t, "x", nil) + probe := func() (panicked bool) { + defer func() { panicked = recover() != nil }() + p.probeAt("192.0.2.9:443", "", "x", hello) + return false + } + if !probe() { + t.Fatal("the probe didn't panic") + } + done := make(chan bool) + go func() { done <- probe() }() + select { + case <-done: + case <-time.After(2 * time.Second): + t.Fatal("a second probe waited on the one that panicked") + } +} + +func TestAnAddressAlreadyFoundWorkingIsTriedBeforeOnesNotYetProbed(t *testing.T) { + p := &Proxy{} + p.probes.cache = map[string]probe{ + "[2001:db8::1]:443|x": {verified: true, at: time.Now()}, + "192.0.2.1:443|x": {unreachable: true, at: time.Now().Add(-2 * ProbeFailedTTL)}, + "192.0.2.3:443|x": {unreachable: true, at: time.Now()}, + } + got := p.preferChecked([]string{"192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443", "[2001:db8::1]:443"}, "x") + want := []string{"[2001:db8::1]:443", "192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443"} + if strings.Join(got, " ") != strings.Join(want, " ") { + t.Fatalf("%v", got) + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex new file mode 100644 index 000000000..2aecf27e8 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex @@ -0,0 +1 @@ +4145434b02000000040000000000000000008ab9359ae51515cd68be7e91e51800000000000000400100000000000000070000000000000002b1a878f27237d300f2052a0100000000f2052a01000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex new file mode 100644 index 000000000..7f6a95f54 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex @@ -0,0 +1 @@ +4145434b02000000020000000000000000000000000000000000000000000000000000000000f03f00000000000000000300000000000000147c4a7fb979c7a100000000000000000000000000000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/upstream.go b/src/agent_env/agents/agents/claude-code/egress/upstream.go new file mode 100644 index 000000000..ccf32704f --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/upstream.go @@ -0,0 +1,268 @@ +package main + +import ( + "crypto/x509" + "errors" + "fmt" + "net" + "strings" + "sync" + "time" + + utls "github.com/refraction-networking/utls" +) + +// upstream is what a handshake with a site found: the protocol it picked, its leaf's validity and names, and whether +// that leaf verifies against real time and the public roots. +type upstream struct { + // Dst is the address the handshake reached. + Dst string + ALPN string + Verified bool + VerifyErr string + NotBefore time.Time + NotAfter time.Time + SANs []string + Version string + Cipher string + Dropped []uint16 +} + +// handshake is uTLS's handshake with the site at dst, built from the client's own ClientHello, so the site sees the +// client's fingerprint and ALPN offer. Records go full-size from the start and the connection closes without a +// close_notify, as Chromium's BoringSSL does. +func (p *Proxy) handshake(dsts []string, sni, verifyName string, hello []byte) (*utls.UConn, upstream, error) { + var found upstream + record, gone, err := helloRecord(hello) + if err != nil { + return nil, found, err + } + found.Dropped = gone + spec, err := (&utls.Fingerprinter{AllowBluntMimicry: true}).FingerprintClientHello(record) + if err != nil { + return nil, found, fmt.Errorf("fingerprint: %w", err) + } + raw, dst, err := p.dialAny(dsts) + if err != nil { + return nil, found, dialError{err} + } + found.Dst = dst + uc := utls.UClient(raw, &utls.Config{ServerName: sni, InsecureSkipVerify: true, OmitEmptyPsk: true, + DynamicRecordSizingDisabled: true}, utls.HelloCustom) + if err := uc.ApplyPreset(spec); err != nil { + raw.Close() + return nil, found, fmt.Errorf("apply: %w", err) + } + raw.SetDeadline(time.Now().Add(HandshakeTimeout)) + if err := uc.Handshake(); err != nil { + raw.Close() + return nil, found, fmt.Errorf("handshake: %w", err) + } + raw.SetDeadline(time.Time{}) + st := uc.ConnectionState() + found.ALPN, found.Version, found.Cipher = st.NegotiatedProtocol, fmt.Sprintf("%#04x", st.Version), + utls.CipherSuiteName(st.CipherSuite) + if len(st.PeerCertificates) == 0 { + found.VerifyErr = "no certificate" + return uc, found, nil + } + leaf := st.PeerCertificates[0] + found.NotBefore, found.NotAfter = leaf.NotBefore, leaf.NotAfter + for _, name := range leaf.DNSNames { + found.SANs = append(found.SANs, "DNS:"+name) + } + for _, ip := range leaf.IPAddresses { + found.SANs = append(found.SANs, "IP:"+ip.String()) + } + intermediates := x509.NewCertPool() + for _, c := range st.PeerCertificates[1:] { + intermediates.AddCert(c) + } + chains, err := leaf.Verify(x509.VerifyOptions{Roots: p.Roots, Intermediates: intermediates, DNSName: verifyName, + CurrentTime: time.Now()}) + if err != nil { + found.VerifyErr = err.Error() + } else { + found.Verified = true + found.NotBefore, found.NotAfter = chainWindow(chains) + } + return uc, found, nil +} + +// chainWindow is the dates every certificate of every verified chain is valid between: a client building any of +// those chains takes the site only then, so passing it through is safe only inside them. +func chainWindow(chains [][]*x509.Certificate) (time.Time, time.Time) { + var from, until time.Time + for _, chain := range chains { + for _, cert := range chain { + if from.IsZero() || cert.NotBefore.After(from) { + from = cert.NotBefore + } + if until.IsZero() || cert.NotAfter.Before(until) { + until = cert.NotAfter + } + } + } + return from, until +} + +// probe is what a handshake of the proxy's own found of one address of a site: the dates its verified chain shares, +// or that it didn't answer, or that its handshake didn't finish. It's cached per (address, SNI): a finished one for +// ProbeTTL, the others for ProbeFailedTTL, since they may be a moment's trouble. +type probe struct { + from, until time.Time + verified bool + // failed is a handshake that didn't finish; unreachable, an address that didn't answer. + failed, unreachable bool + at time.Time + problem string +} + +func (hit probe) fresh() bool { + if hit.failed || hit.unreachable { + return time.Since(hit.at) < ProbeFailedTTL + } + return time.Since(hit.at) < ProbeTTL +} + +// probes caches each address's probe, up to cacheLimit entries, the stale ones dropped first once it's full. An +// address's concurrent probes wait on the one under way; no lock is held while one is. +type probes struct { + mu sync.Mutex + cache map[string]probe + underway map[string]*underway +} + +type underway struct { + done chan struct{} + hit probe + ok bool +} + +// cacheLimit bounds the probe and leaf caches, so a computer that visits many sites keeps a bounded memory. +var cacheLimit = 4096 + +// dialError is a handshake's that never reached the site. +type dialError struct{ error } + +// probeAt is the probe of dst for sni, cached or new, and whether it probed now. +func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) { + key := dst + "|" + sni + p.probes.mu.Lock() + if p.probes.cache == nil { + p.probes.cache, p.probes.underway = map[string]probe{}, map[string]*underway{} + } + if hit, ok := p.probes.cache[key]; ok && hit.fresh() { + p.probes.mu.Unlock() + return hit, false + } + if running, ok := p.probes.underway[key]; ok { + p.probes.mu.Unlock() + <-running.done + if !running.ok { + return probe{at: time.Now(), failed: true, problem: "the probe it waited on failed"}, false + } + return running.hit, false + } + running := &underway{done: make(chan struct{})} + p.probes.underway[key] = running + p.probes.mu.Unlock() + defer func() { + p.probes.mu.Lock() + if running.ok { + p.store(key, running.hit) + } + delete(p.probes.underway, key) + p.probes.mu.Unlock() + close(running.done) + }() + + hit := probe{at: time.Now()} + uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) + var unanswered dialError + switch { + case errors.As(err, &unanswered): + hit.unreachable, hit.problem = true, truncate(err.Error(), 200) + case err != nil: + hit.failed, hit.problem = true, truncate(err.Error(), 200) + case !found.Verified: + hit.problem = truncate(found.VerifyErr, 200) + default: + hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true + } + if uc != nil { + uc.NetConn().Close() + } + running.hit, running.ok = hit, true + return hit, true +} + +// store keeps hit for key, dropping the stale entries, then any others, once the cache is past cacheLimit. +func (p *Proxy) store(key string, hit probe) { + p.probes.cache[key] = hit + if len(p.probes.cache) <= cacheLimit { + return + } + for k, v := range p.probes.cache { + if !v.fresh() { + delete(p.probes.cache, k) + } + } + for k := range p.probes.cache { + if len(p.probes.cache) <= cacheLimit { + break + } + if k != key { + delete(p.probes.cache, k) + } + } +} + +// forget drops dst's probe for sni, for an address that stopped answering since. +func (p *Proxy) forget(dst, sni string) { + p.probes.mu.Lock() + delete(p.probes.cache, dst+"|"+sni) + p.probes.mu.Unlock() +} + +// inside is whether every universe lies inside [from + Margin, until - Margin]. +func inside(from, until time.Time, universes []float64) bool { + lo := float64(from.Add(Margin).UnixNano()) / 1e9 + hi := float64(until.Add(-Margin).UnixNano()) / 1e9 + for _, u := range universes { + if u < lo || u > hi { + return false + } + } + return true +} + +// dialAny dials each address in turn until one answers: which, or every one's error. +func (p *Proxy) dialAny(dsts []string) (net.Conn, string, error) { + var problems []string + for _, dst := range dsts { + conn, err := p.dial(dst) + if err == nil { + return conn, dst, nil + } + problems = append(problems, err.Error()) + } + if len(problems) == 0 { + return nil, "", errors.New("no address to dial") + } + return nil, "", errors.New(strings.Join(problems, "; ")) +} + +func (p *Proxy) dial(dst string) (net.Conn, error) { + if p.Dial != nil { + return p.Dial(dst) + } + return net.DialTimeout("tcp", dst, DialTimeout) +} + +func truncate(s string, n int) string { + if len(s) > n { + return s[:n] + } + return s +} diff --git a/src/agent_env/agents/agents/claude-code/root.conf b/src/agent_env/agents/agents/claude-code/root.conf new file mode 100644 index 000000000..cfa21e733 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/root.conf @@ -0,0 +1,62 @@ +; Root's supervisord (start-agent, FD-5916): the clock follower and the egress proxy, each as its own user, on real +; time, and the A2A server as the agent's user. The server starts with the real-time marker, which agent-env's +; libfaketime takes out of its environment, so the server's own records keep real timestamps and everything it starts, +; the CLI and the model's commands, reads the env's time. Its socket and logs are root's alone, so the agent's user can +; neither stop nor restart any of them. The follower follows nothing until the server writes its config, which +; sync_env_clock has it do, then picks it up within a quarter second. +[supervisord] +nodaemon=true +user=root +logfile=/var/log/agentenv-root/supervisord.log +pidfile=/run/agentenv-root/supervisord.pid +childlogdir=/var/log/agentenv-root + +[unix_http_server] +file=/run/agentenv-root/supervisor.sock +chmod=0700 + +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///run/agentenv-root/supervisor.sock + +[program:clock] +command=/usr/local/bin/python3 -I /usr/local/lib/agentenv-clock/clock-follower +user=agentenv-clock +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/clock.log +environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" + +[program:egress] +command=/usr/local/lib/agentenv-egress/egress serve +user=agentenv-egress +directory=/ +priority=3 +autorestart=true +startsecs=1 +startretries=1000 +redirect_stderr=true +stdout_logfile=/var/log/agentenv-root/egress.log +environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" + +; uvicorn itself, not entrypoint.sh: the marker holds for the process it's given to alone, and a shell that execs +; uvicorn would hand uvicorn an environment without it. +[program:agent] +command=/usr/local/bin/python3 -m uvicorn a2a_server:app --host 0.0.0.0 --port %(ENV_A2A_PORT)s +user=agent +directory=/app +priority=10 +autorestart=true +stopasgroup=true +killasgroup=true +stopwaitsecs=30 +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +environment=HOME="/home/agent",USER="agent",LOGNAME="agent",AGENTENV_REAL_TIME="1",AGENTENV_EGRESS_PROXY="http://127.0.0.1:3128",NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem",SSL_CERT_FILE="/etc/agentenv/ca-bundle.pem",REQUESTS_CA_BUNDLE="/etc/agentenv/ca-bundle.pem" diff --git a/src/agent_env/agents/agents/claude-code/start-agent b/src/agent_env/agents/agents/claude-code/start-agent new file mode 100755 index 000000000..1f170978d --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/start-agent @@ -0,0 +1,19 @@ +#!/bin/sh +# The image's start (FD-5916). As root, it makes what the agent's user must not touch, the clock page and file (the +# clock user's from their first byte) and the egress proxy's CA (its key the egress user's alone, trusted in the +# system store, agent-env's trust files and every certifi bundle), then starts root's supervisord (root.conf), which +# runs the clock follower and the egress proxy, each as its own user on real time, and the A2A server as the agent's +# user on real time, everything it starts reading the env's time from the page. Until sync_env_clock has the agent +# follow a clock, the page holds real time and the proxy relays TLS untouched. Started as another user, the image runs +# the A2A server alone, as before, on real time. +set -eu +if [ "$(id -u)" != 0 ]; then + exec /opt/agent/entrypoint.sh +fi +install -d -o root -g root -m 0700 /var/log/agentenv-root /run/agentenv-root +install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock +python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock +AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off \ + /usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \ + || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; } +exec supervisord -c /etc/agentenv/root.conf diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go index e1ea67894..031555627 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go @@ -31,8 +31,10 @@ type bootPaths struct { NSS func(paths bootPaths) error } +// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in +// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. func defaultBootPaths() bootPaths { - return bootPaths{ + p := bootPaths{ Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, UpdateTrust: []string{"update-ca-certificates"}, @@ -41,6 +43,13 @@ func defaultBootPaths() bootPaths { EgressUser: EgressUser, SessionUser: SessionUser, NSS: sessionNSS, } + if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { + p.SessionUser = name + } + if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { + p.NSS = nil + } + return p } // boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go index 239d8db0a..c5f59ad0a 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go @@ -86,3 +86,14 @@ func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { t.Fatalf("%v", report) } } + +func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { + if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { + t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) + } + t.Setenv("AGENTENV_SESSION_USER", "agent") + t.Setenv("AGENTENV_EGRESS_NSS", "off") + if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { + t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) + } +} diff --git a/tst/unit/agents/claude_code/test_env_clock_image.py b/tst/unit/agents/claude_code/test_env_clock_image.py new file mode 100644 index 000000000..8bef2b3c1 --- /dev/null +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -0,0 +1,66 @@ +"""Claude Code on the env's clock (FD-5916): the image runs the computer base's clock follower and egress proxy, each +as its own user, and agent-env's libfaketime for the agent's user, so the CLI and every command the model runs read +the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. The CLI goes out through +the egress proxy, and to the env's MCP servers directly.""" + +from __future__ import annotations + +import re + +import pytest + +from tst.unit.agents.claude_code.agent import AGENT_DIR, claude_bridge + +DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() +ROOT_CONF = (AGENT_DIR / "root.conf").read_text() +BASE = AGENT_DIR.parents[2] / "computers" / "artifacts" / "ubuntu-24.04" +PROXY = "http://127.0.0.1:3128" + + +@pytest.mark.parametrize("folder", ["clock", "egress"]) +def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(folder): + copied = sorted(p.relative_to(AGENT_DIR / folder) for p in (AGENT_DIR / folder).rglob("*") if p.is_file()) + + assert copied + assert [str(p) for p in copied if (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] + + +def _program(name: str) -> dict[str, str]: + section = re.search(rf"^\[program:{name}\]\n(.*?)(?=^\[|\Z)", ROOT_CONF, re.MULTILINE | re.DOTALL).group(1) + return dict(line.split("=", 1) for line in section.splitlines() if "=" in line and not line.startswith(";")) + + +def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as_the_agents_on_real_time(): + assert (_program("clock")["user"], _program("egress")["user"]) == ("agentenv-clock", "agentenv-egress") + agent = _program("agent") + assert agent["user"] == "agent" + # uvicorn itself, so the marker reaches the server and not a shell that drops it on exec. + assert agent["command"].startswith("/usr/local/bin/python3 -m uvicorn a2a_server:app") + assert 'AGENTENV_REAL_TIME="1"' in agent["environment"] + assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in agent["environment"] + assert 'NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem"' in agent["environment"] + + +def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(): + assert "> /etc/ld.so.preload" in DOCKERFILE and "libfaketimeMT.so.1" in DOCKERFILE + assert 's/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/' in DOCKERFILE + assert "install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock" in DOCKERFILE + assert not re.search(r"^USER ", DOCKERFILE, re.MULTILINE) + + +def test_started_as_another_user_the_image_runs_the_server_alone_as_before(): + start = (AGENT_DIR / "start-agent").read_text() + assert 'if [ "$(id -u)" != 0 ]; then\n exec /opt/agent/entrypoint.sh' in start + assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start + + +def test_where_the_image_runs_the_proxy_the_cli_goes_out_through_it_and_to_the_envs_servers_directly(): + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"]) + + assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY} + assert env["NO_PROXY"] == env["no_proxy"] == "localhost,127.0.0.1,::1,host.docker.internal,gateway" + assert env["NODE_USE_ENV_PROXY"] == "1" + + +def test_without_the_proxy_the_cli_is_run_as_before(): + assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env({})) diff --git a/tst/unit/agents/claude_code/test_image_contract.py b/tst/unit/agents/claude_code/test_image_contract.py index c8e528df3..3dc316cc5 100644 --- a/tst/unit/agents/claude_code/test_image_contract.py +++ b/tst/unit/agents/claude_code/test_image_contract.py @@ -17,7 +17,8 @@ def test_every_file_the_image_copies_is_in_the_folder(): copied = [name for line in re.findall(r"^COPY (?!--from=)(.+) \S+$", DOCKERFILE, re.MULTILINE) for name in line.split()] - assert copied and all((AGENT_DIR / name).is_file() for name in copied), copied + assert copied and all((AGENT_DIR / name).is_file() or (name.endswith("/") and (AGENT_DIR / name).is_dir()) + for name in copied), copied # install.sh and run.sh are install/v1's; a build context pruned to what the Dockerfile reads keeps them so. assert {"a2a_server.py", "claude_bridge.py", "kit.py", "tar_changelog.py", "entrypoint.sh", "install.sh", "run.sh"} <= set(copied) @@ -54,5 +55,5 @@ def test_the_image_pins_its_cli_checks_its_checksum_and_keeps_tls_on(): def test_the_image_starts_through_cmd_which_modal_passes_its_env_to(): - assert 'CMD ["/opt/agent/entrypoint.sh"]' in DOCKERFILE + assert 'CMD ["/usr/local/bin/start-agent"]' in DOCKERFILE assert not re.search(r"^ENTRYPOINT", DOCKERFILE, re.MULTILINE) From bce00d4acb11ae4a3d763d9d5e640e1f07e84826 Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:37:10 -0800 Subject: [PATCH 04/29] fix(agents): agent clock review: new config's status, one clock per agent, retire on teardown - An agent's sync_time takes only a status of the config it just wrote (its URL and name, written after it), so what the follower showed of an earlier gateway or token never passes for success. - An agent follows one env's clock; another env's is skipped as follows_another_env, as its one follower can't watch two gateways. deploy_agent's late follows are recorded with the sync's, so both see it. - teardown_sandboxes retires a torn-down agent's follower (DELETE /clock/followers, retire_follower), so later writes don't wait for it or answer 504 naming it. A declaration no longer replaces a follower the gateway started with. - Logs say an outcome's synced, reason and follower, never the agent's answer; the timeouts, token size and error length are named constants. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../.agentenv/browser-chromium.jsonl | 15 ++++ packages/agentenv-protocol/.agentenv/launches | 12 ++++ .../src/agentenv_protocol/a2a_agent/clock.py | 23 ++++-- .../src/agentenv_protocol/computer/clock.py | 41 ++++++++--- .../tests/test_a2a_agent_clock.py | 47 ++++++++++++- src/agent_env/env/gateway/clock_followers.py | 17 ++++- src/agent_env/env/gateway/constants.py | 3 +- src/agent_env/env/gateway/gateway.py | 11 +++ .../task_step/task_steps/agent_clock.py | 70 ++++++++++++++++--- .../task_step/task_steps/deploy_agent.py | 2 +- .../task_step/task_steps/sync_env_clock.py | 6 +- .../task_steps/teardown_sandboxes.py | 3 + tst/unit/env/gateway/clock_watch_test.py | 40 +++++++++-- tst/unit/task_step/agent_clock_test.py | 61 ++++++++++++++++ 14 files changed, 312 insertions(+), 39 deletions(-) create mode 100644 packages/agentenv-protocol/.agentenv/browser-chromium.jsonl create mode 100644 packages/agentenv-protocol/.agentenv/launches diff --git a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl new file mode 100644 index 000000000..ab4e9b8bf --- /dev/null +++ b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl @@ -0,0 +1,15 @@ +{"seq": 1, "at": "2026-11-17T00:08:55.941Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 2, "at": "2026-11-17T00:08:55.978Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 3, "at": "2026-11-17T00:08:55.978Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 4, "at": "2026-11-17T00:09:25.887Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 5, "at": "2026-11-17T00:09:25.924Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 6, "at": "2026-11-17T00:09:25.924Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 7, "at": "2026-11-17T00:10:10.264Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 8, "at": "2026-11-17T00:10:10.264Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 9, "at": "2026-11-17T00:10:10.282Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 10, "at": "2026-11-17T00:12:10.519Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 11, "at": "2026-11-17T00:12:10.519Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 12, "at": "2026-11-17T00:12:10.536Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 13, "at": "2026-11-17T00:35:20.724Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 14, "at": "2026-11-17T00:35:20.724Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 15, "at": "2026-11-17T00:35:20.741Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} diff --git a/packages/agentenv-protocol/.agentenv/launches b/packages/agentenv-protocol/.agentenv/launches new file mode 100644 index 000000000..0687a6131 --- /dev/null +++ b/packages/agentenv-protocol/.agentenv/launches @@ -0,0 +1,12 @@ +{"at": "2026-11-17T00:10:13.131730+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:10:16.178539+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:10:16.361537+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:10:17.051016+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:12:13.386381+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:12:16.437758+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:12:16.619313+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:12:17.317866+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:35:23.501958+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:35:26.556895+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:35:26.739266+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T00:35:27.431578+00:00", "event": "launch", "pid": 4242, "cause": "launch"} diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py index 938f4f16c..13954b99f 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py @@ -26,20 +26,29 @@ def follows() -> bool: return follower.follows() -def write_config(watch_url: str, name: str, read_token: str) -> None: +def write_config(watch_url: str, name: str, read_token: str) -> int: """The follower's config, written beside its place and renamed into it, so the follower, which reads it every - quarter second, never reads half of one; readable by this user and the follower's group alone.""" + quarter second, never reads half of one; readable by this user and the follower's group alone. When it was + written (``st_mtime_ns``), which a status must postdate to be of this config; 0 when the config was this one + already, which the follower goes on following without writing its status anew.""" path = Path(follower.CONFIG) + raw = json.dumps({"watch_url": watch_url, "name": name, "token": read_token}).encode() + try: + if path.read_bytes() == raw: + return 0 + except OSError: + pass staged = path.with_name(f"{path.name}.{uuid.uuid4().hex}") fd = os.open(staged, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o640) try: - with os.fdopen(fd, "w") as f: - json.dump({"watch_url": watch_url, "name": name, "token": read_token}, f) + with os.fdopen(fd, "wb") as f: + f.write(raw) os.chmod(staged, 0o640) os.replace(staged, path) except BaseException: staged.unlink(missing_ok=True) raise + return path.stat().st_mtime_ns def set_zone(timezone: str) -> str: @@ -59,7 +68,7 @@ async def sync_time(env_get_time_url: str, timezone: str | None = None, watch_ur name: str | None = None, read_token: str | None = None) -> dict: """``clock/v1``'s ``sync_time`` on an agent: the follower configured to follow the gateway at ``watch_url`` as ``name`` with ``read_token``, the task's ``timezone`` set for what this server starts, and an answer once the - follower shows the gateway's version. ``env_get_time_url`` is taken as every server takes it and not used: the + follower shows the gateway's version for that config, never what it showed of one before. ``env_get_time_url`` is taken as every server takes it and not used: the follower reads the gateway's watch, not its time.""" if not follows(): raise TargetUnavailable("this agent's image doesn't run the clock follower") @@ -69,6 +78,6 @@ async def sync_time(env_get_time_url: str, timezone: str | None = None, watch_ur + (", ".join(k for k, v in (("watch_url", watch_url), ("name", name), ("read_token", read_token)) if v) or "none of them")) zone = set_zone(timezone) if timezone is not None else os.environ.get("TZ") or "UTC" - write_config(watch_url, name, read_token) - shown = await follower.following("this agent's clock follower") + written = write_config(watch_url, name, read_token) + shown = await follower.following("this agent's clock follower", follower.CONNECT_WAIT_S, url=watch_url, name=name, since_ns=written) return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, "follower": shown} diff --git a/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py b/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py index d8f19c92d..d19851871 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/computer/clock.py @@ -47,11 +47,17 @@ def session_zone() -> str: def follower_status() -> dict: + return _status_written()[0] + + +def _status_written() -> tuple[dict, int]: + """The follower's status and when it wrote it (``st_mtime_ns``; 0 when there is none).""" try: - status = json.loads(Path(STATUS).read_text()) + path = Path(STATUS) + written, status = path.stat().st_mtime_ns, json.loads(path.read_text()) except (OSError, ValueError): - return {} - return status if isinstance(status, dict) else {} + return {}, 0 + return (status, written) if isinstance(status, dict) else ({}, 0) def gateway_version(watch_url: str) -> str: @@ -60,7 +66,10 @@ def gateway_version(watch_url: str) -> str: return json.loads(answer.read())["version"] -def _why(status: dict, current: Optional[str], unread: Optional[str]) -> str: +def _why(status: dict, current: Optional[str], unread: Optional[str], stale: bool = False) -> str: + if stale: + return (f"it still shows the config it had, the gateway at {status.get('url')} as {status.get('name')!r}, " + f"not the one just written") if status.get("error"): return f"it can't reach the gateway at {status.get('url')}: {status['error']}" if not status.get("url"): @@ -86,21 +95,31 @@ async def sync_time(env_get_time_url: str, timezone: Optional[str] = None) -> di return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, "follower": follower} -async def following(whose: str, wait_s: float = CONNECT_WAIT_S) -> dict: +async def following(whose: str, wait_s: float = CONNECT_WAIT_S, *, url: Optional[str] = None, + name: Optional[str] = None, since_ns: int = 0) -> dict: """The follower's status once it shows the gateway's current version; TargetUnavailable saying why ``whose`` doesn't within ``wait_s``. A computer's ``sync_time`` and an agent's (``agentenv_protocol.a2a_agent.clock``) both answer - with it.""" + with it. An agent's, which has just written the follower's config, names it: only a status of that ``url`` and + ``name`` the follower wrote since ``since_ns`` counts, so what it showed of a config before is never taken for + this one's.""" deadline = time.monotonic() + wait_s + + def read() -> tuple[dict, bool]: + status, written = _status_written() + stale = bool(status) and (written < since_ns or (url is not None and status.get("url") != url) + or (name is not None and status.get("name") != name)) + return status, stale + while True: - status, current, unread = follower_status(), None, None - if status.get("connected") and status.get("url"): + (status, stale), current, unread = read(), None, None + if not stale and status.get("connected") and status.get("url"): try: current = await asyncio.to_thread(gateway_version, status["url"]) except Exception as e: # noqa: BLE001 - away or answering something else: said as why, tried again unread = f"{type(e).__name__}: {e}"[:300] - status = follower_status() - if current is not None and status.get("connected") and status.get("version") == current: + status, stale = read() + if not stale and current is not None and status.get("connected") and status.get("version") == current: return {key: status.get(key) for key in ("name", "url", "version", "armed")} if time.monotonic() >= deadline: - raise TargetUnavailable(f"{whose} isn't following the gateway: " + _why(status, current, unread)) + raise TargetUnavailable(f"{whose} isn't following the gateway: " + _why(status, current, unread, stale)) await asyncio.sleep(0.1) diff --git a/packages/agentenv-protocol/tests/test_a2a_agent_clock.py b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py index 3a09b3a42..186fe506b 100644 --- a/packages/agentenv-protocol/tests/test_a2a_agent_clock.py +++ b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py @@ -6,6 +6,7 @@ import json import os import stat +import threading import time import pytest @@ -44,6 +45,24 @@ def _status(image, **status) -> None: (image / "status.json").write_text(json.dumps(status)) +def _follows_the_next_config(image, **status) -> threading.Thread: + """A follower that, as the real one, writes its status once it reads a config other than the one it had.""" + config = image / "follower.json" + before = config.read_bytes() if config.exists() else None + + def run() -> None: + deadline = time.monotonic() + 5 + while time.monotonic() < deadline: + if config.exists() and config.read_bytes() != before: + _status(image, **status) + return + time.sleep(0.01) + + thread = threading.Thread(target=run, daemon=True) + thread.start() + return thread + + def _app(**declared): @a2a_agent(identity=AgentIdentity(name="clocked", description="test", version="1"), **declared) class Agent(AgentEnvAgent): @@ -74,7 +93,8 @@ def test_an_agent_in_an_image_without_the_follower_serves_no_clock(image, monkey def test_sync_time_writes_the_followers_config_sets_the_zone_and_answers_once_it_follows(image): - _status(image, connected=True, url="http://gw/clock/watch", name="agent-solver", version="b:3", armed=True) + _follows_the_next_config(image, connected=True, url="http://gw/clock/watch", name="agent-solver", version="b:3", + armed=True) with _app() as client: answer = client.post("/ext/clock/sync-time", json={**REQUEST, "timezone": "America/Chicago"}) @@ -91,7 +111,7 @@ def test_sync_time_writes_the_followers_config_sets_the_zone_and_answers_once_it def test_a_follower_behind_the_gateway_is_named(image): - _status(image, connected=True, url="http://gw/clock/watch", version="b:2") + _follows_the_next_config(image, connected=True, url="http://gw/clock/watch", name="agent-solver", version="b:2") with _app() as client: answer = client.post("/ext/clock/sync-time", json=REQUEST) @@ -113,3 +133,26 @@ def test_a_zone_that_isnt_one_is_refused(image): answer = client.post("/ext/clock/sync-time", json={**REQUEST, "timezone": "Mars/Olympus"}) assert answer.status_code == 400 and "IANA" in answer.text and os.environ["TZ"] == "UTC" + + +@pytest.mark.parametrize("before", [ + {"url": "http://old-gw/clock/watch", "name": "agent-solver"}, # another gateway: it may answer the same version + {"url": "http://gw/clock/watch", "name": "agent-solver"}, # this one, under the token the config replaced +]) +def test_what_the_follower_showed_of_its_config_before_never_passes_for_the_new_one(image, before): + _status(image, connected=True, version="b:3", armed=True, **before) + with _app() as client: + answer = client.post("/ext/clock/sync-time", json=REQUEST) + + assert answer.status_code == 404 + assert "still shows the config it had" in answer.text + + +def test_the_config_it_already_follows_resent_answers_from_its_status(image): + with _app() as client: + _follows_the_next_config(image, connected=True, url="http://gw/clock/watch", name="agent-solver", + version="b:3") + assert client.post("/ext/clock/sync-time", json=REQUEST).status_code == 200 + answer = client.post("/ext/clock/sync-time", json=REQUEST) # the follower writes nothing anew + + assert answer.status_code == 200, answer.text diff --git a/src/agent_env/env/gateway/clock_followers.py b/src/agent_env/env/gateway/clock_followers.py index 0f9e4cd66..55404c34c 100644 --- a/src/agent_env/env/gateway/clock_followers.py +++ b/src/agent_env/env/gateway/clock_followers.py @@ -1,5 +1,5 @@ """The followers of the gateway's clock (the computers on it, declared at its start, and the agents a run declares -after it, ``declare``): each long-polls ``/clock/watch`` with the version it +after it, ``declare``, and retires when it tears one down, ``retire``): each long-polls ``/clock/watch`` with the version it shows, which is its acknowledgement, and a clock write can wait until every one shows the write's version.""" from __future__ import annotations @@ -42,6 +42,7 @@ def __init__(self, clock: Callable[[], Clock], tokens: Mapping[str, str]): self._tokens = dict(tokens) self._shown: dict[str, int | None] = dict.fromkeys(self._tokens) self._seen: dict[str, float] = {} + self._declared: set[str] = set() self._changed = asyncio.Event() self._acks = asyncio.Condition() @@ -50,9 +51,23 @@ def declare(self, name: str, token: str) -> None: run deploys. Declared again, as a redeploy does, it takes the new token and is waited for afresh.""" if not name or not token: raise ClockError("a follower takes a name and a read token") + if name in self._tokens and name not in self._declared: + raise ClockError(f"{name!r} is a follower this gateway started with, which a declaration doesn't replace") self._tokens[name] = token self._shown[name] = None self._seen.pop(name, None) + self._declared.add(name) + + def retire(self, name: str) -> bool: + """Stop waiting for ``name``, a follower ``declare`` added, whose agent a run has torn down: its token no + longer acknowledges, and no write waits for it or names it. Whether it was one; a follower the gateway started + with stays.""" + if name not in self._declared: + return False + self._declared.discard(name) + for followers in (self._tokens, self._shown, self._seen): + followers.pop(name, None) + return True def changed(self) -> None: """Answer every held watch: the clock has a new version.""" diff --git a/src/agent_env/env/gateway/constants.py b/src/agent_env/env/gateway/constants.py index 4bdb1555a..b11e9c2f3 100644 --- a/src/agent_env/env/gateway/constants.py +++ b/src/agent_env/env/gateway/constants.py @@ -251,7 +251,7 @@ def random_mcp_server_name() -> str: }, { "uri": EXT_CLOCK_URI, - "description": "Virtual clock (urn:agentenv:clock/v1): read the gateway's virtual time (wall-clock-derived, rate-scaled) via get_time; set_time/set_rate/advance/clear are the harness control path, each changing the clock's opaque version and, given wait_followers_s, answering 504 naming the followers that don't show it in time (set_rate keeps t0, as advance does, so time triggers keep their marks; rate 0 freezes the clock); declare_follower adds a follower after the gateway started (an agent the run deploys), with the read token its watches carry; a follower follows it through watch, a long poll naming the version it shows and held up to hold_s; state's followers block is diagnostic, not part of the contract.", + "description": "Virtual clock (urn:agentenv:clock/v1): read the gateway's virtual time (wall-clock-derived, rate-scaled) via get_time; set_time/set_rate/advance/clear are the harness control path, each changing the clock's opaque version and, given wait_followers_s, answering 504 naming the followers that don't show it in time (set_rate keeps t0, as advance does, so time triggers keep their marks; rate 0 freezes the clock); declare_follower adds a follower after the gateway started (an agent the run deploys), with the read token its watches carry, and retire_follower removes one so writes stop waiting for it (an agent the run tears down); a follower follows it through watch, a long poll naming the version it shows and held up to hold_s; state's followers block is diagnostic, not part of the contract.", "params": {"endpoint": "/clock/time", "methods": { "get_time": {"method": "GET", "endpoint": "/clock/time"}, "set_time": {"method": "PUT", "endpoint": "/clock/set-time", "request": {"type": "object", "properties": {"virtual_time": {"type": "string"}, "virtual_seconds_per_real_second": {"type": "number"}, "time_zone": {"type": "string"}, **_WAIT_FOLLOWERS}, "required": ["virtual_time"]}}, @@ -260,6 +260,7 @@ def random_mcp_server_name() -> str: "clear": {"method": "POST", "endpoint": "/clock/clear", "request": {"type": "object", "properties": {**_WAIT_FOLLOWERS}}}, "state": {"method": "GET", "endpoint": "/clock/state", "request": {"type": "object", "properties": {}}}, "declare_follower": {"method": "PUT", "endpoint": CLOCK_FOLLOWERS_PATH, "request": {"type": "object", "properties": {"name": {"type": "string"}, "read_token": {"type": "string"}}, "required": ["name", "read_token"]}}, + "retire_follower": {"method": "DELETE", "endpoint": CLOCK_FOLLOWERS_PATH, "request": {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}}, "watch": {"method": "GET", "endpoint": CLOCK_WATCH_PATH, "hold_s": CLOCK_WATCH_HOLD_S, "request": {"type": "object", "properties": {"after": {"type": "string"}, "follower": {"type": "string"}}}}}}, "required": False, }, diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index 2101400ec..be79da8d9 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -401,6 +401,17 @@ async def clock_declare_follower(request: Request) -> Response: return self._json_response({"ok": False, "error": str(e)}, 400) return self._json_response({"ok": True, "followers": self._clock_followers.state()}, 200) + @self._mcp.custom_route(CLOCK_FOLLOWERS_PATH, methods=["DELETE"]) + async def clock_retire_follower(request: Request) -> Response: + try: + body = await request.json() + except Exception: + return self._json_response({"ok": False, "error": "Invalid JSON body"}, 400) + if not isinstance(body, dict) or not isinstance(body.get("name"), str) or not body["name"]: + return self._json_response({"ok": False, "error": "body must be a JSON object naming the follower"}, 400) + retired = self._clock_followers.retire(body["name"]) + return self._json_response({"ok": True, "retired": retired, "followers": self._clock_followers.state()}, 200) + @self._mcp.custom_route("/clock/time", methods=["GET"]) async def clock_time(request: Request) -> Response: try: diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index 6c61f0c3b..adb80058e 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -4,7 +4,9 @@ its ``sync_time`` with the follower's config: the gateway's watch URL as the agent's sandbox reaches it, the name the gateway knows it by and that token. From then on a clock write that waits for followers (``wait_followers_s``) waits for the agent too. ``sync_env_clock`` has every agent registered with the env follow once it has armed the clock, and -``deploy_agent`` an agent it deploys while the clock is armed.""" +``deploy_agent`` an agent it deploys while the clock is armed. An agent has one follower, so it follows one env's +clock: the first a run has it follow; another env's is reported and skipped (``follows_another_env``). When +``teardown_sandboxes`` takes the agent down, ``retire_followers`` has each gateway it followed stop waiting for it.""" from __future__ import annotations import logging @@ -27,6 +29,14 @@ #: What an agent's ``sync_time`` must take to run a follower of its own. FOLLOWER_FIELDS = ("watch_url", "name", "read_token") +#: Bytes of randomness in an agent follower's read token. +READ_TOKEN_BYTES = 32 +#: How much of a failing agent's answer an error quotes. +ERROR_TEXT_CHARS = 600 +#: How long ``deploy_agent`` gives a late agent's declaration and ``sync_time``, which waits for its follower to connect. +LATE_FOLLOW_TIMEOUT_S = 60 +#: How long a teardown gives each gateway to retire an agent's follower. +RETIRE_TIMEOUT_S = 10 def follower_name(agent: DeployedAgent) -> str: @@ -67,7 +77,7 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op if timezone is not None and "timezone" not in fields: raise StepContextError(f"agent '{agent.agent_name}' serves {EXT_CLOCK_URI} sync_time without a timezone; " f"rebuild its image or drop timezone={timezone!r} from the task") - name, token = follower_name(agent), secrets.token_urlsafe(32) + name, token = follower_name(agent), secrets.token_urlsafe(READ_TOKEN_BYTES) try: await env.invoke(EXT_CLOCK_URI, "declare_follower", {"name": name, "read_token": token}, timeout=timeout) except EnvCapabilityUnsupported: @@ -79,20 +89,42 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op answer = await client.post(url, json=body, timeout=timeout, headers=sandbox_request_headers_for_url(url)) if answer.status_code >= 400: raise RuntimeError(f"agent '{agent.agent_name}' doesn't follow the clock of env '{env.env_id}' " - f"(HTTP {answer.status_code}): {answer.text[:600]}") + f"(HTTP {answer.status_code}): {answer.text[:ERROR_TEXT_CHARS]}") return {"synced": True, "follower": name, "result": answer.json()} +def followed_env(context: TaskStepContext, agent: DeployedAgent) -> Optional[str]: + """The env whose clock ``agent`` follows in this run, if any.""" + return next((c["env_id"] for c in context.metadata.get("clock_configurations") or [] + for a in c.get("agents") or () if a.get("agent") == agent.agent_name and a.get("synced")), None) + + +async def follow_in(context: TaskStepContext, agent: DeployedAgent, env: DeployedEnv, *, + env_get_time_url: Optional[str], timezone: Optional[str], timeout: float) -> dict: + """``follow``, unless ``agent`` already follows another env's clock in this run, whose follower a second one + would take over while that gateway still waits for it: reported and skipped (``follows_another_env``).""" + followed = followed_env(context, agent) + if followed is not None and followed != env.env_id: + return {"synced": False, "reason": "follows_another_env", "follows": followed} + return await follow(agent, env, env_get_time_url=env_get_time_url, timezone=timezone, timeout=timeout) + + +def summary(outcome: dict) -> dict: + """What a log says of an outcome: never the agent's whole answer.""" + return {key: outcome[key] for key in ("synced", "reason", "follower", "follows") if key in outcome} + + def _armed(context: TaskStepContext, env_id: str) -> Optional[dict]: """The configuration of the last ``sync_env_clock`` that armed ``env_id``'s clock in this run, if any.""" return next((c for c in reversed(context.metadata.get("clock_configurations") or []) if c.get("env_id") == env_id and "env_get_time_url" in c), None) -async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, timeout: float) -> list[dict]: +async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, + timeout: float = LATE_FOLLOW_TIMEOUT_S) -> list[dict]: """Have an agent ``deploy_agent`` just deployed follow the clock of each env registered with it that this run has armed, as ``sync_env_clock`` has every agent then: one that can't is reported and skipped, or fails the step under - that step's ``require_agent_clock``.""" + that step's ``require_agent_clock``. Each outcome is recorded with that step's, under its ``agents``.""" outcomes = [] for env_id in agent.env_ids: if (armed := _armed(context, env_id)) is None: @@ -100,11 +132,33 @@ async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, env = next((d for d in context.deployed_envs if d.env_id == env_id), None) if env is None or gateway_url_of(env) is None: continue - outcome = await follow(agent, env, env_get_time_url=armed.get("env_get_time_url"), - timezone=armed.get("timezone"), timeout=timeout) + outcome = await follow_in(context, agent, env, env_get_time_url=armed.get("env_get_time_url"), + timezone=armed.get("timezone"), timeout=timeout) if not outcome["synced"] and armed.get("require_agent_clock", False): raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env '{env_id}': " f"{outcome['reason']}") - logger.info(f"agent '{agent.agent_name}' on env {env_id}'s clock: {outcome}") + logger.info(f"agent '{agent.agent_name}' on env {env_id}'s clock: {summary(outcome)}") + armed.setdefault("agents", []).append({"agent": agent.agent_name, **outcome}) outcomes.append({"env_id": env_id, "agent": agent.agent_name, **outcome}) return outcomes + + +async def retire_followers(context: TaskStepContext, agent_names, *, timeout: float = RETIRE_TIMEOUT_S) -> list[str]: + """Have each gateway whose clock one of ``agent_names`` follows in this run stop waiting for it, as its agent is + torn down, so later writes that wait for followers don't wait for it or answer 504 naming it. Best-effort, as a + teardown is: a gateway that's gone or takes no retirement is logged. The followers retired.""" + names, retired = set(agent_names), [] + for configuration in context.metadata.get("clock_configurations") or []: + env = next((d for d in context.deployed_envs if d.env_id == configuration.get("env_id")), None) + for outcome in configuration.get("agents") or (): + if env is None or outcome.get("agent") not in names or not outcome.get("synced"): + continue + try: + await env.invoke(EXT_CLOCK_URI, "retire_follower", {"name": outcome["follower"]}, timeout=timeout) + except Exception as e: # noqa: BLE001 - best-effort, as the rest of a teardown + logger.warning(f"env {env.env_id}'s gateway didn't retire follower {outcome['follower']!r}: " + f"{type(e).__name__}: {e}"[:300]) + continue + outcome["synced"], outcome["retired"] = False, True + retired.append(outcome["follower"]) + return retired diff --git a/src/agent_env/task_step/task_steps/deploy_agent.py b/src/agent_env/task_step/task_steps/deploy_agent.py index 443b97447..c09520ec6 100644 --- a/src/agent_env/task_step/task_steps/deploy_agent.py +++ b/src/agent_env/task_step/task_steps/deploy_agent.py @@ -548,7 +548,7 @@ async def _execute(self, context: TaskStepContext, deployed_agents: list) -> Tas )) # An agent deployed while a clock is armed follows it, as sync_env_clock had every agent then. from agent_env.task_step.task_steps.agent_clock import follow_armed_clocks - await follow_armed_clocks(context, context.deployed_agents[-1], timeout=60) + await follow_armed_clocks(context, context.deployed_agents[-1]) return context def _model_endpoint(self, context: TaskStepContext) -> dict[str, str]: diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index f9b783972..ceed5562f 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -193,14 +193,14 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: """Have every agent registered with the env follow its clock (``agent_clock.follow``): one that can't is reported and skipped, or fails the step under ``require_agent_clock``.""" - from agent_env.task_step.task_steps.agent_clock import follow + from agent_env.task_step.task_steps.agent_clock import follow_in outcomes = [] for agent in context.deployed_agents: if self.env_id not in (agent.env_ids or []): continue - outcome = await follow(agent, deployed, env_get_time_url=env_get_time_url, timezone=self.timezone, - timeout=self.timeout_seconds) + outcome = await follow_in(context, agent, deployed, env_get_time_url=env_get_time_url, + timezone=self.timezone, timeout=self.timeout_seconds) if not outcome["synced"]: if self.require_agent_clock: raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env " diff --git a/src/agent_env/task_step/task_steps/teardown_sandboxes.py b/src/agent_env/task_step/task_steps/teardown_sandboxes.py index 76af0ff98..705df8ed9 100644 --- a/src/agent_env/task_step/task_steps/teardown_sandboxes.py +++ b/src/agent_env/task_step/task_steps/teardown_sandboxes.py @@ -27,6 +27,7 @@ tear_down, ) from agent_env.task_step.context import TaskStepContext +from agent_env.task_step.task_steps.agent_clock import retire_followers from agent_env.task_step.task_step import TaskStep, TaskStepDependency logger = logging.getLogger(__name__) @@ -85,6 +86,8 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: logger.warning(f"teardown_sandboxes: not deployed in this run, skipping: {', '.join(missing)}") already = set(context.metadata.get(TORN_DOWN_KEY) or []) await before_teardown(context, LAST_DRAIN_SECONDS, agent_names=self.agent_names) + # Before the agents go: a gateway still running keeps waiting for a follower until it's told it's gone. + await retire_followers(context, self.agent_names) report = await tear_down(context, sandboxes, envs, timeout=TERMINATE_TIMEOUT_SECONDS) for sandbox, why in report.failed: logger.warning(f"teardown_sandboxes: {sandbox.sandbox_id}: {why}"[:300]) diff --git a/tst/unit/env/gateway/clock_watch_test.py b/tst/unit/env/gateway/clock_watch_test.py index 3a848cd08..8cc4db126 100644 --- a/tst/unit/env/gateway/clock_watch_test.py +++ b/tst/unit/env/gateway/clock_watch_test.py @@ -79,13 +79,15 @@ def test_the_card_offers_every_clock_method_with_the_waits_bound_and_the_watchs_ assert {name: (m["method"], m["endpoint"]) for name, m in methods.items()} == { "get_time": ("GET", "/clock/time"), "set_time": ("PUT", "/clock/set-time"), "set_rate": ("PUT", "/clock/set-rate"), "advance": ("PUT", "/clock/advance"), "clear": ("POST", "/clock/clear"), "state": ("GET", "/clock/state"), - "declare_follower": ("PUT", "/clock/followers"), "watch": ("GET", "/clock/watch")} + "declare_follower": ("PUT", "/clock/followers"), "retire_follower": ("DELETE", "/clock/followers"), + "watch": ("GET", "/clock/watch")} assert all(methods[name]["request"]["properties"]["wait_followers_s"] == wait for name in ("set_time", "set_rate", "advance", "clear")) assert methods["set_rate"]["request"]["required"] == ["virtual_seconds_per_real_second"] assert methods["set_time"]["request"]["required"] == ["virtual_time"] assert set(methods["watch"]["request"]["properties"]) == {"after", "follower"} assert methods["declare_follower"]["request"]["required"] == ["name", "read_token"] + assert methods["retire_follower"]["request"]["required"] == ["name"] assert methods["watch"]["hold_s"] == CLOCK_WATCH_HOLD_S == ClockFollowers.WATCH_S == 25.0 @@ -481,10 +483,38 @@ async def test_declaring_a_follower_takes_a_name_and_a_read_token(gateway, body) @pytest.mark.asyncio -async def test_only_the_envs_control_token_declares_a_follower(gateway): +async def test_only_the_envs_control_token_declares_or_retires_a_follower(gateway): gw = gateway(followers={}, control_token="control") + control = {"Authorization": "Bearer control"} async with _client(gw) as client: refused = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "t"}) - taken = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "t"}, - headers={"Authorization": "Bearer control"}) - assert (refused.status_code, taken.status_code) == (401, 200) + taken = await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "t"}, headers=control) + kept = await client.request("DELETE", "/clock/followers", json={"name": "agent-solver"}) + retired = await client.request("DELETE", "/clock/followers", json={"name": "agent-solver"}, headers=control) + assert (refused.status_code, taken.status_code, kept.status_code, retired.status_code) == (401, 200, 401, 200) + + +@pytest.mark.asyncio +async def test_a_retired_agent_is_no_longer_waited_for_or_named_and_its_token_acknowledges_nothing(gateway): + gw = gateway(followers={}) + async with _client(gw) as client: + await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "token-agent"}) + retired = await client.request("DELETE", "/clock/followers", json={"name": "agent-solver"}) + started = time.monotonic() + written = await client.put("/clock/set-time", json={"virtual_time": T0, "wait_followers_s": 5}) + waited = time.monotonic() - started + assert gw._clock_followers.follower_for("agent-solver", "token-agent") is None + assert retired.status_code == 200 and retired.json() == {"ok": True, "retired": True, "followers": {}} + assert written.status_code == 200 and waited < 1, (written.text, waited) + + +@pytest.mark.asyncio +async def test_a_follower_the_gateway_started_with_is_neither_retired_nor_redeclared(gateway): + gw = gateway(followers={"laptop": "token-laptop"}) + async with _client(gw) as client: + retired = await client.request("DELETE", "/clock/followers", json={"name": "laptop"}) + redeclared = await client.put("/clock/followers", json={"name": "laptop", "read_token": "mine"}) + unknown = await client.request("DELETE", "/clock/followers", json={"name": "agent-gone"}) + assert retired.json()["retired"] is False and unknown.json()["retired"] is False + assert redeclared.status_code == 400 and "started with" in redeclared.text + assert gw._clock_followers.follower_for("laptop", "token-laptop") == "laptop" diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 340de33d3..3cd75b03a 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -165,3 +165,64 @@ def test_require_agent_clock_round_trips_and_is_left_out_when_off(): strict = _step(require_agent_clock=True).to_dict() assert strict["require_agent_clock"] is True assert SyncEnvClockTaskStep.from_dict(strict).require_agent_clock is True + + +@pytest.mark.asyncio +async def test_an_agent_follows_one_envs_clock_and_another_envs_is_reported_and_skipped(monkeypatch): + sent = _mock_http(monkeypatch) + context = await _context() + context.metadata["clock_configurations"] = [ + {"env_id": "e0", "env_get_time_url": CLOCK_URL, "agents": [{"agent": "late", "synced": True, + "follower": "agent-late"}]}, + {"env_id": "e1", "env_get_time_url": CLOCK_URL, "require_agent_clock": False}, + ] + + [outcome] = await agent_clock.follow_armed_clocks(context, _agent("late", env_ids=("e0", "e1")), timeout=5) + + assert (outcome["env_id"], outcome["synced"], outcome["reason"], outcome["follows"]) == ( + "e1", False, "follows_another_env", "e0") + assert sent == [] # e0 isn't deployed here; e1's gateway is never asked to take the agent + assert context.metadata["clock_configurations"][1]["agents"][0]["reason"] == "follows_another_env" + + +@pytest.mark.asyncio +async def test_a_teardown_retires_the_agents_follower_at_each_gateway_it_followed(monkeypatch): + from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep + + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent(), _agent("bystander")) + await _step().execute(context) + del sent[:] + + async def nothing(*a, **k): + return SimpleNamespace(terminated=[], failed=[]) + + monkeypatch.setattr("agent_env.task_step.task_steps.teardown_sandboxes.before_teardown", nothing) + monkeypatch.setattr("agent_env.task_step.task_steps.teardown_sandboxes.tear_down", nothing) + await TeardownSandboxesTaskStep(id="down", version=1, agent_names=["solver"]).execute(context) + + assert [(r.method, str(r.url), json.loads(r.content)) for r in sent] == [ + ("DELETE", f"{_CARD}/clock/followers", {"name": "agent-solver"})] + agents = {a["agent"]: a for a in context.metadata["clock_configurations"][0]["agents"]} + assert (agents["solver"]["synced"], agents["solver"]["retired"]) == (False, True) + assert agents["bystander"]["synced"] is True and agent_clock.followed_env(context, _agent()) is None + + +@pytest.mark.asyncio +async def test_a_gateway_that_cant_retire_a_follower_doesnt_fail_the_teardown(monkeypatch): + _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent()) + await _step().execute(context) + methods = next(e for e in context.deployed_envs[0].environment_card["capabilities"]["extensions"] + if e["uri"] == EXT_CLOCK_URI)["params"]["methods"] + del methods["retire_follower"] + + assert await agent_clock.retire_followers(context, ["solver"]) == [] + assert context.metadata["clock_configurations"][0]["agents"][0]["synced"] is True + + +def test_a_log_of_an_outcome_leaves_out_the_agents_answer(): + assert agent_clock.summary({"synced": True, "follower": "agent-solver", "result": {"big": "x" * 10_000}}) == { + "synced": True, "follower": "agent-solver"} From 84381b8a57dc0796f1f88c924aca13d9d710310b Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:43:50 -0800 Subject: [PATCH 05/29] fix(agents): keep each agent's clock record on a key of its own; a retirement wakes waiting writes - Which env an agent follows moves from clock_configurations' items to metadata.agent_clock_following, one key per agent, so late deploy_agent steps that save at once never overwrite each other's record and teardown still finds it. - Retiring a follower notifies writes waiting for it, and a watch by a follower retired meanwhile acknowledges nothing. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/agent_env/env/gateway/clock_followers.py | 25 ++++---- src/agent_env/env/gateway/gateway.py | 2 +- .../task_step/task_steps/agent_clock.py | 58 ++++++++++++------- tst/unit/env/gateway/clock_watch_test.py | 25 ++++++++ tst/unit/task_step/agent_clock_test.py | 42 ++++++++++---- 5 files changed, 108 insertions(+), 44 deletions(-) diff --git a/src/agent_env/env/gateway/clock_followers.py b/src/agent_env/env/gateway/clock_followers.py index 55404c34c..190c58b3c 100644 --- a/src/agent_env/env/gateway/clock_followers.py +++ b/src/agent_env/env/gateway/clock_followers.py @@ -58,15 +58,17 @@ def declare(self, name: str, token: str) -> None: self._seen.pop(name, None) self._declared.add(name) - def retire(self, name: str) -> bool: + async def retire(self, name: str) -> bool: """Stop waiting for ``name``, a follower ``declare`` added, whose agent a run has torn down: its token no - longer acknowledges, and no write waits for it or names it. Whether it was one; a follower the gateway started - with stays.""" + longer acknowledges, no write waits for it or names it, and a write waiting for it now answers. Whether it was + one; a follower the gateway started with stays.""" if name not in self._declared: return False - self._declared.discard(name) - for followers in (self._tokens, self._shown, self._seen): - followers.pop(name, None) + async with self._acks: + self._declared.discard(name) + for followers in (self._tokens, self._shown, self._seen): + followers.pop(name, None) + self._acks.notify_all() return True def changed(self) -> None: @@ -96,11 +98,12 @@ async def watch(self, after: str, follower: str | None) -> None: if follower is not None: count = self._clock().count_of(after) async with self._acks: - shown = self._shown[follower] - if count is not None and (shown is None or count > shown): - self._shown[follower] = count - self._seen[follower] = time.monotonic() - self._acks.notify_all() + if follower in self._shown: # else retired while its acknowledgement waited for the lock + shown = self._shown[follower] + if count is not None and (shown is None or count > shown): + self._shown[follower] = count + self._seen[follower] = time.monotonic() + self._acks.notify_all() if after == self._clock().version: try: await asyncio.wait_for(changed.wait(), self.WATCH_S) diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index be79da8d9..253d1bede 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -409,7 +409,7 @@ async def clock_retire_follower(request: Request) -> Response: return self._json_response({"ok": False, "error": "Invalid JSON body"}, 400) if not isinstance(body, dict) or not isinstance(body.get("name"), str) or not body["name"]: return self._json_response({"ok": False, "error": "body must be a JSON object naming the follower"}, 400) - retired = self._clock_followers.retire(body["name"]) + retired = await self._clock_followers.retire(body["name"]) return self._json_response({"ok": True, "retired": retired, "followers": self._clock_followers.state()}, 200) @self._mcp.custom_route("/clock/time", methods=["GET"]) diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index adb80058e..06bd26905 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -6,7 +6,9 @@ for the agent too. ``sync_env_clock`` has every agent registered with the env follow once it has armed the clock, and ``deploy_agent`` an agent it deploys while the clock is armed. An agent has one follower, so it follows one env's clock: the first a run has it follow; another env's is reported and skipped (``follows_another_env``). When -``teardown_sandboxes`` takes the agent down, ``retire_followers`` has each gateway it followed stop waiting for it.""" +``teardown_sandboxes`` takes the agent down, ``retire_followers`` has the gateway it follows stop waiting for it. Which +env each agent follows is kept under ``metadata[FOLLOWING_KEY]``, one key per agent, so steps that run at once and +each save the context never overwrite another's.""" from __future__ import annotations import logging @@ -37,6 +39,8 @@ LATE_FOLLOW_TIMEOUT_S = 60 #: How long a teardown gives each gateway to retire an agent's follower. RETIRE_TIMEOUT_S = 10 +#: ``metadata`` key of the env each agent follows: ``{slot: {"agent", "env_id", "follower"}}``, one slot per agent. +FOLLOWING_KEY = "agent_clock_following" def follower_name(agent: DeployedAgent) -> str: @@ -93,10 +97,15 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op return {"synced": True, "follower": name, "result": answer.json()} +def _slot(agent_name: str) -> str: + """``agent_name`` as a key of its own when the context is saved, where a ``.`` or a leading ``$`` would make one + save the whole map.""" + return agent_name.replace(".", "_").replace("$", "_") + + def followed_env(context: TaskStepContext, agent: DeployedAgent) -> Optional[str]: """The env whose clock ``agent`` follows in this run, if any.""" - return next((c["env_id"] for c in context.metadata.get("clock_configurations") or [] - for a in c.get("agents") or () if a.get("agent") == agent.agent_name and a.get("synced")), None) + return ((context.metadata.get(FOLLOWING_KEY) or {}).get(_slot(agent.agent_name)) or {}).get("env_id") async def follow_in(context: TaskStepContext, agent: DeployedAgent, env: DeployedEnv, *, @@ -106,7 +115,11 @@ async def follow_in(context: TaskStepContext, agent: DeployedAgent, env: Deploye followed = followed_env(context, agent) if followed is not None and followed != env.env_id: return {"synced": False, "reason": "follows_another_env", "follows": followed} - return await follow(agent, env, env_get_time_url=env_get_time_url, timezone=timezone, timeout=timeout) + outcome = await follow(agent, env, env_get_time_url=env_get_time_url, timezone=timezone, timeout=timeout) + if outcome["synced"]: + context.metadata.setdefault(FOLLOWING_KEY, {})[_slot(agent.agent_name)] = { + "agent": agent.agent_name, "env_id": env.env_id, "follower": outcome["follower"]} + return outcome def summary(outcome: dict) -> dict: @@ -124,7 +137,7 @@ async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, timeout: float = LATE_FOLLOW_TIMEOUT_S) -> list[dict]: """Have an agent ``deploy_agent`` just deployed follow the clock of each env registered with it that this run has armed, as ``sync_env_clock`` has every agent then: one that can't is reported and skipped, or fails the step under - that step's ``require_agent_clock``. Each outcome is recorded with that step's, under its ``agents``.""" + that step's ``require_agent_clock``.""" outcomes = [] for env_id in agent.env_ids: if (armed := _armed(context, env_id)) is None: @@ -138,27 +151,28 @@ async def follow_armed_clocks(context: TaskStepContext, agent: DeployedAgent, *, raise StepContextError(f"agent '{agent.agent_name}' can't follow the clock of env '{env_id}': " f"{outcome['reason']}") logger.info(f"agent '{agent.agent_name}' on env {env_id}'s clock: {summary(outcome)}") - armed.setdefault("agents", []).append({"agent": agent.agent_name, **outcome}) outcomes.append({"env_id": env_id, "agent": agent.agent_name, **outcome}) return outcomes async def retire_followers(context: TaskStepContext, agent_names, *, timeout: float = RETIRE_TIMEOUT_S) -> list[str]: - """Have each gateway whose clock one of ``agent_names`` follows in this run stop waiting for it, as its agent is + """Have the gateway whose clock each of ``agent_names`` follows in this run stop waiting for it, as its agent is torn down, so later writes that wait for followers don't wait for it or answer 504 naming it. Best-effort, as a - teardown is: a gateway that's gone or takes no retirement is logged. The followers retired.""" - names, retired = set(agent_names), [] - for configuration in context.metadata.get("clock_configurations") or []: - env = next((d for d in context.deployed_envs if d.env_id == configuration.get("env_id")), None) - for outcome in configuration.get("agents") or (): - if env is None or outcome.get("agent") not in names or not outcome.get("synced"): - continue - try: - await env.invoke(EXT_CLOCK_URI, "retire_follower", {"name": outcome["follower"]}, timeout=timeout) - except Exception as e: # noqa: BLE001 - best-effort, as the rest of a teardown - logger.warning(f"env {env.env_id}'s gateway didn't retire follower {outcome['follower']!r}: " - f"{type(e).__name__}: {e}"[:300]) - continue - outcome["synced"], outcome["retired"] = False, True - retired.append(outcome["follower"]) + teardown is: a gateway that's gone or takes no retirement is logged and its agent left recorded. The followers + retired.""" + following, retired = context.metadata.get(FOLLOWING_KEY) or {}, [] + for name in agent_names: + if (record := following.get(_slot(name))) is None: + continue + env = next((d for d in context.deployed_envs if d.env_id == record["env_id"]), None) + if env is None: + continue + try: + await env.invoke(EXT_CLOCK_URI, "retire_follower", {"name": record["follower"]}, timeout=timeout) + except Exception as e: # noqa: BLE001 - best-effort, as the rest of a teardown + logger.warning(f"env {env.env_id}'s gateway didn't retire follower {record['follower']!r}: " + f"{type(e).__name__}: {e}"[:300]) + continue + del following[_slot(name)] + retired.append(record["follower"]) return retired diff --git a/tst/unit/env/gateway/clock_watch_test.py b/tst/unit/env/gateway/clock_watch_test.py index 8cc4db126..e18caa897 100644 --- a/tst/unit/env/gateway/clock_watch_test.py +++ b/tst/unit/env/gateway/clock_watch_test.py @@ -518,3 +518,28 @@ async def test_a_follower_the_gateway_started_with_is_neither_retired_nor_redecl assert retired.json()["retired"] is False and unknown.json()["retired"] is False assert redeclared.status_code == 400 and "started with" in redeclared.text assert gw._clock_followers.follower_for("laptop", "token-laptop") == "laptop" + + +@pytest.mark.asyncio +async def test_a_write_waiting_for_an_agent_answers_once_the_agent_is_retired(gateway): + gw = gateway(followers={}) + async with _client(gw) as client: + await client.put("/clock/followers", json={"name": "agent-solver", "read_token": "token-agent"}) + started = time.monotonic() + write = asyncio.create_task(client.put("/clock/set-time", json={"virtual_time": T0, "wait_followers_s": 20})) + await asyncio.sleep(0.3) + await client.request("DELETE", "/clock/followers", json={"name": "agent-solver"}) + written = await asyncio.wait_for(write, 5) + waited = time.monotonic() - started + assert written.status_code == 200 and waited < 3, (written.text, waited) + + +@pytest.mark.asyncio +async def test_a_watch_by_a_follower_retired_meanwhile_acknowledges_nothing(gateway): + gw = gateway(followers={}) + gw._clock_followers.declare("agent-solver", "token-agent") + named = gw._clock_followers.follower_for("agent-solver", "token-agent") + await gw._clock_followers.retire("agent-solver") + gw._clock_followers.WATCH_S = 0.05 + await gw._clock_followers.watch("", named) + assert gw._clock_followers.state() == {} diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 3cd75b03a..6628eec05 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -171,18 +171,16 @@ def test_require_agent_clock_round_trips_and_is_left_out_when_off(): async def test_an_agent_follows_one_envs_clock_and_another_envs_is_reported_and_skipped(monkeypatch): sent = _mock_http(monkeypatch) context = await _context() - context.metadata["clock_configurations"] = [ - {"env_id": "e0", "env_get_time_url": CLOCK_URL, "agents": [{"agent": "late", "synced": True, - "follower": "agent-late"}]}, - {"env_id": "e1", "env_get_time_url": CLOCK_URL, "require_agent_clock": False}, - ] + context.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL}] + context.metadata[agent_clock.FOLLOWING_KEY] = {"late": {"agent": "late", "env_id": "e0", "follower": "agent-late"}} [outcome] = await agent_clock.follow_armed_clocks(context, _agent("late", env_ids=("e0", "e1")), timeout=5) assert (outcome["env_id"], outcome["synced"], outcome["reason"], outcome["follows"]) == ( "e1", False, "follows_another_env", "e0") assert sent == [] # e0 isn't deployed here; e1's gateway is never asked to take the agent - assert context.metadata["clock_configurations"][1]["agents"][0]["reason"] == "follows_another_env" + assert context.metadata[agent_clock.FOLLOWING_KEY] == {"late": {"agent": "late", "env_id": "e0", + "follower": "agent-late"}} @pytest.mark.asyncio @@ -204,9 +202,9 @@ async def nothing(*a, **k): assert [(r.method, str(r.url), json.loads(r.content)) for r in sent] == [ ("DELETE", f"{_CARD}/clock/followers", {"name": "agent-solver"})] - agents = {a["agent"]: a for a in context.metadata["clock_configurations"][0]["agents"]} - assert (agents["solver"]["synced"], agents["solver"]["retired"]) == (False, True) - assert agents["bystander"]["synced"] is True and agent_clock.followed_env(context, _agent()) is None + assert context.metadata[agent_clock.FOLLOWING_KEY] == { + "bystander": {"agent": "bystander", "env_id": "e1", "follower": "agent-bystander"}} + assert agent_clock.followed_env(context, _agent()) is None @pytest.mark.asyncio @@ -220,9 +218,33 @@ async def test_a_gateway_that_cant_retire_a_follower_doesnt_fail_the_teardown(mo del methods["retire_follower"] assert await agent_clock.retire_followers(context, ["solver"]) == [] - assert context.metadata["clock_configurations"][0]["agents"][0]["synced"] is True + assert agent_clock.followed_env(context, _agent()) == "e1" def test_a_log_of_an_outcome_leaves_out_the_agents_answer(): assert agent_clock.summary({"synced": True, "follower": "agent-solver", "result": {"big": "x" * 10_000}}) == { "synced": True, "follower": "agent-solver"} + + +@pytest.mark.asyncio +async def test_late_agents_deployed_at_once_each_save_only_their_own_record(monkeypatch): + import copy + + from agent_env.task_step.context_ops import build_context_update_ops + + _mock_http(monkeypatch) + pre = await _context() + pre.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL}] + saved = {} + for name in ("first", "second"): # each from the same context, as two deploy_agent steps at once + post = copy.deepcopy(pre) + await agent_clock.follow_armed_clocks(post, _agent(name), timeout=5) + ops = build_context_update_ops(pre, post) + saved[name] = {*ops.sets, *ops.add_to_sets, *ops.unsets} + + for name, paths in saved.items(): + assert paths and all(p.startswith(f"context.metadata.{agent_clock.FOLLOWING_KEY}.{name}") for p in paths), paths + + +def test_an_agent_name_with_a_dot_keeps_a_key_of_its_own(): + assert agent_clock._slot("team.solver") == "team_solver" and agent_clock._slot("$x") == "_x" From 56dac45a196634a161c72e8f9e8abcb967aea18f Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:48:48 -0800 Subject: [PATCH 06/29] fix(agents): agent clock keys can't collide; one clock env per agent; retire however its sandbox goes - Agent names are percent-encoded into their metadata keys, so team.solver and team_solver never share a record. - An agent follows the clock of the first env it's registered with, fixed by the agent alone, so two steps arming two of its envs at once never both take its one follower. - teardown_sandboxes retires the follower of every agent whose sandbox it takes down, by agent, sandbox or env name. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../task_step/task_steps/agent_clock.py | 26 +++++---- .../task_steps/teardown_sandboxes.py | 6 +- tst/unit/task_step/agent_clock_test.py | 55 ++++++++++++++++--- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index 06bd26905..0e7f30c8e 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -5,8 +5,9 @@ gateway knows it by and that token. From then on a clock write that waits for followers (``wait_followers_s``) waits for the agent too. ``sync_env_clock`` has every agent registered with the env follow once it has armed the clock, and ``deploy_agent`` an agent it deploys while the clock is armed. An agent has one follower, so it follows one env's -clock: the first a run has it follow; another env's is reported and skipped (``follows_another_env``). When -``teardown_sandboxes`` takes the agent down, ``retire_followers`` has the gateway it follows stop waiting for it. Which +clock: the first env it's registered with (``clock_env``), whatever order steps arm clocks in, even at once; another +env's is reported and skipped (``follows_another_env``). When ``teardown_sandboxes`` takes the agent's sandbox down, +``retire_followers`` has the gateway it follows stop waiting for it. Which env each agent follows is kept under ``metadata[FOLLOWING_KEY]``, one key per agent, so steps that run at once and each save the context never overwrite another's.""" from __future__ import annotations @@ -99,8 +100,8 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op def _slot(agent_name: str) -> str: """``agent_name`` as a key of its own when the context is saved, where a ``.`` or a leading ``$`` would make one - save the whole map.""" - return agent_name.replace(".", "_").replace("$", "_") + save the whole map: percent-encoded, ``%`` first, so two names never share a key.""" + return agent_name.replace("%", "%25").replace(".", "%2E").replace("$", "%24") def followed_env(context: TaskStepContext, agent: DeployedAgent) -> Optional[str]: @@ -108,13 +109,18 @@ def followed_env(context: TaskStepContext, agent: DeployedAgent) -> Optional[str return ((context.metadata.get(FOLLOWING_KEY) or {}).get(_slot(agent.agent_name)) or {}).get("env_id") +def clock_env(agent: DeployedAgent) -> Optional[str]: + """The one env whose clock ``agent`` follows: the first it's registered with. Fixed by the agent alone, so two + steps arming two of its envs' clocks at once never both take its one follower.""" + return next(iter(agent.env_ids or ()), None) + + async def follow_in(context: TaskStepContext, agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Optional[str], timezone: Optional[str], timeout: float) -> dict: - """``follow``, unless ``agent`` already follows another env's clock in this run, whose follower a second one - would take over while that gateway still waits for it: reported and skipped (``follows_another_env``).""" - followed = followed_env(context, agent) - if followed is not None and followed != env.env_id: - return {"synced": False, "reason": "follows_another_env", "follows": followed} + """``follow`` when ``env`` is the agent's ``clock_env``, recording it; another env's clock, which would take over + its follower while that gateway still waits for it, is reported and skipped (``follows_another_env``).""" + if env.env_id != clock_env(agent): + return {"synced": False, "reason": "follows_another_env", "follows": clock_env(agent)} outcome = await follow(agent, env, env_get_time_url=env_get_time_url, timezone=timezone, timeout=timeout) if outcome["synced"]: context.metadata.setdefault(FOLLOWING_KEY, {})[_slot(agent.agent_name)] = { @@ -161,7 +167,7 @@ async def retire_followers(context: TaskStepContext, agent_names, *, timeout: fl teardown is: a gateway that's gone or takes no retirement is logged and its agent left recorded. The followers retired.""" following, retired = context.metadata.get(FOLLOWING_KEY) or {}, [] - for name in agent_names: + for name in dict.fromkeys(agent_names): if (record := following.get(_slot(name))) is None: continue env = next((d for d in context.deployed_envs if d.env_id == record["env_id"]), None) diff --git a/src/agent_env/task_step/task_steps/teardown_sandboxes.py b/src/agent_env/task_step/task_steps/teardown_sandboxes.py index 705df8ed9..4190c2497 100644 --- a/src/agent_env/task_step/task_steps/teardown_sandboxes.py +++ b/src/agent_env/task_step/task_steps/teardown_sandboxes.py @@ -86,8 +86,10 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: logger.warning(f"teardown_sandboxes: not deployed in this run, skipping: {', '.join(missing)}") already = set(context.metadata.get(TORN_DOWN_KEY) or []) await before_teardown(context, LAST_DRAIN_SECONDS, agent_names=self.agent_names) - # Before the agents go: a gateway still running keeps waiting for a follower until it's told it's gone. - await retire_followers(context, self.agent_names) + # Before the agents go, however named: a gateway still running waits for a follower until it's told it's gone. + doomed = {sandbox.sandbox_id for sandbox in sandboxes} + await retire_followers(context, [*self.agent_names, *(a.agent_name for a in context.deployed_agents + if a.sandbox_id in doomed)]) report = await tear_down(context, sandboxes, envs, timeout=TERMINATE_TIMEOUT_SECONDS) for sandbox, why in report.failed: logger.warning(f"teardown_sandboxes: {sandbox.sandbox_id}: {why}"[:300]) diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 6628eec05..4484097e6 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -168,19 +168,16 @@ def test_require_agent_clock_round_trips_and_is_left_out_when_off(): @pytest.mark.asyncio -async def test_an_agent_follows_one_envs_clock_and_another_envs_is_reported_and_skipped(monkeypatch): +async def test_an_agent_follows_the_first_envs_clock_and_another_envs_is_reported_and_skipped(monkeypatch): sent = _mock_http(monkeypatch) - context = await _context() + context = await _context() # e1 alone is deployed and armed: whether e0 has been armed yet doesn't matter context.metadata["clock_configurations"] = [{"env_id": "e1", "env_get_time_url": CLOCK_URL}] - context.metadata[agent_clock.FOLLOWING_KEY] = {"late": {"agent": "late", "env_id": "e0", "follower": "agent-late"}} [outcome] = await agent_clock.follow_armed_clocks(context, _agent("late", env_ids=("e0", "e1")), timeout=5) assert (outcome["env_id"], outcome["synced"], outcome["reason"], outcome["follows"]) == ( "e1", False, "follows_another_env", "e0") - assert sent == [] # e0 isn't deployed here; e1's gateway is never asked to take the agent - assert context.metadata[agent_clock.FOLLOWING_KEY] == {"late": {"agent": "late", "env_id": "e0", - "follower": "agent-late"}} + assert sent == [] and agent_clock.FOLLOWING_KEY not in context.metadata @pytest.mark.asyncio @@ -246,5 +243,47 @@ async def test_late_agents_deployed_at_once_each_save_only_their_own_record(monk assert paths and all(p.startswith(f"context.metadata.{agent_clock.FOLLOWING_KEY}.{name}") for p in paths), paths -def test_an_agent_name_with_a_dot_keeps_a_key_of_its_own(): - assert agent_clock._slot("team.solver") == "team_solver" and agent_clock._slot("$x") == "_x" +@pytest.mark.parametrize("names", [("team.solver", "team_solver", "team%2Esolver"), ("$x", "_x", "%24x")]) +def test_agent_names_that_look_alike_keep_keys_of_their_own(names): + slots = [agent_clock._slot(name) for name in names] + assert len(set(slots)) == len(names) and not any("." in s or s.startswith("$") for s in slots) + + +@pytest.mark.parametrize("by", ["sandbox_names", "env_ids"]) +@pytest.mark.asyncio +async def test_an_agent_whose_sandbox_goes_down_by_another_name_is_retired_too(monkeypatch, by): + from agent_env.task_step.context import DeployedSandbox + from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep + + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + agent = _agent() + agent.sandbox_id = "sb-1" if by == "env_ids" else "sb-shared" + context = await _context(agent) + context.deployed_sandboxes = [DeployedSandbox(sandbox_name="box", sandbox_id="sb-shared", sandbox_mode="container", + sandbox_type="local")] + await _step().execute(context) + del sent[:] + + async def nothing(*a, **k): + return SimpleNamespace(terminated=[], failed=[]) + + monkeypatch.setattr("agent_env.task_step.task_steps.teardown_sandboxes.before_teardown", nothing) + monkeypatch.setattr("agent_env.task_step.task_steps.teardown_sandboxes.tear_down", nothing) + named = {"sandbox_names": ["box"]} if by == "sandbox_names" else {"env_ids": ["e1"]} + await TeardownSandboxesTaskStep(id="down", version=1, **named).execute(context) + + assert [(r.method, json.loads(r.content)) for r in sent] == [("DELETE", {"name": "agent-solver"})] + + +@pytest.mark.asyncio +async def test_tearing_down_one_of_two_look_alike_agents_retires_its_own_follower_alone(monkeypatch): + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + context = await _context(_agent("team.solver"), _agent("team_solver")) + await _step().execute(context) + del sent[:] + + assert await agent_clock.retire_followers(context, ["team.solver"]) == ["agent-team.solver"] + assert [json.loads(r.content) for r in sent] == [{"name": "agent-team.solver"}] + assert agent_clock.followed_env(context, _agent("team_solver")) == "e1" From c72674302682a208708af06f6a8b007882ce2369 Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 16 Nov 2026 16:54:12 -0800 Subject: [PATCH 07/29] chore: drop test run output committed by mistake Co-Authored-By: Claude Opus 5.5 (1M context) --- .../.agentenv/browser-chromium.jsonl | 15 --------------- packages/agentenv-protocol/.agentenv/launches | 12 ------------ 2 files changed, 27 deletions(-) delete mode 100644 packages/agentenv-protocol/.agentenv/browser-chromium.jsonl delete mode 100644 packages/agentenv-protocol/.agentenv/launches diff --git a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl deleted file mode 100644 index ab4e9b8bf..000000000 --- a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl +++ /dev/null @@ -1,15 +0,0 @@ -{"seq": 1, "at": "2026-11-17T00:08:55.941Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 2, "at": "2026-11-17T00:08:55.978Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 3, "at": "2026-11-17T00:08:55.978Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 4, "at": "2026-11-17T00:09:25.887Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 5, "at": "2026-11-17T00:09:25.924Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 6, "at": "2026-11-17T00:09:25.924Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 7, "at": "2026-11-17T00:10:10.264Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 8, "at": "2026-11-17T00:10:10.264Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 9, "at": "2026-11-17T00:10:10.282Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 10, "at": "2026-11-17T00:12:10.519Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 11, "at": "2026-11-17T00:12:10.519Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 12, "at": "2026-11-17T00:12:10.536Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 13, "at": "2026-11-17T00:35:20.724Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 14, "at": "2026-11-17T00:35:20.724Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 15, "at": "2026-11-17T00:35:20.741Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} diff --git a/packages/agentenv-protocol/.agentenv/launches b/packages/agentenv-protocol/.agentenv/launches deleted file mode 100644 index 0687a6131..000000000 --- a/packages/agentenv-protocol/.agentenv/launches +++ /dev/null @@ -1,12 +0,0 @@ -{"at": "2026-11-17T00:10:13.131730+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:10:16.178539+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:10:16.361537+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:10:17.051016+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:12:13.386381+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:12:16.437758+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:12:16.619313+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:12:17.317866+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:35:23.501958+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:35:26.556895+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:35:26.739266+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T00:35:27.431578+00:00", "event": "launch", "pid": 4242, "cause": "launch"} From 3b6f499f74faf0d9f613489221d6d3175dca96d6 Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 09:09:37 -0800 Subject: [PATCH 08/29] test(agents): the Claude Code image follows the env's clock end to end on local Docker (FD-5916) Deploys the image with an env, runs sync_env_clock with require_agent_clock, and checks that the agent's user reads the env's time (date and datetime.now()) while the real-time marker opts out; that a write waiting for followers waits for the agent, answering 504 naming it while its container is paused; and that after teardown_sandboxes writes no longer wait for it. Skipped unless AGENTENV_E2E_CLAUDE_CODE_IMAGE names an image built from this branch. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../task_step/test_agent_env_clock_local.py | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tst/integration/task_step/test_agent_env_clock_local.py diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py new file mode 100644 index 000000000..41f7ae37e --- /dev/null +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -0,0 +1,134 @@ +"""The Claude Code image on a task env's clock (FD-5916), end to end on local Docker: the agent deployed with the env +serves clock/v1 sync_time; sync_env_clock declares it a follower at the real gateway and hands it its config; from then +on every program the agent's user runs reads the env's time while the A2A server stays on real time; a clock write that +waits for followers waits for it; and once teardown_sandboxes takes it down, writes stop waiting for it. + +Runs over an image tag built beforehand (AGENTENV_E2E_CLAUDE_CODE_IMAGE), as the shipped agent's image installs the +protocol from PyPI, which has no CLOCK_V1 until a release.""" + +import asyncio +import os +import subprocess +import time +import uuid +from datetime import datetime, timezone + +import pytest + +from agent_env.a2a_agent.a2a_agent import A2AAgent +from agent_env.artifact import DockerImageArtifact +from agent_env.env.gateway.constants import EXT_CLOCK_URI +from agentenv_protocol.client import ExtensionCallError +from agent_env.task_step.context import TaskStepContext +from agent_env.task_step.task_steps.agent_clock import FOLLOWING_KEY +from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep +from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep +from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep +from tst.integration.store.local_parallel_deploy_test import _seed_bootstrap_envs +from tst.integration.task_step.test_prompt_agent_live_trajectory_local import local_registry as local_registry +from tst.integration.task_step.test_register_env_triggers_local import _deploy_items + +pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] + +IMAGE = os.environ.get("AGENTENV_E2E_CLAUDE_CODE_IMAGE", "") +VIRTUAL = "2031-03-04T05:06:07Z" +LATER = "2031-03-05T10:00:00Z" +PAUSED = "2031-03-05T12:00:00Z" + + +def _in_agent(container: str, *command: str, real_time: bool = False) -> str: + env = ["-e", "AGENTENV_REAL_TIME=1"] if real_time else [] + return subprocess.run(["docker", "exec", "-u", "agent", *env, container, *command], capture_output=True, + text=True, check=True, timeout=30).stdout.strip() + + +def _container_of(sandbox_id: str) -> str: + names = subprocess.run(["docker", "ps", "--format", "{{.Names}}"], capture_output=True, text=True, + check=True).stdout.split() + [name] = [n for n in names if sandbox_id in n] + return name + + +async def _followers(deployed) -> dict: + return (await deployed.invoke(EXT_CLOCK_URI, "state", {}, timeout=10)).get("followers") or {} + + +@pytest.mark.skipif(not IMAGE, reason="set AGENTENV_E2E_CLAUDE_CODE_IMAGE to an image built from this branch") +@pytest.mark.asyncio +async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_down(local_registry): + _seed_bootstrap_envs() + env, deployed, _ = await _deploy_items("ccclock") + uid = uuid.uuid4().hex[:8] + image = DockerImageArtifact.put(id=f"cc-clock-img-{uid}", description="e2e", image_name=IMAGE) + agent = A2AAgent.put(id=f"cc-clock-{uid}", docker_image_artifact=image, + default_env_vars={"LITELLM_API_KEY": "unused", "LITELLM_BASE_URL": "http://unused.invalid"}) + context = TaskStepContext(deployed_envs=[deployed], deployed_agents=[], metadata={}) + try: + await DeployAgentTaskStep(id="deploy-solver", version=None, env_ids=[env.id], a2a_agent_id=agent.id, + a2a_agent_version=agent.version, agent_name="solver", + sandbox_type="local").execute(context) + container = _container_of(context.deployed_agents[-1].sandbox_id) + assert _in_agent(container, "date", "-u", "+%Y") == str(datetime.now(timezone.utc).year) # not armed: real + + await SyncEnvClockTaskStep(id="clock", version=None, env_id=env.id, virtual_time=VIRTUAL, + virtual_seconds_per_real_second=0, require_agent_clock=True, + timeout_seconds=60).execute(context) + + [outcome] = context.metadata["clock_configurations"][-1]["agents"] + print("sync outcome:", {k: v for k, v in outcome.items() if k != "result"}, outcome.get("result")) + assert (outcome["agent"], outcome["synced"], outcome["follower"]) == ("solver", True, "agent-solver") + assert context.metadata[FOLLOWING_KEY]["solver"]["env_id"] == env.id + assert "agent-solver" in await _followers(deployed) + + # Every program the agent's user runs reads the env's time, frozen at rate 0; the marker opts out. + shown = {"date": _in_agent(container, "date", "-u", "+%FT%TZ"), + "python": _in_agent(container, "python3", "-c", "import datetime; print(datetime.datetime.now(" + "datetime.timezone.utc).isoformat())"), + "real": _in_agent(container, "date", "-u", "+%FT%TZ", real_time=True)} + print("agent reads:", shown) + assert shown["date"] == VIRTUAL and shown["python"].startswith("2031-03-04T05:06:07") + assert shown["real"].startswith(str(datetime.now(timezone.utc).year)) + + # A write that waits for followers waits for the agent, which then shows it at once. + started = time.monotonic() + await deployed.invoke(EXT_CLOCK_URI, "advance", {"virtual_time": LATER, "wait_followers_s": 20}, timeout=30) + print(f"advance acknowledged by the agent in {time.monotonic() - started:.2f}s") + assert _in_agent(container, "date", "-u", "+%FT%TZ") == LATER + assert (await _followers(deployed))["agent-solver"]["version"] is not None + + # It is really waited for: frozen, the agent can't acknowledge, and the write answers 504 naming it; thawed, + # its follower catches up on its own. + subprocess.run(["docker", "pause", container], check=True, capture_output=True) + try: + started = time.monotonic() + with pytest.raises(ExtensionCallError) as frozen: + await deployed.invoke(EXT_CLOCK_URI, "advance", {"virtual_time": PAUSED, "wait_followers_s": 3}, + timeout=30) + print(f"advance while the agent was frozen: {time.monotonic() - started:.2f}s, " + f"{frozen.value.response.status_code} {frozen.value.response.text[:200]}") + finally: + subprocess.run(["docker", "unpause", container], check=True, capture_output=True) + assert frozen.value.response.status_code == 504 and frozen.value.response.json()["stale"] == ["agent-solver"] + deadline = time.monotonic() + 30 + while _in_agent(container, "date", "-u", "+%FT%TZ") != PAUSED: + assert time.monotonic() < deadline, "the agent's follower didn't catch up after it was thawed" + await asyncio.sleep(0.5) + + await TeardownSandboxesTaskStep(id="down", version=None, agent_names=["solver"]).execute(context) + assert "agent-solver" not in await _followers(deployed) + assert "solver" not in (context.metadata.get(FOLLOWING_KEY) or {}) + + # Torn down, the agent is no longer waited for: a write answers at once, not after 20 s or with a 504. + started = time.monotonic() + await deployed.invoke(EXT_CLOCK_URI, "advance", {"virtual_time": "2031-03-06T00:00:00Z", + "wait_followers_s": 20}, timeout=30) + waited = time.monotonic() - started + print(f"advance after teardown answered in {waited:.2f}s") + assert waited < 5 + finally: + await asyncio.gather(*(asyncio.to_thread(subprocess.run, ["docker", "rm", "-f", _container_of(a.sandbox_id)], + capture_output=True) + for a in context.deployed_agents if a.sandbox_id + and a.sandbox_id not in (context.metadata.get("torn_down_sandbox_ids") or [])), + return_exceptions=True) + await env.close() From cecc68695e5b3dda5c72be9d59d30ff7981ef0fb Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 09:54:28 -0800 Subject: [PATCH 09/29] fix(agents): a failed agent sync retires its follower; the task's zone survives a server restart; get_time keeps its sentence - follow retires the follower it declared when the agent's sync_time fails, by an HTTP error or a transport one, so the gateway isn't left waiting on every later write for a follower that never watches. - sync_time saves the task's zone in the follower's config, and the A2A server takes it back when it starts, so a server supervisord restarts mid-run keeps the zone as the follower keeps the clock. - get_time's description keeps "the clock on the machine you are running on is not synchronized": most agents won't follow the clock for a while yet. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../.agentenv/browser-chromium.jsonl | 3 ++ packages/agentenv-protocol/.agentenv/launches | 4 ++ .../src/agentenv_protocol/a2a_agent/clock.py | 31 ++++++++++++--- .../agentenv_protocol/a2a_agent/framework.py | 4 +- .../tests/test_a2a_agent_clock.py | 25 +++++++++++- src/agent_env/env/gateway/get_time.py | 5 ++- .../task_step/task_steps/agent_clock.py | 39 +++++++++++++------ tst/unit/task_step/agent_clock_test.py | 27 ++++++++++--- 8 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 packages/agentenv-protocol/.agentenv/browser-chromium.jsonl create mode 100644 packages/agentenv-protocol/.agentenv/launches diff --git a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl new file mode 100644 index 000000000..11767f027 --- /dev/null +++ b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl @@ -0,0 +1,3 @@ +{"seq": 1, "at": "2026-11-17T17:52:18.948Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} +{"seq": 2, "at": "2026-11-17T17:52:18.948Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} +{"seq": 3, "at": "2026-11-17T17:52:18.967Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} diff --git a/packages/agentenv-protocol/.agentenv/launches b/packages/agentenv-protocol/.agentenv/launches new file mode 100644 index 000000000..a7d341049 --- /dev/null +++ b/packages/agentenv-protocol/.agentenv/launches @@ -0,0 +1,4 @@ +{"at": "2026-11-17T17:52:21.848022+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T17:52:24.898589+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T17:52:25.081340+00:00", "event": "launch", "pid": 4242, "cause": "launch"} +{"at": "2026-11-17T17:52:25.777610+00:00", "event": "launch", "pid": 4242, "cause": "launch"} diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py index 13954b99f..d1d66aef6 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/clock.py @@ -7,7 +7,9 @@ computer's, whose config core writes through the sandbox's root channel, an agent's request carries the follower's config itself (``watch_url``, ``name``, ``read_token``), and this server, running as the agent's user, writes it: the model's own commands could rewrite it and fool their own view of the time, though never the env's clock, and the token -only reads. It answers once the follower shows the gateway's current version, or says why it doesn't.""" +only reads. It answers once the follower shows the gateway's current version, or says why it doesn't. The task's zone +is kept in the config beside what the follower reads, so a server restarted mid-run (``restore_zone``) brings the zone +back as the follower brings back the clock.""" from __future__ import annotations import json @@ -26,13 +28,15 @@ def follows() -> bool: return follower.follows() -def write_config(watch_url: str, name: str, read_token: str) -> int: - """The follower's config, written beside its place and renamed into it, so the follower, which reads it every - quarter second, never reads half of one; readable by this user and the follower's group alone. When it was +def write_config(watch_url: str, name: str, read_token: str, timezone: str | None = None) -> int: + """The follower's config, with the task's ``timezone`` for ``restore_zone``, written beside its place and renamed + into it, so the follower, which reads it every quarter second, never reads half of one; readable by this user and + the follower's group alone. When it was written (``st_mtime_ns``), which a status must postdate to be of this config; 0 when the config was this one already, which the follower goes on following without writing its status anew.""" path = Path(follower.CONFIG) - raw = json.dumps({"watch_url": watch_url, "name": name, "token": read_token}).encode() + raw = json.dumps({"watch_url": watch_url, "name": name, "token": read_token, + **({"timezone": timezone} if timezone else {})}).encode() try: if path.read_bytes() == raw: return 0 @@ -64,6 +68,21 @@ def set_zone(timezone: str) -> str: return zone +def restore_zone() -> str | None: + """The task's zone ``sync_time`` saved, set again for what this server starts: called when it starts, so one that + restarts mid-run keeps the zone, as the follower keeps the clock. None when there's none, or it isn't a zone.""" + try: + zone = json.loads(Path(follower.CONFIG).read_text()).get("timezone") + except (OSError, ValueError, AttributeError): + return None + if not isinstance(zone, str) or not zone: + return None + try: + return set_zone(zone) + except InvalidArgument: + return None + + async def sync_time(env_get_time_url: str, timezone: str | None = None, watch_url: str | None = None, name: str | None = None, read_token: str | None = None) -> dict: """``clock/v1``'s ``sync_time`` on an agent: the follower configured to follow the gateway at ``watch_url`` as @@ -78,6 +97,6 @@ async def sync_time(env_get_time_url: str, timezone: str | None = None, watch_ur + (", ".join(k for k, v in (("watch_url", watch_url), ("name", name), ("read_token", read_token)) if v) or "none of them")) zone = set_zone(timezone) if timezone is not None else os.environ.get("TZ") or "UTC" - written = write_config(watch_url, name, read_token) + written = write_config(watch_url, name, read_token, zone) shown = await follower.following("this agent's clock follower", follower.CONNECT_WAIT_S, url=watch_url, name=name, since_ns=written) return {"synced": True, "timezone": zone, "env_get_time_url": env_get_time_url, "follower": shown} diff --git a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py index 648c47872..6851ab3de 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/framework.py @@ -891,9 +891,11 @@ async def clock_sync_time(self, request: ClockSyncTimeRequest) -> dict[str, Any] def _with_clock(definition: _AgentDefinition) -> _AgentDefinition: """``definition`` serving clock/v1 where the agent's image runs the clock follower, so every agent in such an - image follows the env's clock with no code of its own; unchanged elsewhere, or where the agent declares it.""" + image follows the env's clock with no code of its own, and takes back the task's zone if it restarted mid-run; + unchanged elsewhere, or where the agent declares it.""" if not agent_clock.follows() or any(a.definition.uri == CLOCK_V1.uri for a in definition.extensions): return definition + agent_clock.restore_zone() # a server restarted mid-run takes back the task's zone return dataclasses.replace(definition, extensions=(*definition.extensions, ExtensionActivation(CLOCK_V1))) diff --git a/packages/agentenv-protocol/tests/test_a2a_agent_clock.py b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py index 186fe506b..b23e5192f 100644 --- a/packages/agentenv-protocol/tests/test_a2a_agent_clock.py +++ b/packages/agentenv-protocol/tests/test_a2a_agent_clock.py @@ -104,7 +104,7 @@ def test_sync_time_writes_the_followers_config_sets_the_zone_and_answers_once_it "armed": True}} config = image / "follower.json" assert json.loads(config.read_text()) == {"watch_url": "http://gw/clock/watch", "name": "agent-solver", - "token": "token"} + "token": "token", "timezone": "America/Chicago"} assert stat.S_IMODE(config.stat().st_mode) == 0o640 assert os.environ["TZ"] == "America/Chicago" and time.strftime("%Z") in {"CST", "CDT"} assert [p.name for p in image.iterdir() if p.name.startswith("follower.json.")] == [] @@ -156,3 +156,26 @@ def test_the_config_it_already_follows_resent_answers_from_its_status(image): answer = client.post("/ext/clock/sync-time", json=REQUEST) # the follower writes nothing anew assert answer.status_code == 200, answer.text + + +def test_a_server_restarted_mid_run_takes_back_the_tasks_zone(image, monkeypatch): + _follows_the_next_config(image, connected=True, url="http://gw/clock/watch", name="agent-solver", version="b:3") + with _app() as client: + assert client.post("/ext/clock/sync-time", json={**REQUEST, "timezone": "America/Chicago"}).status_code == 200 + monkeypatch.setenv("TZ", "UTC") # the restarted server's environment: the image's, without the task's zone + time.tzset() + + with _app() as client: # starting it again, as supervisord does after the server dies + assert client.get("/.well-known/agent.json").status_code == 200 + + assert os.environ["TZ"] == "America/Chicago" and time.strftime("%Z") in {"CST", "CDT"} + + +@pytest.mark.parametrize("config", ['{"watch_url": "u", "name": "n", "token": "t"}', '{"timezone": "Mars/Olympus"}', + "not json", None]) +def test_a_server_starting_without_a_saved_zone_keeps_its_own(image, monkeypatch, config): + if config is not None: + (image / "follower.json").write_text(config) + with _app() as client: + assert client.get("/.well-known/agent.json").status_code == 200 + assert os.environ["TZ"] == "UTC" diff --git a/src/agent_env/env/gateway/get_time.py b/src/agent_env/env/gateway/get_time.py index 9d4172223..24767249f 100644 --- a/src/agent_env/env/gateway/get_time.py +++ b/src/agent_env/env/gateway/get_time.py @@ -17,8 +17,9 @@ GET_TIME_TOOL_NAME = "get_time" GET_TIME_TOOL_DESCRIPTION = ( - "Returns the current date and time for this environment. Do not rely on your own assumption " - "about today's date. Call this tool and derive from its value every " + "Returns the current date and time for this environment. The clock on the machine you are " + "running on is not synchronized with this environment, so do not use it and do not rely on " + "your own assumption about today's date. Call this tool and derive from its value every " "absolute date, time or timestamp you pass to another tool, and every relative phrase you " 'resolve ("today", "this week", "overdue", "the last 30 days"). current_time is in UTC, as ' "are the dates and times the other tools here take, so derive those from current_time. The " diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index 0e7f30c8e..dbc9c3998 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -72,7 +72,8 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op """Have ``agent`` follow ``env``'s gateway clock. ``{"synced": False, "reason": ...}`` for an agent that can't: its card serves no ``sync_time`` (``not_advertised``) or one without the follower's fields (``no_follower_fields``), or the gateway takes no followers after it starts (``gateway_takes_no_followers``); raises - for one that serves it and fails, or can't take the task's zone, as for a server.""" + for one that serves it and fails, having retired the follower it declared, or can't take the task's zone, as for + a server.""" method, path = _sync_time(agent) if method is None: return {"synced": False, "reason": "not_advertised"} @@ -90,12 +91,30 @@ async def follow(agent: DeployedAgent, env: DeployedEnv, *, env_get_time_url: Op body = {"env_get_time_url": env_get_time_url or "", "watch_url": watch_url_for(agent, env), "name": name, "read_token": token, **({"timezone": timezone} if timezone is not None else {})} url = (agent.a2a_url or agent.api_url).rstrip("/") + path - async with httpx.AsyncClient() as client: - answer = await client.post(url, json=body, timeout=timeout, headers=sandbox_request_headers_for_url(url)) - if answer.status_code >= 400: - raise RuntimeError(f"agent '{agent.agent_name}' doesn't follow the clock of env '{env.env_id}' " - f"(HTTP {answer.status_code}): {answer.text[:ERROR_TEXT_CHARS]}") - return {"synced": True, "follower": name, "result": answer.json()} + try: + async with httpx.AsyncClient() as client: + answer = await client.post(url, json=body, timeout=timeout, headers=sandbox_request_headers_for_url(url)) + except httpx.HTTPError as e: + why = f"({type(e).__name__}): {e}" + else: + if answer.status_code < 400: + return {"synced": True, "follower": name, "result": answer.json()} + why = f"(HTTP {answer.status_code}): {answer.text}" + # Declared and not following: the gateway would wait for it on every later write, and nothing records it to retire. + await _retire(env, name, timeout=RETIRE_TIMEOUT_S) + raise RuntimeError(f"agent '{agent.agent_name}' doesn't follow the clock of env '{env.env_id}' " + f"{why[:ERROR_TEXT_CHARS]}") + + +async def _retire(env: DeployedEnv, follower: str, *, timeout: float) -> bool: + """Have ``env``'s gateway stop waiting for ``follower``; whether it did. Best-effort: a gateway that's gone or + takes no retirement is logged.""" + try: + await env.invoke(EXT_CLOCK_URI, "retire_follower", {"name": follower}, timeout=timeout) + except Exception as e: # noqa: BLE001 - best-effort: a teardown or a failed sync goes on either way + logger.warning(f"env {env.env_id}'s gateway didn't retire follower {follower!r}: {type(e).__name__}: {e}"[:300]) + return False + return True def _slot(agent_name: str) -> str: @@ -173,11 +192,7 @@ async def retire_followers(context: TaskStepContext, agent_names, *, timeout: fl env = next((d for d in context.deployed_envs if d.env_id == record["env_id"]), None) if env is None: continue - try: - await env.invoke(EXT_CLOCK_URI, "retire_follower", {"name": record["follower"]}, timeout=timeout) - except Exception as e: # noqa: BLE001 - best-effort, as the rest of a teardown - logger.warning(f"env {env.env_id}'s gateway didn't retire follower {record['follower']!r}: " - f"{type(e).__name__}: {e}"[:300]) + if not await _retire(env, record["follower"], timeout=timeout): continue del following[_slot(name)] retired.append(record["follower"]) diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 4484097e6..4f6bd7230 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -104,22 +104,37 @@ async def test_a_gateway_from_before_followers_after_start_is_reported_and_the_a @pytest.mark.asyncio -async def test_an_agent_that_serves_sync_time_and_fails_fails_the_step_saying_why(monkeypatch): +@pytest.mark.parametrize("fails, why", [ + ("404", "\\(HTTP 404\\): .*isn't following"), + ("500", "\\(HTTP 500\\): .*boom"), + ("unreachable", "\\(ConnectError\\): .*refused"), +]) +async def test_an_agent_that_serves_sync_time_and_fails_fails_the_step_and_its_follower_is_retired(monkeypatch, fails, + why): import httpx - real = httpx.AsyncClient + real, sent = httpx.AsyncClient, [] def handle(request: httpx.Request) -> httpx.Response: + sent.append((request.method, request.url.path)) if request.url.path == "/ext/clock/sync-time": - return httpx.Response(404, json={"detail": "target_unavailable: this agent's clock follower isn't following"}) + if fails == "unreachable": + raise httpx.ConnectError("connection refused", request=request) + return httpx.Response(int(fails), json={"detail": "target_unavailable: this agent's clock follower isn't " + "following" if fails == "404" else "boom"}) return httpx.Response(200, json={"env_get_time_url": CLOCK_URL} if request.url.path.endswith("/state") else {}) monkeypatch.setattr(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))) _no_servers(monkeypatch) + context = await _context(_agent()) - with pytest.raises(RuntimeError, match="agent 'solver' doesn't follow the clock of env 'e1' \\(HTTP 404\\): .*" - "isn't following"): - await _step().execute(await _context(_agent())) + with pytest.raises(RuntimeError, match=f"agent 'solver' doesn't follow the clock of env 'e1' {why}"): + await _step().execute(context) + + # Declared, then retired: the gateway isn't left waiting for a follower that never watches. + assert [(method, "/" + "/".join(path.split("/")[-2:])) for method, path in sent[-3:]] == [ + ("PUT", "/clock/followers"), ("POST", "/clock/sync-time"), ("DELETE", "/clock/followers")] + assert agent_clock.followed_env(context, _agent()) is None @pytest.mark.asyncio From ac0a39e210abaf6c7264b3dab377cf212cc40697 Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 10:14:57 -0800 Subject: [PATCH 10/29] fix(agents): libfaketime's init can't deadlock the CLI; the CLI goes through the proxy only on an armed clock - The agent's libfaketime takes Edgar's init guard: a clock call made while ftpl_really_init runs on that thread, as the CLI's allocator makes one when dlsym allocates, reads the kernel's clock instead of waiting on pthread_once, which deadlocked: claude --version never returned. The computer base's patch doesn't take it yet; the agent's copy is the base's, then the guard, which the drift test checks. - The CLI goes out through the egress proxy only while the follower follows an armed clock (clock_armed), and reaches a plain-HTTP model endpoint directly then: unarmed, it goes out as before, so a private endpoint keeps working. - egress/ is the base's again after main moved it. - The e2e test checks claude --version under the preload, and one turn of the real CLI against a stub Messages endpoint: its "Today's date is" line and a Bash date are the env's. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/agent_env/agents/README.md | 6 +- .../agents/claude-code/claude_bridge.py | 38 ++++++++---- .../claude-code/clock/libfaketime.patch | 38 ++++++++++++ .../agents/agents/claude-code/egress/main.go | 31 +++++++++- .../agents/claude-code/egress/main_test.go | 27 +++++++++ .../agents/agents/claude-code/egress/proxy.go | 6 +- .../task_step/test_agent_env_clock_local.py | 36 +++++++++++- .../claude_code/test_env_clock_image.py | 58 ++++++++++++++++--- 8 files changed, 213 insertions(+), 27 deletions(-) create mode 100644 src/agent_env/agents/agents/claude-code/egress/main_test.go diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index 60e1221bf..9e94269b0 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -43,9 +43,9 @@ The A2A server starts with the real-time marker, so it and its records stay on r as a follower with a read token of its own and calls that `sync_time` with the follower's config, which the server writes; `deploy_agent` does the same for an agent it deploys while the clock is armed. From then on the CLI, its own "Today's date is" line included, and every command the model runs read the env's time, and a clock write that waits for -followers waits for the agent. The CLI goes out through the egress proxy, which keeps a live site's TLS working at any -date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time and the proxy relays TLS -untouched. Started as a user other than root, the image runs the A2A server alone, on real time, as before. +followers waits for the agent. While the clock is armed the CLI goes out through the egress proxy, which keeps a live +site's TLS working at any date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time +and the CLI goes out directly, as before, so a model endpoint at a private address, which the proxy refuses, still works. Started as a user other than root, the image runs the A2A server alone, on real time, as before. `install_agent` installs it too, into a container a task already runs or onto a `vm` sandbox's Linux host, with `install.sh` from its folder: Claude Code's native build for the target's CPU and libc, uv, and the agent's requirements 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 aabc2ec9b..8e49d8d1e 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -174,18 +174,32 @@ def _model_headers(raw: str | None) -> dict[str, str]: return {str(name): str(value) for name, value in headers.items()} -#: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it. +#: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it +#: while the agent follows an armed clock. EGRESS_PROXY_ENV = "AGENTENV_EGRESS_PROXY" +#: The clock follower's status, which says whether the clock it follows is armed (agentenv_protocol.computer.clock). +CLOCK_STATUS = "/run/agentenv-clock/status.json" _DIRECT_HOSTS = ("localhost", "127.0.0.1", "::1", "host.docker.internal") -def egress_env(environ: Mapping[str, str], direct: Iterable[str] = ()) -> dict[str, str]: - """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916): they read the env's time, - so a client checking a live site's certificate against it would refuse the site once the clock is months off, and - the proxy, on real time, hands it a certificate that holds then. The env's MCP servers (``direct``) are reached - directly: the proxy refuses an address that isn't global, as a local or private gateway's is.""" +def clock_armed(status_path: str | None = None) -> bool: + """Whether this image's clock follower follows an armed clock, so the CLI started now reads the env's time.""" + try: + status = json.loads(Path(status_path or CLOCK_STATUS).read_text()) + except (OSError, ValueError): + return False + return isinstance(status, dict) and bool(status.get("connected")) and bool(status.get("armed")) + + +def egress_env(environ: Mapping[str, str], direct: Iterable[str] = (), *, armed: bool = False) -> dict[str, str]: + """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916) and the clock is ``armed``: + they read the env's time, so a client checking a live site's certificate against it would refuse the site once + the clock is months off, and the proxy, on real time, hands it a certificate that holds then. The env's MCP servers + (``direct``) are reached directly: the proxy refuses an address that isn't global, as a local or private gateway's + is. Unarmed, the CLI reads real time and goes out directly, as an image without the proxy does, so a model endpoint + or mirror at a private address keeps working.""" proxy = environ.get(EGRESS_PROXY_ENV) - if not proxy: + if not proxy or not armed: return {} hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, direct)])) return {**{name: proxy for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY") for name in (base, base.lower())}, @@ -194,7 +208,7 @@ def egress_env(environ: Mapping[str, str], direct: Iterable[str] = ()) -> dict[s def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: str | None = None, task_id: str | None = None, max_thinking_tokens: int | None = None, - direct_hosts: Iterable[str] = ()) -> dict[str, str]: + direct_hosts: Iterable[str] = (), clock_armed: bool = False) -> dict[str, str]: """The CLI's environment: this process's, pointed at the endpoint agent-env configured (its key as a bearer token, so an API key too would conflict), with the endpoint's headers and the run's attribution, and the model's aliases pinned to ``model`` on that endpoint. A variable already set is kept, LITELLM_* too, so a script the model @@ -223,7 +237,11 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" - env.update(egress_env(environ, direct_hosts)) + # A model endpoint over plain HTTP has no certificate to check at the env's date, so it's reached directly, as one + # at a private address, which the proxy refuses, must be. + endpoint = urlsplit(env.get("ANTHROPIC_BASE_URL") or "") + model_host = [endpoint.hostname or ""] if endpoint.scheme == "http" else [] + env.update(egress_env(environ, [*direct_hosts, *model_host], armed=clock_armed)) return env @@ -703,7 +721,7 @@ async def _spawn_claude( stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=cli_env(os.environ, model=model, project_id=project_id, task_id=task_id, - max_thinking_tokens=max_thinking_tokens, + max_thinking_tokens=max_thinking_tokens, clock_armed=clock_armed(), direct_hosts=[urlsplit(str(entry.get("url") or "")).hostname or "" for entry in (mcp_servers or {}).values()]), cwd=WORKSPACE_DIR, diff --git a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch index d26148eed..17555d2b8 100644 --- a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch +++ b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch @@ -1525,3 +1525,41 @@ diff -ruN a/src/libfaketime.map b/src/libfaketime.map + pthread_cond_timedwait; } GLIBC_2.2.5; +diff -ruN a/src/libfaketime.c b/src/libfaketime.c +--- a/src/libfaketime.c ++++ b/src/libfaketime.c +@@ -329,6 +329,9 @@ + check_missing_real(#name, (NULL == real_##name)) + + static pthread_once_t initialized_once_control = PTHREAD_ONCE_INIT; ++/* Set on the thread running ftpl_really_init: a time call it makes, as an allocator that reads the clock does when ++ * dlsym allocates, takes the kernel's clock instead of waiting on the init it is part of. */ ++static __thread int ftpl_initializing = 0; + + /* prototypes */ + static int fake_gettimeofday(struct timeval *tv); +@@ -2326,6 +2329,7 @@ + int result; + + ftpl_init(); ++ if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp); + // If ftpl_init ends up recursing, pthread_once will deadlock. + // (Previously we attempted to detect this situation, and bomb out, + // but the approach taken wasn't thread-safe and broke in practice.) +@@ -3103,8 +3107,15 @@ + dont_fake = dont_fake_final; + } + ++static void ftpl_really_init_guarded(void) { ++ ftpl_initializing = 1; ++ ftpl_really_init(); ++ ftpl_initializing = 0; ++} ++ + inline static void ftpl_init(void) { +- pthread_once(&initialized_once_control, ftpl_really_init); ++ if (ftpl_initializing) return; ++ pthread_once(&initialized_once_control, ftpl_really_init_guarded); + } + + void *ft_dlvsym(void *handle, const char *symbol, const char *version, diff --git a/src/agent_env/agents/agents/claude-code/egress/main.go b/src/agent_env/agents/agents/claude-code/egress/main.go index 0bd5ae5c5..dd9760e99 100644 --- a/src/agent_env/agents/agents/claude-code/egress/main.go +++ b/src/agent_env/agents/agents/claude-code/egress/main.go @@ -1,8 +1,9 @@ // egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. // // It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and -// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128. Session programs reach it through HTTP(S)_PROXY and -// ALL_PROXY; nothing redirects or filters, so it needs no NET_ADMIN and works on any provider. +// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128, or where AGENTENV_EGRESS_PROXY names. Session programs +// reach it through HTTP(S)_PROXY and ALL_PROXY, or a proxy of the base's own that hands it their connections; nothing +// redirects or filters, so it needs no NET_ADMIN and works on any provider. // // A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe // a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is @@ -17,12 +18,15 @@ package main import ( "encoding/json" "fmt" + "net/url" "os" + "strings" "time" ) const ( Listen = "127.0.0.1:3128" + ProxyEnv = "AGENTENV_EGRESS_PROXY" EgressUser = "agentenv-egress" SessionUser = "user" Folder = "/etc/agentenv/egress" @@ -81,7 +85,12 @@ func main() { os.Exit(1) } case "serve": - if err := serveForever(); err != nil { + listen, err := serveListen(os.Args[2:], os.Getenv(ProxyEnv)) + if err != nil { + fmt.Fprintln(os.Stderr, "egress serve:", err) + os.Exit(2) + } + if err := serveForever(listen); err != nil { fmt.Fprintln(os.Stderr, "egress serve:", err) os.Exit(1) } @@ -90,3 +99,19 @@ func main() { os.Exit(2) } } + +// serveListen is the address serve listens on: the host and port of named, the URL a base whose own proxy holds Listen +// gives its proxies for this one in ProxyEnv, else Listen. +func serveListen(args []string, named string) (string, error) { + if len(args) != 0 { + return "", fmt.Errorf("usage: egress serve, not %q", args) + } + if named == "" { + return Listen, nil + } + u, err := url.Parse(named) + if err != nil || u.Scheme != "http" || u.Hostname() == "" || u.Port() == "" || strings.Trim(u.Path, "/") != "" { + return "", fmt.Errorf("%s is http://HOST:PORT, not %q", ProxyEnv, named) + } + return u.Host, nil +} diff --git a/src/agent_env/agents/agents/claude-code/egress/main_test.go b/src/agent_env/agents/agents/claude-code/egress/main_test.go new file mode 100644 index 000000000..77f80844e --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/egress/main_test.go @@ -0,0 +1,27 @@ +package main + +import "testing" + +func TestServeListensWhereTheBaseNamesItElseOnListen(t *testing.T) { + for _, tc := range []struct { + args []string + named string + want string + ok bool + }{ + {nil, "", Listen, true}, + {nil, "http://127.0.0.1:3130", "127.0.0.1:3130", true}, + {nil, "http://127.0.0.1:3130/", "127.0.0.1:3130", true}, + {nil, "http://[::1]:3130", "[::1]:3130", true}, + {nil, "127.0.0.1:3130", "", false}, + {nil, "http://127.0.0.1", "", false}, + {nil, "https://127.0.0.1:3130", "", false}, + {nil, "http://127.0.0.1:3130/proxy", "", false}, + {[]string{"--listen", "127.0.0.1:3130"}, "", "", false}, + } { + got, err := serveListen(tc.args, tc.named) + if (err == nil) != tc.ok || got != tc.want { + t.Errorf("serveListen(%q, %q) = %q, %v; want %q, ok %v", tc.args, tc.named, got, err, tc.want, tc.ok) + } + } +} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy.go b/src/agent_env/agents/agents/claude-code/egress/proxy.go index c575fe5e6..3755399b9 100644 --- a/src/agent_env/agents/agents/claude-code/egress/proxy.go +++ b/src/agent_env/agents/agents/claude-code/egress/proxy.go @@ -37,7 +37,7 @@ type Proxy struct { probes probes } -func serveForever() error { +func serveForever(listen string) error { leaves, err := LoadLeaves(Folder) if err != nil { return fmt.Errorf("the CA (has boot run?): %w", err) @@ -52,12 +52,12 @@ func serveForever() error { } universe := NewUniverse(ClockPage) go universe.Poll() - ln, err := net.Listen("tcp", Listen) + ln, err := net.Listen("tcp", listen) if err != nil { return err } p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} - p.Log(map[string]any{"event": "serving", "listen": Listen, "uid": os.Getuid(), "pid": os.Getpid()}) + p.Log(map[string]any{"event": "serving", "listen": listen, "uid": os.Getuid(), "pid": os.Getpid()}) return p.Serve(ln) } diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index 41f7ae37e..a6d254595 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -7,7 +7,9 @@ protocol from PyPI, which has no CLOCK_V1 until a release.""" import asyncio +import json import os +import re import subprocess import time import uuid @@ -18,15 +20,18 @@ from agent_env.a2a_agent.a2a_agent import A2AAgent from agent_env.artifact import DockerImageArtifact from agent_env.env.gateway.constants import EXT_CLOCK_URI +from agent_env.providers.sandbox_providers.local_docker.provider import _host_ips, host_url_for from agentenv_protocol.client import ExtensionCallError from agent_env.task_step.context import TaskStepContext from agent_env.task_step.task_steps.agent_clock import FOLLOWING_KEY from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep +from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep from tst.integration.store.local_parallel_deploy_test import _seed_bootstrap_envs from tst.integration.task_step.test_prompt_agent_live_trajectory_local import local_registry as local_registry from tst.integration.task_step.test_register_env_triggers_local import _deploy_items +from tst.util.fake_anthropic import FakeAnthropic, tool_then_answer pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] @@ -42,6 +47,14 @@ def _in_agent(container: str, *command: str, real_time: bool = False) -> str: text=True, check=True, timeout=30).stdout.strip() +def _routes_through_proxy(container: str) -> bool: + """Whether the CLI the agent starts now goes out through the egress proxy, as the image's bridge decides.""" + # The A2A server's environment has the proxy (root.conf); this shell's doesn't, so it's passed as the server has it. + return _in_agent(container, "python3", "-c", "import claude_bridge as b; print('HTTPS_PROXY' in b.cli_env(" + "{b.EGRESS_PROXY_ENV: 'http://127.0.0.1:3128'}, clock_armed=b.clock_armed()))", + real_time=True) == "True" + + def _container_of(sandbox_id: str) -> str: names = subprocess.run(["docker", "ps", "--format", "{{.Names}}"], capture_output=True, text=True, check=True).stdout.split() @@ -56,12 +69,19 @@ async def _followers(deployed) -> dict: @pytest.mark.skipif(not IMAGE, reason="set AGENTENV_E2E_CLAUDE_CODE_IMAGE to an image built from this branch") @pytest.mark.asyncio async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_down(local_registry): + with FakeAnthropic(host=_host_ips()[-1]) as endpoint: # where this machine's containers reach it + await _follows_until_torn_down(endpoint) + + +async def _follows_until_torn_down(endpoint: FakeAnthropic) -> None: _seed_bootstrap_envs() env, deployed, _ = await _deploy_items("ccclock") uid = uuid.uuid4().hex[:8] image = DockerImageArtifact.put(id=f"cc-clock-img-{uid}", description="e2e", image_name=IMAGE) agent = A2AAgent.put(id=f"cc-clock-{uid}", docker_image_artifact=image, - default_env_vars={"LITELLM_API_KEY": "unused", "LITELLM_BASE_URL": "http://unused.invalid"}) + default_env_vars={"LITELLM_API_KEY": "fake-key", + # as the agent's container reaches it: on macOS, loopback is the host's + "LITELLM_BASE_URL": host_url_for(endpoint.url, "local")}) context = TaskStepContext(deployed_envs=[deployed], deployed_agents=[], metadata={}) try: await DeployAgentTaskStep(id="deploy-solver", version=None, env_ids=[env.id], a2a_agent_id=agent.id, @@ -69,6 +89,9 @@ async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_dow sandbox_type="local").execute(context) container = _container_of(context.deployed_agents[-1].sandbox_id) assert _in_agent(container, "date", "-u", "+%Y") == str(datetime.now(timezone.utc).year) # not armed: real + assert not _routes_through_proxy(container) # and the CLI goes out directly, as before + # The CLI starts under the preloaded library: without its init guard, its first clock read deadlocked. + assert "(Claude Code)" in _in_agent(container, "timeout", "30", "claude", "--version") await SyncEnvClockTaskStep(id="clock", version=None, env_id=env.id, virtual_time=VIRTUAL, virtual_seconds_per_real_second=0, require_agent_clock=True, @@ -88,6 +111,17 @@ async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_dow print("agent reads:", shown) assert shown["date"] == VIRTUAL and shown["python"].startswith("2031-03-04T05:06:07") assert shown["real"].startswith(str(datetime.now(timezone.utc).year)) + assert _routes_through_proxy(container) # armed: live sites' TLS goes through the proxy, on real time + + # One turn of the real CLI: the date it gives the model and a Bash `date` the model runs are the env's. + endpoint.respond = tool_then_answer("Bash", {"command": "date -u +%FT%TZ"}, prefix="the shell said: ") + await PromptAgentTaskStep(id="ask", version=None, prompt="What's the date? Run date.", agent_name="solver", + max_conversation_turns=1).execute(context) + bodies = [json.dumps(call.body) for call in endpoint.messages()] + dated = {m for body in bodies for m in re.findall(r"Today's date is ([0-9]{4}-[0-9]{2}-[0-9]{2})", body)} + print("the CLI's date line:", dated, "Bash date:", endpoint.tool_results()) + assert dated == {VIRTUAL[:10]}, dated + assert any(VIRTUAL in result for result in endpoint.tool_results()), endpoint.tool_results() # A write that waits for followers waits for the agent, which then shows it at once. started = time.monotonic() 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 8bef2b3c1..7f985df93 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -1,10 +1,11 @@ """Claude Code on the env's clock (FD-5916): the image runs the computer base's clock follower and egress proxy, each as its own user, and agent-env's libfaketime for the agent's user, so the CLI and every command the model runs read -the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. The CLI goes out through -the egress proxy, and to the env's MCP servers directly.""" +the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. While the clock is armed +the CLI goes out through the egress proxy, and to the env's MCP servers directly; unarmed, it goes out as before.""" from __future__ import annotations +import json import re import pytest @@ -22,7 +23,21 @@ def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_s copied = sorted(p.relative_to(AGENT_DIR / folder) for p in (AGENT_DIR / folder).rglob("*") if p.is_file()) assert copied - assert [str(p) for p in copied if (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] + assert [str(p) for p in copied if str(p) != "libfaketime.patch" + and (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] + + +def test_the_agents_libfaketime_patch_is_the_bases_with_the_init_guard_after_it(): + # The Claude Code CLI reads the clock while libfaketime's init runs (an allocator, as dlsym allocates), and + # pthread_once then waits on itself: `claude --version` never returns. The guard has a clock call made during the + # init read the kernel's clock. The base doesn't take it yet, so the agent's copy is the base's patch, then the guard. + base, agent = (BASE / "clock" / "libfaketime.patch").read_text(), (AGENT_DIR / "clock" / "libfaketime.patch").read_text() + guard = agent.removeprefix(base) + + assert agent.startswith(base) and guard.startswith("diff -ruN a/src/libfaketime.c b/src/libfaketime.c\n") + assert "static __thread int ftpl_initializing = 0;" in guard + assert "if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp);" in guard + assert "pthread_once(&initialized_once_control, ftpl_really_init_guarded);" in guard def _program(name: str) -> dict[str, str]: @@ -54,13 +69,42 @@ def test_started_as_another_user_the_image_runs_the_server_alone_as_before(): assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start -def test_where_the_image_runs_the_proxy_the_cli_goes_out_through_it_and_to_the_envs_servers_directly(): - env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"]) +def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy_and_to_the_envs_servers_directly(): + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"], + clock_armed=True) assert {env[name] for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "https_proxy")} == {PROXY} assert env["NO_PROXY"] == env["no_proxy"] == "localhost,127.0.0.1,::1,host.docker.internal,gateway" assert env["NODE_USE_ENV_PROXY"] == "1" -def test_without_the_proxy_the_cli_is_run_as_before(): - assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env({})) +@pytest.mark.parametrize("base_url, direct", [ + ("http://10.0.0.5:4000", True), # plain HTTP, private: nothing to check at the env's date, and the proxy refuses it + ("https://litellm.example.com", False), # TLS: its certificate is checked through the proxy, on real time +]) +def test_a_model_endpoint_over_plain_http_is_reached_directly_on_an_armed_clock(base_url, direct): + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url, + "LITELLM_API_KEY": "k"}, clock_armed=True) + host = base_url.split("//")[1].split(":")[0] + assert (host in env["NO_PROXY"].split(",")) is direct and env["HTTPS_PROXY"] == PROXY + + +@pytest.mark.parametrize("environ, armed", [({}, True), ({claude_bridge.EGRESS_PROXY_ENV: PROXY}, False)]) +def test_without_the_proxy_or_an_armed_clock_the_cli_goes_out_directly_as_before(environ, armed): + # A model endpoint at a private address, which the proxy refuses, keeps working until a task arms the clock. + assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env(environ, clock_armed=armed)) + + +@pytest.mark.parametrize("status, armed", [ + ({"connected": True, "armed": True, "version": "b:3"}, True), + ({"connected": True, "armed": False, "version": "b:1"}, False), # following a gateway whose clock isn't armed + ({"connected": False, "armed": True}, False), # not following: its page holds real time + ({"connected": False, "waiting": "no config yet"}, False), + (None, False), # no follower + ("not json", False), +]) +def test_the_clock_is_armed_when_the_follower_follows_an_armed_clock(tmp_path, status, armed): + path = tmp_path / "status.json" + if status is not None: + path.write_text(status if isinstance(status, str) else json.dumps(status)) + assert claude_bridge.clock_armed(str(path)) is armed From c8ed6e13633432a3dcb3737bf25dca9319febfa5 Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 10:16:28 -0800 Subject: [PATCH 11/29] fix(agents): a later clock sync leaves agents teardown_sandboxes took down alone Their records stay in deployed_agents after teardown, and calling their gone servers failed the step. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../task_step/task_steps/agent_clock.py | 7 +++++++ .../task_step/task_steps/sync_env_clock.py | 6 +++--- tst/unit/task_step/agent_clock_test.py | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/task_step/task_steps/agent_clock.py index dbc9c3998..b0487c6e8 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/task_step/task_steps/agent_clock.py @@ -128,6 +128,13 @@ def followed_env(context: TaskStepContext, agent: DeployedAgent) -> Optional[str return ((context.metadata.get(FOLLOWING_KEY) or {}).get(_slot(agent.agent_name)) or {}).get("env_id") +def torn_down(context: TaskStepContext, agent: DeployedAgent) -> bool: + """Whether ``teardown_sandboxes`` took ``agent``'s sandbox down: its record stays in the context, its server is gone.""" + from agent_env.task.teardown import TORN_DOWN_KEY + + return bool(agent.sandbox_id) and agent.sandbox_id in (context.metadata.get(TORN_DOWN_KEY) or ()) + + def clock_env(agent: DeployedAgent) -> Optional[str]: """The one env whose clock ``agent`` follows: the first it's registered with. Fixed by the agent alone, so two steps arming two of its envs' clocks at once never both take its one follower.""" diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index ceed5562f..8e14aaf8d 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -193,12 +193,12 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: """Have every agent registered with the env follow its clock (``agent_clock.follow``): one that can't is reported and skipped, or fails the step under ``require_agent_clock``.""" - from agent_env.task_step.task_steps.agent_clock import follow_in + from agent_env.task_step.task_steps.agent_clock import follow_in, torn_down outcomes = [] for agent in context.deployed_agents: - if self.env_id not in (agent.env_ids or []): - continue + if self.env_id not in (agent.env_ids or []) or torn_down(context, agent): + continue # not the env's, or taken down by teardown_sandboxes: nothing to follow outcome = await follow_in(context, agent, deployed, env_get_time_url=env_get_time_url, timezone=self.timezone, timeout=self.timeout_seconds) if not outcome["synced"]: diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 4f6bd7230..576fcc9c3 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -302,3 +302,20 @@ async def test_tearing_down_one_of_two_look_alike_agents_retires_its_own_followe assert await agent_clock.retire_followers(context, ["team.solver"]) == ["agent-team.solver"] assert [json.loads(r.content) for r in sent] == [{"name": "agent-team.solver"}] assert agent_clock.followed_env(context, _agent("team_solver")) == "e1" + + +@pytest.mark.asyncio +async def test_a_later_sync_leaves_an_agent_teardown_took_down_alone(monkeypatch): + from agent_env.task.teardown import TORN_DOWN_KEY + + sent = _mock_http(monkeypatch, state={"env_get_time_url": CLOCK_URL}) + _no_servers(monkeypatch) + gone, kept = _agent("gone"), _agent("kept") + gone.sandbox_id, kept.sandbox_id = "sb-gone", "sb-kept" + context = await _context(gone, kept) + context.metadata[TORN_DOWN_KEY] = ["sb-gone"] + + await _step(require_agent_clock=True).execute(context) + + assert [a["agent"] for a in context.metadata["clock_configurations"][0]["agents"]] == ["kept"] + assert not any("agent-gone" in (r.content or b"").decode() for r in sent) From b5d3b4b33c1a420a3eb15a00717d7ca529792c57 Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 10:38:11 -0800 Subject: [PATCH 12/29] fix(ci): the agent clock e2e skips as a declared capability gap; the browser base's egress copy takes boot.go - test_agent_env_clock_local skips as agentenv-capability-missing: agent_clock_image, which the slow integration job allows, as no CI job builds agent images. - ubuntu-24.04-browser's egress/ is ubuntu-24.04's again, with this PR's AGENTENV_SESSION_USER and AGENTENV_EGRESS_NSS. Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/workflows/local-backends.yml | 2 +- .../artifacts/ubuntu-24.04-browser/egress/boot.go | 11 ++++++++++- .../ubuntu-24.04-browser/egress/boot_test.go | 11 +++++++++++ .../task_step/test_agent_env_clock_local.py | 5 +++-- tst/util/capabilities.py | 10 ++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/.github/workflows/local-backends.yml b/.github/workflows/local-backends.yml index 20149e378..cc60694ae 100644 --- a/.github/workflows/local-backends.yml +++ b/.github/workflows/local-backends.yml @@ -251,7 +251,7 @@ jobs: run: | .venv/bin/python .github/scripts/check_skip_policy.py junit-integration-slow.xml \ --allow model_endpoint_configured remote_sandbox default_a2a_agent mcp_server_sources windows_media \ - omarchy_image + omarchy_image agent_clock_image # The tests' times, for .github/scripts/slow_test_durations.py to rebalance the shards with. - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 if: always() diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go index e1ea67894..031555627 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go @@ -31,8 +31,10 @@ type bootPaths struct { NSS func(paths bootPaths) error } +// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in +// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. func defaultBootPaths() bootPaths { - return bootPaths{ + p := bootPaths{ Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, UpdateTrust: []string{"update-ca-certificates"}, @@ -41,6 +43,13 @@ func defaultBootPaths() bootPaths { EgressUser: EgressUser, SessionUser: SessionUser, NSS: sessionNSS, } + if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { + p.SessionUser = name + } + if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { + p.NSS = nil + } + return p } // boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go index 239d8db0a..c5f59ad0a 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go @@ -86,3 +86,14 @@ func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { t.Fatalf("%v", report) } } + +func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { + if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { + t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) + } + t.Setenv("AGENTENV_SESSION_USER", "agent") + t.Setenv("AGENTENV_EGRESS_NSS", "off") + if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { + t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) + } +} diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index a6d254595..2de0c9fcb 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -31,11 +31,12 @@ from tst.integration.store.local_parallel_deploy_test import _seed_bootstrap_envs from tst.integration.task_step.test_prompt_agent_live_trajectory_local import local_registry as local_registry from tst.integration.task_step.test_register_env_triggers_local import _deploy_items +from tst.util.capabilities import AGENT_CLOCK_IMAGE_ENV, skip_without_agent_clock_image from tst.util.fake_anthropic import FakeAnthropic, tool_then_answer pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] -IMAGE = os.environ.get("AGENTENV_E2E_CLAUDE_CODE_IMAGE", "") +IMAGE = os.environ.get(AGENT_CLOCK_IMAGE_ENV, "") VIRTUAL = "2031-03-04T05:06:07Z" LATER = "2031-03-05T10:00:00Z" PAUSED = "2031-03-05T12:00:00Z" @@ -66,7 +67,7 @@ async def _followers(deployed) -> dict: return (await deployed.invoke(EXT_CLOCK_URI, "state", {}, timeout=10)).get("followers") or {} -@pytest.mark.skipif(not IMAGE, reason="set AGENTENV_E2E_CLAUDE_CODE_IMAGE to an image built from this branch") +@skip_without_agent_clock_image() @pytest.mark.asyncio async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_down(local_registry): with FakeAnthropic(host=_host_ips()[-1]) as endpoint: # where this machine's containers reach it diff --git a/tst/util/capabilities.py b/tst/util/capabilities.py index de622a5ee..f53213cc3 100644 --- a/tst/util/capabilities.py +++ b/tst/util/capabilities.py @@ -78,6 +78,10 @@ #: Composing an Omarchy computer builds its base from Omarchy's own VM image, which a run fetches into this machine's #: cache once, about 4 GB; a test that composes one needs it there already, and KVM, which boots linux/amd64 disks. OMARCHY_IMAGE = "omarchy_image" +#: The agent clock end to end (FD-5916) runs over a Claude Code image built from the branch beforehand, named by +#: ``AGENTENV_E2E_CLAUDE_CODE_IMAGE``: no CI job builds agent images, and the shipped one installs the protocol from PyPI. +AGENT_CLOCK_IMAGE = "agent_clock_image" +AGENT_CLOCK_IMAGE_ENV = "AGENTENV_E2E_CLAUDE_CODE_IMAGE" #: The shipped Omarchy base, whose source the capability looks for. OMARCHY_ARTIFACT = Path(__file__).parents[2] / "src" / "agent_env" / "computers" / "artifacts" / "omarchy-4.5" / "artifact.toml" @@ -241,6 +245,12 @@ def omarchy_image_is_available() -> bool: return "linux/amd64" in LocalSandboxProvider.disk_image_platforms() and source_path(sha256).exists() +def skip_without_agent_clock_image() -> pytest.MarkDecorator: + """Collection-time ``skipif`` for tests that run over a Claude Code image built with the agent clock.""" + return pytest.mark.skipif(not os.environ.get(AGENT_CLOCK_IMAGE_ENV), + reason=missing_capability_reason(AGENT_CLOCK_IMAGE)) + + def skip_without_omarchy_image() -> pytest.MarkDecorator: """Collection-time ``skipif`` for tests that compose an Omarchy computer on local Docker.""" return pytest.mark.skipif(not omarchy_image_is_available(), reason=missing_capability_reason(OMARCHY_IMAGE)) From 25af21a53f863c20fdc9b8fe0426e275c3f322fa Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 11:33:26 -0800 Subject: [PATCH 13/29] chore: drop test-run output committed again, and ignore where the protocol's tests write it Co-Authored-By: Claude Opus 5.5 (1M context) --- .gitignore | 2 ++ packages/agentenv-protocol/.agentenv/browser-chromium.jsonl | 3 --- packages/agentenv-protocol/.agentenv/launches | 4 ---- 3 files changed, 2 insertions(+), 7 deletions(-) delete mode 100644 packages/agentenv-protocol/.agentenv/browser-chromium.jsonl delete mode 100644 packages/agentenv-protocol/.agentenv/launches diff --git a/.gitignore b/.gitignore index edb9006f9..9c75a6675 100644 --- a/.gitignore +++ b/.gitignore @@ -168,6 +168,8 @@ cython_debug/ # Generated local-store state (stores default local; see runtime._ensure_agentenv_gitignore) /.agentenv/ +# ...and what the protocol package's tests write when run from its own folder (launches, browser tab events) +/packages/agentenv-protocol/.agentenv/ # Written by the release workflow into the wheels it publishes src/agent_env/env/published_images.json diff --git a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl b/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl deleted file mode 100644 index 11767f027..000000000 --- a/packages/agentenv-protocol/.agentenv/browser-chromium.jsonl +++ /dev/null @@ -1,3 +0,0 @@ -{"seq": 1, "at": "2026-11-17T17:52:18.948Z", "tab": {"event": "opened", "id": "D1", "url": "https://shop.test/", "by": "page"}} -{"seq": 2, "at": "2026-11-17T17:52:18.948Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} -{"seq": 3, "at": "2026-11-17T17:52:18.967Z", "tab": {"event": "opened", "id": "L1", "url": "https://shop.test/", "by": "page"}} diff --git a/packages/agentenv-protocol/.agentenv/launches b/packages/agentenv-protocol/.agentenv/launches deleted file mode 100644 index a7d341049..000000000 --- a/packages/agentenv-protocol/.agentenv/launches +++ /dev/null @@ -1,4 +0,0 @@ -{"at": "2026-11-17T17:52:21.848022+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T17:52:24.898589+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T17:52:25.081340+00:00", "event": "launch", "pid": 4242, "cause": "launch"} -{"at": "2026-11-17T17:52:25.777610+00:00", "event": "launch", "pid": 4242, "cause": "launch"} From 7216c7c8ad782be8ef744fc7356bd93c73b9279d Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 11:34:56 -0800 Subject: [PATCH 14/29] fix(agents): route the CLI through egress by the clock page's armed flag, not the follower's status The page is what libfaketime reads, and it keeps the last armed clock while the follower reconnects to a restarted gateway or restarts itself, when its status says connected false or armed false. A CLI started then reads the env's time, so it must go through the proxy. Unit tests pin both windows. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agents/claude-code/claude_bridge.py | 34 ++++++++++--- .../claude_code/test_env_clock_image.py | 51 +++++++++++++++---- 2 files changed, 67 insertions(+), 18 deletions(-) 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 8e49d8d1e..642d24868 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -11,6 +11,7 @@ import re import signal import stat +import struct import time from collections.abc import Callable, Iterable, Mapping from dataclasses import dataclass, field, replace @@ -177,18 +178,37 @@ def _model_headers(raw: str | None) -> dict[str, str]: #: Set by the image (root.conf) where the egress proxy runs: the CLI and the model's commands go out through it #: while the agent follows an armed clock. EGRESS_PROXY_ENV = "AGENTENV_EGRESS_PROXY" -#: The clock follower's status, which says whether the clock it follows is armed (agentenv_protocol.computer.clock). -CLOCK_STATUS = "/run/agentenv-clock/status.json" +#: The clock page agent-env's libfaketime reads the CLI's time from (clock/clockpage.py, version 2): it keeps the last +#: armed clock while the follower reconnects or restarts, which the follower's status doesn't say. +CLOCK_PAGE = "/dev/shm/agentenv-clock" +_PAGE = struct.Struct(" bool: - """Whether this image's clock follower follows an armed clock, so the CLI started now reads the env's time.""" +def clock_armed(page_path: str | None = None) -> bool: + """Whether the clock page holds an armed clock, so the CLI started now reads the env's time: the page itself, + as libfaketime reads it, retried while a write is under way (seq odd) or its check doesn't match.""" try: - status = json.loads(Path(status_path or CLOCK_STATUS).read_text()) - except (OSError, ValueError): + with open(page_path or CLOCK_PAGE, "rb") as page: + for _ in range(50): + page.seek(0) + raw = page.read(_PAGE.size) + if len(raw) < _PAGE.size: + return False + magic, version, seq, universe, anchor, rate, armed, _, generation, checked, mono_u, mono_a = \ + _PAGE.unpack(raw) + if magic != _PAGE_MAGIC or version != _PAGE_VERSION: + return False + rate_bits = struct.unpack(" dict[str, str]: 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 7f985df93..883ba935a 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -7,6 +7,7 @@ import json import re +import types import pytest @@ -95,16 +96,44 @@ def test_without_the_proxy_or_an_armed_clock_the_cli_goes_out_directly_as_before assert not {"HTTPS_PROXY", "NO_PROXY"} & set(claude_bridge.cli_env(environ, clock_armed=armed)) -@pytest.mark.parametrize("status, armed", [ - ({"connected": True, "armed": True, "version": "b:3"}, True), - ({"connected": True, "armed": False, "version": "b:1"}, False), # following a gateway whose clock isn't armed - ({"connected": False, "armed": True}, False), # not following: its page holds real time - ({"connected": False, "waiting": "no config yet"}, False), - (None, False), # no follower - ("not json", False), +def _clockpage(): + """The image's clock page writer, whose encoding libfaketime and the egress proxy read too.""" + # Run from its source, so no __pycache__ lands in clock/, which must stay the computer base's file for file. + module = types.ModuleType("clockpage") + path = AGENT_DIR / "clock" / "clockpage.py" + exec(compile(path.read_text(), str(path), "exec"), module.__dict__) + return module + + +def _page(armed: int, seq: int = 2) -> bytes: + return _clockpage().encode(seq, 1_900_000_000 * 10**9 if armed else 0, 10**18 if armed else 0, 1.0, armed, 3, 0, 0) + + +@pytest.mark.parametrize("page, armed", [ + (_page(1), True), + (_page(0), False), + # A write under way (seq odd) that never finishes: the armed flag it left is the best guess. + (_page(1, seq=3), True), + (_page(1)[:40], False), # not a page + (b"\0" * 80, False), # not one yet: no magic + (None, False), # no page: an image without the clock, or install_agent's target ]) -def test_the_clock_is_armed_when_the_follower_follows_an_armed_clock(tmp_path, status, armed): - path = tmp_path / "status.json" - if status is not None: - path.write_text(status if isinstance(status, str) else json.dumps(status)) +def test_the_clock_is_armed_when_the_page_libfaketime_reads_says_so(tmp_path, page, armed): + path = tmp_path / "agentenv-clock" + if page is not None: + path.write_bytes(page) assert claude_bridge.clock_armed(str(path)) is armed + + +@pytest.mark.parametrize("status", [ + {"connected": False, "error": "URLError: gateway restarting"}, # the follower lost its gateway + {"connected": False, "waiting": "no config yet"}, # the follower restarted, before its config is read + {"connected": True, "armed": False, "version": None}, # ... and before its first watch answers +]) +def test_while_the_follower_reconnects_or_restarts_the_page_still_routes_the_cli_through_the_proxy(tmp_path, status): + # The page keeps the last armed clock in both windows, so the CLI reads the env's time and must go through egress. + (tmp_path / "status.json").write_text(json.dumps(status)) + page = tmp_path / "agentenv-clock" + page.write_bytes(_page(1)) + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, clock_armed=claude_bridge.clock_armed(str(page))) + assert env["HTTPS_PROXY"] == PROXY From f8bd9fff6aa3f3c0cb13a1a644630f87c009a339 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Tue, 17 Nov 2026 13:55:00 -0800 Subject: [PATCH 15/29] feat(a2a-agent): validate clock/v1 by the time the agent's own commands show An agent whose card serves clock/v1 sync_time is now checked end to end after the validation DAG: the validation env's clock is armed frozen in America/Chicago with require_agent_clock, the agent runs `date` through its harness and must show that instant and zone, a write that waits for followers must be acknowledged, and `date` must show the time it moved to. A synced follower alone doesn't pass: an image whose preload is missing, or whose harness can't run under it, follows and still fails. An agent whose card serves no sync_time is skipped. The result is recorded under validated_a2a_extensions[urn:agentenv:clock/v1] and verifications["a2a_clock"]. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/a2a_agent/validator.py | 91 ++++++++++++++ tst/unit/a2a_agent/test_validator_clock.py | 138 +++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 tst/unit/a2a_agent/test_validator_clock.py diff --git a/src/agent_env/a2a_agent/validator.py b/src/agent_env/a2a_agent/validator.py index 094f1dee6..1bc9d48a0 100644 --- a/src/agent_env/a2a_agent/validator.py +++ b/src/agent_env/a2a_agent/validator.py @@ -16,10 +16,14 @@ import asyncio import base64 import logging +import re import uuid from dataclasses import dataclass +from datetime import datetime from typing import Callable, Optional, Union +from zoneinfo import ZoneInfo +import httpx from agentenv_protocol.a2a_agent import ObjectChangelogApplyResponse from agent_env.a2a_agent.object_transfer import ( @@ -32,6 +36,7 @@ from agent_env.a2a_agent.a2a_agent import A2AAgent from agent_env.bundle.run import _write_bundle from agent_env.config import get_config +from agent_env.env.gateway.constants import EXT_CLOCK_URI from agent_env.env.store import get_env_store from agent_env.plugins._bundles import ENV_ALIASES, installed_aliases from agent_env.store.base import NotFoundError @@ -60,7 +65,10 @@ VIDEO_PROBE_MP4_B64, VIDEO_PROBE_PROMPT, ) +from agent_env.task_step.task_step import TaskStep +from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep from agent_env.task_step.task_steps.sandbox_utils.sandbox_utils import find_agent_container +from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep logger = logging.getLogger(__name__) @@ -126,6 +134,16 @@ class ModalityProbe: ), ] +#: clock/v1: the validation env's clock is armed frozen at the first time, in the zone, then a write that waits for the +#: agent moves it to the second; at each, the agent runs the command through its harness and must show that time. +CLOCK_PROBE_TIMES = ("2031-03-03T23:06:07-06:00", "2031-03-04T23:06:07-06:00") +CLOCK_PROBE_ZONE = "America/Chicago" +CLOCK_PROBE_COMMAND = "date +%Y-%m-%dT%H:%M:%S%z" +CLOCK_PROBE_PROMPT = f"Run exactly one shell command and nothing else: `{CLOCK_PROBE_COMMAND}`. Reply with only its output." +CLOCK_WAIT_FOLLOWERS_S = 10.0 +_CLOCK_STAMP = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}") +_CLOCK_STAMP_FORMAT = "%Y-%m-%dT%H:%M:%S%z" + def write_validation_env(on_progress: Callable[[str], None]) -> None: """Write the env the validator deploys, ``A2AAgent.VALIDATION_ENV_ID``, when the store has none of that id: the env @@ -448,6 +466,9 @@ async def _run( on_progress=on_progress, ) + # clock/v1 after the DAG: arming the validation env's clock moves the clock of every agent that follows it. + await A2AAgentValidator._validate_clock(agent, context, task_id=task_id, on_progress=on_progress) + # install/v1: gated on the LIVE deployed card (not the stored one), so it # runs identically regardless of whether this agent was validated before. await A2AAgentValidator._validate_install( @@ -743,6 +764,65 @@ def record(*, supported, advertised, save_ok, apply_ok, roundtrip_ok, note=""): record(supported=roundtrip_ok, advertised=advertised, save_ok=save_ok, apply_ok=True, roundtrip_ok=roundtrip_ok) + @staticmethod + async def _validate_clock( + agent: "A2AAgent", + context, + task_id: str, + on_progress: Optional[Callable[[str], None]] = None, + ) -> None: + """Validate clock/v1 where the live card serves ``sync_time``: the validation env's clock armed frozen with the + agent required to follow it, its harness's ``date`` showing that time in the task's zone, a write that waits for + the agent acknowledged, and ``date`` showing the time it moved to.""" + log = on_progress or (lambda _msg: None) + main = next((a for a in context.deployed_agents if a.agent_name == TaskStep.DEFAULT_AGENT_NAME), None) + env = next((d for d in context.deployed_envs if d.env_id == agent.VALIDATION_ENV_ID), None) + advertised = main is not None and A2AAgent.extension_method(main.a2a_card or {}, EXT_CLOCK_URI, "sync_time") is not None + result: dict = {"supported": False, "advertised": advertised, "synced": False, "follows_writes": False, + "readings": []} + if not advertised or env is None: + result["skipped_reason"] = ("clock/v1 sync_time not advertised on live agent card" if not advertised + else f"validation env {agent.VALIDATION_ENV_ID!r} not deployed") + log(f"clock/v1 skipped: {result['skipped_reason']}") + else: + log("Validating clock/v1: the agent follows the validation env's clock...") + try: + await SyncEnvClockTaskStep( + id=f"{task_id}-clock", version=None, env_id=env.env_id, virtual_time=CLOCK_PROBE_TIMES[0], + virtual_seconds_per_real_second=0, timezone=CLOCK_PROBE_ZONE, require_agent_clock=True, + ).execute(context) + result["synced"] = True + result["readings"].append(await A2AAgentValidator._read_agent_clock(context, f"{task_id}-clock-1", CLOCK_PROBE_TIMES[0])) + await env.invoke(EXT_CLOCK_URI, "advance", + {"virtual_time": CLOCK_PROBE_TIMES[1], "wait_followers_s": CLOCK_WAIT_FOLLOWERS_S}, + timeout=int(CLOCK_WAIT_FOLLOWERS_S) + 30) + result["follows_writes"] = True + result["readings"].append(await A2AAgentValidator._read_agent_clock(context, f"{task_id}-clock-2", CLOCK_PROBE_TIMES[1])) + except httpx.HTTPStatusError as e: + result["error"] = f"HTTP {e.response.status_code}: {e.response.text}"[:600] + except Exception as e: # noqa: BLE001 - recorded as why the agent doesn't follow + result["error"] = f"{type(e).__name__}: {e}"[:600] + result["supported"] = (result["synced"] and result["follows_writes"] and len(result["readings"]) == 2 + and all(r["time"] and r["zone"] for r in result["readings"])) + log(f"clock/v1: supported={result['supported']} synced={result['synced']} " + f"follows_writes={result['follows_writes']} readings={[r['shown'] for r in result['readings']]}") + try: + fresh = A2AAgent.get(agent.id) + validated = dict(fresh.metadata.get("validated_a2a_extensions", {})) + validated[EXT_CLOCK_URI] = result + fresh.update_metadata({**fresh.metadata, "validated_a2a_extensions": validated}) + except Exception as e: # noqa: BLE001 + logger.warning(f"clock/v1: failed to persist validated_a2a_extensions: {e}") + context.metadata.setdefault("verifications", {})["a2a_clock"] = result + + @staticmethod + async def _read_agent_clock(context, prompt_id: str, at: str) -> dict: + """The agent's ``date``, run through its harness, read against the frozen clock at ``at``.""" + await PromptAgentTaskStep(id=prompt_id, version=None, prompt=CLOCK_PROBE_PROMPT, prompt_id=prompt_id, + timeout_seconds=180).execute(context) + reply = next((p.response or "" for p in context.prompt_responses if p.prompt_id == prompt_id), "") + return {"prompt_id": prompt_id, **clock_reading(reply, at)} + @staticmethod def _upload_install_test_image_fixture(agent: "A2AAgent"): """Upload a minimal Dockerfile as a FileArtifactUniverse so the install @@ -879,3 +959,14 @@ async def _cleanup_sandboxes(context) -> None: await sandbox.terminate() except Exception as e: logger.warning(f"Failed to clean up agent sandbox {deployed_agent.sandbox_id}: {e}") + + +def clock_reading(reply: str, at: str, zone: str = CLOCK_PROBE_ZONE) -> dict: + """What the agent's ``date`` showed of the frozen clock at ``at``: the first timestamp in ``reply``, whether it is + that instant (``time``) and whether it carries ``zone``'s offset then (``zone``).""" + expected = datetime.fromisoformat(at).astimezone(ZoneInfo(zone)) + shown = next(iter(_CLOCK_STAMP.findall(reply or "")), None) + seen = datetime.strptime(shown, _CLOCK_STAMP_FORMAT) if shown else None + return {"expected": expected.strftime(_CLOCK_STAMP_FORMAT), "shown": shown, + "time": seen is not None and seen == expected, + "zone": seen is not None and seen.utcoffset() == expected.utcoffset()} diff --git a/tst/unit/a2a_agent/test_validator_clock.py b/tst/unit/a2a_agent/test_validator_clock.py new file mode 100644 index 000000000..672cdda56 --- /dev/null +++ b/tst/unit/a2a_agent/test_validator_clock.py @@ -0,0 +1,138 @@ +from __future__ import annotations + +from types import SimpleNamespace + +import httpx +import pytest + +from agent_env.a2a_agent import A2AAgent +from agent_env.a2a_agent.validator import CLOCK_PROBE_TIMES, CLOCK_PROBE_ZONE, A2AAgentValidator, clock_reading +from agent_env.env.gateway.constants import EXT_CLOCK_URI +from agent_env.task_step.context import DeployedAgent, PromptResponse, StepContextError, TaskStepContext +from agent_env.task_step.task_step import TaskStep +from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep +from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep + +CLOCK_CARD = {"capabilities": {"extensions": [{"uri": EXT_CLOCK_URI, "params": {"endpoint": "/ext/clock", "methods": { + "sync_time": {"method": "POST", "endpoint": "/ext/clock/sync-time"}}}}]}} + + +@pytest.mark.parametrize(("reply", "time", "zone"), [ + pytest.param("2031-03-03T23:06:07-0600", True, True, id="env-time-in-the-tasks-zone"), + pytest.param("2031-03-04T05:06:07+0000", True, False, id="env-time-without-the-zone"), + pytest.param("2026-11-17T11:20:00-0600", False, True, id="real-time"), + pytest.param("I can't run shell commands here.", False, False, id="no-reading"), +]) +def test_a_reading_is_the_frozen_instant_in_the_tasks_zone(reply, time, zone): + reading = clock_reading(f"Output:\n{reply}\n", CLOCK_PROBE_TIMES[0]) + assert (reading["expected"], reading["time"], reading["zone"]) == ("2031-03-03T23:06:07-0600", time, zone) + + +class _Env: + env_id = A2AAgent.VALIDATION_ENV_ID + + def __init__(self, write_status: int = 200): + self.write_status = write_status + self.calls: list[tuple[str, dict]] = [] + + async def invoke(self, uri, method, params=None, *, timeout=None): + self.calls.append((method, params)) + if self.write_status >= 400: + request = httpx.Request("PUT", "http://gateway/clock/advance") + response = httpx.Response(self.write_status, json={"ok": False, "stale": ["agent-agent"]}, request=request) + raise httpx.HTTPStatusError("not acknowledged", request=request, response=response) + return {"ok": True} + + +@pytest.fixture +def persisted(monkeypatch) -> SimpleNamespace: + agent = SimpleNamespace(metadata={}) + agent.update_metadata = lambda metadata: setattr(agent, "metadata", metadata) + monkeypatch.setattr(A2AAgent, "get", classmethod(lambda cls, agent_id, version=None: agent)) + return agent + + +@pytest.fixture +def synced(monkeypatch) -> list[SyncEnvClockTaskStep]: + armed: list[SyncEnvClockTaskStep] = [] + + async def execute(self, context): + armed.append(self) + return context + + monkeypatch.setattr(SyncEnvClockTaskStep, "execute", execute) + return armed + + +def _env_time(at: str) -> str: + return clock_reading("", at)["expected"] + + +def _real_time(at: str) -> str: + return "2026-11-17T11:20:00-0600" + + +def _shows(monkeypatch, reply_at) -> None: + times = iter(CLOCK_PROBE_TIMES) + + async def execute(self, context): + context.prompt_responses.append(PromptResponse(prompt_id=self.prompt_id, response=reply_at(next(times)))) + return context + + monkeypatch.setattr(PromptAgentTaskStep, "execute", execute) + + +async def _validate(card: dict, env: _Env) -> dict: + agent = SimpleNamespace(id="agent-under-test", VALIDATION_ENV_ID=A2AAgent.VALIDATION_ENV_ID) + context = TaskStepContext(deployed_envs=[env], deployed_agents=[DeployedAgent( + agent_name=TaskStep.DEFAULT_AGENT_NAME, api_url="http://agent", a2a_card=card, env_ids=[env.env_id])]) + await A2AAgentValidator._validate_clock(agent, context, task_id="validate") + return context.metadata["verifications"]["a2a_clock"] + + +@pytest.mark.asyncio +async def test_an_agent_whose_card_serves_no_sync_time_is_skipped_without_arming(persisted, synced): + result = await _validate({"capabilities": {"extensions": []}}, _Env()) + assert (result["supported"], result["advertised"], synced) == (False, False, []) + assert result["skipped_reason"] == "clock/v1 sync_time not advertised on live agent card" + assert persisted.metadata["validated_a2a_extensions"][EXT_CLOCK_URI] == result + + +@pytest.mark.asyncio +async def test_an_agent_that_follows_shows_each_time_the_clock_is_at(monkeypatch, persisted, synced): + _shows(monkeypatch, _env_time) + env = _Env() + result = await _validate(CLOCK_CARD, env) + assert (result["supported"], result["synced"], result["follows_writes"]) == (True, True, True) + assert [r["shown"] for r in result["readings"]] == ["2031-03-03T23:06:07-0600", "2031-03-04T23:06:07-0600"] + assert [(s.virtual_time, s.virtual_seconds_per_real_second, s.timezone, s.require_agent_clock) for s in synced] == [ + (CLOCK_PROBE_TIMES[0], 0, CLOCK_PROBE_ZONE, True)] + assert env.calls == [("advance", {"virtual_time": CLOCK_PROBE_TIMES[1], "wait_followers_s": 10.0})] + assert persisted.metadata["validated_a2a_extensions"][EXT_CLOCK_URI]["supported"] is True + + +@pytest.mark.asyncio +async def test_an_agent_whose_commands_read_real_time_fails(monkeypatch, persisted, synced): + _shows(monkeypatch, _real_time) + result = await _validate(CLOCK_CARD, _Env()) + assert (result["supported"], result["synced"], result["follows_writes"]) == (False, True, True) + assert [(r["time"], r["zone"]) for r in result["readings"]] == [(False, True), (False, True)] + + +@pytest.mark.asyncio +async def test_an_agent_that_doesnt_acknowledge_a_write_fails_naming_it(monkeypatch, persisted, synced): + _shows(monkeypatch, _env_time) + result = await _validate(CLOCK_CARD, _Env(write_status=504)) + assert (result["supported"], result["synced"], result["follows_writes"], len(result["readings"])) == (False, True, False, 1) + assert result["error"].startswith("HTTP 504") and "agent-agent" in result["error"] + + +@pytest.mark.asyncio +async def test_an_agent_that_cant_follow_fails_with_why(monkeypatch, persisted): + async def refuse(self, context): + raise StepContextError("agent 'agent' can't follow the clock of env 'mail': no_follower_fields") + + monkeypatch.setattr(SyncEnvClockTaskStep, "execute", refuse) + result = await _validate(CLOCK_CARD, _Env()) + assert (result["supported"], result["synced"], result["readings"]) == (False, False, []) + assert result["error"] == "StepContextError: agent 'agent' can't follow the clock of env 'mail': no_follower_fields" From f9af1b6d0fd77bbdcd028aeb7755783d2d0df12e Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 14:06:52 -0800 Subject: [PATCH 16/29] refactor(a2a-agent): name the clock validation's timeouts and error length Co-Authored-By: Claude Opus 5.5 (1M context) --- src/agent_env/a2a_agent/validator.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/agent_env/a2a_agent/validator.py b/src/agent_env/a2a_agent/validator.py index 1bc9d48a0..f33ea166f 100644 --- a/src/agent_env/a2a_agent/validator.py +++ b/src/agent_env/a2a_agent/validator.py @@ -141,6 +141,12 @@ class ModalityProbe: CLOCK_PROBE_COMMAND = "date +%Y-%m-%dT%H:%M:%S%z" CLOCK_PROBE_PROMPT = f"Run exactly one shell command and nothing else: `{CLOCK_PROBE_COMMAND}`. Reply with only its output." CLOCK_WAIT_FOLLOWERS_S = 10.0 +#: How much longer than the followers' wait the advance request may take: the gateway answers after it. +CLOCK_ADVANCE_MARGIN_S = 30 +#: How long one probe turn (the agent running ``date`` through its harness) may take. +CLOCK_PROBE_TURN_TIMEOUT_S = 180 +#: How much of a failure the validation result keeps as its error. +CLOCK_ERROR_CHARS = 600 _CLOCK_STAMP = re.compile(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}") _CLOCK_STAMP_FORMAT = "%Y-%m-%dT%H:%M:%S%z" @@ -795,13 +801,13 @@ async def _validate_clock( result["readings"].append(await A2AAgentValidator._read_agent_clock(context, f"{task_id}-clock-1", CLOCK_PROBE_TIMES[0])) await env.invoke(EXT_CLOCK_URI, "advance", {"virtual_time": CLOCK_PROBE_TIMES[1], "wait_followers_s": CLOCK_WAIT_FOLLOWERS_S}, - timeout=int(CLOCK_WAIT_FOLLOWERS_S) + 30) + timeout=int(CLOCK_WAIT_FOLLOWERS_S) + CLOCK_ADVANCE_MARGIN_S) result["follows_writes"] = True result["readings"].append(await A2AAgentValidator._read_agent_clock(context, f"{task_id}-clock-2", CLOCK_PROBE_TIMES[1])) except httpx.HTTPStatusError as e: - result["error"] = f"HTTP {e.response.status_code}: {e.response.text}"[:600] + result["error"] = f"HTTP {e.response.status_code}: {e.response.text}"[:CLOCK_ERROR_CHARS] except Exception as e: # noqa: BLE001 - recorded as why the agent doesn't follow - result["error"] = f"{type(e).__name__}: {e}"[:600] + result["error"] = f"{type(e).__name__}: {e}"[:CLOCK_ERROR_CHARS] result["supported"] = (result["synced"] and result["follows_writes"] and len(result["readings"]) == 2 and all(r["time"] and r["zone"] for r in result["readings"])) log(f"clock/v1: supported={result['supported']} synced={result['synced']} " @@ -819,7 +825,7 @@ async def _validate_clock( async def _read_agent_clock(context, prompt_id: str, at: str) -> dict: """The agent's ``date``, run through its harness, read against the frozen clock at ``at``.""" await PromptAgentTaskStep(id=prompt_id, version=None, prompt=CLOCK_PROBE_PROMPT, prompt_id=prompt_id, - timeout_seconds=180).execute(context) + timeout_seconds=CLOCK_PROBE_TURN_TIMEOUT_S).execute(context) reply = next((p.response or "" for p in context.prompt_responses if p.prompt_id == prompt_id), "") return {"prompt_id": prompt_id, **clock_reading(reply, at)} From 43fdb97c3b0f24e8e65d1d0c6bb01d397fbb95eb Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Tue, 17 Nov 2026 16:09:38 -0800 Subject: [PATCH 17/29] fix(gateway): get_time says the machine's clock may not be synchronized with the env An agent in an image that follows the env's clock reads the env's time, so "is not synchronized, so do not use it" is false for it. "May not be" holds for every agent, following or not, and get_time answers what a following agent's clock already shows. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/gateway/get_time.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent_env/env/gateway/get_time.py b/src/agent_env/env/gateway/get_time.py index 24767249f..fa66a1a5a 100644 --- a/src/agent_env/env/gateway/get_time.py +++ b/src/agent_env/env/gateway/get_time.py @@ -18,7 +18,7 @@ GET_TIME_TOOL_NAME = "get_time" GET_TIME_TOOL_DESCRIPTION = ( "Returns the current date and time for this environment. The clock on the machine you are " - "running on is not synchronized with this environment, so do not use it and do not rely on " + "running on may not be synchronized with this environment, so do not rely on it or on " "your own assumption about today's date. Call this tool and derive from its value every " "absolute date, time or timestamp you pass to another tool, and every relative phrase you " 'resolve ("today", "this week", "overdue", "the last 30 days"). current_time is in UTC, as ' From 8bf8e4ca5ed4a2e21568d1b2385a1be7c10c005b Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Tue, 17 Nov 2026 16:28:16 -0800 Subject: [PATCH 18/29] refactor(a2a-agent): move agent_clock to a2a_agent/clock.py, beside the other agent extension clients It drives an agent's clock/v1 sync_time and the gateway's follower declarations for the steps that call it (sync_env_clock, deploy_agent, teardown_sandboxes), as object_transfer does for snapshot transfers; it defines no step. The name mirrors agentenv_protocol.a2a_agent.clock, the agent's side of the same extension. The persisted metadata key agent_clock_following is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../task_steps/agent_clock.py => a2a_agent/clock.py} | 2 +- src/agent_env/task_step/task_steps/deploy_agent.py | 2 +- src/agent_env/task_step/task_steps/sync_env_clock.py | 9 +++++---- src/agent_env/task_step/task_steps/teardown_sandboxes.py | 2 +- tst/unit/task_step/agent_clock_test.py | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) rename src/agent_env/{task_step/task_steps/agent_clock.py => a2a_agent/clock.py} (99%) diff --git a/src/agent_env/task_step/task_steps/agent_clock.py b/src/agent_env/a2a_agent/clock.py similarity index 99% rename from src/agent_env/task_step/task_steps/agent_clock.py rename to src/agent_env/a2a_agent/clock.py index b0487c6e8..87156d198 100644 --- a/src/agent_env/task_step/task_steps/agent_clock.py +++ b/src/agent_env/a2a_agent/clock.py @@ -18,6 +18,7 @@ import httpx +from agent_env.a2a_agent.a2a_agent import A2AAgent from agent_env.env.env import EnvCapabilityUnsupported, gateway_url_of from agent_env.env.gateway.constants import CLOCK_WATCH_PATH, EXT_CLOCK_URI from agent_env.providers.sandbox_providers.local_docker.provider import host_url_for @@ -50,7 +51,6 @@ def follower_name(agent: DeployedAgent) -> str: def _sync_time(agent: DeployedAgent) -> tuple[Optional[dict], Optional[str]]: - from agent_env.a2a_agent import A2AAgent return A2AAgent.operation(A2AAgent.find_extension(agent.a2a_card or {}, EXT_CLOCK_URI), "sync_time") diff --git a/src/agent_env/task_step/task_steps/deploy_agent.py b/src/agent_env/task_step/task_steps/deploy_agent.py index c09520ec6..f5f16c2c9 100644 --- a/src/agent_env/task_step/task_steps/deploy_agent.py +++ b/src/agent_env/task_step/task_steps/deploy_agent.py @@ -547,7 +547,7 @@ async def _execute(self, context: TaskStepContext, deployed_agents: list) -> Tas env_ids=list(self.env_ids), )) # An agent deployed while a clock is armed follows it, as sync_env_clock had every agent then. - from agent_env.task_step.task_steps.agent_clock import follow_armed_clocks + from agent_env.a2a_agent.clock import follow_armed_clocks await follow_armed_clocks(context, context.deployed_agents[-1]) return context diff --git a/src/agent_env/task_step/task_steps/sync_env_clock.py b/src/agent_env/task_step/task_steps/sync_env_clock.py index 8e14aaf8d..d3f2a0777 100644 --- a/src/agent_env/task_step/task_steps/sync_env_clock.py +++ b/src/agent_env/task_step/task_steps/sync_env_clock.py @@ -6,8 +6,9 @@ An env whose card offers no set_time has no gateway clock to arm, as a computer deployed alone, and is refused saying to deploy it in a multi env; a record that kept no card is refused saying so. Then has every agent registered with the env whose card serves clock/v1 sync_time follow the clock as a computer does -(``agent_clock``), so every program it runs reads the env's time and later clock writes can wait for it. An agent that -can't is skipped, as every agent is until its image runs the follower, or fails the step with require_agent_clock. +(``agent_env.a2a_agent.clock``), so every program it runs reads the env's time and later clock writes can wait for it. +An agent that can't is skipped, as every agent is until its image runs the follower, or fails the step with +require_agent_clock. Non-advertisers are skipped when tolerate_missing_sync_time (default True), and websites always. Place right before prompt_agent so the virtual timeline starts at agent-start; when setup needs the clock armed, arm it at rate 0 and start it with set_env_clock_rate right before prompt_agent. @@ -191,9 +192,9 @@ async def execute(self, context: TaskStepContext) -> TaskStepContext: return context async def _follow_agents(self, context: TaskStepContext, deployed, env_get_time_url: Optional[str]) -> list[dict]: - """Have every agent registered with the env follow its clock (``agent_clock.follow``): one that can't is + """Have every agent registered with the env follow its clock (``a2a_agent.clock.follow``): one that can't is reported and skipped, or fails the step under ``require_agent_clock``.""" - from agent_env.task_step.task_steps.agent_clock import follow_in, torn_down + from agent_env.a2a_agent.clock import follow_in, torn_down outcomes = [] for agent in context.deployed_agents: diff --git a/src/agent_env/task_step/task_steps/teardown_sandboxes.py b/src/agent_env/task_step/task_steps/teardown_sandboxes.py index 4190c2497..0257566ce 100644 --- a/src/agent_env/task_step/task_steps/teardown_sandboxes.py +++ b/src/agent_env/task_step/task_steps/teardown_sandboxes.py @@ -27,7 +27,7 @@ tear_down, ) from agent_env.task_step.context import TaskStepContext -from agent_env.task_step.task_steps.agent_clock import retire_followers +from agent_env.a2a_agent.clock import retire_followers from agent_env.task_step.task_step import TaskStep, TaskStepDependency logger = logging.getLogger(__name__) diff --git a/tst/unit/task_step/agent_clock_test.py b/tst/unit/task_step/agent_clock_test.py index 576fcc9c3..089a28b24 100644 --- a/tst/unit/task_step/agent_clock_test.py +++ b/tst/unit/task_step/agent_clock_test.py @@ -10,7 +10,7 @@ from agent_env.env.gateway.constants import EXT_CLOCK_URI from agent_env.task_step.context import DeployedAgent, StepContextError, TaskStepContext -from agent_env.task_step.task_steps import agent_clock +from agent_env.a2a_agent import clock as agent_clock from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep from tst.unit.task_step.sync_env_clock_test import (CLOCK_URL, T0, _CARD, _CLOCK_EXT, _ZONE_EXT, _carded, _composed_card, _mock_http, _step) From 686fdf10afad791ca63e0d26efae777bf610fe17 Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 16:35:27 -0800 Subject: [PATCH 19/29] test(agents): the agent clock e2e imports FOLLOWING_KEY from a2a_agent.clock, where #714 moved it Co-Authored-By: Claude Opus 5.5 (1M context) --- .../task_step/test_agent_env_clock_local.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index 2de0c9fcb..e56b64d52 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -16,20 +16,25 @@ from datetime import datetime, timezone import pytest +from agentenv_protocol.client import ExtensionCallError from agent_env.a2a_agent.a2a_agent import A2AAgent +from agent_env.a2a_agent.clock import FOLLOWING_KEY from agent_env.artifact import DockerImageArtifact from agent_env.env.gateway.constants import EXT_CLOCK_URI -from agent_env.providers.sandbox_providers.local_docker.provider import _host_ips, host_url_for -from agentenv_protocol.client import ExtensionCallError +from agent_env.providers.sandbox_providers.local_docker.provider import ( + _host_ips, + host_url_for, +) from agent_env.task_step.context import TaskStepContext -from agent_env.task_step.task_steps.agent_clock import FOLLOWING_KEY from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep from agent_env.task_step.task_steps.sync_env_clock import SyncEnvClockTaskStep from agent_env.task_step.task_steps.teardown_sandboxes import TeardownSandboxesTaskStep from tst.integration.store.local_parallel_deploy_test import _seed_bootstrap_envs -from tst.integration.task_step.test_prompt_agent_live_trajectory_local import local_registry as local_registry +from tst.integration.task_step.test_prompt_agent_live_trajectory_local import ( + local_registry as local_registry, +) from tst.integration.task_step.test_register_env_triggers_local import _deploy_items from tst.util.capabilities import AGENT_CLOCK_IMAGE_ENV, skip_without_agent_clock_image from tst.util.fake_anthropic import FakeAnthropic, tool_then_answer From 1ff90bda11828c605ef3046ee6d8f81a254da42f Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Tue, 17 Nov 2026 17:44:56 -0800 Subject: [PATCH 20/29] fix(agents): the Claude Code image installs protocol 0.1.575, the first with an agent's clock/v1 sync_time 0.1.575 is the release that carries #714 (agentenv_protocol.a2a_agent.clock): before it, the image's protocol serves no clock/v1, so the agent advertises none and sync_env_clock skips it as not_advertised. The image is built again only when this folder changes, so the floor is what makes it take the release. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/agents/agents/claude-code/requirements.txt | 7 ++++--- tst/unit/agents/claude_code/test_image_contract.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/agent_env/agents/agents/claude-code/requirements.txt b/src/agent_env/agents/agents/claude-code/requirements.txt index 464574902..6053dbde7 100644 --- a/src/agent_env/agents/agents/claude-code/requirements.txt +++ b/src/agent_env/agents/agents/claude-code/requirements.txt @@ -1,4 +1,5 @@ # The image installs the newest agentenv-framework-protocol at or above this floor; a release doesn't pin it, so the -# image is built again only when this folder changes. The floor is the first that serves peer-agents/v1's tools, which -# the agent advertises without handlers of its own; raise it when the agent needs a newer protocol. -agentenv-framework-protocol[agent]>=0.1.351 +# image is built again only when this folder changes. The floor is the first that serves clock/v1's sync_time on an +# agent whose image runs the clock follower (agentenv_protocol.a2a_agent.clock), which this image does; raise it when +# the agent needs a newer protocol. +agentenv-framework-protocol[agent]>=0.1.575 diff --git a/tst/unit/agents/claude_code/test_image_contract.py b/tst/unit/agents/claude_code/test_image_contract.py index 3dc316cc5..0f20db804 100644 --- a/tst/unit/agents/claude_code/test_image_contract.py +++ b/tst/unit/agents/claude_code/test_image_contract.py @@ -27,7 +27,7 @@ def test_the_image_installs_the_protocol_alone_from_its_requirements(): requirements = [line for line in (AGENT_DIR / "requirements.txt").read_text().splitlines() if line and not line.startswith("#")] - assert requirements == ["agentenv-framework-protocol[agent]>=0.1.351"] + assert requirements == ["agentenv-framework-protocol[agent]>=0.1.575"] def test_the_image_gives_the_model_node_alone_from_a_pinned_image(): From ca97b379c9373aaf6e9b3f588e3d03a04abcbdaa Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Tue, 17 Nov 2026 17:53:55 -0800 Subject: [PATCH 21/29] fix(computers): the clock follower follows a changed config while its gateway refuses at once A watch that fails fast (connection refused) answers before the follower's quarter-second poll sees a changed config, and the retry backoff ends early on that change, so the follower kept retrying the old URL in a tight loop and never followed the new one: an agent re-synced to another gateway after its first one went away stayed on the old clock, burning a core, and every write that waited for it answered 504. It now checks the config after each failed watch. The base's, the browser base's and the Claude Code image's copies stay identical. Co-Authored-By: Claude Opus 5.5 --- .../agents/agents/claude-code/clock/clock-follower | 2 ++ .../ubuntu-24.04-browser/clock/clock-follower | 2 ++ .../artifacts/ubuntu-24.04/clock/clock-follower | 2 ++ tst/unit/env/envs/test_computer_clock_follower.py | 13 +++++++++++++ 4 files changed, 19 insertions(+) diff --git a/src/agent_env/agents/agents/claude-code/clock/clock-follower b/src/agent_env/agents/agents/claude-code/clock/clock-follower index 652431687..fee4586b7 100755 --- a/src/agent_env/agents/agents/claude-code/clock/clock-follower +++ b/src/agent_env/agents/agents/claude-code/clock/clock-follower @@ -141,6 +141,8 @@ class Follower: return # its watch is left to end on its own; nothing it answers is applied if error is not None: self.failed(error) + if read_config()[0] != self.raw: + return # a gateway that refuses at once answers every watch before the poll above sees a change continue if read_config()[0] != self.raw: return diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower index 652431687..fee4586b7 100755 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower @@ -141,6 +141,8 @@ class Follower: return # its watch is left to end on its own; nothing it answers is applied if error is not None: self.failed(error) + if read_config()[0] != self.raw: + return # a gateway that refuses at once answers every watch before the poll above sees a change continue if read_config()[0] != self.raw: return diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower index 652431687..fee4586b7 100755 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower @@ -141,6 +141,8 @@ class Follower: return # its watch is left to end on its own; nothing it answers is applied if error is not None: self.failed(error) + if read_config()[0] != self.raw: + return # a gateway that refuses at once answers every watch before the poll above sees a change continue if read_config()[0] != self.raw: return diff --git a/tst/unit/env/envs/test_computer_clock_follower.py b/tst/unit/env/envs/test_computer_clock_follower.py index 367a7af41..9b0a9b9e6 100644 --- a/tst/unit/env/envs/test_computer_clock_follower.py +++ b/tst/unit/env/envs/test_computer_clock_follower.py @@ -181,6 +181,19 @@ def test_a_gateway_it_cant_reach_is_named_in_its_status_and_it_keeps_trying(foll follower.until(lambda: follower.status().get("errors", 0) >= 2) +def test_a_changed_config_is_followed_while_the_gateway_it_follows_refuses_at_once(follower): + gateway = Gateway() + try: + follower.configure({"watch_url": "http://127.0.0.1:9/clock/watch", "name": "desktop", "token": "t"}) + follower.until(lambda: follower.status().get("errors", 0) >= 3) + + gateway.set("boot:1", datetime(2020, 1, 1, tzinfo=timezone.utc), 1.0) + follower.configure({"watch_url": gateway.url, "name": "desktop", "token": "t"}) + follower.until(lambda: follower.status().get("version") == "boot:1", within=3) + finally: + gateway.close() + + def test_a_config_that_isnt_one_is_named_and_a_good_one_then_followed(follower): gateway = Gateway() try: From 0875e06c29a241a3883383854102649d8363608c Mon Sep 17 00:00:00 2001 From: William Loo Date: Tue, 17 Nov 2026 19:44:38 -0800 Subject: [PATCH 22/29] fix(agents): an armed clock keeps the agent's own NO_PROXY hosts direct; name the egress probe's error limit - egress_env merges the hosts NO_PROXY and no_proxy already name with the direct ones, so a private registry the agent was deployed to reach directly isn't sent to the proxy, which refuses it, once the clock is armed. - egress/upstream.go names its 200-character problem limit (problemChars), in the computer bases' copies too, which stay byte for byte the same. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agents/agents/claude-code/claude_bridge.py | 6 ++++-- .../agents/agents/claude-code/egress/upstream.go | 9 ++++++--- .../artifacts/ubuntu-24.04-browser/egress/upstream.go | 9 ++++++--- .../artifacts/ubuntu-24.04/egress/upstream.go | 9 ++++++--- tst/unit/agents/claude_code/test_env_clock_image.py | 11 +++++++++++ 5 files changed, 33 insertions(+), 11 deletions(-) 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 642d24868..f447a43c0 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -217,11 +217,13 @@ def egress_env(environ: Mapping[str, str], direct: Iterable[str] = (), *, armed: the clock is months off, and the proxy, on real time, hands it a certificate that holds then. The env's MCP servers (``direct``) are reached directly: the proxy refuses an address that isn't global, as a local or private gateway's is. Unarmed, the CLI reads real time and goes out directly, as an image without the proxy does, so a model endpoint - or mirror at a private address keeps working.""" + or mirror at a private address keeps working. Hosts the agent was deployed to reach directly (``NO_PROXY``, + ``no_proxy``) stay direct.""" proxy = environ.get(EGRESS_PROXY_ENV) if not proxy or not armed: return {} - hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, direct)])) + configured = [host.strip() for name in ("NO_PROXY", "no_proxy") for host in (environ.get(name) or "").split(",")] + hosts = ",".join(dict.fromkeys([*_DIRECT_HOSTS, *filter(None, configured), *filter(None, direct)])) return {**{name: proxy for base in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY") for name in (base, base.lower())}, "NO_PROXY": hosts, "no_proxy": hosts, "NODE_USE_ENV_PROXY": "1"} diff --git a/src/agent_env/agents/agents/claude-code/egress/upstream.go b/src/agent_env/agents/agents/claude-code/egress/upstream.go index ccf32704f..bad2ea7db 100644 --- a/src/agent_env/agents/agents/claude-code/egress/upstream.go +++ b/src/agent_env/agents/agents/claude-code/egress/upstream.go @@ -182,11 +182,11 @@ func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) var unanswered dialError switch { case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), 200) + hit.unreachable, hit.problem = true, truncate(err.Error(), problemChars) case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), 200) + hit.failed, hit.problem = true, truncate(err.Error(), problemChars) case !found.Verified: - hit.problem = truncate(found.VerifyErr, 200) + hit.problem = truncate(found.VerifyErr, problemChars) default: hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true } @@ -260,6 +260,9 @@ func (p *Proxy) dial(dst string) (net.Conn, error) { return net.DialTimeout("tcp", dst, DialTimeout) } +// problemChars is the most of an upstream's error a probe keeps as its problem. +const problemChars = 200 + func truncate(s string, n int) string { if len(s) > n { return s[:n] diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go index ccf32704f..bad2ea7db 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go @@ -182,11 +182,11 @@ func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) var unanswered dialError switch { case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), 200) + hit.unreachable, hit.problem = true, truncate(err.Error(), problemChars) case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), 200) + hit.failed, hit.problem = true, truncate(err.Error(), problemChars) case !found.Verified: - hit.problem = truncate(found.VerifyErr, 200) + hit.problem = truncate(found.VerifyErr, problemChars) default: hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true } @@ -260,6 +260,9 @@ func (p *Proxy) dial(dst string) (net.Conn, error) { return net.DialTimeout("tcp", dst, DialTimeout) } +// problemChars is the most of an upstream's error a probe keeps as its problem. +const problemChars = 200 + func truncate(s string, n int) string { if len(s) > n { return s[:n] diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go index ccf32704f..bad2ea7db 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go @@ -182,11 +182,11 @@ func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) var unanswered dialError switch { case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), 200) + hit.unreachable, hit.problem = true, truncate(err.Error(), problemChars) case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), 200) + hit.failed, hit.problem = true, truncate(err.Error(), problemChars) case !found.Verified: - hit.problem = truncate(found.VerifyErr, 200) + hit.problem = truncate(found.VerifyErr, problemChars) default: hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true } @@ -260,6 +260,9 @@ func (p *Proxy) dial(dst string) (net.Conn, error) { return net.DialTimeout("tcp", dst, DialTimeout) } +// problemChars is the most of an upstream's error a probe keeps as its problem. +const problemChars = 200 + func truncate(s string, n int) string { if len(s) > n { return s[:n] 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 883ba935a..67b36da37 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -79,6 +79,17 @@ def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy_and_to_the_envs_se assert env["NODE_USE_ENV_PROXY"] == "1" +def test_hosts_the_agent_was_deployed_to_reach_directly_stay_direct_on_an_armed_clock(): + # A private registry the agent's NO_PROXY names worked before the clock was armed; the proxy would refuse it. + env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "NO_PROXY": "registry.internal, .corp", + "no_proxy": "mirror.internal"}, direct_hosts=["gateway"], clock_armed=True) + + hosts = env["NO_PROXY"].split(",") + assert env["no_proxy"] == env["NO_PROXY"] + assert {"registry.internal", ".corp", "mirror.internal", "gateway", "localhost"} <= set(hosts) + assert len(hosts) == len(set(hosts)) and "" not in hosts + + @pytest.mark.parametrize("base_url, direct", [ ("http://10.0.0.5:4000", True), # plain HTTP, private: nothing to check at the env's date, and the proxy refuses it ("https://litellm.example.com", False), # TLS: its certificate is checked through the proxy, on real time From bb4f07f4c1f9d59a7ea445246838c86d669349cd Mon Sep 17 00:00:00 2001 From: William Loo Date: Wed, 18 Nov 2026 02:34:53 -0800 Subject: [PATCH 23/29] test(agents): the agent clock checks run over every clocked agent image, not Claude Code's alone - tst/unit/agents/test_agent_clock_images.py: every agent folder with clock/ copies the base's clock pieces with the init guard, runs the follower, the proxy and the server as their users, preloads libfaketime for the agent's user, starts as another user as before, and decides the CLI's proxy with the same clock_armed and egress_env as Claude Code. - The local e2e runs over each image AGENTENV_E2E_AGENT_IMAGES names (claude-code=,...), with a CLI turn for an agent whose CLI the stub endpoint speaks to. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../task_step/test_agent_env_clock_local.py | 92 ++++++++++------- .../claude_code/test_env_clock_image.py | 62 +----------- tst/unit/agents/test_agent_clock_images.py | 98 +++++++++++++++++++ tst/util/capabilities.py | 19 ++-- 4 files changed, 171 insertions(+), 100 deletions(-) create mode 100644 tst/unit/agents/test_agent_clock_images.py diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index e56b64d52..71df2d32e 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -1,18 +1,20 @@ -"""The Claude Code image on a task env's clock (FD-5916), end to end on local Docker: the agent deployed with the env -serves clock/v1 sync_time; sync_env_clock declares it a follower at the real gateway and hands it its config; from then -on every program the agent's user runs reads the env's time while the A2A server stays on real time; a clock write that -waits for followers waits for it; and once teardown_sandboxes takes it down, writes stop waiting for it. +"""An agent image on a task env's clock (FD-5916), end to end on local Docker: the agent deployed with the env serves +clock/v1 sync_time; sync_env_clock declares it a follower at the real gateway and hands it its config; from then on +every program the agent's user runs, its CLI included, reads the env's time while the A2A server stays on real time; a +clock write that waits for followers waits for it; and once teardown_sandboxes takes it down, writes stop waiting for +it. An agent whose CLI speaks a protocol the stub endpoint serves also runs one real turn. -Runs over an image tag built beforehand (AGENTENV_E2E_CLAUDE_CODE_IMAGE), as the shipped agent's image installs the -protocol from PyPI, which has no CLOCK_V1 until a release.""" +Runs over images built beforehand, from this branch: AGENTENV_E2E_AGENT_IMAGES names one per agent, as +``claude-code=,codex=``; an agent it doesn't name is skipped as a missing capability.""" import asyncio import json -import os import re import subprocess import time import uuid +from collections.abc import Awaitable, Callable +from dataclasses import dataclass from datetime import datetime, timezone import pytest @@ -36,12 +38,11 @@ local_registry as local_registry, ) from tst.integration.task_step.test_register_env_triggers_local import _deploy_items -from tst.util.capabilities import AGENT_CLOCK_IMAGE_ENV, skip_without_agent_clock_image +from tst.util.capabilities import agent_clock_image, skip_without_agent_clock_image from tst.util.fake_anthropic import FakeAnthropic, tool_then_answer pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] -IMAGE = os.environ.get(AGENT_CLOCK_IMAGE_ENV, "") VIRTUAL = "2031-03-04T05:06:07Z" LATER = "2031-03-05T10:00:00Z" PAUSED = "2031-03-05T12:00:00Z" @@ -53,14 +54,44 @@ def _in_agent(container: str, *command: str, real_time: bool = False) -> str: text=True, check=True, timeout=30).stdout.strip() -def _routes_through_proxy(container: str) -> bool: - """Whether the CLI the agent starts now goes out through the egress proxy, as the image's bridge decides.""" +def _routes_through_proxy(container: str, bridge: str) -> bool: + """Whether the CLI the agent starts now goes out through the egress proxy, as the image's bridge decides: every + bridge has the same ``clock_armed`` and ``egress_env``.""" # The A2A server's environment has the proxy (root.conf); this shell's doesn't, so it's passed as the server has it. - return _in_agent(container, "python3", "-c", "import claude_bridge as b; print('HTTPS_PROXY' in b.cli_env(" - "{b.EGRESS_PROXY_ENV: 'http://127.0.0.1:3128'}, clock_armed=b.clock_armed()))", + return _in_agent(container, "python3", "-c", f"import {bridge} as b; print('HTTPS_PROXY' in b.egress_env(" + "{b.EGRESS_PROXY_ENV: 'http://127.0.0.1:3128'}, armed=b.clock_armed()))", real_time=True) == "True" +async def _claude_turn(context: TaskStepContext, endpoint: FakeAnthropic) -> None: + """One turn of the real Claude Code CLI: the date it gives the model and a Bash `date` the model runs are the env's.""" + endpoint.respond = tool_then_answer("Bash", {"command": "date -u +%FT%TZ"}, prefix="the shell said: ") + await PromptAgentTaskStep(id="ask", version=None, prompt="What's the date? Run date.", agent_name="solver", + max_conversation_turns=1).execute(context) + bodies = [json.dumps(call.body) for call in endpoint.messages()] + dated = {m for body in bodies for m in re.findall(r"Today's date is ([0-9]{4}-[0-9]{2}-[0-9]{2})", body)} + print("the CLI's date line:", dated, "Bash date:", endpoint.tool_results()) + assert dated == {VIRTUAL[:10]}, dated + assert any(VIRTUAL in result for result in endpoint.tool_results()), endpoint.tool_results() + + +@dataclass(frozen=True) +class Agent: + """What the test needs of an agent's image: its bridge module, a command its CLI answers without a model (which + must return under the preloaded library), and a turn against the stub endpoint, for a CLI that speaks its protocol.""" + + bridge: str + version: tuple[str, ...] + shows: str + turn: Callable[[TaskStepContext, FakeAnthropic], Awaitable[None]] | None = None + + +AGENTS = { + "claude-code": Agent(bridge="claude_bridge", version=("claude", "--version"), shows="(Claude Code)", + turn=_claude_turn), +} + + def _container_of(sandbox_id: str) -> str: names = subprocess.run(["docker", "ps", "--format", "{{.Names}}"], capture_output=True, text=True, check=True).stdout.split() @@ -72,19 +103,19 @@ async def _followers(deployed) -> dict: return (await deployed.invoke(EXT_CLOCK_URI, "state", {}, timeout=10)).get("followers") or {} -@skip_without_agent_clock_image() +@pytest.mark.parametrize("name", [pytest.param(name, marks=skip_without_agent_clock_image(name)) for name in AGENTS]) @pytest.mark.asyncio -async def test_the_claude_code_image_follows_the_envs_clock_until_it_is_torn_down(local_registry): +async def test_an_agent_image_follows_the_envs_clock_until_it_is_torn_down(local_registry, name): with FakeAnthropic(host=_host_ips()[-1]) as endpoint: # where this machine's containers reach it - await _follows_until_torn_down(endpoint) + await _follows_until_torn_down(name, AGENTS[name], agent_clock_image(name), endpoint) -async def _follows_until_torn_down(endpoint: FakeAnthropic) -> None: +async def _follows_until_torn_down(name: str, spec: Agent, tag: str, endpoint: FakeAnthropic) -> None: _seed_bootstrap_envs() - env, deployed, _ = await _deploy_items("ccclock") + env, deployed, _ = await _deploy_items("agentclock") uid = uuid.uuid4().hex[:8] - image = DockerImageArtifact.put(id=f"cc-clock-img-{uid}", description="e2e", image_name=IMAGE) - agent = A2AAgent.put(id=f"cc-clock-{uid}", docker_image_artifact=image, + image = DockerImageArtifact.put(id=f"{name}-clock-img-{uid}", description="e2e", image_name=tag) + agent = A2AAgent.put(id=f"{name}-clock-{uid}", docker_image_artifact=image, default_env_vars={"LITELLM_API_KEY": "fake-key", # as the agent's container reaches it: on macOS, loopback is the host's "LITELLM_BASE_URL": host_url_for(endpoint.url, "local")}) @@ -95,9 +126,9 @@ async def _follows_until_torn_down(endpoint: FakeAnthropic) -> None: sandbox_type="local").execute(context) container = _container_of(context.deployed_agents[-1].sandbox_id) assert _in_agent(container, "date", "-u", "+%Y") == str(datetime.now(timezone.utc).year) # not armed: real - assert not _routes_through_proxy(container) # and the CLI goes out directly, as before - # The CLI starts under the preloaded library: without its init guard, its first clock read deadlocked. - assert "(Claude Code)" in _in_agent(container, "timeout", "30", "claude", "--version") + assert not _routes_through_proxy(container, spec.bridge) # and the CLI goes out directly, as before + # The CLI starts under the preloaded library: without its init guard, Claude Code's first clock read deadlocked. + assert spec.shows in _in_agent(container, "timeout", "30", *spec.version) await SyncEnvClockTaskStep(id="clock", version=None, env_id=env.id, virtual_time=VIRTUAL, virtual_seconds_per_real_second=0, require_agent_clock=True, @@ -117,17 +148,10 @@ async def _follows_until_torn_down(endpoint: FakeAnthropic) -> None: print("agent reads:", shown) assert shown["date"] == VIRTUAL and shown["python"].startswith("2031-03-04T05:06:07") assert shown["real"].startswith(str(datetime.now(timezone.utc).year)) - assert _routes_through_proxy(container) # armed: live sites' TLS goes through the proxy, on real time - - # One turn of the real CLI: the date it gives the model and a Bash `date` the model runs are the env's. - endpoint.respond = tool_then_answer("Bash", {"command": "date -u +%FT%TZ"}, prefix="the shell said: ") - await PromptAgentTaskStep(id="ask", version=None, prompt="What's the date? Run date.", agent_name="solver", - max_conversation_turns=1).execute(context) - bodies = [json.dumps(call.body) for call in endpoint.messages()] - dated = {m for body in bodies for m in re.findall(r"Today's date is ([0-9]{4}-[0-9]{2}-[0-9]{2})", body)} - print("the CLI's date line:", dated, "Bash date:", endpoint.tool_results()) - assert dated == {VIRTUAL[:10]}, dated - assert any(VIRTUAL in result for result in endpoint.tool_results()), endpoint.tool_results() + assert _routes_through_proxy(container, spec.bridge) # armed: live sites' TLS goes through the proxy + assert spec.shows in _in_agent(container, "timeout", "30", *spec.version) # the CLI starts on the env's clock + if spec.turn is not None: + await spec.turn(context, endpoint) # A write that waits for followers waits for the agent, which then shows it at once. started = time.monotonic() 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 67b36da37..98f45b681 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -1,75 +1,19 @@ -"""Claude Code on the env's clock (FD-5916): the image runs the computer base's clock follower and egress proxy, each -as its own user, and agent-env's libfaketime for the agent's user, so the CLI and every command the model runs read -the env's time once sync_env_clock has the agent follow; the A2A server stays on real time. While the clock is armed -the CLI goes out through the egress proxy, and to the env's MCP servers directly; unarmed, it goes out as before.""" +"""Claude Code's bridge on the env's clock (FD-5916): while the clock page is armed, the CLI goes out through the +egress proxy, and to the env's MCP servers, the hosts NO_PROXY names and a plain-HTTP model endpoint directly; unarmed, +it goes out as before. What every clocked image shares is checked in tst/unit/agents/test_agent_clock_images.py.""" from __future__ import annotations import json -import re import types import pytest from tst.unit.agents.claude_code.agent import AGENT_DIR, claude_bridge -DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() -ROOT_CONF = (AGENT_DIR / "root.conf").read_text() -BASE = AGENT_DIR.parents[2] / "computers" / "artifacts" / "ubuntu-24.04" PROXY = "http://127.0.0.1:3128" -@pytest.mark.parametrize("folder", ["clock", "egress"]) -def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(folder): - copied = sorted(p.relative_to(AGENT_DIR / folder) for p in (AGENT_DIR / folder).rglob("*") if p.is_file()) - - assert copied - assert [str(p) for p in copied if str(p) != "libfaketime.patch" - and (BASE / folder / p).read_bytes() != (AGENT_DIR / folder / p).read_bytes()] == [] - - -def test_the_agents_libfaketime_patch_is_the_bases_with_the_init_guard_after_it(): - # The Claude Code CLI reads the clock while libfaketime's init runs (an allocator, as dlsym allocates), and - # pthread_once then waits on itself: `claude --version` never returns. The guard has a clock call made during the - # init read the kernel's clock. The base doesn't take it yet, so the agent's copy is the base's patch, then the guard. - base, agent = (BASE / "clock" / "libfaketime.patch").read_text(), (AGENT_DIR / "clock" / "libfaketime.patch").read_text() - guard = agent.removeprefix(base) - - assert agent.startswith(base) and guard.startswith("diff -ruN a/src/libfaketime.c b/src/libfaketime.c\n") - assert "static __thread int ftpl_initializing = 0;" in guard - assert "if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp);" in guard - assert "pthread_once(&initialized_once_control, ftpl_really_init_guarded);" in guard - - -def _program(name: str) -> dict[str, str]: - section = re.search(rf"^\[program:{name}\]\n(.*?)(?=^\[|\Z)", ROOT_CONF, re.MULTILINE | re.DOTALL).group(1) - return dict(line.split("=", 1) for line in section.splitlines() if "=" in line and not line.startswith(";")) - - -def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as_the_agents_on_real_time(): - assert (_program("clock")["user"], _program("egress")["user"]) == ("agentenv-clock", "agentenv-egress") - agent = _program("agent") - assert agent["user"] == "agent" - # uvicorn itself, so the marker reaches the server and not a shell that drops it on exec. - assert agent["command"].startswith("/usr/local/bin/python3 -m uvicorn a2a_server:app") - assert 'AGENTENV_REAL_TIME="1"' in agent["environment"] - assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in agent["environment"] - assert 'NODE_EXTRA_CA_CERTS="/etc/agentenv/ca.pem"' in agent["environment"] - - -def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(): - assert "> /etc/ld.so.preload" in DOCKERFILE and "libfaketimeMT.so.1" in DOCKERFILE - assert 's/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/' in DOCKERFILE - assert "install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock" in DOCKERFILE - assert not re.search(r"^USER ", DOCKERFILE, re.MULTILINE) - - -def test_started_as_another_user_the_image_runs_the_server_alone_as_before(): - start = (AGENT_DIR / "start-agent").read_text() - assert 'if [ "$(id -u)" != 0 ]; then\n exec /opt/agent/entrypoint.sh' in start - assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start - - def test_on_an_armed_clock_the_cli_goes_out_through_the_proxy_and_to_the_envs_servers_directly(): env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, direct_hosts=["gateway", "", "gateway"], clock_armed=True) diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py new file mode 100644 index 000000000..04510a812 --- /dev/null +++ b/tst/unit/agents/test_agent_clock_images.py @@ -0,0 +1,98 @@ +"""Every agent image on the env's clock (FD-5916), whichever agent it is: each folder with ``clock/`` copies the +computer base's clock follower and egress proxy (``clock/``, ``egress/``) until the clock library image (FD-5700) +ships them, with libfaketime's init guard after the base's patch; runs the follower and the proxy, each as its own +user, and the A2A server as the agent's user on real time, under root's supervisord; preloads agent-env's libfaketime +for the agent's user alone; starts as another user as it did before; and its bridge decides the CLI's proxy with the +same ``clock_armed`` and ``egress_env`` as every other agent's.""" + +from __future__ import annotations + +import ast +import re +from pathlib import Path + +import pytest + +AGENTS_DIR = Path(__file__).parents[3] / "src" / "agent_env" / "agents" / "agents" +BASE = AGENTS_DIR.parents[1] / "computers" / "artifacts" / "ubuntu-24.04" +CLOCKED = sorted(p.parent.name for p in AGENTS_DIR.glob("*/clock") if p.is_dir()) +PROXY = "http://127.0.0.1:3128" + + +def test_claude_code_follows_the_clock(): + assert "claude-code" in CLOCKED + + +@pytest.mark.parametrize("agent", CLOCKED) +@pytest.mark.parametrize("folder", ["clock", "egress"]) +def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(agent, folder): + here = AGENTS_DIR / agent / folder + copied = sorted(p.relative_to(here) for p in here.rglob("*") if p.is_file()) + + assert copied + assert [str(p) for p in copied if str(p) != "libfaketime.patch" + and (BASE / folder / p).read_bytes() != (here / p).read_bytes()] == [] + + +@pytest.mark.parametrize("agent", CLOCKED) +def test_the_agents_libfaketime_patch_is_the_bases_with_the_init_guard_after_it(agent): + # A CLI that reads the clock while libfaketime's init runs (an allocator, as dlsym allocates) has pthread_once wait + # on itself: Claude Code's `--version` never returned. The guard has a clock call made during the init read the + # kernel's clock. The base doesn't take it yet, so the agent's copy is the base's patch, then the guard. + base = (BASE / "clock" / "libfaketime.patch").read_text() + patch = (AGENTS_DIR / agent / "clock" / "libfaketime.patch").read_text() + guard = patch.removeprefix(base) + + assert patch.startswith(base) and guard.startswith("diff -ruN a/src/libfaketime.c b/src/libfaketime.c\n") + assert "static __thread int ftpl_initializing = 0;" in guard + assert "if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp);" in guard + assert "pthread_once(&initialized_once_control, ftpl_really_init_guarded);" in guard + + +def _program(agent: str, name: str) -> dict[str, str]: + conf = (AGENTS_DIR / agent / "root.conf").read_text() + section = re.search(rf"^\[program:{name}\]\n(.*?)(?=^\[|\Z)", conf, re.MULTILINE | re.DOTALL).group(1) + return dict(line.split("=", 1) for line in section.splitlines() if "=" in line and not line.startswith(";")) + + +@pytest.mark.parametrize("agent", CLOCKED) +def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as_the_agents_on_real_time(agent): + assert (_program(agent, "clock")["user"], _program(agent, "egress")["user"]) == ("agentenv-clock", "agentenv-egress") + server = _program(agent, "agent") + assert server["user"] == "agent" + # The server's own interpreter, so the marker reaches it, not a shell that drops it on exec. + assert server["command"].split()[0].rsplit("/", 1)[-1].startswith("python") + assert 'AGENTENV_REAL_TIME="1"' in server["environment"] + assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in server["environment"] + + +@pytest.mark.parametrize("agent", CLOCKED) +def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(agent): + dockerfile = (AGENTS_DIR / agent / "Dockerfile").read_text() + assert "> /etc/ld.so.preload" in dockerfile and "libfaketimeMT.so.1" in dockerfile + assert 's/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/' in dockerfile + assert "install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock" in dockerfile + assert 'CMD ["/usr/local/bin/start-agent"]' in dockerfile + assert not re.search(r"^USER ", dockerfile, re.MULTILINE) + + +@pytest.mark.parametrize("agent", CLOCKED) +def test_started_as_another_user_the_image_runs_the_server_alone_as_before(agent): + start = (AGENTS_DIR / agent / "start-agent").read_text() + assert 'if [ "$(id -u)" != 0 ]; then\n exec ' in start + assert "AGENTENV_SESSION_USER=agent AGENTENV_EGRESS_NSS=off" in start + + +def _clock_helpers(agent: str) -> dict[str, str]: + """The bridge's clock helpers, as source: the one module in the agent's folder that defines ``egress_env``.""" + [module] = [p for p in (AGENTS_DIR / agent).glob("*.py") if "def egress_env(" in p.read_text()] + tree = ast.parse(module.read_text()) + names = {"clock_armed", "egress_env"} + found = {node.name: ast.unparse(node) for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in names} + assert set(found) == names, (module, sorted(found)) + return found + + +@pytest.mark.parametrize("agent", CLOCKED) +def test_every_bridge_decides_the_clis_proxy_as_claude_codes_does(agent): + assert _clock_helpers(agent) == _clock_helpers("claude-code") diff --git a/tst/util/capabilities.py b/tst/util/capabilities.py index f53213cc3..475883962 100644 --- a/tst/util/capabilities.py +++ b/tst/util/capabilities.py @@ -78,10 +78,10 @@ #: Composing an Omarchy computer builds its base from Omarchy's own VM image, which a run fetches into this machine's #: cache once, about 4 GB; a test that composes one needs it there already, and KVM, which boots linux/amd64 disks. OMARCHY_IMAGE = "omarchy_image" -#: The agent clock end to end (FD-5916) runs over a Claude Code image built from the branch beforehand, named by -#: ``AGENTENV_E2E_CLAUDE_CODE_IMAGE``: no CI job builds agent images, and the shipped one installs the protocol from PyPI. +#: The agent clock end to end (FD-5916) runs over agent images built from the branch beforehand, which +#: ``AGENTENV_E2E_AGENT_IMAGES`` names one per agent (``claude-code=,codex=``): no CI job builds agent images. AGENT_CLOCK_IMAGE = "agent_clock_image" -AGENT_CLOCK_IMAGE_ENV = "AGENTENV_E2E_CLAUDE_CODE_IMAGE" +AGENT_CLOCK_IMAGES_ENV = "AGENTENV_E2E_AGENT_IMAGES" #: The shipped Omarchy base, whose source the capability looks for. OMARCHY_ARTIFACT = Path(__file__).parents[2] / "src" / "agent_env" / "computers" / "artifacts" / "omarchy-4.5" / "artifact.toml" @@ -245,10 +245,15 @@ def omarchy_image_is_available() -> bool: return "linux/amd64" in LocalSandboxProvider.disk_image_platforms() and source_path(sha256).exists() -def skip_without_agent_clock_image() -> pytest.MarkDecorator: - """Collection-time ``skipif`` for tests that run over a Claude Code image built with the agent clock.""" - return pytest.mark.skipif(not os.environ.get(AGENT_CLOCK_IMAGE_ENV), - reason=missing_capability_reason(AGENT_CLOCK_IMAGE)) +def agent_clock_image(agent: str) -> str: + """The image ``AGENTENV_E2E_AGENT_IMAGES`` names for ``agent``, or ''.""" + named = dict(entry.split("=", 1) for entry in os.environ.get(AGENT_CLOCK_IMAGES_ENV, "").split(",") if "=" in entry) + return named.get(agent, "").strip() + + +def skip_without_agent_clock_image(agent: str) -> pytest.MarkDecorator: + """Collection-time ``skipif`` for a test that runs over ``agent``'s image built with the agent clock.""" + return pytest.mark.skipif(not agent_clock_image(agent), reason=missing_capability_reason(AGENT_CLOCK_IMAGE)) def skip_without_omarchy_image() -> pytest.MarkDecorator: From 3c60fda1706f81bad432c2cc8c2d49df8b13ca0f Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Wed, 18 Nov 2026 09:17:45 -0800 Subject: [PATCH 24/29] refactor(agents): the Claude Code image takes the clock and egress proxy from utils, and every image gets the init guard The image drops its clock/ and egress/ copies for a shared_sources.toml naming utils/faketime_clock and utils/egress_proxy, as the bases do; its Dockerfile still copies from clock/ and egress/. libfaketime's init guard, which only this image's patch had, is in the one patch now, so the computer bases build it too. The agent clock tests check that each clocked image takes the shared folders and that the shared patch has the guard. The clock and egress READMEs say which images take them, and lose links into the base's folder that no longer resolved. Co-Authored-By: Claude Opus 5.5 --- .github/dependabot.yml | 4 +- src/agent_env/agents/README.md | 5 +- .../agents/agents/claude-code/Dockerfile | 6 +- .../agents/agents/claude-code/clock/COPYING | 339 ---- .../claude-code/clock/agentenv_clock_page.c | 320 ---- .../claude-code/clock/agentenv_clock_waits.c | 1175 ------------- .../agents/agents/claude-code/clock/build.sh | 22 - .../agents/claude-code/clock/clock-follower | 206 --- .../agents/claude-code/clock/clock.conf | 11 - .../agents/claude-code/clock/clockpage.py | 157 -- .../claude-code/clock/libfaketime.patch | 1565 ----------------- .../agents/claude-code/clock/tls_realtime.c | 115 -- .../agents/agents/claude-code/egress/boot.go | 294 ---- .../agents/claude-code/egress/boot_test.go | 99 -- .../agents/agents/claude-code/egress/clock.go | 151 -- .../agents/claude-code/egress/clock_test.go | 112 -- .../agents/agents/claude-code/egress/go.mod | 12 - .../agents/agents/claude-code/egress/go.sum | 10 - .../agents/agents/claude-code/egress/hello.go | 164 -- .../agents/claude-code/egress/hello_test.go | 82 - .../agents/claude-code/egress/leaves.go | 188 -- .../agents/agents/claude-code/egress/main.go | 117 -- .../agents/claude-code/egress/main_test.go | 27 - .../agents/agents/claude-code/egress/proxy.go | 598 ------- .../agents/claude-code/egress/proxy_test.go | 564 ------ .../egress/testdata/page-armed.hex | 1 - .../egress/testdata/page-unarmed.hex | 1 - .../agents/claude-code/egress/upstream.go | 271 --- .../agents/claude-code/shared_sources.toml | 4 + src/agent_env/utils/egress_proxy/README.md | 13 +- src/agent_env/utils/faketime_clock/README.md | 14 +- .../utils/faketime_clock/libfaketime.patch | 38 + .../claude_code/test_env_clock_image.py | 5 +- tst/unit/agents/test_agent_clock_images.py | 48 +- 34 files changed, 90 insertions(+), 6648 deletions(-) delete mode 100644 src/agent_env/agents/agents/claude-code/clock/COPYING delete mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c delete mode 100644 src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c delete mode 100755 src/agent_env/agents/agents/claude-code/clock/build.sh delete mode 100755 src/agent_env/agents/agents/claude-code/clock/clock-follower delete mode 100644 src/agent_env/agents/agents/claude-code/clock/clock.conf delete mode 100644 src/agent_env/agents/agents/claude-code/clock/clockpage.py delete mode 100644 src/agent_env/agents/agents/claude-code/clock/libfaketime.patch delete mode 100644 src/agent_env/agents/agents/claude-code/clock/tls_realtime.c delete mode 100644 src/agent_env/agents/agents/claude-code/egress/boot.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/boot_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/clock.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/clock_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/go.mod delete mode 100644 src/agent_env/agents/agents/claude-code/egress/go.sum delete mode 100644 src/agent_env/agents/agents/claude-code/egress/hello.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/hello_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/leaves.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/main.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/main_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/proxy_test.go delete mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex delete mode 100644 src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex delete mode 100644 src/agent_env/agents/agents/claude-code/egress/upstream.go create mode 100644 src/agent_env/agents/agents/claude-code/shared_sources.toml diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 910541a4e..5c0de2b4f 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -70,8 +70,8 @@ updates: groups: infra-base-images: patterns: ["*"] - # The base computers' egress proxy, a Go program their Dockerfiles build: its modules' fixes, uTLS's and - # golang.org/x/crypto's among them. A bump changes the bases' sources, so the next release rebuilds and publishes them. + # The egress proxy, a Go program the base computers' and the Claude Code image's Dockerfiles build: its modules' fixes, + # uTLS's and golang.org/x/crypto's among them. A bump changes their sources, so the next release rebuilds them. # The browser base's copy is the desktop's, byte for byte (a unit test checks it), so one grouped pull request bumps # both. - package-ecosystem: "gomod" diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index 9e94269b0..787a5fabf 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -35,8 +35,9 @@ turn off becomes `--disallowedTools`, which keeps the CLI's defaults for the res when the Dockerfile's `CLAUDE_CODE_VERSION` moves, until `ClaudeCodeTool` and `TOOLS_CLI_VERSION` move with it. The image follows a task env's clock the way a computer does (FD-5916): it runs the computer base's clock follower -and egress proxy, copied into `clock/` and `egress/` until the clock library image (FD-5700) ships them, each as its own -user under root's supervisord (`start-agent`, `root.conf`), with agent-env's libfaketime preloaded for the agent's user. +and egress proxy, taken from `utils/faketime_clock` and `utils/egress_proxy` through its `shared_sources.toml`, each as +its own user under root's supervisord (`start-agent`, `root.conf`), with agent-env's libfaketime preloaded for the +agent's user. The A2A server starts with the real-time marker, so it and its records stay on real time, and serves clock/v1's `sync_time`, which the protocol's framework serves on the card of any agent in an image with the follower (`agentenv_protocol.a2a_agent.clock`). Once `sync_env_clock` has armed the clock, it declares the agent at the gateway diff --git a/src/agent_env/agents/agents/claude-code/Dockerfile b/src/agent_env/agents/agents/claude-code/Dockerfile index 789bfd983..b56404d44 100644 --- a/src/agent_env/agents/agents/claude-code/Dockerfile +++ b/src/agent_env/agents/agents/claude-code/Dockerfile @@ -1,9 +1,9 @@ # Node for the scripts the model runs, without npm or npx; Claude Code itself is a native build. FROM public.ecr.aws/docker/library/node:22-slim@sha256:c3de60bf2f9dd0ac6370e6117950ff62d6e339527e7472301c9c78a017978392 AS node -# The env's clock (FD-5916): the computer base's pieces, copied in (clock/, egress/) until the clock library image -# (FD-5700) ships them for each libc. The egress proxy, one static Go binary, and agent-env's libfaketime and its TLS -# shim, built for this image's libc (Debian bookworm) in stages of their own so no toolchain is left in the image. +# The env's clock: the egress proxy, one static Go binary, and agent-env's libfaketime and its TLS shim, taken from +# agent-env's sources as clock/ and egress/ (shared_sources.toml) and built for this image's libc (Debian bookworm) in +# stages of their own, so no toolchain is left in the image. FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress COPY egress/ /egress/ RUN cd /egress && go vet ./... && go test -count=1 ./... \ diff --git a/src/agent_env/agents/agents/claude-code/clock/COPYING b/src/agent_env/agents/agents/claude-code/clock/COPYING deleted file mode 100644 index d159169d1..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/COPYING +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c deleted file mode 100644 index 9167f7ede..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_page.c +++ /dev/null @@ -1,320 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 1, included by libfaketime.c (libfaketime.patch) before its - * time functions: the process's settings, read once at load from /etc/agentenv/clock.conf, or from the environment - * where there is no such file, so no getenv() runs after main() starts; who is faked (the session user, unless started with AGENTENV_REAL_TIME=1, a marker - * taken out of the environment block in place); and the clock page the follower writes, read without a cache. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * configuration is read once, when the library loads, from AGENTENV_CLOCK_CONF (NAME=VALUE lines) when that - * file exists, else from the environment, and never with getenv() afterwards: a getenv() racing a program's setenv() - * reads a freed environ array, which is how libfaketime's pthread_cond_timedwait crashed Chromium. - */ -#ifndef AGENTENV_CLOCK_CONF -#define AGENTENV_CLOCK_CONF "/etc/agentenv/clock.conf" -#endif -#define FT_CONF_MAX 64 -static char ft_conf_text[8192]; -static const char *ft_conf_name[FT_CONF_MAX]; -static const char *ft_conf_value[FT_CONF_MAX]; -static int ft_conf_count = 0; -static bool ft_conf_loaded = false; -/* this process stays on real time: another user's than AGENTENV_SESSION_UID, or started with - AGENTENV_REAL_TIME=1. */ -static bool ft_disabled = false; -/* bumped with each new time specification parsed, so a wait sees the clock change under it. */ -static volatile unsigned long ft_spec_gen = 0; -static __thread unsigned long ft_tl_gen = 0; -/* real seconds per fake second for what is left of a wait, as of this thread's last clock read: 1 / rate, - or 1 for a frozen clock. */ -static __thread double ft_tl_scale = 1.0; -/* how long a wait that can be cut sleeps before it looks at the clock again (AGENTENV_WAIT_SLICE_MS). */ -static long ft_wait_slice_ns = 100000000L; - -static void ft_conf_add(char *name, char *value) -{ - if (ft_conf_count < FT_CONF_MAX) - { - ft_conf_name[ft_conf_count] = name; - ft_conf_value[ft_conf_count] = value; - ft_conf_count++; - } -} - -static void ft_conf_load(void) -{ - extern char **environ; - size_t used = 0; - int fd; - if (ft_conf_loaded) return; - ft_conf_loaded = true; - fd = open(AGENTENV_CLOCK_CONF, O_RDONLY | O_CLOEXEC); - if (fd >= 0) - { - ssize_t n; - char *line; - while (used < sizeof(ft_conf_text) - 1 && - (n = read(fd, ft_conf_text + used, sizeof(ft_conf_text) - 1 - used)) > 0) - used += n; - close(fd); - ft_conf_text[used] = 0; - line = ft_conf_text; - while (line != NULL && *line != 0) - { - char *next = strchr(line, '\n'); - char *eq; - if (next != NULL) *next++ = 0; - eq = strchr(line, '='); - if (line[0] != '#' && eq != NULL && eq != line) - { - *eq = 0; - ft_conf_add(line, eq + 1); - } - line = next; - } - return; - } - for (char **e = environ; e != NULL && *e != NULL; e++) - { - const char *s = *e; - size_t len; - char *copy, *eq; - if (strncmp(s, "FAKE", 4) && strncmp(s, "NO_FAKE", 7) && strncmp(s, "DONT_FAKE", 9) && strncmp(s, "HOME=", 5) - && strncmp(s, "AGENTENV_", 9)) - continue; - len = strlen(s); - if (used + len + 1 > sizeof(ft_conf_text)) break; - copy = ft_conf_text + used; - memcpy(copy, s, len + 1); - used += len + 1; - eq = strchr(copy, '='); - if (eq == NULL) continue; - *eq = 0; - ft_conf_add(copy, eq + 1); - } -} - -static char *ft_getenv(const char *name) -{ - ft_conf_load(); - for (int i = 0; i < ft_conf_count; i++) - { - if (strcmp(ft_conf_name[i], name) == 0) return (char *) ft_conf_value[i]; - } - return NULL; -} - -/* - * AGENTENV_REAL_TIME=1 taken out of the original environment block in place: its pointer out of the array - * (which is main()'s envp too) and the string itself changed to "=0", so no copy of it is the marker. unsetenv() isn't - * used: a program may define its own (bash does, and before its main() it does nothing). - */ -static void ft_scrub_marker(void) -{ - extern char **environ; - char **e = environ; - if (e == NULL) return; - while (*e != NULL) - { - if (strcmp(*e, "AGENTENV_REAL_TIME=1") == 0) - { - char **d = e; - (*e)[sizeof("AGENTENV_REAL_TIME=") - 1] = '0'; - do d[0] = d[1]; while (*d++ != NULL); - } - else - e++; - } -} - -/* whether a wait's deadline is one to translate: a valid one, in a faked process, outside libfaketime. */ -static bool ft_translates_wait(const struct timespec *abstime) -{ - return abstime != NULL && !ft_disabled && !dont_fake && abstime->tv_nsec >= 0 && abstime->tv_nsec < SEC_TO_nSEC; -} - -/* - * where a CLOCK_REALTIME deadline in fake time falls on a real clock (CLOCK_MONOTONIC or CLOCK_REALTIME): - * now, plus what is left of the fake interval times ft_tl_scale; so a frozen clock counts what is left as real time and - * nothing waits forever, and a deadline already past is now. - */ -static void ft_deadline_from_fake(clockid_t clock, const struct timespec *fake_abs, struct timespec *out) -{ - struct timespec now, real_now, fake_now, left, scaled; - DONT_FAKE_TIME((*real_clock_gettime)(clock, &now)); - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &real_now)); - fake_now = real_now; - (void) fake_clock_gettime(CLOCK_REALTIME, &fake_now); - *out = now; - timespecsub(fake_abs, &fake_now, &left); - if (left.tv_sec < 0) return; - if (left.tv_sec > 100L * 365 * 86400) left.tv_sec = 100L * 365 * 86400; - if (ft_tl_scale != 1.0) - { - timespecmul(&left, ft_tl_scale, &scaled); - } - else - { - scaled = left; - } - timespecadd(&now, &scaled, out); -} - -/* the end of a wait's next slice on CLOCK_MONOTONIC; true when that is the deadline itself. */ -static bool ft_slice_end(const struct timespec *mono_abs, struct timespec *slice) -{ - struct timespec now, step = {ft_wait_slice_ns / SEC_TO_nSEC, ft_wait_slice_ns % SEC_TO_nSEC}; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &now)); - timespecadd(&now, &step, slice); - if (timespeccmp(slice, mono_abs, <)) return false; - *slice = *mono_abs; - return true; -} - -/* after a slice, the clock read again (the file, with no cache); a new specification since ``gen`` moves the - deadline. True when it moved. */ -static bool ft_deadline_moved(const struct timespec *fake_abs, struct timespec *mono_abs, unsigned long *gen) -{ - struct timespec now; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &now)); - (void) fake_clock_gettime(CLOCK_REALTIME, &now); - if (ft_tl_gen == *gen) return false; - *gen = ft_tl_gen; - ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, mono_abs); - return true; -} - -/* - * the clock page. 64 bytes, little-endian, rewritten in place by the follower: seq odd while it writes, - * then even; check is the xor of the other words and a constant. A realtime read in a faked process copies it - * lock-free and computes universe + (now - anchor) * rate (rate 0: universe), so every process agrees with no shared - * start; the first thread to see a new generation also hands it to libfaketime's own state (rates of sleeps, the - * time an absolute spec gives) under the MT lock. The page must be owned by AGENTENV_CLOCK_PAGE_UID (0 by default) and - * writable by no one else, so the session user can't plant one. - */ -struct ft_clock_page -{ - uint32_t magic, version; - uint64_t seq; - int64_t universe_ns, anchor_ns; - double rate; - uint32_t armed, reserved; - uint64_t generation, check; - int64_t mono_universe_ns, mono_anchor_ns; -}; -#define FT_PAGE_MAGIC 0x4B434541u -#define FT_PAGE_VERSION 2u -static const volatile struct ft_clock_page *ft_page = NULL; -static uint64_t ft_page_applied = 0; -static time_t ft_page_last_try = 0; -static void parse_ft_string(const char *user_faked_time); - -static uint64_t ft_page_check(const struct ft_clock_page *p) -{ - uint64_t rate_bits; - memcpy(&rate_bits, &p->rate, sizeof rate_bits); - return p->seq ^ (uint64_t) p->universe_ns ^ (uint64_t) p->anchor_ns ^ rate_bits ^ p->armed ^ p->generation - ^ (uint64_t) p->mono_universe_ns ^ (uint64_t) p->mono_anchor_ns ^ 0x9E3779B97F4A7C15ull; -} - -static bool ft_page_snapshot(struct ft_clock_page *out) -{ - for (int i = 0; i < 1000; i++) - { - uint64_t s1 = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if (s1 & 1) continue; - out->magic = ft_page->magic; - out->seq = s1; - out->universe_ns = ft_page->universe_ns; - out->anchor_ns = ft_page->anchor_ns; - out->rate = ft_page->rate; - out->armed = ft_page->armed; - out->version = ft_page->version; - out->generation = ft_page->generation; - out->check = ft_page->check; - out->mono_universe_ns = ft_page->mono_universe_ns; - out->mono_anchor_ns = ft_page->mono_anchor_ns; - __atomic_thread_fence(__ATOMIC_ACQUIRE); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_RELAXED) == s1 && out->magic == FT_PAGE_MAGIC - && out->version == FT_PAGE_VERSION - && ft_page_check(out) == out->check) - return true; - } - return false; -} - -static void ft_page_open(void) -{ - const char *path = ft_getenv("AGENTENV_CLOCK_PAGE"); - const char *owner = ft_getenv("AGENTENV_CLOCK_PAGE_UID"); - struct stat st; - void *map; - int fd; - if (path == NULL || *path == 0) return; - fd = open(path, O_RDONLY | O_CLOEXEC); - if (fd < 0) return; - if (fstat(fd, &st) != 0 || st.st_size < (off_t) sizeof(struct ft_clock_page) - || st.st_uid != (uid_t) (owner ? strtoul(owner, NULL, 10) : 0) || (st.st_mode & 022) != 0) - { - close(fd); - return; - } - map = mmap(NULL, sizeof(struct ft_clock_page), PROT_READ, MAP_SHARED, fd, 0); - close(fd); - if (map == MAP_FAILED) return; - if (((const struct ft_clock_page *) map)->magic != FT_PAGE_MAGIC - || ((const struct ft_clock_page *) map)->version != FT_PAGE_VERSION) - { - munmap(map, sizeof(struct ft_clock_page)); - return; - } - ft_page = map; -} - -/* libfaketime's own state set to the page's clock; called with the MT lock held. */ -static void ft_page_apply_locked(const struct ft_clock_page *s) -{ - char spec[160]; - if (s->generation == ft_page_applied) return; - if (!s->armed) - snprintf(spec, sizeof spec, "+0 x1"); - else - snprintf(spec, sizeof spec, "@%lld.%09lld x%.17g ^%lld.%09lld", - (long long) (s->universe_ns / 1000000000LL), (long long) (s->universe_ns % 1000000000LL), s->rate, - (long long) (s->anchor_ns / 1000000000LL), (long long) (s->anchor_ns % 1000000000LL)); - parse_ft_string(spec); - __atomic_store_n(&ft_page_applied, s->generation, __ATOMIC_RELEASE); -} - -/* "%s" times are read as a number, without strptime() and mktime(), which reads TZ with getenv(). */ -static const char *ft_parse_epoch(const char *s, struct timespec *out) -{ - char *end; - long long sec; - long nsec = 0, scale = 100000000L; - bool negative; - while (*s == ' ') s++; - negative = (*s == '-'); - sec = strtoll(s, &end, 10); - if (end == s) return NULL; - if (*end == '.') - { - end++; - while (*end >= '0' && *end <= '9') - { - nsec += (*end - '0') * scale; - scale /= 10; - end++; - } - } - if (negative && nsec > 0) - { - sec -= 1; - nsec = SEC_TO_nSEC - nsec; - } - out->tv_sec = (time_t) sec; - out->tv_nsec = nsec; - return end; -} - diff --git a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c b/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c deleted file mode 100644 index 5d8237705..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/agentenv_clock_waits.c +++ /dev/null @@ -1,1175 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 2, included by libfaketime.c (libfaketime.patch) after its time - * functions: clock reads and waits for a set realtime clock time on universe time, re-planned whenever the page - * changes, and everything else (monotonic and boot clocks, relative sleeps and timeouts) the kernel's. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * Elapsed time is real; clock reads and waits for a realtime clock time follow the universe. - */ -#include -#include -#include -#include - -#if !defined(FAKE_PTHREAD) || !defined(FAKE_TIMERS) || !defined(FAKE_SLEEP) || defined(INTERCEPT_SYSCALL) -#error agent-env waits need FAKE_PTHREAD, FAKE_TIMERS and FAKE_SLEEP, and their own syscall(), not the INTERCEPT_SYSCALL one -#endif -#ifndef SYS_futex_waitv -#define SYS_futex_waitv 449 -#endif - -/* - * the system call itself, with no libc and no libfaketime state, so syscall() below works before the - * library is initialised (Chromium's allocator calls syscall() from inside the dynamic loader). - */ -static long ft_raw_syscall6(long n, long a, long b, long c, long d, long e, long f) -{ - long ret; -#if defined(__x86_64__) - register long r10 __asm__("r10") = d; - register long r8 __asm__("r8") = e; - register long r9 __asm__("r9") = f; - __asm__ volatile("syscall" : "=a"(ret) : "0"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9) - : "rcx", "r11", "memory"); -#elif defined(__aarch64__) - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - register long x4 __asm__("x4") = e; - register long x5 __asm__("x5") = f; - __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); - ret = x0; -#else -#error the agent-env syscall() needs x86_64 or aarch64 -#endif - if (ret < 0 && ret > -4096) - { - errno = (int) -ret; - return -1; - } - return ret; -} - -#define FT_NS 1000000000LL -#define FT_NEVER INT64_MAX -static __thread struct ft_clock_page ft_tl_snap; -static __thread bool ft_tl_snap_ok = false; - -/* a consistent copy of the page. While the follower holds it odd (preempted mid-write), wait for it on seq, - which it wakes when done, up to a second: a copy older than the page being written would be extrapolated past the - new page's anchor, and the clock would step back when the write lands. Only a follower that died mid-write (seq odd - for over a second, remembered) leaves this thread on its last good copy. */ -static uint64_t ft_dead_seq = 0; -static bool ft_snap(struct ft_clock_page *s) -{ - for (int i = 0; i < 64; i++) - { - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - sched_yield(); - } - for (int i = 0; i < 100; i++) - { - uint64_t seq = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if ((seq & 1) && seq == __atomic_load_n(&ft_dead_seq, __ATOMIC_RELAXED)) break; - if (seq & 1) - { - struct timespec ts = {0, 10000000}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) (uint32_t) seq, (long) &ts, 0, 0); - } - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - if (i == 99) __atomic_store_n(&ft_dead_seq, seq, __ATOMIC_RELAXED); - } - if (!ft_tl_snap_ok) return false; - *s = ft_tl_snap; - return true; -} - -/* the real time just read, in hand before the loads after it: a load can be carried out before an earlier read of the - CPU's counter (aarch64 and x86_64 both allow it). On aarch64 a load whose address depends on the time, then a load - barrier, as the kernel orders its own counter reads; on x86_64 an lfence. */ -static inline void ft_after_real_read(const struct timespec *tp) -{ -#if defined(__aarch64__) - uint64_t tmp; - __asm__ volatile("eor %0, %1, %1\n\tadd %0, sp, %0\n\tldr xzr, [%0]\n\tdmb ishld" - : "=&r"(tmp) : "r"((uint64_t) tp->tv_nsec) : "memory"); -#elif defined(__x86_64__) - (void) tp; - __asm__ volatile("lfence" ::: "memory"); -#else - (void) tp; - __atomic_thread_fence(__ATOMIC_SEQ_CST); -#endif -} - -/* a consistent copy of the page and the real time read while it was the page: the time is read after the copy and kept - only if the page is still the one copied. A time from before a write paired with the page it wrote (a reader that - waited out the write, or was descheduled between its time and its copy) could put the universe behind what that - reader had shown, since a write is continuous with the page it replaces only at its own real time. */ -static bool ft_snap_at(clockid_t clock, struct ft_clock_page *s, struct timespec *tp) -{ - for (int i = 0; i < 8; i++) - { - int r; - if (!ft_snap(s)) return false; - DONT_FAKE_TIME(r = (*real_clock_gettime)(clock, tp)); - if (r != 0) return false; - ft_after_real_read(tp); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE) == s->seq) return true; - } - return true; -} - -static inline int64_t ft_ns(const struct timespec *t) -{ - if (t->tv_sec > 9000000000LL) return FT_NEVER / 2; - if (t->tv_sec < -9000000000LL) return -(FT_NEVER / 2); - return (int64_t) t->tv_sec * FT_NS + t->tv_nsec; -} -static inline struct timespec ft_ts(int64_t ns) -{ - struct timespec t; - if (ns < 0) ns = 0; - t.tv_sec = (time_t) (ns / FT_NS); - t.tv_nsec = (long) (ns % FT_NS); - return t; -} -static inline int64_t ft_real_rt(void) -{ - struct timespec t; - (*real_clock_gettime)(CLOCK_REALTIME, &t); - return ft_ns(&t); -} -static inline bool ft_uw_active(void) -{ - return ft_page != NULL && !ft_disabled && !dont_fake; -} -static inline bool ft_faked(void) -{ - return !ft_disabled && !dont_fake; -} -static inline bool ft_valid_ts(const struct timespec *t) -{ - return t != NULL && t->tv_nsec >= 0 && t->tv_nsec < FT_NS && t->tv_sec >= 0; -} -static inline bool ft_rt_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_ALARM; -} -/* the clocks the universe has; every other one (monotonic, boot, raw, CPU time) is real. */ -static inline bool ft_universe_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_COARSE || c == CLOCK_REALTIME_ALARM || c == CLOCK_TAI; -} - -/* the universe's realtime at real realtime t; before the anchor, the anchor's. */ -static int64_t ft_page_rt(const struct ft_clock_page *s, int64_t t) -{ - if (!s->armed) return t; - if (s->rate == 0 || t <= s->anchor_ns) return s->universe_ns; - if (s->rate == 1.0) return s->universe_ns + (t - s->anchor_ns); - return s->universe_ns + (int64_t) ((double) (t - s->anchor_ns) * s->rate); -} -/* the real realtime at which the universe reaches ``due``; 0 (the past) once it has, FT_NEVER while frozen - short of it. */ -static int64_t ft_real_of(const struct ft_clock_page *s, int64_t due) -{ - double r; - if (!s->armed) return due < 1 ? 1 : due; - if (due <= s->universe_ns) return 1; - if (s->rate <= 0) return FT_NEVER; - r = (double) (due - s->universe_ns) / s->rate; - if (r > 4e18) return FT_NEVER; - return s->anchor_ns + (int64_t) r + 1; -} -static inline bool ft_reached(const struct ft_clock_page *s, int64_t due) -{ - return ft_page_rt(s, ft_real_rt()) >= due; -} -static inline uint32_t ft_seq32(void) -{ - return (uint32_t) __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); -} - -/* ---- a lock for the tables below, with signals blocked so a handler can't deadlock on it ---- */ -static volatile int ft_tab_lock_word = 0; -static void ft_tab_lock(sigset_t *old) -{ - sigset_t all; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); -} -static void ft_tab_unlock(const sigset_t *old) -{ - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, old, NULL); -} - -/* - * ---- timers for a realtime clock time: timerfds and POSIX timers armed at TIMER_ABSTIME on a realtime clock. The - * kernel timer is armed at the real realtime the universe reaches the deadline at (in the past once it has, so it fires - * now), its interval at interval / rate, and disarmed while frozen short of it; the helper arms it again on each change. - * A one-shot timer that has fired is left alone. ---- - */ -#define FT_TIMER_FDS 8 -struct ft_timer -{ - int kind; /* 0 free, 1 timerfd, 2 POSIX timer */ - int tracked; /* armed for a realtime clock time */ - int nfds, fds[FT_TIMER_FDS]; /* a timerfd's descriptors: the one it was armed through and its duplicates */ - timer_t id; - int clock; - int flags; /* the caller's flags, TFD_TIMER_CANCEL_ON_SET kept */ - int real_set; /* the kernel timer is armed */ - int64_t due, interval; /* universe ns */ - struct ft_clock_page pg; /* the page it was last armed from */ -}; -#define FT_TIMERS_MAX 1024 -static struct ft_timer ft_timers[FT_TIMERS_MAX]; -static int ft_timers_top = 0; -static volatile int ft_timers_any = 0; -static int (*real_timer_create_new)(clockid_t, struct sigevent *, timer_t *); -static int (*real_timer_delete_new)(timer_t); -static int (*real_timer_settime_new)(timer_t, int, const struct itimerspec *, struct itimerspec *); -static int (*real_timer_gettime_new)(timer_t, struct itimerspec *); -#define FT_TFD_CLOCKS 65536 -static int ft_tfd_clock[FT_TFD_CLOCKS]; /* clock + 1 as timerfd_create was told; 0 unknown */ - -static struct ft_timer *ft_timer_find_fd(int fd) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 1) - for (int k = 0; k < ft_timers[i].nfds; k++) - if (ft_timers[i].fds[k] == fd) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_find_id(timer_t id) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2 && ft_timers[i].id == id) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_new(void) -{ - for (int i = 0; i < FT_TIMERS_MAX; i++) - if (ft_timers[i].kind == 0) - { - memset(&ft_timers[i], 0, sizeof ft_timers[i]); - if (i >= ft_timers_top) ft_timers_top = i + 1; - return &ft_timers[i]; - } - return NULL; -} -static int ft_timer_real_get(struct ft_timer *t, struct itimerspec *cur) -{ - return t->kind == 1 ? (*real_timerfd_gettime)(t->fds[0], cur) : (*real_timer_gettime_new)(t->id, cur); -} -/* the next expiration at or after ``after``. */ -static int64_t ft_timer_next(const struct ft_timer *t, int64_t after) -{ - int64_t next = t->due; - if (t->interval > 0 && next < after) next += ((after - next + t->interval - 1) / t->interval) * t->interval; - return next; -} -/* the kernel timer set from page ``s``. A periodic timer goes on from its first expiration past the - universe's time on the page it was armed from, so a jump delivers what it passed at once. Lock held. */ -static int ft_timer_arm(struct ft_timer *t, const struct ft_clock_page *s, bool first) -{ - struct itimerspec real, cur; - double rate = s->armed ? s->rate : 1.0; - int64_t next = t->due, at; - int r; - memset(&real, 0, sizeof real); - if (!first && t->real_set && t->interval == 0 && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; /* fired: the expiration is the kernel's to hand over */ - return 0; - } - if (!first && t->interval > 0) next = ft_timer_next(t, ft_page_rt(&t->pg, ft_real_rt()) + 1); - at = ft_real_of(s, next); - if (at != FT_NEVER) - { - real.it_value = ft_ts(at); - if (real.it_value.tv_sec == 0 && real.it_value.tv_nsec == 0) real.it_value.tv_nsec = 1; - if (t->interval > 0) - { - int64_t iv = (int64_t) ((double) t->interval / rate); - real.it_interval = ft_ts(iv < 1 ? 1 : iv); - } - } - t->due = next; - t->pg = *s; - t->real_set = (at != FT_NEVER); - if (t->kind == 1) - r = (*real_timerfd_settime)(t->fds[0], TFD_TIMER_ABSTIME | (t->flags & TFD_TIMER_CANCEL_ON_SET), &real, NULL); - else - r = (*real_timer_settime_new)(t->id, TIMER_ABSTIME, &real, NULL); - if (r != 0) t->tracked = 0; - return r; -} -/* the timer's value in universe time. Lock held. */ -static void ft_timer_value(struct ft_timer *t, struct itimerspec *out) -{ - struct ft_clock_page s; - struct itimerspec cur; - memset(out, 0, sizeof *out); - if (!t->tracked) return; - if (t->interval == 0 && t->real_set && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; - return; - } - out->it_interval = ft_ts(t->interval); - if (ft_snap(&s)) - { - int64_t now = ft_page_rt(&s, ft_real_rt()), next = ft_timer_next(t, now); - out->it_value = ft_ts(next - now < 1 ? 1 : next - now); - } -} - -/* - * ---- realtime condvar waits in progress: each waits to the real time its deadline falls at; on a page change that - * moves that time, the helper broadcasts its condvar, once, and the wait returns 0 (a spurious wakeup the caller - * rechecks), so the caller waits again on the new page. A broadcast that comes before the waiter is inside glibc's wait - * is sent again until the waiter has seen the change. ---- - */ -struct ft_cw -{ - pthread_cond_t *cond; - int used, woken; - int64_t due, real_due; - uint64_t gen; /* the page generation its real_due came from; FT_CW_UNPLANNED before */ -}; -#define FT_CW_MAX 512 -#define FT_CW_UNPLANNED UINT64_MAX -static struct ft_cw ft_cws[FT_CW_MAX]; -static int ft_cws_top = 0; - -/* ---- the helper thread: one per process, started with the first such timer or wait, blocked on the page's seq ---- */ -static volatile pid_t ft_helper_pid = 0; -static volatile int ft_atfork_set = 0; -static void ft_helper_child(void) -{ - /* POSIX timers and condvar waiters belong to the parent; timerfds are shared with it and stay */ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2) ft_timers[i].kind = 0; - memset(ft_cws, 0, sizeof ft_cws); - ft_cws_top = 0; - ft_tab_lock_word = 0; - ft_helper_pid = 0; -} - -/* on a page change, every timer armed again and every condvar waiter whose real deadline moved woken; true - while a waiter is still to see it (not planned yet, or broadcast to before it was waiting). */ -static bool ft_on_change(const struct ft_clock_page *s, bool rebroadcast) -{ - sigset_t old; - bool pending = false; - ft_tab_lock(&old); - if (!rebroadcast) - for (int i = 0; i < ft_timers_top; i++) - { - struct ft_timer *t = &ft_timers[i]; - if (t->kind != 0 && t->tracked && t->pg.generation != s->generation) ft_timer_arm(t, s, false); - } - for (int i = 0; i < ft_cws_top; i++) - { - struct ft_cw *w = &ft_cws[i]; - if (!w->used || w->gen == s->generation) continue; - if (w->gen == FT_CW_UNPLANNED) - { - pending = true; - continue; - } - if (!w->woken) - { - int64_t nd = ft_real_of(s, w->due), diff; - diff = (nd == FT_NEVER || w->real_due == FT_NEVER) ? (nd == w->real_due ? 0 : FT_NEVER) : nd - w->real_due; - if (diff >= -1000000 && diff <= 1000000) - { - w->gen = s->generation; /* its real end didn't move */ - continue; - } - } - if (!w->woken || rebroadcast) pthread_cond_broadcast(w->cond); - w->woken = 1; - pending = true; - } - ft_tab_unlock(&old); - return pending; -} - -static void *ft_helper_main(void *arg) -{ - struct ft_clock_page s; - uint64_t gen = UINT64_MAX; /* the first pass catches a change made while it started */ - int backoff_ms = 0; - (void) arg; - for (;;) - { - uint32_t seq32 = ft_seq32(); - bool pending = false; - if (ft_snap(&s)) - { - if (s.generation != gen) - { - gen = s.generation; - pending = ft_on_change(&s, false); - backoff_ms = pending ? 1 : 0; - } - else if (backoff_ms > 0) - { - pending = ft_on_change(&s, true); - backoff_ms = pending ? (backoff_ms < 64 ? backoff_ms * 2 : 100) : 0; - } - } - if (backoff_ms > 0) - { - struct timespec ts = {0, (long) backoff_ms * 1000000L}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, (long) &ts, 0, 0); - } - else - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, 0, 0, 0); - } - return NULL; -} - -static void ft_helper_start(void) -{ - pid_t pid = getpid(); - sigset_t all, old; - pthread_t thread; - pthread_attr_t attr; - if (__atomic_load_n(&ft_helper_pid, __ATOMIC_ACQUIRE) == pid) return; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, &old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); - if (ft_helper_pid != pid) - { - if (!ft_atfork_set) - { - pthread_atfork(NULL, NULL, ft_helper_child); - ft_atfork_set = 1; - } - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_setstacksize(&attr, 128 * 1024); - if (pthread_create(&thread, &attr, ft_helper_main, NULL) == 0) /* inherits the all-blocked signal mask */ - { - __atomic_store_n(&ft_helper_pid, pid, __ATOMIC_RELEASE); - pthread_setname_np(thread, "agentenv-clock"); - } - pthread_attr_destroy(&attr); - } - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, &old, NULL); -} - -/* ---- the waits ---- */ - -/* the raw system calls below as the cancellation points their callers are (clock_nanosleep, sem_timedwait): - cancellation acted on at once for the call alone, as glibc's own waits were before its cancellable system call - wrapper. */ -static inline int ft_async_cancel(void) -{ - int old; - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); - return old; -} -static inline void ft_deferred_cancel(int old) -{ - int e = errno, ignored; - pthread_setcanceltype(old, &ignored); - errno = e; -} - -/* a sleep until the universe reaches ``due``, on the page's seq. 0, or EINTR. */ -static int ft_rt_sleep(const struct ft_clock_page *first, int64_t due) -{ - struct ft_clock_page s = *first; - pthread_testcancel(); - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int64_t until; - struct timespec ts; - long r; - int ct; - if (ft_reached(&s, due)) return 0; - until = ft_real_of(&s, due); - ts = ft_ts(until); - ct = ft_async_cancel(); - r = ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, (long) seq32, - until == FT_NEVER ? 0 : (long) &ts, 0, FUTEX_BITSET_MATCH_ANY); - ft_deferred_cancel(ct); - if (r == -1 && errno == EINTR) return EINTR; - if (!ft_snap(&s)) return 0; - } -} - -struct ft_futex_waitv { uint64_t val, uaddr; uint32_t flags, reserved; }; -static volatile int ft_waitv_missing = 0; -enum { FT_W_WORD, FT_W_PAGE, FT_W_TIMEOUT, FT_W_INTR, FT_W_ERR }; -/* without futex_waitv (before Linux 5.16, under gVisor, or refused by a seccomp filter): the word alone, until the real - deadline or a slice (AGENTENV_WAIT_SLICE_MS) at most, and the caller reads the page again between slices. */ -static int ft_wait1(uint32_t *word, uint32_t val, bool private_word, int64_t until) -{ - int64_t end = ft_real_rt() + ft_wait_slice_ns; - struct timespec ts; - if (until < end) end = until; - ts = ft_ts(end); - if (ft_raw_syscall6(SYS_futex, (long) word, - FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME | (private_word ? FUTEX_PRIVATE_FLAG : 0), (long) val, - (long) &ts, 0, FUTEX_BITSET_MATCH_ANY) == 0 || errno == EAGAIN) - return FT_W_WORD; - if (errno == ETIMEDOUT) return FT_W_PAGE; /* a slice's end, or the deadline: the caller reads the page */ - return errno == EINTR ? FT_W_INTR : FT_W_ERR; -} -/* until *word isn't val, the page changes from seq32, or real realtime ``until``; FT_W_ERR with errno the kernel's - (an unaligned or unmapped word). */ -static int ft_wait2(uint32_t *word, uint32_t val, bool private_word, uint32_t seq32, int64_t until) -{ - struct ft_futex_waitv w[2]; - struct timespec ts = ft_ts(until); - long r; - if (ft_waitv_missing) return ft_wait1(word, val, private_word, until); - /* the page first: futex_waitv returns the last of the futexes woken, so a caller's wake is never taken for the page's */ - w[0].val = seq32; - w[0].uaddr = (uint64_t) (uintptr_t) &ft_page->seq; - w[0].flags = 0x02; /* FUTEX2_SIZE_U32, shared */ - w[0].reserved = 0; - w[1].val = val; - w[1].uaddr = (uint64_t) (uintptr_t) word; - w[1].flags = 0x02 | (private_word ? 128 : 0); - w[1].reserved = 0; - r = ft_raw_syscall6(SYS_futex_waitv, (long) w, 2, 0, until == FT_NEVER ? 0 : (long) &ts, CLOCK_REALTIME, 0); - if (r == 1) return FT_W_WORD; - if (r == 0) return FT_W_PAGE; - if (errno == EAGAIN) return __atomic_load_n(word, __ATOMIC_RELAXED) != val ? FT_W_WORD : FT_W_PAGE; - if (errno == ETIMEDOUT) return FT_W_TIMEOUT; - if (errno == EINTR) return FT_W_INTR; - if (errno != ENOSYS && errno != EPERM) return FT_W_ERR; /* EPERM: a seccomp filter that refuses it so */ - ft_waitv_missing = 1; - return ft_wait1(word, val, private_word, until); -} - -/* futex(2) FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME through syscall(): until the universe reaches the deadline, - beside the page; *handled false leaves the call to the kernel as it is. */ -static long ft_u_futex_rt(uint32_t *uaddr, int op, uint32_t val, const struct timespec *abs, bool *handled) -{ - struct ft_clock_page s; - int64_t due = ft_ns(abs); - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int k; - if (ft_reached(&s, due)) - { - errno = ETIMEDOUT; - return -1; - } - k = ft_wait2(uaddr, val, (op & FUTEX_PRIVATE_FLAG) != 0, seq32, ft_real_of(&s, due)); - if (k == FT_W_WORD) - { - if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) - { - errno = EAGAIN; - return -1; - } - return 0; - } - if (k == FT_W_INTR) - { - errno = EINTR; - return -1; - } - if (k == FT_W_ERR) return -1; - if (!ft_snap(&s)) return 0; - } -} - -/* sem_timedwait / sem_clockwait(CLOCK_REALTIME) on glibc's semaphore (its layout is part of the ABI, since - process-shared semaphores outlive glibc versions): the value in the low word of data, the waiters above it, the - futex the value word, which sem_post wakes when there are waiters; ``private`` is glibc's futex flag the other way - round (0 for a process-private semaphore, FUTEX_PRIVATE_FLAG for a shared one). */ -struct ft_new_sem { uint64_t data; int private_flag; int pad; }; -static void ft_sem_unwait(void *sem) -{ - __atomic_fetch_sub(&((struct ft_new_sem *) sem)->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); -} -/* as one of the semaphore's waiters (counted in data's high word): 0 with a token taken, or the error. A cancelled wait - leaves the waiters it joined, as glibc's does. */ -static int ft_sem_wait_slow(struct ft_new_sem *sem, struct ft_clock_page *s, int64_t due) -{ - volatile int err = 0; - pthread_cleanup_push(ft_sem_unwait, sem); - uint64_t d = __atomic_fetch_add(&sem->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); - for (;;) - { - if ((d & 0xffffffffu) == 0) - { - int k, ct; - if (ft_reached(s, due)) - { - err = ETIMEDOUT; - break; - } - ct = ft_async_cancel(); - k = ft_wait2((uint32_t *) &sem->data, 0, sem->private_flag == 0, (uint32_t) s->seq, ft_real_of(s, due)); - ft_deferred_cancel(ct); - if (k == FT_W_INTR || k == FT_W_ERR) - { - err = k == FT_W_INTR ? EINTR : errno; - break; - } - if (!ft_snap(s)) - { - err = ETIMEDOUT; - break; - } - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - } - else if (__atomic_compare_exchange_n(&sem->data, &d, d - 1 - ((uint64_t) 1 << 32), true, __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED)) - break; - } - pthread_cleanup_pop(err != 0); - return err; -} -static int ft_u_sem_wait(sem_t *sem_, int64_t due, bool *handled) -{ - struct ft_new_sem *sem = (struct ft_new_sem *) sem_; - struct ft_clock_page s; - uint64_t d; - int err; - *handled = false; - if (sizeof(sem_t) < sizeof(struct ft_new_sem) || !ft_snap(&s)) return 0; - *handled = true; - pthread_testcancel(); - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - while ((d & 0xffffffffu) > 0) - if (__atomic_compare_exchange_n(&sem->data, &d, d - 1, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) return 0; - err = ft_sem_wait_slow(sem, &s, due); - if (err == 0) return 0; - errno = err; - return -1; -} - -static void ft_cw_release(void *w) -{ - sigset_t old; - ft_tab_lock(&old); - ((struct ft_cw *) w)->used = 0; - ft_tab_unlock(&old); -} -/* glibc's wait, a cancellation point: a cancelled wait gives up its entry, so the helper never wakes a condvar that - may be gone. */ -static int ft_cw_clockwait(struct ft_cw *w, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *end) -{ - volatile int r; - pthread_cleanup_push(ft_cw_release, w); - r = real_pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, end); - pthread_cleanup_pop(0); - return r; -} - -/* a realtime condvar wait (see ft_cw). -1 leaves it to the caller's own path (no page, table full). */ -static int ft_u_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -{ - struct ft_clock_page s; - struct ft_cw *w = NULL; - struct timespec end; - sigset_t old; - int64_t due = ft_ns(abstime), real_due; - int r, woken; - if (!ft_snap(&s)) return -1; - if (ft_reached(&s, due)) return ETIMEDOUT; - ft_helper_start(); - ft_tab_lock(&old); - for (int i = 0; i < FT_CW_MAX; i++) - if (!ft_cws[i].used) - { - w = &ft_cws[i]; - if (i >= ft_cws_top) ft_cws_top = i + 1; - break; - } - if (w != NULL) - { - w->cond = cond; - w->due = due; - w->woken = 0; - w->gen = FT_CW_UNPLANNED; - w->used = 1; - } - ft_tab_unlock(&old); - if (w == NULL) return -1; - if (!ft_snap(&s)) s.armed = 0; - real_due = ft_real_of(&s, due); - ft_tab_lock(&old); - w->real_due = real_due; - w->gen = s.generation; - ft_tab_unlock(&old); - end = ft_ts(real_due == FT_NEVER ? ft_real_rt() + 86400 * FT_NS : real_due); - r = ft_cw_clockwait(w, cond, mutex, &end); - ft_tab_lock(&old); - woken = w->woken; - w->used = 0; - ft_tab_unlock(&old); - /* a real timeout before the universe's deadline (the page moved it later) is a spurious wakeup; the helper's wake for - a jump that passed the deadline is the timeout */ - if (r == ETIMEDOUT) return (ft_snap(&s) && !ft_reached(&s, due)) ? 0 : ETIMEDOUT; - if (r == 0 && woken && ft_snap(&s) && ft_reached(&s, due)) return ETIMEDOUT; - return r; -} - -/* a realtime mutex wait: glibc's mutex has no way in, so slices (AGENTENV_WAIT_SLICE_MS) of a real - CLOCK_MONOTONIC wait, which loses nothing when it times out, with the page read between them. */ -static int ft_u_mutex_lock(pthread_mutex_t *mutex, int64_t due, bool *handled) -{ - struct ft_clock_page s; - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - struct timespec now, end; - int64_t at, sl; - int r; - if (ft_reached(&s, due)) return ETIMEDOUT; - at = ft_real_of(&s, due); - sl = (at == FT_NEVER) ? ft_wait_slice_ns : at - ft_real_rt(); - if (sl > ft_wait_slice_ns) sl = ft_wait_slice_ns; - if (sl < 1) sl = 1; - (*real_clock_gettime)(CLOCK_MONOTONIC, &now); - end = ft_ts(ft_ns(&now) + sl); - r = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &end); - if (r == EINVAL) /* a PI mutex on a kernel without FUTEX_LOCK_PI2 */ - { - struct timespec real = ft_ts(ft_real_rt() + sl); - r = real_pthread_mutex_timedlock(mutex, &real); - } - if (r != ETIMEDOUT) return r; - if (!ft_snap(&s)) return ETIMEDOUT; - } -} - -/* syscall(): futex waits for a realtime clock time follow the universe (raw futex users such as Rust's std - and GLib call it), clock_gettime reads it once the library is up; everything else goes straight to the kernel. */ -long syscall(long number, ...) -{ - va_list ap; - long a[6]; - va_start(ap, number); - for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); - va_end(ap); - if (number == SYS_futex && ft_uw_active()) - { - int op = (int) a[1]; - const struct timespec *ts = (const struct timespec *) a[3]; - if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET && (op & FUTEX_CLOCK_REALTIME) && ft_valid_ts(ts) - && (uint32_t) a[5] == FUTEX_BITSET_MATCH_ANY) - { - bool handled; - long r = ft_u_futex_rt((uint32_t *) a[0], op, (uint32_t) a[2], ts, &handled); - if (handled) return r; - } - } - else if (number == SYS_clock_gettime && ft_page != NULL && !ft_disabled && !dont_fake) - return clock_gettime((clockid_t) a[0], (struct timespec *) a[1]); - return ft_raw_syscall6(number, a[0], a[1], a[2], a[3], a[4], a[5]); -} - -/* ---- timerfds ---- */ -static int ft_u_timerfd_settime(int fd, int flags, const struct itimerspec *nv, struct itimerspec *ov, bool *handled) -{ - struct ft_clock_page s; - struct ft_timer *t = NULL; - struct itimerspec prev; - sigset_t old; - int clock, r; - bool was = false; - *handled = false; - if (fd < 0 || fd >= FT_TFD_CLOCKS || nv == NULL) return 0; - clock = ft_tfd_clock[fd] - 1; - *handled = true; - if (ft_timers_any) - { - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; /* the record stays, with the timer's descriptors, while any is open */ - } - ft_tab_unlock(&old); - } - if (!ft_rt_clock(clock) || !(flags & TFD_TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timerfd_settime)(fd, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if ((t = ft_timer_find_fd(fd)) == NULL && (t = ft_timer_new()) != NULL) - { - t->kind = 1; - t->nfds = 1; - t->fds[0] = fd; - } - if (t == NULL) - { - ft_tab_unlock(&old); - *handled = false; /* table full: the caller's own (translated once) path */ - return 0; - } - t->clock = clock; - t->flags = flags; - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timerfd_gettime)(fd, ov); - } - r = ft_timer_arm(t, &s, true); - if (r != 0) t->tracked = 0; - ft_timers_any = 1; - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -static int ft_u_timerfd_gettime(int fd, struct itimerspec *cur, bool *handled) -{ - struct ft_timer *t; - sigset_t old; - *handled = false; - if (!ft_timers_any || cur == NULL) return 0; - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - *handled = true; - } - ft_tab_unlock(&old); - return 0; -} - -/* - * ---- descriptors: a timerfd's record follows its file, not a number. close() takes a descriptor off it (the record - * goes with the last), dup() and its kin add the new one, so the helper never arms a number the program has closed or - * that now names another file. A number closed where this can't see (closefrom, a raw system call) is dropped when - * timerfd_create hands it out again; a timer with more duplicates than a record holds is left armed as it was. ---- - */ -static int (*ft_real_close)(int); -static int (*ft_real_dup)(int); -static int (*ft_real_dup2)(int, int); -static int (*ft_real_dup3)(int, int, int); -static int (*ft_real_fcntl)(int, int, ...); -static int (*ft_real_fcntl64)(int, int, ...); -static int (*ft_real_close_range)(unsigned int, unsigned int, int); -#define FT_RESOLVE(name) do { if (ft_real_##name == NULL) ft_real_##name = dlsym(RTLD_NEXT, #name); } while (0) - -static inline bool ft_tfd_known(int fd) -{ - return fd >= 0 && fd < FT_TFD_CLOCKS && __atomic_load_n(&ft_tfd_clock[fd], __ATOMIC_RELAXED) != 0; -} -/* the process whose descriptors these tables describe: set at load and in the child of each glibc fork. A child that - shares this memory without fork (vfork, as Python's subprocess uses, or a CLONE_VM clone) has a descriptor table of - its own, so what it closes or duplicates before exec changes nothing here. A fork's child also takes the tables' - lock free: another thread may have held it at the fork, and no thread of the child would ever release it. */ -static volatile pid_t ft_fd_owner = 0; -static void ft_fd_forked(void) -{ - ft_fd_owner = getpid(); - ft_tab_lock_word = 0; -} -static inline bool ft_fd_mine(void) -{ - return getpid() == ft_fd_owner; -} -/* ``fd`` closed, or about to name another file. Lock held. */ -static void ft_fd_gone_locked(int fd) -{ - struct ft_timer *t; - ft_tfd_clock[fd] = 0; - while ((t = ft_timer_find_fd(fd)) != NULL) - { - int k = 0; - while (t->fds[k] != fd) k++; - t->fds[k] = t->fds[--t->nfds]; - if (t->nfds == 0) t->kind = 0; - } -} -static void ft_fd_gone(int fd) -{ - sigset_t old; - if (!ft_tfd_known(fd) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_gone_locked(fd); - ft_tab_unlock(&old); -} -/* ``to`` now names ``from``'s file, a known timerfd. Lock held. */ -static void ft_fd_duped_locked(int from, int to) -{ - struct ft_timer *t; - if (to < 0 || to >= FT_TFD_CLOCKS) return; - ft_tfd_clock[to] = ft_tfd_clock[from]; - if ((t = ft_timer_find_fd(from)) != NULL) - { - if (t->nfds < FT_TIMER_FDS) - t->fds[t->nfds++] = to; - else - t->kind = 0; - } -} -static void ft_fd_duped(int from, int to) -{ - sigset_t old; - if (!ft_tfd_known(from) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_duped_locked(from, to); - ft_tab_unlock(&old); -} -/* dup2 or dup3 onto ``fd2``. One that replaces a timerfd (whose close doesn't block) makes the change and its - bookkeeping under the lock, so a failed call keeps the record and the helper never arms the number in between. */ -static int ft_dup_onto(int fd, int fd2, int flags, bool three) -{ - sigset_t old; - int r; - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - if (fd == fd2 || !ft_tfd_known(fd2) || !ft_fd_mine()) - { - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0 && fd != fd2) ft_fd_duped(fd, r); - return r; - } - ft_tab_lock(&old); - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0) - { - ft_fd_gone_locked(fd2); - if (ft_tfd_known(fd)) ft_fd_duped_locked(fd, fd2); - } - ft_tab_unlock(&old); - return r; -} - -int close(int fd) -{ - ft_fd_gone(fd); - FT_RESOLVE(close); - return (*ft_real_close)(fd); -} -int dup(int fd) -{ - int r; - FT_RESOLVE(dup); - r = (*ft_real_dup)(fd); - if (r >= 0) ft_fd_duped(fd, r); - return r; -} -int dup2(int fd, int fd2) -{ - return ft_dup_onto(fd, fd2, 0, false); -} -int dup3(int fd, int fd2, int flags) -{ - return ft_dup_onto(fd, fd2, flags, true); -} -/* the argument passed on as glibc's own fcntl reads it, a pointer-sized word whatever the command */ -int fcntl(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl); - r = (*ft_real_fcntl)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -int fcntl64(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl64); - r = (*ft_real_fcntl64)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -/* one that closes a timerfd runs under the lock, as dup2 does, so a failed call keeps the records. In practice it runs - in a child of fork before exec, where no other thread wants the lock. */ -int close_range(unsigned int first, unsigned int last, int flags) -{ - sigset_t old; - bool timerfd = false; - int r; - FT_RESOLVE(close_range); - if (!(flags & CLOSE_RANGE_CLOEXEC) && ft_fd_mine()) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS && !timerfd; fd++) timerfd = ft_tfd_known((int) fd); - if (!timerfd) return (*ft_real_close_range)(first, last, flags); - ft_tab_lock(&old); - r = (*ft_real_close_range)(first, last, flags); - if (r == 0) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS; fd++) - if (ft_tfd_known((int) fd)) ft_fd_gone_locked((int) fd); - ft_tab_unlock(&old); - return r; -} -/* the owner, and the real calls resolved at load, so a first close() in a signal handler doesn't call dlsym */ -static void __attribute__((constructor)) ft_fd_init(void) -{ - ft_fd_owner = getpid(); - pthread_atfork(NULL, NULL, ft_fd_forked); - FT_RESOLVE(close); - FT_RESOLVE(dup); - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - FT_RESOLVE(fcntl); - FT_RESOLVE(fcntl64); - FT_RESOLVE(close_range); -} - -/* ---- POSIX timers (unversioned: upstream's GLIBC_2.2 / 2.3.3 versions don't bind glibc 2.34's timer_settime) ---- */ -int timer_create(clockid_t clock, struct sigevent *sev, timer_t *id) -{ - int r; - ftpl_init(); - if (real_timer_create_new == NULL) real_timer_create_new = dlsym(RTLD_NEXT, "timer_create"); - if (real_timer_create_new == NULL) - { - errno = ENOSYS; - return -1; - } - r = (*real_timer_create_new)(clock, sev, id); - if (r == 0 && id != NULL && ft_rt_clock(clock) && ft_faked()) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - t = ft_timer_find_id(*id); - if (t == NULL) t = ft_timer_new(); - if (t != NULL) - { - memset(t, 0, sizeof *t); - t->kind = 2; - t->id = *id; - t->clock = clock; - } - ft_timers_any = 1; - ft_tab_unlock(&old); - } - return r; -} -int timer_delete(timer_t id) -{ - ftpl_init(); - if (real_timer_delete_new == NULL) real_timer_delete_new = dlsym(RTLD_NEXT, "timer_delete"); - if (ft_timers_any) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - if ((t = ft_timer_find_id(id)) != NULL) t->kind = 0; - ft_tab_unlock(&old); - } - return (*real_timer_delete_new)(id); -} -int timer_settime(timer_t id, int flags, const struct itimerspec *nv, struct itimerspec *ov) -{ - struct ft_clock_page s; - struct ft_timer *t; - struct itimerspec prev; - sigset_t old; - bool was = false; - int r; - ftpl_init(); - if (real_timer_settime_new == NULL) real_timer_settime_new = dlsym(RTLD_NEXT, "timer_settime"); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (!ft_timers_any || nv == NULL) return (*real_timer_settime_new)(id, flags, nv, ov); - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; - } - ft_tab_unlock(&old); - if (t == NULL || !ft_uw_active() || !(flags & TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timer_settime_new)(id, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timer_gettime_new)(id, ov); - } - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - r = ft_timer_arm(t, &s, true); - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -int timer_gettime(timer_t id, struct itimerspec *cur) -{ - sigset_t old; - struct ft_timer *t; - ftpl_init(); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (ft_timers_any && cur != NULL) - { - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - ft_tab_unlock(&old); - return 0; - } - ft_tab_unlock(&old); - } - return (*real_timer_gettime_new)(id, cur); -} - diff --git a/src/agent_env/agents/agents/claude-code/clock/build.sh b/src/agent_env/agents/agents/claude-code/clock/build.sh deleted file mode 100755 index 8d7227284..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/build.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -# Builds agent-env's libfaketime: upstream 0.9.12 from its sha256-pinned tarball, libfaketime.patch applied and the two -# engine files beside it copied in, the multithreaded library with stat and utime faking compiled out (file times stay -# real); and the TLS shim beside it (tls_realtime.c). Writes OUT/libfaketimeMT.so.1 and OUT/libtls_realtime.so, which -# /etc/ld.so.preload lists in that order. Needs a C toolchain, make, patch and curl; builds on x86_64 and aarch64. -# build.sh OUT [TARBALL] (TARBALL: a local copy of upstream's v0.9.12 tarball, else it is downloaded) -set -eu -out=$1 -here=$(cd "$(dirname "$0")" && pwd) -work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT -tarball=${2:-$work/libfaketime.tar.gz} -[ -f "$tarball" ] || curl -fsSL -o "$tarball" https://github.com/wolfcw/libfaketime/archive/refs/tags/v0.9.12.tar.gz -echo "4fc32218697c052adcdc5ee395581f2554ca56d086ac817ced2be0d6f1f8a9fa $tarball" | sha256sum -c - >/dev/null -tar xzf "$tarball" -C "$work" -src=$work/libfaketime-0.9.12 -patch -s -d "$src" -p1 < "$here/libfaketime.patch" -cp "$here/agentenv_clock_page.c" "$here/agentenv_clock_waits.c" "$src/src/" -make -s -C "$src/src" FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME" libfaketimeMT.so.1 -mkdir -p "$out" -cp "$src/src/libfaketimeMT.so.1" "$out/" -${CC:-cc} -std=gnu99 -O2 -fPIC -shared -Wall -Wextra -Werror -o "$out/libtls_realtime.so" "$here/tls_realtime.c" -ldl diff --git a/src/agent_env/agents/agents/claude-code/clock/clock-follower b/src/agent_env/agents/agents/claude-code/clock/clock-follower deleted file mode 100755 index fee4586b7..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clock-follower +++ /dev/null @@ -1,206 +0,0 @@ -#!/usr/bin/python3 -I -"""The computer's clock follower: holds a long poll on the gateway's ``/clock/watch`` and writes each state it learns -to the clock page (``clockpage.py``), which agent-env's libfaketime reads in every session program, so the session reads -the universe's time. It runs as its own user, under root's supervisord, so the session user can read the clock but -neither write it nor stop it. - -Core writes its config, the gateway's watch URL as this computer reaches it, the computer's name and its read token, -through the sandbox's root channel (``CONFIG``, readable by root and this user alone); never through the driver, which -runs as the session user. It reads the config every quarter second, during a watch too, and follows a changed one at -once. Until there is one, the clock stays as root made it at boot: real time. It reports how it's doing in ``STATUS``, -which anyone may read and which holds no token: the gateway it follows, whether it's connected, the version it shows, -or its last error. - -The gateway's clock is read against this machine's: its offset is four-stamp, ``((received - t1) + (sent - t4)) / 2``, -from the newest of the last 8 answers whose round trip, less the time the gateway held the watch, isn't an outlier. A -state is written anchored, universe V at this machine's real time A (the gateway's ``sent``) at rate R, so every reader -computes V + (now - A) * R. Each watch names the version it shows, which is its acknowledgement of that version.""" -import json -import os -import queue -import random -import sys -import threading -import time -import urllib.error -import urllib.parse -import urllib.request -from datetime import datetime - -sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) -import clockpage # noqa: E402 - -CONFIG = os.environ.get("AGENTENV_CLOCK_CONFIG", "/etc/agentenv/clock/follower.json") -STATUS = os.environ.get("AGENTENV_CLOCK_STATUS", "/run/agentenv-clock/status.json") -#: How long a watch may take: the gateway holds one up to 25 s (its card's ``hold_s``), plus a margin. -WATCH_TIMEOUT_S = 35.0 -#: How often the config is read, during a watch too. -CONFIG_POLL_S = 0.25 -#: A re-anchoring (the offset moved) is written only past this much, so the page doesn't churn on noise. -ADJUST_S = 0.001 -SAMPLES = 8 -#: After a failed watch it waits before asking again: RETRY_FIRST_S at first, doubled after each failure up to -#: RETRY_MAX_S, each wait a random fraction (RETRY_JITTER) of that, so computers that lost a gateway don't all return at -#: once. -RETRY_FIRST_S = 0.05 -RETRY_MAX_S = 2.0 -RETRY_JITTER = (0.5, 1.0) -OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) - - -def log(event: str, **fields) -> None: - sys.stdout.write(json.dumps({"event": event, "real": time.time(), **fields}) + "\n") - sys.stdout.flush() - - -def atomic_write(path: str, text: str) -> None: - tmp = f"{path}.{os.getpid()}.tmp" - with open(tmp, "w") as f: - f.write(text) - os.chmod(tmp, 0o644) - os.replace(tmp, path) - - -def read_config() -> tuple[bytes | None, dict | None]: - """The config's bytes and what they say, (None, None) when there is none, (bytes, None) when it isn't one.""" - try: - with open(CONFIG, "rb") as f: - raw = f.read() - except OSError: - return None, None - try: - config = json.loads(raw) - return raw, config if isinstance(config, dict) and all( - isinstance(config.get(key), str) and config[key] for key in ("watch_url", "name", "token")) else None - except ValueError: - return raw, None - - -def parse_time(text: str) -> float: - return datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() - - -def offset(samples: list[dict]) -> tuple[float, int]: - """The offset from the newest sample whose delay isn't an outlier (over twice the median and 2 ms), and how many - were dropped.""" - delays = sorted(sample["delay"] for sample in samples) - median = delays[len(delays) // 2] - kept = [sample for sample in samples if sample["delay"] <= max(2 * median, median + 0.002)] - return (kept or samples)[-1]["theta"], len(samples) - len(kept) - - -def state_of(body: dict, theta: float) -> tuple[int, float, float, float]: - """(armed, universe at the anchor, rate, anchor on this machine's clock) for a watch's answer ``body``.""" - if not body.get("armed"): - return 0, 0.0, 1.0, time.time() - return (1, parse_time(body["virtual_time"]), float(body["virtual_seconds_per_real_second"]), - body["sent"] - theta) - - -def watch(url: str, name: str, token: str, after: str, out: queue.Queue) -> None: - """One watch, its answer (or what failed) put on ``out`` with the stamps around it.""" - t1 = time.time() - try: - request = urllib.request.Request( - f"{url}?{urllib.parse.urlencode({'after': after, 'follower': name})}", - headers={"Authorization": f"Bearer {token}"}) - with OPENER.open(request, timeout=WATCH_TIMEOUT_S) as answer: - body = json.loads(answer.read()) - out.put((t1, time.time(), body, None)) - except Exception as e: # noqa: BLE001 - away, restarting or refusing: the follower says so and asks again - out.put((t1, time.time(), None, e)) - - -class Follower: - """Follows one config until it changes.""" - - def __init__(self, raw: bytes, config: dict): - self.raw, self.url, self.name, self.token = raw, config["watch_url"], config["name"], config["token"] - self.after, self.samples, self.written = "", [], None - self.errors, self.backoff = 0, RETRY_FIRST_S - self.status = {"url": self.url, "name": self.name, "connected": False, "version": None, "armed": False, - "error": None} - - def report(self, **fields) -> None: - self.status.update(fields) - atomic_write(STATUS, json.dumps(self.status)) - - def run(self) -> None: - log("following", url=self.url, name=self.name) - self.report() - while True: - answers: queue.Queue = queue.Queue() - threading.Thread(target=watch, args=(self.url, self.name, self.token, self.after, answers), - daemon=True).start() - while True: - try: - t1, t4, body, error = answers.get(timeout=CONFIG_POLL_S) - break - except queue.Empty: - if read_config()[0] != self.raw: - return # its watch is left to end on its own; nothing it answers is applied - if error is not None: - self.failed(error) - if read_config()[0] != self.raw: - return # a gateway that refuses at once answers every watch before the poll above sees a change - continue - if read_config()[0] != self.raw: - return - try: - self.apply(t1, t4, body) - except (KeyError, TypeError, ValueError, AttributeError) as e: - self.failed(ValueError(f"not a clock watch's answer ({type(e).__name__}: {e})")) - - def failed(self, error: Exception) -> None: - self.errors += 1 - code = error.code if isinstance(error, urllib.error.HTTPError) else None - self.report(connected=False, error=f"{type(error).__name__}: {error}"[:300], status=code, errors=self.errors, - error_at=time.time()) - if self.errors <= 20 or self.errors % 100 == 0: - log("error", errors=self.errors, status=code, error=repr(error)[:200], backoff=self.backoff) - deadline = time.monotonic() + self.backoff * random.uniform(*RETRY_JITTER) - while time.monotonic() < deadline and read_config()[0] == self.raw: - time.sleep(min(CONFIG_POLL_S, max(0.0, deadline - time.monotonic()))) - self.backoff = min(self.backoff * 2, RETRY_MAX_S) - - def apply(self, t1: float, t4: float, body: dict) -> None: - if self.errors: - log("reconnected", after_errors=self.errors) - self.errors, self.backoff = 0, RETRY_FIRST_S - held = body["sent"] - body["received"] - self.samples = (self.samples + [{"delay": max(0.0, (t4 - t1) - held), - "theta": ((body["received"] - t1) + (body["sent"] - t4)) / 2}])[-SAMPLES:] - theta, dropped = offset(self.samples) - armed, universe, rate, anchor = state_of(body, theta) - meaning = (armed, rate, universe - rate * anchor if armed else 0.0) - changed = body["version"] != self.after - moved = (self.written is None or meaning[:2] != self.written[:2] - or abs(meaning[2] - self.written[2]) > ADJUST_S) - if changed or moved: - written = clockpage.write(armed, universe, rate, anchor) - self.written = meaning - log("applied" if changed else "adjusted", version=body["version"], armed=bool(armed), - virtual_time=body.get("virtual_time"), spec=written["spec"], theta=theta, held=held, dropped=dropped) - self.after = body["version"] - self.report(connected=True, version=self.after, armed=bool(armed), virtual_time=body.get("virtual_time"), - rate=body.get("virtual_seconds_per_real_second"), theta=theta, applied_at=time.time(), error=None, - status=None, errors=0) - - -def main() -> None: - log("start", pid=os.getpid(), uid=os.getuid()) - atomic_write(STATUS, json.dumps({"connected": False, "error": None, "waiting": "no config yet"})) - while True: - raw, config = read_config() - if config is None: - if raw is not None: - atomic_write(STATUS, json.dumps({"connected": False, "error": "its config isn't one: it needs " - "watch_url, name and token"})) - while read_config()[0] == raw: - time.sleep(CONFIG_POLL_S) - continue - Follower(raw, config).run() - - -if __name__ == "__main__": - main() diff --git a/src/agent_env/agents/agents/claude-code/clock/clock.conf b/src/agent_env/agents/agents/claude-code/clock/clock.conf deleted file mode 100644 index 715987d43..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clock.conf +++ /dev/null @@ -1,11 +0,0 @@ -# Read once by agent-env's libfaketime as each process loads it. While this file exists it is the only source of these -# settings: the environment changes none of them (AGENTENV_REAL_TIME=1 is the one switch a program's environment has). -# Only the session user's processes are faked; the clock comes from the page the follower writes, never from a cache. -AGENTENV_SESSION_UID=1000 -FAKETIME_TIMESTAMP_FILE=/run/agentenv-clock/clock -FAKETIME_NO_CACHE=1 -FAKETIME_DONT_FAKE_MONOTONIC=1 -FAKETIME_FMT=%s -FAKETIME_DISABLE_SHM=1 -AGENTENV_CLOCK_PAGE=/dev/shm/agentenv-clock -AGENTENV_CLOCK_PAGE_UID=0 diff --git a/src/agent_env/agents/agents/claude-code/clock/clockpage.py b/src/agent_env/agents/agents/claude-code/clock/clockpage.py deleted file mode 100644 index 4002bb6c2..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/clockpage.py +++ /dev/null @@ -1,157 +0,0 @@ -"""The follower's side of the clock page (version 2) and of the clock file: writes the universe's realtime -clock to both. Elapsed time is real, so the page's monotonic fields are written as the real monotonic clock (identity) -and no library reads them; they stay so the page keeps its 80-byte layout. - - clockpage.py set UNIVERSE RATE [ANCHOR] armed: UNIVERSE (epoch s) at real time ANCHOR (default now), at RATE - clockpage.py clear real time - clockpage.py create OWNER as root at boot: the page, OWNER's from its first byte, unarmed - clockpage.py show - -The page (default /dev/shm/agentenv-clock, a tmpfs, so readers that mapped it see every write) is created once, 0644, -owned by whoever writes it, and rewritten in place: seq odd, the fields and their check, seq even, then FUTEX_WAKE on -seq, which wakes every wait agent-env's libfaketime has for a clock time, and its helper threads, so they plan again. -Readers never see a torn value: they retry (waiting on seq) while seq is odd or changed, or the check doesn't match. -A page a writer died writing (seq left odd) is taken up by the next write, which then writes the universe as given. - -A write that would put the universe less than CONTINUITY_NS behind where the page has it now (a rate change computed -from a universe read a moment before the write) continues it from where it is instead: the write's real time is taken -last, while seq is odd, and the universe at it read from the page being replaced, so no reader of either page ever sees -the clock step back. A larger step back is a set_time and is written as given.""" -import ctypes -import mmap -import os -import platform -import pwd -import stat -import struct -import sys -import time -from decimal import Decimal - -PAGE = os.environ.get("AGENTENV_CLOCK_PAGE", "/dev/shm/agentenv-clock") -FILE = os.environ.get("AGENTENV_CLOCK_FILE", "/run/agentenv-clock/clock") -MAGIC, VERSION = 0x4B434541, 2 -# magic version seq universe_ns anchor_ns rate armed reserved generation check mono_universe_ns mono_anchor_ns -LAYOUT = struct.Struct(" int: - rate_bits = struct.unpack(" bytes: - """The page's bytes for these fields, its check among them: what every write writes and every reader checks, - agent-env's libfaketime and the egress proxy (its egress/testdata pins them).""" - return LAYOUT.pack(MAGIC, VERSION, seq, universe_ns, anchor_ns, rate, armed, 0, generation, - check(seq, universe_ns, anchor_ns, rate, armed, generation, mono_universe_ns, mono_anchor_ns), - mono_universe_ns, mono_anchor_ns) - - -def open_page() -> mmap.mmap: - try: - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o644) - os.ftruncate(fd, LAYOUT.size) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - except FileExistsError: - fd = os.open(PAGE, os.O_RDWR) - if os.fstat(fd).st_uid != os.getuid(): - raise SystemExit(f"{PAGE} isn't ours") - if os.fstat(fd).st_size < LAYOUT.size: - os.close(fd) - raise SystemExit(f"{PAGE} is an older page") - m = mmap.mmap(fd, LAYOUT.size) - os.close(fd) - return m - - -def _wake(m: mmap.mmap) -> None: - """FUTEX_WAKE every waiter on seq's low word (a shared futex: other processes' read-only mappings are woken too).""" - if _SYS_FUTEX is None: - return - buf = (ctypes.c_char * LAYOUT.size).from_buffer(m) - try: - _libc.syscall(ctypes.c_long(_SYS_FUTEX), ctypes.c_void_p(ctypes.addressof(buf) + 8), ctypes.c_int(_FUTEX_WAKE), - ctypes.c_int(0x7FFFFFFF), None, None, ctypes.c_int(0)) - finally: - del buf - - -def _universe_at(armed, universe_ns, anchor_ns, rate, now_ns) -> int: - if not armed: - return now_ns - if rate == 0 or now_ns <= anchor_ns: - return universe_ns - return universe_ns + int((now_ns - anchor_ns) * rate) - - -def write(armed: int, universe: float, rate: float, anchor: float) -> dict: - m = open_page() - _, _, seq, o_u, o_a, o_rate, o_armed, _, generation, _, _, _ = LAYOUT.unpack(m[:LAYOUT.size]) - universe_ns, anchor_ns = int(Decimal(str(universe)) * 10**9), int(Decimal(str(anchor)) * 10**9) - if not armed: - rate = 1.0 - generation += 1 - dead = seq & 1 # a writer died mid-write: its fields may be torn, so this write goes on from the next even seq - seq += dead - m[8:16] = struct.pack(" 1 and not dead and 0 < old_now - new_now < CONTINUITY_NS - if continued: - universe_ns, anchor_ns = old_now, now_rt - body = encode(seq + 2, universe_ns, anchor_ns, rate, armed, generation, now_mono, now_mono) - m[16:] = body[16:] - m[8:16] = struct.pack(" 1 else 0, - "continued": bool(continued), "universe_ns": universe_ns, "anchor_ns": anchor_ns} - - -def create(owner: str) -> None: - """As root at boot: the page, ``owner``'s from its first byte (unarmed), replacing one anyone else made.""" - user = pwd.getpwnam(owner) - try: - st = os.lstat(PAGE) - if stat.S_ISREG(st.st_mode) and st.st_uid == user.pw_uid and st.st_size == LAYOUT.size: - return - os.unlink(PAGE) - except FileNotFoundError: - pass - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) - try: - os.fchown(fd, user.pw_uid, user.pw_gid) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - os.fchmod(fd, 0o644) - finally: - os.close(fd) - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "set": - anchor = float(sys.argv[4]) if len(sys.argv) > 4 else time.time() - print(write(1, float(sys.argv[2]), float(sys.argv[3]), anchor)) - elif cmd == "create": - create(sys.argv[2]) - elif cmd == "clear": - print(write(0, 0.0, 1.0, time.time())) - else: - with open(PAGE, "rb") as f: - print(LAYOUT.unpack(f.read(LAYOUT.size))) diff --git a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch b/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch deleted file mode 100644 index 17555d2b8..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/libfaketime.patch +++ /dev/null @@ -1,1565 +0,0 @@ -SPDX-License-Identifier: GPL-2.0-only -agent-env's changes to libfaketime 0.9.12 (src/), applied from the tarball's root with patch -p1 by build.sh, which -also copies in agentenv_clock_page.c and agentenv_clock_waits.c, included where marked below. A change to libfaketime, -so licensed as libfaketime is, under the GNU General Public License version 2 (COPYING). - -diff -ruN a/src/libfaketime.c b/src/libfaketime.c ---- a/src/libfaketime.c -+++ b/src/libfaketime.c -@@ -231,11 +231,14 @@ - static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp); - #endif - #ifdef FAKE_PTHREAD --static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict); --static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); --static pthread_rwlock_t monotonic_conds_lock; -+static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_clockwait) (pthread_cond_t *, pthread_mutex_t*, clockid_t, const struct timespec *); -+static int (*real_pthread_mutex_timedlock) (pthread_mutex_t *, const struct timespec *); -+static int (*real_pthread_mutex_clocklock) (pthread_mutex_t *, clockid_t, const struct timespec *); -+#endif -+#ifdef FAKE_TIMERS -+static int (*real_timerfd_create) (int, int); - #endif - - #ifndef __APPLE__ -@@ -334,6 +337,7 @@ - bool str_array_contains(const char *haystack, const char *needle); - void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym); - -+#include "agentenv_clock_page.c" /* agent-env: settings, who is faked, the clock page */ - /** Semaphore protecting shared data */ - static sem_t *shared_sem = NULL; - -@@ -420,6 +424,29 @@ - static bool parse_config_file = true; - - static void ft_cleanup (void) __attribute__ ((destructor)); -+ -+#ifdef PTHREAD_SINGLETHREADED_TIME -+/* agentenv: the lock fake_clock_gettime holds, at file scope so a fork() can't hand the child a held lock. */ -+static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; -+static sigset_t ft_fork_mask; -+static void ft_atfork_prepare(void) -+{ -+ sigset_t all; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &ft_fork_mask); -+ pthread_mutex_lock(&time_mutex); -+} -+static void ft_atfork_parent(void) -+{ -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+static void ft_atfork_child(void) -+{ -+ pthread_mutex_init(&time_mutex, NULL); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+#endif - static void ftpl_init (void) __attribute__ ((constructor)); - - -@@ -519,7 +546,7 @@ - - static void ft_shm_destroy(void) - { -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - - if (ft_shared_env != NULL) - { -@@ -553,7 +580,7 @@ - static void ft_shm_really_init (void) - { - int ticks_shm_fd; -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - sem_t *shared_semR = NULL; - static int nt=1; - -@@ -561,7 +588,7 @@ - if (ft_shared_env == NULL) - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - - /* check for stale semaphore / shared memory information */ -@@ -575,7 +602,7 @@ - if (SEM_FAILED == (shared_semR = sem_open(sem_name, 0))) /* gone stale? */ - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - else - { -@@ -631,7 +658,7 @@ - exit(1); - } - } -- if (getenv("FAKETIME_FLSHM") != NULL) -+ if (ft_getenv("FAKETIME_FLSHM") != NULL) - { /* force the deletion of the shm sync env variable */ - unsetenv("FAKETIME_SHARED"); - } -@@ -653,12 +680,6 @@ - sem_close(shared_sem); - shared_sem = NULL; - } --#ifdef FAKE_PTHREAD -- if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) { -- fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n"); -- exit(-1); -- } --#endif - if (shmCreator == true) ft_shm_destroy(); - } - -@@ -672,8 +693,8 @@ - static void get_fake_monotonic_setting(int* current_value) - { - char *tmp_env; -- if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -- || (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -+ || (tmp_env = ft_getenv("DONT_FAKE_MONOTONIC")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -1140,7 +1161,7 @@ - #endif /* ifndef __APPLE__ */ - #endif /* ifdef FAKE_STAT */ - --#ifdef STATX_TYPE -+#if defined STATX_TYPE && defined FAKE_STAT - static inline void fake_statx_timestamp(struct statx_timestamp* p) - { - struct timespec t = {p->tv_sec,p->tv_nsec}; -@@ -1329,6 +1350,7 @@ - * Contributed by Balint Reczey in v0.9.5 - */ - -+#include "agentenv_clock_waits.c" /* agent-env: universe-time reads and waits */ - #ifdef FAKE_SLEEP - /* - * Faked nanosleep() -@@ -1343,6 +1365,7 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_nanosleep != NULL) return (*real_nanosleep)(req, rem); - if (real_nanosleep == NULL) - { - return -1; -@@ -1395,13 +1418,39 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_clock_nanosleep != NULL && (!(flags & TIMER_ABSTIME) || !ft_rt_clock(clock_id) || !ft_faked())) -+ return (*real_clock_nanosleep)(clock_id, flags, req, rem); -+ if (ft_uw_active() && ft_valid_ts(req)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap(&s)) return ft_rt_sleep(&s, ft_ns(req)); -+ } - if (real_clock_nanosleep == NULL) - { - return -1; - } - if (req != NULL) - { -- if (flags & TIMER_ABSTIME) /* sleep until absolute time */ -+ if ((flags & TIMER_ABSTIME) && clock_id == CLOCK_REALTIME && ft_translates_wait(req)) -+ { -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, req, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_clock_nanosleep)(CLOCK_MONOTONIC, TIMER_ABSTIME, &slice, NULL)); -+ if (result != 0) return result; -+ if (!ft_deadline_moved(req, &mono, &gen) && last) return 0; -+ } -+ } -+ if ((flags & TIMER_ABSTIME) && (ft_disabled || dont_fake || clock_id == CLOCK_REALTIME)) -+ { -+ real_req = *req; -+ } -+ else if (flags & TIMER_ABSTIME) /* sleep until absolute time */ - { - struct timespec tdiff, timeadj; - timespecsub(req, &user_faked_time_timespec, &timeadj); -@@ -1482,6 +1531,7 @@ - int result; - - ftpl_init(); -+ if (real_usleep != NULL) return (*real_usleep)(usec); - if (user_rate_set && !dont_fake) - { - struct timespec real_req; -@@ -1531,6 +1581,7 @@ - #endif - { - ftpl_init(); -+ if (real_sleep != NULL) return (*real_sleep)(seconds); - if (user_rate_set && !dont_fake) - { - if (real_nanosleep == NULL) -@@ -1551,7 +1602,7 @@ - else - { - int result; -- struct timespec real_req = {seconds, 0}, rem; -+ struct timespec real_req = {seconds, 0}, rem = {0, 0}; - timespecmul(&real_req, 1.0 / user_rate, &real_req); - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem)); -@@ -1604,6 +1655,8 @@ - { - return -1; - } -+ (void) seconds_real; -+ return (*real_alarm)(seconds); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*alarm)(seconds_real)); -@@ -1627,6 +1680,7 @@ - { - return -1; - } -+ return (*real_ppoll)(fds, nfds, timeout_ts, sigmask); - if (timeout_ts != NULL) - { - if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0))) -@@ -1662,6 +1716,7 @@ - { - return -1; - } -+ return (*real_epoll_wait)(epfd, events, maxevents, timeout); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1686,6 +1741,7 @@ - { - return -1; - } -+ return (*real_epoll_pwait)(epfd, events, maxevents, timeout, sigmask); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1715,6 +1771,8 @@ - { - return -1; - } -+ (void) timeout_real; -+ return (*real_poll)(fds, nfds, timeout); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real)); -@@ -1748,6 +1806,7 @@ - { - return -1; - } -+ return (*real_select)(nfds, readfds, writefds, errorfds, timeout); - - if (timeout != NULL) - { -@@ -1812,6 +1871,7 @@ - { - return -1; - } -+ return (*real_pselect)(nfds, readfds, writefds, errorfds, timeout, sigmask); - - if (timeout != NULL) - { -@@ -1831,98 +1891,63 @@ - } - #endif - --int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) -+static int ft_sem_wait_until(sem_t *sem, const struct timespec *fake_abs) - { -- int result; -- struct timespec real_abs_timeout, *real_abs_timeout_pt; -- -- /* sanity check */ -- if (abs_timeout == NULL) -- { -- return -1; -- } -- -- if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -- -- if (!dont_fake) -- { -- struct timespec tdiff, timeadj; -- -- timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, CLOCK_MONOTONIC, &slice)); -+ if (result == 0 || errno != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) - { -- timespecmul(&tdiff, user_rate, &timeadj); -+ errno = ETIMEDOUT; -+ return -1; - } -- else -- { -- timeadj = tdiff; -- } -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout); -- real_abs_timeout_pt = &real_abs_timeout; -- } -- else -- { -- /* cast away constness */ -- real_abs_timeout_pt = (struct timespec *)abs_timeout; - } -- -- DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt)); -- return result; - } - --/* EXPERIMENTAL */ --int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) - { - int result; -- struct timespec real_abstime, *real_abstime_pt; -- -- if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC)) -+ ftpl_init(); -+ if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -+ if (ft_uw_active() && ft_translates_wait(abs_timeout)) - { -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -- return result; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abs_timeout), &handled); -+ if (handled) return result; - } -- -- /* sanity check */ -- if (abstime == NULL) -+ if (!ft_translates_wait(abs_timeout) || real_sem_clockwait == NULL) - { -- return -1; -+ DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, abs_timeout)); -+ return result; - } -+ return ft_sem_wait_until(sem, abs_timeout); -+} - -+int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+{ -+ int result; -+ ftpl_init(); - if (!CHECK_MISSING_REAL(sem_clockwait)) return -1; -- -- if (!dont_fake) -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- struct timespec tdiff, timeadj; -- -- timespecsub(abstime, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -- { -- timespecmul(&tdiff, 1.0 / user_rate, &timeadj); -- } -- else -- { -- timeadj = tdiff; -- } -- if (clockid == CLOCK_REALTIME) -- { -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime); -- } -- if (clockid == CLOCK_MONOTONIC) -- { -- timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime); -- } -- real_abstime_pt = &real_abstime; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abstime), &handled); -+ if (handled) return result; - } -- else -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) - { -- /* cast away constness */ -- real_abstime_pt = (struct timespec *)abstime; -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -+ return result; - } -- -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt)); -- return result; -+ return ft_sem_wait_until(sem, abstime); - } - #endif - -@@ -1949,6 +1974,23 @@ - * Faked timer_settime() - * Does not affect timer speed when stepping clock with each time() call. - */ -+#ifdef __linux__ -+/* agentenv: the clock each timerfd was made on (ft_tfd_clock); a record still naming the number is stale. */ -+int timerfd_create(int clockid, int flags) -+{ -+ int fd; -+ ftpl_init(); -+ if (real_timerfd_create == NULL) return -1; -+ fd = real_timerfd_create(clockid, flags); -+ if (fd >= 0 && fd < FT_TFD_CLOCKS && ft_fd_mine()) -+ { -+ ft_fd_gone(fd); -+ ft_tfd_clock[fd] = clockid + 1; -+ } -+ return fd; -+} -+#endif -+ - static int - timer_settime_common(timer_t_or_int timerid, int flags, - const struct itimerspec *new_value, -@@ -1977,18 +2019,11 @@ - { - if (flags & abstime_flag) - { -- struct timespec tdiff, timeadj; -- timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj); -- if (user_rate_set) -- { -- timespecmul(&timeadj, 1.0/user_rate, &tdiff); -- } -+ if (ft_disabled || (compat == FT_FD && ft_tfd_known(timerid.int_member) -+ && !ft_rt_clock(ft_tfd_clock[timerid.int_member] - 1))) -+ new_real.it_value = new_value->it_value; - else -- { -- tdiff = timeadj; -- } -- /* only CLOCK_REALTIME is handled */ -- timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value); -+ ft_deadline_from_fake(CLOCK_REALTIME, &new_value->it_value, &new_real.it_value); - } - else - { -@@ -2061,47 +2096,6 @@ - return result; - } - --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.2 -- */ --int timer_settime_22(int timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.int_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME)); -- } --} -- --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_settime_233(timer_t timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME)); -- } --} - - /* - * Faked timer_gettime() -@@ -2151,48 +2145,6 @@ - return result; - } - --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.2 -- */ --int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_2)); -- } --} -- --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_3_3)); -- } --} -- --__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2"); --__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3"); --__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2"); --__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3"); - - #ifdef __linux__ - /* -@@ -2203,6 +2155,14 @@ - struct itimerspec *old_value) - { - ftpl_init(); -+ if (real_timerfd_settime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_uw_active() ? ft_u_timerfd_settime(fd, flags, new_value, old_value, &handled) : 0; -+ if (handled) return r; -+ if (!(flags & TFD_TIMER_ABSTIME) || fd < 0 || fd >= FT_TFD_CLOCKS || !ft_rt_clock(ft_tfd_clock[fd] - 1)) -+ return (*real_timerfd_settime)(fd, flags, new_value, old_value); -+ } - if (real_timerfd_settime == NULL) - { - return -1; -@@ -2222,6 +2182,13 @@ - int timerfd_gettime(int fd, struct itimerspec *curr_value) - { - ftpl_init(); -+ if (real_timerfd_gettime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_u_timerfd_gettime(fd, curr_value, &handled); -+ if (handled) return r; -+ return (*real_timerfd_gettime)(fd, curr_value); -+ } - if (real_timerfd_gettime == NULL) - { - return -1; -@@ -2379,6 +2346,8 @@ - #endif - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -2495,12 +2464,25 @@ - return; - } - -+ /* agentenv: a specification without "x" is at rate 1, not at the rate of the one before it */ -+ double prev_rate = user_rate; -+ bool prev_rate_set = user_rate_set; -+ if (user_faked_time[0] == 0) return; -+ user_rate = 1.0; -+ user_rate_set = false; -+ - /* check whether the user gave us an absolute time to fake */ - switch (user_faked_time[0]) - { - - default: /* Try and interpret this as a specified time */ - if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(user_faked_time, &user_faked_time_timespec) == NULL) goto parse_failed; -+ user_faked_time_set = true; -+ goto parse_modifiers; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); - -@@ -2518,9 +2500,7 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n" -- "Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - goto parse_modifiers; - break; -@@ -2548,6 +2528,11 @@ - /* Contributed by David North, TDI in version 0.7 */ - case '@': /* Specific time, but clock along relative to that starttime */ - ft_mode = FT_START_AT; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(&user_faked_time[1], &user_faked_time_timespec) == NULL) goto parse_failed; -+ goto parse_anchor; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm); - if (NULL != nstime_str) -@@ -2563,12 +2548,17 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - -- /* Reset starttime */ -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+parse_anchor: -+ /* agentenv: "^" is the real time at which the clock read the time given, so every process reads the -+ same time whenever it started, with no shared memory; without it, the process's own start, as before. */ -+ if (strchr(user_faked_time, '^') != NULL) -+ { -+ if (ft_parse_epoch(strchr(user_faked_time, '^') + 1, &ftpl_starttime.real) == NULL) goto parse_failed; -+ } -+ else if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2577,17 +2567,17 @@ - ft_mode = FT_START_AT; - struct stat master_file_stats; - int ret; -- if (NULL == getenv("FAKETIME_FOLLOW_FILE")) -+ if (NULL == ft_getenv("FAKETIME_FOLLOW_FILE")) - { - fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n"); - exit(1); - } - else - { -- DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); -+ DONT_FAKE_TIME(ret = stat(ft_getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); - if (ret == -1) - { -- fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE")); -+ fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", ft_getenv("FAKETIME_FOLLOW_FILE")); - exit(1); - } - else -@@ -2596,7 +2586,7 @@ - user_faked_time_timespec.tv_nsec = 0; - } - } -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+ if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2609,7 +2599,15 @@ - { - user_rate = atof(strchr(user_faked_time, 'x')+1); - user_rate_set = true; -- if (NULL != getenv("FAKETIME_XRESET")) { -+ if (user_rate <= 0.0) -+ { -+ /* agentenv: rate 0 is the time given, frozen; sleeps and waits then take the real time they ask for */ -+ ft_mode = FT_FREEZE; -+ user_faked_time_set = true; -+ user_rate = 1.0; -+ user_rate_set = false; -+ } -+ if (NULL != ft_getenv("FAKETIME_XRESET")) { - if (ftpl_timecache.real.tv_nsec >= 0) { - user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec; - user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec; -@@ -2639,9 +2637,15 @@ - - strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1); - user_faked_time_saved[BUFFERLEN-1] = 0; -+ ft_spec_gen++; - #ifdef DEBUG - fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved); - #endif -+ return; -+ -+parse_failed: -+ user_rate = prev_rate; -+ user_rate_set = prev_rate_set; - } - - -@@ -2666,8 +2670,8 @@ - const char *progname = __progname; - #endif - -- char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS"); -- bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM"); -+ char *ignore_list = ft_getenv("FAKETIME_IGNORE_SYMBOLS"); -+ bool should_debug_dlsym = ft_getenv("FAKETIME_DEBUG_DLSYM"); - #define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym) - #define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym) - -@@ -2749,32 +2753,25 @@ - #endif - - #ifdef FAKE_PTHREAD -- - #ifdef __GLIBC__ - real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5"); -- - real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2"); -- real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2"); -- real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2"); -+ real_pthread_cond_clockwait = dlvsym(RTLD_NEXT, "pthread_cond_clockwait", "GLIBC_2.30"); -+ real_pthread_mutex_clocklock = dlvsym(RTLD_NEXT, "pthread_mutex_clocklock", "GLIBC_2.30"); - #endif -- - if (NULL == real_pthread_cond_timedwait_232) - { -- real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); -+ real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); - } -- if (NULL == real_pthread_cond_init_232) -+ if (NULL == real_pthread_cond_clockwait) - { -- real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init"); -+ real_pthread_cond_clockwait = dlsym(RTLD_NEXT, "pthread_cond_clockwait"); - } -- if (NULL == real_pthread_cond_destroy_232) -+ if (NULL == real_pthread_mutex_clocklock) - { -- real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy"); -- } -- -- if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) { -- fprintf(stderr,"monotonic_conds_lock init failed\n"); -- exit(-1); -+ real_pthread_mutex_clocklock = dlsym(RTLD_NEXT, "pthread_mutex_clocklock"); - } -+ real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); - #endif - #ifdef __APPLE__ - real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); -@@ -2814,6 +2811,7 @@ - #endif - #ifdef __linux__ - real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime"); -+ real_timerfd_create = dlsym(RTLD_NEXT, "timerfd_create"); - real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime"); - #endif - #endif -@@ -2826,16 +2824,42 @@ - #undef dlsym - #undef dlvsym - -+ /* agentenv: a process of another user than the session's, or one started with AGENTENV_REAL_TIME=1, stays on real -+ time; the marker is taken out of its environment, so what it starts is faked. */ -+ { -+ const char *uid = ft_getenv("AGENTENV_SESSION_UID"); -+ const char *marker = getenv("AGENTENV_REAL_TIME"); -+ if (uid != NULL && *uid != 0 && (uid_t) strtoul(uid, NULL, 10) != getuid()) -+ ft_disabled = true; -+ if (marker != NULL && strcmp(marker, "1") == 0) -+ { -+ ft_disabled = true; -+ ft_scrub_marker(); -+ } -+ if ((tmp_env = ft_getenv("AGENTENV_WAIT_SLICE_MS")) != NULL && atol(tmp_env) > 0) -+ ft_wait_slice_ns = atol(tmp_env) * 1000000L; -+ } -+ if (ft_disabled) -+ { -+ ft_mode = FT_NOOP; -+ dont_fake = dont_fake_final; -+ return; -+ } -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ pthread_atfork(ft_atfork_prepare, ft_atfork_parent, ft_atfork_child); -+#endif -+ ft_page_open(); -+ - #ifdef FAKE_STATELESS - if (0) ft_shm_init(); - #else -- tmp_env = getenv("FAKETIME_DISABLE_SHM"); -+ tmp_env = ft_getenv("FAKETIME_DISABLE_SHM"); - if (!tmp_env || tmp_env[0] == '0') { - ft_shm_init(); - } - #endif - #ifdef FAKE_STAT -- if (getenv("NO_FAKE_STAT")!=NULL) -+ if (ft_getenv("NO_FAKE_STAT")!=NULL) - { - fake_stat_disabled = 1; //Note that this is NOT re-checked - } -@@ -2843,7 +2867,7 @@ - #if defined FAKE_FILE_TIMESTAMPS - #ifdef FAKE_UTIME - // fake_utime_disabled is 0 by default -- if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked -+ if ((tmp_env = ft_getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked - { - if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T') - { /* an empty string or a yes/true value turns off disabling */ -@@ -2860,11 +2884,11 @@ - #endif - #endif - -- if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_CACHE_DURATION")) != NULL) - { - cache_duration = atoi(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_NO_CACHE")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -2875,7 +2899,7 @@ - /* Check whether we actually should be faking the returned timestamp. */ - - /* We can prevent faking time for specified commands */ -- if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SKIP_CMDS")) != NULL) - { - char *skip_cmd, *saveptr, *tmpvar; - /* Don't mess with the env variable directly. */ -@@ -2904,12 +2928,12 @@ - } - - /* We can limit faking time to specified commands */ -- if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_ONLY_CMDS")) != NULL) - { - char *only_cmd, *saveptr, *tmpvar; - bool cmd_matched = false; - -- if (getenv("FAKETIME_SKIP_CMDS") != NULL) -+ if (ft_getenv("FAKETIME_SKIP_CMDS") != NULL) - { - fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n"); - exit(EXIT_FAILURE); -@@ -2941,44 +2965,44 @@ - } - } - -- if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) - { - ft_start_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) - { - ft_stop_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) - { - ft_start_after_ncalls = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) - { - ft_stop_after_ncalls = atol(tmp_env); - limited_faking = true; - } - - /* check whether we should spawn an external command */ -- if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_TARGET")) != NULL) - { - spawnsupport = true; -- (void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); -+ (void) strncpy(ft_spawn_target, ft_getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); - ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0; -- if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_SECONDS")) != NULL) - { - ft_spawn_secs = atol(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) - { - ft_spawn_ncalls = atol(tmp_env); - } - } - -- if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SAVE_FILE")) != NULL) - { - if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT, - S_IWUSR | S_IRUSR))) -@@ -2989,7 +3013,7 @@ - } - - /* load file only if reading timestamps from it is not finished yet */ -- if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_LOAD_FILE")) != NULL) - { - int infile = -1; - struct stat sb; -@@ -3021,7 +3045,7 @@ - infile_set = true; - } - -- tmp_env = getenv("FAKETIME_FMT"); -+ tmp_env = ft_getenv("FAKETIME_FMT"); - if (tmp_env == NULL) - { - strcpy(user_faked_time_fmt, "%Y-%m-%d %T"); -@@ -3061,7 +3085,7 @@ - system_time_from_system(&ftpl_starttime); - } - /* fake time supplied as environment variable? */ -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - parse_config_file = false; - parse_ft_string(tmp_env); -@@ -3070,6 +3094,11 @@ - { - read_config_file(); - } -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } - - dont_fake = dont_fake_final; - } -@@ -3209,8 +3238,8 @@ - * a system-wide /etc/faketimerc present. - * The /etc/faketimerc handling has been contributed by David Burley, - * Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */ -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -- (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", ft_getenv("HOME")); - if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 || - (faketimerc = open(filename, O_RDONLY)) != -1 || - (faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1) -@@ -3248,6 +3277,48 @@ - tp_save.tv_nsec = tp->tv_nsec; - - if (dont_fake) return 0; -+ if (ft_disabled) return 0; -+ if (ft_page != NULL && (clk_id == CLOCK_REALTIME || clk_id == CLOCK_REALTIME_COARSE)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap_at(clk_id, &s, tp)) -+ { -+ if (s.generation != __atomic_load_n(&ft_page_applied, __ATOMIC_ACQUIRE)) -+ { -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ sigset_t all, old; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &old); -+ pthread_mutex_lock(&time_mutex); -+ ft_page_apply_locked(&s); -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &old, NULL); -+#else -+ ft_page_apply_locked(&s); -+#endif -+ } -+ ft_tl_gen = s.generation; -+ ft_tl_scale = (s.armed && s.rate > 0) ? 1.0 / s.rate : 1.0; -+ if (s.armed) -+ { -+ int64_t now = (int64_t) tp->tv_sec * 1000000000LL + tp->tv_nsec, fake; -+ if (s.rate == 0 || now <= s.anchor_ns) -+ fake = s.universe_ns; -+ else if (s.rate == 1.0) -+ fake = s.universe_ns + (now - s.anchor_ns); -+ else -+ fake = s.universe_ns + (int64_t) ((double) (now - s.anchor_ns) * s.rate); -+ tp->tv_sec = fake / 1000000000LL; -+ tp->tv_nsec = fake % 1000000000LL; -+ if (tp->tv_nsec < 0) -+ { -+ tp->tv_sec -= 1; -+ tp->tv_nsec += 1000000000LL; -+ } -+ } -+ return 0; -+ } -+ } - /* Per process timers are only sped up or slowed down */ - if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID)) - { -@@ -3262,7 +3333,6 @@ - volatile int ret = INT_MAX; - - #ifdef PTHREAD_SINGLETHREADED_TIME -- static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; - - // block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation. - sigset_t all_signals, original_mask; -@@ -3367,6 +3437,19 @@ - force_cache_expiration = 0; - } - -+ /* agentenv: with the clock page mapped, the file isn't read; until it is, look for it once a second */ -+ if (ft_page == NULL && ft_getenv("AGENTENV_CLOCK_PAGE") != NULL && current_ts.tv_sec != ft_page_last_try) -+ { -+ ft_page_last_try = current_ts.tv_sec; -+ ft_page_open(); -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } -+ } -+ if (ft_page != NULL) cache_expired = 0; -+ - if (cache_expired == 1) - { - static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */ -@@ -3377,7 +3460,7 @@ - fprintf(stderr, "***************++ Cache expired ++**************\n"); - */ - -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - strncpy(user_faked_time, tmp_env, BUFFERLEN - 1); - user_faked_time[BUFFERLEN - 1] = 0; -@@ -3401,6 +3484,9 @@ - get_fake_monotonic_setting(&fake_monotonic_clock); - } /* cache had expired */ - -+ ft_tl_gen = ft_spec_gen; -+ ft_tl_scale = (ft_mode == FT_START_AT && user_rate_set && user_rate > 0) ? 1.0 / user_rate : 1.0; -+ - if (infile_set) - { - if (load_time(tp)) -@@ -3520,6 +3606,7 @@ - { - struct timespec ts; - int ret; -+ if (ft_disabled) return 0; - ts.tv_sec = tv->tv_sec; - ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000; - -@@ -3627,6 +3714,8 @@ - DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp)); - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -3723,238 +3812,124 @@ - - #ifdef FAKE_PTHREAD - --typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread; -- --struct pthread_cond_monotonic { -- pthread_cond_t *ptr; -- UT_hash_handle hh; --}; -- --static struct pthread_cond_monotonic *monotonic_conds = NULL; -+/* -+ * agentenv: a wait with a CLOCK_REALTIME deadline waits on CLOCK_MONOTONIC instead, until ft_deadline_from_fake's -+ * deadline. A condvar's clock is read from glibc's own condvar word (bit 1 of __wrefs, which glibc's waits read, since -+ * glibc 2.25) instead of a table kept by intercepting pthread_cond_init and pthread_cond_destroy; nothing here calls -+ * getenv() or malloc() or takes a lock. A condvar wait isn't cut into slices, since a slice ending would be a wakeup -+ * its caller must recheck, so a jump or a change of rate reaches it only when it ends; a mutex wait is cut. -+ */ -+#define FT_COND_CLOCK_MONOTONIC 2 - --int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) -+int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- clockid_t clock_id; -- int result; -- -+ struct timespec mono; - ftpl_init(); -- if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1; -- result = real_pthread_cond_init_232(cond, attr); -- -- if (result != 0 || attr == NULL) -- return result; -- -- pthread_condattr_getclock(attr, &clock_id); -- -- if (clock_id == CLOCK_MONOTONIC) { -- struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic)); -- e->ptr = cond; -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_ADD_PTR(monotonic_conds, ptr, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL -+ && !(__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- -- return result; -+ if (!ft_translates_wait(abstime) || real_pthread_cond_clockwait == NULL -+ || (__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ return real_pthread_cond_timedwait_232(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_destroy_232(pthread_cond_t *cond) -+int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- struct pthread_cond_monotonic* e; -- -+ struct timespec real; - ftpl_init(); -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- if (e) { -- HASH_DEL(monotonic_conds, e); -- free(e); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- pthread_rwlock_unlock(&monotonic_conds_lock); -- -- return real_pthread_cond_destroy_232(cond); -+ if (!ft_translates_wait(abstime)) -+ return real_pthread_cond_timedwait_225(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_REALTIME, abstime, &real); -+ return real_pthread_cond_timedwait_225(cond, mutex, &real); - } - --/* -- * Check whether we need a run-time activation of the -- * forced monotonic fix to avoid faked / unfaked timestamp -- * confusion between the application and glibc internals. -- */ --bool needs_forced_monotonic_fix(char *function_name) --{ -- bool result = false; -- char *env_var; --#ifdef __GLIBC__ -- const char *glibc_version_string = gnu_get_libc_version(); -+__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); -+#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); -+#else -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); - #endif - -- if (function_name == NULL) return false; -- -- /* The forced monotonic fix can be activated by setting an -- * environment variable to 1, or disabled by setting it to 0 */ -- if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL) -+int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, -+ const struct timespec *abstime) -+{ -+ struct timespec mono; -+ ftpl_init(); -+ if (real_pthread_cond_clockwait == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- if (env_var[0] == '0') -- result = false; -- else -- result = true; -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- else -- { --#ifdef __GLIBC__ -- /* Here we try to derive the necessity for a forced monotonic fix * -- * based on glibc version. What could possibly go wrong? */ -- -- int glibc_major, glibc_minor; -- sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor); -- -- /* The following decision logic is yet purely based on experiences -- * with pthread_cond_timedwait(). The used boundaries may still be -- * imprecise. */ -- if ( (glibc_major == 2) && -- ((glibc_minor <= 24) || (glibc_minor >= 30)) ) -- { -- result = true; -- } -- else --#endif -- result = false; // avoid forced monotonic fixes unless really necessary -- } -- -- if (getenv("FAKETIME_DEBUG") != NULL) --#ifdef __GLIBC__ -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n", -- function_name, result ? "yes":"no", glibc_version_string); --#else -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n", -- function_name, result ? "yes":"no"); --#endif -- -- return result; -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_cond_clockwait(cond, mutex, clockid, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat) -+static int ft_mutex_lock_until(pthread_mutex_t *mutex, const struct timespec *fake_abs) - { -- struct timespec tp, tdiff_actual, realtime, faketime; -- struct timespec *tf = NULL; -- struct pthread_cond_monotonic* e; -- char *tmp_env; -- int wait_ms; -- clockid_t clk_id; -- int result = 0; -- -- ftpl_init(); -- -- if (abstime != NULL) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) - { -- if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire read monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -- if (e != NULL) -- clk_id = CLOCK_MONOTONIC; -- else -- clk_id = CLOCK_REALTIME; -- -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- faketime = realtime; -- (void)fake_clock_gettime(clk_id, &faketime); -- -- if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL) -- { -- wait_ms = atol(tmp_env); -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- -- tdiff_actual.tv_sec = wait_ms / 1000; -- tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000; -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -+ last = ft_slice_end(&mono, &slice); -+ result = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &slice); -+ if (result == EINVAL && real_pthread_mutex_timedlock != NULL) -+ { /* a PI mutex on a kernel without FUTEX_LOCK_PI2 takes only CLOCK_REALTIME */ -+ struct timespec real; -+ ft_deadline_from_fake(CLOCK_REALTIME, fake_abs, &real); -+ return real_pthread_mutex_timedlock(mutex, &real); - } -- else -- { -- timespecsub(abstime, &faketime, &tp); -- if (user_rate_set) -- { -- timespecmul(&tp, 1.0 / user_rate, &tdiff_actual); -- } -- else -- { -- tdiff_actual = tp; -- } -- } -- -- /* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime -- internally to calculate the appropriate duration for the -- waiting time. This already uses the faked functions, hence, the -- fake time needs to be passed to pthread_cond_timedwait for -- CLOCK_MONOTONIC. */ --#ifndef __ARM_ARCH --#ifndef FORCE_MONOTONIC_FIX -- if (clk_id == CLOCK_MONOTONIC) { -- if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) { -- timespecadd(&realtime, &tdiff_actual, &tp); -- } -- else { -- timespecadd(&faketime, &tdiff_actual, &tp); -- } -- } -- else --#endif --#endif -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -- } -- -- switch (compat) { -- case FT_COMPAT_GLIBC_2_3_2: -- result = real_pthread_cond_timedwait_232(cond, mutex, tf); -- break; -- case FT_COMPAT_GLIBC_2_2_5: -- result = real_pthread_cond_timedwait_225(cond, mutex, tf); -- break; -+ if (result != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) return ETIMEDOUT; - } -- return result; - } - --int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5); -+ ftpl_init(); -+ if (real_pthread_mutex_timedlock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_mutex_clocklock != NULL) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (!ft_translates_wait(abstime) || real_pthread_mutex_clocklock == NULL) -+ return real_pthread_mutex_timedlock(mutex, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2); -+ ftpl_init(); -+ if (real_pthread_mutex_clocklock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_mutex_clocklock(mutex, clockid, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); --#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@"); --#else --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2"); --#endif -- - #endif - - /* -@@ -4005,15 +3980,15 @@ - /* If FAKETIME_TIMESTAMP_FILE was given in environment, - * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it. - * This allows other process instances to share the same time. */ -- if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -- && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -- && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -- && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) -+ if ( (ft_getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -+ && (*ft_getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -+ && (ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -+ && (strcmp(ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) - { - const char *error = NULL; - FILE *envfile; - static char custom_filename[BUFSIZ]; -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); - - if ((envfile = fopen(custom_filename, "wt")) != NULL) - { -@@ -4034,7 +4009,7 @@ - { - fprintf(stderr, "libfaketime: In clock_settime(), failed to " - "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n", -- error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); -+ error, ft_getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); - } - } - -@@ -4125,7 +4100,7 @@ - - /* return 0 if no FAKERANDOM_SEED was seen */ - static int bypass_randomness(void* buf, size_t buflen) { -- char *seedstring = getenv("FAKERANDOM_SEED"); -+ char *seedstring = ft_getenv("FAKERANDOM_SEED"); - char *b = buf; - - if (seedstring != NULL) { -@@ -4169,7 +4144,7 @@ - #else - pid_t getpid() { - #endif -- const char *pidstring = getenv("FAKETIME_FAKEPID"); -+ const char *pidstring = ft_getenv("FAKETIME_FAKEPID"); - if (pidstring != NULL) { - long int pid = strtol(pidstring, NULL, 0); - return (pid_t)(pid); -@@ -4270,7 +4245,7 @@ - va_list ap; - va_start(ap, number); - #ifdef FAKE_RANDOM -- if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) { -+ if (number == __NR_getrandom && ft_getenv("FAKERANDOM_SEED")) { - void *buf; - size_t buflen; - unsigned int flags; -@@ -4282,7 +4257,7 @@ - } - #endif - // static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp); -- if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) { -+ if (number == __NR_clock_gettime && (ft_getenv("FAKETIME") || ft_getenv("FAKETIME_TIMESTAMP_FILE"))) { - clockid_t clk_id; - struct timespec *tp; - clk_id = va_arg(ap, clockid_t); -diff -ruN a/src/libfaketime.map b/src/libfaketime.map ---- a/src/libfaketime.map -+++ b/src/libfaketime.map -@@ -1,21 +1,9 @@ --GLIBC_2.2 { -- global: -- -- timer_gettime; timer_settime; -- local: timer_settime_*; timer_gettime_*; --}; -- --GLIBC_2.3.3 { -- # Changed timer_t. -- timer_gettime; timer_settime; --} GLIBC_2.2; -- - GLIBC_2.2.5 { - global: pthread_cond_timedwait; - local: pthread_cond_timedwait_*; pthread_cond_init_*; pthread_cond_destroy*; - }; - - GLIBC_2.3.2 { -- pthread_cond_timedwait; pthread_cond_init; pthread_cond_destroy; -+ pthread_cond_timedwait; - } GLIBC_2.2.5; - -diff -ruN a/src/libfaketime.c b/src/libfaketime.c ---- a/src/libfaketime.c -+++ b/src/libfaketime.c -@@ -329,6 +329,9 @@ - check_missing_real(#name, (NULL == real_##name)) - - static pthread_once_t initialized_once_control = PTHREAD_ONCE_INIT; -+/* Set on the thread running ftpl_really_init: a time call it makes, as an allocator that reads the clock does when -+ * dlsym allocates, takes the kernel's clock instead of waiting on the init it is part of. */ -+static __thread int ftpl_initializing = 0; - - /* prototypes */ - static int fake_gettimeofday(struct timeval *tv); -@@ -2326,6 +2329,7 @@ - int result; - - ftpl_init(); -+ if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp); - // If ftpl_init ends up recursing, pthread_once will deadlock. - // (Previously we attempted to detect this situation, and bomb out, - // but the approach taken wasn't thread-safe and broke in practice.) -@@ -3103,8 +3107,15 @@ - dont_fake = dont_fake_final; - } - -+static void ftpl_really_init_guarded(void) { -+ ftpl_initializing = 1; -+ ftpl_really_init(); -+ ftpl_initializing = 0; -+} -+ - inline static void ftpl_init(void) { -- pthread_once(&initialized_once_control, ftpl_really_init); -+ if (ftpl_initializing) return; -+ pthread_once(&initialized_once_control, ftpl_really_init_guarded); - } - - void *ft_dlvsym(void *handle, const char *symbol, const char *version, diff --git a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c b/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c deleted file mode 100644 index 0a9a86688..000000000 --- a/src/agent_env/agents/agents/claude-code/clock/tls_realtime.c +++ /dev/null @@ -1,115 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ -/* - * agent-env's TLS shim: OpenSSL checks a peer's certificates against real time, whatever the universe's clock says. - * - * Preloaded after the clock library (/etc/ld.so.preload). A program on universe time months from real time would find - * every live site's certificate not yet valid or expired. Most clients reach live sites through the computer's egress - * proxy, which hands them certificates valid at the universe date; this is for the ones that bypass it. OpenSSL's - * libssl verifies a chain with X509_verify_cert, which this defines first in the global scope: it sets the - * verification's check time to real time, read with a raw system call the clock library doesn't intercept, unless the - * caller set one, then calls OpenSSL's own, the one in the caller's own libcrypto, so a process holding two copies of - * OpenSSL (a bundled one beside the system's) verifies each with its own. - * - * Not covered: TLS stacks of their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries. - */ -#define _GNU_SOURCE -#include -#include -#include -#include - -typedef struct x509_store_ctx_st X509_STORE_CTX; -typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; - -/* OpenSSL's X509_V_FLAG_USE_CHECK_TIME (x509_vfy.h), the same in 1.1 and 3 */ -#define TLS_USE_CHECK_TIME 0x2 -#define TLS_SYSTEM_LIBCRYPTO "libcrypto.so.3" - -/* OpenSSL's functions as one copy of it has them */ -struct tls_openssl -{ - int (*verify)(X509_STORE_CTX *); - void (*set_time)(X509_STORE_CTX *, unsigned long, time_t); - X509_VERIFY_PARAM *(*get0_param)(X509_STORE_CTX *); - unsigned long (*get_flags)(const X509_VERIFY_PARAM *); -}; - -int X509_verify_cert(X509_STORE_CTX *ctx); - -/* the real time in seconds, read with the system call itself (the clock library intercepts libc's), 0 if it fails */ -static long tls_real_time(void) -{ - struct timespec ts = {0, 0}; -#if defined(__x86_64__) - long ret; - __asm__ volatile("syscall" : "=a"(ret) : "0"(228L), "D"((long) CLOCK_REALTIME), "S"(&ts) : "rcx", "r11", "memory"); - if (ret != 0) return 0; -#elif defined(__aarch64__) - register long x8 __asm__("x8") = 113; - register long x0 __asm__("x0") = CLOCK_REALTIME; - register long x1 __asm__("x1") = (long) &ts; - __asm__ volatile("svc 0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory"); - if (x0 != 0) return 0; -#else -#error the TLS shim reads real time with a raw system call on x86_64 or aarch64 -#endif - return ts.tv_sec; -} - -/* ``name`` in ``scope`` (a handle, or RTLD_NEXT), never this file's own definition */ -static void *tls_lookup(void *scope, const char *name) -{ - void *found = dlsym(scope, name); - return found == (void *) X509_verify_cert ? NULL : found; -} - -static void tls_take(void *scope, struct tls_openssl *out) -{ - out->verify = (int (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_verify_cert"); - out->set_time = (void (*)(X509_STORE_CTX *, unsigned long, time_t)) tls_lookup(scope, "X509_STORE_CTX_set_time"); - out->get0_param = (X509_VERIFY_PARAM * (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_STORE_CTX_get0_param"); - out->get_flags = (unsigned long (*)(const X509_VERIFY_PARAM *)) tls_lookup(scope, "X509_VERIFY_PARAM_get_flags"); -} - -/* OpenSSL as the object at ``caller`` sees it: from that object's own dependencies (libssl's libcrypto, whichever copy - it is); for a caller that isn't a shared library depending on one (a program calling it itself), the next definition - in the global scope; else the system's libcrypto where it was loaded privately (a Python extension's). Looked up on - every call, once a handshake: a copy of OpenSSL unloaded and another loaded at its address must not be handed the - first one's functions. */ -static void tls_resolve(void *caller, struct tls_openssl *out) -{ - Dl_info info; - void *handle; - memset(out, 0, sizeof *out); - if (dladdr(caller, &info)) - { - if (info.dli_fname != NULL && info.dli_fname[0] != 0 - && (handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - if (out->verify != NULL) return; - } - } - tls_take(RTLD_NEXT, out); - if (out->verify != NULL) return; - if ((handle = dlopen(TLS_SYSTEM_LIBCRYPTO, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - } -} - -int X509_verify_cert(X509_STORE_CTX *ctx) -{ - struct tls_openssl ssl; - tls_resolve(__builtin_return_address(0), &ssl); - if (ssl.verify == NULL) return -1; - if (ctx != NULL && ssl.set_time != NULL && ssl.get0_param != NULL && ssl.get_flags != NULL) - { - X509_VERIFY_PARAM *param = ssl.get0_param(ctx); - long now = tls_real_time(); - if (param != NULL && !(ssl.get_flags(param) & TLS_USE_CHECK_TIME) && now > 0) ssl.set_time(ctx, 0, (time_t) now); - } - return ssl.verify(ctx); -} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot.go b/src/agent_env/agents/agents/claude-code/egress/boot.go deleted file mode 100644 index 031555627..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/boot.go +++ /dev/null @@ -1,294 +0,0 @@ -package main - -import ( - "crypto/x509" - "encoding/pem" - "errors" - "fmt" - "io/fs" - "os" - "os/exec" - "os/user" - "path/filepath" - "sort" - "strconv" - "strings" - "time" -) - -// bootPaths are where boot reads and writes; tests point them into a folder of their own. -type bootPaths struct { - Folder, PublicCA, Anchor string - // Bundles are the PEM bundles the CA is appended to when they exist: agent-env's trust files, which - // SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS name. - Bundles []string - // UpdateTrust rebuilds the system store from its anchors, and its hooks Java's. - UpdateTrust []string - // CertifiRoots and JavaRoots are searched for certifi's cacert.pem and JDKs' lib/security/cacerts. - CertifiRoots, JavaRoots []string - EgressUser, SessionUser string - // NSS adds the CA to the session user's NSS database, which Chromium and Firefox read; nil leaves it. - NSS func(paths bootPaths) error -} - -// defaultBootPaths are the computer base's. An image whose session user isn't the base's names it in -// AGENTENV_SESSION_USER, and one with no browser to read an NSS database, an agent's, sets AGENTENV_EGRESS_NSS=off. -func defaultBootPaths() bootPaths { - p := bootPaths{ - Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", - Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, - UpdateTrust: []string{"update-ca-certificates"}, - CertifiRoots: []string{"/usr/lib/python3", "/usr/local/lib", "/opt"}, - JavaRoots: []string{"/usr/lib/jvm", "/opt"}, - EgressUser: EgressUser, SessionUser: SessionUser, - NSS: sessionNSS, - } - if name := os.Getenv("AGENTENV_SESSION_USER"); name != "" { - p.SessionUser = name - } - if os.Getenv("AGENTENV_EGRESS_NSS") == "off" { - p.NSS = nil - } - return p -} - -// boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and -// trusted in the system store, agent-env's trust files, every certifi bundle, every JDK's store and the session -// user's NSS database. A step that fails fails the boot, naming it, but for a certifi bundle or a JDK's store, which -// an app brings: those are named among the report's warnings. -func boot(p bootPaths) (map[string]any, error) { - start := time.Now() - report := map[string]any{} - egress, err := user.Lookup(p.EgressUser) - if err != nil { - return report, err - } - uid, _ := strconv.Atoi(egress.Uid) - gid, _ := strconv.Atoi(egress.Gid) - if err := os.MkdirAll(p.Folder, 0o700); err != nil { - return report, err - } - caPath, keyPath := filepath.Join(p.Folder, "ca.pem"), filepath.Join(p.Folder, "ca.key") - if _, err := os.Stat(caPath); errors.Is(err, fs.ErrNotExist) { - if err := writeCA(caPath, keyPath); err != nil { - return report, fmt.Errorf("minting the CA: %w", err) - } - report["minted"] = true - } - for _, path := range []string{p.Folder, caPath, keyPath} { - if err := os.Chown(path, uid, gid); err != nil { - return report, err - } - } - if err := os.Chmod(p.Folder, 0o700); err != nil { - return report, err - } - caPEM, err := os.ReadFile(caPath) - if err != nil { - return report, err - } - for _, path := range []string{p.PublicCA, p.Anchor} { - if err := writeRootFile(path, caPEM); err != nil { - return report, err - } - } - if len(p.UpdateTrust) > 0 { - if out, err := exec.Command(p.UpdateTrust[0], p.UpdateTrust[1:]...).CombinedOutput(); err != nil { - return report, fmt.Errorf("%s: %w: %s", strings.Join(p.UpdateTrust, " "), err, truncate(string(out), ErrorLimit)) - } - } - appended := map[string]bool{} - for _, path := range p.Bundles { - added, err := appendOnce(path, caPEM) - if errors.Is(err, fs.ErrNotExist) { - continue - } - if err != nil { - return report, fmt.Errorf("trusting the CA in %s: %w", path, err) - } - appended[path] = added - } - // An app's own stores, certifi's and JDKs': one that can't take the CA is named, and the computer boots, its - // app then refusing intercepted sites as it would on a store of its own. - var warnings []string - for _, path := range certifiBundles(p.CertifiRoots) { - added, err := appendOnce(path, caPEM) - if err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", path, err)) - continue - } - appended[path] = added - } - report["bundles"] = appended - java := map[string]string{} - for _, store := range javaStores(p.JavaRoots) { - if err := trustJava(store, p.PublicCA); err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", store, err)) - continue - } - java[store] = "trusted" - } - report["java"] = java - if len(warnings) > 0 { - report["warnings"] = warnings - } - if p.NSS != nil { - if err := p.NSS(p); err != nil { - return report, fmt.Errorf("the session's NSS database: %w", err) - } - report["nss"] = "trusted" - } - report["seconds"] = time.Since(start).Seconds() - return report, nil -} - -func writeCA(caPath, keyPath string) error { - ca, err := mintCA(CAName) - if err != nil { - return err - } - key, err := x509.MarshalPKCS8PrivateKey(ca.key) - if err != nil { - return err - } - if err := writeExclusive(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}), 0o600); err != nil { - return err - } - return writeExclusive(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.cert.Raw}), 0o644) -} - -func writeExclusive(path string, data []byte, mode os.FileMode) error { - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) - if err != nil { - return err - } - if _, err := f.Write(data); err != nil { - f.Close() - return err - } - return f.Close() -} - -// writeRootFile writes path whole, 0644, replacing it in one rename. -func writeRootFile(path string, data []byte) error { - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - return err - } - part := path + ".part" - if err := os.WriteFile(part, data, 0o644); err != nil { - return err - } - if err := os.Chmod(part, 0o644); err != nil { - return err - } - return os.Rename(part, path) -} - -// appendOnce appends pem to the bundle at path (through a symlink) unless it's there; whether it did. -func appendOnce(path string, pemData []byte) (bool, error) { - real, err := filepath.EvalSymlinks(path) - if err != nil { - return false, err - } - text, err := os.ReadFile(real) - if err != nil { - return false, err - } - block := strings.TrimSpace(string(pemData)) - if strings.Contains(string(text), block) { - return false, nil - } - f, err := os.OpenFile(real, os.O_WRONLY|os.O_APPEND, 0) - if err != nil { - return false, err - } - defer f.Close() - sep := "" - if len(text) > 0 && !strings.HasSuffix(string(text), "\n") { - sep = "\n" - } - _, err = f.WriteString(sep + block + "\n") - return err == nil, err -} - -func certifiBundles(roots []string) []string { - seen := map[string]bool{} - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacert.pem" || filepath.Base(filepath.Dir(path)) != "certifi" { - return nil - } - real, err := filepath.EvalSymlinks(path) - if err == nil && !seen[real] { - seen[real] = true - out = append(out, path) - } - return nil - }) - } - sort.Strings(out) - return out -} - -// javaStores are the JDKs' stores, lib/security/cacerts, under roots; the system store's hook builds Debian's. -func javaStores(roots []string) []string { - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacerts" { - return nil - } - if filepath.Base(filepath.Dir(path)) == "security" && filepath.Base(filepath.Dir(filepath.Dir(path))) == "lib" { - if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() { - out = append(out, path) - } - } - return nil - }) - } - sort.Strings(out) - return out -} - -// trustJava adds the CA to a JDK's store with that JDK's own keytool, else any on the PATH. -func trustJava(store, ca string) error { - keytool := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(store))), "bin", "keytool") - if _, err := os.Stat(keytool); err != nil { - if keytool, err = exec.LookPath("keytool"); err != nil { - return errors.New("there's no keytool to add the CA with") - } - } - opened := []string{"-keystore", store, "-storepass", "changeit"} - exec.Command(keytool, append([]string{"-delete", "-alias", "agentenv-egress"}, opened...)...).Run() - out, err := exec.Command(keytool, append([]string{"-importcert", "-noprompt", "-trustcacerts", "-alias", - "agentenv-egress", "-file", ca}, opened...)...).CombinedOutput() - if err != nil { - return fmt.Errorf("keytool: %w: %s", err, truncate(string(out), ErrorLimit)) - } - return nil -} - -// sessionNSS adds the CA to the session user's NSS database, as that user, making the database if there's none. -func sessionNSS(p bootPaths) error { - session, err := user.Lookup(p.SessionUser) - if err != nil { - return err - } - db := filepath.Join(session.HomeDir, ".pki", "nssdb") - as := []string{"setpriv", "--reuid=" + p.SessionUser, "--regid=" + p.SessionUser, "--init-groups", "env", - "HOME=" + session.HomeDir} - steps := [][]string{{"mkdir", "-p", db}} - if _, err := os.Stat(filepath.Join(db, "cert9.db")); err != nil { - steps = append(steps, []string{"certutil", "-N", "-d", "sql:" + db, "--empty-password"}) - } - steps = append(steps, []string{"certutil", "-A", "-d", "sql:" + db, "-n", NSSNickname, "-t", "C,,", "-i", - p.PublicCA}) - for _, step := range steps { - argv := append(append([]string{}, as...), step...) - if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil { - return fmt.Errorf("%s: %w: %s", strings.Join(step[:2], " "), err, truncate(string(out), ErrorLimit)) - } - } - return nil -} diff --git a/src/agent_env/agents/agents/claude-code/egress/boot_test.go b/src/agent_env/agents/agents/claude-code/egress/boot_test.go deleted file mode 100644 index c5f59ad0a..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/boot_test.go +++ /dev/null @@ -1,99 +0,0 @@ -package main - -import ( - "os" - "os/user" - "path/filepath" - "strings" - "testing" -) - -func TestBootMintsTheCAOnceItsKeyPrivateAndTrustsItInEveryBundleOnce(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - bundle := filepath.Join(dir, "ca-bundle.pem") - os.WriteFile(bundle, []byte("-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----"), 0o644) - certifi := filepath.Join(dir, "opt", "venv", "lib", "certifi", "cacert.pem") - os.MkdirAll(filepath.Dir(certifi), 0o755) - os.WriteFile(certifi, []byte(""), 0o644) - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - Bundles: []string{bundle, filepath.Join(dir, "missing.pem")}, - CertifiRoots: []string{filepath.Join(dir, "opt")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatal(err, report) - } - if report["minted"] != true { - t.Fatalf("%v", report) - } - info, _ := os.Stat(filepath.Join(p.Folder, "ca.key")) - if info.Mode().Perm() != 0o600 { - t.Fatalf("the CA key is %v", info.Mode().Perm()) - } - if info, _ := os.Stat(p.Folder); info.Mode().Perm() != 0o700 { - t.Fatalf("the folder is %v", info.Mode().Perm()) - } - ca, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - public, _ := os.ReadFile(p.PublicCA) - if string(ca) != string(public) { - t.Fatal("the public CA isn't the CA") - } - if _, err := boot(p); err != nil { - t.Fatal(err) - } - again, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - if string(again) != string(ca) { - t.Fatal("a second boot minted another CA") - } - for _, path := range []string{bundle, certifi} { - text, _ := os.ReadFile(path) - if n := strings.Count(string(text), strings.TrimSpace(string(ca))); n != 1 { - t.Fatalf("%s has the CA %d times", path, n) - } - } - leaves, err := LoadLeaves(p.Folder) - if err != nil || leaves.ca.cert.NotBefore != CAFrom || leaves.ca.cert.NotAfter != CAUntil { - t.Fatalf("%v", err) - } -} - -func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - broken := filepath.Join(dir, "opt", "venv", "lib", "certifi") - os.MkdirAll(filepath.Join(broken, "elsewhere"), 0o755) - os.Symlink(filepath.Join(broken, "elsewhere"), filepath.Join(broken, "cacert.pem")) // a folder, not a bundle - jdk := filepath.Join(dir, "jdk", "lib", "security") - os.MkdirAll(jdk, 0o755) - os.WriteFile(filepath.Join(jdk, "cacerts"), []byte("not a keystore"), 0o644) - t.Setenv("PATH", dir) // and no keytool to add the CA with - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - CertifiRoots: []string{filepath.Join(dir, "opt")}, JavaRoots: []string{filepath.Join(dir, "jdk")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatalf("the boot failed: %v", err) - } - warnings, _ := report["warnings"].([]string) - if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, " "), "keytool") { - t.Fatalf("%v", report) - } -} - -func TestAnImageNamesItsSessionUserAndLeavesOutTheNSSDatabase(t *testing.T) { - if p := defaultBootPaths(); p.SessionUser != SessionUser || p.NSS == nil { - t.Fatalf("the base's defaults changed: %q, NSS %v", p.SessionUser, p.NSS != nil) - } - t.Setenv("AGENTENV_SESSION_USER", "agent") - t.Setenv("AGENTENV_EGRESS_NSS", "off") - if p := defaultBootPaths(); p.SessionUser != "agent" || p.NSS != nil { - t.Fatalf("an agent image's settings weren't taken: %q, NSS %v", p.SessionUser, p.NSS != nil) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock.go b/src/agent_env/agents/agents/claude-code/egress/clock.go deleted file mode 100644 index 30cb880c1..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/clock.go +++ /dev/null @@ -1,151 +0,0 @@ -package main - -import ( - "encoding/binary" - "math" - "os" - "sync" - "syscall" - "time" -) - -// The clock page the follower writes (clockpage.py, version 2): magic version seq universe_ns anchor_ns rate armed -// reserved generation check mono_universe_ns mono_anchor_ns, little-endian, 80 bytes, seq odd while it's written. -const ( - pageSize = 80 - pageMagic = 0x4B434541 - checkSalt = 0x9E3779B97F4A7C15 -) - -// state is the universe as the session reads it: armed, the universe at the anchor (seconds), the anchor (real -// seconds) and the rate. -type state struct { - Armed bool - V float64 - Anchor float64 - Rate float64 -} - -var unarmed = state{Rate: 1} - -func (s state) at(t float64) float64 { - if !s.Armed { - return t - } - if s.Rate == 0 || t <= s.Anchor { - return s.V - } - return s.V + (t-s.Anchor)*s.Rate -} - -func pageCheck(seq uint64, u, a int64, rate float64, armed uint32, generation uint64, mu, ma int64) uint64 { - return seq ^ uint64(u) ^ uint64(a) ^ math.Float64bits(rate) ^ uint64(armed) ^ generation ^ uint64(mu) ^ - uint64(ma) ^ checkSalt -} - -// readPage reads a consistent state from the page's bytes, or false when it's being written, torn or not a page. -func readPage(b []byte) (state, bool) { - if len(b) < pageSize || binary.LittleEndian.Uint32(b[0:]) != pageMagic { - return state{}, false - } - seq := binary.LittleEndian.Uint64(b[8:]) - if seq&1 == 1 { - return state{}, false - } - u := int64(binary.LittleEndian.Uint64(b[16:])) - a := int64(binary.LittleEndian.Uint64(b[24:])) - rate := math.Float64frombits(binary.LittleEndian.Uint64(b[32:])) - armed := binary.LittleEndian.Uint32(b[40:]) - generation := binary.LittleEndian.Uint64(b[48:]) - check := binary.LittleEndian.Uint64(b[56:]) - mu := int64(binary.LittleEndian.Uint64(b[64:])) - ma := int64(binary.LittleEndian.Uint64(b[72:])) - if check != pageCheck(seq, u, a, rate, armed, generation, mu, ma) || binary.LittleEndian.Uint64(b[8:]) != seq { - return state{}, false - } - if armed == 0 { - return unarmed, true - } - return state{Armed: true, V: float64(u) / 1e9, Anchor: float64(a) / 1e9, Rate: rate}, true -} - -// Universe follows the clock page, read every PagePoll, so a change is seen and the universe it left is held for Hold. -type Universe struct { - path string - mu sync.Mutex - page []byte - current state - known bool - previous state - leftAt time.Time -} - -func NewUniverse(path string) *Universe { - u := &Universe{path: path, current: unarmed} - u.refresh(time.Now()) - return u -} - -func (u *Universe) Poll() { - for { - time.Sleep(PagePoll) - u.refresh(time.Now()) - } -} - -func (u *Universe) read() state { - if u.page == nil { - f, err := os.Open(u.path) - if err != nil { - return unarmed - } - mapped, err := syscall.Mmap(int(f.Fd()), 0, pageSize, syscall.PROT_READ, syscall.MAP_SHARED) - f.Close() - if err != nil { - return unarmed - } - u.page = mapped - } - for i := 0; i < PageReads; i++ { - if s, ok := readPage(u.page); ok { - return s - } - } - return u.current -} - -func (u *Universe) refresh(now time.Time) state { - u.mu.Lock() - defer u.mu.Unlock() - s := u.read() - if u.known && s != u.current { - u.previous, u.leftAt = u.current, now - } - u.current, u.known = s, true - return s -} - -// View is false when the session is on real time and was armed nowhere within Hold; else true, with the universes a -// client may check against: now, the universe at the anchor, and the one left within Hold when it was armed. -func (u *Universe) View(now time.Time) (bool, []float64) { - u.refresh(now) - u.mu.Lock() - defer u.mu.Unlock() - return view(u.current, u.previous, u.leftAt, now) -} - -func view(current, previous state, leftAt time.Time, now time.Time) (bool, []float64) { - held := !leftAt.IsZero() && now.Sub(leftAt) <= Hold && previous.Armed - if !current.Armed && !held { - return false, nil - } - t := float64(now.UnixNano()) / 1e9 - out := []float64{current.at(t)} - if current.Armed { - out = append(out, current.V) - } - if held { - out = append(out, previous.at(t)) - } - return true, out -} diff --git a/src/agent_env/agents/agents/claude-code/egress/clock_test.go b/src/agent_env/agents/agents/claude-code/egress/clock_test.go deleted file mode 100644 index 22bb41503..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/clock_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package main - -import ( - "encoding/binary" - "encoding/hex" - "math" - "os" - "path/filepath" - "strings" - "testing" - "time" -) - -func page(seq uint64, universe, anchor time.Time, rate float64, armed bool) []byte { - b := make([]byte, pageSize) - u, a := universe.UnixNano(), anchor.UnixNano() - var on uint32 - if armed { - on = 1 - } - binary.LittleEndian.PutUint32(b[0:], pageMagic) - binary.LittleEndian.PutUint32(b[4:], 2) - binary.LittleEndian.PutUint64(b[8:], seq) - binary.LittleEndian.PutUint64(b[16:], uint64(u)) - binary.LittleEndian.PutUint64(b[24:], uint64(a)) - binary.LittleEndian.PutUint64(b[32:], math.Float64bits(rate)) - binary.LittleEndian.PutUint32(b[40:], on) - binary.LittleEndian.PutUint64(b[48:], 7) - binary.LittleEndian.PutUint64(b[56:], pageCheck(seq, u, a, rate, on, 7, a, a)) - binary.LittleEndian.PutUint64(b[64:], uint64(a)) - binary.LittleEndian.PutUint64(b[72:], uint64(a)) - return b -} - -func TestAPageReadsAsTheUniverseItHoldsAndATornOneNot(t *testing.T) { - universe, anchor := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Unix(time.Now().Unix(), 0) - s, ok := readPage(page(4, universe, anchor, 2, true)) - if !ok || !s.Armed || s.Rate != 2 || int64(s.V) != universe.Unix() { - t.Fatalf("read %+v %v", s, ok) - } - if got := s.at(float64(anchor.Unix()) + 10); int64(got) != universe.Unix()+20 { - t.Fatalf("10 s on at rate 2: %v", got) - } - if _, ok := readPage(page(5, universe, anchor, 2, true)); ok { - t.Fatal("a page being written (odd seq) read as whole") - } - torn := page(4, universe, anchor, 2, true) - torn[20] ^= 1 - if _, ok := readPage(torn); ok { - t.Fatal("a torn page read as whole") - } - if s, ok := readPage(page(2, time.Time{}, time.Time{}, 1, false)); !ok || s.Armed { - t.Fatalf("an unarmed page: %+v %v", s, ok) - } -} - -func TestTheUniverseFollowsThePageFileAndHoldsTheOneItLeft(t *testing.T) { - path := filepath.Join(t.TempDir(), "page") - if err := os.WriteFile(path, page(2, time.Time{}, time.Time{}, 1, false), 0o644); err != nil { - t.Fatal(err) - } - u := NewUniverse(path) - if armed, _ := u.View(time.Now()); armed { - t.Fatal("unarmed page viewed as armed") - } - past := time.Now().Add(-180 * 24 * time.Hour) - f, _ := os.OpenFile(path, os.O_WRONLY, 0) - f.WriteAt(page(4, past, time.Now(), 1, true), 0) - f.Close() - armed, universes := u.View(time.Now()) - if !armed || len(universes) != 2 || int64(universes[1]) != past.Unix() { - t.Fatalf("armed page: %v %v", armed, universes) - } -} - -func TestAClientMayCheckAgainstTheUniverseItLeftForHoldAfterAChange(t *testing.T) { - now := time.Now() - past := state{Armed: true, V: float64(now.Add(-100 * 24 * time.Hour).Unix()), Anchor: float64(now.Unix()), Rate: 1} - if armed, _ := view(unarmed, state{}, time.Time{}, now); armed { - t.Fatal("real time viewed as armed") - } - armed, universes := view(unarmed, past, now.Add(-Hold/2), now) - if !armed || len(universes) != 2 { - t.Fatalf("within Hold of leaving an armed universe: %v %v", armed, universes) - } - if armed, _ := view(unarmed, past, now.Add(-2*Hold), now); armed { - t.Fatal("past Hold, the universe left still counts") - } -} - -// golden is a page the follower's writer wrote (clockpage.encode), as testdata keeps it. -func golden(t *testing.T, name string) []byte { - text, err := os.ReadFile(filepath.Join("testdata", "page-"+name+".hex")) - if err != nil { - t.Fatal(err) - } - b, err := hex.DecodeString(strings.TrimSpace(string(text))) - if err != nil { - t.Fatal(err) - } - return b -} - -func TestThePagesTheFollowersWriterWritesReadAsTheUniverseTheyHold(t *testing.T) { - s, ok := readPage(golden(t, "armed")) - if !ok || !s.Armed || s.V != 1577836800 || s.Rate != 2 || math.Abs(s.Anchor-1794000000.123456789) > 1e-6 { - t.Fatalf("armed: %+v %v", s, ok) - } - if s, ok := readPage(golden(t, "unarmed")); !ok || s != unarmed { - t.Fatalf("unarmed: %+v %v", s, ok) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/go.mod b/src/agent_env/agents/agents/claude-code/egress/go.mod deleted file mode 100644 index 7b1b97e80..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/go.mod +++ /dev/null @@ -1,12 +0,0 @@ -module agentenv/egress - -go 1.26.11 - -require github.com/refraction-networking/utls v1.8.2 - -require ( - github.com/andybalholm/brotli v1.0.6 // indirect - github.com/klauspost/compress v1.17.4 // indirect - golang.org/x/crypto v0.36.0 // indirect - golang.org/x/sys v0.31.0 // indirect -) diff --git a/src/agent_env/agents/agents/claude-code/egress/go.sum b/src/agent_env/agents/agents/claude-code/egress/go.sum deleted file mode 100644 index 3e8a6d1ef..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= -github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= -github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= -golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= -golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/src/agent_env/agents/agents/claude-code/egress/hello.go b/src/agent_env/agents/agents/claude-code/egress/hello.go deleted file mode 100644 index 381ea2993..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/hello.go +++ /dev/null @@ -1,164 +0,0 @@ -package main - -import ( - "bufio" - "encoding/binary" - "errors" - "strings" - "time" -) - -// A TLS record's content type for handshake messages, a ClientHello's handshake type, and the most plaintext one -// record carries. -const ( - recordHandshake = 22 - clientHelloType = 1 - maxRecord = 16384 -) - -// clientHello is the ClientHello handshake message the client has sent, peeked from r so the handshake stays -// untouched for whoever reads it next; it may span several records. -func clientHello(r *bufio.Reader, deadline time.Time) ([]byte, error) { - need := 5 - for { - if time.Now().After(deadline) { - return nil, errors.New("ClientHello incomplete") - } - data, err := r.Peek(need) - if err != nil { - if errors.Is(err, bufio.ErrBufferFull) { - return nil, errors.New("ClientHello larger than the read buffer") - } - return nil, err - } - if data[0] != recordHandshake { - return nil, errors.New("not TLS") - } - var body []byte - for p := 0; ; { - if p+5 > len(data) { - need = p + 5 - break - } - if data[p] != recordHandshake { - return nil, errors.New("ClientHello interrupted by another record") - } - size := int(binary.BigEndian.Uint16(data[p+3:])) - if p+5+size > len(data) { - need = p + 5 + size - break - } - body = append(body, data[p+5:p+5+size]...) - p += 5 + size - if len(body) >= 4 { - if whole := 4 + (int(body[1])<<16 | int(body[2])<<8 | int(body[3])); len(body) >= whole { - return body[:whole], nil - } - } - } - } -} - -// parseHello is the server name and the ALPN protocols a ClientHello message offers. -func parseHello(hello []byte) (string, []string, error) { - p, err := helloExtensions(hello) - if err != nil { - return "", nil, err - } - if len(hello) < p+2 { - return "", nil, nil - } - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return "", nil, errors.New("extensions overrun the ClientHello") - } - var sni string - var alpn []string - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - body := hello[q+4 : min(q+4+size, end)] - q += 4 + size - switch { - case kind == 0 && len(body) > 5 && body[2] == 0: - n := int(binary.BigEndian.Uint16(body[3:])) - if 5+n <= len(body) { - sni = strings.ToLower(string(body[5 : 5+n])) - } - case kind == 16 && len(body) > 2: - for r := 2; r < len(body); { - n := int(body[r]) - if r+1+n > len(body) { - break - } - alpn = append(alpn, string(body[r+1:r+1+n])) - r += 1 + n - } - } - } - return sni, alpn, nil -} - -// resumption: what only the proxy's own session with the client could mean, never the site's. -var dropped = map[uint16]bool{41: true, 42: true} - -// helloRecord is the ClientHello as one handshake record, without the extensions only a resumption with the proxy put -// there, and the extensions it left out. -func helloRecord(hello []byte) ([]byte, []uint16, error) { - p, err := helloExtensions(hello) - if err != nil { - return nil, nil, err - } - body := append([]byte{}, hello[4:p]...) - var gone []uint16 - if len(hello) >= p+2 { - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return nil, nil, errors.New("extensions overrun the ClientHello") - } - var exts []byte - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - if q+4+size > end { - return nil, nil, errors.New("an extension overruns the ClientHello") - } - if dropped[kind] { - gone = append(gone, kind) - } else { - exts = append(exts, hello[q:q+4+size]...) - } - q += 4 + size - } - body = binary.BigEndian.AppendUint16(body, uint16(len(exts))) - body = append(body, exts...) - } - if len(body)+4 > maxRecord { - return nil, nil, errors.New("ClientHello longer than one record") - } - msg := append([]byte{clientHelloType, byte(len(body) >> 16), byte(len(body) >> 8), byte(len(body))}, body...) - return append([]byte{recordHandshake, 3, 1, byte(len(msg) >> 8), byte(len(msg))}, msg...), gone, nil -} - -// helloExtensions is where a ClientHello message's extensions start, past its version, random, session id, cipher -// suites and compression methods, each length checked against the message. -func helloExtensions(hello []byte) (int, error) { - if len(hello) < 4 || hello[0] != clientHelloType { - return 0, errors.New("not a ClientHello") - } - p := 4 + 2 + 32 - for _, width := range []int{1, 2, 1} { // session id, cipher suites, compression methods - if len(hello) < p+width { - return 0, errors.New("short ClientHello") - } - n := int(hello[p]) - if width == 2 { - n = int(binary.BigEndian.Uint16(hello[p:])) - } - p += width + n - if p > len(hello) { - return 0, errors.New("a ClientHello field overruns the message") - } - } - return p, nil -} diff --git a/src/agent_env/agents/agents/claude-code/egress/hello_test.go b/src/agent_env/agents/agents/claude-code/egress/hello_test.go deleted file mode 100644 index a3b30b2f1..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/hello_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "crypto/tls" - "encoding/binary" - "net" - "reflect" - "testing" - "time" -) - -// goHello is the ClientHello message Go's TLS client sends for name, offering alpn. -func goHello(t *testing.T, name string, alpn []string) []byte { - t.Helper() - client, server := net.Pipe() - go tls.Client(client, &tls.Config{ServerName: name, NextProtos: alpn, InsecureSkipVerify: true}).Handshake() - defer client.Close() - defer server.Close() - hello, err := clientHello(bufio.NewReader(server), time.Now().Add(5*time.Second)) - if err != nil { - t.Fatal(err) - } - return hello -} - -func records(message []byte, size int) []byte { - var out []byte - for len(message) > 0 { - n := min(size, len(message)) - out = append(out, 22, 3, 1, byte(n>>8), byte(n)) - out = append(out, message[:n]...) - message = message[n:] - } - return out -} - -func TestAClientHelloIsReadWholeAcrossRecordsAndNamesItsSiteAndProtocols(t *testing.T) { - hello := goHello(t, "Example.COM", []string{"h2", "http/1.1"}) - sni, alpn, err := parseHello(hello) - if err != nil || sni != "example.com" || !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) { - t.Fatalf("parsed %q %v %v", sni, alpn, err) - } - split := records(hello, 100) - again, err := clientHello(bufio.NewReader(bytes.NewReader(split)), time.Now().Add(time.Second)) - if err != nil || !bytes.Equal(again, hello) { - t.Fatalf("across records: %v", err) - } - if _, err := clientHello(bufio.NewReader(bytes.NewReader([]byte("GET / HTTP/1.1\r\n\r\n"))), - time.Now().Add(time.Second)); err == nil { - t.Fatal("plain HTTP read as TLS") - } -} - -func withExtension(hello []byte, kind uint16, body []byte) []byte { - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(binary.BigEndian.Uint16(hello[p:])) - p += 1 + int(hello[p]) - exts := append(append([]byte{}, hello[p+2:]...), byte(kind>>8), byte(kind), byte(len(body)>>8), byte(len(body))) - exts = append(exts, body...) - out := append(append([]byte{}, hello[:p]...), byte(len(exts)>>8), byte(len(exts))) - out = append(out, exts...) - n := len(out) - 4 - out[1], out[2], out[3] = byte(n>>16), byte(n>>8), byte(n) - return out -} - -func TestTheUpstreamRecordLeavesOutWhatOnlyAResumptionWithTheProxyMeans(t *testing.T) { - hello := withExtension(goHello(t, "example.com", nil), 41, []byte{0, 1, 2}) - record, gone, err := helloRecord(hello) - if err != nil || !reflect.DeepEqual(gone, []uint16{41}) { - t.Fatalf("dropped %v %v", gone, err) - } - if record[0] != 22 || int(binary.BigEndian.Uint16(record[3:])) != len(record)-5 { - t.Fatal("not one handshake record") - } - if _, again, _ := helloRecord(record[5:]); len(again) != 0 { - t.Fatalf("the record still has %v", again) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/leaves.go b/src/agent_env/agents/agents/claude-code/egress/leaves.go deleted file mode 100644 index 027ecac92..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/leaves.go +++ /dev/null @@ -1,188 +0,0 @@ -package main - -import ( - "crypto" - "crypto/ecdsa" - "crypto/elliptic" - "crypto/rand" - "crypto/tls" - "crypto/x509" - "crypto/x509/pkix" - "encoding/pem" - "errors" - "math/big" - "net" - "os" - "path/filepath" - "sort" - "strings" - "sync" -) - -// The longest common name X.509 allows (ub-common-name). -const commonNameLimit = 64 - -// Leaves signs the leaves an intercepted client is handed: by this computer's CA when the site verified, else by a CA -// trusted nowhere, so the client refuses it as it would the site. Each is valid over all of its CA's window, so it -// holds wherever the universe goes, and is minted once per name and its alternative names. -type Leaves struct { - ca, untrusted signer - key *ecdsa.PrivateKey - mu sync.Mutex - cache map[string]*tls.Certificate - // order is the cache's keys, oldest first, so it drops the oldest past cacheLimit; a connection keeps its own. - order []string -} - -type signer struct { - cert *x509.Certificate - key crypto.Signer -} - -func LoadLeaves(folder string) (*Leaves, error) { - certPEM, err := os.ReadFile(filepath.Join(folder, "ca.pem")) - if err != nil { - return nil, err - } - keyPEM, err := os.ReadFile(filepath.Join(folder, "ca.key")) - if err != nil { - return nil, err - } - ca, err := parseSigner(certPEM, keyPEM) - if err != nil { - return nil, err - } - return NewLeaves(ca) -} - -func NewLeaves(ca signer) (*Leaves, error) { - untrusted, err := mintCA("Untrusted upstream") - if err != nil { - return nil, err - } - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return nil, err - } - return &Leaves{ca: ca, untrusted: untrusted, key: key, cache: map[string]*tls.Certificate{}}, nil -} - -// For is the leaf for host and the names sans ("DNS:..." or "IP:..."), by this computer's CA when trusted, and -// whether it was minted now. -func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate, bool, error) { - sorted := append([]string{}, sans...) - sort.Strings(sorted) - key := host + "|" + strings.Join(sorted, ",") - by := l.ca - if !trusted { - key, by = "untrusted|"+key, l.untrusted - } - l.mu.Lock() - defer l.mu.Unlock() - if cert, ok := l.cache[key]; ok { - return cert, false, nil - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, - NotBefore: by.cert.NotBefore.Add(LeafInset), - NotAfter: by.cert.NotAfter.Add(-LeafInset), - KeyUsage: x509.KeyUsageDigitalSignature, - ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, - BasicConstraintsValid: true, - } - for _, name := range append([]string{host}, sans...) { - name = strings.TrimPrefix(strings.TrimPrefix(name, "DNS:"), "IP:") - if ip := net.ParseIP(strings.Trim(name, "[]")); ip != nil { - template.IPAddresses = appendIP(template.IPAddresses, ip) - } else if name != "" && !contains(template.DNSNames, name) { - template.DNSNames = append(template.DNSNames, name) - } - } - der, err := x509.CreateCertificate(rand.Reader, template, by.cert, &l.key.PublicKey, by.key) - if err != nil { - return nil, false, err - } - cert := &tls.Certificate{Certificate: [][]byte{der, by.cert.Raw}, PrivateKey: l.key} - l.cache[key] = cert - l.order = append(l.order, key) - for len(l.order) > cacheLimit { - delete(l.cache, l.order[0]) - l.order = l.order[1:] - } - return cert, true, nil -} - -// mintCA is a self-signed CA over the window, its key in memory. -func mintCA(name string) (signer, error) { - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return signer{}, err - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: name}, - NotBefore: CAFrom, - NotAfter: CAUntil, - KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, - BasicConstraintsValid: true, - IsCA: true, - MaxPathLenZero: true, - } - der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) - if err != nil { - return signer{}, err - } - cert, err := x509.ParseCertificate(der) - if err != nil { - return signer{}, err - } - return signer{cert: cert, key: key}, nil -} - -func parseSigner(certPEM, keyPEM []byte) (signer, error) { - block, _ := pem.Decode(certPEM) - if block == nil { - return signer{}, errors.New("no certificate in the CA's PEM") - } - cert, err := x509.ParseCertificate(block.Bytes) - if err != nil { - return signer{}, err - } - block, _ = pem.Decode(keyPEM) - if block == nil { - return signer{}, errors.New("no key in the CA key's PEM") - } - key, err := x509.ParsePKCS8PrivateKey(block.Bytes) - if err != nil { - return signer{}, err - } - s, ok := key.(crypto.Signer) - if !ok { - return signer{}, errors.New("the CA key can't sign") - } - return signer{cert: cert, key: s}, nil -} - -func serial() *big.Int { - n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) - return n -} - -func appendIP(ips []net.IP, ip net.IP) []net.IP { - for _, have := range ips { - if have.Equal(ip) { - return ips - } - } - return append(ips, ip) -} - -func contains(list []string, s string) bool { - for _, have := range list { - if strings.EqualFold(have, s) { - return true - } - } - return false -} diff --git a/src/agent_env/agents/agents/claude-code/egress/main.go b/src/agent_env/agents/agents/claude-code/egress/main.go deleted file mode 100644 index dd9760e99..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/main.go +++ /dev/null @@ -1,117 +0,0 @@ -// egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. -// -// It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and -// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128, or where AGENTENV_EGRESS_PROXY names. Session programs -// reach it through HTTP(S)_PROXY and ALL_PROXY, or a proxy of the base's own that hands it their connections; nothing -// redirects or filters, so it needs no NET_ADMIN and works on any provider. -// -// A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe -// a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is -// intercepted: the site is verified against real time and the public roots, and the client is handed a leaf from this -// computer's CA, valid over every date the clock can show. The upstream handshake is uTLS's, built from the client's -// own ClientHello, so the site sees the client's fingerprint and ALPN offer either way. -// -// egress boot as root at the computer's start: the CA, minted once, its key this user's alone, trusted everywhere -// egress serve as agentenv-egress: serve, one JSON line per connection on stdout -package main - -import ( - "encoding/json" - "fmt" - "net/url" - "os" - "strings" - "time" -) - -const ( - Listen = "127.0.0.1:3128" - ProxyEnv = "AGENTENV_EGRESS_PROXY" - EgressUser = "agentenv-egress" - SessionUser = "user" - Folder = "/etc/agentenv/egress" - PublicCA = "/etc/agentenv/egress-ca.pem" - PublicRoots = "/usr/local/lib/agentenv-egress/roots.pem" - ClockPage = "/dev/shm/agentenv-clock" - Hosts = "/etc/hosts" - HostsBegin = "# agentenv sites" - HostsEnd = "# end agentenv sites" - CAName = "agentenv egress CA" - NSSNickname = "agentenv egress" - Hold = 30 * time.Second - Margin = 8 * 24 * time.Hour - ProbeTTL = time.Hour - ProbeFailedTTL = time.Minute - FirstBytes = 300 * time.Millisecond - HelloTimeout = 10 * time.Second - DialTimeout = 10 * time.Second - - // How long a client has to send its request's head, and how long either handshake of an interception, the - // client's with the proxy and the proxy's with the site, has to finish. - HeadTimeout = 15 * time.Second - HandshakeTimeout = 15 * time.Second - - // The longest request head a client may send, and the most of an error a log row keeps, in bytes. - HeadLimit = 64 << 10 - ErrorLimit = 300 - - // How far inside the CA's dates each leaf it signs is, so no leaf outlasts its CA. - LeafInset = 2 * time.Hour - - // How often the clock page is read, and how many reads may find it mid-write before the last universe stands. - PagePoll = 20 * time.Millisecond - PageReads = 1000 -) - -// The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' -// (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. -var ( - CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) - CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) -) - -func main() { - command := "" - if len(os.Args) > 1 { - command = os.Args[1] - } - switch command { - case "boot": - report, err := boot(defaultBootPaths()) - out, _ := json.Marshal(report) - fmt.Println(string(out)) - if err != nil { - fmt.Fprintln(os.Stderr, "egress boot:", err) - os.Exit(1) - } - case "serve": - listen, err := serveListen(os.Args[2:], os.Getenv(ProxyEnv)) - if err != nil { - fmt.Fprintln(os.Stderr, "egress serve:", err) - os.Exit(2) - } - if err := serveForever(listen); err != nil { - fmt.Fprintln(os.Stderr, "egress serve:", err) - os.Exit(1) - } - default: - fmt.Fprintf(os.Stderr, "usage: egress boot|serve, not %q\n", command) - os.Exit(2) - } -} - -// serveListen is the address serve listens on: the host and port of named, the URL a base whose own proxy holds Listen -// gives its proxies for this one in ProxyEnv, else Listen. -func serveListen(args []string, named string) (string, error) { - if len(args) != 0 { - return "", fmt.Errorf("usage: egress serve, not %q", args) - } - if named == "" { - return Listen, nil - } - u, err := url.Parse(named) - if err != nil || u.Scheme != "http" || u.Hostname() == "" || u.Port() == "" || strings.Trim(u.Path, "/") != "" { - return "", fmt.Errorf("%s is http://HOST:PORT, not %q", ProxyEnv, named) - } - return u.Host, nil -} diff --git a/src/agent_env/agents/agents/claude-code/egress/main_test.go b/src/agent_env/agents/agents/claude-code/egress/main_test.go deleted file mode 100644 index 77f80844e..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/main_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "testing" - -func TestServeListensWhereTheBaseNamesItElseOnListen(t *testing.T) { - for _, tc := range []struct { - args []string - named string - want string - ok bool - }{ - {nil, "", Listen, true}, - {nil, "http://127.0.0.1:3130", "127.0.0.1:3130", true}, - {nil, "http://127.0.0.1:3130/", "127.0.0.1:3130", true}, - {nil, "http://[::1]:3130", "[::1]:3130", true}, - {nil, "127.0.0.1:3130", "", false}, - {nil, "http://127.0.0.1", "", false}, - {nil, "https://127.0.0.1:3130", "", false}, - {nil, "http://127.0.0.1:3130/proxy", "", false}, - {[]string{"--listen", "127.0.0.1:3130"}, "", "", false}, - } { - got, err := serveListen(tc.args, tc.named) - if (err == nil) != tc.ok || got != tc.want { - t.Errorf("serveListen(%q, %q) = %q, %v; want %q, ok %v", tc.args, tc.named, got, err, tc.want, tc.ok) - } - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy.go b/src/agent_env/agents/agents/claude-code/egress/proxy.go deleted file mode 100644 index 3755399b9..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/proxy.go +++ /dev/null @@ -1,598 +0,0 @@ -package main - -import ( - "bufio" - "context" - "crypto/tls" - "crypto/x509" - "encoding/json" - "errors" - "fmt" - "io" - "net" - "net/http" - "net/netip" - "net/url" - "os" - "strconv" - "strings" - "sync" - "time" -) - -type Proxy struct { - Leaves *Leaves - Universe interface { - View(time.Time) (bool, []float64) - } - // Roots are the public roots a site is verified against, as the image had them before any agentenv CA joined. - Roots *x509.CertPool - // Resolve, Allowed, Routed and Dial default to the system's resolver, a global address, the hosts file's agentenv - // sites block and a TCP dial; tests replace them. - Resolve func(ctx context.Context, host string) ([]netip.Addr, error) - Allowed func(netip.Addr) bool - Routed func() map[string]string - Dial func(dst string) (net.Conn, error) - Log func(row map[string]any) - probes probes -} - -func serveForever(listen string) error { - leaves, err := LoadLeaves(Folder) - if err != nil { - return fmt.Errorf("the CA (has boot run?): %w", err) - } - roots, err := os.ReadFile(PublicRoots) - if err != nil { - return err - } - pool := x509.NewCertPool() - if !pool.AppendCertsFromPEM(roots) { - return fmt.Errorf("no certificates in %s", PublicRoots) - } - universe := NewUniverse(ClockPage) - go universe.Poll() - ln, err := net.Listen("tcp", listen) - if err != nil { - return err - } - p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} - p.Log(map[string]any{"event": "serving", "listen": listen, "uid": os.Getuid(), "pid": os.Getpid()}) - return p.Serve(ln) -} - -func jsonLines(w io.Writer) func(map[string]any) { - var mu sync.Mutex - encoder := json.NewEncoder(w) - return func(row map[string]any) { - row["t"] = float64(time.Now().UnixMilli()) / 1e3 - mu.Lock() - defer mu.Unlock() - encoder.Encode(row) - } -} - -func (p *Proxy) Serve(ln net.Listener) error { - for { - conn, err := ln.Accept() - if err != nil { - if errors.Is(err, net.ErrClosed) { - return nil - } - return err - } - go p.handle(conn) - } -} - -func (p *Proxy) log(row map[string]any) { - if p.Log != nil { - p.Log(row) - } -} - -func (p *Proxy) handle(conn net.Conn) { - start := time.Now() - row := map[string]any{"client": conn.RemoteAddr().String()} - defer func() { - if r := recover(); r != nil { - row["error"] = fmt.Sprint("panic: ", r) - p.log(row) - } - conn.Close() - }() - br := bufio.NewReaderSize(conn, HeadLimit) - conn.SetReadDeadline(start.Add(HeadTimeout)) - method, target, version, headers, err := readHead(br) - if err != nil { - row["error"] = truncate("request: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["method"] = method - if method == http.MethodConnect { - host, port := splitAuthority(target, 443) - row["host"], row["port"] = host, port - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - if _, err := io.WriteString(conn, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { - return - } - p.tunnel(conn, br, host, port, dsts, why, row, start) - return - } - u, err := url.Parse(target) - if err != nil || u.Scheme != "http" || u.Host == "" { - row["mode"], row["error"] = "refused", "not an absolute http URL" - reply(conn, "400 Bad Request", "egress: CONNECT, or an absolute http:// URL\n") - p.log(row) - return - } - host, port := splitAuthority(u.Host, 80) - row["host"], row["port"], row["mode"] = host, port, "http" - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate(err.Error(), ErrorLimit) - reply(conn, "502 Bad Gateway", "egress: "+err.Error()+"\n") - p.log(row) - return - } - defer up.Close() - row["dst"] = dst - upgrade := false - for _, h := range headers { - if headerName(h) == "upgrade" { - upgrade = true - } - } - // The client's own head, but for its request line, which names the path alone, and the headers meant for this - // proxy; a request that isn't an upgrade closes once answered, so its connection can't carry one for another site. - var head strings.Builder - head.WriteString(method + " " + u.RequestURI() + " " + version + "\r\n") - for _, h := range headers { - switch headerName(h) { - case "proxy-connection", "proxy-authorization": - continue - case "connection", "keep-alive": - if !upgrade { - continue - } - } - head.WriteString(h + "\r\n") - } - if upgrade { - row["mode"] = "upgrade" - } else { - head.WriteString("Connection: close\r\n") - } - head.WriteString("\r\n") - conn.SetReadDeadline(time.Time{}) - if _, err := io.WriteString(up, head.String()); err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return - } - p.log(row) - // Both ways at once: a body the client sends once told to continue, or an upgraded connection's frames. - go func() { - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) -} - -// readHead is a request's head, read through its blank line: its request line's parts and its header lines. -func readHead(r *bufio.Reader) (string, string, string, []string, error) { - var lines []string - size := 0 - for { - line, err := r.ReadString('\n') - size += len(line) - if size > HeadLimit { - return "", "", "", nil, errors.New("request head too long") - } - if err != nil { - return "", "", "", nil, err - } - line = strings.TrimRight(line, "\r\n") - if line == "" { - if len(lines) == 0 { - continue - } - break - } - lines = append(lines, line) - } - parts := strings.SplitN(lines[0], " ", 3) - if len(parts) != 3 { - return "", "", "", nil, fmt.Errorf("malformed request line %q", truncate(lines[0], 100)) - } - return parts[0], parts[1], parts[2], lines[1:], nil -} - -func headerName(line string) string { - name, _, _ := strings.Cut(line, ":") - return strings.ToLower(strings.TrimSpace(name)) -} - -// destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for -// a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), -// "global" for a name whose every address is global, IPv4 first. Anything else is refused. -func (p *Proxy) destination(host string, port int) ([]string, string, error) { - host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") - routed := p.Routed - if routed == nil { - routed = routedSites - } - if address, ok := routed()[host]; ok { - return []string{net.JoinHostPort(address, strconv.Itoa(port))}, "routed", nil - } - resolve := p.Resolve - if resolve == nil { - resolve = func(ctx context.Context, host string) ([]netip.Addr, error) { - return net.DefaultResolver.LookupNetIP(ctx, "ip", host) - } - } - allowed := p.Allowed - if allowed == nil { - allowed = isGlobal - } - ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) - defer cancel() - addresses, err := resolve(ctx, host) - if err != nil { - return nil, "", fmt.Errorf("%s doesn't resolve: %w", host, err) - } - if len(addresses) == 0 { - return nil, "", fmt.Errorf("%s has no address", host) - } - var private []string - var v4, v6 []string - for _, a := range addresses { - a = a.Unmap() - dst := netip.AddrPortFrom(a, uint16(port)).String() - switch { - case !allowed(a): - private = append(private, a.String()) - case a.Is4(): - v4 = append(v4, dst) - default: - v6 = append(v6, dst) - } - } - if len(private) > 0 { - return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) - } - return append(v4, v6...), "global", nil -} - -// notGlobal are the special-purpose blocks (IANA's registries) no global address is in. -var notGlobal = func() []netip.Prefix { - var out []netip.Prefix - for _, s := range []string{ - "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", - "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", - "224.0.0.0/4", "240.0.0.0/4", - "::/128", "::1/128", "64:ff9b:1::/48", "100::/64", "2001::/23", "2001:db8::/32", "fc00::/7", "fe80::/10", - "ff00::/8", - } { - out = append(out, netip.MustParsePrefix(s)) - } - return out -}() - -func isGlobal(a netip.Addr) bool { - a = a.Unmap() - if !a.IsValid() { - return false - } - for _, block := range notGlobal { - if block.Contains(a) { - return false - } - } - return true -} - -// routedSites is the hosts file's agentenv sites block, name to address: root's, so the session user can't add to it. -func routedSites() map[string]string { - out := map[string]string{} - data, err := os.ReadFile(Hosts) - if err != nil { - return out - } - inside := false - for _, line := range strings.Split(string(data), "\n") { - text := strings.TrimSpace(line) - switch { - case text == HostsBegin: - inside = true - case text == HostsEnd: - inside = false - case inside && text != "" && !strings.HasPrefix(text, "#"): - fields := strings.Fields(text) - for _, name := range fields[1:] { - out[strings.ToLower(name)] = fields[0] - } - } - } - return out -} - -func (p *Proxy) tunnel(conn net.Conn, br *bufio.Reader, host string, port int, dsts []string, why string, - row map[string]any, start time.Time) { - wait := FirstBytes - if port == 443 { - wait = HelloTimeout - } - conn.SetReadDeadline(time.Now().Add(wait)) - first, err := br.Peek(1) - if err != nil && !isTimeout(err) { - row["error"] = "closed before sending" - p.log(row) - return - } - if len(first) == 0 || first[0] != recordHandshake || why == "routed" { - row["mode"], row["decision"] = "pass", "not TLS" - if len(first) > 0 && first[0] == recordHandshake { - row["mode"], row["decision"] = "splice", "routed" - } - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - conn.SetReadDeadline(time.Now().Add(HelloTimeout)) - hello, err := clientHello(br, time.Now().Add(HelloTimeout)) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - sni, offered, err := parseHello(hello) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["sni"], row["alpn_offered"] = sni, offered - verifyName := sni - if verifyName == "" { - verifyName = strings.Trim(host, "[]") - } - armed, universes := p.Universe.View(time.Now()) - if !armed { - row["decision"], row["mode"] = "real", "splice" - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - row["universes"] = rounded(universes) - // Each address in turn, by its own probe: one that doesn't answer is passed over, so the connection goes to a - // server whose own certificate decided it. - var unreachable []string - for _, dst := range p.preferChecked(dsts, sni) { - delete(row, "probe_error") - delete(row, "upstream_valid") - seen, probed := p.probeAt(dst, sni, verifyName, hello) - if seen.unreachable { - unreachable = append(unreachable, seen.problem) - continue - } - row["probed"] = probed - decision := "outside" - switch { - case seen.failed: - // a handshake only the client can finish, as one asking for its certificate: the client decides - decision, row["probe_error"] = "probe_failed", seen.problem - case !seen.verified: - decision, row["probe_error"] = "unverified", seen.problem - case inside(seen.from, seen.until, universes): - decision = "inside" - } - if seen.verified { - row["upstream_valid"] = []int64{seen.from.Unix(), seen.until.Unix()} - } - row["decision"] = decision - var dialled bool - if decision == "inside" || decision == "probe_failed" { - row["mode"] = "splice" - dialled = p.splice(conn, br, []string{dst}, row, start) - } else { - row["mode"] = "intercept" - dialled = p.intercept(conn, br, dst, sni, verifyName, hello, row, start) - } - if dialled { - return - } - // it stopped answering since its probe: forget it, and go on to the next - p.forget(dst, sni) - unreachable = append(unreachable, fmt.Sprint(row["error"])) - delete(row, "error") - } - row["error"] = truncate("upstream: no address answered: "+strings.Join(unreachable, "; "), ErrorLimit) - p.log(row) -} - -// preferChecked is dsts with those whose fresh probe answered first, each group in its own order, so an address -// already found working isn't kept waiting behind one whose probe has to be made again. -func (p *Proxy) preferChecked(dsts []string, sni string) []string { - p.probes.mu.Lock() - defer p.probes.mu.Unlock() - var checked, rest []string - for _, dst := range dsts { - if hit, ok := p.probes.cache[dst+"|"+sni]; ok && hit.fresh() && !hit.unreachable { - checked = append(checked, dst) - } else { - rest = append(rest, dst) - } - } - return append(checked, rest...) -} - -// splice relays the connection to the first of dsts that answers, untouched; false, with the error in row, when none -// does. -func (p *Proxy) splice(conn net.Conn, br *bufio.Reader, dsts []string, row map[string]any, start time.Time) bool { - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - defer up.Close() - row["dst"] = dst - conn.SetReadDeadline(time.Time{}) - row["upstream_ms"] = time.Since(start).Milliseconds() - p.log(row) - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) - closeWrite(conn) - wg.Wait() - return true -} - -// intercept hands the client a leaf of this computer's CA and relays the plaintext to dst over a handshake of the -// proxy's own; false, with the error in row, when dst doesn't answer. -func (p *Proxy) intercept(conn net.Conn, br *bufio.Reader, dst, sni, verifyName string, hello []byte, - row map[string]any, start time.Time) bool { - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - if errors.As(err, &unanswered) { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - defer uc.NetConn().Close() - row["dst"] = dst - row["alpn"], row["verified"], row["upstream_tls"] = found.ALPN, found.Verified, []string{found.Version, found.Cipher} - row["upstream_ms"] = time.Since(start).Milliseconds() - if len(found.Dropped) > 0 { - row["dropped_extensions"] = found.Dropped - } - var sans []string - if found.Verified { - // the site's own names, as its leaf lists them, so the client pools connections as it would with the site's - // certificate - for _, name := range found.SANs { - if sni == "" || strings.HasPrefix(name, "DNS:") { - sans = append(sans, name) - } - } - if sni == "" { - sans = append(sans, "IP:"+strings.Trim(hostOf(dst), "[]")) - } - } else { - row["upstream_verify"] = truncate(found.VerifyErr, 200) - } - cert, minted, err := p.Leaves.For(verifyName, sans, found.Verified) - if err != nil { - row["error"] = truncate("leaf: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - row["minted"] = minted - config := &tls.Config{Certificates: []tls.Certificate{*cert}, SessionTicketsDisabled: true} - if found.ALPN != "" { - config.NextProtos = []string{found.ALPN} - } - client := tls.Server(&peeked{Conn: conn, r: br}, config) - conn.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := client.Handshake(); err != nil { - row["error"] = truncate("client handshake: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - conn.SetDeadline(time.Time{}) - row["handshake_ms"] = time.Since(start).Milliseconds() - p.log(row) - done := make(chan struct{}, 2) - go func() { - io.Copy(uc, client) - done <- struct{}{} - }() - go func() { - io.Copy(client, uc) - done <- struct{}{} - }() - <-done - client.Close() - uc.NetConn().Close() - <-done - return true -} - -// peeked is a connection whose reads come through the reader that peeked its first bytes. -type peeked struct { - net.Conn - r *bufio.Reader -} - -func (c *peeked) Read(b []byte) (int, error) { return c.r.Read(b) } - -func closeWrite(c net.Conn) { - if cw, ok := c.(interface{ CloseWrite() error }); ok { - cw.CloseWrite() - } -} - -func isTimeout(err error) bool { - var ne net.Error - return errors.As(err, &ne) && ne.Timeout() -} - -func reply(conn net.Conn, status, body string) { - fmt.Fprintf(conn, "HTTP/1.1 %s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", - status, len(body), body) -} - -func splitAuthority(authority string, port int) (string, int) { - host, p, err := net.SplitHostPort(authority) - if err != nil { - return strings.Trim(authority, "[]"), port - } - n, err := strconv.Atoi(p) - if err != nil { - return host, port - } - return host, n -} - -func hostOf(hostport string) string { - host, _, err := net.SplitHostPort(hostport) - if err != nil { - return hostport - } - return host -} - -func rounded(values []float64) []int64 { - out := make([]int64, len(values)) - for i, v := range values { - out[i] = int64(v) - } - return out -} diff --git a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go b/src/agent_env/agents/agents/claude-code/egress/proxy_test.go deleted file mode 100644 index c8e57b3e7..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/proxy_test.go +++ /dev/null @@ -1,564 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "context" - "crypto/tls" - "crypto/x509" - "fmt" - "io" - "net" - "net/http" - "net/http/httptest" - "net/netip" - "net/url" - "strings" - "sync" - "testing" - "time" -) - -type fixedUniverse struct { - armed bool - universes []float64 -} - -func (f fixedUniverse) View(time.Time) (bool, []float64) { return f.armed, f.universes } - -func TestOnlyDestinationsWhoseEveryAddressIsGlobalAreDialled(t *testing.T) { - refused := []string{"10.0.0.1", "10.255.255.254", "172.16.0.1", "172.31.255.1", "192.168.1.1", "127.0.0.1", - "127.1.2.3", "0.0.0.0", "169.254.169.254", "169.254.1.1", "100.64.0.1", "100.127.255.254", "192.0.2.1", - "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::", "fd00::1", "fe80::1", "::ffff:10.0.0.1", - "2001:db8::1"} - if len(refused) != 22 { - t.Fatal(len(refused)) - } - p := &Proxy{Routed: func() map[string]string { return nil }} - for _, address := range refused { - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr(address)}, nil - } - if _, _, err := p.destination("somewhere.example", 443); err == nil { - t.Errorf("%s dialled", address) - } - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("2606:4700::1111"), netip.MustParseAddr("1.1.1.1")}, nil - } - dst, why, err := p.destination("one.one.one.one", 443) - if err != nil || strings.Join(dst, " ") != "1.1.1.1:443 [2606:4700::1111]:443" || why != "global" { - t.Fatalf("a public name, IPv4 first: %q %q %v", dst, why, err) - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("10.0.0.1")}, nil - } - if _, _, err := p.destination("split.example", 443); err == nil || !strings.Contains(err.Error(), "10.0.0.1") { - t.Fatalf("a name with one private address: %v", err) - } - p.Routed = func() map[string]string { return map[string]string{"notes.test": "127.0.0.1"} } - if dst, why, err := p.destination("Notes.Test.", 443); err != nil || strings.Join(dst, " ") != "127.0.0.1:443" || - why != "routed" { - t.Fatalf("a routed site: %q %q %v", dst, why, err) - } -} - -// harness is a proxy in front of the site server, which it reaches as example.com, and a client whose clock is at -// clientTime. -type harness struct { - t *testing.T - site *httptest.Server - proxy *Proxy - url *url.URL - ca signer - clientCert *tls.Certificate - rows []map[string]any - rowsMu sync.Mutex -} - -func newHarness(t *testing.T, site *httptest.Server, universe fixedUniverse, trustSite bool) *harness { - t.Helper() - ca, err := mintCA(CAName) - if err != nil { - t.Fatal(err) - } - leaves, err := NewLeaves(ca) - if err != nil { - t.Fatal(err) - } - roots := x509.NewCertPool() - if trustSite && site.Certificate() != nil { - roots.AddCert(site.Certificate()) - } - h := &harness{t: t, site: site, ca: ca} - h.proxy = &Proxy{ - Leaves: leaves, Universe: universe, Roots: roots, - Resolve: func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil - }, - Allowed: func(netip.Addr) bool { return true }, - Routed: func() map[string]string { return nil }, - Log: func(row map[string]any) { - h.rowsMu.Lock() - h.rows = append(h.rows, row) - h.rowsMu.Unlock() - }, - } - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { ln.Close() }) - go h.proxy.Serve(ln) - h.url, _ = url.Parse("http://" + ln.Addr().String()) - return h -} - -func (h *harness) get(scheme string, clientTime time.Time, http2 bool) (*http.Response, string, error) { - roots := x509.NewCertPool() - if h.site.Certificate() != nil { - roots.AddCert(h.site.Certificate()) - } - roots.AddCert(h.ca.cert) - config := &tls.Config{RootCAs: roots, Time: func() time.Time { return clientTime }} - if h.clientCert != nil { - config.Certificates = []tls.Certificate{*h.clientCert} - } - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ForceAttemptHTTP2: http2, TLSClientConfig: config} - defer transport.CloseIdleConnections() - _, port, _ := net.SplitHostPort(h.site.Listener.Addr().String()) - resp, err := (&http.Client{Transport: transport, Timeout: 10 * time.Second}).Get( - fmt.Sprintf("%s://example.com:%s/hello", scheme, port)) - if err != nil { - return nil, "", err - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - return resp, string(body), err -} - -func (h *harness) lastRow() map[string]any { - deadline := time.Now().Add(2 * time.Second) - for time.Now().Before(deadline) { - h.rowsMu.Lock() - n := len(h.rows) - h.rowsMu.Unlock() - if n > 0 { - break - } - time.Sleep(10 * time.Millisecond) - } - h.rowsMu.Lock() - defer h.rowsMu.Unlock() - if len(h.rows) == 0 { - h.t.Fatal("no row logged") - } - return h.rows[len(h.rows)-1] -} - -func site(t *testing.T, http2 bool) *httptest.Server { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello over %s", r.Proto) - })) - s.EnableHTTP2 = http2 - s.StartTLS() - t.Cleanup(s.Close) - return s -} - -func TestOnRealTimeTheClientsBytesReachTheSiteUntouched(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - resp, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatal("the client didn't see the site's own certificate") - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "real" { - t.Fatalf("%v", row) - } -} - -func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t *testing.T) { - s := site(t, true) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now, now}}, true) - resp, _, err := h.get("https", time.Now(), true) - if err != nil || !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatalf("inside: %v", err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "inside" { - t.Fatalf("%v", row) - } - - // httptest's certificate ends in 2084: a universe in 2090 is outside it. - later := time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC) - u := float64(later.Unix()) - h = newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u, u}}, true) - resp, body, err := h.get("https", later, true) - if err != nil || body != "hello over HTTP/2.0" { - t.Fatalf("outside: %q %v", body, err) - } - leaf := resp.TLS.PeerCertificates[0] - if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { - t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) - } - row := h.lastRow() - if row["mode"] != "intercept" || row["decision"] != "outside" || row["alpn"] != "h2" || row["verified"] != true { - t.Fatalf("%v", row) - } -} - -func TestASiteThatDoesntVerifyIsHandedOnUnderACATrustedNowhere(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, false) - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "certificate") { - t.Fatalf("the client took it: %v", err) - } - if row := h.lastRow(); row["decision"] != "unverified" { - t.Fatalf("%v", row) - } -} - -func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "plain %s %s ua=%q", r.Method, r.URL.Path, r.UserAgent()) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, body, err := h.get("http", time.Now(), false) - if err != nil || !strings.HasPrefix(body, "plain GET /hello") { - t.Fatalf("%q %v", body, err) - } -} - -func TestARefusedDestinationIsAnswered403(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Allowed = isGlobal - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "Forbidden") { - t.Fatalf("%v", err) - } - if row := h.lastRow(); row["mode"] != "refused" { - t.Fatalf("%v", row) - } -} - -func TestEveryCheckedAddressIsTriedInTurn(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "127.0.0.1:") { - t.Fatalf("%v", row) - } -} - -func TestAnUpgradeOverPlainHTTPRelaysBothWays(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Upgrade") != "echo" || !strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") { - w.WriteHeader(http.StatusBadRequest) - return - } - conn, rw, _ := w.(http.Hijacker).Hijack() - defer conn.Close() - rw.WriteString("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: echo\r\n\r\n") - rw.Flush() - line, _ := rw.ReadString('\n') - rw.WriteString(line) - rw.Flush() - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - conn, err := net.Dial("tcp", h.url.Host) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - conn.SetDeadline(time.Now().Add(5 * time.Second)) - fmt.Fprintf(conn, "GET http://example.com:%s/ws HTTP/1.1\r\nHost: example.com:%s\r\nConnection: Upgrade\r\n"+ - "Upgrade: echo\r\n\r\n", port, port) - br := bufio.NewReader(conn) - resp, err := http.ReadResponse(br, nil) - if err != nil || resp.StatusCode != http.StatusSwitchingProtocols { - t.Fatalf("%v %v", resp, err) - } - fmt.Fprint(conn, "ping\n") - if line, err := br.ReadString('\n'); err != nil || line != "ping\n" { - t.Fatalf("%q %v", line, err) - } -} - -func TestAnUploadThatWaitsFor100ContinueGoesThroughAtOnce(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - body, _ := io.ReadAll(r.Body) - fmt.Fprint(w, len(body)) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - req, _ := http.NewRequest("POST", "http://example.com:"+port+"/up", bytes.NewReader(make([]byte, 100000))) - req.Header.Set("Expect", "100-continue") - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ExpectContinueTimeout: 10 * time.Second} - defer transport.CloseIdleConnections() - started := time.Now() - resp, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(req) - if err != nil { - t.Fatal(err) - } - body, _ := io.ReadAll(resp.Body) - resp.Body.Close() - if string(body) != "100000" || time.Since(started) > 5*time.Second { - t.Fatalf("%q after %s", body, time.Since(started)) - } -} - -func TestEveryCertificateOfTheVerifiedChainBoundsPassingThrough(t *testing.T) { - year := func(y int) time.Time { return time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) } - leaf := &x509.Certificate{NotBefore: year(2020), NotAfter: year(2030)} - intermediate := &x509.Certificate{NotBefore: year(2019), NotAfter: year(2025)} - root := &x509.Certificate{NotBefore: year(2000), NotAfter: year(2040)} - from, until := chainWindow([][]*x509.Certificate{{leaf, intermediate, root}}) - if !from.Equal(year(2020)) || !until.Equal(year(2025)) { - t.Fatalf("%s to %s", from, until) - } - if inside(from, until, []float64{float64(year(2027).Unix())}) { - t.Fatal("a universe past the intermediate's end passed through") - } -} - -func TestASiteAskingForTheClientsCertificateIsPassedThroughForTheClientToAnswer(t *testing.T) { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello %d certificate", len(r.TLS.PeerCertificates)) - })) - s.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, MaxVersion: tls.VersionTLS12} - s.StartTLS() - t.Cleanup(s.Close) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - client, err := mintCA("client") - if err != nil { - t.Fatal(err) - } - h.clientCert = &tls.Certificate{Certificate: [][]byte{client.cert.Raw}, PrivateKey: client.key} - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello 1 certificate" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "probe_failed" { - t.Fatalf("%v", row) - } -} - -func TestTheProbeAndLeafCachesStayWithinTheirLimit(t *testing.T) { - limit := cacheLimit - cacheLimit = 3 - defer func() { cacheLimit = limit }() - ca, _ := mintCA(CAName) - leaves, _ := NewLeaves(ca) - for i := 0; i < 10; i++ { - if _, _, err := leaves.For(fmt.Sprintf("site%d.test", i), nil, true); err != nil { - t.Fatal(err) - } - } - if len(leaves.cache) != 3 || leaves.cache["site9.test|"] == nil { - t.Fatalf("%d leaves kept", len(leaves.cache)) - } - p := &Proxy{Dial: func(string) (net.Conn, error) { return nil, fmt.Errorf("unreachable") }} - for i := 0; i < 10; i++ { - p.probeAt(fmt.Sprintf("192.0.2.%d:443", i), "", "x", goHello(t, "x", nil)) - } - if len(p.probes.cache) != 3 { - t.Fatalf("%d probes kept", len(p.probes.cache)) - } -} - -func TestAConnectionTheProbeLetsThroughGoesToTheServerItProbed(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - var mu sync.Mutex - var dialled []string - h.proxy.Dial = func(dst string) (net.Conn, error) { - mu.Lock() - dialled = append(dialled, dst) - mu.Unlock() - return net.DialTimeout("tcp", dst, DialTimeout) - } - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - mu.Lock() - defer mu.Unlock() - // the probe tried 127.0.0.2, then reached 127.0.0.1; the connection went straight to 127.0.0.1 - if row["decision"] != "inside" || len(dialled) != 3 || !strings.HasPrefix(dialled[2], "127.0.0.1:") { - t.Fatalf("%v %v", row, dialled) - } - if hit := h.proxy.probes.cache[dialled[0]+"|example.com"]; !hit.unreachable || hit.verified { - t.Fatalf("the address that didn't answer is cached as %+v", hit) - } -} - -func TestASlowProbeHoldsUpNoOtherSite(t *testing.T) { - release := make(chan struct{}) - p := &Proxy{Dial: func(dst string) (net.Conn, error) { - if strings.HasPrefix(dst, "192.0.2.1:") { - <-release - } - return nil, fmt.Errorf("unreachable") - }} - hello := goHello(t, "x", nil) - p.probeAt("192.0.2.2:443", "", "x", hello) - go p.probeAt("192.0.2.1:443", "", "x", hello) - time.Sleep(50 * time.Millisecond) - started := time.Now() - if _, probed := p.probeAt("192.0.2.2:443", "", "x", hello); probed || time.Since(started) > 100*time.Millisecond { - t.Fatalf("a cached site waited %s behind a slow one (probed again: %v)", time.Since(started), probed) - } - close(release) -} - -// standIns resolves example.com to its addresses and dials each to the site unless it's down; both may change -// between requests. -type standIns struct { - mu sync.Mutex - addresses []string - down map[string]bool -} - -func (s *standIns) set(down []string, addresses ...string) { - s.mu.Lock() - defer s.mu.Unlock() - s.addresses, s.down = addresses, map[string]bool{} - for _, a := range down { - s.down[a] = true - } -} - -func newStandIns(h *harness, addresses ...string) *standIns { - s := &standIns{addresses: addresses, down: map[string]bool{}} - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - s.mu.Lock() - defer s.mu.Unlock() - var out []netip.Addr - for _, a := range s.addresses { - out = append(out, netip.MustParseAddr(a)) - } - return out, nil - } - h.proxy.Dial = func(dst string) (net.Conn, error) { - host, _, _ := net.SplitHostPort(dst) - s.mu.Lock() - down := s.down[host] - s.mu.Unlock() - if down { - return nil, fmt.Errorf("%s is down", host) - } - return net.DialTimeout("tcp", h.site.Listener.Addr().String(), DialTimeout) - } - return s -} - -func TestAnAddressThatStopsAnsweringAfterItsProbeGivesWayToTheNext(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - addresses := newStandIns(h, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.1:") { - t.Fatalf("%v", row) - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["decision"] != "inside" { - t.Fatalf("%v", row) - } -} - -func TestAnAddressThatDidntAnswerLeavesANewAddressToItsOwnProbe(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - addresses := newStandIns(h) - addresses.set([]string{"198.51.100.1"}, "198.51.100.1") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err == nil { - t.Fatal("reached a site none of whose addresses answer") - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - if !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["mode"] != "intercept" || row["decision"] != "outside" { - t.Fatalf("the new address wasn't judged by its own certificate: %v", row) - } -} - -func TestAClientHelloWhoseFieldsOverrunItIsRefusedNotPanickedOn(t *testing.T) { - hello := goHello(t, "example.com", nil) - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(hello[p])<<8 | int(hello[p+1]) - bad := append([]byte{}, hello...) - bad[p] = 0xff // compression methods' length, past the message's end - bad = bad[:p+3] - if _, _, err := parseHello(bad); err == nil { - t.Fatal("parseHello took it") - } - if _, _, err := helloRecord(bad); err == nil { - t.Fatal("helloRecord took it") - } -} - -func TestAProbeThatPanicsLeavesNoOneWaitingOnIt(t *testing.T) { - p := &Proxy{Dial: func(string) (net.Conn, error) { panic("dial") }} - hello := goHello(t, "x", nil) - probe := func() (panicked bool) { - defer func() { panicked = recover() != nil }() - p.probeAt("192.0.2.9:443", "", "x", hello) - return false - } - if !probe() { - t.Fatal("the probe didn't panic") - } - done := make(chan bool) - go func() { done <- probe() }() - select { - case <-done: - case <-time.After(2 * time.Second): - t.Fatal("a second probe waited on the one that panicked") - } -} - -func TestAnAddressAlreadyFoundWorkingIsTriedBeforeOnesNotYetProbed(t *testing.T) { - p := &Proxy{} - p.probes.cache = map[string]probe{ - "[2001:db8::1]:443|x": {verified: true, at: time.Now()}, - "192.0.2.1:443|x": {unreachable: true, at: time.Now().Add(-2 * ProbeFailedTTL)}, - "192.0.2.3:443|x": {unreachable: true, at: time.Now()}, - } - got := p.preferChecked([]string{"192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443", "[2001:db8::1]:443"}, "x") - want := []string{"[2001:db8::1]:443", "192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443"} - if strings.Join(got, " ") != strings.Join(want, " ") { - t.Fatalf("%v", got) - } -} diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex deleted file mode 100644 index 2aecf27e8..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/testdata/page-armed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000040000000000000000008ab9359ae51515cd68be7e91e51800000000000000400100000000000000070000000000000002b1a878f27237d300f2052a0100000000f2052a01000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex b/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex deleted file mode 100644 index 7f6a95f54..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/testdata/page-unarmed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000020000000000000000000000000000000000000000000000000000000000f03f00000000000000000300000000000000147c4a7fb979c7a100000000000000000000000000000000 diff --git a/src/agent_env/agents/agents/claude-code/egress/upstream.go b/src/agent_env/agents/agents/claude-code/egress/upstream.go deleted file mode 100644 index bad2ea7db..000000000 --- a/src/agent_env/agents/agents/claude-code/egress/upstream.go +++ /dev/null @@ -1,271 +0,0 @@ -package main - -import ( - "crypto/x509" - "errors" - "fmt" - "net" - "strings" - "sync" - "time" - - utls "github.com/refraction-networking/utls" -) - -// upstream is what a handshake with a site found: the protocol it picked, its leaf's validity and names, and whether -// that leaf verifies against real time and the public roots. -type upstream struct { - // Dst is the address the handshake reached. - Dst string - ALPN string - Verified bool - VerifyErr string - NotBefore time.Time - NotAfter time.Time - SANs []string - Version string - Cipher string - Dropped []uint16 -} - -// handshake is uTLS's handshake with the site at dst, built from the client's own ClientHello, so the site sees the -// client's fingerprint and ALPN offer. Records go full-size from the start and the connection closes without a -// close_notify, as Chromium's BoringSSL does. -func (p *Proxy) handshake(dsts []string, sni, verifyName string, hello []byte) (*utls.UConn, upstream, error) { - var found upstream - record, gone, err := helloRecord(hello) - if err != nil { - return nil, found, err - } - found.Dropped = gone - spec, err := (&utls.Fingerprinter{AllowBluntMimicry: true}).FingerprintClientHello(record) - if err != nil { - return nil, found, fmt.Errorf("fingerprint: %w", err) - } - raw, dst, err := p.dialAny(dsts) - if err != nil { - return nil, found, dialError{err} - } - found.Dst = dst - uc := utls.UClient(raw, &utls.Config{ServerName: sni, InsecureSkipVerify: true, OmitEmptyPsk: true, - DynamicRecordSizingDisabled: true}, utls.HelloCustom) - if err := uc.ApplyPreset(spec); err != nil { - raw.Close() - return nil, found, fmt.Errorf("apply: %w", err) - } - raw.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := uc.Handshake(); err != nil { - raw.Close() - return nil, found, fmt.Errorf("handshake: %w", err) - } - raw.SetDeadline(time.Time{}) - st := uc.ConnectionState() - found.ALPN, found.Version, found.Cipher = st.NegotiatedProtocol, fmt.Sprintf("%#04x", st.Version), - utls.CipherSuiteName(st.CipherSuite) - if len(st.PeerCertificates) == 0 { - found.VerifyErr = "no certificate" - return uc, found, nil - } - leaf := st.PeerCertificates[0] - found.NotBefore, found.NotAfter = leaf.NotBefore, leaf.NotAfter - for _, name := range leaf.DNSNames { - found.SANs = append(found.SANs, "DNS:"+name) - } - for _, ip := range leaf.IPAddresses { - found.SANs = append(found.SANs, "IP:"+ip.String()) - } - intermediates := x509.NewCertPool() - for _, c := range st.PeerCertificates[1:] { - intermediates.AddCert(c) - } - chains, err := leaf.Verify(x509.VerifyOptions{Roots: p.Roots, Intermediates: intermediates, DNSName: verifyName, - CurrentTime: time.Now()}) - if err != nil { - found.VerifyErr = err.Error() - } else { - found.Verified = true - found.NotBefore, found.NotAfter = chainWindow(chains) - } - return uc, found, nil -} - -// chainWindow is the dates every certificate of every verified chain is valid between: a client building any of -// those chains takes the site only then, so passing it through is safe only inside them. -func chainWindow(chains [][]*x509.Certificate) (time.Time, time.Time) { - var from, until time.Time - for _, chain := range chains { - for _, cert := range chain { - if from.IsZero() || cert.NotBefore.After(from) { - from = cert.NotBefore - } - if until.IsZero() || cert.NotAfter.Before(until) { - until = cert.NotAfter - } - } - } - return from, until -} - -// probe is what a handshake of the proxy's own found of one address of a site: the dates its verified chain shares, -// or that it didn't answer, or that its handshake didn't finish. It's cached per (address, SNI): a finished one for -// ProbeTTL, the others for ProbeFailedTTL, since they may be a moment's trouble. -type probe struct { - from, until time.Time - verified bool - // failed is a handshake that didn't finish; unreachable, an address that didn't answer. - failed, unreachable bool - at time.Time - problem string -} - -func (hit probe) fresh() bool { - if hit.failed || hit.unreachable { - return time.Since(hit.at) < ProbeFailedTTL - } - return time.Since(hit.at) < ProbeTTL -} - -// probes caches each address's probe, up to cacheLimit entries, the stale ones dropped first once it's full. An -// address's concurrent probes wait on the one under way; no lock is held while one is. -type probes struct { - mu sync.Mutex - cache map[string]probe - underway map[string]*underway -} - -type underway struct { - done chan struct{} - hit probe - ok bool -} - -// cacheLimit bounds the probe and leaf caches, so a computer that visits many sites keeps a bounded memory. -var cacheLimit = 4096 - -// dialError is a handshake's that never reached the site. -type dialError struct{ error } - -// probeAt is the probe of dst for sni, cached or new, and whether it probed now. -func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) { - key := dst + "|" + sni - p.probes.mu.Lock() - if p.probes.cache == nil { - p.probes.cache, p.probes.underway = map[string]probe{}, map[string]*underway{} - } - if hit, ok := p.probes.cache[key]; ok && hit.fresh() { - p.probes.mu.Unlock() - return hit, false - } - if running, ok := p.probes.underway[key]; ok { - p.probes.mu.Unlock() - <-running.done - if !running.ok { - return probe{at: time.Now(), failed: true, problem: "the probe it waited on failed"}, false - } - return running.hit, false - } - running := &underway{done: make(chan struct{})} - p.probes.underway[key] = running - p.probes.mu.Unlock() - defer func() { - p.probes.mu.Lock() - if running.ok { - p.store(key, running.hit) - } - delete(p.probes.underway, key) - p.probes.mu.Unlock() - close(running.done) - }() - - hit := probe{at: time.Now()} - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - switch { - case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), problemChars) - case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), problemChars) - case !found.Verified: - hit.problem = truncate(found.VerifyErr, problemChars) - default: - hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true - } - if uc != nil { - uc.NetConn().Close() - } - running.hit, running.ok = hit, true - return hit, true -} - -// store keeps hit for key, dropping the stale entries, then any others, once the cache is past cacheLimit. -func (p *Proxy) store(key string, hit probe) { - p.probes.cache[key] = hit - if len(p.probes.cache) <= cacheLimit { - return - } - for k, v := range p.probes.cache { - if !v.fresh() { - delete(p.probes.cache, k) - } - } - for k := range p.probes.cache { - if len(p.probes.cache) <= cacheLimit { - break - } - if k != key { - delete(p.probes.cache, k) - } - } -} - -// forget drops dst's probe for sni, for an address that stopped answering since. -func (p *Proxy) forget(dst, sni string) { - p.probes.mu.Lock() - delete(p.probes.cache, dst+"|"+sni) - p.probes.mu.Unlock() -} - -// inside is whether every universe lies inside [from + Margin, until - Margin]. -func inside(from, until time.Time, universes []float64) bool { - lo := float64(from.Add(Margin).UnixNano()) / 1e9 - hi := float64(until.Add(-Margin).UnixNano()) / 1e9 - for _, u := range universes { - if u < lo || u > hi { - return false - } - } - return true -} - -// dialAny dials each address in turn until one answers: which, or every one's error. -func (p *Proxy) dialAny(dsts []string) (net.Conn, string, error) { - var problems []string - for _, dst := range dsts { - conn, err := p.dial(dst) - if err == nil { - return conn, dst, nil - } - problems = append(problems, err.Error()) - } - if len(problems) == 0 { - return nil, "", errors.New("no address to dial") - } - return nil, "", errors.New(strings.Join(problems, "; ")) -} - -func (p *Proxy) dial(dst string) (net.Conn, error) { - if p.Dial != nil { - return p.Dial(dst) - } - return net.DialTimeout("tcp", dst, DialTimeout) -} - -// problemChars is the most of an upstream's error a probe keeps as its problem. -const problemChars = 200 - -func truncate(s string, n int) string { - if len(s) > n { - return s[:n] - } - return s -} diff --git a/src/agent_env/agents/agents/claude-code/shared_sources.toml b/src/agent_env/agents/agents/claude-code/shared_sources.toml new file mode 100644 index 000000000..dc9d15a01 --- /dev/null +++ b/src/agent_env/agents/agents/claude-code/shared_sources.toml @@ -0,0 +1,4 @@ +# What this image's build context takes from agent-env's own sources, at the name its Dockerfile copies it from: one +# copy of each, shared with the other images that take it (utils/docker/context.py). Paths are under src/agent_env. +clock = "utils/faketime_clock" +egress = "utils/egress_proxy" diff --git a/src/agent_env/utils/egress_proxy/README.md b/src/agent_env/utils/egress_proxy/README.md index 72cc086d5..27051fbfd 100644 --- a/src/agent_env/utils/egress_proxy/README.md +++ b/src/agent_env/utils/egress_proxy/README.md @@ -1,16 +1,17 @@ # The egress proxy `egress` is the `ubuntu-24.04` base computer's explicit proxy, the way every session program goes out. The session's -programs read the universe's time (the clock library in `../clock/`), so a client that checks a site's certificate with +programs read the universe's time (the clock library in `../faketime_clock/`), so a client that checks a site's certificate with its own TLS, Chromium, Java or an official Node build, refuses a live site once the clock is months back or ahead. The proxy runs on real time and hands such a client a certificate that holds at the universe's date when it must. -It is one static Go program. The base's Dockerfile builds it in a stage of its own, after `go vet` and its tests, so no -Go toolchain is left in the image. `go.sum` pins its modules, and Dependabot's `gomod` entry keeps them current. +It is one static Go program. Each image that runs it takes this folder through its `shared_sources.toml` (the two +bases and the Claude Code agent image), and its Dockerfile builds it in a stage of its own, after `go vet` and its +tests, so no Go toolchain is left in the image. `go.sum` pins its modules, and Dependabot's `gomod` entry keeps them current. ## What runs where -- `egress boot` runs once, as root, in the base's init (`../start-desktop`), before root's supervisord starts: +- `egress boot` runs once, as root, in the base's init (`computers/artifacts/ubuntu-24.04/start-desktop`), before root's supervisord starts: - it mints the computer's CA once, valid from 1950 to 9999 as routed sites' certificates are, its key in `/etc/agentenv/egress/` and readable by `agentenv-egress` alone; - it trusts the CA in the system store, agent-env's trust files (`/etc/agentenv/ca.pem`, `ca-bundle.pem`) and the @@ -18,10 +19,10 @@ Go toolchain is left in the image. `go.sum` pins its modules, and Dependabot's ` - it trusts the CA in every certifi bundle and every JDK's store an app brings, naming any that can't take it among its warnings, and the computer boots; - its report is `/var/log/agentenv-root/egress-boot.log`. -- `egress serve` runs as `agentenv-egress` under root's supervisord (`../root.conf`), on `127.0.0.1:3128`, or where +- `egress serve` runs as `agentenv-egress` under root's supervisord (the base's `root.conf`), on `127.0.0.1:3128`, or where `AGENTENV_EGRESS_PROXY` names, as a base whose own proxy holds that address does. The clock library leaves that user on real time. -- The session's supervisord (`../supervisord.conf`), and a login shell through `agentenv-egress.sh`, point +- The session's supervisord (the base's `supervisord.conf`), and a login shell through `agentenv-egress.sh`, point `HTTP(S)_PROXY`, `ALL_PROXY`, Java's proxy properties (`JAVA_TOOL_OPTIONS`) and Node's `fetch` (`NODE_USE_ENV_PROXY`) at it. - agent-env's own servers in the computer, the driver and its apps' servers, run on real time and send their own diff --git a/src/agent_env/utils/faketime_clock/README.md b/src/agent_env/utils/faketime_clock/README.md index 8dcbb5ece..57d09d2fa 100644 --- a/src/agent_env/utils/faketime_clock/README.md +++ b/src/agent_env/utils/faketime_clock/README.md @@ -5,12 +5,12 @@ server does. Instead each session program loads agent-env's build of [libfaketim (through `/etc/ld.so.preload`), which answers clock reads with universe time from a page a follower process keeps in step with the gateway. -It is part of the ubuntu-24.04 base, which builds the library and the shim from this folder in a stage of its -Dockerfile, since an image builds only from its own folder; the ubuntu-24.04-browser base has a copy, which a unit test -keeps identical. The base starts as root: the root start (`../root/init`) sets the zone, makes the clock page and file -for the follower's user, `agentenv-clock`, and starts root's supervisord (`../root.conf`), which runs the follower -(`clock-follower`) as that user and the session's supervisord as the session user. The session user can read the clock but neither write, replace nor -stop it. +Each image that runs it takes this folder into its build context through its `shared_sources.toml` (the ubuntu-24.04 +and ubuntu-24.04-browser bases, and the Claude Code agent image) and builds the library and the shim for its own libc +in a stage of its Dockerfile. A base starts as root: its root start (`computers/artifacts/ubuntu-24.04/root/init`) sets +the zone, makes the clock page and file for the follower's user, `agentenv-clock`, and starts root's supervisord +(`root.conf` beside it), which runs the follower (`clock-follower`) as that user and the session's supervisord as the +session user. The session user can read the clock but neither write, replace nor stop it. The follower long-polls the gateway's `/clock/watch`, writes each state it answers to the page and acknowledges it with its next watch. Core writes its config, the gateway's watch URL as the computer reaches it, the computer's name and its @@ -35,6 +35,8 @@ What follows the universe, and what doesn't: their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries aren't covered. - **Who:** only the session user's processes (`AGENTENV_SESSION_UID`). A program started with `AGENTENV_REAL_TIME=1` stays real; the marker is taken out of its environment block, so its children are faked. +- **Init:** a clock read made while the library initializes (an allocator `dlsym` calls, say) reads the kernel's + clock rather than wait on the initialization it is part of; Claude Code's CLI never started without it. - **Settings:** read once as the library loads, from `/etc/agentenv/clock.conf` (`clock.conf` is the default), or from the environment where there is no such file. While the file exists the environment changes none of them; the marker above is the one switch a program's environment has. No `getenv()` runs once the program has started: a `getenv()` diff --git a/src/agent_env/utils/faketime_clock/libfaketime.patch b/src/agent_env/utils/faketime_clock/libfaketime.patch index d26148eed..17555d2b8 100644 --- a/src/agent_env/utils/faketime_clock/libfaketime.patch +++ b/src/agent_env/utils/faketime_clock/libfaketime.patch @@ -1525,3 +1525,41 @@ diff -ruN a/src/libfaketime.map b/src/libfaketime.map + pthread_cond_timedwait; } GLIBC_2.2.5; +diff -ruN a/src/libfaketime.c b/src/libfaketime.c +--- a/src/libfaketime.c ++++ b/src/libfaketime.c +@@ -329,6 +329,9 @@ + check_missing_real(#name, (NULL == real_##name)) + + static pthread_once_t initialized_once_control = PTHREAD_ONCE_INIT; ++/* Set on the thread running ftpl_really_init: a time call it makes, as an allocator that reads the clock does when ++ * dlsym allocates, takes the kernel's clock instead of waiting on the init it is part of. */ ++static __thread int ftpl_initializing = 0; + + /* prototypes */ + static int fake_gettimeofday(struct timeval *tv); +@@ -2326,6 +2329,7 @@ + int result; + + ftpl_init(); ++ if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp); + // If ftpl_init ends up recursing, pthread_once will deadlock. + // (Previously we attempted to detect this situation, and bomb out, + // but the approach taken wasn't thread-safe and broke in practice.) +@@ -3103,8 +3107,15 @@ + dont_fake = dont_fake_final; + } + ++static void ftpl_really_init_guarded(void) { ++ ftpl_initializing = 1; ++ ftpl_really_init(); ++ ftpl_initializing = 0; ++} ++ + inline static void ftpl_init(void) { +- pthread_once(&initialized_once_control, ftpl_really_init); ++ if (ftpl_initializing) return; ++ pthread_once(&initialized_once_control, ftpl_really_init_guarded); + } + + void *ft_dlvsym(void *handle, const char *symbol, const char *version, 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 98f45b681..750777002 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -10,6 +10,7 @@ import pytest from tst.unit.agents.claude_code.agent import AGENT_DIR, claude_bridge +from tst.util.build_context import sent PROXY = "http://127.0.0.1:3128" @@ -53,9 +54,9 @@ def test_without_the_proxy_or_an_armed_clock_the_cli_goes_out_directly_as_before def _clockpage(): """The image's clock page writer, whose encoding libfaketime and the egress proxy read too.""" - # Run from its source, so no __pycache__ lands in clock/, which must stay the computer base's file for file. + # Run from its source, so no __pycache__ lands in agent-env's sources. module = types.ModuleType("clockpage") - path = AGENT_DIR / "clock" / "clockpage.py" + path = sent(AGENT_DIR, "clock/clockpage.py") exec(compile(path.read_text(), str(path), "exec"), module.__dict__) return module diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py index 04510a812..8f19fa98e 100644 --- a/tst/unit/agents/test_agent_clock_images.py +++ b/tst/unit/agents/test_agent_clock_images.py @@ -1,21 +1,23 @@ -"""Every agent image on the env's clock (FD-5916), whichever agent it is: each folder with ``clock/`` copies the -computer base's clock follower and egress proxy (``clock/``, ``egress/``) until the clock library image (FD-5700) -ships them, with libfaketime's init guard after the base's patch; runs the follower and the proxy, each as its own -user, and the A2A server as the agent's user on real time, under root's supervisord; preloads agent-env's libfaketime -for the agent's user alone; starts as another user as it did before; and its bridge decides the CLI's proxy with the -same ``clock_armed`` and ``egress_env`` as every other agent's.""" +"""Every agent image on the env's clock (FD-5916), whichever agent it is: each folder whose ``shared_sources.toml`` +takes the clock takes the computer bases' clock library and egress proxy from agent-env's one copy, whose libfaketime +patch has the init guard; runs the follower and the proxy, each as its own user, and the A2A server as the agent's user +on real time, under root's supervisord; preloads agent-env's libfaketime for the agent's user alone; starts as another +user as it did before; and its bridge decides the CLI's proxy with the same ``clock_armed`` and ``egress_env`` as every +other agent's.""" from __future__ import annotations import ast import re +import tomllib from pathlib import Path import pytest AGENTS_DIR = Path(__file__).parents[3] / "src" / "agent_env" / "agents" / "agents" -BASE = AGENTS_DIR.parents[1] / "computers" / "artifacts" / "ubuntu-24.04" -CLOCKED = sorted(p.parent.name for p in AGENTS_DIR.glob("*/clock") if p.is_dir()) +SOURCES = AGENTS_DIR.parents[1] +CLOCKED = sorted(p.parent.name for p in AGENTS_DIR.glob("*/shared_sources.toml") + if "clock" in tomllib.loads(p.read_text())) PROXY = "http://127.0.0.1:3128" @@ -24,29 +26,23 @@ def test_claude_code_follows_the_clock(): @pytest.mark.parametrize("agent", CLOCKED) -@pytest.mark.parametrize("folder", ["clock", "egress"]) -def test_the_clock_pieces_are_the_computer_bases_until_the_clock_library_image_ships_them(agent, folder): - here = AGENTS_DIR / agent / folder - copied = sorted(p.relative_to(here) for p in here.rglob("*") if p.is_file()) +@pytest.mark.parametrize(("part", "shared"), [("clock", "utils/faketime_clock"), ("egress", "utils/egress_proxy")]) +def test_the_clock_pieces_are_taken_from_the_one_copy_the_computer_bases_take(agent, part, shared): + taken = tomllib.loads((AGENTS_DIR / agent / "shared_sources.toml").read_text()) - assert copied - assert [str(p) for p in copied if str(p) != "libfaketime.patch" - and (BASE / folder / p).read_bytes() != (here / p).read_bytes()] == [] + assert taken[part] == shared + assert not (AGENTS_DIR / agent / part).exists(), f"{agent}/{part} is a copy: take it in shared_sources.toml instead" -@pytest.mark.parametrize("agent", CLOCKED) -def test_the_agents_libfaketime_patch_is_the_bases_with_the_init_guard_after_it(agent): +def test_the_libfaketime_every_clocked_image_builds_has_the_init_guard(): # A CLI that reads the clock while libfaketime's init runs (an allocator, as dlsym allocates) has pthread_once wait # on itself: Claude Code's `--version` never returned. The guard has a clock call made during the init read the - # kernel's clock. The base doesn't take it yet, so the agent's copy is the base's patch, then the guard. - base = (BASE / "clock" / "libfaketime.patch").read_text() - patch = (AGENTS_DIR / agent / "clock" / "libfaketime.patch").read_text() - guard = patch.removeprefix(base) - - assert patch.startswith(base) and guard.startswith("diff -ruN a/src/libfaketime.c b/src/libfaketime.c\n") - assert "static __thread int ftpl_initializing = 0;" in guard - assert "if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp);" in guard - assert "pthread_once(&initialized_once_control, ftpl_really_init_guarded);" in guard + # kernel's clock. + patch = (SOURCES / "utils" / "faketime_clock" / "libfaketime.patch").read_text() + + assert "static __thread int ftpl_initializing = 0;" in patch + assert "if (ftpl_initializing) return syscall(SYS_clock_gettime, clk_id, tp);" in patch + assert "pthread_once(&initialized_once_control, ftpl_really_init_guarded);" in patch def _program(agent: str, name: str) -> dict[str, str]: From 150c563abe83d6a57241e1549385dcb7521d0ad0 Mon Sep 17 00:00:00 2001 From: William Loo Date: Wed, 18 Nov 2026 09:24:40 -0800 Subject: [PATCH 25/29] test(agents): Claude Code's contract test takes the folders shared_sources.toml brings as in the build context After the move to utils, clock/ and egress/ aren't in the image's folder but are in its build context; the test resolved them in the folder alone and failed. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agents/claude_code/test_image_contract.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tst/unit/agents/claude_code/test_image_contract.py b/tst/unit/agents/claude_code/test_image_contract.py index 0f20db804..1bd602caf 100644 --- a/tst/unit/agents/claude_code/test_image_contract.py +++ b/tst/unit/agents/claude_code/test_image_contract.py @@ -1,24 +1,35 @@ -"""The agent's folder is its build context: the Dockerfile copies only its own files, keeps TLS checks on, and -installs the protocol at or above its floor, which a release doesn't pin.""" +"""The agent's folder is its build context: the Dockerfile copies only its own files and the folders its +shared_sources.toml takes from agent-env's sources, keeps TLS checks on, and installs the protocol at or above its +floor, which a release doesn't pin.""" from __future__ import annotations import re +import tomllib from tst.unit.agents.claude_code.agent import AGENT_DIR DOCKERFILE = (AGENT_DIR / "Dockerfile").read_text() +SOURCES = AGENT_DIR.parents[2] +SHARED = tomllib.loads((AGENT_DIR / "shared_sources.toml").read_text()) # Harbor bundles pin the same versions (agentenvhub-sdk's _DOC_TOOL_PACKAGES), so a task's documents read alike in both. DOC_LIBS = ["openpyxl==3.1.5", "pypdf==6.21.3", "python-docx==1.2.0", "python-pptx==1.0.2"] +def _in_context(name: str) -> bool: + """Whether the build context has ``name``: from the folder, or under a folder shared_sources.toml takes from + agent-env's own sources at that name.""" + first, _, rest = name.partition("/") + path = SOURCES / SHARED[first] / rest if first in SHARED else AGENT_DIR / name + return path.is_dir() if name.endswith("/") else path.is_file() + + def test_every_file_the_image_copies_is_in_the_folder(): copied = [name for line in re.findall(r"^COPY (?!--from=)(.+) \S+$", DOCKERFILE, re.MULTILINE) for name in line.split()] - assert copied and all((AGENT_DIR / name).is_file() or (name.endswith("/") and (AGENT_DIR / name).is_dir()) - for name in copied), copied + assert copied and all(_in_context(name) for name in copied), copied # install.sh and run.sh are install/v1's; a build context pruned to what the Dockerfile reads keeps them so. assert {"a2a_server.py", "claude_bridge.py", "kit.py", "tar_changelog.py", "entrypoint.sh", "install.sh", "run.sh"} <= set(copied) From aed27ee2cc942c985ff003f7eb52ba4805dcd531 Mon Sep 17 00:00:00 2001 From: William Loo Date: Wed, 18 Nov 2026 09:31:48 -0800 Subject: [PATCH 26/29] fix(agents): a private HTTPS model endpoint is reached directly on an armed clock; name the clock page's limits - model_direct_hosts: the model endpoint's host is direct on an armed clock when it's plain HTTP or isn't a public address (an IP literal, or a name that resolves only to non-public ones), which the egress proxy refuses. Resolved only while the clock is armed. Shared, with _global_host, by every clocked bridge. - clock_armed's retry count, delay and check salt are named (_PAGE_READ_TRIES, _PAGE_RETRY_S, _PAGE_CHECK_SALT). - The e2e's cleanup finds a stopped or missing agent container without raising, and always closes the env. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agents/claude-code/claude_bridge.py | 41 +++++++++++++++---- .../task_step/test_agent_env_clock_local.py | 29 +++++++++---- .../claude_code/test_env_clock_image.py | 17 ++++++-- tst/unit/agents/test_agent_clock_images.py | 2 +- 4 files changed, 67 insertions(+), 22 deletions(-) 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 f447a43c0..6ddc21558 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -5,11 +5,13 @@ import asyncio import contextlib +import ipaddress import json import logging import os import re import signal +import socket import stat import struct import time @@ -183,6 +185,10 @@ def _model_headers(raw: str | None) -> dict[str, str]: CLOCK_PAGE = "/dev/shm/agentenv-clock" _PAGE = struct.Struct(" bool: as libfaketime reads it, retried while a write is under way (seq odd) or its check doesn't match.""" try: with open(page_path or CLOCK_PAGE, "rb") as page: - for _ in range(50): + for _ in range(_PAGE_READ_TRIES): page.seek(0) raw = page.read(_PAGE.size) if len(raw) < _PAGE.size: @@ -202,15 +208,37 @@ def clock_armed(page_path: str | None = None) -> bool: return False rate_bits = struct.unpack(" bool: + """Whether ``host`` is a public address, or a name every address of resolves to one: a host the egress proxy + reaches. A name that doesn't resolve here is taken as public, for the proxy to resolve.""" + try: + return ipaddress.ip_address(host).is_global + except ValueError: + pass + try: + addresses = {info[4][0] for info in socket.getaddrinfo(host, None)} + except OSError: + return True + return all(ipaddress.ip_address(address.split("%")[0]).is_global for address in addresses) + + +def model_direct_hosts(base_url: str | None) -> list[str]: + """The model endpoint's host when the CLI reaches it directly on an armed clock: over plain HTTP, which has no + certificate to check at the env's date, or at an address that isn't public, which the egress proxy refuses.""" + endpoint = urlsplit(base_url or "") + host = endpoint.hostname or "" + return [host] if host and (endpoint.scheme == "http" or not _global_host(host)) else [] + + def egress_env(environ: Mapping[str, str], direct: Iterable[str] = (), *, armed: bool = False) -> dict[str, str]: """The egress proxy for the CLI and what it starts, where the image runs one (FD-5916) and the clock is ``armed``: they read the env's time, so a client checking a live site's certificate against it would refuse the site once @@ -259,11 +287,8 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" - # A model endpoint over plain HTTP has no certificate to check at the env's date, so it's reached directly, as one - # at a private address, which the proxy refuses, must be. - endpoint = urlsplit(env.get("ANTHROPIC_BASE_URL") or "") - model_host = [endpoint.hostname or ""] if endpoint.scheme == "http" else [] - env.update(egress_env(environ, [*direct_hosts, *model_host], armed=clock_armed)) + env.update(egress_env(environ, [*direct_hosts, *(model_direct_hosts(env.get("ANTHROPIC_BASE_URL")) if clock_armed else [])], + armed=clock_armed)) return env diff --git a/tst/integration/task_step/test_agent_env_clock_local.py b/tst/integration/task_step/test_agent_env_clock_local.py index 71df2d32e..139c81b89 100644 --- a/tst/integration/task_step/test_agent_env_clock_local.py +++ b/tst/integration/task_step/test_agent_env_clock_local.py @@ -92,13 +92,26 @@ class Agent: } -def _container_of(sandbox_id: str) -> str: - names = subprocess.run(["docker", "ps", "--format", "{{.Names}}"], capture_output=True, text=True, +def _container_of(sandbox_id: str, *, required: bool = True) -> str | None: + """The container behind ``sandbox_id``, stopped or running; None, when not ``required``, for one that's gone.""" + names = subprocess.run(["docker", "ps", "-a", "--format", "{{.Names}}"], capture_output=True, text=True, check=True).stdout.split() - [name] = [n for n in names if sandbox_id in n] + found = [n for n in names if sandbox_id in n] + if not required and len(found) != 1: + return None + [name] = found return name +async def _remove_agents(context: TaskStepContext) -> None: + """Remove the agent containers teardown didn't take down; one already gone, or failing to go, is left.""" + gone = set(context.metadata.get("torn_down_sandbox_ids") or []) + for agent in context.deployed_agents: + name = agent.sandbox_id and agent.sandbox_id not in gone and _container_of(agent.sandbox_id, required=False) + if name: + await asyncio.to_thread(subprocess.run, ["docker", "rm", "-f", name], capture_output=True) + + async def _followers(deployed) -> dict: return (await deployed.invoke(EXT_CLOCK_URI, "state", {}, timeout=10)).get("followers") or {} @@ -190,9 +203,7 @@ async def _follows_until_torn_down(name: str, spec: Agent, tag: str, endpoint: F print(f"advance after teardown answered in {waited:.2f}s") assert waited < 5 finally: - await asyncio.gather(*(asyncio.to_thread(subprocess.run, ["docker", "rm", "-f", _container_of(a.sandbox_id)], - capture_output=True) - for a in context.deployed_agents if a.sandbox_id - and a.sandbox_id not in (context.metadata.get("torn_down_sandbox_ids") or [])), - return_exceptions=True) - await env.close() + try: + await _remove_agents(context) + finally: + await env.close() 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 750777002..5aec6594c 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -6,6 +6,7 @@ import json import types +from urllib.parse import urlsplit import pytest @@ -37,13 +38,21 @@ def test_hosts_the_agent_was_deployed_to_reach_directly_stay_direct_on_an_armed_ @pytest.mark.parametrize("base_url, direct", [ ("http://10.0.0.5:4000", True), # plain HTTP, private: nothing to check at the env's date, and the proxy refuses it - ("https://litellm.example.com", False), # TLS: its certificate is checked through the proxy, on real time + ("https://10.0.0.5:4000", True), # private, over TLS too: the proxy refuses an address that isn't public + ("https://[fd00::5]:4000", True), + ("https://litellm.internal:4000", True), # a name that resolves to a private address + ("https://8.8.8.8", False), # TLS to a public address: its certificate is checked through the proxy + ("https://litellm.example.com", False), # a name that resolves to a public one + ]) -def test_a_model_endpoint_over_plain_http_is_reached_directly_on_an_armed_clock(base_url, direct): +def test_a_model_endpoint_over_plain_http_or_at_a_private_address_is_reached_directly_on_an_armed_clock( + monkeypatch, base_url, direct): + resolves = {"litellm.internal": "10.1.2.3", "litellm.example.com": "93.184.216.34"} + monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", + lambda host, port: [(None, None, None, "", (resolves[host], 0))]) env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url, "LITELLM_API_KEY": "k"}, clock_armed=True) - host = base_url.split("//")[1].split(":")[0] - assert (host in env["NO_PROXY"].split(",")) is direct and env["HTTPS_PROXY"] == PROXY + assert (urlsplit(base_url).hostname in env["NO_PROXY"].split(",")) is direct and env["HTTPS_PROXY"] == PROXY @pytest.mark.parametrize("environ, armed", [({}, True), ({claude_bridge.EGRESS_PROXY_ENV: PROXY}, False)]) diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py index 8f19fa98e..884ca966d 100644 --- a/tst/unit/agents/test_agent_clock_images.py +++ b/tst/unit/agents/test_agent_clock_images.py @@ -83,7 +83,7 @@ def _clock_helpers(agent: str) -> dict[str, str]: """The bridge's clock helpers, as source: the one module in the agent's folder that defines ``egress_env``.""" [module] = [p for p in (AGENTS_DIR / agent).glob("*.py") if "def egress_env(" in p.read_text()] tree = ast.parse(module.read_text()) - names = {"clock_armed", "egress_env"} + names = {"clock_armed", "_global_host", "model_direct_hosts", "egress_env"} found = {node.name: ast.unparse(node) for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in names} assert set(found) == names, (module, sorted(found)) return found From a6f372020d912b33e4139c98236b876ffe74e559 Mon Sep 17 00:00:00 2001 From: William Loo Date: Wed, 18 Nov 2026 09:39:51 -0800 Subject: [PATCH 27/29] fix(agents): the model host's lookup is bounded and kept, so a slow resolver can't stall the A2A server The lookup runs on a thread of its own and waits _RESOLVE_TIMEOUT_S (2 s) at most, once per host per process; a host that doesn't resolve in time is left to the egress proxy. Shared by every clocked bridge. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../agents/claude-code/claude_bridge.py | 24 ++++++++++++++----- .../claude_code/test_env_clock_image.py | 23 ++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) 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 6ddc21558..3f4a2e638 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio +import concurrent.futures import contextlib import ipaddress import json @@ -217,18 +218,29 @@ def clock_armed(page_path: str | None = None) -> bool: return bool(armed) # a writer that died mid-write: the next write takes the page up; its armed flag says the most +#: How long a CLI's start waits for its model host's addresses, once per host per process (``_GLOBAL_HOSTS``): the +#: lookup runs on a thread of its own, so a slow resolver holds the server's loop this long at most, once. +_RESOLVE_TIMEOUT_S = 2.0 +_GLOBAL_HOSTS: dict[str, bool] = {} +_RESOLVER = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="model-host") + + def _global_host(host: str) -> bool: """Whether ``host`` is a public address, or a name every address of resolves to one: a host the egress proxy - reaches. A name that doesn't resolve here is taken as public, for the proxy to resolve.""" + reaches. A name that doesn't resolve here, or not within ``_RESOLVE_TIMEOUT_S``, is taken as public, for the proxy + to resolve; each name's answer is kept.""" try: return ipaddress.ip_address(host).is_global except ValueError: pass - try: - addresses = {info[4][0] for info in socket.getaddrinfo(host, None)} - except OSError: - return True - return all(ipaddress.ip_address(address.split("%")[0]).is_global for address in addresses) + if host not in _GLOBAL_HOSTS: + try: + infos = _RESOLVER.submit(socket.getaddrinfo, host, None).result(timeout=_RESOLVE_TIMEOUT_S) + addresses = {info[4][0] for info in infos} + _GLOBAL_HOSTS[host] = all(ipaddress.ip_address(a.split("%")[0]).is_global for a in addresses) + except (OSError, concurrent.futures.TimeoutError): + _GLOBAL_HOSTS[host] = True + return _GLOBAL_HOSTS[host] def model_direct_hosts(base_url: str | None) -> list[str]: 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 5aec6594c..52d6e801b 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -48,6 +48,7 @@ def test_hosts_the_agent_was_deployed_to_reach_directly_stay_direct_on_an_armed_ def test_a_model_endpoint_over_plain_http_or_at_a_private_address_is_reached_directly_on_an_armed_clock( monkeypatch, base_url, direct): resolves = {"litellm.internal": "10.1.2.3", "litellm.example.com": "93.184.216.34"} + monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {}) monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", lambda host, port: [(None, None, None, "", (resolves[host], 0))]) env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url, @@ -102,3 +103,25 @@ def test_while_the_follower_reconnects_or_restarts_the_page_still_routes_the_cli page.write_bytes(_page(1)) env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, clock_armed=claude_bridge.clock_armed(str(page))) assert env["HTTPS_PROXY"] == PROXY + + +def test_a_model_host_that_doesnt_resolve_in_time_is_left_to_the_proxy_and_asked_once(monkeypatch): + # A slow resolver holds a CLI's start for _RESOLVE_TIMEOUT_S at most, and only the first time. + import threading + + asked, release = [], threading.Event() + + def slow(host, port): + asked.append(host) + release.wait(5) + return [(None, None, None, "", ("10.1.2.3", 0))] + + monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {}) + monkeypatch.setattr(claude_bridge, "_RESOLVE_TIMEOUT_S", 0.05) + monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", slow) + try: + assert claude_bridge.model_direct_hosts("https://slow.internal") == [] + assert claude_bridge.model_direct_hosts("https://slow.internal") == [] + finally: + release.set() + assert asked == ["slow.internal"] From 660d8163c31acc5f03c72c6627fdceb61193095a Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Wed, 18 Nov 2026 09:54:58 -0800 Subject: [PATCH 28/29] fix(agents): a private HTTPS model endpoint goes through the egress proxy, which the agent's image lets reach it Reached directly, Claude Code checks the endpoint's certificate at the env's date, so a private HTTPS model endpoint failed once that date left the certificate's validity; and the bridge's DNS lookup could cache a failed answer for the server's life. Now only a plain-HTTP endpoint is direct. The egress proxy takes AGENTENV_EGRESS_PRIVATE=allow, which the agent image's root.conf sets: it then reaches private networks' addresses too (RFC 1918, RFC 6598, IPv6 unique local), which the agent's processes reach directly anyway, and checks the certificate on real time as for any site. Loopback, link-local and the rest stay refused, and a computer's proxy doesn't set it. The bridge's lookup and cache are gone. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/agents/README.md | 6 ++- .../agents/claude-code/claude_bridge.py | 38 +++------------- .../agents/agents/claude-code/root.conf | 1 + src/agent_env/utils/egress_proxy/README.md | 7 ++- src/agent_env/utils/egress_proxy/main.go | 3 ++ src/agent_env/utils/egress_proxy/proxy.go | 36 +++++++++++++--- .../utils/egress_proxy/proxy_test.go | 35 +++++++++++++++ .../claude_code/test_env_clock_image.py | 43 ++++--------------- tst/unit/agents/test_agent_clock_images.py | 10 ++++- 9 files changed, 102 insertions(+), 77 deletions(-) diff --git a/src/agent_env/agents/README.md b/src/agent_env/agents/README.md index 787a5fabf..76ce7a0b7 100644 --- a/src/agent_env/agents/README.md +++ b/src/agent_env/agents/README.md @@ -45,8 +45,10 @@ as a follower with a read token of its own and calls that `sync_time` with the f writes; `deploy_agent` does the same for an agent it deploys while the clock is armed. From then on the CLI, its own "Today's date is" line included, and every command the model runs read the env's time, and a clock write that waits for followers waits for the agent. While the clock is armed the CLI goes out through the egress proxy, which keeps a live -site's TLS working at any date the clock shows, and to the env's MCP servers directly. Unarmed, the page holds real time -and the CLI goes out directly, as before, so a model endpoint at a private address, which the proxy refuses, still works. Started as a user other than root, the image runs the A2A server alone, on real time, as before. +site's TLS working at any date the clock shows, and to the env's MCP servers and a plain-HTTP model endpoint directly. +The image's proxy reaches private networks too (`AGENTENV_EGRESS_PRIVATE`), so a model endpoint over TLS at a private +address works at any date as well. Unarmed, the page holds real time and the CLI goes out directly, as before. Started +as a user other than root, the image runs the A2A server alone, on real time, as before. `install_agent` installs it too, into a container a task already runs or onto a `vm` sandbox's Linux host, with `install.sh` from its folder: Claude Code's native build for the target's CPU and libc, uv, and the agent's requirements 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 3f4a2e638..e1a30956d 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -4,15 +4,12 @@ from __future__ import annotations import asyncio -import concurrent.futures import contextlib -import ipaddress import json import logging import os import re import signal -import socket import stat import struct import time @@ -218,37 +215,12 @@ def clock_armed(page_path: str | None = None) -> bool: return bool(armed) # a writer that died mid-write: the next write takes the page up; its armed flag says the most -#: How long a CLI's start waits for its model host's addresses, once per host per process (``_GLOBAL_HOSTS``): the -#: lookup runs on a thread of its own, so a slow resolver holds the server's loop this long at most, once. -_RESOLVE_TIMEOUT_S = 2.0 -_GLOBAL_HOSTS: dict[str, bool] = {} -_RESOLVER = concurrent.futures.ThreadPoolExecutor(max_workers=1, thread_name_prefix="model-host") - - -def _global_host(host: str) -> bool: - """Whether ``host`` is a public address, or a name every address of resolves to one: a host the egress proxy - reaches. A name that doesn't resolve here, or not within ``_RESOLVE_TIMEOUT_S``, is taken as public, for the proxy - to resolve; each name's answer is kept.""" - try: - return ipaddress.ip_address(host).is_global - except ValueError: - pass - if host not in _GLOBAL_HOSTS: - try: - infos = _RESOLVER.submit(socket.getaddrinfo, host, None).result(timeout=_RESOLVE_TIMEOUT_S) - addresses = {info[4][0] for info in infos} - _GLOBAL_HOSTS[host] = all(ipaddress.ip_address(a.split("%")[0]).is_global for a in addresses) - except (OSError, concurrent.futures.TimeoutError): - _GLOBAL_HOSTS[host] = True - return _GLOBAL_HOSTS[host] - - def model_direct_hosts(base_url: str | None) -> list[str]: """The model endpoint's host when the CLI reaches it directly on an armed clock: over plain HTTP, which has no - certificate to check at the env's date, or at an address that isn't public, which the egress proxy refuses.""" + certificate to check at the env's date. Over TLS it goes through the egress proxy, which checks the certificate on + real time and, in an agent's image, reaches a private network's address too (``AGENTENV_EGRESS_PRIVATE``).""" endpoint = urlsplit(base_url or "") - host = endpoint.hostname or "" - return [host] if host and (endpoint.scheme == "http" or not _global_host(host)) else [] + return [endpoint.hostname] if endpoint.hostname and endpoint.scheme == "http" else [] def egress_env(environ: Mapping[str, str], direct: Iterable[str] = (), *, armed: bool = False) -> dict[str, str]: @@ -299,8 +271,8 @@ def cli_env(environ: Mapping[str, str], *, model: str | None = None, project_id: # Older CLIs prepend a changing attribution block to the system prompt on custom base URLs, which defeats the # provider's prefix cache. env["CLAUDE_CODE_ATTRIBUTION_HEADER"] = "0" - env.update(egress_env(environ, [*direct_hosts, *(model_direct_hosts(env.get("ANTHROPIC_BASE_URL")) if clock_armed else [])], - armed=clock_armed)) + model_hosts = model_direct_hosts(env.get("ANTHROPIC_BASE_URL")) if clock_armed else [] + env.update(egress_env(environ, [*direct_hosts, *model_hosts], armed=clock_armed)) return env diff --git a/src/agent_env/agents/agents/claude-code/root.conf b/src/agent_env/agents/agents/claude-code/root.conf index cfa21e733..758b6a648 100644 --- a/src/agent_env/agents/agents/claude-code/root.conf +++ b/src/agent_env/agents/agents/claude-code/root.conf @@ -35,6 +35,7 @@ environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" [program:egress] command=/usr/local/lib/agentenv-egress/egress serve +environment=AGENTENV_EGRESS_PRIVATE="allow" user=agentenv-egress directory=/ priority=3 diff --git a/src/agent_env/utils/egress_proxy/README.md b/src/agent_env/utils/egress_proxy/README.md index 27051fbfd..439e5abc1 100644 --- a/src/agent_env/utils/egress_proxy/README.md +++ b/src/agent_env/utils/egress_proxy/README.md @@ -35,7 +35,10 @@ tests, so no Go toolchain is left in the image. `go.sum` pins its modules, and D rewritten, then bytes are relayed both ways. An upgrade keeps its `Connection` and `Upgrade` headers; any other request closes once answered. - **`CONNECT`**: the destination's name is resolved, and the connection is refused unless every address is global - (`notGlobal` lists the blocks that aren't). A routed site's name, in the hosts file's `# agentenv sites` block, goes + (`notGlobal` lists the blocks that aren't). An agent's image sets `AGENTENV_EGRESS_PRIVATE=allow` for the proxy, + which then reaches private networks' addresses too (RFC 1918, RFC 6598's shared space, IPv6 unique local), as the + agent's processes do directly anyway, so a model endpoint there is reached on real time; loopback, link-local and the + rest stay refused. A computer's proxy doesn't set it. A routed site's name, in the hosts file's `# agentenv sites` block, goes to the site proxy there. Bytes that aren't TLS, and a routed site's TLS, are relayed untouched. - **TLS, with the clock unarmed**: relayed untouched. - **TLS, with the clock armed**: each of the name's addresses is decided by its own probe, a handshake the proxy makes @@ -63,7 +66,7 @@ the session user's reach. Its fields, each there when it applies: |---|---| | `t` | When the line was written, in seconds | | `client`, `method`, `host`, `port` | Who asked, how, and for what | -| `allowed`, `dst` | `global` or `routed`, and the address dialled | +| `allowed`, `dst` | `global`, `private` or `routed`, and the address dialled | | `mode` | `pass`, `splice`, `intercept`, `http`, `upgrade` or `refused` | | `decision` | `real`, `inside`, `outside`, `unverified`, `probe_failed`, `routed` or `not TLS` | | `sni`, `alpn_offered`, `alpn` | The client's server name and protocols, and the one the site picked | diff --git a/src/agent_env/utils/egress_proxy/main.go b/src/agent_env/utils/egress_proxy/main.go index dd9760e99..20169dd06 100644 --- a/src/agent_env/utils/egress_proxy/main.go +++ b/src/agent_env/utils/egress_proxy/main.go @@ -63,6 +63,9 @@ const ( PageReads = 1000 ) +// PrivateEnv set to "allow" lets the proxy reach private networks' addresses too (Proxy.PrivateNetworks). +const PrivateEnv = "AGENTENV_EGRESS_PRIVATE" + // The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' // (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. var ( diff --git a/src/agent_env/utils/egress_proxy/proxy.go b/src/agent_env/utils/egress_proxy/proxy.go index 3755399b9..096d8d21b 100644 --- a/src/agent_env/utils/egress_proxy/proxy.go +++ b/src/agent_env/utils/egress_proxy/proxy.go @@ -35,6 +35,10 @@ type Proxy struct { Dial func(dst string) (net.Conn, error) Log func(row map[string]any) probes probes + // PrivateNetworks lets a destination at a private network's address (RFC 1918, RFC 6598's shared space, IPv6 + // unique local) through as well as a global one: an agent's image sets it, whose processes reach those directly + // anyway, so its model endpoint is reached on real time wherever it is. A computer's proxy leaves it off. + PrivateNetworks bool } func serveForever(listen string) error { @@ -56,8 +60,10 @@ func serveForever(listen string) error { if err != nil { return err } - p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} - p.Log(map[string]any{"event": "serving", "listen": listen, "uid": os.Getuid(), "pid": os.Getpid()}) + p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout), + PrivateNetworks: os.Getenv(PrivateEnv) == "allow"} + p.Log(map[string]any{"event": "serving", "listen": listen, "uid": os.Getuid(), "pid": os.Getpid(), + "private_networks": p.PrivateNetworks}) return p.Serve(ln) } @@ -231,7 +237,8 @@ func headerName(line string) string { // destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for // a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), -// "global" for a name whose every address is global, IPv4 first. Anything else is refused. +// "global" for a name whose every address is global, "private" for one with an address on a private network where +// PrivateNetworks lets those through, IPv4 first. Anything else is refused. func (p *Proxy) destination(host string, port int) ([]string, string, error) { host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") routed := p.Routed @@ -262,11 +269,16 @@ func (p *Proxy) destination(host string, port int) ([]string, string, error) { } var private []string var v4, v6 []string + why := "global" for _, a := range addresses { a = a.Unmap() dst := netip.AddrPortFrom(a, uint16(port)).String() + ok := allowed(a) + if !ok && p.PrivateNetworks && isPrivateNetwork(a) { + ok, why = true, "private" + } switch { - case !allowed(a): + case !ok: private = append(private, a.String()) case a.Is4(): v4 = append(v4, dst) @@ -277,7 +289,7 @@ func (p *Proxy) destination(host string, port int) ([]string, string, error) { if len(private) > 0 { return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) } - return append(v4, v6...), "global", nil + return append(v4, v6...), why, nil } // notGlobal are the special-purpose blocks (IANA's registries) no global address is in. @@ -295,6 +307,20 @@ var notGlobal = func() []netip.Prefix { return out }() +// privateNetworks are the blocks a network of one's own is numbered from, which PrivateNetworks lets through. +var privateNetworks = []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8"), netip.MustParsePrefix("172.16.0.0/12"), + netip.MustParsePrefix("192.168.0.0/16"), netip.MustParsePrefix("100.64.0.0/10"), netip.MustParsePrefix("fc00::/7")} + +func isPrivateNetwork(a netip.Addr) bool { + a = a.Unmap() + for _, block := range privateNetworks { + if block.Contains(a) { + return true + } + } + return false +} + func isGlobal(a netip.Addr) bool { a = a.Unmap() if !a.IsValid() { diff --git a/src/agent_env/utils/egress_proxy/proxy_test.go b/src/agent_env/utils/egress_proxy/proxy_test.go index c8e57b3e7..772afe1f8 100644 --- a/src/agent_env/utils/egress_proxy/proxy_test.go +++ b/src/agent_env/utils/egress_proxy/proxy_test.go @@ -236,6 +236,41 @@ func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { } } +func TestAProxyThatAllowsPrivateNetworksReachesThemAndStillRefusesTheRest(t *testing.T) { + p := &Proxy{Routed: func() map[string]string { return nil }, PrivateNetworks: true} + resolving := func(addresses ...string) { + p.Resolve = func(context.Context, string) ([]netip.Addr, error) { + var out []netip.Addr + for _, a := range addresses { + out = append(out, netip.MustParseAddr(a)) + } + return out, nil + } + } + for _, address := range []string{"10.0.0.5", "172.16.0.1", "192.168.1.1", "100.64.0.1", "fd00::5", "::ffff:10.0.0.5"} { + resolving(address) + if dst, why, err := p.destination("model.internal", 4000); err != nil || why != "private" || len(dst) != 1 { + t.Errorf("%s: %q %q %v", address, dst, why, err) + } + } + for _, address := range []string{"127.0.0.1", "::1", "0.0.0.0", "169.254.169.254", "fe80::1", "224.0.0.1", + "192.0.2.1"} { + resolving(address) + if _, _, err := p.destination("model.internal", 4000); err == nil { + t.Errorf("%s dialled", address) + } + } + resolving("1.1.1.1") + if _, why, err := p.destination("one.one.one.one", 443); err != nil || why != "global" { + t.Fatalf("a public name: %q %v", why, err) + } + resolving("1.1.1.1", "10.0.0.5") + dst, why, err := p.destination("split.example", 443) + if err != nil || why != "private" || strings.Join(dst, " ") != "1.1.1.1:443 10.0.0.5:443" { + t.Fatalf("a name with a private address: %q %q %v", dst, why, err) + } +} + func TestARefusedDestinationIsAnswered403(t *testing.T) { s := site(t, false) h := newHarness(t, s, fixedUniverse{}, true) 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 52d6e801b..1f5432a96 100644 --- a/tst/unit/agents/claude_code/test_env_clock_image.py +++ b/tst/unit/agents/claude_code/test_env_clock_image.py @@ -37,20 +37,16 @@ def test_hosts_the_agent_was_deployed_to_reach_directly_stay_direct_on_an_armed_ @pytest.mark.parametrize("base_url, direct", [ - ("http://10.0.0.5:4000", True), # plain HTTP, private: nothing to check at the env's date, and the proxy refuses it - ("https://10.0.0.5:4000", True), # private, over TLS too: the proxy refuses an address that isn't public - ("https://[fd00::5]:4000", True), - ("https://litellm.internal:4000", True), # a name that resolves to a private address - ("https://8.8.8.8", False), # TLS to a public address: its certificate is checked through the proxy - ("https://litellm.example.com", False), # a name that resolves to a public one - + ("http://10.0.0.5:4000", True), # plain HTTP: nothing to check at the env's date + # TLS, at a private address or a public one: its certificate is checked through the proxy, on real time, and the + # agent's proxy reaches private networks (root.conf's AGENTENV_EGRESS_PRIVATE) + ("https://10.0.0.5:4000", False), + ("https://[fd00::5]:4000", False), + ("https://litellm.internal:4000", False), + ("https://litellm.example.com", False), ]) -def test_a_model_endpoint_over_plain_http_or_at_a_private_address_is_reached_directly_on_an_armed_clock( - monkeypatch, base_url, direct): - resolves = {"litellm.internal": "10.1.2.3", "litellm.example.com": "93.184.216.34"} - monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {}) - monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", - lambda host, port: [(None, None, None, "", (resolves[host], 0))]) +def test_a_model_endpoint_over_plain_http_is_reached_directly_and_one_over_tls_through_the_proxy_on_an_armed_clock( + base_url, direct): env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY, "LITELLM_BASE_URL": base_url, "LITELLM_API_KEY": "k"}, clock_armed=True) assert (urlsplit(base_url).hostname in env["NO_PROXY"].split(",")) is direct and env["HTTPS_PROXY"] == PROXY @@ -104,24 +100,3 @@ def test_while_the_follower_reconnects_or_restarts_the_page_still_routes_the_cli env = claude_bridge.cli_env({claude_bridge.EGRESS_PROXY_ENV: PROXY}, clock_armed=claude_bridge.clock_armed(str(page))) assert env["HTTPS_PROXY"] == PROXY - -def test_a_model_host_that_doesnt_resolve_in_time_is_left_to_the_proxy_and_asked_once(monkeypatch): - # A slow resolver holds a CLI's start for _RESOLVE_TIMEOUT_S at most, and only the first time. - import threading - - asked, release = [], threading.Event() - - def slow(host, port): - asked.append(host) - release.wait(5) - return [(None, None, None, "", ("10.1.2.3", 0))] - - monkeypatch.setattr(claude_bridge, "_GLOBAL_HOSTS", {}) - monkeypatch.setattr(claude_bridge, "_RESOLVE_TIMEOUT_S", 0.05) - monkeypatch.setattr(claude_bridge.socket, "getaddrinfo", slow) - try: - assert claude_bridge.model_direct_hosts("https://slow.internal") == [] - assert claude_bridge.model_direct_hosts("https://slow.internal") == [] - finally: - release.set() - assert asked == ["slow.internal"] diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py index 884ca966d..7b8bf7824 100644 --- a/tst/unit/agents/test_agent_clock_images.py +++ b/tst/unit/agents/test_agent_clock_images.py @@ -62,6 +62,14 @@ def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as assert f'AGENTENV_EGRESS_PROXY="{PROXY}"' in server["environment"] +@pytest.mark.parametrize("agent", CLOCKED) +def test_the_agents_proxy_reaches_private_networks_where_its_model_endpoint_may_be_and_a_computers_doesnt(agent): + # The agent's processes reach those directly anyway; its model endpoint, over TLS, goes through the proxy. + assert _program(agent, "egress")["environment"] == 'AGENTENV_EGRESS_PRIVATE="allow"' + base = (SOURCES / "computers" / "artifacts" / "ubuntu-24.04" / "root.conf").read_text() + assert "AGENTENV_EGRESS_PRIVATE" not in base + + @pytest.mark.parametrize("agent", CLOCKED) def test_the_image_preloads_the_library_for_the_agents_user_and_the_follower_alone_reads_its_config(agent): dockerfile = (AGENTS_DIR / agent / "Dockerfile").read_text() @@ -83,7 +91,7 @@ def _clock_helpers(agent: str) -> dict[str, str]: """The bridge's clock helpers, as source: the one module in the agent's folder that defines ``egress_env``.""" [module] = [p for p in (AGENTS_DIR / agent).glob("*.py") if "def egress_env(" in p.read_text()] tree = ast.parse(module.read_text()) - names = {"clock_armed", "_global_host", "model_direct_hosts", "egress_env"} + names = {"clock_armed", "model_direct_hosts", "egress_env"} found = {node.name: ast.unparse(node) for node in tree.body if isinstance(node, ast.FunctionDef) and node.name in names} assert set(found) == names, (module, sorted(found)) return found From 8848ea0fd155fd24f3a4c4a6a1c0f049b1bd949c Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Wed, 18 Nov 2026 10:05:42 -0800 Subject: [PATCH 29/29] fix(agents): the egress proxy's private-network setting joins its program's one environment line supervisord keeps the last environment= of a program, so the separate line was dropped and the proxy still refused private addresses (seen on an arm64 build: "private_networks": false). The test reads the line supervisord uses. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/agents/agents/claude-code/root.conf | 3 +-- tst/unit/agents/test_agent_clock_images.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/agent_env/agents/agents/claude-code/root.conf b/src/agent_env/agents/agents/claude-code/root.conf index 758b6a648..af730a2d9 100644 --- a/src/agent_env/agents/agents/claude-code/root.conf +++ b/src/agent_env/agents/agents/claude-code/root.conf @@ -35,7 +35,6 @@ environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" [program:egress] command=/usr/local/lib/agentenv-egress/egress serve -environment=AGENTENV_EGRESS_PRIVATE="allow" user=agentenv-egress directory=/ priority=3 @@ -44,7 +43,7 @@ startsecs=1 startretries=1000 redirect_stderr=true stdout_logfile=/var/log/agentenv-root/egress.log -environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" +environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress",AGENTENV_EGRESS_PRIVATE="allow" ; uvicorn itself, not entrypoint.sh: the marker holds for the process it's given to alone, and a shell that execs ; uvicorn would hand uvicorn an environment without it. diff --git a/tst/unit/agents/test_agent_clock_images.py b/tst/unit/agents/test_agent_clock_images.py index 7b8bf7824..055ed3683 100644 --- a/tst/unit/agents/test_agent_clock_images.py +++ b/tst/unit/agents/test_agent_clock_images.py @@ -65,7 +65,7 @@ def test_the_follower_and_the_proxy_run_as_their_own_users_and_the_a2a_server_as @pytest.mark.parametrize("agent", CLOCKED) def test_the_agents_proxy_reaches_private_networks_where_its_model_endpoint_may_be_and_a_computers_doesnt(agent): # The agent's processes reach those directly anyway; its model endpoint, over TLS, goes through the proxy. - assert _program(agent, "egress")["environment"] == 'AGENTENV_EGRESS_PRIVATE="allow"' + assert 'AGENTENV_EGRESS_PRIVATE="allow"' in _program(agent, "egress")["environment"].split(",") base = (SOURCES / "computers" / "artifacts" / "ubuntu-24.04" / "root.conf").read_text() assert "AGENTENV_EGRESS_PRIVATE" not in base