@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
@@ -16,6 +16,11 @@ export declare const activity: import("@temporalio/workflow").ActivityInterfaceF
16
16
  * is an independent workflow and will re-evaluate current state. */
17
17
  export declare const scheduledTaskActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "dispatchScheduledTaskRun">>;
18
18
  export declare const automationActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "dispatchAutomationRun">>;
19
+ /** Dispatch has a bounded retry window, but an accepted automation event must
20
+ * not remain durably `dispatching` after that window closes. This compensating
21
+ * database write is idempotent and retries without a cap until the run reaches
22
+ * a terminal state (or is observed as already dispatched). */
23
+ export declare const automationFailureActivity: import("@temporalio/workflow").ActivityInterfaceFor<Pick<typeof activities, "settleAutomationRunFailure">>;
19
24
  /** Goal continuation evaluates a durable Postgres obligation at an idle
20
25
  * boundary. A transient failure gets a short retry window, then records an
21
26
  * explicit delayed outbox wake instead of relying on an unrelated mutation or
@@ -3,4 +3,9 @@ export type AutomationRunWorkflowInput = {
3
3
  workspaceId: string;
4
4
  runId: string;
5
5
  };
6
+ export type AutomationRunWorkflowActivities = {
7
+ dispatchAutomationRun: (input: AutomationRunWorkflowInput) => Promise<unknown>;
8
+ settleAutomationRunFailure: (input: AutomationRunWorkflowInput) => Promise<void>;
9
+ };
10
+ export declare function runAutomationRunWorkflow(input: AutomationRunWorkflowInput, activities: AutomationRunWorkflowActivities): Promise<void>;
6
11
  export declare function automationRunWorkflow(input: AutomationRunWorkflowInput): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.23.4",
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",
58
- "@opengeni/capabilities": "^0.3.1",
59
- "@opengeni/codemode": "^0.4.22",
60
- "@opengeni/codex": "^0.2.20",
61
- "@opengeni/config": "^0.22.5",
62
- "@opengeni/contracts": "^2.9.2",
63
- "@opengeni/core": "^2.6.4",
64
- "@opengeni/db": "^3.7.4",
65
- "@opengeni/documents": "^0.8.14",
66
- "@opengeni/events": "^0.4.12",
67
- "@opengeni/github": "^0.6.7",
68
- "@opengeni/network": "^0.2.3",
69
- "@opengeni/observability": "^0.8.14",
70
- "@opengeni/runtime": "^2.0.1",
71
- "@opengeni/storage": "^0.2.115",
72
- "@opengeni/xai-subscription": "^0.1.2",
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",
@@ -1,4 +1,5 @@
1
1
  import { configuredStaticUsageLimits, type Settings } from "@opengeni/config";
2
+ import { modelFundingForAdmission } from "@opengeni/core";
2
3
  import { getBillingBalance, isCodexBilledTurn, sumUsageQuantity } from "@opengeni/db";
3
4
  import type { ControlActivityServices } from "./types";
4
5
 
@@ -17,12 +18,17 @@ export async function agentRunAdmissionDenial(
17
18
  requestedAgentRuns: number;
18
19
  },
19
20
  ): Promise<AgentRunAdmissionDenial | null> {
20
- const externallyBilled = await isCodexBilledTurn({
21
+ const codexBilled = await isCodexBilledTurn({
21
22
  db: services.db,
22
23
  settings: services.settings,
23
24
  workspaceId: input.workspaceId,
24
25
  model: input.model,
25
26
  });
27
+ const externallyBilled = modelFundingForAdmission(
28
+ services.settings,
29
+ input.model,
30
+ codexBilled,
31
+ ).fundedWithoutCredits;
26
32
  if (
27
33
  !externallyBilled &&
28
34
  (services.settings.billingMode === "stripe" || services.settings.usageLimitsMode === "managed")
@@ -12,6 +12,7 @@ import {
12
12
  type SessionTurn,
13
13
  type ToolAuthNeededPayload,
14
14
  type TurnExecutionPolicyV1,
15
+ type XaiProviderAccountAuthoritySnapshotV1,
15
16
  } from "@opengeni/contracts";
16
17
  import { createHash } from "node:crypto";
17
18
  import { startOfUtcMonth } from "./model-usage";
@@ -38,6 +39,22 @@ export function credentialSubjectIdForTurnInitiator(
38
39
  return directPersonalConnectionSubjectId(turn);
39
40
  }
40
41
 
42
+ export function xaiCatalogReadinessAuthority(
43
+ turn: {
44
+ initiatingHumanSubjectId: string | null;
45
+ xaiProviderAccountAuthoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
46
+ },
47
+ directCredentialSubjectId: string | undefined,
48
+ ): {
49
+ subjectId: string;
50
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
51
+ } | null {
52
+ const subjectId = turn.initiatingHumanSubjectId ?? directCredentialSubjectId;
53
+ return subjectId
54
+ ? { subjectId, authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot }
55
+ : null;
56
+ }
57
+
41
58
  /**
42
59
  * Direct authenticated-human work is the persisted human source with subject
43
60
  * authority and no inherited agent or legacy provenance. Use this complete
@@ -82,13 +99,34 @@ export function shouldPublishToolAuthNeededForTurn(
82
99
  return typeof text === "string" && /\bslack\b/i.test(text);
83
100
  }
84
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
+
85
121
  export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1): {
86
122
  externallyBilled: boolean;
123
+ countsTowardTokenCap: boolean;
87
124
  codexSubscription: boolean;
88
125
  xaiSubscription: boolean;
89
126
  } {
90
127
  return {
91
128
  externallyBilled: policy.billing.metering === "external",
129
+ countsTowardTokenCap: policy.billing.upstreamPayer === "deployment",
92
130
  codexSubscription:
93
131
  policy.providerId === "codex-subscription" &&
94
132
  policy.credentialSource.kind === "connected_subscription" &&
@@ -222,10 +260,13 @@ export async function ensureRunAllowed(
222
260
  workspaceId: string,
223
261
  isExternallyBilledTurn: boolean,
224
262
  entitlements?: ActivityServices["entitlements"],
263
+ chargesOpenGeniCredits = !isExternallyBilledTurn,
264
+ countsTowardTokenCap = !isExternallyBilledTurn,
225
265
  ): Promise<void> {
226
- // Externally billed turns are paid outside OpenGeni: skip the credit-balance
227
- // gate and monthly token cap. The agent-run COUNT cap below is a
228
- // volume/fairness quota (not a credit/cost gate) and is intentionally kept.
266
+ // Upstream settlement and workspace-facing cost are independent. External
267
+ // metering skips the token cap; free/subscription/workspace cost skips the
268
+ // OpenGeni credit gate. The agent-run COUNT cap below is a volume/fairness
269
+ // quota and is intentionally kept for every funding path.
229
270
  //
230
271
  // §7.5 P3 — host-entitlements DELEGATION (the worker half of the same seam the
231
272
  // API edge exposes). For a non-codex turn, when the host binds `entitlements`, its
@@ -239,7 +280,7 @@ export async function ensureRunAllowed(
239
280
  // idempotency-keyed writer at recordModelUsageAndDebitCredits), so a PULL host meter
240
281
  // is consulted without ever double-charging.
241
282
  if (
242
- !isExternallyBilledTurn &&
283
+ chargesOpenGeniCredits &&
243
284
  entitlements &&
244
285
  (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
245
286
  ) {
@@ -253,7 +294,7 @@ export async function ensureRunAllowed(
253
294
  throw new Error(decision.reason || "insufficient OpenGeni credits");
254
295
  }
255
296
  } else if (
256
- !isExternallyBilledTurn &&
297
+ chargesOpenGeniCredits &&
257
298
  (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
258
299
  ) {
259
300
  const balance = await getBillingBalance(db, accountId);
@@ -278,7 +319,7 @@ export async function ensureRunAllowed(
278
319
  );
279
320
  }
280
321
  }
281
- if (!isExternallyBilledTurn && limits.maxMonthlyTokensPerWorkspace) {
322
+ if (countsTowardTokenCap && limits.maxMonthlyTokensPerWorkspace) {
282
323
  const used = await sumUsageQuantity(db, {
283
324
  workspaceId,
284
325
  eventType: "model.tokens",
@@ -24,11 +24,14 @@ import {
24
24
  settingsWithCodexCredential,
25
25
  settingsWithEnabledCapabilityMcpServers,
26
26
  settingsWithWorkspaceGatewayCredential,
27
+ settingsWithWorkspaceOpenRouterCredential,
28
+ settingsWithOrganizationProviderCredentials,
27
29
  withXaiSubscriptionProvider,
28
30
  } from "../capabilities";
29
31
  import { validateIncidentTelemetrySystemUpdateAuthority } from "../incident-telemetry-authority";
30
32
  import {
31
33
  assertSessionAllowsProductModel,
34
+ resolveCatalogSettings,
32
35
  resolveCodexAppsCredentialIdForRun,
33
36
  } from "@opengeni/core";
34
37
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
@@ -72,6 +75,7 @@ import type {
72
75
  export type ClaimTurnDeps = {
73
76
  input: RunAgentTurnInput;
74
77
  settings: Settings;
78
+ catalogSourceSettings: Settings;
75
79
  db: ActivityServices["db"];
76
80
  bus: ActivityServices["bus"];
77
81
  runtime: ActivityServices["runtime"];
@@ -135,6 +139,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
135
139
  const {
136
140
  input,
137
141
  settings,
142
+ catalogSourceSettings,
138
143
  db,
139
144
  bus,
140
145
  runtime,
@@ -156,12 +161,15 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
156
161
  acknowledgeLostAttemptOwnership,
157
162
  } = deps;
158
163
 
164
+ const deploymentCatalogSettings = (await resolveCatalogSettings(db, catalogSourceSettings))
165
+ .settings;
166
+
159
167
  const validatePendingSystemUpdateAuthority: NonNullable<
160
168
  ClaimSessionWorkForAttemptInput["validatePendingSystemUpdateAuthority"]
161
169
  > = async (tx, update) =>
162
170
  await validateIncidentTelemetrySystemUpdateAuthority({
163
171
  db: tx,
164
- settings,
172
+ settings: deploymentCatalogSettings,
165
173
  workspaceId: input.workspaceId,
166
174
  sessionId: input.sessionId,
167
175
  update,
@@ -187,6 +195,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
187
195
  attempt.redispatchesAtDispatch = Number(
188
196
  (turn.metadata as { workerDeathRedispatches?: number } | null)?.workerDeathRedispatches ?? 0,
189
197
  );
198
+ const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
190
199
  // Establish durable attempt ownership before any later read can fail.
191
200
  // Therefore every failure with no turnId came from the one atomic claim
192
201
  // transaction and can be classified without conflating ordinary runtime
@@ -198,7 +207,7 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
198
207
  const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
199
208
  db,
200
209
  input.workspaceId,
201
- settings,
210
+ deploymentCatalogSettings,
202
211
  {
203
212
  ...(credentialSubjectId
204
213
  ? { subjectId: credentialSubjectId }
@@ -227,16 +236,31 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
227
236
  const xaiSettings = codexSettings.supergrokSubscriptionEnabled
228
237
  ? withXaiSubscriptionProvider(codexSettings)
229
238
  : codexSettings;
230
- const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
239
+ const gatewaySettings = await settingsWithWorkspaceGatewayCredential(
231
240
  db,
241
+ input.accountId,
232
242
  input.workspaceId,
233
243
  xaiSettings,
244
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
245
+ );
246
+ const workspaceProviderSettings = await settingsWithWorkspaceOpenRouterCredential(
247
+ db,
248
+ input.accountId,
249
+ input.workspaceId,
250
+ gatewaySettings,
251
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model,
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,
234
259
  );
235
260
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
236
261
  ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
237
262
  : null;
238
263
  runtime.configure(capabilitySettings);
239
- const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
240
264
  const policyForAbsent =
241
265
  claimedPolicy.kind === "valid"
242
266
  ? claimedPolicy.policy
@@ -266,6 +290,8 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
266
290
  assertSessionAllowsProductModel(session, turnExecutionPolicy.productModelId);
267
291
  const billingIdentity = turnExecutionPolicyBillingIdentity(turnExecutionPolicy);
268
292
  billingState.isExternallyBilledTurn = billingIdentity.externallyBilled;
293
+ billingState.chargesOpenGeniCredits = verifiedExecutionPolicy.model.cost === "credits";
294
+ billingState.countsTowardTokenCap = billingIdentity.countsTowardTokenCap;
269
295
  billingState.isCodexTurn = billingIdentity.codexSubscription;
270
296
  billingState.isXaiTurn = billingIdentity.xaiSubscription;
271
297
  const trigger = await getSessionEvent(db, input.workspaceId, attempt.triggerEventId);
@@ -305,12 +331,14 @@ export async function claimTurnAttempt(deps: ClaimTurnDeps): Promise<ClaimTurnOu
305
331
  // the local credit read). Unset port → today's local-ledger path.
306
332
  await waitForTurnOperation(
307
333
  ensureRunAllowed(
308
- settings,
334
+ capabilitySettings,
309
335
  db,
310
336
  input.accountId,
311
337
  input.workspaceId,
312
338
  billingState.isExternallyBilledTurn,
313
339
  entitlements,
340
+ billingState.chargesOpenGeniCredits,
341
+ billingState.countsTowardTokenCap,
314
342
  ),
315
343
  cancellationSignal,
316
344
  undefined,
@@ -3,7 +3,7 @@ import {
3
3
  fetchCodexUsageForAccount,
4
4
  type CodexAccountStatus,
5
5
  } from "@opengeni/db";
6
- import { type RegistryProviderKind, type Settings } from "@opengeni/config";
6
+ import { type ResolvedModelProvider, type Settings } from "@opengeni/config";
7
7
  import { codexAccountNeedsLiveCapacityRefresh } from "../codex-rotation";
8
8
  import {
9
9
  refreshCodexUsageAndRepairCapacityWaiters,
@@ -19,7 +19,7 @@ export function codexWorkspaceMetricKey(workspaceId: string): string {
19
19
  /** Stable public request identity across partial resumes and activity retries. */
20
20
  export function acceptsPromptCacheKeyForTurn(
21
21
  resolvedModel: {
22
- provider: { kind: RegistryProviderKind; builtin?: boolean };
22
+ provider: { kind: ResolvedModelProvider["kind"]; builtin?: boolean };
23
23
  } | null,
24
24
  ): boolean {
25
25
  if (!resolvedModel) {
@@ -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";
@@ -188,6 +191,8 @@ export async function prepareCompaction(deps: CompactionPrepDeps): Promise<Compa
188
191
  providerApi: resolvedModel?.provider.api ?? "responses",
189
192
  model: resolvedModel?.configured.id ?? turn.model,
190
193
  externallyBilled: billingState.isExternallyBilledTurn,
194
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
195
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
191
196
  turnAttemptId: input.attemptId,
192
197
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
193
198
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
@@ -286,6 +291,8 @@ export async function prepareCompaction(deps: CompactionPrepDeps): Promise<Compa
286
291
  const compactionModeOptions = {
287
292
  codexCompactionMode: session.codexCompactionMode,
288
293
  isCodexSubscriptionTurn: billingState.isCodexTurn,
294
+ onCompactionStarted: (trigger: "auto" | "operator" | "proactive" | "overflow") =>
295
+ recordContextCompactionStarted(observability, trigger),
289
296
  publishLiveEvents: publishCompactionLiveEvents,
290
297
  ...(remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {}),
291
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) {
@@ -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
+ }