Skip to content

Declare and enforce children and parent rules per node type #3447

Closed

Description

@malibio

Overview

Implement ADR-089's declared structural rules. Each type declares what it may have as children and what parent it needs, and the database enforces both for every write path.

Problem Statement

  • "Must be a root" is two hand-written triggers. It exists only for collection and schema, as SQL triggers in packages/core/src/db/schema.rs keyed on literal type names, so a pro-collection escapes it.
  • "Must have a parent" doesn't exist. It's needed for ai-chat-message.
  • can_have_children() isn't enforced. NodeBehavior::can_have_children() (packages/core/src/behaviors/mod.rs) is read only by embedding aggregation, never on a write.
  • canBeChild is never consumed. The frontend's flag (packages/desktop-app/src/lib/plugins/core-plugins.ts) has no reader.

Proposed Solution

  1. Two rules per type, declared in the schema and carried by the registry:
    • children: any (default) · none · any_except [types]
    • parent: any (default) · must_be_root · must_have_parent_of [types]
      A named type covers its subtypes, and a subtype may only tighten its base's rules.
  2. Enforcement:
    • Every has_child write (create with parent, move, bulk insert, seeding) checks both rules against the resolved type chains, using the type-ancestry table, in the same transaction.
    • Retyping re-checks both rules.
    • must_have_parent_of is also checked on the create path, because the node row is written before its edge.
  3. Declarations (ADR-089 §4):
    • collection, schema, date: must_be_root.
    • ai-chat-message: must_have_parent_of [ai-chat-native, ai-chat-acp], children: none.
    • code-block, ordered-list, horizontal-line, table, query, tool, database-settings: children: none.
    • The ai-chat base: children: any. A non-message child of a chat is its own embedding root.
    • Everything else: any / any.
  4. Replace the collection/schema root triggers, can_have_children() (embedding aggregation asks the children rule) and the frontend's canBeChild/canHaveChildren flags.
  5. The UI respects the rules: indent, outdent and drag-and-drop aren't offered where a rule would refuse them.

Architecture Impact

Implements decisions/089-structural-rules-children-and-parent.md (nodespace-docs), which supersedes ADR-059 §2's enforcement mechanism (the rule stands). Uses ADR-086 §5's type-ancestry table.

Acceptance Criteria

  • Schemas declare children and parent, the registry carries them, and a subtype can't relax its base's rules (tests)
  • Every write path (create, move, bulk, seeding, generic relationship create, retype) refuses a violating has_child edge (tests per path)
  • A must_have_parent_of type created without a qualifying parent is refused before anything is written (test)
  • collection, schema and date are root-only, including subtypes of them (tests); the old triggers are removed
  • The leaf types refuse children (tests)
  • A non-message child of a chat is embedded as its own root (test)
  • can_have_children() and the frontend canBeChild/canHaveChildren flags are gone; the UI's indent/outdent/drag follow the rules (frontend tests)
  • Follows development/node-type-sequence.md; the node-types.md structural-rule columns updated in the same PR
  • bun run test:changed passes; bun run quality:fix clean

Technical Specifications

Reference Files

  • packages/core/src/db/schema.rs (root triggers), packages/core/src/db/sqlite_store/relationships.rs
  • packages/core/src/services/node_service/crud.rs, hierarchy.rs, bulk.rs
  • packages/core/src/behaviors/mod.rs (can_have_children, embedding aggregation)
  • packages/core/src/db/sqlite_store/embedding_roots.rs
  • packages/core/src/models/core_schemas.rs, packages/nodespace-types/src/schema.rs
  • packages/desktop-app/src/lib/plugins/core-plugins.ts, packages/desktop-app/src/lib/plugins/types.ts, and the outline indent/outdent handlers in base-node-viewer.svelte

Dependencies

Non-Goals

  • How the chat viewer shows non-message children (UI decision)

Activity

  1. malibio commented on Oct 1, 2026

    @malibio
    CollaboratorAuthor

    Baseline — Frontend: 301 test files, 5558 tests passed

  2. self-assigned this
    on Oct 1, 2026
  3. added 3 commits that reference this issue on Oct 1, 2026
    a8d3338
    07f3b20
    f20fbcf
  4. malibio commented on Oct 1, 2026

    @malibio
    CollaboratorAuthor

    Landed in #3475. One item from the proposed solution is not in it: the ai-chat-message declaration (must_have_parent_of [ai-chat-native, ai-chat-acp], children: none). That type is not in the registry yet; the rule mechanism is implemented and tested through user-defined types, and declaring it is a registry entry in the change that adds the message type (#3448).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions