Add a decorator kit for the plugin RPC surface

The method-to-permission association exists twice today: declaratively in
METHOD_PERMISSION, and imperatively as 44 if (has(...)) blocks in rpc-host.ts,
with nothing asserting the two agree. protocol-paths.test.ts checks that each
method has some permission, never that it is the same one the router gates on,
and plugin-audit.ts inherits any drift silently.

@PluginMethod binds the two together. Its permission argument is typed as
MethodPermission<M>, so it resolves to the single literal the table assigns to
that method and a mismatch does not compile. validate() re-checks the same
argument against the same table at boot, because types evaporate under an as
cast. @PluginHook does the same against HOOK_PERMISSION.

UNCONDITIONAL_METHODS was missing from envelope.ts, so the three methods the
router registers outside every grant check had no vocabulary of their own.
Adding it here, with a compile-time proof that it stays disjoint from
KNOWN_METHODS: giving those three a METHOD_PERMISSION row would change which
methods isAuditable considers, and settings.get is deliberately the one
unconditional method that is not audited.

The kit is written to stay extractable into its own package, so an ESLint rule
pins what it may import. Note the rule is a deny list on purpose: these patterns
are gitignore-style, and a leading ** would exclude the parent directory of
every allowed path, which makes the ! re-includes silently ineffective.
This commit is contained in:
Maurice
2026-08-09 11:35:29 +02:00
committed by Maurice
parent 8c5a207a9b
commit 7b11680cc3
15 changed files with 1453 additions and 0 deletions
@@ -0,0 +1,78 @@
# 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 inside `server/` rather than in 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` (`../../protocol/envelope.ts`) and imperatively as the
`if (has('...'))` blocks in `../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 `../../protocol/envelope` and `../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` is the worked example of the same layer as a separate workspace, and the
delta is instructive. Because `nest-mcp` must not import the host, it needs 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
`PluginDataDb`.
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` is
`"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.
What catches it is `server/tests/unit/plugins/rpc-coverage.test.ts`, and, once the
rollout is finished, `requireTotalCoverage: true` turning it into a boot failure.