molecule-core/platform/internal/registry/access_test.go
rabbitblood e09ad565e1 fix(registry): allow ancestor↔descendant A2A so audit_summary can reach PM
Found via deep workspace inspection during a maintenance cycle: Security
Auditor's hourly cron correctly tries to delegate_task its audit_summary
to PM, the platform proxy rejects with "access denied: workspaces cannot
communicate per hierarchy", the agent falls back to delegating to its
direct parent (Dev Lead), and PM's category_routing dispatcher (#75) is
never reached.

This breaks the audit-routing contract end-to-end. Every audit cycle was
landing on Dev Lead instead of being fanned out via PM's category_routing
to the right dev role (security → BE+DevOps, ui/ux → FE, etc).

## Root cause
`registry.CanCommunicate()` only allowed:
- self → self
- siblings (same parent)
- root-level siblings
- direct parent → child
- direct child → parent

A grandchild → grandparent (Security Auditor → PM, where parent is Dev
Lead and grandparent is PM) was DENIED. The original design wanted strict
hierarchy to prevent rogue horizontal A2A — but it also broke the
fundamental "child can talk to its leadership chain" pattern that any
audit/escalation flow needs.

## Fix
Generalise to ancestor ↔ descendant. Any workspace can talk to any
ancestor (any depth) and any descendant (any depth). Direct parent/child
remains a fast path that avoids the walk. Sibling rules unchanged.

Cousins still cannot directly communicate (would need to go through their
shared ancestor). Cross-subtree A2A is still rejected.

Implementation: `isAncestorOf(ancestorID, childID)` walks the parent
chain in Go with a maxAncestorWalk=32 safety cap so a malformed cycle in
the workspaces table cannot loop forever. One DB lookup per step. For a
typical 3-deep tree, this adds 1-2 extra lookups vs the old direct-parent
fast path. Could be optimized to a single recursive CTE if profiling
shows it matters; not now.

## Tests
- TestCanCommunicate_Denied_Grandchild → REPLACED with two new tests:
  - TestCanCommunicate_Allowed_GrandparentToGrandchild
  - TestCanCommunicate_Allowed_GrandchildToGrandparent  (the actual bug)
- TestCanCommunicate_Allowed_DeepAncestor — 4-level chain
- TestCanCommunicate_Denied_UnrelatedAncestors — ensures cross-subtree
  walks still terminate denied
- TestCanCommunicate_Denied_DifferentParents — extended with the walk
  lookup mocks so sqlmock doesn't log warnings
- TestCanCommunicate_Denied_CousinToRoot — same

All 13 tests pass clean. The previous direct parent/child / siblings /
self tests are unchanged (fast paths preserved).

## Why platform-level
Per the "platform-wide fixes are mine to ship" rule. Every org template
hits the same broken audit-routing chain — fixing it at the platform
benefits all users, not just molecule-dev. This unblocks #50 (PM
dispatcher prompt) and #75 (category_routing).
2026-04-14 22:18:38 -07:00

221 lines
7.0 KiB
Go

package registry
import (
"database/sql"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
)
func setupMockDB(t *testing.T) sqlmock.Sqlmock {
t.Helper()
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock: %v", err)
}
db.DB = mockDB
t.Cleanup(func() { mockDB.Close() })
return mock
}
func ptr(s string) *string { return &s }
func expectLookup(mock sqlmock.Sqlmock, id string, parentID *string) {
row := mock.NewRows([]string{"id", "parent_id"})
if parentID != nil {
row.AddRow(id, *parentID)
} else {
row.AddRow(id, nil)
}
mock.ExpectQuery("SELECT id, parent_id FROM workspaces WHERE id").
WithArgs(id).
WillReturnRows(row)
}
func expectNotFound(mock sqlmock.Sqlmock, id string) {
mock.ExpectQuery("SELECT id, parent_id FROM workspaces WHERE id").
WithArgs(id).
WillReturnError(sql.ErrNoRows)
}
// ---------- Tests ----------
func TestCanCommunicate_SameWorkspace(t *testing.T) {
mock := setupMockDB(t)
expectLookup(mock, "ws-1", nil)
expectLookup(mock, "ws-1", nil)
if !CanCommunicate("ws-1", "ws-1") {
t.Error("same workspace should always communicate")
}
}
func TestCanCommunicate_Siblings(t *testing.T) {
mock := setupMockDB(t)
// ws-a and ws-b are siblings (same parent ws-parent)
expectLookup(mock, "ws-a", ptr("ws-parent"))
expectLookup(mock, "ws-b", ptr("ws-parent"))
if !CanCommunicate("ws-a", "ws-b") {
t.Error("siblings should communicate")
}
}
func TestCanCommunicate_RootSiblings(t *testing.T) {
mock := setupMockDB(t)
// Both at root level (no parent)
expectLookup(mock, "ws-a", nil)
expectLookup(mock, "ws-b", nil)
if !CanCommunicate("ws-a", "ws-b") {
t.Error("root-level siblings should communicate")
}
}
func TestCanCommunicate_ParentToChild(t *testing.T) {
mock := setupMockDB(t)
// ws-parent talks to ws-child (whose parent is ws-parent)
expectLookup(mock, "ws-parent", nil)
expectLookup(mock, "ws-child", ptr("ws-parent"))
if !CanCommunicate("ws-parent", "ws-child") {
t.Error("parent should communicate with child")
}
}
func TestCanCommunicate_ChildToParent(t *testing.T) {
mock := setupMockDB(t)
// ws-child talks up to ws-parent
expectLookup(mock, "ws-child", ptr("ws-parent"))
expectLookup(mock, "ws-parent", nil)
if !CanCommunicate("ws-child", "ws-parent") {
t.Error("child should communicate with parent")
}
}
func TestCanCommunicate_Denied_DifferentParents(t *testing.T) {
mock := setupMockDB(t)
// ws-a (parent: p1) and ws-b (parent: p2) — not siblings, no shared ancestor.
expectLookup(mock, "ws-a", ptr("p1"))
expectLookup(mock, "ws-b", ptr("p2"))
// Walk #1: isAncestorOf(ws-a, p2) → p2 is parentless, false.
expectLookup(mock, "p2", nil)
// Walk #2: isAncestorOf(ws-b, p1) → p1 is parentless, false.
expectLookup(mock, "p1", nil)
if CanCommunicate("ws-a", "ws-b") {
t.Error("workspaces with different parents should NOT communicate")
}
}
func TestCanCommunicate_Denied_CousinToRoot(t *testing.T) {
mock := setupMockDB(t)
// ws-child (parent: ws-mid, which has its own root ws-other-root) and
// ws-root (a different parentless workspace).
// The ancestor walk from ws-child should reach ws-other-root but never
// ws-root, so communication is denied.
expectLookup(mock, "ws-child", ptr("ws-mid"))
expectLookup(mock, "ws-root", nil)
// Ancestor walk: starts at *caller.ParentID = ws-mid. Walks ws-mid → ws-other-root → nil.
expectLookup(mock, "ws-mid", ptr("ws-other-root"))
expectLookup(mock, "ws-other-root", nil)
if CanCommunicate("ws-child", "ws-root") {
t.Error("child should NOT communicate with unrelated root workspace")
}
}
func TestCanCommunicate_Denied_CallerNotFound(t *testing.T) {
mock := setupMockDB(t)
expectNotFound(mock, "ws-missing")
if CanCommunicate("ws-missing", "ws-target") {
t.Error("nonexistent caller should be denied")
}
}
func TestCanCommunicate_Denied_TargetNotFound(t *testing.T) {
mock := setupMockDB(t)
expectLookup(mock, "ws-caller", nil)
expectNotFound(mock, "ws-missing")
if CanCommunicate("ws-caller", "ws-missing") {
t.Error("nonexistent target should be denied")
}
}
func TestCanCommunicate_Allowed_GrandparentToGrandchild(t *testing.T) {
mock := setupMockDB(t)
// PM (no parent) → Backend Engineer (parent: Dev Lead, parent: PM).
// Originally rejected ("grandparent should NOT communicate with grandchild
// directly") — that broke audit_summary routing because Security Auditor
// could not delegate up to PM. The hierarchy is now ancestor↔descendant.
expectLookup(mock, "ws-pm", nil)
expectLookup(mock, "ws-be", ptr("ws-dl"))
// Ancestor walk: target.ParentID = ws-dl. isAncestorOf(ws-pm, ws-dl).
// Walks ws-dl → ws-pm → match. (Walk lookup #1: ws-dl.)
expectLookup(mock, "ws-dl", ptr("ws-pm"))
if !CanCommunicate("ws-pm", "ws-be") {
t.Error("PM should be able to communicate with Backend Engineer (descendant)")
}
}
func TestCanCommunicate_Allowed_GrandchildToGrandparent(t *testing.T) {
mock := setupMockDB(t)
// Security Auditor (parent: Dev Lead) → PM (parent of Dev Lead).
// This is the Security Auditor → PM audit_summary delivery path.
expectLookup(mock, "ws-sec", ptr("ws-dl"))
expectLookup(mock, "ws-pm", nil)
// Direct parent → child fast path: target.ParentID = nil, skip.
// Direct child → parent: caller.ParentID = ws-dl, target.ID = ws-pm,
// ws-dl != ws-pm, skip.
// Distant ancestor → descendant: target.ParentID = nil, skip.
// Distant descendant → ancestor: caller.ParentID = ws-dl. Walks
// isAncestorOf(ws-pm, ws-dl) → looks up ws-dl → returns ws-pm → match.
expectLookup(mock, "ws-dl", ptr("ws-pm"))
if !CanCommunicate("ws-sec", "ws-pm") {
t.Error("Security Auditor should be able to send audit_summary up to PM")
}
}
func TestCanCommunicate_Allowed_DeepAncestor(t *testing.T) {
mock := setupMockDB(t)
// Four-level chain: ws-leaf (parent: ws-l3, parent: ws-l2, parent: ws-l1).
// ws-leaf → ws-l1 should be allowed.
expectLookup(mock, "ws-leaf", ptr("ws-l3"))
expectLookup(mock, "ws-l1", nil)
// Distant descendant → ancestor walk: starts at ws-l3.
// ws-l3 → ws-l2: not ws-l1, continue.
// ws-l2 → ws-l1: match!
expectLookup(mock, "ws-l3", ptr("ws-l2"))
expectLookup(mock, "ws-l2", ptr("ws-l1"))
if !CanCommunicate("ws-leaf", "ws-l1") {
t.Error("4-level descendant should reach root ancestor")
}
}
func TestCanCommunicate_Denied_UnrelatedAncestors(t *testing.T) {
mock := setupMockDB(t)
// Two separate org subtrees:
// tree A: ws-a-leaf → ws-a-mid → ws-a-root
// tree B: ws-b-leaf → ws-b-mid → ws-b-root
// ws-a-leaf → ws-b-root must be denied even though both have parents
// (no shared ancestor).
expectLookup(mock, "ws-a-leaf", ptr("ws-a-mid"))
expectLookup(mock, "ws-b-root", nil)
// Walk: isAncestorOf(ws-b-root, ws-a-mid).
// ws-a-mid → ws-a-root: not ws-b-root, continue.
// ws-a-root has no parent → false.
expectLookup(mock, "ws-a-mid", ptr("ws-a-root"))
expectLookup(mock, "ws-a-root", nil)
if CanCommunicate("ws-a-leaf", "ws-b-root") {
t.Error("workspaces in different subtrees should NOT communicate via the walk")
}
}