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]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
fdaad58848
commit
baa689b2a7
@@ -1,9 +1,14 @@
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import type { ModuleDatabase, ModuleRuntimeServices } from "@ekklesia/contracts";
|
||||
import type {
|
||||
ModuleConfiguration,
|
||||
ModuleDatabase,
|
||||
ModuleRuntimeServices,
|
||||
} from "@ekklesia/contracts";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { discoverModules } from "../src/modules/discovery.js";
|
||||
import { resolveModuleEligibility } from "../src/modules/lifecycle.js";
|
||||
import {
|
||||
ModuleDiscoveryError,
|
||||
ModuleRuntimeError,
|
||||
@@ -52,7 +57,7 @@ function manifestFor(
|
||||
} = {},
|
||||
): Record<string, unknown> {
|
||||
return {
|
||||
manifestVersion: 4,
|
||||
manifestVersion: 5,
|
||||
id,
|
||||
name: `${id} module`,
|
||||
version: "0.1.0",
|
||||
@@ -1351,3 +1356,148 @@ describe("module startup rollback and bounded stop hooks", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("module configuration delivery (ADR 0029)", () => {
|
||||
it("hands each module its own frozen configuration in register, start, and stop", async () => {
|
||||
const modulesDirectory = await createModulesDirectory();
|
||||
const recorder = createReferenceRecorder();
|
||||
await createRuntimeModule(modulesDirectory, "announcements", {
|
||||
manifest: {
|
||||
...manifestFor("announcements"),
|
||||
configuration: [
|
||||
{ key: "slideSeconds", type: "integer", default: 7 },
|
||||
{ key: "bucket", type: "string", required: true },
|
||||
],
|
||||
},
|
||||
serverSource: `export default {
|
||||
id: "announcements", version: "0.1.0",
|
||||
register(context) { ${recorder.expression}.push(context.config); },
|
||||
start(context) { ${recorder.expression}.push(context.config); },
|
||||
stop(context) { ${recorder.expression}.push(context.config); }
|
||||
};`,
|
||||
});
|
||||
|
||||
const discovered = await discoverModules(runtimeOptions(modulesDirectory));
|
||||
const resolution = resolveModuleEligibility(
|
||||
discovered,
|
||||
{ modules: [] },
|
||||
{ settings: { records: [{ key: "bucket", moduleId: "announcements", value: "slides" }] } },
|
||||
);
|
||||
const runtime = await startDiscoveredModuleRuntime(
|
||||
resolution.eligibleModules,
|
||||
fakeModuleRuntimeServices(),
|
||||
{ configurations: resolution.configurations },
|
||||
);
|
||||
await runtime.stop();
|
||||
|
||||
expect(recorder.values).toHaveLength(3);
|
||||
const [registerConfig, startConfig, stopConfig] = recorder.values as ModuleConfiguration[];
|
||||
// Configuration is available during `register` too — it is an already-resolved in-memory
|
||||
// snapshot, not a database-backed getter, so it reintroduces no I/O into a hook that must stay
|
||||
// declarative.
|
||||
expect(registerConfig?.getInteger("slideSeconds")).toBe(7);
|
||||
expect(registerConfig?.getString("bucket")).toBe("slides");
|
||||
// The exact same object across every hook, so two hooks can never disagree about a value.
|
||||
expect(startConfig).toBe(registerConfig);
|
||||
expect(stopConfig).toBe(registerConfig);
|
||||
expect(Object.isFrozen(registerConfig)).toBe(true);
|
||||
});
|
||||
|
||||
it("gives each module only its own configuration, never another module's", async () => {
|
||||
const modulesDirectory = await createModulesDirectory();
|
||||
const recorder = createReferenceRecorder();
|
||||
for (const id of ["alpha", "bravo"]) {
|
||||
await createRuntimeModule(modulesDirectory, id, {
|
||||
manifest: {
|
||||
...manifestFor(id),
|
||||
configuration: [{ key: "label", type: "string", required: true }],
|
||||
},
|
||||
serverSource: `export default {
|
||||
id: ${JSON.stringify(id)}, version: "0.1.0",
|
||||
start(context) { ${recorder.expression}.push([context.moduleId, context.config.getString("label"), context.config.keys()]); }
|
||||
};`,
|
||||
});
|
||||
}
|
||||
|
||||
const discovered = await discoverModules(runtimeOptions(modulesDirectory));
|
||||
const resolution = resolveModuleEligibility(
|
||||
discovered,
|
||||
{ modules: [] },
|
||||
{
|
||||
settings: {
|
||||
records: [
|
||||
{ key: "label", moduleId: "alpha", value: "alpha-value" },
|
||||
{ key: "label", moduleId: "bravo", value: "bravo-value" },
|
||||
],
|
||||
},
|
||||
},
|
||||
);
|
||||
const runtime = await startDiscoveredModuleRuntime(
|
||||
resolution.eligibleModules,
|
||||
fakeModuleRuntimeServices(),
|
||||
{ configurations: resolution.configurations },
|
||||
);
|
||||
await runtime.stop();
|
||||
|
||||
expect(recorder.values).toEqual([
|
||||
["alpha", "alpha-value", ["label"]],
|
||||
["bravo", "bravo-value", ["label"]],
|
||||
]);
|
||||
});
|
||||
|
||||
it("supplies an empty configuration, never undefined, to a module that declares none", async () => {
|
||||
const modulesDirectory = await createModulesDirectory();
|
||||
const recorder = createReferenceRecorder();
|
||||
await createRuntimeModule(modulesDirectory, "plain", {
|
||||
serverSource: `export default {
|
||||
id: "plain", version: "0.1.0",
|
||||
start(context) { ${recorder.expression}.push([typeof context.config, context.config.keys()]); }
|
||||
};`,
|
||||
});
|
||||
|
||||
const runtime = await startModuleRuntime(runtimeOptions(modulesDirectory));
|
||||
await runtime.stop();
|
||||
|
||||
// Module code never has to branch on whether the platform supplied a configuration.
|
||||
expect(recorder.values).toEqual([["object", []]]);
|
||||
});
|
||||
|
||||
it("keeps the services object one shared identity even though configuration is per-module", async () => {
|
||||
const modulesDirectory = await createModulesDirectory();
|
||||
const services = fakeModuleRuntimeServices();
|
||||
const recorder = createReferenceRecorder();
|
||||
for (const id of ["alpha", "bravo"]) {
|
||||
await createRuntimeModule(modulesDirectory, id, {
|
||||
manifest: {
|
||||
...manifestFor(id),
|
||||
configuration: [{ key: "label", type: "string", default: id }],
|
||||
},
|
||||
serverSource: `export default {
|
||||
id: ${JSON.stringify(id)}, version: "0.1.0",
|
||||
start(context) { ${recorder.expression}.push(context.services); ${recorder.expression}.push(context.config); }
|
||||
};`,
|
||||
});
|
||||
}
|
||||
|
||||
const discovered = await discoverModules(runtimeOptions(modulesDirectory, services));
|
||||
const resolution = resolveModuleEligibility(
|
||||
discovered,
|
||||
{ modules: [] },
|
||||
{ settings: { records: [] } },
|
||||
);
|
||||
const runtime = await startDiscoveredModuleRuntime(resolution.eligibleModules, services, {
|
||||
configurations: resolution.configurations,
|
||||
});
|
||||
await runtime.stop();
|
||||
|
||||
const [alphaServices, alphaConfig, bravoServices, bravoConfig] = recorder.values;
|
||||
// This is the reason configuration is a sibling of `services` rather than a field inside it:
|
||||
// `services` is deliberately one shared object for the whole runtime, while configuration must
|
||||
// differ per module. Both properties hold simultaneously.
|
||||
expect(alphaServices).toBe(services);
|
||||
expect(bravoServices).toBe(services);
|
||||
expect(alphaConfig).not.toBe(bravoConfig);
|
||||
expect((alphaConfig as ModuleConfiguration).getString("label")).toBe("alpha");
|
||||
expect((bravoConfig as ModuleConfiguration).getString("label")).toBe("bravo");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user