/** * Package-owned invariant companion for `dsh-tool-retriever`. * @module dsh-tool-retriever/invariant */ import type { Context } from '@deepseek-ai/cordis' const PACKAGE_NAME = 'dsh-tool-retriever' /** A package-attributed invariant failure reported by the host registry. */ type InvariantFailure = (message: string) => never /** Installer callback accepted by the host's invariant registry. */ type InvariantInstaller = (ctx: Context, fail: InvariantFailure) => void | Promise /** Minimal runtime contract used by the companion without a source checkout. */ interface InvariantRegistry { register(packageName: string, installer: InvariantInstaller): () => void } /** Cordis companion plugin name. */ export const name = 'tool-retriever-invariant' /** Service required before the companion can reserve package ownership. */ export const inject = ['invariants'] /** * No runtime invariant: this alpha computes each result directly from one live * registry snapshot and owns no cross-event or mutable-state relationship. */ const install: InvariantInstaller = () => {} /** * Resolve the host registry through Cordis's named service lookup. Keeping this * narrow local contract lets the template build without host source files; a * composed DSH profile still supplies the real `invariants` service. * @param ctx - Cordis context carrying the host service. * @returns the host invariant registry. * @throws {Error} when the companion is loaded without its host service. */ function getInvariantRegistry(ctx: Context): InvariantRegistry { const registry = ctx.get('invariants') as InvariantRegistry | undefined if (registry === undefined) { throw new Error(`invariant companion requires the "invariants" service for ${PACKAGE_NAME}`) } return registry } /** * Register this package's invariant companion. * @param ctx - Cordis context carrying the invariant service. * @returns the installed registration's disposer after setup succeeds. */ export const apply = (ctx: Context): Promise<() => void> => Promise.resolve(getInvariantRegistry(ctx).register(PACKAGE_NAME, install))