From 6be0bfdfb07c096d6d58653f01508014aa716690 Mon Sep 17 00:00:00 2001 From: vegard Date: Fri, 20 Mar 2026 06:32:31 +0000 Subject: [PATCH] =?UTF-8?q?synops-respond:=20node+edge+access=20i=20=C3=A9?= =?UTF-8?q?n=20transaksjon=20for=20sanntid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PG NOTIFY fyrer ved COMMIT. Når alt skrives i én transaksjon eksisterer node_access allerede når WS mottar node_changed, og kan levere noden til alle deltakere i sanntid. Co-Authored-By: Claude Opus 4.6 (1M context) --- tools/synops-respond/src/main.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/synops-respond/src/main.rs b/tools/synops-respond/src/main.rs index fc2a179..46ea7e6 100644 --- a/tools/synops-respond/src/main.rs +++ b/tools/synops-respond/src/main.rs @@ -511,7 +511,12 @@ async fn write_to_db( let edge_id = Uuid::now_v7(); let metadata = serde_json::json!({}); - // Svar-node + // Alt i én transaksjon: node + edge + access. + // PG NOTIFY fyrer ved COMMIT — da eksisterer access allerede, + // og WS kan levere noden til alle deltakere i sanntid. + let mut tx = db.begin().await + .map_err(|e| format!("Kunne ikke starte transaksjon: {e}"))?; + sqlx::query( "INSERT INTO nodes (id, node_kind, content, visibility, metadata, created_by) \ VALUES ($1, 'content', $2, 'hidden'::visibility, $3, $4)", @@ -520,13 +525,10 @@ async fn write_to_db( .bind(response_text) .bind(&metadata) .bind(agent_node_id) - .execute(db) + .execute(&mut *tx) .await .map_err(|e| format!("PG insert svar-node feilet: {e}"))?; - tracing::info!(reply_node_id = %reply_id, "Svar-node opprettet"); - - // belongs_to-edge: svar → kommunikasjonsnode sqlx::query( "INSERT INTO edges (id, source_id, target_id, edge_type, metadata, system, created_by) \ VALUES ($1, $2, $3, 'belongs_to', '{}', false, $4)", @@ -535,11 +537,10 @@ async fn write_to_db( .bind(reply_id) .bind(communication_id) .bind(agent_node_id) - .execute(db) + .execute(&mut *tx) .await .map_err(|e| format!("PG insert belongs_to-edge feilet: {e}"))?; - // Propager access: alle deltakere i chatten får lesetilgang til svaret sqlx::query( "INSERT INTO node_access (subject_id, object_id, access, via_edge) SELECT e.source_id, $1, 'reader', $2 @@ -551,11 +552,14 @@ async fn write_to_db( .bind(reply_id) .bind(edge_id) .bind(communication_id) - .execute(db) + .execute(&mut *tx) .await .map_err(|e| format!("PG access-propagering feilet: {e}"))?; - tracing::info!(reply_node_id = %reply_id, "Access propagert til chat-deltakere"); + tx.commit().await + .map_err(|e| format!("Kunne ikke committe transaksjon: {e}"))?; + + tracing::info!(reply_node_id = %reply_id, "Svar + edge + access committet i én transaksjon"); // ai_usage_log sqlx::query(