Skip to content

[schemas] Enhanced thoughts columns and utility RPCs#191

Merged
justfinethanku merged 9 commits into
NateBJones-Projects:mainfrom
alanshurafa:contrib/alanshurafa/enhanced-thoughts
Jun 11, 2026
Merged

[schemas] Enhanced thoughts columns and utility RPCs#191
justfinethanku merged 9 commits into
NateBJones-Projects:mainfrom
alanshurafa:contrib/alanshurafa/enhanced-thoughts

Conversation

@alanshurafa

Copy link
Copy Markdown
Collaborator

Summary

Adds structured columns and utility RPCs to the thoughts table:

  • Columns (all idempotent ADD COLUMN IF NOT EXISTS): type, sensitivity_tier, importance, quality_score, source_type, enriched
  • RPCs: full-text search, aggregate statistics (brain_stats), thought-connection graph queries
  • Indexes on the new columns

All DDL is safe to run repeatedly — no modifications to existing columns, no DROP statements.

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

  • Apply schema.sql to a fresh Open Brain Supabase project — verify all columns and indexes appear
  • Re-apply the same file — verify no errors (idempotency)
  • Call brain_stats RPC — verify it returns non-empty aggregates
  • Insert a test thought and verify the new columns accept writes
  • Verify metadata.json passes .github/metadata.schema.json validation

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.
@alanshurafa alanshurafa force-pushed the contrib/alanshurafa/enhanced-thoughts branch from ab48c49 to 336b6ca Compare May 19, 2026 00:10
@alanshurafa alanshurafa added area: schemas Review area: schemas/primitives/data model risk: schema Touches database schema, migration, or data model behavior review: ready-for-maintainer Community reviewer recommends maintainer review alan-reviewed Reviewed by Alan Shurafa in Community Reviewer role labels May 20, 2026
@alanshurafa

Copy link
Copy Markdown
Collaborator Author

Mergeable, no conflicts against main. No blockers from my side; ready whenever it reaches the queue.

@justfinethanku justfinethanku left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the schema/README changes, refreshed contributor credit, and confirmed the visible required Actions checks pass on the updated head. Approving for maintainer merge.

@justfinethanku justfinethanku merged commit f15cff6 into NateBJones-Projects:main Jun 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

alan-reviewed Reviewed by Alan Shurafa in Community Reviewer role area: schemas Review area: schemas/primitives/data model review: ready-for-maintainer Community reviewer recommends maintainer review risk: schema Touches database schema, migration, or data model behavior schema Contribution: database extension

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants