@wildo-ai/saas-workers-node-lib 1.1.3
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/LICENSE +34 -0
- package/dist/esm/index.d.ts +37 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +37 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/inject/models/container.d.ts +7 -0
- package/dist/esm/inject/models/container.d.ts.map +1 -0
- package/dist/esm/inject/models/container.js +63 -0
- package/dist/esm/inject/models/container.js.map +1 -0
- package/dist/esm/inject/models/service-types.d.ts +6 -0
- package/dist/esm/inject/models/service-types.d.ts.map +1 -0
- package/dist/esm/inject/models/service-types.js +5 -0
- package/dist/esm/inject/models/service-types.js.map +1 -0
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.d.ts +54 -0
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.d.ts.map +1 -0
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.js +61 -0
- package/dist/esm/runtime/middlewares/worker-jwt-verifier.middleware.js.map +1 -0
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.d.ts +35 -0
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.d.ts.map +1 -0
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.js +30 -0
- package/dist/esm/runtime/middlewares/worker-request-context.middleware.js.map +1 -0
- package/dist/esm/runtime/services/define-worker-init.d.ts +85 -0
- package/dist/esm/runtime/services/define-worker-init.d.ts.map +1 -0
- package/dist/esm/runtime/services/define-worker-init.js +4 -0
- package/dist/esm/runtime/services/define-worker-init.js.map +1 -0
- package/dist/esm/runtime/services/read-worker-bootstrap-env.d.ts +47 -0
- package/dist/esm/runtime/services/read-worker-bootstrap-env.d.ts.map +1 -0
- package/dist/esm/runtime/services/read-worker-bootstrap-env.js +43 -0
- package/dist/esm/runtime/services/read-worker-bootstrap-env.js.map +1 -0
- package/dist/esm/runtime/services/read-worker-http-surface-env.d.ts +42 -0
- package/dist/esm/runtime/services/read-worker-http-surface-env.d.ts.map +1 -0
- package/dist/esm/runtime/services/read-worker-http-surface-env.js +49 -0
- package/dist/esm/runtime/services/read-worker-http-surface-env.js.map +1 -0
- package/dist/esm/runtime/services/start-worker-application.backend.service.d.ts +100 -0
- package/dist/esm/runtime/services/start-worker-application.backend.service.d.ts.map +1 -0
- package/dist/esm/runtime/services/start-worker-application.backend.service.js +297 -0
- package/dist/esm/runtime/services/start-worker-application.backend.service.js.map +1 -0
- package/dist/esm/runtime/services/worker-http-server.backend.service.d.ts +56 -0
- package/dist/esm/runtime/services/worker-http-server.backend.service.d.ts.map +1 -0
- package/dist/esm/runtime/services/worker-http-server.backend.service.js +128 -0
- package/dist/esm/runtime/services/worker-http-server.backend.service.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +61 -0
- package/src/index.ts +36 -0
- package/src/inject/models/container.ts +76 -0
- package/src/inject/models/service-types.ts +9 -0
- package/src/runtime/middlewares/__tests__/worker-jwt-verifier.rotation-cutoff.middleware.test.ts +62 -0
- package/src/runtime/middlewares/__tests__/worker-request-context.middleware.test.ts +85 -0
- package/src/runtime/middlewares/worker-jwt-verifier.middleware.ts +112 -0
- package/src/runtime/middlewares/worker-request-context.middleware.ts +47 -0
- package/src/runtime/services/__tests__/read-worker-http-surface-env.test.ts +66 -0
- package/src/runtime/services/__tests__/start-worker-application.backend.service.test.ts +315 -0
- package/src/runtime/services/__tests__/worker-http-server.backend.service.test.ts +69 -0
- package/src/runtime/services/define-worker-init.ts +92 -0
- package/src/runtime/services/read-worker-bootstrap-env.ts +86 -0
- package/src/runtime/services/read-worker-http-surface-env.ts +84 -0
- package/src/runtime/services/start-worker-application.backend.service.ts +346 -0
- package/src/runtime/services/worker-http-server.backend.service.ts +144 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { WildoWorkerBootstrapClientService } from '@wildo-ai/saas-backend-lib';
|
|
3
|
+
import {
|
|
4
|
+
PROVIDER_RUNTIME_ENV_KEYS,
|
|
5
|
+
type AppWorkerBootstrapResponse,
|
|
6
|
+
} from '@wildo-ai/platform-config-lib';
|
|
7
|
+
import { BackgroundRuntimeType } from '@wildo-ai/saas-models';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
defineWorkerInit,
|
|
11
|
+
type WorkerProviderRuntimeArtifact,
|
|
12
|
+
} from '../define-worker-init';
|
|
13
|
+
import { StartWorkerApplicationBackendService } from '../start-worker-application.backend.service';
|
|
14
|
+
import type { WorkerHttpServerBackendService } from '../worker-http-server.backend.service';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Set the runtime env keys that `assertProviderRuntimeEnvInSync` reads
|
|
18
|
+
* at boot, simulating the `.wildo-providers.env` mount that production
|
|
19
|
+
* worker processes always have. Tests that pass a non-empty
|
|
20
|
+
* `providerRefs` artifact MUST stub these or the boot-time fail-closed
|
|
21
|
+
* guard will throw `env-missing` before the worker http server
|
|
22
|
+
* initializes.
|
|
23
|
+
*/
|
|
24
|
+
function withMatchingProviderRuntimeEnv(input: {
|
|
25
|
+
refs: ReadonlyArray<string>;
|
|
26
|
+
digest: string;
|
|
27
|
+
}): void {
|
|
28
|
+
vi.stubEnv(PROVIDER_RUNTIME_ENV_KEYS.REFS, [...input.refs].sort().join(','));
|
|
29
|
+
vi.stubEnv(PROVIDER_RUNTIME_ENV_KEYS.RUNTIME_DIGEST, input.digest);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const TEST_PLATFORM_PRIMARY_SECRET = 'test-platform-primary-secret';
|
|
33
|
+
|
|
34
|
+
function createBootstrapResponse(): AppWorkerBootstrapResponse {
|
|
35
|
+
return {
|
|
36
|
+
appSlug: 'wonder-todos',
|
|
37
|
+
workerName: 'imageWorker',
|
|
38
|
+
worker: {
|
|
39
|
+
name: 'imageWorker',
|
|
40
|
+
displayName: 'Image Worker',
|
|
41
|
+
runtime: {
|
|
42
|
+
language: 'node',
|
|
43
|
+
entrypoint: 'src/index.ts',
|
|
44
|
+
} as any,
|
|
45
|
+
http: {
|
|
46
|
+
port: 4010,
|
|
47
|
+
healthPath: '/healthz',
|
|
48
|
+
readyPath: '/readyz',
|
|
49
|
+
} as any,
|
|
50
|
+
auth: {
|
|
51
|
+
jwtAudience: 'APPLICATION_WORKER_REQUEST',
|
|
52
|
+
} as any,
|
|
53
|
+
replicas: 1,
|
|
54
|
+
},
|
|
55
|
+
jwt: {
|
|
56
|
+
publicKey: 'public-key',
|
|
57
|
+
rotatedPublicKeys: [{ kid: 'next', publicKey: 'next-public-key', notAfter: new Date('2026-09-01T00:00:00.000Z') }],
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createWorkerProviderRuntimeArtifact(): WorkerProviderRuntimeArtifact {
|
|
63
|
+
return {
|
|
64
|
+
__AUTOGENERATED__: 'AUTO-GENERATED FILE. DO NOT EDIT. Regenerate with `wildo config sync`.',
|
|
65
|
+
generatedBy: 'wildo provider-runtime-artifact',
|
|
66
|
+
generatedAt: '2026-05-05T09:00:00.000Z',
|
|
67
|
+
sourceDigest: 'sha256:test',
|
|
68
|
+
providerRefs: ['app-illustration'],
|
|
69
|
+
artifactKind: 'nodeWorkers',
|
|
70
|
+
modules: {
|
|
71
|
+
declaredModules: {},
|
|
72
|
+
moduleIds: [],
|
|
73
|
+
moduleIdsByService: {},
|
|
74
|
+
},
|
|
75
|
+
scopes: {
|
|
76
|
+
imageWorker: {
|
|
77
|
+
providers: {
|
|
78
|
+
'app-illustration': {
|
|
79
|
+
engineCapabilities: [],
|
|
80
|
+
providerCapabilities: ['IMAGE_GENERATION'],
|
|
81
|
+
protocols: ['REST_API'],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
selection: {},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function instantiate() {
|
|
91
|
+
const bootstrapClient = {
|
|
92
|
+
initialize: vi.fn(),
|
|
93
|
+
getBootstrap: vi.fn().mockResolvedValue(createBootstrapResponse()),
|
|
94
|
+
} as unknown as WildoWorkerBootstrapClientService;
|
|
95
|
+
const workerHttpServer = {
|
|
96
|
+
initialize: vi.fn().mockResolvedValue(undefined),
|
|
97
|
+
markReady: vi.fn(),
|
|
98
|
+
markNotReady: vi.fn(),
|
|
99
|
+
shutdown: vi.fn().mockResolvedValue(undefined),
|
|
100
|
+
} as unknown as WorkerHttpServerBackendService;
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
bootstrapClient,
|
|
104
|
+
workerHttpServer,
|
|
105
|
+
service: new StartWorkerApplicationBackendService(
|
|
106
|
+
bootstrapClient,
|
|
107
|
+
workerHttpServer,
|
|
108
|
+
),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
describe('StartWorkerApplicationBackendService', () => {
|
|
113
|
+
afterEach(() => {
|
|
114
|
+
vi.unstubAllEnvs();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('selects the current node-worker scope and passes it to the HTTP host', async () => {
|
|
118
|
+
const { bootstrapClient, workerHttpServer, service } = instantiate();
|
|
119
|
+
const providerRuntime = createWorkerProviderRuntimeArtifact();
|
|
120
|
+
|
|
121
|
+
// Boot-time reconciliation requires the env file to be mounted.
|
|
122
|
+
// Production workers get this via docker-compose `env_file:` or
|
|
123
|
+
// K8s `envFrom`; in tests we stub the equivalent process.env keys.
|
|
124
|
+
withMatchingProviderRuntimeEnv({
|
|
125
|
+
refs: providerRuntime.providerRefs!,
|
|
126
|
+
digest: providerRuntime.sourceDigest,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await service.initialize({
|
|
130
|
+
applicationId: 'app-1',
|
|
131
|
+
platformApplicationPrimarySecret: TEST_PLATFORM_PRIMARY_SECRET,
|
|
132
|
+
config: defineWorkerInit({
|
|
133
|
+
workerName: 'imageWorker',
|
|
134
|
+
serviceKind: BackgroundRuntimeType.WORKER,
|
|
135
|
+
http: {
|
|
136
|
+
port: 4010,
|
|
137
|
+
healthPath: '/healthz',
|
|
138
|
+
readyPath: '/readyz',
|
|
139
|
+
},
|
|
140
|
+
appsManagerBootstrapUrl: 'https://apps-manager.example.com',
|
|
141
|
+
providerRuntime,
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(bootstrapClient.initialize).toHaveBeenCalledWith({
|
|
146
|
+
appsManagerBaseUrl: 'https://apps-manager.example.com',
|
|
147
|
+
applicationId: 'app-1',
|
|
148
|
+
workerName: 'imageWorker',
|
|
149
|
+
platformApplicationPrimarySecret: TEST_PLATFORM_PRIMARY_SECRET,
|
|
150
|
+
refreshIntervalMs: undefined,
|
|
151
|
+
});
|
|
152
|
+
expect(workerHttpServer.initialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
153
|
+
config: expect.objectContaining({
|
|
154
|
+
workerName: 'imageWorker',
|
|
155
|
+
providerRuntime,
|
|
156
|
+
}),
|
|
157
|
+
providerRuntime: expect.objectContaining({
|
|
158
|
+
artifactKind: 'backend',
|
|
159
|
+
scope: providerRuntime.scopes.imageWorker,
|
|
160
|
+
modules: providerRuntime.modules,
|
|
161
|
+
sourceDigest: providerRuntime.sourceDigest,
|
|
162
|
+
}),
|
|
163
|
+
getCurrentPublicKey: expect.any(Function),
|
|
164
|
+
getRotatedPublicKeys: expect.any(Function),
|
|
165
|
+
}));
|
|
166
|
+
const initialized = vi.mocked(workerHttpServer.initialize).mock.calls[0]![0];
|
|
167
|
+
expect(initialized.getRotatedPublicKeys?.()).toEqual([
|
|
168
|
+
{ kid: 'next', publicKey: 'next-public-key', notAfter: new Date('2026-09-01T00:00:00.000Z') },
|
|
169
|
+
]);
|
|
170
|
+
expect(service.getProviderRuntime()).toEqual(expect.objectContaining({
|
|
171
|
+
artifactKind: 'backend',
|
|
172
|
+
scope: providerRuntime.scopes.imageWorker,
|
|
173
|
+
modules: providerRuntime.modules,
|
|
174
|
+
sourceDigest: providerRuntime.sourceDigest,
|
|
175
|
+
}));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('fails fast when the generated worker runtime omits the current worker scope', async () => {
|
|
179
|
+
const { workerHttpServer, service } = instantiate();
|
|
180
|
+
|
|
181
|
+
// Throws before reaching the boot-time reconciliation; no env stub needed.
|
|
182
|
+
await expect(service.initialize({
|
|
183
|
+
applicationId: 'app-1',
|
|
184
|
+
platformApplicationPrimarySecret: TEST_PLATFORM_PRIMARY_SECRET,
|
|
185
|
+
config: defineWorkerInit({
|
|
186
|
+
workerName: 'imageWorker',
|
|
187
|
+
serviceKind: BackgroundRuntimeType.WORKER,
|
|
188
|
+
http: {
|
|
189
|
+
port: 4010,
|
|
190
|
+
healthPath: '/healthz',
|
|
191
|
+
readyPath: '/readyz',
|
|
192
|
+
},
|
|
193
|
+
appsManagerBootstrapUrl: 'https://apps-manager.example.com',
|
|
194
|
+
providerRuntime: {
|
|
195
|
+
...createWorkerProviderRuntimeArtifact(),
|
|
196
|
+
scopes: {},
|
|
197
|
+
},
|
|
198
|
+
}),
|
|
199
|
+
})).rejects.toThrow(
|
|
200
|
+
"Provider runtime artifact does not contain a scope for worker 'imageWorker'.",
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
expect(workerHttpServer.initialize).not.toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('clears the selected provider runtime on shutdown', async () => {
|
|
207
|
+
const { workerHttpServer, service } = instantiate();
|
|
208
|
+
const providerRuntime = createWorkerProviderRuntimeArtifact();
|
|
209
|
+
|
|
210
|
+
withMatchingProviderRuntimeEnv({
|
|
211
|
+
refs: providerRuntime.providerRefs!,
|
|
212
|
+
digest: providerRuntime.sourceDigest,
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
await service.initialize({
|
|
216
|
+
applicationId: 'app-1',
|
|
217
|
+
platformApplicationPrimarySecret: TEST_PLATFORM_PRIMARY_SECRET,
|
|
218
|
+
config: defineWorkerInit({
|
|
219
|
+
workerName: 'imageWorker',
|
|
220
|
+
serviceKind: BackgroundRuntimeType.WORKER,
|
|
221
|
+
http: {
|
|
222
|
+
port: 4010,
|
|
223
|
+
healthPath: '/healthz',
|
|
224
|
+
readyPath: '/readyz',
|
|
225
|
+
},
|
|
226
|
+
appsManagerBootstrapUrl: 'https://apps-manager.example.com',
|
|
227
|
+
providerRuntime,
|
|
228
|
+
}),
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
await service.shutdown();
|
|
232
|
+
|
|
233
|
+
expect(workerHttpServer.markNotReady).toHaveBeenCalledTimes(1);
|
|
234
|
+
expect(workerHttpServer.shutdown).toHaveBeenCalledTimes(1);
|
|
235
|
+
expect(service.getProviderRuntime()).toBeUndefined();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Parallels the minion-side empty-scope skip test. Pins the
|
|
240
|
+
* Option-B fix at `selectWorkerProviderRuntimeScope`: when a
|
|
241
|
+
* multi-scope artifact has `providerRefs` populated by sibling
|
|
242
|
+
* scopes but THIS worker's scope is empty, the per-host projection
|
|
243
|
+
* recomputes `providerRefs` from the projected scope (= `[]`) so
|
|
244
|
+
* the boot-time reconciliation skip gates correctly on per-host
|
|
245
|
+
* truth, not the multi-scope union. Without the fix the boot
|
|
246
|
+
* guard would throw `env-missing` (no env stubs in this test)
|
|
247
|
+
* because the union-leaked `providerRefs` looks non-empty.
|
|
248
|
+
*/
|
|
249
|
+
it('skips boot-time env reconciliation when the per-host scope is empty (Option-B fix)', async () => {
|
|
250
|
+
const { workerHttpServer, service } = instantiate();
|
|
251
|
+
|
|
252
|
+
const multiScopeArtifact: WorkerProviderRuntimeArtifact = {
|
|
253
|
+
__AUTOGENERATED__: 'AUTO-GENERATED FILE. DO NOT EDIT. Regenerate with `wildo config sync`.',
|
|
254
|
+
generatedBy: 'wildo provider-runtime-artifact',
|
|
255
|
+
generatedAt: '2026-05-10T09:00:00.000Z',
|
|
256
|
+
sourceDigest: 'sha256:multi-scope',
|
|
257
|
+
// Multi-scope union — would leak into per-host projection
|
|
258
|
+
// pre-Option-B and trigger a false `env-missing` throw.
|
|
259
|
+
providerRefs: ['app-illustration'],
|
|
260
|
+
artifactKind: 'nodeWorkers',
|
|
261
|
+
modules: {
|
|
262
|
+
declaredModules: {},
|
|
263
|
+
moduleIds: [],
|
|
264
|
+
moduleIdsByService: {},
|
|
265
|
+
},
|
|
266
|
+
scopes: {
|
|
267
|
+
// THIS host (`imageWorker`) authored an explicit empty scope.
|
|
268
|
+
imageWorker: {
|
|
269
|
+
providers: {},
|
|
270
|
+
selection: {},
|
|
271
|
+
},
|
|
272
|
+
// Sibling worker keeps the union ref alive at the artifact level.
|
|
273
|
+
thumbnailWorker: {
|
|
274
|
+
providers: {
|
|
275
|
+
'app-illustration': {
|
|
276
|
+
engineCapabilities: [],
|
|
277
|
+
providerCapabilities: ['IMAGE_GENERATION'],
|
|
278
|
+
protocols: ['REST_API'],
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
selection: {},
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
// NO env stubs — the assertion must skip because the per-host
|
|
287
|
+
// projected `providerRefs` is `[]`. If the projection still leaked
|
|
288
|
+
// the union, the boot guard would throw `env-missing` here.
|
|
289
|
+
await expect(service.initialize({
|
|
290
|
+
applicationId: 'app-1',
|
|
291
|
+
platformApplicationPrimarySecret: TEST_PLATFORM_PRIMARY_SECRET,
|
|
292
|
+
config: defineWorkerInit({
|
|
293
|
+
workerName: 'imageWorker',
|
|
294
|
+
serviceKind: BackgroundRuntimeType.WORKER,
|
|
295
|
+
http: {
|
|
296
|
+
port: 4010,
|
|
297
|
+
healthPath: '/healthz',
|
|
298
|
+
readyPath: '/readyz',
|
|
299
|
+
},
|
|
300
|
+
appsManagerBootstrapUrl: 'https://apps-manager.example.com',
|
|
301
|
+
providerRuntime: multiScopeArtifact,
|
|
302
|
+
}),
|
|
303
|
+
})).resolves.toBeUndefined();
|
|
304
|
+
|
|
305
|
+
// The HTTP server received the projected backend-shaped artifact
|
|
306
|
+
// with `providerRefs: []` (per-host truth, not the multi-scope union).
|
|
307
|
+
expect(workerHttpServer.initialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
308
|
+
providerRuntime: expect.objectContaining({
|
|
309
|
+
artifactKind: 'backend',
|
|
310
|
+
providerRefs: [],
|
|
311
|
+
scope: multiScopeArtifact.scopes!.imageWorker,
|
|
312
|
+
}),
|
|
313
|
+
}));
|
|
314
|
+
});
|
|
315
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { BackgroundRuntimeType } from '@wildo-ai/saas-models';
|
|
3
|
+
import type { ProviderRuntimeArtifact } from '@wildo-ai/saas-backend-lib';
|
|
4
|
+
|
|
5
|
+
import { defineWorkerInit } from '../define-worker-init';
|
|
6
|
+
import { WorkerHttpServerBackendService } from '../worker-http-server.backend.service';
|
|
7
|
+
|
|
8
|
+
describe('WorkerHttpServerBackendService', () => {
|
|
9
|
+
let service: WorkerHttpServerBackendService | undefined;
|
|
10
|
+
|
|
11
|
+
afterEach(async () => {
|
|
12
|
+
if (service) {
|
|
13
|
+
await service.shutdown(0);
|
|
14
|
+
service = undefined;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('threads the selected provider runtime into registerRoutes dependencies', async () => {
|
|
19
|
+
const registerRoutes = vi.fn();
|
|
20
|
+
service = new WorkerHttpServerBackendService();
|
|
21
|
+
const providerRuntime: ProviderRuntimeArtifact = {
|
|
22
|
+
__AUTOGENERATED__: 'AUTO-GENERATED FILE. DO NOT EDIT. Regenerate with `wildo config sync`.',
|
|
23
|
+
generatedBy: 'wildo provider-runtime-artifact',
|
|
24
|
+
generatedAt: '2026-05-05T09:00:00.000Z',
|
|
25
|
+
sourceDigest: 'sha256:test',
|
|
26
|
+
artifactKind: 'backend' as const,
|
|
27
|
+
modules: {
|
|
28
|
+
declaredModules: {},
|
|
29
|
+
moduleIds: [],
|
|
30
|
+
moduleIdsByService: {},
|
|
31
|
+
},
|
|
32
|
+
scope: {
|
|
33
|
+
providers: {
|
|
34
|
+
'app-illustration': {
|
|
35
|
+
engineCapabilities: [],
|
|
36
|
+
providerCapabilities: ['IMAGE_GENERATION'],
|
|
37
|
+
protocols: ['REST_API'],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
selection: {},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
await service.initialize({
|
|
45
|
+
config: defineWorkerInit({
|
|
46
|
+
workerName: 'imageWorker',
|
|
47
|
+
serviceKind: BackgroundRuntimeType.WORKER,
|
|
48
|
+
http: {
|
|
49
|
+
port: 0,
|
|
50
|
+
healthPath: '/healthz',
|
|
51
|
+
readyPath: '/readyz',
|
|
52
|
+
},
|
|
53
|
+
appsManagerBootstrapUrl: 'https://apps-manager.example.com',
|
|
54
|
+
registerRoutes,
|
|
55
|
+
}),
|
|
56
|
+
providerRuntime,
|
|
57
|
+
getCurrentPublicKey: () => 'public-key',
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
expect(registerRoutes).toHaveBeenCalledTimes(1);
|
|
61
|
+
expect(registerRoutes).toHaveBeenCalledWith(
|
|
62
|
+
expect.anything(),
|
|
63
|
+
expect.objectContaining({
|
|
64
|
+
verifyJwt: expect.any(Function),
|
|
65
|
+
providerRuntime,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Router, RequestHandler } from 'express';
|
|
2
|
+
import type { BackgroundRuntimeType } from '@wildo-ai/saas-models';
|
|
3
|
+
import type { ProviderRuntimeArtifact } from '@wildo-ai/saas-backend-lib';
|
|
4
|
+
|
|
5
|
+
type ProviderRuntimeScope = ProviderRuntimeArtifact['scope'];
|
|
6
|
+
type ProviderRuntimeModules = ProviderRuntimeArtifact['modules'];
|
|
7
|
+
|
|
8
|
+
export interface WorkerProviderRuntimeArtifact {
|
|
9
|
+
__AUTOGENERATED__: string;
|
|
10
|
+
generatedBy: ProviderRuntimeArtifact['generatedBy'];
|
|
11
|
+
generatedAt: string;
|
|
12
|
+
sourceDigest: string;
|
|
13
|
+
providerRefs?: ProviderRuntimeArtifact['providerRefs'];
|
|
14
|
+
/**
|
|
15
|
+
* Mirrored from the engine artifact so the per-host projection can FORWARD it (#330).
|
|
16
|
+
*
|
|
17
|
+
* This interface is a hand-written mirror of the CLI's multi-scope artifact, so a field the
|
|
18
|
+
* CLI emits and this shape omits is a field the projection can only reach through a cast —
|
|
19
|
+
* which would assert, across a package boundary, the very thing nobody checked.
|
|
20
|
+
*/
|
|
21
|
+
providerRuntimeImports?: ProviderRuntimeArtifact['providerRuntimeImports'];
|
|
22
|
+
providerGraphDigest?: ProviderRuntimeArtifact['providerGraphDigest'];
|
|
23
|
+
lockfileSectionDigest?: ProviderRuntimeArtifact['lockfileSectionDigest'];
|
|
24
|
+
artifactKind: 'nodeWorkers';
|
|
25
|
+
modules: ProviderRuntimeModules;
|
|
26
|
+
scopes: Record<string, ProviderRuntimeScope>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Inputs the worker host needs to bootstrap. Intentionally minimal per KD-3
|
|
31
|
+
* of `.claude/plans/minions-and-workers-implementation.md`: the worker has
|
|
32
|
+
* no backing-service access (no Mongo / Redis / RabbitMQ), only:
|
|
33
|
+
* - the four-env bootstrap-env-contract
|
|
34
|
+
* (`APPLICATION_ID`, `PLATFORM_APPLICATION_PRIMARY_SECRET`,
|
|
35
|
+
* `PLATFORM_APPS_MANAGER_URL`, `APPLICATION_ENV`), read via
|
|
36
|
+
* `readWorkerBootstrapEnv()`,
|
|
37
|
+
* - the worker's HTTP surface descriptor (port + paths),
|
|
38
|
+
* - the apps-manager `worker-bootstrap` URL it polls for the trusted
|
|
39
|
+
* `APPLICATION_WORKER_REQUEST` public key (Q3 LOCKED — dynamic resolver),
|
|
40
|
+
* - the worker's identity (`workerName`, paired with
|
|
41
|
+
* `APP_OWNED_SERVICE_KIND === BackgroundRuntimeType.WORKER`).
|
|
42
|
+
*
|
|
43
|
+
* The wider DI graph used by backend-api and minions is deliberately NOT
|
|
44
|
+
* imported here — pulling it in would drag Mongo / Redis / RabbitMQ
|
|
45
|
+
* services into the worker boundary and defeat the language-pluggable
|
|
46
|
+
* isolation the worker kind exists to provide.
|
|
47
|
+
*/
|
|
48
|
+
export interface WorkerInitializationConfig {
|
|
49
|
+
workerName: string;
|
|
50
|
+
serviceKind: BackgroundRuntimeType.WORKER;
|
|
51
|
+
http: {
|
|
52
|
+
port: number;
|
|
53
|
+
healthPath: string;
|
|
54
|
+
readyPath: string;
|
|
55
|
+
metricsPath?: string;
|
|
56
|
+
};
|
|
57
|
+
appsManagerBootstrapUrl: string;
|
|
58
|
+
/**
|
|
59
|
+
* Generated provider topology imported from the worker package's local
|
|
60
|
+
* `src/generated/wildo-provider-runtime.generated.ts` module.
|
|
61
|
+
*
|
|
62
|
+
* Node workers receive only their own pre-scoped provider graph. Non-Node
|
|
63
|
+
* workers stay on the deferred env/bootstrap delivery path and therefore do
|
|
64
|
+
* not use this seam.
|
|
65
|
+
*/
|
|
66
|
+
providerRuntime?: ProviderRuntimeArtifact | WorkerProviderRuntimeArtifact;
|
|
67
|
+
/**
|
|
68
|
+
* Cadence (ms) at which the worker re-polls apps-manager for the
|
|
69
|
+
* trusted `APPLICATION_WORKER_REQUEST` public key. JWKS-style refresh
|
|
70
|
+
* per Q3 LOCKED.
|
|
71
|
+
*/
|
|
72
|
+
publicKeyRefreshIntervalMs?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Worker route registration. Called once during HTTP server install
|
|
75
|
+
* with an authenticated `Router` (the JWT verifier middleware is
|
|
76
|
+
* already mounted by the host) plus the verifier handle as `verifyJwt`
|
|
77
|
+
* for routes that need to opt out of the default authenticated
|
|
78
|
+
* pipeline.
|
|
79
|
+
*/
|
|
80
|
+
registerRoutes?: (router: Router, deps: WorkerRouteDependencies) => void;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface WorkerRouteDependencies {
|
|
84
|
+
verifyJwt: RequestHandler;
|
|
85
|
+
providerRuntime?: ProviderRuntimeArtifact;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function defineWorkerInit(
|
|
89
|
+
config: WorkerInitializationConfig,
|
|
90
|
+
): WorkerInitializationConfig {
|
|
91
|
+
return config;
|
|
92
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ApplicationWorkerBootstrapEnvKey } from '@wildo-ai/platform-core-shared';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The four-env bootstrap-env-contract a Node worker needs at boot, read from
|
|
5
|
+
* the process environment and validated fail-closed.
|
|
6
|
+
*
|
|
7
|
+
* These keys are always projected into the worker's `.env` by
|
|
8
|
+
* `wildo config sync` (from `wildo.saas.config.ts`), keyed off the canonical
|
|
9
|
+
* {@link ApplicationWorkerBootstrapEnvKey} enum. Reading them back through the
|
|
10
|
+
* SAME enum closes the loop the backend lane already has — its
|
|
11
|
+
* `ApplicationEnvConfigurationSchema` is keyed off the sibling
|
|
12
|
+
* `ApplicationBackendBootstrapEnvKey`, so an emit-side rename fails to compile
|
|
13
|
+
* against the read side. Workers previously re-derived these names as string
|
|
14
|
+
* literals in each `index.ts`, which is exactly how a worker drifted from the
|
|
15
|
+
* emitter (`APPS_MANAGER_URL` vs the emitted `PLATFORM_APPS_MANAGER_URL`).
|
|
16
|
+
*/
|
|
17
|
+
export interface WorkerBootstrapEnv {
|
|
18
|
+
/** `APPLICATION_ID` — the caller identity forwarded to apps-manager. */
|
|
19
|
+
applicationId: string;
|
|
20
|
+
/**
|
|
21
|
+
* `PLATFORM_APPLICATION_PRIMARY_SECRET` — paired with the application id as
|
|
22
|
+
* the `x-platform-secret` header so the worker-bootstrap fetch authenticates
|
|
23
|
+
* as `APP_TO_PLATFORM`.
|
|
24
|
+
*/
|
|
25
|
+
platformApplicationPrimarySecret: string;
|
|
26
|
+
/**
|
|
27
|
+
* `PLATFORM_APPS_MANAGER_URL` — base URL of the apps-manager the worker polls
|
|
28
|
+
* for the trusted `APPLICATION_WORKER_REQUEST` public key.
|
|
29
|
+
*/
|
|
30
|
+
appsManagerUrl: string;
|
|
31
|
+
/** `APPLICATION_ENV` — the deployment environment (`development`, `staging`, …). */
|
|
32
|
+
applicationEnv: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Read and validate the worker bootstrap-env-contract, failing closed with
|
|
37
|
+
* EVERY missing key named at once (not just the first) so a misconfigured
|
|
38
|
+
* `.env` surfaces in a single boot attempt.
|
|
39
|
+
*
|
|
40
|
+
* The worker entrypoint calls this once and threads the values into
|
|
41
|
+
* {@link StartWorkerApplicationBackendService.initialize}. Keeping the read
|
|
42
|
+
* here — rather than re-deriving `process.env['…']` literals in each worker's
|
|
43
|
+
* `index.ts` — is what prevents the name drift this helper exists to close.
|
|
44
|
+
*
|
|
45
|
+
* @param env Environment source; defaults to `process.env`. Overridable so
|
|
46
|
+
* tests can exercise the fail-closed path without mutating global state.
|
|
47
|
+
* @throws If any of the four contract keys is missing or empty.
|
|
48
|
+
*/
|
|
49
|
+
export function readWorkerBootstrapEnv(
|
|
50
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
51
|
+
): WorkerBootstrapEnv {
|
|
52
|
+
const missingKeys: string[] = [];
|
|
53
|
+
const requireKey = (key: ApplicationWorkerBootstrapEnvKey): string => {
|
|
54
|
+
const value = env[key];
|
|
55
|
+
if (!value) {
|
|
56
|
+
missingKeys.push(key);
|
|
57
|
+
return '';
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const applicationId = requireKey(ApplicationWorkerBootstrapEnvKey.APPLICATION_ID);
|
|
63
|
+
const platformApplicationPrimarySecret = requireKey(
|
|
64
|
+
ApplicationWorkerBootstrapEnvKey.PLATFORM_APPLICATION_PRIMARY_SECRET,
|
|
65
|
+
);
|
|
66
|
+
const appsManagerUrl = requireKey(
|
|
67
|
+
ApplicationWorkerBootstrapEnvKey.PLATFORM_APPS_MANAGER_URL,
|
|
68
|
+
);
|
|
69
|
+
const applicationEnv = requireKey(ApplicationWorkerBootstrapEnvKey.APPLICATION_ENV);
|
|
70
|
+
|
|
71
|
+
if (missingKeys.length > 0) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
'[saas-workers-node-lib] Worker booted without required bootstrap env '
|
|
74
|
+
+ `var(s): ${missingKeys.join(', ')}. These are projected into the worker's `
|
|
75
|
+
+ '`.env` by `wildo config sync` from `wildo.saas.config.ts`; re-run it (and '
|
|
76
|
+
+ 'verify the `.env` is loaded into this process) before starting the worker.',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
applicationId,
|
|
82
|
+
platformApplicationPrimarySecret,
|
|
83
|
+
appsManagerUrl,
|
|
84
|
+
applicationEnv,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ApplicationWorkerHttpSurfaceEnvKey } from '@wildo-ai/platform-core-shared';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The worker's resolved inbound HTTP surface, read from the process
|
|
5
|
+
* environment and threaded into {@link WorkerInitializationConfig.http}.
|
|
6
|
+
*
|
|
7
|
+
* These keys are projected into the worker's `.env` by `wildo config sync`
|
|
8
|
+
* from the authored `wildo.worker.config.ts > http` block (`WorkerHttpSchema`
|
|
9
|
+
* in `@wildo-ai/platform-config-lib`), keyed off the canonical
|
|
10
|
+
* {@link ApplicationWorkerHttpSurfaceEnvKey} enum. Reading them back through
|
|
11
|
+
* the SAME enum closes the loop — an emit-side rename fails to compile against
|
|
12
|
+
* this read side — and stops a worker from re-deriving env-name literals
|
|
13
|
+
* (`process.env.PORT`) that drift from the emitter and from the sibling Rust
|
|
14
|
+
* worker (which reads `WORKER_HTTP_PORT` / `WORKER_HEALTH_PATH` /
|
|
15
|
+
* `WORKER_READY_PATH`).
|
|
16
|
+
*/
|
|
17
|
+
export interface WorkerHttpSurfaceEnv {
|
|
18
|
+
/** `WORKER_HTTP_PORT` — the TCP port the worker's HTTP server binds. */
|
|
19
|
+
port: number;
|
|
20
|
+
/** `WORKER_HEALTH_PATH` — unauthenticated liveness route (K8s probe). */
|
|
21
|
+
healthPath: string;
|
|
22
|
+
/** `WORKER_READY_PATH` — unauthenticated readiness route (K8s probe). */
|
|
23
|
+
readyPath: string;
|
|
24
|
+
/** `WORKER_METRICS_PATH` — optional Prometheus scrape route; `undefined` when the author did not opt in. */
|
|
25
|
+
metricsPath?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Canonical HTTP-surface defaults. They mirror the `WorkerHttpSchema` field
|
|
30
|
+
* defaults in `@wildo-ai/platform-config-lib` AND the Rust reference worker's
|
|
31
|
+
* `std::env::var(...).unwrap_or_else(...)` fallbacks, so every worker runtime
|
|
32
|
+
* agrees on the same values when a key is absent. A generated `.env` always
|
|
33
|
+
* carries the authored values, so these only apply to a worker booted outside
|
|
34
|
+
* the generated env (e.g. an ad-hoc local run).
|
|
35
|
+
*/
|
|
36
|
+
const DEFAULT_WORKER_HTTP_PORT = 8080;
|
|
37
|
+
const DEFAULT_WORKER_HEALTH_PATH = '/healthz';
|
|
38
|
+
const DEFAULT_WORKER_READY_PATH = '/readyz';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Read the worker HTTP-surface contract from the process environment.
|
|
42
|
+
*
|
|
43
|
+
* Unlike {@link readWorkerBootstrapEnv} — which fails closed on any missing
|
|
44
|
+
* REQUIRED identity key — the HTTP surface is DEFAULTED: a missing key falls
|
|
45
|
+
* back to the canonical default (`8080` / `/healthz` / `/readyz`), exactly as
|
|
46
|
+
* the Rust worker does. The one fail-closed case is a PRESENT-but-invalid
|
|
47
|
+
* `WORKER_HTTP_PORT` (non-numeric or non-positive): a bad port must surface at
|
|
48
|
+
* boot rather than silently binding a wrong/default port, mirroring the Rust
|
|
49
|
+
* worker's `WORKER_HTTP_PORT must parse to u16` error.
|
|
50
|
+
*
|
|
51
|
+
* @param env Environment source; defaults to `process.env`. Overridable so
|
|
52
|
+
* tests can exercise the default and fail-closed paths without mutating global
|
|
53
|
+
* state.
|
|
54
|
+
* @throws If `WORKER_HTTP_PORT` is set to a value that is not a positive integer.
|
|
55
|
+
*/
|
|
56
|
+
export function readWorkerHttpSurfaceEnv(
|
|
57
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
58
|
+
): WorkerHttpSurfaceEnv {
|
|
59
|
+
const rawPort = env[ApplicationWorkerHttpSurfaceEnvKey.WORKER_HTTP_PORT];
|
|
60
|
+
let port = DEFAULT_WORKER_HTTP_PORT;
|
|
61
|
+
if (rawPort !== undefined && rawPort !== '') {
|
|
62
|
+
const parsedPort = Number(rawPort);
|
|
63
|
+
if (!Number.isInteger(parsedPort) || parsedPort <= 0) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`[saas-workers-node-lib] ${ApplicationWorkerHttpSurfaceEnvKey.WORKER_HTTP_PORT} must be a `
|
|
66
|
+
+ `positive integer, received '${rawPort}'. It is projected into the worker's \`.env\` by `
|
|
67
|
+
+ '`wildo config sync` from `wildo.worker.config.ts > http.port`; fix the authored value '
|
|
68
|
+
+ '(or the running env) before starting the worker.',
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
port = parsedPort;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// A blank value falls back to the default too — an empty path is never a
|
|
75
|
+
// valid Express route, so treating '' as "unset" is safer than binding it.
|
|
76
|
+
const healthPath =
|
|
77
|
+
env[ApplicationWorkerHttpSurfaceEnvKey.WORKER_HEALTH_PATH] || DEFAULT_WORKER_HEALTH_PATH;
|
|
78
|
+
const readyPath =
|
|
79
|
+
env[ApplicationWorkerHttpSurfaceEnvKey.WORKER_READY_PATH] || DEFAULT_WORKER_READY_PATH;
|
|
80
|
+
const metricsPath =
|
|
81
|
+
env[ApplicationWorkerHttpSurfaceEnvKey.WORKER_METRICS_PATH] || undefined;
|
|
82
|
+
|
|
83
|
+
return { port, healthPath, readyPath, metricsPath };
|
|
84
|
+
}
|