From e4cbbb1fbc404f0458b68c26c1945b6174270568 Mon Sep 17 00:00:00 2001 From: gilesb Date: Sat, 10 Jan 2026 15:42:06 +0000 Subject: [PATCH] Create activity for existing assets in record_run When asset already exists, check if activity exists too. If no activity, create one for the existing asset. This fixes the case where an asset was registered but the activity creation failed or was skipped. Co-Authored-By: Claude Opus 4.5 --- server.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 67d123f..bdea6a9 100644 --- a/server.py +++ b/server.py @@ -2197,7 +2197,34 @@ async def record_run(req: RecordRunRequest, user: User = Depends(get_required_us 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} + # Check if activity already exists for this run + existing_activity = await db.get_activity(provenance["run_id"]) + if existing_activity: + logger.info(f"record_run: Activity {provenance['run_id'][:16]}... also exists") + return {"asset": existing, "activity": existing_activity, "existing": True} + # Asset exists but no activity - create one + logger.info(f"record_run: Creating activity for existing asset") + object_data = { + "type": existing.get("asset_type", "image").capitalize(), + "name": output_hash, + "id": f"https://{DOMAIN}/objects/{output_hash}", + "contentHash": { + "algorithm": "sha3-256", + "value": output_hash + }, + "attributedTo": f"https://{DOMAIN}/users/{user.username}", + "provenance": provenance + } + activity = { + "activity_id": provenance["run_id"], + "activity_type": "Create", + "actor_id": f"https://{DOMAIN}/users/{user.username}", + "object_data": object_data, + "published": now + } + activity = sign_activity(activity, user.username) + created_activity = await db.create_activity_tx(conn, activity) + return {"asset": existing, "activity": created_activity, "existing": True} # Create output asset with provenance - named by content_hash output_asset = {