Skip to content

Implement Namespaced Properties for Type-Safe Node Type Changes #794

Closed

Description

@malibio

Overview

When a node's type changes (e.g., task → text), the old type's properties should remain dormant rather than being orphaned or conflicting with the new type's properties. This requires implementing namespaced properties where each type's properties live under their own namespace.

Architecture Context

This issue is part of the Universal Graph Architecture (Epic #783). The original design specified namespaced properties (node.properties.task.completed), but during the hub-and-spoke transition and back, we ended up with flat properties (node.properties.completed).

Current State (Problematic)

Storage: Flat properties

{
  "properties": {
    "completed": true,
    "due_date": "2025-01-15"
  }
}

Problems when type changes (e.g., task → invoice):

  1. Old properties (completed, due_date) remain orphaned
  2. If both types have same property name with different semantics (e.g., status), values conflict
  3. Partial indexes for old type may still match the node
  4. No way to "restore" old type's data if user changes back

Current code is inconsistent:

  • validate_node_with_fields() (line 876) tries namespaced first, falls back to flat
  • apply_schema_defaults_with_fields() writes flat
  • update_node() merges flat properties

Target State

Storage: Namespaced properties by type

{
  "properties": {
    "task": {
      "completed": true,
      "due_date": "2025-01-15"
    }
  }
}

When type changes to invoice:

{
  "properties": {
    "task": {
      "completed": true,
      "due_date": "2025-01-15"
    },
    "invoice": {
      "status": "draft",
      "amount": null
    }
  }
}

Benefits:

  1. Old type's properties preserved (dormant, not orphaned)
  2. No property name conflicts between types
  3. Can restore previous type's data if user changes back
  4. Clear separation of concerns
  5. Partial indexes work correctly (properties.task.completed vs properties.invoice.status)

Acceptance Criteria

Phase 1: Core Storage Changes

  • Update apply_schema_defaults_with_fields() to write under type namespace
  • Update update_node() property merge to use type namespace
  • Update validate_node_with_fields() to read from type namespace (remove flat fallback)
  • Update create_node() to store properties under type namespace

Phase 2: Query & Index Updates

  • Update partial indexes to use namespaced paths (properties.task.completed)
  • Update query patterns in QueryService for namespaced access
  • Update any direct property access in handlers

Phase 3: Serialization/Deserialization

  • Ensure Node struct properly deserializes namespaced properties
  • Update any frontend property access patterns
  • Verify MCP handlers return correct property structure

Phase 4: Quality

  • All existing tests updated for namespaced properties
  • New tests for type change scenarios (properties preserved)
  • New tests for same-property-name different-type scenarios
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Property Access Pattern

Writing (on create/update):

// Instead of:
props_obj.insert(field.name.clone(), value);

// Do:
let type_props = props_obj
    .entry(&node.node_type)
    .or_insert_with(|| json!({}));
type_props.as_object_mut()
    .unwrap()
    .insert(field.name.clone(), value);

Reading (on validate/query):

// Instead of:
node.properties.get(&field.name)

// Do:
node.properties
    .get(&node.node_type)
    .and_then(|type_props| type_props.get(&field.name))

Files to Modify

  • packages/core/src/services/node_service.rs - Property read/write logic
  • packages/core/src/db/surreal_store.rs - Storage layer
  • packages/core/src/services/query_service/mod.rs - Query patterns
  • packages/core/src/db/schema.surql - Partial index definitions
  • Frontend property access (if any direct access exists)

Migration Consideration

Since this is pre-release with no users, we can:

  1. Reset database after implementation
  2. No migration strategy needed
  3. Update all tests to use new structure

Related Issues

Notes

  • This restores the original design intent that was lost during architecture transitions
  • Pre-release system with zero users - breaking changes are acceptable
  • Should be coordinated with Consolidate Edge Tables into Universal Edge Table #788 (edge table consolidation) as both affect data model

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions