Files
Ekklesia/packages/contracts/src/module-runtime.ts
T
ekklesiaandClaude Opus 5 b9ca0d560d
CI / verify (pull_request) Successful in 36s
CI / infrastructure-smoke (pull_request) Successful in 13s
CI / migration-smoke (pull_request) Failing after 2m20s
feat: add host-provided module object storage
`manifest.storage.buckets` has existed since manifest version 1 and meant
nothing — discovery validated it and no code read it. This turns it into a
real capability, which is what stands between Announcements and the module
system: that product currently fetches a hardcoded LAN S3 endpoint, parses
ListObjectsV2 XML by hand, and hands the browser direct object URLs.

- A module declares a logical bucket and receives a scoped facade at
  `context.storage` — list, head, get, put, delete. The contract names no
  provider: no endpoint, region, credential, or physical bucket is reachable
  from it, which is what lets the same module run under a future standalone
  host over its own or a shared S3-compatible service.
- Core's first provider speaks S3 through the AWS SDK. SeaweedFS is the
  deployment target (ADR 0004), never an API name. Probing the pinned image
  found `CreateBucket` reporting `BucketAlreadyExists` rather than AWS's
  `BucketAlreadyOwnedByYou`; treating only the AWS spelling as success would
  have failed every second boot. A live test pins it.
- Logical → physical mapping is deterministic and collision-safe: an
  installation prefix, the module and bucket ids joined by `--` (which cannot
  occur inside either identifier, so `a-b`+`c` and `a`+`b-c` cannot collide),
  and a digest over the full untruncated identity. Two modules declaring
  `assets` always get separate namespaces; neither can see the physical name.
- Declaring a bucket is now a requirement: with no provider configured the
  module is `blocked · storage_unavailable` through ADR 0028's existing
  resolver, propagating to required dependents. An installation with no object
  storage stays a complete deployment.
- Binding availability is not provider health. Eligibility asks whether
  storage is configured, never whether it answered; bucket creation is lazy on
  first use so a storage outage cannot stop the platform booting; a running
  module whose store blinks stays running and retries. Nothing deletes a
  bucket.
- Credentials are deployment-supplied, never persisted, never routed through
  module configuration, and never visible to modules, module UI, diagnostics,
  or API responses. Provider errors translate to a closed code set carrying no
  endpoint, bucket, request id, or XML.
- Retires ADR 0029's transitional `bucket` configuration key. No manifest
  version change. The template declares no bucket, so it still runs without S3.

See ADR 0030.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-23 08:45:53 -05:00

121 lines
5.2 KiB
TypeScript

import type { ModuleConfiguration } from "./module-configuration.js";
import type { ModuleStorage } from "./module-storage.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 scoped object storage (ADR 0030).
*
* Present here and on HTTP handler contexts but **not** on `ModuleRegistrationContext`, exactly
* like `services`: this is a live facade that performs network I/O, and registration stays
* declarative and I/O-free. `config` is on registration precisely because it is the opposite — an
* already-resolved in-memory snapshot.
*
* Scoped per module and never shared: it resolves only the logical buckets this module's own
* manifest declared, and carries no endpoint, credential, or physical bucket name.
*/
readonly storage: ModuleStorage;
/**
* 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 } : {}),
});
}