@opengeni/worker-bundle 2.3.1 → 2.3.2-canary.37090756480001

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 (66) hide show
  1. package/dist/activities/agent-turn/claude-usage-observer.d.ts +30 -6
  2. package/dist/activities/agent-turn/credential-leases.d.ts +5 -32
  3. package/dist/activities/agent-turn/errors.d.ts +3 -0
  4. package/dist/activities/agent-turn/finalization-monitor.d.ts +1 -1
  5. package/dist/activities/agent-turn/finalization.d.ts +2 -0
  6. package/dist/activities/agent-turn/quiescence.d.ts +3 -4
  7. package/dist/activities/agent-turn/subscription-lease.d.ts +38 -0
  8. package/dist/activities/agent-turn/turn-context.d.ts +1 -1
  9. package/dist/activities/model-call-fact-reconciler.d.ts +34 -0
  10. package/dist/activities/session-state.d.ts +2 -2
  11. package/dist/activities/types.d.ts +7 -0
  12. package/dist/{activities-control-3HAH4FMG.js → activities-control-3BY4IJKL.js} +166 -35
  13. package/dist/activities-control-3BY4IJKL.js.map +1 -0
  14. package/dist/activities-control.d.ts +6 -4
  15. package/dist/{activities-turn-KU25J3VQ.js → activities-turn-TTNAVGKN.js} +263 -308
  16. package/dist/activities-turn-TTNAVGKN.js.map +1 -0
  17. package/dist/activities.d.ts +6 -4
  18. package/dist/{chunk-3K3ULWEE.js → chunk-PA7Z55P4.js} +242 -17
  19. package/dist/chunk-PA7Z55P4.js.map +1 -0
  20. package/dist/{chunk-YG27NEMX.js → chunk-YOI3Z3GF.js} +9 -1
  21. package/dist/chunk-YOI3Z3GF.js.map +1 -0
  22. package/dist/http.d.ts +1 -1
  23. package/dist/index.d.ts +6 -2
  24. package/dist/index.js +82 -16
  25. package/dist/index.js.map +1 -1
  26. package/dist/observability-metrics.d.ts +6 -1
  27. package/dist/sandbox-resume.d.ts +3 -0
  28. package/dist/session-event-fanout.d.ts +6 -0
  29. package/dist/worker-service-lifecycle.d.ts +12 -0
  30. package/dist/workflow-bundle.js +3 -2
  31. package/package.json +21 -21
  32. package/src/activities/agent-turn/claim.ts +13 -0
  33. package/src/activities/agent-turn/claude-usage-observer.ts +99 -8
  34. package/src/activities/agent-turn/credential-leases.ts +86 -213
  35. package/src/activities/agent-turn/errors.ts +61 -1
  36. package/src/activities/agent-turn/failure-settlement.ts +59 -5
  37. package/src/activities/agent-turn/finalization-monitor.ts +3 -4
  38. package/src/activities/agent-turn/finalization.ts +8 -3
  39. package/src/activities/agent-turn/quiescence.ts +3 -4
  40. package/src/activities/agent-turn/run.ts +24 -20
  41. package/src/activities/agent-turn/sandbox-establish.ts +2 -0
  42. package/src/activities/agent-turn/sandbox-runtime.ts +5 -1
  43. package/src/activities/agent-turn/skill-selection.ts +2 -0
  44. package/src/activities/agent-turn/subscription-lease.ts +115 -0
  45. package/src/activities/agent-turn/turn-context.ts +1 -4
  46. package/src/activities/codex-rotation.ts +2 -12
  47. package/src/activities/model-call-fact-reconciler.ts +112 -0
  48. package/src/activities/sandbox-lease.ts +59 -8
  49. package/src/activities/session-state.ts +29 -4
  50. package/src/activities/types.ts +7 -0
  51. package/src/activities-control.ts +2 -0
  52. package/src/activities-turn.ts +5 -0
  53. package/src/activities.ts +7 -0
  54. package/src/http.ts +4 -2
  55. package/src/index.ts +39 -9
  56. package/src/observability-metrics.ts +27 -0
  57. package/src/sandbox-resume.ts +9 -0
  58. package/src/sandbox-routing.ts +77 -0
  59. package/src/session-event-fanout.ts +23 -0
  60. package/src/worker-service-lifecycle.ts +53 -0
  61. package/src/workflows/file-upload-reaper.ts +2 -0
  62. package/src/workflows/session.ts +6 -1
  63. package/dist/activities-control-3HAH4FMG.js.map +0 -1
  64. package/dist/activities-turn-KU25J3VQ.js.map +0 -1
  65. package/dist/chunk-3K3ULWEE.js.map +0 -1
  66. package/dist/chunk-YG27NEMX.js.map +0 -1
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/workflows/activities.ts","../src/knowledge-source-sync-activity-policy.ts","../src/http.ts","../src/worker-service-lifecycle.ts","../src/memory-pressure-guard.ts","../src/unhandled-rejection-boundary.ts","../src/browser-deadline-checkpoint-contract.ts"],"sourcesContent":["import {\n dbSearchPath,\n getSettings,\n resolveNatsControlPlaneAuth,\n retryStartupDependency,\n startupRetryOptions,\n temporalConnectionOptions,\n type Settings,\n} from \"@opengeni/config\";\nimport {\n assertRuntimeDatabasePosture,\n isRetryableRuntimeDatabaseStartupError,\n countSessionRecoveryBacklog,\n createDb,\n getContextCompactionPendingSummary,\n markSessionWorkflowWakeDelivered,\n type Database,\n type RuntimeDatabasePostureOptions,\n} from \"@opengeni/db\";\nimport {\n createNatsEventBus,\n requireSessionEventDurableFanoutCapability,\n type EventBus,\n} from \"@opengeni/events\";\nimport {\n createObservability,\n logStartupDependencyRetry,\n type Observability,\n} from \"@opengeni/observability\";\nimport {\n Connection,\n isGrpcServiceError,\n ScheduleAlreadyRunning,\n ScheduleOverlapPolicy,\n Client as TemporalClient,\n WorkflowExecutionAlreadyStartedError,\n} from \"@temporalio/client\";\nimport { NativeConnection, Worker, type WorkflowBundleOption } from \"@temporalio/worker\";\nimport { existsSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport type { createControlActivities } from \"./activities-control\";\nimport type { createTurnActivities } from \"./activities-turn\";\nimport type { ActivityDependencies } from \"./activities/types\";\nimport type {\n InspectSessionAttemptActivity,\n SignalCodexCapacityWorkflow,\n SignalSessionAttemptQuiesced,\n StartSandboxReaperWorkflow,\n StartVideoGenerationWorkflow,\n WakeSessionWorkflowSignal,\n} from \"./activities/types\";\nimport { turnTaskQueue } from \"./workflows/activities\";\nimport {\n dbReadyCheck,\n natsReadyCheck,\n startWorkerHttpServer,\n temporalReadyCheck,\n type WorkerLifecycleState,\n} from \"./http\";\nimport {\n initializeContextCompactionMetrics,\n initializeWorkerOutcomeMetrics,\n normalizeTurnTaskQueueStats,\n observabilityEventBusOptions,\n startContextCompactionPendingMonitor,\n startSessionRecoveryMonitor,\n startTurnCapacityMonitor,\n type TurnTaskQueueStats,\n} from \"./observability-metrics\";\nimport {\n resolveCatalogSettings,\n SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS,\n SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n SESSION_WORKFLOW_WAKE_DISPATCHER_WORKFLOW_TYPE,\n TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n} from \"@opengeni/core\";\nimport {\n CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n createTurnWorkerConcurrencyPlan,\n turnWorkerConcurrencyLogFields,\n} from \"./concurrency\";\nimport {\n combineWorkerRunTargets,\n constructWithOwnedConnection,\n createWorkerServiceLifecycle,\n type WorkerServiceLifecycle,\n type WorkerRunTarget,\n} from \"./worker-service-lifecycle\";\nimport {\n createTurnWorkerMemoryPressureGuard,\n turnWorkerMemoryPressureGuardEnabled,\n type TurnWorkerMemoryPressureGuard,\n} from \"./memory-pressure-guard\";\nimport { assertSandboxReaperActivityTimeout } from \"./sandbox-reaper-timeout\";\nimport { installWorkerUnhandledRejectionBoundary } from \"./unhandled-rejection-boundary\";\nimport {\n SANDBOX_REAPER_V2_WORKFLOW_ID,\n sandboxLifecycleTaskQueue,\n} from \"./sandbox-reaper-contract\";\nimport {\n BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID,\n browserDeadlineCheckpointTaskQueue,\n} from \"./browser-deadline-checkpoint-contract\";\n\nexport {\n createHostExportPump,\n type HostExportDrainResult,\n type HostExportPump,\n type HostExportPumpOptions,\n type HostEventExport,\n type HostEventExportBatch,\n type HostEventSink,\n type HostLifecycleFactExport,\n type HostLifecycleFactExportBatch,\n type HostLifecycleFactSink,\n type HostUsageExport,\n type HostUsageExportBatch,\n type HostUsageSink,\n} from \"./host-export\";\n\n// The deterministic id of the ONE global reaper Schedule. A single id means\n// create() is idempotent across every worker in the pool: the first worker to\n// boot creates it, all others collide on this id (ScheduleAlreadyRunning) and\n// no-op — so the Schedule is registered EXACTLY ONCE per deployment regardless\n// of replica count.\nconst SANDBOX_REAPER_SCHEDULE_ID = \"opengeni-sandbox-lease-reaper\";\nexport const KNOWLEDGE_INDEXING_SCHEDULE_ID = \"opengeni-knowledge-indexing\";\nexport const KNOWLEDGE_INDEXING_PERIOD_MS = 15_000;\nexport const FILE_UPLOAD_REAPER_SCHEDULE_ID = \"opengeni-file-upload-reaper\";\nexport const FILE_UPLOAD_REAPER_PERIOD_MS = 15 * 60 * 1_000;\nexport const SITE_AUTH_MAINTENANCE_SCHEDULE_ID = \"opengeni-site-auth-maintenance\";\nexport const SITE_AUTH_MAINTENANCE_PERIOD_MS = 60 * 1_000;\nexport type OpenGeniWorkerRole = \"control\" | \"turn\";\n\nexport type WorkerOptions = {\n role: OpenGeniWorkerRole;\n settings?: Settings;\n activities?: ReturnType<typeof createControlActivities> | ReturnType<typeof createTurnActivities>;\n activityDependencies?: ActivityDependencies;\n /** Override the release-coherent workflow artifact. Most hosts should omit this. */\n workflowBundle?: WorkflowBundleOption;\n};\n\ntype TemporalActivityLease = {\n lastHeartbeatTime?: { seconds?: unknown; nanos?: number | null } | null;\n lastStartedTime?: { seconds?: unknown; nanos?: number | null } | null;\n activityOptions?: {\n heartbeatTimeout?: { seconds?: unknown; nanos?: number | null } | null;\n } | null;\n};\n\nfunction temporalTimeMs(\n value: { seconds?: unknown; nanos?: number | null } | null | undefined,\n): number | null {\n if (value?.seconds === undefined || value.seconds === null) return null;\n return Number(String(value.seconds)) * 1_000 + Math.floor((value.nanos ?? 0) / 1_000_000);\n}\n\nexport function temporalActivityLeaseSettled(\n pending: TemporalActivityLease | null | undefined,\n nowMs = Date.now(),\n): boolean {\n if (!pending) return true;\n const heartbeatAt =\n temporalTimeMs(pending.lastHeartbeatTime) ?? temporalTimeMs(pending.lastStartedTime);\n const heartbeatTimeoutMs = temporalTimeMs(pending.activityOptions?.heartbeatTimeout);\n return (\n heartbeatAt !== null && heartbeatTimeoutMs !== null && nowMs >= heartbeatAt + heartbeatTimeoutMs\n );\n}\n\n/** The exact Temporal workflow run is absent, so it cannot retain an activity. */\nexport function temporalWorkflowExecutionNotFound(error: unknown): boolean {\n // gRPC status code 5 is NOT_FOUND. The typed guard prevents unrelated\n // provider/HTTP errors carrying a numeric code from proving quiescence.\n return isGrpcServiceError(error) && error.code === 5;\n}\n\n/** Load exactly one role's activity graph. Exported for embedded hosts and the\n * process-RSS conformance benchmark; construction remains side-effect free\n * until an activity first resolves its injected services. */\nexport async function createDefaultWorkerActivities(\n role: OpenGeniWorkerRole,\n dependencies: ActivityDependencies = {},\n) {\n return role === \"control\"\n ? (await import(\"./activities-control\")).createControlActivities(dependencies)\n : (await import(\"./activities-turn\")).createTurnActivities(dependencies);\n}\n\ntype WorkerWorkflowDefinition =\n | { workflowBundle: WorkflowBundleOption }\n | { workflowsPath: string };\n\n/**\n * Resolve the deterministic workflow graph without asking installed hosts to\n * transpile or relocate package TypeScript. Monorepo source execution retains\n * the source path for the local development loop; published dist execution\n * fails closed unless the build-generated sibling artifact is present.\n */\nexport function resolveOpenGeniWorkflowDefinition(\n moduleUrl: string = import.meta.url,\n): WorkerWorkflowDefinition {\n const modulePath = fileURLToPath(moduleUrl).replaceAll(\"\\\\\", \"/\");\n if (modulePath.endsWith(\"/src/index.ts\")) {\n return { workflowsPath: fileURLToPath(new URL(\"./workflows.ts\", moduleUrl)) };\n }\n const codePath = fileURLToPath(new URL(\"./workflow-bundle.js\", moduleUrl));\n if (!existsSync(codePath)) {\n throw new Error(\n `OpenGeni workflow bundle is missing at ${codePath}; rebuild or reinstall @opengeni/worker-bundle`,\n );\n }\n return { workflowBundle: { codePath } };\n}\n\nexport function turnCancellationHeartbeatThrottleOptions() {\n return {\n maxHeartbeatThrottleInterval: TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n defaultHeartbeatThrottleInterval: TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n } as const;\n}\n\nexport async function createOpenGeniWorker(options: WorkerOptions): Promise<{\n worker: WorkerRunTarget;\n connection: NativeConnection;\n}> {\n const settings = options.settings ?? getSettings();\n if (options.role === \"control\") {\n // The exported lower-level factory is itself poll-capable. Validate here so\n // embedded hosts cannot bypass the reaper budget contract by omitting the\n // higher-level service and its Schedule-registration lifecycle.\n assertSandboxReaperActivityTimeout(settings);\n }\n const observability =\n options.activityDependencies?.observability ??\n createObservability(settings, { component: `worker-${options.role}` });\n initializeWorkerOutcomeMetrics(observability);\n if (options.role === \"turn\") {\n initializeContextCompactionMetrics(observability);\n }\n if (options.role === \"turn\" && options.workflowBundle) {\n throw new Error(\"workflowBundle is valid only for the control worker role\");\n }\n // Pre-resolve a PRIVATE-registry sandbox image before any turn creates a box.\n // A provider-native OPENGENI_MODAL_IMAGE_ID intentionally bypasses this\n // registry path and is resolved by ModalImageSelector.fromId at create time.\n // Otherwise this is a no-op unless the registry secret + logical ref are both\n // set. Memoized in the provider, so it runs once per process.\n if (options.role === \"turn\") {\n const { ensureModalRegistryImage } = await import(\"@opengeni/runtime/sandbox\");\n await retryStartupDependency(\n \"Modal private-registry image\",\n () => ensureModalRegistryImage(settings),\n {\n ...startupRetryOptions(settings),\n onRetry: (event) => logStartupDependencyRetry(observability, event),\n },\n );\n }\n return constructWithOwnedConnection(\n () =>\n retryStartupDependency(\n \"Temporal\",\n () => NativeConnection.connect(temporalConnectionOptions(settings)),\n {\n ...startupRetryOptions(settings),\n onRetry: (event) => logStartupDependencyRetry(observability, event),\n },\n ),\n async (connection) => {\n const activityDependencies = {\n ...options.activityDependencies,\n settings,\n observability,\n };\n const activities =\n options.activities ??\n (await createDefaultWorkerActivities(options.role, activityDependencies));\n const turnConcurrency =\n options.role === \"turn\"\n ? createTurnWorkerConcurrencyPlan(settings, { observability })\n : null;\n const workflowDefinition =\n options.role === \"control\"\n ? options.workflowBundle\n ? { workflowBundle: options.workflowBundle }\n : resolveOpenGeniWorkflowDefinition()\n : {};\n const sharedWorkerOptions = {\n connection,\n namespace: settings.temporalNamespace,\n activities,\n // Cancellation is delivered through an activity heartbeat. Keep the SDK\n // throttle aligned with runAgentTurn's local heartbeat cadence so the\n // four-second control contract still has time for physical writer drain\n // and exact receipt-gated replacement admission.\n ...turnCancellationHeartbeatThrottleOptions(),\n // GRACEFUL DEPLOY SHUTDOWN (with the SIGTERM handler in startWorker):\n // after shutdown() stops polling, in-flight activities get this long to\n // finish naturally; the rest are then CANCELLED with WORKER_SHUTDOWN.\n shutdownGraceTime: \"5s\",\n // Hard ceiling inside the pod's termination grace period.\n shutdownForceTime: \"100s\",\n } as const;\n const worker = await Worker.create({\n ...sharedWorkerOptions,\n taskQueue:\n options.role === \"control\"\n ? settings.temporalTaskQueue\n : turnTaskQueue(settings.temporalTaskQueue),\n ...(options.role === \"control\"\n ? {\n ...workflowDefinition,\n reuseV8Context: true,\n workflowThreadPoolSize: 1,\n maxCachedWorkflows: CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n maxConcurrentWorkflowTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n maxConcurrentActivityTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n }\n : turnConcurrency!.options),\n });\n if (options.role !== \"control\") {\n turnConcurrency?.admission?.finalizeStartupBaseline();\n return { worker, connection };\n }\n\n const ownedWorkers = [worker];\n try {\n // The new activity type has its own queue. Retain the V1 poller so old\n // drain histories finish; old binaries never receive checkpoint work.\n for (const taskQueue of [\n sandboxLifecycleTaskQueue(settings.temporalTaskQueue),\n browserDeadlineCheckpointTaskQueue(settings.temporalTaskQueue),\n ]) {\n const lifecycleWorker = await Worker.create({\n ...sharedWorkerOptions,\n ...workflowDefinition,\n taskQueue,\n reuseV8Context: true,\n workflowThreadPoolSize: 1,\n maxCachedWorkflows: CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n maxConcurrentWorkflowTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n maxConcurrentActivityTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n });\n ownedWorkers.push(lifecycleWorker);\n }\n return {\n worker: combineWorkerRunTargets(ownedWorkers),\n connection,\n };\n } catch (error) {\n for (const ownedWorker of ownedWorkers) {\n try {\n ownedWorker.shutdown();\n } catch {\n // Preserve the lifecycle worker construction failure. The base poller\n // still received its shutdown request; a secondary synchronous\n // shutdown error must not replace the startup root cause.\n }\n }\n throw error;\n }\n },\n (connection) => connection.close(),\n );\n}\n\n// A signalWithStart capability so a worker activity can wake a PARENT\n// session's workflow when a spawned worker completes (the parent may have\n// idled and let its run finish, so a plain signal would not start one).\n// Separate from the worker's NativeConnection: the @temporalio/client\n// Connection is what exposes workflow.signalWithStart.\nexport async function createWorkerWorkflowSignaler(\n settings: Settings,\n db: Database,\n): Promise<{\n wakeSessionWorkflow: WakeSessionWorkflowSignal;\n signalSessionAttemptQuiesced: SignalSessionAttemptQuiesced;\n inspectSessionAttemptActivity: InspectSessionAttemptActivity;\n signalCodexCapacityWorkflow: SignalCodexCapacityWorkflow;\n getTurnTaskQueueStats: () => Promise<TurnTaskQueueStats>;\n startSandboxReaperWorkflow: StartSandboxReaperWorkflow;\n startVideoGenerationWorkflow: StartVideoGenerationWorkflow;\n check: () => Promise<void>;\n close: () => Promise<void>;\n}> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n return {\n wakeSessionWorkflow: async ({\n accountId,\n workspaceId,\n sessionId,\n workflowId,\n wakeRevision,\n interruptionRequested,\n onSignalAccepted,\n }) => {\n if (interruptionRequested) {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"sessionControl\",\n signalArgs: [],\n });\n } else {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"queueChanged\",\n });\n }\n onSignalAccepted?.();\n return await markSessionWorkflowWakeDelivered(db, {\n accountId,\n workspaceId,\n sessionId,\n temporalWorkflowId: workflowId,\n wakeRevision,\n });\n },\n signalSessionAttemptQuiesced: async (proof) => {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId: proof.workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [\n {\n accountId: proof.accountId,\n workspaceId: proof.workspaceId,\n sessionId: proof.sessionId,\n },\n ],\n signal: \"sessionAttemptQuiesced\",\n signalArgs: [proof],\n });\n // No wake-outbox row exists yet: the direct receipt transaction failed.\n // The signalled workflow's DB-only control activity owns committing the\n // receipt and its exact wake revision atomically.\n },\n inspectSessionAttemptActivity: async ({ workflowId, workflowRunId, activityId }) => {\n let description;\n try {\n description = await connection.workflowService.describeWorkflowExecution({\n namespace: settings.temporalNamespace,\n execution: { workflowId, runId: workflowRunId },\n });\n } catch (error) {\n if (temporalWorkflowExecutionNotFound(error)) return \"settled\";\n throw error;\n }\n const pending = description.pendingActivities?.find(\n (activity) => activity.activityId === activityId,\n );\n return temporalActivityLeaseSettled(pending) ? \"settled\" : \"pending\";\n },\n signalCodexCapacityWorkflow: async ({\n accountId,\n workspaceId,\n sessionId,\n workflowId,\n wakeRevision,\n }) => {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"codexCapacityChanged\",\n signalArgs: [wakeRevision],\n });\n // A typed capacity signal cannot acknowledge the generic outbox row:\n // another producer may have advanced it with a Pause/Steer that requires\n // sessionControl. The global dispatcher owns that acknowledgement.\n },\n getTurnTaskQueueStats: async () => {\n const response = await connection.workflowService.describeTaskQueue({\n namespace: settings.temporalNamespace,\n taskQueue: { name: turnTaskQueue(settings.temporalTaskQueue) },\n // temporal.api.enums.v1.TASK_QUEUE_TYPE_ACTIVITY. Keep this request in\n // the supported DEFAULT mode; `stats.approximateBacklogCount` is the\n // server-documented scaling signal.\n taskQueueType: 2,\n reportStats: true,\n });\n return normalizeTurnTaskQueueStats(response.stats);\n },\n startSandboxReaperWorkflow: async () => {\n // Same backend tick, no additional schedule. A bounded independent\n // inventory cannot prevent the existing per-box drain from starting.\n const starts = await Promise.allSettled([\n temporal.workflow.start(\"sandboxReaperWorkflowV2\", {\n taskQueue: sandboxLifecycleTaskQueue(settings.temporalTaskQueue),\n workflowId: SANDBOX_REAPER_V2_WORKFLOW_ID,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [],\n }),\n temporal.workflow.start(\"browserDeadlineCheckpointSweepWorkflow\", {\n taskQueue: browserDeadlineCheckpointTaskQueue(settings.temporalTaskQueue),\n workflowId: BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [],\n }),\n ]);\n for (const start of starts) {\n if (\n start.status === \"rejected\" &&\n !(start.reason instanceof WorkflowExecutionAlreadyStartedError)\n ) {\n // The other start already settled. Retry the router without losing\n // ordinary draining or silently omitting the checkpoint sweep.\n throw start.reason;\n }\n }\n return starts[0].status === \"fulfilled\" ? \"started\" : \"already_running\";\n },\n startVideoGenerationWorkflow: async ({ accountId, workspaceId, operationId }) => {\n try {\n await temporal.workflow.start(\"videoGenerationWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId: `video-generation:${operationId}`,\n // Reject a concurrent or already-completed run, but allow the repair\n // sweep to restart this exact operation if the workflow itself failed.\n // Every provider/storage transition remains CAS/idempotency fenced in\n // Postgres, so recovery never creates a second logical operation.\n workflowIdReusePolicy: \"ALLOW_DUPLICATE_FAILED_ONLY\",\n args: [\n {\n accountId,\n workspaceId,\n operationId,\n baseTaskQueue: settings.temporalTaskQueue,\n },\n ],\n });\n return \"started\";\n } catch (error) {\n if (error instanceof WorkflowExecutionAlreadyStartedError) {\n return \"already_running\";\n }\n throw error;\n }\n },\n check: async () => {\n await connection.workflowService.getSystemInfo({});\n },\n close: async () => {\n await connection.close();\n },\n };\n}\n\n/**\n * Register the ONE global reaper Temporal Schedule (the sole liveness/GC/cost-stop\n * driver — P1.3 / OD-3) and durable system-update outbox repair cadence. With\n * sandbox ownership off the activity performs bounded DB outbox repair and\n * read-only observability projections; it never mutates or terminates sandbox leases.\n *\n * The Schedule permanently fires sandboxReaperWorkflow on the legacy/base queue\n * every settings.sandboxLeaseReaperPeriodMs (the SAME cadence the boot invariant\n * `reaperPeriod < viewerHolderTTL` and `reaperPeriod + idleGrace < providerLifetime`\n * validates in packages/config — wiring the schedule period to it). SKIP overlap means a slow\n * sweep never overlaps itself. Idempotent: a duplicate scheduleId across the\n * worker pool collides on ScheduleAlreadyRunning and reconciles the same\n * definition. New workers route onto the versioned lifecycle queue; old workers\n * can still run the legacy action during rollout or a full binary rollback.\n *\n * Returns a `close()` for the dedicated client connection (separate from the\n * worker's NativeConnection — the Schedule client is a @temporalio/client).\n */\nexport async function registerSandboxReaperSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n assertSandboxReaperActivityTimeout(settings);\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n const spec = { intervals: [{ every: settings.sandboxLeaseReaperPeriodMs }] };\n const action = {\n type: \"startWorkflow\" as const,\n workflowType: \"sandboxReaperWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [] as [],\n };\n const policies = {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n } as const;\n try {\n await temporal.schedule.create({\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n // @every-style interval: fire once per reaper period. The boot invariant\n // guarantees reaperPeriod < viewerHolderTTL and\n // reaperPeriod + idleGrace < providerLifetime.\n spec,\n action,\n // The base workflow is a short router on new binaries; legacy binaries\n // still execute the pre-refactor composite under this same overlap fence.\n policies,\n });\n observability.info(\"Registered the global sandbox-lease reaper Schedule\", {\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n reaperPeriodMs: settings.sandboxLeaseReaperPeriodMs,\n });\n return {\n registered: true,\n close: async () => {\n await connection.close();\n },\n };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n // Never move this action off the base queue: doing so would leave a full\n // rollback with no worker polling the only reaper Schedule. The workflow\n // implementation itself owns the forward-compatible queue handoff.\n await temporal.schedule.getHandle(SANDBOX_REAPER_SCHEDULE_ID).update((previous) => ({\n spec,\n action,\n policies,\n state: {\n paused: previous.state.paused,\n ...(previous.state.note ? { note: previous.state.note } : {}),\n ...(previous.state.remainingActions !== undefined\n ? { remainingActions: previous.state.remainingActions }\n : {}),\n },\n }));\n observability.info(\"Reconciled the global sandbox-lease reaper Schedule\", {\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n temporalTaskQueue: action.taskQueue,\n });\n return {\n registered: false,\n close: async () => {\n await connection.close();\n },\n };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/** Register the deployment-wide worker for rebuildable Knowledge search projections. */\nexport async function registerKnowledgeIndexingSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n spec: { intervals: [{ every: KNOWLEDGE_INDEXING_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"knowledgeIndexingWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global Knowledge indexer Schedule\", {\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n indexingPeriodMs: KNOWLEDGE_INDEXING_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global Knowledge indexer Schedule already registered\", {\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/**\n * Register the one provider-neutral expired direct-upload cleanup Schedule.\n * Unlike sandbox GC this is always registered: file uploads can be enabled in\n * deployments where sandbox ownership is disabled. The activity is a cheap\n * no-op when object storage is not configured.\n */\nexport async function registerFileUploadReaperSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n spec: { intervals: [{ every: FILE_UPLOAD_REAPER_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"fileUploadReaperWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global file-upload reaper Schedule\", {\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n reaperPeriodMs: FILE_UPLOAD_REAPER_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global file-upload reaper Schedule already registered\", {\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/** Register the deployment-wide maintained-auth dispatcher. Its one-minute\n * cadence matches the minimum public policy interval; DB claims own overlap,\n * crash recovery, and exact session idempotency. */\nexport async function registerSiteAuthMaintenanceSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n spec: { intervals: [{ every: SITE_AUTH_MAINTENANCE_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"siteAuthMaintenanceWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global site-auth maintenance Schedule\", {\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n maintenancePeriodMs: SITE_AUTH_MAINTENANCE_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global site-auth maintenance Schedule already registered\", {\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/**\n * Register the one repair cadence for committed workflow-wake revisions. The\n * activity only reads the transactional outbox and sends revision-scoped\n * signals; it is independent of sandbox ownership and child-agent features.\n */\nexport async function registerSessionWorkflowWakeDispatcherSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n spec: { intervals: [{ every: SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: SESSION_WORKFLOW_WAKE_DISPATCHER_WORKFLOW_TYPE,\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the session-workflow wake dispatcher Schedule\", {\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n periodMs: SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Session-workflow wake dispatcher Schedule already registered\", {\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\nexport type OpenGeniWorkerServiceOptions = Omit<WorkerOptions, \"activityDependencies\"> & {\n activityDependencies: ActivityDependencies & { db: Database; bus: EventBus };\n /**\n * Exact catalog posture required by the standalone runtime. Embedded hosts\n * may omit this when they own an equivalent database isolation contract.\n */\n databasePosture?: RuntimeDatabasePostureOptions;\n /**\n * `role-default` registers OpenGeni's internal maintenance schedules on a\n * control worker only. These are engine maintenance schedules, not a host's\n * product-level scheduled-agent jobs. Use `none` when another control worker\n * in the same deployment owns them.\n */\n internalSchedules?: \"role-default\" | \"none\";\n /**\n * Set false only when the host exposes equivalent lifecycle endpoints itself.\n * A dedicated readiness database prevents ordinary activity-pool saturation\n * from making an otherwise healthy Temporal worker fail Kubernetes readiness.\n * The embedding host retains ownership of this handle.\n */\n http?: false | { readinessTimeoutMs?: number; readinessDb?: Database };\n};\n\nexport type OpenGeniWorkerService = {\n readonly role: OpenGeniWorkerRole;\n readonly worker: WorkerRunTarget;\n readonly connection: NativeConnection;\n state(): WorkerLifecycleState;\n run(): Promise<void>;\n drain(reason?: string): void;\n close(): Promise<void>;\n};\n\nexport function workerOwnsInternalSchedules(\n role: OpenGeniWorkerRole,\n policy: OpenGeniWorkerServiceOptions[\"internalSchedules\"] = \"role-default\",\n): boolean {\n return role === \"control\" && policy !== \"none\";\n}\n\n/**\n * Construct one role-specific worker process around the lower-level Temporal\n * worker factory. The service owns every Temporal client and HTTP listener it\n * creates. The embedding host retains ownership of its injected DB and EventBus\n * handles and closes those only after this service has drained.\n */\nexport async function createOpenGeniWorkerService(\n options: OpenGeniWorkerServiceOptions,\n): Promise<OpenGeniWorkerService> {\n requireSessionEventDurableFanoutCapability(options.activityDependencies.bus);\n const settings = options.settings ?? getSettings();\n const observability =\n options.activityDependencies.observability ??\n createObservability(settings, { component: `worker-${options.role}` });\n const retryOptions = startupRetryOptions(settings);\n const onRetry = (event: Parameters<typeof logStartupDependencyRetry>[1]) =>\n logStartupDependencyRetry(observability, event);\n let lifecycle: WorkerServiceLifecycle | undefined;\n let signaler: Awaited<ReturnType<typeof createWorkerWorkflowSignaler>> | undefined;\n let workerBundle: Awaited<ReturnType<typeof createOpenGeniWorker>> | undefined;\n let turnCapacityMonitor: ReturnType<typeof startTurnCapacityMonitor> | undefined;\n let sessionRecoveryMonitor: ReturnType<typeof startSessionRecoveryMonitor> | undefined;\n let contextCompactionPendingMonitor:\n | ReturnType<typeof startContextCompactionPendingMonitor>\n | undefined;\n const schedules: Array<{ close: () => Promise<void> }> = [];\n let httpServer: ReturnType<typeof startWorkerHttpServer> | undefined;\n let memoryPressureGuard: TurnWorkerMemoryPressureGuard | undefined;\n\n try {\n const resolvedCatalog = await retryStartupDependency(\n \"model catalog\",\n () => resolveCatalogSettings(options.activityDependencies.db, settings),\n { ...retryOptions, onRetry },\n );\n observability.info(\"OpenGeni model catalog resolved\", {\n role: options.role,\n catalogSource: resolvedCatalog.source,\n catalogVersion: resolvedCatalog.version,\n });\n const needsSignaler =\n options.role === \"turn\" ||\n !options.activityDependencies.wakeSessionWorkflow ||\n !options.activityDependencies.signalSessionAttemptQuiesced ||\n !options.activityDependencies.inspectSessionAttemptActivity ||\n !options.activityDependencies.signalCodexCapacityWorkflow ||\n !options.activityDependencies.startSandboxReaperWorkflow ||\n !options.activityDependencies.startVideoGenerationWorkflow;\n if (needsSignaler) {\n signaler = await retryStartupDependency(\n \"Temporal client\",\n () => createWorkerWorkflowSignaler(settings, options.activityDependencies.db),\n { ...retryOptions, onRetry },\n );\n }\n const wakeSessionWorkflow =\n options.activityDependencies.wakeSessionWorkflow ?? signaler?.wakeSessionWorkflow;\n const signalSessionAttemptQuiesced =\n options.activityDependencies.signalSessionAttemptQuiesced ??\n signaler?.signalSessionAttemptQuiesced;\n const inspectSessionAttemptActivity =\n options.activityDependencies.inspectSessionAttemptActivity ??\n signaler?.inspectSessionAttemptActivity;\n const signalCodexCapacityWorkflow =\n options.activityDependencies.signalCodexCapacityWorkflow ??\n signaler?.signalCodexCapacityWorkflow;\n const startSandboxReaperWorkflow =\n options.activityDependencies.startSandboxReaperWorkflow ??\n signaler?.startSandboxReaperWorkflow;\n const startVideoGenerationWorkflow =\n options.activityDependencies.startVideoGenerationWorkflow ??\n signaler?.startVideoGenerationWorkflow;\n if (\n !wakeSessionWorkflow ||\n !signalSessionAttemptQuiesced ||\n !inspectSessionAttemptActivity ||\n !signalCodexCapacityWorkflow ||\n !startSandboxReaperWorkflow ||\n !startVideoGenerationWorkflow\n ) {\n throw new Error(\"OpenGeni worker lifecycle could not resolve its workflow signalers\");\n }\n workerBundle = await createOpenGeniWorker({\n role: options.role,\n settings,\n ...(options.activities ? { activities: options.activities } : {}),\n ...(options.workflowBundle ? { workflowBundle: options.workflowBundle } : {}),\n activityDependencies: {\n ...options.activityDependencies,\n settings,\n observability,\n wakeSessionWorkflow,\n signalSessionAttemptQuiesced,\n inspectSessionAttemptActivity,\n signalCodexCapacityWorkflow,\n startSandboxReaperWorkflow,\n startVideoGenerationWorkflow,\n },\n });\n\n if (options.role === \"turn\") {\n if (!signaler) {\n throw new Error(\"Turn worker capacity monitor could not resolve its Temporal stats client\");\n }\n turnCapacityMonitor = startTurnCapacityMonitor({\n observability,\n read: signaler.getTurnTaskQueueStats,\n });\n } else {\n sessionRecoveryMonitor = startSessionRecoveryMonitor({\n observability,\n read: async () => await countSessionRecoveryBacklog(options.activityDependencies.db),\n });\n contextCompactionPendingMonitor = startContextCompactionPendingMonitor({\n observability,\n read: async () => await getContextCompactionPendingSummary(options.activityDependencies.db),\n });\n }\n\n if (workerOwnsInternalSchedules(options.role, options.internalSchedules)) {\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (sandbox reaper)\",\n () => registerSandboxReaperSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (Knowledge indexing)\",\n () => registerKnowledgeIndexingSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (file upload reaper)\",\n () => registerFileUploadReaperSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (site auth maintenance)\",\n () => registerSiteAuthMaintenanceSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (session-workflow wake dispatcher)\",\n () => registerSessionWorkflowWakeDispatcherSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n }\n\n if (options.http !== false) {\n const databaseReady = dbReadyCheck(\n options.http?.readinessDb ?? options.activityDependencies.db,\n options.databasePosture,\n );\n httpServer = startWorkerHttpServer({\n settings,\n observability,\n checks: {\n db: async () => {\n await databaseReady();\n await resolveCatalogSettings(options.activityDependencies.db, settings);\n },\n nats: natsReadyCheck(options.activityDependencies.bus),\n temporal: temporalReadyCheck(workerBundle.connection),\n },\n ...(options.http?.readinessTimeoutMs ? { timeoutMs: options.http.readinessTimeoutMs } : {}),\n lifecycle: { role: options.role, state: () => lifecycle?.state() ?? \"starting\" },\n });\n }\n } catch (error) {\n httpServer?.stop(true);\n await Promise.allSettled([\n turnCapacityMonitor?.close(),\n sessionRecoveryMonitor?.close(),\n contextCompactionPendingMonitor?.close(),\n workerBundle?.connection.close(),\n signaler?.close(),\n ...schedules.map((schedule) => schedule.close()),\n ]);\n throw error;\n }\n\n const activeWorkerBundle = workerBundle;\n const activeSignaler = signaler;\n if (!activeWorkerBundle) {\n throw new Error(\"OpenGeni worker service initialization did not complete\");\n }\n\n lifecycle = createWorkerServiceLifecycle({\n role: options.role,\n worker: activeWorkerBundle.worker,\n observability,\n closeOwnedResources: async () => {\n memoryPressureGuard?.close();\n httpServer?.stop(true);\n await Promise.allSettled([\n turnCapacityMonitor?.close(),\n sessionRecoveryMonitor?.close(),\n contextCompactionPendingMonitor?.close(),\n activeWorkerBundle.connection.close(),\n activeSignaler?.close(),\n ...schedules.map((schedule) => schedule.close()),\n ]);\n },\n onReady: () => {\n observability.info(\"OpenGeni worker listening\", {\n role: options.role,\n temporalTaskQueue:\n options.role === \"control\"\n ? settings.temporalTaskQueue\n : turnTaskQueue(settings.temporalTaskQueue),\n ...(options.role === \"turn\"\n ? turnWorkerConcurrencyLogFields(settings)\n : {\n concurrencyMode: \"fixed\",\n maxConcurrentTurns: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n targetCpuUsage: null,\n targetMemoryUsage: null,\n }),\n maxConcurrentWorkflowTaskExecutions:\n options.role === \"control\" ? CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS : 0,\n maxCachedWorkflows: options.role === \"control\" ? CONTROL_WORKER_MAX_CACHED_WORKFLOWS : 0,\n httpPort: options.http === false ? null : settings.workerHttpPort,\n });\n },\n });\n const activeLifecycle = lifecycle;\n\n if (turnWorkerMemoryPressureGuardEnabled(options.role, settings)) {\n try {\n memoryPressureGuard = createTurnWorkerMemoryPressureGuard({\n settings,\n observability,\n drain: () => {\n if (!activeLifecycle.drain(\"memory pressure guard\")) {\n throw new Error(\"worker shutdown request failed\");\n }\n },\n });\n } catch (error) {\n await activeLifecycle.close();\n throw error;\n }\n }\n\n return {\n role: options.role,\n worker: activeWorkerBundle.worker,\n connection: activeWorkerBundle.connection,\n state: activeLifecycle.state,\n run: activeLifecycle.run,\n drain: (reason) => {\n activeLifecycle.drain(reason);\n },\n close: activeLifecycle.close,\n };\n}\n\nexport type RunOpenGeniWorkerOptions = OpenGeniWorkerServiceOptions & {\n /** Defaults to SIGTERM and SIGINT. Pass false when the host owns process signals. */\n shutdownSignals?: false | ReadonlyArray<\"SIGTERM\" | \"SIGINT\">;\n};\n\n/** Start, drain, and close one embedded worker process. */\nexport async function runOpenGeniWorker(options: RunOpenGeniWorkerOptions): Promise<void> {\n const service = await createOpenGeniWorkerService(options);\n const signals =\n options.shutdownSignals === false ? [] : (options.shutdownSignals ?? [\"SIGTERM\", \"SIGINT\"]);\n const handlers = signals.map((signal) => {\n const handler = () => service.drain(signal);\n process.on(signal, handler);\n return { signal, handler };\n });\n try {\n await service.run();\n } finally {\n for (const { signal, handler } of handlers) {\n process.off(signal, handler);\n }\n await service.close();\n }\n}\n\nexport async function startWorker() {\n const role = process.env.OPENGENI_WORKER_ROLE;\n if (role !== \"control\" && role !== \"turn\") {\n throw new Error(\"OPENGENI_WORKER_ROLE must be explicitly set to 'control' or 'turn'\");\n }\n const settings = getSettings();\n const observability = createObservability(settings, { component: `worker-${role}` });\n // The Agents SDK also listens for unhandled rejections and exits the process.\n // Keep detached SDK/transport promises observational here: durable turn\n // activity boundaries own failures, while process exit causes cross-session\n // lease loss for every turn assigned to this shared worker pod.\n const disposeUnhandledRejectionBoundary = installWorkerUnhandledRejectionBoundary();\n const retryOptions = startupRetryOptions(settings);\n const onRetry = (event: Parameters<typeof logStartupDependencyRetry>[1]) =>\n logStartupDependencyRetry(observability, event);\n const searchPath = dbSearchPath(settings);\n const dbClient = createDb(settings.databaseUrl, {\n ...(searchPath ? { searchPath } : {}),\n rlsStrategy: settings.rlsStrategy,\n });\n // Readiness must prove PostgreSQL independently of activity demand. Sharing\n // the default application pool makes the probe wait behind live turns when\n // all ten slots are occupied, even while PostgreSQL itself is healthy. Since\n // Kubernetes readiness cannot stop Temporal polling, that false negative\n // only wedges rollouts and service health; reserve one probe connection.\n const readinessDbClient = createDb(settings.databaseUrl, {\n ...(searchPath ? { searchPath } : {}),\n rlsStrategy: settings.rlsStrategy,\n max: 1,\n });\n const databasePosture = {\n rlsStrategy: settings.rlsStrategy,\n expectedRole: settings.runtimeDatabaseRole,\n targetSchema: settings.dbSchema.trim() || \"public\",\n organizationTenancyCanonicalActivationEnabled:\n settings.organizationTenancyCanonicalActivationEnabled,\n } as const;\n const controlPlaneAuth = resolveNatsControlPlaneAuth(settings);\n let bus: Awaited<ReturnType<typeof createNatsEventBus>> | undefined;\n try {\n await retryStartupDependency(\n \"PostgreSQL runtime posture\",\n () => assertRuntimeDatabasePosture(dbClient.db, databasePosture),\n { ...retryOptions, onRetry, shouldRetry: isRetryableRuntimeDatabaseStartupError },\n );\n bus = await retryStartupDependency(\n \"NATS\",\n () =>\n createNatsEventBus(\n settings.natsUrl,\n controlPlaneAuth\n ? { user: controlPlaneAuth.user, pass: controlPlaneAuth.password }\n : undefined,\n observabilityEventBusOptions(observability),\n ),\n { ...retryOptions, onRetry },\n );\n await runOpenGeniWorker({\n role,\n settings,\n databasePosture,\n http: { readinessDb: readinessDbClient.db },\n activityDependencies: {\n observability,\n db: dbClient.db,\n bus,\n },\n });\n } finally {\n await Promise.allSettled([bus?.close(), readinessDbClient.close(), dbClient.close()]);\n disposeUnhandledRejectionBoundary();\n }\n}\n\nif (import.meta.main) {\n // `bun --watch` keeps its supervisor alive after the application consumes\n // SIGINT. Once the worker, NATS, DB, and Temporal resources have all closed,\n // explicitly terminate that supervisor too; otherwise a later file edit can\n // resurrect this stopped process as a duplicate worker with stale env/code.\n let interrupted = false;\n const markInterrupted = () => {\n interrupted = true;\n };\n process.once(\"SIGINT\", markInterrupted);\n try {\n await startWorker();\n } finally {\n process.off(\"SIGINT\", markInterrupted);\n if (interrupted) process.exit(0);\n }\n}\n","import {\n ActivityCancellationType,\n ActivityFailure,\n ApplicationFailure,\n proxyActivities,\n} from \"@temporalio/workflow\";\nimport type * as activities from \"../activities\";\nimport {\n KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS,\n KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS,\n} from \"../knowledge-source-sync-activity-policy\";\n\ntype WorkflowControlActivities = Pick<\n typeof activities,\n | \"enqueueGoalRetryWake\"\n | \"expireSessionHumanInput\"\n | \"expireSessionInteractionIntervention\"\n | \"expireScheduledRunHumanWait\"\n | \"failSessionAttempt\"\n | \"getCodexCapacityWait\"\n | \"markSessionIdle\"\n | \"peekSessionWork\"\n | \"persistSessionAttemptQuiescence\"\n | \"reconcileSessionAttemptQuiescence\"\n | \"reconcileCodexCapacityWait\"\n | \"recoverDispatch\"\n | \"recoverEscapedMcpTimeout\"\n | \"settleSessionInterruptions\"\n | \"settleSessionInputWait\"\n>;\n\n/**\n * Session/schedule workflow control activities are bounded, idempotent database\n * and provider-metadata operations. They must not inherit the agent turn's\n * 30-day attempt: Temporal cannot otherwise detect that the pod which accepted\n * a non-heartbeating control activity disappeared, leaving the workflow pinned\n * to a dead worker for the full 30 days. A bounded attempt plus an unbounded\n * Temporal retry keeps the durable workflow alive across rollout, node loss, or\n * transient database/network failure; retries re-run on a healthy control pod.\n */\nexport const activity = proxyActivities<WorkflowControlActivities>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n },\n});\n\n/** One schedule occurrence is bounded. Transient control-plane failures get a\n * short retry window, but a revoked binding or other permanent task error must\n * not leave a Temporal activity retrying forever; the next scheduled occurrence\n * is an independent workflow and will re-evaluate current state. */\nexport const scheduledTaskActivity = proxyActivities<\n Pick<typeof activities, \"dispatchScheduledTaskRun\">\n>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 5,\n },\n});\n\nexport const automationActivity = proxyActivities<Pick<typeof activities, \"dispatchAutomationRun\">>(\n {\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 5,\n },\n },\n);\n\n/** Dispatch has a bounded retry window, but an accepted automation event must\n * not remain durably `dispatching` after that window closes. This compensating\n * database write is idempotent and retries without a cap until the run reaches\n * a terminal state (or is observed as already dispatched). */\nexport const automationFailureActivity = proxyActivities<\n Pick<typeof activities, \"settleAutomationRunFailure\">\n>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n },\n});\n\n/** Goal continuation evaluates a durable Postgres obligation at an idle\n * boundary. A transient failure gets a short retry window, then records an\n * explicit delayed outbox wake instead of relying on an unrelated mutation or\n * keeping workflow history alive with polling. */\nexport const goalActivity = proxyActivities<Pick<typeof activities, \"maybeContinueGoal\">>({\n startToCloseTimeout: \"30 seconds\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"5 seconds\",\n maximumAttempts: 3,\n },\n});\n\nexport function turnTaskQueue(baseTaskQueue: string): string {\n return `${baseTaskQueue}-turns`;\n}\n\nexport function turnActivityForTaskQueue(baseTaskQueue: string, receiptGatedCancellation = true) {\n return proxyActivities<Pick<typeof activities, \"runAgentTurn\">>({\n taskQueue: turnTaskQueue(baseTaskQueue),\n // Agent segments legitimately run for days. A started turn heartbeats;\n // queued activities remain queued truthfully until a capped turn worker\n // accepts them and performs the atomic claim.\n startToCloseTimeout: \"30 days\",\n heartbeatTimeout: \"2 minutes\",\n // Pause/Steer first closes the exact attempt in Postgres, then asks\n // Temporal to deliver cancellation. The workflow must not wait for the\n // Temporal activity promise: a provider cleanup promise can outlive the\n // fenced activity body, and Temporal terminalization is not proof that\n // sandbox tools or processes are physically quiescent. The activity owns\n // that proof and writes an exact receipt after its hard tool fence; the\n // receipt transaction wakes the workflow to admit a replacement.\n cancellationType: receiptGatedCancellation\n ? ActivityCancellationType.TRY_CANCEL\n : ActivityCancellationType.WAIT_CANCELLATION_COMPLETED,\n retry: { maximumAttempts: 1 },\n });\n}\n\nexport function videoGenerationActivityForTaskQueue(baseTaskQueue: string) {\n return proxyActivities<Pick<typeof activities, \"reconcileVideoGenerationOperation\">>({\n taskQueue: turnTaskQueue(baseTaskQueue),\n startToCloseTimeout: \"20 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 3,\n },\n });\n}\n\nexport const documentActivity = proxyActivities<Pick<typeof activities, \"indexDocument\">>({\n startToCloseTimeout: \"30 minutes\",\n retry: { maximumAttempts: 1 },\n});\n\n/** Connector inventory/download/index batches are provider-I/O activities. The\n * implementation heartbeats between bounded items and owns its resumable\n * checkpoint; Temporal retries therefore repeat only idempotent source/object\n * operations and never invoke the agent runtime. */\nexport const knowledgeSourceSyncActivity = proxyActivities<\n Pick<typeof activities, \"runKnowledgeSourceSyncBatch\">\n>({\n startToCloseTimeout: \"1 hour\",\n heartbeatTimeout: KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS,\n retry: {\n initialInterval: \"2 seconds\",\n backoffCoefficient: 2,\n maximumInterval: \"1 minute\",\n maximumAttempts: KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS,\n },\n});\n\nexport function workflowFailureMessage(error: unknown): string {\n if (\n error instanceof ActivityFailure &&\n error.activityType === \"runAgentTurn\" &&\n error.cause instanceof ApplicationFailure &&\n error.cause.type === \"TurnExecutionPolicyDefinitionMismatchError\"\n ) {\n return error.cause.message;\n }\n if (error instanceof Error) {\n return error.message;\n }\n return String(error);\n}\n","export const KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS = 120_000;\nexport const KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS = 1;\n","import type { Settings } from \"@opengeni/config\";\nimport {\n assertRuntimeDatabasePosture,\n dbSql,\n type Database,\n type RuntimeDatabasePostureOptions,\n} from \"@opengeni/db\";\nimport { requireSessionEventDurableFanoutCapability, type EventBus } from \"@opengeni/events\";\nimport { githubAppBotIdentityWarnings } from \"@opengeni/github\";\nimport type { Observability } from \"@opengeni/observability\";\n\nexport type ReadinessCheckName = \"db\" | \"nats\" | \"temporal\";\nexport type ReadinessChecks = Record<ReadinessCheckName, () => Promise<void> | void>;\n\nexport type ReadinessResult = {\n ok: boolean;\n state?: WorkerLifecycleState;\n checks: Record<ReadinessCheckName, { ok: boolean; error?: string }>;\n};\n\nexport type WorkerLifecycleState = \"starting\" | \"ready\" | \"draining\" | \"stopped\" | \"failed\";\n\nexport type WorkerHttpLifecycle = {\n role: \"control\" | \"turn\";\n state: () => WorkerLifecycleState;\n};\n\nexport type WorkerHttpServerInput = {\n settings: Settings;\n observability: Observability;\n checks: ReadinessChecks;\n timeoutMs?: number;\n lifecycle?: WorkerHttpLifecycle;\n};\n\nexport function createWorkerHttpHandler(\n input: WorkerHttpServerInput,\n): (request: Request) => Promise<Response> {\n const { settings, observability, checks, lifecycle } = input;\n const timeoutMs = input.timeoutMs ?? 2_000;\n return async (request) => {\n const url = new URL(request.url);\n if (request.method !== \"GET\") {\n return Response.json({ error: \"method_not_allowed\" }, { status: 405 });\n }\n if (url.pathname === \"/healthz\") {\n const state = lifecycle?.state();\n const ok = state !== \"failed\" && state !== \"stopped\";\n const warnings = githubAppBotIdentityWarnings(settings);\n return Response.json(\n {\n service: settings.serviceName,\n environment: settings.environment,\n deploymentRevision: settings.deploymentRevision,\n ...(lifecycle ? { role: lifecycle.role, state } : {}),\n ...(warnings.length > 0 ? { warnings } : {}),\n ok,\n },\n { status: ok ? 200 : 503 },\n );\n }\n if (url.pathname === \"/metrics\") {\n return new Response(await observability.prometheusMetrics(), {\n status: 200,\n headers: { \"content-type\": \"text/plain; version=0.0.4; charset=utf-8\" },\n });\n }\n if (url.pathname === \"/readyz\") {\n const state = lifecycle?.state();\n const result =\n state && state !== \"ready\"\n ? {\n ok: false,\n state,\n checks: Object.fromEntries(\n (Object.keys(checks) as ReadinessCheckName[]).map((name) => [\n name,\n { ok: false, error: \"worker_not_ready\" },\n ]),\n ) as ReadinessResult[\"checks\"],\n }\n : { ...(await runReadinessChecks(checks, timeoutMs)), ...(state ? { state } : {}) };\n return Response.json(result, { status: result.ok ? 200 : 503 });\n }\n return Response.json({ error: \"not_found\" }, { status: 404 });\n };\n}\n\nexport function startWorkerHttpServer(input: WorkerHttpServerInput): ReturnType<typeof Bun.serve> {\n return Bun.serve({\n hostname: \"0.0.0.0\",\n port: input.settings.workerHttpPort,\n fetch: createWorkerHttpHandler(input),\n });\n}\n\nexport async function runReadinessChecks(\n checks: ReadinessChecks,\n timeoutMs = 2_000,\n): Promise<ReadinessResult> {\n const entries = await Promise.all(\n (Object.entries(checks) as Array<[ReadinessCheckName, () => Promise<void> | void]>).map(\n async ([name, check]) => {\n try {\n await withTimeout(Promise.resolve().then(check), timeoutMs);\n return [name, { ok: true }] as const;\n } catch {\n return [name, { ok: false, error: \"dependency_unavailable\" }] as const;\n }\n },\n ),\n );\n const result = Object.fromEntries(entries) as ReadinessResult[\"checks\"];\n return {\n ok: Object.values(result).every((check) => check.ok),\n checks: result,\n };\n}\n\nexport function dbReadyCheck(\n db: Database,\n posture?: RuntimeDatabasePostureOptions,\n): () => Promise<void> {\n let inFlight: Promise<void> | undefined;\n return () => {\n // The HTTP timeout cannot cancel a driver query. Reuse an outstanding\n // attempt so repeated kubelet probes cannot queue an unbounded tail behind\n // one slow or disconnected database connection.\n if (inFlight) return inFlight;\n inFlight = (async () => {\n if (posture) {\n await assertRuntimeDatabasePosture(db, posture);\n return;\n }\n await db.execute(dbSql`select 1`);\n })().finally(() => {\n inFlight = undefined;\n });\n return inFlight;\n };\n}\n\nexport function natsReadyCheck(bus: EventBus): () => void {\n return () => {\n requireSessionEventDurableFanoutCapability(bus);\n if (bus.isConnected && !bus.isConnected()) {\n throw new Error(\"NATS is not connected\");\n }\n };\n}\n\nexport function temporalReadyCheck(\n connection: Pick<\n import(\"@temporalio/worker\").NativeConnection,\n \"workflowService\" | \"withDeadline\"\n >,\n): () => Promise<void> {\n return async () => {\n await connection.withDeadline(Date.now() + 2_000, () =>\n connection.workflowService.getSystemInfo({}),\n );\n };\n}\n\nasync function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n promise,\n new Promise<never>((_, reject) => {\n timer = setTimeout(\n () => reject(new Error(`readiness check timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n","import type { Observability } from \"@opengeni/observability\";\nimport type { WorkerLifecycleState } from \"./http\";\n\nexport async function constructWithOwnedConnection<Connection, Result>(\n connect: () => Promise<Connection>,\n construct: (connection: Connection) => Promise<Result>,\n close: (connection: Connection) => Promise<void>,\n): Promise<Result> {\n const connection = await connect();\n try {\n return await construct(connection);\n } catch (error) {\n await close(connection).catch(() => undefined);\n throw error;\n }\n}\n\nexport type WorkerRunTarget = {\n run(): Promise<void>;\n shutdown(): void;\n};\n\n/** One process may poll several disjoint Temporal queues, but it still has one\n * host lifecycle. Start and stop every poller as a unit; a failed poller drains\n * its siblings and preserves the original failure. */\nexport function combineWorkerRunTargets(targets: readonly WorkerRunTarget[]): WorkerRunTarget {\n if (targets.length === 0) {\n throw new Error(\"a worker service requires at least one Temporal poller\");\n }\n let runPromise: Promise<void> | undefined;\n const shutdown = (): void => {\n let firstError: unknown;\n for (const target of targets) {\n try {\n target.shutdown();\n } catch (error) {\n firstError ??= error;\n }\n }\n if (firstError !== undefined) throw firstError;\n };\n return {\n run: () => {\n runPromise ??= (async () => {\n const runs = targets.map((target) => {\n try {\n return target.run();\n } catch (error) {\n return Promise.reject(error);\n }\n });\n try {\n await Promise.all(runs);\n } catch (error) {\n try {\n shutdown();\n } catch {\n // Preserve the poller failure. Every sibling still received its\n // shutdown request even when one request itself failed.\n }\n await Promise.allSettled(runs);\n throw error;\n }\n })();\n return runPromise;\n },\n shutdown,\n };\n}\n\nexport type WorkerServiceLifecycle = {\n state(): WorkerLifecycleState;\n run(): Promise<void>;\n drain(reason?: string): boolean;\n close(): Promise<void>;\n};\n\nexport function createWorkerServiceLifecycle(input: {\n role: \"control\" | \"turn\";\n worker: WorkerRunTarget;\n observability: Observability;\n closeOwnedResources: () => Promise<void>;\n onReady?: () => void;\n}): WorkerServiceLifecycle {\n let state: WorkerLifecycleState = \"starting\";\n let runPromise: Promise<void> | undefined;\n let resourcesClosed: Promise<void> | undefined;\n const closeOwnedResources = () => {\n resourcesClosed ??= input.closeOwnedResources();\n return resourcesClosed;\n };\n\n const lifecycle: WorkerServiceLifecycle = {\n state: () => state,\n run: () => {\n if (!runPromise && state === \"draining\") {\n runPromise = (async () => {\n state = \"stopped\";\n await closeOwnedResources();\n })();\n return runPromise;\n }\n if (!runPromise && (state === \"stopped\" || state === \"failed\")) {\n return Promise.reject(new Error(`cannot run a worker service that is ${state}`));\n }\n runPromise ??= (async () => {\n if (state === \"starting\") {\n state = \"ready\";\n }\n input.onReady?.();\n try {\n await input.worker.run();\n state = \"stopped\";\n } catch (error) {\n state = \"failed\";\n throw error;\n } finally {\n await closeOwnedResources();\n }\n })();\n return runPromise;\n },\n drain: (_reason = \"host request\") => {\n if (state === \"draining\" || state === \"stopped\" || state === \"failed\") {\n return true;\n }\n const previousState = state;\n safeLifecycleLog(() =>\n input.observability.info(\"OpenGeni worker draining (graceful shutdown)\", {\n role: input.role,\n errorClass: \"WorkerLifecycleOperation\",\n errorCode: \"worker_draining\",\n origin: \"worker-lifecycle\",\n }),\n );\n state = \"draining\";\n try {\n input.worker.shutdown();\n return true;\n } catch {\n state = previousState;\n safeLifecycleLog(() =>\n input.observability.warn(\"worker shutdown request failed\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_shutdown_request_failed\",\n origin: \"worker-lifecycle\",\n }),\n );\n return false;\n }\n },\n close: async () => {\n if (!lifecycle.drain(\"service close\")) {\n throw new Error(\"worker shutdown request failed\");\n }\n if (runPromise) {\n await runPromise.catch(() => undefined);\n } else {\n state = \"stopped\";\n await closeOwnedResources();\n }\n },\n };\n\n return lifecycle;\n}\n\nfunction safeLifecycleLog(log: () => void): void {\n try {\n log();\n } catch {\n // Telemetry cannot change worker lifecycle state or shutdown delivery.\n }\n}\n","import { readFileSync } from \"node:fs\";\nimport { freemem, totalmem } from \"node:os\";\nimport { posix } from \"node:path\";\nimport type { Settings } from \"@opengeni/config\";\nimport type { Observability } from \"@opengeni/observability\";\n\nexport type TurnWorkerMemoryScope = \"host\" | \"cgroup\";\n\nexport type TurnWorkerMemoryScopeSnapshot = {\n scope: TurnWorkerMemoryScope;\n usedBytes: number;\n limitBytes: number;\n};\n\nexport type TurnWorkerMemoryPressureSnapshot = {\n processRssBytes: number;\n processHeapUsedBytes: number;\n processExternalBytes: number;\n processArrayBuffersBytes: number;\n scopes: TurnWorkerMemoryScopeSnapshot[];\n};\n\nexport type TurnWorkerMemoryPressureGuard = {\n sampleNow(): void;\n close(): void;\n};\n\ntype GuardSettings = Pick<\n Settings,\n | \"turnWorkerConcurrencyMode\"\n | \"turnWorkerTargetMemoryUsage\"\n | \"turnWorkerEmergencyMemoryUsage\"\n | \"turnWorkerMemoryGuardIntervalMs\"\n | \"turnWorkerMemoryGuardSustainMs\"\n>;\n\ntype GuardDependencies = {\n now?: () => number;\n sample?: () => TurnWorkerMemoryPressureSnapshot;\n setInterval?: typeof setInterval;\n clearInterval?: typeof clearInterval;\n gc?: () => void;\n};\n\ntype SnapshotDependencies = {\n readText?: (path: string) => string | null;\n hostTotalBytes?: () => number;\n hostAvailableBytes?: () => number;\n processMemoryUsage?: () => Pick<\n NodeJS.MemoryUsage,\n \"rss\" | \"heapUsed\" | \"external\" | \"arrayBuffers\"\n >;\n};\n\nexport type ProcessCgroupMembership = {\n v2Path: string | null;\n v1MemoryPath: string | null;\n};\n\nexport type CgroupMemoryFilePair = {\n currentPath: string;\n maxPath: string;\n};\n\nconst CGROUP_V2_MOUNT = \"/sys/fs/cgroup\";\nconst CGROUP_V1_MEMORY_MOUNT = \"/sys/fs/cgroup/memory\";\n\nexport function parseLinuxMeminfo(\n value: string,\n): { totalBytes: number; availableBytes: number } | null {\n const fields = new Map<string, number>();\n for (const line of value.split(\"\\n\")) {\n const match = /^([A-Za-z_()]+):\\s+(\\d+)\\s+kB$/.exec(line.trim());\n if (!match) continue;\n fields.set(match[1] as string, Number(match[2]) * 1024);\n }\n const totalBytes = fields.get(\"MemTotal\");\n const availableBytes = fields.get(\"MemAvailable\");\n if (\n totalBytes === undefined ||\n availableBytes === undefined ||\n !Number.isFinite(totalBytes) ||\n !Number.isFinite(availableBytes) ||\n totalBytes <= 0 ||\n availableBytes < 0\n ) {\n return null;\n }\n return { totalBytes, availableBytes: Math.min(totalBytes, availableBytes) };\n}\n\nexport function parseFiniteCgroupMemoryLimit(value: string): number | null {\n const trimmed = value.trim();\n if (!trimmed || trimmed === \"max\") return null;\n const parsed = Number(trimmed);\n // cgroup v1 represents \"unlimited\" with a near-int64 sentinel. Treat only\n // realistic finite limits as authority; host headroom remains the fallback.\n if (!Number.isSafeInteger(parsed) || parsed <= 0 || parsed >= 2 ** 60) return null;\n return parsed;\n}\n\nexport function parseProcessCgroupMembership(value: string): ProcessCgroupMembership {\n let v2Path: string | null = null;\n let v1MemoryPath: string | null = null;\n for (const line of value.split(\"\\n\")) {\n const firstSeparator = line.indexOf(\":\");\n const secondSeparator = line.indexOf(\":\", firstSeparator + 1);\n if (firstSeparator <= 0 || secondSeparator < 0) continue;\n const hierarchyId = line.slice(0, firstSeparator);\n const controllers = line.slice(firstSeparator + 1, secondSeparator);\n const path = normalizeCgroupPath(line.slice(secondSeparator + 1));\n if (!path) continue;\n if (hierarchyId === \"0\" && controllers === \"\") {\n v2Path = path;\n }\n if (controllers.split(\",\").includes(\"memory\")) {\n v1MemoryPath = path;\n }\n }\n return { v2Path, v1MemoryPath };\n}\n\nexport function cgroupMemoryFilePairs(membership: ProcessCgroupMembership): CgroupMemoryFilePair[] {\n const pairs: CgroupMemoryFilePair[] = [];\n const seen = new Set<string>();\n const appendHierarchy = (\n mount: string,\n path: string | null,\n currentFilename: string,\n maxFilename: string,\n ) => {\n for (const candidate of cgroupAncestorPaths(path ?? \"/\")) {\n const relative = candidate === \"/\" ? \"\" : candidate.slice(1);\n const currentPath = posix.join(mount, relative, currentFilename);\n const maxPath = posix.join(mount, relative, maxFilename);\n const key = `${currentPath}\\0${maxPath}`;\n if (seen.has(key)) continue;\n seen.add(key);\n pairs.push({ currentPath, maxPath });\n }\n };\n\n appendHierarchy(CGROUP_V2_MOUNT, membership.v2Path, \"memory.current\", \"memory.max\");\n appendHierarchy(\n CGROUP_V1_MEMORY_MOUNT,\n membership.v1MemoryPath,\n \"memory.usage_in_bytes\",\n \"memory.limit_in_bytes\",\n );\n return pairs;\n}\n\nexport function readTurnWorkerMemoryPressureSnapshot(\n dependencies: SnapshotDependencies = {},\n): TurnWorkerMemoryPressureSnapshot {\n const readText = dependencies.readText ?? readTextOrNull;\n const linux = parseLinuxMeminfo(readText(\"/proc/meminfo\") ?? \"\");\n const hostTotalBytes = linux?.totalBytes ?? (dependencies.hostTotalBytes ?? totalmem)();\n const hostAvailableBytes =\n linux?.availableBytes ?? (dependencies.hostAvailableBytes ?? freemem)();\n const host = {\n scope: \"host\" as const,\n usedBytes: Math.max(0, hostTotalBytes - Math.min(hostTotalBytes, hostAvailableBytes)),\n limitBytes: hostTotalBytes,\n };\n const cgroup = readCgroupMemoryScope(readText);\n // A finite process cgroup is the pod's actual admission boundary. Host\n // pressure includes unrelated workloads and must not make every replica\n // drain simultaneously; use it only when no finite cgroup authority exists.\n const processMemory = (dependencies.processMemoryUsage ?? process.memoryUsage)();\n\n return {\n processRssBytes: processMemory.rss,\n processHeapUsedBytes: processMemory.heapUsed,\n processExternalBytes: processMemory.external,\n processArrayBuffersBytes: processMemory.arrayBuffers,\n scopes: [cgroup ?? host],\n };\n}\n\nexport function turnWorkerMemoryPressureGuardEnabled(\n role: \"control\" | \"turn\",\n settings: Pick<Settings, \"turnWorkerConcurrencyMode\">,\n): boolean {\n return role === \"turn\" && settings.turnWorkerConcurrencyMode === \"resource-based\";\n}\n\nexport function createTurnWorkerMemoryPressureGuard(input: {\n settings: GuardSettings;\n observability: Observability;\n drain: () => void;\n dependencies?: GuardDependencies;\n}): TurnWorkerMemoryPressureGuard {\n const now = input.dependencies?.now ?? Date.now;\n const sample = input.dependencies?.sample ?? readTurnWorkerMemoryPressureSnapshot;\n const schedule = input.dependencies?.setInterval ?? setInterval;\n const cancel = input.dependencies?.clearInterval ?? clearInterval;\n const collectGarbage = input.dependencies?.gc ?? (() => Bun.gc(false));\n const breachStartedAt = new Map<TurnWorkerMemoryScope, number>();\n let lastGcAt = Number.NEGATIVE_INFINITY;\n let drainRequested = false;\n let closed = false;\n\n const sampleNow = () => {\n if (closed) return;\n const observedAt = now();\n let snapshot: TurnWorkerMemoryPressureSnapshot;\n try {\n snapshot = sample();\n } catch {\n breachStartedAt.clear();\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_sample_failures_total\",\n help: \"Turn-worker memory pressure guard samples that could not be read.\",\n });\n return;\n }\n\n const validScopes = snapshot.scopes.filter(\n ({ usedBytes, limitBytes }) =>\n Number.isFinite(usedBytes) &&\n Number.isFinite(limitBytes) &&\n usedBytes >= 0 &&\n limitBytes > 0,\n );\n if (validScopes.length === 0) {\n breachStartedAt.clear();\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_sample_failures_total\",\n help: \"Turn-worker memory pressure guard samples that could not be read.\",\n });\n return;\n }\n\n const sustainedBreaches: Array<{\n scope: TurnWorkerMemoryScope;\n utilizationRatio: number;\n breachSeconds: number;\n }> = [];\n for (const [kind, value] of [\n [\"rss\", snapshot.processRssBytes],\n [\"heap_used\", snapshot.processHeapUsedBytes],\n [\"external\", snapshot.processExternalBytes],\n [\"array_buffers\", snapshot.processArrayBuffersBytes],\n ] as const) {\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_process_memory_bytes\",\n help: \"Turn-worker process memory by runtime-reported category.\",\n labels: { kind },\n value: Number.isFinite(value) && value >= 0 ? value : 0,\n });\n }\n let shouldCollectGarbage = false;\n for (const scope of validScopes) {\n const utilizationRatio = scope.usedBytes / scope.limitBytes;\n const processRssRatio = snapshot.processRssBytes / scope.limitBytes;\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_utilization_ratio\",\n help: \"Observed memory utilization ratio used by the turn-worker pressure guard.\",\n labels: { scope: scope.scope },\n value: utilizationRatio,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_target_ratio\",\n help: \"Memory admission target enforced by the Temporal resource tuner.\",\n labels: { scope: scope.scope },\n value: input.settings.turnWorkerTargetMemoryUsage,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_emergency_ratio\",\n help: \"Emergency memory threshold enforced by the turn-worker pressure guard.\",\n labels: { scope: scope.scope },\n value: input.settings.turnWorkerEmergencyMemoryUsage,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_available_bytes\",\n help: \"Memory headroom remaining in the turn-worker pressure guard scope.\",\n labels: { scope: scope.scope },\n value: Math.max(0, scope.limitBytes - scope.usedBytes),\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_process_rss_ratio\",\n help: \"Turn-worker RSS as a ratio of the observed host or finite cgroup memory scope.\",\n labels: { scope: scope.scope },\n value: processRssRatio,\n });\n shouldCollectGarbage ||= processRssRatio >= input.settings.turnWorkerTargetMemoryUsage;\n\n if (utilizationRatio < input.settings.turnWorkerEmergencyMemoryUsage) {\n breachStartedAt.delete(scope.scope);\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_breach_seconds\",\n help: \"Seconds the turn-worker emergency threshold has remained continuously breached.\",\n labels: { scope: scope.scope },\n value: 0,\n });\n continue;\n }\n\n const startedAt = breachStartedAt.get(scope.scope) ?? observedAt;\n breachStartedAt.set(scope.scope, startedAt);\n const breachMs = Math.max(0, observedAt - startedAt);\n const breachSeconds = breachMs / 1_000;\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_breach_seconds\",\n help: \"Seconds the turn-worker emergency threshold has remained continuously breached.\",\n labels: { scope: scope.scope },\n value: breachSeconds,\n });\n if (breachMs >= input.settings.turnWorkerMemoryGuardSustainMs) {\n sustainedBreaches.push({ scope: scope.scope, utilizationRatio, breachSeconds });\n }\n }\n\n // Large request buffers can become unreachable well before JSC's ordinary\n // heap trigger notices native/external pressure. Nudge an asynchronous GC\n // at most once per sustain window before escalating to a worker drain.\n const sustainedBreachesHadGcOpportunity =\n sustainedBreaches.length > 0 &&\n sustainedBreaches.every(({ scope }) => {\n const breachStarted = breachStartedAt.get(scope);\n return breachStarted !== undefined && lastGcAt >= breachStarted && lastGcAt < observedAt;\n });\n let collectedGarbageThisSample = false;\n if (\n shouldCollectGarbage &&\n observedAt - lastGcAt >= input.settings.turnWorkerMemoryGuardSustainMs &&\n (sustainedBreaches.length === 0 || !sustainedBreachesHadGcOpportunity)\n ) {\n lastGcAt = observedAt;\n try {\n collectGarbage();\n collectedGarbageThisSample = true;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_gc_total\",\n help: \"Proactive garbage-collection nudges under process memory pressure.\",\n });\n } catch {\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_gc_failures_total\",\n help: \"Proactive memory-pressure garbage-collection nudges that failed.\",\n });\n }\n }\n\n // The nudge is asynchronous. Give it one sampling interval to make the\n // reclaimed RSS visible before deciding that process recycling is needed.\n if (\n (collectedGarbageThisSample && !sustainedBreachesHadGcOpportunity) ||\n drainRequested ||\n sustainedBreaches.length === 0\n ) {\n return;\n }\n\n const primaryBreach = sustainedBreaches.sort(\n (left, right) => right.utilizationRatio - left.utilizationRatio,\n )[0];\n if (!primaryBreach) return;\n\n drainRequested = true;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_drains_total\",\n help: \"Graceful turn-worker drains requested after sustained memory pressure.\",\n labels: { scope: primaryBreach.scope },\n });\n safeWarn(input.observability, \"turn worker memory guard requested graceful drain\", {\n errorClass: \"WorkerLifecycleOperation\",\n errorCode: \"worker_draining\",\n origin: \"worker-lifecycle\",\n reason: `memory pressure (${primaryBreach.scope}, ${boundedRatio(primaryBreach.utilizationRatio)} >= ${input.settings.turnWorkerEmergencyMemoryUsage}, ${primaryBreach.breachSeconds}s)`,\n });\n try {\n input.drain();\n } catch {\n drainRequested = false;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_drain_failures_total\",\n help: \"Turn-worker memory pressure guard drain requests that failed synchronously.\",\n labels: { scope: primaryBreach.scope },\n });\n safeWarn(input.observability, \"turn worker memory guard drain request failed\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_shutdown_request_failed\",\n origin: \"worker-lifecycle\",\n });\n }\n };\n\n const timer = schedule(sampleNow, input.settings.turnWorkerMemoryGuardIntervalMs);\n timer.unref?.();\n sampleNow();\n\n return {\n sampleNow,\n close: () => {\n if (closed) return;\n closed = true;\n cancel(timer);\n },\n };\n}\n\nfunction readCgroupMemoryScope(\n readText: (path: string) => string | null,\n): TurnWorkerMemoryScopeSnapshot | null {\n const membership = parseProcessCgroupMembership(readText(\"/proc/self/cgroup\") ?? \"\");\n let mostPressured: TurnWorkerMemoryScopeSnapshot | null = null;\n for (const { currentPath, maxPath } of cgroupMemoryFilePairs(membership)) {\n const currentRaw = readText(currentPath);\n const maxRaw = readText(maxPath);\n if (currentRaw === null || maxRaw === null) continue;\n const current = Number(currentRaw.trim());\n const limit = parseFiniteCgroupMemoryLimit(maxRaw);\n if (!Number.isFinite(current) || current < 0 || limit === null) continue;\n const candidate = { scope: \"cgroup\" as const, usedBytes: current, limitBytes: limit };\n if (\n !mostPressured ||\n candidate.usedBytes / candidate.limitBytes >\n mostPressured.usedBytes / mostPressured.limitBytes\n ) {\n mostPressured = candidate;\n }\n }\n return mostPressured;\n}\n\nfunction readTextOrNull(path: string): string | null {\n try {\n return readFileSync(path, \"utf8\");\n } catch {\n return null;\n }\n}\n\nfunction boundedRatio(value: number): number {\n return Math.round(Math.max(0, Math.min(value, 10)) * 1_000_000) / 1_000_000;\n}\n\nfunction normalizeCgroupPath(value: string): string | null {\n const trimmed = value.trim();\n if (!trimmed.startsWith(\"/\") || trimmed.includes(\"\\0\")) return null;\n if (trimmed.split(\"/\").includes(\"..\")) return null;\n return posix.normalize(trimmed);\n}\n\nfunction cgroupAncestorPaths(path: string): string[] {\n const ancestors: string[] = [];\n let current = path;\n while (true) {\n ancestors.push(current);\n if (current === \"/\") return ancestors;\n current = posix.dirname(current);\n }\n}\n\nfunction safeSetGauge(\n observability: Observability,\n input: Parameters<Observability[\"setGauge\"]>[0],\n): void {\n try {\n observability.setGauge(input);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n\nfunction safeIncrementCounter(\n observability: Observability,\n input: Parameters<Observability[\"incrementCounter\"]>[0],\n): void {\n try {\n observability.incrementCounter(input);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n\nfunction safeWarn(\n observability: Observability,\n message: string,\n attributes: Parameters<Observability[\"warn\"]>[1],\n): void {\n try {\n observability.warn(message, attributes);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n","export type WorkerUnhandledRejectionProcess = {\n on: (event: \"unhandledRejection\", listener: (reason: unknown) => void) => void;\n off: (event: \"unhandledRejection\", listener: (reason: unknown) => void) => void;\n};\n\nexport type WorkerUnhandledRejectionClassification =\n | \"detached_runtime_mcp_lifecycle\"\n | \"detached_unknown\";\n\ntype RuntimeMcpLifecycleError = {\n name: \"McpLifecycleError\";\n code: \"mcp_connect_failed\" | \"mcp_close_failed\";\n origin: \"runtime\";\n serverId: string;\n};\n\n/**\n * The Agents SDK owns MCP connect promises and also installs a process-global\n * unhandled-rejection listener. A best-effort lifecycle rejection can surface\n * twice under concurrent teardown: once through the awaited MCP result and once\n * through the SDK's detached worker promise. The first path already records and\n * degrades the server; the second must not terminate the whole turn-worker pod.\n */\nexport function isDetachedRuntimeMcpLifecycleRejection(\n reason: unknown,\n): reason is RuntimeMcpLifecycleError {\n try {\n if (!reason || typeof reason !== \"object\") return false;\n const candidate = reason as Partial<RuntimeMcpLifecycleError>;\n return (\n candidate.name === \"McpLifecycleError\" &&\n (candidate.code === \"mcp_connect_failed\" || candidate.code === \"mcp_close_failed\") &&\n candidate.origin === \"runtime\" &&\n typeof candidate.serverId === \"string\" &&\n candidate.serverId.length > 0\n );\n } catch {\n return false;\n }\n}\n\nfunction reportIsolatedUnhandledRejection(\n classification: WorkerUnhandledRejectionClassification,\n): void {\n // Never render the rejection itself: provider and transport errors can carry\n // request bodies or credentials. The durable activity path owns exact errors.\n console.error(\"[worker] isolated detached unhandled rejection\", { classification });\n}\n\n/**\n * Keep process-global promise rejection handling observational. Turn activities\n * have their own durable failure/lease boundary; killing the shared worker here\n * converts one detached SDK/transport promise into unrelated lease losses across\n * every session assigned to the pod. The Agents SDK also checks for a consumer\n * listener before applying its own process.exit(1), so this boundary must never\n * reintroduce that fail-fast behavior for an unknown rejection shape.\n */\nexport function installWorkerUnhandledRejectionBoundary(\n runtimeProcess: WorkerUnhandledRejectionProcess = process,\n report: (\n classification: WorkerUnhandledRejectionClassification,\n ) => void = reportIsolatedUnhandledRejection,\n): () => void {\n const onUnhandledRejection = (reason: unknown): void => {\n report(\n isDetachedRuntimeMcpLifecycleRejection(reason)\n ? \"detached_runtime_mcp_lifecycle\"\n : \"detached_unknown\",\n );\n };\n runtimeProcess.on(\"unhandledRejection\", onUnhandledRejection);\n return () => runtimeProcess.off(\"unhandledRejection\", onUnhandledRejection);\n}\n","export const BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID = \"opengeni-browser-deadline-checkpoints-v1\";\nconst QUEUE_SUFFIX = \"-browser-checkpoint-v1\";\nexport function browserDeadlineCheckpointTaskQueue(base: string): string {\n return base.endsWith(QUEUE_SUFFIX) ? base : `${base}${QUEUE_SUFFIX}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE,gCAAAA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA,8CAAAC;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAU;AAAA,EACV;AAAA,OACK;AACP,SAAS,kBAAkB,cAAyC;AACpE,SAAS,kBAAkB;AAC3B,SAAS,qBAAqB;;;ACvC9B;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACLA,IAAM,sDAAsD;AAC5D,IAAM,kDAAkD;;;ADuCxD,IAAM,WAAW,gBAA2C;AAAA,EACjE,qBAAqB;AAAA,EACrB,OAAO;AAAA,IACL,iBAAiB;AAAA,IACjB,oBAAoB;AAAA,IACpB,iBAAiB;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untimeDatabasePosture","requireSessionEventDurableFanoutCapability","activity","requireSessionEventDurableFanoutCapability","assertRuntimeDatabasePosture"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/workflows/activities.ts","../src/knowledge-source-sync-activity-policy.ts","../src/http.ts","../src/worker-service-lifecycle.ts","../src/memory-pressure-guard.ts","../src/unhandled-rejection-boundary.ts","../src/browser-deadline-checkpoint-contract.ts"],"sourcesContent":["import {\n dbSearchPath,\n getSettings,\n resolveNatsControlPlaneAuth,\n retryStartupDependency,\n startupRetryOptions,\n temporalConnectionOptions,\n type Settings,\n} from \"@opengeni/config\";\nimport {\n assertRuntimeDatabasePosture,\n isRetryableRuntimeDatabaseStartupError,\n countSessionRecoveryBacklog,\n createDb,\n getContextCompactionPendingSummary,\n markSessionWorkflowWakeDelivered,\n type Database,\n type RuntimeDatabasePostureOptions,\n} from \"@opengeni/db\";\nimport {\n createNatsEventBus,\n requireSessionEventDurableFanoutCapability,\n type EventBus,\n} from \"@opengeni/events\";\nimport {\n createObservability,\n logStartupDependencyRetry,\n type Observability,\n} from \"@opengeni/observability\";\nimport {\n Connection,\n isGrpcServiceError,\n ScheduleAlreadyRunning,\n ScheduleOverlapPolicy,\n Client as TemporalClient,\n WorkflowExecutionAlreadyStartedError,\n} from \"@temporalio/client\";\nimport { NativeConnection, Worker, type WorkflowBundleOption } from \"@temporalio/worker\";\nimport { existsSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport type { createControlActivities } from \"./activities-control\";\nimport type { createTurnActivities } from \"./activities-turn\";\nimport type { ActivityDependencies } from \"./activities/types\";\nimport type {\n InspectSessionAttemptActivity,\n SignalCodexCapacityWorkflow,\n SignalSessionAttemptQuiesced,\n StartSandboxReaperWorkflow,\n StartVideoGenerationWorkflow,\n WakeSessionWorkflowSignal,\n} from \"./activities/types\";\nimport { turnTaskQueue } from \"./workflows/activities\";\nimport {\n dbReadyCheck,\n natsReadyCheck,\n startWorkerHttpServer,\n temporalReadyCheck,\n type WorkerLifecycleState,\n} from \"./http\";\nimport {\n initializeContextCompactionMetrics,\n initializeWorkerOutcomeMetrics,\n normalizeTurnTaskQueueStats,\n observabilityEventBusOptions,\n startContextCompactionPendingMonitor,\n startSessionRecoveryMonitor,\n startTurnCapacityMonitor,\n type TurnTaskQueueStats,\n} from \"./observability-metrics\";\nimport {\n resolveCatalogSettings,\n SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS,\n SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n SESSION_WORKFLOW_WAKE_DISPATCHER_WORKFLOW_TYPE,\n TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n} from \"@opengeni/core\";\nimport {\n CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n createTurnWorkerConcurrencyPlan,\n turnWorkerConcurrencyLogFields,\n} from \"./concurrency\";\nimport {\n combineWorkerRunTargets,\n constructWithOwnedConnection,\n createWorkerServiceLifecycle,\n createWorkerCleanupContainment,\n type WorkerServiceLifecycle,\n type WorkerRunTarget,\n} from \"./worker-service-lifecycle\";\nimport {\n createTurnWorkerMemoryPressureGuard,\n turnWorkerMemoryPressureGuardEnabled,\n type TurnWorkerMemoryPressureGuard,\n} from \"./memory-pressure-guard\";\nimport { assertSandboxReaperActivityTimeout } from \"./sandbox-reaper-timeout\";\nimport { installWorkerUnhandledRejectionBoundary } from \"./unhandled-rejection-boundary\";\nimport {\n SANDBOX_REAPER_V2_WORKFLOW_ID,\n sandboxLifecycleTaskQueue,\n} from \"./sandbox-reaper-contract\";\nimport {\n BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID,\n browserDeadlineCheckpointTaskQueue,\n} from \"./browser-deadline-checkpoint-contract\";\n\nexport {\n createHostExportPump,\n type HostExportDrainResult,\n type HostExportPump,\n type HostExportPumpOptions,\n type HostEventExport,\n type HostEventExportBatch,\n type HostEventSink,\n type HostLifecycleFactExport,\n type HostLifecycleFactExportBatch,\n type HostLifecycleFactSink,\n type HostUsageExport,\n type HostUsageExportBatch,\n type HostUsageSink,\n} from \"./host-export\";\n\n// The deterministic id of the ONE global reaper Schedule. A single id means\n// create() is idempotent across every worker in the pool: the first worker to\n// boot creates it, all others collide on this id (ScheduleAlreadyRunning) and\n// no-op — so the Schedule is registered EXACTLY ONCE per deployment regardless\n// of replica count.\nconst SANDBOX_REAPER_SCHEDULE_ID = \"opengeni-sandbox-lease-reaper\";\nexport const KNOWLEDGE_INDEXING_SCHEDULE_ID = \"opengeni-knowledge-indexing\";\nexport const KNOWLEDGE_INDEXING_PERIOD_MS = 15_000;\nexport const FILE_UPLOAD_REAPER_SCHEDULE_ID = \"opengeni-file-upload-reaper\";\nexport const FILE_UPLOAD_REAPER_PERIOD_MS = 15 * 60 * 1_000;\nexport const SITE_AUTH_MAINTENANCE_SCHEDULE_ID = \"opengeni-site-auth-maintenance\";\nexport const SITE_AUTH_MAINTENANCE_PERIOD_MS = 60 * 1_000;\nexport type OpenGeniWorkerRole = \"control\" | \"turn\";\n\nexport type WorkerOptions = {\n role: OpenGeniWorkerRole;\n settings?: Settings;\n activities?: ReturnType<typeof createControlActivities> | ReturnType<typeof createTurnActivities>;\n activityDependencies?: ActivityDependencies;\n /** Override the release-coherent workflow artifact. Most hosts should omit this. */\n workflowBundle?: WorkflowBundleOption;\n};\n\ntype TemporalActivityLease = {\n lastHeartbeatTime?: { seconds?: unknown; nanos?: number | null } | null;\n lastStartedTime?: { seconds?: unknown; nanos?: number | null } | null;\n activityOptions?: {\n heartbeatTimeout?: { seconds?: unknown; nanos?: number | null } | null;\n } | null;\n};\n\nfunction temporalTimeMs(\n value: { seconds?: unknown; nanos?: number | null } | null | undefined,\n): number | null {\n if (value?.seconds === undefined || value.seconds === null) return null;\n return Number(String(value.seconds)) * 1_000 + Math.floor((value.nanos ?? 0) / 1_000_000);\n}\n\nexport function temporalActivityLeaseSettled(\n pending: TemporalActivityLease | null | undefined,\n nowMs = Date.now(),\n): boolean {\n if (!pending) return true;\n const heartbeatAt =\n temporalTimeMs(pending.lastHeartbeatTime) ?? temporalTimeMs(pending.lastStartedTime);\n const heartbeatTimeoutMs = temporalTimeMs(pending.activityOptions?.heartbeatTimeout);\n return (\n heartbeatAt !== null && heartbeatTimeoutMs !== null && nowMs >= heartbeatAt + heartbeatTimeoutMs\n );\n}\n\n/** The exact Temporal workflow run is absent, so it cannot retain an activity. */\nexport function temporalWorkflowExecutionNotFound(error: unknown): boolean {\n // gRPC status code 5 is NOT_FOUND. The typed guard prevents unrelated\n // provider/HTTP errors carrying a numeric code from proving quiescence.\n return isGrpcServiceError(error) && error.code === 5;\n}\n\n/** Load exactly one role's activity graph. Exported for embedded hosts and the\n * process-RSS conformance benchmark; construction remains side-effect free\n * until an activity first resolves its injected services. */\nexport async function createDefaultWorkerActivities(\n role: OpenGeniWorkerRole,\n dependencies: ActivityDependencies = {},\n) {\n return role === \"control\"\n ? (await import(\"./activities-control\")).createControlActivities(dependencies)\n : (await import(\"./activities-turn\")).createTurnActivities(dependencies);\n}\n\ntype WorkerWorkflowDefinition =\n | { workflowBundle: WorkflowBundleOption }\n | { workflowsPath: string };\n\n/**\n * Resolve the deterministic workflow graph without asking installed hosts to\n * transpile or relocate package TypeScript. Monorepo source execution retains\n * the source path for the local development loop; published dist execution\n * fails closed unless the build-generated sibling artifact is present.\n */\nexport function resolveOpenGeniWorkflowDefinition(\n moduleUrl: string = import.meta.url,\n): WorkerWorkflowDefinition {\n const modulePath = fileURLToPath(moduleUrl).replaceAll(\"\\\\\", \"/\");\n if (modulePath.endsWith(\"/src/index.ts\")) {\n return { workflowsPath: fileURLToPath(new URL(\"./workflows.ts\", moduleUrl)) };\n }\n const codePath = fileURLToPath(new URL(\"./workflow-bundle.js\", moduleUrl));\n if (!existsSync(codePath)) {\n throw new Error(\n `OpenGeni workflow bundle is missing at ${codePath}; rebuild or reinstall @opengeni/worker-bundle`,\n );\n }\n return { workflowBundle: { codePath } };\n}\n\nexport function turnCancellationHeartbeatThrottleOptions() {\n return {\n maxHeartbeatThrottleInterval: TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n defaultHeartbeatThrottleInterval: TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS,\n } as const;\n}\n\nexport async function createOpenGeniWorker(options: WorkerOptions): Promise<{\n worker: WorkerRunTarget;\n connection: NativeConnection;\n}> {\n const settings = options.settings ?? getSettings();\n if (options.role === \"control\") {\n // The exported lower-level factory is itself poll-capable. Validate here so\n // embedded hosts cannot bypass the reaper budget contract by omitting the\n // higher-level service and its Schedule-registration lifecycle.\n assertSandboxReaperActivityTimeout(settings);\n }\n const observability =\n options.activityDependencies?.observability ??\n createObservability(settings, { component: `worker-${options.role}` });\n initializeWorkerOutcomeMetrics(observability);\n if (options.role === \"turn\") {\n initializeContextCompactionMetrics(observability);\n }\n if (options.role === \"turn\" && options.workflowBundle) {\n throw new Error(\"workflowBundle is valid only for the control worker role\");\n }\n // Pre-resolve a PRIVATE-registry sandbox image before any turn creates a box.\n // A provider-native OPENGENI_MODAL_IMAGE_ID intentionally bypasses this\n // registry path and is resolved by ModalImageSelector.fromId at create time.\n // Otherwise this is a no-op unless the registry secret + logical ref are both\n // set. Memoized in the provider, so it runs once per process.\n if (options.role === \"turn\") {\n const { ensureModalRegistryImage } = await import(\"@opengeni/runtime/sandbox\");\n await retryStartupDependency(\n \"Modal private-registry image\",\n () => ensureModalRegistryImage(settings),\n {\n ...startupRetryOptions(settings),\n onRetry: (event) => logStartupDependencyRetry(observability, event),\n },\n );\n }\n return constructWithOwnedConnection(\n () =>\n retryStartupDependency(\n \"Temporal\",\n () => NativeConnection.connect(temporalConnectionOptions(settings)),\n {\n ...startupRetryOptions(settings),\n onRetry: (event) => logStartupDependencyRetry(observability, event),\n },\n ),\n async (connection) => {\n let turnWorker: Worker | undefined;\n let cleanupDrainRequested = false;\n const activityDependencies = {\n ...options.activityDependencies,\n settings,\n observability,\n requestWorkerDrain:\n options.activityDependencies?.requestWorkerDrain ??\n (() => {\n if (cleanupDrainRequested) return;\n if (!turnWorker) throw new Error(\"turn worker lifecycle is not initialized\");\n turnWorker.shutdown();\n cleanupDrainRequested = true;\n }),\n };\n const activities =\n options.activities ??\n (await createDefaultWorkerActivities(options.role, activityDependencies));\n const turnConcurrency =\n options.role === \"turn\"\n ? createTurnWorkerConcurrencyPlan(settings, { observability })\n : null;\n const workflowDefinition =\n options.role === \"control\"\n ? options.workflowBundle\n ? { workflowBundle: options.workflowBundle }\n : resolveOpenGeniWorkflowDefinition()\n : {};\n const sharedWorkerOptions = {\n connection,\n namespace: settings.temporalNamespace,\n activities,\n // Cancellation is delivered through an activity heartbeat. Keep the SDK\n // throttle aligned with runAgentTurn's local heartbeat cadence so the\n // four-second control contract still has time for physical writer drain\n // and exact receipt-gated replacement admission.\n ...turnCancellationHeartbeatThrottleOptions(),\n // GRACEFUL DEPLOY SHUTDOWN (with the SIGTERM handler in startWorker):\n // after shutdown() stops polling, in-flight activities get this long to\n // finish naturally; the rest are then CANCELLED with WORKER_SHUTDOWN.\n shutdownGraceTime: \"5s\",\n // Hard ceiling inside the pod's termination grace period.\n shutdownForceTime: \"100s\",\n } as const;\n const worker = await Worker.create({\n ...sharedWorkerOptions,\n taskQueue:\n options.role === \"control\"\n ? settings.temporalTaskQueue\n : turnTaskQueue(settings.temporalTaskQueue),\n ...(options.role === \"control\"\n ? {\n ...workflowDefinition,\n reuseV8Context: true,\n workflowThreadPoolSize: 1,\n maxCachedWorkflows: CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n maxConcurrentWorkflowTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n maxConcurrentActivityTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n }\n : turnConcurrency!.options),\n });\n if (options.role !== \"control\") {\n turnWorker = worker;\n turnConcurrency?.admission?.finalizeStartupBaseline();\n return { worker, connection };\n }\n\n const ownedWorkers = [worker];\n try {\n // The new activity type has its own queue. Retain the V1 poller so old\n // drain histories finish; old binaries never receive checkpoint work.\n for (const taskQueue of [\n sandboxLifecycleTaskQueue(settings.temporalTaskQueue),\n browserDeadlineCheckpointTaskQueue(settings.temporalTaskQueue),\n ]) {\n const lifecycleWorker = await Worker.create({\n ...sharedWorkerOptions,\n ...workflowDefinition,\n taskQueue,\n reuseV8Context: true,\n workflowThreadPoolSize: 1,\n maxCachedWorkflows: CONTROL_WORKER_MAX_CACHED_WORKFLOWS,\n maxConcurrentWorkflowTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,\n maxConcurrentActivityTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n });\n ownedWorkers.push(lifecycleWorker);\n }\n return {\n worker: combineWorkerRunTargets(ownedWorkers),\n connection,\n };\n } catch (error) {\n for (const ownedWorker of ownedWorkers) {\n try {\n ownedWorker.shutdown();\n } catch {\n // Preserve the lifecycle worker construction failure. The base poller\n // still received its shutdown request; a secondary synchronous\n // shutdown error must not replace the startup root cause.\n }\n }\n throw error;\n }\n },\n (connection) => connection.close(),\n );\n}\n\n// A signalWithStart capability so a worker activity can wake a PARENT\n// session's workflow when a spawned worker completes (the parent may have\n// idled and let its run finish, so a plain signal would not start one).\n// Separate from the worker's NativeConnection: the @temporalio/client\n// Connection is what exposes workflow.signalWithStart.\nexport async function createWorkerWorkflowSignaler(\n settings: Settings,\n db: Database,\n): Promise<{\n wakeSessionWorkflow: WakeSessionWorkflowSignal;\n signalSessionAttemptQuiesced: SignalSessionAttemptQuiesced;\n inspectSessionAttemptActivity: InspectSessionAttemptActivity;\n signalCodexCapacityWorkflow: SignalCodexCapacityWorkflow;\n getTurnTaskQueueStats: () => Promise<TurnTaskQueueStats>;\n startSandboxReaperWorkflow: StartSandboxReaperWorkflow;\n startVideoGenerationWorkflow: StartVideoGenerationWorkflow;\n check: () => Promise<void>;\n close: () => Promise<void>;\n}> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n return {\n wakeSessionWorkflow: async ({\n accountId,\n workspaceId,\n sessionId,\n workflowId,\n wakeRevision,\n interruptionRequested,\n onSignalAccepted,\n }) => {\n if (interruptionRequested) {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"sessionControl\",\n signalArgs: [],\n });\n } else {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"queueChanged\",\n });\n }\n onSignalAccepted?.();\n return await markSessionWorkflowWakeDelivered(db, {\n accountId,\n workspaceId,\n sessionId,\n temporalWorkflowId: workflowId,\n wakeRevision,\n });\n },\n signalSessionAttemptQuiesced: async (proof) => {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId: proof.workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [\n {\n accountId: proof.accountId,\n workspaceId: proof.workspaceId,\n sessionId: proof.sessionId,\n },\n ],\n signal: \"sessionAttemptQuiesced\",\n signalArgs: [proof],\n });\n // No wake-outbox row exists yet: the direct receipt transaction failed.\n // The signalled workflow's DB-only control activity owns committing the\n // receipt and its exact wake revision atomically.\n },\n inspectSessionAttemptActivity: async ({ workflowId, workflowRunId, activityId }) => {\n let description;\n try {\n description = await connection.workflowService.describeWorkflowExecution({\n namespace: settings.temporalNamespace,\n execution: { workflowId, runId: workflowRunId },\n });\n } catch (error) {\n if (temporalWorkflowExecutionNotFound(error)) return \"settled\";\n throw error;\n }\n const pending = description.pendingActivities?.find(\n (activity) => activity.activityId === activityId,\n );\n return temporalActivityLeaseSettled(pending) ? \"settled\" : \"pending\";\n },\n signalCodexCapacityWorkflow: async ({\n accountId,\n workspaceId,\n sessionId,\n workflowId,\n wakeRevision,\n }) => {\n await temporal.workflow.signalWithStart(\"sessionWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [{ accountId, workspaceId, sessionId }],\n signal: \"codexCapacityChanged\",\n signalArgs: [wakeRevision],\n });\n // A typed capacity signal cannot acknowledge the generic outbox row:\n // another producer may have advanced it with a Pause/Steer that requires\n // sessionControl. The global dispatcher owns that acknowledgement.\n },\n getTurnTaskQueueStats: async () => {\n const response = await connection.workflowService.describeTaskQueue({\n namespace: settings.temporalNamespace,\n taskQueue: { name: turnTaskQueue(settings.temporalTaskQueue) },\n // temporal.api.enums.v1.TASK_QUEUE_TYPE_ACTIVITY. Keep this request in\n // the supported DEFAULT mode; `stats.approximateBacklogCount` is the\n // server-documented scaling signal.\n taskQueueType: 2,\n reportStats: true,\n });\n return normalizeTurnTaskQueueStats(response.stats);\n },\n startSandboxReaperWorkflow: async () => {\n // Same backend tick, no additional schedule. A bounded independent\n // inventory cannot prevent the existing per-box drain from starting.\n const starts = await Promise.allSettled([\n temporal.workflow.start(\"sandboxReaperWorkflowV2\", {\n taskQueue: sandboxLifecycleTaskQueue(settings.temporalTaskQueue),\n workflowId: SANDBOX_REAPER_V2_WORKFLOW_ID,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [],\n }),\n temporal.workflow.start(\"browserDeadlineCheckpointSweepWorkflow\", {\n taskQueue: browserDeadlineCheckpointTaskQueue(settings.temporalTaskQueue),\n workflowId: BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID,\n workflowIdReusePolicy: \"ALLOW_DUPLICATE\",\n args: [],\n }),\n ]);\n for (const start of starts) {\n if (\n start.status === \"rejected\" &&\n !(start.reason instanceof WorkflowExecutionAlreadyStartedError)\n ) {\n // The other start already settled. Retry the router without losing\n // ordinary draining or silently omitting the checkpoint sweep.\n throw start.reason;\n }\n }\n return starts[0].status === \"fulfilled\" ? \"started\" : \"already_running\";\n },\n startVideoGenerationWorkflow: async ({ accountId, workspaceId, operationId }) => {\n try {\n await temporal.workflow.start(\"videoGenerationWorkflow\", {\n taskQueue: settings.temporalTaskQueue,\n workflowId: `video-generation:${operationId}`,\n // Reject a concurrent or already-completed run, but allow the repair\n // sweep to restart this exact operation if the workflow itself failed.\n // Every provider/storage transition remains CAS/idempotency fenced in\n // Postgres, so recovery never creates a second logical operation.\n workflowIdReusePolicy: \"ALLOW_DUPLICATE_FAILED_ONLY\",\n args: [\n {\n accountId,\n workspaceId,\n operationId,\n baseTaskQueue: settings.temporalTaskQueue,\n },\n ],\n });\n return \"started\";\n } catch (error) {\n if (error instanceof WorkflowExecutionAlreadyStartedError) {\n return \"already_running\";\n }\n throw error;\n }\n },\n check: async () => {\n await connection.workflowService.getSystemInfo({});\n },\n close: async () => {\n await connection.close();\n },\n };\n}\n\n/**\n * Register the ONE global reaper Temporal Schedule (the sole liveness/GC/cost-stop\n * driver — P1.3 / OD-3) and durable system-update outbox repair cadence. With\n * sandbox ownership off the activity performs bounded DB outbox repair and\n * read-only observability projections; it never mutates or terminates sandbox leases.\n *\n * The Schedule permanently fires sandboxReaperWorkflow on the legacy/base queue\n * every settings.sandboxLeaseReaperPeriodMs (the SAME cadence the boot invariant\n * `reaperPeriod < viewerHolderTTL` and `reaperPeriod + idleGrace < providerLifetime`\n * validates in packages/config — wiring the schedule period to it). SKIP overlap means a slow\n * sweep never overlaps itself. Idempotent: a duplicate scheduleId across the\n * worker pool collides on ScheduleAlreadyRunning and reconciles the same\n * definition. New workers route onto the versioned lifecycle queue; old workers\n * can still run the legacy action during rollout or a full binary rollback.\n *\n * Returns a `close()` for the dedicated client connection (separate from the\n * worker's NativeConnection — the Schedule client is a @temporalio/client).\n */\nexport async function registerSandboxReaperSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n assertSandboxReaperActivityTimeout(settings);\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n const spec = { intervals: [{ every: settings.sandboxLeaseReaperPeriodMs }] };\n const action = {\n type: \"startWorkflow\" as const,\n workflowType: \"sandboxReaperWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [] as [],\n };\n const policies = {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n } as const;\n try {\n await temporal.schedule.create({\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n // @every-style interval: fire once per reaper period. The boot invariant\n // guarantees reaperPeriod < viewerHolderTTL and\n // reaperPeriod + idleGrace < providerLifetime.\n spec,\n action,\n // The base workflow is a short router on new binaries; legacy binaries\n // still execute the pre-refactor composite under this same overlap fence.\n policies,\n });\n observability.info(\"Registered the global sandbox-lease reaper Schedule\", {\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n reaperPeriodMs: settings.sandboxLeaseReaperPeriodMs,\n });\n return {\n registered: true,\n close: async () => {\n await connection.close();\n },\n };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n // Never move this action off the base queue: doing so would leave a full\n // rollback with no worker polling the only reaper Schedule. The workflow\n // implementation itself owns the forward-compatible queue handoff.\n await temporal.schedule.getHandle(SANDBOX_REAPER_SCHEDULE_ID).update((previous) => ({\n spec,\n action,\n policies,\n state: {\n paused: previous.state.paused,\n ...(previous.state.note ? { note: previous.state.note } : {}),\n ...(previous.state.remainingActions !== undefined\n ? { remainingActions: previous.state.remainingActions }\n : {}),\n },\n }));\n observability.info(\"Reconciled the global sandbox-lease reaper Schedule\", {\n scheduleId: SANDBOX_REAPER_SCHEDULE_ID,\n temporalTaskQueue: action.taskQueue,\n });\n return {\n registered: false,\n close: async () => {\n await connection.close();\n },\n };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/** Register the deployment-wide worker for rebuildable Knowledge search projections. */\nexport async function registerKnowledgeIndexingSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n spec: { intervals: [{ every: KNOWLEDGE_INDEXING_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"knowledgeIndexingWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global Knowledge indexer Schedule\", {\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n indexingPeriodMs: KNOWLEDGE_INDEXING_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global Knowledge indexer Schedule already registered\", {\n scheduleId: KNOWLEDGE_INDEXING_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/**\n * Register the one provider-neutral expired direct-upload cleanup Schedule.\n * Unlike sandbox GC this is always registered: file uploads can be enabled in\n * deployments where sandbox ownership is disabled. The activity is a cheap\n * no-op when object storage is not configured.\n */\nexport async function registerFileUploadReaperSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n spec: { intervals: [{ every: FILE_UPLOAD_REAPER_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"fileUploadReaperWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global file-upload reaper Schedule\", {\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n reaperPeriodMs: FILE_UPLOAD_REAPER_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global file-upload reaper Schedule already registered\", {\n scheduleId: FILE_UPLOAD_REAPER_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/** Register the deployment-wide maintained-auth dispatcher. Its one-minute\n * cadence matches the minimum public policy interval; DB claims own overlap,\n * crash recovery, and exact session idempotency. */\nexport async function registerSiteAuthMaintenanceSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n spec: { intervals: [{ every: SITE_AUTH_MAINTENANCE_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: \"siteAuthMaintenanceWorkflow\",\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the global site-auth maintenance Schedule\", {\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n maintenancePeriodMs: SITE_AUTH_MAINTENANCE_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Global site-auth maintenance Schedule already registered\", {\n scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\n/**\n * Register the one repair cadence for committed workflow-wake revisions. The\n * activity only reads the transactional outbox and sends revision-scoped\n * signals; it is independent of sandbox ownership and child-agent features.\n */\nexport async function registerSessionWorkflowWakeDispatcherSchedule(\n settings: Settings,\n observability: Observability,\n): Promise<{ registered: boolean; close: () => Promise<void> }> {\n const connection = await Connection.connect(temporalConnectionOptions(settings));\n const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });\n try {\n await temporal.schedule.create({\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n spec: { intervals: [{ every: SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS }] },\n action: {\n type: \"startWorkflow\",\n workflowType: SESSION_WORKFLOW_WAKE_DISPATCHER_WORKFLOW_TYPE,\n taskQueue: settings.temporalTaskQueue,\n args: [],\n },\n policies: {\n overlap: ScheduleOverlapPolicy.SKIP,\n catchupWindow: \"1m\",\n pauseOnFailure: false,\n },\n });\n observability.info(\"Registered the session-workflow wake dispatcher Schedule\", {\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n periodMs: SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS,\n });\n return { registered: true, close: async () => connection.close() };\n } catch (error) {\n if (error instanceof ScheduleAlreadyRunning) {\n observability.info(\"Session-workflow wake dispatcher Schedule already registered\", {\n scheduleId: SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID,\n });\n return { registered: false, close: async () => connection.close() };\n }\n await connection.close().catch(() => undefined);\n throw error;\n }\n}\n\nexport type OpenGeniWorkerServiceOptions = Omit<WorkerOptions, \"activityDependencies\"> & {\n activityDependencies: ActivityDependencies & { db: Database; bus: EventBus };\n /** Host-owned final containment after a stalled cleanup has drained peers.\n * The standalone process exits; embedded hosts own their termination policy. */\n terminateWorker?: () => void;\n /**\n * Exact catalog posture required by the standalone runtime. Embedded hosts\n * may omit this when they own an equivalent database isolation contract.\n */\n databasePosture?: RuntimeDatabasePostureOptions;\n /**\n * `role-default` registers OpenGeni's internal maintenance schedules on a\n * control worker only. These are engine maintenance schedules, not a host's\n * product-level scheduled-agent jobs. Use `none` when another control worker\n * in the same deployment owns them.\n */\n internalSchedules?: \"role-default\" | \"none\";\n /**\n * Set false only when the host exposes equivalent lifecycle endpoints itself.\n * A dedicated readiness database prevents ordinary activity-pool saturation\n * from making an otherwise healthy Temporal worker fail Kubernetes readiness.\n * The embedding host retains ownership of this handle.\n */\n http?: false | { readinessTimeoutMs?: number; readinessDb?: Database };\n};\n\nexport type OpenGeniWorkerService = {\n readonly role: OpenGeniWorkerRole;\n readonly worker: WorkerRunTarget;\n readonly connection: NativeConnection;\n state(): WorkerLifecycleState;\n run(): Promise<void>;\n drain(reason?: string): void;\n close(): Promise<void>;\n};\n\nexport function workerOwnsInternalSchedules(\n role: OpenGeniWorkerRole,\n policy: OpenGeniWorkerServiceOptions[\"internalSchedules\"] = \"role-default\",\n): boolean {\n return role === \"control\" && policy !== \"none\";\n}\n\n/**\n * Construct one role-specific worker process around the lower-level Temporal\n * worker factory. The service owns every Temporal client and HTTP listener it\n * creates. The embedding host retains ownership of its injected DB and EventBus\n * handles and closes those only after this service has drained.\n */\nexport async function createOpenGeniWorkerService(\n options: OpenGeniWorkerServiceOptions,\n): Promise<OpenGeniWorkerService> {\n requireSessionEventDurableFanoutCapability(options.activityDependencies.bus);\n const settings = options.settings ?? getSettings();\n const observability =\n options.activityDependencies.observability ??\n createObservability(settings, { component: `worker-${options.role}` });\n const retryOptions = startupRetryOptions(settings);\n const onRetry = (event: Parameters<typeof logStartupDependencyRetry>[1]) =>\n logStartupDependencyRetry(observability, event);\n let lifecycle: WorkerServiceLifecycle | undefined;\n let cleanupContainment: ReturnType<typeof createWorkerCleanupContainment> | undefined;\n let signaler: Awaited<ReturnType<typeof createWorkerWorkflowSignaler>> | undefined;\n let workerBundle: Awaited<ReturnType<typeof createOpenGeniWorker>> | undefined;\n let turnCapacityMonitor: ReturnType<typeof startTurnCapacityMonitor> | undefined;\n let sessionRecoveryMonitor: ReturnType<typeof startSessionRecoveryMonitor> | undefined;\n let contextCompactionPendingMonitor:\n | ReturnType<typeof startContextCompactionPendingMonitor>\n | undefined;\n const schedules: Array<{ close: () => Promise<void> }> = [];\n let httpServer: ReturnType<typeof startWorkerHttpServer> | undefined;\n let memoryPressureGuard: TurnWorkerMemoryPressureGuard | undefined;\n\n try {\n const resolvedCatalog = await retryStartupDependency(\n \"model catalog\",\n () => resolveCatalogSettings(options.activityDependencies.db, settings),\n { ...retryOptions, onRetry },\n );\n observability.info(\"OpenGeni model catalog resolved\", {\n role: options.role,\n catalogSource: resolvedCatalog.source,\n catalogVersion: resolvedCatalog.version,\n });\n const needsSignaler =\n options.role === \"turn\" ||\n !options.activityDependencies.wakeSessionWorkflow ||\n !options.activityDependencies.signalSessionAttemptQuiesced ||\n !options.activityDependencies.inspectSessionAttemptActivity ||\n !options.activityDependencies.signalCodexCapacityWorkflow ||\n !options.activityDependencies.startSandboxReaperWorkflow ||\n !options.activityDependencies.startVideoGenerationWorkflow;\n if (needsSignaler) {\n signaler = await retryStartupDependency(\n \"Temporal client\",\n () => createWorkerWorkflowSignaler(settings, options.activityDependencies.db),\n { ...retryOptions, onRetry },\n );\n }\n const wakeSessionWorkflow =\n options.activityDependencies.wakeSessionWorkflow ?? signaler?.wakeSessionWorkflow;\n const signalSessionAttemptQuiesced =\n options.activityDependencies.signalSessionAttemptQuiesced ??\n signaler?.signalSessionAttemptQuiesced;\n const inspectSessionAttemptActivity =\n options.activityDependencies.inspectSessionAttemptActivity ??\n signaler?.inspectSessionAttemptActivity;\n const signalCodexCapacityWorkflow =\n options.activityDependencies.signalCodexCapacityWorkflow ??\n signaler?.signalCodexCapacityWorkflow;\n const startSandboxReaperWorkflow =\n options.activityDependencies.startSandboxReaperWorkflow ??\n signaler?.startSandboxReaperWorkflow;\n const startVideoGenerationWorkflow =\n options.activityDependencies.startVideoGenerationWorkflow ??\n signaler?.startVideoGenerationWorkflow;\n if (\n !wakeSessionWorkflow ||\n !signalSessionAttemptQuiesced ||\n !inspectSessionAttemptActivity ||\n !signalCodexCapacityWorkflow ||\n !startSandboxReaperWorkflow ||\n !startVideoGenerationWorkflow\n ) {\n throw new Error(\"OpenGeni worker lifecycle could not resolve its workflow signalers\");\n }\n workerBundle = await createOpenGeniWorker({\n role: options.role,\n settings,\n ...(options.activities ? { activities: options.activities } : {}),\n ...(options.workflowBundle ? { workflowBundle: options.workflowBundle } : {}),\n activityDependencies: {\n ...options.activityDependencies,\n settings,\n observability,\n wakeSessionWorkflow,\n signalSessionAttemptQuiesced,\n inspectSessionAttemptActivity,\n signalCodexCapacityWorkflow,\n startSandboxReaperWorkflow,\n startVideoGenerationWorkflow,\n requestWorkerDrain: () => {\n if (!cleanupContainment) throw new Error(\"worker cleanup containment is not initialized\");\n cleanupContainment.request();\n },\n },\n });\n\n if (options.role === \"turn\") {\n if (!signaler) {\n throw new Error(\"Turn worker capacity monitor could not resolve its Temporal stats client\");\n }\n turnCapacityMonitor = startTurnCapacityMonitor({\n observability,\n read: signaler.getTurnTaskQueueStats,\n });\n } else {\n sessionRecoveryMonitor = startSessionRecoveryMonitor({\n observability,\n read: async () => await countSessionRecoveryBacklog(options.activityDependencies.db),\n });\n contextCompactionPendingMonitor = startContextCompactionPendingMonitor({\n observability,\n read: async () => await getContextCompactionPendingSummary(options.activityDependencies.db),\n });\n }\n\n if (workerOwnsInternalSchedules(options.role, options.internalSchedules)) {\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (sandbox reaper)\",\n () => registerSandboxReaperSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (Knowledge indexing)\",\n () => registerKnowledgeIndexingSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (file upload reaper)\",\n () => registerFileUploadReaperSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (site auth maintenance)\",\n () => registerSiteAuthMaintenanceSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n schedules.push(\n await retryStartupDependency(\n \"Temporal schedule (session-workflow wake dispatcher)\",\n () => registerSessionWorkflowWakeDispatcherSchedule(settings, observability),\n { ...retryOptions, onRetry },\n ),\n );\n }\n\n if (options.http !== false) {\n const readinessDb = options.http?.readinessDb ?? options.activityDependencies.db;\n const databaseReady = dbReadyCheck(readinessDb, options.databasePosture, async () => {\n await resolveCatalogSettings(readinessDb, settings);\n });\n httpServer = startWorkerHttpServer({\n settings,\n observability,\n checks: {\n db: databaseReady,\n nats: natsReadyCheck(options.activityDependencies.bus),\n temporal: temporalReadyCheck(workerBundle.connection),\n },\n ...(options.http?.readinessTimeoutMs ? { timeoutMs: options.http.readinessTimeoutMs } : {}),\n lifecycle: { role: options.role, state: () => lifecycle?.state() ?? \"starting\" },\n });\n }\n } catch (error) {\n httpServer?.stop(true);\n await Promise.allSettled([\n turnCapacityMonitor?.close(),\n sessionRecoveryMonitor?.close(),\n contextCompactionPendingMonitor?.close(),\n workerBundle?.connection.close(),\n signaler?.close(),\n ...schedules.map((schedule) => schedule.close()),\n ]);\n throw error;\n }\n\n const activeWorkerBundle = workerBundle;\n const activeSignaler = signaler;\n if (!activeWorkerBundle) {\n throw new Error(\"OpenGeni worker service initialization did not complete\");\n }\n\n cleanupContainment = createWorkerCleanupContainment({\n drain: () => lifecycle?.drain(\"stalled turn finalization\") ?? false,\n ...(options.terminateWorker ? { terminate: options.terminateWorker } : {}),\n observability,\n });\n const activeCleanupContainment = cleanupContainment;\n lifecycle = createWorkerServiceLifecycle({\n role: options.role,\n worker: {\n shutdown: () => activeWorkerBundle.worker.shutdown(),\n run: async () => {\n await activeWorkerBundle.worker.run();\n activeCleanupContainment.finished();\n },\n },\n observability,\n closeOwnedResources: async () => {\n memoryPressureGuard?.close();\n httpServer?.stop(true);\n await Promise.allSettled([\n turnCapacityMonitor?.close(),\n sessionRecoveryMonitor?.close(),\n contextCompactionPendingMonitor?.close(),\n activeWorkerBundle.connection.close(),\n activeSignaler?.close(),\n ...schedules.map((schedule) => schedule.close()),\n ]);\n },\n onReady: () => {\n observability.info(\"OpenGeni worker listening\", {\n role: options.role,\n temporalTaskQueue:\n options.role === \"control\"\n ? settings.temporalTaskQueue\n : turnTaskQueue(settings.temporalTaskQueue),\n ...(options.role === \"turn\"\n ? turnWorkerConcurrencyLogFields(settings)\n : {\n concurrencyMode: \"fixed\",\n maxConcurrentTurns: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,\n targetCpuUsage: null,\n targetMemoryUsage: null,\n }),\n maxConcurrentWorkflowTaskExecutions:\n options.role === \"control\" ? CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS : 0,\n maxCachedWorkflows: options.role === \"control\" ? CONTROL_WORKER_MAX_CACHED_WORKFLOWS : 0,\n httpPort: options.http === false ? null : settings.workerHttpPort,\n });\n },\n });\n const activeLifecycle = lifecycle;\n\n if (turnWorkerMemoryPressureGuardEnabled(options.role, settings)) {\n try {\n memoryPressureGuard = createTurnWorkerMemoryPressureGuard({\n settings,\n observability,\n drain: () => {\n if (!activeLifecycle.drain(\"memory pressure guard\")) {\n throw new Error(\"worker shutdown request failed\");\n }\n },\n });\n } catch (error) {\n await activeLifecycle.close();\n throw error;\n }\n }\n\n return {\n role: options.role,\n worker: activeWorkerBundle.worker,\n connection: activeWorkerBundle.connection,\n state: activeLifecycle.state,\n run: activeLifecycle.run,\n drain: (reason) => {\n activeLifecycle.drain(reason);\n },\n close: activeLifecycle.close,\n };\n}\n\nexport type RunOpenGeniWorkerOptions = OpenGeniWorkerServiceOptions & {\n /** Defaults to SIGTERM and SIGINT. Pass false when the host owns process signals. */\n shutdownSignals?: false | ReadonlyArray<\"SIGTERM\" | \"SIGINT\">;\n};\n\n/** Start, drain, and close one embedded worker process. */\nexport async function runOpenGeniWorker(options: RunOpenGeniWorkerOptions): Promise<void> {\n const service = await createOpenGeniWorkerService(options);\n const signals =\n options.shutdownSignals === false ? [] : (options.shutdownSignals ?? [\"SIGTERM\", \"SIGINT\"]);\n const handlers = signals.map((signal) => {\n const handler = () => service.drain(signal);\n process.on(signal, handler);\n return { signal, handler };\n });\n try {\n await service.run();\n } finally {\n for (const { signal, handler } of handlers) {\n process.off(signal, handler);\n }\n await service.close();\n }\n}\n\nexport async function startWorker() {\n const role = process.env.OPENGENI_WORKER_ROLE;\n if (role !== \"control\" && role !== \"turn\") {\n throw new Error(\"OPENGENI_WORKER_ROLE must be explicitly set to 'control' or 'turn'\");\n }\n const settings = getSettings();\n const observability = createObservability(settings, { component: `worker-${role}` });\n // The Agents SDK also listens for unhandled rejections and exits the process.\n // Keep detached SDK/transport promises observational here: durable turn\n // activity boundaries own failures, while process exit causes cross-session\n // lease loss for every turn assigned to this shared worker pod.\n const disposeUnhandledRejectionBoundary = installWorkerUnhandledRejectionBoundary();\n const retryOptions = startupRetryOptions(settings);\n const onRetry = (event: Parameters<typeof logStartupDependencyRetry>[1]) =>\n logStartupDependencyRetry(observability, event);\n const searchPath = dbSearchPath(settings);\n const dbClient = createDb(settings.databaseUrl, {\n ...(searchPath ? { searchPath } : {}),\n rlsStrategy: settings.rlsStrategy,\n });\n // Readiness must prove PostgreSQL independently of activity demand. Sharing\n // the default application pool makes the probe wait behind live turns when\n // all ten slots are occupied, even while PostgreSQL itself is healthy. Since\n // Kubernetes readiness cannot stop Temporal polling, that false negative\n // only wedges rollouts and service health; reserve one probe connection.\n const readinessDbClient = createDb(settings.databaseUrl, {\n ...(searchPath ? { searchPath } : {}),\n rlsStrategy: settings.rlsStrategy,\n max: 1,\n });\n const databasePosture = {\n rlsStrategy: settings.rlsStrategy,\n expectedRole: settings.runtimeDatabaseRole,\n targetSchema: settings.dbSchema.trim() || \"public\",\n organizationTenancyCanonicalActivationEnabled:\n settings.organizationTenancyCanonicalActivationEnabled,\n } as const;\n const controlPlaneAuth = resolveNatsControlPlaneAuth(settings);\n let bus: Awaited<ReturnType<typeof createNatsEventBus>> | undefined;\n try {\n await retryStartupDependency(\n \"PostgreSQL runtime posture\",\n () => assertRuntimeDatabasePosture(dbClient.db, databasePosture),\n { ...retryOptions, onRetry, shouldRetry: isRetryableRuntimeDatabaseStartupError },\n );\n bus = await retryStartupDependency(\n \"NATS\",\n () =>\n createNatsEventBus(\n settings.natsUrl,\n controlPlaneAuth\n ? { user: controlPlaneAuth.user, pass: controlPlaneAuth.password }\n : undefined,\n observabilityEventBusOptions(observability),\n ),\n { ...retryOptions, onRetry },\n );\n await runOpenGeniWorker({\n role,\n terminateWorker: () => process.exit(1),\n settings,\n databasePosture,\n http: { readinessDb: readinessDbClient.db },\n activityDependencies: {\n observability,\n db: dbClient.db,\n bus,\n },\n });\n } finally {\n await Promise.allSettled([bus?.close(), readinessDbClient.close(), dbClient.close()]);\n disposeUnhandledRejectionBoundary();\n }\n}\n\nif (import.meta.main) {\n // `bun --watch` keeps its supervisor alive after the application consumes\n // SIGINT. Once the worker, NATS, DB, and Temporal resources have all closed,\n // explicitly terminate that supervisor too; otherwise a later file edit can\n // resurrect this stopped process as a duplicate worker with stale env/code.\n let interrupted = false;\n const markInterrupted = () => {\n interrupted = true;\n };\n process.once(\"SIGINT\", markInterrupted);\n try {\n await startWorker();\n } finally {\n process.off(\"SIGINT\", markInterrupted);\n if (interrupted) process.exit(0);\n }\n}\n","import {\n ActivityCancellationType,\n ActivityFailure,\n ApplicationFailure,\n proxyActivities,\n} from \"@temporalio/workflow\";\nimport type * as activities from \"../activities\";\nimport {\n KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS,\n KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS,\n} from \"../knowledge-source-sync-activity-policy\";\n\ntype WorkflowControlActivities = Pick<\n typeof activities,\n | \"enqueueGoalRetryWake\"\n | \"expireSessionHumanInput\"\n | \"expireSessionInteractionIntervention\"\n | \"expireScheduledRunHumanWait\"\n | \"failSessionAttempt\"\n | \"getCodexCapacityWait\"\n | \"markSessionIdle\"\n | \"peekSessionWork\"\n | \"persistSessionAttemptQuiescence\"\n | \"reconcileSessionAttemptQuiescence\"\n | \"reconcileCodexCapacityWait\"\n | \"recoverDispatch\"\n | \"recoverEscapedMcpTimeout\"\n | \"settleSessionInterruptions\"\n | \"settleSessionInputWait\"\n>;\n\n/**\n * Session/schedule workflow control activities are bounded, idempotent database\n * and provider-metadata operations. They must not inherit the agent turn's\n * 30-day attempt: Temporal cannot otherwise detect that the pod which accepted\n * a non-heartbeating control activity disappeared, leaving the workflow pinned\n * to a dead worker for the full 30 days. A bounded attempt plus an unbounded\n * Temporal retry keeps the durable workflow alive across rollout, node loss, or\n * transient database/network failure; retries re-run on a healthy control pod.\n */\nexport const activity = proxyActivities<WorkflowControlActivities>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n },\n});\n\n/** One schedule occurrence is bounded. Transient control-plane failures get a\n * short retry window, but a revoked binding or other permanent task error must\n * not leave a Temporal activity retrying forever; the next scheduled occurrence\n * is an independent workflow and will re-evaluate current state. */\nexport const scheduledTaskActivity = proxyActivities<\n Pick<typeof activities, \"dispatchScheduledTaskRun\">\n>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 5,\n },\n});\n\nexport const automationActivity = proxyActivities<Pick<typeof activities, \"dispatchAutomationRun\">>(\n {\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 5,\n },\n },\n);\n\n/** Dispatch has a bounded retry window, but an accepted automation event must\n * not remain durably `dispatching` after that window closes. This compensating\n * database write is idempotent and retries without a cap until the run reaches\n * a terminal state (or is observed as already dispatched). */\nexport const automationFailureActivity = proxyActivities<\n Pick<typeof activities, \"settleAutomationRunFailure\">\n>({\n startToCloseTimeout: \"2 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n },\n});\n\n/** Goal continuation evaluates a durable Postgres obligation at an idle\n * boundary. A transient failure gets a short retry window, then records an\n * explicit delayed outbox wake instead of relying on an unrelated mutation or\n * keeping workflow history alive with polling. */\nexport const goalActivity = proxyActivities<Pick<typeof activities, \"maybeContinueGoal\">>({\n startToCloseTimeout: \"30 seconds\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"5 seconds\",\n maximumAttempts: 3,\n },\n});\n\nexport function turnTaskQueue(baseTaskQueue: string): string {\n return `${baseTaskQueue}-turns`;\n}\n\nexport function turnActivityForTaskQueue(baseTaskQueue: string, receiptGatedCancellation = true) {\n return proxyActivities<Pick<typeof activities, \"runAgentTurn\">>({\n taskQueue: turnTaskQueue(baseTaskQueue),\n // Agent segments legitimately run for days. A started turn heartbeats;\n // queued activities remain queued truthfully until a capped turn worker\n // accepts them and performs the atomic claim.\n startToCloseTimeout: \"30 days\",\n heartbeatTimeout: \"2 minutes\",\n // Pause/Steer first closes the exact attempt in Postgres, then asks\n // Temporal to deliver cancellation. The workflow must not wait for the\n // Temporal activity promise: a provider cleanup promise can outlive the\n // fenced activity body, and Temporal terminalization is not proof that\n // sandbox tools or processes are physically quiescent. The activity owns\n // that proof and writes an exact receipt after its hard tool fence; the\n // receipt transaction wakes the workflow to admit a replacement.\n cancellationType: receiptGatedCancellation\n ? ActivityCancellationType.TRY_CANCEL\n : ActivityCancellationType.WAIT_CANCELLATION_COMPLETED,\n retry: { maximumAttempts: 1 },\n });\n}\n\nexport function videoGenerationActivityForTaskQueue(baseTaskQueue: string) {\n return proxyActivities<Pick<typeof activities, \"reconcileVideoGenerationOperation\">>({\n taskQueue: turnTaskQueue(baseTaskQueue),\n startToCloseTimeout: \"20 minutes\",\n retry: {\n initialInterval: \"1 second\",\n backoffCoefficient: 2,\n maximumInterval: \"30 seconds\",\n maximumAttempts: 3,\n },\n });\n}\n\nexport const documentActivity = proxyActivities<Pick<typeof activities, \"indexDocument\">>({\n startToCloseTimeout: \"30 minutes\",\n retry: { maximumAttempts: 1 },\n});\n\n/** Connector inventory/download/index batches are provider-I/O activities. The\n * implementation heartbeats between bounded items and owns its resumable\n * checkpoint; Temporal retries therefore repeat only idempotent source/object\n * operations and never invoke the agent runtime. */\nexport const knowledgeSourceSyncActivity = proxyActivities<\n Pick<typeof activities, \"runKnowledgeSourceSyncBatch\">\n>({\n startToCloseTimeout: \"1 hour\",\n heartbeatTimeout: KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS,\n retry: {\n initialInterval: \"2 seconds\",\n backoffCoefficient: 2,\n maximumInterval: \"1 minute\",\n maximumAttempts: KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS,\n },\n});\n\nexport function workflowFailureMessage(error: unknown): string {\n if (\n error instanceof ActivityFailure &&\n error.activityType === \"runAgentTurn\" &&\n error.cause instanceof ApplicationFailure &&\n error.cause.type === \"TurnExecutionPolicyDefinitionMismatchError\"\n ) {\n return error.cause.message;\n }\n if (error instanceof Error) {\n return error.message;\n }\n return String(error);\n}\n","export const KNOWLEDGE_SOURCE_SYNC_ACTIVITY_HEARTBEAT_TIMEOUT_MS = 120_000;\nexport const KNOWLEDGE_SOURCE_SYNC_ACTIVITY_MAXIMUM_ATTEMPTS = 1;\n","import type { Settings } from \"@opengeni/config\";\nimport {\n assertRuntimeDatabasePosture,\n dbSql,\n type Database,\n type RuntimeDatabasePostureOptions,\n} from \"@opengeni/db\";\nimport { requireSessionEventDurableFanoutCapability, type EventBus } from \"@opengeni/events\";\nimport { githubAppBotIdentityWarnings } from \"@opengeni/github\";\nimport type { Observability } from \"@opengeni/observability\";\n\nexport type ReadinessCheckName = \"db\" | \"nats\" | \"temporal\";\nexport type ReadinessChecks = Record<ReadinessCheckName, () => Promise<void> | void>;\n\nexport type ReadinessResult = {\n ok: boolean;\n state?: WorkerLifecycleState;\n checks: Record<ReadinessCheckName, { ok: boolean; error?: string }>;\n};\n\nexport type WorkerLifecycleState = \"starting\" | \"ready\" | \"draining\" | \"stopped\" | \"failed\";\n\nexport type WorkerHttpLifecycle = {\n role: \"control\" | \"turn\";\n state: () => WorkerLifecycleState;\n};\n\nexport type WorkerHttpServerInput = {\n settings: Settings;\n observability: Observability;\n checks: ReadinessChecks;\n timeoutMs?: number;\n lifecycle?: WorkerHttpLifecycle;\n};\n\nexport function createWorkerHttpHandler(\n input: WorkerHttpServerInput,\n): (request: Request) => Promise<Response> {\n const { settings, observability, checks, lifecycle } = input;\n const timeoutMs = input.timeoutMs ?? 2_000;\n return async (request) => {\n const url = new URL(request.url);\n if (request.method !== \"GET\") {\n return Response.json({ error: \"method_not_allowed\" }, { status: 405 });\n }\n if (url.pathname === \"/healthz\") {\n const state = lifecycle?.state();\n const ok = state !== \"failed\" && state !== \"stopped\";\n const warnings = githubAppBotIdentityWarnings(settings);\n return Response.json(\n {\n service: settings.serviceName,\n environment: settings.environment,\n deploymentRevision: settings.deploymentRevision,\n ...(lifecycle ? { role: lifecycle.role, state } : {}),\n ...(warnings.length > 0 ? { warnings } : {}),\n ok,\n },\n { status: ok ? 200 : 503 },\n );\n }\n if (url.pathname === \"/metrics\") {\n return new Response(await observability.prometheusMetrics(), {\n status: 200,\n headers: { \"content-type\": \"text/plain; version=0.0.4; charset=utf-8\" },\n });\n }\n if (url.pathname === \"/readyz\") {\n const state = lifecycle?.state();\n const result =\n state && state !== \"ready\"\n ? {\n ok: false,\n state,\n checks: Object.fromEntries(\n (Object.keys(checks) as ReadinessCheckName[]).map((name) => [\n name,\n { ok: false, error: \"worker_not_ready\" },\n ]),\n ) as ReadinessResult[\"checks\"],\n }\n : { ...(await runReadinessChecks(checks, timeoutMs)), ...(state ? { state } : {}) };\n return Response.json(result, { status: result.ok ? 200 : 503 });\n }\n return Response.json({ error: \"not_found\" }, { status: 404 });\n };\n}\n\nexport function startWorkerHttpServer(input: WorkerHttpServerInput): ReturnType<typeof Bun.serve> {\n return Bun.serve({\n hostname: \"0.0.0.0\",\n port: input.settings.workerHttpPort,\n fetch: createWorkerHttpHandler(input),\n });\n}\n\nexport async function runReadinessChecks(\n checks: ReadinessChecks,\n timeoutMs = 2_000,\n): Promise<ReadinessResult> {\n const entries = await Promise.all(\n (Object.entries(checks) as Array<[ReadinessCheckName, () => Promise<void> | void]>).map(\n async ([name, check]) => {\n try {\n await withTimeout(Promise.resolve().then(check), timeoutMs);\n return [name, { ok: true }] as const;\n } catch {\n return [name, { ok: false, error: \"dependency_unavailable\" }] as const;\n }\n },\n ),\n );\n const result = Object.fromEntries(entries) as ReadinessResult[\"checks\"];\n return {\n ok: Object.values(result).every((check) => check.ok),\n checks: result,\n };\n}\n\nexport function dbReadyCheck(\n db: Database,\n posture?: RuntimeDatabasePostureOptions,\n afterDatabaseReady?: () => Promise<void>,\n): () => Promise<void> {\n let inFlight: Promise<void> | undefined;\n return () => {\n // The HTTP timeout cannot cancel a driver query. Reuse an outstanding\n // attempt so repeated kubelet probes cannot queue an unbounded tail behind\n // one slow or disconnected database connection.\n if (inFlight) return inFlight;\n inFlight = (async () => {\n if (posture) {\n await assertRuntimeDatabasePosture(db, posture);\n } else {\n await db.execute(dbSql`select 1`);\n }\n await afterDatabaseReady?.();\n })().finally(() => {\n inFlight = undefined;\n });\n return inFlight;\n };\n}\n\nexport function natsReadyCheck(bus: EventBus): () => void {\n return () => {\n requireSessionEventDurableFanoutCapability(bus);\n if (bus.isConnected && !bus.isConnected()) {\n throw new Error(\"NATS is not connected\");\n }\n };\n}\n\nexport function temporalReadyCheck(\n connection: Pick<\n import(\"@temporalio/worker\").NativeConnection,\n \"workflowService\" | \"withDeadline\"\n >,\n): () => Promise<void> {\n return async () => {\n await connection.withDeadline(Date.now() + 2_000, () =>\n connection.workflowService.getSystemInfo({}),\n );\n };\n}\n\nasync function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n promise,\n new Promise<never>((_, reject) => {\n timer = setTimeout(\n () => reject(new Error(`readiness check timed out after ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n","import type { Observability } from \"@opengeni/observability\";\nimport type { WorkerLifecycleState } from \"./http\";\n\nexport async function constructWithOwnedConnection<Connection, Result>(\n connect: () => Promise<Connection>,\n construct: (connection: Connection) => Promise<Result>,\n close: (connection: Connection) => Promise<void>,\n): Promise<Result> {\n const connection = await connect();\n try {\n return await construct(connection);\n } catch (error) {\n await close(connection).catch(() => undefined);\n throw error;\n }\n}\n\nexport type WorkerRunTarget = {\n run(): Promise<void>;\n shutdown(): void;\n};\n\n/** One process may poll several disjoint Temporal queues, but it still has one\n * host lifecycle. Start and stop every poller as a unit; a failed poller drains\n * its siblings and preserves the original failure. */\nexport function combineWorkerRunTargets(targets: readonly WorkerRunTarget[]): WorkerRunTarget {\n if (targets.length === 0) {\n throw new Error(\"a worker service requires at least one Temporal poller\");\n }\n let runPromise: Promise<void> | undefined;\n const shutdown = (): void => {\n let firstError: unknown;\n for (const target of targets) {\n try {\n target.shutdown();\n } catch (error) {\n firstError ??= error;\n }\n }\n if (firstError !== undefined) throw firstError;\n };\n return {\n run: () => {\n runPromise ??= (async () => {\n const runs = targets.map((target) => {\n try {\n return target.run();\n } catch (error) {\n return Promise.reject(error);\n }\n });\n try {\n await Promise.all(runs);\n } catch (error) {\n try {\n shutdown();\n } catch {\n // Preserve the poller failure. Every sibling still received its\n // shutdown request even when one request itself failed.\n }\n await Promise.allSettled(runs);\n throw error;\n }\n })();\n return runPromise;\n },\n shutdown,\n };\n}\n\nexport type WorkerServiceLifecycle = {\n state(): WorkerLifecycleState;\n run(): Promise<void>;\n drain(reason?: string): boolean;\n close(): Promise<void>;\n};\n\nexport function createWorkerServiceLifecycle(input: {\n role: \"control\" | \"turn\";\n worker: WorkerRunTarget;\n observability: Observability;\n closeOwnedResources: () => Promise<void>;\n onReady?: () => void;\n}): WorkerServiceLifecycle {\n let state: WorkerLifecycleState = \"starting\";\n let runPromise: Promise<void> | undefined;\n let resourcesClosed: Promise<void> | undefined;\n const closeOwnedResources = () => {\n resourcesClosed ??= input.closeOwnedResources();\n return resourcesClosed;\n };\n\n const lifecycle: WorkerServiceLifecycle = {\n state: () => state,\n run: () => {\n if (!runPromise && state === \"draining\") {\n runPromise = (async () => {\n state = \"stopped\";\n await closeOwnedResources();\n })();\n return runPromise;\n }\n if (!runPromise && (state === \"stopped\" || state === \"failed\")) {\n return Promise.reject(new Error(`cannot run a worker service that is ${state}`));\n }\n runPromise ??= (async () => {\n if (state === \"starting\") {\n state = \"ready\";\n }\n input.onReady?.();\n try {\n await input.worker.run();\n state = \"stopped\";\n } catch (error) {\n state = \"failed\";\n throw error;\n } finally {\n await closeOwnedResources();\n }\n })();\n return runPromise;\n },\n drain: (_reason = \"host request\") => {\n if (state === \"draining\" || state === \"stopped\" || state === \"failed\") {\n return true;\n }\n const previousState = state;\n safeLifecycleLog(() =>\n input.observability.info(\"OpenGeni worker draining (graceful shutdown)\", {\n role: input.role,\n errorClass: \"WorkerLifecycleOperation\",\n errorCode: \"worker_draining\",\n origin: \"worker-lifecycle\",\n }),\n );\n state = \"draining\";\n try {\n input.worker.shutdown();\n return true;\n } catch {\n state = previousState;\n safeLifecycleLog(() =>\n input.observability.warn(\"worker shutdown request failed\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_shutdown_request_failed\",\n origin: \"worker-lifecycle\",\n }),\n );\n return false;\n }\n },\n close: async () => {\n if (!lifecycle.drain(\"service close\")) {\n throw new Error(\"worker shutdown request failed\");\n }\n if (runPromise) {\n await runPromise.catch(() => undefined);\n } else {\n state = \"stopped\";\n await closeOwnedResources();\n }\n },\n };\n\n return lifecycle;\n}\n\nfunction safeLifecycleLog(log: () => void): void {\n try {\n log();\n } catch {\n // Telemetry cannot change worker lifecycle state or shutdown delivery.\n }\n}\n\n/** A cleanup stall first drains peers through WORKER_SHUTDOWN. The standalone\n * process owns the final exit, because Temporal cannot kill JavaScript writers.\n * Embedded hosts may supply their own termination policy at this same edge. */\nexport function createWorkerCleanupContainment(input: {\n drain: () => boolean;\n terminate?: () => void;\n observability: Observability;\n timeoutMs?: number;\n}) {\n let requested = false;\n let finished = false;\n let timer: ReturnType<typeof setTimeout> | undefined;\n return {\n request() {\n if (requested || finished) return;\n requested = true;\n if (input.terminate) {\n timer = setTimeout(() => {\n safeLifecycleLog(() =>\n input.observability.error(\"worker cleanup drain exhausted; terminating host\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_cleanup_drain_exhausted\",\n origin: \"worker-lifecycle\",\n }),\n );\n input.terminate?.();\n }, input.timeoutMs ?? 100_000);\n timer.unref?.();\n }\n let accepted = false;\n try {\n accepted = input.drain();\n } catch {\n /* The host backstop remains armed. */\n }\n if (!accepted)\n safeLifecycleLog(() =>\n input.observability.error(\"worker cleanup drain request failed\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_shutdown_request_failed\",\n origin: \"worker-lifecycle\",\n }),\n );\n },\n finished() {\n // Call only after Worker.run resolves. ForceShutdownError does not prove\n // its activity promises stopped, so rejection must retain the backstop.\n finished = true;\n clearTimeout(timer);\n },\n };\n}\n","import { readFileSync } from \"node:fs\";\nimport { freemem, totalmem } from \"node:os\";\nimport { posix } from \"node:path\";\nimport type { Settings } from \"@opengeni/config\";\nimport type { Observability } from \"@opengeni/observability\";\n\nexport type TurnWorkerMemoryScope = \"host\" | \"cgroup\";\n\nexport type TurnWorkerMemoryScopeSnapshot = {\n scope: TurnWorkerMemoryScope;\n usedBytes: number;\n limitBytes: number;\n};\n\nexport type TurnWorkerMemoryPressureSnapshot = {\n processRssBytes: number;\n processHeapUsedBytes: number;\n processExternalBytes: number;\n processArrayBuffersBytes: number;\n scopes: TurnWorkerMemoryScopeSnapshot[];\n};\n\nexport type TurnWorkerMemoryPressureGuard = {\n sampleNow(): void;\n close(): void;\n};\n\ntype GuardSettings = Pick<\n Settings,\n | \"turnWorkerConcurrencyMode\"\n | \"turnWorkerTargetMemoryUsage\"\n | \"turnWorkerEmergencyMemoryUsage\"\n | \"turnWorkerMemoryGuardIntervalMs\"\n | \"turnWorkerMemoryGuardSustainMs\"\n>;\n\ntype GuardDependencies = {\n now?: () => number;\n sample?: () => TurnWorkerMemoryPressureSnapshot;\n setInterval?: typeof setInterval;\n clearInterval?: typeof clearInterval;\n gc?: () => void;\n};\n\ntype SnapshotDependencies = {\n readText?: (path: string) => string | null;\n hostTotalBytes?: () => number;\n hostAvailableBytes?: () => number;\n processMemoryUsage?: () => Pick<\n NodeJS.MemoryUsage,\n \"rss\" | \"heapUsed\" | \"external\" | \"arrayBuffers\"\n >;\n};\n\nexport type ProcessCgroupMembership = {\n v2Path: string | null;\n v1MemoryPath: string | null;\n};\n\nexport type CgroupMemoryFilePair = {\n currentPath: string;\n maxPath: string;\n};\n\nconst CGROUP_V2_MOUNT = \"/sys/fs/cgroup\";\nconst CGROUP_V1_MEMORY_MOUNT = \"/sys/fs/cgroup/memory\";\n\nexport function parseLinuxMeminfo(\n value: string,\n): { totalBytes: number; availableBytes: number } | null {\n const fields = new Map<string, number>();\n for (const line of value.split(\"\\n\")) {\n const match = /^([A-Za-z_()]+):\\s+(\\d+)\\s+kB$/.exec(line.trim());\n if (!match) continue;\n fields.set(match[1] as string, Number(match[2]) * 1024);\n }\n const totalBytes = fields.get(\"MemTotal\");\n const availableBytes = fields.get(\"MemAvailable\");\n if (\n totalBytes === undefined ||\n availableBytes === undefined ||\n !Number.isFinite(totalBytes) ||\n !Number.isFinite(availableBytes) ||\n totalBytes <= 0 ||\n availableBytes < 0\n ) {\n return null;\n }\n return { totalBytes, availableBytes: Math.min(totalBytes, availableBytes) };\n}\n\nexport function parseFiniteCgroupMemoryLimit(value: string): number | null {\n const trimmed = value.trim();\n if (!trimmed || trimmed === \"max\") return null;\n const parsed = Number(trimmed);\n // cgroup v1 represents \"unlimited\" with a near-int64 sentinel. Treat only\n // realistic finite limits as authority; host headroom remains the fallback.\n if (!Number.isSafeInteger(parsed) || parsed <= 0 || parsed >= 2 ** 60) return null;\n return parsed;\n}\n\nexport function parseProcessCgroupMembership(value: string): ProcessCgroupMembership {\n let v2Path: string | null = null;\n let v1MemoryPath: string | null = null;\n for (const line of value.split(\"\\n\")) {\n const firstSeparator = line.indexOf(\":\");\n const secondSeparator = line.indexOf(\":\", firstSeparator + 1);\n if (firstSeparator <= 0 || secondSeparator < 0) continue;\n const hierarchyId = line.slice(0, firstSeparator);\n const controllers = line.slice(firstSeparator + 1, secondSeparator);\n const path = normalizeCgroupPath(line.slice(secondSeparator + 1));\n if (!path) continue;\n if (hierarchyId === \"0\" && controllers === \"\") {\n v2Path = path;\n }\n if (controllers.split(\",\").includes(\"memory\")) {\n v1MemoryPath = path;\n }\n }\n return { v2Path, v1MemoryPath };\n}\n\nexport function cgroupMemoryFilePairs(membership: ProcessCgroupMembership): CgroupMemoryFilePair[] {\n const pairs: CgroupMemoryFilePair[] = [];\n const seen = new Set<string>();\n const appendHierarchy = (\n mount: string,\n path: string | null,\n currentFilename: string,\n maxFilename: string,\n ) => {\n for (const candidate of cgroupAncestorPaths(path ?? \"/\")) {\n const relative = candidate === \"/\" ? \"\" : candidate.slice(1);\n const currentPath = posix.join(mount, relative, currentFilename);\n const maxPath = posix.join(mount, relative, maxFilename);\n const key = `${currentPath}\\0${maxPath}`;\n if (seen.has(key)) continue;\n seen.add(key);\n pairs.push({ currentPath, maxPath });\n }\n };\n\n appendHierarchy(CGROUP_V2_MOUNT, membership.v2Path, \"memory.current\", \"memory.max\");\n appendHierarchy(\n CGROUP_V1_MEMORY_MOUNT,\n membership.v1MemoryPath,\n \"memory.usage_in_bytes\",\n \"memory.limit_in_bytes\",\n );\n return pairs;\n}\n\nexport function readTurnWorkerMemoryPressureSnapshot(\n dependencies: SnapshotDependencies = {},\n): TurnWorkerMemoryPressureSnapshot {\n const readText = dependencies.readText ?? readTextOrNull;\n const linux = parseLinuxMeminfo(readText(\"/proc/meminfo\") ?? \"\");\n const hostTotalBytes = linux?.totalBytes ?? (dependencies.hostTotalBytes ?? totalmem)();\n const hostAvailableBytes =\n linux?.availableBytes ?? (dependencies.hostAvailableBytes ?? freemem)();\n const host = {\n scope: \"host\" as const,\n usedBytes: Math.max(0, hostTotalBytes - Math.min(hostTotalBytes, hostAvailableBytes)),\n limitBytes: hostTotalBytes,\n };\n const cgroup = readCgroupMemoryScope(readText);\n // A finite process cgroup is the pod's actual admission boundary. Host\n // pressure includes unrelated workloads and must not make every replica\n // drain simultaneously; use it only when no finite cgroup authority exists.\n const processMemory = (dependencies.processMemoryUsage ?? process.memoryUsage)();\n\n return {\n processRssBytes: processMemory.rss,\n processHeapUsedBytes: processMemory.heapUsed,\n processExternalBytes: processMemory.external,\n processArrayBuffersBytes: processMemory.arrayBuffers,\n scopes: [cgroup ?? host],\n };\n}\n\nexport function turnWorkerMemoryPressureGuardEnabled(\n role: \"control\" | \"turn\",\n settings: Pick<Settings, \"turnWorkerConcurrencyMode\">,\n): boolean {\n return role === \"turn\" && settings.turnWorkerConcurrencyMode === \"resource-based\";\n}\n\nexport function createTurnWorkerMemoryPressureGuard(input: {\n settings: GuardSettings;\n observability: Observability;\n drain: () => void;\n dependencies?: GuardDependencies;\n}): TurnWorkerMemoryPressureGuard {\n const now = input.dependencies?.now ?? Date.now;\n const sample = input.dependencies?.sample ?? readTurnWorkerMemoryPressureSnapshot;\n const schedule = input.dependencies?.setInterval ?? setInterval;\n const cancel = input.dependencies?.clearInterval ?? clearInterval;\n const collectGarbage = input.dependencies?.gc ?? (() => Bun.gc(false));\n const breachStartedAt = new Map<TurnWorkerMemoryScope, number>();\n let lastGcAt = Number.NEGATIVE_INFINITY;\n let drainRequested = false;\n let closed = false;\n\n const sampleNow = () => {\n if (closed) return;\n const observedAt = now();\n let snapshot: TurnWorkerMemoryPressureSnapshot;\n try {\n snapshot = sample();\n } catch {\n breachStartedAt.clear();\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_sample_failures_total\",\n help: \"Turn-worker memory pressure guard samples that could not be read.\",\n });\n return;\n }\n\n const validScopes = snapshot.scopes.filter(\n ({ usedBytes, limitBytes }) =>\n Number.isFinite(usedBytes) &&\n Number.isFinite(limitBytes) &&\n usedBytes >= 0 &&\n limitBytes > 0,\n );\n if (validScopes.length === 0) {\n breachStartedAt.clear();\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_sample_failures_total\",\n help: \"Turn-worker memory pressure guard samples that could not be read.\",\n });\n return;\n }\n\n const sustainedBreaches: Array<{\n scope: TurnWorkerMemoryScope;\n utilizationRatio: number;\n breachSeconds: number;\n }> = [];\n for (const [kind, value] of [\n [\"rss\", snapshot.processRssBytes],\n [\"heap_used\", snapshot.processHeapUsedBytes],\n [\"external\", snapshot.processExternalBytes],\n [\"array_buffers\", snapshot.processArrayBuffersBytes],\n ] as const) {\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_process_memory_bytes\",\n help: \"Turn-worker process memory by runtime-reported category.\",\n labels: { kind },\n value: Number.isFinite(value) && value >= 0 ? value : 0,\n });\n }\n let shouldCollectGarbage = false;\n for (const scope of validScopes) {\n const utilizationRatio = scope.usedBytes / scope.limitBytes;\n const processRssRatio = snapshot.processRssBytes / scope.limitBytes;\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_utilization_ratio\",\n help: \"Observed memory utilization ratio used by the turn-worker pressure guard.\",\n labels: { scope: scope.scope },\n value: utilizationRatio,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_target_ratio\",\n help: \"Memory admission target enforced by the Temporal resource tuner.\",\n labels: { scope: scope.scope },\n value: input.settings.turnWorkerTargetMemoryUsage,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_emergency_ratio\",\n help: \"Emergency memory threshold enforced by the turn-worker pressure guard.\",\n labels: { scope: scope.scope },\n value: input.settings.turnWorkerEmergencyMemoryUsage,\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_available_bytes\",\n help: \"Memory headroom remaining in the turn-worker pressure guard scope.\",\n labels: { scope: scope.scope },\n value: Math.max(0, scope.limitBytes - scope.usedBytes),\n });\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_process_rss_ratio\",\n help: \"Turn-worker RSS as a ratio of the observed host or finite cgroup memory scope.\",\n labels: { scope: scope.scope },\n value: processRssRatio,\n });\n shouldCollectGarbage ||= processRssRatio >= input.settings.turnWorkerTargetMemoryUsage;\n\n if (utilizationRatio < input.settings.turnWorkerEmergencyMemoryUsage) {\n breachStartedAt.delete(scope.scope);\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_breach_seconds\",\n help: \"Seconds the turn-worker emergency threshold has remained continuously breached.\",\n labels: { scope: scope.scope },\n value: 0,\n });\n continue;\n }\n\n const startedAt = breachStartedAt.get(scope.scope) ?? observedAt;\n breachStartedAt.set(scope.scope, startedAt);\n const breachMs = Math.max(0, observedAt - startedAt);\n const breachSeconds = breachMs / 1_000;\n safeSetGauge(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_breach_seconds\",\n help: \"Seconds the turn-worker emergency threshold has remained continuously breached.\",\n labels: { scope: scope.scope },\n value: breachSeconds,\n });\n if (breachMs >= input.settings.turnWorkerMemoryGuardSustainMs) {\n sustainedBreaches.push({ scope: scope.scope, utilizationRatio, breachSeconds });\n }\n }\n\n // Large request buffers can become unreachable well before JSC's ordinary\n // heap trigger notices native/external pressure. Nudge an asynchronous GC\n // at most once per sustain window before escalating to a worker drain.\n const sustainedBreachesHadGcOpportunity =\n sustainedBreaches.length > 0 &&\n sustainedBreaches.every(({ scope }) => {\n const breachStarted = breachStartedAt.get(scope);\n return breachStarted !== undefined && lastGcAt >= breachStarted && lastGcAt < observedAt;\n });\n let collectedGarbageThisSample = false;\n if (\n shouldCollectGarbage &&\n observedAt - lastGcAt >= input.settings.turnWorkerMemoryGuardSustainMs &&\n (sustainedBreaches.length === 0 || !sustainedBreachesHadGcOpportunity)\n ) {\n lastGcAt = observedAt;\n try {\n collectGarbage();\n collectedGarbageThisSample = true;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_gc_total\",\n help: \"Proactive garbage-collection nudges under process memory pressure.\",\n });\n } catch {\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_gc_failures_total\",\n help: \"Proactive memory-pressure garbage-collection nudges that failed.\",\n });\n }\n }\n\n // The nudge is asynchronous. Give it one sampling interval to make the\n // reclaimed RSS visible before deciding that process recycling is needed.\n if (\n (collectedGarbageThisSample && !sustainedBreachesHadGcOpportunity) ||\n drainRequested ||\n sustainedBreaches.length === 0\n ) {\n return;\n }\n\n const primaryBreach = sustainedBreaches.sort(\n (left, right) => right.utilizationRatio - left.utilizationRatio,\n )[0];\n if (!primaryBreach) return;\n\n drainRequested = true;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_drains_total\",\n help: \"Graceful turn-worker drains requested after sustained memory pressure.\",\n labels: { scope: primaryBreach.scope },\n });\n safeWarn(input.observability, \"turn worker memory guard requested graceful drain\", {\n errorClass: \"WorkerLifecycleOperation\",\n errorCode: \"worker_draining\",\n origin: \"worker-lifecycle\",\n reason: `memory pressure (${primaryBreach.scope}, ${boundedRatio(primaryBreach.utilizationRatio)} >= ${input.settings.turnWorkerEmergencyMemoryUsage}, ${primaryBreach.breachSeconds}s)`,\n });\n try {\n input.drain();\n } catch {\n drainRequested = false;\n safeIncrementCounter(input.observability, {\n name: \"opengeni_turn_worker_memory_guard_drain_failures_total\",\n help: \"Turn-worker memory pressure guard drain requests that failed synchronously.\",\n labels: { scope: primaryBreach.scope },\n });\n safeWarn(input.observability, \"turn worker memory guard drain request failed\", {\n errorClass: \"WorkerLifecycleOperationError\",\n errorCode: \"worker_shutdown_request_failed\",\n origin: \"worker-lifecycle\",\n });\n }\n };\n\n const timer = schedule(sampleNow, input.settings.turnWorkerMemoryGuardIntervalMs);\n timer.unref?.();\n sampleNow();\n\n return {\n sampleNow,\n close: () => {\n if (closed) return;\n closed = true;\n cancel(timer);\n },\n };\n}\n\nfunction readCgroupMemoryScope(\n readText: (path: string) => string | null,\n): TurnWorkerMemoryScopeSnapshot | null {\n const membership = parseProcessCgroupMembership(readText(\"/proc/self/cgroup\") ?? \"\");\n let mostPressured: TurnWorkerMemoryScopeSnapshot | null = null;\n for (const { currentPath, maxPath } of cgroupMemoryFilePairs(membership)) {\n const currentRaw = readText(currentPath);\n const maxRaw = readText(maxPath);\n if (currentRaw === null || maxRaw === null) continue;\n const current = Number(currentRaw.trim());\n const limit = parseFiniteCgroupMemoryLimit(maxRaw);\n if (!Number.isFinite(current) || current < 0 || limit === null) continue;\n const candidate = { scope: \"cgroup\" as const, usedBytes: current, limitBytes: limit };\n if (\n !mostPressured ||\n candidate.usedBytes / candidate.limitBytes >\n mostPressured.usedBytes / mostPressured.limitBytes\n ) {\n mostPressured = candidate;\n }\n }\n return mostPressured;\n}\n\nfunction readTextOrNull(path: string): string | null {\n try {\n return readFileSync(path, \"utf8\");\n } catch {\n return null;\n }\n}\n\nfunction boundedRatio(value: number): number {\n return Math.round(Math.max(0, Math.min(value, 10)) * 1_000_000) / 1_000_000;\n}\n\nfunction normalizeCgroupPath(value: string): string | null {\n const trimmed = value.trim();\n if (!trimmed.startsWith(\"/\") || trimmed.includes(\"\\0\")) return null;\n if (trimmed.split(\"/\").includes(\"..\")) return null;\n return posix.normalize(trimmed);\n}\n\nfunction cgroupAncestorPaths(path: string): string[] {\n const ancestors: string[] = [];\n let current = path;\n while (true) {\n ancestors.push(current);\n if (current === \"/\") return ancestors;\n current = posix.dirname(current);\n }\n}\n\nfunction safeSetGauge(\n observability: Observability,\n input: Parameters<Observability[\"setGauge\"]>[0],\n): void {\n try {\n observability.setGauge(input);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n\nfunction safeIncrementCounter(\n observability: Observability,\n input: Parameters<Observability[\"incrementCounter\"]>[0],\n): void {\n try {\n observability.incrementCounter(input);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n\nfunction safeWarn(\n observability: Observability,\n message: string,\n attributes: Parameters<Observability[\"warn\"]>[1],\n): void {\n try {\n observability.warn(message, attributes);\n } catch {\n // Telemetry cannot disable the memory-safety action.\n }\n}\n","export type WorkerUnhandledRejectionProcess = {\n on: (event: \"unhandledRejection\", listener: (reason: unknown) => void) => void;\n off: (event: \"unhandledRejection\", listener: (reason: unknown) => void) => void;\n};\n\nexport type WorkerUnhandledRejectionClassification =\n | \"detached_runtime_mcp_lifecycle\"\n | \"detached_unknown\";\n\ntype RuntimeMcpLifecycleError = {\n name: \"McpLifecycleError\";\n code: \"mcp_connect_failed\" | \"mcp_close_failed\";\n origin: \"runtime\";\n serverId: string;\n};\n\n/**\n * The Agents SDK owns MCP connect promises and also installs a process-global\n * unhandled-rejection listener. A best-effort lifecycle rejection can surface\n * twice under concurrent teardown: once through the awaited MCP result and once\n * through the SDK's detached worker promise. The first path already records and\n * degrades the server; the second must not terminate the whole turn-worker pod.\n */\nexport function isDetachedRuntimeMcpLifecycleRejection(\n reason: unknown,\n): reason is RuntimeMcpLifecycleError {\n try {\n if (!reason || typeof reason !== \"object\") return false;\n const candidate = reason as Partial<RuntimeMcpLifecycleError>;\n return (\n candidate.name === \"McpLifecycleError\" &&\n (candidate.code === \"mcp_connect_failed\" || candidate.code === \"mcp_close_failed\") &&\n candidate.origin === \"runtime\" &&\n typeof candidate.serverId === \"string\" &&\n candidate.serverId.length > 0\n );\n } catch {\n return false;\n }\n}\n\nfunction reportIsolatedUnhandledRejection(\n classification: WorkerUnhandledRejectionClassification,\n): void {\n // Never render the rejection itself: provider and transport errors can carry\n // request bodies or credentials. The durable activity path owns exact errors.\n console.error(\"[worker] isolated detached unhandled rejection\", { classification });\n}\n\n/**\n * Keep process-global promise rejection handling observational. Turn activities\n * have their own durable failure/lease boundary; killing the shared worker here\n * converts one detached SDK/transport promise into unrelated lease losses across\n * every session assigned to the pod. The Agents SDK also checks for a consumer\n * listener before applying its own process.exit(1), so this boundary must never\n * reintroduce that fail-fast behavior for an unknown rejection shape.\n */\nexport function installWorkerUnhandledRejectionBoundary(\n runtimeProcess: WorkerUnhandledRejectionProcess = process,\n report: (\n classification: WorkerUnhandledRejectionClassification,\n ) => void = reportIsolatedUnhandledRejection,\n): () => void {\n const onUnhandledRejection = (reason: unknown): void => {\n report(\n isDetachedRuntimeMcpLifecycleRejection(reason)\n ? \"detached_runtime_mcp_lifecycle\"\n : \"detached_unknown\",\n );\n };\n runtimeProcess.on(\"unhandledRejection\", onUnhandledRejection);\n return () => runtimeProcess.off(\"unhandledRejection\", onUnhandledRejection);\n}\n","export const BROWSER_DEADLINE_CHECKPOINT_SWEEP_ID = \"opengeni-browser-deadline-checkpoints-v1\";\nconst QUEUE_SUFFIX = \"-browser-checkpoint-v1\";\nexport function browserDeadlineCheckpointTaskQueue(base: string): string {\n return base.endsWith(QUEUE_SUFFIX) ? base : `${base}${QUEUE_SUFFIX}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE,gCAAAA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EAC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,GAAG,cAAc,QAAQ;AAAA,IAC7B;AACA,UAAM,kBAAkB;AAAA,MACtB;AAAA,MACA,iBAAiB,MAAM,QAAQ,KAAK,CAAC;AAAA,MACrC;AAAA,MACA;AAAA,MACA,MAAM,EAAE,aAAa,kBAAkB,GAAG;AAAA,MAC1C,sBAAsB;AAAA,QACpB;AAAA,QACA,IAAI,SAAS;AAAA,QACb;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,UAAE;AACA,UAAM,QAAQ,WAAW,CAAC,KAAK,MAAM,GAAG,kBAAkB,MAAM,GAAG,SAAS,MAAM,CAAC,CAAC;AACpF,sCAAkC;AAAA,EACpC;AACF;AAEA,IAAI,YAAY,MAAM;AAKpB,MAAI,cAAc;AAClB,QAAM,kBAAkB,MAAM;AAC5B,kBAAc;AAAA,EAChB;AACA,UAAQ,KAAK,UAAU,eAAe;AACtC,MAAI;AACF,UAAM,YAAY;AAAA,EACpB,UAAE;AACA,YAAQ,IAAI,UAAU,eAAe;AACrC,QAAI,YAAa,SAAQ,KAAK,CAAC;AAAA,EACjC;AACF;","names":["assertRuntimeDatabasePosture","requireSessionEventDurableFanoutCapability","activity","requireSessionEventDurableFanoutCapability","assertRuntimeDatabasePosture"]}