From 9607294637d79a18fbf3e98b924677c5b2ace463 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 15 Nov 2026 07:53:11 +0000 Subject: [PATCH 1/2] test(agents): pin how claude-code's workspace archive stores and restores bytes --- .../claude_code/test_workspace_snapshot.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tst/unit/agents/claude_code/test_workspace_snapshot.py b/tst/unit/agents/claude_code/test_workspace_snapshot.py index ff0b29607..85eb3d4dc 100644 --- a/tst/unit/agents/claude_code/test_workspace_snapshot.py +++ b/tst/unit/agents/claude_code/test_workspace_snapshot.py @@ -8,6 +8,8 @@ import tarfile from pathlib import Path +import pytest + from agentenv_protocol.transfers import Uploaded from tst.unit.agents.claude_code.support import extension_methods, read_grant, write_grant @@ -344,3 +346,46 @@ def test_a_saved_conversation_holds_no_key_an_api_error_echoed(monkeypatch, tmp_ saved = a2a_server._read_complete_session_jsonl(transcript).decode() assert key not in saved and "[redacted]" in saved and saved.startswith(f"{ok}\n") + + +def test_the_workspace_archive_stores_bytes_as_they_are_in_a_gzip_stream_older_loaders_read(monkeypatch, tmp_path): + workspace, _ = _snapshot_roots(monkeypatch, tmp_path) + text = b"the same line of source code\n" * 40_000 + text_size = len(text) + (workspace / "notes.txt").write_bytes(text) + archive = tmp_path / "workspace.tar.gz" + + _, bytes_written = a2a_server._build_workspace_archive(archive) + + assert bytes_written > text_size + with tarfile.open(archive, mode="r:gz") as tar: + assert tar.extractfile("notes.txt").read() == text + + +def _recompressed(archive: Path, level: int) -> Path: + with tarfile.open(archive, mode="r:gz") as source: + members = [(member, source.extractfile(member).read() if member.isfile() else None) + for member in source.getmembers()] + copy = archive.with_name(f"level-{level}.tar.gz") + with tarfile.open(copy, mode="w:gz", compresslevel=level) as tar: + for member, data in members: + tar.addfile(member, io.BytesIO(data) if data is not None else None) + return copy + + +@pytest.mark.parametrize("level", [None, 9], ids=["as-saved", "compressed-by-an-older-agent"]) +def test_a_workspace_archive_restores_whether_or_not_its_gzip_stream_is_compressed(monkeypatch, tmp_path, level): + _snapshot_roots(monkeypatch, tmp_path / "source") + archive = tmp_path / "workspace.tar.gz" + a2a_server._build_workspace_archive(archive) + if level is not None: + archive = _recompressed(archive, level) + destination_workspace = tmp_path / "destination" / "app" + destination_state = tmp_path / "destination" / "home" / ".claude" + monkeypatch.setattr(a2a_server, "WORKSPACE_DIR", destination_workspace) + monkeypatch.setattr(a2a_server, "CLAUDE_STATE_DIR", destination_state) + + a2a_server._restore_workspace_archive(archive) + + assert (destination_workspace / "report" / "answer.txt").read_text() == "done" + assert (destination_state / "projects" / "-app" / "memory" / "MEMORY.md").read_text() == "remember this" From 8c1955adbff3cf51794d4bd56d369d4450be11ea Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 15 Nov 2026 07:53:27 +0000 Subject: [PATCH 2/2] perf(agents): store claude-code's workspace archive uncompressed in its gzip stream Deflate on one core limited the snapshot save, and it gains nothing on media. Level 0 keeps the gzip container, so every loader still reads it. --- src/agent_env/agents/agents/claude-code/a2a_server.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/agent_env/agents/agents/claude-code/a2a_server.py b/src/agent_env/agents/agents/claude-code/a2a_server.py index 7dc27625e..ee0b0fbdf 100644 --- a/src/agent_env/agents/agents/claude-code/a2a_server.py +++ b/src/agent_env/agents/agents/claude-code/a2a_server.py @@ -238,7 +238,9 @@ def add_tree(tar: tarfile.TarFile, source_root: Path, *, archive_prefix: str = " count += 1 return count - with tarfile.open(archive_path, mode="w:gz", dereference=False) as tar: + # Level 0 stores the bytes in a gzip stream: deflate on one core is the save's bottleneck and gains nothing on + # media, and the stream stays one that every agent's loader opens. + with tarfile.open(archive_path, mode="w:gz", compresslevel=0, dereference=False) as tar: # The CLI's state goes in as `.claude` from wherever it lives, so a `/app/.claude` isn't added twice. file_count = add_tree(tar, WORKSPACE_DIR, excluded_top_level=frozenset({".claude"})) if CLAUDE_STATE_DIR.is_dir(): @@ -247,9 +249,9 @@ def add_tree(tar: tarfile.TarFile, source_root: Path, *, archive_prefix: str = " def _build_workspace_archive(archive_path: Path) -> tuple[int, int]: - """``/app`` and the CLI's state as a gzip tar on disk, out of the agent's heap; its file count and size. A file - that changes while it's read rebuilds the archive with each file staged first, so it can be skipped. Symlinked - directories aren't followed.""" + """``/app`` and the CLI's state as a tar in an uncompressed gzip stream on disk, out of the agent's heap; its file + count and size. A file that changes while it's read rebuilds the archive with each file staged first, so it can be + skipped. Symlinked directories aren't followed.""" if not WORKSPACE_DIR.is_dir(): raise FileNotFoundError(f"workspace directory does not exist: {WORKSPACE_DIR}") try: