Skip to content

improvement(self-host): enterprise features enabling - #6028

Merged
icecrasher321 merged 15 commits into
stagingfrom
improvement/enterprise-self-host
Jul 29, 2026
Merged

improvement(self-host): enterprise features enabling#6028
icecrasher321 merged 15 commits into
stagingfrom
improvement/enterprise-self-host

Conversation

@icecrasher321

Copy link
Copy Markdown
Collaborator

Summary

Adds ENTERPRISE_ENABLED — one switch that turns on the full enterprise feature set on self-hosted deployments without billing — plus INSTANCE_ORG_NAME, which puts every user in a shared organization at signup so org-scoped features (whitelabeling, PII redaction, permission groups, data drains, audit scoping) actually have something to apply to.

Along the way it fixes three server flags that were never read, audit logs and retention deletion that could not work self-hosted at any setting, and a dead end where an auto-joined member could not create a workspace at all; deployments that set nothing keep exactly today's behavior.

Type of Change

  • Bug fix
  • Documentation
  • Other: Self Host

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@icecrasher321
icecrasher321 requested a review from a team as a code owner July 28, 2026 23:30
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 29, 2026 1:42am

Request Review

@cursor

cursor Bot commented Jul 28, 2026

Copy link
Copy Markdown

PR Summary

High Risk
Touches authentication/SSO registration, signup org provisioning, org deletion with workspace detach, and opt-in data retention deletion—mistakes could block sign-in, strand billing, or delete data unexpectedly.

Overview
Self-hosted enterprise is now driven by ENTERPRISE_ENABLED / NEXT_PUBLIC_ENTERPRISE_ENABLED, with per-feature flags that override the master switch. A new entitlement resolver preserves pre-upgrade behavior when the master switch is unset, and gates (SSO plugin, settings nav, APIs) use the same resolved env-flags instead of raw NEXT_PUBLIC_* vars.

Instance organization mode (INSTANCE_ORG_NAME, optional slug/owner) provisions one shared org (advisory-locked, signup hook via joinInstanceOrganization) so org-scoped settings apply; docs add a self-hosted enterprise guide, env reference updates, and a consolidate-users script entry.

Runtime fixes: audit log access without billing when audit logs are enabled; retention deletion only when DATA_RETENTION_ENABLED is on (no hosted free-tier defaults off billing); plain org members may create org workspaces when billing is disabled; shared PII/foreign-workspace checks for settings and Admin API.

Admin API: organization DELETE (slug confirm, detach workspaces in-tx, block live subscriptions), plus PATCH routes for whitelabel, data retention, and session policy with the same entitlement gates as the UI.

Reviewed by Cursor Bugbot for commit 3390208. Configure here.

Comment thread apps/sim/lib/organizations/instance-org.ts
Comment thread apps/sim/app/api/v1/admin/organizations/[id]/route.ts
Comment thread apps/sim/components/settings/navigation.ts
@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds self-hosted enterprise master switch (ENTERPRISE_ENABLED), instance org auto-join (INSTANCE_ORG_NAME), entitlement resolution, Admin API org settings, and retention/audit fixes so enterprise features work without billing.

  • Introduces ENTERPRISE_ENABLED / NEXT_PUBLIC_ENTERPRISE_ENABLED and per-feature override resolution in env-flags / enterprise-entitlements.
  • Provisions a shared instance organization at signup and backfill script for existing users/workspaces.
  • Aligns settings nav, session policy, whitelabel, SSO, audit, and retention cleanup with resolved flags; adds admin PATCH routes for retention/whitelabel/session policy.
  • Documents self-hosted enterprise setup and env vars; Helm/setup twins updated.

Confidence Score: 5/5

Safe to merge; the prior retention-nav note was accepted as intentional and no remaining blocking failures were established for this follow-up scope.

No blocking failure remains. The only prior inline thread (retention settings nav gated on isDataRetentionEnabled) was confirmed intentional by the author and left unchanged.

Important Files Changed

Filename Overview
apps/sim/lib/core/config/env-flags.ts Central enterprise entitlement resolution (master switch, explicit overrides, billing-on vs off) for SSO, access control, retention deletion, and related flags.
apps/sim/lib/organizations/instance-org.ts Idempotent instance-org provisioning and auto-join at signup when INSTANCE_ORG_NAME is set and billing is off.
apps/sim/components/settings/navigation.ts Self-hosted settings sections use resolved env-flags; data retention nav intentionally gated on deletion enablement.
apps/sim/lib/billing/cleanup-dispatcher.ts Retention cleanup runs when DATA_RETENTION_ENABLED without billing; avoids hosted free-tier default deletion off-hosted.
apps/sim/lib/workspaces/policy.ts Org workspace creation policy updated so instance-org members can create workspaces where intended.

Reviews (8): Last reviewed commit: "fix(self-host): refuse instance-org reso..." | Re-trigger Greptile

values.yaml gained the ENTERPRISE_ENABLED switch and INSTANCE_ORG_* keys, and
the feature-flag envDefaults moved from "false" to empty so the master switch
can resolve them. Additive and backward compatible, so a minor bump.
Drop the per-process instance-org id cache. It went stale once the
organization was deleted through the Admin API, and clearing it from the
delete handler would only heal the replica that served that request. The
lookup runs on the signup path against a single-row table, so re-reading
costs nothing and keeps every replica self-correcting.

Scope the org-delete subscription conflict to entitled statuses. Matching any
row regardless of status let a canceled subscription — which bills nobody —
permanently block deletion.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/v1/admin/organizations/[id]/route.ts
…led ones

ENTITLED_SUBSCRIPTION_STATUSES excludes trialing, so a trial — which grants no
entitlement but is a live Stripe subscription that will convert — slipped past
the delete guard and could be stranded against a removed organization id.

Adds TERMINAL_SUBSCRIPTION_STATUSES and inverts the predicate: block unless the
row is finished. Expressed as the terminal set so a status Stripe adds later
defaults to blocking, which is the safe direction for a destructive operation.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/components/settings/navigation.ts
… env var

Nine client consumers still read NEXT_PUBLIC_SSO_ENABLED /
NEXT_PUBLIC_ACCESS_CONTROL_ENABLED directly while the server gates and settings
nav had moved to the resolver. With only ENTERPRISE_ENABLED set that produced
dead ends: the SSO settings section appeared but ssoClient() was never
registered and no login button rendered, and the Access Control section
appeared but its page reported "not entitled".

Points every consumer at isSsoEnabled / isAccessControlEnabled so visibility and
capability come from one place.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

retentionOverrides and per-workspace PII rules both name a workspace, and
neither field is a foreign key. The settings UI rejected ids belonging to
another organization; the Admin API did not, so the two paths could persist
different data for the same org.

Extracts the check as getForeignWorkspaceTargetsReason and points both routes
at it, so they cannot drift apart again.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/v1/admin/organizations/[id]/route.ts Outdated
Comment thread apps/sim/lib/billing/cleanup-dispatcher.ts
Make org delete atomic. detachOrganizationWorkspaces committed on its own, so a
failed delete left workspaces detached and re-billed while the organization,
its members, and its settings survived. Adds a Tx variant so both commit
together.

Gate the admin session-policy PATCH on entitlement, matching the settings UI.
Without it the stored policy was inert — getSessionPolicy resolves to no-op when
the feature is off, so the one eager clamp would be undone on the next refresh.

Stop emitting plan-wide housekeeping when billing is off. It is keyed to the
hosted free-tier 30-day window, the same default the per-workspace pass
deliberately refuses to apply off-hosted.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/v1/admin/organizations/[id]/whitelabel/route.ts
Comment thread apps/sim/lib/workspaces/organization-workspaces.ts
…t-commit

The Admin whitelabel PATCH skipped the entitlement check the settings UI runs,
so an admin key could set branding the product had not granted the org.

detachOrganizationWorkspacesTx also wrote its audit rows inside the caller's
transaction, contradicting its own doc comment — a rolled-back delete would have
left audit history describing detachments that never happened. It now returns
the rows and callers emit them after commit.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/organizations/instance-org.ts
…uous

organization.slug has no unique constraint, and the lookup took the first of
however many matched. The choice is unordered, so two replicas could resolve
different organizations and split new signups between them.

Resolution is now three-state. Ambiguity is distinct from absence, so it both
declines to adopt an arbitrary organization and declines to provision another
one on top of the duplicates.
@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 3390208. Configure here.

@icecrasher321
icecrasher321 merged commit c809845 into staging Jul 29, 2026
23 checks passed
@icecrasher321
icecrasher321 deleted the improvement/enterprise-self-host branch July 29, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant