Skip to content
MSO CloudDocumentation

ADR 0002 — Plugin architecture (core + per-org plugins)

On this page
  • Status: Accepted
  • Date: 2026-05
  • Context owner: Platform

Context#

UCIP is a multi-tenant commerce-intelligence platform. Different customers buy different capabilities: some want AI chat, some want decision intelligence or live-commerce analytics, many want none of it. Before this work, every feature was always-on for every org, gated only loosely by RBAC. We needed a way to package the product into composable features that a super admin enables per organization, without forking the codebase per customer, and to keep the codebase clean and extensible as the catalog grows.

Constraints that shape the design:

  • Next.js App Router: route handlers (app/api/**) and pages (app/(dashboard)/**) must live under apps/web/src/app — they cannot be moved into a workspace package.
  • Centralized Drizzle schema + RLS: every table lives in @yng/schema so the migration generator and the single rls-policies.sql see the whole schema.
  • Interconnected feature routers: core workspace pages already read briefing/actions/playbooks; /activation reads audiences. Features are not cleanly isolated at the data layer.
  • Other agents were active on main during the work, raising the cost of large file moves.

Decisions#

D1 — A compiled, central plugin catalog (PLUGIN_REGISTRY)

Plugins are declared as manifests in one array in packages/plugin-host/src/registry.ts, Zod-validated at module load. Not a dynamic/remote loader; plugins compile into the build and are toggled per org at runtime via org_plugin_installations.

Why: simplest thing that is correct and extensible. One file is the catalog; a malformed manifest fails the build. No module-cycle or boot-ordering risk.

D2 — Per-org enablement via org_plugin_installations + a 3-step gate

Resolution: install row → legacy org flag → defaultEnabled. defaultEnabled expresses opt-out (formerly-always-on features stay on = zero disruption) vs opt-in (net-new gated features start off). A backfill seeds rows for every org × plugin so the gate flip was a no-op on deploy.

D3 — Entitlement at the page + nav layers, not the tRPC procedure layer

requirePlugin(pluginId) redirects a disabled plugin's page server-side; getDisabledNavItemsForOrg hides its nav items. Feature routers stay on orgProcedure/brandProcedure.

Why: the feature routers are interconnected (D-context). An earlier implementation gated the routers with a pluginProcedure factory; adversarial review found it 500s the core workspace pages and /activation when a plugin is off. Reverted. chat-with-data is the exception — its router is not shared, so it keeps a dedicated chatProcedure gate (org flag + role + AI-config).

Consequence / accepted residual: a determined caller could hit a disabled plugin's read API for their own org's data — an entitlement gap, not a tenant-isolation gap (RLS still scopes rows). Closing it requires a dedicated, non-shared router per plugin (the chat pattern).

D4 — Pass A (describe in place), not Pass B (relocate)

Plugin code stays in its framework home (router in routers/, pages in app/, tables in @yng/schema); the manifest describes it. We did not physically relocate code into packages/plugins/<id>/.

Why: the App-Router/Drizzle constraints mean a plugin package could only hold a manifest + pure logic anyway; the churn (hundreds of import changes) carried real regression risk while other agents were on main, for little behavioral gain. The system is modular via manifest + gating regardless of file location.

D5 — Where a plugin's settings live, and what is a plugin at all

A plugin is on or off. Two questions kept being answered per feature, so they are answered here once.

Settings. A plugin's behavioural settings — thresholds, labels, sub-toggles, default views — belong in org_plugin_installations.config, validated by a settingsSchema the manifest declares. That mechanism is added with its first consumer, not before: the column has no reader and no writer today, and an unschematised write to it is the failure this rule exists to prevent. A plugin's domain data — a chat assistant's persona, the MSO policy models — keeps its own tables, because a column a query filters on beats a jsonb blob. Secrets never, as the column itself already states (packages/schema/src/tables/org-plugin-installations.ts).

What is a plugin. A per-org switch that gates a tenant-facing surface is a plugin. A per-org switch that gates a platform behaviour — auth method, theme, demo posture, BYOK — is an org setting, a column on organizations. A per-org switch that a separate ADR made a licensed entitlement (white_label, ldap) stays where that ADR put it. No third store, and no second feature-flag system beside the plugin ledger — that is the alternative rejected below.

Data outlives the switch. Disabling a plugin never deletes what it wrote; purging an org's data is a maintenance script under the archive posture, never a side effect of the plugin system.

Alternatives considered#

  • Feature flags only — a org_features(org_id, key, enabled) table with scattered if checks. Rejected: no code boundary, no catalog UI, no schema-ownership signal, no lifecycle. The plugin model subsumes flags.
  • Runtime/remote plugins (WordPress-style) — install from a URL, sandboxed. Rejected: plugins are first-party trusted code; sandboxing + a remote contract is large overhead with a new security surface, unjustified today.
  • Per-package manifests + build SKUs (proposal §6) — each plugin a package exporting its manifest, composed by apps/web/src/plugins.<sku>.ts. Deferred, not rejected: it adds module-cycle/boot-ordering complexity for marginal gain at six plugins. Revisit when a plugin has heavy isolated code, a third-party author, or when per-customer bundle tree-shaking becomes worthwhile.

Consequences#

  • Adding a plugin is a one-manifest + i18n + backfill + page-guard task, scaffolded by scripts/scaffold-plugin.ts and CI-guarded by scripts/audit/plugin-manifest-validity.ts.
  • /admin/orgs/<slug>/plugins is registry-driven; new plugins appear automatically.
  • Legacy per-org flags (chatAssistantEnabled) are deprecated once all orgs have install rows; no new legacy flags.
  • The per-package/SKU path remains open and is documented in the developer guide.

Amendment 2026-11-28#

Code diverged from D2 and the consequences list; everything not named here stands.

  • D2 resolution order. The gate no longer resolves "install row, legacy org flag, defaultEnabled". A missing installation row means off; defaultEnabled is read by provisioning and reconcilePluginRows, which write the row (packages/plugin-host/src/resolve.ts resolveEnabled, packages/plugin-host/src/gate.ts). The legacy org flag is consulted only for chatAssistantEnabled, and only while that plugin has no row.
  • Install row. A row counts only while enabled, is_active, status active or trial, and before valid_to (installBlockReason); legacy plugin ids fold onto current ids through aliases.
  • Kinds. A manifest kind is feature, connector, satellite or shared_feed (packages/plugin-host/src/manifest.ts).
  • Backfill. "Adding a plugin" no longer needs a backfill: the reconcile writes the rows on every deploy.