@opengeni/worker-bundle 0.12.5 → 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/dist/index.d.ts +55 -6
- package/dist/index.js +114 -88
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +64 -6
- package/package.json +10 -10
- package/src/activities/agent-turn.ts +27 -25
- package/src/activities/capabilities.ts +3 -62
- package/src/activities/goals.ts +0 -14
- package/src/activities/run-input.ts +13 -24
- package/src/activities/scheduled-tasks.ts +29 -17
- package/src/activities/session-state.ts +46 -0
- package/src/activities/types.ts +54 -12
- package/src/activities.ts +5 -0
- package/src/index.ts +51 -1
- package/src/workflows/activities.ts +17 -1
- package/src/workflows/scheduled-tasks.ts +40 -11
- package/src/workflows/session.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeni/worker-bundle",
|
|
3
|
-
"version": "0.12.
|
|
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.
|
|
45
|
-
"@opengeni/contracts": "^0.
|
|
46
|
-
"@opengeni/core": "^0.12.
|
|
47
|
-
"@opengeni/db": "^0.
|
|
48
|
-
"@opengeni/documents": "^0.2.
|
|
49
|
-
"@opengeni/events": "^0.3.
|
|
50
|
-
"@opengeni/github": "^0.3.
|
|
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.14.
|
|
53
|
-
"@opengeni/storage": "^0.2.
|
|
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 {
|
|
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,
|
|
@@ -1300,16 +1300,26 @@ export function historyRowsToAppend(
|
|
|
1300
1300
|
nextWatermark: number;
|
|
1301
1301
|
nextPosition: number;
|
|
1302
1302
|
} {
|
|
1303
|
-
const sanitized = sanitizeHistoryItemsForModel(rawHistory, toolOutputTruncationTokens)
|
|
1304
|
-
|
|
1305
|
-
|
|
1303
|
+
const sanitized = sanitizeHistoryItemsForModel(rawHistory, toolOutputTruncationTokens).map(
|
|
1304
|
+
(item) => redactValue(item) as Record<string, unknown>,
|
|
1305
|
+
);
|
|
1306
1306
|
if (sanitized.length <= persistedHistoryCount) {
|
|
1307
1307
|
return { rows: [], nextWatermark: persistedHistoryCount, nextPosition };
|
|
1308
1308
|
}
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
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
|
+
}));
|
|
1313
1323
|
return {
|
|
1314
1324
|
rows,
|
|
1315
1325
|
nextWatermark: sanitized.length,
|
|
@@ -4070,19 +4080,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4070
4080
|
const turnResources = mergeResourceRefs(session.resources, turn.resources);
|
|
4071
4081
|
// Attach the first-party MCP server to EVERY turn, regardless of how/when
|
|
4072
4082
|
// the session was created (API, scheduled task, or a pre-existing session
|
|
4073
|
-
// whose stored tools predate this)
|
|
4074
|
-
//
|
|
4075
|
-
//
|
|
4076
|
-
// overlay injected it into runSettings.mcpServers (active subscription +
|
|
4077
|
-
// connector scopes); no-op for every other turn. Its refreshing bearer is
|
|
4078
|
-
// resolved at connect time from the codex ALS (see the withCodex-wrapped
|
|
4079
|
-
// 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.
|
|
4080
4086
|
// Resolve the durable policy at the turn boundary. Workspace-default
|
|
4081
|
-
// sessions may discover newly enabled capability MCPs
|
|
4082
|
-
// inherited-fixed, and legacy sessions remain narrowed
|
|
4083
|
-
// materialized allow-list.
|
|
4084
|
-
// single Codex lazy router/connector overlay; it is not a second policy
|
|
4085
|
-
// 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.
|
|
4086
4090
|
const resolvedToolPolicy = resolveSessionToolPolicy({
|
|
4087
4091
|
...(session.toolPolicy ? { toolPolicy: session.toolPolicy } : {}),
|
|
4088
4092
|
sessionTools: session.tools,
|
|
@@ -4097,10 +4101,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4097
4101
|
defaultNativeToolsAllowed = sessionToolPolicyAllowsDefaultNativeTools(
|
|
4098
4102
|
resolvedToolPolicy.effectivePolicy,
|
|
4099
4103
|
);
|
|
4100
|
-
const turnTools =
|
|
4101
|
-
runSettings,
|
|
4102
|
-
withFirstPartyTools(runSettings, effectivePolicyTools),
|
|
4103
|
-
);
|
|
4104
|
+
const turnTools = withFirstPartyTools(runSettings, effectivePolicyTools);
|
|
4104
4105
|
// §7.6 connection-credential provider — load (and decrypt) the variable set via the host
|
|
4105
4106
|
// `sandboxSecrets` provider when bound; unset → today's local decrypt.
|
|
4106
4107
|
const connectionScope = {
|
|
@@ -5003,6 +5004,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5003
5004
|
...(session.firstPartyMcpPermissions?.length
|
|
5004
5005
|
? { firstPartyPermissions: session.firstPartyMcpPermissions }
|
|
5005
5006
|
: {}),
|
|
5007
|
+
firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
|
|
5006
5008
|
}),
|
|
5007
5009
|
),
|
|
5008
5010
|
cancellationSignal,
|
|
@@ -3,15 +3,8 @@ import {
|
|
|
3
3
|
type Settings,
|
|
4
4
|
withCodexCatalogProvider,
|
|
5
5
|
} from "@opengeni/config";
|
|
6
|
-
import {
|
|
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
|
|
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
|
-
|
|
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. */
|
package/src/activities/goals.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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 (
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
163
|
-
//
|
|
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
|
|
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<
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
package/src/activities/types.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
ConnectionCredentialsPort,
|
|
4
4
|
EntitlementsPort,
|
|
5
5
|
ScheduledTaskTriggerType,
|
|
6
|
+
TurnInitiator,
|
|
6
7
|
} from "@opengeni/contracts";
|
|
7
8
|
import type { Database } from "@opengeni/db";
|
|
8
9
|
import type { DocumentServices } from "@opengeni/documents";
|
|
@@ -51,6 +52,12 @@ export type SessionAttemptQuiescenceProof = {
|
|
|
51
52
|
|
|
52
53
|
export type SignalSessionAttemptQuiesced = (input: SessionAttemptQuiescenceProof) => Promise<void>;
|
|
53
54
|
|
|
55
|
+
export type InspectSessionAttemptActivity = (input: {
|
|
56
|
+
workflowId: string;
|
|
57
|
+
workflowRunId: string;
|
|
58
|
+
activityId: string;
|
|
59
|
+
}) => Promise<"pending" | "settled">;
|
|
60
|
+
|
|
54
61
|
export type ActivityServices = {
|
|
55
62
|
settings: Settings;
|
|
56
63
|
db: Database;
|
|
@@ -63,6 +70,9 @@ export type ActivityServices = {
|
|
|
63
70
|
/** Durable signalWithStart fallback used only after the activity's direct
|
|
64
71
|
* physical-quiescence receipt write exhausts its bounded DB retries. */
|
|
65
72
|
signalSessionAttemptQuiesced: SignalSessionAttemptQuiesced | null;
|
|
73
|
+
/** Server-authoritative Temporal activity lease inspection used only to
|
|
74
|
+
* recover a missing quiescence receipt after the original activity vanished. */
|
|
75
|
+
inspectSessionAttemptActivity: InspectSessionAttemptActivity | null;
|
|
66
76
|
/** Revision-carrying capacity nudge; generic outbox repair is also sufficient. */
|
|
67
77
|
signalCodexCapacityWorkflow?: SignalCodexCapacityWorkflow | null;
|
|
68
78
|
// §7.5 P3 — host-entitlements port, the WORKER half of the same seam the API
|
|
@@ -150,6 +160,18 @@ export type SettleSessionInterruptionsInput = {
|
|
|
150
160
|
|
|
151
161
|
export type PersistSessionAttemptQuiescenceInput = SessionAttemptQuiescenceProof;
|
|
152
162
|
|
|
163
|
+
export type ReconcileSessionAttemptQuiescenceInput = {
|
|
164
|
+
accountId: string;
|
|
165
|
+
workspaceId: string;
|
|
166
|
+
sessionId: string;
|
|
167
|
+
attemptId: string;
|
|
168
|
+
workflowId: string;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type ReconcileSessionAttemptQuiescenceResult = {
|
|
172
|
+
action: "quiesced" | "pending" | "stale";
|
|
173
|
+
};
|
|
174
|
+
|
|
153
175
|
export type FailSessionAttemptInput = {
|
|
154
176
|
accountId: string;
|
|
155
177
|
workspaceId: string;
|
|
@@ -215,21 +237,41 @@ export type MaybeContinueGoalResult = {
|
|
|
215
237
|
export type DispatchScheduledTaskRunInput = {
|
|
216
238
|
workspaceId: string;
|
|
217
239
|
taskId: string;
|
|
218
|
-
triggerType: ScheduledTaskTriggerType;
|
|
219
240
|
/** Stable Temporal workflow identity; retries must reuse the same source row. */
|
|
220
241
|
producerKey?: string;
|
|
221
|
-
|
|
222
|
-
|
|
242
|
+
} & (
|
|
243
|
+
| {
|
|
244
|
+
triggerType: Extract<ScheduledTaskTriggerType, "scheduled">;
|
|
245
|
+
agentRunUsageIdempotencyKey?: never;
|
|
246
|
+
initiator?: never;
|
|
247
|
+
}
|
|
248
|
+
| {
|
|
249
|
+
triggerType: Extract<ScheduledTaskTriggerType, "manual">;
|
|
250
|
+
agentRunUsageIdempotencyKey: string;
|
|
251
|
+
/** Exact identity used by the API-side charge for this same trigger. */
|
|
252
|
+
initiator: TurnInitiator;
|
|
253
|
+
}
|
|
254
|
+
);
|
|
223
255
|
|
|
224
|
-
export type DispatchScheduledTaskRunResult =
|
|
225
|
-
action: "
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
256
|
+
export type DispatchScheduledTaskRunResult =
|
|
257
|
+
| { action: "deleted" }
|
|
258
|
+
| {
|
|
259
|
+
action: "blocked";
|
|
260
|
+
reason:
|
|
261
|
+
| "insufficient_credits"
|
|
262
|
+
| "monthly_model_cost_limit"
|
|
263
|
+
| "monthly_agent_run_limit"
|
|
264
|
+
| "malformed_manual_trigger";
|
|
265
|
+
}
|
|
266
|
+
| {
|
|
267
|
+
action: "start" | "signal";
|
|
268
|
+
accountId: string;
|
|
269
|
+
workspaceId: string;
|
|
270
|
+
sessionId: string;
|
|
271
|
+
triggerEventId: string;
|
|
272
|
+
workflowId: string;
|
|
273
|
+
workflowWakeRevision: number | null;
|
|
274
|
+
};
|
|
233
275
|
|
|
234
276
|
export type IndexDocumentInput = {
|
|
235
277
|
accountId: string;
|