Skip to content

[recipes] Brain health monitoring views#194

Open
alanshurafa wants to merge 4 commits into
NateBJones-Projects:mainfrom
alanshurafa:contrib/alanshurafa/brain-health-monitoring
Open

[recipes] Brain health monitoring views#194
alanshurafa wants to merge 4 commits into
NateBJones-Projects:mainfrom
alanshurafa:contrib/alanshurafa/brain-health-monitoring

Conversation

@alanshurafa
Copy link
Copy Markdown
Collaborator

Summary

Adds a recipe of operational views for monitoring an Open Brain deployment — enrichment coverage, source volume trends, stalled ingestion/extraction queues.

Five core views work on any stock Open Brain install; three additional views unlock if companion schemas are installed (smart-ingest, entity-extraction). Optional views are wrapped in to_regclass guards so missing companion tables produce a NOTICE and skip, not an error.

  • ops_recent_thoughts — last N thoughts with source/type/importance (restricted content excluded from preview)
  • ops_enrichment_gaps — thoughts pending classification, grouped by source
  • ops_source_volume_by_day — capture volume per source over 30 days
  • ops_type_distribution — type-class breakdown with percentages
  • ops_quality_distribution — quality_score distribution buckets
  • ops_ingestion_summary — (optional, requires smart-ingest schema) jobs by status
  • ops_stalled_entity_queue — (optional, requires entity-extraction schema) items stuck in pending/retry
  • ops_graph_coverage — (optional, requires entity-extraction schema) fraction of thoughts with extracted entities

Why

Hitting the Supabase dashboard + writing ad-hoc SQL is fine for one-brain inspection, but once a brain exceeds 10k thoughts, operators need recurring views to notice enrichment drift, source outages, and queue stalls. These views are a zero-dependency reference set anyone can psql -f ops-views.sql and get.

Grants are service_role only — no anon, no authenticated. Previews filter WHERE sensitivity_tier IS DISTINCT FROM 'restricted' to match the convention from the enhanced-thoughts schema.

Test plan

  • Apply ops-views.sql to a stock Open Brain project — verify views 1-5 appear, views 6-8 emit NOTICE and skip
  • Install smart-ingest + entity-extraction schemas first, re-apply — verify views 6-8 now appear
  • Call SELECT * FROM ops_recent_thoughts LIMIT 5; — verify no restricted content in preview column
  • Re-run the file — verify idempotent, no errors on re-apply
  • Verify metadata.json passes .github/metadata.schema.json

@github-actions github-actions Bot added recipe Contribution: step-by-step recipe schema Contribution: database extension labels Apr 18, 2026
@alanshurafa alanshurafa added area: recipes Review area: recipes risk: schema Touches database schema, migration, or data model behavior review: needs-refresh Branch is stale, conflicted, or needs rebase before review alan-reviewed Reviewed by Alan Shurafa in Community Reviewer role labels May 20, 2026
@alanshurafa
Copy link
Copy Markdown
Collaborator Author

Conflicts with main. Before I rebase, should this stay as-is, be reshaped, or be dropped? I can refresh it quickly if you want to keep it.

…class

Why: views 6-8 reference public.ingestion_jobs and public.entity_extraction_queue,
which live in optional companion schemas (schemas/smart-ingest and
schemas/entity-extraction). Running ops-views.sql on a stock install created
views 1-5, then failed at view 6 with "relation does not exist" and left the
user in an ambiguous half-installed state.

Wrap each optional view in DO $$ IF to_regclass(...) IS NOT NULL THEN EXECUTE
'CREATE OR REPLACE VIEW ...' END IF $$ so the file runs cleanly on any shape
of install. Move the matching GRANT SELECT ... TO service_role statements
inside each DO block so grants only run when the view was actually created.
Emit a NOTICE for each skipped view pointing at the schema that enables it.

Also refresh the file-header comment block to match the corrected schema paths
(schemas/smart-ingest, schemas/entity-extraction) and note that views 6-8 are
gracefully skipped rather than erroring.
Why: the Prerequisites section and the "requires" labels in the views table
pointed at schemas/smart-ingest-tables and schemas/knowledge-graph, neither
of which exists under those names. The actual schema directories shipped by
the Wave 3 PRs are schemas/smart-ingest (ships public.ingestion_jobs) and
schemas/entity-extraction (ships public.entity_extraction_queue).

Users following the README literally would hit broken paths and not know
where to install the optional prerequisites. Replace every reference with the
real directory names (incl. the Troubleshooting section and the Steps §1
review block), and reframe the "comment out views 6-8" guidance now that the
SQL guards missing tables with to_regclass instead of erroring.

Paired with the SQL guard commit, this closes HIGH-1 end-to-end.
…w views

Why: ops_recent_thoughts and ops_enrichment_gaps both emit left(content, 180)
as a preview column without filtering on sensitivity_tier. Today the views
are service_role-only, so there is no blast radius -- but the README markets
them as queryable via PostgREST and "your dashboard", which primes a future
maintainer to add GRANT SELECT ... TO authenticated. The moment that happens,
the first 180 chars of every restricted thought flow out through these views.

Add WHERE sensitivity_tier IS DISTINCT FROM 'restricted' to both views. This
matches the convention used by search_thoughts_text(p_exclude_restricted) in
schemas/enhanced-thoughts/schema.sql and costs one clause per view. Cheap
insurance against a future "widen the grants for the dashboard" regression.
@alanshurafa alanshurafa force-pushed the contrib/alanshurafa/brain-health-monitoring branch from 96377dc to 3ff5306 Compare May 20, 2026 17:13
@alanshurafa
Copy link
Copy Markdown
Collaborator Author

Rebased onto main — conflicts cleared. The only conflict was a stray repo-wide markdown-lint commit on this branch that collided with main's own lint cleanup; I dropped that commit. The branch's actual changes are untouched. Now mergeable.

@alanshurafa alanshurafa added review: ready-for-maintainer Community reviewer recommends maintainer review and removed review: needs-refresh Branch is stale, conflicted, or needs rebase before review labels May 20, 2026
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: recipes Review area: recipes recipe Contribution: step-by-step recipe 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.

1 participant