@opengeni/worker-bundle 0.23.4 → 0.24.2-canary.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 (84) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/claim.d.ts +1 -0
  3. package/dist/activities/agent-turn/codex.d.ts +2 -2
  4. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  5. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  6. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  7. package/dist/activities/agent-turn/run.d.ts +9 -0
  8. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  9. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  10. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  11. package/dist/activities/agent-turn/tool-environment.d.ts +2 -0
  12. package/dist/activities/agent-turn/tool-policy.d.ts +36 -5
  13. package/dist/activities/agent-turn/turn-context.d.ts +4 -1
  14. package/dist/activities/agent-turn.d.ts +1 -1
  15. package/dist/activities/automations.d.ts +1 -0
  16. package/dist/activities/capabilities.d.ts +3 -1
  17. package/dist/activities/context-compaction.d.ts +2 -0
  18. package/dist/activities/goals.d.ts +11 -0
  19. package/dist/activities/image-generation-references.d.ts +2 -1
  20. package/dist/activities/scheduled-tasks.d.ts +1 -0
  21. package/dist/activities/types.d.ts +8 -0
  22. package/dist/{activities-control-52W5IDWM.js → activities-control-ZWH7J4KZ.js} +297 -180
  23. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  24. package/dist/activities-control.d.ts +2 -0
  25. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-I4OPLQLS.js} +898 -333
  26. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  27. package/dist/activities.d.ts +2 -0
  28. package/dist/{chunk-GFNXVZVQ.js → chunk-65JEWP2Q.js} +107 -7
  29. package/dist/chunk-65JEWP2Q.js.map +1 -0
  30. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  31. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  32. package/dist/{chunk-IPVMCAW5.js → chunk-RQNH6CGR.js} +111 -62
  33. package/dist/chunk-RQNH6CGR.js.map +1 -0
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.js +43 -7
  36. package/dist/index.js.map +1 -1
  37. package/dist/observability-metrics.d.ts +34 -7
  38. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  39. package/dist/sandbox-resume.d.ts +6 -1
  40. package/dist/workflow-bundle.js +26 -3
  41. package/dist/workflows/activities.d.ts +5 -0
  42. package/dist/workflows/automations.d.ts +5 -0
  43. package/package.json +17 -17
  44. package/src/activities/agent-run-admission.ts +7 -1
  45. package/src/activities/agent-turn/admission.ts +47 -6
  46. package/src/activities/agent-turn/claim.ts +33 -5
  47. package/src/activities/agent-turn/codex.ts +2 -2
  48. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  49. package/src/activities/agent-turn/finalization.ts +31 -17
  50. package/src/activities/agent-turn/list-models.ts +101 -0
  51. package/src/activities/agent-turn/model-usage.ts +120 -45
  52. package/src/activities/agent-turn/quiescence.ts +11 -2
  53. package/src/activities/agent-turn/run.ts +80 -4
  54. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  55. package/src/activities/agent-turn/session-title.ts +55 -2
  56. package/src/activities/agent-turn/stream-attempt.ts +266 -135
  57. package/src/activities/agent-turn/tool-environment.ts +102 -7
  58. package/src/activities/agent-turn/tool-policy.ts +58 -5
  59. package/src/activities/agent-turn/turn-context.ts +8 -2
  60. package/src/activities/agent-turn.ts +5 -1
  61. package/src/activities/api-integrations.ts +11 -1
  62. package/src/activities/automations.ts +122 -67
  63. package/src/activities/capabilities.ts +129 -2
  64. package/src/activities/context-compaction.ts +3 -0
  65. package/src/activities/goals.ts +108 -31
  66. package/src/activities/image-generation-references.ts +9 -9
  67. package/src/activities/mcp-credentials.ts +53 -0
  68. package/src/activities/rig-verification.ts +5 -3
  69. package/src/activities/scheduled-tasks.ts +217 -110
  70. package/src/activities/site-auth-maintenance.ts +12 -12
  71. package/src/activities/types.ts +6 -0
  72. package/src/activities.ts +1 -0
  73. package/src/activity-services.ts +4 -1
  74. package/src/github-rest-mcp.ts +2 -1
  75. package/src/index.ts +34 -4
  76. package/src/observability-metrics.ts +154 -14
  77. package/src/sandbox-reaper-timeout.ts +10 -4
  78. package/src/sandbox-resume.ts +58 -8
  79. package/src/workflows/activities.ts +15 -0
  80. package/src/workflows/automations.ts +21 -2
  81. package/dist/activities-control-52W5IDWM.js.map +0 -1
  82. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  83. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  84. package/dist/chunk-IPVMCAW5.js.map +0 -1
@@ -15,11 +15,13 @@ import {
15
15
  } from "@opengeni/runtime";
16
16
  import {
17
17
  calculateGatewayReportedCostBreakdown,
18
+ calculateGatewayReportedProviderCostMicros,
18
19
  calculateModelUsageCostBreakdown,
19
20
  configuredModelPricingSchedules,
21
+ resolveModelProvider,
20
22
  responseSatisfiesLatencyMode,
21
- OPENGENI_GATEWAY_MODELS,
22
23
  OPENGENI_GATEWAY_PROVIDER_ID,
24
+ WORKSPACE_GATEWAY_PROVIDER_ID,
23
25
  type ModelUsageInput,
24
26
  type ModelProviderApi,
25
27
  type Settings,
@@ -123,6 +125,8 @@ export function createCompactionModelUsageEventState(
123
125
  return { usageCount: 0, claimedSourceKeys };
124
126
  }
125
127
 
128
+ export const createSessionTitleModelUsageEventState = createCompactionModelUsageEventState;
129
+
126
130
  export function modelResponseContextSignal(
127
131
  state: ModelResponseEventState,
128
132
  ): { revision: number; totalTokens: number } | null {
@@ -191,6 +195,8 @@ export async function processModelResponseTerminalEvent(input: {
191
195
  latencyMode?: LatencyMode;
192
196
  metricProvider: string;
193
197
  externallyBilled: boolean;
198
+ chargesOpenGeniCredits?: boolean;
199
+ countsTowardTokenCap?: boolean;
194
200
  servingCredentialId: string | null;
195
201
  priorSessionCredentialId: string | null;
196
202
  emittedSourceKeys: Set<string>;
@@ -251,9 +257,14 @@ export async function processModelResponseTerminalEvent(input: {
251
257
  turnAttemptId: input.turnAttemptId,
252
258
  model: input.model,
253
259
  externallyBilled: input.externallyBilled,
260
+ ...(input.chargesOpenGeniCredits !== undefined
261
+ ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
262
+ : {}),
263
+ ...(input.countsTowardTokenCap !== undefined
264
+ ? { countsTowardTokenCap: input.countsTowardTokenCap }
265
+ : {}),
254
266
  usage: responseUsage.usage,
255
267
  normalizedUsage,
256
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
257
268
  gatewayBilling: responseUsage.gatewayBilling,
258
269
  sourceKey,
259
270
  ...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
@@ -272,6 +283,8 @@ export async function processModelResponseTerminalEvent(input: {
272
283
  sourceKey,
273
284
  usage: responseUsage,
274
285
  normalizedUsage,
286
+ ...(billing ? { billingPath: billing.billingPath } : {}),
287
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
275
288
  servingAccountHash: accountContext.servingAccountHash,
276
289
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
277
290
  emittedSourceKeys: input.emittedSourceKeys,
@@ -331,6 +344,7 @@ export async function processModelResponseTerminalEvent(input: {
331
344
  export async function processCompactionModelUsageEvent(input: {
332
345
  usage: ModelResponseUsage;
333
346
  state: CompactionModelUsageEventState;
347
+ sourceKind?: "compaction" | "session-title";
334
348
  dispatchId: string | null;
335
349
  settings: Settings;
336
350
  db: ActivityServices["db"];
@@ -345,6 +359,8 @@ export async function processCompactionModelUsageEvent(input: {
345
359
  providerApi: ModelProviderApi;
346
360
  model: string;
347
361
  externallyBilled: boolean;
362
+ chargesOpenGeniCredits?: boolean;
363
+ countsTowardTokenCap?: boolean;
348
364
  servingCredentialId: string | null;
349
365
  priorSessionCredentialId: string | null;
350
366
  emittedSourceKeys: Set<string>;
@@ -360,7 +376,7 @@ export async function processCompactionModelUsageEvent(input: {
360
376
  const sourceKey = modelUsageSourceKey({
361
377
  responseId: input.usage.responseId,
362
378
  dispatchId: input.dispatchId,
363
- positionalKey: `compaction-${usageOrdinal}`,
379
+ positionalKey: `${input.sourceKind ?? "compaction"}-${usageOrdinal}`,
364
380
  });
365
381
  if (input.state.claimedSourceKeys.has(sourceKey)) {
366
382
  return { status: "duplicate", sourceKey };
@@ -388,9 +404,14 @@ export async function processCompactionModelUsageEvent(input: {
388
404
  turnAttemptId: input.turnAttemptId,
389
405
  model: input.model,
390
406
  externallyBilled: input.externallyBilled,
407
+ ...(input.chargesOpenGeniCredits !== undefined
408
+ ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
409
+ : {}),
410
+ ...(input.countsTowardTokenCap !== undefined
411
+ ? { countsTowardTokenCap: input.countsTowardTokenCap }
412
+ : {}),
391
413
  usage: input.usage.usage,
392
414
  normalizedUsage,
393
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
394
415
  gatewayBilling: input.usage.gatewayBilling,
395
416
  sourceKey,
396
417
  observability: input.observability,
@@ -408,6 +429,8 @@ export async function processCompactionModelUsageEvent(input: {
408
429
  sourceKey,
409
430
  usage: input.usage,
410
431
  normalizedUsage,
432
+ ...(billing ? { billingPath: billing.billingPath } : {}),
433
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
411
434
  servingAccountHash: accountContext.servingAccountHash,
412
435
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
413
436
  emittedSourceKeys: input.emittedSourceKeys,
@@ -436,6 +459,15 @@ export async function processCompactionModelUsageEvent(input: {
436
459
  return { status: "processed", sourceKey, authoritative };
437
460
  }
438
461
 
462
+ export async function processSessionTitleModelUsageEvent(
463
+ input: Omit<Parameters<typeof processCompactionModelUsageEvent>[0], "sourceKind">,
464
+ ): ReturnType<typeof processCompactionModelUsageEvent> {
465
+ return await processCompactionModelUsageEvent({
466
+ ...input,
467
+ sourceKind: "session-title",
468
+ });
469
+ }
470
+
439
471
  export async function emitModelCallUsage(input: {
440
472
  observability: ActivityServices["observability"];
441
473
  publish: TurnEventPublisher | null;
@@ -449,6 +481,10 @@ export async function emitModelCallUsage(input: {
449
481
  sourceKey: string;
450
482
  usage: ModelResponseUsage | { usage?: unknown | null } | null;
451
483
  normalizedUsage?: ModelCallUsageNormalization;
484
+ /** Accepted billing authority persisted for Insights repair after a soft fact-write failure. */
485
+ billingPath?: ModelUsageBillingRecord["billingPath"];
486
+ /** Validated Gateway endpoint provider persisted for exact Insights repair. */
487
+ upstreamProvider?: string;
452
488
  // Prompt-cache research dimensions (log-only; NEVER on a metric label or a
453
489
  // durable event). The opaque serving-account tag and whether it changed since
454
490
  // the session's previous call — the account-switch hypothesis for cache misses.
@@ -480,6 +516,8 @@ export async function emitModelCallUsage(input: {
480
516
  providerApi: input.providerApi,
481
517
  model: input.model,
482
518
  sourceKey: input.sourceKey,
519
+ ...(input.billingPath ? { billingPath: input.billingPath } : {}),
520
+ ...(input.upstreamProvider ? { upstreamProvider: input.upstreamProvider } : {}),
483
521
  ...telemetry,
484
522
  },
485
523
  },
@@ -555,6 +593,8 @@ export type ModelUsageBillingRecord = {
555
593
  pricedCostMicros: number;
556
594
  /** Hypothetical provider-rate USD micros; never an OpenGeni charge. */
557
595
  estimatedProviderCostMicros: number | null;
596
+ /** Hypothetical OpenGeni credit price at the captured rate; never a debit. */
597
+ equivalentCreditCostMicros: number | null;
558
598
  pricingSource: "configured_list_price" | "gateway_reported" | null;
559
599
  normalizedUsage: ModelCallUsageNormalization;
560
600
  upstreamProvider?: string;
@@ -572,7 +612,8 @@ export async function recordModelUsageAndDebitCredits(
572
612
  turnAttemptId: string;
573
613
  model: string;
574
614
  externallyBilled: boolean;
575
- gatewayManaged?: boolean;
615
+ chargesOpenGeniCredits?: boolean;
616
+ countsTowardTokenCap?: boolean;
576
617
  gatewayBilling?: ModelResponseUsage["gatewayBilling"];
577
618
  usage?: ModelUsageInput | ModelCallUsageInput | null;
578
619
  normalizedUsage?: ModelCallUsageNormalization;
@@ -589,19 +630,35 @@ export async function recordModelUsageAndDebitCredits(
589
630
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
590
631
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
591
632
  const totalTokens = sanitizedUsage.totalTokens ?? 0;
592
- const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : undefined;
633
+ const chargesOpenGeniCredits = input.chargesOpenGeniCredits ?? !input.externallyBilled;
634
+ const countsTowardTokenCap = input.countsTowardTokenCap ?? !input.externallyBilled;
635
+ const resolvedGatewayModel = input.gatewayBilling
636
+ ? resolveModelProvider(settings, input.model)
637
+ : undefined;
638
+ const gatewayProviderId = resolvedGatewayModel?.provider.id;
639
+ const gatewayBilling =
640
+ gatewayProviderId === OPENGENI_GATEWAY_PROVIDER_ID ||
641
+ gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID
642
+ ? input.gatewayBilling
643
+ : undefined;
644
+ const allowedProviders = resolvedGatewayModel?.model.requestPolicy?.gateway.only;
645
+ const unpinnedWorkspaceGatewayModel =
646
+ gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID && allowedProviders === undefined;
593
647
  if (gatewayBilling) {
594
- const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
595
- (candidate) => candidate.productId === input.model,
596
- );
597
648
  if (
598
- !gatewayModel ||
599
- !(gatewayModel.providers as readonly string[]).includes(gatewayBilling.finalProvider)
649
+ !unpinnedWorkspaceGatewayModel &&
650
+ (!allowedProviders ||
651
+ !(allowedProviders as readonly string[]).includes(gatewayBilling.finalProvider))
600
652
  ) {
601
653
  throw new Error(
602
654
  `AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`,
603
655
  );
604
656
  }
657
+ if (unpinnedWorkspaceGatewayModel && chargesOpenGeniCredits) {
658
+ throw new Error(
659
+ `Workspace Gateway custom model ${input.model} cannot charge OpenGeni credits without pinned pricing`,
660
+ );
661
+ }
605
662
  }
606
663
  const pricingSchedules = configuredModelPricingSchedules(settings);
607
664
  const configuredPricingModel = pricingSchedules[input.model]
@@ -610,12 +667,19 @@ export async function recordModelUsageAndDebitCredits(
610
667
  ? input.model.slice("codex/".length)
611
668
  : null;
612
669
  const pricingBreakdown = gatewayBilling
613
- ? calculateGatewayReportedCostBreakdown(
614
- settings,
615
- input.model,
616
- gatewayBilling.inferenceCostUsd,
617
- { inputTokens },
618
- )
670
+ ? unpinnedWorkspaceGatewayModel
671
+ ? {
672
+ providerCostMicros: calculateGatewayReportedProviderCostMicros(
673
+ gatewayBilling.inferenceCostUsd,
674
+ ),
675
+ creditCostMicros: 0,
676
+ }
677
+ : calculateGatewayReportedCostBreakdown(
678
+ settings,
679
+ configuredPricingModel ?? input.model,
680
+ gatewayBilling.inferenceCostUsd,
681
+ { inputTokens },
682
+ )
619
683
  : configuredPricingModel
620
684
  ? calculateModelUsageCostBreakdown(settings, configuredPricingModel, sanitizedUsage, {
621
685
  latencyMode: input.latencyMode ?? "standard",
@@ -629,22 +693,38 @@ export async function recordModelUsageAndDebitCredits(
629
693
  : hasCompleteCoreTokenTelemetry
630
694
  ? (pricingBreakdown?.providerCostMicros ?? null)
631
695
  : null;
696
+ const equivalentCreditCostMicros =
697
+ pricingBreakdown && !unpinnedWorkspaceGatewayModel
698
+ ? gatewayBilling || hasCompleteCoreTokenTelemetry
699
+ ? pricingBreakdown.creditCostMicros
700
+ : null
701
+ : null;
632
702
  const pricingSource = gatewayBilling
633
703
  ? ("gateway_reported" as const)
634
704
  : estimatedProviderCostMicros !== null
635
705
  ? ("configured_list_price" as const)
636
706
  : null;
637
- // An externally billed turn is paid outside OpenGeni, so it
638
- // consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
639
- // codex/<slug> model has no entry in configuredModelPricing, so the normal path
640
- // below would throw "Missing model pricing". We:
641
- // - do NOT emit the cap-feeding `model.tokens` event (ensureRunAllowed and
642
- // the API tokens:consume cap sum `model.tokens` with NO cost dimension, so
643
- // any row would count against maxMonthlyTokensPerWorkspace);
644
- // - record a `model.cost = 0` audit marker (harmless to the monthly cost cap);
645
- // - optionally calculate a non-charging provider estimate for Insights when
646
- // core token telemetry is complete, but never debit credits.
647
- if (input.externallyBilled) {
707
+ // Provider settlement and workspace-facing cost are separate. Externally
708
+ // metered subscription/workspace turns remain exempt from the OpenGeni token
709
+ // cap, while a deployment-funded free model still records model.tokens. Every
710
+ // non-credit path records a zero-cost marker and never consults pricing for a
711
+ // debit.
712
+ if (countsTowardTokenCap && totalTokens > 0) {
713
+ await recordUsageEvent(db, {
714
+ accountId: input.accountId,
715
+ workspaceId: input.workspaceId,
716
+ eventType: "model.tokens",
717
+ quantity: totalTokens,
718
+ unit: "tokens",
719
+ sourceResourceType: "model_response",
720
+ sourceResourceId: `${input.turnId}:${input.sourceKey}`,
721
+ sessionId: input.sessionId,
722
+ turnId: input.turnId,
723
+ turnAttemptId: input.turnAttemptId,
724
+ idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
725
+ });
726
+ }
727
+ if (!chargesOpenGeniCredits) {
648
728
  await recordUsageEvent(db, {
649
729
  accountId: input.accountId,
650
730
  workspaceId: input.workspaceId,
@@ -662,31 +742,19 @@ export async function recordModelUsageAndDebitCredits(
662
742
  billingPath: "external",
663
743
  pricedCostMicros: 0,
664
744
  estimatedProviderCostMicros,
745
+ equivalentCreditCostMicros,
665
746
  pricingSource,
666
747
  normalizedUsage,
748
+ ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
667
749
  };
668
750
  }
669
- if (totalTokens > 0) {
670
- await recordUsageEvent(db, {
671
- accountId: input.accountId,
672
- workspaceId: input.workspaceId,
673
- eventType: "model.tokens",
674
- quantity: totalTokens,
675
- unit: "tokens",
676
- sourceResourceType: "model_response",
677
- sourceResourceId: `${input.turnId}:${input.sourceKey}`,
678
- sessionId: input.sessionId,
679
- turnId: input.turnId,
680
- turnAttemptId: input.turnAttemptId,
681
- idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
682
- });
683
- }
684
751
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
685
752
  if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
686
753
  return {
687
754
  billingPath: "opengeni_credits",
688
755
  pricedCostMicros: 0,
689
756
  estimatedProviderCostMicros,
757
+ equivalentCreditCostMicros,
690
758
  pricingSource,
691
759
  normalizedUsage,
692
760
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
@@ -740,6 +808,7 @@ export async function recordModelUsageAndDebitCredits(
740
808
  billingPath: "opengeni_credits",
741
809
  pricedCostMicros: costMicros,
742
810
  estimatedProviderCostMicros,
811
+ equivalentCreditCostMicros,
743
812
  pricingSource,
744
813
  normalizedUsage,
745
814
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
@@ -777,6 +846,7 @@ export async function recordAuthoritativeModelCallFact(input: {
777
846
  billingPath: input.billing.billingPath,
778
847
  pricedCostMicros: input.billing.pricedCostMicros,
779
848
  estimatedProviderCostMicros: input.billing.estimatedProviderCostMicros,
849
+ equivalentCreditCostMicros: input.billing.equivalentCreditCostMicros,
780
850
  pricingSource: input.billing.pricingSource,
781
851
  inputTokens: telemetry.inputTokens,
782
852
  outputTokens: telemetry.outputTokens,
@@ -804,10 +874,15 @@ export function sanitizedModelUsageInput(normalized: ModelCallUsageNormalization
804
874
  ? { outputTokens: normalized.telemetry.outputTokens }
805
875
  : {}),
806
876
  ...(normalized.totalTokens !== null ? { totalTokens: normalized.totalTokens } : {}),
807
- ...(normalized.telemetry.cachedTokens !== null
877
+ ...(normalized.telemetry.cachedTokens !== null || normalized.telemetry.cacheWriteTokens !== null
808
878
  ? {
809
879
  inputTokensDetails: {
810
- cached_tokens: normalized.telemetry.cachedTokens,
880
+ ...(normalized.telemetry.cachedTokens === null
881
+ ? {}
882
+ : { cached_tokens: normalized.telemetry.cachedTokens }),
883
+ ...(normalized.telemetry.cacheWriteTokens === null
884
+ ? {}
885
+ : { cache_write_tokens: normalized.telemetry.cacheWriteTokens }),
811
886
  },
812
887
  }
813
888
  : {}),
@@ -15,8 +15,13 @@ import { sleep } from "./sandbox-provision";
15
15
  export function shouldRunTurnEndWorkspacePersistence(input: {
16
16
  activityStatus: RunAgentTurnResult["status"] | "unknown";
17
17
  cancellationRequested: boolean;
18
+ deadlineRotationRequested?: boolean;
18
19
  }): boolean {
19
- return input.activityStatus !== "cancelled" && !input.cancellationRequested;
20
+ return (
21
+ input.activityStatus !== "cancelled" &&
22
+ !input.cancellationRequested &&
23
+ input.deadlineRotationRequested !== true
24
+ );
20
25
  }
21
26
 
22
27
  /**
@@ -29,9 +34,13 @@ export function shouldStartPeriodicWorkspaceSnapshot(input: {
29
34
  firstProviderRequestStarted: boolean;
30
35
  snapshotInFlight: boolean;
31
36
  turnEndCaptureInProgress: boolean;
37
+ deadlineRotationRequested: boolean;
32
38
  }): boolean {
33
39
  return (
34
- input.firstProviderRequestStarted && !input.snapshotInFlight && !input.turnEndCaptureInProgress
40
+ input.firstProviderRequestStarted &&
41
+ !input.snapshotInFlight &&
42
+ !input.turnEndCaptureInProgress &&
43
+ !input.deadlineRotationRequested
35
44
  );
36
45
  }
37
46
 
@@ -22,7 +22,10 @@ import {
22
22
  withCodexRequestOverrides,
23
23
  type CodexRequestContext,
24
24
  } from "@opengeni/codex";
25
- import { xaiSubscriptionRequestStorage } from "@opengeni/xai-subscription";
25
+ import {
26
+ xaiSubscriptionRequestStorage,
27
+ type XaiSubscriptionRequestContext,
28
+ } from "@opengeni/xai-subscription";
26
29
  import { buildXaiTurnRequestAuthorization } from "../xai-auth";
27
30
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
28
31
  import { currentActivityContext } from "../streaming";
@@ -79,6 +82,51 @@ import { prepareTurnToolPolicy, prepareTurnToolRuntime } from "./tool-environmen
79
82
  import { applyTurnGitHubRepositoryBindings } from "./github-repository-bindings";
80
83
  import { buildTurnAgent } from "./agent-build";
81
84
 
85
+ /**
86
+ * Retain subscription credential/account authority for the title sidecar
87
+ * without sharing main-stream recovery, startup, audit, or opaque-artifact
88
+ * callbacks. Title usage is returned by the runtime and metered separately.
89
+ */
90
+ export function sessionTitleCodexRequestContext(
91
+ context: CodexRequestContext,
92
+ nextRequestId: () => string,
93
+ ): CodexRequestContext {
94
+ return {
95
+ clientVersion: context.clientVersion,
96
+ ...(context.sessionId !== undefined ? { sessionId: context.sessionId } : {}),
97
+ getToken: context.getToken,
98
+ refresh: context.refresh,
99
+ resolveModel: context.resolveModel,
100
+ ...(context.onUsageHeaders ? { onUsageHeaders: context.onUsageHeaders } : {}),
101
+ ...(context.responseTimeoutPolicy
102
+ ? { responseTimeoutPolicy: context.responseTimeoutPolicy }
103
+ : {}),
104
+ nextRequestId,
105
+ turnMetadata: { request_kind: "session_title" },
106
+ };
107
+ }
108
+
109
+ export function sessionTitleXaiRequestContext(
110
+ context: XaiSubscriptionRequestContext,
111
+ nextRequestId: () => string,
112
+ ): XaiSubscriptionRequestContext {
113
+ return {
114
+ clientVersion: context.clientVersion,
115
+ sessionId: context.sessionId,
116
+ turnId: context.turnId,
117
+ getToken: context.getToken,
118
+ refresh: context.refresh,
119
+ resolveModel: context.resolveModel,
120
+ ...(context.streamIdleTimeoutMs !== undefined
121
+ ? { streamIdleTimeoutMs: context.streamIdleTimeoutMs }
122
+ : {}),
123
+ ...(context.hostedToolContinuationTimeoutMs !== undefined
124
+ ? { hostedToolContinuationTimeoutMs: context.hostedToolContinuationTimeoutMs }
125
+ : {}),
126
+ nextRequestId,
127
+ };
128
+ }
129
+
82
130
  /** Lifecycle orchestrator: claim → capacity → governance → sandbox → tools → stream. */
83
131
  export function createRunAgentTurnActivity(services: () => Promise<ActivityServices>) {
84
132
  const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
@@ -89,6 +137,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
89
137
  return async function runAgentTurn(input: RunAgentTurnInput): Promise<RunAgentTurnResult> {
90
138
  const {
91
139
  settings,
140
+ catalogSourceSettings = settings,
92
141
  db,
93
142
  bus,
94
143
  runtime,
@@ -302,6 +351,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
302
351
  const claimed = await claimTurnAttempt({
303
352
  input,
304
353
  settings,
354
+ catalogSourceSettings,
305
355
  db,
306
356
  bus,
307
357
  runtime,
@@ -778,6 +828,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
778
828
  providerTurn.xaiRequestContext
779
829
  ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn)
780
830
  : withCodex(fn);
831
+ let codexSessionTitleRequestSequence = 0;
832
+ let xaiSessionTitleRequestSequence = 0;
833
+ const codexSessionTitleContext = codexContext
834
+ ? sessionTitleCodexRequestContext(
835
+ codexContext,
836
+ () => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`,
837
+ )
838
+ : null;
839
+ const xaiSessionTitleContext = providerTurn.xaiRequestContext
840
+ ? sessionTitleXaiRequestContext(
841
+ providerTurn.xaiRequestContext,
842
+ () => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`,
843
+ )
844
+ : null;
845
+ const withSessionTitleProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
846
+ xaiSessionTitleContext
847
+ ? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn)
848
+ : codexSessionTitleContext
849
+ ? codexRequestStorage.run(codexSessionTitleContext, fn)
850
+ : fn();
781
851
  const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
782
852
  withCodex(() =>
783
853
  withCodexRequestOverrides(
@@ -796,7 +866,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
796
866
  );
797
867
  const compactionPrep = await prepareCompaction({
798
868
  input,
799
- settings,
869
+ settings: capabilitySettings,
800
870
  db,
801
871
  bus,
802
872
  observability,
@@ -1062,6 +1132,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1062
1132
  ];
1063
1133
  const toolRuntime = await prepareTurnToolRuntime({
1064
1134
  input,
1135
+ catalogSourceSettings,
1065
1136
  db,
1066
1137
  bus,
1067
1138
  runtime,
@@ -1101,6 +1172,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1101
1172
  const {
1102
1173
  attemptConnectorActionBindings,
1103
1174
  connectorActionIdentity,
1175
+ generateSessionTitleInParallel,
1104
1176
  postToolPreparationStartedAt,
1105
1177
  preparationIndependentToolNames,
1106
1178
  } = toolRuntime;
@@ -1243,7 +1315,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1243
1315
  };
1244
1316
  const postAgentCompaction = await runPostAgentCompaction({
1245
1317
  input,
1246
- settings,
1318
+ settings: capabilitySettings,
1247
1319
  db,
1248
1320
  bus,
1249
1321
  observability,
@@ -1408,8 +1480,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1408
1480
  );
1409
1481
  return await runTurnStreamAttempt({
1410
1482
  input,
1411
- settings,
1483
+ settings: capabilitySettings,
1412
1484
  db,
1485
+ bus,
1413
1486
  runtime,
1414
1487
  objectStorage,
1415
1488
  observability,
@@ -1440,6 +1513,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1440
1513
  attachCodemodeTokenRenewal,
1441
1514
  attachRunCredentialRenewal,
1442
1515
  withProviderRequestContext,
1516
+ withSessionTitleProviderRequestContext,
1443
1517
  publishCompactionLiveEvents,
1444
1518
  publishCompactionOutcomeEvents,
1445
1519
  recordCompanyBrainContributionReceiptOnce,
@@ -1465,6 +1539,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1465
1539
  providerApi,
1466
1540
  runSettings,
1467
1541
  turnTools,
1542
+ generateSessionTitleInParallel,
1543
+ sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
1468
1544
  compactSummarizer,
1469
1545
  settleDeferredSteerAfterCompaction,
1470
1546
  compactionModelHistoryProjector,