@opengeni/worker-bundle 0.23.4 → 0.26.0-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 (115) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +3 -10
  3. package/dist/activities/agent-turn/claim.d.ts +8 -0
  4. package/dist/activities/agent-turn/codex.d.ts +2 -2
  5. package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
  6. package/dist/activities/agent-turn/errors.d.ts +1 -1
  7. package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +11 -6
  11. package/dist/activities/agent-turn/run.d.ts +9 -0
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  13. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +4 -13
  16. package/dist/activities/agent-turn/tool-policy.d.ts +40 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +13 -2
  18. package/dist/activities/agent-turn.d.ts +1 -1
  19. package/dist/activities/automations.d.ts +1 -0
  20. package/dist/activities/capabilities.d.ts +3 -1
  21. package/dist/activities/codemode-dispatcher.d.ts +10 -1
  22. package/dist/activities/codex-capacity.d.ts +32 -3
  23. package/dist/activities/codex-fleet-shadow.d.ts +1 -1
  24. package/dist/activities/codex-image-generation.d.ts +1 -1
  25. package/dist/activities/codex-rotation.d.ts +8 -67
  26. package/dist/activities/context-compaction.d.ts +2 -0
  27. package/dist/activities/goals.d.ts +12 -1
  28. package/dist/activities/google-drive-publication.d.ts +2 -13
  29. package/dist/activities/image-generation-operation.d.ts +13 -4
  30. package/dist/activities/image-generation-references.d.ts +2 -1
  31. package/dist/activities/scheduled-tasks.d.ts +1 -0
  32. package/dist/activities/session-state.d.ts +4 -2
  33. package/dist/activities/types.d.ts +19 -1
  34. package/dist/{activities-control-52W5IDWM.js → activities-control-CTEMLTHG.js} +339 -195
  35. package/dist/activities-control-CTEMLTHG.js.map +1 -0
  36. package/dist/activities-control.d.ts +4 -0
  37. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-S5IU347A.js} +1846 -1136
  38. package/dist/activities-turn-S5IU347A.js.map +1 -0
  39. package/dist/activities.d.ts +4 -0
  40. package/dist/{chunk-GFNXVZVQ.js → chunk-D4ZEFOYH.js} +107 -7
  41. package/dist/chunk-D4ZEFOYH.js.map +1 -0
  42. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  43. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  44. package/dist/{chunk-IPVMCAW5.js → chunk-TAUE3GYT.js} +225 -154
  45. package/dist/chunk-TAUE3GYT.js.map +1 -0
  46. package/dist/github-rest-mcp.d.ts +0 -2
  47. package/dist/index.d.ts +2 -0
  48. package/dist/index.js +43 -7
  49. package/dist/index.js.map +1 -1
  50. package/dist/observability-metrics.d.ts +35 -8
  51. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  52. package/dist/sandbox-resume.d.ts +6 -1
  53. package/dist/workflow-bundle.js +96 -10
  54. package/dist/workflows/activities.d.ts +6 -1
  55. package/dist/workflows/automations.d.ts +5 -0
  56. package/dist/workflows/session.d.ts +3 -0
  57. package/package.json +17 -17
  58. package/src/activities/agent-run-admission.ts +7 -1
  59. package/src/activities/agent-turn/admission.ts +47 -6
  60. package/src/activities/agent-turn/agent-build.ts +20 -26
  61. package/src/activities/agent-turn/claim.ts +59 -6
  62. package/src/activities/agent-turn/codex-capacity.ts +311 -234
  63. package/src/activities/agent-turn/codex.ts +2 -2
  64. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  65. package/src/activities/agent-turn/credential-leases.ts +36 -0
  66. package/src/activities/agent-turn/errors.ts +29 -1
  67. package/src/activities/agent-turn/failure-settlement.ts +451 -323
  68. package/src/activities/agent-turn/finalization.ts +31 -17
  69. package/src/activities/agent-turn/list-models.ts +101 -0
  70. package/src/activities/agent-turn/model-usage.ts +120 -45
  71. package/src/activities/agent-turn/quiescence.ts +32 -17
  72. package/src/activities/agent-turn/run.ts +98 -9
  73. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  74. package/src/activities/agent-turn/session-title.ts +55 -2
  75. package/src/activities/agent-turn/stream-attempt.ts +281 -136
  76. package/src/activities/agent-turn/tool-environment.ts +132 -39
  77. package/src/activities/agent-turn/tool-policy.ts +71 -12
  78. package/src/activities/agent-turn/turn-context.ts +21 -2
  79. package/src/activities/agent-turn.ts +5 -1
  80. package/src/activities/automations.ts +122 -67
  81. package/src/activities/capabilities.ts +129 -2
  82. package/src/activities/codemode-dispatcher.ts +235 -59
  83. package/src/activities/codex-capacity.ts +89 -10
  84. package/src/activities/codex-fleet-shadow.ts +8 -1
  85. package/src/activities/codex-image-generation.ts +19 -2
  86. package/src/activities/codex-rotation.ts +45 -142
  87. package/src/activities/context-compaction.ts +3 -0
  88. package/src/activities/goals.ts +116 -39
  89. package/src/activities/google-drive-publication.ts +4 -81
  90. package/src/activities/image-generation-operation.ts +28 -3
  91. package/src/activities/image-generation-references.ts +9 -9
  92. package/src/activities/mcp-credentials.ts +53 -0
  93. package/src/activities/rig-verification.ts +5 -3
  94. package/src/activities/sandbox-lease.ts +29 -12
  95. package/src/activities/scheduled-tasks.ts +217 -110
  96. package/src/activities/session-state.ts +17 -0
  97. package/src/activities/site-auth-maintenance.ts +12 -12
  98. package/src/activities/types.ts +22 -8
  99. package/src/activities.ts +2 -0
  100. package/src/activity-services.ts +4 -1
  101. package/src/github-rest-mcp.ts +2 -83
  102. package/src/index.ts +34 -4
  103. package/src/observability-metrics.ts +158 -14
  104. package/src/sandbox-reaper-timeout.ts +10 -4
  105. package/src/sandbox-resume.ts +58 -8
  106. package/src/sandbox-routing.ts +71 -16
  107. package/src/workflows/activities.ts +16 -0
  108. package/src/workflows/automations.ts +21 -2
  109. package/src/workflows/session.ts +85 -7
  110. package/dist/activities/api-integrations.d.ts +0 -21
  111. package/dist/activities-control-52W5IDWM.js.map +0 -1
  112. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  113. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  114. package/dist/chunk-IPVMCAW5.js.map +0 -1
  115. package/src/activities/api-integrations.ts +0 -141
@@ -152,6 +152,23 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
152
152
  cancellationSignal,
153
153
  control.activityStatus,
154
154
  );
155
+ const drainInFlightWarmSnapshot = async (): Promise<void> => {
156
+ const snapshot = sandboxState.snapshotInFlight;
157
+ if (!snapshot) return;
158
+ // This is physical quiescence, not optional turn-end persistence. The
159
+ // provider capture SDK is not cancellable, so an aborted finalizer signal
160
+ // must not detach this wait and release the holder while the continuation
161
+ // is still reading or publishing the exact workspace generation.
162
+ await snapshot.catch((error) => {
163
+ console.error(
164
+ "in-flight workspace snapshot settlement failed (turn outcome unaffected)",
165
+ safeErrorDiagnostic(error),
166
+ );
167
+ });
168
+ if (sandboxState.snapshotInFlight === snapshot) {
169
+ sandboxState.snapshotInFlight = null;
170
+ }
171
+ };
155
172
  try {
156
173
  const toolCancellationFence = eventing.toolCancellationFenceRef.current;
157
174
  // Every renewal controller is an attempt-owned sandbox writer. Capture
@@ -468,6 +485,7 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
468
485
  const runTurnEndPersistence = shouldRunTurnEndWorkspacePersistence({
469
486
  activityStatus: control.activityStatus,
470
487
  cancellationRequested: finalizerSignal?.aborted === true,
488
+ deadlineRotationRequested: sandboxState.deadlineRotationRequested,
471
489
  });
472
490
  if (
473
491
  runTurnEndPersistence &&
@@ -565,11 +583,13 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
565
583
  if (sandboxState.leaseHeartbeatTimer) {
566
584
  stopLeaseHeartbeat();
567
585
  }
568
- if (sandboxState.rotationInFlight) {
569
- await waitForTurnFinalizerStep(sandboxState.rotationInFlight, finalizerSignal).catch(
570
- () => undefined,
571
- );
586
+ if (sandboxState.rotationPreemptionInFlight) {
587
+ await sandboxState.rotationPreemptionInFlight.catch(() => undefined);
572
588
  }
589
+ // No heartbeat can register another capture after this point. Join the
590
+ // actual provider continuation before either starting a turn-end capture
591
+ // or entering the proof-bearing holder release.
592
+ await drainInFlightWarmSnapshot();
573
593
  if (sandboxState.resolvedSandbox) {
574
594
  // TURN-END mid-session snapshot (sandbox-file-persistence): fold the
575
595
  // turn's finished /workspace onto the lease before releasing the holder,
@@ -585,19 +605,7 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
585
605
  sandboxState.sandboxGroupId &&
586
606
  settledTurnId
587
607
  ) {
588
- // Single-flight vs the heartbeat capture: the timer is already cleared
589
- // above, but a capture it launched may still be in flight — and that
590
- // capture predates the turn's final writes. Wait for it, but only up
591
- // to the snapshot timeout: release must never depend on an unbounded
592
- // provider capture.
593
- if (sandboxState.snapshotInFlight) {
594
- await waitForWarmSnapshot(
595
- sandboxState.snapshotInFlight,
596
- settings.sandboxSnapshotTimeoutMs,
597
- finalizerSignal,
598
- );
599
- }
600
- const persisted = await maybePersistWarmWorkspaceSnapshot(
608
+ const snapshot = maybePersistWarmWorkspaceSnapshot(
601
609
  { db, settings, objectStorage },
602
610
  {
603
611
  accountId: input.accountId,
@@ -611,6 +619,8 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
611
619
  sandboxState.resolvedSandbox.leaseEpoch,
612
620
  finalizerSignal,
613
621
  );
622
+ sandboxState.snapshotInFlight = snapshot.settled;
623
+ const persisted = await snapshot;
614
624
  if (persisted && eventing.publish) {
615
625
  await eventing
616
626
  .publish([
@@ -641,6 +651,10 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
641
651
  // Temporal cancellation: the eager listener may already have dropped
642
652
  // the holder, and this idempotent proof-bearing pass still must run.
643
653
  stopLeaseHeartbeat();
654
+ if (sandboxState.rotationPreemptionInFlight) {
655
+ await sandboxState.rotationPreemptionInFlight.catch(() => undefined);
656
+ }
657
+ await drainInFlightWarmSnapshot();
644
658
  const sandboxReleaseTargets = new Set(sandboxState.lateSandboxesAwaitingWriterDrain);
645
659
  sandboxState.lateSandboxesAwaitingWriterDrain.clear();
646
660
  if (sandboxState.resolvedSandbox) {
@@ -0,0 +1,101 @@
1
+ import type { AttemptToolDefinition } from "@opengeni/codemode";
2
+ import {
3
+ compareModelPickerOrder,
4
+ modelPickerBillingClassFor,
5
+ } from "@opengeni/contracts/model-picker-order";
6
+ import type { WorkspaceModelSelection } from "@opengeni/core";
7
+
8
+ export const LIST_MODELS_TOOL_NAME = "list_models";
9
+
10
+ export const LIST_MODELS_TOOL_DESCRIPTION =
11
+ "List the models this workspace can select right now, in picker order, with deployment-defined cost and optional guidance. This does not switch the current session model; use an ID with session_create or the human model picker.";
12
+
13
+ function safeLineField(value: string): string {
14
+ return value
15
+ .replace(/[\r\n|]/gu, " ")
16
+ .replace(/\s+/gu, " ")
17
+ .trim();
18
+ }
19
+
20
+ export function renderListModels(input: {
21
+ currentModelId: string | null;
22
+ selections: readonly WorkspaceModelSelection[];
23
+ modelNotes: Readonly<Record<string, string>>;
24
+ }): string {
25
+ const lines: string[] = [];
26
+ if (input.currentModelId) {
27
+ lines.push(`Current: ${safeLineField(input.currentModelId)}`);
28
+ }
29
+
30
+ const selectable = input.selections
31
+ .filter((entry) => entry.availability.selectable)
32
+ .map((selection) => ({
33
+ selection,
34
+ billingClass: modelPickerBillingClassFor(selection.model),
35
+ selectable: selection.availability.selectable,
36
+ label: selection.model.label,
37
+ }))
38
+ .sort(compareModelPickerOrder);
39
+ if (selectable.length === 0) {
40
+ lines.push("No models are available in this workspace.");
41
+ return lines.join("\n");
42
+ }
43
+
44
+ for (const { selection } of selectable) {
45
+ const { model } = selection;
46
+ const fields = [safeLineField(model.id), safeLineField(model.label), model.cost];
47
+ const note = input.modelNotes[model.id];
48
+ if (note) fields.push(safeLineField(note));
49
+ lines.push(`- ${fields.join(" | ")}`);
50
+ }
51
+ return lines.join("\n");
52
+ }
53
+
54
+ export function createListModelsAttemptToolDefinition(input: {
55
+ currentModelId: string | null;
56
+ load: () => Promise<{
57
+ selections: readonly WorkspaceModelSelection[];
58
+ modelNotes: Readonly<Record<string, string>>;
59
+ }>;
60
+ }): AttemptToolDefinition {
61
+ return {
62
+ identity: { serverId: "opengeni", toolName: LIST_MODELS_TOOL_NAME },
63
+ modelName: LIST_MODELS_TOOL_NAME,
64
+ codemodePath: ["opengeni", LIST_MODELS_TOOL_NAME],
65
+ title: "List selectable models",
66
+ description: LIST_MODELS_TOOL_DESCRIPTION,
67
+ inputSchema: {
68
+ type: "object",
69
+ properties: {},
70
+ additionalProperties: false,
71
+ },
72
+ annotations: {
73
+ title: "List selectable models",
74
+ readOnlyHint: true,
75
+ destructiveHint: false,
76
+ idempotentHint: true,
77
+ openWorldHint: false,
78
+ },
79
+ source: "opengeni",
80
+ approval: "none",
81
+ execute: async (args) => {
82
+ if (Object.keys(args).length !== 0) {
83
+ throw new Error("list_models accepts no arguments");
84
+ }
85
+ const catalog = await input.load();
86
+ return {
87
+ isError: false,
88
+ content: [
89
+ {
90
+ type: "text",
91
+ text: renderListModels({
92
+ currentModelId: input.currentModelId,
93
+ selections: catalog.selections,
94
+ modelNotes: catalog.modelNotes,
95
+ }),
96
+ },
97
+ ],
98
+ };
99
+ },
100
+ };
101
+ }
@@ -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
 
@@ -80,6 +89,7 @@ export function assertSessionAttemptQuiescenceRecoveryDurable(input: {
80
89
  export const QUIESCENCE_PROOF_SIGNAL_INITIAL_RETRY_MS = 250;
81
90
  export const QUIESCENCE_PROOF_SIGNAL_MAX_RETRY_MS = 5_000;
82
91
  export const TURN_QUIESCENCE_WATCHDOG_MS = 5 * 60_000;
92
+ export const TURN_FINALIZER_STEP_TIMEOUT_MS = 30_000;
83
93
 
84
94
  /** A fenced activity must never heartbeat forever while its physical writer
85
95
  * drain is stuck. The last-resort worker exit stops every in-process writer;
@@ -232,35 +242,40 @@ export async function releaseTurnSandboxAfterWriterDrain(
232
242
  }
233
243
 
234
244
  /**
235
- * Await a finalizer operation only while this Temporal activity still owns its
236
- * execution window. Once Pause/Steer cancellation arrives, the operation keeps
237
- * its own rejection handler and may finish its idempotent, attempt-scoped
238
- * cleanup in the background, but it cannot pin activity terminalization or
239
- * delay the separately receipt-gated replacement dispatch.
245
+ * Await bounded, idempotent finalizer housekeeping only while this Temporal
246
+ * activity still owns its execution window. A completed turn must never keep
247
+ * heartbeating forever because a provider/tool close promise did not settle:
248
+ * that strands every later durable prompt behind an already-terminal attempt.
249
+ * Cancellation or the hard timeout detaches the operation with a rejection
250
+ * handler; the operation may still finish its attempt-scoped cleanup, but it
251
+ * cannot pin activity terminalization.
240
252
  */
241
253
  export async function waitForTurnFinalizerStep<T>(
242
254
  operation: Promise<T>,
243
255
  signal: AbortSignal | undefined,
256
+ timeoutMs = TURN_FINALIZER_STEP_TIMEOUT_MS,
244
257
  ): Promise<T | undefined> {
245
- if (!signal) return await operation;
246
- if (signal.aborted) {
258
+ if (signal?.aborted) {
247
259
  void operation.catch(() => undefined);
248
260
  return undefined;
249
261
  }
250
262
 
251
- let resolveCancellation: (() => void) | undefined;
252
- const cancelled = new Promise<undefined>((resolve) => {
253
- resolveCancellation = () => resolve(undefined);
263
+ let resolveDetached: (() => void) | undefined;
264
+ const detached = new Promise<undefined>((resolve) => {
265
+ resolveDetached = () => resolve(undefined);
254
266
  });
255
- const cancel = (): void => {
267
+ const detach = (): void => {
256
268
  void operation.catch(() => undefined);
257
- resolveCancellation?.();
269
+ resolveDetached?.();
258
270
  };
259
- signal.addEventListener("abort", cancel, { once: true });
271
+ signal?.addEventListener("abort", detach, { once: true });
272
+ const timeout = setTimeout(detach, Math.max(1, timeoutMs));
273
+ timeout.unref?.();
260
274
  try {
261
- return await Promise.race([operation, cancelled]);
275
+ return await Promise.race([operation, detached]);
262
276
  } finally {
263
- signal.removeEventListener("abort", cancel);
277
+ clearTimeout(timeout);
278
+ signal?.removeEventListener("abort", detach);
264
279
  }
265
280
  }
266
281