@opengeni/worker-bundle 0.20.3 → 0.22.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/activities/agent-turn/admission.d.ts +52 -0
- package/dist/activities/agent-turn/agent-build.d.ts +89 -0
- package/dist/activities/agent-turn/claim.d.ts +67 -0
- package/dist/activities/agent-turn/codex-capacity.d.ts +40 -0
- package/dist/activities/agent-turn/codex.d.ts +32 -0
- package/dist/activities/agent-turn/compaction-prep.d.ts +88 -0
- package/dist/activities/agent-turn/credential-leases.d.ts +56 -0
- package/dist/activities/agent-turn/errors.d.ts +253 -0
- package/dist/activities/agent-turn/failure-settlement.d.ts +33 -0
- package/dist/activities/agent-turn/file-resources.d.ts +37 -0
- package/dist/activities/agent-turn/finalization.d.ts +42 -0
- package/dist/activities/agent-turn/github-repository-bindings.d.ts +105 -0
- package/dist/activities/agent-turn/governance-model.d.ts +66 -0
- package/dist/activities/agent-turn/history-sink.d.ts +52 -0
- package/dist/activities/agent-turn/history.d.ts +63 -0
- package/dist/activities/agent-turn/media-artifacts.d.ts +145 -0
- package/dist/activities/agent-turn/model-usage.d.ts +218 -0
- package/dist/activities/agent-turn/quiescence.d.ts +135 -0
- package/dist/activities/agent-turn/run-credentials.d.ts +84 -0
- package/dist/activities/agent-turn/run.d.ts +3 -0
- package/dist/activities/agent-turn/sandbox-establish.d.ts +93 -0
- package/dist/activities/agent-turn/sandbox-provision.d.ts +89 -0
- package/dist/activities/agent-turn/sandbox-route.d.ts +151 -0
- package/dist/activities/agent-turn/sandbox-runtime.d.ts +44 -0
- package/dist/activities/agent-turn/stream-attempt.d.ts +107 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +142 -0
- package/dist/activities/agent-turn/tool-policy.d.ts +127 -0
- package/dist/activities/agent-turn/tool-result-spill.d.ts +59 -0
- package/dist/activities/agent-turn/turn-context.d.ts +144 -0
- package/dist/activities/agent-turn/xai-capacity.d.ts +2 -0
- package/dist/activities/agent-turn.d.ts +12 -954
- package/dist/activities/automations.d.ts +16 -0
- package/dist/activities/common.d.ts +1 -1
- package/dist/activities/connector-attachments.d.ts +13 -0
- package/dist/activities/documents.d.ts +1 -0
- package/dist/activities/environment.d.ts +26 -15
- package/dist/activities/generated-images.d.ts +1 -1
- package/dist/activities/git-credential-renewal.d.ts +3 -1
- package/dist/activities/goals.d.ts +4 -1
- package/dist/activities/google-drive-provider.d.ts +29 -0
- package/dist/activities/google-drive-publication.d.ts +17 -4
- package/dist/activities/incident-telemetry-authority.d.ts +52 -0
- package/dist/activities/incident-telemetry-preflight.d.ts +71 -0
- package/dist/activities/knowledge-source-sync.d.ts +4 -0
- package/dist/activities/mcp-credentials.d.ts +9 -3
- package/dist/activities/open-suffix-resume.d.ts +77 -0
- package/dist/activities/packs.d.ts +3 -11
- package/dist/activities/parent-wake.d.ts +19 -0
- package/dist/activities/retained-screenshot-reaper.d.ts +1 -1
- package/dist/activities/retained-screenshots.d.ts +1 -1
- package/dist/activities/rig-verification.d.ts +1 -1
- package/dist/activities/run-input.d.ts +9 -10
- package/dist/activities/sandbox-images.d.ts +1 -14
- package/dist/activities/sandbox-lease.d.ts +21 -3
- package/dist/activities/scheduled-tasks.d.ts +47 -0
- package/dist/activities/types.d.ts +23 -2
- package/dist/activities/video-media-validation.d.ts +1 -1
- package/dist/activities/video-output-retention.d.ts +1 -2
- package/dist/activities/xai-auth.d.ts +3 -0
- package/dist/{activities-control-BJSDRSIP.js → activities-control-DKG22ONJ.js} +2646 -551
- package/dist/activities-control-DKG22ONJ.js.map +1 -0
- package/dist/activities-control.d.ts +6 -2
- package/dist/activities-turn-4IMC3MAA.js +21326 -0
- package/dist/activities-turn-4IMC3MAA.js.map +1 -0
- package/dist/activities.d.ts +7 -3
- package/dist/artifact-materializer-entry.js +41 -22
- package/dist/artifact-materializer-entry.js.map +1 -1
- package/dist/{chunk-N5TJIFCE.js → chunk-CE7TPKBS.js} +468 -1
- package/dist/chunk-CE7TPKBS.js.map +1 -0
- package/dist/chunk-CNYGVRKF.js +4698 -0
- package/dist/chunk-CNYGVRKF.js.map +1 -0
- package/dist/connection-provider-use.d.ts +18 -0
- package/dist/editable-artifact-materializer.d.ts +4 -3
- package/dist/github-rest-mcp.d.ts +26 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.js +58 -14
- package/dist/index.js.map +1 -1
- package/dist/knowledge-source-sync-activity-policy.d.ts +2 -0
- package/dist/model-context-contributions.d.ts +8 -4
- package/dist/observability-metrics.d.ts +63 -4
- package/dist/opensandbox-kubernetes-inventory.d.ts +21 -0
- package/dist/personal-github-git-credentials.d.ts +9 -0
- package/dist/pr-review-credentials.d.ts +6 -0
- package/dist/sandbox-archive-storage.d.ts +45 -0
- package/dist/sandbox-resume.d.ts +65 -6
- package/dist/sandbox-routing.d.ts +38 -2
- package/dist/scheduled-alert-occurrence.d.ts +33 -0
- package/dist/workflow-bundle.js +87 -28
- package/dist/workflows/activities.d.ts +1 -0
- package/dist/workflows/automations.d.ts +6 -0
- package/dist/workflows/document-index.d.ts +1 -0
- package/dist/workflows.d.ts +1 -0
- package/package.json +17 -17
- package/src/activities/agent-turn/admission.ts +292 -0
- package/src/activities/agent-turn/agent-build.ts +802 -0
- package/src/activities/agent-turn/claim.ts +590 -0
- package/src/activities/agent-turn/codex-capacity.ts +720 -0
- package/src/activities/agent-turn/codex.ts +94 -0
- package/src/activities/agent-turn/compaction-prep.ts +722 -0
- package/src/activities/agent-turn/credential-leases.ts +240 -0
- package/src/activities/agent-turn/errors.ts +1068 -0
- package/src/activities/agent-turn/failure-settlement.ts +1436 -0
- package/src/activities/agent-turn/file-resources.ts +227 -0
- package/src/activities/agent-turn/finalization.ts +754 -0
- package/src/activities/agent-turn/github-repository-bindings.ts +300 -0
- package/src/activities/agent-turn/governance-model.ts +465 -0
- package/src/activities/agent-turn/history-sink.ts +139 -0
- package/src/activities/agent-turn/history.ts +200 -0
- package/src/activities/agent-turn/media-artifacts.ts +359 -0
- package/src/activities/agent-turn/model-usage.ts +823 -0
- package/src/activities/agent-turn/quiescence.ts +353 -0
- package/src/activities/agent-turn/run-credentials.ts +761 -0
- package/src/activities/agent-turn/run.ts +1530 -0
- package/src/activities/agent-turn/sandbox-establish.ts +1207 -0
- package/src/activities/agent-turn/sandbox-provision.ts +569 -0
- package/src/activities/agent-turn/sandbox-route.ts +358 -0
- package/src/activities/agent-turn/sandbox-runtime.ts +731 -0
- package/src/activities/agent-turn/stream-attempt.ts +1670 -0
- package/src/activities/agent-turn/tool-environment.ts +729 -0
- package/src/activities/agent-turn/tool-policy.ts +216 -0
- package/src/activities/agent-turn/tool-result-spill.ts +265 -0
- package/src/activities/agent-turn/turn-context.ts +281 -0
- package/src/activities/agent-turn/xai-capacity.ts +229 -0
- package/src/activities/agent-turn.ts +12 -12701
- package/src/activities/api-integrations.ts +3 -0
- package/src/activities/automations.ts +214 -0
- package/src/activities/codemode-dispatcher.ts +12 -1
- package/src/activities/common.ts +1 -5
- package/src/activities/connector-attachments.ts +168 -0
- package/src/activities/context-compaction.ts +18 -0
- package/src/activities/environment.ts +151 -55
- package/src/activities/generated-images.ts +26 -11
- package/src/activities/git-credential-renewal.ts +13 -3
- package/src/activities/goals.ts +85 -19
- package/src/activities/google-drive-provider.ts +178 -0
- package/src/activities/google-drive-publication.ts +147 -49
- package/src/activities/incident-telemetry-authority.ts +327 -0
- package/src/activities/incident-telemetry-preflight.ts +317 -0
- package/src/activities/knowledge-source-sync.ts +178 -37
- package/src/activities/mcp-credentials.ts +191 -17
- package/src/activities/open-suffix-resume.ts +422 -0
- package/src/activities/packs.ts +12 -60
- package/src/activities/parent-wake.ts +64 -14
- package/src/activities/retained-screenshot-reaper.ts +6 -3
- package/src/activities/retained-screenshots.ts +15 -47
- package/src/activities/rig-verification.ts +1 -1
- package/src/activities/run-input.ts +102 -142
- package/src/activities/sandbox-images.ts +7 -166
- package/src/activities/sandbox-lease.ts +406 -15
- package/src/activities/scheduled-tasks.ts +2103 -411
- package/src/activities/session-state.ts +10 -2
- package/src/activities/types.ts +32 -2
- package/src/activities/video-media-validation.ts +3 -3
- package/src/activities/video-output-retention.ts +2 -57
- package/src/activities/xai-auth.ts +8 -0
- package/src/activities-control.ts +2 -0
- package/src/activities.ts +3 -0
- package/src/activity-services.ts +13 -4
- package/src/connection-provider-use.ts +36 -0
- package/src/editable-artifact-materializer-subprocess.ts +25 -14
- package/src/editable-artifact-materializer.ts +29 -18
- package/src/github-rest-mcp.ts +713 -0
- package/src/http.ts +16 -6
- package/src/index.ts +40 -5
- package/src/knowledge-source-sync-activity-policy.ts +2 -0
- package/src/model-context-contributions.ts +33 -10
- package/src/observability-metrics.ts +357 -0
- package/src/opensandbox-kubernetes-inventory.ts +292 -0
- package/src/personal-github-git-credentials.ts +331 -0
- package/src/pr-review-credentials.ts +163 -0
- package/src/sandbox-archive-storage.ts +159 -0
- package/src/sandbox-resume.ts +449 -63
- package/src/sandbox-routing.ts +314 -16
- package/src/scheduled-alert-occurrence.ts +176 -0
- package/src/workflows/activities.ts +18 -2
- package/src/workflows/automations.ts +11 -0
- package/src/workflows/session.ts +13 -3
- package/src/workflows.ts +1 -0
- package/dist/activities-control-BJSDRSIP.js.map +0 -1
- package/dist/activities-turn-J5YO7XXX.js +0 -17243
- package/dist/activities-turn-J5YO7XXX.js.map +0 -1
- package/dist/chunk-N5TJIFCE.js.map +0 -1
- package/dist/chunk-P6Z2SLAA.js +0 -3092
- package/dist/chunk-P6Z2SLAA.js.map +0 -1
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type { Settings } from "@opengeni/config";
|
|
2
|
+
import type { CodexUsageHeaderSnapshot } from "@opengeni/codex";
|
|
3
|
+
import type { AppendEventInput, ApplySessionTurnSettlementInput } from "@opengeni/db";
|
|
4
|
+
import type {
|
|
5
|
+
ModelContextContributionSummary,
|
|
6
|
+
SessionStatus,
|
|
7
|
+
XaiProviderAccountAuthoritySnapshotV1,
|
|
8
|
+
} from "@opengeni/contracts";
|
|
9
|
+
import type {
|
|
10
|
+
EstablishedSandboxSession,
|
|
11
|
+
OpenGeniRuntime,
|
|
12
|
+
RunCredentialCommandSession,
|
|
13
|
+
SelfhostedSession,
|
|
14
|
+
TurnToolCancellationFence,
|
|
15
|
+
} from "@opengeni/runtime";
|
|
16
|
+
import type { XaiSubscriptionRequestContext } from "@opengeni/xai-subscription";
|
|
17
|
+
import type { CodemodeAttemptDispatcher } from "../codemode-dispatcher";
|
|
18
|
+
import type { CodemodeTokenRenewalController } from "../codemode-token-renewal";
|
|
19
|
+
import type { GitCredentialRenewalController } from "../git-credential-renewal";
|
|
20
|
+
import type { RunCredentialRenewalController } from "../run-credential-renewal";
|
|
21
|
+
import type { ActiveRecording } from "../recording";
|
|
22
|
+
import type { createRuntimeBatcher, startActivityHeartbeat } from "../streaming";
|
|
23
|
+
import type { RunAgentTurnResult } from "../types";
|
|
24
|
+
import type { TurnOutcome } from "../../observability-metrics";
|
|
25
|
+
import type { ResumedTurnSandbox, TurnSandboxLeaseHolderId } from "../../sandbox-resume";
|
|
26
|
+
import type { TurnEventPublisher } from "./model-usage";
|
|
27
|
+
import type { TurnSandboxProvisioner } from "./sandbox-provision";
|
|
28
|
+
|
|
29
|
+
/** Stamps the claimed turn/attempt ids onto a phase's own outcome. */
|
|
30
|
+
export type ClaimedResult = (
|
|
31
|
+
result: Omit<
|
|
32
|
+
Extract<RunAgentTurnResult, { status: Exclude<RunAgentTurnResult["status"], "unclaimed"> }>,
|
|
33
|
+
"turnId" | "attemptId"
|
|
34
|
+
>,
|
|
35
|
+
) => RunAgentTurnResult;
|
|
36
|
+
|
|
37
|
+
export type TurnSettleFn = (input: {
|
|
38
|
+
events: Array<Omit<AppendEventInput, "producerId" | "producerSeq" | "turnId">>;
|
|
39
|
+
turnStatus: "queued" | "running" | "completed" | "failed" | "cancelled" | "requires_action";
|
|
40
|
+
sessionStatus: SessionStatus;
|
|
41
|
+
activeTurnId: string | null;
|
|
42
|
+
consumeRequestedCompactionFailure?: boolean;
|
|
43
|
+
runState?: ApplySessionTurnSettlementInput["runState"];
|
|
44
|
+
}) => Promise<boolean>;
|
|
45
|
+
|
|
46
|
+
export type TurnControlState = {
|
|
47
|
+
cancellationRequestedAt: number | null;
|
|
48
|
+
activityStatus: RunAgentTurnResult["status"] | "unknown";
|
|
49
|
+
turnMetricOutcome: TurnOutcome | null;
|
|
50
|
+
activityError: unknown;
|
|
51
|
+
acknowledgeQuiescence: boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type AttemptIdentityState = {
|
|
55
|
+
turnId: string | undefined;
|
|
56
|
+
triggerEventId: string | undefined;
|
|
57
|
+
executionGeneration: number;
|
|
58
|
+
providerRecoveryCount: number;
|
|
59
|
+
modelRequestStarted: boolean;
|
|
60
|
+
redispatchesAtDispatch: number;
|
|
61
|
+
// Held for same-turn recovery: an approval-decision rerun must re-enter
|
|
62
|
+
// through the suffix/history resume path, never through a swapped trigger.
|
|
63
|
+
triggerType: string | null;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type BillingState = {
|
|
67
|
+
isCodexTurn: boolean;
|
|
68
|
+
isXaiTurn: boolean;
|
|
69
|
+
isExternallyBilledTurn: boolean;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export type SandboxRuntimeState = {
|
|
73
|
+
resolvedSandbox: ResumedTurnSandbox | null;
|
|
74
|
+
attemptWritersDrained: boolean;
|
|
75
|
+
lateSandboxesAwaitingWriterDrain: Set<ResumedTurnSandbox>;
|
|
76
|
+
machinePrimarySession: SelfhostedSession | null;
|
|
77
|
+
lazyOwnedSandbox: EstablishedSandboxSession | null;
|
|
78
|
+
firstModelPreparationNestedSandboxMs: number;
|
|
79
|
+
firstModelPreparationNestedSandboxPhases: Array<{
|
|
80
|
+
phase: "admission" | "provider" | "settlement" | "snapshot_wait";
|
|
81
|
+
outcome: "completed" | "failed";
|
|
82
|
+
durationSeconds: number;
|
|
83
|
+
}>;
|
|
84
|
+
turnSandboxProvisioner: TurnSandboxProvisioner<ResumedTurnSandbox> | null;
|
|
85
|
+
resumeManagedGroupBox: (() => Promise<ResumedTurnSandbox>) | null;
|
|
86
|
+
prefetchedManagedBox: Promise<ResumedTurnSandbox> | null;
|
|
87
|
+
prefetchedManagedBoxResult: ResumedTurnSandbox | null;
|
|
88
|
+
setupBoxSession: unknown;
|
|
89
|
+
sandboxHolderId: TurnSandboxLeaseHolderId | null;
|
|
90
|
+
sandboxGroupId: string | null;
|
|
91
|
+
leaseHeartbeatTimer: ReturnType<typeof setInterval> | undefined;
|
|
92
|
+
rotationInFlight: Promise<void> | null;
|
|
93
|
+
snapshotInFlight: Promise<void> | null;
|
|
94
|
+
firstProviderRequestStarted: boolean;
|
|
95
|
+
turnEndCaptureInProgress: boolean;
|
|
96
|
+
// Backend label for canonical startup milestones. Null until the route
|
|
97
|
+
// resolves, so milestones recorded before then fall back to the turn's own
|
|
98
|
+
// recorded backend and later ones report the effective route.
|
|
99
|
+
startupMilestoneBackend: Settings["sandboxBackend"] | null;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export type RenewalState = {
|
|
103
|
+
gitCredentialRenewals: GitCredentialRenewalController[];
|
|
104
|
+
gitCredentialRenewalClosed: boolean;
|
|
105
|
+
runCredentialRenewal: RunCredentialRenewalController | null;
|
|
106
|
+
runCredentialRenewalClosed: boolean;
|
|
107
|
+
runCredentialSession: RunCredentialCommandSession | null;
|
|
108
|
+
codemodeTokenRenewal: CodemodeTokenRenewalController | null;
|
|
109
|
+
codemodeTokenRenewalClosed: boolean;
|
|
110
|
+
publishedRunCredentialNotices: Set<string>;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type RecordingState = {
|
|
114
|
+
activeRecording: ActiveRecording | null;
|
|
115
|
+
computerUseRecordingStart: Promise<void> | null;
|
|
116
|
+
didComputerUse: boolean;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type EventingState = {
|
|
120
|
+
heartbeatTimer: ReturnType<typeof startActivityHeartbeat> | undefined;
|
|
121
|
+
batcher: ReturnType<typeof createRuntimeBatcher> | null;
|
|
122
|
+
preparedTools: Awaited<ReturnType<OpenGeniRuntime["prepareTools"]>> | null;
|
|
123
|
+
toolPreparationReady: Promise<void> | null;
|
|
124
|
+
toolPreparationClosing: boolean;
|
|
125
|
+
codemodeDispatcher: CodemodeAttemptDispatcher | null;
|
|
126
|
+
toolCancellationFenceRef: { current: TurnToolCancellationFence | null };
|
|
127
|
+
publish: TurnEventPublisher | null;
|
|
128
|
+
settle: TurnSettleFn | null;
|
|
129
|
+
turnStartedPublished: boolean;
|
|
130
|
+
stream: Awaited<ReturnType<OpenGeniRuntime["runStream"]>> | undefined;
|
|
131
|
+
modelRunSettings: Settings;
|
|
132
|
+
firstModelRequestPreparationStartedAt: number | null;
|
|
133
|
+
firstModelRequestPreparationRecorded: boolean;
|
|
134
|
+
firstModelRequestCheckpointAt: number | null;
|
|
135
|
+
companyBrainContextContributions: readonly ModelContextContributionSummary[] | null;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Rig telemetry (M3): set once the session loads; empty string for a rig-less
|
|
139
|
+
* turn (mirrors variableSetId). Read by the activity span's finally block. */
|
|
140
|
+
export type WorkspaceRefState = {
|
|
141
|
+
variableSetId: string;
|
|
142
|
+
rigId: string;
|
|
143
|
+
rigVersionId: string;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export type ProviderTurnState = {
|
|
147
|
+
// The Codex account this turn runs on (pin > workspace active), resolved once
|
|
148
|
+
// a codex-billed turn is confirmed and threaded into the token resolver.
|
|
149
|
+
effectiveCodexCredentialId: string | null;
|
|
150
|
+
effectiveXaiCredentialId: string | null;
|
|
151
|
+
xaiRotationEnabled: boolean;
|
|
152
|
+
xaiAuthoritySnapshot: XaiProviderAccountAuthoritySnapshotV1 | null;
|
|
153
|
+
xaiRequestContext: XaiSubscriptionRequestContext | null;
|
|
154
|
+
xaiCredentialQuarantined: boolean;
|
|
155
|
+
// The session's Codex credential BEFORE this turn resolved its own — captured
|
|
156
|
+
// before recordSessionActiveCodexCredential overwrites the durable pointer, so
|
|
157
|
+
// a per-call usage log can report whether the serving account CHANGED since the
|
|
158
|
+
// session's previous call (the prompt-cache account-switch hypothesis).
|
|
159
|
+
priorSessionCodexCredentialId: string | null;
|
|
160
|
+
// The latest usage-header snapshot scraped for free off this turn's
|
|
161
|
+
// `/codex/responses` responses (a turn issues many model calls; latest wins).
|
|
162
|
+
// Flushed ONCE into the P2 usage cache for the serving account in the turn
|
|
163
|
+
// finalizer — cheaper than a /wham/usage poll AND it self-heals P3 rotation
|
|
164
|
+
// (the proactive + 429 rankers read these exact columns). null ⇒ nothing
|
|
165
|
+
// scraped. Lives on the turn context so the finalizer sees it; the sink is
|
|
166
|
+
// wired into codexContext.onUsageHeaders by the orchestrator.
|
|
167
|
+
latestCodexUsage: CodexUsageHeaderSnapshot | null;
|
|
168
|
+
lastCodexRequestOpaqueArtifacts: readonly string[];
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type TurnContext = {
|
|
172
|
+
control: TurnControlState;
|
|
173
|
+
attempt: AttemptIdentityState;
|
|
174
|
+
billingState: BillingState;
|
|
175
|
+
sandboxState: SandboxRuntimeState;
|
|
176
|
+
renewals: RenewalState;
|
|
177
|
+
recordingState: RecordingState;
|
|
178
|
+
eventing: EventingState;
|
|
179
|
+
workspaceRefs: WorkspaceRefState;
|
|
180
|
+
providerTurn: ProviderTurnState;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export function createTurnContext(input: {
|
|
184
|
+
settings: Settings;
|
|
185
|
+
cancellationRequestedAt: number | null;
|
|
186
|
+
}): TurnContext {
|
|
187
|
+
return {
|
|
188
|
+
control: {
|
|
189
|
+
cancellationRequestedAt: input.cancellationRequestedAt,
|
|
190
|
+
activityStatus: "unknown",
|
|
191
|
+
turnMetricOutcome: null,
|
|
192
|
+
activityError: undefined,
|
|
193
|
+
acknowledgeQuiescence: false,
|
|
194
|
+
},
|
|
195
|
+
attempt: {
|
|
196
|
+
turnId: undefined,
|
|
197
|
+
triggerEventId: undefined,
|
|
198
|
+
executionGeneration: 0,
|
|
199
|
+
providerRecoveryCount: 0,
|
|
200
|
+
modelRequestStarted: false,
|
|
201
|
+
redispatchesAtDispatch: 0,
|
|
202
|
+
triggerType: null,
|
|
203
|
+
},
|
|
204
|
+
billingState: {
|
|
205
|
+
isCodexTurn: false,
|
|
206
|
+
isXaiTurn: false,
|
|
207
|
+
isExternallyBilledTurn: false,
|
|
208
|
+
},
|
|
209
|
+
sandboxState: {
|
|
210
|
+
resolvedSandbox: null,
|
|
211
|
+
attemptWritersDrained: false,
|
|
212
|
+
lateSandboxesAwaitingWriterDrain: new Set(),
|
|
213
|
+
machinePrimarySession: null,
|
|
214
|
+
lazyOwnedSandbox: null,
|
|
215
|
+
firstModelPreparationNestedSandboxMs: 0,
|
|
216
|
+
firstModelPreparationNestedSandboxPhases: [],
|
|
217
|
+
turnSandboxProvisioner: null,
|
|
218
|
+
resumeManagedGroupBox: null,
|
|
219
|
+
prefetchedManagedBox: null,
|
|
220
|
+
prefetchedManagedBoxResult: null,
|
|
221
|
+
setupBoxSession: null,
|
|
222
|
+
sandboxHolderId: null,
|
|
223
|
+
sandboxGroupId: null,
|
|
224
|
+
leaseHeartbeatTimer: undefined,
|
|
225
|
+
rotationInFlight: null,
|
|
226
|
+
snapshotInFlight: null,
|
|
227
|
+
firstProviderRequestStarted: false,
|
|
228
|
+
turnEndCaptureInProgress: false,
|
|
229
|
+
startupMilestoneBackend: null,
|
|
230
|
+
},
|
|
231
|
+
renewals: {
|
|
232
|
+
gitCredentialRenewals: [],
|
|
233
|
+
gitCredentialRenewalClosed: false,
|
|
234
|
+
runCredentialRenewal: null,
|
|
235
|
+
runCredentialRenewalClosed: false,
|
|
236
|
+
runCredentialSession: null,
|
|
237
|
+
codemodeTokenRenewal: null,
|
|
238
|
+
codemodeTokenRenewalClosed: false,
|
|
239
|
+
publishedRunCredentialNotices: new Set(),
|
|
240
|
+
},
|
|
241
|
+
recordingState: {
|
|
242
|
+
activeRecording: null,
|
|
243
|
+
computerUseRecordingStart: null,
|
|
244
|
+
didComputerUse: false,
|
|
245
|
+
},
|
|
246
|
+
eventing: {
|
|
247
|
+
heartbeatTimer: undefined,
|
|
248
|
+
batcher: null,
|
|
249
|
+
preparedTools: null,
|
|
250
|
+
toolPreparationReady: null,
|
|
251
|
+
toolPreparationClosing: false,
|
|
252
|
+
codemodeDispatcher: null,
|
|
253
|
+
toolCancellationFenceRef: { current: null },
|
|
254
|
+
publish: null,
|
|
255
|
+
settle: null,
|
|
256
|
+
turnStartedPublished: false,
|
|
257
|
+
stream: undefined,
|
|
258
|
+
modelRunSettings: input.settings,
|
|
259
|
+
firstModelRequestPreparationStartedAt: null,
|
|
260
|
+
firstModelRequestPreparationRecorded: false,
|
|
261
|
+
firstModelRequestCheckpointAt: null,
|
|
262
|
+
companyBrainContextContributions: null,
|
|
263
|
+
},
|
|
264
|
+
workspaceRefs: {
|
|
265
|
+
variableSetId: "",
|
|
266
|
+
rigId: "",
|
|
267
|
+
rigVersionId: "",
|
|
268
|
+
},
|
|
269
|
+
providerTurn: {
|
|
270
|
+
effectiveCodexCredentialId: null,
|
|
271
|
+
effectiveXaiCredentialId: null,
|
|
272
|
+
xaiRotationEnabled: false,
|
|
273
|
+
xaiAuthoritySnapshot: null,
|
|
274
|
+
xaiRequestContext: null,
|
|
275
|
+
xaiCredentialQuarantined: false,
|
|
276
|
+
priorSessionCodexCredentialId: null,
|
|
277
|
+
latestCodexUsage: null,
|
|
278
|
+
lastCodexRequestOpaqueArtifacts: [],
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSessionGoal,
|
|
3
|
+
acquireXaiCredentialLease,
|
|
4
|
+
getXaiSessionAccountPin,
|
|
5
|
+
setXaiSessionAccountPin,
|
|
6
|
+
recordXaiSessionLastAccount,
|
|
7
|
+
XAI_CREDENTIAL_LEASE_TTL_MS,
|
|
8
|
+
armXaiCapacityWait,
|
|
9
|
+
} from "@opengeni/db";
|
|
10
|
+
import { publishDurableSessionEvents } from "@opengeni/events";
|
|
11
|
+
|
|
12
|
+
import type { CapacityPhaseDeps, CapacityPhaseOutcome } from "./codex-capacity";
|
|
13
|
+
|
|
14
|
+
export async function selectXaiTurnCapacity(
|
|
15
|
+
deps: CapacityPhaseDeps,
|
|
16
|
+
): Promise<CapacityPhaseOutcome> {
|
|
17
|
+
const {
|
|
18
|
+
input,
|
|
19
|
+
db,
|
|
20
|
+
bus,
|
|
21
|
+
dispatchId,
|
|
22
|
+
control,
|
|
23
|
+
billingState,
|
|
24
|
+
eventing,
|
|
25
|
+
providerTurn,
|
|
26
|
+
leases,
|
|
27
|
+
claimedResult,
|
|
28
|
+
turn,
|
|
29
|
+
} = deps;
|
|
30
|
+
|
|
31
|
+
if (billingState.isXaiTurn) {
|
|
32
|
+
const authoritySnapshot = turn.xaiProviderAccountAuthoritySnapshot;
|
|
33
|
+
providerTurn.xaiAuthoritySnapshot = authoritySnapshot;
|
|
34
|
+
const subjectId =
|
|
35
|
+
authoritySnapshot.scope === "user" ? turn.initiatingHumanSubjectId : "worker:xai-workspace";
|
|
36
|
+
if (!subjectId) {
|
|
37
|
+
throw new Error("User-scoped SuperGrok work has no frozen initiating human");
|
|
38
|
+
}
|
|
39
|
+
const sessionPin = await getXaiSessionAccountPin(db, {
|
|
40
|
+
workspaceId: input.workspaceId,
|
|
41
|
+
subjectId,
|
|
42
|
+
sessionId: input.sessionId,
|
|
43
|
+
authoritySnapshot,
|
|
44
|
+
});
|
|
45
|
+
const leaseStartedAtMs = performance.now();
|
|
46
|
+
const leased = await acquireXaiCredentialLease(db, {
|
|
47
|
+
accountId: input.accountId,
|
|
48
|
+
workspaceId: input.workspaceId,
|
|
49
|
+
subjectId,
|
|
50
|
+
sessionId: input.sessionId,
|
|
51
|
+
turnId: turn.id,
|
|
52
|
+
holderId: dispatchId,
|
|
53
|
+
authoritySnapshot,
|
|
54
|
+
pinnedCredentialId: sessionPin?.pinnedCredentialId ?? null,
|
|
55
|
+
pinSource:
|
|
56
|
+
sessionPin?.pinSource === "manual" || sessionPin?.pinSource === "policy"
|
|
57
|
+
? sessionPin.pinSource
|
|
58
|
+
: null,
|
|
59
|
+
});
|
|
60
|
+
providerTurn.effectiveXaiCredentialId = leased.credentialId;
|
|
61
|
+
providerTurn.xaiRotationEnabled = leased.rotationEnabled;
|
|
62
|
+
leases.xai.subjectId = subjectId;
|
|
63
|
+
leases.xai.holderId = leased.holderId;
|
|
64
|
+
leases.xai.generation = leased.generation;
|
|
65
|
+
leases.xai.confirmedUntilMs = leased.leasedUntil
|
|
66
|
+
? leaseStartedAtMs + XAI_CREDENTIAL_LEASE_TTL_MS
|
|
67
|
+
: null;
|
|
68
|
+
leases.xai.held =
|
|
69
|
+
providerTurn.effectiveXaiCredentialId !== null &&
|
|
70
|
+
leased.holderId !== null &&
|
|
71
|
+
leased.generation !== null &&
|
|
72
|
+
leases.xai.confirmedUntilMs !== null;
|
|
73
|
+
if (!providerTurn.effectiveXaiCredentialId) {
|
|
74
|
+
const connected = leased.accounts.length;
|
|
75
|
+
const allocatorEnabled = leased.accounts.filter((account) => account.allocatorEnabled).length;
|
|
76
|
+
if (connected === 0) {
|
|
77
|
+
throw Object.assign(
|
|
78
|
+
new Error("No SuperGrok subscription account is connected for this authority scope"),
|
|
79
|
+
{ code: "xai_not_connected" },
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (turn.source === "compaction") {
|
|
83
|
+
if (
|
|
84
|
+
!(await eventing.settle!({
|
|
85
|
+
events: [
|
|
86
|
+
{
|
|
87
|
+
type: "turn.cancelled",
|
|
88
|
+
payload: {
|
|
89
|
+
maintenance: "context_compaction",
|
|
90
|
+
reason: "xai_capacity_unavailable",
|
|
91
|
+
requestPreserved: true,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: "session.status.changed",
|
|
96
|
+
payload: { status: "idle" },
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
turnStatus: "cancelled",
|
|
100
|
+
sessionStatus: "idle",
|
|
101
|
+
activeTurnId: null,
|
|
102
|
+
}))
|
|
103
|
+
) {
|
|
104
|
+
return { exit: claimedResult({ status: "cancelled" }) };
|
|
105
|
+
}
|
|
106
|
+
control.turnMetricOutcome = "cancelled";
|
|
107
|
+
control.activityStatus = "idle";
|
|
108
|
+
return { exit: claimedResult({ status: "idle", deferredUntilWake: true }) };
|
|
109
|
+
}
|
|
110
|
+
const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(() => null);
|
|
111
|
+
const activeGoal = goal?.status === "active" ? goal : null;
|
|
112
|
+
const now = new Date();
|
|
113
|
+
const futureResets = leased.accounts
|
|
114
|
+
.map((account) => account.exhaustedUntil)
|
|
115
|
+
.filter((date): date is Date => date !== null && date > now);
|
|
116
|
+
const earliestResetAt = futureResets.length
|
|
117
|
+
? new Date(Math.min(...futureResets.map((date) => date.getTime())))
|
|
118
|
+
: null;
|
|
119
|
+
const error =
|
|
120
|
+
allocatorEnabled === 0
|
|
121
|
+
? "All connected SuperGrok subscription accounts are disabled for allocation"
|
|
122
|
+
: "All connected SuperGrok subscription accounts are temporarily unavailable";
|
|
123
|
+
const armed = await armXaiCapacityWait(db, {
|
|
124
|
+
accountId: input.accountId,
|
|
125
|
+
workspaceId: input.workspaceId,
|
|
126
|
+
subjectId,
|
|
127
|
+
sessionId: input.sessionId,
|
|
128
|
+
turnId: turn.id,
|
|
129
|
+
attemptId: input.attemptId,
|
|
130
|
+
workflowId: input.workflowId,
|
|
131
|
+
authoritySnapshot,
|
|
132
|
+
goalId: activeGoal?.id ?? null,
|
|
133
|
+
goalVersion: activeGoal?.version ?? null,
|
|
134
|
+
earliestResetAt,
|
|
135
|
+
failurePayload: {
|
|
136
|
+
error,
|
|
137
|
+
code: allocatorEnabled === 0 ? "xai_allocator_disabled" : "xai_capacity_unavailable",
|
|
138
|
+
detail: "waiting for an eligible account, reconnect, pin change, or quota reset",
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
if (armed.action === "waiting") {
|
|
142
|
+
await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, armed.events);
|
|
143
|
+
control.turnMetricOutcome = "recovering";
|
|
144
|
+
control.activityStatus = "waiting_capacity";
|
|
145
|
+
return {
|
|
146
|
+
exit: claimedResult({
|
|
147
|
+
status: "waiting_capacity",
|
|
148
|
+
capacityWait: {
|
|
149
|
+
provider: "xai",
|
|
150
|
+
waiterId: armed.waiter.id,
|
|
151
|
+
generation: armed.waiter.generation,
|
|
152
|
+
nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
|
|
153
|
+
wakeRevision: armed.waiter.wakeRevision,
|
|
154
|
+
},
|
|
155
|
+
}),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (
|
|
159
|
+
!(await eventing.settle!({
|
|
160
|
+
events: [
|
|
161
|
+
{
|
|
162
|
+
type: "turn.failed",
|
|
163
|
+
payload: {
|
|
164
|
+
error,
|
|
165
|
+
code: "xai_capacity_wait_stale",
|
|
166
|
+
retryable: false,
|
|
167
|
+
recovery: "user_message",
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
{ type: "session.status.changed", payload: { status: "idle" } },
|
|
171
|
+
],
|
|
172
|
+
turnStatus: "failed",
|
|
173
|
+
sessionStatus: "idle",
|
|
174
|
+
activeTurnId: null,
|
|
175
|
+
}))
|
|
176
|
+
) {
|
|
177
|
+
return { exit: claimedResult({ status: "cancelled" }) };
|
|
178
|
+
}
|
|
179
|
+
control.turnMetricOutcome = "failed";
|
|
180
|
+
control.activityStatus = "idle";
|
|
181
|
+
return { exit: claimedResult({ status: "idle" }) };
|
|
182
|
+
}
|
|
183
|
+
if (leases.xai.held) leases.xai.startHeartbeat();
|
|
184
|
+
if (
|
|
185
|
+
leased.rotationEnabled &&
|
|
186
|
+
sessionPin?.pinSource !== "manual" &&
|
|
187
|
+
(sessionPin?.pinnedCredentialId !== providerTurn.effectiveXaiCredentialId ||
|
|
188
|
+
sessionPin?.pinSource !== "policy")
|
|
189
|
+
) {
|
|
190
|
+
await setXaiSessionAccountPin(db, {
|
|
191
|
+
accountId: input.accountId,
|
|
192
|
+
workspaceId: input.workspaceId,
|
|
193
|
+
subjectId,
|
|
194
|
+
sessionId: input.sessionId,
|
|
195
|
+
authoritySnapshot,
|
|
196
|
+
credentialId: providerTurn.effectiveXaiCredentialId,
|
|
197
|
+
pinSource: "policy",
|
|
198
|
+
expectedVersion: sessionPin?.version ?? null,
|
|
199
|
+
}).catch((error: unknown) => {
|
|
200
|
+
if (error instanceof Error && error.message === "xAI session pin changed") return;
|
|
201
|
+
throw error;
|
|
202
|
+
});
|
|
203
|
+
} else if (!leased.rotationEnabled && sessionPin?.pinSource === "policy") {
|
|
204
|
+
await setXaiSessionAccountPin(db, {
|
|
205
|
+
accountId: input.accountId,
|
|
206
|
+
workspaceId: input.workspaceId,
|
|
207
|
+
subjectId,
|
|
208
|
+
sessionId: input.sessionId,
|
|
209
|
+
authoritySnapshot,
|
|
210
|
+
credentialId: null,
|
|
211
|
+
pinSource: null,
|
|
212
|
+
expectedVersion: sessionPin.version,
|
|
213
|
+
}).catch((error: unknown) => {
|
|
214
|
+
if (error instanceof Error && error.message === "xAI session pin changed") return;
|
|
215
|
+
throw error;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
await recordXaiSessionLastAccount(db, {
|
|
219
|
+
accountId: input.accountId,
|
|
220
|
+
workspaceId: input.workspaceId,
|
|
221
|
+
subjectId,
|
|
222
|
+
sessionId: input.sessionId,
|
|
223
|
+
authoritySnapshot,
|
|
224
|
+
credentialId: providerTurn.effectiveXaiCredentialId,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { ok: true };
|
|
229
|
+
}
|