@opengeni/worker-bundle 0.17.2 → 0.20.1
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 +49 -1
- 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/run-input.d.ts +8 -0
- package/dist/activities/session-state.d.ts +7 -3
- package/dist/activities/site-auth-maintenance.d.ts +27 -0
- package/dist/activities/types.d.ts +43 -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-SMKF2OBE.js} +438 -55
- package/dist/activities-control-SMKF2OBE.js.map +1 -0
- package/dist/activities-control.d.ts +10 -2
- package/dist/{activities-turn-K4CEDEGJ.js → activities-turn-IDS3WEHA.js} +6704 -4847
- package/dist/activities-turn-IDS3WEHA.js.map +1 -0
- package/dist/activities.d.ts +11 -3
- package/dist/{chunk-E3RI3I33.js → chunk-N5TJIFCE.js} +156 -10
- package/dist/chunk-N5TJIFCE.js.map +1 -0
- package/dist/{chunk-KKCRGAGZ.js → chunk-P6Z2SLAA.js} +99 -98
- package/dist/chunk-P6Z2SLAA.js.map +1 -0
- package/dist/index.d.ts +14 -1
- 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 +57 -1
- package/dist/sandbox-routing.d.ts +4 -0
- package/dist/workflow-bundle.js +232 -18
- package/dist/workflows/activities.d.ts +1 -1
- package/dist/workflows/session.d.ts +17 -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 +2777 -1241
- 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/run-input.ts +137 -45
- package/src/activities/scheduled-tasks.ts +30 -73
- package/src/activities/session-state.ts +87 -5
- package/src/activities/site-auth-maintenance.ts +269 -0
- package/src/activities/types.ts +44 -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 +7 -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 +282 -12
- 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 +219 -14
- 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
|
@@ -85,8 +85,54 @@ export type TurnInputOptions = {
|
|
|
85
85
|
materializeSerializedRunState?: (serialized: string) => Promise<string>;
|
|
86
86
|
projectModelHistory?: ModelHistoryAttachmentProjector;
|
|
87
87
|
loadActiveHistory?: typeof getActiveSessionHistoryItemsPaged;
|
|
88
|
+
/** Bounded critical-path timings; telemetry failures never affect preparation. */
|
|
89
|
+
onPreparationPhase?: (measurement: HistoryPreparationPhaseMeasurement) => void;
|
|
88
90
|
};
|
|
89
91
|
|
|
92
|
+
export type HistoryPreparationPhase =
|
|
93
|
+
| "system_update_load"
|
|
94
|
+
| "current_attachment_resolution"
|
|
95
|
+
| "durable_history_load"
|
|
96
|
+
| "sandbox_envelope_load"
|
|
97
|
+
| "canonical_projection"
|
|
98
|
+
| "provider_projection"
|
|
99
|
+
| "attachment_ref_projection"
|
|
100
|
+
| "screenshot_materialization"
|
|
101
|
+
| "model_attachment_projection"
|
|
102
|
+
| "runtime_input_assembly"
|
|
103
|
+
| "artifact_candidate_scan";
|
|
104
|
+
|
|
105
|
+
export type HistoryPreparationPhaseMeasurement = {
|
|
106
|
+
phase: HistoryPreparationPhase;
|
|
107
|
+
outcome: "completed" | "failed";
|
|
108
|
+
durationSeconds: number;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
async function measureHistoryPreparationPhase<T>(
|
|
112
|
+
options: Pick<TurnInputOptions, "onPreparationPhase">,
|
|
113
|
+
phase: HistoryPreparationPhase,
|
|
114
|
+
operation: () => T | Promise<T>,
|
|
115
|
+
): Promise<T> {
|
|
116
|
+
const startedAt = performance.now();
|
|
117
|
+
let outcome: HistoryPreparationPhaseMeasurement["outcome"] = "completed";
|
|
118
|
+
try {
|
|
119
|
+
return await operation();
|
|
120
|
+
} catch (error) {
|
|
121
|
+
outcome = "failed";
|
|
122
|
+
throw error;
|
|
123
|
+
} finally {
|
|
124
|
+
try {
|
|
125
|
+
options.onPreparationPhase?.({
|
|
126
|
+
phase,
|
|
127
|
+
outcome,
|
|
128
|
+
durationSeconds: (performance.now() - startedAt) / 1_000,
|
|
129
|
+
});
|
|
130
|
+
} catch {
|
|
131
|
+
// Diagnostics must not change durable history or provider input.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
90
136
|
export const MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
|
|
91
137
|
|
|
92
138
|
export type ModelAttachmentContent = {
|
|
@@ -378,11 +424,16 @@ export async function turnInput(
|
|
|
378
424
|
if (!trigger) {
|
|
379
425
|
throw new Error("Missing trigger event");
|
|
380
426
|
}
|
|
381
|
-
const updates = await
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
427
|
+
const updates = await measureHistoryPreparationPhase(
|
|
428
|
+
options,
|
|
429
|
+
"system_update_load",
|
|
430
|
+
async () =>
|
|
431
|
+
await listSessionSystemUpdatesForTurn(
|
|
432
|
+
db,
|
|
433
|
+
trigger.workspaceId,
|
|
434
|
+
trigger.sessionId,
|
|
435
|
+
options.turnId,
|
|
436
|
+
),
|
|
386
437
|
);
|
|
387
438
|
if (updates.length > 0) {
|
|
388
439
|
const historyItemIds = new Set(
|
|
@@ -413,10 +464,10 @@ export async function turnInput(
|
|
|
413
464
|
throw new Error("user.message payload is missing text and annotations");
|
|
414
465
|
}
|
|
415
466
|
const resources = Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [];
|
|
416
|
-
const fileAttachments = await
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
resources,
|
|
467
|
+
const fileAttachments = await measureHistoryPreparationPhase(
|
|
468
|
+
options,
|
|
469
|
+
"current_attachment_resolution",
|
|
470
|
+
async () => await resolveUserMessageFileAttachments(db, trigger.workspaceId, resources),
|
|
420
471
|
);
|
|
421
472
|
const attachmentContext = userMessageAttachmentsContext(fileAttachments);
|
|
422
473
|
return await messageInput(
|
|
@@ -432,6 +483,7 @@ export async function turnInput(
|
|
|
432
483
|
options.materializeModelHistory,
|
|
433
484
|
options.projectModelHistory,
|
|
434
485
|
options.loadActiveHistory,
|
|
486
|
+
options,
|
|
435
487
|
);
|
|
436
488
|
}
|
|
437
489
|
if (trigger.type === "system.update.delivered") {
|
|
@@ -451,6 +503,7 @@ export async function turnInput(
|
|
|
451
503
|
options.materializeModelHistory,
|
|
452
504
|
options.projectModelHistory,
|
|
453
505
|
options.loadActiveHistory,
|
|
506
|
+
options,
|
|
454
507
|
);
|
|
455
508
|
}
|
|
456
509
|
if (trigger.type === "user.approvalDecision") {
|
|
@@ -536,46 +589,85 @@ async function messageInput(
|
|
|
536
589
|
materializeModelHistory?: ModelHistoryAttachmentProjector,
|
|
537
590
|
projectModelHistory?: ModelHistoryAttachmentProjector,
|
|
538
591
|
loadActiveHistory: typeof getActiveSessionHistoryItemsPaged = getActiveSessionHistoryItemsPaged,
|
|
592
|
+
preparationOptions: Pick<TurnInputOptions, "onPreparationPhase"> = {},
|
|
539
593
|
): Promise<PreparedTurnInput> {
|
|
540
|
-
const stored = await
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
const
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
594
|
+
const [stored, envelope] = await Promise.all([
|
|
595
|
+
measureHistoryPreparationPhase(preparationOptions, "durable_history_load", async () =>
|
|
596
|
+
loadActiveHistory(db, trigger.workspaceId, trigger.sessionId),
|
|
597
|
+
),
|
|
598
|
+
measureHistoryPreparationPhase(preparationOptions, "sandbox_envelope_load", async () =>
|
|
599
|
+
getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId),
|
|
600
|
+
),
|
|
601
|
+
]);
|
|
602
|
+
const canonicalView = await measureHistoryPreparationPhase(
|
|
603
|
+
preparationOptions,
|
|
604
|
+
"canonical_projection",
|
|
605
|
+
async () => {
|
|
606
|
+
const active = projectRejectedProviderArtifacts(stored);
|
|
607
|
+
return projectCanonicalHistory ? await projectCanonicalHistory(active) : active;
|
|
608
|
+
},
|
|
609
|
+
);
|
|
610
|
+
const providerView = await measureHistoryPreparationPhase(
|
|
611
|
+
preparationOptions,
|
|
612
|
+
"provider_projection",
|
|
613
|
+
() => projectHistoryForProvider(canonicalView, providerApi),
|
|
614
|
+
);
|
|
615
|
+
const referencedHistory = await measureHistoryPreparationPhase(
|
|
616
|
+
preparationOptions,
|
|
617
|
+
"attachment_ref_projection",
|
|
618
|
+
() => withCurrentUserAttachmentRefs(providerView, currentAttachmentRefs),
|
|
619
|
+
);
|
|
620
|
+
const materializedHistory = await measureHistoryPreparationPhase(
|
|
621
|
+
preparationOptions,
|
|
622
|
+
"screenshot_materialization",
|
|
623
|
+
async () =>
|
|
624
|
+
materializeModelHistory
|
|
625
|
+
? await materializeModelHistory(referencedHistory)
|
|
626
|
+
: referencedHistory,
|
|
627
|
+
);
|
|
628
|
+
const historyItems = await measureHistoryPreparationPhase(
|
|
629
|
+
preparationOptions,
|
|
630
|
+
"model_attachment_projection",
|
|
631
|
+
async () =>
|
|
632
|
+
projectModelHistory ? await projectModelHistory(materializedHistory) : materializedHistory,
|
|
633
|
+
);
|
|
634
|
+
const prepared = await measureHistoryPreparationPhase(
|
|
635
|
+
preparationOptions,
|
|
636
|
+
"runtime_input_assembly",
|
|
637
|
+
async () =>
|
|
638
|
+
await runtime.prepareInput(agent, {
|
|
639
|
+
kind: "message",
|
|
640
|
+
...(text ? { text } : {}),
|
|
641
|
+
...(internalContext ? { internalContext } : {}),
|
|
642
|
+
historyItems: historyItems as any,
|
|
643
|
+
sandboxEnvelope: envelope,
|
|
644
|
+
...(projectModelHistory ? { modelInputAlreadyProjected: true } : {}),
|
|
645
|
+
}),
|
|
646
|
+
);
|
|
647
|
+
const providerArtifactCandidates = await measureHistoryPreparationPhase(
|
|
648
|
+
preparationOptions,
|
|
649
|
+
"artifact_candidate_scan",
|
|
650
|
+
() => {
|
|
651
|
+
const preparedItems = Array.isArray(prepared.input)
|
|
652
|
+
? new Set(prepared.input)
|
|
653
|
+
: new Set<unknown>();
|
|
654
|
+
return {
|
|
655
|
+
knownHistoryItemIds: stored.map((row) => row.id),
|
|
656
|
+
historyItemIds: stored
|
|
657
|
+
.filter(
|
|
658
|
+
(row) =>
|
|
659
|
+
row.providerArtifactInvalidatedAt === null &&
|
|
660
|
+
hasOpaqueProviderArtifact(row.item) &&
|
|
661
|
+
preparedItems.has(row.item),
|
|
662
|
+
)
|
|
663
|
+
.map((row) => row.id),
|
|
664
|
+
};
|
|
665
|
+
},
|
|
666
|
+
);
|
|
565
667
|
return {
|
|
566
668
|
input: prepared,
|
|
567
669
|
persistedHistoryCount: prepared.persistedHistoryCount,
|
|
568
|
-
providerArtifactCandidates
|
|
569
|
-
knownHistoryItemIds: stored.map((row) => row.id),
|
|
570
|
-
historyItemIds: stored
|
|
571
|
-
.filter(
|
|
572
|
-
(row) =>
|
|
573
|
-
row.providerArtifactInvalidatedAt === null &&
|
|
574
|
-
hasOpaqueProviderArtifact(row.item) &&
|
|
575
|
-
preparedItems.has(row.item),
|
|
576
|
-
)
|
|
577
|
-
.map((row) => row.id),
|
|
578
|
-
},
|
|
670
|
+
providerArtifactCandidates,
|
|
579
671
|
};
|
|
580
672
|
}
|
|
581
673
|
|
|
@@ -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
|
-
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
settleSessionAttemptInterruptions,
|
|
3
3
|
applySessionTurnSettlement,
|
|
4
|
+
enqueueSessionWorkflowWake,
|
|
5
|
+
failSessionWorkBeforeAttemptClaim,
|
|
4
6
|
requestSessionTurnRecovery,
|
|
5
7
|
recoverSessionDispatch,
|
|
6
8
|
reconcileSessionAttemptQuiescence,
|
|
@@ -9,6 +11,7 @@ import {
|
|
|
9
11
|
getSessionAttemptActivityRef,
|
|
10
12
|
getSessionEvent,
|
|
11
13
|
getSessionTurnForAttempt,
|
|
14
|
+
expireSessionInteractionIntervention as expireSessionInteractionInterventionDb,
|
|
12
15
|
expireSessionHumanInputRequest,
|
|
13
16
|
markSessionAttemptQuiesced,
|
|
14
17
|
requireSession,
|
|
@@ -21,8 +24,11 @@ import type {
|
|
|
21
24
|
ControlActivityServices,
|
|
22
25
|
ExpireSessionHumanInputInput,
|
|
23
26
|
ExpireSessionHumanInputResult,
|
|
27
|
+
ExpireSessionInteractionInterventionInput,
|
|
28
|
+
ExpireSessionInteractionInterventionResult,
|
|
24
29
|
PeekSessionWorkInput,
|
|
25
30
|
FailSessionAttemptInput,
|
|
31
|
+
FailSessionAttemptResult,
|
|
26
32
|
SettleSessionInterruptionsInput,
|
|
27
33
|
MarkSessionIdleInput,
|
|
28
34
|
PersistSessionAttemptQuiescenceInput,
|
|
@@ -37,6 +43,8 @@ import type {
|
|
|
37
43
|
export type SessionStateActivityOverrides = Partial<{
|
|
38
44
|
settleSessionAttemptInterruptions: typeof settleSessionAttemptInterruptions;
|
|
39
45
|
applySessionTurnSettlement: typeof applySessionTurnSettlement;
|
|
46
|
+
enqueueSessionWorkflowWake: typeof enqueueSessionWorkflowWake;
|
|
47
|
+
failSessionWorkBeforeAttemptClaim: typeof failSessionWorkBeforeAttemptClaim;
|
|
40
48
|
requestSessionTurnRecovery: typeof requestSessionTurnRecovery;
|
|
41
49
|
recoverSessionDispatch: typeof recoverSessionDispatch;
|
|
42
50
|
reconcileSessionAttemptQuiescence: typeof reconcileSessionAttemptQuiescence;
|
|
@@ -46,6 +54,7 @@ export type SessionStateActivityOverrides = Partial<{
|
|
|
46
54
|
getSessionEvent: typeof getSessionEvent;
|
|
47
55
|
getSessionTurnForAttempt: typeof getSessionTurnForAttempt;
|
|
48
56
|
expireSessionHumanInputRequest: typeof expireSessionHumanInputRequest;
|
|
57
|
+
expireSessionInteractionIntervention: typeof expireSessionInteractionInterventionDb;
|
|
49
58
|
requireSession: typeof requireSession;
|
|
50
59
|
settleSessionIdleWithParentOutbox: typeof settleSessionIdleWithParentOutbox;
|
|
51
60
|
markSessionAttemptQuiesced: typeof markSessionAttemptQuiesced;
|
|
@@ -69,6 +78,10 @@ export function createSessionStateActivities(
|
|
|
69
78
|
overrides.settleSessionAttemptInterruptions ?? settleSessionAttemptInterruptions;
|
|
70
79
|
const applySessionTurnSettlementFn =
|
|
71
80
|
overrides.applySessionTurnSettlement ?? applySessionTurnSettlement;
|
|
81
|
+
const enqueueSessionWorkflowWakeFn =
|
|
82
|
+
overrides.enqueueSessionWorkflowWake ?? enqueueSessionWorkflowWake;
|
|
83
|
+
const failSessionWorkBeforeAttemptClaimFn =
|
|
84
|
+
overrides.failSessionWorkBeforeAttemptClaim ?? failSessionWorkBeforeAttemptClaim;
|
|
72
85
|
const requestSessionTurnRecoveryFn =
|
|
73
86
|
overrides.requestSessionTurnRecovery ?? requestSessionTurnRecovery;
|
|
74
87
|
const recoverSessionDispatchFn = overrides.recoverSessionDispatch ?? recoverSessionDispatch;
|
|
@@ -82,6 +95,8 @@ export function createSessionStateActivities(
|
|
|
82
95
|
const getSessionTurnForAttemptFn = overrides.getSessionTurnForAttempt ?? getSessionTurnForAttempt;
|
|
83
96
|
const expireSessionHumanInputRequestFn =
|
|
84
97
|
overrides.expireSessionHumanInputRequest ?? expireSessionHumanInputRequest;
|
|
98
|
+
const expireSessionInteractionInterventionFn =
|
|
99
|
+
overrides.expireSessionInteractionIntervention ?? expireSessionInteractionInterventionDb;
|
|
85
100
|
const requireSessionFn = overrides.requireSession ?? requireSession;
|
|
86
101
|
const settleSessionIdleWithParentOutboxFn =
|
|
87
102
|
overrides.settleSessionIdleWithParentOutbox ?? settleSessionIdleWithParentOutbox;
|
|
@@ -94,19 +109,73 @@ export function createSessionStateActivities(
|
|
|
94
109
|
const notifyParentOfChildIdleFn = overrides.notifyParentOfChildIdle ?? notifyParentOfChildIdle;
|
|
95
110
|
const recordTurnsQueuedGaugeFn = overrides.recordTurnsQueuedGauge ?? recordTurnsQueuedGauge;
|
|
96
111
|
|
|
97
|
-
async function failSessionAttempt(
|
|
112
|
+
async function failSessionAttempt(
|
|
113
|
+
input: FailSessionAttemptInput,
|
|
114
|
+
): Promise<FailSessionAttemptResult> {
|
|
98
115
|
const { db, bus, settings, observability, wakeSessionWorkflow } = await services();
|
|
99
116
|
const session = await requireSessionFn(db, input.workspaceId, input.sessionId);
|
|
100
|
-
if (session.status === "failed") {
|
|
101
|
-
|
|
117
|
+
if (session.status === "failed" || session.status === "cancelled") {
|
|
118
|
+
// The activity may be retried after failure settlement committed but its
|
|
119
|
+
// response or event fanout was lost. Preserve terminal session truth so
|
|
120
|
+
// the workflow cannot interpret an idle peek as permission to synthesize
|
|
121
|
+
// an active-goal continuation.
|
|
122
|
+
return { action: "terminal" };
|
|
102
123
|
}
|
|
124
|
+
const workflowId =
|
|
125
|
+
input.workflowId ?? session.temporalWorkflowId ?? `session-${input.sessionId}`;
|
|
103
126
|
const turn = await getSessionTurnForAttemptFn(
|
|
104
127
|
db,
|
|
105
128
|
input.workspaceId,
|
|
106
129
|
input.sessionId,
|
|
107
130
|
input.attemptId,
|
|
108
131
|
);
|
|
109
|
-
if (!turn)
|
|
132
|
+
if (!turn) {
|
|
133
|
+
const attempt = await getSessionAttemptActivityRefFn(db, {
|
|
134
|
+
accountId: input.accountId,
|
|
135
|
+
workspaceId: input.workspaceId,
|
|
136
|
+
sessionId: input.sessionId,
|
|
137
|
+
attemptId: input.attemptId,
|
|
138
|
+
temporalWorkflowId: workflowId,
|
|
139
|
+
});
|
|
140
|
+
if (attempt) return { action: "stale" };
|
|
141
|
+
|
|
142
|
+
if (input.preClaimFailureDisposition === "permanent" && input.trigger) {
|
|
143
|
+
const failed = await failSessionWorkBeforeAttemptClaimFn(db, input.workspaceId, {
|
|
144
|
+
accountId: input.accountId,
|
|
145
|
+
sessionId: input.sessionId,
|
|
146
|
+
workflowId,
|
|
147
|
+
trigger: input.trigger,
|
|
148
|
+
error: input.error ?? "Agent turn admission failed before attempt claim.",
|
|
149
|
+
});
|
|
150
|
+
if (failed.action === "terminal") return { action: "terminal" };
|
|
151
|
+
if (failed.action === "stale") return { action: "stale" };
|
|
152
|
+
await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, failed.events);
|
|
153
|
+
if (failed.turnId) {
|
|
154
|
+
await deliverFailedChildTurnToParentFn(
|
|
155
|
+
{ db, bus, settings, observability, wakeSessionWorkflow },
|
|
156
|
+
input.workspaceId,
|
|
157
|
+
input.sessionId,
|
|
158
|
+
failed.turnId,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
return { action: "failed" };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const requestedRetryDelayMs = input.retryDelayMs;
|
|
165
|
+
const retryDelayMs =
|
|
166
|
+
typeof requestedRetryDelayMs === "number" && Number.isFinite(requestedRetryDelayMs)
|
|
167
|
+
? Math.max(1_000, Math.min(60_000, Math.trunc(requestedRetryDelayMs)))
|
|
168
|
+
: 60_000;
|
|
169
|
+
await enqueueSessionWorkflowWakeFn(db, {
|
|
170
|
+
accountId: input.accountId,
|
|
171
|
+
workspaceId: input.workspaceId,
|
|
172
|
+
sessionId: input.sessionId,
|
|
173
|
+
temporalWorkflowId: workflowId,
|
|
174
|
+
reason: "turn_activity_failed_before_attempt_claim",
|
|
175
|
+
notBefore: new Date(Date.now() + retryDelayMs),
|
|
176
|
+
});
|
|
177
|
+
return { action: "unclaimed" };
|
|
178
|
+
}
|
|
110
179
|
const trigger = await getSessionEventFn(db, input.workspaceId, turn.triggerEventId);
|
|
111
180
|
const result = await applySessionTurnSettlementFn(db, input.workspaceId, {
|
|
112
181
|
sessionId: input.sessionId,
|
|
@@ -129,7 +198,7 @@ export function createSessionStateActivities(
|
|
|
129
198
|
],
|
|
130
199
|
});
|
|
131
200
|
if (result.action === "stale") {
|
|
132
|
-
return;
|
|
201
|
+
return { action: "stale" };
|
|
133
202
|
}
|
|
134
203
|
await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, result.events);
|
|
135
204
|
await deliverFailedChildTurnToParentFn(
|
|
@@ -138,6 +207,7 @@ export function createSessionStateActivities(
|
|
|
138
207
|
input.sessionId,
|
|
139
208
|
turn.id,
|
|
140
209
|
);
|
|
210
|
+
return { action: "failed" };
|
|
141
211
|
}
|
|
142
212
|
|
|
143
213
|
async function settleSessionInterruptions(
|
|
@@ -347,6 +417,17 @@ export function createSessionStateActivities(
|
|
|
347
417
|
};
|
|
348
418
|
}
|
|
349
419
|
|
|
420
|
+
async function expireSessionInteractionIntervention(
|
|
421
|
+
input: ExpireSessionInteractionInterventionInput,
|
|
422
|
+
): Promise<ExpireSessionInteractionInterventionResult> {
|
|
423
|
+
const { db, bus } = await services();
|
|
424
|
+
const result = await expireSessionInteractionInterventionFn(db, input);
|
|
425
|
+
if (result.events.length > 0) {
|
|
426
|
+
await publishDurableSessionEventsFn(bus, input.workspaceId, input.sessionId, result.events);
|
|
427
|
+
}
|
|
428
|
+
return { action: result.action };
|
|
429
|
+
}
|
|
430
|
+
|
|
350
431
|
async function markSessionIdle(input: MarkSessionIdleInput): Promise<void> {
|
|
351
432
|
const { db, bus, settings, observability, wakeSessionWorkflow } = await services();
|
|
352
433
|
const settled = await settleSessionIdleWithParentOutboxFn(
|
|
@@ -383,6 +464,7 @@ export function createSessionStateActivities(
|
|
|
383
464
|
recoverEscapedMcpTimeout,
|
|
384
465
|
peekSessionWork,
|
|
385
466
|
expireSessionHumanInput,
|
|
467
|
+
expireSessionInteractionIntervention,
|
|
386
468
|
markSessionIdle,
|
|
387
469
|
};
|
|
388
470
|
}
|