|
| 1 | +# RFC-0002: Plugin Manifest & Lifecycle |
| 2 | + |
| 3 | +**Status:** Draft |
| 4 | +**Author:** Kasun B. |
| 5 | +**Created:** 2025-10-21 |
| 6 | +**Target Version:** Sovereign v0.1 |
| 7 | +**Tags:** Tags: Plugins, SDK, Manifest, Extensibility, Lifecycle |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 1. Purpose |
| 12 | + |
| 13 | +This RFC defines the structure and behavior of the Sovereign Plugin Manifest and its associated lifecycle. It ensures all plugins follow a consistent, predictable format while remaining lightweight and isolated. Plugins extend the platform without modifying core code, interacting only through stable contracts and SDK APIs. |
| 14 | + |
| 15 | +## 2. Design Goals |
| 16 | + |
| 17 | +- Standardize plugin definition and metadata using a manifest file. |
| 18 | +- Provide clear lifecycle hooks for setup, enablement, and teardown. |
| 19 | +- Support backend, frontend, and hybrid (full-stack) plugins. |
| 20 | +- Allow plugin-specific database schemas, migrations, and configuration. |
| 21 | +- Maintain version and API compatibility across Sovereign releases. |
| 22 | +- Enable validation and integrity checks for plugins before activation. |
| 23 | + |
| 24 | +## 3. Manifest File |
| 25 | + |
| 26 | +_Example `plugin.json`_ |
| 27 | + |
| 28 | +```json |
| 29 | +{ |
| 30 | + "name": "@sovereign/papertrail", |
| 31 | + "version": "1.0.0", |
| 32 | + "description": "Evidence and documentation board system for Sovereign.", |
| 33 | + "author": "CommonsEngine Team", |
| 34 | + "license": "AGPL-3.0", |
| 35 | + "sovereign": { |
| 36 | + "engine": ">=0.1.0 <2.0.0", |
| 37 | + "entry": "./index.mjs" |
| 38 | + }, |
| 39 | + "capabilities": [ |
| 40 | + "papertrail.board.read", |
| 41 | + "papertrail.board.write", |
| 42 | + "papertrail.board.update", |
| 43 | + "papertrail.board.delete" |
| 44 | + ], |
| 45 | + "events": { |
| 46 | + "subscribe": ["board.created", "user.deleted"] |
| 47 | + }, |
| 48 | + "schema": "./prisma/schema.prisma", |
| 49 | + "migrations": "./prisma/migrations", |
| 50 | + "config": { |
| 51 | + "PT_MAX_CARDS": { "type": "number", "default": 500 } |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## 4. Lifecycle Phases |
| 57 | + |
| 58 | +The plugin passes through defined stages handled by the Extension Host. |
| 59 | + |
| 60 | +**1. Discovery** |
| 61 | + |
| 62 | +The extension host scans `/plugins/*/plugin.json` and validates manifests. |
| 63 | +Invalid or incompatible plugins are skipped with logged warnings. |
| 64 | + |
| 65 | +**2. Validation** |
| 66 | + |
| 67 | +Manifest is validated using a JSON schema (zod or ajv). |
| 68 | +Compatibility ranges for api and engine are checked. |
| 69 | + |
| 70 | +**3. Initialization** |
| 71 | + |
| 72 | +The backend entry module is imported in a sandboxed VM or worker. |
| 73 | +The module must export an object implementing the SovereignPlugin interface. |
| 74 | + |
| 75 | +**4. Registration** |
| 76 | + |
| 77 | +The plugin’s register() method is called with a scoped SDK context: |
| 78 | + |
| 79 | +``` |
| 80 | +{ |
| 81 | + router, db, events, scheduler, config, caps, log |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Plugins register routes, events, jobs, and declare capabilities here. |
| 86 | + |
| 87 | +**5. Activation** |
| 88 | + |
| 89 | +Optional hook: `onEnable(tenantId)` |
| 90 | + |
| 91 | +Called when the plugin is enabled for a tenant. Used to seed data or initialize resources. |
| 92 | + |
| 93 | +**6. Migration** |
| 94 | + |
| 95 | +Optional hook: `onMigrate()` |
| 96 | + |
| 97 | +Runs when the plugin schema version changes or after install. Executes plugin-provided migrations through the Prisma migration runner. |
| 98 | + |
| 99 | +**7. Deactivation** |
| 100 | + |
| 101 | +Optional hook: `onDisable(tenantId)` |
| 102 | + |
| 103 | +Called when plugin is disabled or tenant deactivated. Used to clean temporary data or stop background jobs. |
| 104 | + |
| 105 | +**8. Shutdown** |
| 106 | + |
| 107 | +When Sovereign stops, the extension host calls `onShutdown()` if present, giving the plugin a chance to release resources gracefully. |
| 108 | + |
| 109 | +## 5. UI Contributions |
| 110 | + |
| 111 | +If the plugin includes a UI section, the manifest’s "ui" block declares routes and slot components. The front-end loader dynamically mounts these into the Handlebars/React SSR application. Slots allow plugins to inject UI fragments into predefined areas such as navbar, sidebar, or settings. |
| 112 | + |
| 113 | +## 6. Migrations and Databases |
| 114 | + |
| 115 | +Each plugin can ship its own Prisma schema and migrations directory. Migrations are applied per plugin, per tenant (if multi-tenancy enabled). The core migration runner maintains a migration registry table to ensure order and idempotency. |
| 116 | + |
| 117 | +## 7. Security Model |
| 118 | + |
| 119 | +- Plugins execute in isolated sandbox or worker contexts. |
| 120 | +- Only whitelisted SDK APIs are available. |
| 121 | +- Direct fs or network access is prohibited; storage and fetch go through provided service facades. |
| 122 | +- Plugin manifests can be signed or verified against an allow-list. |
| 123 | +- Capability declarations must match a known prefix (e.g., papertrail.\*). |
| 124 | +- Access checks enforced via caps.require() in backend routes. |
| 125 | + |
| 126 | +## 8. Versioning and Compatibility |
| 127 | + |
| 128 | +- The manifest’s `sovereign.api` and `sovereign.engine` define compatibility. |
| 129 | +- Deprecated fields remain supported for one minor version before removal. |
| 130 | +- Plugins should use semantic versioning and increment minor when changing interfaces. |
| 131 | + |
| 132 | +## 9. CLI Integration |
| 133 | + |
| 134 | +The CLI uses manifest metadata for management commands: |
| 135 | + |
| 136 | +``` |
| 137 | +sv plugins:list |
| 138 | +sv plugins:enable |
| 139 | +sv plugins:disable |
| 140 | +sv migrate:deploy –plugin |
| 141 | +sv plugin:info |
| 142 | +``` |
| 143 | + |
| 144 | +## 10. Error Handling and Logging |
| 145 | + |
| 146 | +Each plugin receives a scoped logger instance with context `{ pluginId, tenantId }`. |
| 147 | + |
| 148 | +Fatal errors during registration or migration disable the plugin and log diagnostics without halting the platform. |
0 commit comments