@opengeni/worker-bundle 0.23.4-canary.0 → 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 (56) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +7 -0
  2. package/dist/activities/agent-turn/model-usage.d.ts +5 -0
  3. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  4. package/dist/activities/agent-turn/run.d.ts +9 -0
  5. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  6. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  7. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  8. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  9. package/dist/activities/agent-turn/turn-context.d.ts +2 -1
  10. package/dist/activities/agent-turn.d.ts +1 -1
  11. package/dist/activities/capabilities.d.ts +1 -0
  12. package/dist/activities/context-compaction.d.ts +2 -0
  13. package/dist/{activities-control-SGU2WEVB.js → activities-control-ZWH7J4KZ.js} +102 -94
  14. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  15. package/dist/{activities-turn-7EAQXW6Q.js → activities-turn-I4OPLQLS.js} +613 -284
  16. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  17. package/dist/{chunk-TJIPCHMP.js → chunk-65JEWP2Q.js} +105 -6
  18. package/dist/chunk-65JEWP2Q.js.map +1 -0
  19. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  20. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  21. package/dist/{chunk-S2QVRVJW.js → chunk-RQNH6CGR.js} +28 -41
  22. package/dist/chunk-RQNH6CGR.js.map +1 -0
  23. package/dist/index.js +16 -3
  24. package/dist/index.js.map +1 -1
  25. package/dist/observability-metrics.d.ts +33 -7
  26. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  27. package/dist/sandbox-resume.d.ts +6 -1
  28. package/package.json +17 -17
  29. package/src/activities/agent-turn/admission.ts +19 -0
  30. package/src/activities/agent-turn/claim.ts +9 -1
  31. package/src/activities/agent-turn/compaction-prep.ts +6 -1
  32. package/src/activities/agent-turn/finalization.ts +31 -17
  33. package/src/activities/agent-turn/model-usage.ts +33 -4
  34. package/src/activities/agent-turn/quiescence.ts +11 -2
  35. package/src/activities/agent-turn/run.ts +74 -1
  36. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  37. package/src/activities/agent-turn/session-title.ts +55 -2
  38. package/src/activities/agent-turn/stream-attempt.ts +258 -135
  39. package/src/activities/agent-turn/tool-environment.ts +41 -7
  40. package/src/activities/agent-turn/turn-context.ts +4 -2
  41. package/src/activities/agent-turn.ts +5 -1
  42. package/src/activities/api-integrations.ts +11 -1
  43. package/src/activities/capabilities.ts +57 -0
  44. package/src/activities/context-compaction.ts +3 -0
  45. package/src/activities/goals.ts +1 -0
  46. package/src/activities/mcp-credentials.ts +53 -0
  47. package/src/activities/rig-verification.ts +5 -3
  48. package/src/activities/scheduled-tasks.ts +111 -98
  49. package/src/index.ts +15 -0
  50. package/src/observability-metrics.ts +152 -14
  51. package/src/sandbox-reaper-timeout.ts +10 -4
  52. package/src/sandbox-resume.ts +58 -8
  53. package/dist/activities-control-SGU2WEVB.js.map +0 -1
  54. package/dist/activities-turn-7EAQXW6Q.js.map +0 -1
  55. package/dist/chunk-S2QVRVJW.js.map +0 -1
  56. package/dist/chunk-TJIPCHMP.js.map +0 -1
@@ -24,6 +24,10 @@ export type SessionRecoveryBacklog = {
24
24
  quiescence_missing: number;
25
25
  projection_stale: number;
26
26
  };
27
+ export type ContextCompactionPendingSummary = {
28
+ pendingCount: number;
29
+ oldestStartedAt: Date | null;
30
+ };
27
31
  export type TemporalTurnTaskQueueStats = {
28
32
  approximateBacklogCount?: unknown;
29
33
  approximateBacklogAge?: {
@@ -33,6 +37,8 @@ export type TemporalTurnTaskQueueStats = {
33
37
  tasksAddRate?: unknown;
34
38
  tasksDispatchRate?: unknown;
35
39
  } | null;
40
+ declare const CONTEXT_COMPACTION_TRIGGERS: readonly ["auto", "operator", "proactive", "overflow"];
41
+ export type ContextCompactionTrigger = (typeof CONTEXT_COMPACTION_TRIGGERS)[number];
36
42
  export declare function observabilityEventLogger(observability: Observability): EventLogger;
37
43
  export declare function runtimeMetricsHooksForObservability(observability: Observability): RuntimeMetricsHooks;
38
44
  /**
@@ -71,6 +77,13 @@ export declare function makeMachineOpObserver(hooks: RuntimeMetricsHooks): {
71
77
  };
72
78
  export declare function turnLifecycleMetricsFor(observability: Observability): TurnLifecycleMetrics;
73
79
  export declare function initializeWorkerOutcomeMetrics(observability: Observability): void;
80
+ /**
81
+ * Publish every bounded compaction lifecycle series before the first event.
82
+ * The zero counters make the closed trigger catalog explicit before a scrape.
83
+ * Alerting uses the separate durable control-worker projection below; these
84
+ * process-local counters are lifecycle-rate diagnostics only.
85
+ */
86
+ export declare function initializeContextCompactionMetrics(observability: Observability): void;
74
87
  export declare function recordWorkerDeathRecoveryMetrics(observability: Observability, input: {
75
88
  outcome: WorkerDeathRecoveryOutcome;
76
89
  timeoutType: "HEARTBEAT" | "SCHEDULE_TO_START";
@@ -142,6 +155,15 @@ export declare function startSessionRecoveryMonitor(input: {
142
155
  }): {
143
156
  close: () => Promise<void>;
144
157
  };
158
+ export declare function recordContextCompactionPendingGauges(observability: Observability, summary: ContextCompactionPendingSummary, nowMs?: number): void;
159
+ export declare function startContextCompactionPendingMonitor(input: {
160
+ observability: Observability;
161
+ read: () => Promise<ContextCompactionPendingSummary>;
162
+ intervalMs?: number;
163
+ now?: () => number;
164
+ }): {
165
+ close: () => Promise<void>;
166
+ };
145
167
  export declare function recordSandboxLeaseGauges(observability: Observability, counts: Partial<Record<SandboxLeaseLiveness, number>>): void;
146
168
  export declare function recordOpenSandboxKubernetesInventoryGauges(observability: Observability, inventory: OpenSandboxKubernetesInventory): void;
147
169
  export declare const SANDBOX_INVENTORY_PROJECTION_DOMAINS: readonly ["leases", "checkpoint_artifacts", "rotation_backlog", "retained_processes", "expired_drains", "opensandbox_kubernetes"];
@@ -319,14 +341,17 @@ export declare function recordSessionEventAppendPhase(observability: Observabili
319
341
  export declare function recordSessionEventPublishLatency(observability: Observability, input: {
320
342
  durationSeconds: number;
321
343
  }): void;
322
- /** Observed input (context) tokens per model response — the context-pressure
323
- * signal. Paired with `opengeni_context_compactions_total`, it makes the
324
- * "compaction never firing while contexts run hot" failure mode expressible. */
344
+ /** Observed input (context) tokens per model response for context-pressure
345
+ * dashboards and provider diagnostics. */
325
346
  export declare function recordModelInputTokens(observability: Observability, provider: string, inputTokens: number): void;
326
- /** A context compaction actually fired, by trigger (operator | overflow | proactive
327
- * | auto). The rate of this — against the input-tokens histogram above — is how an
328
- * operator sees compaction working (or silently not). */
329
- export declare function recordContextCompaction(observability: Observability, trigger: string): void;
347
+ /** A context compaction successfully completed, by bounded trigger. */
348
+ export declare function recordContextCompaction(observability: Observability, trigger: ContextCompactionTrigger): void;
349
+ /**
350
+ * A durable context-compaction start, recorded only after the attempt-fenced
351
+ * `compaction.started` transaction commits. This is the model-aware threshold
352
+ * signal; it does not guess from a static token count shared by unlike models.
353
+ */
354
+ export declare function recordContextCompactionStarted(observability: Observability, trigger: ContextCompactionTrigger): void;
330
355
  /**
331
356
  * Metadata-only Company Brain exposure telemetry. Labels are closed enums and
332
357
  * never include tenant ids, record ids, titles, descriptions, or content.
@@ -381,3 +406,4 @@ export declare function modelCallAccountContext(input: {
381
406
  servingAccountHash: string;
382
407
  accountChangedFromPrevCall: boolean;
383
408
  };
409
+ export {};
@@ -1,12 +1,12 @@
1
1
  import { type Settings } from "@opengeni/config";
2
2
  import { type SandboxDrainTimeoutClass } from "./sandbox-reaper-contract.js";
3
- export declare function sandboxDrainTiming(settings: Pick<Settings, "sandboxSnapshotTimeoutMs">): {
3
+ export declare function sandboxDrainTiming(settings: Pick<Settings, "sandboxSnapshotTimeoutMs" | "sandboxDrainSnapshotTimeoutMs">): {
4
4
  snapshotTimeoutMs: number;
5
5
  captureTimeoutMs: number;
6
6
  timeoutClass: SandboxDrainTimeoutClass;
7
7
  activityTimeoutMs: number;
8
8
  };
9
- export declare function assertSandboxReaperActivityTimeout(settings: Pick<Settings, "sandboxSnapshotTimeoutMs">): void;
9
+ export declare function assertSandboxReaperActivityTimeout(settings: Pick<Settings, "sandboxSnapshotTimeoutMs" | "sandboxDrainSnapshotTimeoutMs">): void;
10
10
  export declare function assertSandboxDrainInputTiming(input: {
11
11
  timeoutClass: SandboxDrainTimeoutClass;
12
12
  snapshotTimeoutMs: number;
@@ -223,6 +223,11 @@ export declare function persistSandboxDeadlineRotationCheckpoint(services: Sandb
223
223
  * the user's machine IS the persistence), or a snapshot newer than the
224
224
  * interval already rides the lease.
225
225
  */
226
+ export type WarmWorkspaceSnapshotPromise = Promise<boolean> & {
227
+ /** Settles only after any provider capture continuation has published or
228
+ * failed and released its exact workspace-capture admission. */
229
+ readonly settled: Promise<void>;
230
+ };
226
231
  export declare function maybePersistWarmWorkspaceSnapshot(services: SandboxResumeServices, ids: {
227
232
  accountId: string;
228
233
  workspaceId: string;
@@ -230,7 +235,7 @@ export declare function maybePersistWarmWorkspaceSnapshot(services: SandboxResum
230
235
  turnId: string;
231
236
  attemptId: string;
232
237
  sandboxGroupId: string;
233
- }, session: unknown, leaseEpoch: number, signal?: AbortSignal, force?: boolean): Promise<boolean>;
238
+ }, session: unknown, leaseEpoch: number, signal?: AbortSignal, force?: boolean): WarmWorkspaceSnapshotPromise;
234
239
  /**
235
240
  * Resume the one box for a single turn (or any worker-side resume op). Returns
236
241
  * the live non-owned session + the fence epoch + a release fn. The CALLER owns
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.23.4-canary.0",
3
+ "version": "0.24.2-canary.0",
4
4
  "description": "OpenGeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -54,22 +54,22 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@llamaindex/liteparse": "2.14.2",
57
- "@opengeni/agent-proto": "^0.5.1-canary.7",
58
- "@opengeni/capabilities": "^0.3.1-canary.6",
59
- "@opengeni/codemode": "^0.4.22-canary.1",
60
- "@opengeni/codex": "^0.2.20-canary.1",
61
- "@opengeni/config": "^0.22.5-canary.1",
62
- "@opengeni/contracts": "^2.9.2-canary.1",
63
- "@opengeni/core": "^2.6.4-canary.0",
64
- "@opengeni/db": "^3.7.4-canary.0",
65
- "@opengeni/documents": "^0.8.14-canary.0",
66
- "@opengeni/events": "^0.4.12-canary.0",
67
- "@opengeni/github": "^0.6.7-canary.1",
68
- "@opengeni/network": "^0.2.3-canary.6",
69
- "@opengeni/observability": "^0.8.14-canary.1",
70
- "@opengeni/runtime": "^2.0.1-canary.1",
71
- "@opengeni/storage": "^0.2.115-canary.1",
72
- "@opengeni/xai-subscription": "^0.1.2-canary.6",
57
+ "@opengeni/agent-proto": "^0.5.1-canary.8",
58
+ "@opengeni/capabilities": "^0.3.1-canary.7",
59
+ "@opengeni/codemode": "^0.4.25-canary.0",
60
+ "@opengeni/codex": "^0.2.20-canary.2",
61
+ "@opengeni/config": "^0.23.2-canary.0",
62
+ "@opengeni/contracts": "^2.11.1-canary.0",
63
+ "@opengeni/core": "^2.7.2-canary.0",
64
+ "@opengeni/db": "^3.8.2-canary.0",
65
+ "@opengeni/documents": "^0.8.17-canary.0",
66
+ "@opengeni/events": "^0.4.15-canary.0",
67
+ "@opengeni/github": "^0.7.1-canary.0",
68
+ "@opengeni/network": "^0.2.3-canary.7",
69
+ "@opengeni/observability": "^0.8.17-canary.0",
70
+ "@opengeni/runtime": "^2.1.2-canary.0",
71
+ "@opengeni/storage": "^0.2.118-canary.0",
72
+ "@opengeni/xai-subscription": "^0.1.2-canary.7",
73
73
  "@temporalio/activity": "^1.17.0",
74
74
  "@temporalio/client": "^1.17.0",
75
75
  "@temporalio/worker": "^1.17.0",
@@ -99,6 +99,25 @@ export function shouldPublishToolAuthNeededForTurn(
99
99
  return typeof text === "string" && /\bslack\b/i.test(text);
100
100
  }
101
101
 
102
+ /**
103
+ * Keep host-owned auth events inert in browsers from before authoritySource
104
+ * existed. Those bundles treat unsupported_auth as unavailable and therefore
105
+ * do not invoke native OAuth; upgraded clients read hostReason and the
106
+ * host-minted authorizationUrl.
107
+ */
108
+ export function rollingSafeToolAuthNeededPayload(
109
+ payload: ToolAuthNeededPayload,
110
+ ): ToolAuthNeededPayload {
111
+ if (payload.authoritySource !== "host") {
112
+ return payload;
113
+ }
114
+ return {
115
+ ...payload,
116
+ reason: "unsupported_auth",
117
+ hostReason: payload.hostReason ?? payload.reason,
118
+ };
119
+ }
120
+
102
121
  export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1): {
103
122
  externallyBilled: boolean;
104
123
  countsTowardTokenCap: boolean;
@@ -25,6 +25,7 @@ import {
25
25
  settingsWithEnabledCapabilityMcpServers,
26
26
  settingsWithWorkspaceGatewayCredential,
27
27
  settingsWithWorkspaceOpenRouterCredential,
28
+ settingsWithOrganizationProviderCredentials,
28
29
  withXaiSubscriptionProvider,
29
30
  } from "../capabilities";
30
31
  import { validateIncidentTelemetrySystemUpdateAuthority } from "../incident-telemetry-authority";
@@ -242,13 +243,20 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
242
243
  xaiSettings,
243
244
  claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
244
245
  );
245
- const capabilitySettings = await settingsWithWorkspaceOpenRouterCredential(
246
+ const workspaceProviderSettings = await settingsWithWorkspaceOpenRouterCredential(
246
247
  db,
247
248
  input.accountId,
248
249
  input.workspaceId,
249
250
  gatewaySettings,
250
251
  claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
251
252
  );
253
+ const capabilitySettings = await settingsWithOrganizationProviderCredentials(
254
+ db,
255
+ input.accountId,
256
+ input.workspaceId,
257
+ workspaceProviderSettings,
258
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
259
+ );
252
260
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
253
261
  ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
254
262
  : null;
@@ -20,7 +20,10 @@ import type {
20
20
  RunAgentTurnInput,
21
21
  RunAgentTurnResult,
22
22
  } from "../types";
23
- import { recordContextCompaction } from "../../observability-metrics";
23
+ import {
24
+ recordContextCompaction,
25
+ recordContextCompactionStarted,
26
+ } from "../../observability-metrics";
24
27
  import { createTurnCredentialLeases } from "./credential-leases";
25
28
  import { createTurnMediaArtifacts } from "./media-artifacts";
26
29
  import { type SessionEvent } from "@opengeni/contracts";
@@ -288,6 +291,8 @@ export async function prepareCompaction(deps: CompactionPrepDeps): Promise<Compa
288
291
  const compactionModeOptions = {
289
292
  codexCompactionMode: session.codexCompactionMode,
290
293
  isCodexSubscriptionTurn: billingState.isCodexTurn,
294
+ onCompactionStarted: (trigger: "auto" | "operator" | "proactive" | "overflow") =>
295
+ recordContextCompactionStarted(observability, trigger),
291
296
  publishLiveEvents: publishCompactionLiveEvents,
292
297
  ...(remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {}),
293
298
  } as const;
@@ -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) {
@@ -125,6 +125,8 @@ export function createCompactionModelUsageEventState(
125
125
  return { usageCount: 0, claimedSourceKeys };
126
126
  }
127
127
 
128
+ export const createSessionTitleModelUsageEventState = createCompactionModelUsageEventState;
129
+
128
130
  export function modelResponseContextSignal(
129
131
  state: ModelResponseEventState,
130
132
  ): { revision: number; totalTokens: number } | null {
@@ -342,6 +344,7 @@ export async function processModelResponseTerminalEvent(input: {
342
344
  export async function processCompactionModelUsageEvent(input: {
343
345
  usage: ModelResponseUsage;
344
346
  state: CompactionModelUsageEventState;
347
+ sourceKind?: "compaction" | "session-title";
345
348
  dispatchId: string | null;
346
349
  settings: Settings;
347
350
  db: ActivityServices["db"];
@@ -373,7 +376,7 @@ export async function processCompactionModelUsageEvent(input: {
373
376
  const sourceKey = modelUsageSourceKey({
374
377
  responseId: input.usage.responseId,
375
378
  dispatchId: input.dispatchId,
376
- positionalKey: `compaction-${usageOrdinal}`,
379
+ positionalKey: `${input.sourceKind ?? "compaction"}-${usageOrdinal}`,
377
380
  });
378
381
  if (input.state.claimedSourceKeys.has(sourceKey)) {
379
382
  return { status: "duplicate", sourceKey };
@@ -456,6 +459,15 @@ export async function processCompactionModelUsageEvent(input: {
456
459
  return { status: "processed", sourceKey, authoritative };
457
460
  }
458
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
+
459
471
  export async function emitModelCallUsage(input: {
460
472
  observability: ActivityServices["observability"];
461
473
  publish: TurnEventPublisher | null;
@@ -581,6 +593,8 @@ export type ModelUsageBillingRecord = {
581
593
  pricedCostMicros: number;
582
594
  /** Hypothetical provider-rate USD micros; never an OpenGeni charge. */
583
595
  estimatedProviderCostMicros: number | null;
596
+ /** Hypothetical OpenGeni credit price at the captured rate; never a debit. */
597
+ equivalentCreditCostMicros: number | null;
584
598
  pricingSource: "configured_list_price" | "gateway_reported" | null;
585
599
  normalizedUsage: ModelCallUsageNormalization;
586
600
  upstreamProvider?: string;
@@ -662,7 +676,7 @@ export async function recordModelUsageAndDebitCredits(
662
676
  }
663
677
  : calculateGatewayReportedCostBreakdown(
664
678
  settings,
665
- input.model,
679
+ configuredPricingModel ?? input.model,
666
680
  gatewayBilling.inferenceCostUsd,
667
681
  { inputTokens },
668
682
  )
@@ -679,6 +693,12 @@ export async function recordModelUsageAndDebitCredits(
679
693
  : hasCompleteCoreTokenTelemetry
680
694
  ? (pricingBreakdown?.providerCostMicros ?? null)
681
695
  : null;
696
+ const equivalentCreditCostMicros =
697
+ pricingBreakdown && !unpinnedWorkspaceGatewayModel
698
+ ? gatewayBilling || hasCompleteCoreTokenTelemetry
699
+ ? pricingBreakdown.creditCostMicros
700
+ : null
701
+ : null;
682
702
  const pricingSource = gatewayBilling
683
703
  ? ("gateway_reported" as const)
684
704
  : estimatedProviderCostMicros !== null
@@ -722,6 +742,7 @@ export async function recordModelUsageAndDebitCredits(
722
742
  billingPath: "external",
723
743
  pricedCostMicros: 0,
724
744
  estimatedProviderCostMicros,
745
+ equivalentCreditCostMicros,
725
746
  pricingSource,
726
747
  normalizedUsage,
727
748
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
@@ -733,6 +754,7 @@ export async function recordModelUsageAndDebitCredits(
733
754
  billingPath: "opengeni_credits",
734
755
  pricedCostMicros: 0,
735
756
  estimatedProviderCostMicros,
757
+ equivalentCreditCostMicros,
736
758
  pricingSource,
737
759
  normalizedUsage,
738
760
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
@@ -786,6 +808,7 @@ export async function recordModelUsageAndDebitCredits(
786
808
  billingPath: "opengeni_credits",
787
809
  pricedCostMicros: costMicros,
788
810
  estimatedProviderCostMicros,
811
+ equivalentCreditCostMicros,
789
812
  pricingSource,
790
813
  normalizedUsage,
791
814
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
@@ -823,6 +846,7 @@ export async function recordAuthoritativeModelCallFact(input: {
823
846
  billingPath: input.billing.billingPath,
824
847
  pricedCostMicros: input.billing.pricedCostMicros,
825
848
  estimatedProviderCostMicros: input.billing.estimatedProviderCostMicros,
849
+ equivalentCreditCostMicros: input.billing.equivalentCreditCostMicros,
826
850
  pricingSource: input.billing.pricingSource,
827
851
  inputTokens: telemetry.inputTokens,
828
852
  outputTokens: telemetry.outputTokens,
@@ -850,10 +874,15 @@ export function sanitizedModelUsageInput(normalized: ModelCallUsageNormalization
850
874
  ? { outputTokens: normalized.telemetry.outputTokens }
851
875
  : {}),
852
876
  ...(normalized.totalTokens !== null ? { totalTokens: normalized.totalTokens } : {}),
853
- ...(normalized.telemetry.cachedTokens !== null
877
+ ...(normalized.telemetry.cachedTokens !== null || normalized.telemetry.cacheWriteTokens !== null
854
878
  ? {
855
879
  inputTokensDetails: {
856
- 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 }),
857
886
  },
858
887
  }
859
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();
@@ -780,6 +828,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
780
828
  providerTurn.xaiRequestContext
781
829
  ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn)
782
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();
783
851
  const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
784
852
  withCodex(() =>
785
853
  withCodexRequestOverrides(
@@ -1104,6 +1172,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1104
1172
  const {
1105
1173
  attemptConnectorActionBindings,
1106
1174
  connectorActionIdentity,
1175
+ generateSessionTitleInParallel,
1107
1176
  postToolPreparationStartedAt,
1108
1177
  preparationIndependentToolNames,
1109
1178
  } = toolRuntime;
@@ -1413,6 +1482,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1413
1482
  input,
1414
1483
  settings: capabilitySettings,
1415
1484
  db,
1485
+ bus,
1416
1486
  runtime,
1417
1487
  objectStorage,
1418
1488
  observability,
@@ -1443,6 +1513,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1443
1513
  attachCodemodeTokenRenewal,
1444
1514
  attachRunCredentialRenewal,
1445
1515
  withProviderRequestContext,
1516
+ withSessionTitleProviderRequestContext,
1446
1517
  publishCompactionLiveEvents,
1447
1518
  publishCompactionOutcomeEvents,
1448
1519
  recordCompanyBrainContributionReceiptOnce,
@@ -1468,6 +1539,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1468
1539
  providerApi,
1469
1540
  runSettings,
1470
1541
  turnTools,
1542
+ generateSessionTitleInParallel,
1543
+ sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
1471
1544
  compactSummarizer,
1472
1545
  settleDeferredSteerAfterCompaction,
1473
1546
  compactionModelHistoryProjector,