Skip to content

[schemas] Knowledge graph tables and extraction trigger#5

Open
alanshurafa wants to merge 5 commits intomainfrom
contrib/alanshurafa/knowledge-graph-schema
Open

[schemas] Knowledge graph tables and extraction trigger#5
alanshurafa wants to merge 5 commits intomainfrom
contrib/alanshurafa/knowledge-graph-schema

Conversation

@alanshurafa
Copy link
Copy Markdown
Owner

Summary

  • Adds 5 tables for a knowledge graph layer: entities, edges, thought_entities, entity_extraction_queue, consolidation_log
  • Installs auto-queue trigger on thoughts that enqueues new/updated thoughts for entity extraction
  • Part of the OB1 alpha milestone (Wave 2 schema — depends on PR [schemas] Enhanced thoughts columns and utility RPCs #1 enhanced-thoughts)

What's included

Object Type Purpose
entities table Canonical graph nodes (person, project, topic, tool, organization, place)
edges table Typed relationships between entities with support counts and confidence
thought_entities table Evidence-bearing links from thoughts to entities
entity_extraction_queue table Async queue for pending entity extraction work
consolidation_log table Audit trail for dedup merges and quality operations
queue_entity_extraction() trigger function Auto-queues thoughts on insert/update with fingerprint guards
trg_queue_entity_extraction trigger Attached to thoughts table
8 indexes indexes Entity type, normalized name, edge traversal, thought links, pending queue

Design decisions

  • Idempotent DDL: All CREATE TABLE IF NOT EXISTS, CREATE OR REPLACE FUNCTION, and DROP TRIGGER IF EXISTS patterns — safe to run multiple times
  • Trigger guards: Skips system-generated artifacts (metadata.generated_by IS NOT NULL) and no-op updates where content fingerprint hasn't changed
  • Queue dedup: ON CONFLICT (thought_id) DO UPDATE only resets when source_fingerprint IS DISTINCT FROM the stored value, preventing churn from metadata-only writes
  • Cascade cleanup: Deleting a thought or entity cascades to remove related links and queue entries
  • Optional backfill: Commented SQL at bottom for existing brains to queue pre-existing thoughts — not auto-run to avoid surprise load on large brains
  • Read access: anon/authenticated get SELECT on all graph tables for MCP tools and REST API queries

Dependencies

Test plan

  • Paste schema.sql into Supabase SQL Editor and run — no errors
  • Confirm 5 new tables appear in Table Editor
  • Confirm queue_entity_extraction function in Database > Functions
  • Confirm trg_queue_entity_extraction trigger on thoughts table
  • Insert a thought and verify a row appears in entity_extraction_queue
  • Re-run the migration — no errors (idempotency check)

🤖 Generated with Claude Code

Add entities, edges, thought_entities, entity_extraction_queue,
and consolidation_log tables for automatic entity and relationship
extraction from thoughts. Install auto-queue trigger that enqueues
thoughts on insert or content change with fingerprint-based
dedup guards. Part of the OB1 alpha milestone.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the schema label Apr 6, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 087c43f333

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread schemas/knowledge-graph/schema.sql Outdated
-- ============================================================

CREATE TABLE IF NOT EXISTS public.thought_entities (
thought_id BIGINT NOT NULL REFERENCES public.thoughts(id) ON DELETE CASCADE,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use UUID thought_id to match thoughts.id

The new schema defines thought_id as BIGINT, but the documented base Open Brain schema creates thoughts.id as UUID (docs/01-getting-started.md, lines 67-74). In Postgres, this makes the FK invalid (BIGINT cannot reference UUID), so applying this migration will fail when creating thought_entities (and similarly entity_extraction_queue), and the trigger path that inserts NEW.id into the queue will also be type-incompatible. This blocks installation for standard deployments that followed the setup guide.

Useful? React with 👍 / 👎.

alanshurafa and others added 2 commits April 6, 2026 10:39
Add blank lines around headings (MD022), fenced code blocks (MD031),
and between adjacent blockquotes (MD028). Fix broken link fragment
(MD051) and remove extra blank line (MD012). No content changes.

These errors were blocking CI on all open PRs since the lint check
runs repo-wide.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
alanshurafa and others added 2 commits April 6, 2026 13:53
Base OB1 thoughts.id is UUID (gen_random_uuid()), not BIGINT.
Fixed thought_entities.thought_id, entity_extraction_queue.thought_id,
consolidation_log.survivor_id, and consolidation_log.loser_id.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation integration recipe schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant