Files
Ekklesia/packages/contracts/src/module-runtime.ts
T
ekklesiaandClaude Opus 5 baa689b2a7
CI / verify (pull_request) Successful in 35s
CI / infrastructure-smoke (pull_request) Successful in 21s
CI / migration-smoke (pull_request) Successful in 46s
feat: add the module configuration and secrets contract
Modules had no way to be told anything. Announcements — the first real
product Ekklesia will absorb — has zero environment variables and compiles
a LAN IP address into its source, so this is the contract standing between
it and the module system.

- A module declares what it needs in its manifest, using six bounded types
  (string, integer, boolean, enum, url, secret) with no nesting and no
  regular expressions. Static declaration is what lets the platform validate
  configuration *before* deciding whether a module may start: an enabled
  module missing a required value is reported `blocked ·
  configuration_missing · <key>` rather than taking startup down from inside
  its own `start` hook, and Doctor reports the same while still importing no
  module code. That forces `manifestVersion: 5`, a hard cutover.
- Ordinary values persist in one Core table (migration 0005), one row per
  (module_id, key), stored as text until validated against the declaring
  module's current manifest. Nothing is coerced; an invalid stored value
  blocks the module rather than falling back to the default. A key the
  manifest no longer declares is retained and ignored.
- Secrets are never persisted. There is no encryption at rest in this
  repository, so a `secret` key is deployment-supplied and mapped in by the
  host under a name the module never sees. A secret declaration has no
  `default` field, the write API refuses secret material, and the HTTP view
  has no `value` field for one — absent by construction, not filtered.
- Modules receive a frozen `context.config` on the registration, runtime,
  and HTTP handler contexts: the same object identity, per module, beside
  `services` rather than inside it (services is deliberately one shared
  instance). No environment access, no database reference, no way to reach
  another module's values.
- Configuration decides eligibility through ADR 0028's existing resolver:
  three new blocked reasons, each with a distinct operator fix, propagating
  to required dependents through the same mechanism as a disabled module.
- Changes take effect at the next restart; both resources report
  restartRequired. New restricted permission
  `core.modules.configuration.manage`, separate from lifecycle, at the
  sibling prefix `/api/v1/module-configuration`.
- Migration participation stays independent of configuration validity.
  Endpoints and credentials for shared infrastructure stay platform
  concerns; a module declares which bucket it needs, not an S3 endpoint.

See ADR 0029.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-22 21:44:43 -05:00

108 lines
4.5 KiB
TypeScript

import type { ModuleConfiguration } from "./module-configuration.js";
import type { CapabilityId, ModuleId } from "./identifiers.js";
import { moduleIdSchema } from "./identifiers.js";
import type { ModuleRuntimeServices } from "./module-database.js";
import type { ModuleHttpRouteRegistrar } from "./module-http.js";
import { semanticVersionSchema } from "./versions.js";
export type Awaitable<Value> = Value | PromiseLike<Value>;
export interface RegisteredCapability {
readonly id: CapabilityId;
readonly implementation: unknown;
readonly moduleId: ModuleId;
readonly version: string;
}
export interface CapabilityResolver {
resolve(capabilityId: CapabilityId): RegisteredCapability | undefined;
}
export interface CapabilityRegistrar extends CapabilityResolver {
provide(capabilityId: CapabilityId, implementation: unknown): void;
}
export interface ModuleRegistrationContext {
readonly capabilities: CapabilityRegistrar;
/** Available only during `register` — never on `ModuleRuntimeContext` — so a route can only ever
* be declared while Core is still building the immutable startup snapshot, never added or removed
* afterward from `start`, `stop`, or any other post-registration code path. */
readonly http: ModuleHttpRouteRegistrar;
readonly moduleId: ModuleId;
/**
* This module's own validated configuration, available here as well as at runtime (ADR 0029).
*
* Present during `register` deliberately, and without reopening the rule that registration stays
* declarative and I/O-free: this is an already-resolved frozen snapshot held in memory, not a
* database-backed getter, so reading it performs no query and cannot fail. That is exactly why
* `services` — which *is* a live database facade — remains absent from this context.
*/
readonly config: ModuleConfiguration;
}
/** The same context shape for both `start` and `stop`: whichever hook runs, it receives the exact
* same frozen `services` object identity as the other, for a given API runtime — never a fresh
* `ModuleRuntimeServices` built per hook invocation. Unlike `ModuleRegistrationContext`, this never
* carries `http`: a route can only ever be declared during `register`. */
export interface ModuleRuntimeContext {
readonly capabilities: CapabilityResolver;
readonly moduleId: ModuleId;
readonly services: ModuleRuntimeServices;
/**
* This module's own validated configuration, frozen for the lifetime of the process.
*
* Deliberately **not** a field on `ModuleRuntimeServices`: that object is one shared instance
* every module receives, and configuration is per-module by definition. It sits beside `moduleId`
* — the context's other per-module fact — so the scoping is visible in the shape rather than
* asserted in a comment.
*
* Immutable for the process lifetime: a configuration change is applied by the next restart, so a
* value read during `start` and the same value read in a request handler an hour later are the
* same value (ADR 0029).
*/
readonly config: ModuleConfiguration;
}
export interface ModuleRuntimeDefinition {
readonly id: ModuleId;
readonly register?: (context: ModuleRegistrationContext) => Awaitable<void>;
readonly start?: (context: ModuleRuntimeContext) => Awaitable<void>;
readonly stop?: (context: ModuleRuntimeContext) => Awaitable<void>;
readonly version: string;
}
const runtimeDefinitionKeys = new Set(["id", "register", "start", "stop", "version"]);
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function isModuleRuntimeDefinition(value: unknown): value is ModuleRuntimeDefinition {
if (
!isRecord(value) ||
!moduleIdSchema.safeParse(value.id).success ||
!semanticVersionSchema.safeParse(value.version).success
) {
return false;
}
if (Object.keys(value).some((key) => !runtimeDefinitionKeys.has(key))) {
return false;
}
return [value.register, value.start, value.stop].every(
(hook) => hook === undefined || typeof hook === "function",
);
}
export function parseModuleRuntimeDefinition(value: unknown): ModuleRuntimeDefinition {
if (!isModuleRuntimeDefinition(value)) {
throw new TypeError("Invalid Ekklesia module runtime definition.");
}
return Object.freeze({
id: value.id,
version: value.version,
...(value.register ? { register: value.register } : {}),
...(value.start ? { start: value.start } : {}),
...(value.stop ? { stop: value.stop } : {}),
});
}