@opengeni/worker-bundle 0.16.29 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/activities/agent-run-admission.d.ts +12 -0
- package/dist/activities/agent-turn.d.ts +110 -19
- package/dist/activities/api-integrations.d.ts +21 -0
- package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
- package/dist/activities/capabilities.d.ts +6 -0
- package/dist/activities/codemode-dispatcher.d.ts +35 -0
- package/dist/activities/codemode-token-renewal.d.ts +23 -0
- package/dist/activities/codex-capacity.d.ts +6 -5
- package/dist/activities/codex-image-generation.d.ts +21 -0
- package/dist/activities/context-compaction.d.ts +1 -0
- package/dist/activities/documents.d.ts +3 -2
- package/dist/activities/environment.d.ts +17 -8
- package/dist/activities/expiring-material-renewal.d.ts +1 -1
- package/dist/activities/file-upload-reaper.d.ts +7 -2
- package/dist/activities/gateway-image-generation.d.ts +39 -0
- package/dist/activities/gateway-video-generation.d.ts +53 -0
- package/dist/activities/generated-images.d.ts +112 -0
- package/dist/activities/git-credential-renewal.d.ts +1 -1
- package/dist/activities/goals.d.ts +2 -2
- package/dist/activities/google-drive-changes.d.ts +99 -0
- package/dist/activities/image-generation-operation.d.ts +74 -0
- package/dist/activities/image-generation-references.d.ts +25 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
- package/dist/activities/knowledge-source-sync.d.ts +75 -0
- package/dist/activities/packs.d.ts +21 -36
- package/dist/activities/parent-wake.d.ts +1 -1
- package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
- package/dist/activities/retained-screenshots.d.ts +36 -0
- package/dist/activities/rig-verification.d.ts +82 -5
- package/dist/activities/run-credential-renewal.d.ts +1 -1
- package/dist/activities/run-input.d.ts +3 -1
- package/dist/activities/sandbox-images.d.ts +14 -0
- package/dist/activities/sandbox-lease.d.ts +47 -9
- package/dist/activities/scheduled-tasks.d.ts +2 -2
- package/dist/activities/session-state.d.ts +7 -5
- package/dist/activities/site-auth-maintenance.d.ts +27 -0
- package/dist/activities/streaming.d.ts +1 -1
- package/dist/activities/types.d.ts +87 -5
- package/dist/activities/video-generation-admission.d.ts +43 -0
- package/dist/activities/video-generation-credential.d.ts +24 -0
- package/dist/activities/video-generation-reconciliation.d.ts +13 -0
- package/dist/activities/video-media-validation.d.ts +34 -0
- package/dist/activities/video-output-retention.d.ts +36 -0
- package/dist/activities/video-reference-staging.d.ts +55 -0
- package/dist/activities/workflow-wake.d.ts +2 -2
- package/dist/activities/workspace-capture.d.ts +10 -1
- package/dist/activities/xai-auth.d.ts +23 -0
- package/dist/activities/xai-image-generation.d.ts +26 -0
- package/dist/activities/xai-video-generation.d.ts +18 -0
- package/dist/activities-control-XURE4VKN.js +7221 -0
- package/dist/activities-control-XURE4VKN.js.map +1 -0
- package/dist/activities-control.d.ts +297 -0
- package/dist/activities-turn-6EBXGGLS.js +16036 -0
- package/dist/activities-turn-6EBXGGLS.js.map +1 -0
- package/dist/activities-turn.d.ts +17 -0
- package/dist/activities.d.ts +138 -150
- package/dist/activity-services.d.ts +10 -0
- package/dist/artifact-materializer-entry.d.ts +6 -0
- package/dist/artifact-materializer-entry.js +2662 -0
- package/dist/artifact-materializer-entry.js.map +1 -0
- package/dist/artifact-outbox-entry.d.ts +2 -0
- package/dist/artifact-outbox-entry.js +681 -0
- package/dist/artifact-outbox-entry.js.map +1 -0
- package/dist/chunk-5GRJIP5H.js +3092 -0
- package/dist/chunk-5GRJIP5H.js.map +1 -0
- package/dist/chunk-GRFQTTSQ.js +448 -0
- package/dist/chunk-GRFQTTSQ.js.map +1 -0
- package/dist/chunk-TYNPCG4U.js +906 -0
- package/dist/chunk-TYNPCG4U.js.map +1 -0
- package/dist/concurrency.d.ts +108 -5
- package/dist/editable-artifact-hint-broker.d.ts +49 -0
- package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
- package/dist/editable-artifact-materializer-service.d.ts +108 -0
- package/dist/editable-artifact-materializer-storage.d.ts +12 -0
- package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
- package/dist/editable-artifact-materializer.d.ts +262 -0
- package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
- package/dist/editable-artifact-outbox-service.d.ts +70 -0
- package/dist/host-export.d.ts +1 -1
- package/dist/index.d.ts +189 -10
- package/dist/index.js +715 -17380
- package/dist/index.js.map +1 -1
- package/dist/memory-pressure-guard.d.ts +57 -0
- package/dist/model-checkpoint-memory-collector.d.ts +23 -0
- package/dist/model-context-contributions.d.ts +40 -0
- package/dist/observability-metrics.d.ts +53 -1
- package/dist/sandbox-reaper-contract.d.ts +40 -0
- package/dist/sandbox-reaper-timeout.d.ts +14 -0
- package/dist/sandbox-resume.d.ts +34 -7
- package/dist/sandbox-routing.d.ts +6 -0
- package/dist/worker-service-lifecycle.d.ts +6 -2
- package/dist/workflow-bundle.js +1094 -370
- package/dist/workflows/activities.d.ts +8 -2
- package/dist/workflows/document-index.d.ts +1 -1
- package/dist/workflows/knowledge-source-sync.d.ts +13 -0
- package/dist/workflows/rig-verification.d.ts +1 -1
- package/dist/workflows/sandbox-reaper.d.ts +8 -0
- package/dist/workflows/scheduled-tasks.d.ts +1 -1
- package/dist/workflows/session.d.ts +2 -2
- package/dist/workflows/site-auth-maintenance.d.ts +2 -0
- package/dist/workflows/video-generation.d.ts +10 -0
- package/dist/workflows/workflow-wake.d.ts +1 -1
- package/dist/workflows.d.ts +10 -7
- package/package.json +35 -15
- package/src/activities/agent-run-admission.ts +76 -0
- package/src/activities/agent-turn.ts +2600 -340
- package/src/activities/api-integrations.ts +138 -0
- package/src/activities/browser-state-artifact-reaper.ts +120 -0
- package/src/activities/capabilities.ts +10 -0
- package/src/activities/codemode-dispatcher.ts +335 -0
- package/src/activities/codemode-token-renewal.ts +64 -0
- package/src/activities/codex-capacity.ts +54 -7
- package/src/activities/codex-image-generation.ts +56 -0
- package/src/activities/context-compaction.ts +38 -3
- package/src/activities/documents.ts +14 -4
- package/src/activities/environment.ts +54 -45
- package/src/activities/expiring-material-renewal.ts +1 -1
- package/src/activities/file-upload-reaper.ts +34 -3
- package/src/activities/gateway-image-generation.ts +146 -0
- package/src/activities/gateway-video-generation.ts +298 -0
- package/src/activities/generated-images.ts +860 -0
- package/src/activities/goals.ts +8 -3
- package/src/activities/google-drive-changes.ts +454 -0
- package/src/activities/image-generation-operation.ts +333 -0
- package/src/activities/image-generation-references.ts +118 -0
- package/src/activities/knowledge-source-sync-driver.ts +27 -0
- package/src/activities/knowledge-source-sync.ts +2840 -0
- package/src/activities/mcp-credentials.ts +1 -0
- package/src/activities/packs.ts +75 -152
- package/src/activities/parent-wake.ts +13 -1
- package/src/activities/retained-screenshot-reaper.ts +2 -2
- package/src/activities/retained-screenshots.ts +453 -28
- package/src/activities/rig-verification.ts +554 -43
- package/src/activities/run-input.ts +22 -6
- package/src/activities/sandbox-images.ts +167 -0
- package/src/activities/sandbox-lease.ts +1065 -483
- package/src/activities/scheduled-tasks.ts +104 -74
- package/src/activities/session-state.ts +22 -4
- package/src/activities/site-auth-maintenance.ts +269 -0
- package/src/activities/streaming.ts +42 -3
- package/src/activities/types.ts +104 -5
- package/src/activities/video-generation-admission.ts +303 -0
- package/src/activities/video-generation-credential.ts +112 -0
- package/src/activities/video-generation-reconciliation.ts +771 -0
- package/src/activities/video-media-validation.ts +332 -0
- package/src/activities/video-output-retention.ts +314 -0
- package/src/activities/video-reference-staging.ts +350 -0
- package/src/activities/workflow-wake.ts +2 -2
- package/src/activities/workspace-capture.ts +44 -10
- package/src/activities/xai-auth.ts +122 -0
- package/src/activities/xai-image-generation.ts +86 -0
- package/src/activities/xai-video-generation.ts +69 -0
- package/src/activities-control.ts +58 -0
- package/src/activities-turn.ts +40 -0
- package/src/activities.ts +53 -124
- package/src/activity-services.ts +62 -0
- package/src/artifact-materializer-entry.ts +35 -0
- package/src/artifact-outbox-entry.ts +29 -0
- package/src/concurrency.ts +484 -6
- package/src/editable-artifact-hint-broker.ts +235 -0
- package/src/editable-artifact-materializer-launcher.ts +515 -0
- package/src/editable-artifact-materializer-service.ts +787 -0
- package/src/editable-artifact-materializer-storage.ts +413 -0
- package/src/editable-artifact-materializer-subprocess.ts +956 -0
- package/src/editable-artifact-materializer.ts +1289 -0
- package/src/editable-artifact-outbox-dispatcher.ts +244 -0
- package/src/editable-artifact-outbox-service.ts +438 -0
- package/src/index.ts +328 -72
- package/src/memory-pressure-guard.ts +489 -0
- package/src/model-checkpoint-memory-collector.ts +123 -0
- package/src/model-context-contributions.ts +159 -0
- package/src/observability-metrics.ts +297 -13
- package/src/sandbox-reaper-contract.ts +73 -0
- package/src/sandbox-reaper-timeout.ts +79 -0
- package/src/sandbox-resume.ts +294 -153
- package/src/sandbox-routing.ts +32 -24
- package/src/worker-service-lifecycle.ts +80 -14
- package/src/workflows/activities.ts +31 -0
- package/src/workflows/file-upload-reaper.ts +9 -1
- package/src/workflows/knowledge-source-sync.ts +83 -0
- package/src/workflows/sandbox-reaper.ts +183 -21
- package/src/workflows/scheduled-tasks.ts +42 -6
- package/src/workflows/session.ts +20 -6
- package/src/workflows/site-auth-maintenance.ts +14 -0
- package/src/workflows/video-generation.ts +36 -0
- package/src/workflows.ts +18 -1
- package/dist/activities/toolspace-token-renewal.d.ts +0 -23
- package/src/activities/toolspace-token-renewal.ts +0 -64
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/observability-metrics.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport { errorCodeToJSON } from \"@opengeni/agent-proto\";\nimport { SandboxBackend, type SessionEventType } from \"@opengeni/contracts\";\nimport type { EventLogger } from \"@opengeni/events\";\nimport type { Attributes, AttributeValue, Observability } from \"@opengeni/observability\";\nimport type { CompanyBrainContributionReceipt } from \"./model-context-contributions\";\nimport {\n SELFHOSTED_INFRASTRUCTURE_FAULT_CLASSES,\n modelUsageTokenCountOrNull,\n type RuntimeMetricsHooks,\n type SelfhostedOpObservation,\n type SelfhostedOpObserver,\n} from \"@opengeni/runtime\";\n\nexport type TurnOutcome = \"completed\" | \"failed\" | \"cancelled\" | \"recovering\";\nexport type CreditMicrosKind = \"usage\" | \"grant\" | \"topup\" | \"refund\";\nexport type SandboxLeaseLiveness = \"cold\" | \"warming\" | \"warm\" | \"draining\";\nexport type CreditBalanceGauge = { accountId: string; balanceMicros: number };\nexport type TurnTaskQueueStats = {\n /** Temporal activity tasks ready for a turn worker to accept. */\n eligibleBacklog: number;\n oldestBacklogAgeSeconds: number;\n tasksAddRate: number;\n tasksDispatchRate: number;\n};\n\nexport type TemporalTurnTaskQueueStats = {\n approximateBacklogCount?: unknown;\n approximateBacklogAge?: {\n seconds?: unknown;\n nanos?: unknown;\n } | null;\n tasksAddRate?: unknown;\n tasksDispatchRate?: unknown;\n} | null;\n\nconst turnTrackers = new WeakMap<Observability, TurnLifecycleMetrics>();\nconst creditBalanceGaugeAccounts = new WeakMap<Observability, Set<string>>();\nconst modelCacheCounterTotals = new WeakMap<Observability, Map<string, number>>();\n\n// A worker process reaching one trillion tokens in one provider/cache counter is\n// already far outside an ordinary scrape lifetime. Refuse further increments\n// before floating-point precision can degrade; a restart resets both the\n// process-local Prometheus registry and this guard together.\nconst MAX_MODEL_CACHE_COUNTER_TOTAL = 1_000_000_000_000;\n\nexport function observabilityEventLogger(observability: Observability): EventLogger {\n return {\n debug: (message, attributes) => observability.debug(message, eventAttributes(attributes)),\n warn: (message, attributes) => observability.warn(message, eventAttributes(attributes)),\n };\n}\n\nfunction eventAttributes(attributes: Record<string, unknown> | undefined): Attributes | undefined {\n if (!attributes) {\n return undefined;\n }\n const projected: Attributes = {};\n for (const [key, value] of Object.entries(attributes)) {\n projected[key] = eventAttributeValue(value);\n }\n return projected;\n}\n\nfunction eventAttributeValue(value: unknown): AttributeValue {\n if (\n value === null ||\n value === undefined ||\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\"\n ) {\n return value;\n }\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n}\n\nexport function runtimeMetricsHooksForObservability(\n observability: Observability,\n): RuntimeMetricsHooks {\n return {\n onModelCall: ({ provider, outcome, durationSeconds }) => {\n observability.incrementCounter({\n name: \"opengeni_model_calls_total\",\n help: \"Total model calls by provider and outcome.\",\n labels: { provider, outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_model_call_duration_seconds\",\n help: \"Model call duration in seconds by provider.\",\n labels: { provider },\n value: durationSeconds,\n });\n },\n onSandboxCreate: ({ backend, imageSource, outcome, durationSeconds }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_creates_total\",\n help: \"Total sandbox create attempts by backend and outcome.\",\n labels: { backend, image_source: imageSource, outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_create_duration_seconds\",\n help: \"Sandbox create duration in seconds by backend.\",\n labels: { backend, image_source: imageSource },\n value: durationSeconds,\n });\n },\n onSandboxWarmingTimeout: ({ backend, stage }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_warming_timeouts_total\",\n help: \"Total sandbox warming timeouts.\",\n labels: { backend, stage },\n });\n },\n onSandboxOp: ({ backend, op, outcome, code, healed, durationSeconds, replyBytes }) => {\n observability.incrementCounter({\n name: \"opengeni_machine_op_total\",\n help: \"Total Connected Machine control ops by op, outcome, and typed fault code.\",\n labels: { backend, op, outcome, code: code ?? \"\" },\n });\n observability.observeHistogram({\n name: \"opengeni_machine_op_duration_seconds\",\n help: \"Connected Machine control-op duration in seconds by op.\",\n labels: { backend, op },\n value: durationSeconds,\n });\n // The healed-fault leading indicator: an op that only succeeded after a retry\n // (a blip/backpressure the transport absorbed). The doctrine: healed faults are\n // the leading indicator of the next unhealed one, so they are always recorded.\n if (healed) {\n observability.incrementCounter({\n name: \"opengeni_machine_op_healed_total\",\n help: \"Connected Machine ops that succeeded only after ≥1 in-call retry.\",\n labels: { backend, op },\n });\n }\n // The payload-wall indicator (bytes known only on a PAYLOAD_TOO_LARGE fault today).\n if (replyBytes !== undefined) {\n observability.observeHistogram({\n name: \"opengeni_machine_op_reply_bytes\",\n help: \"Connected Machine control-op reply size in bytes (payload-wall indicator).\",\n labels: { backend, op },\n value: replyBytes,\n });\n }\n },\n };\n}\n\n/**\n * Adapt the runtime's transport-agnostic `SelfhostedOpObserver` to the metrics\n * hooks: map a completed-op observation onto `onSandboxOp`, converting the wire\n * `ErrorCode` to its stable enum-name string (a bounded metric label) and the\n * duration to seconds. Wired into the selfhosted session build so every Connected\n * Machine control op meters.\n */\nexport function selfhostedOpObserverForMetrics(hooks: RuntimeMetricsHooks): SelfhostedOpObserver {\n return (o) => {\n hooks.onSandboxOp?.({\n backend: \"selfhosted\",\n op: o.op,\n outcome: o.outcome,\n healed: o.healed,\n retries: o.retries,\n durationSeconds: o.durationMs / 1000,\n ...(o.code !== undefined ? { code: errorCodeToJSON(o.code) } : {}),\n ...(o.replyBytes !== undefined ? { replyBytes: o.replyBytes } : {}),\n });\n };\n}\n\n/** A session-scoped `machine.op.*` event mapped from a completed-op observation. */\nexport type MachineOpSessionEvent = {\n type: \"machine.op.failed\" | \"machine.op.recovered\";\n payload: { op: string; faultClass: string; attempts: number; machineId?: string };\n};\n\n/** Map an observation to a `machine.op.*` session event, or null if it is not\n * eventable. `machine.op.failed` fires ONLY for infrastructure fault classes (a\n * semantic miss the model asked about is an outcome, not an infra fault);\n * `machine.op.recovered` fires for a healed op (success after ≥1 retry). */\nexport function machineOpSessionEventFor(o: SelfhostedOpObservation): MachineOpSessionEvent | null {\n if (\n o.outcome === \"failed\" &&\n o.faultClass &&\n SELFHOSTED_INFRASTRUCTURE_FAULT_CLASSES.has(o.faultClass)\n ) {\n return {\n type: \"machine.op.failed\",\n payload: {\n op: o.op,\n faultClass: o.faultClass,\n attempts: o.retries,\n ...(o.machineId ? { machineId: o.machineId } : {}),\n },\n };\n }\n if (o.outcome === \"ok\" && o.healed) {\n return {\n type: \"machine.op.recovered\",\n payload: {\n op: o.op,\n faultClass: o.faultClass ?? \"unknown\",\n attempts: o.retries,\n ...(o.machineId ? { machineId: o.machineId } : {}),\n },\n };\n }\n return null;\n}\n\n/**\n * The Connected Machine op observer wired into a turn: it meters EVERY op (the\n * metrics sink) and BUFFERS the eventable ops (infra failures + healed recoveries)\n * as `machine.op.*` session events. The observer is SYNC (fire-and-forget), so the\n * turn drains the buffer to durable session events at a known checkpoint (turn end),\n * awaited — never an unawaited DB write inside the Temporal activity.\n */\nexport function makeMachineOpObserver(hooks: RuntimeMetricsHooks): {\n observer: SelfhostedOpObserver;\n drainEvents(): MachineOpSessionEvent[];\n} {\n const meter = selfhostedOpObserverForMetrics(hooks);\n const buffered: MachineOpSessionEvent[] = [];\n return {\n observer: (o) => {\n meter(o);\n const event = machineOpSessionEventFor(o);\n if (event) {\n buffered.push(event);\n }\n },\n drainEvents: () => buffered.splice(0, buffered.length),\n };\n}\n\nexport function turnLifecycleMetricsFor(observability: Observability): TurnLifecycleMetrics {\n const existing = turnTrackers.get(observability);\n if (existing) {\n return existing;\n }\n const tracker = new TurnLifecycleMetrics(observability);\n turnTrackers.set(observability, tracker);\n return tracker;\n}\n\nexport class TurnLifecycleMetrics {\n private readonly turns = new Map<string, { startedAt: number; lastProgressAt: number }>();\n private timer: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private readonly observability: Observability,\n private readonly options: { now?: () => number; refreshIntervalMs?: number } = {},\n ) {}\n\n start(turnId: string): void {\n const now = this.now();\n this.turns.set(turnId, { startedAt: now, lastProgressAt: now });\n this.ensureTimer();\n this.refreshGauges();\n }\n\n progress(turnId: string): void {\n const turn = this.turns.get(turnId);\n if (!turn) return;\n turn.lastProgressAt = this.now();\n }\n\n finish(turnId: string, outcome: TurnOutcome | null, durationSeconds?: number): void {\n const startedAt = this.turns.get(turnId)?.startedAt;\n if (startedAt !== undefined) {\n this.turns.delete(turnId);\n }\n if (outcome) {\n const observedDuration =\n durationSeconds ??\n (startedAt === undefined ? 0 : Math.max(0, (this.now() - startedAt) / 1000));\n this.observability.incrementCounter({\n name: \"opengeni_turns_total\",\n help: \"Total agent turns by terminal outcome.\",\n labels: { outcome },\n });\n this.observability.observeHistogram({\n name: \"opengeni_turn_duration_seconds\",\n help: \"Agent turn duration in seconds by terminal outcome.\",\n labels: { outcome },\n value: observedDuration,\n });\n }\n this.refreshGauges();\n if (this.turns.size === 0) {\n this.stopTimer();\n }\n }\n\n refreshGauges(): void {\n this.observability.setGauge({\n name: \"opengeni_turns_inflight\",\n help: \"Current number of in-flight agent turns in this worker process.\",\n value: this.turns.size,\n });\n this.observability.setGauge({\n name: \"opengeni_turn_oldest_inflight_age_seconds\",\n help: \"Age in seconds of the oldest in-flight agent turn in this worker process.\",\n value: this.oldestInflightAgeSeconds(),\n });\n this.observability.setGauge({\n name: \"opengeni_turn_oldest_no_progress_age_seconds\",\n help: \"Seconds since durable progress for the least recently progressing in-flight turn.\",\n value: this.oldestNoProgressAgeSeconds(),\n });\n }\n\n stop(): void {\n this.turns.clear();\n this.refreshGauges();\n this.stopTimer();\n }\n\n private oldestInflightAgeSeconds(): number {\n if (this.turns.size === 0) {\n return 0;\n }\n let oldest = Number.POSITIVE_INFINITY;\n for (const { startedAt } of this.turns.values()) {\n oldest = Math.min(oldest, startedAt);\n }\n return Math.max(0, (this.now() - oldest) / 1000);\n }\n\n private oldestNoProgressAgeSeconds(): number {\n if (this.turns.size === 0) return 0;\n let leastRecentProgress = Number.POSITIVE_INFINITY;\n for (const { lastProgressAt } of this.turns.values()) {\n leastRecentProgress = Math.min(leastRecentProgress, lastProgressAt);\n }\n return Math.max(0, (this.now() - leastRecentProgress) / 1000);\n }\n\n private ensureTimer(): void {\n if (this.timer) {\n return;\n }\n this.timer = setInterval(() => this.refreshGauges(), this.options.refreshIntervalMs ?? 15_000);\n this.timer.unref?.();\n }\n\n private stopTimer(): void {\n if (!this.timer) {\n return;\n }\n clearInterval(this.timer);\n this.timer = null;\n }\n\n private now(): number {\n return this.options.now?.() ?? Date.now();\n }\n}\n\nexport function recordTurnsQueuedGauge(observability: Observability, value: number): void {\n observability.setGauge({\n name: \"opengeni_turns_queued\",\n help: \"Current number of queued session turns.\",\n value,\n });\n}\n\n/**\n * Record the authoritative turn activity queue rather than aggregate Postgres\n * prompts. A paused human prompt is durable queue truth but is not runnable and\n * never reaches this Temporal task queue. DescribeTaskQueue's approximate\n * backlog count and age are explicitly documented by Temporal as autoscaling\n * signals.\n */\nexport function recordTurnTaskQueueStats(\n observability: Observability,\n stats: TurnTaskQueueStats,\n): void {\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog\",\n help: \"Temporal runAgentTurn activity tasks eligible for immediate worker admission.\",\n value: nonnegativeFinite(stats.eligibleBacklog),\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog_oldest_age_seconds\",\n help: \"Approximate age of the oldest eligible runAgentTurn activity task.\",\n value: nonnegativeFinite(stats.oldestBacklogAgeSeconds),\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_add_rate\",\n help: \"Temporal runAgentTurn tasks added per second over its rolling window.\",\n value: nonnegativeFinite(stats.tasksAddRate),\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_dispatch_rate\",\n help: \"Temporal runAgentTurn tasks dispatched per second over its rolling window.\",\n value: nonnegativeFinite(stats.tasksDispatchRate),\n });\n}\n\nexport function normalizeTurnTaskQueueStats(\n stats: TemporalTurnTaskQueueStats | undefined,\n): TurnTaskQueueStats {\n if (!stats) {\n throw new Error(\"Temporal DescribeTaskQueue response omitted required stats\");\n }\n const eligibleBacklog = requiredTemporalNumber(\n stats.approximateBacklogCount,\n \"approximateBacklogCount\",\n { integer: true },\n );\n let oldestBacklogAgeSeconds = 0;\n if (stats.approximateBacklogAge) {\n const seconds = optionalTemporalNumber(\n stats.approximateBacklogAge.seconds,\n \"approximateBacklogAge.seconds\",\n { integer: true },\n );\n const nanos = optionalTemporalNumber(\n stats.approximateBacklogAge.nanos,\n \"approximateBacklogAge.nanos\",\n { integer: true },\n );\n if (nanos >= 1_000_000_000) {\n throw new Error(\"Temporal approximateBacklogAge.nanos must be less than one second\");\n }\n oldestBacklogAgeSeconds = seconds + nanos / 1_000_000_000;\n } else if (eligibleBacklog > 0) {\n throw new Error(\"Temporal stats omitted approximateBacklogAge for a nonzero backlog\");\n }\n return {\n eligibleBacklog,\n oldestBacklogAgeSeconds,\n tasksAddRate: optionalTemporalNumber(stats.tasksAddRate, \"tasksAddRate\"),\n tasksDispatchRate: optionalTemporalNumber(stats.tasksDispatchRate, \"tasksDispatchRate\"),\n };\n}\n\nexport function startTurnCapacityMonitor(input: {\n observability: Observability;\n read: () => Promise<TurnTaskQueueStats>;\n intervalMs?: number;\n now?: () => number;\n}): { close: () => Promise<void> } {\n const intervalMs = input.intervalMs ?? 15_000;\n const now = input.now ?? Date.now;\n const startedAt = now();\n let lastSuccessAt: number | null = null;\n let lastReadSucceeded = false;\n let stopped = false;\n let running: Promise<void> | null = null;\n const recordStatus = () => {\n const observedAt = now();\n const successAgeMs = observedAt - (lastSuccessAt ?? startedAt);\n const set = (name: string, help: string, value: number) =>\n input.observability.setGauge({ name, help, value });\n set(\n \"opengeni_turn_capacity_monitor_last_read_success\",\n \"Whether the latest Temporal turn-queue capacity read completed successfully.\",\n lastReadSucceeded ? 1 : 0,\n );\n set(\n \"opengeni_turn_capacity_monitor_last_success_timestamp_seconds\",\n \"Unix timestamp of the latest successful Temporal turn-queue capacity read, or zero before one succeeds.\",\n lastSuccessAt === null ? 0 : lastSuccessAt / 1_000,\n );\n set(\n \"opengeni_turn_capacity_monitor_last_success_age_seconds\",\n \"Age of the latest successful Temporal turn-queue capacity read, or monitor age before one succeeds.\",\n Math.max(0, successAgeMs / 1_000),\n );\n set(\n \"opengeni_turn_capacity_monitor_fresh\",\n \"Whether eligible-backlog gauges have a successful Temporal read within three monitor intervals.\",\n lastReadSucceeded && lastSuccessAt !== null && successAgeMs <= intervalMs * 3 ? 1 : 0,\n );\n };\n const refresh = () => {\n // Advance freshness age even while a Temporal read is hung. Old backlog\n // values remain observable for diagnosis but cease to be authoritative.\n recordStatus();\n if (stopped || running) return;\n running = input\n .read()\n .then((stats) => {\n recordTurnTaskQueueStats(input.observability, stats);\n lastSuccessAt = now();\n lastReadSucceeded = true;\n recordStatus();\n })\n .catch((error) => {\n lastReadSucceeded = false;\n recordStatus();\n input.observability.warn(\"turn capacity monitor: Temporal task-queue stats failed\", {\n error: error instanceof Error ? error.message : String(error),\n });\n })\n .finally(() => {\n running = null;\n });\n };\n refresh();\n const timer = setInterval(refresh, intervalMs);\n timer.unref?.();\n return {\n close: async () => {\n stopped = true;\n clearInterval(timer);\n await running;\n },\n };\n}\n\nexport function recordSandboxLeaseGauges(\n observability: Observability,\n counts: Partial<Record<SandboxLeaseLiveness, number>>,\n): void {\n for (const liveness of [\"cold\", \"warming\", \"warm\", \"draining\"] as const) {\n observability.setGauge({\n name: \"opengeni_sandbox_leases\",\n help: \"Current sandbox leases by liveness state.\",\n labels: { liveness },\n value: counts[liveness] ?? 0,\n });\n }\n}\n\nexport const SANDBOX_INVENTORY_PROJECTION_DOMAINS = [\n \"leases\",\n \"checkpoint_artifacts\",\n \"rotation_backlog\",\n \"retained_processes\",\n \"expired_drains\",\n] as const;\n\nexport type SandboxInventoryProjectionDomain =\n (typeof SANDBOX_INVENTORY_PROJECTION_DOMAINS)[number];\n\nexport function recordSandboxInventoryProjectionSuccess(\n observability: Observability,\n domain: SandboxInventoryProjectionDomain,\n timestampSeconds = Date.now() / 1_000,\n): void {\n observability.setGauge({\n name: \"opengeni_sandbox_inventory_refresh_timestamp_seconds\",\n help: \"Unix timestamp of the last complete successful sandbox inventory projection by domain.\",\n labels: { domain },\n value: timestampSeconds,\n });\n}\n\nexport function recordSandboxInventoryProjectionFailure(\n observability: Observability,\n domain: SandboxInventoryProjectionDomain,\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_inventory_refresh_failures_total\",\n help: \"Failed sandbox inventory projection refreshes by domain.\",\n labels: { domain },\n });\n}\n\nexport function recordCreditBalanceGauges(\n observability: Observability,\n balances: CreditBalanceGauge[],\n): void {\n const previous = creditBalanceGaugeAccounts.get(observability) ?? new Set<string>();\n const current = new Set<string>();\n for (const balance of balances) {\n current.add(balance.accountId);\n observability.setGauge({\n name: \"opengeni_credit_balance_micros\",\n help: \"Current credit balance in micros by account.\",\n labels: { account_id: balance.accountId },\n value: balance.balanceMicros,\n });\n }\n for (const accountId of previous) {\n if (!current.has(accountId)) {\n observability.setGauge({\n name: \"opengeni_credit_balance_micros\",\n help: \"Current credit balance in micros by account.\",\n labels: { account_id: accountId },\n value: 0,\n });\n }\n }\n creditBalanceGaugeAccounts.set(observability, current);\n}\n\nexport function recordSandboxOrphansTerminated(observability: Observability, count: number): void {\n if (count <= 0) {\n return;\n }\n observability.incrementCounter({\n name: \"opengeni_sandbox_orphans_terminated_total\",\n help: \"Total provider-side orphan sandboxes terminated by defensive sweeps.\",\n amount: count,\n });\n}\n\nexport type SandboxCheckpointArtifactMetricState =\n | \"candidate\"\n | \"current\"\n | \"previous\"\n | \"delete_pending\"\n | \"deleting\"\n | \"delete_failed\"\n | \"deleted\";\n\nexport function recordSandboxCheckpointArtifactGauges(\n observability: Observability,\n counts: Record<SandboxCheckpointArtifactMetricState, number>,\n): void {\n for (const state of [\n \"candidate\",\n \"current\",\n \"previous\",\n \"delete_pending\",\n \"deleting\",\n \"delete_failed\",\n \"deleted\",\n ] as const) {\n observability.setGauge({\n name: \"opengeni_sandbox_checkpoint_artifacts\",\n help: \"Current durable provider checkpoint artifacts by lifecycle state.\",\n labels: { state },\n value: counts[state],\n });\n }\n}\n\nexport type SandboxCheckpointArtifactOutcome =\n | \"legacy_adopted\"\n | \"claimed\"\n | \"deleted\"\n | \"delete_failed\"\n | \"tombstone_pruned\";\n\nexport function recordSandboxCheckpointArtifactOutcome(\n observability: Observability,\n outcome: SandboxCheckpointArtifactOutcome,\n count = 1,\n): void {\n if (count <= 0) return;\n observability.incrementCounter({\n name: \"opengeni_sandbox_checkpoint_artifact_operations_total\",\n help: \"Durable provider checkpoint artifact operations by fixed outcome.\",\n labels: { outcome },\n amount: count,\n });\n}\n\nexport function recordSandboxDeadlineRotationsRequested(\n observability: Observability,\n count: number,\n): void {\n if (count <= 0) return;\n observability.incrementCounter({\n name: \"opengeni_sandbox_deadline_rotations_requested_total\",\n help: \"Total finite-lifetime sandbox rotations requested before provider deadline.\",\n amount: count,\n });\n}\n\nexport function recordSandboxRotationBacklogGauges(\n observability: Observability,\n backlog: {\n requested: number;\n overdue: number;\n turnBlocked: number;\n directBlocked: number;\n processBlocked: number;\n },\n): void {\n const values = {\n requested: backlog.requested,\n overdue: backlog.overdue,\n turn_blocked: backlog.turnBlocked,\n direct_blocked: backlog.directBlocked,\n process_blocked: backlog.processBlocked,\n } as const;\n for (const [kind, value] of Object.entries(values)) {\n observability.setGauge({\n name: \"opengeni_sandbox_rotation_backlog\",\n help: \"Current finite-lifetime sandbox rotation backlog by fixed condition.\",\n labels: { kind },\n value,\n });\n }\n}\n\nconst RETAINED_PROCESS_OWNER_STATES = [\n \"direct\",\n \"queued\",\n \"running\",\n \"requires_action\",\n \"recovering\",\n \"waiting_capacity\",\n \"completed\",\n \"failed\",\n \"cancelled\",\n \"superseded\",\n \"withdrawn_for_edit\",\n \"missing\",\n \"unknown\",\n] as const;\n\nconst RETAINED_PROCESS_OWNER_STATE_SET = new Set<string>(RETAINED_PROCESS_OWNER_STATES);\n\nexport function recordRetainedProcessInventoryGauges(\n observability: Observability,\n counts: Array<{ ownerState: string; activeCount: number; terminalOwnerCount: number }>,\n): void {\n const normalized = new Map<string, { active: number; terminal: number }>();\n for (const ownerState of RETAINED_PROCESS_OWNER_STATES) {\n normalized.set(ownerState, { active: 0, terminal: 0 });\n }\n for (const count of counts) {\n const ownerState = RETAINED_PROCESS_OWNER_STATE_SET.has(count.ownerState)\n ? count.ownerState\n : \"unknown\";\n const current = normalized.get(ownerState)!;\n current.active += Math.max(0, count.activeCount);\n current.terminal += Math.max(0, count.terminalOwnerCount);\n }\n\n for (const [ownerState, count] of normalized) {\n observability.setGauge({\n name: \"opengeni_retained_processes_active\",\n help: \"Current active retained provider processes by durable owner state.\",\n labels: { owner_state: ownerState },\n value: count.active,\n });\n observability.setGauge({\n name: \"opengeni_retained_processes_terminal_owner_backlog\",\n help: \"Current active retained processes whose exact owner attempt is terminal.\",\n labels: { owner_state: ownerState },\n value: count.terminal,\n });\n }\n}\n\nexport const EXPIRED_DRAINING_BACKENDS = [...SandboxBackend.options, \"unknown\"] as const;\nconst EXPIRED_DRAINING_BACKEND_SET = new Set<string>(EXPIRED_DRAINING_BACKENDS);\nexport const EXPIRED_DRAINING_AGE_BUCKETS = [\"lt_5m\", \"5m_1h\", \"1h_1d\", \"gte_1d\"] as const;\n\nexport function recordExpiredDrainingSandboxLeaseGauges(\n observability: Observability,\n counts: Array<{ backend: string; ageBucket: string; count: number }>,\n): void {\n const normalized = new Map<string, number>();\n for (const backend of EXPIRED_DRAINING_BACKENDS) {\n for (const ageBucket of EXPIRED_DRAINING_AGE_BUCKETS) {\n normalized.set(`${backend}:${ageBucket}`, 0);\n }\n }\n for (const count of counts) {\n const backend = EXPIRED_DRAINING_BACKEND_SET.has(count.backend) ? count.backend : \"unknown\";\n if (!EXPIRED_DRAINING_AGE_BUCKETS.includes(count.ageBucket as never)) continue;\n const key = `${backend}:${count.ageBucket}`;\n normalized.set(key, (normalized.get(key) ?? 0) + Math.max(0, count.count));\n }\n for (const [key, value] of normalized) {\n const [backend, ageBucket] = key.split(\":\") as [string, string];\n observability.setGauge({\n name: \"opengeni_sandbox_leases_expired_draining\",\n help: \"Current expired draining sandbox leases by backend and fixed age bucket.\",\n labels: { backend, age_bucket: ageBucket },\n value,\n });\n }\n}\n\nexport const RETAINED_PROCESS_RECONCILIATION_OUTCOMES = [\n \"claim_failed\",\n \"proof_exited\",\n \"proof_lost\",\n \"proof_checkpoint_failed\",\n \"settled_exited\",\n \"settled_lost\",\n \"settlement_failed\",\n \"identity_mismatch\",\n \"resume_state_missing\",\n \"backend_unsupported\",\n \"provider_running\",\n \"provider_unknown\",\n \"provider_timeout\",\n \"provider_error\",\n \"provider_binding_missing\",\n \"provider_binding_mismatch\",\n \"provider_binding_adopted\",\n \"defer_failed\",\n] as const;\n\nexport type RetainedProcessReconciliationOutcome =\n (typeof RETAINED_PROCESS_RECONCILIATION_OUTCOMES)[number];\n\nexport function recordRetainedProcessReconciliation(\n observability: Observability,\n outcome: RetainedProcessReconciliationOutcome,\n): void {\n observability.incrementCounter({\n name: \"opengeni_retained_process_reconciliation_total\",\n help: \"Bounded retained-process reconciliation observations by fixed app outcome.\",\n labels: { outcome },\n });\n}\n\nexport function recordCreditMicros(\n observability: Observability | undefined,\n kind: CreditMicrosKind,\n amountMicros: number,\n): void {\n if (!observability || amountMicros <= 0) {\n return;\n }\n observability.incrementCounter({\n name: \"opengeni_credit_micros_total\",\n help: \"Total credit micros recorded by kind.\",\n labels: { kind },\n amount: amountMicros,\n });\n}\n\nconst MODEL_REQUEST_PHASE_BUCKETS = [\n 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.35, 0.5, 1, 2, 5, 10, 30, 60, 300,\n];\n\n/**\n * Record provider lifecycle diagnostics from the synchronous transport observer.\n * Provider ids come from the resolved provider registry; phase/outcome are closed\n * enums. This function never performs I/O outside the in-memory metrics registry.\n */\nexport function recordModelRequestPhase(\n observability: Observability,\n input: {\n provider: string;\n phase: \"headers\" | \"first_byte\" | \"terminal\";\n outcome?: \"completed\" | \"failed\" | \"timed_out\";\n durationSeconds: number;\n },\n): void {\n const outcome = input.outcome ?? \"\";\n observability.incrementCounter({\n name: \"opengeni_model_request_phases_total\",\n help: \"Provider model-request lifecycle phases by bounded phase and outcome.\",\n labels: { provider: input.provider, phase: input.phase, outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_model_request_phase_duration_seconds\",\n help: \"Monotonic seconds from provider dispatch to a model-request lifecycle phase.\",\n buckets: MODEL_REQUEST_PHASE_BUCKETS,\n labels: { provider: input.provider, phase: input.phase },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n// ── Streaming SLIs ────────────────────────────────────────────────────────────\n// Instruments the token-streaming pipeline so \"streaming is sluggish\" is a number,\n// not a vibe. Split across the three attributable stages so an operator can tell\n// WHERE the latency lives: the model (TTFT + inter-delta gaps), our durable write\n// path (append latency), or delivery (publish latency + batcher flush shape). All\n// labels are bounded (provider from the model registry, a two-value delta class) —\n// never a session id or a raw user-supplied model string.\n\nexport type StreamDeltaClass = \"message\" | \"reasoning\";\n\n/** Content-delta classes only. A `null` return means \"not a content delta\" — the\n * event that re-arms the TTFT anchor and closes an inter-delta run. */\nfunction contentDeltaClass(type: SessionEventType): StreamDeltaClass | null {\n if (type === \"agent.message.delta\") {\n return \"message\";\n }\n if (type === \"agent.reasoning.delta\") {\n return \"reasoning\";\n }\n return null;\n}\n\n// TTFT and inter-delta live on a human-perceptible scale (tens of ms to a few\n// seconds), so they get their own SHORT buckets — the default duration buckets\n// (which run to 3600s) would collapse every real streaming value into one bucket.\nconst STREAM_TTFT_BUCKETS = [0.02, 0.05, 0.1, 0.2, 0.35, 0.5, 0.75, 1, 1.5, 2, 3, 5, 10];\nconst STREAM_INTER_DELTA_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.35, 0.5, 1, 2, 5];\n\n/**\n * Per-turn stream-timing tracker fed every normalized runtime event in push order.\n * It emits two model-responsiveness SLIs from the worker's seat on the stream:\n *\n * - `opengeni_stream_ttft_seconds{provider}` — time from a model (re)start to its\n * first streamed content delta. The anchor starts at construction (≈ runStream\n * start, so the first observation is \"how long until text appears\") and re-arms\n * on every non-content event (a tool call, a completed message, a usage frame),\n * so a post-tool response measures the model's restart latency, NOT our own\n * tool-execution time.\n * - `opengeni_stream_inter_delta_gap_seconds{provider,class}` — gap between\n * consecutive content deltas of the SAME class. The run resets on any\n * non-content event so a gap never spans a tool call or a model boundary — it\n * measures only the choppiness of a live token stream.\n *\n * Purely observational and clock-injectable; it never touches the events it sees.\n */\nexport class StreamTimingMetrics {\n private readonly now: () => number;\n private ttftAnchor: number;\n private ttftArmed = true;\n private readonly lastDeltaAt = new Map<StreamDeltaClass, number>();\n\n constructor(\n private readonly observability: Observability,\n private readonly options: { provider: string; now?: () => number },\n ) {\n this.now = options.now ?? (() => performance.now());\n this.ttftAnchor = this.now();\n }\n\n onEvent(type: SessionEventType): void {\n const deltaClass = contentDeltaClass(type);\n if (deltaClass === null) {\n // A non-content event: the model paused emitting. Re-arm TTFT so the next\n // content delta measures (re)start latency, and close every inter-delta run\n // so no gap spans a tool call / model boundary.\n this.ttftAnchor = this.now();\n this.ttftArmed = true;\n this.lastDeltaAt.clear();\n return;\n }\n const at = this.now();\n if (this.ttftArmed) {\n this.observability.observeHistogram({\n name: \"opengeni_stream_ttft_seconds\",\n help: \"Seconds from a model (re)start to its first streamed content delta.\",\n buckets: STREAM_TTFT_BUCKETS,\n labels: { provider: this.options.provider },\n value: Math.max(0, (at - this.ttftAnchor) / 1000),\n });\n this.ttftArmed = false;\n }\n const last = this.lastDeltaAt.get(deltaClass);\n if (last !== undefined) {\n this.observability.observeHistogram({\n name: \"opengeni_stream_inter_delta_gap_seconds\",\n help: \"Seconds between consecutive streamed content deltas of the same class.\",\n buckets: STREAM_INTER_DELTA_BUCKETS,\n labels: { provider: this.options.provider, class: deltaClass },\n value: Math.max(0, (at - last) / 1000),\n });\n }\n this.lastDeltaAt.set(deltaClass, at);\n }\n}\n\n// Batch shapes: sizes are small integers; durations are the append+publish round\n// trip the flush performs (sub-ms to a couple seconds under contention).\nconst STREAM_BATCH_SIZE_BUCKETS = [1, 2, 5, 10, 20, 50, 100, 200, 500];\nconst STREAM_IO_BUCKETS = [0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5];\n\n/** Flush shape of the streaming batcher: how many events coalesced into one flush\n * (the coalescing win) and how long that flush took (append + publish). */\nexport function recordBatchFlush(\n observability: Observability,\n input: { events: number; durationSeconds: number },\n): void {\n observability.observeHistogram({\n name: \"opengeni_stream_batch_flush_events\",\n help: \"Session events coalesced into one streaming batcher flush.\",\n buckets: STREAM_BATCH_SIZE_BUCKETS,\n value: input.events,\n });\n observability.observeHistogram({\n name: \"opengeni_stream_batch_flush_duration_seconds\",\n help: \"Duration in seconds of one streaming batcher flush (append + publish).\",\n buckets: STREAM_IO_BUCKETS,\n value: input.durationSeconds,\n });\n}\n\n/** Latency of the durable `appendSessionEvents` DB write — the write path. A p99\n * climb here is our Postgres, not the model or NATS. */\nexport function recordSessionEventAppendLatency(\n observability: Observability,\n input: { durationSeconds: number },\n): void {\n observability.observeHistogram({\n name: \"opengeni_session_event_append_seconds\",\n help: \"Duration in seconds of an appendSessionEvents DB write (the durable write path).\",\n buckets: STREAM_IO_BUCKETS,\n value: input.durationSeconds,\n });\n}\n\n/** Latency of the best-effort NATS live fan-out — the delivery path. A p99 climb\n * here (with append healthy) is delivery, not the write path. */\nexport function recordSessionEventPublishLatency(\n observability: Observability,\n input: { durationSeconds: number },\n): void {\n observability.observeHistogram({\n name: \"opengeni_session_event_publish_seconds\",\n help: \"Duration in seconds of the best-effort NATS live fan-out publish (the delivery path).\",\n buckets: STREAM_IO_BUCKETS,\n value: input.durationSeconds,\n });\n}\n\n// Context tokens per response span a wide range; buckets track the pressure toward\n// a model's window so \"sessions are running hot but never compacting\" is queryable.\nconst MODEL_INPUT_TOKENS_BUCKETS = [\n 1_000, 5_000, 10_000, 25_000, 50_000, 100_000, 150_000, 200_000, 300_000, 500_000, 1_000_000,\n];\n\n/** Observed input (context) tokens per model response — the context-pressure\n * signal. Paired with `opengeni_context_compactions_total`, it makes the\n * \"compaction never firing while contexts run hot\" failure mode expressible. */\nexport function recordModelInputTokens(\n observability: Observability,\n provider: string,\n inputTokens: number,\n): void {\n const normalizedInputTokens = modelUsageTokenCountOrNull(inputTokens);\n if (normalizedInputTokens === null || normalizedInputTokens === 0) {\n return;\n }\n observability.observeHistogram({\n name: \"opengeni_model_input_tokens\",\n help: \"Observed input (context) tokens per model response, by provider.\",\n buckets: MODEL_INPUT_TOKENS_BUCKETS,\n labels: { provider },\n value: normalizedInputTokens,\n });\n}\n\n/** A context compaction actually fired, by trigger (operator | overflow | proactive\n * | auto). The rate of this — against the input-tokens histogram above — is how an\n * operator sees compaction working (or silently not). */\nexport function recordContextCompaction(observability: Observability, trigger: string): void {\n observability.incrementCounter({\n name: \"opengeni_context_compactions_total\",\n help: \"Total context compactions performed, by trigger.\",\n labels: { trigger },\n });\n}\n\nconst MODEL_CONTEXT_CONTRIBUTION_TOKEN_BUCKETS = [1, 8, 32, 128, 512, 2_048, 8_192];\n\n/**\n * Metadata-only Company Brain exposure telemetry. Labels are closed enums and\n * never include tenant ids, record ids, titles, descriptions, or content.\n */\nexport function recordCompanyBrainContributions(\n observability: Observability,\n receipt: CompanyBrainContributionReceipt,\n): void {\n for (const contribution of receipt.contributions) {\n const labels = {\n category: contribution.category,\n source: contribution.source,\n inclusion_reason: contribution.inclusionReason,\n authority_scope: contribution.authorityScope,\n session_role: receipt.sessionRole,\n memory_prompt_mode: receipt.memoryPromptMode,\n };\n observability.incrementCounter({\n name: \"opengeni_model_context_contributions_total\",\n help: \"Company Brain contributions exposed to a model, by bounded reason and authority.\",\n labels,\n });\n observability.observeHistogram({\n name: \"opengeni_model_context_contribution_estimated_tokens\",\n help: \"Estimated tokens per model-visible Company Brain contribution.\",\n buckets: MODEL_CONTEXT_CONTRIBUTION_TOKEN_BUCKETS,\n labels,\n value: contribution.estimatedTokens,\n });\n }\n}\n\n// ── Prompt-cache efficiency ─────────────────────────────────────────────────\n// Per model-call prompt-cache signal, provider-labelled only (bounded\n// cardinality — never a session id or account). `cached_tokens` is the slice of\n// the prompt the provider served from its prompt cache; the ratio cached/prompt\n// is the efficiency of that call. The account-switch hypothesis (a codex account\n// rotation cold-starts the provider's per-account prompt cache) is tested from\n// the per-call STRUCTURED LOG below — the account id is unbounded, so it is\n// hashed into a log field and NEVER a Prometheus label.\n\n// The hit ratio lives in [0, 1]; bucket tighter around the alerting threshold so\n// a p50 near 40% resolves. The default duration buckets (to 3600) would collapse\n// every ratio into the first bucket.\nconst MODEL_CACHE_HIT_RATIO_BUCKETS = [\n 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 1,\n];\n\n/**\n * Prompt-cache efficiency of one model response, by provider. Reads from the SAME\n * usage frame that feeds input-token accounting, so the two are always consistent:\n * - `opengeni_model_cached_tokens_total{provider}` — cumulative prompt tokens the\n * provider served from cache. Advanced only when >0, so a provider that never\n * reports cached tokens contributes nothing rather than phantom zero-increments.\n * - `opengeni_model_cache_write_tokens_total{provider}` — cumulative tokens a\n * reporting provider wrote into its prompt cache. Absent telemetry contributes\n * nothing; in particular, Codex subscription usage does not currently expose it.\n * - `opengeni_model_cache_hit_ratio{provider}` — cached/prompt for the call,\n * observed only when prompt tokens are known (>0) AND cached tokens are reported.\n * A reported cached-token zero records a real 0%; absent/null telemetry remains\n * unknown and must not fabricate a low-cache signal.\n * - `opengeni_model_cache_read_telemetry_total{provider,status}` — one bounded\n * availability observation per authoritative call (`reported` or `missing`).\n * Invalid, fractional, unsafe, or over-contract token telemetry is rejected. A call\n * with no prompt tokens has no ratio (skipped).\n */\nexport function recordModelCacheTokens(\n observability: Observability,\n provider: string,\n input: {\n cachedTokens: number | null | undefined;\n cacheWriteTokens?: number | null | undefined;\n promptTokens: number | null | undefined;\n },\n): void {\n const cached = modelUsageTokenCountOrNull(input.cachedTokens);\n const cacheWrite = modelUsageTokenCountOrNull(input.cacheWriteTokens);\n const prompt = modelUsageTokenCountOrNull(input.promptTokens);\n incrementBoundedModelCacheCounter(observability, {\n name: \"opengeni_model_cache_read_telemetry_total\",\n help: \"Authoritative model calls with reported or missing cache-read telemetry, by provider.\",\n provider,\n labels: { provider, status: cached === null ? \"missing\" : \"reported\" },\n amount: 1,\n });\n if (cached !== null && cached > 0) {\n incrementBoundedModelCacheCounter(observability, {\n name: \"opengeni_model_cached_tokens_total\",\n help: \"Total prompt tokens served from the provider's prompt cache, by provider.\",\n provider,\n labels: { provider },\n amount: cached,\n });\n }\n if (cacheWrite !== null && cacheWrite > 0) {\n incrementBoundedModelCacheCounter(observability, {\n name: \"opengeni_model_cache_write_tokens_total\",\n help: \"Total prompt tokens written to the provider's prompt cache, by provider.\",\n provider,\n labels: { provider },\n amount: cacheWrite,\n });\n }\n if (cached !== null && prompt !== null && prompt > 0) {\n observability.observeHistogram({\n name: \"opengeni_model_cache_hit_ratio\",\n help: \"Per-call prompt-cache hit ratio (cached/prompt tokens) by provider.\",\n buckets: MODEL_CACHE_HIT_RATIO_BUCKETS,\n labels: { provider },\n // Clamp to [0, 1]: a provider that (rarely) reports cached >= prompt must\n // not skew the histogram past 1.0.\n value: Math.min(1, cached / prompt),\n });\n }\n}\n\nfunction incrementBoundedModelCacheCounter(\n observability: Observability,\n input: {\n name: string;\n help: string;\n provider: string;\n labels: Record<string, string>;\n amount: number;\n },\n): void {\n const totals = modelCacheCounterTotals.get(observability) ?? new Map<string, number>();\n if (!modelCacheCounterTotals.has(observability)) {\n modelCacheCounterTotals.set(observability, totals);\n }\n const labelKey = Object.entries(input.labels)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(([key, value]) => `${key}=${value}`)\n .join(\",\");\n const key = `${input.name}:${labelKey}`;\n const current = totals.get(key) ?? 0;\n if (input.amount > MAX_MODEL_CACHE_COUNTER_TOTAL - current) {\n observability.warn(\"model cache metric cumulative limit reached\", {\n provider: input.provider,\n metric: input.name,\n });\n return;\n }\n observability.incrementCounter({\n name: input.name,\n help: input.help,\n labels: input.labels,\n amount: input.amount,\n });\n totals.set(key, current + input.amount);\n}\n\nfunction nonnegativeFinite(value: number): number {\n return Number.isFinite(value) && value > 0 ? value : 0;\n}\n\nfunction requiredTemporalNumber(\n value: unknown,\n field: string,\n options: { integer?: boolean } = {},\n): number {\n if (value === null || value === undefined) {\n throw new Error(`Temporal stats omitted required ${field}`);\n }\n const parsed = Number(value);\n if (\n !Number.isFinite(parsed) ||\n parsed < 0 ||\n (options.integer && !Number.isSafeInteger(parsed))\n ) {\n throw new Error(`Temporal stats returned invalid ${field}: ${String(value)}`);\n }\n return parsed;\n}\n\nfunction optionalTemporalNumber(\n value: unknown,\n field: string,\n options: { integer?: boolean } = {},\n): number {\n return value === null || value === undefined ? 0 : requiredTemporalNumber(value, field, options);\n}\n\n/**\n * An opaque, stable, non-reversible tag for a codex credential (account) — the\n * per-call log dimension the account-switch hypothesis correlates against. It is\n * a hash of the NON-SECRET credential ROW id (never a token/bearer), truncated so\n * it is short in logs while still distinguishing a handful of accounts without\n * collision. A null/absent credential (non-codex turn, no active account) tags as\n * \"none\" so the field is always present and never leaks an id verbatim.\n */\nexport function stableAccountHash(credentialId: string | null | undefined): string {\n if (!credentialId) {\n return \"none\";\n }\n return createHash(\"sha256\").update(credentialId).digest(\"hex\").slice(0, 12);\n}\n\n/**\n * The per-call account dimensions for the usage log: the opaque serving-account\n * tag and whether that account CHANGED versus the session's previous call. Within\n * one turn the serving credential is fixed, so a switch can only surface on the\n * turn's FIRST call (compared against the session's durably-recorded prior\n * credential); later calls in the same turn report `false`. A switch is reported\n * only when there was a KNOWN prior account that differs — a session's very first\n * call (no prior) is a cold start, not a switch.\n */\nexport function modelCallAccountContext(input: {\n servingCredentialId: string | null;\n priorSessionCredentialId: string | null;\n isFirstCallOfTurn: boolean;\n}): { servingAccountHash: string; accountChangedFromPrevCall: boolean } {\n const accountChangedFromPrevCall =\n input.isFirstCallOfTurn &&\n input.servingCredentialId !== null &&\n input.priorSessionCredentialId !== null &&\n input.priorSessionCredentialId !== input.servingCredentialId;\n return {\n servingAccountHash: stableAccountHash(input.servingCredentialId),\n accountChangedFromPrevCall,\n };\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB;AAChC,SAAS,sBAA6C;AAItD;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAwBP,IAAM,eAAe,oBAAI,QAA6C;AACtE,IAAM,6BAA6B,oBAAI,QAAoC;AAC3E,IAAM,0BAA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A,SAAO,WAAW,QAAQ,EAAE,OAAO,YAAY,EAAE,OAAO,KAAK,EAAE,MAAM,GAAG,EAAE;AAC5E;AAWO,SAAS,wBAAwB,OAIgC;AACtE,QAAM,6BACJ,MAAM,qBACN,MAAM,wBAAwB,QAC9B,MAAM,6BAA6B,QACnC,MAAM,6BAA6B,MAAM;AAC3C,SAAO;AAAA,IACL,oBAAoB,kBAAkB,MAAM,mBAAmB;AAAA,IAC/D;AAAA,EACF;AACF;","names":["key"]}
|
package/dist/concurrency.d.ts
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import type { Settings } from "@opengeni/config";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Observability } from "@opengeni/observability";
|
|
3
|
+
import type { ActivitySlotInfo, CustomSlotSupplier, SlotMarkUsedContext, SlotPermit, SlotReleaseContext, SlotReserveContext, WorkerOptions, WorkerTuner } from "@temporalio/worker";
|
|
4
|
+
/**
|
|
5
|
+
* Default fixed/HPA turn-worker admission contract. The configured fixed-mode
|
|
6
|
+
* ceiling may be lower, but every live Temporal permit reserves the complete
|
|
7
|
+
* hard budget until the runAgentTurn activity promise physically completes.
|
|
8
|
+
*/
|
|
9
|
+
export declare const TURN_WORKER_MAX_CONCURRENT_TURNS = 16;
|
|
10
|
+
export declare const TURN_WORKER_HARD_MEMORY_BYTES_PER_TURN: number;
|
|
11
|
+
export declare const TURN_WORKER_NATIVE_HEADROOM_BYTES: number;
|
|
3
12
|
export type TurnWorkerConcurrencySettings = Pick<Settings, "turnWorkerConcurrencyMode" | "turnWorkerMaxConcurrentTurns" | "turnWorkerTargetCpuUsage" | "turnWorkerTargetMemoryUsage">;
|
|
4
13
|
export type TurnWorkerConcurrencyOptions = Pick<WorkerOptions, "maxConcurrentActivityTaskExecutions" | "tuner">;
|
|
5
14
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
15
|
+
* Fixed profiles keep a per-process ceiling. Resource-aware profiles retain a
|
|
16
|
+
* hard per-process maximum while Temporal meters new activity admission against
|
|
17
|
+
* cgroup/host CPU and memory. The ramp lets usage become visible before a burst
|
|
18
|
+
* can reserve the entire ceiling.
|
|
10
19
|
*/
|
|
11
20
|
export declare function turnWorkerConcurrencyOptions(settings: TurnWorkerConcurrencySettings): TurnWorkerConcurrencyOptions;
|
|
21
|
+
export type TurnWorkerConcurrencyPlan = {
|
|
22
|
+
options: TurnWorkerConcurrencyOptions;
|
|
23
|
+
admission: MemoryAwareTurnSlotSupplier | null;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Build the runtime concurrency policy for a turn worker.
|
|
27
|
+
*
|
|
28
|
+
* Fixed/HPA deployments use the hard-reservation supplier below. The explicit
|
|
29
|
+
* non-HA single-machine profile keeps Temporal's native CPU/memory resource
|
|
30
|
+
* tuner because it controls whole-machine load rather than a pod cgroup; that
|
|
31
|
+
* profile documents that its configured maximum is not a per-turn reservation.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createTurnWorkerConcurrencyPlan(settings: TurnWorkerConcurrencySettings, options?: Omit<TurnAdmissionOptions, "maximumTurns">): TurnWorkerConcurrencyPlan;
|
|
12
34
|
export declare function turnWorkerConcurrencyLogFields(settings: TurnWorkerConcurrencySettings): {
|
|
13
35
|
concurrencyMode: "fixed" | "resource-based";
|
|
14
36
|
maxConcurrentTurns: number;
|
|
@@ -17,3 +39,84 @@ export declare function turnWorkerConcurrencyLogFields(settings: TurnWorkerConcu
|
|
|
17
39
|
};
|
|
18
40
|
export declare const CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES = 32;
|
|
19
41
|
export declare const CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS = 40;
|
|
42
|
+
export type CgroupMemorySnapshot = {
|
|
43
|
+
currentBytes: number;
|
|
44
|
+
limitBytes: number | null;
|
|
45
|
+
source: "cgroup-v1" | "cgroup-v2" | "process";
|
|
46
|
+
};
|
|
47
|
+
export type TurnAdmissionSnapshot = {
|
|
48
|
+
baselineBytes: number;
|
|
49
|
+
currentBytes: number;
|
|
50
|
+
limitBytes: number | null;
|
|
51
|
+
hardBytesPerTurn: number;
|
|
52
|
+
nativeHeadroomBytes: number;
|
|
53
|
+
maximumTurns: number;
|
|
54
|
+
memoryBoundCapacity: number;
|
|
55
|
+
reservedSlots: number;
|
|
56
|
+
usedSlots: number;
|
|
57
|
+
availableSlots: number;
|
|
58
|
+
};
|
|
59
|
+
export type TurnAdmissionOptions = {
|
|
60
|
+
maximumTurns?: number;
|
|
61
|
+
hardBytesPerTurn?: number;
|
|
62
|
+
nativeHeadroomBytes?: number;
|
|
63
|
+
memorySnapshot?: () => CgroupMemorySnapshot;
|
|
64
|
+
retryDelayMs?: number;
|
|
65
|
+
observability?: Observability;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Temporal custom activity-slot supplier for turn workers.
|
|
69
|
+
*
|
|
70
|
+
* A permit is required before Temporal polls a task. Reservation is allowed
|
|
71
|
+
* only when both independent safety checks pass:
|
|
72
|
+
*
|
|
73
|
+
* baseline + reserved-after × hard-turn-budget + native headroom <= limit
|
|
74
|
+
* current + reserved-after × hard-turn-budget + native headroom <= limit
|
|
75
|
+
*
|
|
76
|
+
* The first check enforces the release contract even when current RSS happens
|
|
77
|
+
* to be low. The second accounts for native/retained memory observed after
|
|
78
|
+
* startup and keeps the complete hard budget reserved for every live permit.
|
|
79
|
+
* A used permit is not evidence that its allocation is already visible in the
|
|
80
|
+
* cgroup, and unrelated native growth cannot safely be attributed to a turn.
|
|
81
|
+
* Kubernetes OOM is never an admission mechanism.
|
|
82
|
+
*/
|
|
83
|
+
export declare class MemoryAwareTurnSlotSupplier implements CustomSlotSupplier<ActivitySlotInfo> {
|
|
84
|
+
readonly type: "custom";
|
|
85
|
+
private readonly owner;
|
|
86
|
+
private readonly permits;
|
|
87
|
+
private baselineBytes;
|
|
88
|
+
private readonly maximumTurns;
|
|
89
|
+
private readonly hardBytesPerTurn;
|
|
90
|
+
private readonly nativeHeadroomBytes;
|
|
91
|
+
private readonly memorySnapshot;
|
|
92
|
+
private readonly retryDelayMs;
|
|
93
|
+
private readonly observability;
|
|
94
|
+
constructor(options?: TurnAdmissionOptions);
|
|
95
|
+
reserveSlot(_ctx: SlotReserveContext, abortSignal: AbortSignal): Promise<SlotPermit>;
|
|
96
|
+
tryReserveSlot(_ctx: SlotReserveContext): SlotPermit | null;
|
|
97
|
+
markSlotUsed(ctx: SlotMarkUsedContext<ActivitySlotInfo>): void;
|
|
98
|
+
releaseSlot(ctx: SlotReleaseContext<ActivitySlotInfo>): void;
|
|
99
|
+
snapshot(memory?: CgroupMemorySnapshot): TurnAdmissionSnapshot;
|
|
100
|
+
/**
|
|
101
|
+
* Finalize the admission baseline after Temporal/native construction.
|
|
102
|
+
* Worker.create can retain memory after the constructor's provisional
|
|
103
|
+
* sample. That initialization delta must remain in the baseline for every
|
|
104
|
+
* later projection rather than competing with turn reservations.
|
|
105
|
+
*/
|
|
106
|
+
finalizeStartupBaseline(): void;
|
|
107
|
+
private reserveIfSafe;
|
|
108
|
+
private memoryAllowsAnother;
|
|
109
|
+
private availableSlots;
|
|
110
|
+
private memoryBoundCapacity;
|
|
111
|
+
private usedSlots;
|
|
112
|
+
private ownedPermit;
|
|
113
|
+
private refreshMetrics;
|
|
114
|
+
}
|
|
115
|
+
export declare function createTurnWorkerTuner(options?: TurnAdmissionOptions): {
|
|
116
|
+
tuner: WorkerTuner;
|
|
117
|
+
admission: MemoryAwareTurnSlotSupplier;
|
|
118
|
+
};
|
|
119
|
+
export declare function readCgroupMemorySnapshot(): CgroupMemorySnapshot;
|
|
120
|
+
export declare function parseCgroupLimitValue(value: string): number | null;
|
|
121
|
+
export declare function parseCgroupCurrentValue(value: string): number;
|
|
122
|
+
export declare const CONTROL_WORKER_MAX_CACHED_WORKFLOWS = 64;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type EditableArtifactHintBrokerPort } from "@opengeni/core";
|
|
2
|
+
import { type NatsConnection } from "@opengeni/events";
|
|
3
|
+
export type EditableArtifactHintNatsAuth = Readonly<{
|
|
4
|
+
kind: "user-password";
|
|
5
|
+
user: string;
|
|
6
|
+
pass: string;
|
|
7
|
+
}> | Readonly<{
|
|
8
|
+
kind: "token";
|
|
9
|
+
token: string;
|
|
10
|
+
}> | Readonly<{
|
|
11
|
+
kind: "anonymous";
|
|
12
|
+
}>;
|
|
13
|
+
export type EditableArtifactHintNatsBrokerOptions = Readonly<{
|
|
14
|
+
maxHintBytes?: number;
|
|
15
|
+
}>;
|
|
16
|
+
export type EditableArtifactHintNatsConnection = Pick<NatsConnection, "publish" | "flush" | "drain" | "close" | "closed" | "isClosed" | "isDraining">;
|
|
17
|
+
/**
|
|
18
|
+
* Core-NATS adapter for the durable outbox. A publish resolves only after a
|
|
19
|
+
* server round-trip flush ACK. Reconnect buffering may create duplicates after
|
|
20
|
+
* a timeout; hints are intentionally idempotent wakeups for durable gap-fill.
|
|
21
|
+
*/
|
|
22
|
+
export declare class ConfirmedNatsEditableArtifactHintBroker implements EditableArtifactHintBrokerPort {
|
|
23
|
+
private readonly connection;
|
|
24
|
+
private readonly maxHintBytes;
|
|
25
|
+
constructor(connection: EditableArtifactHintNatsConnection, options?: EditableArtifactHintNatsBrokerOptions);
|
|
26
|
+
publish(input: Readonly<{
|
|
27
|
+
subject: string;
|
|
28
|
+
payload: Uint8Array;
|
|
29
|
+
signal: AbortSignal;
|
|
30
|
+
}>): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* A readiness probe must prove the server is reachable, not merely that the
|
|
33
|
+
* client object has not observed closure yet. `flush()` is a bounded NATS
|
|
34
|
+
* round trip and has the same acknowledgement semantics as publication.
|
|
35
|
+
*/
|
|
36
|
+
check(signal?: AbortSignal): Promise<void>;
|
|
37
|
+
close(drainTimeoutMs?: number): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
export declare class EditableArtifactNatsHintPublishError extends Error {
|
|
40
|
+
readonly code: "broker_unavailable" | "broker_backpressure";
|
|
41
|
+
constructor(code: "broker_unavailable" | "broker_backpressure", message: string, options?: ErrorOptions);
|
|
42
|
+
}
|
|
43
|
+
/** Opens one resilient dispatcher identity; callers own `broker.close()`. */
|
|
44
|
+
export declare function connectEditableArtifactHintBroker(input: Readonly<{
|
|
45
|
+
natsUrl: string;
|
|
46
|
+
auth: EditableArtifactHintNatsAuth;
|
|
47
|
+
name?: string;
|
|
48
|
+
maxHintBytes?: number;
|
|
49
|
+
}>): Promise<ConfirmedNatsEditableArtifactHintBroker>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { VerifiedEditableArtifactProcessLauncher } from "./editable-artifact-materializer-subprocess.js";
|
|
2
|
+
export type LinuxEditableArtifactLauncherOptions = Readonly<{
|
|
3
|
+
materializerExecutable: string;
|
|
4
|
+
bubblewrapExecutable: string;
|
|
5
|
+
prlimitExecutable: string;
|
|
6
|
+
runtimeRoot: string;
|
|
7
|
+
memoryLimitBytes: number;
|
|
8
|
+
cpuTimeLimitMs: number;
|
|
9
|
+
fileDescriptorLimit: number;
|
|
10
|
+
processLimit: number;
|
|
11
|
+
fileSizeLimitBytes: number;
|
|
12
|
+
probeExecutable?: string;
|
|
13
|
+
}>;
|
|
14
|
+
export type DevelopmentEditableArtifactLauncherOptions = Readonly<{
|
|
15
|
+
materializerExecutable: string;
|
|
16
|
+
explicitlyEnabled: boolean;
|
|
17
|
+
nodeEnvironment: string | undefined;
|
|
18
|
+
}>;
|
|
19
|
+
/**
|
|
20
|
+
* Current-host local development only. This deliberately reports that no OS
|
|
21
|
+
* sandbox, network namespace, or process resource limit is enforced. The
|
|
22
|
+
* caller must surface that degraded capability and can never select this path
|
|
23
|
+
* in production.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createDevelopmentEditableArtifactProcessLauncher(options: DevelopmentEditableArtifactLauncherOptions): Promise<VerifiedEditableArtifactProcessLauncher>;
|
|
26
|
+
/**
|
|
27
|
+
* Concrete production launcher for Linux. Isolation facts are established by
|
|
28
|
+
* a parent-run behavioral probe of the actual bwrap+prlimit command, never by
|
|
29
|
+
* the untrusted codec child. macOS/Windows require their own native launcher
|
|
30
|
+
* implementation and therefore fail closed in production today.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createLinuxEditableArtifactProcessLauncher(options: LinuxEditableArtifactLauncherOptions): Promise<VerifiedEditableArtifactProcessLauncher>;
|
|
33
|
+
export declare function linuxEditableArtifactSandboxArguments(input: Readonly<{
|
|
34
|
+
executable: string;
|
|
35
|
+
args: readonly string[];
|
|
36
|
+
runtimeRoot: string;
|
|
37
|
+
scratchLimitBytes: number;
|
|
38
|
+
}>): readonly string[];
|
|
39
|
+
export declare function linuxEditableArtifactPrlimitArguments(input: Readonly<{
|
|
40
|
+
memoryLimitBytes: number;
|
|
41
|
+
cpuSeconds: number;
|
|
42
|
+
fileDescriptorLimit: number;
|
|
43
|
+
processLimit: number;
|
|
44
|
+
fileSizeLimitBytes: number;
|
|
45
|
+
}>): readonly string[];
|
|
46
|
+
export declare function linuxEditableArtifactSandboxRootEntries(input: Readonly<{
|
|
47
|
+
executable: string;
|
|
48
|
+
runtimeRoot: string;
|
|
49
|
+
}>): readonly string[];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type Settings } from "@opengeni/config";
|
|
2
|
+
import { type Database } from "@opengeni/db";
|
|
3
|
+
import { type Observability } from "@opengeni/observability";
|
|
4
|
+
import { type ObjectStorage } from "@opengeni/storage";
|
|
5
|
+
import { EditableArtifactMaterializer, type EditableArtifactMaterializerPassSummary } from "./editable-artifact-materializer.js";
|
|
6
|
+
export type EditableArtifactRuntimeAuthority = Readonly<{
|
|
7
|
+
mode: "production";
|
|
8
|
+
environmentVariable: "OPENGENI_ARTIFACT_RUNTIME_MANIFEST";
|
|
9
|
+
manifestPath: string;
|
|
10
|
+
}> | Readonly<{
|
|
11
|
+
mode: "development-current-host";
|
|
12
|
+
environmentVariable: "OPENGENI_ARTIFACT_DEVELOPMENT_RUNTIME_MANIFEST";
|
|
13
|
+
manifestPath: string;
|
|
14
|
+
}>;
|
|
15
|
+
export type EditableArtifactMaterializerServiceState = "starting" | "ready" | "draining" | "stopped" | "failed";
|
|
16
|
+
export type EditableArtifactMaterializerService = Readonly<{
|
|
17
|
+
state(): EditableArtifactMaterializerServiceState;
|
|
18
|
+
run(): Promise<void>;
|
|
19
|
+
drain(reason?: string): boolean;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
dispatchOnce(signal?: AbortSignal): Promise<EditableArtifactMaterializerPassSummary>;
|
|
22
|
+
fetch(request: Request): Promise<Response>;
|
|
23
|
+
}>;
|
|
24
|
+
export type CreateEditableArtifactMaterializerServiceInput = Readonly<{
|
|
25
|
+
materializer: EditableArtifactMaterializer;
|
|
26
|
+
db: Database;
|
|
27
|
+
closeDatabase: () => Promise<void>;
|
|
28
|
+
observability: Observability;
|
|
29
|
+
serviceName: string;
|
|
30
|
+
environment: string;
|
|
31
|
+
deploymentRevision?: string;
|
|
32
|
+
readinessTimeoutMs?: number;
|
|
33
|
+
runtimeCapability?: Readonly<{
|
|
34
|
+
mode: EditableArtifactRuntimeAuthority["mode"];
|
|
35
|
+
sandboxEnforced: boolean;
|
|
36
|
+
}>;
|
|
37
|
+
}>;
|
|
38
|
+
export type ProductionEditableArtifactMaterializerServiceOptions = Readonly<{
|
|
39
|
+
settings: Settings;
|
|
40
|
+
/** Dedicated DSN; never defaults to settings.databaseUrl. */
|
|
41
|
+
databaseUrl: string;
|
|
42
|
+
declaredDatabaseRole: string;
|
|
43
|
+
executable: string;
|
|
44
|
+
bubblewrapExecutable: string | null;
|
|
45
|
+
prlimitExecutable: string | null;
|
|
46
|
+
runtimeAuthority: EditableArtifactRuntimeAuthority;
|
|
47
|
+
/** Explicit exception selected only by the validated local-dev sidecar config. */
|
|
48
|
+
allowUnsandboxedLocalDevelopment?: boolean;
|
|
49
|
+
skillFacadeEntrypoint: string;
|
|
50
|
+
owner: string;
|
|
51
|
+
objectStorage?: ObjectStorage;
|
|
52
|
+
batchSize?: number;
|
|
53
|
+
concurrency?: number;
|
|
54
|
+
leaseDurationMs?: number;
|
|
55
|
+
leaseRenewIntervalMs?: number;
|
|
56
|
+
pollIntervalMs?: number;
|
|
57
|
+
maxAttempts?: number;
|
|
58
|
+
memoryLimitBytes: number;
|
|
59
|
+
cpuTimeLimitMs: number;
|
|
60
|
+
fileDescriptorLimit: number;
|
|
61
|
+
processLimit: number;
|
|
62
|
+
fileSizeLimitBytes: number;
|
|
63
|
+
wallTimeoutMs: number;
|
|
64
|
+
nodeEnvironment?: string;
|
|
65
|
+
maxSourceBytes?: number;
|
|
66
|
+
maxOutputBytes?: number;
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* Compose the distinct production sidecar. It creates its own connection pool
|
|
70
|
+
* from the dedicated DSN and never reuses a control/turn worker handle.
|
|
71
|
+
*/
|
|
72
|
+
export declare function createProductionEditableArtifactMaterializerService(options: ProductionEditableArtifactMaterializerServiceOptions): Promise<EditableArtifactMaterializerService>;
|
|
73
|
+
export declare function createEditableArtifactMaterializerService(input: CreateEditableArtifactMaterializerServiceInput): EditableArtifactMaterializerService;
|
|
74
|
+
export declare function assertDedicatedMaterializerDatabaseRole(db: Database): Promise<void>;
|
|
75
|
+
export type EditableArtifactMaterializerSidecarEnvironment = Readonly<{
|
|
76
|
+
databaseUrl: string;
|
|
77
|
+
declaredDatabaseRole: string;
|
|
78
|
+
executable: string;
|
|
79
|
+
bubblewrapExecutable: string | null;
|
|
80
|
+
prlimitExecutable: string | null;
|
|
81
|
+
runtimeAuthority: EditableArtifactRuntimeAuthority;
|
|
82
|
+
skillFacadeEntrypoint: string;
|
|
83
|
+
owner: string;
|
|
84
|
+
httpHostname: string;
|
|
85
|
+
httpPort: number;
|
|
86
|
+
batchSize: number;
|
|
87
|
+
concurrency: number;
|
|
88
|
+
leaseDurationMs: number;
|
|
89
|
+
leaseRenewIntervalMs: number;
|
|
90
|
+
pollIntervalMs: number;
|
|
91
|
+
maxAttempts: number;
|
|
92
|
+
memoryLimitBytes: number;
|
|
93
|
+
cpuTimeLimitMs: number;
|
|
94
|
+
fileDescriptorLimit: number;
|
|
95
|
+
processLimit: number;
|
|
96
|
+
fileSizeLimitBytes: number;
|
|
97
|
+
wallTimeoutMs: number;
|
|
98
|
+
maxSourceBytes: number;
|
|
99
|
+
maxOutputBytes: number;
|
|
100
|
+
allowUnsandboxedLocalDevelopment: boolean;
|
|
101
|
+
nodeEnvironment?: string;
|
|
102
|
+
}>;
|
|
103
|
+
export declare function readEditableArtifactMaterializerSidecarEnvironment(environment?: NodeJS.ProcessEnv): EditableArtifactMaterializerSidecarEnvironment | null;
|
|
104
|
+
export declare function createMaterializerSidecarFromEnvironment(environment?: NodeJS.ProcessEnv): Promise<Readonly<{
|
|
105
|
+
service: EditableArtifactMaterializerService;
|
|
106
|
+
httpHostname: string;
|
|
107
|
+
httpPort: number;
|
|
108
|
+
}> | null>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type BoundedImmutableObjectWritePort, type BoundedObjectReadPort, type ObjectStorage } from "@opengeni/storage";
|
|
2
|
+
import { type EditableArtifactMaterializationSemanticVerifierPort, type EditableArtifactMaterializationVerifierPort } from "./editable-artifact-materializer.js";
|
|
3
|
+
/** Provider-versioned, immutable range reader. No whole-object fallback. */
|
|
4
|
+
export declare function createEditableArtifactObjectStorageReader(storage: ObjectStorage): BoundedObjectReadPort;
|
|
5
|
+
/** Conditional-create digest writer with independent version-pinned readback. */
|
|
6
|
+
export declare function createEditableArtifactObjectStorageWriter(storage: ObjectStorage): BoundedImmutableObjectWritePort;
|
|
7
|
+
/**
|
|
8
|
+
* Independent immutable readback + structural parser. When the native source
|
|
9
|
+
* projection supplies a semantic digest, a second isolated codec process must
|
|
10
|
+
* re-import the published Office bytes and prove that digest before success.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createEditableArtifactObjectStorageVerifier(storage: ObjectStorage, semanticVerifier: EditableArtifactMaterializationSemanticVerifierPort): EditableArtifactMaterializationVerifierPort;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import { type NativeEditableArtifactMaterializerPort } from "./editable-artifact-materializer.js";
|
|
3
|
+
export type NativeEditableArtifactSubprocessOptions = Readonly<{
|
|
4
|
+
/** Absolute path to the exact codec executable. Isolation belongs to launcher. */
|
|
5
|
+
executable: string;
|
|
6
|
+
launcher: VerifiedEditableArtifactProcessLauncher;
|
|
7
|
+
childEnvironment?: Readonly<Record<string, string>>;
|
|
8
|
+
/** Explicitly permits the current-host local-dev launcher; never set in production. */
|
|
9
|
+
allowUnsandboxedDevelopment?: boolean;
|
|
10
|
+
wallTimeoutMs: number;
|
|
11
|
+
maxSourceBytes: number;
|
|
12
|
+
maxOutputBytes: number;
|
|
13
|
+
probeTimeoutMs?: number;
|
|
14
|
+
}>;
|
|
15
|
+
export type VerifiedEditableArtifactProcessLauncher = Readonly<{
|
|
16
|
+
identity: Readonly<{
|
|
17
|
+
platform: string;
|
|
18
|
+
isolation: "subprocess";
|
|
19
|
+
network: "denied" | "host";
|
|
20
|
+
officeAutomation: false;
|
|
21
|
+
sandboxEnforced: boolean;
|
|
22
|
+
memoryLimitBytes: number;
|
|
23
|
+
cpuTimeLimitMs: number;
|
|
24
|
+
fileDescriptorLimit: number;
|
|
25
|
+
processLimit: number;
|
|
26
|
+
fileSizeLimitBytes: number;
|
|
27
|
+
}>;
|
|
28
|
+
spawn(input: {
|
|
29
|
+
executable: string;
|
|
30
|
+
args: readonly string[];
|
|
31
|
+
environment: Readonly<Record<string, string>>;
|
|
32
|
+
}): ChildProcessWithoutNullStreams;
|
|
33
|
+
}>;
|
|
34
|
+
export type NativeEditableArtifactSubprocessPort = NativeEditableArtifactMaterializerPort & Readonly<{
|
|
35
|
+
verifyMaterialization(input: {
|
|
36
|
+
format: string;
|
|
37
|
+
codecId: string;
|
|
38
|
+
codecVersion: string;
|
|
39
|
+
expectedSemanticHash: string;
|
|
40
|
+
byteSize: number;
|
|
41
|
+
chunks: AsyncIterable<Uint8Array>;
|
|
42
|
+
signal: AbortSignal;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* Probe exact codec/kernel capabilities and combine them with independently
|
|
47
|
+
* verified parent-launcher isolation. The child is never trusted to attest its
|
|
48
|
+
* own network namespace, resource limits, or platform sandbox.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createNativeEditableArtifactSubprocessPort(options: NativeEditableArtifactSubprocessOptions): Promise<NativeEditableArtifactSubprocessPort>;
|