@opengeni/worker-bundle 0.12.2 → 0.12.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.12.2",
3
+ "version": "0.12.7",
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": {
@@ -41,16 +41,16 @@
41
41
  "dependencies": {
42
42
  "@opengeni/agent-proto": "^0.3.0",
43
43
  "@opengeni/codex": "^0.2.7",
44
- "@opengeni/config": "^0.7.8",
45
- "@opengeni/contracts": "^0.20.1",
46
- "@opengeni/core": "^0.12.2",
47
- "@opengeni/db": "^0.13.1",
48
- "@opengeni/documents": "^0.2.38",
49
- "@opengeni/events": "^0.3.29",
50
- "@opengeni/github": "^0.3.20",
44
+ "@opengeni/config": "^0.7.11",
45
+ "@opengeni/contracts": "^0.22.0",
46
+ "@opengeni/core": "^0.12.7",
47
+ "@opengeni/db": "^0.14.0",
48
+ "@opengeni/documents": "^0.2.42",
49
+ "@opengeni/events": "^0.3.33",
50
+ "@opengeni/github": "^0.3.23",
51
51
  "@opengeni/observability": "^0.3.0",
52
- "@opengeni/runtime": "^0.13.13",
53
- "@opengeni/storage": "^0.2.32",
52
+ "@opengeni/runtime": "^0.14.1",
53
+ "@opengeni/storage": "^0.2.35",
54
54
  "@temporalio/activity": "^1.17.0",
55
55
  "@temporalio/client": "^1.17.0",
56
56
  "@temporalio/worker": "^1.17.0",
@@ -82,7 +82,6 @@ import {
82
82
  normalizeModelCallUsage,
83
83
  normalizeSdkEvent,
84
84
  sanitizeHistoryItemsForModel,
85
- isEphemeralInternalContext,
86
85
  appendPersistentSessionSettings,
87
86
  appendSessionInstructions,
88
87
  appendWorkspaceMemory,
@@ -222,7 +221,7 @@ import {
222
221
  runCredentialAuthNeededPayloads,
223
222
  runCredentialModelNote,
224
223
  } from "./run-credentials";
225
- import { withCodexAppsTool, withFirstPartyTools } from "./goals";
224
+ import { withFirstPartyTools } from "./goals";
226
225
  import {
227
226
  mergeRigDefaultVariableSetEnvironment,
228
227
  resolveWorkspaceAgentInstructions,
@@ -297,6 +296,7 @@ import {
297
296
  } from "@opengeni/runtime";
298
297
  import {
299
298
  CAPABILITY_DESCRIPTORS,
299
+ DEFAULT_FIRST_PARTY_MCP_TOOLS,
300
300
  evaluateWorkspaceModelPolicy,
301
301
  readTurnExecutionPolicyV1,
302
302
  type ResourceRef,
@@ -305,6 +305,7 @@ import {
305
305
  type SessionStatus,
306
306
  type SessionTurn,
307
307
  type TurnExecutionPolicyV1,
308
+ type TurnInitiator,
308
309
  } from "@opengeni/contracts";
309
310
  import { createHash, randomUUID } from "node:crypto";
310
311
 
@@ -314,6 +315,13 @@ import { createHash, randomUUID } from "node:crypto";
314
315
  // budget against the same window.
315
316
  export const PROVIDER_BACKPRESSURE_DELAY_MS = 60_000;
316
317
 
318
+ /** Personal connection authority follows the immutable human turn initiator, never the worker. */
319
+ export function credentialSubjectIdForTurnInitiator(
320
+ initiator: Pick<TurnInitiator, "kind" | "subjectId">,
321
+ ): string | undefined {
322
+ return initiator.kind === "subject" ? initiator.subjectId : undefined;
323
+ }
324
+
317
325
  export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1): {
318
326
  externallyBilled: boolean;
319
327
  codexSubscription: boolean;
@@ -1292,16 +1300,26 @@ export function historyRowsToAppend(
1292
1300
  nextWatermark: number;
1293
1301
  nextPosition: number;
1294
1302
  } {
1295
- const sanitized = sanitizeHistoryItemsForModel(rawHistory, toolOutputTruncationTokens)
1296
- .filter((item) => !isEphemeralInternalContext(item))
1297
- .map((item) => redactValue(item) as Record<string, unknown>);
1303
+ const sanitized = sanitizeHistoryItemsForModel(rawHistory, toolOutputTruncationTokens).map(
1304
+ (item) => redactValue(item) as Record<string, unknown>,
1305
+ );
1298
1306
  if (sanitized.length <= persistedHistoryCount) {
1299
1307
  return { rows: [], nextWatermark: persistedHistoryCount, nextPosition };
1300
1308
  }
1301
- const rows = sanitized.slice(persistedHistoryCount).map((item, offset) => ({
1302
- position: nextPosition + offset,
1303
- item: item as Record<string, unknown>,
1304
- }));
1309
+ // Canonical model-facing inputs (including machine-input batches) are
1310
+ // persisted before inference and therefore live inside the prefix represented
1311
+ // by persistedHistoryCount. System messages synthesized only for this
1312
+ // attempt—recovery diagnostics, credential notices, attachment materialization
1313
+ // notes—must not accidentally become conversation memory during reconciliation.
1314
+ // Advance the in-memory watermark past them, but only allocate durable
1315
+ // positions to actual model/tool output.
1316
+ const rows = sanitized
1317
+ .slice(persistedHistoryCount)
1318
+ .filter((item) => !(item.type === "message" && item.role === "system"))
1319
+ .map((item, offset) => ({
1320
+ position: nextPosition + offset,
1321
+ item: item as Record<string, unknown>,
1322
+ }));
1305
1323
  return {
1306
1324
  rows,
1307
1325
  nextWatermark: sanitized.length,
@@ -4062,19 +4080,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4062
4080
  const turnResources = mergeResourceRefs(session.resources, turn.resources);
4063
4081
  // Attach the first-party MCP server to EVERY turn, regardless of how/when
4064
4082
  // the session was created (API, scheduled task, or a pre-existing session
4065
- // whose stored tools predate this) — so set_session_title and the rest are
4066
- // always reachable. Idempotent: mergeToolRefs dedupes if already present.
4067
- // Attach codex_apps (the ChatGPT/Codex connectors MCP) when the codex
4068
- // overlay injected it into runSettings.mcpServers (active subscription +
4069
- // connector scopes); no-op for every other turn. Its refreshing bearer is
4070
- // resolved at connect time from the codex ALS (see the withCodex-wrapped
4071
- // prepareTools call below).
4083
+ // whose stored tools predate this). The server registration is then
4084
+ // narrowed by the session's exact firstPartyMcpTools selection and
4085
+ // authorization. Idempotent: mergeToolRefs dedupes if already present.
4072
4086
  // Resolve the durable policy at the turn boundary. Workspace-default
4073
- // sessions may discover newly enabled capability MCPs, while explicit,
4074
- // inherited-fixed, and legacy sessions remain narrowed to their stored
4075
- // materialized allow-list. `withCodexAppsTool` below keeps the existing
4076
- // single Codex lazy router/connector overlay; it is not a second policy
4077
- // or discovery path.
4087
+ // sessions may discover newly enabled capability MCPs and Codex Apps,
4088
+ // while explicit, inherited-fixed, and legacy sessions remain narrowed
4089
+ // to their stored materialized allow-list.
4078
4090
  const resolvedToolPolicy = resolveSessionToolPolicy({
4079
4091
  ...(session.toolPolicy ? { toolPolicy: session.toolPolicy } : {}),
4080
4092
  sessionTools: session.tools,
@@ -4089,10 +4101,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4089
4101
  defaultNativeToolsAllowed = sessionToolPolicyAllowsDefaultNativeTools(
4090
4102
  resolvedToolPolicy.effectivePolicy,
4091
4103
  );
4092
- const turnTools = withCodexAppsTool(
4093
- runSettings,
4094
- withFirstPartyTools(runSettings, effectivePolicyTools),
4095
- );
4104
+ const turnTools = withFirstPartyTools(runSettings, effectivePolicyTools);
4096
4105
  // §7.6 connection-credential provider — load (and decrypt) the variable set via the host
4097
4106
  // `sandboxSecrets` provider when bound; unset → today's local decrypt.
4098
4107
  const connectionScope = {
@@ -4970,6 +4979,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4970
4979
  }
4971
4980
  return result;
4972
4981
  };
4982
+ const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn.initiator);
4973
4983
  preparedTools = await waitForTurnOperation(
4974
4984
  withCodex(() =>
4975
4985
  runtime.prepareTools(runSettings, turnTools, {
@@ -4984,6 +4994,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4984
4994
  executionGeneration,
4985
4995
  subjectId: "worker:first-party-mcp",
4986
4996
  subjectLabel: "OpenGeni worker",
4997
+ ...(credentialSubjectId ? { credentialSubjectId } : {}),
4987
4998
  resolveCredential,
4988
4999
  onAuthNeeded: async (payload) => {
4989
5000
  await publish!([{ type: "tool.auth_needed", payload }], true);
@@ -4993,6 +5004,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4993
5004
  ...(session.firstPartyMcpPermissions?.length
4994
5005
  ? { firstPartyPermissions: session.firstPartyMcpPermissions }
4995
5006
  : {}),
5007
+ firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
4996
5008
  }),
4997
5009
  ),
4998
5010
  cancellationSignal,
@@ -5149,6 +5161,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5149
5161
  await maybeStartOnTurnRecording(resolvedSandbox, activeSandboxBackend);
5150
5162
  },
5151
5163
  ...(packRuntime.skills.length > 0 ? { packSkills: packRuntime.skills } : {}),
5164
+ ...(session.skills.length > 0 ? { sessionSkills: session.skills } : {}),
5152
5165
  ...(skillLibraryRuntime.skillLibrarySkills.length > 0
5153
5166
  ? {
5154
5167
  skillLibrarySkills: skillLibraryRuntime.skillLibrarySkills,
@@ -3,15 +3,8 @@ import {
3
3
  type Settings,
4
4
  withCodexCatalogProvider,
5
5
  } from "@opengeni/config";
6
- import { settingsWithMcpCapabilityServers } from "@opengeni/core";
6
+ import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
7
7
  import {
8
- CODEX_APPS_MCP_SERVER_ID,
9
- CODEX_APPS_MCP_SERVER_NAME,
10
- CODEX_APPS_MCP_URL,
11
- CODEX_APPS_STARTUP_TIMEOUT_MS,
12
- } from "@opengeni/codex";
13
- import {
14
- listEnabledMcpCapabilityServers,
15
8
  listSessionMcpServerMetadata,
16
9
  listSessionMcpServersForRun,
17
10
  workspaceCodexSubscriptionActive,
@@ -19,14 +12,7 @@ import {
19
12
  type SessionMcpServerForRun,
20
13
  } from "@opengeni/db";
21
14
 
22
- export async function settingsWithEnabledCapabilityMcpServers(
23
- db: Database,
24
- workspaceId: string,
25
- settings: Settings,
26
- ): Promise<Settings> {
27
- const enabled = await listEnabledMcpCapabilityServers(db, workspaceId);
28
- return settingsWithMcpCapabilityServers(settings, enabled);
29
- }
15
+ export { settingsWithEnabledCapabilityMcpServers };
30
16
 
31
17
  export async function settingsWithSessionMcpServersForRun(
32
18
  db: Database,
@@ -117,52 +103,7 @@ export async function settingsWithCodexCredential(
117
103
  return settings; // disabled / not connected / needs_relogin / error -> leave settings unchanged
118
104
  }
119
105
  const withProvider = withCodexProvider(settings);
120
- if (!settings.codexConnectedAppsEnabled) {
121
- return withProvider;
122
- }
123
- // Additive: append the synthetic codex_apps connectors MCP server for ANY
124
- // active credential. Connector access is gated SERVER-SIDE per ChatGPT account
125
- // (via chatgpt-account-id), NOT by token scopes — confirmed live: a `pro` token
126
- // whose only scopes are openid/profile/email/offline_access still lists all 217
127
- // connector tools at .../ps/mcp. So we inject unconditionally and let
128
- // runtime-discovery decide: an account with no connectors yields an empty
129
- // tools/list, and a connect failure best-effort-drops the server without
130
- // failing the turn. The bearer is injected dynamically at connect time
131
- // (runtime/codexAppsMcpRequestInit).
132
- return withCodexAppsMcpServer(withProvider);
133
- }
134
-
135
- /**
136
- * Pure: append the synthetic codex_apps MCP server, idempotently. Connector
137
- * access is gated SERVER-SIDE per ChatGPT account (chatgpt-account-id), not by
138
- * token scopes, so we inject for any active credential and let runtime-discovery
139
- * resolve the actual tool set (empty list / dropped server when unavailable).
140
- * No secrets here — the refreshing bearer is injected at connect time from
141
- * codexRequestStorage (runtime/codexAppsMcpRequestInit). The connectors backend
142
- * tolerates serial and parallel tool invocation, so no per-server serialization
143
- * is enforced (the SDK exposes no per-server parallel-tool-calls flag in
144
- * @openai/agents 0.11.6).
145
- */
146
- export function withCodexAppsMcpServer(settings: Settings): Settings {
147
- if (settings.mcpServers.some((server) => server.id === CODEX_APPS_MCP_SERVER_ID)) {
148
- return settings; // already injected
149
- }
150
- return {
151
- ...settings,
152
- mcpServers: [
153
- ...settings.mcpServers,
154
- {
155
- id: CODEX_APPS_MCP_SERVER_ID,
156
- name: CODEX_APPS_MCP_SERVER_NAME,
157
- url: CODEX_APPS_MCP_URL,
158
- timeoutMs: CODEX_APPS_STARTUP_TIMEOUT_MS,
159
- // Connector availability is per-credential and must re-discover each
160
- // run; never poison a process-global tools-list cache.
161
- cacheToolsList: false,
162
- // deliberately NO `headers` — the refreshing bearer is dynamic
163
- },
164
- ],
165
- };
106
+ return withProvider;
166
107
  }
167
108
 
168
109
  /** Pure: append the synthetic codex-subscription provider, idempotently. */
@@ -169,20 +169,6 @@ export function withFirstPartyTools(settings: Settings, tools: ToolRef[]): ToolR
169
169
  return mergeToolRefs(tools, [{ kind: "mcp", id: "opengeni" }]);
170
170
  }
171
171
 
172
- /**
173
- * Ensures a turn references the synthetic codex_apps connectors MCP server when
174
- * the codex overlay injected it (active subscription + connector scopes). A
175
- * registry entry is inert until a ToolRef references its id, so this wires the
176
- * server into the run. No-op when the server is not configured (every non-codex
177
- * turn), and idempotent via mergeToolRefs.
178
- */
179
- export function withCodexAppsTool(settings: Settings, tools: ToolRef[]): ToolRef[] {
180
- if (!settings.mcpServers.some((server) => server.id === "codex_apps")) {
181
- return tools;
182
- }
183
- return mergeToolRefs(tools, [{ kind: "mcp", id: "codex_apps" }]);
184
- }
185
-
186
172
  /**
187
173
  * Non-throwing variant of the scheduled-run admission check: returns a human
188
174
  * readable reason when balance or monthly caps block another agent run.
@@ -1,4 +1,4 @@
1
- import type { FileAsset, ResourceRef, SessionSystemUpdate } from "@opengeni/contracts";
1
+ import type { FileAsset, ResourceRef } from "@opengeni/contracts";
2
2
  import { createHash } from "node:crypto";
3
3
  import {
4
4
  getActiveSessionHistoryItems,
@@ -313,7 +313,14 @@ export async function turnInput(
313
313
  trigger.sessionId,
314
314
  options.turnId,
315
315
  );
316
- const updateContext = systemUpdateContext(updates);
316
+ if (updates.length > 0) {
317
+ const historyItemIds = new Set(
318
+ updates.map((update) => update.deliveredHistoryItemId).filter(Boolean),
319
+ );
320
+ if (historyItemIds.size !== 1 || updates.some((update) => !update.deliveredHistoryItemId)) {
321
+ throw new Error("Delivered internal updates have no single durable model-memory batch");
322
+ }
323
+ }
317
324
  const internalContext = joinInternalContext(
318
325
  options.recovering
319
326
  ? [
@@ -321,7 +328,6 @@ export async function turnInput(
321
328
  "Continue the same inference from durable conversation and sandbox state. A previous execution stopped before it could finish. Do not repeat completed side effects; inspect actual state when uncertain.",
322
329
  ].join("\n")
323
330
  : undefined,
324
- updateContext,
325
331
  options.unavailableSandboxFilesNote,
326
332
  options.runCredentialsNote,
327
333
  );
@@ -355,7 +361,9 @@ export async function turnInput(
355
361
  );
356
362
  }
357
363
  if (trigger.type === "system.update.delivered") {
358
- if (!internalContext) throw new Error("Internal update inference has no delivered updates");
364
+ if (updates.length === 0) {
365
+ throw new Error("Internal update inference has no delivered updates");
366
+ }
359
367
  return await messageInput(db, runtime, agent, trigger, undefined, internalContext, current);
360
368
  }
361
369
  if (trigger.type === "user.approvalDecision") {
@@ -416,26 +424,7 @@ function joinInternalContext(...parts: Array<string | undefined>): string | unde
416
424
  return content.length > 0 ? content.join("\n\n") : undefined;
417
425
  }
418
426
 
419
- function systemUpdateContext(updates: SessionSystemUpdate[]): string | undefined {
420
- if (updates.length === 0) return undefined;
421
- return [
422
- "[OpenGeni internal updates]",
423
- "These platform updates were delivered together for this inference. They are not human prompts.",
424
- JSON.stringify({
425
- updates: updates.map((update) => ({
426
- id: update.id,
427
- kind: update.kind,
428
- classification: update.classification,
429
- sourceId: update.sourceId,
430
- summary: update.summary,
431
- payload: update.payload,
432
- lineage: update.lineage,
433
- })),
434
- }),
435
- ].join("\n");
436
- }
437
-
438
- /** Build one inference from canonical history plus optional ephemeral system context. */
427
+ /** Build one inference from canonical history plus attempt-local operational context. */
439
428
  async function messageInput(
440
429
  db: Database,
441
430
  runtime: OpenGeniRuntime,
@@ -16,12 +16,12 @@ import {
16
16
  createSessionGoal,
17
17
  enqueueSessionWorkflowWakeIfRunnable,
18
18
  getBillingBalance,
19
+ getScheduledTask,
19
20
  getRig,
20
21
  getVariableSet,
21
22
  isCodexBilledTurn,
22
23
  markScheduledTaskRunFailedIfQueued,
23
24
  recordUsageEvent,
24
- requireScheduledTask,
25
25
  requireSession,
26
26
  setTemporalWorkflowId,
27
27
  settleScheduledTaskRunInTransaction,
@@ -50,7 +50,19 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
50
50
  input: DispatchScheduledTaskRunInput,
51
51
  ): Promise<DispatchScheduledTaskRunResult> => {
52
52
  const { settings, db, bus, wakeSessionWorkflow } = await services();
53
- const task = await requireScheduledTask(db, input.workspaceId, input.taskId);
53
+ // Histories created before the manual-initiator workflow patch already
54
+ // contain this activity command. Reject their incomplete wire input here
55
+ // so replay consumes the recorded command and the retry loop settles.
56
+ if (input.triggerType === "manual" && !input.initiator) {
57
+ return { action: "blocked", reason: "malformed_manual_trigger" };
58
+ }
59
+ const task = await getScheduledTask(db, input.workspaceId, input.taskId);
60
+ // Deleting a schedule is authoritative. A fire workflow that was already
61
+ // created may still start afterward; completing it as a no-op prevents an
62
+ // unbounded retry loop for work that no longer exists.
63
+ if (!task) {
64
+ return { action: "deleted" };
65
+ }
54
66
  // A scheduled bot selection was authorized when the task was written,
55
67
  // but connection status and tenant/role binding are mutable. Revalidate
56
68
  // before any session/model cost and never fall back to a personal Slack
@@ -79,7 +91,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
79
91
  workspaceId: task.workspaceId,
80
92
  model,
81
93
  });
82
- await ensureScheduledRunAllowed(
94
+ const admissionDenial = await scheduledRunAdmissionDenial(
83
95
  settings,
84
96
  db,
85
97
  task.accountId,
@@ -87,6 +99,9 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
87
99
  input.agentRunUsageIdempotencyKey ? 0 : 1,
88
100
  isCodexRun,
89
101
  );
102
+ if (admissionDenial) {
103
+ return { action: "blocked", reason: admissionDenial };
104
+ }
90
105
  const run = await createScheduledTaskRun(db, {
91
106
  workspaceId: task.workspaceId,
92
107
  taskId: task.id,
@@ -105,7 +120,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
105
120
  unit: "run",
106
121
  sourceResourceType: "scheduled_task_run",
107
122
  sourceResourceId: run.id,
108
- initiator: { kind: "service", subjectId: "scheduler" },
123
+ initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
109
124
  initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
110
125
  origin: "scheduled_task",
111
126
  idempotencyKey: `usage:scheduled_task.fired:${run.id}`,
@@ -120,7 +135,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
120
135
  sourceResourceType: "scheduled_task_run",
121
136
  sourceResourceId: run.id,
122
137
  sessionId: run.sessionId,
123
- initiator: { kind: "service", subjectId: "scheduler" },
138
+ initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
124
139
  initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
125
140
  origin: "scheduled_task",
126
141
  idempotencyKey:
@@ -159,8 +174,8 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
159
174
  const reasoningEffort = task.agentConfig.reasoningEffort ?? settings.openaiReasoningEffort;
160
175
  const sandboxBackend = task.agentConfig.sandboxBackend ?? settings.sandboxBackend;
161
176
  const goalSpec = task.agentConfig.goal ?? null;
162
- // Every dispatch carries the first-party MCP server (set_session_title,
163
- // goal tools, and the permission-gated tools), matching the API path.
177
+ // Every dispatch carries the first-party MCP server; runtime visibility
178
+ // still follows the session's exact selection and authorization.
164
179
  const taskTools = withFirstPartyTools(settings, task.agentConfig.tools);
165
180
  if (task.runMode === "new_session_per_run" || !task.reusableSessionId) {
166
181
  // The FK on scheduled_tasks.variable_set_id is ON DELETE RESTRICT, so
@@ -475,7 +490,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
475
490
  sourceResourceType: "scheduled_task_run",
476
491
  sourceResourceId: run.id,
477
492
  sessionId: result.sessionId,
478
- initiator: { kind: "service", subjectId: "scheduler" },
493
+ initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
479
494
  initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
480
495
  origin: "scheduled_task",
481
496
  idempotencyKey:
@@ -495,14 +510,14 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
495
510
  };
496
511
  }
497
512
 
498
- async function ensureScheduledRunAllowed(
513
+ async function scheduledRunAdmissionDenial(
499
514
  settings: Settings,
500
515
  db: ActivityServices["db"],
501
516
  accountId: string,
502
517
  workspaceId: string,
503
518
  requestedAgentRuns: number,
504
519
  isCodexRun: boolean,
505
- ): Promise<void> {
520
+ ): Promise<"insufficient_credits" | "monthly_model_cost_limit" | "monthly_agent_run_limit" | null> {
506
521
  // Codex-billed runs are paid by the user's ChatGPT/Codex plan: skip the
507
522
  // credit-balance gate and the monthly model-cost cap. The agent-run COUNT cap
508
523
  // below is a volume quota (not a credit/cost gate) and is intentionally kept.
@@ -512,7 +527,7 @@ async function ensureScheduledRunAllowed(
512
527
  ) {
513
528
  const balance = await getBillingBalance(db, accountId);
514
529
  if (balance.balanceMicros <= 0) {
515
- throw new Error("insufficient OpenGeni credits");
530
+ return "insufficient_credits";
516
531
  }
517
532
  }
518
533
  if (settings.usageLimitsMode === "static" || settings.usageLimitsMode === "managed") {
@@ -524,9 +539,7 @@ async function ensureScheduledRunAllowed(
524
539
  since: startOfUtcMonth(),
525
540
  });
526
541
  if (used >= limits.maxMonthlyCostMicrosPerAccount) {
527
- throw new Error(
528
- `monthly model cost limit reached (${limits.maxMonthlyCostMicrosPerAccount} micros)`,
529
- );
542
+ return "monthly_model_cost_limit";
530
543
  }
531
544
  }
532
545
  if (limits.maxMonthlyAgentRunsPerWorkspace) {
@@ -536,12 +549,11 @@ async function ensureScheduledRunAllowed(
536
549
  since: startOfUtcMonth(),
537
550
  });
538
551
  if (used + requestedAgentRuns > limits.maxMonthlyAgentRunsPerWorkspace) {
539
- throw new Error(
540
- `monthly agent run limit reached (${limits.maxMonthlyAgentRunsPerWorkspace})`,
541
- );
552
+ return "monthly_agent_run_limit";
542
553
  }
543
554
  }
544
555
  }
556
+ return null;
545
557
  }
546
558
 
547
559
  function startOfUtcMonth(): Date {
@@ -2,8 +2,10 @@ import {
2
2
  settleSessionAttemptInterruptions,
3
3
  applySessionTurnSettlement,
4
4
  recoverSessionDispatch,
5
+ reconcileSessionAttemptQuiescence,
5
6
  peekSessionWork as peekSessionWorkDb,
6
7
  countQueuedTurns,
8
+ getSessionAttemptActivityRef,
7
9
  getSessionEvent,
8
10
  getSessionTurnForAttempt,
9
11
  expireSessionHumanInputRequest,
@@ -23,6 +25,8 @@ import type {
23
25
  SettleSessionInterruptionsInput,
24
26
  MarkSessionIdleInput,
25
27
  PersistSessionAttemptQuiescenceInput,
28
+ ReconcileSessionAttemptQuiescenceInput,
29
+ ReconcileSessionAttemptQuiescenceResult,
26
30
  RecoverDispatchInput,
27
31
  RecoverDispatchResult,
28
32
  } from "./types";
@@ -31,8 +35,10 @@ export type SessionStateActivityOverrides = Partial<{
31
35
  settleSessionAttemptInterruptions: typeof settleSessionAttemptInterruptions;
32
36
  applySessionTurnSettlement: typeof applySessionTurnSettlement;
33
37
  recoverSessionDispatch: typeof recoverSessionDispatch;
38
+ reconcileSessionAttemptQuiescence: typeof reconcileSessionAttemptQuiescence;
34
39
  peekSessionWork: typeof peekSessionWorkDb;
35
40
  countQueuedTurns: typeof countQueuedTurns;
41
+ getSessionAttemptActivityRef: typeof getSessionAttemptActivityRef;
36
42
  getSessionEvent: typeof getSessionEvent;
37
43
  getSessionTurnForAttempt: typeof getSessionTurnForAttempt;
38
44
  expireSessionHumanInputRequest: typeof expireSessionHumanInputRequest;
@@ -60,8 +66,12 @@ export function createSessionStateActivities(
60
66
  const applySessionTurnSettlementFn =
61
67
  overrides.applySessionTurnSettlement ?? applySessionTurnSettlement;
62
68
  const recoverSessionDispatchFn = overrides.recoverSessionDispatch ?? recoverSessionDispatch;
69
+ const reconcileSessionAttemptQuiescenceFn =
70
+ overrides.reconcileSessionAttemptQuiescence ?? reconcileSessionAttemptQuiescence;
63
71
  const peekSessionWorkFn = overrides.peekSessionWork ?? peekSessionWorkDb;
64
72
  const countQueuedTurnsFn = overrides.countQueuedTurns ?? countQueuedTurns;
73
+ const getSessionAttemptActivityRefFn =
74
+ overrides.getSessionAttemptActivityRef ?? getSessionAttemptActivityRef;
65
75
  const getSessionEventFn = overrides.getSessionEvent ?? getSessionEvent;
66
76
  const getSessionTurnForAttemptFn = overrides.getSessionTurnForAttempt ?? getSessionTurnForAttempt;
67
77
  const expireSessionHumanInputRequestFn =
@@ -186,6 +196,41 @@ export function createSessionStateActivities(
186
196
  }
187
197
  }
188
198
 
199
+ async function reconcileSessionAttemptQuiescenceActivity(
200
+ input: ReconcileSessionAttemptQuiescenceInput,
201
+ ): Promise<ReconcileSessionAttemptQuiescenceResult> {
202
+ const { db, bus, inspectSessionAttemptActivity } = await services();
203
+ const activityRef = await getSessionAttemptActivityRefFn(db, {
204
+ ...input,
205
+ temporalWorkflowId: input.workflowId,
206
+ });
207
+ if (!activityRef) return { action: "stale" };
208
+ if (!activityRef.quiesced) {
209
+ if (!inspectSessionAttemptActivity) return { action: "pending" };
210
+ const state = await inspectSessionAttemptActivity(activityRef);
211
+ if (state === "pending") return { action: "pending" };
212
+ }
213
+ const result = await reconcileSessionAttemptQuiescenceFn(db, {
214
+ accountId: input.accountId,
215
+ workspaceId: input.workspaceId,
216
+ sessionId: input.sessionId,
217
+ attemptId: input.attemptId,
218
+ temporalWorkflowId: input.workflowId,
219
+ temporalWorkflowRunId: activityRef.workflowRunId,
220
+ temporalActivityId: activityRef.activityId,
221
+ activitySettled: true,
222
+ });
223
+ if (result.events.length > 0) {
224
+ await publishDurableSessionEventsFn(
225
+ bus,
226
+ input.workspaceId,
227
+ input.sessionId,
228
+ result.events,
229
+ ).catch(() => undefined);
230
+ }
231
+ return { action: result.action };
232
+ }
233
+
189
234
  /**
190
235
  * Recover the same current inference when its worker dies without completing
191
236
  * a graceful checkpoint (heartbeat timeout, SIGKILL, OOM, or node loss).
@@ -278,6 +323,7 @@ export function createSessionStateActivities(
278
323
  failSessionAttempt,
279
324
  settleSessionInterruptions,
280
325
  persistSessionAttemptQuiescence,
326
+ reconcileSessionAttemptQuiescence: reconcileSessionAttemptQuiescenceActivity,
281
327
  recoverDispatch,
282
328
  peekSessionWork,
283
329
  expireSessionHumanInput,