From 678d0e0ea31b7ca9c45651ffa29b55a295d372e6 Mon Sep 17 00:00:00 2001 From: gilesb Date: Sat, 10 Jan 2026 15:07:42 +0000 Subject: [PATCH] Fix NoneType subscript error in record_run - Add get_asset_by_name_tx for transaction-aware asset lookup - Use transaction connection instead of separate connection - Prevents race condition where asset might not be visible Co-Authored-By: Claude Opus 4.5 --- db.py | 13 +++++++++++++ server.py | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index 35f2441..3bff73c 100644 --- a/db.py +++ b/db.py @@ -464,6 +464,19 @@ async def asset_exists_by_name_tx(conn, name: str) -> bool: ) +async def get_asset_by_name_tx(conn, name: str) -> Optional[dict]: + """Get asset by name within a transaction.""" + row = await conn.fetchrow( + """SELECT name, content_hash, ipfs_cid, asset_type, tags, metadata, url, + provenance, description, origin, owner, created_at, updated_at + FROM assets WHERE name = $1""", + name + ) + if row: + return _parse_asset_row(row) + return None + + async def create_asset_tx(conn, asset: dict) -> dict: """Create a new asset within a transaction.""" row = await conn.fetchrow( diff --git a/server.py b/server.py index 7df3af4..67d123f 100644 --- a/server.py +++ b/server.py @@ -2194,8 +2194,8 @@ async def record_run(req: RecordRunRequest, user: User = Depends(get_required_us await db.create_asset_tx(conn, input_asset) # Check if output already exists (by content_hash) - return existing if so - if await db.asset_exists_by_name_tx(conn, output_hash): - existing = await db.get_asset(output_hash) + existing = await db.get_asset_by_name_tx(conn, output_hash) + if existing: logger.info(f"record_run: Output {output_hash[:16]}... already exists") return {"asset": existing, "activity": None, "existing": True}