@@ -22,8 +22,8 @@ import {
2222import { aiTestEmbedderActionHandler } from '@objectstack/service-settings' ;
2323import { RECORD_RESTORED_EVENT } from '@objectstack/spec/data' ;
2424import { stripReadDecorations } from '@objectstack/spec/kernel' ;
25- import { KnowledgeService } from './knowledge-service.js' ;
26- import type { KnowledgeLogger } from './knowledge-service.js' ;
25+ import { KnowledgeService , heldSourcesText } from './knowledge-service.js' ;
26+ import type { KnowledgeHeldReading , KnowledgeLogger } from './knowledge-service.js' ;
2727import {
2828 RECONCILE_DEFAULT_CRON ,
2929 RECONCILE_JOB ,
@@ -114,6 +114,22 @@ type EmbedderFromSettings =
114114 quiet : boolean ;
115115 } ;
116116
117+ /**
118+ * What `KnowledgeServicePlugin.provideEmbedder` provided: whether an embedder
119+ * is available now, and its boot line, said by the caller — not at all when
120+ * the service is held, whose one line says it instead.
121+ */
122+ interface EmbedderProvision {
123+ /** An embedder another plugin registered, or one the saved settings built. */
124+ available : boolean ;
125+ /** Why none is available (`available: false`), as operator text. */
126+ reason ?: string ;
127+ /** `available: false` only: nothing asked for an embedder (the line is `info`), or the saved settings build none (`warn`). */
128+ quiet ?: boolean ;
129+ /** Log the provision's own boot line. */
130+ say : ( ) => void ;
131+ }
132+
117133/**
118134 * The `IEmbedder` registered when the settings configure none. Every use of it
119135 * — `embed()` with any input, or reading `dimensions` to size a vector column —
@@ -424,6 +440,30 @@ export interface KnowledgeServicePluginOptions {
424440 * an adapter-contract member.
425441 */
426442 deploymentAdapter ?: string ;
443+ /**
444+ * [ADR-0161 K8, the dated note on item 12] The runtime composed this plugin
445+ * (`resolveCapabilityArgument` in `@objectstack/core`, beside
446+ * {@link deploymentAdapter}), so the service is enabled by configuration:
447+ * mounted is not enabled. At `kernel:bootstrapped` the boot reads, once,
448+ * whether an embedder is available (another plugin registered one, or the
449+ * saved `ai.embedder_*` settings build one) and whether the deployment's
450+ * store is mounted (an adapter of {@link deploymentAdapter}'s id is
451+ * registered). Both ⇒ the service serves, as one a host mounts always does.
452+ * Either missing ⇒ every source is held, whichever store it names: loaded
453+ * and listed to administrators, never built, synced, reconciled or searched;
454+ * no tool definition is handed out; no lifecycle reap guard is registered
455+ * and no store is called (`KnowledgeService.settleEnablement`). The state is
456+ * said once, in one line: `info` when only a shipped `knowledge_source`
457+ * mounted the service and nothing was saved; `warn` when `declared` (the
458+ * stack lists `requires: ['knowledge']`), when the saved embedder settings
459+ * do not build an embedder, or when the store is not mounted. Never a boot
460+ * refusal (ADR-0078 §4). A change takes effect at the next restart.
461+ *
462+ * Left unset (a host that mounts the plugin itself), nothing of this
463+ * applies: its adapters may embed on their own (K1), and the service serves
464+ * from the start, as it always has.
465+ */
466+ runtimeComposed ?: { declared : boolean } ;
427467 /**
428468 * The reconcile (ADR-0161 K1: "a reconcile pass keeps it correct";
429469 * `KnowledgeService.reconcileSources`): one job on the platform's job
@@ -528,8 +568,18 @@ export class KnowledgeServicePlugin implements Plugin {
528568 private syncQueue : IQueueService | undefined ;
529569 /** Whether init() bound a data engine to the service: the queued sync's worker re-reads through it. */
530570 private dataEngineBound = false ;
571+ /**
572+ * Whether the lifecycle reap guard may be registered ({@link syncReapGuards}):
573+ * from the start on a plugin a host mounted; on one the runtime composed,
574+ * only once the boot read the service as enabled (ADR-0161 K8, the dated
575+ * note on item 12, point 3: none while it is not). The lifecycle service has
576+ * no unregister, so registration waits for that reading.
577+ */
578+ private reapGuardsAllowed : boolean ;
531579
532- constructor ( private readonly options : KnowledgeServicePluginOptions = { } ) { }
580+ constructor ( private readonly options : KnowledgeServicePluginOptions = { } ) {
581+ this . reapGuardsAllowed = options . runtimeComposed === undefined ;
582+ }
533583
534584 async init ( ctx : PluginContext ) : Promise < void > {
535585 let engine : IDataEngine | undefined ;
@@ -580,6 +630,7 @@ export class KnowledgeServicePlugin implements Plugin {
580630 ...( syncState === undefined ? { } : { syncState } ) ,
581631 defaultTopK : this . options . defaultTopK ,
582632 ...( this . options . deploymentAdapter === undefined ? { } : { deploymentAdapter : this . options . deploymentAdapter } ) ,
633+ ...( this . options . runtimeComposed === undefined ? { } : { enabledByConfiguration : true } ) ,
583634 // The field gate's `security` service, resolved per search so plugin
584635 // order is free (plugin-security registers it in its own start()).
585636 // Only a GENUINE absence reads as "no security service" (the contract's
@@ -702,7 +753,17 @@ export class KnowledgeServicePlugin implements Plugin {
702753 await service . recordSources ( ) ;
703754 this . unsubscribes . push ( service . onSourcesChanged ( ( ) => { void service . recordSources ( ) ; } ) ) ;
704755 }
705- await this . provideEmbedder ( ctx ) ;
756+ const embedder = await this . provideEmbedder ( ctx ) ;
757+ // [ADR-0161 K8, the dated note on item 12] A service the runtime
758+ // composed is enabled by configuration, read here once per boot: an
759+ // embedder (just provided) and the deployment's store (registered in
760+ // its own start()). Not enabled ⇒ held, said in one line, and nothing
761+ // below runs for it.
762+ if ( this . options . runtimeComposed !== undefined && this . service ) {
763+ const held = this . settleEnablement ( ctx , this . service , embedder ) ;
764+ if ( held ) return ;
765+ }
766+ embedder . say ( ) ;
706767 // [ADR-0161 K7] The first build: every source a build is due for is
707768 // built once, in the background — after the embedder is provided,
708769 // since a build embeds, and never awaited, so the boot does not wait
@@ -720,7 +781,9 @@ export class KnowledgeServicePlugin implements Plugin {
720781 } ) ;
721782 // Every store registers its adapter in its own start(), so the reading of
722783 // which sources name an unmounted one is taken once all have started.
784+ // Held, the one line of the held state names what is missing instead.
723785 ctx . hook ( 'kernel:bootstrapped' , ( ) => {
786+ if ( this . service ?. heldReading ( ) ) return ;
724787 this . service ?. reportUnmountedAdapters ( ) ;
725788 } ) ;
726789 // [ADR-0161 K1] The daily reconcile. The job service may start after this
@@ -915,6 +978,8 @@ export class KnowledgeServicePlugin implements Plugin {
915978 * not stale anywhere: `debug` only.
916979 */
917980 private onPredicateWrite ( ctx : PluginContext , service : KnowledgeService , object : string , type : string , matched : unknown ) : void {
981+ // Held: nothing is synced or reconciled, which the boot line said once.
982+ if ( service . heldReading ( ) ) return ;
918983 const count = typeof matched === 'number' ? matched : 'an unreported number of' ;
919984 const head =
920985 `KnowledgeServicePlugin: '${ object } ' had a predicate write (${ type } ) affecting ${ count } record(s). ` +
@@ -971,7 +1036,8 @@ export class KnowledgeServicePlugin implements Plugin {
9711036 */
9721037 private async scheduleReconcile ( ctx : PluginContext ) : Promise < void > {
9731038 const service = this . service ;
974- if ( ! service ) return ;
1039+ // Held (ADR-0161 K8, the dated note on item 12): no reconcile job.
1040+ if ( ! service || service . heldReading ( ) ) return ;
9751041 const option = this . options . reconcile ;
9761042 if ( option === false ) {
9771043 ctx . logger . info ?.(
@@ -1040,7 +1106,7 @@ export class KnowledgeServicePlugin implements Plugin {
10401106 * settings page says so). Nothing is sent to a provider until the operator
10411107 * has saved one, and the API key is never logged.
10421108 */
1043- private async provideEmbedder ( ctx : PluginContext ) : Promise < void > {
1109+ private async provideEmbedder ( ctx : PluginContext ) : Promise < EmbedderProvision > {
10441110 let registered = true ;
10451111 let existing : unknown ;
10461112 try {
@@ -1050,32 +1116,80 @@ export class KnowledgeServicePlugin implements Plugin {
10501116 }
10511117 if ( registered ) {
10521118 const id = ( existing as { id ?: unknown } | undefined ) ?. id ;
1053- ctx . logger . info ?.(
1054- `KnowledgeServicePlugin: an embedder is already registered as '${ EMBEDDER_SERVICE } '` +
1055- `${ typeof id === 'string' ? ` (id=${ id } )` : '' } — kept; the ai.embedder_* settings are not applied.` ,
1056- ) ;
1057- return ;
1119+ return {
1120+ available : true ,
1121+ say : ( ) =>
1122+ ctx . logger . info ?.(
1123+ `KnowledgeServicePlugin: an embedder is already registered as '${ EMBEDDER_SERVICE } '` +
1124+ `${ typeof id === 'string' ? ` (id=${ id } )` : '' } — kept; the ai.embedder_* settings are not applied.` ,
1125+ ) ,
1126+ } ;
10581127 }
10591128
10601129 const outcome = await embedderFromSettings ( ctx ) ;
10611130 if ( outcome . kind === 'configured' ) {
10621131 ctx . registerService ( EMBEDDER_SERVICE , outcome . embedder ) ;
1063- ctx . logger . info ?.(
1064- `KnowledgeServicePlugin: registered '${ EMBEDDER_SERVICE } ' from the ai.embedder_* settings ` +
1065- `(provider=${ outcome . provider } , endpoint=${ outcome . endpointHost } , model=${ outcome . model ?? '(embedder default)' } , ` +
1066- `batch size=${ outcome . batchSize ?? "the model's own cap" } ` +
1067- `${ outcome . savedBatchSize !== undefined ? ` — the saved ${ outcome . savedBatchSize } , held to the model's documented cap` : '' } ). ` +
1068- 'A saved change takes effect on the next restart.' ,
1069- ) ;
1070- return ;
1132+ return {
1133+ available : true ,
1134+ say : ( ) =>
1135+ ctx . logger . info ?.(
1136+ `KnowledgeServicePlugin: registered '${ EMBEDDER_SERVICE } ' from the ai.embedder_* settings ` +
1137+ `(provider=${ outcome . provider } , endpoint=${ outcome . endpointHost } , model=${ outcome . model ?? '(embedder default)' } , ` +
1138+ `batch size=${ outcome . batchSize ?? "the model's own cap" } ` +
1139+ `${ outcome . savedBatchSize !== undefined ? ` — the saved ${ outcome . savedBatchSize } , held to the model's documented cap` : '' } ). ` +
1140+ 'A saved change takes effect on the next restart.' ,
1141+ ) ,
1142+ } ;
10711143 }
10721144
10731145 ctx . registerService ( EMBEDDER_SERVICE , new UnconfiguredEmbedder ( outcome . reason ) ) ;
10741146 const line =
10751147 `KnowledgeServicePlugin: no embedder is configured (${ outcome . reason } ). The knowledge service is mounted; ` +
10761148 'an adapter that embeds refuses indexing and search with this reason until an embedder is configured.' ;
1077- if ( outcome . quiet ) ctx . logger . info ?.( line ) ;
1149+ return {
1150+ available : false ,
1151+ reason : outcome . reason ,
1152+ quiet : outcome . quiet ,
1153+ say : ( ) => {
1154+ if ( outcome . quiet ) ctx . logger . info ?.( line ) ;
1155+ else ctx . logger . warn ?.( line ) ;
1156+ } ,
1157+ } ;
1158+ }
1159+
1160+ /**
1161+ * [ADR-0161 K8, the dated note on item 12] Read, once per boot, whether the
1162+ * service the runtime composed is enabled, and settle it
1163+ * (`KnowledgeService.settleEnablement`). Enabled ⇒ the reap guard may now
1164+ * be registered, and the boot goes on as for a service a host mounts.
1165+ * Not enabled ⇒ the one line that says so, at the level the note rules,
1166+ * and the answer `true`: the caller builds nothing.
1167+ */
1168+ private settleEnablement ( ctx : PluginContext , service : KnowledgeService , embedder : EmbedderProvision ) : boolean {
1169+ const store = this . options . deploymentAdapter ;
1170+ const storeMissing = store !== undefined && ! service . listAdapters ( ) . includes ( store ) ;
1171+ if ( embedder . available && ! storeMissing ) {
1172+ service . settleEnablement ( undefined ) ;
1173+ this . reapGuardsAllowed = true ;
1174+ if ( this . options . enableEventSync !== false ) this . syncReapGuards ( ctx , service ) ;
1175+ return false ;
1176+ }
1177+ const reading : KnowledgeHeldReading = {
1178+ ...( embedder . available ? { } : { embedder : embedder . reason } ) ,
1179+ ...( storeMissing ? { store } : { } ) ,
1180+ } ;
1181+ service . settleEnablement ( reading ) ;
1182+ const held = service . describeSources ( ) . map ( ( { id } ) => id ) ;
1183+ const line =
1184+ `KnowledgeServicePlugin: ${ heldSourcesText ( reading ) . replace ( / ^ \[ k n o w l e d g e \] / , '' ) } ` +
1185+ `${ held . length } source(s) held${ held . length > 0 ? `: ${ held . join ( ', ' ) } ` : '' } .` ;
1186+ // `info` only where a shipped item alone mounted the service and nothing
1187+ // was saved; a request, saved settings that build nothing, or a missing
1188+ // store is `warn`. Never a boot refusal (ADR-0078 §4).
1189+ const quiet = this . options . runtimeComposed ?. declared !== true && embedder . quiet !== false && ! storeMissing ;
1190+ if ( quiet ) ctx . logger . info ?.( line ) ;
10781191 else ctx . logger . warn ?.( line ) ;
1192+ return true ;
10791193 }
10801194
10811195 /**
@@ -1188,6 +1302,7 @@ export class KnowledgeServicePlugin implements Plugin {
11881302 * de-indexes nothing.
11891303 */
11901304 private syncReapGuards ( ctx : PluginContext , service : KnowledgeService ) : void {
1305+ if ( ! this . reapGuardsAllowed ) return ;
11911306 const objects = guardedObjectsFor ( service . indexedSources ( ) ) . filter ( ( object ) => ! this . guardedObjects . has ( object ) ) ;
11921307 if ( objects . length === 0 ) return ;
11931308
0 commit comments