[schemas] Enhanced thoughts columns and utility RPCs#191
Merged
justfinethanku merged 9 commits intoJun 11, 2026
Merged
Conversation
This was referenced Apr 18, 2026
Why: The README claimed defaults (importance=5, quality_score=0.50, sensitivity_tier='normal') that disagreed with schema.sql's actual values (3, 50, 'standard'). The 'normal' tier would have broken every other contribution in the repo that expects 'standard'. The SQL is authoritative -- update the README to match. Also fix the ranking formula's coalesce fallback (quality_score used 0.50 while the column is 0..100) so NULL rows don't get a near-zero rank bonus.
Why: Two of three RPCs are SECURITY DEFINER and all three were granted to anon -- which turns the publishable anon key into a universal read handle over the entire thoughts table, inverting Open Brain's stock RLS-behind-service_role posture. Restrict EXECUTE to authenticated and service_role. Keep SECURITY DEFINER with SET search_path = public (defense-in-depth against search-path hijacks). Document the security posture in the README so anyone who wants public read can opt in explicitly rather than inherit it silently.
Why: `t.id NOT IN (SELECT hit_id ...)` has NULL-unsafe semantics -- if the subquery ever yields a NULL, the predicate becomes NULL (not true) and the row is silently filtered out. In this schema the PK is NOT NULL so the bug cannot fire today, but NOT EXISTS is the correct discipline and usually plans better for anti-joins.
Why: search_thoughts_text was VOLATILE and get_thought_connections had no volatility declared (defaults to VOLATILE). Both are pure readers over their inputs within a transaction -- they touch no sequences, write no rows, and do not depend on now() for results. Marking them STABLE unlocks planner optimizations (function inlining, CSE, index-only scans when used in predicates) and keeps PostgREST calls fast under dashboard load.
Why: The inline backfill hard-coded an 8-value type allowlist (idea/task/person_note/reference/decision/lesson/meeting/journal) and silently discarded every other value. Users with brains that already use 'article', 'quote', 'bookmark', etc. would run the migration, see the README promise a backfill, and get NULL on all their rows with no warning. Wrap the backfill in backfill_thought_types( p_allowed_types TEXT[]) with the canonical 8 as the default, so paste-and-run keeps working while power users can override the list (or pass NULL to accept any value). Document the knob in the README and update Troubleshooting to point at it.
Why: get_thought_connections used `bt.sensitivity_tier != 'restricted'` which evaluates to NULL (not true) when the column is NULL, silently dropping rows. brain_stats_aggregate already uses the NULL-safe `IS DISTINCT FROM 'restricted'`. Match that pattern so both RPCs see the same set of rows.
Why: The `updated` field in metadata.json is a signal for downstream consumers that the file has been revised. Since this commit chain adds security, correctness, and configurability changes, bump the date from 2026-04-06 to the current 2026-04-17 so anyone reading the metadata can see the contribution was recently touched.
ab48c49 to
336b6ca
Compare
Collaborator
Author
|
Mergeable, no conflicts against |
justfinethanku
approved these changes
Jun 11, 2026
justfinethanku
left a comment
Collaborator
There was a problem hiding this comment.
Reviewed the schema/README changes, refreshed contributor credit, and confirmed the visible required Actions checks pass on the updated head. Approving for maintainer merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds structured columns and utility RPCs to the
thoughtstable:ADD COLUMN IF NOT EXISTS):type,sensitivity_tier,importance,quality_score,source_type,enrichedbrain_stats), thought-connection graph queriesAll DDL is safe to run repeatedly — no modifications to existing columns, no
DROPstatements.Why
This is the schema foundation for downstream Alpha-milestone work (thought-enrichment pipeline, smart-ingest, enhanced-mcp). Split out as its own PR so reviewers can evaluate the column contract in isolation before any code depends on it.
Part 1 of 12 in the OB1 Alpha Milestone consolidation — this replaces the content previously tracked in the now-closed PR bundle from 2026-03.
Test plan
schema.sqlto a fresh Open Brain Supabase project — verify all columns and indexes appearbrain_statsRPC — verify it returns non-empty aggregatesmetadata.jsonpasses.github/metadata.schema.jsonvalidation