Files
TREK/server/src/nest-rpc/rpc-kit
Maurice c970245e0d fix(plugins): hold plugin data on Postgres to its own schema and to one contract on both engines
The plugin SQL guard now refuses what still reached outside a plugin's
schema or depended on the server:
- reads of the system columns (xmin, xmax, cmin, cmax, ctid, tableoid),
  which show transaction ids and storage layout and do not exist on
  SQLite; a value of the plugin's own may still be named xmin with AS
- the xid and xid8 types wherever the word stands for the type, since
  age() of an xid is the cluster's transaction counter
- the text search functions that coerce a text argument to regconfig, a
  catalog lookup by name, which leave the allowlist
- collations and access methods outside the set every server has, which
  could tell whether a locale or an extension exists

Every plugin transaction pins, with SET LOCAL, the settings that decide
how values print and parse (TimeZone UTC, DateStyle, the C locales, float,
bytea and xml output, the text search configuration), so a plugin's output
neither depends on nor reveals the server's configuration.

Both stores now keep one contract. Its texts and caps live once, in
plugin-data-contract.ts, and both stores throw only those: NUL in SQL,
an argument or a migration id is refused on SQLite as on Postgres,
transaction control is refused in every call on both engines (tx() is
how a batch becomes atomic), a closed store refuses every call, and the
Postgres store answers the cases better-sqlite3 names with its words.
The SQLite store moves beside the Postgres one as SqlitePluginDataStore
in db/engine/sqlite, takes its file from the backend so src/db never
imports src/nest, and no longer needs the driver exemption. A contract
test runs every violation on both stores and fails when they answer
differently. The Postgres lane holds the allowlist, the pinned settings
and the SQL plugins commonly write against a real database.
2026-11-18 13:20:07 +01:00
..

rpc-kit — the plugin RPC decorator layer

Conceptually this is trek-plugin-host: @PluginController, @PluginMethod, @PluginOpenMethod and @PluginHook, plus the registry that binds them into the per-plugin dispatch map. It lives in server/src/nest-rpc/, beside src/nest-mcp/ and outside every domain, so a domain's *.rpc.ts does not depend on the plugins domain. It is not its own workspace, and this file records why, and what extracting it later would cost.

What the decorators buy

The method-to-permission association used to exist twice: declaratively in METHOD_PERMISSION (src/nest/plugins/protocol/envelope.ts) and imperatively as the if (has('...')) blocks in src/nest/plugins/host/rpc-host.ts, with nothing asserting they agree. protocol-paths.test.ts checked that each method had some permission, never that it was the same permission the router gates on, and plugin-audit.ts's isAuditable silently inherits any drift.

A @PluginMethod declaration is checked against that table twice:

  • at compile time, because MethodPermission<M> resolves to the single literal the table assigns to M, so @PluginMethod('tags.list', { permission: 'db:write:tags' }) does not typecheck;
  • at boot, in registry.validate(), because types evaporate under an as cast or a hand-emitted JS consumer.

@PluginHook gets the same treatment against HOOK_PERMISSION.

The import rule

The kit may import:

  • @nestjs/common and @nestjs/core,
  • type-only from ../../nest/plugins/protocol/envelope and ../../nest/plugins/host/plugin-data.service.

Nothing else. It must not reach into rpc-host.ts, the deps factory, the supervisor, or any domain service. registry.ts is the single exception that imports envelope values (the tables it validates against), which is deliberate: the kit sits in the same package as its data source, so it can read it directly.

If this ever becomes its own package

nest-mcp was the worked example of the same layer as a separate workspace (it has since been folded into server/src/nest-mcp/, which retired the machinery below), and the delta is instructive. Because nest-mcp could not import the host, it needed a declare module augmentation for its registries plus two AssertExact drift guards in src/mcp/nest-mcp-policy.ts, and a permissive keyof X extends never ? string fallback that silently disables all checking if someone forgets to augment.

rpc-kit needs none of that today. Extracting it would mean:

  1. PluginRpcContext.data becomes a generic parameter instead of importing PluginDataStore.
  2. The four envelope type imports (KnownMethod, MethodPermission, HookKey, HookPermission) become empty-interface augmentation points, with AssertExact guards in the server proving the augmentation still matches the tables.
  3. Root package.json workspaces, server/package.json deps, the root lockfile, the Dockerfile COPY stages and a source alias in server/vitest.config.ts all change.

Note that a workspace would not let trek-plugin-sdk consume it: nest-mcp was "private": true, and so would this be. The SDK gets its copy of the permission facts from the generated plugin-sdk/src/generated/host-facts.ts instead.

Files

File What it is
metadata.ts WeakSet/WeakMap keyed by constructor. Deliberately not reflect-metadata, so a bare vitest worker can build a registry with no Nest app.
types.ts PluginRpcContext, the entry union, the option shapes.
decorators.ts The four decorators.
registry.ts register / validate / bindInto / hookContracts. Plain class, no DI.
registry.service.ts The DI-discovered subclass. Scans providers at boot and validates.
testing.ts createTestPluginRegistry, same fail-fast contract as the boot path.

The failure mode to know about

A @PluginController() class that is missing from its module's providers: [] is invisible to discovery. Its methods are simply never bound, and every call to them comes back PERMISSION_DENIED with no other symptom. The compiler cannot see this.

requireTotalCoverage is now on, so the app refuses to boot instead: validate() names every KNOWN_METHOD left without a handler, and every hook left without a host-side consumer. server/tests/unit/plugins/rpc-coverage.test.ts catches the same gap in CI, before anyone starts the server.