@opengeni/worker-bundle 0.7.5 → 0.8.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.
Files changed (41) hide show
  1. package/dist/chunk-BJUW5LCM.js +325 -0
  2. package/dist/chunk-BJUW5LCM.js.map +1 -0
  3. package/dist/host-export.d.ts +70 -0
  4. package/dist/host-export.js +7 -0
  5. package/dist/host-export.js.map +1 -0
  6. package/dist/index.d.ts +98 -6
  7. package/dist/index.js +2812 -813
  8. package/dist/index.js.map +1 -1
  9. package/dist/workflow-bundle.js +12358 -0
  10. package/package.json +18 -12
  11. package/src/activities/agent-turn.ts +2045 -545
  12. package/src/activities/capabilities.ts +9 -2
  13. package/src/activities/codex-capacity.ts +6 -11
  14. package/src/activities/codex-rotation.ts +75 -86
  15. package/src/activities/context-compaction.ts +104 -24
  16. package/src/activities/environment.ts +367 -92
  17. package/src/activities/expiring-material-renewal.ts +143 -0
  18. package/src/activities/goals.ts +4 -0
  19. package/src/activities/mcp-credentials.ts +38 -0
  20. package/src/activities/packs.ts +115 -2
  21. package/src/activities/run-credential-renewal.ts +70 -0
  22. package/src/activities/run-credentials.ts +132 -0
  23. package/src/activities/run-input.ts +23 -0
  24. package/src/activities/sandbox-lease.ts +1 -1
  25. package/src/activities/scheduled-tasks.ts +21 -0
  26. package/src/activities/session-state.ts +72 -0
  27. package/src/activities/streaming.ts +1 -0
  28. package/src/activities/toolspace-token-renewal.ts +57 -0
  29. package/src/activities/types.ts +44 -6
  30. package/src/activities/workspace-capture.ts +113 -93
  31. package/src/activities.ts +10 -2
  32. package/src/host-export-pump.ts +416 -0
  33. package/src/host-export.ts +22 -0
  34. package/src/http.ts +77 -28
  35. package/src/index.ts +360 -156
  36. package/src/sandbox-resume.ts +47 -1
  37. package/src/sandbox-routing.ts +1 -1
  38. package/src/worker-service-lifecycle.ts +104 -0
  39. package/src/workflows/activities.ts +13 -7
  40. package/src/workflows/session.ts +219 -23
  41. package/src/workflows.ts +1 -0
package/src/activities.ts CHANGED
@@ -25,6 +25,8 @@ export type {
25
25
  ActivityDependencies,
26
26
  DispatchScheduledTaskRunInput,
27
27
  DispatchScheduledTaskRunResult,
28
+ ExpireSessionHumanInputInput,
29
+ ExpireSessionHumanInputResult,
28
30
  IndexDocumentInput,
29
31
  MaybeContinueGoalInput,
30
32
  MaybeContinueGoalResult,
@@ -34,8 +36,10 @@ export type {
34
36
  ReconcileCodexCapacityWaitResult,
35
37
  RecoverDispatchInput,
36
38
  RecoverDispatchResult,
39
+ PersistSessionAttemptQuiescenceInput,
37
40
  RunAgentTurnInput,
38
41
  RunAgentTurnResult,
42
+ SessionAttemptQuiescenceProof,
39
43
  } from "./activities/types";
40
44
 
41
45
  function createActivityServices(
@@ -73,7 +77,7 @@ function createActivityServices(
73
77
  // broker binding (the same `createNatsEventBus(natsUrl)`) here AND on
74
78
  // the mounted API, so the two SEPARATE processes share one broker and
75
79
  // derive the IDENTICAL `sessionSubject` — the only way live fanout
76
- // (worker emit → API SSE) works cross-process (SPIKE-1 F5/F6, proven).
80
+ // (worker emit → API SSE) works cross-process (schema-isolation contract F5/F6, proven).
77
81
  // NEVER default to an in-memory bus: it fans out intra-process only and
78
82
  // would silently break live SSE. unset → today's NATS default,
79
83
  // byte-for-byte. The bus is live-fanout ONLY — the durable Postgres
@@ -97,13 +101,14 @@ function createActivityServices(
97
101
  documentServices: dependencies.documentServices ?? createDocumentServices(settings),
98
102
  observability,
99
103
  wakeSessionWorkflow: dependencies.wakeSessionWorkflow ?? null,
104
+ signalSessionAttemptQuiesced: dependencies.signalSessionAttemptQuiesced ?? null,
100
105
  signalCodexCapacityWorkflow: dependencies.signalCodexCapacityWorkflow ?? null,
101
106
  // §7.5 P3 — host-entitlements port. No constructed default: standalone
102
107
  // has no host meter, so unset → null → `ensureRunAllowed` reads the
103
108
  // local ledger exactly as today (mirrors `wakeSessionWorkflow`'s
104
109
  // null-degrades-gracefully shape, not a `createX(settings)` default).
105
110
  entitlements: dependencies.entitlements ?? null,
106
- // §7.6 P4a — host connection-credential provider. No constructed
111
+ // §7.6 connection-credential provider — host connection-credential provider. No constructed
107
112
  // default: standalone owns its own GitHub App + encryption key, so unset
108
113
  // → null → the per-run credential mint self-mints from `settings`
109
114
  // (createGitHubAppInstallationToken + environmentsEncryptionKeyBytes)
@@ -154,8 +159,11 @@ export const runAgentTurn = defaultTurnActivities.runAgentTurn;
154
159
  export const indexDocument = defaultControlActivities.indexDocument;
155
160
  export const failSessionAttempt = defaultControlActivities.failSessionAttempt;
156
161
  export const settleSessionInterruptions = defaultControlActivities.settleSessionInterruptions;
162
+ export const persistSessionAttemptQuiescence =
163
+ defaultControlActivities.persistSessionAttemptQuiescence;
157
164
  export const recoverDispatch = defaultControlActivities.recoverDispatch;
158
165
  export const peekSessionWork = defaultControlActivities.peekSessionWork;
166
+ export const expireSessionHumanInput = defaultControlActivities.expireSessionHumanInput;
159
167
  export const markSessionIdle = defaultControlActivities.markSessionIdle;
160
168
  export const dispatchScheduledTaskRun = defaultControlActivities.dispatchScheduledTaskRun;
161
169
  export const enqueueGoalRetryWake = defaultControlActivities.enqueueGoalRetryWake;
@@ -0,0 +1,416 @@
1
+ import type { HostEventSink, HostUsageSink } from "@opengeni/contracts";
2
+ import {
3
+ acknowledgeHostExportBatch,
4
+ claimHostExportBatch,
5
+ disableHostExportConsumer,
6
+ failHostExportBatch,
7
+ getHostExportConsumerStatus,
8
+ pruneHostExportOutbox,
9
+ registerHostExportConsumer,
10
+ retireHostExportConsumer,
11
+ type Database,
12
+ type HostExportConsumerStatus,
13
+ type HostExportKind,
14
+ } from "@opengeni/db";
15
+ import type { Observability } from "@opengeni/observability";
16
+
17
+ export type HostExportDrainResult =
18
+ | { kind: HostExportKind; outcome: "not_configured" | "idle" }
19
+ | { kind: HostExportKind; outcome: "blocked"; failures: number }
20
+ | {
21
+ kind: HostExportKind;
22
+ outcome: "delivered";
23
+ count: number;
24
+ throughCursor: string;
25
+ }
26
+ | {
27
+ kind: HostExportKind;
28
+ outcome: "failed";
29
+ failures: number;
30
+ blocked: boolean;
31
+ };
32
+
33
+ export type HostExportPumpOptions = {
34
+ /** A handle authenticated as the separately provisioned host-export role. */
35
+ db: Database;
36
+ eventSink?: HostEventSink;
37
+ usageSink?: HostUsageSink;
38
+ observability?: Observability;
39
+ instanceId?: string;
40
+ pollIntervalMs?: number;
41
+ leaseSeconds?: number;
42
+ batchLimit?: number;
43
+ batchMaxBytes?: number;
44
+ maxFailures?: number;
45
+ /** Maximum catch-up batches per poll tick; bounds one iteration's work. */
46
+ maxBatchesPerPoll?: number;
47
+ /** Acked rows younger than this remain available for replay. */
48
+ pruneGraceSeconds?: number;
49
+ /** Maximum rows pruned after one successful acknowledgement. */
50
+ pruneLimit?: number;
51
+ };
52
+
53
+ export type HostExportPump = {
54
+ /** Register configured consumers and start the non-overlapping poll loop. */
55
+ start: () => Promise<void>;
56
+ /** Stop polling and wait for the currently executing sink call, if any. */
57
+ stop: () => Promise<void>;
58
+ /** Execute at most one bounded batch for each configured sink. */
59
+ drainOnce: () => Promise<HostExportDrainResult[]>;
60
+ /**
61
+ * Disable this named consumer while retaining its replay checkpoint. If it is
62
+ * the last enabled consumer of the kind, capture stops and that interval is
63
+ * intentionally unrecoverable. Normal process shutdown must not call this.
64
+ */
65
+ disable: (kind: HostExportKind) => Promise<void>;
66
+ /** Permanently remove a stopped consumer and release its retention floor. */
67
+ retire: (kind: HostExportKind) => Promise<void>;
68
+ status: (kind: HostExportKind) => Promise<HostExportConsumerStatus | null>;
69
+ };
70
+
71
+ /**
72
+ * Build the durable delivery loop used by embedded hosts.
73
+ *
74
+ * The source transaction writes the outbox; this pump only leases, delivers,
75
+ * and checkpoints it. Delivery is intentionally at least once: a process death
76
+ * after sink success but before acknowledgement repeats the same idempotency
77
+ * keys. Sink implementations must therefore deduplicate those keys.
78
+ */
79
+ export function createHostExportPump(options: HostExportPumpOptions): HostExportPump {
80
+ if (!options.eventSink && !options.usageSink) {
81
+ throw new Error("createHostExportPump requires at least one host sink");
82
+ }
83
+ const instanceId = validateInstanceId(
84
+ options.instanceId ?? `worker-${process.pid}-${crypto.randomUUID()}`,
85
+ );
86
+ const pollIntervalMs = boundedInteger(options.pollIntervalMs, 1_000, 25, 60_000);
87
+ const leaseSeconds = boundedInteger(options.leaseSeconds, 60, 5, 300);
88
+ const batchLimit = boundedInteger(options.batchLimit, 100, 1, 256);
89
+ const batchMaxBytes = boundedInteger(options.batchMaxBytes, 1_048_576, 98_304, 4_194_304);
90
+ const maxFailures = boundedInteger(options.maxFailures, 20, 1, 1_000);
91
+ const maxBatchesPerPoll = boundedInteger(options.maxBatchesPerPoll, 32, 1, 1_000);
92
+ const pruneGraceSeconds = boundedInteger(options.pruneGraceSeconds, 3_600, 0, 604_800);
93
+ const pruneLimit = boundedInteger(options.pruneLimit, 1_000, 1, 10_000);
94
+ let running = false;
95
+ let loopPromise: Promise<void> | null = null;
96
+ let waitTimer: ReturnType<typeof setTimeout> | null = null;
97
+ let wakeWait: (() => void) | null = null;
98
+ let lifecycleTail: Promise<void> = Promise.resolve();
99
+ let drainTail: Promise<void> = Promise.resolve();
100
+ const retiringKinds = new Set<HostExportKind>();
101
+
102
+ const configuredConsumer = (kind: HostExportKind): string | null =>
103
+ kind === "session_event"
104
+ ? (options.eventSink?.consumerId ?? null)
105
+ : (options.usageSink?.consumerId ?? null);
106
+
107
+ const recordResult = (result: HostExportDrainResult, durationSeconds: number) => {
108
+ const labels = { kind: result.kind, outcome: result.outcome };
109
+ options.observability?.incrementCounter({
110
+ name: "opengeni_host_export_batches_total",
111
+ help: "Durable host export batch attempts by outcome.",
112
+ labels,
113
+ });
114
+ options.observability?.observeHistogram({
115
+ name: "opengeni_host_export_batch_duration_seconds",
116
+ help: "Duration of durable host export batch attempts.",
117
+ labels: { kind: result.kind },
118
+ value: durationSeconds,
119
+ });
120
+ if (result.outcome === "delivered") {
121
+ options.observability?.incrementCounter({
122
+ name: "opengeni_host_export_records_total",
123
+ help: "Durable host export records acknowledged by a host sink.",
124
+ labels: { kind: result.kind },
125
+ amount: result.count,
126
+ });
127
+ options.observability?.setGauge({
128
+ name: "opengeni_host_export_consumer_blocked",
129
+ help: "Whether the durable host export consumer is blocked.",
130
+ labels: { kind: result.kind },
131
+ value: 0,
132
+ });
133
+ } else if (result.outcome === "failed") {
134
+ options.observability?.setGauge({
135
+ name: "opengeni_host_export_consumer_blocked",
136
+ help: "Whether the durable host export consumer is blocked.",
137
+ labels: { kind: result.kind },
138
+ value: result.blocked ? 1 : 0,
139
+ });
140
+ } else if (result.outcome === "blocked") {
141
+ options.observability?.setGauge({
142
+ name: "opengeni_host_export_consumer_blocked",
143
+ help: "Whether the durable host export consumer is blocked.",
144
+ labels: { kind: result.kind },
145
+ value: 1,
146
+ });
147
+ }
148
+ };
149
+
150
+ const drainKind = async (kind: HostExportKind): Promise<HostExportDrainResult> => {
151
+ if (retiringKinds.has(kind)) return { kind, outcome: "not_configured" };
152
+ const startedAt = performance.now();
153
+ const consumerId = configuredConsumer(kind);
154
+ if (!consumerId) {
155
+ const result: HostExportDrainResult = { kind, outcome: "not_configured" };
156
+ recordResult(result, (performance.now() - startedAt) / 1_000);
157
+ return result;
158
+ }
159
+ const leaseToken = crypto.randomUUID();
160
+ const common = {
161
+ consumerId,
162
+ leaseToken,
163
+ leaseHolderId: instanceId,
164
+ leaseSeconds,
165
+ limit: batchLimit,
166
+ maxBytes: batchMaxBytes,
167
+ };
168
+ const settleFailure = async (error: unknown): Promise<HostExportDrainResult> => {
169
+ const message = safeErrorMessage(error);
170
+ let failures = 0;
171
+ try {
172
+ failures = await failHostExportBatch(options.db, {
173
+ kind,
174
+ consumerId,
175
+ leaseToken,
176
+ error: message,
177
+ maxFailures,
178
+ });
179
+ } catch (settlementError) {
180
+ options.observability?.warn("host export failure settlement was stale", {
181
+ kind,
182
+ consumerId,
183
+ error: safeErrorMessage(settlementError),
184
+ });
185
+ }
186
+ const blocked = failures >= maxFailures;
187
+ options.observability?.warn("host export batch failed", {
188
+ kind,
189
+ consumerId,
190
+ failures,
191
+ blocked,
192
+ error: message,
193
+ });
194
+ const result: HostExportDrainResult = {
195
+ kind,
196
+ outcome: "failed",
197
+ failures,
198
+ blocked,
199
+ };
200
+ recordResult(result, (performance.now() - startedAt) / 1_000);
201
+ return result;
202
+ };
203
+ const deliverClaim = async (
204
+ count: number,
205
+ deliver: () => Promise<void>,
206
+ ): Promise<HostExportDrainResult> => {
207
+ await deliver();
208
+ const throughCursor = await acknowledgeHostExportBatch(options.db, {
209
+ kind,
210
+ consumerId,
211
+ leaseToken,
212
+ });
213
+ try {
214
+ await pruneHostExportOutbox(options.db, {
215
+ kind,
216
+ graceSeconds: pruneGraceSeconds,
217
+ limit: pruneLimit,
218
+ });
219
+ } catch (error) {
220
+ // The sink and checkpoint are already committed. A retention failure
221
+ // must be visible, but must never turn into a false delivery failure or
222
+ // a duplicate settlement attempt for the now-stale lease.
223
+ options.observability?.error("host export retention prune failed", {
224
+ kind,
225
+ consumerId,
226
+ error: safeErrorMessage(error),
227
+ });
228
+ }
229
+ const result: HostExportDrainResult = {
230
+ kind,
231
+ outcome: "delivered",
232
+ count,
233
+ throughCursor,
234
+ };
235
+ recordResult(result, (performance.now() - startedAt) / 1_000);
236
+ return result;
237
+ };
238
+
239
+ try {
240
+ if (kind === "session_event") {
241
+ const sink = options.eventSink;
242
+ if (!sink) return { kind, outcome: "not_configured" };
243
+ const batch = await claimHostExportBatch(options.db, {
244
+ kind,
245
+ ...common,
246
+ });
247
+ if (batch) return await deliverClaim(batch.events.length, () => sink.deliverEvents(batch));
248
+ } else {
249
+ const sink = options.usageSink;
250
+ if (!sink) return { kind, outcome: "not_configured" };
251
+ const batch = await claimHostExportBatch(options.db, {
252
+ kind,
253
+ ...common,
254
+ });
255
+ if (batch) return await deliverClaim(batch.events.length, () => sink.deliverUsage(batch));
256
+ }
257
+ } catch (error) {
258
+ return await settleFailure(error);
259
+ }
260
+ const status = await getHostExportConsumerStatus(options.db, {
261
+ kind,
262
+ consumerId,
263
+ });
264
+ if (status?.blockedAt) {
265
+ const result: HostExportDrainResult = {
266
+ kind,
267
+ outcome: "blocked",
268
+ failures: status.consecutiveFailures,
269
+ };
270
+ recordResult(result, (performance.now() - startedAt) / 1_000);
271
+ return result;
272
+ }
273
+ const result: HostExportDrainResult = { kind, outcome: "idle" };
274
+ recordResult(result, (performance.now() - startedAt) / 1_000);
275
+ return result;
276
+ };
277
+
278
+ const drainConfiguredOnce = async (): Promise<HostExportDrainResult[]> => {
279
+ const results: HostExportDrainResult[] = [];
280
+ if (options.eventSink) results.push(await drainKind("session_event"));
281
+ if (options.usageSink) results.push(await drainKind("usage_event"));
282
+ return results;
283
+ };
284
+
285
+ const drainOnce = (): Promise<HostExportDrainResult[]> => {
286
+ const next = drainTail.then(drainConfiguredOnce);
287
+ drainTail = next.then(
288
+ () => undefined,
289
+ () => undefined,
290
+ );
291
+ return next;
292
+ };
293
+
294
+ const serializeLifecycle = <T>(operation: () => Promise<T>): Promise<T> => {
295
+ const next = lifecycleTail.then(operation);
296
+ lifecycleTail = next.then(
297
+ () => undefined,
298
+ () => undefined,
299
+ );
300
+ return next;
301
+ };
302
+
303
+ const waitForPoll = async (): Promise<void> => {
304
+ await new Promise<void>((resolve) => {
305
+ wakeWait = resolve;
306
+ waitTimer = setTimeout(resolve, pollIntervalMs);
307
+ waitTimer.unref?.();
308
+ });
309
+ wakeWait = null;
310
+ if (waitTimer) clearTimeout(waitTimer);
311
+ waitTimer = null;
312
+ };
313
+
314
+ return {
315
+ start: () =>
316
+ serializeLifecycle(async () => {
317
+ if (running) return;
318
+ await options.db.transaction(async (tx) => {
319
+ const scoped = tx as unknown as Database;
320
+ if (options.eventSink) {
321
+ await registerHostExportConsumer(scoped, {
322
+ kind: "session_event",
323
+ consumerId: options.eventSink.consumerId,
324
+ });
325
+ }
326
+ if (options.usageSink) {
327
+ await registerHostExportConsumer(scoped, {
328
+ kind: "usage_event",
329
+ consumerId: options.usageSink.consumerId,
330
+ });
331
+ }
332
+ });
333
+ if (options.eventSink) retiringKinds.delete("session_event");
334
+ if (options.usageSink) retiringKinds.delete("usage_event");
335
+ running = true;
336
+ loopPromise = (async () => {
337
+ while (true) {
338
+ if (!running) break;
339
+ try {
340
+ for (let batch = 0; batch < maxBatchesPerPoll; batch += 1) {
341
+ if (!running) break;
342
+ const results = await drainOnce();
343
+ if (!results.some((result) => result.outcome === "delivered")) break;
344
+ }
345
+ } catch (error) {
346
+ options.observability?.error("host export pump iteration failed", {
347
+ error: safeErrorMessage(error),
348
+ });
349
+ }
350
+ if (running) await waitForPoll();
351
+ }
352
+ })();
353
+ }),
354
+ stop: () =>
355
+ serializeLifecycle(async () => {
356
+ running = false;
357
+ if (waitTimer) clearTimeout(waitTimer);
358
+ wakeWait?.();
359
+ await loopPromise;
360
+ loopPromise = null;
361
+ }),
362
+ drainOnce,
363
+ disable: async (kind) => {
364
+ const consumerId = configuredConsumer(kind);
365
+ if (!consumerId) throw new Error(`No ${kind} sink is configured`);
366
+ await disableHostExportConsumer(options.db, { kind, consumerId });
367
+ },
368
+ retire: (kind) =>
369
+ serializeLifecycle(async () => {
370
+ if (running) throw new Error("Stop the host export pump before retiring a consumer");
371
+ const consumerId = configuredConsumer(kind);
372
+ if (!consumerId) throw new Error(`No ${kind} sink is configured`);
373
+ retiringKinds.add(kind);
374
+ try {
375
+ await drainTail;
376
+ await retireHostExportConsumer(options.db, { kind, consumerId });
377
+ } catch (error) {
378
+ retiringKinds.delete(kind);
379
+ throw error;
380
+ }
381
+ }),
382
+ status: async (kind) => {
383
+ const consumerId = configuredConsumer(kind);
384
+ if (!consumerId) return null;
385
+ return await getHostExportConsumerStatus(options.db, {
386
+ kind,
387
+ consumerId,
388
+ });
389
+ },
390
+ };
391
+ }
392
+
393
+ function boundedInteger(
394
+ value: number | undefined,
395
+ fallback: number,
396
+ minimum: number,
397
+ maximum: number,
398
+ ): number {
399
+ const candidate = value ?? fallback;
400
+ if (!Number.isInteger(candidate) || candidate < minimum || candidate > maximum) {
401
+ throw new Error(`Expected an integer between ${minimum} and ${maximum}`);
402
+ }
403
+ return candidate;
404
+ }
405
+
406
+ function validateInstanceId(value: string): string {
407
+ if (value.length < 1 || value.length > 128) {
408
+ throw new Error("Host export instanceId must contain 1 to 128 characters");
409
+ }
410
+ return value;
411
+ }
412
+
413
+ function safeErrorMessage(error: unknown): string {
414
+ const message = error instanceof Error ? error.message : String(error);
415
+ return message.trim().slice(0, 500) || "host sink failed";
416
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Lightweight host-export entrypoint for embedded API processes.
3
+ *
4
+ * Importing the worker package root initializes Temporal's native worker
5
+ * dependency graph. Host applications that only project durable OpenGeni
6
+ * events or usage must not need that native runtime (or its libc contract).
7
+ */
8
+ export {
9
+ createHostExportPump,
10
+ type HostExportDrainResult,
11
+ type HostExportPump,
12
+ type HostExportPumpOptions,
13
+ } from "./host-export-pump";
14
+
15
+ export type {
16
+ HostEventExport,
17
+ HostEventExportBatch,
18
+ HostEventSink,
19
+ HostUsageExport,
20
+ HostUsageExportBatch,
21
+ HostUsageSink,
22
+ } from "@opengeni/contracts";
package/src/http.ts CHANGED
@@ -8,45 +8,81 @@ export type ReadinessChecks = Record<ReadinessCheckName, () => Promise<void> | v
8
8
 
9
9
  export type ReadinessResult = {
10
10
  ok: boolean;
11
+ state?: WorkerLifecycleState;
11
12
  checks: Record<ReadinessCheckName, { ok: boolean; error?: string }>;
12
13
  };
13
14
 
14
- export function startWorkerHttpServer(input: {
15
+ export type WorkerLifecycleState = "starting" | "ready" | "draining" | "stopped" | "failed";
16
+
17
+ export type WorkerHttpLifecycle = {
18
+ role: "control" | "turn";
19
+ state: () => WorkerLifecycleState;
20
+ };
21
+
22
+ export type WorkerHttpServerInput = {
15
23
  settings: Settings;
16
24
  observability: Observability;
17
25
  checks: ReadinessChecks;
18
26
  timeoutMs?: number;
19
- }): ReturnType<typeof Bun.serve> {
20
- const { settings, observability, checks } = input;
27
+ lifecycle?: WorkerHttpLifecycle;
28
+ };
29
+
30
+ export function createWorkerHttpHandler(
31
+ input: WorkerHttpServerInput,
32
+ ): (request: Request) => Promise<Response> {
33
+ const { settings, observability, checks, lifecycle } = input;
21
34
  const timeoutMs = input.timeoutMs ?? 2_000;
22
- return Bun.serve({
23
- hostname: "0.0.0.0",
24
- port: settings.workerHttpPort,
25
- async fetch(request) {
26
- const url = new URL(request.url);
27
- if (request.method !== "GET") {
28
- return Response.json({ error: "method_not_allowed" }, { status: 405 });
29
- }
30
- if (url.pathname === "/healthz") {
31
- return Response.json({
35
+ return async (request) => {
36
+ const url = new URL(request.url);
37
+ if (request.method !== "GET") {
38
+ return Response.json({ error: "method_not_allowed" }, { status: 405 });
39
+ }
40
+ if (url.pathname === "/healthz") {
41
+ const state = lifecycle?.state();
42
+ const ok = state !== "failed" && state !== "stopped";
43
+ return Response.json(
44
+ {
32
45
  service: settings.serviceName,
33
46
  environment: settings.environment,
34
47
  deploymentRevision: settings.deploymentRevision,
35
- ok: true,
36
- });
37
- }
38
- if (url.pathname === "/metrics") {
39
- return new Response(await observability.prometheusMetrics(), {
40
- status: 200,
41
- headers: { "content-type": "text/plain; version=0.0.4; charset=utf-8" },
42
- });
43
- }
44
- if (url.pathname === "/readyz") {
45
- const result = await runReadinessChecks(checks, timeoutMs);
46
- return Response.json(result, { status: result.ok ? 200 : 503 });
47
- }
48
- return Response.json({ error: "not_found" }, { status: 404 });
49
- },
48
+ ...(lifecycle ? { role: lifecycle.role, state } : {}),
49
+ ok,
50
+ },
51
+ { status: ok ? 200 : 503 },
52
+ );
53
+ }
54
+ if (url.pathname === "/metrics") {
55
+ return new Response(await observability.prometheusMetrics(), {
56
+ status: 200,
57
+ headers: { "content-type": "text/plain; version=0.0.4; charset=utf-8" },
58
+ });
59
+ }
60
+ if (url.pathname === "/readyz") {
61
+ const state = lifecycle?.state();
62
+ const result =
63
+ state && state !== "ready"
64
+ ? {
65
+ ok: false,
66
+ state,
67
+ checks: Object.fromEntries(
68
+ (Object.keys(checks) as ReadinessCheckName[]).map((name) => [
69
+ name,
70
+ { ok: false, error: `worker is ${state}` },
71
+ ]),
72
+ ) as ReadinessResult["checks"],
73
+ }
74
+ : { ...(await runReadinessChecks(checks, timeoutMs)), ...(state ? { state } : {}) };
75
+ return Response.json(result, { status: result.ok ? 200 : 503 });
76
+ }
77
+ return Response.json({ error: "not_found" }, { status: 404 });
78
+ };
79
+ }
80
+
81
+ export function startWorkerHttpServer(input: WorkerHttpServerInput): ReturnType<typeof Bun.serve> {
82
+ return Bun.serve({
83
+ hostname: "0.0.0.0",
84
+ port: input.settings.workerHttpPort,
85
+ fetch: createWorkerHttpHandler(input),
50
86
  });
51
87
  }
52
88
 
@@ -90,6 +126,19 @@ export function natsReadyCheck(bus: EventBus): () => void {
90
126
  };
91
127
  }
92
128
 
129
+ export function temporalReadyCheck(
130
+ connection: Pick<
131
+ import("@temporalio/worker").NativeConnection,
132
+ "workflowService" | "withDeadline"
133
+ >,
134
+ ): () => Promise<void> {
135
+ return async () => {
136
+ await connection.withDeadline(Date.now() + 2_000, () =>
137
+ connection.workflowService.getSystemInfo({}),
138
+ );
139
+ };
140
+ }
141
+
93
142
  async function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
94
143
  let timer: ReturnType<typeof setTimeout> | undefined;
95
144
  try {