Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/31908-p4b1-sealer-chain-isolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/plugin-audit': patch
'@objectstack/cli': patch
---

fix(plugin-audit, cli): the audit sealer skips only the chain a chain-local fault belongs to; `os serve` hands its crypto provider to an AuditPlugin the app composed with none

Clause-②: yes (widening)
Metadata-protocol: none

**The sealer (ADR-0154 D3, S4).** A fault that belongs to one chain of a sealed object — a row a canonical rule refuses, or a malformed checkpoint of that chain (its head among them) — now skips that chain for the rest of the pass instead of stopping the whole object. Every other chain is still sealed. Each chain now resumes from its own oldest unsealed window, as `window` checkpoints, so a skipped chain catches up once its fault is fixed. No checkpoint's `sealed_through` passes a window a skipped chain leaves unsealed. The sealer reports each skipped chain once at `error`, naming the object, the chain, the fault and the window it is unsealed from, and again each hour while it lasts. A lock lapse, a signer, crypto-provider or storage fault still stops the object's pass, as before. `os audit verify` reads the skipped chain's rows in a window another chain sealed as `late` and unsealed, and fails.

**An app-supplied AuditPlugin (ADR-0154 D2, D6).** An app that puts its own `AuditPlugin` in its stack `plugins` (to turn on `readAudit`, for example) supersedes the one `os serve` composes, and so lost the host's crypto provider: its sealer sealed nothing and said so at `error`. `os serve` now hands it the host's provider when the app composed it with no `cryptoProvider`, and keeps its sealer off on an `OS_MIGRATE_AND_EXIT=1` boot, as for the instance it composes itself. A provider the app composed is never replaced, an app's `armSealer: false` stays off, and a plugin that only carries the AuditPlugin name is never handed anything. The new `AuditPlugin.adoptHostCryptoProvider(provider, { armSealer })` is the method a host calls to do this.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ describe('os serve registers AuditPlugin with no config-derived options, above t
expect(SERVE).toMatch(/cap === 'audit' && hostCrypto\s*\?\s*\{ \.\.\.\(arg \?\? \{\}\), cryptoProvider: hostCrypto\.provider, armSealer: !migrateAndExit \}/);
});

it('hands the host crypto provider to an app-supplied AuditPlugin in the stack `plugins` loop, before the kernel takes it (seat ruling 6884540128 item 3)', () => {
// The app's `plugins` entry supersedes the CLI's instance (last one wins), so without this hand-over an app
// that composed its own AuditPlugin seals nothing. The same provider function, the same one-shot posture.
const HAND = 'handHostCryptoToAppAuditPlugin(pluginToLoad, RealAuditPlugin, () => hostCrypto?.provider, !migrateAndExit)';
const loop = soleOffset(SERVE, PLUGINS_LOOP, 'stack-plugins-loop');
const hand = soleOffset(SERVE, HAND, 'app AuditPlugin hand-over');
expect(hand).toBeGreaterThan(loop);
expect(hand).toBeLessThan(soleOffset(SERVE, 'await kernel.use(pluginToLoad);', 'stack-plugin registration'));
// Judged against the real class, loaded once ahead of the loop: never by name, so a lookalike is never fed.
expect(soleOffset(SERVE, '({ AuditPlugin: RealAuditPlugin } = await import(', 'real AuditPlugin class')).toBeLessThan(loop);
});

it('arms no sealer on an `OS_MIGRATE_AND_EXIT=1` boot, a one-shot job (ADR-0154 D3)', () => {
// One reading of the variable, ahead of both constructions; the exit reads the same constant.
const reading = "const migrateAndExit = process.env.OS_MIGRATE_AND_EXIT === '1';";
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { loadConfig, bundleConfigModule } from '../utils/config.js';
import { mergeBootConfig } from '../utils/merge-boot-config.js';
import { provisionTelemetryDatasource, standaloneTelemetryPrimary } from '../utils/telemetry-datasource.js';
import { isHostConfig, shouldBootWithLibrary } from '../utils/plugin-detection.js';
import { handHostCryptoToAppAuditPlugin } from '../utils/app-audit-plugin-host-crypto.js';
import { mountsAiServicePlugin, resolveAiServiceLoad, resolveOptionalPluginLoad } from '../utils/ai-service-auto-inject.js';
import { registerInstalledPlugins } from '../utils/installed-plugins-boot.js';
import { readInternalArtifactPath, readInternalConfigOutputPath } from '../utils/internal-artifact-channel.js';
Expand Down Expand Up @@ -4978,6 +4979,17 @@ export default class Serve extends Command {
// got that far), for `refuseUnloadedCryptoProviderPlugins` below.
const unloadedStackPlugins: UnloadedStackPlugin[] = [];
if (plugins.length > 0) {
// [ADR-0154 D2, D6; seat ruling 6884540128 item 3] The class an app's
// own AuditPlugin is judged by, loaded as this host loads it: only an
// instance of it is handed the host's crypto provider below, never a
// plugin that merely carries its name. Optional, as at 5d.
let RealAuditPlugin: typeof import('@objectstack/plugin-audit').AuditPlugin | undefined;
try {
const auditPkg = '@objectstack/plugin-audit';
({ AuditPlugin: RealAuditPlugin } = await import(/* webpackIgnore: true */ auditPkg));
} catch {
RealAuditPlugin = undefined;
}
let index = -1;
for (const plugin of plugins) {
index += 1;
Expand Down Expand Up @@ -5014,6 +5026,14 @@ export default class Serve extends Command {
// declared contract (`packages/core/src/plugin-registration.ts`),
// with a `warn` naming both versions. That is how an app supplies
// options to a plugin this CLI mounts without them.
//
// [ADR-0154 D2, D6; seat ruling 6884540128 item 3] An app's own
// AuditPlugin composed with no crypto provider is handed the
// host's — the same provider function, and the same one-shot
// posture, as the instance at 5d and the `requires: ['audit']`
// resolver — so superseding the CLI's instance does not leave the
// sealed objects unsealed. An explicit provider is never replaced.
handHostCryptoToAppAuditPlugin(pluginToLoad, RealAuditPlugin, () => hostCrypto?.provider, !migrateAndExit);
await kernel.use(pluginToLoad);
stackMountedPlugins.push(pluginToLoad);
const pluginName = plugin.name || plugin.constructor?.name || 'unnamed';
Expand Down
45 changes: 45 additions & 0 deletions packages/cli/src/utils/app-audit-plugin-host-crypto.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

import { describe, expect, it } from 'vitest';
import { AuditPlugin } from '@objectstack/plugin-audit';
import { handHostCryptoToAppAuditPlugin } from './app-audit-plugin-host-crypto.js';

/**
* [ADR-0154 D2, D6; seat ruling 6884540128 item 3] `os serve` hands its one
* crypto provider to an AuditPlugin the app put in its stack `plugins`: only
* to an instance of the real class, only when the app composed it with none.
*/
describe('os serve hands its crypto provider to an app-supplied AuditPlugin', () => {
const host = () => ({ label: 'host' }) as never;

it('hands it to a real AuditPlugin composed with no provider', () => {
const plugin = new AuditPlugin({ readAudit: { objects: [] } });
expect(handHostCryptoToAppAuditPlugin(plugin, AuditPlugin, host, true)).toBe(true);
expect((plugin as unknown as { hostCryptoProvider: unknown }).hostCryptoProvider).toBe(host);
});

it('never overrides a provider the app composed it with', () => {
const own = { label: 'own' } as never;
const plugin = new AuditPlugin({ cryptoProvider: own });
expect(handHostCryptoToAppAuditPlugin(plugin, AuditPlugin, host, true)).toBe(false);
expect((plugin as unknown as { hostCryptoProvider: unknown }).hostCryptoProvider).toBeUndefined();
});

it('never feeds a same-named lookalike, whatever it answers to', () => {
const calls: unknown[] = [];
const lookalike = { name: 'com.objectstack.audit', adoptHostCryptoProvider: (...args: unknown[]) => (calls.push(args), true) };
expect(handHostCryptoToAppAuditPlugin(lookalike, AuditPlugin, host, true)).toBe(false);
expect(calls).toEqual([]);
});

it('feeds nothing when the class could not be loaded, and never throws', () => {
expect(handHostCryptoToAppAuditPlugin(new AuditPlugin(), undefined, host, true)).toBe(false);
expect(handHostCryptoToAppAuditPlugin(undefined, AuditPlugin, host, true)).toBe(false);
});

it("keeps the sealer off on a one-shot boot (`armSealer: false`)", () => {
const plugin = new AuditPlugin();
expect(handHostCryptoToAppAuditPlugin(plugin, AuditPlugin, host, false)).toBe(true);
expect((plugin as unknown as { hostDisarmsSealer: boolean }).hostDisarmsSealer).toBe(true);
});
});
32 changes: 32 additions & 0 deletions packages/cli/src/utils/app-audit-plugin-host-crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

import type { AuditPlugin } from '@objectstack/plugin-audit';
import type { ICryptoProvider } from '@objectstack/spec/contracts';

/**
* [ADR-0154 D2, D6; seat ruling 6884540128 item 3] Hand `os serve`'s one
* crypto provider to an AuditPlugin the app put in its stack `plugins`.
*
* The app's entry supersedes the instance the CLI composes (the declared
* last-one-wins registration), and an app composing its own instance for
* `readAudit` rarely spells a crypto provider, so without this its sealer
* seals nothing. The hand-over matches the capability resolver's
* `requires: ['audit']` path: the same provider, and no sealer on a one-shot
* boot (`armSealer: false`).
*
* Only an instance of the real class (`RealAuditPlugin`, as this host loads
* it) is handed anything — never a plugin that merely carries its name — and
* only one the app composed with no provider of its own (the class's
* `adoptHostCryptoProvider` decides that). Nothing here throws or refuses the
* boot: an instance left without a provider reports that once, at `error`,
* from its sealer (ADR-0154 D3). Resolves whether the provider was handed.
*/
export function handHostCryptoToAppAuditPlugin(
plugin: unknown,
RealAuditPlugin: typeof AuditPlugin | undefined,
provider: () => ICryptoProvider | undefined,
armSealer: boolean,
): boolean {
if (RealAuditPlugin === undefined || !(plugin instanceof RealAuditPlugin)) return false;
return plugin.adoptHostCryptoProvider(provider, { armSealer });
}
44 changes: 44 additions & 0 deletions packages/plugins/plugin-audit/src/audit-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,47 @@ describe('AuditPlugin — sys_comment access gates are mounted', () => {
expect(denials[0].reason).toMatchObject({ code: 'RECORD_NOT_ACCESSIBLE', status: 403 });
});
});

describe('AuditPlugin — a host hands an app-supplied instance its crypto provider (ADR-0154 D2, D6; seat ruling 6884540128 item 3)', () => {
const crypto = (label: string) => ({ label }) as never;
/** The sealer `startSealer` composes over a storage double whose registry holds no sealed object: its pass seals nothing. */
const sealerOf = async (plugin: AuditPlugin): Promise<{ crypto?: unknown } | null> => {
const engine = { getDriverForObject: () => undefined, registry: { getAllObjects: () => [] } };
const ctx = { logger: { info() {}, warn() {}, error() {}, debug() {} }, getService: () => undefined };
(plugin as unknown as { startSealer(e: unknown, c: unknown): void }).startSealer(engine, ctx);
const sealer = (plugin as unknown as { sealer: { options: { crypto?: unknown }; stop(): Promise<void> } | null }).sealer;
await sealer?.stop();
return sealer && sealer.options;
};

it('takes the host provider when it was composed with none, and seals under it', async () => {
const plugin = new AuditPlugin({ readAudit: { objects: [] } });
const host = crypto('host');
expect(plugin.adoptHostCryptoProvider(() => host, { armSealer: true })).toBe(true);
expect((await sealerOf(plugin))?.crypto).toBe(host);
});

it('never overrides a provider the app composed it with', async () => {
const own = crypto('own');
const plugin = new AuditPlugin({ cryptoProvider: own });
expect(plugin.adoptHostCryptoProvider(() => crypto('host'), { armSealer: true })).toBe(false);
expect((await sealerOf(plugin))?.crypto).toBe(own);
});

it("keeps the sealer off on a host's one-shot boot, and an app's own `armSealer: false` stands", async () => {
const oneShot = new AuditPlugin();
expect(oneShot.adoptHostCryptoProvider(() => crypto('host'), { armSealer: false })).toBe(true);
expect(await sealerOf(oneShot)).toBeNull();
const off = new AuditPlugin({ armSealer: false });
expect(off.adoptHostCryptoProvider(() => crypto('host'), { armSealer: true })).toBe(true);
expect(await sealerOf(off)).toBeNull();
});

it('composes no provider when the host has none to hand: the sealer reports it once, at error (D3)', async () => {
const plugin = new AuditPlugin();
expect(plugin.adoptHostCryptoProvider(() => undefined, { armSealer: true })).toBe(true);
const sealer = await sealerOf(plugin);
expect(sealer).not.toBeNull();
expect(sealer?.crypto).toBeUndefined();
});
});
29 changes: 27 additions & 2 deletions packages/plugins/plugin-audit/src/audit-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,34 @@ export class AuditPlugin implements Plugin {
* at `kernel:ready`; the activity door's writer reads it at each call.
*/
private actorSnapshotOf: ((api: unknown, userId: string) => Promise<AuditActorSnapshot>) | undefined;
/** [ADR-0154 D2, D6] The provider a host handed over, for an instance composed with none. */
private hostCryptoProvider: (() => ICryptoProvider | undefined) | undefined;
/** [ADR-0154 D3] Set when a host that handed its provider over is a one-shot boot. */
private hostDisarmsSealer = false;

constructor(private readonly options: AuditPluginOptions = {}) {}

/**
* [ADR-0154 D2, D6; seat ruling 6884540128 item 3] A host hands an instance
* the app composed (a stack `plugins` entry) the host's own crypto provider,
* which the sealer then seals under. It is taken only when this instance was
* composed with no `cryptoProvider`: one the app composed is never
* overridden. `armSealer: false` — the host's one-shot boot — keeps the
* sealer off; `true` leaves this instance's own `armSealer` standing, so an
* app's `armSealer: false` is never turned on.
*
* The provider is read when the sealer starts (`kernel:ready`), so a host
* hands it over before then. Resolves whether it was taken. A provider that
* answers `undefined` is an absent one: the sealer reports it once, at
* `error`, and seals nothing; the boot is never refused.
*/
adoptHostCryptoProvider(provider: () => ICryptoProvider | undefined, host: { armSealer: boolean }): boolean {
if (this.options.cryptoProvider !== undefined) return false;
this.hostCryptoProvider = provider;
if (!host.armSealer) this.hostDisarmsSealer = true;
return true;
}

async init(ctx: PluginContext): Promise<void> {
// Register audit system objects via the manifest service.
ctx.getService<{ register(m: any): void }>('manifest').register({
Expand Down Expand Up @@ -768,11 +793,11 @@ export class AuditPlugin implements Plugin {
* command's boot (`armSealer: false`).
*/
private startSealer(engine: IDataEngine, ctx: PluginContext): void {
if (this.options.armSealer === false) return;
if (this.options.armSealer === false || this.hostDisarmsSealer) return;
const storage = engine as unknown as Partial<AuditSealerEngine>;
if (typeof storage.getDriverForObject !== 'function' || typeof storage.registry?.getAllObjects !== 'function') return;
const sealEngine = storage as AuditSealerEngine;
const option = this.options.cryptoProvider;
const option = this.options.cryptoProvider ?? this.hostCryptoProvider;
const crypto = typeof option === 'function' ? option() : option;
const localSigner = crypto ? new LocalCheckpointSigner({ engine: sealEngine, crypto }) : undefined;
const optional = <T>(name: string): T | undefined => {
Expand Down
Loading
Loading