@opengeni/worker-bundle 0.17.2 → 0.20.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.
- package/dist/activities/agent-run-admission.d.ts +12 -0
- package/dist/activities/agent-turn.d.ts +25 -0
- package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
- package/dist/activities/capabilities.d.ts +6 -0
- package/dist/activities/codex-capacity.d.ts +1 -0
- package/dist/activities/environment.d.ts +7 -1
- package/dist/activities/packs.d.ts +8 -10
- package/dist/activities/session-state.d.ts +4 -2
- package/dist/activities/site-auth-maintenance.d.ts +27 -0
- package/dist/activities/types.d.ts +16 -0
- package/dist/activities/video-generation-admission.d.ts +9 -4
- package/dist/activities/video-generation-credential.d.ts +17 -0
- package/dist/activities/video-reference-staging.d.ts +7 -1
- package/dist/activities/xai-auth.d.ts +23 -0
- package/dist/activities/xai-image-generation.d.ts +26 -0
- package/dist/activities/xai-video-generation.d.ts +18 -0
- package/dist/{activities-control-44KBWLSM.js → activities-control-XURE4VKN.js} +387 -51
- package/dist/activities-control-XURE4VKN.js.map +1 -0
- package/dist/activities-control.d.ts +8 -0
- package/dist/{activities-turn-K4CEDEGJ.js → activities-turn-6EBXGGLS.js} +5312 -3951
- package/dist/activities-turn-6EBXGGLS.js.map +1 -0
- package/dist/activities.d.ts +9 -1
- package/dist/{chunk-KKCRGAGZ.js → chunk-5GRJIP5H.js} +99 -98
- package/dist/chunk-5GRJIP5H.js.map +1 -0
- package/dist/{chunk-E3RI3I33.js → chunk-TYNPCG4U.js} +88 -10
- package/dist/chunk-TYNPCG4U.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +49 -2
- package/dist/index.js.map +1 -1
- package/dist/model-checkpoint-memory-collector.d.ts +1 -0
- package/dist/model-context-contributions.d.ts +40 -0
- package/dist/observability-metrics.d.ts +20 -1
- package/dist/sandbox-routing.d.ts +4 -0
- package/dist/workflow-bundle.js +59 -15
- package/dist/workflows/activities.d.ts +1 -1
- package/dist/workflows/site-auth-maintenance.d.ts +2 -0
- package/dist/workflows.d.ts +1 -0
- package/package.json +16 -15
- package/src/activities/agent-run-admission.ts +76 -0
- package/src/activities/agent-turn.ts +1178 -142
- package/src/activities/browser-state-artifact-reaper.ts +120 -0
- package/src/activities/capabilities.ts +10 -0
- package/src/activities/codex-capacity.ts +48 -1
- package/src/activities/environment.ts +14 -8
- package/src/activities/goals.ts +2 -1
- package/src/activities/packs.ts +45 -122
- package/src/activities/parent-wake.ts +13 -1
- package/src/activities/scheduled-tasks.ts +30 -73
- package/src/activities/session-state.ts +18 -0
- package/src/activities/site-auth-maintenance.ts +269 -0
- package/src/activities/types.ts +17 -0
- package/src/activities/video-generation-admission.ts +37 -4
- package/src/activities/video-generation-credential.ts +84 -6
- package/src/activities/video-generation-reconciliation.ts +212 -19
- package/src/activities/video-media-validation.ts +8 -2
- package/src/activities/video-reference-staging.ts +121 -34
- package/src/activities/xai-auth.ts +122 -0
- package/src/activities/xai-image-generation.ts +86 -0
- package/src/activities/xai-video-generation.ts +69 -0
- package/src/activities-control.ts +4 -0
- package/src/activities.ts +5 -0
- package/src/index.ts +51 -0
- package/src/model-checkpoint-memory-collector.ts +8 -1
- package/src/model-context-contributions.ts +159 -0
- package/src/observability-metrics.ts +98 -9
- package/src/sandbox-routing.ts +13 -0
- package/src/workflows/activities.ts +1 -0
- package/src/workflows/file-upload-reaper.ts +8 -2
- package/src/workflows/session.ts +20 -6
- package/src/workflows/site-auth-maintenance.ts +14 -0
- package/src/workflows.ts +1 -0
- package/dist/activities-control-44KBWLSM.js.map +0 -1
- package/dist/activities-turn-K4CEDEGJ.js.map +0 -1
- package/dist/chunk-E3RI3I33.js.map +0 -1
- package/dist/chunk-KKCRGAGZ.js.map +0 -1
|
@@ -15,13 +15,12 @@ import {
|
|
|
15
15
|
createSession,
|
|
16
16
|
createSessionGoal,
|
|
17
17
|
enqueueSessionWorkflowWakeIfRunnable,
|
|
18
|
-
getBillingBalance,
|
|
19
18
|
getScheduledTask,
|
|
20
19
|
ensureKnowledgeSourceSyncState,
|
|
21
20
|
getScheduledTaskPersonalConnectionDelegations,
|
|
21
|
+
getScheduledTaskXaiProviderAccountAuthoritySnapshot,
|
|
22
22
|
getRig,
|
|
23
23
|
getVariableSet,
|
|
24
|
-
isCodexBilledTurn,
|
|
25
24
|
markScheduledTaskRunFailedIfQueued,
|
|
26
25
|
recordKnowledgeSourceSyncWake,
|
|
27
26
|
recordUsageEvent,
|
|
@@ -30,19 +29,19 @@ import {
|
|
|
30
29
|
setTemporalWorkflowId,
|
|
31
30
|
settleScheduledTaskRunInTransaction,
|
|
32
31
|
SessionSpawnDeniedDbError,
|
|
33
|
-
sumUsageQuantity,
|
|
34
32
|
updateScheduledTask,
|
|
35
33
|
updateScheduledTaskRun,
|
|
36
34
|
upsertSessionGoal,
|
|
37
35
|
} from "@opengeni/db";
|
|
38
36
|
import { appendAndPublishEvents, publishDurableSessionEvents } from "@opengeni/events";
|
|
39
|
-
import {
|
|
37
|
+
import { resolveFirstPartyMcpToolPolicy } from "@opengeni/config";
|
|
40
38
|
import {
|
|
41
39
|
assertReusableSessionRevivable,
|
|
42
40
|
scheduledUserMessagePayload,
|
|
43
41
|
workflowIdForSession,
|
|
44
42
|
} from "./common";
|
|
45
43
|
import { withFirstPartyTools } from "./goals";
|
|
44
|
+
import { agentRunAdmissionDenial } from "./agent-run-admission";
|
|
46
45
|
import type {
|
|
47
46
|
ControlActivityServices,
|
|
48
47
|
DispatchScheduledTaskRunInput,
|
|
@@ -54,7 +53,8 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
54
53
|
dispatchScheduledTaskRun: async (
|
|
55
54
|
input: DispatchScheduledTaskRunInput,
|
|
56
55
|
): Promise<DispatchScheduledTaskRunResult> => {
|
|
57
|
-
const
|
|
56
|
+
const service = await services();
|
|
57
|
+
const { settings, db, bus, wakeSessionWorkflow } = service;
|
|
58
58
|
// Histories created before the manual-initiator workflow patch already
|
|
59
59
|
// contain this activity command. Reject their incomplete wire input here
|
|
60
60
|
// so replay consumes the recorded command and the retry loop settles.
|
|
@@ -131,6 +131,8 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
131
131
|
task.workspaceId,
|
|
132
132
|
task.id,
|
|
133
133
|
);
|
|
134
|
+
const taskXaiProviderAccountAuthoritySnapshot =
|
|
135
|
+
await getScheduledTaskXaiProviderAccountAuthoritySnapshot(db, task.workspaceId, task.id);
|
|
134
136
|
// A scheduled bot selection was authorized when the task was written,
|
|
135
137
|
// but connection status and tenant/role binding are mutable. Revalidate
|
|
136
138
|
// before any session/model cost and never fall back to a personal Slack
|
|
@@ -148,25 +150,13 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
148
150
|
if (slackBotConnection && !slackBotMetadata) {
|
|
149
151
|
throw new Error("OpenGeni Slack bot connection metadata is invalid");
|
|
150
152
|
}
|
|
151
|
-
// The scheduled task's model can be codex/<slug>; resolve it here so the
|
|
152
|
-
// admission gate can skip the credit/cost gates for a codex-billed run
|
|
153
|
-
// (paid by the user's ChatGPT/Codex plan). This file uses BASE settings (no
|
|
154
|
-
// codex overlay), so the predicate does its own active-credential read.
|
|
155
153
|
const model = task.agentConfig.model ?? settings.openaiModel;
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
settings,
|
|
154
|
+
const admissionDenial = await agentRunAdmissionDenial(service, {
|
|
155
|
+
accountId: task.accountId,
|
|
159
156
|
workspaceId: task.workspaceId,
|
|
160
157
|
model,
|
|
158
|
+
requestedAgentRuns: input.agentRunUsageIdempotencyKey ? 0 : 1,
|
|
161
159
|
});
|
|
162
|
-
const admissionDenial = await scheduledRunAdmissionDenial(
|
|
163
|
-
settings,
|
|
164
|
-
db,
|
|
165
|
-
task.accountId,
|
|
166
|
-
task.workspaceId,
|
|
167
|
-
input.agentRunUsageIdempotencyKey ? 0 : 1,
|
|
168
|
-
isCodexRun,
|
|
169
|
-
);
|
|
170
160
|
if (admissionDenial) {
|
|
171
161
|
return { action: "blocked", reason: admissionDenial };
|
|
172
162
|
}
|
|
@@ -287,6 +277,7 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
287
277
|
initialMessage: task.agentConfig.prompt,
|
|
288
278
|
resources: task.agentConfig.resources,
|
|
289
279
|
tools: taskTools,
|
|
280
|
+
firstPartyMcpTools: resolveFirstPartyMcpToolPolicy(settings).default,
|
|
290
281
|
metadata: {
|
|
291
282
|
...taskMetadata,
|
|
292
283
|
model,
|
|
@@ -314,6 +305,7 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
314
305
|
rigId: frozenRigId,
|
|
315
306
|
rigVersionId: frozenRigVersionId,
|
|
316
307
|
personalConnectionDelegations: taskPersonalConnectionDelegations,
|
|
308
|
+
initialXaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
|
|
317
309
|
maxNestedAgentDepthOverride: task.agentConfig.maxNestedAgentDepth ?? null,
|
|
318
310
|
// The durable agent config was privilege-checked when the task
|
|
319
311
|
// was created/updated. Preserve that explicit policy if a broader
|
|
@@ -404,8 +396,16 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
404
396
|
task.id,
|
|
405
397
|
run.id,
|
|
406
398
|
),
|
|
407
|
-
lineage: {
|
|
399
|
+
lineage: {
|
|
400
|
+
scheduledTaskId: task.id,
|
|
401
|
+
scheduledTaskRunId: run.id,
|
|
402
|
+
...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
|
|
403
|
+
task.createdBy.kind === "subject"
|
|
404
|
+
? { xaiAuthoritySubjectId: task.createdBy.subjectId }
|
|
405
|
+
: {}),
|
|
406
|
+
},
|
|
408
407
|
personalConnectionDelegations: taskPersonalConnectionDelegations,
|
|
408
|
+
xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
|
|
409
409
|
},
|
|
410
410
|
async (tx, wakeEventId) => {
|
|
411
411
|
if (!wakeEventId) throw new Error("Scheduled delivery has no wake event");
|
|
@@ -522,8 +522,16 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
|
|
|
522
522
|
task.id,
|
|
523
523
|
run.id,
|
|
524
524
|
),
|
|
525
|
-
lineage: {
|
|
525
|
+
lineage: {
|
|
526
|
+
scheduledTaskId: task.id,
|
|
527
|
+
scheduledTaskRunId: run.id,
|
|
528
|
+
...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
|
|
529
|
+
task.createdBy.kind === "subject"
|
|
530
|
+
? { xaiAuthoritySubjectId: task.createdBy.subjectId }
|
|
531
|
+
: {}),
|
|
532
|
+
},
|
|
526
533
|
personalConnectionDelegations: taskPersonalConnectionDelegations,
|
|
534
|
+
xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
|
|
527
535
|
},
|
|
528
536
|
async (tx, wakeEventId) => {
|
|
529
537
|
if (task.runMode === "existing_session") {
|
|
@@ -607,54 +615,3 @@ function knowledgeSourceSyncEffectivelyPaused(task: {
|
|
|
607
615
|
const control = value as Record<string, unknown>;
|
|
608
616
|
return control.sourceEnabled === false || control.connectionPaused === true;
|
|
609
617
|
}
|
|
610
|
-
|
|
611
|
-
async function scheduledRunAdmissionDenial(
|
|
612
|
-
settings: Settings,
|
|
613
|
-
db: ControlActivityServices["db"],
|
|
614
|
-
accountId: string,
|
|
615
|
-
workspaceId: string,
|
|
616
|
-
requestedAgentRuns: number,
|
|
617
|
-
isCodexRun: boolean,
|
|
618
|
-
): Promise<"insufficient_credits" | "monthly_model_cost_limit" | "monthly_agent_run_limit" | null> {
|
|
619
|
-
// Codex-billed runs are paid by the user's ChatGPT/Codex plan: skip the
|
|
620
|
-
// credit-balance gate and the monthly model-cost cap. The agent-run COUNT cap
|
|
621
|
-
// below is a volume quota (not a credit/cost gate) and is intentionally kept.
|
|
622
|
-
if (
|
|
623
|
-
!isCodexRun &&
|
|
624
|
-
(settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")
|
|
625
|
-
) {
|
|
626
|
-
const balance = await getBillingBalance(db, accountId);
|
|
627
|
-
if (balance.balanceMicros <= 0) {
|
|
628
|
-
return "insufficient_credits";
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
if (settings.usageLimitsMode === "static" || settings.usageLimitsMode === "managed") {
|
|
632
|
-
const limits = configuredStaticUsageLimits(settings);
|
|
633
|
-
if (!isCodexRun && limits.maxMonthlyCostMicrosPerAccount) {
|
|
634
|
-
const used = await sumUsageQuantity(db, {
|
|
635
|
-
accountId,
|
|
636
|
-
eventType: "model.cost",
|
|
637
|
-
since: startOfUtcMonth(),
|
|
638
|
-
});
|
|
639
|
-
if (used >= limits.maxMonthlyCostMicrosPerAccount) {
|
|
640
|
-
return "monthly_model_cost_limit";
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
if (limits.maxMonthlyAgentRunsPerWorkspace) {
|
|
644
|
-
const used = await sumUsageQuantity(db, {
|
|
645
|
-
workspaceId,
|
|
646
|
-
eventType: "agent_run.created",
|
|
647
|
-
since: startOfUtcMonth(),
|
|
648
|
-
});
|
|
649
|
-
if (used + requestedAgentRuns > limits.maxMonthlyAgentRunsPerWorkspace) {
|
|
650
|
-
return "monthly_agent_run_limit";
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
return null;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
function startOfUtcMonth(): Date {
|
|
658
|
-
const now = new Date();
|
|
659
|
-
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
|
660
|
-
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
getSessionAttemptActivityRef,
|
|
10
10
|
getSessionEvent,
|
|
11
11
|
getSessionTurnForAttempt,
|
|
12
|
+
expireSessionInteractionIntervention as expireSessionInteractionInterventionDb,
|
|
12
13
|
expireSessionHumanInputRequest,
|
|
13
14
|
markSessionAttemptQuiesced,
|
|
14
15
|
requireSession,
|
|
@@ -21,6 +22,8 @@ import type {
|
|
|
21
22
|
ControlActivityServices,
|
|
22
23
|
ExpireSessionHumanInputInput,
|
|
23
24
|
ExpireSessionHumanInputResult,
|
|
25
|
+
ExpireSessionInteractionInterventionInput,
|
|
26
|
+
ExpireSessionInteractionInterventionResult,
|
|
24
27
|
PeekSessionWorkInput,
|
|
25
28
|
FailSessionAttemptInput,
|
|
26
29
|
SettleSessionInterruptionsInput,
|
|
@@ -46,6 +49,7 @@ export type SessionStateActivityOverrides = Partial<{
|
|
|
46
49
|
getSessionEvent: typeof getSessionEvent;
|
|
47
50
|
getSessionTurnForAttempt: typeof getSessionTurnForAttempt;
|
|
48
51
|
expireSessionHumanInputRequest: typeof expireSessionHumanInputRequest;
|
|
52
|
+
expireSessionInteractionIntervention: typeof expireSessionInteractionInterventionDb;
|
|
49
53
|
requireSession: typeof requireSession;
|
|
50
54
|
settleSessionIdleWithParentOutbox: typeof settleSessionIdleWithParentOutbox;
|
|
51
55
|
markSessionAttemptQuiesced: typeof markSessionAttemptQuiesced;
|
|
@@ -82,6 +86,8 @@ export function createSessionStateActivities(
|
|
|
82
86
|
const getSessionTurnForAttemptFn = overrides.getSessionTurnForAttempt ?? getSessionTurnForAttempt;
|
|
83
87
|
const expireSessionHumanInputRequestFn =
|
|
84
88
|
overrides.expireSessionHumanInputRequest ?? expireSessionHumanInputRequest;
|
|
89
|
+
const expireSessionInteractionInterventionFn =
|
|
90
|
+
overrides.expireSessionInteractionIntervention ?? expireSessionInteractionInterventionDb;
|
|
85
91
|
const requireSessionFn = overrides.requireSession ?? requireSession;
|
|
86
92
|
const settleSessionIdleWithParentOutboxFn =
|
|
87
93
|
overrides.settleSessionIdleWithParentOutbox ?? settleSessionIdleWithParentOutbox;
|
|
@@ -347,6 +353,17 @@ export function createSessionStateActivities(
|
|
|
347
353
|
};
|
|
348
354
|
}
|
|
349
355
|
|
|
356
|
+
async function expireSessionInteractionIntervention(
|
|
357
|
+
input: ExpireSessionInteractionInterventionInput,
|
|
358
|
+
): Promise<ExpireSessionInteractionInterventionResult> {
|
|
359
|
+
const { db, bus } = await services();
|
|
360
|
+
const result = await expireSessionInteractionInterventionFn(db, input);
|
|
361
|
+
if (result.events.length > 0) {
|
|
362
|
+
await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, result.events);
|
|
363
|
+
}
|
|
364
|
+
return { action: result.action };
|
|
365
|
+
}
|
|
366
|
+
|
|
350
367
|
async function markSessionIdle(input: MarkSessionIdleInput): Promise<void> {
|
|
351
368
|
const { db, bus, settings, observability, wakeSessionWorkflow } = await services();
|
|
352
369
|
const settled = await settleSessionIdleWithParentOutboxFn(
|
|
@@ -383,6 +400,7 @@ export function createSessionStateActivities(
|
|
|
383
400
|
recoverEscapedMcpTimeout,
|
|
384
401
|
peekSessionWork,
|
|
385
402
|
expireSessionHumanInput,
|
|
403
|
+
expireSessionInteractionIntervention,
|
|
386
404
|
markSessionIdle,
|
|
387
405
|
};
|
|
388
406
|
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { resolveTurnExecutionPolicyV1 } from "@opengeni/config";
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_FIRST_PARTY_MCP_PERMISSIONS,
|
|
4
|
+
SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY,
|
|
5
|
+
SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY,
|
|
6
|
+
type FirstPartyMcpToolName,
|
|
7
|
+
} from "@opengeni/contracts";
|
|
8
|
+
import {
|
|
9
|
+
assertWorkspaceModelPolicyAllows,
|
|
10
|
+
canonicalConfiguredModel,
|
|
11
|
+
createAndStartSessionWithOutcome,
|
|
12
|
+
recordWorkspaceUsage,
|
|
13
|
+
} from "@opengeni/core";
|
|
14
|
+
import {
|
|
15
|
+
claimSiteAuthMaintenance,
|
|
16
|
+
confirmSiteAuthMaintenanceSessionInTransaction,
|
|
17
|
+
deferSiteAuthMaintenance,
|
|
18
|
+
type SiteAuthMaintenanceClaim,
|
|
19
|
+
} from "@opengeni/db";
|
|
20
|
+
import { agentRunAdmissionDenial } from "./agent-run-admission";
|
|
21
|
+
import type { ControlActivityServices } from "./types";
|
|
22
|
+
|
|
23
|
+
export const SITE_AUTH_MAINTENANCE_CLAIM_TIMEOUT_MS = 10 * 60 * 1_000;
|
|
24
|
+
export const SITE_AUTH_MAINTENANCE_BATCH_SIZE = 16;
|
|
25
|
+
|
|
26
|
+
const MAINTENANCE_TOOLS = [
|
|
27
|
+
"interaction_discover",
|
|
28
|
+
"browser_open",
|
|
29
|
+
"browser_tabs",
|
|
30
|
+
"browser_observe",
|
|
31
|
+
"browser_act",
|
|
32
|
+
"browser_debug",
|
|
33
|
+
"browser_auth",
|
|
34
|
+
"interaction_request_human",
|
|
35
|
+
"browser_identity",
|
|
36
|
+
"browser_lifecycle",
|
|
37
|
+
] as const satisfies readonly FirstPartyMcpToolName[];
|
|
38
|
+
|
|
39
|
+
export type MaintainSiteAuthConnectionsResult = {
|
|
40
|
+
claimed: number;
|
|
41
|
+
started: number;
|
|
42
|
+
deferred: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type SiteAuthMaintenanceOptions = {
|
|
46
|
+
claimTimeoutMs?: number;
|
|
47
|
+
batchSize?: number;
|
|
48
|
+
claim?: typeof claimSiteAuthMaintenance;
|
|
49
|
+
confirm?: typeof confirmSiteAuthMaintenanceSessionInTransaction;
|
|
50
|
+
defer?: typeof deferSiteAuthMaintenance;
|
|
51
|
+
createSession?: typeof createAndStartSessionWithOutcome;
|
|
52
|
+
admit?: typeof agentRunAdmissionDenial;
|
|
53
|
+
recordUsage?: typeof recordWorkspaceUsage;
|
|
54
|
+
assertModelPolicy?: typeof assertWorkspaceModelPolicyAllows;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Dispatch bounded, auditable agent sessions for due authentication checks.
|
|
58
|
+
* A hidden claim id/session id pair makes every crash point replayable. */
|
|
59
|
+
export function createSiteAuthMaintenanceActivities(
|
|
60
|
+
services: () => Promise<ControlActivityServices>,
|
|
61
|
+
options: SiteAuthMaintenanceOptions = {},
|
|
62
|
+
) {
|
|
63
|
+
const claimTimeoutMs = options.claimTimeoutMs ?? SITE_AUTH_MAINTENANCE_CLAIM_TIMEOUT_MS;
|
|
64
|
+
const batchSize = options.batchSize ?? SITE_AUTH_MAINTENANCE_BATCH_SIZE;
|
|
65
|
+
const claim = options.claim ?? claimSiteAuthMaintenance;
|
|
66
|
+
const confirm = options.confirm ?? confirmSiteAuthMaintenanceSessionInTransaction;
|
|
67
|
+
const defer = options.defer ?? deferSiteAuthMaintenance;
|
|
68
|
+
const createSession = options.createSession ?? createAndStartSessionWithOutcome;
|
|
69
|
+
const admit = options.admit ?? agentRunAdmissionDenial;
|
|
70
|
+
const recordUsage = options.recordUsage ?? recordWorkspaceUsage;
|
|
71
|
+
const assertModelPolicy = options.assertModelPolicy ?? assertWorkspaceModelPolicyAllows;
|
|
72
|
+
|
|
73
|
+
async function maintainSiteAuthConnections(): Promise<MaintainSiteAuthConnectionsResult> {
|
|
74
|
+
const service = await services();
|
|
75
|
+
const claims = await claim(service.db, { claimTimeoutMs, limit: batchSize });
|
|
76
|
+
const result: MaintainSiteAuthConnectionsResult = {
|
|
77
|
+
claimed: claims.length,
|
|
78
|
+
started: 0,
|
|
79
|
+
deferred: 0,
|
|
80
|
+
};
|
|
81
|
+
for (const maintenance of claims) {
|
|
82
|
+
try {
|
|
83
|
+
await dispatchMaintenanceSession(service, maintenance, {
|
|
84
|
+
confirm,
|
|
85
|
+
createSession,
|
|
86
|
+
admit,
|
|
87
|
+
recordUsage,
|
|
88
|
+
assertModelPolicy,
|
|
89
|
+
});
|
|
90
|
+
result.started += 1;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const released = await defer(service.db, {
|
|
93
|
+
accountId: maintenance.accountId,
|
|
94
|
+
workspaceId: maintenance.workspaceId,
|
|
95
|
+
siteAuthConnectionId: maintenance.siteAuthConnectionId,
|
|
96
|
+
operationId: maintenance.operationId,
|
|
97
|
+
sessionId: maintenance.sessionId,
|
|
98
|
+
retryAt: maintenanceRetryAt(maintenance),
|
|
99
|
+
}).catch(() => false);
|
|
100
|
+
if (released) result.deferred += 1;
|
|
101
|
+
service.observability.warn("site auth maintenance dispatch deferred", {
|
|
102
|
+
workspaceId: maintenance.workspaceId,
|
|
103
|
+
siteAuthConnectionId: maintenance.siteAuthConnectionId,
|
|
104
|
+
action: maintenance.action,
|
|
105
|
+
errorCategory: maintenanceErrorCategory(error),
|
|
106
|
+
claimReleased: released,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (result.claimed > 0) {
|
|
111
|
+
service.observability.info("site auth maintenance swept", result);
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { maintainSiteAuthConnections };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function dispatchMaintenanceSession(
|
|
120
|
+
service: ControlActivityServices,
|
|
121
|
+
maintenance: SiteAuthMaintenanceClaim,
|
|
122
|
+
dependencies: {
|
|
123
|
+
confirm: typeof confirmSiteAuthMaintenanceSessionInTransaction;
|
|
124
|
+
createSession: typeof createAndStartSessionWithOutcome;
|
|
125
|
+
admit: typeof agentRunAdmissionDenial;
|
|
126
|
+
recordUsage: typeof recordWorkspaceUsage;
|
|
127
|
+
assertModelPolicy: typeof assertWorkspaceModelPolicyAllows;
|
|
128
|
+
},
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
const model = canonicalConfiguredModel(service.settings, service.settings.openaiModel);
|
|
131
|
+
if (!model) throw new Error("site auth maintenance has no configured model");
|
|
132
|
+
await dependencies.assertModelPolicy(
|
|
133
|
+
service.db,
|
|
134
|
+
service.settings,
|
|
135
|
+
maintenance.workspaceId,
|
|
136
|
+
model,
|
|
137
|
+
);
|
|
138
|
+
const denial = await dependencies.admit(service, {
|
|
139
|
+
accountId: maintenance.accountId,
|
|
140
|
+
workspaceId: maintenance.workspaceId,
|
|
141
|
+
model,
|
|
142
|
+
requestedAgentRuns: 1,
|
|
143
|
+
});
|
|
144
|
+
if (denial) throw new Error(`site auth maintenance admission denied: ${denial}`);
|
|
145
|
+
const reasoningEffort = service.settings.openaiReasoningEffort;
|
|
146
|
+
const turnExecutionPolicy = resolveTurnExecutionPolicyV1(service.settings, {
|
|
147
|
+
modelId: model,
|
|
148
|
+
requestedModelId: null,
|
|
149
|
+
modelSource: "deployment",
|
|
150
|
+
reasoningEffort,
|
|
151
|
+
reasoningSource: "deployment",
|
|
152
|
+
latencyMode: "standard",
|
|
153
|
+
latencyModeSource: "deployment",
|
|
154
|
+
});
|
|
155
|
+
const created = await dependencies.createSession({
|
|
156
|
+
requestedSessionId: maintenance.sessionId,
|
|
157
|
+
db: service.db,
|
|
158
|
+
bus: service.bus,
|
|
159
|
+
workflowClient: {
|
|
160
|
+
wakeSessionWorkflow: async (input) => {
|
|
161
|
+
await service.wakeSessionWorkflow?.(input);
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
beforeCreateCommit: async (tx, sessionId) => {
|
|
165
|
+
const confirmed = await dependencies.confirm(tx, {
|
|
166
|
+
accountId: maintenance.accountId,
|
|
167
|
+
workspaceId: maintenance.workspaceId,
|
|
168
|
+
siteAuthConnectionId: maintenance.siteAuthConnectionId,
|
|
169
|
+
operationId: maintenance.operationId,
|
|
170
|
+
sessionId,
|
|
171
|
+
});
|
|
172
|
+
if (!confirmed) throw new Error("site auth maintenance claim changed before session start");
|
|
173
|
+
},
|
|
174
|
+
accountId: maintenance.accountId,
|
|
175
|
+
workspaceId: maintenance.workspaceId,
|
|
176
|
+
initialMessage: maintenancePrompt(maintenance),
|
|
177
|
+
resources: [],
|
|
178
|
+
tools: [{ kind: "mcp", id: "opengeni" }],
|
|
179
|
+
toolPolicy: { mode: "explicit", inheritedFromSessionId: null },
|
|
180
|
+
model,
|
|
181
|
+
reasoningEffort,
|
|
182
|
+
turnExecutionPolicy,
|
|
183
|
+
sandboxBackend: service.settings.sandboxBackend,
|
|
184
|
+
metadata: {
|
|
185
|
+
role: "site_auth_maintenance",
|
|
186
|
+
[SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY]: maintenance.siteAuthConnectionId,
|
|
187
|
+
[SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY]: maintenance.operationId,
|
|
188
|
+
},
|
|
189
|
+
createdBy: {
|
|
190
|
+
kind: "service",
|
|
191
|
+
subjectId: "site-auth-maintenance",
|
|
192
|
+
label: "OpenGeni authentication maintenance",
|
|
193
|
+
},
|
|
194
|
+
createdByContext: {
|
|
195
|
+
[SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY]: maintenance.siteAuthConnectionId,
|
|
196
|
+
[SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY]: maintenance.operationId,
|
|
197
|
+
},
|
|
198
|
+
instructions:
|
|
199
|
+
"Complete only the exact browser-auth maintenance run in the initial request. Use the canonical browser/auth tools, preserve their causal fences, expose no secrets, never publish a BrowserIdentity revision, and do not perform unrelated work.",
|
|
200
|
+
policyRole: "site_auth_maintenance",
|
|
201
|
+
firstPartyMcpPermissions: [...DEFAULT_FIRST_PARTY_MCP_PERMISSIONS],
|
|
202
|
+
firstPartyMcpTools: [...MAINTENANCE_TOOLS],
|
|
203
|
+
createIdempotencyKey: `site-auth-maintenance:${maintenance.operationId}`,
|
|
204
|
+
subjectId: "site-auth-maintenance",
|
|
205
|
+
});
|
|
206
|
+
try {
|
|
207
|
+
await dependencies.recordUsage(
|
|
208
|
+
{ db: service.db, settings: service.settings },
|
|
209
|
+
{
|
|
210
|
+
accountId: maintenance.accountId,
|
|
211
|
+
workspaceId: maintenance.workspaceId,
|
|
212
|
+
subjectId: "site-auth-maintenance",
|
|
213
|
+
eventType: "agent_run.created",
|
|
214
|
+
quantity: 1,
|
|
215
|
+
unit: "run",
|
|
216
|
+
sourceResourceType: "site_auth_connection",
|
|
217
|
+
sourceResourceId: maintenance.siteAuthConnectionId,
|
|
218
|
+
sessionId: created.session.id,
|
|
219
|
+
initiator: created.session.createdBy,
|
|
220
|
+
initiatorContext: created.session.createdByContext,
|
|
221
|
+
origin: "system",
|
|
222
|
+
idempotencyKey: `agent_run.created:site-auth-maintenance:${maintenance.operationId}`,
|
|
223
|
+
},
|
|
224
|
+
);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
service.observability.warn("site auth maintenance usage recording failed", {
|
|
227
|
+
workspaceId: maintenance.workspaceId,
|
|
228
|
+
siteAuthConnectionId: maintenance.siteAuthConnectionId,
|
|
229
|
+
errorCategory: maintenanceErrorCategory(error),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function maintenancePrompt(maintenance: SiteAuthMaintenanceClaim): string {
|
|
235
|
+
const action = maintenance.action === "repair" ? "repair authentication" : "check authentication";
|
|
236
|
+
return [
|
|
237
|
+
`Perform one scheduled SiteAuthConnection maintenance run: ${action}.`,
|
|
238
|
+
`Connection id: ${maintenance.siteAuthConnectionId}`,
|
|
239
|
+
`Connection: ${maintenance.name} (${maintenance.accountLabel})`,
|
|
240
|
+
`Purpose: ${maintenance.action}`,
|
|
241
|
+
maintenance.loginUrl ? `Login URL: ${maintenance.loginUrl}` : null,
|
|
242
|
+
maintenance.verificationUrlPrefixes.length > 0
|
|
243
|
+
? `Verification URL prefixes: ${maintenance.verificationUrlPrefixes.join(", ")}`
|
|
244
|
+
: "No dedicated verification URL is configured; use the connection's allowed origins.",
|
|
245
|
+
maintenance.preferredIdentityId
|
|
246
|
+
? `Preferred BrowserIdentity: ${maintenance.preferredIdentityId}`
|
|
247
|
+
: "No preferred BrowserIdentity is configured.",
|
|
248
|
+
maintenance.preferredPlacement
|
|
249
|
+
? `Preferred placement: ${JSON.stringify(maintenance.preferredPlacement)}`
|
|
250
|
+
: "Use the normal managed-browser placement.",
|
|
251
|
+
maintenance.preferredNetworkRouteId
|
|
252
|
+
? `Preferred NetworkRoute: ${maintenance.preferredNetworkRouteId}`
|
|
253
|
+
: "No preferred NetworkRoute is configured.",
|
|
254
|
+
"First call browser_auth get_connection and ensure this exact maintenance action is still active. Open a new browser (do not reuse an unrelated session) with the preferred identity, placement, and network route. Observe the current page, start one AuthRun with the exact purpose above, and settle it as verified, failed, cancelled, or a durable human intervention. For an external_provider authority use advance_external. If authentication is already valid, verify it without logging in again. If a health check finds invalid authentication, report it failed; automatic repair, when enabled, is a separate maintenance run. Never save/publish browser state in this run.",
|
|
255
|
+
]
|
|
256
|
+
.filter((line): line is string => line !== null)
|
|
257
|
+
.join("\n");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function maintenanceRetryAt(maintenance: SiteAuthMaintenanceClaim): Date {
|
|
261
|
+
const retrySeconds = Math.min(maintenance.healthPolicy.intervalSeconds ?? 300, 15 * 60);
|
|
262
|
+
return new Date(Date.now() + retrySeconds * 1_000);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function maintenanceErrorCategory(error: unknown): string {
|
|
266
|
+
if (!error || typeof error !== "object") return "unknown";
|
|
267
|
+
const name = (error as { name?: unknown }).name;
|
|
268
|
+
return typeof name === "string" && name.length <= 100 ? name : "unknown";
|
|
269
|
+
}
|
package/src/activities/types.ts
CHANGED
|
@@ -142,12 +142,16 @@ export type ActivityServices = ControlActivityServices &
|
|
|
142
142
|
};
|
|
143
143
|
|
|
144
144
|
export type CodexCapacityWaitRef = {
|
|
145
|
+
/** Absent only for Temporal histories written before provider-tagged waits. */
|
|
146
|
+
provider?: "codex" | "xai";
|
|
145
147
|
waiterId: string;
|
|
146
148
|
generation: number;
|
|
147
149
|
nextCheckAt: string;
|
|
148
150
|
wakeRevision: number;
|
|
149
151
|
};
|
|
150
152
|
|
|
153
|
+
export type XaiCapacityWaitRef = CodexCapacityWaitRef & { provider: "xai" };
|
|
154
|
+
|
|
151
155
|
export type GetCodexCapacityWaitInput = {
|
|
152
156
|
workspaceId: string;
|
|
153
157
|
sessionId: string;
|
|
@@ -160,6 +164,8 @@ export type ReconcileCodexCapacityWaitInput = {
|
|
|
160
164
|
waiterId: string;
|
|
161
165
|
generation: number;
|
|
162
166
|
cause: "timer" | "signal" | "queue" | "recovery";
|
|
167
|
+
/** Absent only for Codex waits and pre-provider-tagged workflow histories. */
|
|
168
|
+
provider?: "codex" | "xai";
|
|
163
169
|
};
|
|
164
170
|
|
|
165
171
|
export type ReconcileCodexCapacityWaitResult =
|
|
@@ -286,6 +292,17 @@ export type ExpireSessionHumanInputResult = {
|
|
|
286
292
|
action: "expired" | "stale" | "not_found";
|
|
287
293
|
};
|
|
288
294
|
|
|
295
|
+
export type ExpireSessionInteractionInterventionInput = {
|
|
296
|
+
accountId: string;
|
|
297
|
+
workspaceId: string;
|
|
298
|
+
sessionId: string;
|
|
299
|
+
interventionId: string;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export type ExpireSessionInteractionInterventionResult = {
|
|
303
|
+
action: "expired" | "stale" | "not_found";
|
|
304
|
+
};
|
|
305
|
+
|
|
289
306
|
export type MarkSessionIdleInput = {
|
|
290
307
|
workspaceId: string;
|
|
291
308
|
sessionId: string;
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
type GenerateVideoToolInput,
|
|
4
4
|
type VideoGenerationCapabilities,
|
|
5
5
|
type VideoGenerationPolicy,
|
|
6
|
+
type XaiProviderAccountAuthoritySnapshotV1,
|
|
6
7
|
} from "@opengeni/contracts";
|
|
7
8
|
import {
|
|
8
9
|
normalizeVideoGenerationRequest,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
markVideoGenerationPrepared,
|
|
19
20
|
type Database,
|
|
20
21
|
type VideoGenerationReference,
|
|
22
|
+
type XaiCredentialForRun,
|
|
21
23
|
} from "@opengeni/db";
|
|
22
24
|
import {
|
|
23
25
|
calculateVideoGenerationCreditCostMicros,
|
|
@@ -32,14 +34,16 @@ import {
|
|
|
32
34
|
videoReferenceStagingKey,
|
|
33
35
|
type SandboxCommandRunner,
|
|
34
36
|
} from "./video-reference-staging";
|
|
35
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
encryptVideoGenerationApiKey,
|
|
39
|
+
encryptVideoGenerationXaiCredential,
|
|
40
|
+
} from "./video-generation-credential";
|
|
36
41
|
|
|
37
42
|
export type VideoGenerationCredentialLease = Readonly<{
|
|
38
|
-
fundingSource: "opengeni_credits" | "workspace_gateway";
|
|
43
|
+
fundingSource: "opengeni_credits" | "workspace_gateway" | "supergrok_subscription";
|
|
39
44
|
connectionId: string | null;
|
|
40
45
|
version: number;
|
|
41
46
|
credentialEncrypted: string;
|
|
42
|
-
apiKey: string;
|
|
43
47
|
}>;
|
|
44
48
|
|
|
45
49
|
export type AcceptedVideoGeneration = Readonly<{
|
|
@@ -77,7 +81,36 @@ export function managedVideoGenerationCredentialLease(
|
|
|
77
81
|
encryptionKey,
|
|
78
82
|
settings.vercelAiGatewayApiKey,
|
|
79
83
|
),
|
|
80
|
-
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function xaiVideoGenerationCredentialLease(input: {
|
|
88
|
+
settings: Settings;
|
|
89
|
+
credential: XaiCredentialForRun;
|
|
90
|
+
subjectId: string;
|
|
91
|
+
authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
|
|
92
|
+
}): VideoGenerationCredentialLease {
|
|
93
|
+
const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
|
|
94
|
+
if (!encryptionKey) {
|
|
95
|
+
throw new Error("SuperGrok video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
|
|
96
|
+
}
|
|
97
|
+
const { accessToken, refreshToken } = input.credential.secret;
|
|
98
|
+
const userId = input.credential.providerAccountId;
|
|
99
|
+
if (!accessToken || !refreshToken || !userId) {
|
|
100
|
+
throw new Error("The selected SuperGrok account cannot fund video generation");
|
|
101
|
+
}
|
|
102
|
+
return Object.freeze({
|
|
103
|
+
fundingSource: "supergrok_subscription",
|
|
104
|
+
connectionId: null,
|
|
105
|
+
version: input.credential.version,
|
|
106
|
+
credentialEncrypted: encryptVideoGenerationXaiCredential(encryptionKey, {
|
|
107
|
+
accessToken,
|
|
108
|
+
refreshToken,
|
|
109
|
+
userId,
|
|
110
|
+
credentialId: input.credential.id,
|
|
111
|
+
subjectId: input.subjectId,
|
|
112
|
+
authoritySnapshot: input.authoritySnapshot,
|
|
113
|
+
}),
|
|
81
114
|
});
|
|
82
115
|
}
|
|
83
116
|
|