Skip to content

Conversation

@maparent
Copy link
Collaborator

@maparent maparent commented Jun 28, 2025

Upon space creation, create an anonymous supabase user for the space.
Ensure it is written in as a space "participant" (for future RLS function)
Trigger to delete it when the space is deleted.
All this machinery is tied to getContext.
Auxiliary function to get a supabase client logged in with that anonymous user.

Summary by CodeRabbit

  • New Features

    • Introduced support for anonymous users, allowing spaces to have associated anonymous accounts.
    • Added utility to generate standardized anonymous user emails for each space and platform.
    • Enhanced API to create or retrieve spaces with linked anonymous user accounts and access control.
  • Improvements

    • Improved handling and cleanup of anonymous users and related records when a space is deleted.
    • Strengthened type definitions and validation for account and space management.
  • Bug Fixes

    • Ensured idempotent creation and linking of user, account, and access records to prevent partial failures.

@linear
Copy link

linear bot commented Jun 28, 2025

@supabase
Copy link

supabase bot commented Jun 28, 2025

This pull request has been ignored for the connected project plzkmulvmuhdpcmnyrhm because there are no changes detected in projects/database/supabase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@maparent
Copy link
Collaborator Author

@CodeRabbit review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 28, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 28, 2025

📝 Walkthrough

Walkthrough

This change introduces anonymous user support for spaces by updating database schemas, API endpoints, and utility functions. It adds an 'anonymous' agent type, new SQL functions and triggers for managing anonymous users, and modifies the API and Supabase context logic to handle anonymous user creation, authentication, and cleanup when spaces are deleted.

Changes

File(s) Change Summary
apps/roam/src/utils/supabaseContext.ts Updated to support anonymous user creation: changed function signatures, added parameters for user account ID and password, introduced a cached logged-in client, and added a function to sign in as an anonymous user for a space.
apps/website/app/api/supabase/space/route.ts Extended API to accept account ID and password, create or retrieve anonymous users, handle related platform account and access records, and ensure idempotency and error handling throughout the process.
packages/database/supabase/migrations/20250627172412_upsert_space.sql
.../schemas/account.sql
.../schemas/space.sql
Added 'anonymous' to AgentType enum, created SQL functions and triggers for anonymous email generation and cleanup of users/accounts on space deletion.
packages/database/types.gen.ts Updated generated types: added 'anonymous' to AgentType enum, added get_space_anonymous_email function, updated constants.
packages/ui/src/lib/supabase/client.ts Improved typing for Supabase client, removed redundant constants, and updated client creation logic to use environment variables directly.
packages/ui/src/lib/utils.ts Added a utility function to generate anonymous user email addresses based on platform and space ID.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API (route.ts)
    participant Supabase DB
    participant Auth Service

    Client->>API (route.ts): POST /api/supabase/space { space, account_id, password }
    API (route.ts)->>Supabase DB: Create or retrieve Space
    API (route.ts)->>Auth Service: Sign in anonymous user (email, password)
    alt Anonymous user does not exist
        API (route.ts)->>Auth Service: Create anonymous user (email, password)
    end
    API (route.ts)->>Supabase DB: Create/retrieve PlatformAccount for anon user
    API (route.ts)->>Supabase DB: Grant SpaceAccess (editor) to anon account and account_id
    API (route.ts)-->>Client: Return space record or error
Loading

Possibly related PRs

  • DiscourseGraphs/discourse-graph#245: Extends fetchOrCreateSpaceId and adds getLoggedInClient to manage space passwords and Supabase context, directly related to the anonymous user and password handling in this PR.
  • DiscourseGraphs/discourse-graph#187: Introduces the initial getSupabaseContext function, upon which the current PR builds by extending functionality for anonymous users and space authentication.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 8

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2294492 and 612cbb3.

📒 Files selected for processing (8)
  • apps/roam/src/utils/supabaseContext.ts (3 hunks)
  • apps/website/app/api/supabase/space/route.ts (3 hunks)
  • packages/database/supabase/migrations/20250627172412_upsert_space.sql (1 hunks)
  • packages/database/supabase/schemas/account.sql (1 hunks)
  • packages/database/supabase/schemas/space.sql (1 hunks)
  • packages/database/types.gen.ts (3 hunks)
  • packages/ui/src/lib/supabase/client.ts (1 hunks)
  • packages/ui/src/lib/utils.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (7)
📓 Common learnings
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: packages/database/supabase/schemas/account.sql:0-0
Timestamp: 2025-06-09T16:55:48.941Z
Learning: Security work including RLS policies and permission restrictions is currently postponed in the discourse-graph project as communicated by maparent.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#189
File: packages/database/supabase/migrations/20250603144146_account_centric.sql:123-136
Timestamp: 2025-06-04T11:41:08.589Z
Learning: Currently, there are no Obsidian spaces in the database - all existing spaces are Roam spaces. This means database migrations targeting space platform assignments only need to handle Roam for now.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#165
File: packages/database/supabase/migrations/20250504195841_remote_schema.sql:1-7
Timestamp: 2025-05-20T03:01:11.220Z
Learning: Supabase automatically creates necessary schemas like the "extensions" schema before migrations run, so there's no need to explicitly create these schemas in migration files.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: apps/website/app/api/supabase/agent_identifier/route.ts:0-0
Timestamp: 2025-06-09T16:54:29.659Z
Learning: In the AgentIdentifier API route (apps/website/app/api/supabase/agent_identifier/route.ts), the trusted field is intentionally optional and should accept undefined values. The validation logic `if (trusted !== undefined && ...)` is designed to allow undefined as expected behavior.
packages/ui/src/lib/supabase/client.ts (4)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/utils/supabase/dbUtils.ts:22-28
Timestamp: 2025-05-30T14:49:24.016Z
Learning: In apps/website/app/utils/supabase/dbUtils.ts, expanding the KNOWN_EMBEDDINGS and DEFAULT_DIMENSIONS mappings to support additional embedding models requires corresponding database model changes (creating new embedding tables), which should be scoped as separate work from API route implementations.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#241
File: packages/database/tsconfig.json:3-7
Timestamp: 2025-06-25T18:03:52.648Z
Learning: The packages/database directory in the discourse-graph repository has a unique structure as a database schema/migration package. It contains doc/, scripts/, supabase/ directories and TypeScript files at the root level, but no typical src/, test/, dist/, or node_modules directories. The current tsconfig.json with "include": ["."] and "exclude": ["supabase"] is appropriate for this structure.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#189
File: packages/database/types.gen.ts:336-371
Timestamp: 2025-06-04T11:54:37.239Z
Learning: The team prefers not to manually edit generated files (like Supabase TypeScript types) even when they contain inaccuracies, as manual changes would be overwritten during regeneration. They treat generated files as read-only artifacts.
Learnt from: CR
PR: DiscourseGraphs/discourse-graph#0
File: .cursor/rules/main.mdc:0-0
Timestamp: 2025-06-23T13:20:12.846Z
Learning: In TypeScript codebases, prefer 'type' aliases over 'interface' declarations for defining types, unless an interface is specifically required.
packages/database/supabase/schemas/account.sql (1)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: packages/database/schema.yaml:0-0
Timestamp: 2025-06-09T16:53:13.253Z
Learning: In LinkML schema files, enum default values should use the enum constructor syntax like `ifabsent: AgentType(person)` rather than literal string values like `ifabsent: 'person'`. This is the correct syntax that passes LinkML linting.
packages/database/supabase/migrations/20250627172412_upsert_space.sql (4)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: packages/database/supabase/migrations/20250605083319_alpha_upload.sql:157-337
Timestamp: 2025-06-09T16:57:14.681Z
Learning: Migration files in packages/database/supabase/migrations/ are historical snapshots that preserve database schema and functions as they existed at the time of creation. These files should not be updated to reflect current schema changes, even if they reference tables or columns that have since been modified or removed. Schema incompatibilities in migration files are expected and acceptable as they represent the valid state at the time of migration.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#165
File: packages/database/supabase/migrations/20250504195841_remote_schema.sql:1-7
Timestamp: 2025-05-20T03:01:11.220Z
Learning: Supabase automatically creates necessary schemas like the "extensions" schema before migrations run, so there's no need to explicitly create these schemas in migration files.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#165
File: packages/database/supabase/migrations/20250512142307_sync_table.sql:20-21
Timestamp: 2025-05-20T03:04:21.602Z
Learning: Database migration files (like the SQL files in packages/database/supabase/migrations/) should not be modified after they're created, even to fix minor issues like unused variables. Issues should be addressed in newer schema files instead to maintain migration history integrity.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#165
File: packages/database/supabase/schemas/extensions.sql:1-11
Timestamp: 2025-05-20T03:02:36.027Z
Learning: The necessary schemas for Supabase extensions (like 'extensions', 'graphql', and 'vault') are automatically created by Supabase before extensions are installed, so they don't need to be explicitly created in SQL migration files.
packages/database/types.gen.ts (3)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/utils/supabase/dbUtils.ts:22-28
Timestamp: 2025-05-30T14:49:24.016Z
Learning: In apps/website/app/utils/supabase/dbUtils.ts, expanding the KNOWN_EMBEDDINGS and DEFAULT_DIMENSIONS mappings to support additional embedding models requires corresponding database model changes (creating new embedding tables), which should be scoped as separate work from API route implementations.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: packages/database/schema.yaml:0-0
Timestamp: 2025-06-09T16:53:13.253Z
Learning: In LinkML schema files, enum default values should use the enum constructor syntax like `ifabsent: AgentType(person)` rather than literal string values like `ifabsent: 'person'`. This is the correct syntax that passes LinkML linting.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: apps/website/app/api/supabase/agent_identifier/route.ts:0-0
Timestamp: 2025-06-09T16:54:29.659Z
Learning: In the AgentIdentifier API route (apps/website/app/api/supabase/agent_identifier/route.ts), the trusted field is intentionally optional and should accept undefined values. The validation logic `if (trusted !== undefined && ...)` is designed to allow undefined as expected behavior.
apps/roam/src/utils/supabaseContext.ts (8)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/utils/supabase/dbUtils.ts:22-28
Timestamp: 2025-05-30T14:49:24.016Z
Learning: In apps/website/app/utils/supabase/dbUtils.ts, expanding the KNOWN_EMBEDDINGS and DEFAULT_DIMENSIONS mappings to support additional embedding models requires corresponding database model changes (creating new embedding tables), which should be scoped as separate work from API route implementations.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/api/supabase/rpc/search-content/route.ts:53-56
Timestamp: 2025-05-30T14:37:30.215Z
Learning: The search-content route (apps/website/app/api/supabase/rpc/search-content/route.ts) is intentionally designed to be platform-agnostic rather than Roam-specific. The generic "Platform" terminology (like subsetPlatformIds) is used because the route will support multiple platforms in the near future, not just Roam.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#189
File: packages/database/supabase/migrations/20250603144146_account_centric.sql:123-136
Timestamp: 2025-06-04T11:41:08.589Z
Learning: Currently, there are no Obsidian spaces in the database - all existing spaces are Roam spaces. This means database migrations targeting space platform assignments only need to handle Roam for now.
Learnt from: sid597
PR: DiscourseGraphs/discourse-graph#232
File: apps/roam/src/utils/getAllDiscourseNodesSince.ts:18-31
Timestamp: 2025-06-22T10:40:52.752Z
Learning: In apps/roam/src/utils/getAllDiscourseNodesSince.ts, the user confirmed that querying for `?title` with `:node/title` and mapping it to the `text` field in the DiscourseGraphContent type is the correct implementation for retrieving discourse node content from Roam Research, despite it appearing to query page titles rather than block text content.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#0
File: :0-0
Timestamp: 2025-06-25T22:56:17.467Z
Learning: In the Roam discourse-graph system, the existence of the configuration page (identified by DISCOURSE_CONFIG_PAGE_TITLE) and its corresponding UID is a system invariant. The code can safely assume this page will always exist, so defensive null checks are not needed when using `getPageUidByPageTitle(DISCOURSE_CONFIG_PAGE_TITLE)`.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#220
File: apps/roam/src/utils/conceptConversion.ts:42-56
Timestamp: 2025-06-17T23:37:45.289Z
Learning: In the DiscourseNode interface from apps/roam/src/utils/getDiscourseNodes.ts, the field `type` serves as the unique identifier field, not a type classification field. The interface has no `uid` or `id` field, making `node.type` the correct field to use for UID-related operations.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#220
File: apps/roam/src/utils/conceptConversion.ts:42-56
Timestamp: 2025-06-17T23:37:45.289Z
Learning: In the DiscourseNode interface from apps/roam/src/utils/getDiscourseNodes.ts, the field `node.type` serves as the UID field rather than having a conventional `node.uid` field. This is an unusual naming convention where the type field actually contains the unique identifier.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#241
File: packages/database/tsconfig.json:3-7
Timestamp: 2025-06-25T18:03:52.648Z
Learning: The packages/database directory in the discourse-graph repository has a unique structure as a database schema/migration package. It contains doc/, scripts/, supabase/ directories and TypeScript files at the root level, but no typical src/, test/, dist/, or node_modules directories. The current tsconfig.json with "include": ["."] and "exclude": ["supabase"] is appropriate for this structure.
apps/website/app/api/supabase/space/route.ts (4)
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/utils/supabase/dbUtils.ts:22-28
Timestamp: 2025-05-30T14:49:24.016Z
Learning: In apps/website/app/utils/supabase/dbUtils.ts, expanding the KNOWN_EMBEDDINGS and DEFAULT_DIMENSIONS mappings to support additional embedding models requires corresponding database model changes (creating new embedding tables), which should be scoped as separate work from API route implementations.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#182
File: apps/website/app/api/supabase/rpc/search-content/route.ts:53-56
Timestamp: 2025-05-30T14:37:30.215Z
Learning: The search-content route (apps/website/app/api/supabase/rpc/search-content/route.ts) is intentionally designed to be platform-agnostic rather than Roam-specific. The generic "Platform" terminology (like subsetPlatformIds) is used because the route will support multiple platforms in the near future, not just Roam.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#198
File: apps/website/app/api/supabase/agent_identifier/route.ts:0-0
Timestamp: 2025-06-09T16:54:29.659Z
Learning: In the AgentIdentifier API route (apps/website/app/api/supabase/agent_identifier/route.ts), the trusted field is intentionally optional and should accept undefined values. The validation logic `if (trusted !== undefined && ...)` is designed to allow undefined as expected behavior.
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#241
File: packages/database/tsconfig.json:3-7
Timestamp: 2025-06-25T18:03:52.648Z
Learning: The packages/database directory in the discourse-graph repository has a unique structure as a database schema/migration package. It contains doc/, scripts/, supabase/ directories and TypeScript files at the root level, but no typical src/, test/, dist/, or node_modules directories. The current tsconfig.json with "include": ["."] and "exclude": ["supabase"] is appropriate for this structure.
🧬 Code Graph Analysis (1)
packages/ui/src/lib/supabase/client.ts (2)
packages/database/types.gen.ts (1)
  • Database (9-821)
apps/website/app/utils/supabase/client.ts (1)
  • createClient (6-14)
🔇 Additional comments (8)
packages/database/supabase/schemas/account.sql (1)

4-5: LGTM! Clean enum extension for anonymous agent support.

The addition of the 'anonymous' value to the AgentType enum follows the existing pattern and supports the new anonymous user functionality.

packages/ui/src/lib/supabase/client.ts (2)

1-4: LGTM! Improved imports and type safety.

The explicit import of SupabaseClient type and renaming of the import function enhances code clarity.


9-13: LGTM! Excellent type safety improvement.

The DGSupabaseClient type alias provides strong typing for the Supabase client with proper database schema constraints.

packages/database/supabase/schemas/space.sql (2)

31-33: LGTM! Clean email generation function.

The get_space_anonymous_email function is correctly marked as IMMUTABLE and generates a consistent email format for anonymous users.


47-47: LGTM! Proper trigger setup for cleanup.

The trigger is correctly configured to execute the cleanup function after each DELETE operation on the Space table.

packages/database/types.gen.ts (3)

615-621: LGTM! Correct function type definition.

The get_space_anonymous_email function type correctly reflects the SQL function signature with proper parameter and return types.


706-706: LGTM! AgentType enum updated correctly.

The enum type definition properly includes the new "anonymous" value, maintaining consistency with the database schema.


932-932: LGTM! Constants array updated consistently.

The Constants array correctly includes "anonymous" in the AgentType values, ensuring runtime consistency with the type definitions.

@maparent maparent force-pushed the ENG-420-create-space-anon-account branch from 612cbb3 to c6c7048 Compare June 28, 2025 21:14
@maparent maparent requested a review from mdroidian June 29, 2025 14:35
@maparent maparent force-pushed the ENG-420-create-space-anon-account branch 2 times, most recently from 204857f to cfe7380 Compare July 3, 2025 19:49
Copy link
Contributor

@mdroidian mdroidian left a comment

Choose a reason for hiding this comment

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

Mostly nits

@maparent maparent force-pushed the ENG-420-create-space-anon-account branch from cfe7380 to 3acac2d Compare July 4, 2025 18:59
@maparent maparent merged commit 8b231ba into main Jul 5, 2025
4 checks passed
@github-project-automation github-project-automation bot moved this to Done in General Jul 5, 2025
@maparent maparent deleted the ENG-420-create-space-anon-account branch July 5, 2025 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants