From c256fb2334bf37de6a9ff335f1a0f3bc962a6c42 Mon Sep 17 00:00:00 2001 From: daddy Date: Tue, 15 Sep 2026 00:33:46 +0000 Subject: [PATCH] tests/conftest.py: derive the hermetic-bootstrap compose project/ports per checkout Hardcoded _COMPOSE_PROJECT="agp-pytest" + fixed ports meant every concurrent checkout running pytest (e.g. multiple Agent-tool subagents, each in their own workspace clone) fought over the same Docker Compose project and host ports -- one run's pytest_unconfigure teardown could yank the ephemeral Postgres out from under a completely unrelated concurrent run. Confirmed live running 3 parallel subagents: one hit "connection was closed in the middle of operation", another independently hit a transient DeadlockDetectedError from concurrent table truncation on the same shared container. Same root cause and fix pattern as the agent_core workspace/Docker-Compose collision documented in project_agp memory -- derive both the project name and the ports from a hash of the checkout directory so concurrent checkouts land on different projects/ports while a single checkout stays stable across repeated runs. --- backend/tests/conftest.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 64612e2..dcceedc 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -16,6 +16,7 @@ out dozens of times. """ import asyncio +import hashlib import os import socket import subprocess @@ -54,9 +55,23 @@ TEST_TABLES = ( ) BACKEND_DIR = Path(__file__).resolve().parents[1] -_COMPOSE_PROJECT = "agp-pytest" -_EPHEMERAL_PG_PORT = 25432 -_EPHEMERAL_REDIS_PORT = 26379 + +# A hardcoded project name/port here would mean every concurrent checkout +# running `pytest` (e.g. several Claude Agent-tool subagents, each in their +# own ~/agp/workspace/task_ clone) fights over the exact same Docker +# Compose project and host ports -- one run's pytest_unconfigure teardown +# can yank the ephemeral Postgres out from under a completely unrelated +# concurrent run, surfacing as "connection was closed in the middle of +# operation" with no obvious cause. Confirmed live 2026-09-15 running 3 +# parallel subagents. Same root cause and fix pattern as the agent_core +# workspace/Docker-Compose collision documented in project_agp memory -- +# derive both the project name and the port from a hash of the checkout +# directory, so different concurrent checkouts land on different projects/ +# ports while the same checkout is stable across repeated runs. +_WORKSPACE_HASH = hashlib.sha256(str(BACKEND_DIR.parent).encode()).hexdigest()[:10] +_COMPOSE_PROJECT = f"agp-pytest-{_WORKSPACE_HASH}" +_EPHEMERAL_PG_PORT = 25400 + (int(_WORKSPACE_HASH, 16) % 100) +_EPHEMERAL_REDIS_PORT = 26300 + (int(_WORKSPACE_HASH, 16) % 100) # Set by pytest_configure() when it starts container(s) it owns; torn down in # pytest_unconfigure.