<% '======================================================================================================================= ' Graph relationships (spec FR-023, section 8.1, section 14, ADR-014): entities and typed, directed, evidence-backed ' edges between entities, memories and documents. ' ' Graph_NodeNamespace kind, id namespace of an endpoint in the caller's tenant, "" when it does not exist ' Graph_NodeVisibleSql side, allowed, activeOnly ' SQL condition on relationships r: the endpoint ("source"|"target") lives in one ' of the allowed namespaces (and, with activeOnly, is not a retired memory) ' Graph_ForgetNode kind, id deletes every edge touching the node and the edges' evidence (inside the ' caller's transaction; used when a memory or entity is deleted) ' Graph_IsType / Graph_IsKind validation helpers; relationship types are upper-case words (IS_A, DEPENDS_ON) ' ' Visibility rule (section 8.1): an edge is shown only when the caller can read the edge's namespace and both ' endpoints' namespaces. Anything else is left out without a trace, so traversal cannot reveal hidden nodes or edges. '======================================================================================================================= Const GRAPH_MAX_DEPTH = 3 Const GRAPH_MAX_NODES = 100 Const GRAPH_MAX_EDGES = 300 Function Graph_IsKind(k) Graph_IsKind = False If k = "entity" Or k = "memory" Or k = "document" Then Graph_IsKind = True End If End Function Function Graph_IsType(t) Dim re Set re = New RegExp re.Pattern = "^[A-Z][A-Z0-9_]{1,39}$" re.IgnoreCase = False Graph_IsType = re.Test(t) End Function Function Graph_NodeNamespace(kind, id) Dim rs Graph_NodeNamespace = "" If Not Api_IsId(id) Then Exit Function End If Select Case kind Case "entity" Set rs = DAL().Query("SELECT namespace_id FROM entities WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId)) Case "memory" Set rs = DAL().Query("SELECT namespace_id FROM memories WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId)) Case "document" Set rs = DAL().Query("SELECT namespace_id FROM documents WHERE id = ? AND tenant_id = ?", Array(id, Api_TenantId)) Case Else Exit Function End Select If Not rs.EOF Then Graph_NodeNamespace = "" & rs(0) End If rs.Close End Function ' allowed: the inline id list from Api_AllowedNamespaceIds("read") (never empty when called) Function Graph_NodeVisibleSql(side, allowed, activeOnly) Dim memExtra memExtra = "" If activeOnly Then memExtra = " AND m.lifecycle_status = 'active'" End If Graph_NodeVisibleSql = "(CASE r." & side & "_kind" & _ " WHEN 'entity' THEN (SELECT e.namespace_id FROM entities e WHERE e.id = r." & side & "_id AND e.tenant_id = r.tenant_id)" & _ " WHEN 'memory' THEN (SELECT m.namespace_id FROM memories m WHERE m.id = r." & side & "_id AND m.tenant_id = r.tenant_id" & memExtra & ")" & _ " WHEN 'document' THEN (SELECT d.namespace_id FROM documents d WHERE d.id = r." & side & "_id AND d.tenant_id = r.tenant_id)" & _ " END) IN (" & allowed & ")" End Function Sub Graph_ForgetNode(kind, id) DAL().Execute "DELETE FROM evidence_links WHERE owner_kind = 'relationship' AND owner_id IN " & _ "(SELECT id FROM relationships WHERE (source_kind = ? AND source_id = ?) OR (target_kind = ? AND target_id = ?))", Array(kind, id, kind, id) DAL().Execute "DELETE FROM relationships WHERE (source_kind = ? AND source_id = ?) OR (target_kind = ? AND target_id = ?)", Array(kind, id, kind, id) End Sub ' Lower-case, trimmed, inner whitespace collapsed: the default canonical key of an entity name Function Graph_CanonicalKey(s) Dim re Set re = New RegExp re.Pattern = "\s+" re.Global = True Graph_CanonicalKey = LCase(Trim(re.Replace("" & s, " "))) End Function %>