@opengeni/worker-bundle 3.2.2 → 3.4.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.
Files changed (38) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +1 -1
  2. package/dist/{activities-control-LC7WXEIX.js → activities-control-EWP3UDZP.js} +5 -5
  3. package/dist/{activities-turn-PEDPUZI4.js → activities-turn-COUX2R3A.js} +17 -4
  4. package/dist/activities-turn-COUX2R3A.js.map +1 -0
  5. package/dist/artifact-materializer-entry.js +1 -0
  6. package/dist/artifact-materializer-entry.js.map +1 -1
  7. package/dist/artifact-outbox-entry.js +3 -1
  8. package/dist/artifact-outbox-entry.js.map +1 -1
  9. package/dist/{chunk-5NNS4327.js → chunk-BX3KRGDM.js} +10 -4
  10. package/dist/{chunk-5NNS4327.js.map → chunk-BX3KRGDM.js.map} +1 -1
  11. package/dist/{chunk-NQDVJ6RI.js → chunk-PFCSGZGA.js} +10 -5
  12. package/dist/{chunk-NQDVJ6RI.js.map → chunk-PFCSGZGA.js.map} +1 -1
  13. package/dist/{chunk-O7HT4LHX.js → chunk-PG66GHGH.js} +3 -3
  14. package/dist/{chunk-436TOEBL.js → chunk-XUIWRRTY.js} +5 -4
  15. package/dist/chunk-XUIWRRTY.js.map +1 -0
  16. package/dist/editable-artifact-outbox-dispatcher.d.ts +2 -0
  17. package/dist/index.js +30 -5
  18. package/dist/index.js.map +1 -1
  19. package/dist/internal-maintenance.d.ts +3 -1
  20. package/dist/observability-metrics.d.ts +14 -0
  21. package/dist/{sandbox-lease-F47EDHHY.js → sandbox-lease-XBZWILZ3.js} +4 -4
  22. package/dist/sandbox-routing.d.ts +1 -0
  23. package/package.json +26 -26
  24. package/src/activities/agent-turn/run.ts +15 -0
  25. package/src/activities/agent-turn/sandbox-establish.ts +2 -0
  26. package/src/activity-services.ts +3 -0
  27. package/src/editable-artifact-materializer-service.ts +3 -0
  28. package/src/editable-artifact-outbox-dispatcher.ts +5 -0
  29. package/src/editable-artifact-outbox-service.ts +3 -0
  30. package/src/index.ts +6 -0
  31. package/src/internal-maintenance.ts +24 -0
  32. package/src/observability-metrics.ts +20 -3
  33. package/src/sandbox-routing.ts +6 -0
  34. package/dist/activities-turn-PEDPUZI4.js.map +0 -1
  35. package/dist/chunk-436TOEBL.js.map +0 -1
  36. /package/dist/{activities-control-LC7WXEIX.js.map → activities-control-EWP3UDZP.js.map} +0 -0
  37. /package/dist/{chunk-O7HT4LHX.js.map → chunk-PG66GHGH.js.map} +0 -0
  38. /package/dist/{sandbox-lease-F47EDHHY.js.map → sandbox-lease-XBZWILZ3.js.map} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/opensandbox-kubernetes-inventory.ts","../src/observability-metrics.ts","../src/telemetry-scheduler.ts"],"sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { request as httpsRequest } from \"node:https\";\n\nconst KUBERNETES_API_URL = \"https://kubernetes.default.svc\";\nconst KUBERNETES_TOKEN_FILE = \"/var/run/secrets/opengeni.io/opensandbox-kubernetes-inventory/token\";\nconst KUBERNETES_CA_FILE = \"/var/run/secrets/opengeni.io/opensandbox-kubernetes-inventory/ca.crt\";\nconst KUBERNETES_REQUEST_TIMEOUT_MS = 10_000;\nconst KUBERNETES_RESPONSE_MAX_BYTES = 64 * 1024 * 1024;\nconst KUBERNETES_LIST_PAGE_SIZE = 200;\nconst KUBERNETES_LIST_MAX_PAGES = 10_000;\n\nexport const OPENSANDBOX_CLEANUP_STUCK_SECONDS = 300;\nexport const OPENSANDBOX_EXPIRATION_OVERDUE_SECONDS = 180;\n\nexport const OPENSANDBOX_BATCHSANDBOX_PHASES = [\n \"pending\",\n \"running\",\n \"pausing\",\n \"paused\",\n \"resuming\",\n \"failed\",\n \"unknown\",\n] as const;\n\nexport type OpenSandboxBatchSandboxPhase = (typeof OPENSANDBOX_BATCHSANDBOX_PHASES)[number];\n\nexport const OPENSANDBOX_WORKLOAD_POD_CONDITIONS = [\n \"pending\",\n \"image_pull\",\n \"unschedulable\",\n] as const;\n\nexport type OpenSandboxWorkloadPodCondition = (typeof OPENSANDBOX_WORKLOAD_POD_CONDITIONS)[number];\n\nexport type OpenSandboxKubernetesInventory = {\n batchSandboxPhases: Record<OpenSandboxBatchSandboxPhase, number>;\n workloadPodConditions: Record<OpenSandboxWorkloadPodCondition, number>;\n cleanupStuck: number;\n expirationOverdue: number;\n};\n\nexport type KubernetesJsonRequest = (url: URL) => Promise<unknown>;\n\ntype JsonRecord = Record<string, unknown>;\n\nconst IMAGE_PULL_REASONS = new Set([\"ErrImagePull\", \"ImagePullBackOff\", \"InvalidImageName\"]);\n\nexport async function inspectOpenSandboxKubernetesInventory(input: {\n namespace: string;\n nowMs?: number;\n requestJson?: KubernetesJsonRequest;\n apiUrl?: string;\n tokenFile?: string;\n caFile?: string;\n}): Promise<OpenSandboxKubernetesInventory> {\n const namespace = kubernetesNamespace(input.namespace);\n const nowMs = input.nowMs ?? Date.now();\n const requestJson =\n input.requestJson ??\n (await authenticatedKubernetesJsonRequest({\n tokenFile: input.tokenFile ?? KUBERNETES_TOKEN_FILE,\n caFile: input.caFile ?? KUBERNETES_CA_FILE,\n }));\n const apiUrl = new URL(input.apiUrl ?? KUBERNETES_API_URL);\n if (apiUrl.protocol !== \"https:\") {\n throw new Error(\"OpenSandbox Kubernetes inventory API must use HTTPS\");\n }\n\n const inventory: OpenSandboxKubernetesInventory = {\n batchSandboxPhases: zeroRecord(OPENSANDBOX_BATCHSANDBOX_PHASES),\n workloadPodConditions: zeroRecord(OPENSANDBOX_WORKLOAD_POD_CONDITIONS),\n cleanupStuck: 0,\n expirationOverdue: 0,\n };\n\n await visitKubernetesList({\n apiUrl,\n requestJson,\n path: `/apis/sandbox.opensandbox.io/v1alpha1/namespaces/${encodeURIComponent(namespace)}/batchsandboxes`,\n visit: (item) => observeBatchSandbox(inventory, item, nowMs),\n });\n await visitKubernetesList({\n apiUrl,\n requestJson,\n path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods`,\n query: { labelSelector: \"opensandbox.io/id\" },\n visit: (item) => observeWorkloadPod(inventory, item),\n });\n\n return inventory;\n}\n\nfunction observeBatchSandbox(\n inventory: OpenSandboxKubernetesInventory,\n item: unknown,\n nowMs: number,\n): void {\n const resource = record(item);\n const metadata = record(resource.metadata);\n const spec = record(resource.spec);\n const status = record(resource.status);\n\n inventory.batchSandboxPhases[batchSandboxPhase(status.phase)] += 1;\n\n const deletionTimestampMs = dateMilliseconds(metadata.deletionTimestamp);\n const finalizers = Array.isArray(metadata.finalizers) ? metadata.finalizers : [];\n if (\n deletionTimestampMs !== null &&\n finalizers.length > 0 &&\n nowMs - deletionTimestampMs >= OPENSANDBOX_CLEANUP_STUCK_SECONDS * 1_000\n ) {\n inventory.cleanupStuck += 1;\n }\n\n const expirationMs = dateMilliseconds(spec.expireTime);\n if (\n expirationMs !== null &&\n nowMs - expirationMs >= OPENSANDBOX_EXPIRATION_OVERDUE_SECONDS * 1_000\n ) {\n inventory.expirationOverdue += 1;\n }\n}\n\nfunction observeWorkloadPod(inventory: OpenSandboxKubernetesInventory, item: unknown): void {\n const pod = record(item);\n const metadata = record(pod.metadata);\n if (dateMilliseconds(metadata.deletionTimestamp) !== null) return;\n\n const status = record(pod.status);\n if (status.phase === \"Pending\") inventory.workloadPodConditions.pending += 1;\n if (podHasImagePullFailure(status)) inventory.workloadPodConditions.image_pull += 1;\n if (podIsUnschedulable(status)) inventory.workloadPodConditions.unschedulable += 1;\n}\n\nfunction podHasImagePullFailure(status: JsonRecord): boolean {\n for (const key of [\"initContainerStatuses\", \"containerStatuses\", \"ephemeralContainerStatuses\"]) {\n const statuses = Array.isArray(status[key]) ? status[key] : [];\n for (const value of statuses) {\n const waiting = record(record(record(value).state).waiting);\n if (typeof waiting.reason === \"string\" && IMAGE_PULL_REASONS.has(waiting.reason)) return true;\n }\n }\n return false;\n}\n\nfunction podIsUnschedulable(status: JsonRecord): boolean {\n const conditions = Array.isArray(status.conditions) ? status.conditions : [];\n return conditions.some((value) => {\n const condition = record(value);\n return (\n condition.type === \"PodScheduled\" &&\n condition.status === \"False\" &&\n condition.reason === \"Unschedulable\"\n );\n });\n}\n\nfunction batchSandboxPhase(value: unknown): OpenSandboxBatchSandboxPhase {\n switch (value) {\n case \"Pending\":\n return \"pending\";\n case \"Succeed\":\n case \"Running\":\n return \"running\";\n case \"Pausing\":\n return \"pausing\";\n case \"Paused\":\n return \"paused\";\n case \"Resuming\":\n return \"resuming\";\n case \"Failed\":\n return \"failed\";\n default:\n return \"unknown\";\n }\n}\n\nasync function visitKubernetesList(input: {\n apiUrl: URL;\n requestJson: KubernetesJsonRequest;\n path: string;\n query?: Record<string, string>;\n visit: (item: unknown) => void;\n}): Promise<void> {\n let continuation = \"\";\n for (let page = 0; page < KUBERNETES_LIST_MAX_PAGES; page += 1) {\n const url = new URL(input.path, input.apiUrl);\n url.searchParams.set(\"limit\", String(KUBERNETES_LIST_PAGE_SIZE));\n for (const [key, value] of Object.entries(input.query ?? {})) {\n url.searchParams.set(key, value);\n }\n if (continuation) url.searchParams.set(\"continue\", continuation);\n\n const payload = record(await input.requestJson(url));\n if (!Array.isArray(payload.items)) {\n throw new Error(`Kubernetes list ${input.path} returned no items array`);\n }\n for (const item of payload.items) input.visit(item);\n\n const next = record(payload.metadata).continue;\n if (typeof next !== \"string\" || next.length === 0) return;\n if (next === continuation) {\n throw new Error(`Kubernetes list ${input.path} repeated its continuation token`);\n }\n continuation = next;\n }\n throw new Error(`Kubernetes list ${input.path} exceeded its page safety bound`);\n}\n\nasync function authenticatedKubernetesJsonRequest(input: {\n tokenFile: string;\n caFile: string;\n}): Promise<KubernetesJsonRequest> {\n const [tokenText, ca] = await Promise.all([\n readFile(input.tokenFile, \"utf8\"),\n readFile(input.caFile),\n ]);\n const token = tokenText.trim();\n if (!token) throw new Error(\"OpenSandbox Kubernetes inventory token is empty\");\n\n return async (url) =>\n await new Promise<unknown>((resolve, reject) => {\n const request = httpsRequest(\n url,\n {\n method: \"GET\",\n ca,\n headers: {\n accept: \"application/json\",\n authorization: `Bearer ${token}`,\n },\n },\n (response) => {\n const chunks: Buffer[] = [];\n let bytes = 0;\n response.on(\"data\", (chunk: Buffer | string) => {\n const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);\n bytes += buffer.length;\n if (bytes > KUBERNETES_RESPONSE_MAX_BYTES) {\n request.destroy(new Error(\"Kubernetes inventory response exceeded its byte limit\"));\n return;\n }\n chunks.push(buffer);\n });\n response.on(\"end\", () => {\n const statusCode = response.statusCode ?? 0;\n if (statusCode < 200 || statusCode >= 300) {\n reject(new Error(`Kubernetes inventory request failed with HTTP ${statusCode}`));\n return;\n }\n try {\n resolve(JSON.parse(Buffer.concat(chunks).toString(\"utf8\")));\n } catch {\n reject(new Error(\"Kubernetes inventory response was not valid JSON\"));\n }\n });\n response.on(\"error\", reject);\n },\n );\n request.setTimeout(KUBERNETES_REQUEST_TIMEOUT_MS, () => {\n request.destroy(new Error(\"Kubernetes inventory request timed out\"));\n });\n request.on(\"error\", reject);\n request.end();\n });\n}\n\nfunction kubernetesNamespace(value: string): string {\n const namespace = value.trim();\n if (\n namespace.length === 0 ||\n namespace.length > 63 ||\n !/^[a-z0-9](?:[-a-z0-9]*[a-z0-9])?$/u.test(namespace)\n ) {\n throw new Error(\"OpenSandbox Kubernetes inventory namespace is invalid\");\n }\n return namespace;\n}\n\nfunction dateMilliseconds(value: unknown): number | null {\n if (typeof value !== \"string\" || value.length === 0) return null;\n const milliseconds = Date.parse(value);\n return Number.isFinite(milliseconds) ? milliseconds : null;\n}\n\nfunction record(value: unknown): JsonRecord {\n return value && typeof value === \"object\" && !Array.isArray(value) ? (value as JsonRecord) : {};\n}\n\nfunction zeroRecord<const T extends readonly string[]>(keys: T): Record<T[number], number> {\n return Object.fromEntries(keys.map((key) => [key, 0])) as Record<T[number], number>;\n}\n","import { createHash } from \"node:crypto\";\nimport { errorCodeToJSON } from \"@opengeni/agent-proto\";\nimport {\n BundledSkillId,\n CREDIT_GRANT_CLASSES,\n SandboxBackend,\n type SessionEventType,\n type SkillReadKind,\n type SkillUseSource,\n} from \"@opengeni/contracts\";\nimport {\n ACTIVE_USER_WINDOWS,\n type ActiveUserWindow,\n type CreditGrantTotals,\n type IdleInteractionInspection,\n type SessionEventAppendPhaseObservation,\n} from \"@opengeni/db\";\nimport {\n natsSubscriptionTerminationCounter,\n type EventBusOptions,\n type EventLogger,\n} from \"@opengeni/events\";\nimport type { Attributes, AttributeValue, Observability } from \"@opengeni/observability\";\nimport type { CompanyBrainContributionReceipt } from \"./model-context-contributions\";\nimport {\n createTelemetryClock,\n telemetryInterval,\n telemetryScheduler,\n type TelemetryScheduler,\n} from \"./telemetry-scheduler\";\nimport {\n OPENSANDBOX_BATCHSANDBOX_PHASES,\n OPENSANDBOX_WORKLOAD_POD_CONDITIONS,\n type OpenSandboxKubernetesInventory,\n} from \"./opensandbox-kubernetes-inventory\";\nimport {\n MCP_TOOL_METRIC_EXTERNAL_LABEL,\n SELFHOSTED_INFRASTRUCTURE_FAULT_CLASSES,\n WORKSPACE_CAPTURE_SKIP_REASONS,\n isMcpToolMetricLabel,\n modelUsageTokenCountOrNull,\n type RuntimeMetricsHooks,\n type SelfhostedOpObservation,\n type SelfhostedOpObserver,\n type ToolPreparationPhaseMeasurement,\n} from \"@opengeni/runtime\";\n\nexport type TurnOutcome = \"completed\" | \"failed\" | \"cancelled\" | \"recovering\";\nexport type WorkerDeathRecoveryOutcome = \"recovering\" | \"exhausted\";\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};\nexport type TurnTaskQueueIdentity = { temporalNamespace: string; taskQueue: string };\nexport type TurnTaskQueueReadOptions = { signal: AbortSignal; deadline: number };\n\nexport function turnTaskQueueMetricLabels(identity: TurnTaskQueueIdentity) {\n return { temporal_namespace: identity.temporalNamespace, task_queue: identity.taskQueue };\n}\nexport type SessionRecoveryBacklogStateSummary = {\n /** Every durable recovering session in this state with no active attempt. */\n count: number;\n /** Of those, sessions still inside the backoff their recovery request recorded. */\n scheduled: number;\n /** Seconds the single oldest candidate has been past its recovery due time; 0 when none is. */\n oldestOverdueSeconds: number;\n};\nexport type SessionRecoveryBacklog = {\n quiescence_missing: SessionRecoveryBacklogStateSummary;\n projection_stale: SessionRecoveryBacklogStateSummary;\n};\nexport type ContextCompactionPendingSummary = {\n pendingCount: number;\n oldestStartedAt: Date | null;\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 modelRequestTrackers = new WeakMap<Observability, ModelRequestLifecycleMetrics>();\nconst creditBalanceGaugeAccounts = new WeakMap<Observability, Set<string>>();\nconst modelCacheCounterTotals = new WeakMap<Observability, Map<string, number>>();\nconst initializedContextCompactionMetrics = new WeakSet<Observability>();\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;\nconst TURN_OUTCOMES: readonly TurnOutcome[] = [\"completed\", \"failed\", \"cancelled\", \"recovering\"];\nconst WORKER_DEATH_RECOVERY_OUTCOMES: readonly WorkerDeathRecoveryOutcome[] = [\n \"recovering\",\n \"exhausted\",\n];\nconst WORKER_DEATH_TIMEOUT_TYPES = [\"heartbeat\", \"schedule_to_start\"] as const;\nconst CONTEXT_COMPACTION_TRIGGERS = [\"auto\", \"operator\", \"proactive\", \"overflow\"] as const;\n\nexport type ContextCompactionTrigger = (typeof CONTEXT_COMPACTION_TRIGGERS)[number];\n\nconst CONTEXT_COMPACTION_STARTS_METRIC = {\n name: \"opengeni_context_compaction_starts_total\",\n help: \"Total durable context compaction starts, by trigger.\",\n} as const;\nconst CONTEXT_COMPACTIONS_METRIC = {\n name: \"opengeni_context_compactions_total\",\n help: \"Total completed context compactions, by trigger.\",\n} as const;\n\n/** Logger plus the closed-label subscription-termination counter for NATS connections. */\nexport function observabilityEventBusOptions(\n observability: Observability,\n): Pick<EventBusOptions, \"logger\" | \"onSubscriptionTerminated\"> {\n return {\n logger: observabilityEventLogger(observability),\n onSubscriptionTerminated: natsSubscriptionTerminationCounter(observability),\n };\n}\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 completedOperationSpan(observability, \"worker.model.call\", durationSeconds, {\n provider,\n outcome,\n });\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 onSandboxReadinessReplacement: ({ backend, outcome }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_readiness_replacements_total\",\n help: \"Fresh sandbox command-readiness replacement decisions by backend and outcome.\",\n labels: { backend, outcome },\n });\n },\n onSandboxProviderApiThrottle: ({ backend, operation }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provider_api_throttles_total\",\n help: \"Total sandbox provider API throttle responses.\",\n labels: { backend, operation },\n });\n },\n onSandboxTtlRenewal: ({ backend, outcome }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_ttl_renewals_total\",\n help: \"Total renewable sandbox provider TTL refresh attempts.\",\n labels: { backend, outcome },\n });\n },\n onOpenSandboxSignedEndpoint: ({ outcome, port }) => {\n observability.incrementCounter({\n name: \"opengeni_opensandbox_signed_endpoint_total\",\n help: \"OpenSandbox signed Channel B mint and host-fetch outcomes.\",\n labels: { outcome, port: String(port) },\n });\n },\n onWorkspaceCapture: ({ backend, outcome, durationSeconds }) => {\n if (!Number.isFinite(durationSeconds) || durationSeconds < 0) return;\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n completedOperationSpan(observability, \"worker.workspace_capture\", durationSeconds, {\n backend: safeBackend,\n outcome,\n });\n observability.observeHistogram({\n name: \"opengeni_workspace_capture_duration_seconds\",\n help: \"Physical warm workspace capture and publication duration, including late settlement after caller timeout.\",\n labels: { backend: safeBackend, outcome },\n value: durationSeconds,\n });\n },\n onWorkspaceCaptureSkipped: ({ backend, reason }) => {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n const safeReason = (WORKSPACE_CAPTURE_SKIP_REASONS as readonly string[]).includes(reason)\n ? reason\n : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_workspace_capture_skipped_total\",\n help: \"Due warm workspace checkpoints that could not start, by backend and fixed reason. Not-yet-due (throttled) and already-complete checkpoints are excluded.\",\n labels: { backend: safeBackend, reason: safeReason },\n });\n },\n onWorkspaceArchiveObject: ({ outcome, backend }) => {\n observability.incrementCounter({\n name: \"opengeni_workspace_archive_object_total\",\n help: \"Workspace archive object-storage put/delete outcomes.\",\n labels: { outcome, backend },\n });\n },\n onMcpToolCall: ({ outcome, tool, durationSeconds }) => {\n // Re-check the closed label set at the export boundary so a caller can\n // never turn a raw user-defined tool name into a metric series.\n const safeTool = isMcpToolMetricLabel(tool) ? tool : MCP_TOOL_METRIC_EXTERNAL_LABEL;\n completedOperationSpan(observability, \"worker.mcp.tool_call\", durationSeconds, {\n outcome,\n tool: safeTool,\n });\n observability.incrementCounter({\n name: \"opengeni_mcp_tool_calls_total\",\n help: \"Total physical MCP tool calls by bounded structural outcome and first-party tool (other tools are 'external').\",\n labels: { outcome, tool: safeTool },\n });\n observability.observeHistogram({\n name: \"opengeni_mcp_tool_call_duration_seconds\",\n help: \"MCP tool-call duration in seconds by bounded structural outcome and first-party tool (other tools are 'external').\",\n labels: { outcome, tool: safeTool },\n value: durationSeconds,\n });\n },\n onMcpLifecycle: ({ phase, policy, outcome, durationSeconds }) => {\n observability.incrementCounter({\n name: \"opengeni_mcp_lifecycle_operations_total\",\n help: \"Total physical MCP lifecycle operations by bounded phase, policy, and outcome.\",\n labels: { phase, policy, outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_mcp_lifecycle_operation_duration_seconds\",\n help: \"MCP lifecycle operation duration in seconds by bounded phase, policy, and outcome.\",\n labels: { phase, policy, outcome },\n value: durationSeconds,\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: {\n op: string;\n faultClass: string;\n attempts: number;\n machineId?: string;\n };\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 function initializeWorkerOutcomeMetrics(observability: Observability): void {\n for (const outcome of TURN_OUTCOMES) {\n observability.incrementCounter({\n name: \"opengeni_turns_total\",\n help: \"Total agent turns by terminal outcome.\",\n labels: { outcome },\n amount: 0,\n });\n }\n for (const outcome of WORKER_DEATH_RECOVERY_OUTCOMES) {\n for (const timeoutType of WORKER_DEATH_TIMEOUT_TYPES) {\n observability.incrementCounter({\n name: \"opengeni_turn_worker_death_recoveries_total\",\n help: \"Total durable same-turn worker-death recovery outcomes.\",\n labels: { outcome, timeout_type: timeoutType },\n amount: 0,\n });\n }\n }\n}\n\n/**\n * Publish every bounded compaction lifecycle series before the first event.\n * The zero counters make the closed trigger catalog explicit before a scrape.\n * Alerting uses the separate durable control-worker projection below; these\n * process-local counters are lifecycle-rate diagnostics only.\n */\nexport function initializeContextCompactionMetrics(observability: Observability): void {\n if (initializedContextCompactionMetrics.has(observability)) return;\n for (const trigger of CONTEXT_COMPACTION_TRIGGERS) {\n observability.incrementCounter({\n ...CONTEXT_COMPACTION_STARTS_METRIC,\n labels: { trigger },\n amount: 0,\n });\n observability.incrementCounter({\n ...CONTEXT_COMPACTIONS_METRIC,\n labels: { trigger },\n amount: 0,\n });\n }\n initializedContextCompactionMetrics.add(observability);\n}\n\nexport function recordWorkerDeathRecoveryMetrics(\n observability: Observability,\n input: {\n outcome: WorkerDeathRecoveryOutcome;\n timeoutType: \"HEARTBEAT\" | \"SCHEDULE_TO_START\";\n },\n): void {\n const turnOutcome: TurnOutcome = input.outcome === \"exhausted\" ? \"failed\" : \"recovering\";\n observability.incrementCounter({\n name: \"opengeni_turn_worker_death_recoveries_total\",\n help: \"Total durable same-turn worker-death recovery outcomes.\",\n labels: {\n outcome: input.outcome,\n timeout_type: input.timeoutType.toLowerCase(),\n },\n });\n // The turn process that owned this attempt is gone, so its process-local\n // lifecycle tracker cannot publish the outcome. Record it from the fenced\n // control activity only after the durable recovery transaction wins.\n observability.incrementCounter({\n name: \"opengeni_turns_total\",\n help: \"Total agent turns by terminal outcome.\",\n labels: { outcome: turnOutcome },\n });\n}\n\nexport class TurnLifecycleMetrics {\n private readonly attempts = 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: {\n now?: () => number;\n refreshIntervalMs?: number;\n } = {},\n ) {}\n\n start(input: { attemptId: string }): void {\n const now = this.now();\n this.attempts.set(input.attemptId, { startedAt: now, lastProgressAt: now });\n this.ensureTimer();\n this.refreshGauges();\n }\n\n progress(input: { attemptId: string }): void {\n const attempt = this.attempts.get(input.attemptId);\n if (!attempt) return;\n attempt.lastProgressAt = this.now();\n }\n\n finish(input: {\n attemptId: string;\n outcome: TurnOutcome | null;\n durationSeconds?: number;\n }): void {\n const startedAt = this.attempts.get(input.attemptId)?.startedAt;\n if (startedAt !== undefined) {\n this.attempts.delete(input.attemptId);\n }\n if (input.outcome) {\n const observedDuration =\n input.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: input.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: input.outcome },\n value: observedDuration,\n });\n }\n this.refreshGauges();\n if (this.attempts.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 physical agent-turn attempts in this worker process.\",\n value: this.attempts.size,\n });\n this.observability.setGauge({\n name: \"opengeni_turn_oldest_inflight_age_seconds\",\n help: \"Age in seconds of the oldest in-flight physical agent-turn attempt 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 physical agent-turn attempt.\",\n value: this.oldestNoProgressAgeSeconds(),\n });\n }\n\n stop(): void {\n this.attempts.clear();\n this.refreshGauges();\n this.stopTimer();\n }\n\n private oldestInflightAgeSeconds(): number {\n if (this.attempts.size === 0) {\n return 0;\n }\n let oldest = Number.POSITIVE_INFINITY;\n for (const { startedAt } of this.attempts.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.attempts.size === 0) return 0;\n let leastRecentProgress = Number.POSITIVE_INFINITY;\n for (const { lastProgressAt } of this.attempts.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 modelRequestLifecycleMetricsFor(\n observability: Observability,\n): ModelRequestLifecycleMetrics {\n const existing = modelRequestTrackers.get(observability);\n if (existing) return existing;\n const tracker = new ModelRequestLifecycleMetrics(observability);\n modelRequestTrackers.set(observability, tracker);\n return tracker;\n}\n\nexport class ModelRequestLifecycleMetrics {\n private readonly requests = new Map<\n string,\n { provider: string; startedAt: number; lastEventAt: number }\n >();\n private readonly providers = new Set<string>();\n private timer: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private readonly observability: Observability,\n private readonly options: {\n now?: () => number;\n refreshIntervalMs?: number;\n } = {},\n ) {}\n\n start(key: string, provider: string): void {\n const now = this.now();\n this.providers.add(provider);\n this.requests.set(key, { provider, startedAt: now, lastEventAt: now });\n this.ensureTimer();\n this.refreshGauges();\n }\n\n event(key: string, interEventGapMs?: number): void {\n const request = this.requests.get(key);\n if (!request) return;\n request.lastEventAt = this.now();\n this.observability.incrementCounter({\n name: \"opengeni_model_request_stream_events_total\",\n help: \"Complete, valid provider SSE data events by provider.\",\n labels: { provider: request.provider },\n });\n if (interEventGapMs !== undefined) {\n this.observability.observeHistogram({\n name: \"opengeni_model_request_stream_event_gap_seconds\",\n help: \"Seconds between complete, valid provider SSE data events.\",\n buckets: MODEL_REQUEST_PHASE_BUCKETS,\n labels: { provider: request.provider },\n value: Math.max(0, interEventGapMs / 1000),\n });\n }\n }\n\n finish(key: string): void {\n this.requests.delete(key);\n this.refreshGauges();\n if (this.requests.size === 0) this.stopTimer();\n }\n\n refreshGauges(): void {\n const now = this.now();\n for (const provider of this.providers) {\n const active = [...this.requests.values()].filter((request) => request.provider === provider);\n this.observability.setGauge({\n name: \"opengeni_model_requests_inflight\",\n help: \"Current in-flight provider model requests in this worker process.\",\n labels: { provider },\n value: active.length,\n });\n this.observability.setGauge({\n name: \"opengeni_model_request_oldest_no_event_age_seconds\",\n help: \"Seconds since the least recently progressing in-flight provider request received a valid SSE event.\",\n labels: { provider },\n value:\n active.length === 0\n ? 0\n : Math.max(0, (now - Math.min(...active.map((request) => request.lastEventAt))) / 1000),\n });\n }\n }\n\n stop(): void {\n this.requests.clear();\n this.refreshGauges();\n this.stopTimer();\n }\n\n private ensureTimer(): void {\n if (this.timer) return;\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) return;\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 approximate shared turn-worker activity queue rather than 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 identity: TurnTaskQueueIdentity,\n): void {\n const labels = turnTaskQueueMetricLabels(identity);\n // Invalid observations are failures, not a fabricated fresh zero. Validate\n // every field before publishing any portion of a new observation.\n const values = {\n backlog: requiredTemporalNumber(stats.eligibleBacklog, \"eligibleBacklog\", { integer: true }),\n age: requiredTemporalNumber(stats.oldestBacklogAgeSeconds, \"oldestBacklogAgeSeconds\"),\n add: requiredTemporalNumber(stats.tasksAddRate, \"tasksAddRate\"),\n dispatch: requiredTemporalNumber(stats.tasksDispatchRate, \"tasksDispatchRate\"),\n };\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog\",\n help: \"Approximate Temporal activity backlog on the shared turn-worker queue, including video and retries.\",\n labels,\n value: values.backlog,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog_oldest_age_seconds\",\n help: \"Approximate age of the oldest eligible activity task on the shared turn-worker queue.\",\n labels,\n value: values.age,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_add_rate\",\n help: \"Temporal activities added per second to the shared turn-worker queue over its rolling window.\",\n labels,\n value: values.add,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_dispatch_rate\",\n help: \"Temporal activities dispatched per second from the shared turn-worker queue over its rolling window.\",\n labels,\n value: values.dispatch,\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 if (typeof stats !== \"object\" || Array.isArray(stats)) {\n throw new Error(\"Temporal DescribeTaskQueue returned malformed stats\");\n }\n const eligibleBacklog = requiredTemporalNumber(\n stats.approximateBacklogCount,\n \"approximateBacklogCount\",\n { integer: true },\n );\n let oldestBacklogAgeSeconds = 0;\n if (stats.approximateBacklogAge !== null && stats.approximateBacklogAge !== undefined) {\n if (\n typeof stats.approximateBacklogAge !== \"object\" ||\n Array.isArray(stats.approximateBacklogAge)\n ) {\n throw new Error(\"Temporal stats returned invalid approximateBacklogAge\");\n }\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\n/** Requires a fresh metric registry/schema; pre-registered legacy unlabelled\n * queue gauges are not migrated in place. Each collector owns its native read. */\nexport function startTurnCapacityMonitor(input: {\n observability: Observability;\n identity: TurnTaskQueueIdentity;\n read: (options: TurnTaskQueueReadOptions) => Promise<TurnTaskQueueStats>;\n intervalMs?: number;\n readTimeoutMs?: number;\n closeTimeoutMs?: number;\n now?: () => number;\n monotonicNow?: () => number;\n scheduler?: TelemetryScheduler;\n}): { close: () => Promise<void> } {\n const intervalMs = telemetryInterval(input.intervalMs ?? 15_000, \"queue monitor intervalMs\");\n const readTimeoutMs = telemetryInterval(\n input.readTimeoutMs ?? 5_000,\n \"queue monitor readTimeoutMs\",\n );\n const closeTimeoutMs = telemetryInterval(\n input.closeTimeoutMs ?? 1_000,\n \"queue monitor closeTimeoutMs\",\n );\n const scheduler = input.scheduler ?? telemetryScheduler;\n const labels = turnTaskQueueMetricLabels(input.identity);\n const wallClock = createTelemetryClock(input.now ?? Date.now);\n const monotonicClock = createTelemetryClock(input.monotonicNow ?? (() => performance.now()));\n let startedAt = wallClock.read();\n const readClock = () => {\n const wall = wallClock.read();\n const monotonic = monotonicClock.read();\n return wall === null || monotonic === null ? null : { wall, monotonic };\n };\n let lastSuccessAt: number | null = null;\n let lastReadSucceeded = false;\n let stopped = false;\n let running: {\n controller: AbortController;\n done: Promise<void>;\n cancelTimeout: () => void;\n } | null = null;\n let closePromise: Promise<void> | undefined;\n const safely = (action: () => void) => {\n try {\n action();\n } catch {\n /* Telemetry must not break the worker. */\n }\n };\n const recordStatus = (observation = readClock()) => {\n if (observation === null) lastReadSucceeded = false;\n const observedAt = wallClock.latest();\n startedAt ??= observedAt;\n const successAgeMs =\n observedAt === null ? 0 : observedAt - (lastSuccessAt ?? startedAt ?? observedAt);\n const set = (name: string, help: string, value: number) =>\n safely(() => input.observability.setGauge({ name, help, labels, 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 the latest read succeeded and the shared activity-queue gauges are 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 if (stopped) return;\n const observation = readClock();\n recordStatus(observation);\n if (running || observation === null) return;\n const deadline = observation.wall + readTimeoutMs;\n if (!Number.isFinite(deadline) || deadline > Number.MAX_SAFE_INTEGER) {\n recordStatus(null);\n return;\n }\n const controller = new AbortController();\n const attempt = { controller, done: Promise.resolve(), cancelTimeout: () => {} };\n running = attempt;\n attempt.cancelTimeout = scheduler.timeout(() => {\n if (stopped || running !== attempt) return;\n lastReadSucceeded = false;\n recordStatus();\n controller.abort();\n // Keep the attempt owned until its native call settles. A misbehaving\n // reader ignoring cancellation must not cause overlapping RPCs.\n }, readTimeoutMs);\n let read: Promise<TurnTaskQueueStats>;\n try {\n read = input.read({ signal: controller.signal, deadline });\n } catch (error) {\n read = Promise.reject(error);\n }\n attempt.done = read\n .then((stats) => {\n if (stopped || controller.signal.aborted || running !== attempt) return;\n const completedAt = readClock();\n // A native RPC may settle before its deadline but its Promise callback\n // can run after it, ahead of a delayed JS timeout. Never restamp that old\n // response as fresh. Monotonic age also fences wall-clock adjustments.\n if (\n completedAt === null ||\n completedAt.wall >= deadline ||\n completedAt.monotonic - observation.monotonic >= readTimeoutMs\n ) {\n lastReadSucceeded = false;\n recordStatus(completedAt);\n controller.abort();\n return;\n }\n recordTurnTaskQueueStats(input.observability, stats, input.identity);\n lastSuccessAt = completedAt.wall;\n lastReadSucceeded = true;\n recordStatus(completedAt);\n })\n .catch(() => {\n if (stopped || controller.signal.aborted || running !== attempt) return;\n lastReadSucceeded = false;\n recordStatus();\n safely(() =>\n input.observability.warn(\"turn capacity monitor: Temporal task-queue stats failed\", {\n // Do not export arbitrary server errors or credentials.\n errorClass: \"WorkerTelemetryReadError\",\n origin: \"turn-capacity-monitor\",\n }),\n );\n })\n .finally(() => {\n attempt.cancelTimeout();\n if (running === attempt) running = null;\n });\n };\n refresh();\n const stopTimer = scheduler.interval(refresh, intervalMs);\n return {\n close: () => {\n if (closePromise) return closePromise;\n stopped = true;\n stopTimer();\n lastReadSucceeded = false;\n recordStatus();\n const attempt = running;\n attempt?.cancelTimeout();\n attempt?.controller.abort();\n closePromise = attempt\n ? new Promise<void>((resolve) => {\n const cancel = scheduler.timeout(resolve, closeTimeoutMs);\n void attempt.done.then(() => {\n cancel();\n resolve();\n });\n })\n : Promise.resolve();\n return closePromise;\n },\n };\n}\n\nexport function recordSessionRecoveryBacklogGauges(\n observability: Observability,\n backlog: SessionRecoveryBacklog,\n): void {\n for (const state of [\"quiescence_missing\", \"projection_stale\"] as const) {\n const summary = backlog[state];\n observability.setGauge({\n name: \"opengeni_session_recovery_backlog\",\n help: \"Current durable recovering-session obligations with no active attempt, by bounded reconciliation state. Includes sessions legitimately sleeping in their recorded recovery backoff.\",\n labels: { state },\n value: nonnegativeFinite(summary.count),\n });\n observability.setGauge({\n name: \"opengeni_session_recovery_scheduled\",\n help: \"Durable recovering sessions still inside the recovery backoff (provider Retry-After or connectivity pacing) their recovery request recorded, by bounded reconciliation state.\",\n labels: { state },\n value: nonnegativeFinite(summary.scheduled),\n });\n observability.setGauge({\n name: \"opengeni_session_recovery_oldest_overdue_seconds\",\n help: \"Seconds the single oldest durable recovering session with no active attempt has been past its recovery due time, by bounded reconciliation state; 0 when none is overdue.\",\n labels: { state },\n value: nonnegativeFinite(summary.oldestOverdueSeconds),\n });\n }\n}\n\nexport function startSessionRecoveryMonitor(input: {\n observability: Observability;\n read: () => Promise<SessionRecoveryBacklog>;\n intervalMs?: number;\n now?: () => number;\n}): { close: () => Promise<void> } {\n const intervalMs = input.intervalMs ?? 60_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_session_recovery_monitor_last_read_success\",\n \"Whether the latest durable session-recovery aggregate read completed successfully.\",\n lastReadSucceeded ? 1 : 0,\n );\n set(\n \"opengeni_session_recovery_monitor_last_success_timestamp_seconds\",\n \"Unix timestamp of the latest successful durable session-recovery aggregate read, or zero before one succeeds.\",\n lastSuccessAt === null ? 0 : lastSuccessAt / 1_000,\n );\n set(\n \"opengeni_session_recovery_monitor_fresh\",\n \"Whether durable session-recovery backlog gauges have a successful read within three monitor intervals.\",\n lastReadSucceeded && lastSuccessAt !== null && successAgeMs <= intervalMs * 3 ? 1 : 0,\n );\n };\n const refresh = () => {\n recordStatus();\n if (stopped || running) return;\n running = input\n .read()\n .then((backlog) => {\n recordSessionRecoveryBacklogGauges(input.observability, backlog);\n lastSuccessAt = now();\n lastReadSucceeded = true;\n recordStatus();\n })\n .catch((error) => {\n lastReadSucceeded = false;\n recordStatus();\n input.observability.warn(\"session recovery monitor: durable aggregate read 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 recordContextCompactionPendingGauges(\n observability: Observability,\n summary: ContextCompactionPendingSummary,\n nowMs = Date.now(),\n): void {\n const oldestStartedAtMs = summary.oldestStartedAt?.getTime();\n const oldestPendingAgeSeconds =\n oldestStartedAtMs === undefined || !Number.isFinite(oldestStartedAtMs)\n ? 0\n : Math.max(0, (nowMs - oldestStartedAtMs) / 1_000);\n observability.setGauge({\n name: \"opengeni_context_compaction_pending\",\n help: \"Current exact active attempts whose latest automatic compaction landmark is still started.\",\n value: nonnegativeFinite(summary.pendingCount),\n });\n observability.setGauge({\n name: \"opengeni_context_compaction_oldest_pending_age_seconds\",\n help: \"Age in seconds of the oldest durably pending automatic compaction, or zero when none are pending.\",\n value: oldestPendingAgeSeconds,\n });\n}\n\nexport function startContextCompactionPendingMonitor(input: {\n observability: Observability;\n read: () => Promise<ContextCompactionPendingSummary>;\n intervalMs?: number;\n now?: () => number;\n}): { close: () => Promise<void> } {\n const intervalMs = input.intervalMs ?? 60_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_context_compaction_monitor_last_read_success\",\n \"Whether the latest durable context-compaction aggregate read completed successfully.\",\n lastReadSucceeded ? 1 : 0,\n );\n set(\n \"opengeni_context_compaction_monitor_last_success_timestamp_seconds\",\n \"Unix timestamp of the latest successful durable context-compaction aggregate read, or zero before one succeeds.\",\n lastSuccessAt === null ? 0 : lastSuccessAt / 1_000,\n );\n set(\n \"opengeni_context_compaction_monitor_fresh\",\n \"Whether durable context-compaction pending gauges have a successful read within three monitor intervals.\",\n lastReadSucceeded && lastSuccessAt !== null && successAgeMs <= intervalMs * 3 ? 1 : 0,\n );\n };\n const refresh = () => {\n recordStatus();\n if (stopped || running) return;\n running = input\n .read()\n .then((summary) => {\n recordContextCompactionPendingGauges(input.observability, summary, now());\n lastSuccessAt = now();\n lastReadSucceeded = true;\n recordStatus();\n })\n .catch((error) => {\n lastReadSucceeded = false;\n recordStatus();\n input.observability.warn(\"context compaction monitor: durable aggregate read 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 function recordOpenSandboxKubernetesInventoryGauges(\n observability: Observability,\n inventory: OpenSandboxKubernetesInventory,\n): void {\n for (const phase of OPENSANDBOX_BATCHSANDBOX_PHASES) {\n observability.setGauge({\n name: \"opengeni_opensandbox_batchsandboxes\",\n help: \"Current OpenSandbox BatchSandboxes by bounded lifecycle phase.\",\n labels: { phase },\n value: inventory.batchSandboxPhases[phase],\n });\n }\n for (const condition of OPENSANDBOX_WORKLOAD_POD_CONDITIONS) {\n observability.setGauge({\n name: \"opengeni_opensandbox_workload_pods\",\n help: \"Current OpenSandbox workload Pods by bounded operational condition.\",\n labels: { condition },\n value: inventory.workloadPodConditions[condition],\n });\n }\n observability.setGauge({\n name: \"opengeni_opensandbox_cleanup_stuck\",\n help: \"Current OpenSandbox BatchSandboxes deleting past the cleanup threshold with finalizers.\",\n value: inventory.cleanupStuck,\n });\n observability.setGauge({\n name: \"opengeni_opensandbox_expiration_overdue\",\n help: \"Current OpenSandbox BatchSandboxes still present past the provider-expiry threshold.\",\n value: inventory.expirationOverdue,\n });\n}\n\nexport const SANDBOX_INVENTORY_PROJECTION_DOMAINS = [\n \"leases\",\n \"checkpoint_artifacts\",\n \"recovery_observations\",\n \"rotation_backlog\",\n \"retained_processes\",\n \"expired_drains\",\n \"opensandbox_kubernetes\",\n \"modal_provider\",\n \"interaction_idle\",\n \"checkpoint_staleness\",\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\n/**\n * Distinct managed people with authenticated browser activity in each fixed\n * window (migration 0565 presence). Every control worker publishes the same\n * global value, so dashboards must aggregate with `max()` across pods, never\n * `sum()`. API keys, services and embedded hosts are never counted.\n */\nexport function recordActiveUserGauges(\n observability: Observability,\n counts: Record<ActiveUserWindow, number>,\n): void {\n for (const window of ACTIVE_USER_WINDOWS) {\n observability.setGauge({\n name: \"opengeni_active_users\",\n help: \"Distinct managed users with authenticated activity within the window (global; use max across pods).\",\n labels: { window },\n value: counts[window],\n });\n }\n}\n\n/**\n * Positive credit grants observed by the ledger trigger since migration 0565,\n * by closed class. These are cumulative database totals published as gauges by\n * every control worker: aggregate with `max()` across pods, then subtract the\n * same expression `offset` by the window. A per-pod `increase()` sees only one\n * pod's lifetime and undercounts across control-worker restarts.\n * Counting in the database covers the grants no application process writes:\n * the verified-signup trial grant (a setup trigger) and operator grants.\n */\nexport function recordCreditGrantGauges(\n observability: Observability,\n totals: CreditGrantTotals,\n): void {\n for (const grantClass of CREDIT_GRANT_CLASSES) {\n const total = totals[grantClass];\n observability.setGauge({\n name: \"opengeni_credit_grants_total\",\n help: \"Positive credit grants observed in the ledger since migration 0565, by grant class (global cumulative; use max across pods).\",\n labels: { grant_class: grantClass },\n value: total.count,\n });\n observability.setGauge({\n name: \"opengeni_credit_granted_micros_total\",\n help: \"Credit micros granted in the ledger since migration 0565, by grant class (global cumulative; use max across pods).\",\n labels: { grant_class: grantClass },\n value: total.micros,\n });\n }\n}\n\n/**\n * The deployment-level runtime switch for the one-time verified signup trial\n * credit (migration 0521): 1 while it allows grants, 0 when an operator has\n * disabled it or no revision exists. A grant also needs the API's\n * OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in, which\n * {@link recordVerifiedSignupTrialDeploymentFlagGauge} reports separately.\n */\nexport function recordVerifiedSignupTrialSwitchGauge(\n observability: Observability,\n grantsEnabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_verified_signup_trial_credits_runtime_enabled\",\n help: \"Whether the runtime switch allows new verified signup trial credit grants (1) or blocks them (0).\",\n value: grantsEnabled ? 1 : 0,\n });\n}\n\n/**\n * The deployment-level runtime switch for new managed account sign-ups\n * (migration 0585): 1 while it allows new accounts (also when no revision\n * exists), 0 when an operator has paused them. The API additionally honours\n * its OPENGENI_MANAGED_AUTH_NEW_SIGNUPS_ENABLED ceiling, which this gauge does\n * not see; `GET /v1/config/client` reports the combined decision.\n */\nexport function recordManagedAuthNewSignupsSwitchGauge(\n observability: Observability,\n signupsEnabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_managed_auth_new_signups_runtime_enabled\",\n help: \"Whether the runtime switch allows new managed account sign-ups (1) or has paused them (0).\",\n value: signupsEnabled ? 1 : 0,\n });\n}\n\n/**\n * The OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in as this\n * worker's configuration sees it. The API reads the same shared setting; new\n * grants happen only while this gauge and the runtime switch gauge are both 1.\n */\nexport function recordVerifiedSignupTrialDeploymentFlagGauge(\n observability: Observability,\n enabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_verified_signup_trial_credits_deployment_enabled\",\n help: \"Whether the OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in is on (1) or off (0) in this deployment's configuration.\",\n value: enabled ? 1 : 0,\n });\n}\n\n/**\n * Provider-side Modal inventory reconciled against live leases by the orphan\n * sweep: `running` = every running box in the app, `unleased` = running boxes\n * no live lease protects (orphans, counted before termination), and\n * `lease_missing_instance` = warm leases whose exact provider instance is no\n * longer running (zombies).\n */\nexport function recordModalSandboxInventoryGauges(\n observability: Observability,\n inventory: { running: number; unleased: number; liveLeaseInstancesMissing: number },\n): void {\n const states = {\n running: inventory.running,\n unleased: inventory.unleased,\n lease_missing_instance: inventory.liveLeaseInstancesMissing,\n };\n for (const [state, value] of Object.entries(states)) {\n observability.setGauge({\n name: \"opengeni_modal_sandbox_inventory\",\n help: \"Running Modal sandboxes reconciled against live sandbox leases by the orphan sweep.\",\n labels: { state },\n value: Math.max(0, value),\n });\n }\n}\n\n/** Warm leases held only by Browser/Computer interaction holders, by time since\n * the newest interaction activity. Such boxes never drain on their own until the\n * interaction session ends or the provider deadline. */\nexport function recordInteractionOnlyLeaseGauges(\n observability: Observability,\n counts: Record<string, number>,\n): void {\n for (const [idleBucket, value] of Object.entries(counts)) {\n observability.setGauge({\n name: \"opengeni_sandbox_leases_interaction_only\",\n help: \"Warm sandbox leases held only by Browser/Computer sessions, by time since last interaction activity.\",\n labels: { idle_bucket: idleBucket },\n value: Math.max(0, value),\n });\n }\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\n/** Fixed outcomes of legacy retained-command containment: per-candidate\n * enrollment inspection, then the enrolled drain's exact cold commit. */\nexport const SANDBOX_COMMAND_CONTAINMENT_OUTCOMES = [\n \"idle_enrolled\",\n \"deadline_enrolled\",\n \"forced_deadline_enrolled\",\n \"quiescence_enrolled\",\n \"resumed_enrolled\",\n \"not_eligible\",\n \"inspection_failed\",\n \"contained\",\n \"provider_missing\",\n] as const;\n\nexport type SandboxCommandContainmentOutcome =\n (typeof SANDBOX_COMMAND_CONTAINMENT_OUTCOMES)[number];\n\n/**\n * Shared subscription core shadow (docs/subscription-accounts.md, SUB-COMPAT-03).\n * Every label is a closed set: provider, security parity, placement agreement,\n * contract requirement, skip reason and the legacy decision inputs present.\n */\nexport const SUBSCRIPTION_CORE_SHADOW_PROVIDERS = [\"codex\", \"xai\", \"claude\"] as const;\n/** Is the account the legacy selector chose inside the core's eligible set? */\nexport const SUBSCRIPTION_CORE_SHADOW_PARITY = [\n \"eligible\",\n \"not_authorized\",\n \"not_servable\",\n \"unknown_connection\",\n \"no_selection\",\n] as const;\n/** How the core's decision compares with the legacy decision (would-switch). */\nexport const SUBSCRIPTION_CORE_SHADOW_PLACEMENTS = [\n \"same_account\",\n \"different_account\",\n \"core_waits\",\n \"core_runs_legacy_waits\",\n \"both_wait\",\n] as const;\n/** Requirements the reference checker can report, plus checker failure. */\nexport const SUBSCRIPTION_CORE_SHADOW_REQUIREMENTS = [\n \"SUB-ACCESS-06\",\n \"SUB-CREDITS-01\",\n \"SUB-ELIG-01\",\n \"SUB-ELIG-02\",\n \"SUB-ELIG-03\",\n \"SUB-ELIG-04\",\n \"SUB-ELIG-05\",\n \"SUB-FAIL-02\",\n \"SUB-FAIL-03\",\n \"SUB-FAIL-04\",\n \"SUB-OWN-05\",\n \"SUB-SEL-02\",\n \"SUB-SEL-04\",\n \"SUB-SET-06\",\n \"SUB-STICK-02\",\n \"SUB-WAIT-01\",\n \"SUB-WAIT-02\",\n \"other\",\n \"checker_error\",\n] as const;\nexport const SUBSCRIPTION_CORE_SHADOW_SKIPS = [\n \"busy\",\n \"no_session_actor\",\n \"session_not_visible\",\n \"timeout\",\n \"cancelled\",\n \"error\",\n] as const;\n/**\n * Legacy decision inputs the Codex fleet shadow does not record (inventory\n * Part 3, answer (c)), counted when present in a compared decision.\n */\nexport const SUBSCRIPTION_CORE_SHADOW_INPUTS = [\n \"manual_pin\",\n \"policy_pin\",\n \"last_account\",\n \"rotation_off\",\n \"rotation_on\",\n \"organization_pool\",\n \"personal_pool\",\n \"codex_mode_override\",\n \"model_policy\",\n \"model_filtered\",\n \"plan_excluded\",\n \"model_cooldown\",\n \"exhausted\",\n \"unknown_quota\",\n \"compaction_lock\",\n \"lease_reused\",\n \"truncated\",\n] as const;\n\nexport type SubscriptionCoreShadowProvider = (typeof SUBSCRIPTION_CORE_SHADOW_PROVIDERS)[number];\nexport type SubscriptionCoreShadowParity = (typeof SUBSCRIPTION_CORE_SHADOW_PARITY)[number];\nexport type SubscriptionCoreShadowPlacement = (typeof SUBSCRIPTION_CORE_SHADOW_PLACEMENTS)[number];\nexport type SubscriptionCoreShadowSkip = (typeof SUBSCRIPTION_CORE_SHADOW_SKIPS)[number];\nexport type SubscriptionCoreShadowInput = (typeof SUBSCRIPTION_CORE_SHADOW_INPUTS)[number];\n\nexport type SubscriptionCoreShadowObservation =\n | { outcome: \"skipped\"; reason: SubscriptionCoreShadowSkip }\n | {\n outcome: \"compared\";\n parity: SubscriptionCoreShadowParity;\n /** The first reason the legacy account is outside the eligible set. */\n parityReason: string | null;\n placement: SubscriptionCoreShadowPlacement;\n violations: readonly string[];\n inputs: readonly SubscriptionCoreShadowInput[];\n };\n\nconst SUBSCRIPTION_CORE_SHADOW_REQUIREMENT_SET = new Set<string>(\n SUBSCRIPTION_CORE_SHADOW_REQUIREMENTS,\n);\n/** First reasons a legacy account is outside the eligible set (policy ineligibility). */\nexport const SUBSCRIPTION_CORE_SHADOW_PARITY_REASONS = [\n \"provider_disabled\",\n \"organization_accounts_off\",\n \"out_of_scope\",\n \"personal_connections_disabled\",\n \"personal_owner_inactive\",\n \"personal_not_owners_work\",\n \"personal_authority_missing\",\n \"unhealthy\",\n \"allocator_disabled\",\n \"wrong_provider\",\n \"model_unknown\",\n \"model_not_allowed_by_workspace\",\n \"model_not_entitled\",\n \"model_not_allowed_by_connection\",\n \"model_cooling_down\",\n \"exhausted\",\n] as const;\nconst SUBSCRIPTION_CORE_SHADOW_PARITY_REASON_SET = new Set<string>(\n SUBSCRIPTION_CORE_SHADOW_PARITY_REASONS,\n);\n\nconst SUBSCRIPTION_CORE_SHADOW_DURATION_BUCKETS = [\n 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1,\n];\n\nexport function recordSubscriptionCoreShadow(\n observability: Pick<Observability, \"incrementCounter\" | \"observeHistogram\">,\n provider: SubscriptionCoreShadowProvider,\n observation: SubscriptionCoreShadowObservation,\n durationSeconds: number,\n): void {\n observability.observeHistogram({\n name: \"opengeni_subscription_core_shadow_duration_seconds\",\n help: \"Shared subscription core shadow comparison duration by provider, including skips.\",\n labels: { provider },\n value: durationSeconds,\n buckets: SUBSCRIPTION_CORE_SHADOW_DURATION_BUCKETS,\n });\n if (observation.outcome === \"skipped\") {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_skips_total\",\n help: \"Shadow comparisons not made, by fixed reason; placement is unaffected.\",\n labels: { provider, reason: observation.reason },\n });\n return;\n }\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_comparisons_total\",\n help: \"Shadow comparisons by security parity of the legacy account and core placement agreement.\",\n labels: { provider, parity: observation.parity, placement: observation.placement },\n });\n if (observation.parityReason !== null) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_parity_failures_total\",\n help: \"Legacy-selected accounts outside the core's eligible set, by first fixed reason.\",\n labels: {\n provider,\n reason: SUBSCRIPTION_CORE_SHADOW_PARITY_REASON_SET.has(observation.parityReason)\n ? observation.parityReason\n : \"other\",\n },\n });\n }\n for (const requirement of new Set(observation.violations)) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_violations_total\",\n help: \"Contract invariant violations of the core's own shadow decision, by requirement.\",\n labels: {\n provider,\n requirement: SUBSCRIPTION_CORE_SHADOW_REQUIREMENT_SET.has(requirement)\n ? requirement\n : \"other\",\n },\n });\n }\n for (const input of observation.inputs) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_inputs_total\",\n help: \"Legacy decision inputs present in compared shadow decisions, by fixed input.\",\n labels: { provider, input },\n });\n }\n}\n\n/** A shadow load that outlived its slot ceiling; its slot was given back. */\nexport function recordSubscriptionCoreShadowStuckLoad(\n observability: Pick<Observability, \"incrementCounter\">,\n provider: SubscriptionCoreShadowProvider,\n): void {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_stuck_loads_total\",\n help: \"Shadow world loads still unsettled at the slot ceiling; the slot was released.\",\n labels: { provider },\n });\n}\n\n/** Idle browser/desktop release inspections by fixed outcome: `checkpointing`\n * (idle browser saves prepared), `released` (holders released without a save). */\nexport function recordSandboxIdleInteractionRelease(\n observability: Observability,\n outcome: IdleInteractionInspection,\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_idle_interaction_release_total\",\n help: \"Idle browser and desktop release inspections on warm boxes, by fixed outcome.\",\n labels: { outcome },\n });\n}\n\nexport function recordSandboxCommandContainment(\n observability: Observability,\n outcome: SandboxCommandContainmentOutcome,\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_command_containment_total\",\n help: \"Legacy retained-command containment inspections and enrolled drain commits by fixed outcome.\",\n labels: { outcome },\n });\n}\n\n/** Only call after the exact draining->cold commit reports wentCold. The\n * backend is validated against the closed contract so provider IDs and other\n * per-sandbox values can never become metric labels. */\nexport function recordSandboxProviderMissingBeforeCapture(\n observability: Observability,\n backend: string,\n): void {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_sandbox_provider_missing_before_capture_total\",\n help: \"Exact sandbox cold commits after definitive provider disappearance before workspace capture.\",\n labels: { backend: safeBackend },\n });\n}\n\n/** Host workspace directory outcome after a Docker drain's exact-container\n * teardown (`retained` means the directory stays on disk). */\nexport function recordSandboxDockerWorkspaceRelease(\n observability: Observability,\n status: \"released\" | \"already_released\" | \"retained\",\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_docker_workspace_release_total\",\n help: \"Docker drain host workspace directories released or retained after exact-container teardown.\",\n labels: { status },\n });\n}\n\nexport function recordSandboxRecoveryObservationGauges(\n observability: Observability,\n observations: {\n providerLosses: number;\n fallbackSelections: number;\n freshWorkspaceSelections?: number;\n },\n): void {\n for (const [kind, value] of [\n [\"provider_missing_before_capture\", observations.providerLosses],\n [\"checkpoint_fallback_selected\", observations.fallbackSelections],\n [\"fresh_workspace_selected\", observations.freshWorkspaceSelections ?? 0],\n ] as const) {\n observability.setGauge({\n name: \"opengeni_sandbox_recovery_observations_recent\",\n help: \"Committed sandbox recovery observations in the last 30 minutes by fixed kind.\",\n labels: { kind },\n value,\n });\n }\n}\n\n/** Fixed outcomes for a committed automatic continuity decision after\n * definitive managed-provider loss: a singleton checkpoint (`selected`), a\n * shared-group checkpoint (`selected_shared`), or a new empty workspace\n * (`fresh_workspace`). Call only after the durable authorization committed. */\nexport const SANDBOX_AUTOMATIC_RECOVERY_OUTCOMES = [\n \"selected\",\n \"selected_shared\",\n \"fresh_workspace\",\n] as const;\nexport type SandboxAutomaticRecoveryOutcome = (typeof SANDBOX_AUTOMATIC_RECOVERY_OUTCOMES)[number];\n\nexport function sandboxAutomaticRecoveryOutcome(input: {\n lane: \"checkpoint\" | \"fresh_workspace\";\n groupSessionCount: number;\n}): SandboxAutomaticRecoveryOutcome {\n if (input.lane === \"fresh_workspace\") return \"fresh_workspace\";\n return input.groupSessionCount > 1 ? \"selected_shared\" : \"selected\";\n}\n\nexport function recordSandboxAutomaticRecoverySelected(\n observability: Observability,\n backend: string,\n outcome: SandboxAutomaticRecoveryOutcome,\n): void {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_sandbox_checkpoint_fallback_total\",\n help: \"System-selected continuity after managed provider loss: verified checkpoint or empty workspace.\",\n labels: { backend: safeBackend, outcome },\n });\n}\n\n/** How long live boxes have held changes no checkpoint covers yet. */\nexport function recordSandboxCheckpointStalenessGauges(\n observability: Observability,\n staleness: { dirty: number; stale4h: number; stale12h: number; maxAgeSeconds: number },\n): void {\n const values = {\n dirty: staleness.dirty,\n stale_4h: staleness.stale4h,\n stale_12h: staleness.stale12h,\n } as const;\n for (const [kind, value] of Object.entries(values)) {\n observability.setGauge({\n name: \"opengeni_sandbox_checkpoint_staleness\",\n help: \"Live Modal sandboxes holding a write their last checkpoint did not capture, by age of the first such write.\",\n labels: { kind },\n value,\n });\n }\n observability.setGauge({\n name: \"opengeni_sandbox_checkpoint_age_max_seconds\",\n help: \"Age in seconds of the oldest write a live Modal sandbox's last checkpoint did not capture.\",\n value: Number.isFinite(staleness.maxAgeSeconds) ? Math.max(0, staleness.maxAgeSeconds) : 0,\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 interactionBlocked: 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 interaction_blocked: backlog.interactionBlocked,\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 // Session-owned background commands (migration 0637). Running is live\n // session work after its launch turn ended; stopping already has a stop\n // request, so it still counts as terminal-owner backlog.\n \"background_running\",\n \"background_stopping\",\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<{\n ownerState: string;\n activeCount: number;\n terminalOwnerCount: number;\n }>,\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 owner is gone: a closed launch attempt or released direct request, or a background command already asked to stop. A running session background command is never counted.\",\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 \"background_recovered\",\n \"proof_exited\",\n \"proof_lost\",\n \"proof_checkpoint_failed\",\n \"settled_exited\",\n \"settled_lost\",\n \"provider_lost_whole_box\",\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 \"process_observation_unavailable\",\n \"quarantined_process_observation_unavailable\",\n \"quarantined_binding_missing\",\n \"quarantined_binding_mismatch\",\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\n// ── Per-model usage and money ───────────────────────────────────────────────\n// `model` is the deployment catalog product id (resolved by the caller), or\n// `custom` for a workspace-owned model. The recorder bounds it again: a value\n// outside a conservative id shape becomes `custom`, and after\n// MAX_MODEL_METRIC_LABELS distinct values per process every new value becomes\n// `other`, so a catalog change or a bug can never grow the series count without\n// bound. Account, workspace, user and session ids are never labels here.\n\n/** Who pays the upstream provider for the call. */\nexport type ModelUsagePayer = \"deployment\" | \"external\";\n\nconst MODEL_METRIC_LABEL_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/@+-]{0,127}$/;\nconst MAX_MODEL_METRIC_LABELS = 64;\nconst modelMetricLabels = new WeakMap<Observability, Set<string>>();\n\nexport function boundedModelMetricLabel(observability: Observability, model: string): string {\n if (model === \"custom\" || model === \"other\") return model;\n if (!MODEL_METRIC_LABEL_PATTERN.test(model)) return \"custom\";\n let seen = modelMetricLabels.get(observability);\n if (!seen) {\n seen = new Set<string>();\n modelMetricLabels.set(observability, seen);\n }\n if (seen.has(model)) return model;\n if (seen.size >= MAX_MODEL_METRIC_LABELS) return \"other\";\n seen.add(model);\n return model;\n}\n\nfunction nonnegativeSafeInteger(value: unknown): number {\n return typeof value === \"number\" && Number.isSafeInteger(value) && value > 0 ? value : 0;\n}\n\n/**\n * One authoritative model response, by provider and catalog model. Recorded\n * behind the same durable usage-event fence as the provider-only cache metrics,\n * so a duplicate or late response is not counted twice.\n * - `opengeni_model_responses_total{provider,model,priced}` — responses with\n * reported usage; `priced=\"false\"` means no provider cost estimate exists.\n * - `opengeni_model_tokens_total{provider,model,type}` — `input` (all prompt\n * tokens, including cached and cache-write), `cached_input` and\n * `cache_write` (subsets of `input`), `output`, and `reasoning` (a subset\n * of `output`).\n * - `opengeni_model_provider_cost_micros_total{provider,model,payer,pricing_source}`\n * — estimated upstream cost in USD micros: configured list price, or the\n * gateway-reported cost. `payer=\"external\"` is a customer-paid subscription\n * or key, so that cost is a list-price equivalent Opengeni does not pay.\n */\nexport function recordModelResponseUsage(\n observability: Observability,\n input: {\n provider: string;\n model: string;\n payer: ModelUsagePayer;\n tokens: {\n inputTokens: number | null;\n cachedTokens: number | null;\n cacheWriteTokens: number | null;\n outputTokens: number | null;\n reasoningTokens: number | null;\n };\n estimatedProviderCostMicros: number | null;\n pricingSource: \"configured_list_price\" | \"gateway_reported\" | null;\n webSearchRequests?: number;\n },\n): void {\n const model = boundedModelMetricLabel(observability, input.model);\n const base = { provider: input.provider, model };\n const priced = input.pricingSource !== null && input.estimatedProviderCostMicros !== null;\n observability.incrementCounter({\n name: \"opengeni_model_responses_total\",\n help: \"Authoritative model responses with reported usage, by provider, catalog model and whether a cost estimate exists.\",\n labels: { ...base, priced: priced ? \"true\" : \"false\" },\n });\n const tokenTypes = [\n [\"input\", input.tokens.inputTokens],\n [\"cached_input\", input.tokens.cachedTokens],\n [\"cache_write\", input.tokens.cacheWriteTokens],\n [\"output\", input.tokens.outputTokens],\n [\"reasoning\", input.tokens.reasoningTokens],\n ] as const;\n for (const [type, value] of tokenTypes) {\n const amount = modelUsageTokenCountOrNull(value);\n if (amount === null || amount === 0) continue;\n incrementBoundedModelCacheCounter(observability, {\n name: \"opengeni_model_tokens_total\",\n help: \"Tokens of authoritative model responses by provider, catalog model and token type (cached_input and cache_write are subsets of input; reasoning is a subset of output).\",\n provider: input.provider,\n labels: { ...base, type },\n amount,\n });\n }\n const searches = nonnegativeSafeInteger(input.webSearchRequests ?? 0);\n if (searches > 0)\n observability.incrementCounter({\n name: \"opengeni_model_web_search_requests_total\",\n help: \"Provider-run web searches billed with authoritative model responses, by provider, catalog model and payer.\",\n labels: { ...base, payer: input.payer },\n amount: searches,\n });\n const cost = nonnegativeSafeInteger(input.estimatedProviderCostMicros);\n if (priced && input.pricingSource !== null && cost > 0) {\n observability.incrementCounter({\n name: \"opengeni_model_provider_cost_micros_total\",\n help: \"Estimated upstream provider cost in USD micros by provider, catalog model, payer and pricing source.\",\n labels: { ...base, payer: input.payer, pricing_source: input.pricingSource },\n amount: cost,\n });\n }\n}\n\n/**\n * Credits actually debited for one model response, recorded once when the\n * ledger row is inserted (an idempotent replay debits 0 and records nothing).\n * `funding=\"promotional\"` is the part paid by scoped promotional grants\n * (verified-signup trial, scoped coupon offers); `funding=\"general\"` is the\n * rest, paid from general credit: purchased credits plus any unscoped grant.\n */\nexport function recordModelCreditsCharged(\n observability: Observability | undefined,\n input: { provider: string; model: string; debitedMicros: number; grantDebitedMicros: number },\n): void {\n if (!observability) return;\n const debited = nonnegativeSafeInteger(input.debitedMicros);\n if (debited === 0) return;\n const promotional = Math.min(debited, nonnegativeSafeInteger(input.grantDebitedMicros));\n const model = boundedModelMetricLabel(observability, input.model);\n for (const [funding, amount] of [\n [\"promotional\", promotional],\n [\"general\", debited - promotional],\n ] as const) {\n if (amount === 0) continue;\n observability.incrementCounter({\n name: \"opengeni_model_credits_charged_micros_total\",\n help: \"Credit micros debited for model usage by provider, catalog model and funding (promotional grant or general credit).\",\n labels: { provider: input.provider, model, funding },\n amount,\n });\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\nexport type TurnStartupPhase =\n | \"services_initialization\"\n | \"claim_catalog_read\"\n | \"claim_atomic\"\n | \"claim_session_read\"\n | \"claim_capability_settings\"\n | \"learning_policy_freeze\"\n | \"attachment_authority_projection\"\n | \"claim_and_policy\"\n | \"turn_start_settlement\"\n | \"credential_selection\"\n | \"runtime_preparation\"\n | \"session_mcp_settings\"\n | \"sandbox_establish\"\n | \"file_resolution\"\n | \"tool_context_preparation\"\n | \"tool_preparation\"\n | \"tool_server_construction\"\n | \"tool_required_connect\"\n | \"tool_optional_connect\"\n | \"tool_attempt_catalog_build\"\n | \"tool_attempt_catalog_persist\"\n | \"tool_workspace_gateway_catalog_build\"\n | \"post_tool_preparation\"\n | \"agent_construction\"\n | \"post_agent_preparation\"\n | \"file_materialization\"\n | \"history_preparation\"\n | \"history_system_update_load\"\n | \"history_current_attachment_resolution\"\n | \"history_durable_history_load\"\n | \"history_sandbox_envelope_load\"\n | \"history_canonical_projection\"\n | \"history_provider_projection\"\n | \"history_attachment_ref_projection\"\n | \"history_screenshot_materialization\"\n | \"history_model_attachment_projection\"\n | \"history_runtime_input_assembly\"\n | \"history_artifact_candidate_scan\"\n | \"history_generated_image_materialization\"\n | \"history_position_load\"\n | \"owned_sandbox_setup\"\n | \"runtime_stream_initialization\"\n | \"model_request_preparation\"\n | \"model_sdk_serialization\"\n | \"model_prepare_sandbox_agent_preparation\"\n | \"model_prepare_sandbox_agent_manifest_inventory\"\n | \"model_prepare_sandbox_session_manifest_inventory\"\n | \"model_prepare_sandbox_manifest_apply\"\n | \"model_prepare_sandbox_entry_materialization\"\n | \"model_prepare_sandbox_running_check\"\n | \"model_prepare_sandbox_start\"\n | \"model_prepare_sandbox_client_create\"\n | \"model_prepare_sandbox_client_resume\"\n | \"model_prepare_sandbox_client_delete\"\n | \"model_prepare_sandbox_client_state_serialize\"\n | \"model_prepare_sandbox_client_reuse_check\"\n | \"model_prepare_sandbox_workspace_mutation_admission\"\n | \"model_prepare_sandbox_workspace_mutation_provider\"\n | \"model_prepare_sandbox_workspace_mutation_settlement\"\n | \"model_prepare_sandbox_first_routed_resolution_other\"\n | \"model_prepare_sandbox_first_routed_mutation_admission\"\n | \"model_prepare_sandbox_first_routed_provider_operation\"\n | \"model_prepare_sandbox_first_routed_mutation_settlement\"\n | \"model_prepare_sandbox_first_routed_other\"\n | \"model_prepare_sandbox_snapshot_wait\"\n | \"model_prepare_runner_before_first_sandbox_operation\"\n | \"model_prepare_sdk_after_first_sandbox_operation\"\n | \"model_prepare_runner_before_mcp_tools\"\n | \"model_prepare_mcp_tools_snapshot\"\n | \"model_prepare_mcp_tools_before_input_filter\"\n | \"model_prepare_mcp_tools_before_repository_skill_discovery\"\n | \"model_prepare_repository_skill_discovery\"\n | \"model_prepare_repository_skill_discovery_before_input_filter\"\n | \"model_prepare_input_filter_base\"\n | \"model_prepare_input_filter_genesis\"\n | \"model_prepare_input_filter_host\"\n | \"model_prepare_input_filter_tool_output\"\n | \"model_prepare_input_filter_modality\"\n | \"model_prepare_input_filter_context\"\n | \"model_prepare_responses_input_conversion\"\n | \"model_prepare_responses_request_build\"\n | \"model_credential_resolution\"\n | \"model_wire_normalization\"\n | \"model_request_audit\"\n | \"stream_bootstrap\";\n\nexport type TurnStartupOutcome = \"completed\" | \"failed\";\nexport type TurnStartupMilestone = \"queue\" | \"provider_dispatch\" | \"first_byte\";\nexport type TurnStartupCache = \"hit\" | \"miss\" | \"disabled\" | \"none\";\nexport type TurnStartupCountBucket = \"0\" | \"1\" | \"2-5\" | \"6-20\" | \"21+\" | \"unknown\";\nexport type TurnSandboxEstablishPolicy = \"eager\" | \"on-demand\";\nexport type TurnSandboxEstablishReason =\n | \"eligible\"\n | \"lazy_disabled\"\n | \"machine_primary\"\n | \"backend_none\"\n | \"initial_run_credentials\"\n | \"initial_run_credentials_deferred\"\n | \"generated_video_files\"\n | \"signed_file_resources\";\nexport type SandboxLogicalProvisionCategory =\n | \"create\"\n | \"resume\"\n | \"exec_readiness\"\n | \"sibling_warming\"\n | \"lease_superseded\"\n | \"drain_capture_wait\"\n | \"archive_recovery\"\n | \"provider_transport\"\n | \"configuration\"\n | \"unknown\";\nexport type SandboxLogicalProvisionStage =\n | \"create\"\n | \"resume\"\n | \"exec_readiness\"\n | \"sibling_warming\"\n | \"lease_admission\"\n | \"lifecycle_wait\"\n | \"archive_recovery\"\n | \"configuration\"\n | \"provider_transport\"\n | \"unknown\";\nexport type SandboxLogicalProvisionOutcome = \"completed\" | \"expected_transition\" | \"failed\";\nexport type SandboxProvisionAttemptOutcome = \"completed\" | \"retrying\" | \"failed\";\n\nconst TURN_STARTUP_PHASE_BUCKETS = [\n 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120,\n];\nconst TURN_STARTUP_MILESTONE_BUCKETS = [\n ...TURN_STARTUP_PHASE_BUCKETS,\n 300,\n 600,\n 1_800,\n 3_600,\n 7_200,\n];\n\nexport function turnStartupCountBucket(count: number | null): TurnStartupCountBucket {\n if (count === null || !Number.isFinite(count) || count < 0) return \"unknown\";\n if (count === 0) return \"0\";\n if (count === 1) return \"1\";\n if (count <= 5) return \"2-5\";\n if (count <= 20) return \"6-20\";\n return \"21+\";\n}\n\n/**\n * Measure startup operations, including nested and parallel work. These are\n * not additive critical-path intervals; use milestones for elapsed latency.\n * Every label is a closed or configuration-derived enum; high-cardinality turn,\n * session, credential, connection, file, and model identifiers are forbidden.\n */\nexport function recordTurnStartupPhase(\n observability: Observability,\n input: {\n phase: TurnStartupPhase;\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n count?: number | null;\n cache?: TurnStartupCache;\n /** Trace-only lookup for the completed claim while its execution root is still open. */\n executionCorrelationId?: string;\n },\n): void {\n completedOperationSpan(observability, `worker.prepare.${input.phase}`, input.durationSeconds, {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n ...(input.phase === \"claim_and_policy\" &&\n /^turn_[0-9a-f]{32}$/.test(input.executionCorrelationId ?? \"\")\n ? { correlationId: input.executionCorrelationId }\n : {}),\n });\n observability.observeHistogram({\n name: \"opengeni_turn_startup_phase_duration_seconds\",\n help: \"Turn startup phase duration before the model response stream begins.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n phase: input.phase,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n count_bucket: turnStartupCountBucket(input.count ?? null),\n cache: input.cache ?? \"none\",\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/** Observe an existing dependency only. Child intervals can overlap and are\n * not additive; observer failure must never change authority or error identity. */\nexport async function measureTurnStartupPhase<T>(\n observability: Observability,\n input: Omit<Parameters<typeof recordTurnStartupPhase>[1], \"durationSeconds\" | \"outcome\">,\n work: () => Promise<T>,\n): Promise<T> {\n const started = performance.now();\n let outcome: TurnStartupOutcome = \"failed\";\n try {\n const result = await work();\n outcome = \"completed\";\n return result;\n } finally {\n try {\n recordTurnStartupPhase(observability, {\n ...input,\n outcome,\n durationSeconds: (performance.now() - started) / 1_000,\n });\n } catch {\n // Telemetry is not an execution gate.\n }\n }\n}\n\n/** Background MCP preparation must not inflate startup phase distributions. */\nexport function recordToolPreparationPhase(\n observability: Observability,\n input: ToolPreparationPhaseMeasurement & { provider: string; backend: string },\n): void {\n if (input.execution === \"blocking\") {\n recordTurnStartupPhase(observability, {\n ...input,\n phase: `tool_${input.phase}`,\n });\n return;\n }\n completedOperationSpan(\n observability,\n `worker.tool_prepare.${input.phase}`,\n input.durationSeconds,\n {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n );\n observability.observeHistogram({\n name: \"opengeni_tool_background_preparation_duration_seconds\",\n help: \"Nonblocking MCP preparation operations; may overlap startup and later execution.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n phase: input.phase,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/** Completed measurements become siblings under the scoped physical attempt.\n * They never establish ambient ancestry for work which has already finished. */\nfunction completedOperationSpan(\n observability: Observability,\n name: string,\n durationSeconds: number,\n attributes: {\n outcome: string;\n provider?: string;\n backend?: string;\n tool?: string;\n correlationId?: string | undefined;\n },\n): void {\n if (!Number.isFinite(durationSeconds) || durationSeconds < 0) return;\n try {\n const failed = [\n \"failed\",\n \"error\",\n \"provider_declared_error\",\n \"auth_needed\",\n \"outcome_uncertain\",\n \"timeout\",\n \"thrown_transport_error\",\n \"thrown_protocol_error\",\n ].includes(attributes.outcome);\n observability\n .startSpan(name, attributes, { startTimeMs: Date.now() - durationSeconds * 1_000 })\n .end({\n ...(failed ? { error: true } : {}),\n });\n } catch {\n // Observers cannot change the completed model/tool/phase outcome.\n }\n}\n\n/**\n * Explain why a turn did or did not keep provider sandbox work off the\n * pre-model critical path. The reason is a closed enum: never attach session,\n * sandbox, credential, or file identifiers here.\n */\nexport function recordTurnSandboxEstablishPolicy(\n observability: Observability,\n input: {\n policy: TurnSandboxEstablishPolicy;\n reason: TurnSandboxEstablishReason;\n backend: string;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_turn_sandbox_establish_policy_total\",\n help: \"Turn sandbox establish policy decisions by bounded reason.\",\n labels: {\n policy: input.policy,\n reason: input.reason,\n backend: input.backend,\n },\n });\n}\n\n/** Physical establish/resume attempts inside one logical provision. Safe\n * lifecycle retries are counted here, never as another user-visible failure. */\nexport function recordSandboxProvisionAttempt(\n observability: Observability,\n input: {\n backend: string;\n stage: SandboxLogicalProvisionStage;\n category: SandboxLogicalProvisionCategory;\n outcome: SandboxProvisionAttemptOutcome;\n durationSeconds: number;\n },\n): void {\n const labels = {\n backend: input.backend,\n stage: input.stage,\n category: input.category,\n outcome: input.outcome,\n };\n try {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provision_attempts_total\",\n help: \"Internal physical attempts within logical sandbox provisions.\",\n labels,\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_attempt_duration_seconds\",\n help: \"Internal sandbox provision attempt duration by bounded structural outcome.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels,\n value: Math.max(0, input.durationSeconds),\n });\n } catch {\n try {\n observability.incrementCounter({\n name: \"opengeni_observability_observer_errors_total\",\n help: \"Observability observer failures isolated from product execution.\",\n labels: { observer: \"sandbox_provision\" },\n });\n } catch {\n // The registry itself is unhealthy; provisioning remains authoritative.\n }\n }\n}\n\n/** One terminal observation for one correlation-qualified logical provision. */\nexport function recordSandboxLogicalProvision(\n observability: Observability,\n input: {\n backend: string;\n stage: SandboxLogicalProvisionStage;\n category: SandboxLogicalProvisionCategory | \"none\";\n outcome: SandboxLogicalProvisionOutcome;\n expected: boolean;\n internalAttempts: number;\n durationSeconds: number;\n },\n): void {\n const labels = {\n backend: input.backend,\n stage: input.stage,\n category: input.category,\n outcome: input.outcome,\n expected: input.expected ? \"true\" : \"false\",\n };\n try {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provisions_total\",\n help: \"Logical sandbox provisions by terminal outcome and bounded failure taxonomy.\",\n labels,\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_duration_seconds\",\n help: \"Logical sandbox provision duration including internal safe retries.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels,\n value: Math.max(0, input.durationSeconds),\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_internal_attempts\",\n help: \"Internal physical attempts per terminal logical sandbox provision.\",\n buckets: [1, 2, 3, 4, 5, 8],\n labels,\n value: Math.max(1, input.internalAttempts),\n });\n } catch {\n try {\n observability.incrementCounter({\n name: \"opengeni_observability_observer_errors_total\",\n help: \"Observability observer failures isolated from product execution.\",\n labels: { observer: \"sandbox_provision\" },\n });\n } catch {\n // The registry itself is unhealthy; provisioning remains authoritative.\n }\n }\n}\n\nexport function recordSandboxSharedPreparation(\n observability: Observability,\n measurement: {\n path: \"owner\" | \"joined\" | \"reused\";\n outcome: \"completed\" | \"failed\";\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_sandbox_shared_preparation_duration_seconds\",\n help: \"Duration of exact-lease immutable sandbox preparation by durable coordination path and outcome.\",\n labels: { path: measurement.path, outcome: measurement.outcome },\n value: Math.max(0, measurement.durationSeconds),\n });\n}\n\nexport function recordTurnWorkerPreparationTotal(\n observability: Observability,\n input: {\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_turn_worker_preparation_duration_seconds\",\n help: \"Worker preparation from the durable turn-start boundary until entering the runtime; lazy SDK request preparation and model-request audit are separate phases.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/**\n * Measure cumulative user-visible startup latency from the durable turn queue\n * timestamp to a bounded milestone. Unlike the per-phase histogram above,\n * these samples are end-to-end and can therefore back real queue/dispatch/TTFB\n * SLOs without adding turn or session identifiers to Prometheus.\n */\nexport function recordTurnStartupMilestone(\n observability: Observability,\n input: {\n milestone: TurnStartupMilestone;\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_turn_startup_milestone_duration_seconds\",\n help: \"Cumulative seconds from durable turn queueing to a bounded startup milestone.\",\n buckets: TURN_STARTUP_MILESTONE_BUCKETS,\n labels: {\n milestone: input.milestone,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\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 lives on a human-perceptible scale, but reasoning models legitimately\n// think for tens of seconds before their first streamed token. The finite\n// buckets therefore run well past 10s: a top bucket at 10s made every slower\n// first token land in +Inf, so p50/p99 saturated at exactly \"10\" and hid how\n// slow the tail really was.\nconst STREAM_TTFT_BUCKETS = [\n 0.02, 0.05, 0.1, 0.2, 0.35, 0.5, 0.75, 1, 1.5, 2, 3, 5, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300,\n];\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// Opengeni's own per-request work before the provider sees bytes: admission,\n// durable history/audit checkpoints, request build. Normally milliseconds to a\n// second; tens of seconds means our database or consumer is the bottleneck.\nconst MODEL_REQUEST_PRE_DISPATCH_BUCKETS = [\n 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120,\n];\n\n/** First streamed content of a provider response. `any` is the first reasoning\n * or answer delta; `text` is the first answer-text delta. They overlap — never\n * sum across `content`. */\nexport type ProviderFirstContent = \"any\" | \"text\";\n\n/**\n * Per-stream timing tracker fed every normalized runtime event in push order,\n * plus two producer-side hooks. It separates OUR latency from the PROVIDER's:\n *\n * - `opengeni_stream_ttft_seconds{provider}` — user-perceived (re)start latency:\n * time from a stream start / structural boundary (tool call, tool result,\n * completed message, usage frame) to the first streamed content delta. It\n * mixes our between-call work with provider time; keep it for the absolute\n * dashboard view, not for provider alerting.\n * - `opengeni_model_request_pre_dispatch_seconds{provider}` — OUR per-request\n * latency: SDK model entry (the first admission check) to the literal\n * provider dispatch. Covers admission, the durable history checkpoint,\n * credit revalidation, the durable request audit and request build.\n * - `opengeni_model_provider_ttft_seconds{provider,content}` — the PROVIDER's\n * latency: literal dispatch to the first streamed `any` (reasoning or text)\n * delta and to the first answer `text` delta. Includes network, provider\n * queueing, prompt prefill and reasoning before the first streamed token.\n * - `opengeni_stream_inter_delta_gap_seconds{provider,class}` — gap between\n * consecutive content deltas of the SAME class, reset at every structural\n * event so a gap never spans a tool call or a model boundary.\n *\n * Dispatch timing deliberately survives structural events: the consumer may\n * still be persisting the previous response's tool results when the producer\n * dispatches the next request, but the next request's first delta always\n * follows its own dispatch. Purely observational and clock-injectable.\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 private modelEntryAt: number | null = null;\n private dispatchedAt: number | null = null;\n private readonly providerFirstRecorded = new Set<ProviderFirstContent>();\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 /** Producer side: the SDK entered a model request (first admission check).\n * Admission can be re-entered for the same request; keep the earliest. */\n onModelRequestEntry(): void {\n if (this.modelEntryAt === null) this.modelEntryAt = this.now();\n }\n\n /** Producer side: request bytes are about to leave this process. */\n onProviderDispatch(): void {\n const at = this.now();\n if (this.modelEntryAt !== null) {\n this.observability.observeHistogram({\n name: \"opengeni_model_request_pre_dispatch_seconds\",\n help: \"Seconds of Opengeni work from SDK model-request entry to literal provider dispatch.\",\n buckets: MODEL_REQUEST_PRE_DISPATCH_BUCKETS,\n labels: { provider: this.options.provider },\n value: Math.max(0, (at - this.modelEntryAt) / 1000),\n });\n this.modelEntryAt = null;\n }\n // A transport retry re-dispatches the same request; the first delta then\n // belongs to the latest dispatch.\n this.dispatchedAt = at;\n this.providerFirstRecorded.clear();\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 stream start or structural boundary to the next streamed content delta (user-perceived; includes Opengeni between-call work).\",\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 if (this.dispatchedAt !== null) {\n this.observeProviderFirst(\"any\", at);\n if (deltaClass === \"message\") this.observeProviderFirst(\"text\", at);\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 private observeProviderFirst(content: ProviderFirstContent, at: number): void {\n if (this.dispatchedAt === null || this.providerFirstRecorded.has(content)) return;\n this.providerFirstRecorded.add(content);\n this.observability.observeHistogram({\n name: \"opengeni_model_provider_ttft_seconds\",\n help: \"Seconds from literal provider dispatch to the first streamed content delta (any = reasoning or text, text = answer text).\",\n buckets: STREAM_TTFT_BUCKETS,\n labels: { provider: this.options.provider, content },\n value: Math.max(0, (at - this.dispatchedAt) / 1000),\n });\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\nexport function sessionEventBatchSizeClass(eventCount: number): string {\n if (!Number.isFinite(eventCount) || eventCount <= 0) return \"unknown\";\n if (eventCount === 1) return \"1\";\n if (eventCount <= 5) return \"2-5\";\n if (eventCount <= 10) return \"6-10\";\n if (eventCount <= 25) return \"11-25\";\n if (eventCount <= 50) return \"26-50\";\n return \"51+\";\n}\n\n/** Bounded database-phase attribution for exact-attempt event appends. */\nexport function recordSessionEventAppendPhase(\n observability: Observability,\n observation: SessionEventAppendPhaseObservation,\n): void {\n observability.observeHistogram({\n name: \"opengeni_session_event_append_phase_seconds\",\n help: \"Duration of one bounded database phase in an exact-attempt session event append.\",\n buckets: STREAM_IO_BUCKETS,\n labels: {\n path: \"turn_attempt\",\n event_class: observation.eventClass,\n batch_size_class: sessionEventBatchSizeClass(observation.eventCount),\n phase: observation.phase,\n outcome: observation.outcome,\n },\n value: Math.max(0, observation.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 retain context-pressure\n// diagnostics while compaction alerting follows the durable model-aware start.\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 for context-pressure\n * dashboards and provider diagnostics. */\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 successfully completed, by bounded trigger. */\nexport function recordContextCompaction(\n observability: Observability,\n trigger: ContextCompactionTrigger,\n): void {\n observability.incrementCounter({ ...CONTEXT_COMPACTIONS_METRIC, labels: { trigger } });\n}\n\n/**\n * A durable context-compaction start, recorded only after the attempt-fenced\n * `compaction.started` transaction commits. This is the model-aware threshold\n * signal; it does not guess from a static token count shared by unlike models.\n */\nexport function recordContextCompactionStarted(\n observability: Observability,\n trigger: ContextCompactionTrigger,\n): void {\n observability.incrementCounter({ ...CONTEXT_COMPACTION_STARTS_METRIC, labels: { trigger } });\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: \"Agent Knowledge 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 Agent Knowledge 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 // protobuf Long is supported through its string representation. Refuse JS\n // coercions (booleans, arrays, blanks) and hostile toString implementations.\n let raw: unknown = value;\n try {\n if (typeof raw === \"object\" && raw !== null && !Array.isArray(raw)) raw = raw.toString();\n } catch {\n throw new Error(`Temporal stats returned invalid ${field}`);\n }\n const parsed =\n typeof raw === \"number\"\n ? raw\n : typeof raw === \"string\" && /^\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/.test(raw)\n ? Number(raw)\n : Number.NaN;\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}`);\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\nexport type CodeSearchCallOutcome =\n | \"completed\"\n | \"jev_unavailable\"\n | \"jev_rejected\"\n | \"workspace_unavailable\"\n | \"invalid_arguments\"\n | \"breaker_open\"\n | \"cancelled\"\n | \"failed\";\n\n/** One `code_search` tool call: outcome, wall time and the Jev work it used. */\nexport function recordCodeSearchCall(\n observability: Observability,\n input: {\n outcome: CodeSearchCallOutcome;\n durationSeconds: number;\n jevRequests: number;\n jevCostUsd: number;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_code_search_calls_total\",\n help: \"Jev-backed code_search tool calls by outcome.\",\n labels: { outcome: input.outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_code_search_duration_seconds\",\n help: \"Wall time of one code_search tool call.\",\n buckets: [0.5, 1, 2, 4, 8, 15, 30, 60],\n labels: { outcome: input.outcome },\n value: Math.max(0, input.durationSeconds),\n });\n if (input.jevRequests > 0) {\n observability.incrementCounter({\n name: \"opengeni_code_search_jev_requests_total\",\n help: \"Jev requests made by code_search.\",\n amount: input.jevRequests,\n });\n }\n if (input.jevCostUsd > 0) {\n observability.incrementCounter({\n name: \"opengeni_code_search_jev_cost_micro_usd_total\",\n help: \"Estimated Jev list-price cost of code_search, in micro-USD.\",\n amount: Math.round(input.jevCostUsd * 1_000_000),\n });\n }\n}\n\n/**\n * One skill_read call. Labels are closed sets: `skill` is a built-in id or\n * `custom` for every other Skill, so tenant Skill ids, names, and requested\n * identifiers never become labels. `source` is `unknown` when the read was\n * refused before a Skill resolved.\n */\nexport function recordSkillRead(\n observability: Observability,\n read: {\n caller: \"model\" | \"codemode\";\n kind: SkillReadKind;\n source: SkillUseSource | null;\n /** The resolved Skill id, or the requested identifier when none resolved. */\n skill: string;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_skill_reads_total\",\n help: \"skill_read calls by Skill source, built-in Skill id (custom for any other Skill), result kind, and caller.\",\n labels: {\n source: read.source ?? \"unknown\",\n skill: skillReadMetricLabel(read.source, read.skill),\n kind: read.kind,\n caller: read.caller,\n },\n });\n}\n\n/**\n * One skill_checkout call. Labels are closed sets and never carry Skill ids,\n * names, or paths. Phases that did not run are not observed.\n */\nexport function recordSkillCheckout(\n observability: Observability,\n checkout: {\n outcome: \"written\" | \"unchanged\" | \"refused\" | \"failed\";\n selection: \"all\" | \"paths\";\n written: number;\n unchanged: number;\n resolveSeconds: number | null;\n sandboxSeconds: number | null;\n writeSeconds: number | null;\n totalSeconds: number;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_skill_checkouts_total\",\n help: \"skill_checkout calls by outcome and whether they selected specific paths.\",\n labels: { outcome: checkout.outcome, selection: checkout.selection },\n });\n const phases = [\n [\"resolve\", checkout.resolveSeconds],\n [\"sandbox\", checkout.sandboxSeconds],\n [\"write\", checkout.writeSeconds],\n [\"total\", checkout.totalSeconds],\n ] as const;\n for (const [phase, seconds] of phases) {\n if (seconds === null) continue;\n observability.observeHistogram({\n name: \"opengeni_skill_checkout_duration_seconds\",\n help: \"Wall time of skill_checkout phases: resolve (authority and Skill read), sandbox (filesystem handle, including lazy box start), write (batched file write), and total.\",\n buckets: [0.05, 0.1, 0.25, 0.5, 1, 2, 4, 8, 15, 30, 60],\n labels: { phase, outcome: checkout.outcome },\n value: Math.max(0, seconds),\n });\n }\n for (const [result, amount] of [\n [\"written\", checkout.written],\n [\"unchanged\", checkout.unchanged],\n ] as const) {\n if (amount <= 0) continue;\n observability.incrementCounter({\n name: \"opengeni_skill_checkout_files_total\",\n help: \"Files skill_checkout wrote or found already present with the same content.\",\n labels: { result },\n amount,\n });\n }\n}\n\nfunction skillReadMetricLabel(source: SkillUseSource | null, skill: string): string {\n if (source !== null && source !== \"builtin\") return \"custom\";\n // A refused read may name a built-in by its plain name.\n for (const candidate of source === null ? [skill, `builtin:${skill}`] : [skill]) {\n const parsed = BundledSkillId.safeParse(candidate);\n if (parsed.success) return parsed.data;\n }\n return \"custom\";\n}\n","/** Small injectable clock edges for deterministic telemetry lifecycle tests. */\nexport type TelemetryScheduler = {\n timeout(callback: () => void, delayMs: number): () => void;\n interval(callback: () => void, intervalMs: number): () => void;\n defer(callback: () => void): void;\n};\n\nexport const telemetryScheduler: TelemetryScheduler = {\n timeout(callback, delayMs) {\n const timer = setTimeout(callback, delayMs);\n timer.unref?.();\n return () => clearTimeout(timer);\n },\n interval(callback, intervalMs) {\n const timer = setInterval(callback, intervalMs);\n timer.unref?.();\n return () => clearInterval(timer);\n },\n defer: queueMicrotask,\n};\n\nexport function telemetryInterval(value: number, name: string): number {\n if (!Number.isSafeInteger(value) || value < 1) {\n throw new Error(`${name} must be a positive integer`);\n }\n return value;\n}\n\n/** Bad/regressing clocks are unknown, not a new timestamp or a zero age.\n * Retain the last valid high-water observation for diagnostic age reporting. */\nexport function createTelemetryClock(now: () => number) {\n let latest: number | null = null;\n return {\n read(): number | null {\n let observed: number;\n try {\n observed = now();\n } catch {\n return null;\n }\n if (\n !Number.isFinite(observed) ||\n observed < 0 ||\n observed > Number.MAX_SAFE_INTEGER ||\n (latest !== null && observed < latest)\n )\n return null;\n latest = observed;\n return observed;\n },\n latest: () => latest,\n };\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,WAAW,oBAAoB;AAExC,IAAM,qBAAqB;AAC3B,IAAM,wBAAwB;AAC9B,IAAM,qBAAqB;AAC3B,IAAM,gCAAgC;AACtC,IAAM,gCAAgC,KAAK,OAAO;AAClD,IAAM,4BAA4B;AAClC,IAAM,4BAA4B;AAE3B,IAAM,oCAAoC;AAC1C,IAAM,yCAAyC;AAE/C,IAAM,kCAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAIO,IAAM,sCAAsC;AAAA,EACjD;AAAA,EACA;AAAA,EACA;AACF;AAeA,IAAM,qBAAqB,oBAAI,IAAI,CAAC,gBAAgB,oBAAoB,kBAAkB,CAAC;AAE3F,eAAsB,sCAAsC,OAOhB;AAC1C,QAAM,YAAY,oBAAoB,MAAM,SAAS;AACrD,QAAM,QAAQ,MAAM,SAAS,KAAK,IAAI;AACtC,QAAM,cACJ,MAAM,eACL,MAAM,mCAAmC;AAAA,IACxC,WAAW,MAAM,aAAa;AAAA,IAC9B,QAAQ,MAAM,UAAU;AAAA,EAC1B,CAAC;AACH,QAAM,SAAS,IAAI,IAAI,MAAM,UAAU,kBAAkB;AACzD,MAAI,OAAO,aAAa,UAAU;AAChC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AAEA,QAAM,YAA4C;AAAA,IAChD,oBAAoB,WAAW,+BAA+B;AAAA,IAC9D,uBAAuB,WAAW,mCAAmC;AAAA,IACrE,cAAc;AAAA,IACd,mBAAmB;AAAA,EACrB;AAEA,QAAM,oBAAoB;AAAA,IACxB;AAAA,IACA;AAAA,IACA,MAAM,oDAAoD,mBAAmB,SAAS,CAAC;AAAA,IACvF,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,WAAW,SAAS,OAAO;AAAA,IAC5B,CAAC,aAAa,SAAS,SAAS;AAAA,EAClC,GAAY;AACV,QAAI,UAAU,EAAG;AACjB,kBAAc,iBAAiB;AAAA,MAC7B,MAAM;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,OAAO;AAAA,MACjB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEA,SAAS,qBAAqB,QAA+B,OAAuB;AAClF,MAAI,WAAW,QAAQ,WAAW,UAAW,QAAO;AAEpD,aAAW,aAAa,WAAW,OAAO,CAAC,OAAO,WAAW,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG;AAC/E,UAAM,SAAS,eAAe,UAAU,SAAS;AACjD,QAAI,OAAO,QAAS,QAAO,OAAO;AAAA,EACpC;AACA,SAAO;AACT;","names":["key"]}
1
+ {"version":3,"sources":["../src/opensandbox-kubernetes-inventory.ts","../src/observability-metrics.ts","../src/telemetry-scheduler.ts"],"sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { request as httpsRequest } from \"node:https\";\n\nconst KUBERNETES_API_URL = \"https://kubernetes.default.svc\";\nconst KUBERNETES_TOKEN_FILE = \"/var/run/secrets/opengeni.io/opensandbox-kubernetes-inventory/token\";\nconst KUBERNETES_CA_FILE = \"/var/run/secrets/opengeni.io/opensandbox-kubernetes-inventory/ca.crt\";\nconst KUBERNETES_REQUEST_TIMEOUT_MS = 10_000;\nconst KUBERNETES_RESPONSE_MAX_BYTES = 64 * 1024 * 1024;\nconst KUBERNETES_LIST_PAGE_SIZE = 200;\nconst KUBERNETES_LIST_MAX_PAGES = 10_000;\n\nexport const OPENSANDBOX_CLEANUP_STUCK_SECONDS = 300;\nexport const OPENSANDBOX_EXPIRATION_OVERDUE_SECONDS = 180;\n\nexport const OPENSANDBOX_BATCHSANDBOX_PHASES = [\n \"pending\",\n \"running\",\n \"pausing\",\n \"paused\",\n \"resuming\",\n \"failed\",\n \"unknown\",\n] as const;\n\nexport type OpenSandboxBatchSandboxPhase = (typeof OPENSANDBOX_BATCHSANDBOX_PHASES)[number];\n\nexport const OPENSANDBOX_WORKLOAD_POD_CONDITIONS = [\n \"pending\",\n \"image_pull\",\n \"unschedulable\",\n] as const;\n\nexport type OpenSandboxWorkloadPodCondition = (typeof OPENSANDBOX_WORKLOAD_POD_CONDITIONS)[number];\n\nexport type OpenSandboxKubernetesInventory = {\n batchSandboxPhases: Record<OpenSandboxBatchSandboxPhase, number>;\n workloadPodConditions: Record<OpenSandboxWorkloadPodCondition, number>;\n cleanupStuck: number;\n expirationOverdue: number;\n};\n\nexport type KubernetesJsonRequest = (url: URL) => Promise<unknown>;\n\ntype JsonRecord = Record<string, unknown>;\n\nconst IMAGE_PULL_REASONS = new Set([\"ErrImagePull\", \"ImagePullBackOff\", \"InvalidImageName\"]);\n\nexport async function inspectOpenSandboxKubernetesInventory(input: {\n namespace: string;\n nowMs?: number;\n requestJson?: KubernetesJsonRequest;\n apiUrl?: string;\n tokenFile?: string;\n caFile?: string;\n}): Promise<OpenSandboxKubernetesInventory> {\n const namespace = kubernetesNamespace(input.namespace);\n const nowMs = input.nowMs ?? Date.now();\n const requestJson =\n input.requestJson ??\n (await authenticatedKubernetesJsonRequest({\n tokenFile: input.tokenFile ?? KUBERNETES_TOKEN_FILE,\n caFile: input.caFile ?? KUBERNETES_CA_FILE,\n }));\n const apiUrl = new URL(input.apiUrl ?? KUBERNETES_API_URL);\n if (apiUrl.protocol !== \"https:\") {\n throw new Error(\"OpenSandbox Kubernetes inventory API must use HTTPS\");\n }\n\n const inventory: OpenSandboxKubernetesInventory = {\n batchSandboxPhases: zeroRecord(OPENSANDBOX_BATCHSANDBOX_PHASES),\n workloadPodConditions: zeroRecord(OPENSANDBOX_WORKLOAD_POD_CONDITIONS),\n cleanupStuck: 0,\n expirationOverdue: 0,\n };\n\n await visitKubernetesList({\n apiUrl,\n requestJson,\n path: `/apis/sandbox.opensandbox.io/v1alpha1/namespaces/${encodeURIComponent(namespace)}/batchsandboxes`,\n visit: (item) => observeBatchSandbox(inventory, item, nowMs),\n });\n await visitKubernetesList({\n apiUrl,\n requestJson,\n path: `/api/v1/namespaces/${encodeURIComponent(namespace)}/pods`,\n query: { labelSelector: \"opensandbox.io/id\" },\n visit: (item) => observeWorkloadPod(inventory, item),\n });\n\n return inventory;\n}\n\nfunction observeBatchSandbox(\n inventory: OpenSandboxKubernetesInventory,\n item: unknown,\n nowMs: number,\n): void {\n const resource = record(item);\n const metadata = record(resource.metadata);\n const spec = record(resource.spec);\n const status = record(resource.status);\n\n inventory.batchSandboxPhases[batchSandboxPhase(status.phase)] += 1;\n\n const deletionTimestampMs = dateMilliseconds(metadata.deletionTimestamp);\n const finalizers = Array.isArray(metadata.finalizers) ? metadata.finalizers : [];\n if (\n deletionTimestampMs !== null &&\n finalizers.length > 0 &&\n nowMs - deletionTimestampMs >= OPENSANDBOX_CLEANUP_STUCK_SECONDS * 1_000\n ) {\n inventory.cleanupStuck += 1;\n }\n\n const expirationMs = dateMilliseconds(spec.expireTime);\n if (\n expirationMs !== null &&\n nowMs - expirationMs >= OPENSANDBOX_EXPIRATION_OVERDUE_SECONDS * 1_000\n ) {\n inventory.expirationOverdue += 1;\n }\n}\n\nfunction observeWorkloadPod(inventory: OpenSandboxKubernetesInventory, item: unknown): void {\n const pod = record(item);\n const metadata = record(pod.metadata);\n if (dateMilliseconds(metadata.deletionTimestamp) !== null) return;\n\n const status = record(pod.status);\n if (status.phase === \"Pending\") inventory.workloadPodConditions.pending += 1;\n if (podHasImagePullFailure(status)) inventory.workloadPodConditions.image_pull += 1;\n if (podIsUnschedulable(status)) inventory.workloadPodConditions.unschedulable += 1;\n}\n\nfunction podHasImagePullFailure(status: JsonRecord): boolean {\n for (const key of [\"initContainerStatuses\", \"containerStatuses\", \"ephemeralContainerStatuses\"]) {\n const statuses = Array.isArray(status[key]) ? status[key] : [];\n for (const value of statuses) {\n const waiting = record(record(record(value).state).waiting);\n if (typeof waiting.reason === \"string\" && IMAGE_PULL_REASONS.has(waiting.reason)) return true;\n }\n }\n return false;\n}\n\nfunction podIsUnschedulable(status: JsonRecord): boolean {\n const conditions = Array.isArray(status.conditions) ? status.conditions : [];\n return conditions.some((value) => {\n const condition = record(value);\n return (\n condition.type === \"PodScheduled\" &&\n condition.status === \"False\" &&\n condition.reason === \"Unschedulable\"\n );\n });\n}\n\nfunction batchSandboxPhase(value: unknown): OpenSandboxBatchSandboxPhase {\n switch (value) {\n case \"Pending\":\n return \"pending\";\n case \"Succeed\":\n case \"Running\":\n return \"running\";\n case \"Pausing\":\n return \"pausing\";\n case \"Paused\":\n return \"paused\";\n case \"Resuming\":\n return \"resuming\";\n case \"Failed\":\n return \"failed\";\n default:\n return \"unknown\";\n }\n}\n\nasync function visitKubernetesList(input: {\n apiUrl: URL;\n requestJson: KubernetesJsonRequest;\n path: string;\n query?: Record<string, string>;\n visit: (item: unknown) => void;\n}): Promise<void> {\n let continuation = \"\";\n for (let page = 0; page < KUBERNETES_LIST_MAX_PAGES; page += 1) {\n const url = new URL(input.path, input.apiUrl);\n url.searchParams.set(\"limit\", String(KUBERNETES_LIST_PAGE_SIZE));\n for (const [key, value] of Object.entries(input.query ?? {})) {\n url.searchParams.set(key, value);\n }\n if (continuation) url.searchParams.set(\"continue\", continuation);\n\n const payload = record(await input.requestJson(url));\n if (!Array.isArray(payload.items)) {\n throw new Error(`Kubernetes list ${input.path} returned no items array`);\n }\n for (const item of payload.items) input.visit(item);\n\n const next = record(payload.metadata).continue;\n if (typeof next !== \"string\" || next.length === 0) return;\n if (next === continuation) {\n throw new Error(`Kubernetes list ${input.path} repeated its continuation token`);\n }\n continuation = next;\n }\n throw new Error(`Kubernetes list ${input.path} exceeded its page safety bound`);\n}\n\nasync function authenticatedKubernetesJsonRequest(input: {\n tokenFile: string;\n caFile: string;\n}): Promise<KubernetesJsonRequest> {\n const [tokenText, ca] = await Promise.all([\n readFile(input.tokenFile, \"utf8\"),\n readFile(input.caFile),\n ]);\n const token = tokenText.trim();\n if (!token) throw new Error(\"OpenSandbox Kubernetes inventory token is empty\");\n\n return async (url) =>\n await new Promise<unknown>((resolve, reject) => {\n const request = httpsRequest(\n url,\n {\n method: \"GET\",\n ca,\n headers: {\n accept: \"application/json\",\n authorization: `Bearer ${token}`,\n },\n },\n (response) => {\n const chunks: Buffer[] = [];\n let bytes = 0;\n response.on(\"data\", (chunk: Buffer | string) => {\n const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);\n bytes += buffer.length;\n if (bytes > KUBERNETES_RESPONSE_MAX_BYTES) {\n request.destroy(new Error(\"Kubernetes inventory response exceeded its byte limit\"));\n return;\n }\n chunks.push(buffer);\n });\n response.on(\"end\", () => {\n const statusCode = response.statusCode ?? 0;\n if (statusCode < 200 || statusCode >= 300) {\n reject(new Error(`Kubernetes inventory request failed with HTTP ${statusCode}`));\n return;\n }\n try {\n resolve(JSON.parse(Buffer.concat(chunks).toString(\"utf8\")));\n } catch {\n reject(new Error(\"Kubernetes inventory response was not valid JSON\"));\n }\n });\n response.on(\"error\", reject);\n },\n );\n request.setTimeout(KUBERNETES_REQUEST_TIMEOUT_MS, () => {\n request.destroy(new Error(\"Kubernetes inventory request timed out\"));\n });\n request.on(\"error\", reject);\n request.end();\n });\n}\n\nfunction kubernetesNamespace(value: string): string {\n const namespace = value.trim();\n if (\n namespace.length === 0 ||\n namespace.length > 63 ||\n !/^[a-z0-9](?:[-a-z0-9]*[a-z0-9])?$/u.test(namespace)\n ) {\n throw new Error(\"OpenSandbox Kubernetes inventory namespace is invalid\");\n }\n return namespace;\n}\n\nfunction dateMilliseconds(value: unknown): number | null {\n if (typeof value !== \"string\" || value.length === 0) return null;\n const milliseconds = Date.parse(value);\n return Number.isFinite(milliseconds) ? milliseconds : null;\n}\n\nfunction record(value: unknown): JsonRecord {\n return value && typeof value === \"object\" && !Array.isArray(value) ? (value as JsonRecord) : {};\n}\n\nfunction zeroRecord<const T extends readonly string[]>(keys: T): Record<T[number], number> {\n return Object.fromEntries(keys.map((key) => [key, 0])) as Record<T[number], number>;\n}\n","import { createHash } from \"node:crypto\";\nimport { errorCodeToJSON } from \"@opengeni/agent-proto\";\nimport {\n BundledSkillId,\n CREDIT_GRANT_CLASSES,\n SandboxBackend,\n type SessionEventType,\n type SkillReadKind,\n type SkillUseSource,\n} from \"@opengeni/contracts\";\nimport {\n ACTIVE_USER_WINDOWS,\n type ActiveUserWindow,\n type CreditGrantTotals,\n type IdleInteractionInspection,\n type SessionEventAppendPhaseObservation,\n} from \"@opengeni/db\";\nimport {\n natsSubscriptionTerminationCounter,\n type EventBusOptions,\n type EventLogger,\n} from \"@opengeni/events\";\nimport type { Attributes, AttributeValue, Observability } from \"@opengeni/observability\";\nimport type { CompanyBrainContributionReceipt } from \"./model-context-contributions\";\nimport {\n createTelemetryClock,\n telemetryInterval,\n telemetryScheduler,\n type TelemetryScheduler,\n} from \"./telemetry-scheduler\";\nimport {\n OPENSANDBOX_BATCHSANDBOX_PHASES,\n OPENSANDBOX_WORKLOAD_POD_CONDITIONS,\n type OpenSandboxKubernetesInventory,\n} from \"./opensandbox-kubernetes-inventory\";\nimport {\n MCP_TOOL_METRIC_EXTERNAL_LABEL,\n SELFHOSTED_INFRASTRUCTURE_FAULT_CLASSES,\n WORKSPACE_CAPTURE_SKIP_REASONS,\n isMcpToolMetricLabel,\n modelUsageTokenCountOrNull,\n type RuntimeMetricsHooks,\n type SelfhostedOpObservation,\n type SelfhostedOpObserver,\n type ToolPreparationPhaseMeasurement,\n} from \"@opengeni/runtime\";\n\nexport type TurnOutcome = \"completed\" | \"failed\" | \"cancelled\" | \"recovering\";\nexport type WorkerDeathRecoveryOutcome = \"recovering\" | \"exhausted\";\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};\nexport type TurnTaskQueueIdentity = { temporalNamespace: string; taskQueue: string };\nexport type TurnTaskQueueReadOptions = { signal: AbortSignal; deadline: number };\n\nexport function turnTaskQueueMetricLabels(identity: TurnTaskQueueIdentity) {\n return { temporal_namespace: identity.temporalNamespace, task_queue: identity.taskQueue };\n}\nexport type SessionRecoveryBacklogStateSummary = {\n /** Every durable recovering session in this state with no active attempt. */\n count: number;\n /** Of those, sessions still inside the backoff their recovery request recorded. */\n scheduled: number;\n /** Seconds the single oldest candidate has been past its recovery due time; 0 when none is. */\n oldestOverdueSeconds: number;\n};\nexport type SessionRecoveryBacklog = {\n quiescence_missing: SessionRecoveryBacklogStateSummary;\n projection_stale: SessionRecoveryBacklogStateSummary;\n};\nexport type ContextCompactionPendingSummary = {\n pendingCount: number;\n oldestStartedAt: Date | null;\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 modelRequestTrackers = new WeakMap<Observability, ModelRequestLifecycleMetrics>();\nconst creditBalanceGaugeAccounts = new WeakMap<Observability, Set<string>>();\nconst modelCacheCounterTotals = new WeakMap<Observability, Map<string, number>>();\nconst initializedContextCompactionMetrics = new WeakSet<Observability>();\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;\nconst TURN_OUTCOMES: readonly TurnOutcome[] = [\"completed\", \"failed\", \"cancelled\", \"recovering\"];\nconst WORKER_DEATH_RECOVERY_OUTCOMES: readonly WorkerDeathRecoveryOutcome[] = [\n \"recovering\",\n \"exhausted\",\n];\nconst WORKER_DEATH_TIMEOUT_TYPES = [\"heartbeat\", \"schedule_to_start\"] as const;\nconst CONTEXT_COMPACTION_TRIGGERS = [\"auto\", \"operator\", \"proactive\", \"overflow\"] as const;\n\nexport type ContextCompactionTrigger = (typeof CONTEXT_COMPACTION_TRIGGERS)[number];\n\nconst CONTEXT_COMPACTION_STARTS_METRIC = {\n name: \"opengeni_context_compaction_starts_total\",\n help: \"Total durable context compaction starts, by trigger.\",\n} as const;\nconst CONTEXT_COMPACTIONS_METRIC = {\n name: \"opengeni_context_compactions_total\",\n help: \"Total completed context compactions, by trigger.\",\n} as const;\n\n/** Logger plus the closed-label subscription-termination counter for NATS connections. */\nexport function observabilityEventBusOptions(\n observability: Observability,\n): Pick<EventBusOptions, \"logger\" | \"onSubscriptionTerminated\"> {\n return {\n logger: observabilityEventLogger(observability),\n onSubscriptionTerminated: natsSubscriptionTerminationCounter(observability),\n };\n}\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 completedOperationSpan(observability, \"worker.model.call\", durationSeconds, {\n provider,\n outcome,\n });\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 onSandboxReadinessReplacement: ({ backend, outcome }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_readiness_replacements_total\",\n help: \"Fresh sandbox command-readiness replacement decisions by backend and outcome.\",\n labels: { backend, outcome },\n });\n },\n onSandboxProviderApiThrottle: ({ backend, operation }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provider_api_throttles_total\",\n help: \"Total sandbox provider API throttle responses.\",\n labels: { backend, operation },\n });\n },\n onSandboxTtlRenewal: ({ backend, outcome }) => {\n observability.incrementCounter({\n name: \"opengeni_sandbox_ttl_renewals_total\",\n help: \"Total renewable sandbox provider TTL refresh attempts.\",\n labels: { backend, outcome },\n });\n },\n onOpenSandboxSignedEndpoint: ({ outcome, port }) => {\n observability.incrementCounter({\n name: \"opengeni_opensandbox_signed_endpoint_total\",\n help: \"OpenSandbox signed Channel B mint and host-fetch outcomes.\",\n labels: { outcome, port: String(port) },\n });\n },\n onWorkspaceCapture: ({ backend, outcome, durationSeconds }) => {\n if (!Number.isFinite(durationSeconds) || durationSeconds < 0) return;\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n completedOperationSpan(observability, \"worker.workspace_capture\", durationSeconds, {\n backend: safeBackend,\n outcome,\n });\n observability.observeHistogram({\n name: \"opengeni_workspace_capture_duration_seconds\",\n help: \"Physical warm workspace capture and publication duration, including late settlement after caller timeout.\",\n labels: { backend: safeBackend, outcome },\n value: durationSeconds,\n });\n },\n onWorkspaceCaptureSkipped: ({ backend, reason }) => {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n const safeReason = (WORKSPACE_CAPTURE_SKIP_REASONS as readonly string[]).includes(reason)\n ? reason\n : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_workspace_capture_skipped_total\",\n help: \"Due warm workspace checkpoints that could not start, by backend and fixed reason. Not-yet-due (throttled) and already-complete checkpoints are excluded.\",\n labels: { backend: safeBackend, reason: safeReason },\n });\n },\n onWorkspaceArchiveObject: ({ outcome, backend }) => {\n observability.incrementCounter({\n name: \"opengeni_workspace_archive_object_total\",\n help: \"Workspace archive object-storage put/delete outcomes.\",\n labels: { outcome, backend },\n });\n },\n onMcpToolCall: ({ outcome, tool, durationSeconds }) => {\n // Re-check the closed label set at the export boundary so a caller can\n // never turn a raw user-defined tool name into a metric series.\n const safeTool = isMcpToolMetricLabel(tool) ? tool : MCP_TOOL_METRIC_EXTERNAL_LABEL;\n completedOperationSpan(observability, \"worker.mcp.tool_call\", durationSeconds, {\n outcome,\n tool: safeTool,\n });\n observability.incrementCounter({\n name: \"opengeni_mcp_tool_calls_total\",\n help: \"Total physical MCP tool calls by bounded structural outcome and first-party tool (other tools are 'external').\",\n labels: { outcome, tool: safeTool },\n });\n observability.observeHistogram({\n name: \"opengeni_mcp_tool_call_duration_seconds\",\n help: \"MCP tool-call duration in seconds by bounded structural outcome and first-party tool (other tools are 'external').\",\n labels: { outcome, tool: safeTool },\n value: durationSeconds,\n });\n },\n onMcpLifecycle: ({ phase, policy, outcome, durationSeconds }) => {\n observability.incrementCounter({\n name: \"opengeni_mcp_lifecycle_operations_total\",\n help: \"Total physical MCP lifecycle operations by bounded phase, policy, and outcome.\",\n labels: { phase, policy, outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_mcp_lifecycle_operation_duration_seconds\",\n help: \"MCP lifecycle operation duration in seconds by bounded phase, policy, and outcome.\",\n labels: { phase, policy, outcome },\n value: durationSeconds,\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: {\n op: string;\n faultClass: string;\n attempts: number;\n machineId?: string;\n };\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 function initializeWorkerOutcomeMetrics(observability: Observability): void {\n for (const outcome of TURN_OUTCOMES) {\n observability.incrementCounter({\n name: \"opengeni_turns_total\",\n help: \"Total agent turns by terminal outcome.\",\n labels: { outcome },\n amount: 0,\n });\n }\n for (const outcome of WORKER_DEATH_RECOVERY_OUTCOMES) {\n for (const timeoutType of WORKER_DEATH_TIMEOUT_TYPES) {\n observability.incrementCounter({\n name: \"opengeni_turn_worker_death_recoveries_total\",\n help: \"Total durable same-turn worker-death recovery outcomes.\",\n labels: { outcome, timeout_type: timeoutType },\n amount: 0,\n });\n }\n }\n}\n\n/**\n * Publish every bounded compaction lifecycle series before the first event.\n * The zero counters make the closed trigger catalog explicit before a scrape.\n * Alerting uses the separate durable control-worker projection below; these\n * process-local counters are lifecycle-rate diagnostics only.\n */\nexport function initializeContextCompactionMetrics(observability: Observability): void {\n if (initializedContextCompactionMetrics.has(observability)) return;\n for (const trigger of CONTEXT_COMPACTION_TRIGGERS) {\n observability.incrementCounter({\n ...CONTEXT_COMPACTION_STARTS_METRIC,\n labels: { trigger },\n amount: 0,\n });\n observability.incrementCounter({\n ...CONTEXT_COMPACTIONS_METRIC,\n labels: { trigger },\n amount: 0,\n });\n }\n initializedContextCompactionMetrics.add(observability);\n}\n\nexport function recordWorkerDeathRecoveryMetrics(\n observability: Observability,\n input: {\n outcome: WorkerDeathRecoveryOutcome;\n timeoutType: \"HEARTBEAT\" | \"SCHEDULE_TO_START\";\n },\n): void {\n const turnOutcome: TurnOutcome = input.outcome === \"exhausted\" ? \"failed\" : \"recovering\";\n observability.incrementCounter({\n name: \"opengeni_turn_worker_death_recoveries_total\",\n help: \"Total durable same-turn worker-death recovery outcomes.\",\n labels: {\n outcome: input.outcome,\n timeout_type: input.timeoutType.toLowerCase(),\n },\n });\n // The turn process that owned this attempt is gone, so its process-local\n // lifecycle tracker cannot publish the outcome. Record it from the fenced\n // control activity only after the durable recovery transaction wins.\n observability.incrementCounter({\n name: \"opengeni_turns_total\",\n help: \"Total agent turns by terminal outcome.\",\n labels: { outcome: turnOutcome },\n });\n}\n\nexport class TurnLifecycleMetrics {\n private readonly attempts = 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: {\n now?: () => number;\n refreshIntervalMs?: number;\n } = {},\n ) {}\n\n start(input: { attemptId: string }): void {\n const now = this.now();\n this.attempts.set(input.attemptId, { startedAt: now, lastProgressAt: now });\n this.ensureTimer();\n this.refreshGauges();\n }\n\n progress(input: { attemptId: string }): void {\n const attempt = this.attempts.get(input.attemptId);\n if (!attempt) return;\n attempt.lastProgressAt = this.now();\n }\n\n finish(input: {\n attemptId: string;\n outcome: TurnOutcome | null;\n durationSeconds?: number;\n }): void {\n const startedAt = this.attempts.get(input.attemptId)?.startedAt;\n if (startedAt !== undefined) {\n this.attempts.delete(input.attemptId);\n }\n if (input.outcome) {\n const observedDuration =\n input.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: input.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: input.outcome },\n value: observedDuration,\n });\n }\n this.refreshGauges();\n if (this.attempts.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 physical agent-turn attempts in this worker process.\",\n value: this.attempts.size,\n });\n this.observability.setGauge({\n name: \"opengeni_turn_oldest_inflight_age_seconds\",\n help: \"Age in seconds of the oldest in-flight physical agent-turn attempt 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 physical agent-turn attempt.\",\n value: this.oldestNoProgressAgeSeconds(),\n });\n }\n\n stop(): void {\n this.attempts.clear();\n this.refreshGauges();\n this.stopTimer();\n }\n\n private oldestInflightAgeSeconds(): number {\n if (this.attempts.size === 0) {\n return 0;\n }\n let oldest = Number.POSITIVE_INFINITY;\n for (const { startedAt } of this.attempts.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.attempts.size === 0) return 0;\n let leastRecentProgress = Number.POSITIVE_INFINITY;\n for (const { lastProgressAt } of this.attempts.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 modelRequestLifecycleMetricsFor(\n observability: Observability,\n): ModelRequestLifecycleMetrics {\n const existing = modelRequestTrackers.get(observability);\n if (existing) return existing;\n const tracker = new ModelRequestLifecycleMetrics(observability);\n modelRequestTrackers.set(observability, tracker);\n return tracker;\n}\n\nexport class ModelRequestLifecycleMetrics {\n private readonly requests = new Map<\n string,\n { provider: string; startedAt: number; lastEventAt: number }\n >();\n private readonly providers = new Set<string>();\n private timer: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n private readonly observability: Observability,\n private readonly options: {\n now?: () => number;\n refreshIntervalMs?: number;\n } = {},\n ) {}\n\n start(key: string, provider: string): void {\n const now = this.now();\n this.providers.add(provider);\n this.requests.set(key, { provider, startedAt: now, lastEventAt: now });\n this.ensureTimer();\n this.refreshGauges();\n }\n\n event(key: string, interEventGapMs?: number): void {\n const request = this.requests.get(key);\n if (!request) return;\n request.lastEventAt = this.now();\n this.observability.incrementCounter({\n name: \"opengeni_model_request_stream_events_total\",\n help: \"Complete, valid provider SSE data events by provider.\",\n labels: { provider: request.provider },\n });\n if (interEventGapMs !== undefined) {\n this.observability.observeHistogram({\n name: \"opengeni_model_request_stream_event_gap_seconds\",\n help: \"Seconds between complete, valid provider SSE data events.\",\n buckets: MODEL_REQUEST_PHASE_BUCKETS,\n labels: { provider: request.provider },\n value: Math.max(0, interEventGapMs / 1000),\n });\n }\n }\n\n finish(key: string): void {\n this.requests.delete(key);\n this.refreshGauges();\n if (this.requests.size === 0) this.stopTimer();\n }\n\n refreshGauges(): void {\n const now = this.now();\n for (const provider of this.providers) {\n const active = [...this.requests.values()].filter((request) => request.provider === provider);\n this.observability.setGauge({\n name: \"opengeni_model_requests_inflight\",\n help: \"Current in-flight provider model requests in this worker process.\",\n labels: { provider },\n value: active.length,\n });\n this.observability.setGauge({\n name: \"opengeni_model_request_oldest_no_event_age_seconds\",\n help: \"Seconds since the least recently progressing in-flight provider request received a valid SSE event.\",\n labels: { provider },\n value:\n active.length === 0\n ? 0\n : Math.max(0, (now - Math.min(...active.map((request) => request.lastEventAt))) / 1000),\n });\n }\n }\n\n stop(): void {\n this.requests.clear();\n this.refreshGauges();\n this.stopTimer();\n }\n\n private ensureTimer(): void {\n if (this.timer) return;\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) return;\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 approximate shared turn-worker activity queue rather than 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 identity: TurnTaskQueueIdentity,\n): void {\n const labels = turnTaskQueueMetricLabels(identity);\n // Invalid observations are failures, not a fabricated fresh zero. Validate\n // every field before publishing any portion of a new observation.\n const values = {\n backlog: requiredTemporalNumber(stats.eligibleBacklog, \"eligibleBacklog\", { integer: true }),\n age: requiredTemporalNumber(stats.oldestBacklogAgeSeconds, \"oldestBacklogAgeSeconds\"),\n add: requiredTemporalNumber(stats.tasksAddRate, \"tasksAddRate\"),\n dispatch: requiredTemporalNumber(stats.tasksDispatchRate, \"tasksDispatchRate\"),\n };\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog\",\n help: \"Approximate Temporal activity backlog on the shared turn-worker queue, including video and retries.\",\n labels,\n value: values.backlog,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_backlog_oldest_age_seconds\",\n help: \"Approximate age of the oldest eligible activity task on the shared turn-worker queue.\",\n labels,\n value: values.age,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_add_rate\",\n help: \"Temporal activities added per second to the shared turn-worker queue over its rolling window.\",\n labels,\n value: values.add,\n });\n observability.setGauge({\n name: \"opengeni_turn_eligible_tasks_dispatch_rate\",\n help: \"Temporal activities dispatched per second from the shared turn-worker queue over its rolling window.\",\n labels,\n value: values.dispatch,\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 if (typeof stats !== \"object\" || Array.isArray(stats)) {\n throw new Error(\"Temporal DescribeTaskQueue returned malformed stats\");\n }\n const eligibleBacklog = requiredTemporalNumber(\n stats.approximateBacklogCount,\n \"approximateBacklogCount\",\n { integer: true },\n );\n let oldestBacklogAgeSeconds = 0;\n if (stats.approximateBacklogAge !== null && stats.approximateBacklogAge !== undefined) {\n if (\n typeof stats.approximateBacklogAge !== \"object\" ||\n Array.isArray(stats.approximateBacklogAge)\n ) {\n throw new Error(\"Temporal stats returned invalid approximateBacklogAge\");\n }\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\n/** Requires a fresh metric registry/schema; pre-registered legacy unlabelled\n * queue gauges are not migrated in place. Each collector owns its native read. */\nexport function startTurnCapacityMonitor(input: {\n observability: Observability;\n identity: TurnTaskQueueIdentity;\n read: (options: TurnTaskQueueReadOptions) => Promise<TurnTaskQueueStats>;\n intervalMs?: number;\n readTimeoutMs?: number;\n closeTimeoutMs?: number;\n now?: () => number;\n monotonicNow?: () => number;\n scheduler?: TelemetryScheduler;\n}): { close: () => Promise<void> } {\n const intervalMs = telemetryInterval(input.intervalMs ?? 15_000, \"queue monitor intervalMs\");\n const readTimeoutMs = telemetryInterval(\n input.readTimeoutMs ?? 5_000,\n \"queue monitor readTimeoutMs\",\n );\n const closeTimeoutMs = telemetryInterval(\n input.closeTimeoutMs ?? 1_000,\n \"queue monitor closeTimeoutMs\",\n );\n const scheduler = input.scheduler ?? telemetryScheduler;\n const labels = turnTaskQueueMetricLabels(input.identity);\n const wallClock = createTelemetryClock(input.now ?? Date.now);\n const monotonicClock = createTelemetryClock(input.monotonicNow ?? (() => performance.now()));\n let startedAt = wallClock.read();\n const readClock = () => {\n const wall = wallClock.read();\n const monotonic = monotonicClock.read();\n return wall === null || monotonic === null ? null : { wall, monotonic };\n };\n let lastSuccessAt: number | null = null;\n let lastReadSucceeded = false;\n let stopped = false;\n let running: {\n controller: AbortController;\n done: Promise<void>;\n cancelTimeout: () => void;\n } | null = null;\n let closePromise: Promise<void> | undefined;\n const safely = (action: () => void) => {\n try {\n action();\n } catch {\n /* Telemetry must not break the worker. */\n }\n };\n const recordStatus = (observation = readClock()) => {\n if (observation === null) lastReadSucceeded = false;\n const observedAt = wallClock.latest();\n startedAt ??= observedAt;\n const successAgeMs =\n observedAt === null ? 0 : observedAt - (lastSuccessAt ?? startedAt ?? observedAt);\n const set = (name: string, help: string, value: number) =>\n safely(() => input.observability.setGauge({ name, help, labels, 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 the latest read succeeded and the shared activity-queue gauges are 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 if (stopped) return;\n const observation = readClock();\n recordStatus(observation);\n if (running || observation === null) return;\n const deadline = observation.wall + readTimeoutMs;\n if (!Number.isFinite(deadline) || deadline > Number.MAX_SAFE_INTEGER) {\n recordStatus(null);\n return;\n }\n const controller = new AbortController();\n const attempt = { controller, done: Promise.resolve(), cancelTimeout: () => {} };\n running = attempt;\n attempt.cancelTimeout = scheduler.timeout(() => {\n if (stopped || running !== attempt) return;\n lastReadSucceeded = false;\n recordStatus();\n controller.abort();\n // Keep the attempt owned until its native call settles. A misbehaving\n // reader ignoring cancellation must not cause overlapping RPCs.\n }, readTimeoutMs);\n let read: Promise<TurnTaskQueueStats>;\n try {\n read = input.read({ signal: controller.signal, deadline });\n } catch (error) {\n read = Promise.reject(error);\n }\n attempt.done = read\n .then((stats) => {\n if (stopped || controller.signal.aborted || running !== attempt) return;\n const completedAt = readClock();\n // A native RPC may settle before its deadline but its Promise callback\n // can run after it, ahead of a delayed JS timeout. Never restamp that old\n // response as fresh. Monotonic age also fences wall-clock adjustments.\n if (\n completedAt === null ||\n completedAt.wall >= deadline ||\n completedAt.monotonic - observation.monotonic >= readTimeoutMs\n ) {\n lastReadSucceeded = false;\n recordStatus(completedAt);\n controller.abort();\n return;\n }\n recordTurnTaskQueueStats(input.observability, stats, input.identity);\n lastSuccessAt = completedAt.wall;\n lastReadSucceeded = true;\n recordStatus(completedAt);\n })\n .catch(() => {\n if (stopped || controller.signal.aborted || running !== attempt) return;\n lastReadSucceeded = false;\n recordStatus();\n safely(() =>\n input.observability.warn(\"turn capacity monitor: Temporal task-queue stats failed\", {\n // Do not export arbitrary server errors or credentials.\n errorClass: \"WorkerTelemetryReadError\",\n origin: \"turn-capacity-monitor\",\n }),\n );\n })\n .finally(() => {\n attempt.cancelTimeout();\n if (running === attempt) running = null;\n });\n };\n refresh();\n const stopTimer = scheduler.interval(refresh, intervalMs);\n return {\n close: () => {\n if (closePromise) return closePromise;\n stopped = true;\n stopTimer();\n lastReadSucceeded = false;\n recordStatus();\n const attempt = running;\n attempt?.cancelTimeout();\n attempt?.controller.abort();\n closePromise = attempt\n ? new Promise<void>((resolve) => {\n const cancel = scheduler.timeout(resolve, closeTimeoutMs);\n void attempt.done.then(() => {\n cancel();\n resolve();\n });\n })\n : Promise.resolve();\n return closePromise;\n },\n };\n}\n\nexport function recordSessionRecoveryBacklogGauges(\n observability: Observability,\n backlog: SessionRecoveryBacklog,\n): void {\n for (const state of [\"quiescence_missing\", \"projection_stale\"] as const) {\n const summary = backlog[state];\n observability.setGauge({\n name: \"opengeni_session_recovery_backlog\",\n help: \"Current durable recovering-session obligations with no active attempt, by bounded reconciliation state. Includes sessions legitimately sleeping in their recorded recovery backoff.\",\n labels: { state },\n value: nonnegativeFinite(summary.count),\n });\n observability.setGauge({\n name: \"opengeni_session_recovery_scheduled\",\n help: \"Durable recovering sessions still inside the recovery backoff (provider Retry-After or connectivity pacing) their recovery request recorded, by bounded reconciliation state.\",\n labels: { state },\n value: nonnegativeFinite(summary.scheduled),\n });\n observability.setGauge({\n name: \"opengeni_session_recovery_oldest_overdue_seconds\",\n help: \"Seconds the single oldest durable recovering session with no active attempt has been past its recovery due time, by bounded reconciliation state; 0 when none is overdue.\",\n labels: { state },\n value: nonnegativeFinite(summary.oldestOverdueSeconds),\n });\n }\n}\n\nexport function startSessionRecoveryMonitor(input: {\n observability: Observability;\n read: () => Promise<SessionRecoveryBacklog>;\n intervalMs?: number;\n now?: () => number;\n}): { close: () => Promise<void> } {\n const intervalMs = input.intervalMs ?? 60_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_session_recovery_monitor_last_read_success\",\n \"Whether the latest durable session-recovery aggregate read completed successfully.\",\n lastReadSucceeded ? 1 : 0,\n );\n set(\n \"opengeni_session_recovery_monitor_last_success_timestamp_seconds\",\n \"Unix timestamp of the latest successful durable session-recovery aggregate read, or zero before one succeeds.\",\n lastSuccessAt === null ? 0 : lastSuccessAt / 1_000,\n );\n set(\n \"opengeni_session_recovery_monitor_fresh\",\n \"Whether durable session-recovery backlog gauges have a successful read within three monitor intervals.\",\n lastReadSucceeded && lastSuccessAt !== null && successAgeMs <= intervalMs * 3 ? 1 : 0,\n );\n };\n const refresh = () => {\n recordStatus();\n if (stopped || running) return;\n running = input\n .read()\n .then((backlog) => {\n recordSessionRecoveryBacklogGauges(input.observability, backlog);\n lastSuccessAt = now();\n lastReadSucceeded = true;\n recordStatus();\n })\n .catch((error) => {\n lastReadSucceeded = false;\n recordStatus();\n input.observability.warn(\"session recovery monitor: durable aggregate read 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 recordContextCompactionPendingGauges(\n observability: Observability,\n summary: ContextCompactionPendingSummary,\n nowMs = Date.now(),\n): void {\n const oldestStartedAtMs = summary.oldestStartedAt?.getTime();\n const oldestPendingAgeSeconds =\n oldestStartedAtMs === undefined || !Number.isFinite(oldestStartedAtMs)\n ? 0\n : Math.max(0, (nowMs - oldestStartedAtMs) / 1_000);\n observability.setGauge({\n name: \"opengeni_context_compaction_pending\",\n help: \"Current exact active attempts whose latest automatic compaction landmark is still started.\",\n value: nonnegativeFinite(summary.pendingCount),\n });\n observability.setGauge({\n name: \"opengeni_context_compaction_oldest_pending_age_seconds\",\n help: \"Age in seconds of the oldest durably pending automatic compaction, or zero when none are pending.\",\n value: oldestPendingAgeSeconds,\n });\n}\n\nexport function startContextCompactionPendingMonitor(input: {\n observability: Observability;\n read: () => Promise<ContextCompactionPendingSummary>;\n intervalMs?: number;\n now?: () => number;\n}): { close: () => Promise<void> } {\n const intervalMs = input.intervalMs ?? 60_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_context_compaction_monitor_last_read_success\",\n \"Whether the latest durable context-compaction aggregate read completed successfully.\",\n lastReadSucceeded ? 1 : 0,\n );\n set(\n \"opengeni_context_compaction_monitor_last_success_timestamp_seconds\",\n \"Unix timestamp of the latest successful durable context-compaction aggregate read, or zero before one succeeds.\",\n lastSuccessAt === null ? 0 : lastSuccessAt / 1_000,\n );\n set(\n \"opengeni_context_compaction_monitor_fresh\",\n \"Whether durable context-compaction pending gauges have a successful read within three monitor intervals.\",\n lastReadSucceeded && lastSuccessAt !== null && successAgeMs <= intervalMs * 3 ? 1 : 0,\n );\n };\n const refresh = () => {\n recordStatus();\n if (stopped || running) return;\n running = input\n .read()\n .then((summary) => {\n recordContextCompactionPendingGauges(input.observability, summary, now());\n lastSuccessAt = now();\n lastReadSucceeded = true;\n recordStatus();\n })\n .catch((error) => {\n lastReadSucceeded = false;\n recordStatus();\n input.observability.warn(\"context compaction monitor: durable aggregate read 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 function recordOpenSandboxKubernetesInventoryGauges(\n observability: Observability,\n inventory: OpenSandboxKubernetesInventory,\n): void {\n for (const phase of OPENSANDBOX_BATCHSANDBOX_PHASES) {\n observability.setGauge({\n name: \"opengeni_opensandbox_batchsandboxes\",\n help: \"Current OpenSandbox BatchSandboxes by bounded lifecycle phase.\",\n labels: { phase },\n value: inventory.batchSandboxPhases[phase],\n });\n }\n for (const condition of OPENSANDBOX_WORKLOAD_POD_CONDITIONS) {\n observability.setGauge({\n name: \"opengeni_opensandbox_workload_pods\",\n help: \"Current OpenSandbox workload Pods by bounded operational condition.\",\n labels: { condition },\n value: inventory.workloadPodConditions[condition],\n });\n }\n observability.setGauge({\n name: \"opengeni_opensandbox_cleanup_stuck\",\n help: \"Current OpenSandbox BatchSandboxes deleting past the cleanup threshold with finalizers.\",\n value: inventory.cleanupStuck,\n });\n observability.setGauge({\n name: \"opengeni_opensandbox_expiration_overdue\",\n help: \"Current OpenSandbox BatchSandboxes still present past the provider-expiry threshold.\",\n value: inventory.expirationOverdue,\n });\n}\n\nexport const SANDBOX_INVENTORY_PROJECTION_DOMAINS = [\n \"leases\",\n \"checkpoint_artifacts\",\n \"recovery_observations\",\n \"rotation_backlog\",\n \"retained_processes\",\n \"expired_drains\",\n \"opensandbox_kubernetes\",\n \"modal_provider\",\n \"interaction_idle\",\n \"checkpoint_staleness\",\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\n/**\n * Distinct managed people with authenticated browser activity in each fixed\n * window (migration 0565 presence). Every control worker publishes the same\n * global value, so dashboards must aggregate with `max()` across pods, never\n * `sum()`. API keys, services and embedded hosts are never counted.\n */\nexport function recordActiveUserGauges(\n observability: Observability,\n counts: Record<ActiveUserWindow, number>,\n): void {\n for (const window of ACTIVE_USER_WINDOWS) {\n observability.setGauge({\n name: \"opengeni_active_users\",\n help: \"Distinct managed users with authenticated activity within the window (global; use max across pods).\",\n labels: { window },\n value: counts[window],\n });\n }\n}\n\n/**\n * Positive credit grants observed by the ledger trigger since migration 0565,\n * by closed class. These are cumulative database totals published as gauges by\n * every control worker: aggregate with `max()` across pods, then subtract the\n * same expression `offset` by the window. A per-pod `increase()` sees only one\n * pod's lifetime and undercounts across control-worker restarts.\n * Counting in the database covers the grants no application process writes:\n * the verified-signup trial grant (a setup trigger) and operator grants.\n */\nexport function recordCreditGrantGauges(\n observability: Observability,\n totals: CreditGrantTotals,\n): void {\n for (const grantClass of CREDIT_GRANT_CLASSES) {\n const total = totals[grantClass];\n observability.setGauge({\n name: \"opengeni_credit_grants_total\",\n help: \"Positive credit grants observed in the ledger since migration 0565, by grant class (global cumulative; use max across pods).\",\n labels: { grant_class: grantClass },\n value: total.count,\n });\n observability.setGauge({\n name: \"opengeni_credit_granted_micros_total\",\n help: \"Credit micros granted in the ledger since migration 0565, by grant class (global cumulative; use max across pods).\",\n labels: { grant_class: grantClass },\n value: total.micros,\n });\n }\n}\n\n/**\n * The deployment-level runtime switch for the one-time verified signup trial\n * credit (migration 0521): 1 while it allows grants, 0 when an operator has\n * disabled it or no revision exists. A grant also needs the API's\n * OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in, which\n * {@link recordVerifiedSignupTrialDeploymentFlagGauge} reports separately.\n */\nexport function recordVerifiedSignupTrialSwitchGauge(\n observability: Observability,\n grantsEnabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_verified_signup_trial_credits_runtime_enabled\",\n help: \"Whether the runtime switch allows new verified signup trial credit grants (1) or blocks them (0).\",\n value: grantsEnabled ? 1 : 0,\n });\n}\n\n/**\n * The deployment-level runtime switch for new managed account sign-ups\n * (migration 0585): 1 while it allows new accounts (also when no revision\n * exists), 0 when an operator has paused them. The API additionally honours\n * its OPENGENI_MANAGED_AUTH_NEW_SIGNUPS_ENABLED ceiling, which this gauge does\n * not see; `GET /v1/config/client` reports the combined decision.\n */\nexport function recordManagedAuthNewSignupsSwitchGauge(\n observability: Observability,\n signupsEnabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_managed_auth_new_signups_runtime_enabled\",\n help: \"Whether the runtime switch allows new managed account sign-ups (1) or has paused them (0).\",\n value: signupsEnabled ? 1 : 0,\n });\n}\n\n/**\n * The OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in as this\n * worker's configuration sees it. The API reads the same shared setting; new\n * grants happen only while this gauge and the runtime switch gauge are both 1.\n */\nexport function recordVerifiedSignupTrialDeploymentFlagGauge(\n observability: Observability,\n enabled: boolean,\n): void {\n observability.setGauge({\n name: \"opengeni_verified_signup_trial_credits_deployment_enabled\",\n help: \"Whether the OPENGENI_VERIFIED_SIGNUP_TRIAL_CREDITS_ENABLED master opt-in is on (1) or off (0) in this deployment's configuration.\",\n value: enabled ? 1 : 0,\n });\n}\n\n/**\n * Provider-side Modal inventory reconciled against live leases by the orphan\n * sweep: `running` = every running box in the app, `unleased` = running boxes\n * no live lease protects (orphans, counted before termination), and\n * `lease_missing_instance` = warm leases whose exact provider instance is no\n * longer running (zombies).\n */\nexport function recordModalSandboxInventoryGauges(\n observability: Observability,\n inventory: { running: number; unleased: number; liveLeaseInstancesMissing: number },\n): void {\n const states = {\n running: inventory.running,\n unleased: inventory.unleased,\n lease_missing_instance: inventory.liveLeaseInstancesMissing,\n };\n for (const [state, value] of Object.entries(states)) {\n observability.setGauge({\n name: \"opengeni_modal_sandbox_inventory\",\n help: \"Running Modal sandboxes reconciled against live sandbox leases by the orphan sweep.\",\n labels: { state },\n value: Math.max(0, value),\n });\n }\n}\n\n/** Warm leases held only by Browser/Computer interaction holders, by time since\n * the newest interaction activity. Such boxes never drain on their own until the\n * interaction session ends or the provider deadline. */\nexport function recordInteractionOnlyLeaseGauges(\n observability: Observability,\n counts: Record<string, number>,\n): void {\n for (const [idleBucket, value] of Object.entries(counts)) {\n observability.setGauge({\n name: \"opengeni_sandbox_leases_interaction_only\",\n help: \"Warm sandbox leases held only by Browser/Computer sessions, by time since last interaction activity.\",\n labels: { idle_bucket: idleBucket },\n value: Math.max(0, value),\n });\n }\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\n/** Fixed outcomes of legacy retained-command containment: per-candidate\n * enrollment inspection, then the enrolled drain's exact cold commit. */\nexport const SANDBOX_COMMAND_CONTAINMENT_OUTCOMES = [\n \"idle_enrolled\",\n \"deadline_enrolled\",\n \"forced_deadline_enrolled\",\n \"quiescence_enrolled\",\n \"resumed_enrolled\",\n \"not_eligible\",\n \"inspection_failed\",\n \"contained\",\n \"provider_missing\",\n] as const;\n\nexport type SandboxCommandContainmentOutcome =\n (typeof SANDBOX_COMMAND_CONTAINMENT_OUTCOMES)[number];\n\n/**\n * Shared subscription core shadow (docs/subscription-accounts.md, SUB-COMPAT-03).\n * Every label is a closed set: provider, security parity, placement agreement,\n * contract requirement, skip reason and the legacy decision inputs present.\n */\nexport const SUBSCRIPTION_CORE_SHADOW_PROVIDERS = [\"codex\", \"xai\", \"claude\"] as const;\n/** Is the account the legacy selector chose inside the core's eligible set? */\nexport const SUBSCRIPTION_CORE_SHADOW_PARITY = [\n \"eligible\",\n \"not_authorized\",\n \"not_servable\",\n \"unknown_connection\",\n \"no_selection\",\n] as const;\n/** How the core's decision compares with the legacy decision (would-switch). */\nexport const SUBSCRIPTION_CORE_SHADOW_PLACEMENTS = [\n \"same_account\",\n \"different_account\",\n \"core_waits\",\n \"core_runs_legacy_waits\",\n \"both_wait\",\n] as const;\n/** Requirements the reference checker can report, plus checker failure. */\nexport const SUBSCRIPTION_CORE_SHADOW_REQUIREMENTS = [\n \"SUB-ACCESS-06\",\n \"SUB-CREDITS-01\",\n \"SUB-ELIG-01\",\n \"SUB-ELIG-02\",\n \"SUB-ELIG-03\",\n \"SUB-ELIG-04\",\n \"SUB-ELIG-05\",\n \"SUB-FAIL-02\",\n \"SUB-FAIL-03\",\n \"SUB-FAIL-04\",\n \"SUB-OWN-05\",\n \"SUB-SEL-02\",\n \"SUB-SEL-04\",\n \"SUB-SET-06\",\n \"SUB-STICK-02\",\n \"SUB-WAIT-01\",\n \"SUB-WAIT-02\",\n \"other\",\n \"checker_error\",\n] as const;\nexport const SUBSCRIPTION_CORE_SHADOW_SKIPS = [\n \"busy\",\n \"no_session_actor\",\n \"session_not_visible\",\n \"timeout\",\n \"cancelled\",\n \"error\",\n] as const;\n/**\n * Legacy decision inputs the Codex fleet shadow does not record (inventory\n * Part 3, answer (c)), counted when present in a compared decision.\n */\nexport const SUBSCRIPTION_CORE_SHADOW_INPUTS = [\n \"manual_pin\",\n \"policy_pin\",\n \"last_account\",\n \"rotation_off\",\n \"rotation_on\",\n \"organization_pool\",\n \"personal_pool\",\n \"codex_mode_override\",\n \"model_policy\",\n \"model_filtered\",\n \"plan_excluded\",\n \"model_cooldown\",\n \"exhausted\",\n \"unknown_quota\",\n \"compaction_lock\",\n \"lease_reused\",\n \"truncated\",\n] as const;\n\nexport type SubscriptionCoreShadowProvider = (typeof SUBSCRIPTION_CORE_SHADOW_PROVIDERS)[number];\nexport type SubscriptionCoreShadowParity = (typeof SUBSCRIPTION_CORE_SHADOW_PARITY)[number];\nexport type SubscriptionCoreShadowPlacement = (typeof SUBSCRIPTION_CORE_SHADOW_PLACEMENTS)[number];\nexport type SubscriptionCoreShadowSkip = (typeof SUBSCRIPTION_CORE_SHADOW_SKIPS)[number];\nexport type SubscriptionCoreShadowInput = (typeof SUBSCRIPTION_CORE_SHADOW_INPUTS)[number];\n\nexport type SubscriptionCoreShadowObservation =\n | { outcome: \"skipped\"; reason: SubscriptionCoreShadowSkip }\n | {\n outcome: \"compared\";\n parity: SubscriptionCoreShadowParity;\n /** The first reason the legacy account is outside the eligible set. */\n parityReason: string | null;\n placement: SubscriptionCoreShadowPlacement;\n violations: readonly string[];\n inputs: readonly SubscriptionCoreShadowInput[];\n };\n\nconst SUBSCRIPTION_CORE_SHADOW_REQUIREMENT_SET = new Set<string>(\n SUBSCRIPTION_CORE_SHADOW_REQUIREMENTS,\n);\n/** First reasons a legacy account is outside the eligible set (policy ineligibility). */\nexport const SUBSCRIPTION_CORE_SHADOW_PARITY_REASONS = [\n \"provider_disabled\",\n \"organization_accounts_off\",\n \"out_of_scope\",\n \"personal_connections_disabled\",\n \"personal_owner_inactive\",\n \"personal_not_owners_work\",\n \"personal_authority_missing\",\n \"unhealthy\",\n \"allocator_disabled\",\n \"wrong_provider\",\n \"model_unknown\",\n \"model_not_allowed_by_workspace\",\n \"model_not_entitled\",\n \"model_not_allowed_by_connection\",\n \"model_cooling_down\",\n \"exhausted\",\n] as const;\nconst SUBSCRIPTION_CORE_SHADOW_PARITY_REASON_SET = new Set<string>(\n SUBSCRIPTION_CORE_SHADOW_PARITY_REASONS,\n);\n\nconst SUBSCRIPTION_CORE_SHADOW_DURATION_BUCKETS = [\n 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1,\n];\n\nexport function recordSubscriptionCoreShadow(\n observability: Pick<Observability, \"incrementCounter\" | \"observeHistogram\">,\n provider: SubscriptionCoreShadowProvider,\n observation: SubscriptionCoreShadowObservation,\n durationSeconds: number,\n): void {\n observability.observeHistogram({\n name: \"opengeni_subscription_core_shadow_duration_seconds\",\n help: \"Shared subscription core shadow comparison duration by provider, including skips.\",\n labels: { provider },\n value: durationSeconds,\n buckets: SUBSCRIPTION_CORE_SHADOW_DURATION_BUCKETS,\n });\n if (observation.outcome === \"skipped\") {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_skips_total\",\n help: \"Shadow comparisons not made, by fixed reason; placement is unaffected.\",\n labels: { provider, reason: observation.reason },\n });\n return;\n }\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_comparisons_total\",\n help: \"Shadow comparisons by security parity of the legacy account and core placement agreement.\",\n labels: { provider, parity: observation.parity, placement: observation.placement },\n });\n if (observation.parityReason !== null) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_parity_failures_total\",\n help: \"Legacy-selected accounts outside the core's eligible set, by first fixed reason.\",\n labels: {\n provider,\n reason: SUBSCRIPTION_CORE_SHADOW_PARITY_REASON_SET.has(observation.parityReason)\n ? observation.parityReason\n : \"other\",\n },\n });\n }\n for (const requirement of new Set(observation.violations)) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_violations_total\",\n help: \"Contract invariant violations of the core's own shadow decision, by requirement.\",\n labels: {\n provider,\n requirement: SUBSCRIPTION_CORE_SHADOW_REQUIREMENT_SET.has(requirement)\n ? requirement\n : \"other\",\n },\n });\n }\n for (const input of observation.inputs) {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_inputs_total\",\n help: \"Legacy decision inputs present in compared shadow decisions, by fixed input.\",\n labels: { provider, input },\n });\n }\n}\n\n/** A shadow load that outlived its slot ceiling; its slot was given back. */\nexport function recordSubscriptionCoreShadowStuckLoad(\n observability: Pick<Observability, \"incrementCounter\">,\n provider: SubscriptionCoreShadowProvider,\n): void {\n observability.incrementCounter({\n name: \"opengeni_subscription_core_shadow_stuck_loads_total\",\n help: \"Shadow world loads still unsettled at the slot ceiling; the slot was released.\",\n labels: { provider },\n });\n}\n\n/** Idle browser/desktop release inspections by fixed outcome: `checkpointing`\n * (idle browser saves prepared), `released` (holders released without a save). */\nexport function recordSandboxIdleInteractionRelease(\n observability: Observability,\n outcome: IdleInteractionInspection,\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_idle_interaction_release_total\",\n help: \"Idle browser and desktop release inspections on warm boxes, by fixed outcome.\",\n labels: { outcome },\n });\n}\n\nexport function recordSandboxCommandContainment(\n observability: Observability,\n outcome: SandboxCommandContainmentOutcome,\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_command_containment_total\",\n help: \"Legacy retained-command containment inspections and enrolled drain commits by fixed outcome.\",\n labels: { outcome },\n });\n}\n\n/** Only call after the exact draining->cold commit reports wentCold. The\n * backend is validated against the closed contract so provider IDs and other\n * per-sandbox values can never become metric labels. */\nexport function recordSandboxProviderMissingBeforeCapture(\n observability: Observability,\n backend: string,\n): void {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_sandbox_provider_missing_before_capture_total\",\n help: \"Exact sandbox cold commits after definitive provider disappearance before workspace capture.\",\n labels: { backend: safeBackend },\n });\n}\n\n/** Host workspace directory outcome after a Docker drain's exact-container\n * teardown (`retained` means the directory stays on disk). */\nexport function recordSandboxDockerWorkspaceRelease(\n observability: Observability,\n status: \"released\" | \"already_released\" | \"retained\",\n): void {\n observability.incrementCounter({\n name: \"opengeni_sandbox_docker_workspace_release_total\",\n help: \"Docker drain host workspace directories released or retained after exact-container teardown.\",\n labels: { status },\n });\n}\n\nexport function recordSandboxRecoveryObservationGauges(\n observability: Observability,\n observations: {\n providerLosses: number;\n fallbackSelections: number;\n freshWorkspaceSelections?: number;\n },\n): void {\n for (const [kind, value] of [\n [\"provider_missing_before_capture\", observations.providerLosses],\n [\"checkpoint_fallback_selected\", observations.fallbackSelections],\n [\"fresh_workspace_selected\", observations.freshWorkspaceSelections ?? 0],\n ] as const) {\n observability.setGauge({\n name: \"opengeni_sandbox_recovery_observations_recent\",\n help: \"Committed sandbox recovery observations in the last 30 minutes by fixed kind.\",\n labels: { kind },\n value,\n });\n }\n}\n\n/** Fixed outcomes for a committed automatic continuity decision after\n * definitive managed-provider loss: a singleton checkpoint (`selected`), a\n * shared-group checkpoint (`selected_shared`), or a new empty workspace\n * (`fresh_workspace`). Call only after the durable authorization committed. */\nexport const SANDBOX_AUTOMATIC_RECOVERY_OUTCOMES = [\n \"selected\",\n \"selected_shared\",\n \"fresh_workspace\",\n] as const;\nexport type SandboxAutomaticRecoveryOutcome = (typeof SANDBOX_AUTOMATIC_RECOVERY_OUTCOMES)[number];\n\nexport function sandboxAutomaticRecoveryOutcome(input: {\n lane: \"checkpoint\" | \"fresh_workspace\";\n groupSessionCount: number;\n}): SandboxAutomaticRecoveryOutcome {\n if (input.lane === \"fresh_workspace\") return \"fresh_workspace\";\n return input.groupSessionCount > 1 ? \"selected_shared\" : \"selected\";\n}\n\nexport function recordSandboxAutomaticRecoverySelected(\n observability: Observability,\n backend: string,\n outcome: SandboxAutomaticRecoveryOutcome,\n): void {\n const safeBackend = SandboxBackend.safeParse(backend).success ? backend : \"unknown\";\n observability.incrementCounter({\n name: \"opengeni_sandbox_checkpoint_fallback_total\",\n help: \"System-selected continuity after managed provider loss: verified checkpoint or empty workspace.\",\n labels: { backend: safeBackend, outcome },\n });\n}\n\n/** How long live boxes have held changes no checkpoint covers yet. */\nexport function recordSandboxCheckpointStalenessGauges(\n observability: Observability,\n staleness: { dirty: number; stale4h: number; stale12h: number; maxAgeSeconds: number },\n): void {\n const values = {\n dirty: staleness.dirty,\n stale_4h: staleness.stale4h,\n stale_12h: staleness.stale12h,\n } as const;\n for (const [kind, value] of Object.entries(values)) {\n observability.setGauge({\n name: \"opengeni_sandbox_checkpoint_staleness\",\n help: \"Live Modal sandboxes holding a write their last checkpoint did not capture, by age of the first such write.\",\n labels: { kind },\n value,\n });\n }\n observability.setGauge({\n name: \"opengeni_sandbox_checkpoint_age_max_seconds\",\n help: \"Age in seconds of the oldest write a live Modal sandbox's last checkpoint did not capture.\",\n value: Number.isFinite(staleness.maxAgeSeconds) ? Math.max(0, staleness.maxAgeSeconds) : 0,\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 interactionBlocked: 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 interaction_blocked: backlog.interactionBlocked,\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 // Session-owned background commands (migration 0637). Running is live\n // session work after its launch turn ended; stopping already has a stop\n // request, so it still counts as terminal-owner backlog.\n \"background_running\",\n \"background_stopping\",\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<{\n ownerState: string;\n activeCount: number;\n terminalOwnerCount: number;\n }>,\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 owner is gone: a closed launch attempt or released direct request, or a background command already asked to stop. A running session background command is never counted.\",\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 \"background_recovered\",\n \"proof_exited\",\n \"proof_lost\",\n \"proof_checkpoint_failed\",\n \"settled_exited\",\n \"settled_lost\",\n \"provider_lost_whole_box\",\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 \"process_observation_unavailable\",\n \"quarantined_process_observation_unavailable\",\n \"quarantined_binding_missing\",\n \"quarantined_binding_mismatch\",\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\n// ── Per-model usage and money ───────────────────────────────────────────────\n// `model` is the deployment catalog product id (resolved by the caller), or\n// `custom` for a workspace-owned model. The recorder bounds it again: a value\n// outside a conservative id shape becomes `custom`, and after\n// MAX_MODEL_METRIC_LABELS distinct values per process every new value becomes\n// `other`, so a catalog change or a bug can never grow the series count without\n// bound. Account, workspace, user and session ids are never labels here.\n\n/** Who pays the upstream provider for the call. */\nexport type ModelUsagePayer = \"deployment\" | \"external\";\n\nconst MODEL_METRIC_LABEL_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/@+-]{0,127}$/;\nconst MAX_MODEL_METRIC_LABELS = 64;\nconst modelMetricLabels = new WeakMap<Observability, Set<string>>();\n\nexport function boundedModelMetricLabel(observability: Observability, model: string): string {\n if (model === \"custom\" || model === \"other\") return model;\n if (!MODEL_METRIC_LABEL_PATTERN.test(model)) return \"custom\";\n let seen = modelMetricLabels.get(observability);\n if (!seen) {\n seen = new Set<string>();\n modelMetricLabels.set(observability, seen);\n }\n if (seen.has(model)) return model;\n if (seen.size >= MAX_MODEL_METRIC_LABELS) return \"other\";\n seen.add(model);\n return model;\n}\n\nfunction nonnegativeSafeInteger(value: unknown): number {\n return typeof value === \"number\" && Number.isSafeInteger(value) && value > 0 ? value : 0;\n}\n\n/**\n * One authoritative model response, by provider and catalog model. Recorded\n * behind the same durable usage-event fence as the provider-only cache metrics,\n * so a duplicate or late response is not counted twice.\n * - `opengeni_model_responses_total{provider,model,priced}` — responses with\n * reported usage; `priced=\"false\"` means no provider cost estimate exists.\n * - `opengeni_model_tokens_total{provider,model,type}` — `input` (all prompt\n * tokens, including cached and cache-write), `cached_input` and\n * `cache_write` (subsets of `input`), `output`, and `reasoning` (a subset\n * of `output`).\n * - `opengeni_model_provider_cost_micros_total{provider,model,payer,pricing_source}`\n * — estimated upstream cost in USD micros: configured list price, or the\n * gateway-reported cost. `payer=\"external\"` is a customer-paid subscription\n * or key, so that cost is a list-price equivalent Opengeni does not pay.\n */\nexport function recordModelResponseUsage(\n observability: Observability,\n input: {\n provider: string;\n model: string;\n payer: ModelUsagePayer;\n tokens: {\n inputTokens: number | null;\n cachedTokens: number | null;\n cacheWriteTokens: number | null;\n outputTokens: number | null;\n reasoningTokens: number | null;\n };\n estimatedProviderCostMicros: number | null;\n pricingSource: \"configured_list_price\" | \"gateway_reported\" | null;\n webSearchRequests?: number;\n },\n): void {\n const model = boundedModelMetricLabel(observability, input.model);\n const base = { provider: input.provider, model };\n const priced = input.pricingSource !== null && input.estimatedProviderCostMicros !== null;\n observability.incrementCounter({\n name: \"opengeni_model_responses_total\",\n help: \"Authoritative model responses with reported usage, by provider, catalog model and whether a cost estimate exists.\",\n labels: { ...base, priced: priced ? \"true\" : \"false\" },\n });\n const tokenTypes = [\n [\"input\", input.tokens.inputTokens],\n [\"cached_input\", input.tokens.cachedTokens],\n [\"cache_write\", input.tokens.cacheWriteTokens],\n [\"output\", input.tokens.outputTokens],\n [\"reasoning\", input.tokens.reasoningTokens],\n ] as const;\n for (const [type, value] of tokenTypes) {\n const amount = modelUsageTokenCountOrNull(value);\n if (amount === null || amount === 0) continue;\n incrementBoundedModelCacheCounter(observability, {\n name: \"opengeni_model_tokens_total\",\n help: \"Tokens of authoritative model responses by provider, catalog model and token type (cached_input and cache_write are subsets of input; reasoning is a subset of output).\",\n provider: input.provider,\n labels: { ...base, type },\n amount,\n });\n }\n const searches = nonnegativeSafeInteger(input.webSearchRequests ?? 0);\n if (searches > 0)\n observability.incrementCounter({\n name: \"opengeni_model_web_search_requests_total\",\n help: \"Provider-run web searches billed with authoritative model responses, by provider, catalog model and payer.\",\n labels: { ...base, payer: input.payer },\n amount: searches,\n });\n const cost = nonnegativeSafeInteger(input.estimatedProviderCostMicros);\n if (priced && input.pricingSource !== null && cost > 0) {\n observability.incrementCounter({\n name: \"opengeni_model_provider_cost_micros_total\",\n help: \"Estimated upstream provider cost in USD micros by provider, catalog model, payer and pricing source.\",\n labels: { ...base, payer: input.payer, pricing_source: input.pricingSource },\n amount: cost,\n });\n }\n}\n\n/**\n * Credits actually debited for one model response, recorded once when the\n * ledger row is inserted (an idempotent replay debits 0 and records nothing).\n * `funding=\"promotional\"` is the part paid by scoped promotional grants\n * (verified-signup trial, scoped coupon offers); `funding=\"general\"` is the\n * rest, paid from general credit: purchased credits plus any unscoped grant.\n */\nexport function recordModelCreditsCharged(\n observability: Observability | undefined,\n input: { provider: string; model: string; debitedMicros: number; grantDebitedMicros: number },\n): void {\n if (!observability) return;\n const debited = nonnegativeSafeInteger(input.debitedMicros);\n if (debited === 0) return;\n const promotional = Math.min(debited, nonnegativeSafeInteger(input.grantDebitedMicros));\n const model = boundedModelMetricLabel(observability, input.model);\n for (const [funding, amount] of [\n [\"promotional\", promotional],\n [\"general\", debited - promotional],\n ] as const) {\n if (amount === 0) continue;\n observability.incrementCounter({\n name: \"opengeni_model_credits_charged_micros_total\",\n help: \"Credit micros debited for model usage by provider, catalog model and funding (promotional grant or general credit).\",\n labels: { provider: input.provider, model, funding },\n amount,\n });\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 */\n/**\n * How a model request reached its provider, where the provider has more than\n * one transport (Codex: a new or reused Responses WebSocket, or HTTP). Empty\n * for providers with a single transport.\n */\nexport type ModelRequestTransportLabel = \"\" | \"websocket_new\" | \"websocket_reused\" | \"http\";\n\n/** The bounded metric label for how a Codex request was carried. */\nexport function codexRequestTransportLabel(\n transport: { kind: \"websocket\"; connection: \"new\" | \"reused\" } | { kind: \"http\" },\n): ModelRequestTransportLabel {\n if (transport.kind === \"http\") return \"http\";\n return transport.connection === \"reused\" ? \"websocket_reused\" : \"websocket_new\";\n}\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 transport?: ModelRequestTransportLabel;\n },\n): void {\n const outcome = input.outcome ?? \"\";\n const transport = input.transport ?? \"\";\n observability.incrementCounter({\n name: \"opengeni_model_request_phases_total\",\n help: \"Provider model-request lifecycle phases by bounded phase, outcome and transport.\",\n labels: { provider: input.provider, phase: input.phase, outcome, transport },\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, transport },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\nexport type TurnStartupPhase =\n | \"services_initialization\"\n | \"claim_catalog_read\"\n | \"claim_atomic\"\n | \"claim_session_read\"\n | \"claim_capability_settings\"\n | \"learning_policy_freeze\"\n | \"attachment_authority_projection\"\n | \"claim_and_policy\"\n | \"turn_start_settlement\"\n | \"credential_selection\"\n | \"runtime_preparation\"\n | \"session_mcp_settings\"\n | \"sandbox_establish\"\n | \"file_resolution\"\n | \"tool_context_preparation\"\n | \"tool_preparation\"\n | \"tool_server_construction\"\n | \"tool_required_connect\"\n | \"tool_optional_connect\"\n | \"tool_attempt_catalog_build\"\n | \"tool_attempt_catalog_persist\"\n | \"tool_workspace_gateway_catalog_build\"\n | \"post_tool_preparation\"\n | \"agent_construction\"\n | \"post_agent_preparation\"\n | \"file_materialization\"\n | \"history_preparation\"\n | \"history_system_update_load\"\n | \"history_current_attachment_resolution\"\n | \"history_durable_history_load\"\n | \"history_sandbox_envelope_load\"\n | \"history_canonical_projection\"\n | \"history_provider_projection\"\n | \"history_attachment_ref_projection\"\n | \"history_screenshot_materialization\"\n | \"history_model_attachment_projection\"\n | \"history_runtime_input_assembly\"\n | \"history_artifact_candidate_scan\"\n | \"history_generated_image_materialization\"\n | \"history_position_load\"\n | \"owned_sandbox_setup\"\n | \"runtime_stream_initialization\"\n | \"model_request_preparation\"\n | \"model_sdk_serialization\"\n | \"model_prepare_sandbox_agent_preparation\"\n | \"model_prepare_sandbox_agent_manifest_inventory\"\n | \"model_prepare_sandbox_session_manifest_inventory\"\n | \"model_prepare_sandbox_manifest_apply\"\n | \"model_prepare_sandbox_entry_materialization\"\n | \"model_prepare_sandbox_running_check\"\n | \"model_prepare_sandbox_start\"\n | \"model_prepare_sandbox_client_create\"\n | \"model_prepare_sandbox_client_resume\"\n | \"model_prepare_sandbox_client_delete\"\n | \"model_prepare_sandbox_client_state_serialize\"\n | \"model_prepare_sandbox_client_reuse_check\"\n | \"model_prepare_sandbox_workspace_mutation_admission\"\n | \"model_prepare_sandbox_workspace_mutation_provider\"\n | \"model_prepare_sandbox_workspace_mutation_settlement\"\n | \"model_prepare_sandbox_first_routed_resolution_other\"\n | \"model_prepare_sandbox_first_routed_mutation_admission\"\n | \"model_prepare_sandbox_first_routed_provider_operation\"\n | \"model_prepare_sandbox_first_routed_mutation_settlement\"\n | \"model_prepare_sandbox_first_routed_other\"\n | \"model_prepare_sandbox_snapshot_wait\"\n | \"model_prepare_runner_before_first_sandbox_operation\"\n | \"model_prepare_sdk_after_first_sandbox_operation\"\n | \"model_prepare_runner_before_mcp_tools\"\n | \"model_prepare_mcp_tools_snapshot\"\n | \"model_prepare_mcp_tools_before_input_filter\"\n | \"model_prepare_mcp_tools_before_repository_skill_discovery\"\n | \"model_prepare_repository_skill_discovery\"\n | \"model_prepare_repository_skill_discovery_before_input_filter\"\n | \"model_prepare_input_filter_base\"\n | \"model_prepare_input_filter_genesis\"\n | \"model_prepare_input_filter_host\"\n | \"model_prepare_input_filter_tool_output\"\n | \"model_prepare_input_filter_modality\"\n | \"model_prepare_input_filter_context\"\n | \"model_prepare_responses_input_conversion\"\n | \"model_prepare_responses_request_build\"\n | \"model_credential_resolution\"\n | \"model_wire_normalization\"\n | \"model_request_audit\"\n | \"stream_bootstrap\";\n\nexport type TurnStartupOutcome = \"completed\" | \"failed\";\nexport type TurnStartupMilestone = \"queue\" | \"provider_dispatch\" | \"first_byte\";\nexport type TurnStartupCache = \"hit\" | \"miss\" | \"disabled\" | \"none\";\nexport type TurnStartupCountBucket = \"0\" | \"1\" | \"2-5\" | \"6-20\" | \"21+\" | \"unknown\";\nexport type TurnSandboxEstablishPolicy = \"eager\" | \"on-demand\";\nexport type TurnSandboxEstablishReason =\n | \"eligible\"\n | \"lazy_disabled\"\n | \"machine_primary\"\n | \"backend_none\"\n | \"initial_run_credentials\"\n | \"initial_run_credentials_deferred\"\n | \"generated_video_files\"\n | \"signed_file_resources\";\nexport type SandboxLogicalProvisionCategory =\n | \"create\"\n | \"resume\"\n | \"exec_readiness\"\n | \"sibling_warming\"\n | \"lease_superseded\"\n | \"drain_capture_wait\"\n | \"archive_recovery\"\n | \"provider_transport\"\n | \"configuration\"\n | \"unknown\";\nexport type SandboxLogicalProvisionStage =\n | \"create\"\n | \"resume\"\n | \"exec_readiness\"\n | \"sibling_warming\"\n | \"lease_admission\"\n | \"lifecycle_wait\"\n | \"archive_recovery\"\n | \"configuration\"\n | \"provider_transport\"\n | \"unknown\";\nexport type SandboxLogicalProvisionOutcome = \"completed\" | \"expected_transition\" | \"failed\";\nexport type SandboxProvisionAttemptOutcome = \"completed\" | \"retrying\" | \"failed\";\n\nconst TURN_STARTUP_PHASE_BUCKETS = [\n 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120,\n];\nconst TURN_STARTUP_MILESTONE_BUCKETS = [\n ...TURN_STARTUP_PHASE_BUCKETS,\n 300,\n 600,\n 1_800,\n 3_600,\n 7_200,\n];\n\nexport function turnStartupCountBucket(count: number | null): TurnStartupCountBucket {\n if (count === null || !Number.isFinite(count) || count < 0) return \"unknown\";\n if (count === 0) return \"0\";\n if (count === 1) return \"1\";\n if (count <= 5) return \"2-5\";\n if (count <= 20) return \"6-20\";\n return \"21+\";\n}\n\n/**\n * Measure startup operations, including nested and parallel work. These are\n * not additive critical-path intervals; use milestones for elapsed latency.\n * Every label is a closed or configuration-derived enum; high-cardinality turn,\n * session, credential, connection, file, and model identifiers are forbidden.\n */\nexport function recordTurnStartupPhase(\n observability: Observability,\n input: {\n phase: TurnStartupPhase;\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n count?: number | null;\n cache?: TurnStartupCache;\n /** Trace-only lookup for the completed claim while its execution root is still open. */\n executionCorrelationId?: string;\n },\n): void {\n completedOperationSpan(observability, `worker.prepare.${input.phase}`, input.durationSeconds, {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n ...(input.phase === \"claim_and_policy\" &&\n /^turn_[0-9a-f]{32}$/.test(input.executionCorrelationId ?? \"\")\n ? { correlationId: input.executionCorrelationId }\n : {}),\n });\n observability.observeHistogram({\n name: \"opengeni_turn_startup_phase_duration_seconds\",\n help: \"Turn startup phase duration before the model response stream begins.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n phase: input.phase,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n count_bucket: turnStartupCountBucket(input.count ?? null),\n cache: input.cache ?? \"none\",\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/** Observe an existing dependency only. Child intervals can overlap and are\n * not additive; observer failure must never change authority or error identity. */\nexport async function measureTurnStartupPhase<T>(\n observability: Observability,\n input: Omit<Parameters<typeof recordTurnStartupPhase>[1], \"durationSeconds\" | \"outcome\">,\n work: () => Promise<T>,\n): Promise<T> {\n const started = performance.now();\n let outcome: TurnStartupOutcome = \"failed\";\n try {\n const result = await work();\n outcome = \"completed\";\n return result;\n } finally {\n try {\n recordTurnStartupPhase(observability, {\n ...input,\n outcome,\n durationSeconds: (performance.now() - started) / 1_000,\n });\n } catch {\n // Telemetry is not an execution gate.\n }\n }\n}\n\n/** Background MCP preparation must not inflate startup phase distributions. */\nexport function recordToolPreparationPhase(\n observability: Observability,\n input: ToolPreparationPhaseMeasurement & { provider: string; backend: string },\n): void {\n if (input.execution === \"blocking\") {\n recordTurnStartupPhase(observability, {\n ...input,\n phase: `tool_${input.phase}`,\n });\n return;\n }\n completedOperationSpan(\n observability,\n `worker.tool_prepare.${input.phase}`,\n input.durationSeconds,\n {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n );\n observability.observeHistogram({\n name: \"opengeni_tool_background_preparation_duration_seconds\",\n help: \"Nonblocking MCP preparation operations; may overlap startup and later execution.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n phase: input.phase,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/** Completed measurements become siblings under the scoped physical attempt.\n * They never establish ambient ancestry for work which has already finished. */\nfunction completedOperationSpan(\n observability: Observability,\n name: string,\n durationSeconds: number,\n attributes: {\n outcome: string;\n provider?: string;\n backend?: string;\n tool?: string;\n correlationId?: string | undefined;\n },\n): void {\n if (!Number.isFinite(durationSeconds) || durationSeconds < 0) return;\n try {\n const failed = [\n \"failed\",\n \"error\",\n \"provider_declared_error\",\n \"auth_needed\",\n \"outcome_uncertain\",\n \"timeout\",\n \"thrown_transport_error\",\n \"thrown_protocol_error\",\n ].includes(attributes.outcome);\n observability\n .startSpan(name, attributes, { startTimeMs: Date.now() - durationSeconds * 1_000 })\n .end({\n ...(failed ? { error: true } : {}),\n });\n } catch {\n // Observers cannot change the completed model/tool/phase outcome.\n }\n}\n\n/**\n * Explain why a turn did or did not keep provider sandbox work off the\n * pre-model critical path. The reason is a closed enum: never attach session,\n * sandbox, credential, or file identifiers here.\n */\nexport function recordTurnSandboxEstablishPolicy(\n observability: Observability,\n input: {\n policy: TurnSandboxEstablishPolicy;\n reason: TurnSandboxEstablishReason;\n backend: string;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_turn_sandbox_establish_policy_total\",\n help: \"Turn sandbox establish policy decisions by bounded reason.\",\n labels: {\n policy: input.policy,\n reason: input.reason,\n backend: input.backend,\n },\n });\n}\n\n/** Physical establish/resume attempts inside one logical provision. Safe\n * lifecycle retries are counted here, never as another user-visible failure. */\nexport function recordSandboxProvisionAttempt(\n observability: Observability,\n input: {\n backend: string;\n stage: SandboxLogicalProvisionStage;\n category: SandboxLogicalProvisionCategory;\n outcome: SandboxProvisionAttemptOutcome;\n durationSeconds: number;\n },\n): void {\n const labels = {\n backend: input.backend,\n stage: input.stage,\n category: input.category,\n outcome: input.outcome,\n };\n try {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provision_attempts_total\",\n help: \"Internal physical attempts within logical sandbox provisions.\",\n labels,\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_attempt_duration_seconds\",\n help: \"Internal sandbox provision attempt duration by bounded structural outcome.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels,\n value: Math.max(0, input.durationSeconds),\n });\n } catch {\n try {\n observability.incrementCounter({\n name: \"opengeni_observability_observer_errors_total\",\n help: \"Observability observer failures isolated from product execution.\",\n labels: { observer: \"sandbox_provision\" },\n });\n } catch {\n // The registry itself is unhealthy; provisioning remains authoritative.\n }\n }\n}\n\n/** One terminal observation for one correlation-qualified logical provision. */\nexport function recordSandboxLogicalProvision(\n observability: Observability,\n input: {\n backend: string;\n stage: SandboxLogicalProvisionStage;\n category: SandboxLogicalProvisionCategory | \"none\";\n outcome: SandboxLogicalProvisionOutcome;\n expected: boolean;\n internalAttempts: number;\n durationSeconds: number;\n },\n): void {\n const labels = {\n backend: input.backend,\n stage: input.stage,\n category: input.category,\n outcome: input.outcome,\n expected: input.expected ? \"true\" : \"false\",\n };\n try {\n observability.incrementCounter({\n name: \"opengeni_sandbox_provisions_total\",\n help: \"Logical sandbox provisions by terminal outcome and bounded failure taxonomy.\",\n labels,\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_duration_seconds\",\n help: \"Logical sandbox provision duration including internal safe retries.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels,\n value: Math.max(0, input.durationSeconds),\n });\n observability.observeHistogram({\n name: \"opengeni_sandbox_provision_internal_attempts\",\n help: \"Internal physical attempts per terminal logical sandbox provision.\",\n buckets: [1, 2, 3, 4, 5, 8],\n labels,\n value: Math.max(1, input.internalAttempts),\n });\n } catch {\n try {\n observability.incrementCounter({\n name: \"opengeni_observability_observer_errors_total\",\n help: \"Observability observer failures isolated from product execution.\",\n labels: { observer: \"sandbox_provision\" },\n });\n } catch {\n // The registry itself is unhealthy; provisioning remains authoritative.\n }\n }\n}\n\nexport function recordSandboxSharedPreparation(\n observability: Observability,\n measurement: {\n path: \"owner\" | \"joined\" | \"reused\";\n outcome: \"completed\" | \"failed\";\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_sandbox_shared_preparation_duration_seconds\",\n help: \"Duration of exact-lease immutable sandbox preparation by durable coordination path and outcome.\",\n labels: { path: measurement.path, outcome: measurement.outcome },\n value: Math.max(0, measurement.durationSeconds),\n });\n}\n\nexport function recordTurnWorkerPreparationTotal(\n observability: Observability,\n input: {\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_turn_worker_preparation_duration_seconds\",\n help: \"Worker preparation from the durable turn-start boundary until entering the runtime; lazy SDK request preparation and model-request audit are separate phases.\",\n buckets: TURN_STARTUP_PHASE_BUCKETS,\n labels: {\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\n value: Math.max(0, input.durationSeconds),\n });\n}\n\n/**\n * Measure cumulative user-visible startup latency from the durable turn queue\n * timestamp to a bounded milestone. Unlike the per-phase histogram above,\n * these samples are end-to-end and can therefore back real queue/dispatch/TTFB\n * SLOs without adding turn or session identifiers to Prometheus.\n */\nexport function recordTurnStartupMilestone(\n observability: Observability,\n input: {\n milestone: TurnStartupMilestone;\n provider: string;\n backend: string;\n outcome: TurnStartupOutcome;\n durationSeconds: number;\n },\n): void {\n observability.observeHistogram({\n name: \"opengeni_turn_startup_milestone_duration_seconds\",\n help: \"Cumulative seconds from durable turn queueing to a bounded startup milestone.\",\n buckets: TURN_STARTUP_MILESTONE_BUCKETS,\n labels: {\n milestone: input.milestone,\n provider: input.provider,\n backend: input.backend,\n outcome: input.outcome,\n },\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 lives on a human-perceptible scale, but reasoning models legitimately\n// think for tens of seconds before their first streamed token. The finite\n// buckets therefore run well past 10s: a top bucket at 10s made every slower\n// first token land in +Inf, so p50/p99 saturated at exactly \"10\" and hid how\n// slow the tail really was.\nconst STREAM_TTFT_BUCKETS = [\n 0.02, 0.05, 0.1, 0.2, 0.35, 0.5, 0.75, 1, 1.5, 2, 3, 5, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300,\n];\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// Opengeni's own per-request work before the provider sees bytes: admission,\n// durable history/audit checkpoints, request build. Normally milliseconds to a\n// second; tens of seconds means our database or consumer is the bottleneck.\nconst MODEL_REQUEST_PRE_DISPATCH_BUCKETS = [\n 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120,\n];\n\n/** First streamed content of a provider response. `any` is the first reasoning\n * or answer delta; `text` is the first answer-text delta. They overlap — never\n * sum across `content`. */\nexport type ProviderFirstContent = \"any\" | \"text\";\n\n/**\n * Per-stream timing tracker fed every normalized runtime event in push order,\n * plus two producer-side hooks. It separates OUR latency from the PROVIDER's:\n *\n * - `opengeni_stream_ttft_seconds{provider}` — user-perceived (re)start latency:\n * time from a stream start / structural boundary (tool call, tool result,\n * completed message, usage frame) to the first streamed content delta. It\n * mixes our between-call work with provider time; keep it for the absolute\n * dashboard view, not for provider alerting.\n * - `opengeni_model_request_pre_dispatch_seconds{provider}` — OUR per-request\n * latency: SDK model entry (the first admission check) to the literal\n * provider dispatch. Covers admission, the durable history checkpoint,\n * credit revalidation, the durable request audit and request build.\n * - `opengeni_model_provider_ttft_seconds{provider,content}` — the PROVIDER's\n * latency: literal dispatch to the first streamed `any` (reasoning or text)\n * delta and to the first answer `text` delta. Includes network, provider\n * queueing, prompt prefill and reasoning before the first streamed token.\n * - `opengeni_stream_inter_delta_gap_seconds{provider,class}` — gap between\n * consecutive content deltas of the SAME class, reset at every structural\n * event so a gap never spans a tool call or a model boundary.\n *\n * Dispatch timing deliberately survives structural events: the consumer may\n * still be persisting the previous response's tool results when the producer\n * dispatches the next request, but the next request's first delta always\n * follows its own dispatch. Purely observational and clock-injectable.\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 private modelEntryAt: number | null = null;\n private dispatchedAt: number | null = null;\n private readonly providerFirstRecorded = new Set<ProviderFirstContent>();\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 /** Producer side: the SDK entered a model request (first admission check).\n * Admission can be re-entered for the same request; keep the earliest. */\n onModelRequestEntry(): void {\n if (this.modelEntryAt === null) this.modelEntryAt = this.now();\n }\n\n /** Producer side: request bytes are about to leave this process. */\n onProviderDispatch(): void {\n const at = this.now();\n if (this.modelEntryAt !== null) {\n this.observability.observeHistogram({\n name: \"opengeni_model_request_pre_dispatch_seconds\",\n help: \"Seconds of Opengeni work from SDK model-request entry to literal provider dispatch.\",\n buckets: MODEL_REQUEST_PRE_DISPATCH_BUCKETS,\n labels: { provider: this.options.provider },\n value: Math.max(0, (at - this.modelEntryAt) / 1000),\n });\n this.modelEntryAt = null;\n }\n // A transport retry re-dispatches the same request; the first delta then\n // belongs to the latest dispatch.\n this.dispatchedAt = at;\n this.providerFirstRecorded.clear();\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 stream start or structural boundary to the next streamed content delta (user-perceived; includes Opengeni between-call work).\",\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 if (this.dispatchedAt !== null) {\n this.observeProviderFirst(\"any\", at);\n if (deltaClass === \"message\") this.observeProviderFirst(\"text\", at);\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 private observeProviderFirst(content: ProviderFirstContent, at: number): void {\n if (this.dispatchedAt === null || this.providerFirstRecorded.has(content)) return;\n this.providerFirstRecorded.add(content);\n this.observability.observeHistogram({\n name: \"opengeni_model_provider_ttft_seconds\",\n help: \"Seconds from literal provider dispatch to the first streamed content delta (any = reasoning or text, text = answer text).\",\n buckets: STREAM_TTFT_BUCKETS,\n labels: { provider: this.options.provider, content },\n value: Math.max(0, (at - this.dispatchedAt) / 1000),\n });\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\nexport function sessionEventBatchSizeClass(eventCount: number): string {\n if (!Number.isFinite(eventCount) || eventCount <= 0) return \"unknown\";\n if (eventCount === 1) return \"1\";\n if (eventCount <= 5) return \"2-5\";\n if (eventCount <= 10) return \"6-10\";\n if (eventCount <= 25) return \"11-25\";\n if (eventCount <= 50) return \"26-50\";\n return \"51+\";\n}\n\n/** Bounded database-phase attribution for exact-attempt event appends. */\nexport function recordSessionEventAppendPhase(\n observability: Observability,\n observation: SessionEventAppendPhaseObservation,\n): void {\n observability.observeHistogram({\n name: \"opengeni_session_event_append_phase_seconds\",\n help: \"Duration of one bounded database phase in an exact-attempt session event append.\",\n buckets: STREAM_IO_BUCKETS,\n labels: {\n path: \"turn_attempt\",\n event_class: observation.eventClass,\n batch_size_class: sessionEventBatchSizeClass(observation.eventCount),\n phase: observation.phase,\n outcome: observation.outcome,\n },\n value: Math.max(0, observation.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 retain context-pressure\n// diagnostics while compaction alerting follows the durable model-aware start.\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 for context-pressure\n * dashboards and provider diagnostics. */\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 successfully completed, by bounded trigger. */\nexport function recordContextCompaction(\n observability: Observability,\n trigger: ContextCompactionTrigger,\n): void {\n observability.incrementCounter({ ...CONTEXT_COMPACTIONS_METRIC, labels: { trigger } });\n}\n\n/**\n * A durable context-compaction start, recorded only after the attempt-fenced\n * `compaction.started` transaction commits. This is the model-aware threshold\n * signal; it does not guess from a static token count shared by unlike models.\n */\nexport function recordContextCompactionStarted(\n observability: Observability,\n trigger: ContextCompactionTrigger,\n): void {\n observability.incrementCounter({ ...CONTEXT_COMPACTION_STARTS_METRIC, labels: { trigger } });\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: \"Agent Knowledge 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 Agent Knowledge 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 // protobuf Long is supported through its string representation. Refuse JS\n // coercions (booleans, arrays, blanks) and hostile toString implementations.\n let raw: unknown = value;\n try {\n if (typeof raw === \"object\" && raw !== null && !Array.isArray(raw)) raw = raw.toString();\n } catch {\n throw new Error(`Temporal stats returned invalid ${field}`);\n }\n const parsed =\n typeof raw === \"number\"\n ? raw\n : typeof raw === \"string\" && /^\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/.test(raw)\n ? Number(raw)\n : Number.NaN;\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}`);\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\nexport type CodeSearchCallOutcome =\n | \"completed\"\n | \"jev_unavailable\"\n | \"jev_rejected\"\n | \"workspace_unavailable\"\n | \"invalid_arguments\"\n | \"breaker_open\"\n | \"cancelled\"\n | \"failed\";\n\n/** One `code_search` tool call: outcome, wall time and the Jev work it used. */\nexport function recordCodeSearchCall(\n observability: Observability,\n input: {\n outcome: CodeSearchCallOutcome;\n durationSeconds: number;\n jevRequests: number;\n jevCostUsd: number;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_code_search_calls_total\",\n help: \"Jev-backed code_search tool calls by outcome.\",\n labels: { outcome: input.outcome },\n });\n observability.observeHistogram({\n name: \"opengeni_code_search_duration_seconds\",\n help: \"Wall time of one code_search tool call.\",\n buckets: [0.5, 1, 2, 4, 8, 15, 30, 60],\n labels: { outcome: input.outcome },\n value: Math.max(0, input.durationSeconds),\n });\n if (input.jevRequests > 0) {\n observability.incrementCounter({\n name: \"opengeni_code_search_jev_requests_total\",\n help: \"Jev requests made by code_search.\",\n amount: input.jevRequests,\n });\n }\n if (input.jevCostUsd > 0) {\n observability.incrementCounter({\n name: \"opengeni_code_search_jev_cost_micro_usd_total\",\n help: \"Estimated Jev list-price cost of code_search, in micro-USD.\",\n amount: Math.round(input.jevCostUsd * 1_000_000),\n });\n }\n}\n\n/**\n * One skill_read call. Labels are closed sets: `skill` is a built-in id or\n * `custom` for every other Skill, so tenant Skill ids, names, and requested\n * identifiers never become labels. `source` is `unknown` when the read was\n * refused before a Skill resolved.\n */\nexport function recordSkillRead(\n observability: Observability,\n read: {\n caller: \"model\" | \"codemode\";\n kind: SkillReadKind;\n source: SkillUseSource | null;\n /** The resolved Skill id, or the requested identifier when none resolved. */\n skill: string;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_skill_reads_total\",\n help: \"skill_read calls by Skill source, built-in Skill id (custom for any other Skill), result kind, and caller.\",\n labels: {\n source: read.source ?? \"unknown\",\n skill: skillReadMetricLabel(read.source, read.skill),\n kind: read.kind,\n caller: read.caller,\n },\n });\n}\n\n/**\n * One skill_checkout call. Labels are closed sets and never carry Skill ids,\n * names, or paths. Phases that did not run are not observed.\n */\nexport function recordSkillCheckout(\n observability: Observability,\n checkout: {\n outcome: \"written\" | \"unchanged\" | \"refused\" | \"failed\";\n selection: \"all\" | \"paths\";\n written: number;\n unchanged: number;\n resolveSeconds: number | null;\n sandboxSeconds: number | null;\n writeSeconds: number | null;\n totalSeconds: number;\n },\n): void {\n observability.incrementCounter({\n name: \"opengeni_skill_checkouts_total\",\n help: \"skill_checkout calls by outcome and whether they selected specific paths.\",\n labels: { outcome: checkout.outcome, selection: checkout.selection },\n });\n const phases = [\n [\"resolve\", checkout.resolveSeconds],\n [\"sandbox\", checkout.sandboxSeconds],\n [\"write\", checkout.writeSeconds],\n [\"total\", checkout.totalSeconds],\n ] as const;\n for (const [phase, seconds] of phases) {\n if (seconds === null) continue;\n observability.observeHistogram({\n name: \"opengeni_skill_checkout_duration_seconds\",\n help: \"Wall time of skill_checkout phases: resolve (authority and Skill read), sandbox (filesystem handle, including lazy box start), write (batched file write), and total.\",\n buckets: [0.05, 0.1, 0.25, 0.5, 1, 2, 4, 8, 15, 30, 60],\n labels: { phase, outcome: checkout.outcome },\n value: Math.max(0, seconds),\n });\n }\n for (const [result, amount] of [\n [\"written\", checkout.written],\n [\"unchanged\", checkout.unchanged],\n ] as const) {\n if (amount <= 0) continue;\n observability.incrementCounter({\n name: \"opengeni_skill_checkout_files_total\",\n help: \"Files skill_checkout wrote or found already present with the same content.\",\n labels: { result },\n amount,\n });\n }\n}\n\nfunction skillReadMetricLabel(source: SkillUseSource | null, skill: string): string {\n if (source !== null && source !== \"builtin\") return \"custom\";\n // A refused read may name a built-in by its plain name.\n for (const candidate of source === null ? [skill, `builtin:${skill}`] : [skill]) {\n const parsed = BundledSkillId.safeParse(candidate);\n if (parsed.success) return parsed.data;\n }\n return \"custom\";\n}\n","/** Small injectable clock edges for deterministic telemetry lifecycle tests. */\nexport type TelemetryScheduler = {\n timeout(callback: () => void, delayMs: number): () => void;\n interval(callback: () => void, intervalMs: number): () => void;\n defer(callback: () => void): void;\n};\n\nexport const telemetryScheduler: TelemetryScheduler = {\n timeout(callback, delayMs) {\n const timer = setTimeout(callback, delayMs);\n timer.unref?.();\n return () => clearTimeout(timer);\n },\n interval(callback, intervalMs) {\n const timer = setInterval(callback, intervalMs);\n timer.unref?.();\n return () => clearInterval(timer);\n },\n defer: queueMicrotask,\n};\n\nexport function telemetryInterval(value: number, name: string): number {\n if (!Number.isSafeInteger(value) || value < 1) {\n throw new Error(`${name} must be a positive integer`);\n }\n return value;\n}\n\n/** Bad/regressing clocks are unknown, not a new timestamp or a zero age.\n * Retain the last valid high-water observation for diagnostic age reporting. */\nexport function createTelemetryClock(now: () => number) {\n let latest: number | null = null;\n return {\n read(): number | null {\n let observed: number;\n try {\n observed = now();\n } catch {\n return null;\n }\n if (\n !Number.isFinite(observed) ||\n observed < 0 ||\n observed > Number.MAX_SAFE_INTEGER ||\n (latest !== null && observed < latest)\n )\n return null;\n latest = observed;\n return observed;\n },\n latest: () => latest,\n };\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,WAAW,oBAAoB;AAExC,IAAM,qBAAqB;AAC3B,IAAM,wBAAwB;AAC9B,IAAM,qBAAqB;AAC3B,IAAM,gCAAgC;AACtC,IAAM,gCAAgC,KAAK,OAAO;AAClD,IAAM,4BAA4B;AAClC,IAAM,4BAA4B;AAE3B,IAAM,oCAAoC;AAC1C,IAAM,yCAAyC;AAE/C,IAAM,kCAAkC;AAAA,EAC7C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAIO,IAAM,sCAAsC;AAAA,EACjD;AAAA,EACA;AAAA,EACA;AACF;AAeA,IAAM,qBAAqB,oBAAI,IAAI,CAAC,gBAAgB,oBAAoB,kBAAkB,CAAC;AAE3F,eAAsB,sCAAsC,OAOhB;AAC1C,QAAM,YAAY,oBAAoB,MAAM,SAAS;AACrD,QAAM,QAAQ,MAAM,SAAS,KAAK,IAAI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