@opengeni/worker-bundle 0.26.0-canary.0 → 0.26.0-canary.2

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.
Files changed (36) hide show
  1. package/dist/activities/agent-turn/errors.d.ts +5 -4
  2. package/dist/activities/agent-turn/history-prefix.d.ts +8 -0
  3. package/dist/activities/agent-turn/history-sink.d.ts +6 -2
  4. package/dist/activities/agent-turn/quiescence.d.ts +4 -2
  5. package/dist/activities/sandbox-lease.d.ts +7 -2
  6. package/dist/activities/xai-quota.d.ts +3 -0
  7. package/dist/{activities-control-CTEMLTHG.js → activities-control-3SKLDTXQ.js} +205 -86
  8. package/dist/activities-control-3SKLDTXQ.js.map +1 -0
  9. package/dist/{activities-turn-S5IU347A.js → activities-turn-GHSFZB64.js} +176 -1077
  10. package/dist/activities-turn-GHSFZB64.js.map +1 -0
  11. package/dist/{chunk-TAUE3GYT.js → chunk-O2LQWJIK.js} +3115 -1948
  12. package/dist/chunk-O2LQWJIK.js.map +1 -0
  13. package/dist/index.js +1 -1
  14. package/dist/sandbox-routing.d.ts +6 -0
  15. package/dist/workflow-bundle.js +24 -2
  16. package/package.json +17 -17
  17. package/src/activities/agent-turn/agent-build.ts +1 -0
  18. package/src/activities/agent-turn/errors.ts +44 -11
  19. package/src/activities/agent-turn/failure-settlement.ts +21 -8
  20. package/src/activities/agent-turn/history-prefix.ts +51 -0
  21. package/src/activities/agent-turn/history-sink.ts +30 -6
  22. package/src/activities/agent-turn/quiescence.ts +8 -2
  23. package/src/activities/agent-turn/run.ts +4 -0
  24. package/src/activities/agent-turn/stream-attempt.ts +20 -7
  25. package/src/activities/agent-turn/tool-environment.ts +12 -0
  26. package/src/activities/agent-turn/xai-capacity.ts +11 -0
  27. package/src/activities/codex-capacity.ts +18 -0
  28. package/src/activities/run-input.ts +6 -7
  29. package/src/activities/sandbox-lease.ts +229 -87
  30. package/src/activities/session-state.ts +8 -7
  31. package/src/activities/xai-quota.ts +35 -0
  32. package/src/sandbox-routing.ts +115 -0
  33. package/src/workflows/session.ts +29 -1
  34. package/dist/activities-control-CTEMLTHG.js.map +0 -1
  35. package/dist/activities-turn-S5IU347A.js.map +0 -1
  36. package/dist/chunk-TAUE3GYT.js.map +0 -1
@@ -10,6 +10,7 @@ import {
10
10
  assertSessionAttemptQuiescenceRecoveryDurable,
11
11
  assertSuccessfulAgentStreamCompletion,
12
12
  authoritativeCodexCapacityResetAt,
13
+ buildXaiTurnRequestAuthorization,
13
14
  classifyCodexCredentialFailure,
14
15
  classifyCodexPin,
15
16
  classifyContextWindowOverflowError,
@@ -30,11 +31,13 @@ import {
30
31
  deliverFailedChildTurnToParent,
31
32
  drainAttemptOwnedSandboxWriters,
32
33
  escapedMcpTimeoutRecoveryFailure,
34
+ establishSelfhostedTurnSession,
33
35
  finalizeDurableTurnOpStreams,
34
36
  isCodexCredentialEligible,
35
37
  isCompactionSummaryFailure,
36
38
  isLazySandboxProvisionRetryable,
37
39
  isWorkerShutdownCancellation,
40
+ lazyProvisionEnabled,
38
41
  maybePersistWarmWorkspaceSnapshot,
39
42
  mergeResourceRefs,
40
43
  nextStreamEvent,
@@ -47,12 +50,14 @@ import {
47
50
  providerRecoveryResult,
48
51
  providerRetryAfterMs,
49
52
  refreshCodexUsageAndRepairCapacityWaiters,
53
+ refreshExhaustedXaiQuota,
50
54
  releaseTurnSandboxAfterWriterDrain,
51
55
  requireAgentStreamFinalOutput,
52
56
  resolveWorkspaceInstalledSkillRuntime,
53
57
  resolveWorkspacePackRuntime,
54
58
  resumeBoxForTurn,
55
59
  rigProviderImageSourceImage,
60
+ routingEnabled,
56
61
  runMandatoryHistoryPersistenceStep,
57
62
  safeErrorDiagnostic,
58
63
  safeErrorForTelemetry,
@@ -76,8 +81,10 @@ import {
76
81
  waitForTurnFinalizerStep,
77
82
  waitForTurnOperation,
78
83
  waitForWarmSnapshot,
79
- withFirstPartyTools
80
- } from "./chunk-TAUE3GYT.js";
84
+ withFirstPartyTools,
85
+ wrapLazyTurnBoxWithRouting,
86
+ wrapTurnBoxWithRouting
87
+ } from "./chunk-O2LQWJIK.js";
81
88
  import {
82
89
  StreamTimingMetrics,
83
90
  makeMachineOpObserver,
@@ -137,100 +144,6 @@ import {
137
144
  xaiSubscriptionRequestStorage
138
145
  } from "@opengeni/xai-subscription";
139
146
 
140
- // src/activities/xai-auth.ts
141
- import { environmentsEncryptionKeyBytes } from "@opengeni/config";
142
- import {
143
- materializeXaiCredentialForRun,
144
- refreshXaiSubscriptionCredentialSerialized
145
- } from "@opengeni/db";
146
- import {
147
- refreshXaiToken,
148
- XAI_CLIENT_VERSION,
149
- XAI_SUBSCRIPTION_MODEL_ID_PREFIX,
150
- XaiSubscriptionReloginRequired,
151
- xaiAccessTokenExpiry
152
- } from "@opengeni/xai-subscription";
153
- async function buildXaiTurnRequestAuthorization(input) {
154
- const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
155
- if (!encryptionKey) {
156
- throw new Error("SuperGrok subscriptions require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
157
- }
158
- let credential = await materializeXaiCredentialForRun(input.db, {
159
- workspaceId: input.workspaceId,
160
- subjectId: input.subjectId,
161
- credentialId: input.credentialId,
162
- authoritySnapshot: input.authoritySnapshot,
163
- encryptionKey
164
- });
165
- const tokenSnapshot = () => {
166
- const accessToken = credential.secret.accessToken;
167
- const userId = credential.providerAccountId;
168
- if (!accessToken || !userId) {
169
- throw new XaiSubscriptionReloginRequired(
170
- "The SuperGrok connection is missing a verified token identity. Reconnect the account."
171
- );
172
- }
173
- return { accessToken, userId };
174
- };
175
- const refresh = async () => {
176
- const observedAccessToken = credential.secret.accessToken;
177
- const observedRefreshToken = credential.secret.refreshToken;
178
- if (!observedRefreshToken) {
179
- throw new XaiSubscriptionReloginRequired(
180
- "The SuperGrok connection cannot be refreshed. Reconnect the account."
181
- );
182
- }
183
- const result = await refreshXaiSubscriptionCredentialSerialized(input.db, {
184
- accountId: input.accountId,
185
- workspaceId: input.workspaceId,
186
- subjectId: input.subjectId,
187
- credentialId: input.credentialId,
188
- authoritySnapshot: input.authoritySnapshot,
189
- encryptionKey,
190
- observedAccessToken,
191
- observedRefreshToken,
192
- refresh: async (current) => {
193
- const refreshToken = current.secret.refreshToken;
194
- if (!refreshToken) {
195
- throw new XaiSubscriptionReloginRequired(
196
- "The SuperGrok connection cannot be refreshed. Reconnect the account."
197
- );
198
- }
199
- const tokens = await refreshXaiToken(refreshToken, {
200
- ...input.fetch ? { fetch: input.fetch } : {}
201
- });
202
- return {
203
- secret: {
204
- version: 1,
205
- accessToken: tokens.accessToken,
206
- refreshToken: tokens.refreshToken
207
- },
208
- expiresAt: xaiAccessTokenExpiry(tokens.accessToken) ?? new Date(Date.now() + tokens.expiresInSeconds * 1e3)
209
- };
210
- }
211
- });
212
- credential = result.credential;
213
- return tokenSnapshot();
214
- };
215
- return {
216
- credentialId: input.credentialId,
217
- context: {
218
- clientVersion: XAI_CLIENT_VERSION,
219
- sessionId: input.sessionId,
220
- turnId: input.turnId,
221
- getToken: async () => tokenSnapshot(),
222
- refresh,
223
- resolveModel: (model) => model.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX) ? model.slice(XAI_SUBSCRIPTION_MODEL_ID_PREFIX.length) : model,
224
- ...input.hostedSearch ? { hostedSearch: input.hostedSearch } : {},
225
- ...input.onFinalContextUsage ? { onFinalContextUsage: input.onFinalContextUsage } : {},
226
- ...input.nextRequestId ? { nextRequestId: input.nextRequestId } : {},
227
- ...input.streamIdleTimeoutMs ? { streamIdleTimeoutMs: input.streamIdleTimeoutMs } : {},
228
- ...input.onModelRequestDiagnostic ? { onModelRequestDiagnostic: input.onModelRequestDiagnostic } : {},
229
- ...input.onModelRequestEvent ? { onModelRequestEvent: input.onModelRequestEvent } : {}
230
- }
231
- };
232
- }
233
-
234
147
  // src/activities/turn-attempt-fenced.ts
235
148
  var TurnAttemptFencedError = class extends Error {
236
149
  constructor(message) {
@@ -3292,11 +3205,63 @@ function completedToolCallFromSdkEvent(event) {
3292
3205
  };
3293
3206
  }
3294
3207
 
3208
+ // src/activities/agent-turn/history-prefix.ts
3209
+ import { createHash as createHash5 } from "crypto";
3210
+ import { normalizeProtocolJsonValue as normalizeProtocolJsonValue2, sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2 } from "@opengeni/runtime";
3211
+ var HistoryPrefixGuard = class {
3212
+ persisted = [];
3213
+ seed(items, count, truncationTokens) {
3214
+ const keys = this.keys(items, truncationTokens);
3215
+ if (keys.length < count)
3216
+ throw new Error("Conversation history seed is shorter than its durable prefix");
3217
+ this.persisted = keys.slice(0, count);
3218
+ }
3219
+ verify(items, truncationTokens) {
3220
+ const keys = this.keys(items, truncationTokens);
3221
+ for (let i = 0; i < this.persisted.length; i++) {
3222
+ if (keys[i] !== this.persisted[i]) {
3223
+ throw new Error(
3224
+ `Conversation history durable prefix changed at item ${i}; refusing to skip unsaved work`
3225
+ );
3226
+ }
3227
+ }
3228
+ return keys;
3229
+ }
3230
+ acknowledge(keys, count) {
3231
+ this.persisted = keys.slice(0, count);
3232
+ }
3233
+ keys(items, truncationTokens) {
3234
+ return sanitizeHistoryItemsForModel2(items, truncationTokens).map(
3235
+ (item) => createHash5("sha256").update(JSON.stringify(sorted(normalizeProtocolJsonValue2(item)))).digest("hex")
3236
+ );
3237
+ }
3238
+ };
3239
+ function sorted(value) {
3240
+ if (Array.isArray(value)) return value.map(sorted);
3241
+ if (value !== null && typeof value === "object") {
3242
+ return Object.fromEntries(
3243
+ Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([key, entry]) => [key, sorted(entry)])
3244
+ );
3245
+ }
3246
+ return value;
3247
+ }
3248
+
3295
3249
  // src/activities/agent-turn/history-sink.ts
3296
3250
  var TurnHistorySink = class {
3297
3251
  constructor(deps) {
3298
3252
  this.deps = deps;
3299
3253
  }
3254
+ prefix = new HistoryPrefixGuard();
3255
+ seedHistory(input, count) {
3256
+ const items = typeof input === "string" && count === 0 ? [] : Array.isArray(input) ? input : input?.history;
3257
+ if (!Array.isArray(items)) throw new Error("Conversation history seed is unavailable");
3258
+ this.prefix.seed(
3259
+ items,
3260
+ count,
3261
+ this.deps.getModelRunSettings().modelToolOutputTruncationTokens
3262
+ );
3263
+ this.persistedHistoryCount = count;
3264
+ }
3300
3265
  persistedHistoryCount = 0;
3301
3266
  nextHistoryPosition = 0;
3302
3267
  providerArtifactCandidates = {
@@ -3315,6 +3280,10 @@ var TurnHistorySink = class {
3315
3280
  const rawHistory = stream.state.history;
3316
3281
  if (Array.isArray(rawHistory)) {
3317
3282
  const typedHistory = rawHistory;
3283
+ const verifiedKeys = this.prefix.verify(
3284
+ typedHistory,
3285
+ this.deps.getModelRunSettings().modelToolOutputTruncationTokens
3286
+ );
3318
3287
  await media.retainNativeGeneratedImagesFromHistory(typedHistory);
3319
3288
  const durableHistory = compactGeneratedImageHistory(
3320
3289
  compactRetainedScreenshotHistory(typedHistory, media.retainedScreenshotReceiptsByCallId),
@@ -3350,9 +3319,14 @@ var TurnHistorySink = class {
3350
3319
  });
3351
3320
  }
3352
3321
  if (shouldAppendRows || !options.skipInputOnlyRows) {
3322
+ this.prefix.acknowledge(verifiedKeys, nextWatermark);
3353
3323
  this.persistedHistoryCount = nextWatermark;
3354
3324
  this.nextHistoryPosition = nextPosition;
3355
3325
  }
3326
+ } else if (options.requireDurable) {
3327
+ throw new Error(
3328
+ "SDK conversation history is unavailable at a mandatory persistence boundary"
3329
+ );
3356
3330
  }
3357
3331
  const envelope = sandboxStateEntryFromRunState(stream.state);
3358
3332
  if (envelope) {
@@ -3367,10 +3341,7 @@ var TurnHistorySink = class {
3367
3341
  );
3368
3342
  }
3369
3343
  } catch (persistError) {
3370
- console.error(
3371
- "session history dual-write failed (run unaffected)",
3372
- safeErrorDiagnostic(persistError)
3373
- );
3344
+ console.error("session history persistence failed", safeErrorDiagnostic(persistError));
3374
3345
  if (options.requireDurable) throw persistError;
3375
3346
  }
3376
3347
  };
@@ -3580,7 +3551,7 @@ import { clearRunCredentialsForAttempt } from "@opengeni/runtime";
3580
3551
  import { fetchXaiSubscriptionQuota } from "@opengeni/xai-subscription";
3581
3552
 
3582
3553
  // src/activities/workspace-capture.ts
3583
- import { createHash as createHash5 } from "crypto";
3554
+ import { createHash as createHash6 } from "crypto";
3584
3555
  import {
3585
3556
  deleteWorkspaceCaptureRows,
3586
3557
  insertFailedWorkspaceCapture,
@@ -4334,7 +4305,7 @@ function joinRepoPath(repoRoot, repoRelPath) {
4334
4305
  return `${repoRoot.replace(/\/+$/, "")}/${repoRelPath}`;
4335
4306
  }
4336
4307
  function sha256(bytes) {
4337
- return createHash5("sha256").update(bytes).digest("hex");
4308
+ return createHash6("sha256").update(bytes).digest("hex");
4338
4309
  }
4339
4310
  function utf8(s) {
4340
4311
  return new TextEncoder().encode(s);
@@ -4965,7 +4936,7 @@ import {
4965
4936
  } from "@opengeni/config";
4966
4937
  import { selectCodexCredentialId } from "@opengeni/codex";
4967
4938
  import { directPersonalConnectionSubjectId } from "@opengeni/core";
4968
- import { createHash as createHash6 } from "crypto";
4939
+ import { createHash as createHash7 } from "crypto";
4969
4940
 
4970
4941
  // src/activities/agent-turn/model-usage.ts
4971
4942
  import {
@@ -5569,7 +5540,7 @@ function legacyTurnExecutionPolicyInput(turn) {
5569
5540
  };
5570
5541
  }
5571
5542
  function stableHumanInputRequestId(sessionId, turnId, toolCallId) {
5572
- const hex = createHash6("sha256").update("opengeni-human-input-v1\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
5543
+ const hex = createHash7("sha256").update("opengeni-human-input-v1\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
5573
5544
  hex[12] = "5";
5574
5545
  hex[16] = ["8", "9", "a", "b"][Number.parseInt(hex[16] ?? "0", 16) % 4] ?? "8";
5575
5546
  const value = hex.join("");
@@ -5592,7 +5563,7 @@ function stableInteractionInterventionOperationId(sessionId, turnId, toolCallId)
5592
5563
  );
5593
5564
  }
5594
5565
  function stableInteractionInterventionUuid(namespace, sessionId, turnId, toolCallId) {
5595
- const hex = createHash6("sha256").update(namespace).update("\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
5566
+ const hex = createHash7("sha256").update(namespace).update("\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
5596
5567
  hex[12] = "5";
5597
5568
  hex[16] = ["8", "9", "a", "b"][Number.parseInt(hex[16] ?? "0", 16) % 4] ?? "8";
5598
5569
  const value = hex.join("");
@@ -5741,6 +5712,12 @@ async function settleTurnFailure(deps) {
5741
5712
  const rotationPending = fencedLease?.rotationRequestedAt != null || lifecycleTransition?.reason === "rotation_in_progress";
5742
5713
  const transitionPending = lifecycleTransition !== null || rotationPending;
5743
5714
  const deadlineRotationPending = rotationPending && fencedLease?.rotationReason === "provider_deadline";
5715
+ const sandboxLifecycleWait = rotationPending ? {
5716
+ version: 1,
5717
+ sandboxGroupId: leaseControlError.sandboxGroupId,
5718
+ leaseEpoch: fencedLease?.leaseEpoch ?? leaseControlError.leaseEpoch,
5719
+ reason: "rotation_in_progress"
5720
+ } : void 0;
5744
5721
  const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
5745
5722
  sessionId: input.sessionId,
5746
5723
  turnId: recoveryTurnId,
@@ -5756,7 +5733,8 @@ async function settleTurnFailure(deps) {
5756
5733
  } : {},
5757
5734
  ...lifecycleTransition ? { transitionReason: lifecycleTransition.reason } : {}
5758
5735
  }
5759
- } : {}
5736
+ } : {},
5737
+ ...sandboxLifecycleWait ? { sandboxLifecycleWait } : {}
5760
5738
  });
5761
5739
  if (recovery.action === "stale") {
5762
5740
  acknowledgeLostAttemptOwnership();
@@ -5770,7 +5748,7 @@ async function settleTurnFailure(deps) {
5770
5748
  control.turnMetricOutcome = "recovering";
5771
5749
  return claimedResult({
5772
5750
  status: "recovering",
5773
- ...transitionPending ? {
5751
+ ...transitionPending && !sandboxLifecycleWait ? {
5774
5752
  continueDelayMs: sandboxDeadlineRotationRecoveryDelayMs(settings)
5775
5753
  } : {}
5776
5754
  });
@@ -5829,6 +5807,12 @@ async function settleTurnFailure(deps) {
5829
5807
  detail: {
5830
5808
  sandboxGroupId: rotation.sandboxGroupId,
5831
5809
  leaseEpoch: rotation.leaseEpoch
5810
+ },
5811
+ sandboxLifecycleWait: {
5812
+ version: 1,
5813
+ sandboxGroupId: rotation.sandboxGroupId,
5814
+ leaseEpoch: rotation.leaseEpoch,
5815
+ reason: "rotation_in_progress"
5832
5816
  }
5833
5817
  });
5834
5818
  if (recovery.action === "stale") {
@@ -5841,10 +5825,7 @@ async function settleTurnFailure(deps) {
5841
5825
  await publishDurableSessionEvents2(bus, input.workspaceId, input.sessionId, recovery.events);
5842
5826
  control.activityStatus = "recovering";
5843
5827
  control.turnMetricOutcome = "recovering";
5844
- return claimedResult({
5845
- status: "recovering",
5846
- continueDelayMs: sandboxDeadlineRotationRecoveryDelayMs(settings)
5847
- });
5828
+ return claimedResult({ status: "recovering" });
5848
5829
  } catch (recoveryError) {
5849
5830
  console.error(
5850
5831
  "sandbox deadline rotation recovery failed",
@@ -6844,7 +6825,7 @@ import {
6844
6825
  latestCompactionReplacementFingerprint,
6845
6826
  prepareCompactionPromptInput,
6846
6827
  projectRemoteCompactionOverflowRetryInput,
6847
- sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
6828
+ sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel3,
6848
6829
  summarizeForCompaction
6849
6830
  } from "@opengeni/runtime";
6850
6831
  import { contextInputBudgetTokens } from "@opengeni/config";
@@ -6855,7 +6836,7 @@ import {
6855
6836
  FileResourceRef,
6856
6837
  resourceMountPath as resourceMountPath2
6857
6838
  } from "@opengeni/contracts";
6858
- import { createHash as createHash7 } from "crypto";
6839
+ import { createHash as createHash8 } from "crypto";
6859
6840
  import {
6860
6841
  getActiveSessionHistoryItemsPaged as getActiveSessionHistoryItemsPaged2,
6861
6842
  getFilesForSubject as getFilesForSubject2,
@@ -6964,7 +6945,7 @@ async function modelAttachmentContentForFiles(files, readFileBytes) {
6964
6945
  });
6965
6946
  continue;
6966
6947
  }
6967
- if (createHash7("sha256").update(bytes).digest("hex") !== checksum) {
6948
+ if (createHash8("sha256").update(bytes).digest("hex") !== checksum) {
6968
6949
  console.error("model attachment checksum did not match finalized metadata", {
6969
6950
  fileId: file.id
6970
6951
  });
@@ -7093,13 +7074,8 @@ async function turnInput(db, runtime, agent, trigger, options) {
7093
7074
  options.turnId
7094
7075
  )
7095
7076
  );
7096
- if (updates.length > 0) {
7097
- const historyItemIds = new Set(
7098
- updates.map((update) => update.deliveredHistoryItemId).filter(Boolean)
7099
- );
7100
- if (historyItemIds.size !== 1 || updates.some((update) => !update.deliveredHistoryItemId)) {
7101
- throw new Error("Delivered internal updates have no single durable model-memory batch");
7102
- }
7077
+ if (updates.some((update) => !update.deliveredHistoryItemId)) {
7078
+ throw new Error("Delivered internal update has no durable model-memory batch");
7103
7079
  }
7104
7080
  const internalContext = joinInternalContext(
7105
7081
  options.recovering ? [
@@ -7363,7 +7339,7 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
7363
7339
  const canonicalItems = projectRejectedProviderArtifacts(active);
7364
7340
  const projectForWire = async (input) => {
7365
7341
  const materialized = options.materializeHistory ? await options.materializeHistory(input) : input;
7366
- return sanitizeHistoryItemsForModel2(
7342
+ return sanitizeHistoryItemsForModel3(
7367
7343
  options.projectModelInput ? await options.projectModelInput(materialized) : materialized,
7368
7344
  settings.modelToolOutputTruncationTokens
7369
7345
  );
@@ -7649,7 +7625,7 @@ import {
7649
7625
  import {
7650
7626
  functionCallResultItem,
7651
7627
  invokePreparedAgentTool,
7652
- sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel3
7628
+ sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel4
7653
7629
  } from "@opengeni/runtime";
7654
7630
  function argumentsJsonFromCallItem(callItem) {
7655
7631
  const raw = callItem.arguments;
@@ -7679,7 +7655,7 @@ function remainingRunStatePendingApprovalsFromSuffix(rows) {
7679
7655
  return remainingSuffixApprovals(rows, /* @__PURE__ */ new Set(["approval", "interaction_intervention"]));
7680
7656
  }
7681
7657
  function openSuffixHistoryItems(row, resultItem) {
7682
- return sanitizeHistoryItemsForModel3([
7658
+ return sanitizeHistoryItemsForModel4([
7683
7659
  ...row.tiedReasoningItems,
7684
7660
  row.callItem,
7685
7661
  resultItem
@@ -8387,7 +8363,7 @@ async function runTurnStreamAttempt(deps) {
8387
8363
  });
8388
8364
  runInput = prepared.input;
8389
8365
  historySink.providerArtifactCandidates = prepared.providerArtifactCandidates;
8390
- historySink.persistedHistoryCount = prepared.persistedHistoryCount;
8366
+ historySink.seedHistory(prepared.input.input, prepared.persistedHistoryCount);
8391
8367
  preparedHistoryCount = prepared.persistedHistoryCount;
8392
8368
  const historyPositionStartedAt = performance.now();
8393
8369
  let historyPositionOutcome = "completed";
@@ -8788,6 +8764,7 @@ async function runTurnStreamAttempt(deps) {
8788
8764
  ]);
8789
8765
  };
8790
8766
  const iterator = eventing.stream.toStream()[Symbol.asyncIterator]();
8767
+ const closeStreamWaitAdmission = eventing.preparedTools?.inputWaitYield?.captureStreamClose();
8791
8768
  let streamDone = false;
8792
8769
  try {
8793
8770
  while (true) {
@@ -9088,6 +9065,7 @@ async function runTurnStreamAttempt(deps) {
9088
9065
  }
9089
9066
  }
9090
9067
  } catch (error) {
9068
+ closeStreamWaitAdmission?.();
9091
9069
  if (fallbackProviderRequestLifecycleStartedAt !== null) {
9092
9070
  const durationMs = Math.max(
9093
9071
  0,
@@ -9146,7 +9124,8 @@ async function runTurnStreamAttempt(deps) {
9146
9124
  temporalCancellationSignal: cancellationSignal,
9147
9125
  runtimeCancellationSignal
9148
9126
  });
9149
- if (options.requireTerminalModelResponse && eventing.stream.interruptions.length === 0 && modelResponseState.responseCount === responseCountBeforeStream) {
9127
+ await eventing.preparedTools?.inputWaitYield?.sealForSettlement(runtimeCancellationSignal);
9128
+ if (options.requireTerminalModelResponse && !eventing.preparedTools?.inputWaitYield?.yielded && eventing.stream.interruptions.length === 0 && modelResponseState.responseCount === responseCountBeforeStream) {
9150
9129
  throwIfWorkerShuttingDown();
9151
9130
  throwIfTurnCancelled();
9152
9131
  throw new PostCompactionContinuationEmptyError();
@@ -9370,16 +9349,16 @@ async function runTurnStreamAttempt(deps) {
9370
9349
  control.activityStatus = "requires_action";
9371
9350
  return claimedResult({ status: "requires_action" });
9372
9351
  }
9373
- const finalOutput = String(requireAgentStreamFinalOutput(eventing.stream.finalOutput));
9352
+ const inputWaitYielded = eventing.preparedTools?.inputWaitYield?.yielded === true;
9353
+ const finalOutput = String(
9354
+ requireAgentStreamFinalOutput(eventing.stream.finalOutput, inputWaitYielded)
9355
+ );
9374
9356
  await historySink.reconcileConversationTruth({ requireDurable: true });
9375
9357
  await finalizeTurnOpStreamOps();
9376
9358
  await finishParallelSessionTitle();
9377
9359
  if (!await eventing.settle({
9378
9360
  events: [
9379
- {
9380
- type: "agent.message.completed",
9381
- payload: { text: finalOutput }
9382
- },
9361
+ ...inputWaitYielded ? [] : [{ type: "agent.message.completed", payload: { text: finalOutput } }],
9383
9362
  { type: "turn.completed", payload: { output: finalOutput } },
9384
9363
  { type: "session.status.changed", payload: { status: "idle" } }
9385
9364
  ],
@@ -9612,7 +9591,7 @@ import {
9612
9591
 
9613
9592
  // src/activities/capabilities.ts
9614
9593
  import {
9615
- environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes2,
9594
+ environmentsEncryptionKeyBytes,
9616
9595
  WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
9617
9596
  WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
9618
9597
  withCodexCatalogProvider,
@@ -9644,7 +9623,7 @@ import {
9644
9623
  loadOrganizationModelProviderApiKey
9645
9624
  } from "@opengeni/db";
9646
9625
  async function settingsWithSessionMcpServersForRun(db, workspaceId, sessionId, attemptId, settings, options) {
9647
- const encryptionKey = environmentsEncryptionKeyBytes2(settings);
9626
+ const encryptionKey = environmentsEncryptionKeyBytes(settings);
9648
9627
  if (!encryptionKey) {
9649
9628
  const metadata = await listSessionMcpServerMetadata(db, workspaceId, sessionId);
9650
9629
  if (metadata.length === 0) {
@@ -11030,7 +11009,7 @@ import {
11030
11009
  prepareImageGenerationOperation,
11031
11010
  resetImageGenerationOperationBeforeProviderDispatch
11032
11011
  } from "@opengeni/db";
11033
- import { createHash as createHash8 } from "crypto";
11012
+ import { createHash as createHash9 } from "crypto";
11034
11013
  var ImageGenerationOutcomeUnknownError = class extends Error {
11035
11014
  constructor(operationId) {
11036
11015
  super(
@@ -11243,7 +11222,7 @@ async function recoverCompletedImage(input, identity) {
11243
11222
  return retainedGeneratedImageFromArtifact(recovered).receipt;
11244
11223
  }
11245
11224
  function digest(prefix, ...values) {
11246
- const hash = createHash8("sha256").update(prefix);
11225
+ const hash = createHash9("sha256").update(prefix);
11247
11226
  for (const value of values) hash.update(value).update("\0");
11248
11227
  return hash.digest("hex");
11249
11228
  }
@@ -12518,9 +12497,9 @@ function createSandboxTurnRuntime(deps) {
12518
12497
  // src/activities/agent-turn/sandbox-establish.ts
12519
12498
  import {
12520
12499
  getEnrollment,
12521
- getSandbox as getSandbox2,
12522
- readActiveSandbox as readActiveSandbox3,
12523
- getLiveEnrollmentConnection as getLiveEnrollmentConnection2,
12500
+ getSandbox,
12501
+ readActiveSandbox as readActiveSandbox2,
12502
+ getLiveEnrollmentConnection,
12524
12503
  assertPersonalMachineForAttempt
12525
12504
  } from "@opengeni/db";
12526
12505
  import {
@@ -12529,911 +12508,13 @@ import {
12529
12508
  markModelPreparationFirstSandboxOperation,
12530
12509
  recordModelPreparationMeasurement,
12531
12510
  sdkBackendIdForSandboxBackend,
12532
- resolveConnectedMachineWorkspaceRoot as resolveConnectedMachineWorkspaceRoot2
12511
+ resolveConnectedMachineWorkspaceRoot
12533
12512
  } from "@opengeni/runtime";
12534
12513
  import { managedSessionGroupBackend, managedSessionGroupOs } from "@opengeni/core";
12535
12514
 
12536
- // src/sandbox-routing.ts
12537
- import { sandboxLifecycleTransitionWaitMs as sandboxLifecycleTransitionWaitMs2 } from "@opengeni/config";
12538
- import {
12539
- adoptConnectedMachineSessionBackgroundCommand,
12540
- adoptManagedSessionBackgroundCommand,
12541
- advanceWorkspaceGenerationForRetainedProcess,
12542
- advanceWorkspaceGeneration as advanceWorkspaceGeneration2,
12543
- getRetainedProcess,
12544
- retainWorkspaceMutationProcess,
12545
- retainedProcessSettlementIdentity,
12546
- settleConnectedMachineSessionBackgroundCommand,
12547
- settleRetainedProcess,
12548
- verifyRetainedProcessMutationSettlement,
12549
- verifyWorkspaceMutationSettlement as verifyWorkspaceMutationSettlement2,
12550
- getSandbox,
12551
- getLiveEnrollmentConnection,
12552
- markWarmLeaseInstanceLost,
12553
- readLease as readLease3,
12554
- readActiveSandbox,
12555
- resolvePersonalMachineConnectionForAttempt,
12556
- SandboxRetainedProcessPromotionFencedError
12557
- } from "@opengeni/db";
12558
- import {
12559
- buildSelfhostedBackendSession,
12560
- ActiveBackendUnresolvableError,
12561
- establishSandboxSessionFromEnvelope,
12562
- isProviderSandboxGoneDuringRoutedOperation,
12563
- isProviderSandboxNotFoundError,
12564
- makeActiveBackendResolver,
12565
- NatsControlRpc,
12566
- NatsOpStreamTransport,
12567
- RoutingBackendRecoveryRequiredError,
12568
- RoutingSandboxSession,
12569
- resolveModalCheckpointProviderBindingForSession,
12570
- resolveConnectedMachineWorkspaceRoot,
12571
- sandboxProviderInstanceIdFromEnvelope,
12572
- sandboxBackendForSdkBackendId,
12573
- selectBackend,
12574
- verifySandboxExecReadiness
12575
- } from "@opengeni/runtime";
12576
- function relayConfigFromSettings(settings) {
12577
- const raw = settings.selfhostedRelayUrl?.trim();
12578
- if (!raw) {
12579
- return { host: "relay.opengeni.local", port: 443, tls: true };
12580
- }
12581
- try {
12582
- const url = new URL(raw.includes("://") ? raw : `wss://${raw}`);
12583
- const tls = url.protocol === "wss:" || url.protocol === "https:";
12584
- const port = url.port ? Number(url.port) : tls ? 443 : 80;
12585
- return { host: url.hostname, port, tls };
12586
- } catch {
12587
- return { host: raw, port: 443, tls: true };
12588
- }
12589
- }
12590
- function selfhostedTimeoutsFromSettings(settings) {
12591
- return {
12592
- timeoutMs: settings.sandboxSelfhostedControlTimeoutMs,
12593
- execTimeoutMs: settings.sandboxSelfhostedExecTimeoutMs
12594
- };
12595
- }
12596
- function selfhostedResolverTimeouts(settings) {
12597
- const { timeoutMs, execTimeoutMs } = selfhostedTimeoutsFromSettings(settings);
12598
- return {
12599
- selfhostedTimeoutMs: timeoutMs,
12600
- selfhostedExecTimeoutMs: execTimeoutMs
12601
- };
12602
- }
12603
- function homeRouteRecoveryDisposition(lease) {
12604
- if (!lease) return "unrecoverable";
12605
- if (lease.liveness === "warming") return "pending";
12606
- if (lease.liveness === "draining") return "superseded";
12607
- if (lease.recovery.restore.status === "pending" || lease.recovery.restore.status === "restoring" || lease.recovery.restore.status === "verifying") {
12608
- return "pending";
12609
- }
12610
- if (lease.recovery.restore.status === "degraded" || lease.recovery.workspace.status === "degraded" || lease.recovery.archive.status === "unverified" || lease.recovery.archive.status === "invalid") {
12611
- return "degraded";
12612
- }
12613
- return "unrecoverable";
12614
- }
12615
- function homeRouteRecoveryError(lease, fallbackEpoch) {
12616
- return new RoutingBackendRecoveryRequiredError(
12617
- "resolve_home_backend",
12618
- lease?.leaseEpoch ?? fallbackEpoch,
12619
- homeRouteRecoveryDisposition(lease)
12620
- );
12621
- }
12622
- function providerIdentityFromResumeState(resumeState) {
12623
- return sandboxProviderInstanceIdFromEnvelope(resumeState);
12624
- }
12625
- async function resolveCurrentHomeBackend(services, ids, established) {
12626
- const lease = await readLease3(services.db, ids.workspaceId, ids.sandboxGroupId);
12627
- const fallbackEpoch = lease?.leaseEpoch ?? 0;
12628
- if (!lease || lease.liveness !== "warm" || lease.recovery.provider.status !== "exists" || lease.instanceId === null || lease.recovery.provider.instanceId !== lease.instanceId || lease.recovery.workspace.status !== "ready" || lease.recovery.restore.status !== "not_required" && lease.recovery.restore.status !== "ready") {
12629
- throw homeRouteRecoveryError(lease, fallbackEpoch);
12630
- }
12631
- if (lease.instanceId === established.instanceId) {
12632
- services.onHomeSandboxRebound?.({
12633
- established,
12634
- leaseEpoch: lease.leaseEpoch
12635
- });
12636
- return {
12637
- session: established.session,
12638
- sandboxId: null,
12639
- kind: established.backendId,
12640
- leaseEpoch: lease.leaseEpoch,
12641
- providerInstanceId: lease.instanceId
12642
- };
12643
- }
12644
- const resumeState = lease.resumeState;
12645
- const durableResumeBackend = lease.resumeBackendId ?? lease.backend ?? ids.backend;
12646
- const resumeBackend = sandboxBackendForSdkBackendId(durableResumeBackend) ?? durableResumeBackend;
12647
- if (!resumeState || resumeBackend !== ids.backend || providerIdentityFromResumeState(resumeState) !== lease.instanceId) {
12648
- throw homeRouteRecoveryError(lease, lease.leaseEpoch);
12649
- }
12650
- let rebound;
12651
- try {
12652
- rebound = await establishSandboxSessionFromEnvelope(services.settings, resumeState, {
12653
- sessionId: ids.sessionId,
12654
- recovery: "resume-only",
12655
- backendOverride: resumeBackend
12656
- });
12657
- await verifySandboxExecReadiness(rebound);
12658
- } catch (error) {
12659
- if (!isProviderSandboxNotFoundError(resumeBackend, error)) {
12660
- throw new RoutingBackendRecoveryRequiredError(
12661
- "resolve_home_backend",
12662
- lease.leaseEpoch,
12663
- "pending"
12664
- );
12665
- }
12666
- const marked = await markWarmLeaseInstanceLost(services.db, {
12667
- accountId: ids.accountId,
12668
- workspaceId: ids.workspaceId,
12669
- sandboxGroupId: ids.sandboxGroupId,
12670
- expectedEpoch: lease.leaseEpoch,
12671
- expectedInstanceId: lease.instanceId,
12672
- diagnostic: "provider_not_found_during_home_route_rebind"
12673
- });
12674
- if (marked.status === "marked") {
12675
- await services.onHomeSandboxLost?.({
12676
- sandboxGroupId: ids.sandboxGroupId,
12677
- instanceId: lease.instanceId,
12678
- leaseEpoch: marked.lease.leaseEpoch
12679
- });
12680
- }
12681
- throw new RoutingBackendRecoveryRequiredError(
12682
- "resolve_home_backend",
12683
- marked.lease?.leaseEpoch ?? lease.leaseEpoch,
12684
- marked.status === "stale" ? "superseded" : homeRouteRecoveryDisposition(marked.lease)
12685
- );
12686
- }
12687
- const reboundBackend = sandboxBackendForSdkBackendId(rebound.backendId) ?? rebound.backendId;
12688
- if (rebound.instanceId !== lease.instanceId || reboundBackend !== resumeBackend) {
12689
- throw homeRouteRecoveryError(lease, lease.leaseEpoch);
12690
- }
12691
- services.onHomeSandboxRebound?.({
12692
- established: rebound,
12693
- leaseEpoch: lease.leaseEpoch
12694
- });
12695
- Object.assign(established, rebound);
12696
- return {
12697
- session: rebound.session,
12698
- sandboxId: null,
12699
- kind: rebound.backendId,
12700
- leaseEpoch: lease.leaseEpoch,
12701
- providerInstanceId: lease.instanceId
12702
- };
12703
- }
12704
- function beforePersistableHomeMutation(services, ids, home) {
12705
- const fence = ids.workspaceMutationFence;
12706
- if (!home || !fence) return void 0;
12707
- return async ({ op, backend, onCaptureWait }) => {
12708
- if (backend.sandboxId !== null || backend.leaseEpoch === void 0 || backend.providerInstanceId === void 0) {
12709
- return null;
12710
- }
12711
- const providerBinding = home.backend === "modal" ? await resolveModalCheckpointProviderBindingForSession(services.settings, backend.session) : null;
12712
- const admission = await advanceWorkspaceGeneration2(services.db, {
12713
- accountId: fence.accountId,
12714
- workspaceId: ids.workspaceId,
12715
- sessionId: ids.sessionId,
12716
- turnId: fence.turnId,
12717
- executionGeneration: fence.executionGeneration,
12718
- attemptId: fence.attemptId,
12719
- holderId: sandboxLeaseHolderIdForAttempt(fence.attemptId),
12720
- sandboxGroupId: home.sandboxGroupId,
12721
- expectedEpoch: backend.leaseEpoch,
12722
- expectedInstanceId: backend.providerInstanceId,
12723
- operation: op,
12724
- captureWaitMs: sandboxLifecycleTransitionWaitMs2(services.settings),
12725
- ...services.waitSignal ? { waitSignal: services.waitSignal } : {},
12726
- ...onCaptureWait ? { onCaptureWait } : {}
12727
- });
12728
- return { admission, providerBinding };
12729
- };
12730
- }
12731
- function afterPersistableHomeMutation(services, ids, home) {
12732
- const fence = ids.workspaceMutationFence;
12733
- if (!home || !fence) return void 0;
12734
- return async ({ op, backend, admission, outcome, retainedProcess }) => {
12735
- if (backend.sandboxId !== null || backend.leaseEpoch === void 0 || backend.providerInstanceId === void 0 || !admission || typeof admission !== "object") {
12736
- return;
12737
- }
12738
- const boundAdmission = admission;
12739
- const exactAdmission = boundAdmission.admission;
12740
- if (!exactAdmission || typeof exactAdmission.id !== "string" || typeof exactAdmission.workspaceGeneration !== "number" || !("providerBinding" in boundAdmission)) {
12741
- throw new Error("Persistable home mutation settlement lacked its bound admission");
12742
- }
12743
- if (outcome === "resolved" && retainedProcess) {
12744
- try {
12745
- await retainWorkspaceMutationProcess(services.db, {
12746
- accountId: fence.accountId,
12747
- workspaceId: ids.workspaceId,
12748
- sessionId: ids.sessionId,
12749
- processId: retainedProcess.id,
12750
- providerSessionId: retainedProcess.providerSessionId,
12751
- admissionId: exactAdmission.id,
12752
- admittedWorkspaceGeneration: exactAdmission.workspaceGeneration,
12753
- operation: op,
12754
- providerBinding: boundAdmission.providerBinding ?? null,
12755
- owner: {
12756
- kind: "turn",
12757
- turnId: fence.turnId,
12758
- executionGeneration: fence.executionGeneration,
12759
- attemptId: fence.attemptId,
12760
- holderId: sandboxLeaseHolderIdForAttempt(fence.attemptId),
12761
- sandboxGroupId: home.sandboxGroupId,
12762
- expectedEpoch: backend.leaseEpoch,
12763
- expectedInstanceId: backend.providerInstanceId,
12764
- routeKind: exactAdmission.routeKind,
12765
- routeTargetId: exactAdmission.routeTargetId,
12766
- routeEpoch: exactAdmission.routeEpoch
12767
- }
12768
- });
12769
- } catch (error) {
12770
- if (!(error instanceof SandboxRetainedProcessPromotionFencedError)) throw error;
12771
- const durable = error.process;
12772
- if (durable.id !== retainedProcess.id || durable.providerSessionId !== retainedProcess.providerSessionId || durable.sessionId !== ids.sessionId || durable.sandboxGroupId !== home.sandboxGroupId || durable.parentAdmissionId !== exactAdmission.id || durable.leaseEpoch !== backend.leaseEpoch || durable.providerInstanceId !== backend.providerInstanceId || durable.routeKind !== exactAdmission.routeKind || durable.routeTargetId !== exactAdmission.routeTargetId || durable.routeEpoch !== exactAdmission.routeEpoch || durable.state !== "active") {
12773
- throw new Error("Durable retained-process promotion returned a mismatched identity", {
12774
- cause: error
12775
- });
12776
- }
12777
- return {
12778
- status: "retained_process_durable_output_rejected",
12779
- retainedProcess: {
12780
- id: durable.id,
12781
- providerSessionId: durable.providerSessionId
12782
- }
12783
- };
12784
- }
12785
- return;
12786
- }
12787
- await verifyWorkspaceMutationSettlement2(services.db, {
12788
- accountId: fence.accountId,
12789
- workspaceId: ids.workspaceId,
12790
- sessionId: ids.sessionId,
12791
- turnId: fence.turnId,
12792
- executionGeneration: fence.executionGeneration,
12793
- attemptId: fence.attemptId,
12794
- holderId: sandboxLeaseHolderIdForAttempt(fence.attemptId),
12795
- sandboxGroupId: home.sandboxGroupId,
12796
- expectedEpoch: backend.leaseEpoch,
12797
- expectedInstanceId: backend.providerInstanceId,
12798
- admission: exactAdmission,
12799
- operation: op,
12800
- outcome
12801
- });
12802
- };
12803
- }
12804
- function beforeRetainedProcessMutation(services, ids) {
12805
- const fence = ids.workspaceMutationFence;
12806
- if (!fence) return void 0;
12807
- return async ({ op, process: process2 }) => await advanceWorkspaceGenerationForRetainedProcess(services.db, {
12808
- accountId: fence.accountId,
12809
- workspaceId: ids.workspaceId,
12810
- sessionId: ids.sessionId,
12811
- processId: process2.id,
12812
- operation: op,
12813
- captureWaitMs: sandboxLifecycleTransitionWaitMs2(services.settings),
12814
- ...services.waitSignal ? { waitSignal: services.waitSignal } : {}
12815
- });
12816
- }
12817
- function afterRetainedProcessMutation(services, ids) {
12818
- const fence = ids.workspaceMutationFence;
12819
- if (!fence) return void 0;
12820
- return async ({ op, process: process2, admission, outcome }) => {
12821
- if (!admission || typeof admission !== "object" || typeof admission.id !== "string" || typeof admission.workspaceGeneration !== "number") {
12822
- throw new Error("Retained-process mutation settlement lacked its exact admission");
12823
- }
12824
- await verifyRetainedProcessMutationSettlement(services.db, {
12825
- accountId: fence.accountId,
12826
- workspaceId: ids.workspaceId,
12827
- sessionId: ids.sessionId,
12828
- processId: process2.id,
12829
- admission,
12830
- operation: op,
12831
- outcome
12832
- });
12833
- };
12834
- }
12835
- function settleRetainedProcessForTurn(services, ids) {
12836
- const fence = ids.workspaceMutationFence;
12837
- if (!fence) return void 0;
12838
- return async ({ backend, process: process2, proof }) => {
12839
- const durable = await getRetainedProcess(services.db, {
12840
- workspaceId: ids.workspaceId,
12841
- sessionId: ids.sessionId,
12842
- processId: process2.id
12843
- });
12844
- if (!durable || durable.providerSessionId !== process2.providerSessionId || durable.providerBackend !== backend.kind || durable.providerInstanceId !== backend.providerInstanceId || durable.leaseEpoch !== backend.leaseEpoch || durable.routeKind !== (backend.sandboxId === null ? "home" : "active") || durable.routeTargetId !== backend.sandboxId || durable.routeEpoch !== backend.activeEpoch) {
12845
- throw new Error("Retained-process settlement lost its exact durable backend identity");
12846
- }
12847
- const settlement = await settleRetainedProcess(services.db, {
12848
- accountId: fence.accountId,
12849
- workspaceId: ids.workspaceId,
12850
- sessionId: ids.sessionId,
12851
- processId: process2.id,
12852
- expected: retainedProcessSettlementIdentity(durable),
12853
- outcome: proof.outcome,
12854
- exitCode: proof.exitCode,
12855
- reason: proof.reason,
12856
- idleGraceMs: services.settings.sandboxIdleGraceMs
12857
- });
12858
- if (settlement.process.state === "active") {
12859
- throw new Error("Retained-process settlement returned an active durable process");
12860
- }
12861
- if (settlement.backgroundCommandEvents.length > 0 && services.bus) {
12862
- await services.bus.publish(ids.workspaceId, ids.sessionId, settlement.backgroundCommandEvents).catch(() => void 0);
12863
- }
12864
- };
12865
- }
12866
- function adoptRetainedProcessAsBackgroundCommandForTurn(services, ids) {
12867
- const fence = ids.workspaceMutationFence;
12868
- if (!fence) return void 0;
12869
- return async ({ backend, process: process2 }) => {
12870
- const durable = await getRetainedProcess(services.db, {
12871
- workspaceId: ids.workspaceId,
12872
- sessionId: ids.sessionId,
12873
- processId: process2.id
12874
- });
12875
- if (!durable || durable.providerSessionId !== process2.providerSessionId || durable.providerBackend !== backend.kind || durable.providerInstanceId !== backend.providerInstanceId || durable.leaseEpoch !== backend.leaseEpoch || durable.routeKind !== (backend.sandboxId === null ? "home" : "active") || durable.routeTargetId !== backend.sandboxId || durable.routeEpoch !== backend.activeEpoch) {
12876
- throw new Error("Background command adoption lost its exact retained-process identity");
12877
- }
12878
- await adoptManagedSessionBackgroundCommand(services.db, {
12879
- accountId: fence.accountId,
12880
- workspaceId: ids.workspaceId,
12881
- sessionId: ids.sessionId,
12882
- turnId: fence.turnId,
12883
- executionGeneration: fence.executionGeneration,
12884
- attemptId: fence.attemptId,
12885
- processId: process2.id,
12886
- expected: retainedProcessSettlementIdentity(durable),
12887
- command: "execCommand"
12888
- });
12889
- };
12890
- }
12891
- function controlRpcFactory(bus) {
12892
- return () => new NatsControlRpc(async () => {
12893
- if (!bus) {
12894
- return null;
12895
- }
12896
- return bus.getRequestConnection();
12897
- });
12898
- }
12899
- function wrapTurnBoxWithRouting(services, ids, established) {
12900
- const { db, settings, bus, onOp } = services;
12901
- const backgroundCommandAccountId = ids.workspaceMutationFence?.accountId;
12902
- const backgroundCommandAttempt = ids.workspaceMutationFence;
12903
- const beforeMutation = beforePersistableHomeMutation(services, ids, ids.homeLease);
12904
- const afterMutation = afterPersistableHomeMutation(services, ids, ids.homeLease);
12905
- const beforeProcessMutation = beforeRetainedProcessMutation(services, ids);
12906
- const afterProcessMutation = afterRetainedProcessMutation(services, ids);
12907
- const settleProcess = settleRetainedProcessForTurn(services, ids);
12908
- const adoptProcessAsBackgroundCommand = adoptRetainedProcessAsBackgroundCommandForTurn(
12909
- services,
12910
- ids
12911
- );
12912
- const resolver = makeActiveBackendResolver({
12913
- workspaceId: ids.workspaceId,
12914
- defaultBackend: established.session,
12915
- defaultKind: established.backendId,
12916
- getSandbox: async (sandboxId) => {
12917
- const sandbox = await getSandbox(
12918
- db,
12919
- ids.resourceAccountId && ids.resourceSubjectId ? {
12920
- accountId: ids.resourceAccountId,
12921
- workspaceId: ids.workspaceId,
12922
- subjectId: ids.resourceSubjectId
12923
- } : ids.workspaceId,
12924
- sandboxId
12925
- );
12926
- return sandbox ? {
12927
- id: sandbox.id,
12928
- workspaceId: sandbox.workspaceId,
12929
- kind: sandbox.kind,
12930
- name: sandbox.name,
12931
- ...sandbox.scope ? { scope: sandbox.scope } : {},
12932
- enrollmentId: sandbox.enrollmentId
12933
- } : null;
12934
- },
12935
- controlRpcFactory: controlRpcFactory(bus),
12936
- resolveSelfhostedConnection: async (sandbox) => {
12937
- if (!sandbox.enrollmentId) return null;
12938
- const enrollment = await resolveRoutedMachineConnection(db, ids, sandbox);
12939
- return connectionBindingFor(services, enrollment);
12940
- },
12941
- relay: relayConfigFromSettings(settings),
12942
- // A selfhosted swap target runs real commands too, so give it the same split
12943
- // deadlines the machine-primary establish path uses (short control, long exec).
12944
- ...selfhostedResolverTimeouts(settings),
12945
- ...onOp !== void 0 ? { selfhostedOnOp: onOp } : {},
12946
- ...backgroundCommandAccountId && backgroundCommandAttempt ? {
12947
- selfhostedAdoptBackgroundCommand: async (command) => {
12948
- const adopted = await adoptConnectedMachineSessionBackgroundCommand(db, {
12949
- accountId: backgroundCommandAccountId,
12950
- workspaceId: ids.workspaceId,
12951
- sessionId: ids.sessionId,
12952
- turnId: backgroundCommandAttempt.turnId,
12953
- executionGeneration: backgroundCommandAttempt.executionGeneration,
12954
- attemptId: backgroundCommandAttempt.attemptId,
12955
- commandId: crypto.randomUUID(),
12956
- controlWorkspaceId: command.controlWorkspaceId,
12957
- enrollmentId: command.enrollmentId,
12958
- connectionInstanceId: command.connectionInstanceId,
12959
- opId: command.opId,
12960
- command: command.command
12961
- });
12962
- return { commandId: adopted.id };
12963
- },
12964
- selfhostedSettleBackgroundCommand: async (command) => {
12965
- const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
12966
- accountId: backgroundCommandAccountId,
12967
- workspaceId: ids.workspaceId,
12968
- sessionId: ids.sessionId,
12969
- commandId: command.commandId,
12970
- controlWorkspaceId: command.controlWorkspaceId,
12971
- enrollmentId: command.enrollmentId,
12972
- connectionInstanceId: command.connectionInstanceId,
12973
- opId: command.opId,
12974
- outcome: command.outcome,
12975
- exitCode: command.exitCode,
12976
- reason: command.reason
12977
- });
12978
- if (settlement && bus) {
12979
- await bus.publish(ids.workspaceId, ids.sessionId, settlement.events).catch(() => void 0);
12980
- }
12981
- }
12982
- } : {},
12983
- // The turn's declared environment → a selfhosted swap target's manifest, so the
12984
- // SDK's per-turn manifest-env delta is empty (no "cannot change manifest
12985
- // environment variables" throw when the turn pins to a vm). Mirrors the group
12986
- // box, which is created WITH this same environment (resumeBoxForTurn).
12987
- ...ids.environment !== void 0 ? { environment: ids.environment } : {},
12988
- ...ids.transientExecEnvironment !== void 0 ? { transientExecEnvironment: ids.transientExecEnvironment } : {},
12989
- // Stage D machine-primary: pin THIS established SelfhostedSession for the machine
12990
- // pointer so the resolver returns the SAME instance (no two-instance manifest
12991
- // divergence). `established.session` is the SelfhostedSession the establish branch
12992
- // bound to (sandboxId, epoch).
12993
- ...ids.pinnedSelfhosted ? {
12994
- pinnedSelfhosted: {
12995
- sandboxId: ids.pinnedSelfhosted.sandboxId,
12996
- epoch: ids.pinnedSelfhosted.epoch,
12997
- session: established.session
12998
- }
12999
- } : {},
13000
- // A modal swap target in the turn path would need its own lease resume-by-id;
13001
- // that is a future cross-group-box concern. Until then a modal swap target is
13002
- // unresolvable (the swap tool validates liveness, so this only triggers if a
13003
- // session points at a sibling modal box the turn cannot resume here) and the
13004
- // op surfaces unresolvable — never a silent wrong-box landing.
13005
- //
13006
- // For a machine-primary turn of a Modal-HOME session (pinned to a machine, no
13007
- // group box established this turn), a mid-turn clear-to-null must NOT fall back to
13008
- // the pinned machine — passing defaultIsHome:false makes the null branch throw typed
13009
- // `home_unavailable_this_turn` instead. Forward the explicit boolean (including false).
13010
- ...ids.defaultIsHome !== void 0 ? { defaultIsHome: ids.defaultIsHome } : {},
13011
- ...ids.homeLease ? {
13012
- resolveDefaultBackend: () => resolveCurrentHomeBackend(
13013
- services,
13014
- {
13015
- workspaceId: ids.workspaceId,
13016
- sessionId: ids.sessionId,
13017
- accountId: ids.homeLease.accountId,
13018
- sandboxGroupId: ids.homeLease.sandboxGroupId,
13019
- backend: ids.homeLease.backend
13020
- },
13021
- established
13022
- )
13023
- } : {}
13024
- });
13025
- const proxy = new RoutingSandboxSession({
13026
- // Seed the DEFAULT backend (the established group box) at construction so
13027
- // `session.state` is the real backend's state object BEFORE the first op. The
13028
- // SDK reads `session.state.manifest` at turn START (and writes it back); an
13029
- // empty `{}` there crashes serializeManifestEnvironment /
13030
- // validateProvidedSessionManifestUpdate. This is byte-identical to what the
13031
- // resolver returns for the default pointer (`activeSandboxId === null`).
13032
- defaultResolved: {
13033
- session: established.session,
13034
- sandboxId: null,
13035
- kind: established.backendId,
13036
- ...ids.homeLease ? {
13037
- leaseEpoch: ids.homeLease.leaseEpoch,
13038
- providerInstanceId: ids.homeLease.instanceId
13039
- } : {}
13040
- },
13041
- readPointer: async () => {
13042
- if (!routingEnabled(settings)) {
13043
- return { activeSandboxId: null, activeEpoch: 0 };
13044
- }
13045
- const pointer = await readActiveSandbox(db, ids.workspaceId, ids.sessionId);
13046
- return pointer ?? { activeSandboxId: null, activeEpoch: 0 };
13047
- },
13048
- resolveActiveBackend: resolver,
13049
- ...services.onSandboxOperation ? { onOperation: services.onSandboxOperation } : {},
13050
- ...beforeMutation ? { beforeMutation } : {},
13051
- ...afterMutation ? { afterMutation } : {},
13052
- ...beforeProcessMutation ? { beforeProcessMutation } : {},
13053
- ...afterProcessMutation ? { afterProcessMutation } : {},
13054
- ...settleProcess ? { settleProcess } : {},
13055
- ...adoptProcessAsBackgroundCommand ? { adoptProcessAsBackgroundCommand } : {},
13056
- ...ids.homeLease ? {
13057
- onDefaultBackendError: async ({
13058
- error,
13059
- backend
13060
- }) => {
13061
- const home = ids.homeLease;
13062
- if (!isProviderSandboxGoneDuringRoutedOperation(home.backend, error)) return null;
13063
- const expectedEpoch = backend.leaseEpoch ?? home.leaseEpoch;
13064
- const expectedInstanceId = backend.providerInstanceId ?? home.instanceId;
13065
- const marked = await markWarmLeaseInstanceLost(db, {
13066
- accountId: home.accountId,
13067
- workspaceId: ids.workspaceId,
13068
- sandboxGroupId: home.sandboxGroupId,
13069
- expectedEpoch,
13070
- expectedInstanceId,
13071
- diagnostic: "provider_not_found_during_routed_operation"
13072
- });
13073
- if (marked.status === "marked") {
13074
- await services.onHomeSandboxLost?.({
13075
- sandboxGroupId: home.sandboxGroupId,
13076
- instanceId: expectedInstanceId,
13077
- leaseEpoch: marked.lease.leaseEpoch
13078
- });
13079
- }
13080
- const lease = marked.lease;
13081
- const restore = lease?.recovery.restore.status;
13082
- return {
13083
- leaseEpoch: lease?.leaseEpoch ?? expectedEpoch,
13084
- recovery: marked.status === "stale" ? "superseded" : restore === "pending" ? "pending" : restore === "degraded" ? "degraded" : "unrecoverable"
13085
- };
13086
- }
13087
- } : {}
13088
- });
13089
- return { ...established, session: proxy };
13090
- }
13091
- function wrapLazyTurnBoxWithRouting(services, ids, args) {
13092
- const { db, settings, bus, onOp } = services;
13093
- const backgroundCommandAccountId = ids.workspaceMutationFence?.accountId;
13094
- const backgroundCommandAttempt = ids.workspaceMutationFence;
13095
- const beforeMutation = beforePersistableHomeMutation(services, ids, args.homeLeaseIdentity);
13096
- const afterMutation = afterPersistableHomeMutation(services, ids, args.homeLeaseIdentity);
13097
- const beforeProcessMutation = beforeRetainedProcessMutation(services, ids);
13098
- const afterProcessMutation = afterRetainedProcessMutation(services, ids);
13099
- const settleProcess = settleRetainedProcessForTurn(services, ids);
13100
- const adoptProcessAsBackgroundCommand = adoptRetainedProcessAsBackgroundCommandForTurn(
13101
- services,
13102
- ids
13103
- );
13104
- const defaultBackend = sandboxBackendForSdkBackendId(args.backendId);
13105
- const defaultSupportsPty = defaultBackend ? selectBackend(defaultBackend).capabilities.Terminal.pty : false;
13106
- const syntheticSession = {
13107
- state: { manifest: args.agentDefaultManifest },
13108
- // The SDK binds shell tools synchronously before lazy provisioning. Preserve
13109
- // the authoritative descriptor's PTY shape so a Modal home exposes
13110
- // write_stdin on its first turn operation; the async cancellation transport
13111
- // below still resolves the exact live route before executing a command.
13112
- supportsPty: () => defaultSupportsPty
13113
- };
13114
- const routedResolver = makeActiveBackendResolver({
13115
- workspaceId: ids.workspaceId,
13116
- defaultBackend: syntheticSession,
13117
- defaultKind: "unprovisioned",
13118
- getSandbox: async (sandboxId) => {
13119
- const sandbox = await getSandbox(
13120
- db,
13121
- ids.resourceAccountId && ids.resourceSubjectId ? {
13122
- accountId: ids.resourceAccountId,
13123
- workspaceId: ids.workspaceId,
13124
- subjectId: ids.resourceSubjectId
13125
- } : ids.workspaceId,
13126
- sandboxId
13127
- );
13128
- return sandbox ? {
13129
- id: sandbox.id,
13130
- workspaceId: sandbox.workspaceId,
13131
- kind: sandbox.kind,
13132
- name: sandbox.name,
13133
- ...sandbox.scope ? { scope: sandbox.scope } : {},
13134
- enrollmentId: sandbox.enrollmentId
13135
- } : null;
13136
- },
13137
- controlRpcFactory: controlRpcFactory(bus),
13138
- resolveSelfhostedConnection: async (sandbox) => {
13139
- if (!sandbox.enrollmentId) return null;
13140
- const enrollment = await resolveRoutedMachineConnection(db, ids, sandbox);
13141
- return connectionBindingFor(services, enrollment);
13142
- },
13143
- relay: relayConfigFromSettings(settings),
13144
- ...selfhostedResolverTimeouts(settings),
13145
- ...onOp !== void 0 ? { selfhostedOnOp: onOp } : {},
13146
- ...backgroundCommandAccountId && backgroundCommandAttempt ? {
13147
- selfhostedAdoptBackgroundCommand: async (command) => {
13148
- const adopted = await adoptConnectedMachineSessionBackgroundCommand(db, {
13149
- accountId: backgroundCommandAccountId,
13150
- workspaceId: ids.workspaceId,
13151
- sessionId: ids.sessionId,
13152
- turnId: backgroundCommandAttempt.turnId,
13153
- executionGeneration: backgroundCommandAttempt.executionGeneration,
13154
- attemptId: backgroundCommandAttempt.attemptId,
13155
- commandId: crypto.randomUUID(),
13156
- controlWorkspaceId: command.controlWorkspaceId,
13157
- enrollmentId: command.enrollmentId,
13158
- connectionInstanceId: command.connectionInstanceId,
13159
- opId: command.opId,
13160
- command: command.command
13161
- });
13162
- return { commandId: adopted.id };
13163
- },
13164
- selfhostedSettleBackgroundCommand: async (command) => {
13165
- const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
13166
- accountId: backgroundCommandAccountId,
13167
- workspaceId: ids.workspaceId,
13168
- sessionId: ids.sessionId,
13169
- commandId: command.commandId,
13170
- controlWorkspaceId: command.controlWorkspaceId,
13171
- enrollmentId: command.enrollmentId,
13172
- connectionInstanceId: command.connectionInstanceId,
13173
- opId: command.opId,
13174
- outcome: command.outcome,
13175
- exitCode: command.exitCode,
13176
- reason: command.reason
13177
- });
13178
- if (settlement && bus) {
13179
- await bus.publish(ids.workspaceId, ids.sessionId, settlement.events).catch(() => void 0);
13180
- }
13181
- }
13182
- } : {},
13183
- ...ids.environment !== void 0 ? { environment: ids.environment } : {},
13184
- ...ids.transientExecEnvironment !== void 0 ? { transientExecEnvironment: ids.transientExecEnvironment } : {}
13185
- });
13186
- const proxy = new RoutingSandboxSession({
13187
- // Before the first op the SDK reads `state.manifest`; the synthetic backend
13188
- // points at agent.defaultManifest BY REFERENCE so the provided-session delta is
13189
- // empty. The first default-pointer op resolves the real box through the
13190
- // provisioner and `state` switches to that real backend by reference.
13191
- defaultResolved: {
13192
- session: syntheticSession,
13193
- sandboxId: null,
13194
- kind: "unprovisioned"
13195
- },
13196
- readPointer: async () => {
13197
- const pointer = await readActiveSandbox(db, ids.workspaceId, ids.sessionId);
13198
- return pointer ?? { activeSandboxId: null, activeEpoch: 0 };
13199
- },
13200
- resolveActiveBackend: async (pointer) => {
13201
- if (pointer.activeSandboxId === null || !routingEnabled(settings)) {
13202
- const provisioned = await args.provisioner.get();
13203
- if (args.homeLeaseIdentity && provisioned.leaseEpoch !== void 0) {
13204
- return resolveCurrentHomeBackend(
13205
- services,
13206
- {
13207
- workspaceId: ids.workspaceId,
13208
- sessionId: ids.sessionId,
13209
- accountId: args.homeLeaseIdentity.accountId,
13210
- sandboxGroupId: args.homeLeaseIdentity.sandboxGroupId,
13211
- backend: args.homeLeaseIdentity.backend
13212
- },
13213
- provisioned.established
13214
- );
13215
- }
13216
- return {
13217
- session: provisioned.established.session,
13218
- sandboxId: null,
13219
- kind: provisioned.established.backendId
13220
- };
13221
- }
13222
- return routedResolver(pointer);
13223
- },
13224
- ...services.onSandboxOperation ? { onOperation: services.onSandboxOperation } : {},
13225
- ...args.onFirstOperation ? { onFirstOperation: args.onFirstOperation } : {},
13226
- ...beforeMutation ? { beforeMutation } : {},
13227
- ...afterMutation ? { afterMutation } : {},
13228
- ...beforeProcessMutation ? { beforeProcessMutation } : {},
13229
- ...afterProcessMutation ? { afterProcessMutation } : {},
13230
- ...settleProcess ? { settleProcess } : {},
13231
- ...adoptProcessAsBackgroundCommand ? { adoptProcessAsBackgroundCommand } : {},
13232
- ...args.homeLeaseIdentity ? {
13233
- onDefaultBackendError: async ({
13234
- error,
13235
- backend
13236
- }) => {
13237
- const home = args.homeLeaseIdentity;
13238
- if (!isProviderSandboxGoneDuringRoutedOperation(home.backend, error)) return null;
13239
- if (backend.leaseEpoch === void 0 || backend.providerInstanceId === void 0) {
13240
- return null;
13241
- }
13242
- const marked = await markWarmLeaseInstanceLost(db, {
13243
- accountId: home.accountId,
13244
- workspaceId: ids.workspaceId,
13245
- sandboxGroupId: home.sandboxGroupId,
13246
- expectedEpoch: backend.leaseEpoch,
13247
- expectedInstanceId: backend.providerInstanceId,
13248
- diagnostic: "provider_not_found_during_routed_operation"
13249
- });
13250
- if (marked.status === "marked") {
13251
- await services.onHomeSandboxLost?.({
13252
- sandboxGroupId: home.sandboxGroupId,
13253
- instanceId: backend.providerInstanceId,
13254
- leaseEpoch: marked.lease.leaseEpoch
13255
- });
13256
- }
13257
- const lease = marked.lease;
13258
- const restore = lease?.recovery.restore.status;
13259
- return {
13260
- leaseEpoch: lease?.leaseEpoch ?? backend.leaseEpoch,
13261
- recovery: marked.status === "stale" ? "superseded" : restore === "pending" ? "pending" : restore === "degraded" ? "degraded" : "unrecoverable"
13262
- };
13263
- }
13264
- } : {}
13265
- });
13266
- return {
13267
- client: args.client,
13268
- session: proxy,
13269
- sessionState: void 0,
13270
- instanceId: "unprovisioned",
13271
- backendId: args.backendId
13272
- };
13273
- }
13274
- function opStreamDepsFor(services, machineAdvertisesOpStream) {
13275
- const { settings, bus, opJournal } = services;
13276
- if (!machineAdvertisesOpStream || settings.agentOpStreamEnabled !== true || !bus) {
13277
- return void 0;
13278
- }
13279
- return {
13280
- transport: new NatsOpStreamTransport(async () => bus.getOpStreamConnection?.() ?? null),
13281
- ...opJournal !== void 0 ? { journal: opJournal } : {}
13282
- };
13283
- }
13284
- function connectionBindingFor(services, enrollment) {
13285
- if (!enrollment?.connectionInstanceId) return null;
13286
- const opStream = opStreamDepsFor(services, enrollment.opStream === true);
13287
- return {
13288
- workspaceId: enrollment.workspaceId,
13289
- connectionInstanceId: enrollment.connectionInstanceId,
13290
- workspaceRoot: enrollment.workspaceRoot,
13291
- ...opStream !== void 0 ? { opStream } : {},
13292
- operationResourcePolicy: enrollment.operationPolicy,
13293
- operationResourcePolicySupported: enrollment.agentCapabilities.operationResourcePolicy === true,
13294
- operationCpuQuotaSupported: enrollment.agentCapabilities.operationCpuQuota === true
13295
- };
13296
- }
13297
- async function resolveRoutedMachineConnection(db, ids, sandbox) {
13298
- if (!sandbox.enrollmentId) return null;
13299
- if (sandbox.scope !== "user") {
13300
- return await getLiveEnrollmentConnection(
13301
- db,
13302
- ids.resourceAccountId && ids.resourceSubjectId ? {
13303
- accountId: ids.resourceAccountId,
13304
- workspaceId: ids.workspaceId,
13305
- subjectId: ids.resourceSubjectId
13306
- } : sandbox.workspaceId,
13307
- sandbox.enrollmentId
13308
- );
13309
- }
13310
- const authority = ids.personalMachineAttempt;
13311
- if (!authority) return null;
13312
- return await resolvePersonalMachineConnectionForAttempt(db, {
13313
- accountId: authority.accountId,
13314
- workspaceId: ids.workspaceId,
13315
- subjectId: authority.subjectId,
13316
- sessionId: ids.sessionId,
13317
- turnId: authority.turnId,
13318
- attemptId: authority.attemptId,
13319
- executionGeneration: authority.executionGeneration,
13320
- enrollmentId: sandbox.enrollmentId,
13321
- requireActiveSandbox: true
13322
- });
13323
- }
13324
- async function establishSelfhostedTurnSession(services, args) {
13325
- const { db, settings, bus, onOp } = services;
13326
- const { timeoutMs, execTimeoutMs } = selfhostedTimeoutsFromSettings(settings);
13327
- const opStream = opStreamDepsFor(services, args.opStream);
13328
- const { client, session } = await buildSelfhostedBackendSession({
13329
- workspaceId: args.workspaceId,
13330
- ...args.controlWorkspaceId !== void 0 ? { controlWorkspaceId: args.controlWorkspaceId } : {},
13331
- agentId: args.agentId,
13332
- connectionInstanceId: args.connectionInstanceId,
13333
- workspaceRoot: args.workspaceRoot,
13334
- relay: relayConfigFromSettings(settings),
13335
- controlRpcFactory: controlRpcFactory(bus),
13336
- epoch: args.epoch,
13337
- environment: args.environment,
13338
- ...args.transientExecEnvironment !== void 0 ? { transientExecEnvironment: args.transientExecEnvironment } : {},
13339
- // Give this turn's exec ops the long deadline (control ops stay short) so a real
13340
- // command is not killed at the control wall.
13341
- timeoutMs,
13342
- execTimeoutMs,
13343
- operationResourcePolicy: args.operationResourcePolicy,
13344
- operationResourcePolicySupported: args.operationResourcePolicySupported,
13345
- operationCpuQuotaSupported: args.operationCpuQuotaSupported,
13346
- resolveOperationAdmission: async () => {
13347
- const enrollment = args.personalMachineAttempt ? await resolvePersonalMachineConnectionForAttempt(db, {
13348
- accountId: args.personalMachineAttempt.accountId,
13349
- workspaceId: args.workspaceId,
13350
- subjectId: args.personalMachineAttempt.subjectId,
13351
- sessionId: args.personalMachineAttempt.sessionId,
13352
- turnId: args.personalMachineAttempt.turnId,
13353
- attemptId: args.personalMachineAttempt.attemptId,
13354
- executionGeneration: args.personalMachineAttempt.executionGeneration,
13355
- enrollmentId: args.agentId,
13356
- requireActiveSandbox: true
13357
- }) : await getLiveEnrollmentConnection(
13358
- db,
13359
- args.controlWorkspaceId ?? args.workspaceId,
13360
- args.agentId
13361
- );
13362
- const binding = connectionBindingFor(services, enrollment);
13363
- if (!binding) return null;
13364
- if (!binding.workspaceRoot) {
13365
- throw new ActiveBackendUnresolvableError(
13366
- "workspace_root_unavailable",
13367
- "Connected Machine no longer has a reported workspace root"
13368
- );
13369
- }
13370
- return {
13371
- ...binding,
13372
- workspaceRoot: resolveConnectedMachineWorkspaceRoot(binding.workspaceRoot, args.workingDir)
13373
- };
13374
- },
13375
- ...args.backgroundCommandAttempt ? {
13376
- adoptBackgroundCommand: async (command) => {
13377
- const adopted = await adoptConnectedMachineSessionBackgroundCommand(db, {
13378
- accountId: args.accountId,
13379
- workspaceId: args.workspaceId,
13380
- sessionId: args.sessionId,
13381
- turnId: args.backgroundCommandAttempt.turnId,
13382
- executionGeneration: args.backgroundCommandAttempt.executionGeneration,
13383
- attemptId: args.backgroundCommandAttempt.attemptId,
13384
- commandId: crypto.randomUUID(),
13385
- controlWorkspaceId: command.controlWorkspaceId,
13386
- enrollmentId: command.enrollmentId,
13387
- connectionInstanceId: command.connectionInstanceId,
13388
- opId: command.opId,
13389
- command: command.command
13390
- });
13391
- return { commandId: adopted.id };
13392
- }
13393
- } : {},
13394
- settleBackgroundCommand: async (command) => {
13395
- const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
13396
- accountId: args.accountId,
13397
- workspaceId: args.workspaceId,
13398
- sessionId: args.sessionId,
13399
- commandId: command.commandId,
13400
- controlWorkspaceId: command.controlWorkspaceId,
13401
- enrollmentId: command.enrollmentId,
13402
- connectionInstanceId: command.connectionInstanceId,
13403
- opId: command.opId,
13404
- outcome: command.outcome,
13405
- exitCode: command.exitCode,
13406
- reason: command.reason
13407
- });
13408
- if (settlement && bus) {
13409
- await bus.publish(args.workspaceId, args.sessionId, settlement.events).catch(() => void 0);
13410
- }
13411
- },
13412
- // Meter every control op (out-of-band telemetry) — no-op when unwired.
13413
- ...onOp !== void 0 ? { onOp } : {},
13414
- // The streaming exec transport — present iff the machine advertised the
13415
- // capability AND the server flag is on. Connected Machine exec fails closed
13416
- // when it is absent; there is no request/reply downgrade.
13417
- ...opStream !== void 0 ? { opStream } : {}
13418
- });
13419
- return {
13420
- client,
13421
- session,
13422
- sessionState: { agentId: args.agentId },
13423
- instanceId: args.agentId,
13424
- backendId: "selfhosted"
13425
- };
13426
- }
13427
- function routingEnabled(settings) {
13428
- return settings.sandboxSelfhostedEnabled === true;
13429
- }
13430
- function lazyProvisionEnabled(settings) {
13431
- return settings.sandboxLazyProvisionEnabled === true && settings.sandboxOwnershipEnabled === true;
13432
- }
13433
-
13434
12515
  // src/activities/agent-turn/sandbox-route.ts
13435
12516
  import {
13436
- readActiveSandbox as readActiveSandbox2,
12517
+ readActiveSandbox,
13437
12518
  setActiveSandbox
13438
12519
  } from "@opengeni/db";
13439
12520
  import {
@@ -13546,7 +12627,7 @@ async function reconcileActiveSandboxPointer(db, ids, pointer, loadRecord, publi
13546
12627
  ).catch(() => void 0);
13547
12628
  return { pointer: reset.pointer, record: null };
13548
12629
  }
13549
- const reread = await readActiveSandbox2(db, ids.workspaceId, ids.sessionId).catch(() => null);
12630
+ const reread = await readActiveSandbox(db, ids.workspaceId, ids.sessionId).catch(() => null);
13550
12631
  if (!reread) {
13551
12632
  return { pointer, record: null };
13552
12633
  }
@@ -13615,7 +12696,7 @@ async function resolveSandboxRoute(deps) {
13615
12696
  logicalSandboxSettings
13616
12697
  } = deps;
13617
12698
  const routingOn = routingEnabled(settings);
13618
- let activeSandboxPointer = routingOn ? await readActiveSandbox3(db, input.workspaceId, input.sessionId).catch(() => null) : null;
12699
+ let activeSandboxPointer = routingOn ? await readActiveSandbox2(db, input.workspaceId, input.sessionId).catch(() => null) : null;
13619
12700
  let activeSandboxRecord = null;
13620
12701
  if (routingOn) {
13621
12702
  const reconciled = await reconcileActiveSandboxPointer(
@@ -13626,7 +12707,7 @@ async function resolveSandboxRoute(deps) {
13626
12707
  sessionId: input.sessionId
13627
12708
  },
13628
12709
  activeSandboxPointer,
13629
- (sandboxId) => getSandbox2(
12710
+ (sandboxId) => getSandbox(
13630
12711
  db,
13631
12712
  fileAuthoritySubjectId ? {
13632
12713
  accountId: input.accountId,
@@ -13769,7 +12850,7 @@ async function establishTurnSandbox(deps) {
13769
12850
  throw new Error("personal Connected Machine use was not admitted for this turn");
13770
12851
  }
13771
12852
  }
13772
- const liveMachineEnrollment = await getLiveEnrollmentConnection2(
12853
+ const liveMachineEnrollment = await getLiveEnrollmentConnection(
13773
12854
  db,
13774
12855
  activeSandboxRecord.workspaceId,
13775
12856
  activeSandboxRecord.enrollmentId
@@ -13784,7 +12865,7 @@ async function establishTurnSandbox(deps) {
13784
12865
  "Connected Machine has not reported an absolute workspace root; reconnect it with a current agent"
13785
12866
  );
13786
12867
  }
13787
- const machineWorkspaceRoot = resolveConnectedMachineWorkspaceRoot2(
12868
+ const machineWorkspaceRoot = resolveConnectedMachineWorkspaceRoot(
13788
12869
  machineEnrollment.workspaceRoot,
13789
12870
  activeSandboxPointer.workingDir
13790
12871
  );
@@ -14509,6 +13590,16 @@ async function selectXaiTurnCapacity(deps) {
14509
13590
  authoritySnapshot
14510
13591
  });
14511
13592
  const leaseStartedAtMs = performance.now();
13593
+ await refreshExhaustedXaiQuota({
13594
+ db,
13595
+ settings: deps.settings,
13596
+ accountId: input.accountId,
13597
+ workspaceId: input.workspaceId,
13598
+ subjectId,
13599
+ sessionId: input.sessionId,
13600
+ turnId: turn.id,
13601
+ authoritySnapshot
13602
+ });
14512
13603
  const leased = await acquireXaiCredentialLease(db, {
14513
13604
  accountId: input.accountId,
14514
13605
  workspaceId: input.workspaceId,
@@ -15607,7 +14698,7 @@ import {
15607
14698
  } from "@opengeni/runtime";
15608
14699
 
15609
14700
  // src/activities/google-drive-publication.ts
15610
- import { createHash as createHash9 } from "crypto";
14701
+ import { createHash as createHash10 } from "crypto";
15611
14702
  import {
15612
14703
  GOOGLE_DRIVE_FILE_SCOPE,
15613
14704
  GOOGLE_DRIVE_FULL_SCOPE,
@@ -15707,7 +14798,7 @@ function googleDrivePublicationConnectorCall(target, rawRequest, approvalId) {
15707
14798
  sourceSha256: request.file.sha256
15708
14799
  },
15709
14800
  canonicalReceipt: { ...request.file },
15710
- idempotencyKeyDigest: createHash9("sha256").update(request.idempotencyKey, "utf8").digest("hex")
14801
+ idempotencyKeyDigest: createHash10("sha256").update(request.idempotencyKey, "utf8").digest("hex")
15711
14802
  }
15712
14803
  };
15713
14804
  }
@@ -15895,7 +14986,7 @@ async function executeGoogleDrivePublication(input, ports = defaultPorts) {
15895
14986
  );
15896
14987
  if (!object) throw new Error("Editable artifact export object is missing");
15897
14988
  const bytes = object.bytes;
15898
- if (bytes.byteLength !== request.file.sizeBytes || createHash9("sha256").update(bytes).digest("hex") !== request.file.sha256) {
14989
+ if (bytes.byteLength !== request.file.sizeBytes || createHash10("sha256").update(bytes).digest("hex") !== request.file.sha256) {
15899
14990
  throw new Error("Editable artifact export bytes failed verification");
15900
14991
  }
15901
14992
  const authorizedFetch = async (url, init) => {
@@ -15977,7 +15068,7 @@ function assertPublicationConnection(input, connection) {
15977
15068
  }
15978
15069
  }
15979
15070
  function publicationActorReplicaId(identity) {
15980
- const value = createHash9("sha256").update(
15071
+ const value = createHash10("sha256").update(
15981
15072
  `google-drive-publication:${identity.sessionId}:${identity.turnId}:${identity.attemptId}:${identity.executionGeneration}`,
15982
15073
  "utf8"
15983
15074
  ).digest("hex").slice(0, 16);
@@ -16022,7 +15113,7 @@ function officeFormat(modality, contentType) {
16022
15113
  return expected.format;
16023
15114
  }
16024
15115
  function deterministicUuid(seed) {
16025
- const bytes = createHash9("sha256").update(seed, "utf8").digest().subarray(0, 16);
15116
+ const bytes = createHash10("sha256").update(seed, "utf8").digest().subarray(0, 16);
16026
15117
  bytes[6] = (bytes[6] ?? 0) & 15 | 80;
16027
15118
  bytes[8] = (bytes[8] ?? 0) & 63 | 128;
16028
15119
  const hex = bytes.toString("hex");
@@ -16160,7 +15251,7 @@ function driveReceipt(input, file, replayed) {
16160
15251
  });
16161
15252
  }
16162
15253
  function publicationOperationKey(input) {
16163
- return createHash9("sha256").update(
15254
+ return createHash10("sha256").update(
16164
15255
  JSON.stringify({
16165
15256
  workspaceId: input.workspaceId,
16166
15257
  connectionId: input.connectionId,
@@ -16829,7 +15920,7 @@ function canWrite(permissions) {
16829
15920
  }
16830
15921
 
16831
15922
  // src/activities/connector-attachments.ts
16832
- import { createHash as createHash10 } from "crypto";
15923
+ import { createHash as createHash11 } from "crypto";
16833
15924
  import {
16834
15925
  CONNECTOR_ATTACHMENT_RECEIPT_VERSION,
16835
15926
  ConnectorAttachmentReceiptEnvelope
@@ -16849,7 +15940,7 @@ var ConnectorAttachmentMaterializationError = class extends Error {
16849
15940
  }
16850
15941
  };
16851
15942
  function digestParts(...parts) {
16852
- const hash = createHash10("sha256");
15943
+ const hash = createHash11("sha256");
16853
15944
  for (const part of parts) {
16854
15945
  const bytes = Buffer.from(part, "utf8");
16855
15946
  hash.update(String(bytes.byteLength));
@@ -17968,6 +17059,12 @@ async function prepareTurnToolRuntime(deps) {
17968
17059
  resolveCredential,
17969
17060
  onAuthNeeded: publishToolAuthNeeded,
17970
17061
  materializeConnectorAttachments,
17062
+ refreshOwnedCommand: async (commandId) => {
17063
+ throwIfWorkerShuttingDown();
17064
+ throwIfTurnCancelled();
17065
+ const owned = sandboxState.lazyOwnedSandbox?.session ?? sandboxState.resolvedSandbox?.established.session ?? media.sdkOwnedSandboxSession;
17066
+ return await owned?.refreshOwnedCommand?.(commandId) ?? false;
17067
+ },
17971
17068
  spillOversizedModelToolResult: async ({ operationId, result }) => await toolResultSpill.spill({ operationId, result }),
17972
17069
  localMcpServers,
17973
17070
  ...deferNonEagerToolPreparation ? { deferNonEagerUntilToolDemand: true } : {},
@@ -18245,7 +17342,7 @@ async function applyTurnGitHubRepositoryBindings(input) {
18245
17342
  // src/activities/agent-turn/agent-build.ts
18246
17343
  import {
18247
17344
  selectXaiCredentialForUse,
18248
- materializeXaiCredentialForRun as materializeXaiCredentialForRun2,
17345
+ materializeXaiCredentialForRun,
18249
17346
  resolveXaiProviderAccountAuthoritySnapshotForAcceptance,
18250
17347
  getXaiSessionAccountPin as getXaiSessionAccountPin2,
18251
17348
  setXaiSessionAccountPin as setXaiSessionAccountPin2,
@@ -18254,7 +17351,7 @@ import {
18254
17351
  } from "@opengeni/db";
18255
17352
  import {
18256
17353
  serviceTierForLatencyMode as serviceTierForLatencyMode2,
18257
- environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes4,
17354
+ environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes3,
18258
17355
  WORKSPACE_GATEWAY_PROVIDER_ID as WORKSPACE_GATEWAY_PROVIDER_ID2,
18259
17356
  resolveModelProvider as resolveModelProvider2
18260
17357
  } from "@opengeni/config";
@@ -18333,9 +17430,9 @@ import {
18333
17430
  } from "@opengeni/db";
18334
17431
  import {
18335
17432
  calculateVideoGenerationCreditCostMicros,
18336
- environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes3
17433
+ environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes2
18337
17434
  } from "@opengeni/config";
18338
- import { createHash as createHash12, randomUUID as randomUUID5 } from "crypto";
17435
+ import { createHash as createHash13, randomUUID as randomUUID5 } from "crypto";
18339
17436
 
18340
17437
  // src/activities/video-reference-staging.ts
18341
17438
  import {
@@ -18344,7 +17441,7 @@ import {
18344
17441
 
18345
17442
  // src/activities/video-media-validation.ts
18346
17443
  import { retryWhileMissing as retryWhileMissing5 } from "@opengeni/storage";
18347
- import { createHash as createHash11, randomUUID as randomUUID4 } from "crypto";
17444
+ import { createHash as createHash12, randomUUID as randomUUID4 } from "crypto";
18348
17445
  import { open, mkdir, rm, stat } from "fs/promises";
18349
17446
  import { join } from "path";
18350
17447
  import { spawn } from "child_process";
@@ -18372,7 +17469,7 @@ async function copyVersionedObjectToVerifiedTemp(input) {
18372
17469
  const path = join(directory, "payload");
18373
17470
  await mkdir(directory, { recursive: false, mode: 448 });
18374
17471
  const handle = await open(path, "wx", 384);
18375
- const hash = createHash11("sha256");
17472
+ const hash = createHash12("sha256");
18376
17473
  let offset = 0;
18377
17474
  try {
18378
17475
  while (offset < input.expectedSizeBytes) {
@@ -18892,7 +17989,7 @@ function videoCapabilitiesForTurn(input) {
18892
17989
  }
18893
17990
  function managedVideoGenerationCredentialLease(settings) {
18894
17991
  if (!settings.vercelAiGatewayApiKey) return null;
18895
- const encryptionKey = environmentsEncryptionKeyBytes3(settings);
17992
+ const encryptionKey = environmentsEncryptionKeyBytes2(settings);
18896
17993
  if (!encryptionKey) {
18897
17994
  throw new Error("Managed video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
18898
17995
  }
@@ -18907,7 +18004,7 @@ function managedVideoGenerationCredentialLease(settings) {
18907
18004
  });
18908
18005
  }
18909
18006
  function xaiVideoGenerationCredentialLease(input) {
18910
- const encryptionKey = environmentsEncryptionKeyBytes3(input.settings);
18007
+ const encryptionKey = environmentsEncryptionKeyBytes2(input.settings);
18911
18008
  if (!encryptionKey) {
18912
18009
  throw new Error("SuperGrok video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
18913
18010
  }
@@ -18967,7 +18064,7 @@ async function admitVideoGenerationRequest(input) {
18967
18064
  operationId: candidateOperationId,
18968
18065
  requestDigest
18969
18066
  });
18970
- const encryptionKey = environmentsEncryptionKeyBytes3(input.settings);
18067
+ const encryptionKey = environmentsEncryptionKeyBytes2(input.settings);
18971
18068
  if (!encryptionKey) {
18972
18069
  throw new Error("Video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
18973
18070
  }
@@ -18991,7 +18088,7 @@ async function admitVideoGenerationRequest(input) {
18991
18088
  toolCallId: input.toolCallId,
18992
18089
  admissionKey,
18993
18090
  requestDigest,
18994
- promptDigest: createHash12("sha256").update(canonical.prompt).digest("hex"),
18091
+ promptDigest: createHash13("sha256").update(canonical.prompt).digest("hex"),
18995
18092
  requestEncrypted: encryptEnvironmentValue2(encryptionKey, JSON.stringify(canonical)),
18996
18093
  modelId: canonical.modelId,
18997
18094
  sourceMode: canonical.sourceMode,
@@ -19652,7 +18749,7 @@ async function buildTurnAgent(deps) {
19652
18749
  };
19653
18750
  }
19654
18751
  } else if (videoGenerationPolicy.fundingSource === "supergrok_subscription") {
19655
- const encryptionKey = environmentsEncryptionKeyBytes4(eventing.modelRunSettings);
18752
+ const encryptionKey = environmentsEncryptionKeyBytes3(eventing.modelRunSettings);
19656
18753
  const subjectId = leases.xai.subjectId ?? turn.initiatingHumanSubjectId;
19657
18754
  if (encryptionKey && subjectId) {
19658
18755
  const authoritySnapshot = providerTurn.xaiAuthoritySnapshot ?? await resolveXaiProviderAccountAuthoritySnapshotForAcceptance(db, {
@@ -19693,7 +18790,7 @@ async function buildTurnAgent(deps) {
19693
18790
  throw error;
19694
18791
  });
19695
18792
  }
19696
- const credential = await materializeXaiCredentialForRun2(db, {
18793
+ const credential = await materializeXaiCredentialForRun(db, {
19697
18794
  workspaceId: input.workspaceId,
19698
18795
  subjectId,
19699
18796
  credentialId: selected.credentialId,
@@ -19823,6 +18920,7 @@ async function buildTurnAgent(deps) {
19823
18920
  let agentConstructionOutcome = "completed";
19824
18921
  try {
19825
18922
  return runtime.buildAgent(eventing.modelRunSettings, runtimeResources, {
18923
+ ...preparedTools.inputWaitYield ? { inputWaitYield: preparedTools.inputWaitYield } : {},
19826
18924
  reasoningEffort: turn.reasoningEffort,
19827
18925
  latencyMode: turnExecutionPolicy.latencyMode,
19828
18926
  ...serviceTier ? { serviceTier } : {},
@@ -21257,6 +20355,7 @@ function createRunAgentTurnActivity(services) {
21257
20355
  videoGenerationAcceptancesByCallId
21258
20356
  });
21259
20357
  } catch (error) {
20358
+ eventing.preparedTools?.inputWaitYield?.closeAdmission();
21260
20359
  return await settleTurnFailure({
21261
20360
  error,
21262
20361
  input,
@@ -21318,7 +20417,7 @@ function createRunAgentTurnActivity(services) {
21318
20417
  }
21319
20418
 
21320
20419
  // src/activities/video-generation-reconciliation.ts
21321
- import { environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes5 } from "@opengeni/config";
20420
+ import { environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes4 } from "@opengeni/config";
21322
20421
  import { videoGenerationRequestDigest as videoGenerationRequestDigest2 } from "@opengeni/core";
21323
20422
  import {
21324
20423
  addSessionSystemUpdateWithSourceMutation,
@@ -21330,8 +20429,8 @@ import {
21330
20429
  markVideoGenerationRetaining,
21331
20430
  markVideoGenerationSubmissionIntent,
21332
20431
  markVideoGenerationSubmissionUncertain,
21333
- materializeXaiCredentialForRun as materializeXaiCredentialForRun3,
21334
- refreshXaiSubscriptionCredentialSerialized as refreshXaiSubscriptionCredentialSerialized2,
20432
+ materializeXaiCredentialForRun as materializeXaiCredentialForRun2,
20433
+ refreshXaiSubscriptionCredentialSerialized,
21335
20434
  rotateVideoGenerationCredential,
21336
20435
  markVideoGenerationTerminalUpdate,
21337
20436
  markVideoGenerationTerminalUpdateInTransaction,
@@ -21544,7 +20643,7 @@ function publicProviderReason(error, cancelled) {
21544
20643
  // src/activities/video-output-retention.ts
21545
20644
  import { GENERATED_VIDEO_MAX_BYTES as GENERATED_VIDEO_MAX_BYTES2 } from "@opengeni/contracts";
21546
20645
  import { pinnedFetch } from "@opengeni/network";
21547
- import { createHash as createHash13, randomUUID as randomUUID7 } from "crypto";
20646
+ import { createHash as createHash14, randomUUID as randomUUID7 } from "crypto";
21548
20647
  import { createReadStream } from "fs";
21549
20648
  import { mkdir as mkdir2, open as open2, rm as rm2 } from "fs/promises";
21550
20649
  import { join as join2 } from "path";
@@ -21582,7 +20681,7 @@ async function downloadGeneratedVideoToVerifiedTemp(input) {
21582
20681
  await mkdir2(directory, { recursive: false, mode: 448 });
21583
20682
  const handle = await open2(path, "wx", 384);
21584
20683
  const reader = response.body.getReader();
21585
- const digest2 = createHash13("sha256");
20684
+ const digest2 = createHash14("sha256");
21586
20685
  let sizeBytes = 0;
21587
20686
  try {
21588
20687
  for (; ; ) {
@@ -21751,9 +20850,9 @@ function throwIfAborted3(signal) {
21751
20850
 
21752
20851
  // src/activities/video-generation-reconciliation.ts
21753
20852
  import {
21754
- refreshXaiToken as refreshXaiToken2,
20853
+ refreshXaiToken,
21755
20854
  XaiSubscriptionError,
21756
- xaiAccessTokenExpiry as xaiAccessTokenExpiry2
20855
+ xaiAccessTokenExpiry
21757
20856
  } from "@opengeni/xai-subscription";
21758
20857
 
21759
20858
  // src/activities/xai-video-generation.ts
@@ -21822,7 +20921,7 @@ async function reconcileVideoGenerationOperation(service, input) {
21822
20921
  await deliverTerminalResult(service, operation);
21823
20922
  return { action: "terminal", status: operation.status };
21824
20923
  }
21825
- const key = environmentsEncryptionKeyBytes5(service.settings);
20924
+ const key = environmentsEncryptionKeyBytes4(service.settings);
21826
20925
  if (!key) throw new Error("Video generation recovery encryption key is unavailable");
21827
20926
  if (!operation.credentialEncrypted) {
21828
20927
  throw new Error("Video provider credential lease was erased too early");
@@ -22267,7 +21366,7 @@ async function durableXaiVideoAuth(service, operation, key, credential) {
22267
21366
  let currentEncrypted = frozenEncrypted;
22268
21367
  let connectedAccount = null;
22269
21368
  try {
22270
- connectedAccount = await materializeXaiCredentialForRun3(service.db, {
21369
+ connectedAccount = await materializeXaiCredentialForRun2(service.db, {
22271
21370
  workspaceId: operation.workspaceId,
22272
21371
  subjectId: current.subjectId,
22273
21372
  credentialId: current.credentialId,
@@ -22285,7 +21384,7 @@ async function durableXaiVideoAuth(service, operation, key, credential) {
22285
21384
  let next = null;
22286
21385
  if (connectedAccount) {
22287
21386
  try {
22288
- const result = await refreshXaiSubscriptionCredentialSerialized2(service.db, {
21387
+ const result = await refreshXaiSubscriptionCredentialSerialized(service.db, {
22289
21388
  accountId: operation.accountId,
22290
21389
  workspaceId: operation.workspaceId,
22291
21390
  subjectId: current.subjectId,
@@ -22299,14 +21398,14 @@ async function durableXaiVideoAuth(service, operation, key, credential) {
22299
21398
  if (!refreshToken) {
22300
21399
  throw new Error("The connected SuperGrok account cannot be refreshed");
22301
21400
  }
22302
- const tokens = await refreshXaiToken2(refreshToken);
21401
+ const tokens = await refreshXaiToken(refreshToken);
22303
21402
  return {
22304
21403
  secret: {
22305
21404
  version: 1,
22306
21405
  accessToken: tokens.accessToken,
22307
21406
  refreshToken: tokens.refreshToken
22308
21407
  },
22309
- expiresAt: xaiAccessTokenExpiry2(tokens.accessToken) ?? new Date(Date.now() + tokens.expiresInSeconds * 1e3)
21408
+ expiresAt: xaiAccessTokenExpiry(tokens.accessToken) ?? new Date(Date.now() + tokens.expiresInSeconds * 1e3)
22310
21409
  };
22311
21410
  }
22312
21411
  });
@@ -22324,7 +21423,7 @@ async function durableXaiVideoAuth(service, operation, key, credential) {
22324
21423
  }
22325
21424
  }
22326
21425
  if (!next) {
22327
- const tokens = await refreshXaiToken2(current.refreshToken);
21426
+ const tokens = await refreshXaiToken(current.refreshToken);
22328
21427
  next = Object.freeze({
22329
21428
  ...current,
22330
21429
  accessToken: tokens.accessToken,
@@ -22407,4 +21506,4 @@ export {
22407
21506
  createTurnActivities,
22408
21507
  createTurnActivitiesFromServices
22409
21508
  };
22410
- //# sourceMappingURL=activities-turn-S5IU347A.js.map
21509
+ //# sourceMappingURL=activities-turn-GHSFZB64.js.map