@opengeni/worker-bundle 0.22.8 → 0.23.4-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 (103) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +10 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +1 -4
  3. package/dist/activities/agent-turn/claim.d.ts +3 -4
  4. package/dist/activities/agent-turn/codex.d.ts +8 -7
  5. package/dist/activities/agent-turn/compaction-prep.d.ts +2 -0
  6. package/dist/activities/agent-turn/errors.d.ts +13 -0
  7. package/dist/activities/agent-turn/finalization.d.ts +1 -3
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +10 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +39 -0
  11. package/dist/activities/agent-turn/sandbox-route.d.ts +14 -24
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -4
  13. package/dist/activities/agent-turn/sandbox-shared-preparation.d.ts +30 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +7 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  16. package/dist/activities/agent-turn/tool-policy.d.ts +7 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +3 -8
  18. package/dist/activities/agent-turn/turn-sandbox-access.d.ts +12 -0
  19. package/dist/activities/agent-turn/video-reference-sandbox.d.ts +7 -0
  20. package/dist/activities/automations.d.ts +1 -0
  21. package/dist/activities/capabilities.d.ts +2 -1
  22. package/dist/activities/codex-rotation.d.ts +2 -0
  23. package/dist/activities/goals.d.ts +11 -0
  24. package/dist/activities/image-generation-references.d.ts +2 -1
  25. package/dist/activities/scheduled-tasks.d.ts +1 -0
  26. package/dist/activities/session-state.d.ts +2 -1
  27. package/dist/activities/types.d.ts +8 -0
  28. package/dist/{activities-control-VZIIV7RX.js → activities-control-SGU2WEVB.js} +328 -107
  29. package/dist/activities-control-SGU2WEVB.js.map +1 -0
  30. package/dist/activities-control.d.ts +2 -0
  31. package/dist/{activities-turn-5RGQDBFV.js → activities-turn-7EAQXW6Q.js} +828 -540
  32. package/dist/activities-turn-7EAQXW6Q.js.map +1 -0
  33. package/dist/activities.d.ts +2 -0
  34. package/dist/{chunk-O4HDX5SC.js → chunk-S2QVRVJW.js} +227 -36
  35. package/dist/chunk-S2QVRVJW.js.map +1 -0
  36. package/dist/{chunk-NAVSAZ56.js → chunk-TJIPCHMP.js} +162 -2
  37. package/dist/chunk-TJIPCHMP.js.map +1 -0
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.js +69 -6
  40. package/dist/index.js.map +1 -1
  41. package/dist/observability-metrics.d.ts +29 -0
  42. package/dist/personal-github-git-credentials.d.ts +1 -0
  43. package/dist/unhandled-rejection-boundary.d.ts +29 -0
  44. package/dist/workflow-bundle.js +26 -3
  45. package/dist/workflows/activities.d.ts +5 -0
  46. package/dist/workflows/automations.d.ts +5 -0
  47. package/package.json +18 -18
  48. package/src/activities/agent-run-admission.ts +7 -1
  49. package/src/activities/agent-turn/admission.ts +28 -6
  50. package/src/activities/agent-turn/agent-build.ts +15 -31
  51. package/src/activities/agent-turn/claim.ts +53 -72
  52. package/src/activities/agent-turn/codex.ts +21 -12
  53. package/src/activities/agent-turn/compaction-prep.ts +52 -1
  54. package/src/activities/agent-turn/errors.ts +78 -0
  55. package/src/activities/agent-turn/failure-settlement.ts +30 -19
  56. package/src/activities/agent-turn/finalization.ts +24 -21
  57. package/src/activities/agent-turn/history.ts +5 -1
  58. package/src/activities/agent-turn/list-models.ts +101 -0
  59. package/src/activities/agent-turn/model-usage.ts +88 -42
  60. package/src/activities/agent-turn/quiescence.ts +86 -0
  61. package/src/activities/agent-turn/run-credentials.ts +12 -1
  62. package/src/activities/agent-turn/run.ts +20 -14
  63. package/src/activities/agent-turn/sandbox-establish.ts +46 -11
  64. package/src/activities/agent-turn/sandbox-route.ts +21 -25
  65. package/src/activities/agent-turn/sandbox-runtime.ts +1 -116
  66. package/src/activities/agent-turn/sandbox-shared-preparation.ts +206 -0
  67. package/src/activities/agent-turn/stream-attempt.ts +106 -14
  68. package/src/activities/agent-turn/tool-environment.ts +72 -14
  69. package/src/activities/agent-turn/tool-policy.ts +7 -6
  70. package/src/activities/agent-turn/turn-context.ts +12 -14
  71. package/src/activities/agent-turn/{image-reference-sandbox.ts → turn-sandbox-access.ts} +15 -13
  72. package/src/activities/agent-turn/video-reference-sandbox.ts +23 -0
  73. package/src/activities/automations.ts +122 -67
  74. package/src/activities/capabilities.ts +72 -2
  75. package/src/activities/codex-capacity.ts +13 -5
  76. package/src/activities/codex-rotation.ts +17 -0
  77. package/src/activities/goals.ts +117 -35
  78. package/src/activities/image-generation-references.ts +9 -9
  79. package/src/activities/retained-screenshots.ts +104 -21
  80. package/src/activities/sandbox-lease.ts +7 -4
  81. package/src/activities/scheduled-tasks.ts +248 -30
  82. package/src/activities/session-state.ts +8 -1
  83. package/src/activities/site-auth-maintenance.ts +12 -12
  84. package/src/activities/types.ts +6 -0
  85. package/src/activities.ts +1 -0
  86. package/src/activity-services.ts +4 -1
  87. package/src/github-rest-mcp.ts +8 -4
  88. package/src/http.ts +3 -0
  89. package/src/index.ts +38 -4
  90. package/src/observability-metrics.ts +202 -0
  91. package/src/personal-github-git-credentials.ts +45 -4
  92. package/src/sandbox-resume.ts +1 -1
  93. package/src/sandbox-routing.ts +7 -4
  94. package/src/unhandled-rejection-boundary.ts +73 -0
  95. package/src/workflows/activities.ts +15 -0
  96. package/src/workflows/automations.ts +21 -2
  97. package/dist/activities/agent-turn/image-reference-sandbox.d.ts +0 -14
  98. package/dist/activities/recording.d.ts +0 -90
  99. package/dist/activities-control-VZIIV7RX.js.map +0 -1
  100. package/dist/activities-turn-5RGQDBFV.js.map +0 -1
  101. package/dist/chunk-NAVSAZ56.js.map +0 -1
  102. package/dist/chunk-O4HDX5SC.js.map +0 -1
  103. package/src/activities/recording.ts +0 -279
@@ -1,9 +1,15 @@
1
- import { resolveTurnExecutionPolicyV1 } from "@opengeni/config";
1
+ import {
2
+ resolveTurnExecutionPolicyV1,
3
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
4
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
5
+ } from "@opengeni/config";
2
6
  import {
3
7
  assertWorkspaceModelPolicyAllows,
4
8
  canonicalConfiguredModel,
5
9
  createAndStartSessionWithOutcome,
6
10
  recordWorkspaceUsage,
11
+ resolveCatalogSettings,
12
+ resolveWorkspaceCatalogSettings,
7
13
  } from "@opengeni/core";
8
14
  import {
9
15
  AutomationAuthorityRevokedError,
@@ -40,10 +46,23 @@ export function createAutomationActivities(
40
46
  const assertModelPolicy = options.assertModelPolicy ?? assertWorkspaceModelPolicyAllows;
41
47
  const recordUsage = options.recordUsage ?? recordWorkspaceUsage;
42
48
  return {
49
+ settleAutomationRunFailure: async (input: DispatchAutomationRunInput): Promise<void> => {
50
+ const service = await services();
51
+ await settle(service.db, {
52
+ workspaceId: input.workspaceId,
53
+ runId: input.runId,
54
+ status: "failed",
55
+ errorCode: "dispatch_failed",
56
+ });
57
+ },
43
58
  dispatchAutomationRun: async (
44
59
  input: DispatchAutomationRunInput,
45
60
  ): Promise<DispatchAutomationRunResult> => {
46
61
  const service = await services();
62
+ const catalogSourceSettings = service.catalogSourceSettings ?? service.settings;
63
+ const deploymentCatalogSettings = (
64
+ await resolveCatalogSettings(service.db, catalogSourceSettings)
65
+ ).settings;
47
66
  const run = await claim(service.db, input);
48
67
  if (!run) return { action: "not_found" };
49
68
  if (run.status === "dispatched") {
@@ -51,71 +70,102 @@ export function createAutomationActivities(
51
70
  return { action: "already_dispatched", sessionId: run.sessionId };
52
71
  }
53
72
  if (run.status === "skipped") {
54
- return { action: "skipped", reason: run.errorCode ?? "authority_revoked" };
73
+ return {
74
+ action: "skipped",
75
+ reason: run.errorCode ?? "authority_revoked",
76
+ };
55
77
  }
56
-
57
- const accepted = run.acceptedExecution;
58
- if (
59
- accepted.accountId !== input.accountId ||
60
- accepted.workspaceId !== input.workspaceId ||
61
- accepted.sourceId !== run.sourceId ||
62
- accepted.triggerId !== run.triggerId ||
63
- accepted.triggerRevision !== run.triggerRevision ||
64
- accepted.eventId !== run.eventId
65
- ) {
66
- await settle(service.db, {
67
- workspaceId: input.workspaceId,
68
- runId: run.id,
69
- status: "skipped",
70
- errorCode: "accepted_execution_mismatch",
71
- });
72
- return { action: "skipped", reason: "accepted_execution_mismatch" };
78
+ if (run.status === "failed") {
79
+ return { action: "failed", reason: run.errorCode ?? "dispatch_failed" };
73
80
  }
74
81
 
75
- const template = accepted.sessionTemplate;
76
- const model = canonicalConfiguredModel(
77
- service.settings,
78
- template.model ?? service.settings.openaiModel,
79
- );
80
- if (!model) throw new Error("automation has no configured model");
81
- await assertModelPolicy(service.db, service.settings, input.workspaceId, model);
82
- const denial = await admit(service, {
83
- accountId: input.accountId,
84
- workspaceId: input.workspaceId,
85
- model,
86
- requestedAgentRuns: 1,
87
- });
88
- if (denial) {
89
- await settle(service.db, {
82
+ try {
83
+ const accepted = run.acceptedExecution;
84
+ if (
85
+ accepted.accountId !== input.accountId ||
86
+ accepted.workspaceId !== input.workspaceId ||
87
+ accepted.sourceId !== run.sourceId ||
88
+ accepted.triggerId !== run.triggerId ||
89
+ accepted.triggerRevision !== run.triggerRevision ||
90
+ accepted.eventId !== run.eventId
91
+ ) {
92
+ await settle(service.db, {
93
+ workspaceId: input.workspaceId,
94
+ runId: run.id,
95
+ status: "skipped",
96
+ errorCode: "accepted_execution_mismatch",
97
+ });
98
+ return { action: "skipped", reason: "accepted_execution_mismatch" };
99
+ }
100
+
101
+ const template = accepted.sessionTemplate;
102
+ const requestedModel = template.model ?? deploymentCatalogSettings.openaiModel;
103
+ const catalogSettings =
104
+ requestedModel.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
105
+ requestedModel.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX)
106
+ ? (
107
+ await resolveWorkspaceCatalogSettings(service.db, catalogSourceSettings, {
108
+ accountId: input.accountId,
109
+ workspaceId: input.workspaceId,
110
+ retainedProductModelId: requestedModel,
111
+ })
112
+ ).settings
113
+ : deploymentCatalogSettings;
114
+ const catalogService = { ...service, settings: catalogSettings };
115
+ let model: string;
116
+ try {
117
+ const configuredModel = canonicalConfiguredModel(catalogSettings, requestedModel);
118
+ if (!configuredModel) throw new Error("automation has no configured model");
119
+ await assertModelPolicy(service.db, catalogSettings, input.workspaceId, configuredModel);
120
+ model = configuredModel;
121
+ } catch (error) {
122
+ if (!isUnprocessableEntity(error)) throw error;
123
+ await settle(service.db, {
124
+ workspaceId: input.workspaceId,
125
+ runId: run.id,
126
+ status: "failed",
127
+ errorCode: "dispatch_failed",
128
+ });
129
+ return { action: "failed", reason: "dispatch_failed" };
130
+ }
131
+ const denial = await admit(catalogService, {
132
+ accountId: input.accountId,
90
133
  workspaceId: input.workspaceId,
91
- runId: run.id,
92
- status: "failed",
93
- errorCode: denial,
134
+ model,
135
+ requestedAgentRuns: 1,
94
136
  });
95
- throw new Error(`automation admission denied: ${denial}`);
96
- }
97
- const reasoningEffort = template.reasoningEffort ?? service.settings.openaiReasoningEffort;
98
- const sandboxBackend = template.sandboxBackend ?? service.settings.sandboxBackend;
99
- if (sandboxBackend === "selfhosted") {
100
- await settle(service.db, {
101
- workspaceId: input.workspaceId,
102
- runId: run.id,
103
- status: "skipped",
104
- errorCode: "interactive_compute_not_allowed",
137
+ if (denial) {
138
+ await settle(service.db, {
139
+ workspaceId: input.workspaceId,
140
+ runId: run.id,
141
+ status: "failed",
142
+ errorCode: denial,
143
+ });
144
+ return { action: "failed", reason: denial };
145
+ }
146
+ const reasoningEffort = template.reasoningEffort ?? catalogSettings.openaiReasoningEffort;
147
+ const sandboxBackend = template.sandboxBackend ?? catalogSettings.sandboxBackend;
148
+ if (sandboxBackend === "selfhosted") {
149
+ await settle(service.db, {
150
+ workspaceId: input.workspaceId,
151
+ runId: run.id,
152
+ status: "skipped",
153
+ errorCode: "interactive_compute_not_allowed",
154
+ });
155
+ return {
156
+ action: "skipped",
157
+ reason: "interactive_compute_not_allowed",
158
+ };
159
+ }
160
+ const turnExecutionPolicy = resolveTurnExecutionPolicyV1(catalogSettings, {
161
+ modelId: model,
162
+ requestedModelId: template.model,
163
+ modelSource: template.model ? "explicit" : "deployment",
164
+ reasoningEffort,
165
+ reasoningSource: template.reasoningEffort ? "explicit" : "deployment",
166
+ latencyMode: "standard",
167
+ latencyModeSource: "deployment",
105
168
  });
106
- return { action: "skipped", reason: "interactive_compute_not_allowed" };
107
- }
108
- const turnExecutionPolicy = resolveTurnExecutionPolicyV1(service.settings, {
109
- modelId: model,
110
- requestedModelId: template.model,
111
- modelSource: template.model ? "explicit" : "deployment",
112
- reasoningEffort,
113
- reasoningSource: template.reasoningEffort ? "explicit" : "deployment",
114
- latencyMode: "standard",
115
- latencyModeSource: "deployment",
116
- });
117
-
118
- try {
119
169
  const created = await createSession({
120
170
  db: service.db,
121
171
  bus: service.bus,
@@ -163,11 +213,14 @@ export function createAutomationActivities(
163
213
  policyRole: template.policyRole,
164
214
  firstPartyMcpPermissions: template.firstPartyMcpPermissions,
165
215
  firstPartyMcpTools: template.firstPartyMcpTools,
216
+ retainWorkspaceCustomModel:
217
+ model.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
218
+ model.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX),
166
219
  createIdempotencyKey: `automation-run:${run.id}`,
167
220
  subjectId: accepted.serviceSubjectId,
168
221
  });
169
222
  await recordUsage(
170
- { db: service.db, settings: service.settings },
223
+ { db: service.db, settings: catalogSettings },
171
224
  {
172
225
  accountId: input.accountId,
173
226
  workspaceId: input.workspaceId,
@@ -201,14 +254,16 @@ export function createAutomationActivities(
201
254
  });
202
255
  return { action: "skipped", reason: "authority_revoked" };
203
256
  }
204
- await settle(service.db, {
205
- workspaceId: input.workspaceId,
206
- runId: run.id,
207
- status: "failed",
208
- errorCode: "dispatch_failed",
209
- });
210
257
  throw error;
211
258
  }
212
259
  },
213
260
  };
214
261
  }
262
+
263
+ function isUnprocessableEntity(error: unknown): boolean {
264
+ return (
265
+ error instanceof Error &&
266
+ "status" in error &&
267
+ (error as Error & { status?: unknown }).status === 422
268
+ );
269
+ }
@@ -1,17 +1,26 @@
1
1
  import {
2
2
  environmentsEncryptionKeyBytes,
3
3
  type Settings,
4
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
5
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
4
6
  withCodexCatalogProvider,
5
7
  withWorkspaceGatewayCatalogProvider,
6
8
  withWorkspaceGatewayCredential,
9
+ withWorkspaceOpenRouterCatalogProvider,
10
+ withWorkspaceOpenRouterCredential,
7
11
  withXaiSubscriptionCatalogProvider,
8
12
  } from "@opengeni/config";
9
13
  import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
10
14
  import {
15
+ getWorkspaceGatewayCustomModelForExecution,
16
+ getWorkspaceOpenRouterCustomModelForExecution,
11
17
  listSessionMcpServerMetadata,
12
18
  listSessionMcpServersForRun,
19
+ listWorkspaceGatewayCustomModels,
20
+ listWorkspaceOpenRouterCustomModels,
13
21
  workspaceCodexSubscriptionActive,
14
22
  loadWorkspaceVercelAiGatewayApiKey,
23
+ loadWorkspaceOpenRouterApiKey,
15
24
  type Database,
16
25
  type SessionMcpServerForRun,
17
26
  } from "@opengeni/db";
@@ -126,10 +135,71 @@ export function withXaiSubscriptionProvider(settings: Settings): Settings {
126
135
 
127
136
  export async function settingsWithWorkspaceGatewayCredential(
128
137
  db: Database,
138
+ accountId: string,
129
139
  workspaceId: string,
130
140
  settings: Settings,
141
+ retainedProductModelId?: string | null,
131
142
  ): Promise<Settings> {
132
- const catalogSettings = withWorkspaceGatewayCatalogProvider(settings);
143
+ const activeCustomModels = await listWorkspaceGatewayCustomModels(db, { accountId, workspaceId });
144
+ const retainedUpstreamModelId = retainedProductModelId?.startsWith(
145
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
146
+ )
147
+ ? retainedProductModelId.slice(WORKSPACE_GATEWAY_MODEL_ID_PREFIX.length)
148
+ : null;
149
+ const retainedCustomModel = retainedUpstreamModelId
150
+ ? await getWorkspaceGatewayCustomModelForExecution(db, {
151
+ accountId,
152
+ workspaceId,
153
+ upstreamModelId: retainedUpstreamModelId,
154
+ })
155
+ : null;
156
+ const customModels =
157
+ retainedCustomModel &&
158
+ !activeCustomModels.some(
159
+ (model) => model.upstreamModelId === retainedCustomModel.upstreamModelId,
160
+ )
161
+ ? [...activeCustomModels, retainedCustomModel]
162
+ : activeCustomModels;
163
+ const catalogSettings = withWorkspaceGatewayCatalogProvider(settings, customModels);
133
164
  const apiKey = await loadWorkspaceVercelAiGatewayApiKey(db, settings, workspaceId);
134
- return apiKey ? withWorkspaceGatewayCredential(catalogSettings, apiKey) : catalogSettings;
165
+ return apiKey
166
+ ? withWorkspaceGatewayCredential(catalogSettings, apiKey, customModels)
167
+ : catalogSettings;
168
+ }
169
+
170
+ export async function settingsWithWorkspaceOpenRouterCredential(
171
+ db: Database,
172
+ accountId: string,
173
+ workspaceId: string,
174
+ settings: Settings,
175
+ retainedProductModelId?: string | null,
176
+ ): Promise<Settings> {
177
+ const activeCustomModels = await listWorkspaceOpenRouterCustomModels(db, {
178
+ accountId,
179
+ workspaceId,
180
+ });
181
+ const retainedUpstreamModelId = retainedProductModelId?.startsWith(
182
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
183
+ )
184
+ ? retainedProductModelId.slice(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX.length)
185
+ : null;
186
+ const retainedCustomModel = retainedUpstreamModelId
187
+ ? await getWorkspaceOpenRouterCustomModelForExecution(db, {
188
+ accountId,
189
+ workspaceId,
190
+ upstreamModelId: retainedUpstreamModelId,
191
+ })
192
+ : null;
193
+ const customModels =
194
+ retainedCustomModel &&
195
+ !activeCustomModels.some(
196
+ (model) => model.upstreamModelId === retainedCustomModel.upstreamModelId,
197
+ )
198
+ ? [...activeCustomModels, retainedCustomModel]
199
+ : activeCustomModels;
200
+ const catalogSettings = withWorkspaceOpenRouterCatalogProvider(settings, customModels);
201
+ const apiKey = await loadWorkspaceOpenRouterApiKey(db, settings, workspaceId);
202
+ return apiKey
203
+ ? withWorkspaceOpenRouterCredential(catalogSettings, apiKey, customModels)
204
+ : catalogSettings;
135
205
  }
@@ -11,8 +11,8 @@ import {
11
11
  } from "@opengeni/db";
12
12
  import type { Settings } from "@opengeni/config";
13
13
  import {
14
- CODEX_USAGE_EXHAUSTED_PCT,
15
14
  authoritativeCodexCapacityResetAt,
15
+ codexAccountNeedsLiveCapacityRefresh,
16
16
  isCodexCredentialEligible,
17
17
  isCodexCredentialHealthy,
18
18
  selectCodexCredentialLeaseForTurn,
@@ -119,10 +119,19 @@ export function codexCapacityDecision(
119
119
  };
120
120
  }
121
121
  const authoritativeReset = authoritativeCodexCapacityResetAt(context.accounts, now);
122
+ const hasReconcilableQuotaCooldown = context.accounts.some(
123
+ (account) =>
124
+ account.status === "active" &&
125
+ account.allocatorEnabled &&
126
+ account.exhaustedKind === "quota" &&
127
+ account.exhaustedUntil !== null &&
128
+ account.exhaustedUntil > now,
129
+ );
122
130
  return {
123
131
  kind: "unavailable",
124
132
  earliestResetAt: authoritativeReset,
125
- resetKind: authoritativeReset ? "authoritative" : "bounded_refresh",
133
+ resetKind:
134
+ authoritativeReset && !hasReconcilableQuotaCooldown ? "authoritative" : "bounded_refresh",
126
135
  diagnostic: {
127
136
  connectedCount: context.accounts.length,
128
137
  allocatorEnabledCount: context.accounts.filter((account) => account.allocatorEnabled).length,
@@ -136,13 +145,12 @@ async function refreshCapacityMetadata(
136
145
  workspaceId: string,
137
146
  ): Promise<void> {
138
147
  const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
148
+ const now = new Date();
139
149
  const stale = accounts.filter(
140
150
  (account) =>
141
151
  account.allocatorEnabled &&
142
152
  account.status === "active" &&
143
- ((account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
144
- (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
145
- account.usageCheckedAt === null),
153
+ (codexAccountNeedsLiveCapacityRefresh(account, now) || account.usageCheckedAt === null),
146
154
  );
147
155
  await refreshCodexUsageAndRepairCapacityWaiters(
148
156
  stale.map(
@@ -15,6 +15,23 @@ import type {
15
15
 
16
16
  export type CodexRotationAccount = CodexAccountStatus | CodexLeaseAccountStatus;
17
17
 
18
+ /** Capacity metadata worth an authoritative live read before/all through an idle. */
19
+ export function codexAccountNeedsLiveCapacityRefresh(
20
+ account: Pick<
21
+ CodexAccountStatus,
22
+ "primaryUsedPercent" | "secondaryUsedPercent" | "exhaustedUntil" | "exhaustedKind"
23
+ >,
24
+ now: Date,
25
+ ): boolean {
26
+ return (
27
+ (account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
28
+ (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
29
+ (account.exhaustedKind === "quota" &&
30
+ account.exhaustedUntil !== null &&
31
+ account.exhaustedUntil > now)
32
+ );
33
+ }
34
+
18
35
  export type CodexRotationStrategy =
19
36
  | "most_remaining"
20
37
  | "round_robin"
@@ -2,6 +2,12 @@ import {
2
2
  allowedFirstPartyMcpToolsForSession,
3
3
  configuredStaticUsageLimits,
4
4
  policyProviderIdForModel,
5
+ resolveModelProvider,
6
+ resolveTurnExecutionPolicyV1,
7
+ withCodexCatalogProvider,
8
+ withXaiSubscriptionCatalogProvider,
9
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
10
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
5
11
  type Settings,
6
12
  } from "@opengeni/config";
7
13
  import {
@@ -28,6 +34,11 @@ import type {
28
34
  MaybeContinueGoalInput,
29
35
  MaybeContinueGoalResult,
30
36
  } from "./types";
37
+ import {
38
+ modelFundingForAdmission,
39
+ resolveCatalogSettings,
40
+ resolveWorkspaceCatalogSettings,
41
+ } from "@opengeni/core";
31
42
 
32
43
  export function createGoalActivities(services: () => Promise<ControlActivityServices>) {
33
44
  async function enqueueGoalRetryWake(input: MaybeContinueGoalInput): Promise<void> {
@@ -47,12 +58,15 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
47
58
  async function maybeContinueGoal(
48
59
  input: MaybeContinueGoalInput,
49
60
  ): Promise<MaybeContinueGoalResult> {
50
- const { settings, db, bus } = await services();
61
+ const service = await services();
62
+ const { db, bus } = service;
63
+ const catalogSourceSettings = service.catalogSourceSettings ?? service.settings;
51
64
  // Cheap pre-read: the common goal-less session skips the budget queries.
52
65
  const existingGoal = await getSessionGoal(db, input.workspaceId, input.sessionId);
53
66
  if (!existingGoal || existingGoal.status !== "active") {
54
67
  return { action: "none" };
55
68
  }
69
+ let settings = (await resolveCatalogSettings(db, catalogSourceSettings)).settings;
56
70
  // Loaded before the budget check so the codex-billed predicate and the
57
71
  // synthesized turn use the SAME effective policy. An explicit per-turn
58
72
  // model can differ from the persisted session default; follow-up goal work
@@ -66,34 +80,34 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
66
80
  input.workspaceId,
67
81
  input.sessionId,
68
82
  );
69
- let continuationModel = latestStartedTurn?.model ?? session.model;
83
+ const inheritedContinuationModel = latestStartedTurn?.model ?? session.model;
84
+ let continuationModel = inheritedContinuationModel;
70
85
  const continuationReasoningEffort =
71
86
  latestStartedTurn?.reasoningEffort ?? session.reasoningEffort;
72
87
  const continuationLatencyMode = latestStartedTurn?.latencyMode ?? session.latencyMode;
73
- // Workspace model policy: a continuation inherits the last STARTED turn's
74
- // model, so a single policy-violating turn would otherwise re-arm itself on
75
- // every continuation (exactly how one bare-model turn kept a goal loop on
76
- // the paid built-in provider all night). If the inherited model is blocked
77
- // but the session's own default is allowed, recover to the default; if
78
- // both are blocked, pause the goal visibly (the budget-pause channel, with
79
- // a truthful rationale) instead of synthesizing a turn the worker's hard
80
- // gate would fail over and over.
81
- let modelPolicyBlocked: string | null = null;
82
88
  const workspaceModelPolicy = await getWorkspaceModelPolicy(db, input.workspaceId);
83
- if (workspaceModelPolicy) {
84
- const policyBlocks = (modelId: string): boolean =>
85
- !evaluateWorkspaceModelPolicy(workspaceModelPolicy, {
86
- providerId: policyProviderIdForModel(settings, modelId),
87
- modelId,
88
- }).allowed;
89
- if (policyBlocks(continuationModel)) {
90
- if (continuationModel !== session.model && !policyBlocks(session.model)) {
91
- continuationModel = session.model;
92
- } else {
93
- modelPolicyBlocked = `workspace model policy blocks model "${continuationModel}"; pick an allowed model or change the workspace model policy`;
94
- }
95
- }
89
+ if (
90
+ inheritedContinuationModel.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
91
+ inheritedContinuationModel.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX) ||
92
+ session.model.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
93
+ session.model.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX)
94
+ ) {
95
+ settings = (
96
+ await resolveWorkspaceCatalogSettings(db, catalogSourceSettings, {
97
+ accountId: input.accountId,
98
+ workspaceId: input.workspaceId,
99
+ retainedProductModelIds: [inheritedContinuationModel, session.model],
100
+ })
101
+ ).settings;
96
102
  }
103
+ const modelDecision = goalContinuationModelDecision({
104
+ settings,
105
+ workspaceModelPolicy,
106
+ inheritedModel: inheritedContinuationModel,
107
+ sessionModel: session.model,
108
+ });
109
+ continuationModel = modelDecision.model;
110
+ let modelPolicyBlocked = modelDecision.blocked;
97
111
  // remote_v2 sessions may only continue on Codex models — refuse synthesis
98
112
  // that would leave the portable/non-Codex path (and mixed history shapes).
99
113
  if (
@@ -112,6 +126,11 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
112
126
  workspaceId: input.workspaceId,
113
127
  model: continuationModel,
114
128
  });
129
+ const fundedWithoutCredits = goalContinuationFundedWithoutCredits(
130
+ settings,
131
+ continuationModel,
132
+ isCodexRun,
133
+ );
115
134
  // Budget exhaustion pauses the goal visibly instead of failing the
116
135
  // session. Computed up front and applied inside the locked decision so a
117
136
  // limits pause never consumes continuation budget.
@@ -120,8 +139,17 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
120
139
  db,
121
140
  input.accountId,
122
141
  input.workspaceId,
123
- isCodexRun,
142
+ fundedWithoutCredits,
124
143
  );
144
+ const turnExecutionPolicy = resolveTurnExecutionPolicyV1(settings, {
145
+ modelId: continuationModel,
146
+ requestedModelId: null,
147
+ modelSource: "continuation",
148
+ reasoningEffort: continuationReasoningEffort,
149
+ reasoningSource: "continuation",
150
+ latencyMode: continuationLatencyMode,
151
+ latencyModeSource: "continuation",
152
+ });
125
153
  const decision = await materializeGoalContinuation(db, {
126
154
  accountId: input.accountId,
127
155
  workspaceId: input.workspaceId,
@@ -142,6 +170,7 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
142
170
  model: continuationModel,
143
171
  reasoningEffort: continuationReasoningEffort,
144
172
  latencyMode: continuationLatencyMode,
173
+ turnExecutionPolicy,
145
174
  tools: withFirstPartyTools(settings, session.tools),
146
175
  sandboxBackend: session.sandboxBackend,
147
176
  },
@@ -174,6 +203,53 @@ export function createGoalActivities(services: () => Promise<ControlActivityServ
174
203
  };
175
204
  }
176
205
 
206
+ export function goalContinuationModelDecision(input: {
207
+ settings: Settings;
208
+ workspaceModelPolicy: Awaited<ReturnType<typeof getWorkspaceModelPolicy>>;
209
+ inheritedModel: string;
210
+ sessionModel: string;
211
+ }): { model: string; blocked: string | null } {
212
+ const catalogSettings = input.settings.supergrokSubscriptionEnabled
213
+ ? withXaiSubscriptionCatalogProvider(
214
+ input.settings.codexSubscriptionEnabled
215
+ ? withCodexCatalogProvider(input.settings)
216
+ : input.settings,
217
+ )
218
+ : input.settings.codexSubscriptionEnabled
219
+ ? withCodexCatalogProvider(input.settings)
220
+ : input.settings;
221
+ const policyBlocks = (modelId: string): boolean =>
222
+ input.workspaceModelPolicy !== null &&
223
+ !evaluateWorkspaceModelPolicy(input.workspaceModelPolicy, {
224
+ providerId: policyProviderIdForModel(catalogSettings, modelId),
225
+ modelId,
226
+ }).allowed;
227
+ const candidates = [...new Set([input.inheritedModel, input.sessionModel])];
228
+ for (const model of candidates) {
229
+ if (resolveModelProvider(catalogSettings, model) && !policyBlocks(model)) {
230
+ return { model, blocked: null };
231
+ }
232
+ }
233
+ if (!resolveModelProvider(catalogSettings, input.inheritedModel)) {
234
+ return {
235
+ model: input.inheritedModel,
236
+ blocked: `model "${input.inheritedModel}" is no longer in the deployment or workspace catalog; choose an available model before resuming the goal`,
237
+ };
238
+ }
239
+ return {
240
+ model: input.inheritedModel,
241
+ blocked: `workspace model policy blocks model "${input.inheritedModel}"; pick an allowed model or change the workspace model policy`,
242
+ };
243
+ }
244
+
245
+ export function goalContinuationFundedWithoutCredits(
246
+ settings: Settings,
247
+ model: string,
248
+ codexBilled: boolean,
249
+ ): boolean {
250
+ return modelFundingForAdmission(settings, model, codexBilled).fundedWithoutCredits;
251
+ }
252
+
177
253
  export function goalContinuationPrompt(
178
254
  _goal: SessionGoal,
179
255
  _autoContinuation: number,
@@ -200,15 +276,21 @@ export function goalContinuationPrompt(
200
276
  "",
201
277
  ];
202
278
  return [
203
- "Continue working toward the active session goal.",
279
+ "Reconcile the active session goal against authoritative current state, then carry the work through to the full requested end state and verify it.",
280
+ "",
281
+ "Goal recovery:",
282
+ "- Treat this continuation as re-entry into the full objective, not as a request to perform one step and stop.",
283
+ "- Do not rely on previous assistant claims of progress or completion; use them only to locate authoritative evidence.",
284
+ "- If authoritative evidence already proves the full objective, call opengeni__goal_complete instead of manufacturing more work.",
285
+ "- Before repeating a state-setting action, verify whether its desired state already holds. If it does, do not repeat it; continue reconciling the overall goal.",
204
286
  "",
205
287
  "Continuation behavior:",
206
- "- This goal persists across turns. Ending this turn does not require shrinking the objective to what fits now.",
207
- "- Keep the full objective intact. If it cannot be finished now, make concrete progress toward the real requested end state, leave the goal active, and do not redefine success around a smaller or easier task.",
288
+ "- This goal persists across turns. A runtime boundary can end one turn without shrinking the objective; the next continuation resumes the same full objective.",
289
+ "- Keep working until the requested end state is true and verified. Do not end the turn merely because one useful action completed, and do not redefine success around a smaller or easier task.",
208
290
  "- Temporary rough edges are acceptable while the work is moving in the right direction. Completion still requires the requested end state to be true and verified.",
209
291
  "",
210
292
  "Work from evidence:",
211
- "Use the current workspace and external state as authoritative. Previous conversation context can help locate relevant work, but inspect the current state before relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
293
+ "Use the current workspace and external state as authoritative. Conversation context can help locate relevant work, but it is not proof of the current state. Inspect the current state before relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
212
294
  "",
213
295
  "Progress visibility:",
214
296
  "If a planning tool is available and the next work is meaningfully multi-step, use it to show a concise plan tied to the real objective. Keep the plan current as steps complete or the next best action changes. Skip planning overhead for trivial one-step progress, and do not treat a plan update as a substitute for doing the work.",
@@ -263,13 +345,13 @@ async function goalRunBudgetBlocked(
263
345
  db: Database,
264
346
  accountId: string,
265
347
  workspaceId: string,
266
- isCodexRun: boolean,
348
+ fundedWithoutCredits: boolean,
267
349
  ): Promise<string | null> {
268
- // Codex-billed continuations are paid by the user's ChatGPT/Codex plan: skip
269
- // the credit-balance gate and the monthly model-cost cap. The agent-run COUNT
270
- // cap below is a volume quota (not a credit/cost gate) and is intentionally kept.
350
+ // Free, subscription, and workspace-funded continuations skip OpenGeni's
351
+ // credit-balance gate and monthly model-cost cap. The agent-run COUNT cap
352
+ // below is a volume quota (not a credit/cost gate) and remains enforced.
271
353
  if (
272
- !isCodexRun &&
354
+ !fundedWithoutCredits &&
273
355
  (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
274
356
  ) {
275
357
  const balance = await getBillingBalance(db, accountId);
@@ -279,7 +361,7 @@ async function goalRunBudgetBlocked(
279
361
  }
280
362
  if (settings.usageLimitsMode === "static" || settings.usageLimitsMode === "managed") {
281
363
  const limits = configuredStaticUsageLimits(settings);
282
- if (!isCodexRun && limits.maxMonthlyCostMicrosPerAccount) {
364
+ if (!fundedWithoutCredits && limits.maxMonthlyCostMicrosPerAccount) {
283
365
  const used = await sumUsageQuantity(db, {
284
366
  accountId,
285
367
  eventType: "model.cost",