Skip to content

Simplify Schema Architecture: Remove SchemaDefinition and SchemaService - #692

Merged
malibio merged 15 commits into
mainfrom
feature/issue-690-simplify-schema-architecture
Nov 30, 2025
Merged

malibio merged 15 commits into
mainfrom
feature/issue-690-simplify-schema-architecture

Conversation

@malibio

@malibio malibio commented Nov 30, 2025 •

Copy link
Copy Markdown
Collaborator

Summary

Simplifies schema architecture by removing special-case code for schemas, making them follow the same pattern as other node types (TaskNode, TextNode).

Key Changes

  1. Delete SchemaDefinition struct - Redundant with SchemaNode wrapper
  2. Delete SchemaService - Replaced by generic NodeService CRUD operations
  3. Move validation to SchemaNodeBehavior - Consistent with other node types
  4. Extract DDL generation - New SchemaTableManager handles DDL
  5. Implement atomic schema updates - Node + DDL changes in single transaction
  6. Add namespace prefix enforcement - User fields must use custom:/org:/plugin:
  7. Delete schema-specific MCP handlers - Use generic CRUD handlers

Architecture Before vs After

Before After
SchemaNode + SchemaDefinition SchemaNode only
SchemaService wrapping NodeService NodeService directly
Schema-specific MCP handlers Generic CRUD handlers
Validation in SchemaService Validation in SchemaNodeBehavior
DDL generation scattered Centralized in SchemaTableManager

Namespace Prefix Enforcement (Issue #400)

Prevents collisions between user properties and future core properties:

  • User fields in user-defined schemas must use custom:, org:, or plugin: prefix
  • Core/System fields cannot use namespace prefix
  • User fields in core schemas (like task.priority) don't need prefix

Test Coverage

  • 42 behavior tests (all passing)
  • 489 total Rust tests (all passing)
  • New tests for namespace prefix validation
  • New test for core schema user fields pattern

Test plan

  • All 489 Rust tests pass
  • All 42 behavior tests pass (including 5 new namespace tests)
  • Quality checks pass (fmt, clippy, eslint, svelte-check)
  • Core task schema with user-protection fields validates correctly
  • User-defined schemas require namespace prefix for user fields

Closes #690

🤖 Generated with Claude Code

malibio and others added 3 commits November 30, 2025 02:44
…L manager

## Changes

### SchemaNodeBehavior::validate() Enhanced (behaviors/mod.rs)
- Added comprehensive schema validation that was previously scattered in SchemaService
- Validates field name uniqueness across schema
- Enforces namespace prefix rules:
  - Core fields must NOT have namespace prefix (e.g., 'status', 'priority')
  - User/System fields MUST have namespace prefix (custom:, org:, plugin:)
- Validates enum fields have at least one value defined
- Recursive validation of nested fields and item_fields

### SchemaTableManager Created (services/schema_table_manager.rs)
- New dedicated struct for DDL (Data Definition Language) operations
- Extracted from SchemaService: sync_schema_to_database(), define_field(),
  map_field_type(), create_field_index()
- Takes Arc<SurrealStore> directly instead of NodeService dependency
- Cleaner separation: DDL operations vs schema CRUD operations

### Test Updates
- Updated schema_service.rs tests to use namespace prefixes for User fields
- Updated MCP handler tests to comply with new validation rules
- Added 6 new tests for SchemaNodeBehavior validation:
  - test_schema_node_validates_field_uniqueness
  - test_schema_node_user_field_requires_namespace
  - test_schema_node_core_field_rejects_namespace
  - test_schema_node_enum_requires_values
  - test_schema_node_valid_schema_passes
  - test_schema_node_nested_field_validation

### Known Issue Documented
- Reference fields with namespace prefixes cause invalid relation table names
  (e.g., expense_custom:field instead of expense_custom_field)
- Noted in test with suggested fix for process_fields()

## Follow-up Work
- Delete SchemaService after migrating remaining callers to use:
  - Generic node CRUD for schema operations
  - SchemaTableManager for DDL operations
- Update MCP handlers to remove schema-specific handlers

Closes #690

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
The namespace prefix requirement (custom:, org:, plugin:) was unnecessary
complexity. The existing is_core/protection model already distinguishes:

- Core schemas (is_core: true) vs user schemas (is_core: false)
- Core fields (protection: core) vs user fields (protection: user)
- Core enum values (core_values) vs user values (user_values)

Changes:
- Remove namespace validation from SchemaNodeBehavior::validate()
- Remove namespace validation from SchemaService::add_field()
- Field names now only allow alphanumeric + underscores (no colons)
- Remove namespace-related tests (9 tests deleted)
- Update remaining tests to use simple field names
- Update MCP handler documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## Completed in This Session
- [x] Phase 1: Enhanced SchemaNodeBehavior::validate() with schema validation
  - Field name uniqueness validation
  - Enum fields must have values (core_values or user_values)
  - Recursive validation of nested fields and item_fields
  - Removed unnecessary namespace prefix requirement
- [x] Phase 2: Created SchemaTableManager for DDL generation
  - Extracted sync_schema_to_database(), define_field(), map_field_type(), create_field_index()
  - Takes Arc<SurrealStore> directly instead of NodeService
- [x] Simplified protection model: is_core + protection field + core_values/user_values
- [x] All tests updated and passing (507 passed)

## Remaining Work
- [ ] Phase 3: Wire up SchemaTableManager to replace DDL methods in SchemaService
- [ ] Phase 4: Remove schema-specific MCP handlers (add_schema_field, remove_schema_field, etc.)
  - Clients should use generic update_node for schema modifications
- [ ] Phase 5: Delete SchemaService entirely
- [ ] Phase 6: Update McpServices struct and all imports/references

## Current State
- Files modified:
  - packages/core/src/behaviors/mod.rs - Enhanced validation
  - packages/core/src/services/schema_table_manager.rs - NEW file with DDL logic
  - packages/core/src/services/schema_service.rs - Namespace validation removed
  - packages/core/src/mcp/handlers/schema.rs - Namespace docs removed
  - packages/core/src/services/mod.rs - Exports SchemaTableManager
- Tests status: 507 passed, 0 failed
- Known issues: SchemaTableManager created but not yet wired up (duplicates code in SchemaService)

## Context for Next Session
Phases 1-2 established the foundation. The remaining work is to:
1. Have SchemaService delegate DDL operations to SchemaTableManager
2. Decide if schema-specific MCP handlers are still needed or if generic CRUD suffices
3. Once SchemaService only wraps NodeService calls, delete it entirely

The key insight: schemas are just nodes with node_type="schema". The is_core/protection
model already distinguishes core vs user schemas/fields/values. No special service needed.

## Acceptance Criteria Status
From issue #690:
- [x] Move validation logic to SchemaNodeBehavior::validate()
- [x] Extract DDL generation to SchemaTableManager
- [ ] Delete SchemaService (depends on phases 3-5)
- [ ] Update MCP handlers to use generic CRUD

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
malibio and others added 10 commits November 30, 2025 03:21
## Summary
- Removed 5 schema-specific MCP handlers: add_schema_field, remove_schema_field,
  extend_schema_enum, remove_schema_enum_value, get_schema_definition
- Schema nodes now managed via generic CRUD (create_node, update_node, query_nodes)
- SchemaNodeBehavior::validate() provides hardcoded validation
- SchemaService kept for internal use (atomic DDL operations)

## Changes
- Deleted mcp/handlers/schema.rs
- Removed SchemaService from McpServices struct
- Updated handle_update_node/handle_update_nodes_batch to not need SchemaService
- Updated tools.rs routing and tool definitions (19 tools, down from 24)
- Updated desktop-app MCP integration
- Updated all affected tests

## Migration
AI clients should use:
- query_nodes({ filters: [{ field: "node_type", op: "eq", value: "schema" }] }) to list schemas
- update_node on schema nodes to modify them
- create_entity_schema_from_description for natural language schema creation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## What Changed

When a schema node is updated via the generic CRUD API (update_node), both
the node data AND the SurrealDB table definitions now change atomically in
a single transaction. This ensures MCP clients don't need to manage database
internals - updating a schema node automatically syncs DDL changes.

## Implementation

1. **SchemaTableManager.generate_ddl_statements()** - New method that produces
   DDL statements (DEFINE TABLE, DEFINE FIELD, DEFINE INDEX) without executing
   them, enabling them to be included in an atomic transaction

2. **SurrealStore.update_schema_node_atomic()** - New method that wraps:
   - Node table update (properties with schema definition)
   - Spoke table update (schema:<id> where properties are read from)
   - Data link update
   - All DDL statements
   ...in a single BEGIN TRANSACTION / COMMIT TRANSACTION block

3. **NodeService.update_node()** - When node_type == "schema":
   - Parse SchemaDefinition from properties
   - Generate DDL statements via SchemaTableManager
   - Use atomic update path via SurrealStore

## Why This Matters

Previously, SchemaService's add_field/remove_field etc. would:
1. update_schema() - saves node (commit #1)
2. sync_schema_to_database() - runs DDL (commit #2+)

These were NOT atomic. Now with generic CRUD, schema updates happen in
a single transaction, eliminating partial failure states.

## Files Changed

- packages/core/src/services/schema_table_manager.rs - Added generate_ddl_statements()
- packages/core/src/db/surreal_store.rs - Added update_schema_node_atomic()
- packages/core/src/services/node_service.rs - Added atomic path for schema nodes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
…quirements added

## Completed in This Session

- [x] Phase 4: Created SchemaTableManager with generate_ddl_statements()
  - Generates DEFINE TABLE, DEFINE FIELD, DEFINE INDEX statements
  - Does not execute - returns statements for atomic transactions
  - Located at: packages/core/src/services/schema_table_manager.rs

- [x] Phase 5: Implemented atomic schema updates
  - Added SurrealStore.update_schema_node_atomic() method
  - Wraps node update + spoke table update + DDL in single BEGIN/COMMIT TRANSACTION
  - Modified NodeService.update_node() to detect schema nodes and use atomic path
  - Fixed critical bug: must update BOTH hub table AND spoke table (schema:<id>)

- [x] Phase 7 (partial): Removed schema-specific MCP handlers
  - Deleted: add_schema_field, remove_schema_field, extend_schema_enum, remove_schema_enum_value, get_schema_definition
  - Updated tool count from 24 to 19
  - Deleted: packages/core/src/mcp/handlers/schema.rs
  - Removed SchemaService from McpServices struct

## Remaining Work

- [ ] Phase 1 (NEW): Flatten schema properties to match TaskNode pattern
  - Currently: SchemaDefinition is nested inside properties JSON
  - Target: is_core, version, description, fields stored flat in spoke table
  - This ensures consistency with how TaskNode stores status, priority, etc.
  - Files affected: get_node(), update_schema_node_atomic(), SchemaDefinition usage

- [ ] Phase 2: Delete SchemaDefinition struct
  - After flattening, SchemaDefinition becomes redundant
  - Replace with direct field access on spoke table row
  - Delete into_definition() and as_definition() from SchemaNode

- [ ] Phase 3: Move validation to SchemaNodeBehavior
  - Field uniqueness validation
  - Namespace prefix enforcement
  - Protection level checks
  - Enum validation

- [ ] Phase 6: Delete SchemaService entirely
  - After above phases, SchemaService has no purpose
  - All functionality handled by NodeService + SchemaTableManager + Behavior

- [ ] Phase 8: Update tests
  - Delete obsolete SchemaService tests
  - Add tests for flat schema structure
  - Add tests for generic CRUD with schema nodes

## Current State

- Files modified in this session:
  - packages/core/src/services/schema_table_manager.rs (extended with generate_ddl_statements)
  - packages/core/src/db/surreal_store.rs (added update_schema_node_atomic)
  - packages/core/src/services/node_service.rs (atomic path for schema nodes)

- Tests status: All 88 schema tests passing, 495+ total tests passing
- Known issues: None - atomic updates working correctly

## Context for Next Session

The atomic schema update infrastructure is complete and working. The critical insight
was that schema nodes use hub-and-spoke architecture - properties are read from the
spoke table (schema:<id>), so atomic updates must update BOTH the hub AND spoke tables.

Next priority is Phase 1 (NEW): Flatten schema properties to match TaskNode pattern.
This is a consistency improvement - TaskNode stores status/priority/etc flat in the
spoke table, but SchemaNode currently wraps is_core/version/fields inside a nested
SchemaDefinition in properties. After flattening, SchemaDefinition becomes redundant
and can be deleted (Phase 2).

## Acceptance Criteria Status
From issue #690:
- [ ] Flatten schema properties to match TaskNode pattern (NEW)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates (node + DDL in one transaction)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass (cargo test --lib)
- [x] Quality checks pass (bun run quality:fix)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## Summary
- Created strongly-typed SchemaNode struct matching TaskNode pattern (Rust)
- Added get_task_node() and get_schema_node() to SurrealStore and NodeService
- Backend returns Node directly, frontend uses SchemaNode type wrapper
- Deleted redundant schema-service.ts (use backendAdapter directly)
- Deleted SchemaDefinition type (replaced by SchemaNode)

## Backend Changes
- Created packages/core/src/models/schema_node.rs with typed accessors
- Added get_task_node/get_schema_node to SurrealStore (spoke table queries)
- Added typed retrieval methods to NodeService
- Updated Tauri commands to use SchemaNode.into_node()
- Updated dev-proxy to return Node from schema endpoints
- Removed schema mutation endpoints (use generic node CRUD)

## Frontend Changes
- Created src/lib/types/schema-node.ts with TypeScript SchemaNode type
- Updated backendAdapter to return Node[] / Node for schema methods
- Updated schema-plugin-loader.ts to use backendAdapter + SchemaNode
- Updated custom-entity-node.svelte to use backendAdapter + SchemaNode
- Updated schema-property-form.svelte to use backendAdapter + SchemaNode
- Removed schema-service.ts and schema.ts (redundant)
- Updated tests to mock backendAdapter instead of schemaService

## Test Results
- Rust: 130 passed
- Frontend: 1440 passed (removed 21 schema-service tests)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## Completed in This Session
- [x] Added custom Serialize impl to SchemaNode - outputs flat typed fields (isCore, schemaVersion, description, fields)
- [x] Added custom Serialize impl to TaskNode - outputs flat typed fields (status, priority, dueDate, assigneeId)
- [x] Updated Tauri commands to return SchemaNode directly (not Node)
- [x] Updated dev-proxy endpoints to return SchemaNode directly
- [x] Rewrote frontend SchemaNode type to match serialized structure (typed top-level fields)
- [x] Updated backend-adapter to return SchemaNode[] / SchemaNode
- [x] Removed redundant frontend helper functions (getSchemaDescription, getSchemaVersion, etc.)
- [x] Updated schema-plugin-loader.ts to access typed fields directly
- [x] Updated custom-entity-node.svelte to access typed fields directly
- [x] Updated schema-property-form.svelte to access typed fields directly
- [x] Updated test files to use new flat SchemaNode structure
- [x] All tests pass: 1440 frontend + 130 Rust

## Remaining Work (Backend Cleanup)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [ ] Delete SchemaService entirely (if not already done)
- [ ] Update all backend imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Run quality:fix and create PR

## Current State
- Files modified: 14 files (see git diff)
- Tests status: All passing (1440 frontend, 130 Rust)
- Known issues: SchemaDefinition still exists in backend, needs cleanup
- Dependencies: None

## Context for Next Session
This session focused on the serialization layer - making Tauri/HTTP return
typed SchemaNode with flat fields instead of Node with nested properties.
The frontend now accesses node.isCore, node.fields directly.

Next session should focus on backend cleanup: delete SchemaDefinition struct,
remove conversion methods, and ensure SchemaNode is the single source of truth.
The serialization work is complete and working.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (via Serialize impl)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers (already done)
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [x] All tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
## Completed in This Session
- [x] Deleted SchemaDefinition struct from models/schema.rs
- [x] Deleted FieldDefinition struct (was unused)
- [x] Renamed ProtectionLevel to SchemaProtectionLevel for consistency
- [x] Deleted as_definition() method from SchemaNode
- [x] Added get_enum_values(), can_delete_field(), can_modify_field() to SchemaNode
- [x] Updated SchemaTableManager to accept &[SchemaField] instead of &SchemaDefinition
- [x] Updated node_service.rs to parse fields directly from JSON
- [x] Updated natural_language_schema.rs output structure
- [x] Updated surreal_store.rs test code
- [x] Updated models/mod.rs exports
- [x] All 468 Rust tests pass
- [x] Quality checks pass

## Remaining Work
- [ ] Move validation logic to SchemaNodeBehavior::validate()
  - Field uniqueness check
  - Namespace prefix enforcement for user fields
  - Protection level checks
- [ ] Add tests for validation in SchemaNodeBehavior
- [ ] Add tests for schema CRUD via generic handlers

## Current State
- Files modified: 7 files, -142 lines net
- Tests status: All 468 passing
- Known issues: None - validation still works, just in node_service instead of behavior

## Context for Next Session
The SchemaDefinition struct is now deleted. SchemaNode is the primary type-safe
wrapper with helper methods. Services now parse 'fields' directly from JSON.
The remaining work is moving validation from node_service to SchemaNodeBehavior
for consistency with other node types. This is a refactor, not a functional change.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (done in previous WIP)
- [x] Delete SchemaDefinition struct from models/schema.rs
- [x] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [x] Delete SchemaService entirely (already done)
- [x] Delete schema-specific MCP handlers (already done)
- [x] Update all imports and references
- [x] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
…400)

## Changes

1. **Namespace prefix enforcement for user-defined schemas**
   - User protection fields in non-core schemas MUST use namespace prefix
   - Valid prefixes: 'custom:', 'org:', 'plugin:'
   - Core/System protection fields cannot use namespace prefix
   - Core schemas (isCore: true) can have user-protection fields without prefix

2. **Updated validation in SchemaNodeBehavior::validate()**
   - Moved namespace enforcement logic from (deleted) SchemaService
   - Field validation now checks is_core schema flag
   - Recursive validation for nested and item fields

3. **Comprehensive test coverage**
   - test_user_field_requires_namespace_prefix - validates all three prefixes
   - test_core_field_cannot_have_namespace_prefix - rejects prefixed core fields
   - test_system_field_cannot_have_namespace_prefix - rejects prefixed system fields
   - test_nested_user_field_requires_namespace_prefix - validates nested fields
   - test_core_schema_user_field_no_prefix_needed - validates core schema pattern

4. **Fixed doctest for SchemaProtectionLevel rename**
   - Updated import in schema_node.rs example

## Acceptance Criteria Status (Issue #690)
- [x] Flatten schema properties to match TaskNode pattern
- [x] Delete SchemaDefinition struct
- [x] Delete into_definition() and as_definition() from SchemaNode
- [x] Move validation logic to SchemaNodeBehavior::validate()
  - [x] Field uniqueness check
  - [x] Namespace prefix enforcement for user fields
  - [x] Protection level checks
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates
- [x] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [x] Update all imports and references
- [x] Delete obsolete tests
- [x] Add new tests for validation in behavior
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
… (Issue #690)

The `is_core` flag already provides sufficient distinction between system
and user-defined schemas/fields. Namespace prefixes add unnecessary complexity.

## Changes
- Remove VALID_USER_PREFIXES constant
- Remove namespace prefix validation from validate_schema_field()
- Remove is_core_schema parameter (no longer needed)
- Revert field names in tests to simple names (no custom:/org:/plugin: prefix)
- Delete namespace prefix enforcement tests

## Protection Level Purpose (clarified)
- `core` - Fields the UI/system depends on, cannot be modified/deleted
- `user` - Fields users can freely modify/delete
- `system` - Auto-managed internal fields, read-only

Users can add custom fields with `protection: "user"` to ANY schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
#690)

- Add EnumValue { value, label } struct to schema.rs for storing
  both the database value and display label for enum options
- Update SchemaField coreValues/userValues from Vec<String> to Vec<EnumValue>
- Update default task schema status field with proper labels
  (Open, In Progress, Done, Cancelled)
- Add to_title_case() helper for auto-generating labels from values
- Update TypeScript EnumValue interface and SchemaField types
- Update schema-property-form.svelte to display labels in UI while
  storing values in database
- Add get_enum_value_strings() helper for backward-compatible validation

This completes the three-tier description system:
- Schema level: description field
- Field level: description field
- Enum option level: label field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Rewrite schema-management-implementation-guide.md to reflect simplified architecture:
  - SchemaService deleted (use generic CRUD)
  - SchemaDefinition deleted (use SchemaNode wrapper)
  - Namespace prefix enforcement removed (use is_core flag)
  - EnumValue with labels documented
  - SchemaTableManager for DDL sync documented
- Update node-behavior-system.md property ownership table:
  - Remove namespace prefix examples (custom:, org:, plugin:)
  - Add clarification about is_core flag usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Nov 30, 2025

Copy link
Copy Markdown
Collaborator Author

Code Review Report

PR: #692 - Simplify Schema Architecture: Remove SchemaDefinition and SchemaService
Branch: feature/issue-690-simplify-schema-architecture
Review Type: Initial Review


Requirements Check

From Issue #690:

  • Flatten schema properties to match TaskNode pattern - SchemaNode now uses custom Serialize impl with flat typed fields
  • Delete SchemaDefinition struct - Removed from models/schema.rs
  • Delete into_definition() and as_definition() - Removed from SchemaNode
  • Move validation logic to SchemaNodeBehavior::validate() - Field uniqueness, name validation, enum value checks implemented
  • Create SchemaTableManager for DDL generation - generate_ddl_statements() supports atomic transactions
  • Implement atomic schema updates - SurrealStore.update_schema_node_atomic() wraps node + DDL in single transaction
  • Delete SchemaService entirely - File removed (2829 lines deleted)
  • Delete schema-specific MCP handlers - Removed 5 handlers, tool count reduced from 24 to 19
  • Update all imports and references - Comprehensive updates across codebase
  • Delete obsolete tests - Schema-specific handler tests removed
  • Add new tests for validation in behavior - Validation tests in behaviors module
  • All tests pass - 468 Rust tests + 5 NLP tests passing

Code Review Findings

Architectural Assessment

This PR represents a significant architectural improvement. The changes correctly follow the "everything is a node" principle by eliminating special-case handling for schema nodes. The refactoring achieves:

  1. Consistency: SchemaNode now follows the same pattern as TaskNode with flat typed fields
  2. Simplification: ~3000+ lines of code removed by deleting SchemaService and related handlers
  3. Single Responsibility: Validation in behavior, DDL in SchemaTableManager, CRUD via NodeService

[+] Improvement: Well-Structured Serialization

/Users/malibio/nodespace/nodespace-core-dev3/packages/core/src/models/schema_node.rs:61-96

The custom Serialize implementation for SchemaNode is well-designed. It correctly outputs a flat structure with typed fields (isCore, schemaVersion, description, fields) matching the TaskNode pattern. This eliminates the need for frontend code to dig into nested properties.

struct SchemaNodeSerialized<'a> {
    id: &'a str,
    node_type: &'a str,
    content: &'a str,
    // ...
    is_core: bool,
    schema_version: u32,
    description: String,
    fields: Vec<SchemaField>,
}

Engineering Principle: Separation of storage format from API contract. The internal storage can evolve independently from the serialization format.


[+] Improvement: Comprehensive Validation in SchemaNodeBehavior

/Users/malibio/nodespace/nodespace-core-dev3/packages/core/src/behaviors/mod.rs:822-860

The validate_schema_field function provides thorough recursive validation:

  • Field name character validation (alphanumeric + underscores)
  • Enum fields must have at least one value
  • Recursive validation for nested fields and item_fields

This correctly consolidates validation that was scattered across SchemaService into the behavior layer.

Engineering Principle: Single source of truth for validation logic.


[Nit] Suggestion: Consider Extracting Helper Method

/Users/malibio/nodespace/nodespace-core-dev3/packages/core/src/models/schema.rs:153

The get_enum_value_strings helper method is useful but could benefit from documentation explaining when to use it vs accessing EnumValue directly. Currently it's used for backward-compatible validation.

Recommendation: Add a doc comment explaining the use case.


[+] Improvement: EnumValue Struct with Labels

/Users/malibio/nodespace/nodespace-core-dev3/packages/core/src/models/schema.rs:47-58

The new EnumValue struct with value and label fields is a good addition:

pub struct EnumValue {
    pub value: String,
    pub label: String,
}

This enables human-readable display labels for enum options in UI/MCP clients while maintaining clean database values. The to_title_case() helper for auto-generating labels from values is practical.

Engineering Principle: Domain modeling - separating stored value from display representation.


[+] Improvement: Atomic Schema Updates

/Users/malibio/nodespace/nodespace-core-dev3/packages/core/src/services/schema_table_manager.rs:75-139

The generate_ddl_statements method correctly produces DDL statements without executing them, enabling atomic transactions:

pub fn generate_ddl_statements(
    &self,
    type_name: &str,
    fields: &[SchemaField],
) -> Result<Vec<String>, NodeServiceError>

This is used by SurrealStore.update_schema_node_atomic() to wrap node updates and DDL changes in a single transaction, eliminating partial failure states that existed in the old SchemaService approach.

Engineering Principle: ACID compliance - ensuring atomicity for schema changes.


[Nit] Documentation Cleanup

/Users/malibio/nodespace/nodespace-core-dev3/docs/architecture/development/schema-management-implementation-guide.md

The documentation was significantly reduced from ~2455 lines to ~370 lines. While this reflects the simplified architecture, some useful reference material about the lazy migration system and protection levels may be worth preserving in a separate document or archived section. The current document is appropriately focused on the new simplified approach.


[+] Improvement: Clean Frontend Type Updates

/Users/malibio/nodespace/nodespace-core-dev3/packages/desktop-app/src/lib/types/schema-node.ts

The new TypeScript SchemaNode interface correctly mirrors the Rust serialization output with typed top-level fields. The type guard isSchemaNode() provides safe runtime checking.


[+] Improvement: Test Coverage

The PR maintains strong test coverage:

  • 468 Rust tests passing (core package)
  • 5 NLP engine tests passing
  • Removed obsolete schema handler tests that no longer apply
  • Added validation tests in SchemaNodeBehavior

Summary

  • Critical issues: 0
  • Important issues: 0
  • Suggestions/Nits: 2

Recommendation

APPROVE

This PR represents a significant and well-executed architectural simplification. The changes:

  1. Reduce complexity by eliminating ~3000+ lines of special-case code
  2. Improve consistency by aligning SchemaNode with the TaskNode pattern
  3. Enhance reliability with atomic schema updates via transactions
  4. Maintain test coverage with all 473 tests passing

The implementation correctly follows the project's "everything is a node" principle and eliminates unnecessary abstraction layers. The decision to remove namespace prefix enforcement in favor of the simpler is_core flag is pragmatic given the pre-release state of the project.

The two nits identified are minor documentation suggestions and do not block merge.

Enhanced doc comment for get_enum_value_strings() method to clarify:
- When to use it (validation) vs get_enum_values() (UI display)
- Added example showing expected return format

Addresses reviewer suggestion from PR #692 review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Nov 30, 2025

Copy link
Copy Markdown
Collaborator Author

Review Recommendations Addressed

Summary

  • ✅ Addressed: 1 recommendation
  • ⏭️ Skipped: 1 recommendation

Addressed

🟢 Suggestion: Add doc comment to get_enum_value_strings

  • Enhanced documentation for get_enum_value_strings() method
  • Added guidance on when to use it vs get_enum_values()
  • Added example showing expected return format
  • Commit: 9e49c90

Skipped (with justification)

🟢 Suggestion: Preserve documentation about lazy migration/protection levels

  • Reason: The reviewer acknowledged the current docs are "appropriately focused on the new simplified approach"
  • The old documentation described systems that no longer exist (SchemaService, complex protection levels)
  • Preserving obsolete documentation would create confusion and maintenance burden
  • The simplified architecture docs accurately reflect the current system

Quality Checks

  • ✅ Rust tests: 18 schema_node tests passing
  • ✅ Clippy: No warnings
  • ✅ Format: Clean
  • ✅ Pre-commit hooks: All passed

Resolved conflicts between PR #692 (schema simplification) and PR #685
(strongly-typed node structs):

Architecture decisions (per user requirements):
- Keep flat struct pattern for direct spoke table deserialization (performance)
- Single-query pattern (one DB call, not two)
- EnumValue struct with labels from PR #692
- Comprehensive schema validation in SchemaNodeBehavior

Conflict resolutions:
- schema_node.rs: Flat struct with direct deserialization + EnumValue support
- behaviors/mod.rs: Combined validation from both PRs
- surreal_store.rs: Kept single-query pattern, removed duplicate two-query functions
- node_service.rs: Documentation cleanup
- task_node.rs: Fixed imports, removed conflicting custom Serialize impl
- MCP handlers: Fixed imports, removed SchemaService
- Tauri commands: Fixed imports
- dev-proxy.rs: Fixed models import

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio
malibio merged commit ad0d0aa into main Nov 30, 2025
@malibio
malibio deleted the feature/issue-690-simplify-schema-architecture branch November 30, 2025 23:42
malibio added a commit that referenced this pull request Feb 4, 2026
…ce (#692)

* Simplify schema architecture: Move validation to behavior, extract DDL manager

## Changes

### SchemaNodeBehavior::validate() Enhanced (behaviors/mod.rs)
- Added comprehensive schema validation that was previously scattered in SchemaService
- Validates field name uniqueness across schema
- Enforces namespace prefix rules:
  - Core fields must NOT have namespace prefix (e.g., 'status', 'priority')
  - User/System fields MUST have namespace prefix (custom:, org:, plugin:)
- Validates enum fields have at least one value defined
- Recursive validation of nested fields and item_fields

### SchemaTableManager Created (services/schema_table_manager.rs)
- New dedicated struct for DDL (Data Definition Language) operations
- Extracted from SchemaService: sync_schema_to_database(), define_field(),
  map_field_type(), create_field_index()
- Takes Arc<SurrealStore> directly instead of NodeService dependency
- Cleaner separation: DDL operations vs schema CRUD operations

### Test Updates
- Updated schema_service.rs tests to use namespace prefixes for User fields
- Updated MCP handler tests to comply with new validation rules
- Added 6 new tests for SchemaNodeBehavior validation:
  - test_schema_node_validates_field_uniqueness
  - test_schema_node_user_field_requires_namespace
  - test_schema_node_core_field_rejects_namespace
  - test_schema_node_enum_requires_values
  - test_schema_node_valid_schema_passes
  - test_schema_node_nested_field_validation

### Known Issue Documented
- Reference fields with namespace prefixes cause invalid relation table names
  (e.g., expense_custom:field instead of expense_custom_field)
- Noted in test with suggested fix for process_fields()

## Follow-up Work
- Delete SchemaService after migrating remaining callers to use:
  - Generic node CRUD for schema operations
  - SchemaTableManager for DDL operations
- Update MCP handlers to remove schema-specific handlers

Closes #690

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix requirement - simplify to is_core distinction

The namespace prefix requirement (custom:, org:, plugin:) was unnecessary
complexity. The existing is_core/protection model already distinguishes:

- Core schemas (is_core: true) vs user schemas (is_core: false)
- Core fields (protection: core) vs user fields (protection: user)
- Core enum values (core_values) vs user values (user_values)

Changes:
- Remove namespace validation from SchemaNodeBehavior::validate()
- Remove namespace validation from SchemaService::add_field()
- Field names now only allow alphanumeric + underscores (no colons)
- Remove namespace-related tests (9 tests deleted)
- Update remaining tests to use simple field names
- Update MCP handler documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 1-2 complete

## Completed in This Session
- [x] Phase 1: Enhanced SchemaNodeBehavior::validate() with schema validation
  - Field name uniqueness validation
  - Enum fields must have values (core_values or user_values)
  - Recursive validation of nested fields and item_fields
  - Removed unnecessary namespace prefix requirement
- [x] Phase 2: Created SchemaTableManager for DDL generation
  - Extracted sync_schema_to_database(), define_field(), map_field_type(), create_field_index()
  - Takes Arc<SurrealStore> directly instead of NodeService
- [x] Simplified protection model: is_core + protection field + core_values/user_values
- [x] All tests updated and passing (507 passed)

## Remaining Work
- [ ] Phase 3: Wire up SchemaTableManager to replace DDL methods in SchemaService
- [ ] Phase 4: Remove schema-specific MCP handlers (add_schema_field, remove_schema_field, etc.)
  - Clients should use generic update_node for schema modifications
- [ ] Phase 5: Delete SchemaService entirely
- [ ] Phase 6: Update McpServices struct and all imports/references

## Current State
- Files modified:
  - packages/core/src/behaviors/mod.rs - Enhanced validation
  - packages/core/src/services/schema_table_manager.rs - NEW file with DDL logic
  - packages/core/src/services/schema_service.rs - Namespace validation removed
  - packages/core/src/mcp/handlers/schema.rs - Namespace docs removed
  - packages/core/src/services/mod.rs - Exports SchemaTableManager
- Tests status: 507 passed, 0 failed
- Known issues: SchemaTableManager created but not yet wired up (duplicates code in SchemaService)

## Context for Next Session
Phases 1-2 established the foundation. The remaining work is to:
1. Have SchemaService delegate DDL operations to SchemaTableManager
2. Decide if schema-specific MCP handlers are still needed or if generic CRUD suffices
3. Once SchemaService only wraps NodeService calls, delete it entirely

The key insight: schemas are just nodes with node_type="schema". The is_core/protection
model already distinguishes core vs user schemas/fields/values. No special service needed.

## Acceptance Criteria Status
From issue #690:
- [x] Move validation logic to SchemaNodeBehavior::validate()
- [x] Extract DDL generation to SchemaTableManager
- [ ] Delete SchemaService (depends on phases 3-5)
- [ ] Update MCP handlers to use generic CRUD

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove schema-specific MCP handlers - use generic CRUD (#690)

## Summary
- Removed 5 schema-specific MCP handlers: add_schema_field, remove_schema_field,
  extend_schema_enum, remove_schema_enum_value, get_schema_definition
- Schema nodes now managed via generic CRUD (create_node, update_node, query_nodes)
- SchemaNodeBehavior::validate() provides hardcoded validation
- SchemaService kept for internal use (atomic DDL operations)

## Changes
- Deleted mcp/handlers/schema.rs
- Removed SchemaService from McpServices struct
- Updated handle_update_node/handle_update_nodes_batch to not need SchemaService
- Updated tools.rs routing and tool definitions (19 tools, down from 24)
- Updated desktop-app MCP integration
- Updated all affected tests

## Migration
AI clients should use:
- query_nodes({ filters: [{ field: "node_type", op: "eq", value: "schema" }] }) to list schemas
- update_node on schema nodes to modify them
- create_entity_schema_from_description for natural language schema creation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Implement atomic schema updates with DDL sync (Issue #690)

## What Changed

When a schema node is updated via the generic CRUD API (update_node), both
the node data AND the SurrealDB table definitions now change atomically in
a single transaction. This ensures MCP clients don't need to manage database
internals - updating a schema node automatically syncs DDL changes.

## Implementation

1. **SchemaTableManager.generate_ddl_statements()** - New method that produces
   DDL statements (DEFINE TABLE, DEFINE FIELD, DEFINE INDEX) without executing
   them, enabling them to be included in an atomic transaction

2. **SurrealStore.update_schema_node_atomic()** - New method that wraps:
   - Node table update (properties with schema definition)
   - Spoke table update (schema:<id> where properties are read from)
   - Data link update
   - All DDL statements
   ...in a single BEGIN TRANSACTION / COMMIT TRANSACTION block

3. **NodeService.update_node()** - When node_type == "schema":
   - Parse SchemaDefinition from properties
   - Generate DDL statements via SchemaTableManager
   - Use atomic update path via SurrealStore

## Why This Matters

Previously, SchemaService's add_field/remove_field etc. would:
1. update_schema() - saves node (commit #1)
2. sync_schema_to_database() - runs DDL (commit #2+)

These were NOT atomic. Now with generic CRUD, schema updates happen in
a single transaction, eliminating partial failure states.

## Files Changed

- packages/core/src/services/schema_table_manager.rs - Added generate_ddl_statements()
- packages/core/src/db/surreal_store.rs - Added update_schema_node_atomic()
- packages/core/src/services/node_service.rs - Added atomic path for schema nodes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 3-5 complete, new requirements added

## Completed in This Session

- [x] Phase 4: Created SchemaTableManager with generate_ddl_statements()
  - Generates DEFINE TABLE, DEFINE FIELD, DEFINE INDEX statements
  - Does not execute - returns statements for atomic transactions
  - Located at: packages/core/src/services/schema_table_manager.rs

- [x] Phase 5: Implemented atomic schema updates
  - Added SurrealStore.update_schema_node_atomic() method
  - Wraps node update + spoke table update + DDL in single BEGIN/COMMIT TRANSACTION
  - Modified NodeService.update_node() to detect schema nodes and use atomic path
  - Fixed critical bug: must update BOTH hub table AND spoke table (schema:<id>)

- [x] Phase 7 (partial): Removed schema-specific MCP handlers
  - Deleted: add_schema_field, remove_schema_field, extend_schema_enum, remove_schema_enum_value, get_schema_definition
  - Updated tool count from 24 to 19
  - Deleted: packages/core/src/mcp/handlers/schema.rs
  - Removed SchemaService from McpServices struct

## Remaining Work

- [ ] Phase 1 (NEW): Flatten schema properties to match TaskNode pattern
  - Currently: SchemaDefinition is nested inside properties JSON
  - Target: is_core, version, description, fields stored flat in spoke table
  - This ensures consistency with how TaskNode stores status, priority, etc.
  - Files affected: get_node(), update_schema_node_atomic(), SchemaDefinition usage

- [ ] Phase 2: Delete SchemaDefinition struct
  - After flattening, SchemaDefinition becomes redundant
  - Replace with direct field access on spoke table row
  - Delete into_definition() and as_definition() from SchemaNode

- [ ] Phase 3: Move validation to SchemaNodeBehavior
  - Field uniqueness validation
  - Namespace prefix enforcement
  - Protection level checks
  - Enum validation

- [ ] Phase 6: Delete SchemaService entirely
  - After above phases, SchemaService has no purpose
  - All functionality handled by NodeService + SchemaTableManager + Behavior

- [ ] Phase 8: Update tests
  - Delete obsolete SchemaService tests
  - Add tests for flat schema structure
  - Add tests for generic CRUD with schema nodes

## Current State

- Files modified in this session:
  - packages/core/src/services/schema_table_manager.rs (extended with generate_ddl_statements)
  - packages/core/src/db/surreal_store.rs (added update_schema_node_atomic)
  - packages/core/src/services/node_service.rs (atomic path for schema nodes)

- Tests status: All 88 schema tests passing, 495+ total tests passing
- Known issues: None - atomic updates working correctly

## Context for Next Session

The atomic schema update infrastructure is complete and working. The critical insight
was that schema nodes use hub-and-spoke architecture - properties are read from the
spoke table (schema:<id>), so atomic updates must update BOTH the hub AND spoke tables.

Next priority is Phase 1 (NEW): Flatten schema properties to match TaskNode pattern.
This is a consistency improvement - TaskNode stores status/priority/etc flat in the
spoke table, but SchemaNode currently wraps is_core/version/fields inside a nested
SchemaDefinition in properties. After flattening, SchemaDefinition becomes redundant
and can be deleted (Phase 2).

## Acceptance Criteria Status
From issue #690:
- [ ] Flatten schema properties to match TaskNode pattern (NEW)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates (node + DDL in one transaction)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass (cargo test --lib)
- [x] Quality checks pass (bun run quality:fix)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Complete schema architecture simplification (Issue #690)

## Summary
- Created strongly-typed SchemaNode struct matching TaskNode pattern (Rust)
- Added get_task_node() and get_schema_node() to SurrealStore and NodeService
- Backend returns Node directly, frontend uses SchemaNode type wrapper
- Deleted redundant schema-service.ts (use backendAdapter directly)
- Deleted SchemaDefinition type (replaced by SchemaNode)

## Backend Changes
- Created packages/core/src/models/schema_node.rs with typed accessors
- Added get_task_node/get_schema_node to SurrealStore (spoke table queries)
- Added typed retrieval methods to NodeService
- Updated Tauri commands to use SchemaNode.into_node()
- Updated dev-proxy to return Node from schema endpoints
- Removed schema mutation endpoints (use generic node CRUD)

## Frontend Changes
- Created src/lib/types/schema-node.ts with TypeScript SchemaNode type
- Updated backendAdapter to return Node[] / Node for schema methods
- Updated schema-plugin-loader.ts to use backendAdapter + SchemaNode
- Updated custom-entity-node.svelte to use backendAdapter + SchemaNode
- Updated schema-property-form.svelte to use backendAdapter + SchemaNode
- Removed schema-service.ts and schema.ts (redundant)
- Updated tests to mock backendAdapter instead of schemaService

## Test Results
- Rust: 130 passed
- Frontend: 1440 passed (removed 21 schema-service tests)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Add typed serialization for SchemaNode and TaskNode (Issue #690)

## Completed in This Session
- [x] Added custom Serialize impl to SchemaNode - outputs flat typed fields (isCore, schemaVersion, description, fields)
- [x] Added custom Serialize impl to TaskNode - outputs flat typed fields (status, priority, dueDate, assigneeId)
- [x] Updated Tauri commands to return SchemaNode directly (not Node)
- [x] Updated dev-proxy endpoints to return SchemaNode directly
- [x] Rewrote frontend SchemaNode type to match serialized structure (typed top-level fields)
- [x] Updated backend-adapter to return SchemaNode[] / SchemaNode
- [x] Removed redundant frontend helper functions (getSchemaDescription, getSchemaVersion, etc.)
- [x] Updated schema-plugin-loader.ts to access typed fields directly
- [x] Updated custom-entity-node.svelte to access typed fields directly
- [x] Updated schema-property-form.svelte to access typed fields directly
- [x] Updated test files to use new flat SchemaNode structure
- [x] All tests pass: 1440 frontend + 130 Rust

## Remaining Work (Backend Cleanup)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [ ] Delete SchemaService entirely (if not already done)
- [ ] Update all backend imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Run quality:fix and create PR

## Current State
- Files modified: 14 files (see git diff)
- Tests status: All passing (1440 frontend, 130 Rust)
- Known issues: SchemaDefinition still exists in backend, needs cleanup
- Dependencies: None

## Context for Next Session
This session focused on the serialization layer - making Tauri/HTTP return
typed SchemaNode with flat fields instead of Node with nested properties.
The frontend now accesses node.isCore, node.fields directly.

Next session should focus on backend cleanup: delete SchemaDefinition struct,
remove conversion methods, and ensure SchemaNode is the single source of truth.
The serialization work is complete and working.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (via Serialize impl)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers (already done)
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [x] All tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Delete SchemaDefinition struct, rename ProtectionLevel (Issue #690)

## Completed in This Session
- [x] Deleted SchemaDefinition struct from models/schema.rs
- [x] Deleted FieldDefinition struct (was unused)
- [x] Renamed ProtectionLevel to SchemaProtectionLevel for consistency
- [x] Deleted as_definition() method from SchemaNode
- [x] Added get_enum_values(), can_delete_field(), can_modify_field() to SchemaNode
- [x] Updated SchemaTableManager to accept &[SchemaField] instead of &SchemaDefinition
- [x] Updated node_service.rs to parse fields directly from JSON
- [x] Updated natural_language_schema.rs output structure
- [x] Updated surreal_store.rs test code
- [x] Updated models/mod.rs exports
- [x] All 468 Rust tests pass
- [x] Quality checks pass

## Remaining Work
- [ ] Move validation logic to SchemaNodeBehavior::validate()
  - Field uniqueness check
  - Namespace prefix enforcement for user fields
  - Protection level checks
- [ ] Add tests for validation in SchemaNodeBehavior
- [ ] Add tests for schema CRUD via generic handlers

## Current State
- Files modified: 7 files, -142 lines net
- Tests status: All 468 passing
- Known issues: None - validation still works, just in node_service instead of behavior

## Context for Next Session
The SchemaDefinition struct is now deleted. SchemaNode is the primary type-safe
wrapper with helper methods. Services now parse 'fields' directly from JSON.
The remaining work is moving validation from node_service to SchemaNodeBehavior
for consistency with other node types. This is a refactor, not a functional change.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (done in previous WIP)
- [x] Delete SchemaDefinition struct from models/schema.rs
- [x] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [x] Delete SchemaService entirely (already done)
- [x] Delete schema-specific MCP handlers (already done)
- [x] Update all imports and references
- [x] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add namespace prefix enforcement for user schema fields (Issue #690, #400)

## Changes

1. **Namespace prefix enforcement for user-defined schemas**
   - User protection fields in non-core schemas MUST use namespace prefix
   - Valid prefixes: 'custom:', 'org:', 'plugin:'
   - Core/System protection fields cannot use namespace prefix
   - Core schemas (isCore: true) can have user-protection fields without prefix

2. **Updated validation in SchemaNodeBehavior::validate()**
   - Moved namespace enforcement logic from (deleted) SchemaService
   - Field validation now checks is_core schema flag
   - Recursive validation for nested and item fields

3. **Comprehensive test coverage**
   - test_user_field_requires_namespace_prefix - validates all three prefixes
   - test_core_field_cannot_have_namespace_prefix - rejects prefixed core fields
   - test_system_field_cannot_have_namespace_prefix - rejects prefixed system fields
   - test_nested_user_field_requires_namespace_prefix - validates nested fields
   - test_core_schema_user_field_no_prefix_needed - validates core schema pattern

4. **Fixed doctest for SchemaProtectionLevel rename**
   - Updated import in schema_node.rs example

## Acceptance Criteria Status (Issue #690)
- [x] Flatten schema properties to match TaskNode pattern
- [x] Delete SchemaDefinition struct
- [x] Delete into_definition() and as_definition() from SchemaNode
- [x] Move validation logic to SchemaNodeBehavior::validate()
  - [x] Field uniqueness check
  - [x] Namespace prefix enforcement for user fields
  - [x] Protection level checks
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates
- [x] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [x] Update all imports and references
- [x] Delete obsolete tests
- [x] Add new tests for validation in behavior
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix enforcement - simplify to is_core distinction (Issue #690)

The `is_core` flag already provides sufficient distinction between system
and user-defined schemas/fields. Namespace prefixes add unnecessary complexity.

## Changes
- Remove VALID_USER_PREFIXES constant
- Remove namespace prefix validation from validate_schema_field()
- Remove is_core_schema parameter (no longer needed)
- Revert field names in tests to simple names (no custom:/org:/plugin: prefix)
- Delete namespace prefix enforcement tests

## Protection Level Purpose (clarified)
- `core` - Fields the UI/system depends on, cannot be modified/deleted
- `user` - Fields users can freely modify/delete
- `system` - Auto-managed internal fields, read-only

Users can add custom fields with `protection: "user"` to ANY schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add EnumValue struct with human-readable labels for enum options (Issue #690)

- Add EnumValue { value, label } struct to schema.rs for storing
  both the database value and display label for enum options
- Update SchemaField coreValues/userValues from Vec<String> to Vec<EnumValue>
- Update default task schema status field with proper labels
  (Open, In Progress, Done, Cancelled)
- Add to_title_case() helper for auto-generating labels from values
- Update TypeScript EnumValue interface and SchemaField types
- Update schema-property-form.svelte to display labels in UI while
  storing values in database
- Add get_enum_value_strings() helper for backward-compatible validation

This completes the three-tier description system:
- Schema level: description field
- Field level: description field
- Enum option level: label field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Update architecture docs for Issue #690 schema simplification

- Rewrite schema-management-implementation-guide.md to reflect simplified architecture:
  - SchemaService deleted (use generic CRUD)
  - SchemaDefinition deleted (use SchemaNode wrapper)
  - Namespace prefix enforcement removed (use is_core flag)
  - EnumValue with labels documented
  - SchemaTableManager for DDL sync documented
- Update node-behavior-system.md property ownership table:
  - Remove namespace prefix examples (custom:, org:, plugin:)
  - Add clarification about is_core flag usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address review: Add documentation for get_enum_value_strings helper

Enhanced doc comment for get_enum_value_strings() method to clarify:
- When to use it (validation) vs get_enum_values() (UI display)
- Added example showing expected return format

Addresses reviewer suggestion from PR #692 review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 5, 2026
…ce (#692)

* Simplify schema architecture: Move validation to behavior, extract DDL manager

## Changes

### SchemaNodeBehavior::validate() Enhanced (behaviors/mod.rs)
- Added comprehensive schema validation that was previously scattered in SchemaService
- Validates field name uniqueness across schema
- Enforces namespace prefix rules:
  - Core fields must NOT have namespace prefix (e.g., 'status', 'priority')
  - User/System fields MUST have namespace prefix (custom:, org:, plugin:)
- Validates enum fields have at least one value defined
- Recursive validation of nested fields and item_fields

### SchemaTableManager Created (services/schema_table_manager.rs)
- New dedicated struct for DDL (Data Definition Language) operations
- Extracted from SchemaService: sync_schema_to_database(), define_field(),
  map_field_type(), create_field_index()
- Takes Arc<SurrealStore> directly instead of NodeService dependency
- Cleaner separation: DDL operations vs schema CRUD operations

### Test Updates
- Updated schema_service.rs tests to use namespace prefixes for User fields
- Updated MCP handler tests to comply with new validation rules
- Added 6 new tests for SchemaNodeBehavior validation:
  - test_schema_node_validates_field_uniqueness
  - test_schema_node_user_field_requires_namespace
  - test_schema_node_core_field_rejects_namespace
  - test_schema_node_enum_requires_values
  - test_schema_node_valid_schema_passes
  - test_schema_node_nested_field_validation

### Known Issue Documented
- Reference fields with namespace prefixes cause invalid relation table names
  (e.g., expense_custom:field instead of expense_custom_field)
- Noted in test with suggested fix for process_fields()

## Follow-up Work
- Delete SchemaService after migrating remaining callers to use:
  - Generic node CRUD for schema operations
  - SchemaTableManager for DDL operations
- Update MCP handlers to remove schema-specific handlers

Closes #690

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix requirement - simplify to is_core distinction

The namespace prefix requirement (custom:, org:, plugin:) was unnecessary
complexity. The existing is_core/protection model already distinguishes:

- Core schemas (is_core: true) vs user schemas (is_core: false)
- Core fields (protection: core) vs user fields (protection: user)
- Core enum values (core_values) vs user values (user_values)

Changes:
- Remove namespace validation from SchemaNodeBehavior::validate()
- Remove namespace validation from SchemaService::add_field()
- Field names now only allow alphanumeric + underscores (no colons)
- Remove namespace-related tests (9 tests deleted)
- Update remaining tests to use simple field names
- Update MCP handler documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 1-2 complete

## Completed in This Session
- [x] Phase 1: Enhanced SchemaNodeBehavior::validate() with schema validation
  - Field name uniqueness validation
  - Enum fields must have values (core_values or user_values)
  - Recursive validation of nested fields and item_fields
  - Removed unnecessary namespace prefix requirement
- [x] Phase 2: Created SchemaTableManager for DDL generation
  - Extracted sync_schema_to_database(), define_field(), map_field_type(), create_field_index()
  - Takes Arc<SurrealStore> directly instead of NodeService
- [x] Simplified protection model: is_core + protection field + core_values/user_values
- [x] All tests updated and passing (507 passed)

## Remaining Work
- [ ] Phase 3: Wire up SchemaTableManager to replace DDL methods in SchemaService
- [ ] Phase 4: Remove schema-specific MCP handlers (add_schema_field, remove_schema_field, etc.)
  - Clients should use generic update_node for schema modifications
- [ ] Phase 5: Delete SchemaService entirely
- [ ] Phase 6: Update McpServices struct and all imports/references

## Current State
- Files modified:
  - packages/core/src/behaviors/mod.rs - Enhanced validation
  - packages/core/src/services/schema_table_manager.rs - NEW file with DDL logic
  - packages/core/src/services/schema_service.rs - Namespace validation removed
  - packages/core/src/mcp/handlers/schema.rs - Namespace docs removed
  - packages/core/src/services/mod.rs - Exports SchemaTableManager
- Tests status: 507 passed, 0 failed
- Known issues: SchemaTableManager created but not yet wired up (duplicates code in SchemaService)

## Context for Next Session
Phases 1-2 established the foundation. The remaining work is to:
1. Have SchemaService delegate DDL operations to SchemaTableManager
2. Decide if schema-specific MCP handlers are still needed or if generic CRUD suffices
3. Once SchemaService only wraps NodeService calls, delete it entirely

The key insight: schemas are just nodes with node_type="schema". The is_core/protection
model already distinguishes core vs user schemas/fields/values. No special service needed.

## Acceptance Criteria Status
From issue #690:
- [x] Move validation logic to SchemaNodeBehavior::validate()
- [x] Extract DDL generation to SchemaTableManager
- [ ] Delete SchemaService (depends on phases 3-5)
- [ ] Update MCP handlers to use generic CRUD

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove schema-specific MCP handlers - use generic CRUD (#690)

## Summary
- Removed 5 schema-specific MCP handlers: add_schema_field, remove_schema_field,
  extend_schema_enum, remove_schema_enum_value, get_schema_definition
- Schema nodes now managed via generic CRUD (create_node, update_node, query_nodes)
- SchemaNodeBehavior::validate() provides hardcoded validation
- SchemaService kept for internal use (atomic DDL operations)

## Changes
- Deleted mcp/handlers/schema.rs
- Removed SchemaService from McpServices struct
- Updated handle_update_node/handle_update_nodes_batch to not need SchemaService
- Updated tools.rs routing and tool definitions (19 tools, down from 24)
- Updated desktop-app MCP integration
- Updated all affected tests

## Migration
AI clients should use:
- query_nodes({ filters: [{ field: "node_type", op: "eq", value: "schema" }] }) to list schemas
- update_node on schema nodes to modify them
- create_entity_schema_from_description for natural language schema creation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Implement atomic schema updates with DDL sync (Issue #690)

## What Changed

When a schema node is updated via the generic CRUD API (update_node), both
the node data AND the SurrealDB table definitions now change atomically in
a single transaction. This ensures MCP clients don't need to manage database
internals - updating a schema node automatically syncs DDL changes.

## Implementation

1. **SchemaTableManager.generate_ddl_statements()** - New method that produces
   DDL statements (DEFINE TABLE, DEFINE FIELD, DEFINE INDEX) without executing
   them, enabling them to be included in an atomic transaction

2. **SurrealStore.update_schema_node_atomic()** - New method that wraps:
   - Node table update (properties with schema definition)
   - Spoke table update (schema:<id> where properties are read from)
   - Data link update
   - All DDL statements
   ...in a single BEGIN TRANSACTION / COMMIT TRANSACTION block

3. **NodeService.update_node()** - When node_type == "schema":
   - Parse SchemaDefinition from properties
   - Generate DDL statements via SchemaTableManager
   - Use atomic update path via SurrealStore

## Why This Matters

Previously, SchemaService's add_field/remove_field etc. would:
1. update_schema() - saves node (commit #1)
2. sync_schema_to_database() - runs DDL (commit #2+)

These were NOT atomic. Now with generic CRUD, schema updates happen in
a single transaction, eliminating partial failure states.

## Files Changed

- packages/core/src/services/schema_table_manager.rs - Added generate_ddl_statements()
- packages/core/src/db/surreal_store.rs - Added update_schema_node_atomic()
- packages/core/src/services/node_service.rs - Added atomic path for schema nodes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 3-5 complete, new requirements added

## Completed in This Session

- [x] Phase 4: Created SchemaTableManager with generate_ddl_statements()
  - Generates DEFINE TABLE, DEFINE FIELD, DEFINE INDEX statements
  - Does not execute - returns statements for atomic transactions
  - Located at: packages/core/src/services/schema_table_manager.rs

- [x] Phase 5: Implemented atomic schema updates
  - Added SurrealStore.update_schema_node_atomic() method
  - Wraps node update + spoke table update + DDL in single BEGIN/COMMIT TRANSACTION
  - Modified NodeService.update_node() to detect schema nodes and use atomic path
  - Fixed critical bug: must update BOTH hub table AND spoke table (schema:<id>)

- [x] Phase 7 (partial): Removed schema-specific MCP handlers
  - Deleted: add_schema_field, remove_schema_field, extend_schema_enum, remove_schema_enum_value, get_schema_definition
  - Updated tool count from 24 to 19
  - Deleted: packages/core/src/mcp/handlers/schema.rs
  - Removed SchemaService from McpServices struct

## Remaining Work

- [ ] Phase 1 (NEW): Flatten schema properties to match TaskNode pattern
  - Currently: SchemaDefinition is nested inside properties JSON
  - Target: is_core, version, description, fields stored flat in spoke table
  - This ensures consistency with how TaskNode stores status, priority, etc.
  - Files affected: get_node(), update_schema_node_atomic(), SchemaDefinition usage

- [ ] Phase 2: Delete SchemaDefinition struct
  - After flattening, SchemaDefinition becomes redundant
  - Replace with direct field access on spoke table row
  - Delete into_definition() and as_definition() from SchemaNode

- [ ] Phase 3: Move validation to SchemaNodeBehavior
  - Field uniqueness validation
  - Namespace prefix enforcement
  - Protection level checks
  - Enum validation

- [ ] Phase 6: Delete SchemaService entirely
  - After above phases, SchemaService has no purpose
  - All functionality handled by NodeService + SchemaTableManager + Behavior

- [ ] Phase 8: Update tests
  - Delete obsolete SchemaService tests
  - Add tests for flat schema structure
  - Add tests for generic CRUD with schema nodes

## Current State

- Files modified in this session:
  - packages/core/src/services/schema_table_manager.rs (extended with generate_ddl_statements)
  - packages/core/src/db/surreal_store.rs (added update_schema_node_atomic)
  - packages/core/src/services/node_service.rs (atomic path for schema nodes)

- Tests status: All 88 schema tests passing, 495+ total tests passing
- Known issues: None - atomic updates working correctly

## Context for Next Session

The atomic schema update infrastructure is complete and working. The critical insight
was that schema nodes use hub-and-spoke architecture - properties are read from the
spoke table (schema:<id>), so atomic updates must update BOTH the hub AND spoke tables.

Next priority is Phase 1 (NEW): Flatten schema properties to match TaskNode pattern.
This is a consistency improvement - TaskNode stores status/priority/etc flat in the
spoke table, but SchemaNode currently wraps is_core/version/fields inside a nested
SchemaDefinition in properties. After flattening, SchemaDefinition becomes redundant
and can be deleted (Phase 2).

## Acceptance Criteria Status
From issue #690:
- [ ] Flatten schema properties to match TaskNode pattern (NEW)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates (node + DDL in one transaction)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass (cargo test --lib)
- [x] Quality checks pass (bun run quality:fix)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Complete schema architecture simplification (Issue #690)

## Summary
- Created strongly-typed SchemaNode struct matching TaskNode pattern (Rust)
- Added get_task_node() and get_schema_node() to SurrealStore and NodeService
- Backend returns Node directly, frontend uses SchemaNode type wrapper
- Deleted redundant schema-service.ts (use backendAdapter directly)
- Deleted SchemaDefinition type (replaced by SchemaNode)

## Backend Changes
- Created packages/core/src/models/schema_node.rs with typed accessors
- Added get_task_node/get_schema_node to SurrealStore (spoke table queries)
- Added typed retrieval methods to NodeService
- Updated Tauri commands to use SchemaNode.into_node()
- Updated dev-proxy to return Node from schema endpoints
- Removed schema mutation endpoints (use generic node CRUD)

## Frontend Changes
- Created src/lib/types/schema-node.ts with TypeScript SchemaNode type
- Updated backendAdapter to return Node[] / Node for schema methods
- Updated schema-plugin-loader.ts to use backendAdapter + SchemaNode
- Updated custom-entity-node.svelte to use backendAdapter + SchemaNode
- Updated schema-property-form.svelte to use backendAdapter + SchemaNode
- Removed schema-service.ts and schema.ts (redundant)
- Updated tests to mock backendAdapter instead of schemaService

## Test Results
- Rust: 130 passed
- Frontend: 1440 passed (removed 21 schema-service tests)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Add typed serialization for SchemaNode and TaskNode (Issue #690)

## Completed in This Session
- [x] Added custom Serialize impl to SchemaNode - outputs flat typed fields (isCore, schemaVersion, description, fields)
- [x] Added custom Serialize impl to TaskNode - outputs flat typed fields (status, priority, dueDate, assigneeId)
- [x] Updated Tauri commands to return SchemaNode directly (not Node)
- [x] Updated dev-proxy endpoints to return SchemaNode directly
- [x] Rewrote frontend SchemaNode type to match serialized structure (typed top-level fields)
- [x] Updated backend-adapter to return SchemaNode[] / SchemaNode
- [x] Removed redundant frontend helper functions (getSchemaDescription, getSchemaVersion, etc.)
- [x] Updated schema-plugin-loader.ts to access typed fields directly
- [x] Updated custom-entity-node.svelte to access typed fields directly
- [x] Updated schema-property-form.svelte to access typed fields directly
- [x] Updated test files to use new flat SchemaNode structure
- [x] All tests pass: 1440 frontend + 130 Rust

## Remaining Work (Backend Cleanup)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [ ] Delete SchemaService entirely (if not already done)
- [ ] Update all backend imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Run quality:fix and create PR

## Current State
- Files modified: 14 files (see git diff)
- Tests status: All passing (1440 frontend, 130 Rust)
- Known issues: SchemaDefinition still exists in backend, needs cleanup
- Dependencies: None

## Context for Next Session
This session focused on the serialization layer - making Tauri/HTTP return
typed SchemaNode with flat fields instead of Node with nested properties.
The frontend now accesses node.isCore, node.fields directly.

Next session should focus on backend cleanup: delete SchemaDefinition struct,
remove conversion methods, and ensure SchemaNode is the single source of truth.
The serialization work is complete and working.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (via Serialize impl)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers (already done)
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [x] All tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Delete SchemaDefinition struct, rename ProtectionLevel (Issue #690)

## Completed in This Session
- [x] Deleted SchemaDefinition struct from models/schema.rs
- [x] Deleted FieldDefinition struct (was unused)
- [x] Renamed ProtectionLevel to SchemaProtectionLevel for consistency
- [x] Deleted as_definition() method from SchemaNode
- [x] Added get_enum_values(), can_delete_field(), can_modify_field() to SchemaNode
- [x] Updated SchemaTableManager to accept &[SchemaField] instead of &SchemaDefinition
- [x] Updated node_service.rs to parse fields directly from JSON
- [x] Updated natural_language_schema.rs output structure
- [x] Updated surreal_store.rs test code
- [x] Updated models/mod.rs exports
- [x] All 468 Rust tests pass
- [x] Quality checks pass

## Remaining Work
- [ ] Move validation logic to SchemaNodeBehavior::validate()
  - Field uniqueness check
  - Namespace prefix enforcement for user fields
  - Protection level checks
- [ ] Add tests for validation in SchemaNodeBehavior
- [ ] Add tests for schema CRUD via generic handlers

## Current State
- Files modified: 7 files, -142 lines net
- Tests status: All 468 passing
- Known issues: None - validation still works, just in node_service instead of behavior

## Context for Next Session
The SchemaDefinition struct is now deleted. SchemaNode is the primary type-safe
wrapper with helper methods. Services now parse 'fields' directly from JSON.
The remaining work is moving validation from node_service to SchemaNodeBehavior
for consistency with other node types. This is a refactor, not a functional change.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (done in previous WIP)
- [x] Delete SchemaDefinition struct from models/schema.rs
- [x] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [x] Delete SchemaService entirely (already done)
- [x] Delete schema-specific MCP handlers (already done)
- [x] Update all imports and references
- [x] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add namespace prefix enforcement for user schema fields (Issue #690, #400)

## Changes

1. **Namespace prefix enforcement for user-defined schemas**
   - User protection fields in non-core schemas MUST use namespace prefix
   - Valid prefixes: 'custom:', 'org:', 'plugin:'
   - Core/System protection fields cannot use namespace prefix
   - Core schemas (isCore: true) can have user-protection fields without prefix

2. **Updated validation in SchemaNodeBehavior::validate()**
   - Moved namespace enforcement logic from (deleted) SchemaService
   - Field validation now checks is_core schema flag
   - Recursive validation for nested and item fields

3. **Comprehensive test coverage**
   - test_user_field_requires_namespace_prefix - validates all three prefixes
   - test_core_field_cannot_have_namespace_prefix - rejects prefixed core fields
   - test_system_field_cannot_have_namespace_prefix - rejects prefixed system fields
   - test_nested_user_field_requires_namespace_prefix - validates nested fields
   - test_core_schema_user_field_no_prefix_needed - validates core schema pattern

4. **Fixed doctest for SchemaProtectionLevel rename**
   - Updated import in schema_node.rs example

## Acceptance Criteria Status (Issue #690)
- [x] Flatten schema properties to match TaskNode pattern
- [x] Delete SchemaDefinition struct
- [x] Delete into_definition() and as_definition() from SchemaNode
- [x] Move validation logic to SchemaNodeBehavior::validate()
  - [x] Field uniqueness check
  - [x] Namespace prefix enforcement for user fields
  - [x] Protection level checks
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates
- [x] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [x] Update all imports and references
- [x] Delete obsolete tests
- [x] Add new tests for validation in behavior
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix enforcement - simplify to is_core distinction (Issue #690)

The `is_core` flag already provides sufficient distinction between system
and user-defined schemas/fields. Namespace prefixes add unnecessary complexity.

## Changes
- Remove VALID_USER_PREFIXES constant
- Remove namespace prefix validation from validate_schema_field()
- Remove is_core_schema parameter (no longer needed)
- Revert field names in tests to simple names (no custom:/org:/plugin: prefix)
- Delete namespace prefix enforcement tests

## Protection Level Purpose (clarified)
- `core` - Fields the UI/system depends on, cannot be modified/deleted
- `user` - Fields users can freely modify/delete
- `system` - Auto-managed internal fields, read-only

Users can add custom fields with `protection: "user"` to ANY schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add EnumValue struct with human-readable labels for enum options (Issue #690)

- Add EnumValue { value, label } struct to schema.rs for storing
  both the database value and display label for enum options
- Update SchemaField coreValues/userValues from Vec<String> to Vec<EnumValue>
- Update default task schema status field with proper labels
  (Open, In Progress, Done, Cancelled)
- Add to_title_case() helper for auto-generating labels from values
- Update TypeScript EnumValue interface and SchemaField types
- Update schema-property-form.svelte to display labels in UI while
  storing values in database
- Add get_enum_value_strings() helper for backward-compatible validation

This completes the three-tier description system:
- Schema level: description field
- Field level: description field
- Enum option level: label field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Update architecture docs for Issue #690 schema simplification

- Rewrite schema-management-implementation-guide.md to reflect simplified architecture:
  - SchemaService deleted (use generic CRUD)
  - SchemaDefinition deleted (use SchemaNode wrapper)
  - Namespace prefix enforcement removed (use is_core flag)
  - EnumValue with labels documented
  - SchemaTableManager for DDL sync documented
- Update node-behavior-system.md property ownership table:
  - Remove namespace prefix examples (custom:, org:, plugin:)
  - Add clarification about is_core flag usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address review: Add documentation for get_enum_value_strings helper

Enhanced doc comment for get_enum_value_strings() method to clarify:
- When to use it (validation) vs get_enum_values() (UI display)
- Added example showing expected return format

Addresses reviewer suggestion from PR #692 review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 26, 2026
…ce (#692)

* Simplify schema architecture: Move validation to behavior, extract DDL manager

## Changes

### SchemaNodeBehavior::validate() Enhanced (behaviors/mod.rs)
- Added comprehensive schema validation that was previously scattered in SchemaService
- Validates field name uniqueness across schema
- Enforces namespace prefix rules:
  - Core fields must NOT have namespace prefix (e.g., 'status', 'priority')
  - User/System fields MUST have namespace prefix (custom:, org:, plugin:)
- Validates enum fields have at least one value defined
- Recursive validation of nested fields and item_fields

### SchemaTableManager Created (services/schema_table_manager.rs)
- New dedicated struct for DDL (Data Definition Language) operations
- Extracted from SchemaService: sync_schema_to_database(), define_field(),
  map_field_type(), create_field_index()
- Takes Arc<SurrealStore> directly instead of NodeService dependency
- Cleaner separation: DDL operations vs schema CRUD operations

### Test Updates
- Updated schema_service.rs tests to use namespace prefixes for User fields
- Updated MCP handler tests to comply with new validation rules
- Added 6 new tests for SchemaNodeBehavior validation:
  - test_schema_node_validates_field_uniqueness
  - test_schema_node_user_field_requires_namespace
  - test_schema_node_core_field_rejects_namespace
  - test_schema_node_enum_requires_values
  - test_schema_node_valid_schema_passes
  - test_schema_node_nested_field_validation

### Known Issue Documented
- Reference fields with namespace prefixes cause invalid relation table names
  (e.g., expense_custom:field instead of expense_custom_field)
- Noted in test with suggested fix for process_fields()

## Follow-up Work
- Delete SchemaService after migrating remaining callers to use:
  - Generic node CRUD for schema operations
  - SchemaTableManager for DDL operations
- Update MCP handlers to remove schema-specific handlers

Closes #690

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix requirement - simplify to is_core distinction

The namespace prefix requirement (custom:, org:, plugin:) was unnecessary
complexity. The existing is_core/protection model already distinguishes:

- Core schemas (is_core: true) vs user schemas (is_core: false)
- Core fields (protection: core) vs user fields (protection: user)
- Core enum values (core_values) vs user values (user_values)

Changes:
- Remove namespace validation from SchemaNodeBehavior::validate()
- Remove namespace validation from SchemaService::add_field()
- Field names now only allow alphanumeric + underscores (no colons)
- Remove namespace-related tests (9 tests deleted)
- Update remaining tests to use simple field names
- Update MCP handler documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 1-2 complete

## Completed in This Session
- [x] Phase 1: Enhanced SchemaNodeBehavior::validate() with schema validation
  - Field name uniqueness validation
  - Enum fields must have values (core_values or user_values)
  - Recursive validation of nested fields and item_fields
  - Removed unnecessary namespace prefix requirement
- [x] Phase 2: Created SchemaTableManager for DDL generation
  - Extracted sync_schema_to_database(), define_field(), map_field_type(), create_field_index()
  - Takes Arc<SurrealStore> directly instead of NodeService
- [x] Simplified protection model: is_core + protection field + core_values/user_values
- [x] All tests updated and passing (507 passed)

## Remaining Work
- [ ] Phase 3: Wire up SchemaTableManager to replace DDL methods in SchemaService
- [ ] Phase 4: Remove schema-specific MCP handlers (add_schema_field, remove_schema_field, etc.)
  - Clients should use generic update_node for schema modifications
- [ ] Phase 5: Delete SchemaService entirely
- [ ] Phase 6: Update McpServices struct and all imports/references

## Current State
- Files modified:
  - packages/core/src/behaviors/mod.rs - Enhanced validation
  - packages/core/src/services/schema_table_manager.rs - NEW file with DDL logic
  - packages/core/src/services/schema_service.rs - Namespace validation removed
  - packages/core/src/mcp/handlers/schema.rs - Namespace docs removed
  - packages/core/src/services/mod.rs - Exports SchemaTableManager
- Tests status: 507 passed, 0 failed
- Known issues: SchemaTableManager created but not yet wired up (duplicates code in SchemaService)

## Context for Next Session
Phases 1-2 established the foundation. The remaining work is to:
1. Have SchemaService delegate DDL operations to SchemaTableManager
2. Decide if schema-specific MCP handlers are still needed or if generic CRUD suffices
3. Once SchemaService only wraps NodeService calls, delete it entirely

The key insight: schemas are just nodes with node_type="schema". The is_core/protection
model already distinguishes core vs user schemas/fields/values. No special service needed.

## Acceptance Criteria Status
From issue #690:
- [x] Move validation logic to SchemaNodeBehavior::validate()
- [x] Extract DDL generation to SchemaTableManager
- [ ] Delete SchemaService (depends on phases 3-5)
- [ ] Update MCP handlers to use generic CRUD

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove schema-specific MCP handlers - use generic CRUD (#690)

## Summary
- Removed 5 schema-specific MCP handlers: add_schema_field, remove_schema_field,
  extend_schema_enum, remove_schema_enum_value, get_schema_definition
- Schema nodes now managed via generic CRUD (create_node, update_node, query_nodes)
- SchemaNodeBehavior::validate() provides hardcoded validation
- SchemaService kept for internal use (atomic DDL operations)

## Changes
- Deleted mcp/handlers/schema.rs
- Removed SchemaService from McpServices struct
- Updated handle_update_node/handle_update_nodes_batch to not need SchemaService
- Updated tools.rs routing and tool definitions (19 tools, down from 24)
- Updated desktop-app MCP integration
- Updated all affected tests

## Migration
AI clients should use:
- query_nodes({ filters: [{ field: "node_type", op: "eq", value: "schema" }] }) to list schemas
- update_node on schema nodes to modify them
- create_entity_schema_from_description for natural language schema creation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Implement atomic schema updates with DDL sync (Issue #690)

## What Changed

When a schema node is updated via the generic CRUD API (update_node), both
the node data AND the SurrealDB table definitions now change atomically in
a single transaction. This ensures MCP clients don't need to manage database
internals - updating a schema node automatically syncs DDL changes.

## Implementation

1. **SchemaTableManager.generate_ddl_statements()** - New method that produces
   DDL statements (DEFINE TABLE, DEFINE FIELD, DEFINE INDEX) without executing
   them, enabling them to be included in an atomic transaction

2. **SurrealStore.update_schema_node_atomic()** - New method that wraps:
   - Node table update (properties with schema definition)
   - Spoke table update (schema:<id> where properties are read from)
   - Data link update
   - All DDL statements
   ...in a single BEGIN TRANSACTION / COMMIT TRANSACTION block

3. **NodeService.update_node()** - When node_type == "schema":
   - Parse SchemaDefinition from properties
   - Generate DDL statements via SchemaTableManager
   - Use atomic update path via SurrealStore

## Why This Matters

Previously, SchemaService's add_field/remove_field etc. would:
1. update_schema() - saves node (commit #1)
2. sync_schema_to_database() - runs DDL (commit #2+)

These were NOT atomic. Now with generic CRUD, schema updates happen in
a single transaction, eliminating partial failure states.

## Files Changed

- packages/core/src/services/schema_table_manager.rs - Added generate_ddl_statements()
- packages/core/src/db/surreal_store.rs - Added update_schema_node_atomic()
- packages/core/src/services/node_service.rs - Added atomic path for schema nodes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Schema architecture simplification - Phases 3-5 complete, new requirements added

## Completed in This Session

- [x] Phase 4: Created SchemaTableManager with generate_ddl_statements()
  - Generates DEFINE TABLE, DEFINE FIELD, DEFINE INDEX statements
  - Does not execute - returns statements for atomic transactions
  - Located at: packages/core/src/services/schema_table_manager.rs

- [x] Phase 5: Implemented atomic schema updates
  - Added SurrealStore.update_schema_node_atomic() method
  - Wraps node update + spoke table update + DDL in single BEGIN/COMMIT TRANSACTION
  - Modified NodeService.update_node() to detect schema nodes and use atomic path
  - Fixed critical bug: must update BOTH hub table AND spoke table (schema:<id>)

- [x] Phase 7 (partial): Removed schema-specific MCP handlers
  - Deleted: add_schema_field, remove_schema_field, extend_schema_enum, remove_schema_enum_value, get_schema_definition
  - Updated tool count from 24 to 19
  - Deleted: packages/core/src/mcp/handlers/schema.rs
  - Removed SchemaService from McpServices struct

## Remaining Work

- [ ] Phase 1 (NEW): Flatten schema properties to match TaskNode pattern
  - Currently: SchemaDefinition is nested inside properties JSON
  - Target: is_core, version, description, fields stored flat in spoke table
  - This ensures consistency with how TaskNode stores status, priority, etc.
  - Files affected: get_node(), update_schema_node_atomic(), SchemaDefinition usage

- [ ] Phase 2: Delete SchemaDefinition struct
  - After flattening, SchemaDefinition becomes redundant
  - Replace with direct field access on spoke table row
  - Delete into_definition() and as_definition() from SchemaNode

- [ ] Phase 3: Move validation to SchemaNodeBehavior
  - Field uniqueness validation
  - Namespace prefix enforcement
  - Protection level checks
  - Enum validation

- [ ] Phase 6: Delete SchemaService entirely
  - After above phases, SchemaService has no purpose
  - All functionality handled by NodeService + SchemaTableManager + Behavior

- [ ] Phase 8: Update tests
  - Delete obsolete SchemaService tests
  - Add tests for flat schema structure
  - Add tests for generic CRUD with schema nodes

## Current State

- Files modified in this session:
  - packages/core/src/services/schema_table_manager.rs (extended with generate_ddl_statements)
  - packages/core/src/db/surreal_store.rs (added update_schema_node_atomic)
  - packages/core/src/services/node_service.rs (atomic path for schema nodes)

- Tests status: All 88 schema tests passing, 495+ total tests passing
- Known issues: None - atomic updates working correctly

## Context for Next Session

The atomic schema update infrastructure is complete and working. The critical insight
was that schema nodes use hub-and-spoke architecture - properties are read from the
spoke table (schema:<id>), so atomic updates must update BOTH the hub AND spoke tables.

Next priority is Phase 1 (NEW): Flatten schema properties to match TaskNode pattern.
This is a consistency improvement - TaskNode stores status/priority/etc flat in the
spoke table, but SchemaNode currently wraps is_core/version/fields inside a nested
SchemaDefinition in properties. After flattening, SchemaDefinition becomes redundant
and can be deleted (Phase 2).

## Acceptance Criteria Status
From issue #690:
- [ ] Flatten schema properties to match TaskNode pattern (NEW)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates (node + DDL in one transaction)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass (cargo test --lib)
- [x] Quality checks pass (bun run quality:fix)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Complete schema architecture simplification (Issue #690)

## Summary
- Created strongly-typed SchemaNode struct matching TaskNode pattern (Rust)
- Added get_task_node() and get_schema_node() to SurrealStore and NodeService
- Backend returns Node directly, frontend uses SchemaNode type wrapper
- Deleted redundant schema-service.ts (use backendAdapter directly)
- Deleted SchemaDefinition type (replaced by SchemaNode)

## Backend Changes
- Created packages/core/src/models/schema_node.rs with typed accessors
- Added get_task_node/get_schema_node to SurrealStore (spoke table queries)
- Added typed retrieval methods to NodeService
- Updated Tauri commands to use SchemaNode.into_node()
- Updated dev-proxy to return Node from schema endpoints
- Removed schema mutation endpoints (use generic node CRUD)

## Frontend Changes
- Created src/lib/types/schema-node.ts with TypeScript SchemaNode type
- Updated backendAdapter to return Node[] / Node for schema methods
- Updated schema-plugin-loader.ts to use backendAdapter + SchemaNode
- Updated custom-entity-node.svelte to use backendAdapter + SchemaNode
- Updated schema-property-form.svelte to use backendAdapter + SchemaNode
- Removed schema-service.ts and schema.ts (redundant)
- Updated tests to mock backendAdapter instead of schemaService

## Test Results
- Rust: 130 passed
- Frontend: 1440 passed (removed 21 schema-service tests)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Add typed serialization for SchemaNode and TaskNode (Issue #690)

## Completed in This Session
- [x] Added custom Serialize impl to SchemaNode - outputs flat typed fields (isCore, schemaVersion, description, fields)
- [x] Added custom Serialize impl to TaskNode - outputs flat typed fields (status, priority, dueDate, assigneeId)
- [x] Updated Tauri commands to return SchemaNode directly (not Node)
- [x] Updated dev-proxy endpoints to return SchemaNode directly
- [x] Rewrote frontend SchemaNode type to match serialized structure (typed top-level fields)
- [x] Updated backend-adapter to return SchemaNode[] / SchemaNode
- [x] Removed redundant frontend helper functions (getSchemaDescription, getSchemaVersion, etc.)
- [x] Updated schema-plugin-loader.ts to access typed fields directly
- [x] Updated custom-entity-node.svelte to access typed fields directly
- [x] Updated schema-property-form.svelte to access typed fields directly
- [x] Updated test files to use new flat SchemaNode structure
- [x] All tests pass: 1440 frontend + 130 Rust

## Remaining Work (Backend Cleanup)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [ ] Delete SchemaService entirely (if not already done)
- [ ] Update all backend imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Run quality:fix and create PR

## Current State
- Files modified: 14 files (see git diff)
- Tests status: All passing (1440 frontend, 130 Rust)
- Known issues: SchemaDefinition still exists in backend, needs cleanup
- Dependencies: None

## Context for Next Session
This session focused on the serialization layer - making Tauri/HTTP return
typed SchemaNode with flat fields instead of Node with nested properties.
The frontend now accesses node.isCore, node.fields directly.

Next session should focus on backend cleanup: delete SchemaDefinition struct,
remove conversion methods, and ensure SchemaNode is the single source of truth.
The serialization work is complete and working.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (via Serialize impl)
- [ ] Delete SchemaDefinition struct from models/schema.rs
- [ ] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [ ] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers (already done)
- [ ] Update all imports and references
- [ ] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [x] All tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* WIP: Delete SchemaDefinition struct, rename ProtectionLevel (Issue #690)

## Completed in This Session
- [x] Deleted SchemaDefinition struct from models/schema.rs
- [x] Deleted FieldDefinition struct (was unused)
- [x] Renamed ProtectionLevel to SchemaProtectionLevel for consistency
- [x] Deleted as_definition() method from SchemaNode
- [x] Added get_enum_values(), can_delete_field(), can_modify_field() to SchemaNode
- [x] Updated SchemaTableManager to accept &[SchemaField] instead of &SchemaDefinition
- [x] Updated node_service.rs to parse fields directly from JSON
- [x] Updated natural_language_schema.rs output structure
- [x] Updated surreal_store.rs test code
- [x] Updated models/mod.rs exports
- [x] All 468 Rust tests pass
- [x] Quality checks pass

## Remaining Work
- [ ] Move validation logic to SchemaNodeBehavior::validate()
  - Field uniqueness check
  - Namespace prefix enforcement for user fields
  - Protection level checks
- [ ] Add tests for validation in SchemaNodeBehavior
- [ ] Add tests for schema CRUD via generic handlers

## Current State
- Files modified: 7 files, -142 lines net
- Tests status: All 468 passing
- Known issues: None - validation still works, just in node_service instead of behavior

## Context for Next Session
The SchemaDefinition struct is now deleted. SchemaNode is the primary type-safe
wrapper with helper methods. Services now parse 'fields' directly from JSON.
The remaining work is moving validation from node_service to SchemaNodeBehavior
for consistency with other node types. This is a refactor, not a functional change.

## Acceptance Criteria Status
From issue #690:
- [x] Flatten schema properties to match TaskNode pattern (done in previous WIP)
- [x] Delete SchemaDefinition struct from models/schema.rs
- [x] Delete into_definition() and as_definition() from SchemaNode
- [ ] Move validation logic to SchemaNodeBehavior::validate()
- [x] Create SchemaTableManager for DDL generation (already done)
- [x] Implement atomic schema updates (already done)
- [x] Delete SchemaService entirely (already done)
- [x] Delete schema-specific MCP handlers (already done)
- [x] Update all imports and references
- [x] Delete obsolete tests
- [ ] Add new tests for validation in behavior
- [ ] Add new tests for schema CRUD via generic handlers
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add namespace prefix enforcement for user schema fields (Issue #690, #400)

## Changes

1. **Namespace prefix enforcement for user-defined schemas**
   - User protection fields in non-core schemas MUST use namespace prefix
   - Valid prefixes: 'custom:', 'org:', 'plugin:'
   - Core/System protection fields cannot use namespace prefix
   - Core schemas (isCore: true) can have user-protection fields without prefix

2. **Updated validation in SchemaNodeBehavior::validate()**
   - Moved namespace enforcement logic from (deleted) SchemaService
   - Field validation now checks is_core schema flag
   - Recursive validation for nested and item fields

3. **Comprehensive test coverage**
   - test_user_field_requires_namespace_prefix - validates all three prefixes
   - test_core_field_cannot_have_namespace_prefix - rejects prefixed core fields
   - test_system_field_cannot_have_namespace_prefix - rejects prefixed system fields
   - test_nested_user_field_requires_namespace_prefix - validates nested fields
   - test_core_schema_user_field_no_prefix_needed - validates core schema pattern

4. **Fixed doctest for SchemaProtectionLevel rename**
   - Updated import in schema_node.rs example

## Acceptance Criteria Status (Issue #690)
- [x] Flatten schema properties to match TaskNode pattern
- [x] Delete SchemaDefinition struct
- [x] Delete into_definition() and as_definition() from SchemaNode
- [x] Move validation logic to SchemaNodeBehavior::validate()
  - [x] Field uniqueness check
  - [x] Namespace prefix enforcement for user fields
  - [x] Protection level checks
- [x] Create SchemaTableManager for DDL generation
- [x] Implement atomic schema updates
- [x] Delete SchemaService entirely
- [x] Delete schema-specific MCP handlers
- [x] Update all imports and references
- [x] Delete obsolete tests
- [x] Add new tests for validation in behavior
- [x] All tests pass
- [x] Quality checks pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Remove namespace prefix enforcement - simplify to is_core distinction (Issue #690)

The `is_core` flag already provides sufficient distinction between system
and user-defined schemas/fields. Namespace prefixes add unnecessary complexity.

## Changes
- Remove VALID_USER_PREFIXES constant
- Remove namespace prefix validation from validate_schema_field()
- Remove is_core_schema parameter (no longer needed)
- Revert field names in tests to simple names (no custom:/org:/plugin: prefix)
- Delete namespace prefix enforcement tests

## Protection Level Purpose (clarified)
- `core` - Fields the UI/system depends on, cannot be modified/deleted
- `user` - Fields users can freely modify/delete
- `system` - Auto-managed internal fields, read-only

Users can add custom fields with `protection: "user"` to ANY schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Add EnumValue struct with human-readable labels for enum options (Issue #690)

- Add EnumValue { value, label } struct to schema.rs for storing
  both the database value and display label for enum options
- Update SchemaField coreValues/userValues from Vec<String> to Vec<EnumValue>
- Update default task schema status field with proper labels
  (Open, In Progress, Done, Cancelled)
- Add to_title_case() helper for auto-generating labels from values
- Update TypeScript EnumValue interface and SchemaField types
- Update schema-property-form.svelte to display labels in UI while
  storing values in database
- Add get_enum_value_strings() helper for backward-compatible validation

This completes the three-tier description system:
- Schema level: description field
- Field level: description field
- Enum option level: label field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Update architecture docs for Issue #690 schema simplification

- Rewrite schema-management-implementation-guide.md to reflect simplified architecture:
  - SchemaService deleted (use generic CRUD)
  - SchemaDefinition deleted (use SchemaNode wrapper)
  - Namespace prefix enforcement removed (use is_core flag)
  - EnumValue with labels documented
  - SchemaTableManager for DDL sync documented
- Update node-behavior-system.md property ownership table:
  - Remove namespace prefix examples (custom:, org:, plugin:)
  - Add clarification about is_core flag usage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* Address review: Add documentation for get_enum_value_strings helper

Enhanced doc comment for get_enum_value_strings() method to clarify:
- When to use it (validation) vs get_enum_values() (UI display)
- Added example showing expected return format

Addresses reviewer suggestion from PR #692 review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify Schema Architecture: Remove SchemaDefinition and SchemaService

1 participant