@wildo-ai/saas-workers-node-lib 1.1.11 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/inject/models/container.d.ts.map +1 -1
- package/dist/esm/inject/models/container.js +1 -2
- package/dist/esm/inject/models/container.js.map +1 -1
- package/dist/esm/inject/models/service-types.d.ts.map +1 -1
- package/dist/esm/inject/models/service-types.js.map +1 -1
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.d.ts +24 -3
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.d.ts.map +1 -1
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.js +84 -20
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.js.map +1 -1
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.d.ts.map +1 -1
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.js.map +1 -1
- package/dist/esm/runtime/services/define-worker-init.d.ts +5 -2
- package/dist/esm/runtime/services/define-worker-init.d.ts.map +1 -1
- package/dist/esm/runtime/services/define-worker-init.js.map +1 -1
- package/dist/esm/runtime/services/read-worker-bootstrap-env.d.ts.map +1 -1
- package/dist/esm/runtime/services/read-worker-bootstrap-env.js.map +1 -1
- package/dist/esm/runtime/services/read-worker-http-surface-env.d.ts.map +1 -1
- package/dist/esm/runtime/services/read-worker-http-surface-env.js.map +1 -1
- package/dist/esm/runtime/services/start-worker-application.backend.service.d.ts +31 -20
- package/dist/esm/runtime/services/start-worker-application.backend.service.d.ts.map +1 -1
- package/dist/esm/runtime/services/start-worker-application.backend.service.js +45 -10
- package/dist/esm/runtime/services/start-worker-application.backend.service.js.map +1 -1
- package/dist/esm/runtime/services/worker-http-server.backend.service.d.ts +6 -5
- package/dist/esm/runtime/services/worker-http-server.backend.service.d.ts.map +1 -1
- package/dist/esm/runtime/services/worker-http-server.backend.service.js +6 -6
- package/dist/esm/runtime/services/worker-http-server.backend.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +7 -7
- package/src/index.ts +1 -2
- package/src/inject/models/container.ts +1 -2
- package/src/runtime/middlewares/__tests__/worker-jwt-verifier.rotation-cutoff.middleware.test.ts +104 -9
- package/src/runtime/middlewares/worker-jwt-verifier.middleware.ts +90 -22
- package/src/runtime/services/__tests__/start-worker-application.backend.service.test.ts +115 -2
- package/src/runtime/services/define-worker-init.ts +5 -2
- package/src/runtime/services/start-worker-application.backend.service.ts +61 -21
- package/src/runtime/services/worker-http-server.backend.service.ts +8 -5
|
@@ -11,22 +11,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
11
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
12
|
};
|
|
13
13
|
import { inject, injectable } from 'inversify';
|
|
14
|
-
import { getOtelSdkHandle, SAAS_SERVICE_TYPES, WildoWorkerBootstrapClientService, } from '@wildo-ai/saas-backend-lib';
|
|
14
|
+
import { describeCaughtError, getOtelSdkHandle, recordBackendStartOutcome, SAAS_SERVICE_TYPES, WildoWorkerBootstrapClientService, } from '@wildo-ai/saas-backend-lib';
|
|
15
|
+
import { BackendStartOutcome } from '@wildo-ai/platform-core-shared';
|
|
16
|
+
import { narrowRuntimeProviderDataResidency } from '@wildo-ai/saas-models';
|
|
15
17
|
import { assertProviderRuntimeEnvInSync, } from '@wildo-ai/platform-config-lib';
|
|
16
18
|
import { SAAS_WORKERS_NODE_SERVICE_TYPES } from '../../inject/models/service-types.js';
|
|
17
19
|
import { WorkerHttpServerBackendService } from './worker-http-server.backend.service.js';
|
|
18
20
|
/**
|
|
19
|
-
* Worker entrypoint
|
|
20
|
-
* `.claude/plans/minions-and-workers-implementation.md`: a standalone
|
|
21
|
+
* Worker entrypoint: a standalone
|
|
21
22
|
* runtime kernel that does NOT consume the backend-api's heavy bootstrap
|
|
22
|
-
* (no `AppConfigurationService`, no Mongo / Redis / RabbitMQ).
|
|
23
|
-
*
|
|
24
|
-
* `bootstrap-base` steps:
|
|
23
|
+
* (no `AppConfigurationService`, no Mongo / Redis / RabbitMQ). The worker
|
|
24
|
+
* only inherits these `bootstrap-base` steps:
|
|
25
25
|
* - Env validation
|
|
26
26
|
* - Apps-manager handshake (limited to fetching the trusted
|
|
27
27
|
* `APPLICATION_WORKER_REQUEST` public key)
|
|
28
28
|
* - Final validation gate
|
|
29
|
-
* - SIGTERM handler (graceful shutdown
|
|
29
|
+
* - SIGTERM handler (graceful shutdown)
|
|
30
30
|
*
|
|
31
31
|
* Plus the two worker-specific steps:
|
|
32
32
|
* - HTTP server + controllers (with JWT verifier middleware)
|
|
@@ -57,7 +57,28 @@ let StartWorkerApplicationBackendService = class StartWorkerApplicationBackendSe
|
|
|
57
57
|
this.workerBootstrapClient = workerBootstrapClient;
|
|
58
58
|
this.workerHttpServer = workerHttpServer;
|
|
59
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Start the worker, and record how the start ended (#3288).
|
|
62
|
+
*
|
|
63
|
+
* The record is the backend's (`recordBackendStartOutcome`, under `APPLICATION_RUNTIME_SIGNALS_HOME/backend-start/`,
|
|
64
|
+
* filed as `workers.<name>`): `started` once the HTTP server listens, `failed` with the classified cause otherwise.
|
|
65
|
+
* A port another process holds is `port-in-use` with the port, read from the server's `EADDRINUSE`. The dev
|
|
66
|
+
* supervisor (`wildo-dev-runtime serve`) looks the record up by pid and names the holder, stops an orphan of this
|
|
67
|
+
* package or pauses on a foreign one, instead of crash-looping into `CRASH_RESPAWN_EXHAUSTED`. Nothing is written
|
|
68
|
+
* when the variable is absent, which is every non-local deployment.
|
|
69
|
+
*/
|
|
60
70
|
async initialize(input) {
|
|
71
|
+
const serviceIdentifier = `workers.${input.config.workerName}`;
|
|
72
|
+
try {
|
|
73
|
+
await this.initializeWorker(input);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
recordBackendStartOutcome({ outcome: BackendStartOutcome.FAILED, serviceIdentifier, message: describeCaughtError(error), failure: error });
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
recordBackendStartOutcome({ outcome: BackendStartOutcome.STARTED, serviceIdentifier });
|
|
80
|
+
}
|
|
81
|
+
async initializeWorker(input) {
|
|
61
82
|
const { config, applicationId, platformApplicationPrimarySecret } = input;
|
|
62
83
|
this.providerRuntime = this.normalizeProviderRuntime(config.providerRuntime, config.workerName);
|
|
63
84
|
// Boot-time fail-closed reconciliation between the typed
|
|
@@ -107,11 +128,16 @@ let StartWorkerApplicationBackendService = class StartWorkerApplicationBackendSe
|
|
|
107
128
|
publicKey: entry.publicKey,
|
|
108
129
|
...(entry.notAfter ? { notAfter: entry.notAfter } : {}),
|
|
109
130
|
})),
|
|
131
|
+
// #2895 — a request naming a key this worker has not fetched yet (the first ones after a rotation) fetches the
|
|
132
|
+
// bootstrap again; the middleware bounds how often. A failed fetch keeps the previous bootstrap.
|
|
133
|
+
refreshKeysForUnknownKid: async () => {
|
|
134
|
+
this.bootstrapResponse = await this.workerBootstrapClient.getBootstrap({ force: true });
|
|
135
|
+
},
|
|
110
136
|
});
|
|
111
137
|
this.workerHttpServer.markReady();
|
|
112
138
|
this.isInitialized = true;
|
|
113
139
|
// Audit A-11 + A-3: install the worker's own graceful-shutdown handler (the JSDoc's "SIGTERM
|
|
114
|
-
// handler
|
|
140
|
+
// handler", previously claimed but absent). See `registerGracefulShutdown`.
|
|
115
141
|
this.registerGracefulShutdown();
|
|
116
142
|
}
|
|
117
143
|
/**
|
|
@@ -145,8 +171,8 @@ let StartWorkerApplicationBackendService = class StartWorkerApplicationBackendSe
|
|
|
145
171
|
}
|
|
146
172
|
/**
|
|
147
173
|
* Install the worker's SIGTERM/SIGINT graceful-shutdown handler — the JSDoc's "SIGTERM handler
|
|
148
|
-
*
|
|
149
|
-
* worker, dropping any final telemetry). The worker is deliberately queue-free
|
|
174
|
+
* (graceful shutdown)" that audit A-11 found was claimed but absent (a signal would hard-kill the
|
|
175
|
+
* worker, dropping any final telemetry). The worker is deliberately queue-free, so it does
|
|
150
176
|
* NOT use the RabbitMQ-coupled `WorkerShutdownManager`; instead it runs its own cleanup
|
|
151
177
|
* (`shutdown()`) and flushes OTel telemetry before exit — the worker-local analogue of the
|
|
152
178
|
* platform hosts' note-6 flush. `process.once` keeps the host killable (the handler exits).
|
|
@@ -276,6 +302,9 @@ let StartWorkerApplicationBackendService = class StartWorkerApplicationBackendSe
|
|
|
276
302
|
// sibling's opt-in must not load a package into this process.
|
|
277
303
|
const projectedAugmentations = Object.entries(providerRuntime.providerRuntimeAugmentations ?? {})
|
|
278
304
|
.filter(([providerRef]) => projectedProviderRefs.includes(providerRef));
|
|
305
|
+
// Narrowed with the refs too (#3513): the region a sibling's provider is bound to is not this host's to apply.
|
|
306
|
+
const projectedProcessingRegions = Object.entries(providerRuntime.providerProcessingRegions ?? {})
|
|
307
|
+
.filter(([providerRef]) => projectedProviderRefs.includes(providerRef));
|
|
279
308
|
return {
|
|
280
309
|
__AUTOGENERATED__: providerRuntime.__AUTOGENERATED__,
|
|
281
310
|
generatedBy: providerRuntime.generatedBy,
|
|
@@ -284,6 +313,12 @@ let StartWorkerApplicationBackendService = class StartWorkerApplicationBackendSe
|
|
|
284
313
|
providerRefs: projectedProviderRefs,
|
|
285
314
|
providerRuntimeImports: projectedProviderRuntimeImports,
|
|
286
315
|
...(projectedAugmentations.length === 0 ? {} : { providerRuntimeAugmentations: Object.fromEntries(projectedAugmentations) }),
|
|
316
|
+
...(projectedProcessingRegions.length === 0 ? {} : { providerProcessingRegions: Object.fromEntries(projectedProcessingRegions) }),
|
|
317
|
+
// Narrowed with the refs too (#2294), and never dropped: a projection that lost it would load this host's providers
|
|
318
|
+
// without the residency the application declared, which is the one thing the loader's re-check exists to prevent.
|
|
319
|
+
...(providerRuntime.providerDataResidency === undefined
|
|
320
|
+
? {}
|
|
321
|
+
: { providerDataResidency: narrowRuntimeProviderDataResidency(providerRuntime.providerDataResidency, projectedProviderRefs) }),
|
|
287
322
|
providerGraphDigest: providerRuntime.providerGraphDigest,
|
|
288
323
|
lockfileSectionDigest: providerRuntime.lockfileSectionDigest,
|
|
289
324
|
artifactKind: 'backend',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"start-worker-application.backend.service.js","sourceRoot":"","sources":["../../../../../src/runtime/services/start-worker-application.backend.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EACL,gBAAgB,EAEhB,kBAAkB,EAClB,iCAAiC,GAClC,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,8BAA8B,GAE/B,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,+BAA+B,EAAE,MAAM,mCAAmC,CAAC;AAKpF,OAAO,EAAE,8BAA8B,EAAE,MAAM,sCAAsC,CAAC;AAEtF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEI,IAAM,oCAAoC,GAA1C,MAAM,oCAAoC;IAS5B;IAEA;IAVX,iBAAiB,CAA8B;IAC/C,eAAe,CAA2B;IAC1C,YAAY,CAAkB;IAC9B,aAAa,GAAG,KAAK,CAAC;IACtB,wBAAwB,GAAG,KAAK,CAAC;IAEzC,YAEmB,qBAAwD,EAExD,gBAAgD;QAFhD,0BAAqB,GAArB,qBAAqB,CAAmC;QAExD,qBAAgB,GAAhB,gBAAgB,CAAgC;IAChE,CAAC;IAEG,KAAK,CAAC,UAAU,CAAC,KAWvB;QACC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,gCAAgC,EAAE,GAAG,KAAK,CAAC;QAC1E,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,wBAAwB,CAClD,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,UAAU,CAClB,CAAC;QAEF,yDAAyD;QACzD,yDAAyD;QACzD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Ae,CAAC,4BAA4B,IAAI,EAAE,CAAC;aAC9F,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,EAAE,CAAC,qBAAqB,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC;QAE1E,OAAO;YACL,iBAAiB,EAAE,eAAe,CAAC,iBAAiB;YACpD,WAAW,EAAE,eAAe,CAAC,WAAW;YACxC,WAAW,EAAE,eAAe,CAAC,WAAW;YACxC,YAAY,EAAE,eAAe,CAAC,YAAY;YAC1C,YAAY,EAAE,qBAAqB;YACnC,sBAAsB,EAAE,+BAA+B;YACvD,GAAG,CAAC,sBAAsB,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,4BAA4B,EAAE,MAAM,CAAC,WAAW,CAAC,sBAAsB,CAAC,EAAE,CAAC;YAC5H,mBAAmB,EAAE,eAAe,CAAC,mBAAmB;YACxD,qBAAqB,EAAE,eAAe,CAAC,qBAAqB;YAC5D,YAAY,EAAE,SAAS;YACvB,OAAO,EAAE,eAAe,CAAC,OAAO;YAChC,KAAK;SACN,CAAC;IACJ,CAAC;CACF,CAAA;AA9SY,oCAAoC;IADhD,UAAU,EAAE;IASR,WAAA,MAAM,CAAC,kBAAkB,CAAC,iCAAiC,CAAC,CAAA;IAE5D,WAAA,MAAM,CAAC,+BAA+B,CAAC,uBAAuB,CAAC,CAAA;qCADxB,iCAAiC;QAEtC,8BAA8B;GAXxD,oCAAoC,CA8ShD","sourcesContent":["import { inject, injectable } from 'inversify';\nimport {\n getOtelSdkHandle,\n type ProviderRuntimeArtifact,\n SAAS_SERVICE_TYPES,\n WildoWorkerBootstrapClientService,\n} from '@wildo-ai/saas-backend-lib';\nimport {\n assertProviderRuntimeEnvInSync,\n type AppWorkerBootstrapResponse,\n} from '@wildo-ai/platform-config-lib';\nimport { SAAS_WORKERS_NODE_SERVICE_TYPES } from '../../inject/models/service-types';\nimport type {\n WorkerInitializationConfig,\n WorkerProviderRuntimeArtifact,\n} from './define-worker-init';\nimport { WorkerHttpServerBackendService } from './worker-http-server.backend.service';\n\n/**\n * Worker entrypoint per Q1 LOCKED of\n * `.claude/plans/minions-and-workers-implementation.md`: a standalone\n * runtime kernel that does NOT consume the backend-api's heavy bootstrap\n * (no `AppConfigurationService`, no Mongo / Redis / RabbitMQ). Per the\n * Phase 1.5 mapping doc Table A, the worker only inherits these\n * `bootstrap-base` steps:\n * - Env validation\n * - Apps-manager handshake (limited to fetching the trusted\n * `APPLICATION_WORKER_REQUEST` public key)\n * - Final validation gate\n * - SIGTERM handler (graceful shutdown per G-18 + KD-7)\n *\n * Plus the two worker-specific steps:\n * - HTTP server + controllers (with JWT verifier middleware)\n * - K8s liveness writer (HTTP `/healthz` per `WildoWorkerConfig.http.healthPath`)\n *\n * Shipped behaviour:\n * 1. Initializes `WildoWorkerBootstrapClientService` and prefetches the\n * trusted public key.\n * 2. Schedules the JWKS-style refresh loop; the cached bootstrap response\n * feeds the JWT verifier on every inbound request, so a rotation\n * rolls in without restarting the server.\n * 3. Installs the lightweight `WorkerHttpServerBackendService` (express +\n * helmet, no CORS, no websockets), mounts liveness/readiness probes\n * unauthenticated, and gates everything else behind the\n * `APPLICATION_WORKER_REQUEST` JWT verifier middleware.\n * 4. Marks the readiness probe ready once the bootstrap response is\n * loaded.\n */\n@injectable()\nexport class StartWorkerApplicationBackendService {\n private bootstrapResponse?: AppWorkerBootstrapResponse;\n private providerRuntime?: ProviderRuntimeArtifact;\n private refreshTimer?: NodeJS.Timeout;\n private isInitialized = false;\n private signalHandlersRegistered = false;\n\n constructor(\n @inject(SAAS_SERVICE_TYPES.WildoWorkerBootstrapClientService)\n private readonly workerBootstrapClient: WildoWorkerBootstrapClientService,\n @inject(SAAS_WORKERS_NODE_SERVICE_TYPES.WorkerHttpServerService)\n private readonly workerHttpServer: WorkerHttpServerBackendService,\n ) {}\n\n public async initialize(input: {\n config: WorkerInitializationConfig;\n applicationId: string;\n /**\n * `PLATFORM_APPLICATION_PRIMARY_SECRET` from the four-env\n * bootstrap-env-contract — required so the worker's\n * `WildoWorkerBootstrapClientService` can authenticate against the\n * apps-manager `apps-to-manager/workers/:workerName/bootstrap`\n * endpoint as APP_TO_PLATFORM.\n */\n platformApplicationPrimarySecret: string;\n }): Promise<void> {\n const { config, applicationId, platformApplicationPrimarySecret } = input;\n this.providerRuntime = this.normalizeProviderRuntime(\n config.providerRuntime,\n config.workerName,\n );\n\n // Boot-time fail-closed reconciliation between the typed\n // `wildo-provider-runtime.generated.ts` artifact and the\n // `.wildo-providers.env` file mounted into this process. Catches\n // stale-sync drift before the registry filter would silently boot\n // with mismatched topology.\n //\n // Skipped when:\n // - the artifact is absent (worker authored zero providers AND no\n // scope block in `wildo.saas.config.ts`); or\n // - the artifact has zero `providerRefs` (worker authored an\n // explicit empty scope `providers: {}`). The CLI emitter\n // deliberately does NOT write `.wildo-providers.env` in that\n // case, so the env-vars are absent — running the assertion\n // would raise a false-positive fail-closed.\n // Defensive `?? []` — the schema marks `providerRefs` optional, so\n // a hand-crafted artifact (test fixture / future format drift)\n // could omit it. Without the fallback `.length` would TypeError\n // before fail-closed produced its actionable message. Production\n // CLI emit always defaults to `[]`, so this is hardening only.\n const projectedProviderRefs = this.providerRuntime?.providerRefs ?? [];\n if (this.providerRuntime && projectedProviderRefs.length > 0) {\n assertProviderRuntimeEnvInSync({\n artifactProviderRefs: projectedProviderRefs,\n artifactSourceDigest: this.providerRuntime.sourceDigest,\n hostKind: 'node-worker',\n hostKey: config.workerName,\n });\n }\n\n this.workerBootstrapClient.initialize({\n appsManagerBaseUrl: config.appsManagerBootstrapUrl,\n applicationId,\n workerName: config.workerName,\n platformApplicationPrimarySecret,\n refreshIntervalMs: config.publicKeyRefreshIntervalMs,\n });\n // The startup read tolerates apps-manager restarting; the refresh loop below keeps a single attempt.\n this.bootstrapResponse = await this.workerBootstrapClient.getBootstrapAtStartup();\n this.scheduleRefresh(config.publicKeyRefreshIntervalMs);\n\n await this.workerHttpServer.initialize({\n config,\n providerRuntime: this.providerRuntime,\n getCurrentPublicKey: () => this.requireBootstrap().jwt.publicKey,\n getRotatedPublicKeys: () =>\n (this.requireBootstrap().jwt.rotatedPublicKeys ?? []).map((entry) => ({\n kid: entry.kid,\n publicKey: entry.publicKey,\n ...(entry.notAfter ? { notAfter: entry.notAfter } : {}),\n })),\n });\n this.workerHttpServer.markReady();\n this.isInitialized = true;\n\n // Audit A-11 + A-3: install the worker's own graceful-shutdown handler (the JSDoc's \"SIGTERM\n // handler per G-18 + KD-7\", previously claimed but absent). See `registerGracefulShutdown`.\n this.registerGracefulShutdown();\n }\n\n /**\n * Returns the trusted public key + rotation metadata. The HTTP server's\n * verifier middleware already reads this on every request via the\n * `getCurrentPublicKey` / `getRotatedPublicKeys` callbacks; this getter\n * exists so test code and the worker's own route handlers can inspect\n * the active rotation state.\n */\n public getBootstrapResponse(): AppWorkerBootstrapResponse {\n return this.requireBootstrap();\n }\n\n /**\n * Returns the generated, backend-shaped provider runtime selected for the\n * current worker. Undefined means the worker was started without a generated\n * provider artifact (valid for provider-free workers).\n */\n public getProviderRuntime(): ProviderRuntimeArtifact | undefined {\n return this.providerRuntime;\n }\n\n public async shutdown(gracePeriodMs?: number): Promise<void> {\n this.workerHttpServer.markNotReady();\n await this.workerHttpServer.shutdown(gracePeriodMs);\n if (this.refreshTimer) {\n clearInterval(this.refreshTimer);\n this.refreshTimer = undefined;\n }\n this.bootstrapResponse = undefined;\n this.providerRuntime = undefined;\n this.isInitialized = false;\n }\n\n /**\n * Install the worker's SIGTERM/SIGINT graceful-shutdown handler — the JSDoc's \"SIGTERM handler\n * per G-18 + KD-7\" that audit A-11 found was claimed but absent (a signal would hard-kill the\n * worker, dropping any final telemetry). The worker is deliberately queue-free (KD-3), so it does\n * NOT use the RabbitMQ-coupled `WorkerShutdownManager`; instead it runs its own cleanup\n * (`shutdown()`) and flushes OTel telemetry before exit — the worker-local analogue of the\n * platform hosts' note-6 flush. `process.once` keeps the host killable (the handler exits).\n * Idempotent.\n */\n private registerGracefulShutdown(): void {\n if (this.signalHandlersRegistered) {\n return;\n }\n this.signalHandlersRegistered = true;\n process.once('SIGTERM', () => void this.gracefulShutdown('SIGTERM'));\n process.once('SIGINT', () => void this.gracefulShutdown('SIGINT'));\n }\n\n private async gracefulShutdown(signal: string): Promise<void> {\n // `console` is intentional — this runtime kernel injects no LoggerService.\n // eslint-disable-next-line no-console\n console.log(`[worker] received ${signal}, shutting down gracefully…`);\n // Bounded force-exit so a stuck drain/flush can never wedge the process (mirrors the platform\n // hosts' 5s budget). `unref()` keeps the timer from holding the loop open on its own.\n const forceExit = setTimeout(() => process.exit(1), 5_000);\n forceExit.unref();\n try {\n await this.shutdown();\n // Audit A-3: flush the final OTel trace/log batch before exit. `process.exit` skips the\n // preload's `beforeExit` backstop, so without this the worker's last batch is lost on a\n // signal. No-op when telemetry is off (the global handle is undefined). Fail-open — a flush\n // error must never turn a clean shutdown into a non-zero exit.\n await getOtelSdkHandle()\n ?.shutdown()\n .catch(() => {\n // best-effort flush\n });\n process.exit(0);\n } catch {\n process.exit(1);\n }\n }\n\n private requireBootstrap(): AppWorkerBootstrapResponse {\n if (!this.isInitialized || !this.bootstrapResponse) {\n throw new Error(\n 'StartWorkerApplicationBackendService.getBootstrapResponse() called before initialize() resolved.',\n );\n }\n return this.bootstrapResponse;\n }\n\n private scheduleRefresh(intervalMs?: number): void {\n if (this.refreshTimer) {\n clearInterval(this.refreshTimer);\n }\n if (intervalMs === undefined || intervalMs <= 0) {\n return;\n }\n this.refreshTimer = setInterval(() => {\n void this.workerBootstrapClient\n .getBootstrap({ force: true })\n .then((response) => {\n this.bootstrapResponse = response;\n })\n .catch(() => {\n // Refresh failures keep the previously-cached key live so the\n // verifier does not flap; the next interval re-attempts.\n });\n }, intervalMs);\n if (typeof this.refreshTimer.unref === 'function') {\n this.refreshTimer.unref();\n }\n }\n\n /**\n * Bridge the per-worker materialized provider runtime artifact into the\n * `ProviderRuntimeArtifact` shape consumed by `StartApplicationBackendService`.\n *\n * Three accepted input shapes — see `StartMinionApplicationBackendService`\n * for the same pattern. Summary:\n *\n * 1. `{ artifactKind: 'backend', scope: ... }` — already projected.\n * 2. `{ artifactKind: 'nodeWorkers', scope: ..., scopes: undefined }` —\n * pre-scoped per-worker module emitted by `wildo config sync`.\n * 3. `{ artifactKind: 'nodeWorkers', scopes: { [name]: ... } }` —\n * multi-scope artifact (legacy / non-CLI hosts).\n */\n private normalizeProviderRuntime(\n providerRuntime: WorkerInitializationConfig['providerRuntime'],\n workerName: string,\n ): ProviderRuntimeArtifact | undefined {\n if (!providerRuntime) {\n return undefined;\n }\n if (providerRuntime.artifactKind === 'backend') {\n return providerRuntime;\n }\n const artifactKind = providerRuntime.artifactKind;\n if (artifactKind !== 'nodeWorkers') {\n throw new Error(\n `Unsupported provider runtime artifactKind '${artifactKind}' for worker '${workerName}'.`,\n );\n }\n\n return this.selectWorkerProviderRuntimeScope(providerRuntime, workerName);\n }\n\n private selectWorkerProviderRuntimeScope(\n providerRuntime: WorkerProviderRuntimeArtifact,\n workerName: string,\n ): ProviderRuntimeArtifact {\n const preScopedScope = (providerRuntime as WorkerProviderRuntimeArtifact & {\n scope?: ProviderRuntimeArtifact['scope'];\n }).scope;\n const scopes = providerRuntime.scopes as\n | Record<string, ProviderRuntimeArtifact['scope']>\n | undefined;\n\n let scope: ProviderRuntimeArtifact['scope'] | undefined;\n\n if (preScopedScope && (!scopes || Object.keys(scopes).length === 0)) {\n scope = preScopedScope;\n } else if (scopes) {\n scope = scopes[workerName];\n if (!scope) {\n throw new Error(\n `Provider runtime artifact does not contain a scope for worker '${workerName}'. `\n + `Re-run \\`wildo config sync\\` and verify the worker is declared in `\n + `wildo.saas.config.ts \\`providers.scopes.workers\\`.`,\n );\n }\n }\n\n if (!scope) {\n throw new Error(\n `Provider runtime artifact for worker '${workerName}' is missing both a `\n + `pre-scoped \\`scope\\` and a multi-scope \\`scopes\\` map. The artifact is malformed.`,\n );\n }\n\n // Recompute `providerRefs` from the projected per-host scope, NOT\n // from the multi-scope union the parent artifact carries. The\n // `buildMultiScopeArtifactMetadata` upstream computes\n // `providerRefs` as the union across all hosts; preserving that\n // here would make the boot-time `assertProviderRuntimeEnvInSync`\n // guard treat sibling-scope refs as \"this host has providers\" and\n // mistakenly run reconciliation on hosts whose own scope is empty.\n // Mirrors `selectScopedProviderRuntimeArtifact` in\n // `provider-runtime-artifacts.ts`.\n const projectedProviderRefs = Object.keys(scope.providers ?? {}).sort();\n // Narrowed with the refs, for the same reason (#330): the import map on a multi-scope artifact\n // is the union across every host, and handing this host a sibling's package specifiers would\n // tell it that it may load something it never declared.\n const projectedProviderRuntimeImports = Object.fromEntries(\n Object.entries(\n providerRuntime.providerRuntimeImports ?? {},\n ).filter(([providerRef]) => projectedProviderRefs.includes(providerRef)),\n );\n // Narrowed with the refs as well (#1672): a host applies augmentations only to providers it declares, and a\n // sibling's opt-in must not load a package into this process.\n const projectedAugmentations = Object.entries(providerRuntime.providerRuntimeAugmentations ?? {})\n .filter(([providerRef]) => projectedProviderRefs.includes(providerRef));\n\n return {\n __AUTOGENERATED__: providerRuntime.__AUTOGENERATED__,\n generatedBy: providerRuntime.generatedBy,\n generatedAt: providerRuntime.generatedAt,\n sourceDigest: providerRuntime.sourceDigest,\n providerRefs: projectedProviderRefs,\n providerRuntimeImports: projectedProviderRuntimeImports,\n ...(projectedAugmentations.length === 0 ? {} : { providerRuntimeAugmentations: Object.fromEntries(projectedAugmentations) }),\n providerGraphDigest: providerRuntime.providerGraphDigest,\n lockfileSectionDigest: providerRuntime.lockfileSectionDigest,\n artifactKind: 'backend',\n modules: providerRuntime.modules,\n scope,\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"start-worker-application.backend.service.js","sourceRoot":"","sources":["../../../../src/runtime/services/start-worker-application.backend.service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,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AAE,CAAC;YAC5H,GAAG,CAAC,0BAA0B,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,yBAAyB,EAAE,MAAM,CAAC,WAAW,CAAC,0BAA0B,CAAC,EAAE,CAAC;YACjI,oHAAoH;YACpH,kHAAkH;YAClH,GAAG,CAAC,eAAe,CAAC,qBAAqB,KAAK,SAAS;gBACrD,CAAC,CAAC,EAAE;gBACJ,CAAC,CAAC,EAAE,qBAAqB,EAAE,kCAAkC,CAAC,eAAe,CAAC,qBAAqB,EAAE,qBAAqB,CAAC,EAAE,CAAC;YAChI,mBAAmB,EAAE,eAAe,CAAC,mBAAmB;YACxD,qBAAqB,EAAE,eAAe,CAAC,qBAAqB;YAC5D,YAAY,EAAE,SAAS;YACvB,OAAO,EAAE,eAAe,CAAC,OAAO;YAChC,KAAK;SACN,CAAC;IACJ,CAAC;CACF,CAAA;AAtUY,oCAAoC;IADhD,UAAU,EAAE;IASR,WAAA,MAAM,CAAC,kBAAkB,CAAC,iCAAiC,CAAC,CAAA;IAE5D,WAAA,MAAM,CAAC,+BAA+B,CAAC,uBAAuB,CAAC,CAAA;qCADxB,iCAAiC;QAEtC,8BAA8B;GAXxD,oCAAoC,CAsUhD","sourcesContent":["import { inject, injectable } from 'inversify';\nimport {\n describeCaughtError,\n getOtelSdkHandle,\n type ProviderRuntimeArtifact,\n recordBackendStartOutcome,\n SAAS_SERVICE_TYPES,\n WildoWorkerBootstrapClientService,\n} from '@wildo-ai/saas-backend-lib';\nimport { BackendStartOutcome } from '@wildo-ai/platform-core-shared';\nimport { narrowRuntimeProviderDataResidency } from '@wildo-ai/saas-models';\nimport {\n assertProviderRuntimeEnvInSync,\n type AppWorkerBootstrapResponse,\n} from '@wildo-ai/platform-config-lib';\nimport { SAAS_WORKERS_NODE_SERVICE_TYPES } from '../../inject/models/service-types';\nimport type {\n WorkerInitializationConfig,\n WorkerProviderRuntimeArtifact,\n} from './define-worker-init';\nimport { WorkerHttpServerBackendService } from './worker-http-server.backend.service';\n\n/** What {@link StartWorkerApplicationBackendService.initialize} takes. */\nexport interface WorkerApplicationInitializationInput {\n readonly config: WorkerInitializationConfig;\n readonly applicationId: string;\n /**\n * `PLATFORM_APPLICATION_PRIMARY_SECRET` from the four-env\n * bootstrap-env-contract — required so the worker's\n * `WildoWorkerBootstrapClientService` can authenticate against the\n * apps-manager `apps-to-manager/workers/:workerName/bootstrap`\n * endpoint as APP_TO_PLATFORM.\n */\n readonly platformApplicationPrimarySecret: string;\n}\n\n/**\n * Worker entrypoint: a standalone\n * runtime kernel that does NOT consume the backend-api's heavy bootstrap\n * (no `AppConfigurationService`, no Mongo / Redis / RabbitMQ). The worker\n * only inherits these `bootstrap-base` steps:\n * - Env validation\n * - Apps-manager handshake (limited to fetching the trusted\n * `APPLICATION_WORKER_REQUEST` public key)\n * - Final validation gate\n * - SIGTERM handler (graceful shutdown)\n *\n * Plus the two worker-specific steps:\n * - HTTP server + controllers (with JWT verifier middleware)\n * - K8s liveness writer (HTTP `/healthz` per `WildoWorkerConfig.http.healthPath`)\n *\n * Shipped behaviour:\n * 1. Initializes `WildoWorkerBootstrapClientService` and prefetches the\n * trusted public key.\n * 2. Schedules the JWKS-style refresh loop; the cached bootstrap response\n * feeds the JWT verifier on every inbound request, so a rotation\n * rolls in without restarting the server.\n * 3. Installs the lightweight `WorkerHttpServerBackendService` (express +\n * helmet, no CORS, no websockets), mounts liveness/readiness probes\n * unauthenticated, and gates everything else behind the\n * `APPLICATION_WORKER_REQUEST` JWT verifier middleware.\n * 4. Marks the readiness probe ready once the bootstrap response is\n * loaded.\n */\n@injectable()\nexport class StartWorkerApplicationBackendService {\n private bootstrapResponse?: AppWorkerBootstrapResponse;\n private providerRuntime?: ProviderRuntimeArtifact;\n private refreshTimer?: NodeJS.Timeout;\n private isInitialized = false;\n private signalHandlersRegistered = false;\n\n constructor(\n @inject(SAAS_SERVICE_TYPES.WildoWorkerBootstrapClientService)\n private readonly workerBootstrapClient: WildoWorkerBootstrapClientService,\n @inject(SAAS_WORKERS_NODE_SERVICE_TYPES.WorkerHttpServerService)\n private readonly workerHttpServer: WorkerHttpServerBackendService,\n ) {}\n\n /**\n * Start the worker, and record how the start ended (#3288).\n *\n * The record is the backend's (`recordBackendStartOutcome`, under `APPLICATION_RUNTIME_SIGNALS_HOME/backend-start/`,\n * filed as `workers.<name>`): `started` once the HTTP server listens, `failed` with the classified cause otherwise.\n * A port another process holds is `port-in-use` with the port, read from the server's `EADDRINUSE`. The dev\n * supervisor (`wildo-dev-runtime serve`) looks the record up by pid and names the holder, stops an orphan of this\n * package or pauses on a foreign one, instead of crash-looping into `CRASH_RESPAWN_EXHAUSTED`. Nothing is written\n * when the variable is absent, which is every non-local deployment.\n */\n public async initialize(input: WorkerApplicationInitializationInput): Promise<void> {\n const serviceIdentifier = `workers.${input.config.workerName}`;\n try {\n await this.initializeWorker(input);\n } catch (error) {\n recordBackendStartOutcome({ outcome: BackendStartOutcome.FAILED, serviceIdentifier, message: describeCaughtError(error), failure: error });\n throw error;\n }\n recordBackendStartOutcome({ outcome: BackendStartOutcome.STARTED, serviceIdentifier });\n }\n\n private async initializeWorker(input: WorkerApplicationInitializationInput): Promise<void> {\n const { config, applicationId, platformApplicationPrimarySecret } = input;\n this.providerRuntime = this.normalizeProviderRuntime(\n config.providerRuntime,\n config.workerName,\n );\n\n // Boot-time fail-closed reconciliation between the typed\n // `wildo-provider-runtime.generated.ts` artifact and the\n // `.wildo-providers.env` file mounted into this process. Catches\n // stale-sync drift before the registry filter would silently boot\n // with mismatched topology.\n //\n // Skipped when:\n // - the artifact is absent (worker authored zero providers AND no\n // scope block in `wildo.saas.config.ts`); or\n // - the artifact has zero `providerRefs` (worker authored an\n // explicit empty scope `providers: {}`). The CLI emitter\n // deliberately does NOT write `.wildo-providers.env` in that\n // case, so the env-vars are absent — running the assertion\n // would raise a false-positive fail-closed.\n // Defensive `?? []` — the schema marks `providerRefs` optional, so\n // a hand-crafted artifact (test fixture / future format drift)\n // could omit it. Without the fallback `.length` would TypeError\n // before fail-closed produced its actionable message. Production\n // CLI emit always defaults to `[]`, so this is hardening only.\n const projectedProviderRefs = this.providerRuntime?.providerRefs ?? [];\n if (this.providerRuntime && projectedProviderRefs.length > 0) {\n assertProviderRuntimeEnvInSync({\n artifactProviderRefs: projectedProviderRefs,\n artifactSourceDigest: this.providerRuntime.sourceDigest,\n hostKind: 'node-worker',\n hostKey: config.workerName,\n });\n }\n\n this.workerBootstrapClient.initialize({\n appsManagerBaseUrl: config.appsManagerBootstrapUrl,\n applicationId,\n workerName: config.workerName,\n platformApplicationPrimarySecret,\n refreshIntervalMs: config.publicKeyRefreshIntervalMs,\n });\n // The startup read tolerates apps-manager restarting; the refresh loop below keeps a single attempt.\n this.bootstrapResponse = await this.workerBootstrapClient.getBootstrapAtStartup();\n this.scheduleRefresh(config.publicKeyRefreshIntervalMs);\n\n await this.workerHttpServer.initialize({\n config,\n providerRuntime: this.providerRuntime,\n getCurrentPublicKey: () => this.requireBootstrap().jwt.publicKey,\n getRotatedPublicKeys: () =>\n (this.requireBootstrap().jwt.rotatedPublicKeys ?? []).map((entry) => ({\n kid: entry.kid,\n publicKey: entry.publicKey,\n ...(entry.notAfter ? { notAfter: entry.notAfter } : {}),\n })),\n // #2895 — a request naming a key this worker has not fetched yet (the first ones after a rotation) fetches the\n // bootstrap again; the middleware bounds how often. A failed fetch keeps the previous bootstrap.\n refreshKeysForUnknownKid: async () => {\n this.bootstrapResponse = await this.workerBootstrapClient.getBootstrap({ force: true });\n },\n });\n this.workerHttpServer.markReady();\n this.isInitialized = true;\n\n // Audit A-11 + A-3: install the worker's own graceful-shutdown handler (the JSDoc's \"SIGTERM\n // handler\", previously claimed but absent). See `registerGracefulShutdown`.\n this.registerGracefulShutdown();\n }\n\n /**\n * Returns the trusted public key + rotation metadata. The HTTP server's\n * verifier middleware already reads this on every request via the\n * `getCurrentPublicKey` / `getRotatedPublicKeys` callbacks; this getter\n * exists so test code and the worker's own route handlers can inspect\n * the active rotation state.\n */\n public getBootstrapResponse(): AppWorkerBootstrapResponse {\n return this.requireBootstrap();\n }\n\n /**\n * Returns the generated, backend-shaped provider runtime selected for the\n * current worker. Undefined means the worker was started without a generated\n * provider artifact (valid for provider-free workers).\n */\n public getProviderRuntime(): ProviderRuntimeArtifact | undefined {\n return this.providerRuntime;\n }\n\n public async shutdown(gracePeriodMs?: number): Promise<void> {\n this.workerHttpServer.markNotReady();\n await this.workerHttpServer.shutdown(gracePeriodMs);\n if (this.refreshTimer) {\n clearInterval(this.refreshTimer);\n this.refreshTimer = undefined;\n }\n this.bootstrapResponse = undefined;\n this.providerRuntime = undefined;\n this.isInitialized = false;\n }\n\n /**\n * Install the worker's SIGTERM/SIGINT graceful-shutdown handler — the JSDoc's \"SIGTERM handler\n * (graceful shutdown)\" that audit A-11 found was claimed but absent (a signal would hard-kill the\n * worker, dropping any final telemetry). The worker is deliberately queue-free, so it does\n * NOT use the RabbitMQ-coupled `WorkerShutdownManager`; instead it runs its own cleanup\n * (`shutdown()`) and flushes OTel telemetry before exit — the worker-local analogue of the\n * platform hosts' note-6 flush. `process.once` keeps the host killable (the handler exits).\n * Idempotent.\n */\n private registerGracefulShutdown(): void {\n if (this.signalHandlersRegistered) {\n return;\n }\n this.signalHandlersRegistered = true;\n process.once('SIGTERM', () => void this.gracefulShutdown('SIGTERM'));\n process.once('SIGINT', () => void this.gracefulShutdown('SIGINT'));\n }\n\n private async gracefulShutdown(signal: string): Promise<void> {\n // `console` is intentional — this runtime kernel injects no LoggerService.\n // eslint-disable-next-line no-console\n console.log(`[worker] received ${signal}, shutting down gracefully…`);\n // Bounded force-exit so a stuck drain/flush can never wedge the process (mirrors the platform\n // hosts' 5s budget). `unref()` keeps the timer from holding the loop open on its own.\n const forceExit = setTimeout(() => process.exit(1), 5_000);\n forceExit.unref();\n try {\n await this.shutdown();\n // Audit A-3: flush the final OTel trace/log batch before exit. `process.exit` skips the\n // preload's `beforeExit` backstop, so without this the worker's last batch is lost on a\n // signal. No-op when telemetry is off (the global handle is undefined). Fail-open — a flush\n // error must never turn a clean shutdown into a non-zero exit.\n await getOtelSdkHandle()\n ?.shutdown()\n .catch(() => {\n // best-effort flush\n });\n process.exit(0);\n } catch {\n process.exit(1);\n }\n }\n\n private requireBootstrap(): AppWorkerBootstrapResponse {\n if (!this.isInitialized || !this.bootstrapResponse) {\n throw new Error(\n 'StartWorkerApplicationBackendService.getBootstrapResponse() called before initialize() resolved.',\n );\n }\n return this.bootstrapResponse;\n }\n\n private scheduleRefresh(intervalMs?: number): void {\n if (this.refreshTimer) {\n clearInterval(this.refreshTimer);\n }\n if (intervalMs === undefined || intervalMs <= 0) {\n return;\n }\n this.refreshTimer = setInterval(() => {\n void this.workerBootstrapClient\n .getBootstrap({ force: true })\n .then((response) => {\n this.bootstrapResponse = response;\n })\n .catch(() => {\n // Refresh failures keep the previously-cached key live so the\n // verifier does not flap; the next interval re-attempts.\n });\n }, intervalMs);\n if (typeof this.refreshTimer.unref === 'function') {\n this.refreshTimer.unref();\n }\n }\n\n /**\n * Bridge the per-worker materialized provider runtime artifact into the\n * `ProviderRuntimeArtifact` shape consumed by `StartApplicationBackendService`.\n *\n * Three accepted input shapes — see `StartMinionApplicationBackendService`\n * for the same pattern. Summary:\n *\n * 1. `{ artifactKind: 'backend', scope: ... }` — already projected.\n * 2. `{ artifactKind: 'nodeWorkers', scope: ..., scopes: undefined }` —\n * pre-scoped per-worker module emitted by `wildo config sync`.\n * 3. `{ artifactKind: 'nodeWorkers', scopes: { [name]: ... } }` —\n * multi-scope artifact (legacy / non-CLI hosts).\n */\n private normalizeProviderRuntime(\n providerRuntime: WorkerInitializationConfig['providerRuntime'],\n workerName: string,\n ): ProviderRuntimeArtifact | undefined {\n if (!providerRuntime) {\n return undefined;\n }\n if (providerRuntime.artifactKind === 'backend') {\n return providerRuntime;\n }\n const artifactKind = providerRuntime.artifactKind;\n if (artifactKind !== 'nodeWorkers') {\n throw new Error(\n `Unsupported provider runtime artifactKind '${artifactKind}' for worker '${workerName}'.`,\n );\n }\n\n return this.selectWorkerProviderRuntimeScope(providerRuntime, workerName);\n }\n\n private selectWorkerProviderRuntimeScope(\n providerRuntime: WorkerProviderRuntimeArtifact,\n workerName: string,\n ): ProviderRuntimeArtifact {\n const preScopedScope = (providerRuntime as WorkerProviderRuntimeArtifact & {\n scope?: ProviderRuntimeArtifact['scope'];\n }).scope;\n const scopes = providerRuntime.scopes as\n | Record<string, ProviderRuntimeArtifact['scope']>\n | undefined;\n\n let scope: ProviderRuntimeArtifact['scope'] | undefined;\n\n if (preScopedScope && (!scopes || Object.keys(scopes).length === 0)) {\n scope = preScopedScope;\n } else if (scopes) {\n scope = scopes[workerName];\n if (!scope) {\n throw new Error(\n `Provider runtime artifact does not contain a scope for worker '${workerName}'. `\n + `Re-run \\`wildo config sync\\` and verify the worker is declared in `\n + `wildo.saas.config.ts \\`providers.scopes.workers\\`.`,\n );\n }\n }\n\n if (!scope) {\n throw new Error(\n `Provider runtime artifact for worker '${workerName}' is missing both a `\n + `pre-scoped \\`scope\\` and a multi-scope \\`scopes\\` map. The artifact is malformed.`,\n );\n }\n\n // Recompute `providerRefs` from the projected per-host scope, NOT\n // from the multi-scope union the parent artifact carries. The\n // `buildMultiScopeArtifactMetadata` upstream computes\n // `providerRefs` as the union across all hosts; preserving that\n // here would make the boot-time `assertProviderRuntimeEnvInSync`\n // guard treat sibling-scope refs as \"this host has providers\" and\n // mistakenly run reconciliation on hosts whose own scope is empty.\n // Mirrors `selectScopedProviderRuntimeArtifact` in\n // `provider-runtime-artifacts.ts`.\n const projectedProviderRefs = Object.keys(scope.providers ?? {}).sort();\n // Narrowed with the refs, for the same reason (#330): the import map on a multi-scope artifact\n // is the union across every host, and handing this host a sibling's package specifiers would\n // tell it that it may load something it never declared.\n const projectedProviderRuntimeImports = Object.fromEntries(\n Object.entries(\n providerRuntime.providerRuntimeImports ?? {},\n ).filter(([providerRef]) => projectedProviderRefs.includes(providerRef)),\n );\n // Narrowed with the refs as well (#1672): a host applies augmentations only to providers it declares, and a\n // sibling's opt-in must not load a package into this process.\n const projectedAugmentations = Object.entries(providerRuntime.providerRuntimeAugmentations ?? {})\n .filter(([providerRef]) => projectedProviderRefs.includes(providerRef));\n // Narrowed with the refs too (#3513): the region a sibling's provider is bound to is not this host's to apply.\n const projectedProcessingRegions = Object.entries(providerRuntime.providerProcessingRegions ?? {})\n .filter(([providerRef]) => projectedProviderRefs.includes(providerRef));\n\n return {\n __AUTOGENERATED__: providerRuntime.__AUTOGENERATED__,\n generatedBy: providerRuntime.generatedBy,\n generatedAt: providerRuntime.generatedAt,\n sourceDigest: providerRuntime.sourceDigest,\n providerRefs: projectedProviderRefs,\n providerRuntimeImports: projectedProviderRuntimeImports,\n ...(projectedAugmentations.length === 0 ? {} : { providerRuntimeAugmentations: Object.fromEntries(projectedAugmentations) }),\n ...(projectedProcessingRegions.length === 0 ? {} : { providerProcessingRegions: Object.fromEntries(projectedProcessingRegions) }),\n // Narrowed with the refs too (#2294), and never dropped: a projection that lost it would load this host's providers\n // without the residency the application declared, which is the one thing the loader's re-check exists to prevent.\n ...(providerRuntime.providerDataResidency === undefined\n ? {}\n : { providerDataResidency: narrowRuntimeProviderDataResidency(providerRuntime.providerDataResidency, projectedProviderRefs) }),\n providerGraphDigest: providerRuntime.providerGraphDigest,\n lockfileSectionDigest: providerRuntime.lockfileSectionDigest,\n artifactKind: 'backend',\n modules: providerRuntime.modules,\n scope,\n };\n }\n}\n"]}
|
|
@@ -4,8 +4,7 @@ import type { WorkerInitializationConfig } from './define-worker-init';
|
|
|
4
4
|
/**
|
|
5
5
|
* Lightweight HTTP server install for the Wildo worker runtime.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
* worker is a stateless HTTP-only runtime. The server here intentionally
|
|
7
|
+
* The worker is a stateless HTTP-only runtime. The server here intentionally
|
|
9
8
|
* does NOT pull `HttpServerBackendService` from `saas-backend-lib` — that
|
|
10
9
|
* service depends on `AppConfigurationService`, `RedisService`,
|
|
11
10
|
* `WebSocketService`, `ControllersRegistryHandlerBackendService`, etc.,
|
|
@@ -19,7 +18,7 @@ import type { WorkerInitializationConfig } from './define-worker-init';
|
|
|
19
18
|
* `WildoWorkerConfig.http.{healthPath,readyPath}` (no JWT required —
|
|
20
19
|
* K8s probe traffic is unauthenticated by design).
|
|
21
20
|
* - Optional `metricsPath` mounted unauthenticated for in-cluster
|
|
22
|
-
* prom-client scraping (
|
|
21
|
+
* prom-client scraping (no public ingress, so this is fine).
|
|
23
22
|
* - All other routes are gated by the
|
|
24
23
|
* `APPLICATION_WORKER_REQUEST`-keyed JWT verifier; worker code
|
|
25
24
|
* registers them via `WorkerInitializationConfig.registerRoutes`.
|
|
@@ -30,8 +29,8 @@ import type { WorkerInitializationConfig } from './define-worker-init';
|
|
|
30
29
|
* backend-lib logs share the dispatching request's id and `req.correlationId`
|
|
31
30
|
* is available to handlers. The kernel itself still logs via `console` by
|
|
32
31
|
* design (it injects no `LoggerService`). Subsequent slices may layer in
|
|
33
|
-
* prom-client metrics scaffolding and graceful-shutdown drain
|
|
34
|
-
* `gracePeriodSec`); the surface above is otherwise the minimum needed to
|
|
32
|
+
* prom-client metrics scaffolding and graceful-shutdown drain
|
|
33
|
+
* (`gracePeriodSec`); the surface above is otherwise the minimum needed to
|
|
35
34
|
* verify a backend-api → worker round-trip end-to-end.
|
|
36
35
|
*/
|
|
37
36
|
export declare class WorkerHttpServerBackendService {
|
|
@@ -47,6 +46,8 @@ export declare class WorkerHttpServerBackendService {
|
|
|
47
46
|
publicKey: string;
|
|
48
47
|
notAfter?: Date | string;
|
|
49
48
|
}>;
|
|
49
|
+
/** Fetch the bootstrap again when a request names an unknown `kid` (#2895); see the verifier middleware. */
|
|
50
|
+
refreshKeysForUnknownKid?: () => Promise<void>;
|
|
50
51
|
metricsHandler?: RequestHandler;
|
|
51
52
|
}): Promise<void>;
|
|
52
53
|
markReady(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-http-server.backend.service.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"worker-http-server.backend.service.d.ts","sourceRoot":"","sources":["../../../../src/runtime/services/worker-http-server.backend.service.ts"],"names":[],"mappings":"AACA,OAAgB,EAAgB,KAAK,cAAc,EAAU,MAAM,SAAS,CAAC;AAG7E,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAC1E,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAC;AAIvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBACa,8BAA8B;IACzC,OAAO,CAAC,GAAG,CAAC,CAAU;IACtB,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,OAAO,CAAS;IAEjB,UAAU,CAAC,KAAK,EAAE;QACvB,MAAM,EAAE,0BAA0B,CAAC;QACnC,eAAe,CAAC,EAAE,uBAAuB,CAAC;QAC1C,mBAAmB,EAAE,MAAM,MAAM,CAAC;QAClC,oBAAoB,CAAC,EAAE,MAAM,KAAK,CAAC;YAAE,GAAG,EAAE,MAAM,CAAC;YAAC,SAAS,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,EAAE,IAAI,GAAG,MAAM,CAAA;SAAE,CAAC,CAAC;QACjG,4GAA4G;QAC5G,wBAAwB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/C,cAAc,CAAC,EAAE,cAAc,CAAC;KACjC,GAAG,OAAO,CAAC,IAAI,CAAC;IAkEV,SAAS,IAAI,IAAI;IAIjB,YAAY,IAAI,IAAI;IAId,QAAQ,CAAC,aAAa,GAAE,MAAe,GAAG,OAAO,CAAC,IAAI,CAAC;CAiBrE"}
|
|
@@ -12,8 +12,7 @@ import { createWorkerRequestContextMiddleware } from '../middlewares/worker-requ
|
|
|
12
12
|
/**
|
|
13
13
|
* Lightweight HTTP server install for the Wildo worker runtime.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
* worker is a stateless HTTP-only runtime. The server here intentionally
|
|
15
|
+
* The worker is a stateless HTTP-only runtime. The server here intentionally
|
|
17
16
|
* does NOT pull `HttpServerBackendService` from `saas-backend-lib` — that
|
|
18
17
|
* service depends on `AppConfigurationService`, `RedisService`,
|
|
19
18
|
* `WebSocketService`, `ControllersRegistryHandlerBackendService`, etc.,
|
|
@@ -27,7 +26,7 @@ import { createWorkerRequestContextMiddleware } from '../middlewares/worker-requ
|
|
|
27
26
|
* `WildoWorkerConfig.http.{healthPath,readyPath}` (no JWT required —
|
|
28
27
|
* K8s probe traffic is unauthenticated by design).
|
|
29
28
|
* - Optional `metricsPath` mounted unauthenticated for in-cluster
|
|
30
|
-
* prom-client scraping (
|
|
29
|
+
* prom-client scraping (no public ingress, so this is fine).
|
|
31
30
|
* - All other routes are gated by the
|
|
32
31
|
* `APPLICATION_WORKER_REQUEST`-keyed JWT verifier; worker code
|
|
33
32
|
* registers them via `WorkerInitializationConfig.registerRoutes`.
|
|
@@ -38,8 +37,8 @@ import { createWorkerRequestContextMiddleware } from '../middlewares/worker-requ
|
|
|
38
37
|
* backend-lib logs share the dispatching request's id and `req.correlationId`
|
|
39
38
|
* is available to handlers. The kernel itself still logs via `console` by
|
|
40
39
|
* design (it injects no `LoggerService`). Subsequent slices may layer in
|
|
41
|
-
* prom-client metrics scaffolding and graceful-shutdown drain
|
|
42
|
-
* `gracePeriodSec`); the surface above is otherwise the minimum needed to
|
|
40
|
+
* prom-client metrics scaffolding and graceful-shutdown drain
|
|
41
|
+
* (`gracePeriodSec`); the surface above is otherwise the minimum needed to
|
|
43
42
|
* verify a backend-api → worker round-trip end-to-end.
|
|
44
43
|
*/
|
|
45
44
|
let WorkerHttpServerBackendService = class WorkerHttpServerBackendService {
|
|
@@ -50,7 +49,7 @@ let WorkerHttpServerBackendService = class WorkerHttpServerBackendService {
|
|
|
50
49
|
if (this.app) {
|
|
51
50
|
throw new Error('WorkerHttpServerBackendService.initialize called twice.');
|
|
52
51
|
}
|
|
53
|
-
const { config, providerRuntime, getCurrentPublicKey, getRotatedPublicKeys, metricsHandler, } = input;
|
|
52
|
+
const { config, providerRuntime, getCurrentPublicKey, getRotatedPublicKeys, refreshKeysForUnknownKid, metricsHandler, } = input;
|
|
54
53
|
const app = express();
|
|
55
54
|
app.disable('x-powered-by');
|
|
56
55
|
app.use(helmet());
|
|
@@ -77,6 +76,7 @@ let WorkerHttpServerBackendService = class WorkerHttpServerBackendService {
|
|
|
77
76
|
const verifyJwt = createWorkerJwtVerifierMiddleware({
|
|
78
77
|
getCurrentPublicKey,
|
|
79
78
|
getRotatedPublicKeys,
|
|
79
|
+
...(refreshKeysForUnknownKid ? { refreshKeysForUnknownKid } : {}),
|
|
80
80
|
});
|
|
81
81
|
if (config.registerRoutes) {
|
|
82
82
|
const authenticatedRouter = Router();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-http-server.backend.service.js","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"worker-http-server.backend.service.js","sourceRoot":"","sources":["../../../../src/runtime/services/worker-http-server.backend.service.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,OAAO,EAAE,EAAqC,MAAM,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,MAAM,MAAM,QAAQ,CAAC;AAI5B,OAAO,EAAE,iCAAiC,EAAE,MAAM,+CAA+C,CAAC;AAClG,OAAO,EAAE,oCAAoC,EAAE,MAAM,kDAAkD,CAAC;AAExG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEI,IAAM,8BAA8B,GAApC,MAAM,8BAA8B;IACjC,GAAG,CAAW;IACd,MAAM,CAAU;IAChB,OAAO,GAAG,KAAK,CAAC;IAEjB,UAAU,CAAC,KAQjB;QACC,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CA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,OAAO,GAAG,KAAK,CAAC;IACvB,CAAC;IAEM,KAAK,CAAC,QAAQ,CAAC,gBAAwB,MAAM;QAClD,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO;QACzB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAClC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC5B,MAAM,CAAC,mBAAmB,EAAE,EAAE,CAAC;gBAC/B,OAAO,EAAE,CAAC;YACZ,CAAC,EAAE,aAAa,CAAC,CAAC;YAClB,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;gBAChB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,CAAC,GAAG,GAAG,SAAS,CAAC;IACvB,CAAC;CACF,CAAA;AAxGY,8BAA8B;IAD1C,UAAU,EAAE;GACA,8BAA8B,CAwG1C","sourcesContent":["import { injectable } from 'inversify';\nimport express, { type Express, type RequestHandler, Router } from 'express';\nimport helmet from 'helmet';\nimport type { Server } from 'http';\nimport type { ProviderRuntimeArtifact } from '@wildo-ai/saas-backend-lib';\nimport type { WorkerInitializationConfig } from './define-worker-init';\nimport { createWorkerJwtVerifierMiddleware } from '../middlewares/worker-jwt-verifier.middleware';\nimport { createWorkerRequestContextMiddleware } from '../middlewares/worker-request-context.middleware';\n\n/**\n * Lightweight HTTP server install for the Wildo worker runtime.\n *\n * The worker is a stateless HTTP-only runtime. The server here intentionally\n * does NOT pull `HttpServerBackendService` from `saas-backend-lib` — that\n * service depends on `AppConfigurationService`, `RedisService`,\n * `WebSocketService`, `ControllersRegistryHandlerBackendService`, etc.,\n * none of which exist (or should exist) at the worker boundary.\n *\n * Surface:\n * - `helmet` security headers (no CORS — workers are ClusterIP-only,\n * never reached by browsers, never granted ingress).\n * - `express.json()` body parser with a tight default limit.\n * - Liveness / readiness probes mounted on the authored\n * `WildoWorkerConfig.http.{healthPath,readyPath}` (no JWT required —\n * K8s probe traffic is unauthenticated by design).\n * - Optional `metricsPath` mounted unauthenticated for in-cluster\n * prom-client scraping (no public ingress, so this is fine).\n * - All other routes are gated by the\n * `APPLICATION_WORKER_REQUEST`-keyed JWT verifier; worker code\n * registers them via `WorkerInitializationConfig.registerRoutes`.\n *\n * Correlation-id propagation (CC-8 / Phase 6) is wired: an early\n * `createWorkerRequestContextMiddleware()` ingests the backend-stamped\n * `x-correlation-id` into the request-context ALS frame, so a dispatch's\n * backend-lib logs share the dispatching request's id and `req.correlationId`\n * is available to handlers. The kernel itself still logs via `console` by\n * design (it injects no `LoggerService`). Subsequent slices may layer in\n * prom-client metrics scaffolding and graceful-shutdown drain\n * (`gracePeriodSec`); the surface above is otherwise the minimum needed to\n * verify a backend-api → worker round-trip end-to-end.\n */\n@injectable()\nexport class WorkerHttpServerBackendService {\n private app?: Express;\n private server?: Server;\n private isReady = false;\n\n public initialize(input: {\n config: WorkerInitializationConfig;\n providerRuntime?: ProviderRuntimeArtifact;\n getCurrentPublicKey: () => string;\n getRotatedPublicKeys?: () => Array<{ kid: string; publicKey: string; notAfter?: Date | string }>;\n /** Fetch the bootstrap again when a request names an unknown `kid` (#2895); see the verifier middleware. */\n refreshKeysForUnknownKid?: () => Promise<void>;\n metricsHandler?: RequestHandler;\n }): Promise<void> {\n if (this.app) {\n throw new Error('WorkerHttpServerBackendService.initialize called twice.');\n }\n\n const {\n config,\n providerRuntime,\n getCurrentPublicKey,\n getRotatedPublicKeys,\n refreshKeysForUnknownKid,\n metricsHandler,\n } = input;\n const app = express();\n app.disable('x-powered-by');\n app.use(helmet());\n app.use(express.json({ limit: '1mb' }));\n\n app.get(config.http.healthPath, (_req, res) => {\n res.status(200).json({ status: 'ok' });\n });\n app.get(config.http.readyPath, (_req, res) => {\n if (this.isReady) {\n res.status(200).json({ status: 'ready' });\n } else {\n res.status(503).json({ status: 'not_ready' });\n }\n });\n\n if (config.http.metricsPath && metricsHandler) {\n app.get(config.http.metricsPath, metricsHandler);\n }\n\n // Phase 6 — establish the correlation frame for dispatch traffic. Mounted AFTER the health / ready /\n // metrics probes (they are registered above and short-circuit first, keeping high-frequency K8s probe\n // traffic out of a frame) and BEFORE the authenticated router, so every worker dispatch — and the 404\n // fallthrough — runs inside a frame carrying the backend-stamped `x-correlation-id`.\n app.use(createWorkerRequestContextMiddleware());\n\n const verifyJwt = createWorkerJwtVerifierMiddleware({\n getCurrentPublicKey,\n getRotatedPublicKeys,\n ...(refreshKeysForUnknownKid ? { refreshKeysForUnknownKid } : {}),\n });\n\n if (config.registerRoutes) {\n const authenticatedRouter = Router();\n authenticatedRouter.use(verifyJwt);\n config.registerRoutes(authenticatedRouter, { verifyJwt, providerRuntime });\n app.use(authenticatedRouter);\n }\n\n app.use((_req, res) => {\n res.status(404).json({ error: 'not_found' });\n });\n\n return new Promise((resolve, reject) => {\n const server = app.listen(config.http.port, () => {\n this.app = app;\n this.server = server;\n resolve();\n });\n server.on('error', reject);\n });\n }\n\n public markReady(): void {\n this.isReady = true;\n }\n\n public markNotReady(): void {\n this.isReady = false;\n }\n\n public async shutdown(gracePeriodMs: number = 10_000): Promise<void> {\n this.isReady = false;\n if (!this.server) return;\n const server = this.server;\n await new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n server.closeAllConnections?.();\n resolve();\n }, gracePeriodMs);\n server.close(() => {\n clearTimeout(timer);\n resolve();\n });\n });\n this.server = undefined;\n this.app = undefined;\n }\n}\n"]}
|