Skip to content

Commit 5857db0

Browse files
authored
Merge pull request #134 from agent-diff-bench/fixes-kdd
DB Fix
2 parents 77e4f46 + cfa5047 commit 5857db0

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

backend/src/platform/api/middleware.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import time
55

6+
from starlette.exceptions import HTTPException
67
from starlette.middleware.base import BaseHTTPMiddleware
78
from starlette.requests import Request
89
from starlette.responses import Response, JSONResponse
@@ -61,6 +62,8 @@ async def dispatch(self, request: Request, call_next) -> Response:
6162
{"detail": str(exc)},
6263
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
6364
)
65+
except HTTPException:
66+
raise # Let Starlette handle route-level HTTP errors (e.g. 404)
6467
except Exception:
6568
logger.exception("Unhandled exception in PlatformMiddleware")
6669
return JSONResponse(

backend/src/platform/api/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ async def init_environment(request: Request) -> JSONResponse:
407407
logger.warning("Unauthorized template access in init_environment")
408408
return unauthorized()
409409
except ValueError as e:
410-
logger.warning(f"Template resolution failed in init_environment: {e}")
410+
logger.warning(
411+
f"Template resolution failed in init_environment: {e} "
412+
f"(service={body.templateService!r}, name={body.templateName!r}, "
413+
f"testId={body.testId!r}, schema={body.templateSchema!r})"
414+
)
411415
return bad_request(str(e))
412416

413417
if not body.testId and not body.impersonateUserId and not body.impersonateEmail:

backend/src/platform/isolationEngine/environment.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,35 @@ def migrate_schema(self, template_schema: str, target_schema: str) -> None:
5555
self._set_replica_identity(target_schema)
5656

5757
def _ensure_box_columns(self, schema: str) -> None:
58-
"""Add columns that may be missing from older template snapshots."""
58+
"""Add columns that may be missing from older template snapshots.
59+
60+
Only runs against schemas that actually contain Box tables. Each
61+
ALTER TABLE is wrapped in a SAVEPOINT so that a single failure does
62+
not poison the surrounding transaction (the same pattern used by
63+
``_copy_custom_indexes``).
64+
"""
5965
_columns = [
6066
# (table, column, SQL type, default)
6167
("box_folders", "path", "VARCHAR(500)", "'/'"),
6268
("box_files", "path", "VARCHAR(500)", "'/0/'"),
6369
]
6470
with self.session_manager.base_engine.begin() as conn:
71+
# Quick check: skip entirely when the schema has no Box tables.
72+
has_box = conn.execute(
73+
text(
74+
"SELECT EXISTS("
75+
" SELECT 1 FROM information_schema.tables"
76+
" WHERE table_schema = :schema"
77+
" AND table_name = 'box_folders'"
78+
")"
79+
),
80+
{"schema": schema},
81+
).scalar()
82+
if not has_box:
83+
return
84+
6585
for table, col, sql_type, default in _columns:
86+
nested = conn.begin_nested()
6687
try:
6788
conn.execute(
6889
text(
@@ -71,7 +92,9 @@ def _ensure_box_columns(self, schema: str) -> None:
7192
f"DEFAULT {default}"
7293
)
7394
)
95+
nested.commit()
7496
except Exception as exc:
97+
nested.rollback()
7598
logger.warning(
7699
f"Could not ensure column {schema}.{table}.{col}: {exc}"
77100
)

0 commit comments

Comments
 (0)