M3 plugin platform: SDK + loader, RLS-isolated tables, migrations, jobs
Backend foundation for the plugin platform (SPEC §11), Phases 1-3.
Phase 1 — SDK + loader:
- app/plugins/types.py: frozen Plugin/UiManifest/Slot/Page/JobSpec contract.
- app/plugins/sdk.py: sdk(name) facade — the only app.* import allowed for plugins
(scoped session, plugin.{name}.* publish, blob, outbox, settings, SSRF http_get, KV, enqueue).
- app/plugins/loader.py: entry-point discovery (webmail.plugins), hook forwarding
(API + worker), router mounting at /api/plugins/{name}, ui-manifest aggregation.
- plugin_kv core table (migration 0013) + GET /api/plugins/ui-manifest.
Phase 2 — non-removable core plugins + DB-enforced privacy:
- builtin flag (calendar/contacts will be builtin=True; uninstall refuses them).
- RLS per-user isolation on plg_* tables: least-privilege webmail_plugin role
(migration 0014), plugin_session(user_id) sets LOCAL ROLE + app.current_user_id,
rls.enable_rls() (ENABLE+FORCE RLS + USING/WITH CHECK policy). Mail untouched.
- Per-plugin Alembic runner (shared env, dedicated version table) + install/uninstall
--drop-data CLI; skeleton fixture proves install -> run -> uninstall --drop-data
leaves zero residue (§12 M3 acceptance).
Phase 3 — plugin jobs (§11.2):
- JobSpec registered as arq function plg.{plugin}.{func} + cron (5-field parser);
singleton=True guarded by a per-(plugin,user) Redis lock; wired onto the shared
interactive worker.
Gates: ruff, mypy --strict (94 files + tests), full suite 326 passed. RLS isolation,
migration round-trip, and singleton lock covered by tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rcj3TpydMTzTSmU7kunzf8
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""Plugin management CLI (SPEC §11.1).
|
||||
|
||||
python -m app.plugins.cli list
|
||||
python -m app.plugins.cli install <name>
|
||||
python -m app.plugins.cli uninstall <name> [--drop-data]
|
||||
|
||||
Builtin (core) plugins refuse uninstall.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
from app.plugins import loader, migrate
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="python -m app.plugins.cli", description="Manage webmail plugins"
|
||||
)
|
||||
sub = parser.add_subparsers(dest="cmd", required=True)
|
||||
sub.add_parser("list", help="list discovered plugins")
|
||||
p_install = sub.add_parser("install", help="apply a plugin's migrations")
|
||||
p_install.add_argument("name")
|
||||
p_uninstall = sub.add_parser("uninstall", help="remove a plugin (refuses builtin)")
|
||||
p_uninstall.add_argument("name")
|
||||
p_uninstall.add_argument(
|
||||
"--drop-data", action="store_true", help="also drop the plugin's tables"
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.cmd == "list":
|
||||
for plugin in loader.get_plugins():
|
||||
kind = "builtin" if plugin.builtin else "removable"
|
||||
migr = "migrations" if plugin.migrations else "no-migrations"
|
||||
print(f"{plugin.name}\t{plugin.version}\t{kind}\t{migr}")
|
||||
return 0
|
||||
|
||||
if args.cmd == "install":
|
||||
migrate.install(args.name)
|
||||
print(f"installed {args.name}")
|
||||
return 0
|
||||
|
||||
if args.cmd == "uninstall":
|
||||
try:
|
||||
migrate.uninstall(args.name, drop_data=args.drop_data)
|
||||
except loader.PluginRemovalError as exc:
|
||||
print(f"error: {exc}")
|
||||
return 1
|
||||
suffix = " (data dropped)" if args.drop_data else " (data kept)"
|
||||
print(f"uninstalled {args.name}{suffix}")
|
||||
return 0
|
||||
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user