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 = {