Skip to content

Commit 63d2ffd

Browse files
committed
fix(service-automation): reconcile connectors a plugin registered during kernel:ready, at kernel:bootstrapped
Claude-Session: https://claude.ai/code/session_017xRBcVBKAFk1ntXSjvPt7U Co-authored-by: Claude <noreply@anthropic.com>
1 parent e8f9beb commit 63d2ffd

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

  • packages/services/service-automation/src

‎packages/services/service-automation/src/plugin.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,26 @@ function connectorInstanceSignature(entry: {
688688
});
689689
}
690690

691+
/**
692+
* [#28591] The provider-bound instances a declaration asks the reconcile for,
693+
* as one comparable string: the name and {@link connectorInstanceSignature} of
694+
* every named, provider-bound entry not `enabled: false`, sorted. Two reads
695+
* with the same fingerprint ask for the same set of instances with the same
696+
* inputs. Credential and connection state are not in it: they are read by the
697+
* reconcile itself, not declared.
698+
*/
699+
function declaredInstanceFingerprint(declared: readonly unknown[]): string {
700+
const parts: string[] = [];
701+
for (const raw of declared) {
702+
const entry = raw as DeclaredConnectorItem | undefined;
703+
if (typeof entry?.name !== 'string' || entry.name.length === 0) continue;
704+
if (typeof entry.provider !== 'string' || entry.provider.length === 0) continue;
705+
if (entry.enabled === false) continue;
706+
parts.push(`${entry.name}=${connectorInstanceSignature(entry)}`);
707+
}
708+
return stableStringify(parts.sort());
709+
}
710+
691711
/**
692712
* The shape of a declarative `connectors:` stack entry as it sits in the
693713
* ObjectQL metadata registry (registered by `registerApp` under kind
@@ -1048,6 +1068,13 @@ export class AutomationServicePlugin implements Plugin {
10481068
* pre-edit one.
10491069
*/
10501070
private reloadedConnectors?: { items: unknown[]; owners: ReadonlySet<string> };
1071+
/**
1072+
* [#28591] The {@link declaredInstanceFingerprint} of the declaration the
1073+
* most recent reconcile read. The `kernel:bootstrapped` check compares the
1074+
* registry against it, so a boot whose registry did not change after
1075+
* `start()` runs no second reconcile.
1076+
*/
1077+
private reconciledDeclaration?: string;
10511078
/** Serializes reconcile runs — see {@link materializeDeclaredConnectors}. */
10521079
private reconcileQueue: Promise<void> = Promise.resolve();
10531080
private destroyed = false;
@@ -1956,6 +1983,20 @@ export class AutomationServicePlugin implements Plugin {
19561983
// An operational blip in one integration must not take the whole app down.
19571984
await this.materializeDeclaredConnectors(ctx, { fatal: true });
19581985

1986+
// ── [#28591] Connectors registered after start(), during the boot ─────
1987+
// The reconcile above reads the registry as `start()` leaves it. A
1988+
// package a plugin registers in its own `kernel:ready` handler arrives
1989+
// after it — the install-local rehydrate re-registers every installed
1990+
// package there — and announces no reload, so its connectors were never
1991+
// materialized until some unrelated reload. Its flows already bind: the
1992+
// `kernel:ready` flow sync below reads the protocol's view after the
1993+
// rehydrate. Connectors are read again at `kernel:bootstrapped`, which
1994+
// fires once every `kernel:ready` handler has settled, whatever the
1995+
// plugin order. See {@link reconcileConnectorsRegisteredDuringBoot}.
1996+
ctx.hook('kernel:bootstrapped', async () => {
1997+
await this.reconcileConnectorsRegisteredDuringBoot(ctx);
1998+
});
1999+
19592000
// ── Runtime re-bind: re-sync flow triggers on 'metadata:reloaded' ──────
19602001
// Fires on two RUNTIME events (never on a cold boot — the kernel:ready
19612002
// bind below covers that): a dev `os dev` artifact recompile (MetadataPlugin
@@ -2378,6 +2419,36 @@ export class AutomationServicePlugin implements Plugin {
23782419
* re-materialize. Degraded instances are retried with exponential backoff
23792420
* ({@link DECLARATIVE_RETRY_BASE_MS}) and on every reconcile.
23802421
*/
2422+
/**
2423+
* [#28591] Reconcile once more at `kernel:bootstrapped`, only when the
2424+
* registry's connector declaration differs from the one the last reconcile
2425+
* read — i.e. a plugin registered (or changed) a provider-bound entry after
2426+
* the boot reconcile in `start()`. A boot that registered nothing more runs
2427+
* no second reconcile.
2428+
*
2429+
* Reload semantics (`fatal: false`): an entry registered here arrives the
2430+
* way a hot install's does, so a configuration fault in it is logged and
2431+
* the entry skipped, as on install — it never fails the boot. A run that
2432+
* throws anyway is reported at `warn`: a connector that is not live is a
2433+
* functional degradation, visible on `GET /automation/connectors`.
2434+
*/
2435+
private reconcileConnectorsRegisteredDuringBoot(ctx: PluginContext): Promise<void> {
2436+
return this.onReconcileQueue(async () => {
2437+
if (!this.engine || this.destroyed) return;
2438+
const declared = await this.readDeclaredConnectorItems(ctx, { post: false });
2439+
if (!declared || declaredInstanceFingerprint(declared) === this.reconciledDeclaration) return;
2440+
try {
2441+
await this.reconcileDeclaredConnectors(ctx, { fatal: false });
2442+
} catch (err) {
2443+
ctx.logger.warn(
2444+
'[Automation] the connector reconcile for packages registered during the boot failed — their '
2445+
+ 'connector instances are not live until the next metadata reload or restart.',
2446+
describeThrownForLog(err),
2447+
);
2448+
}
2449+
});
2450+
}
2451+
23812452
private materializeDeclaredConnectors(ctx: PluginContext, opts: { fatal: boolean }): Promise<void> {
23822453
// Serialize runs: boot, `metadata:reloaded`, and the degraded-retry
23832454
// timer can all request a reconcile; concurrent runs would race the
@@ -2413,6 +2484,7 @@ export class AutomationServicePlugin implements Plugin {
24132484
// wrote after boot.
24142485
const declared = await this.readDeclaredConnectorItems(ctx, { post: !opts.fatal });
24152486
if (!declared) return; // no registry, no reloaded artifact — nothing declared
2487+
this.reconciledDeclaration = declaredInstanceFingerprint(declared);
24162488

24172489
// Report a reconcile problem: fatal (boot) throws; soft (reload) logs.
24182490
//

0 commit comments

Comments
 (0)