@opengeni/worker-bundle 0.22.4-canary.1 → 0.22.7-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.
Files changed (41) hide show
  1. package/dist/activities/agent-turn/errors.d.ts +20 -0
  2. package/dist/activities/agent-turn/governance-model.d.ts +9 -0
  3. package/dist/activities/agent-turn/image-reference-sandbox.d.ts +14 -0
  4. package/dist/activities/agent-turn/provider-dispatch-barrier.d.ts +8 -0
  5. package/dist/activities/generated-images.d.ts +3 -1
  6. package/dist/activities/image-generation-references.d.ts +39 -7
  7. package/dist/activities/sandbox-lease.d.ts +12 -0
  8. package/dist/activities/types.d.ts +24 -0
  9. package/dist/{activities-control-4XVBA3Q5.js → activities-control-VZIIV7RX.js} +77 -12
  10. package/dist/activities-control-VZIIV7RX.js.map +1 -0
  11. package/dist/{activities-turn-J3CR27E6.js → activities-turn-5RGQDBFV.js} +253 -51
  12. package/dist/activities-turn-5RGQDBFV.js.map +1 -0
  13. package/dist/{chunk-FA35XBBC.js → chunk-NAVSAZ56.js} +1 -1
  14. package/dist/chunk-NAVSAZ56.js.map +1 -0
  15. package/dist/{chunk-XWQPS6GG.js → chunk-O4HDX5SC.js} +59 -10
  16. package/dist/{chunk-XWQPS6GG.js.map → chunk-O4HDX5SC.js.map} +1 -1
  17. package/dist/index.js +2 -2
  18. package/dist/observability-metrics.d.ts +1 -1
  19. package/dist/workflow-bundle.js +62 -9
  20. package/dist/workflows/session.d.ts +4 -1
  21. package/package.json +17 -17
  22. package/src/activities/agent-turn/agent-build.ts +29 -18
  23. package/src/activities/agent-turn/claim.ts +6 -6
  24. package/src/activities/agent-turn/errors.ts +104 -21
  25. package/src/activities/agent-turn/failure-settlement.ts +40 -0
  26. package/src/activities/agent-turn/governance-model.ts +31 -4
  27. package/src/activities/agent-turn/history.ts +7 -3
  28. package/src/activities/agent-turn/image-reference-sandbox.ts +35 -0
  29. package/src/activities/agent-turn/provider-dispatch-barrier.ts +13 -0
  30. package/src/activities/agent-turn/run.ts +7 -0
  31. package/src/activities/agent-turn/stream-attempt.ts +25 -2
  32. package/src/activities/generated-images.ts +5 -2
  33. package/src/activities/image-generation-references.ts +184 -37
  34. package/src/activities/sandbox-lease.ts +48 -9
  35. package/src/activities/session-state.ts +69 -1
  36. package/src/activities/types.ts +26 -0
  37. package/src/observability-metrics.ts +2 -0
  38. package/src/workflows/session.ts +95 -21
  39. package/dist/activities-control-4XVBA3Q5.js.map +0 -1
  40. package/dist/activities-turn-J3CR27E6.js.map +0 -1
  41. package/dist/chunk-FA35XBBC.js.map +0 -1
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  CODEX_USAGE_EXHAUSTED_PCT,
3
3
  CODEX_USAGE_LIMIT_MAX_RESUME_MS,
4
+ PostCompactionContinuationEmptyError,
4
5
  SandboxDeadlineRotationError,
5
6
  agentRunFailurePayload,
6
7
  assertPhysicalToolQuiescenceForCancellation,
@@ -38,6 +39,7 @@ import {
38
39
  nextStreamEvent,
39
40
  persistOrSignalSessionAttemptQuiescence,
40
41
  persistSandboxDeadlineRotationCheckpoint,
42
+ postClaimDatabaseRecoveryFailure,
41
43
  preClaimAdmissionFailure,
42
44
  providerRecoveryCountFromMetadata,
43
45
  providerRecoveryExhaustedFailure,
@@ -74,7 +76,7 @@ import {
74
76
  waitForTurnStreamCleanup,
75
77
  waitForWarmSnapshot,
76
78
  withFirstPartyTools
77
- } from "./chunk-XWQPS6GG.js";
79
+ } from "./chunk-O4HDX5SC.js";
78
80
  import {
79
81
  StreamTimingMetrics,
80
82
  makeMachineOpObserver,
@@ -97,7 +99,7 @@ import {
97
99
  recordTurnWorkerPreparationTotal,
98
100
  runtimeMetricsHooksForObservability,
99
101
  turnLifecycleMetricsFor
100
- } from "./chunk-FA35XBBC.js";
102
+ } from "./chunk-NAVSAZ56.js";
101
103
 
102
104
  // src/activities-turn.ts
103
105
  import { createProductionAgentRuntime, summarizeForCompaction as summarizeForCompaction2 } from "@opengeni/runtime";
@@ -2370,8 +2372,9 @@ function crc32(bytes) {
2370
2372
  var GENERATED_IMAGE_MARKER = "generated_image";
2371
2373
  var UPLOAD_INTENT_TTL_MS2 = 60 * 6e4;
2372
2374
  var GeneratedImageValidationError = class extends Error {
2373
- constructor(message) {
2375
+ constructor(message, reason) {
2374
2376
  super(message);
2377
+ this.reason = reason;
2375
2378
  this.name = "GeneratedImageValidationError";
2376
2379
  }
2377
2380
  };
@@ -2469,7 +2472,7 @@ function validateGeneratedImage(input) {
2469
2472
  );
2470
2473
  } catch (error) {
2471
2474
  if (error instanceof ScreenshotValidationError) {
2472
- throw new GeneratedImageValidationError(error.message);
2475
+ throw new GeneratedImageValidationError(error.message, error.reason);
2473
2476
  }
2474
2477
  throw error;
2475
2478
  }
@@ -3120,7 +3123,11 @@ import { appendSessionHistoryItems, upsertSandboxSessionEnvelope } from "@openge
3120
3123
  import { sandboxStateEntryFromRunState } from "@opengeni/runtime";
3121
3124
 
3122
3125
  // src/activities/agent-turn/history.ts
3123
- import { normalizeProtocolJsonValue, sanitizeHistoryItemsForModel } from "@opengeni/runtime";
3126
+ import {
3127
+ normalizeProtocolJsonValue,
3128
+ sanitizeHistoryItemsForModel,
3129
+ toolCallIdFromSdkItem
3130
+ } from "@opengeni/runtime";
3124
3131
  import { opaqueProviderArtifactFingerprint } from "@opengeni/codex";
3125
3132
  function historyRowsToAppend(rawHistory, persistedHistoryCount, nextPosition = persistedHistoryCount, toolOutputTruncationTokens) {
3126
3133
  const modelReady = sanitizeHistoryItemsForModel(rawHistory, toolOutputTruncationTokens);
@@ -3187,7 +3194,7 @@ function pendingToolCallFromSdkEvent(event) {
3187
3194
  }
3188
3195
  const raw = item.rawItem;
3189
3196
  if (raw.type === "hosted_tool_call" && raw.name === "image_generation_call") return null;
3190
- const callId = raw.callId ?? raw.call_id ?? raw.id;
3197
+ const callId = toolCallIdFromSdkItem(raw) ?? raw.id;
3191
3198
  const callType = raw.type;
3192
3199
  if (typeof callId !== "string" || callId.length === 0 || typeof callType !== "string") {
3193
3200
  return null;
@@ -3211,7 +3218,7 @@ function completedToolCallFromSdkEvent(event) {
3211
3218
  return null;
3212
3219
  }
3213
3220
  const raw = item.rawItem && typeof item.rawItem === "object" && !Array.isArray(item.rawItem) ? item.rawItem : {};
3214
- const callId = raw.callId ?? raw.call_id ?? item.id;
3221
+ const callId = toolCallIdFromSdkItem(raw) ?? item.id;
3215
3222
  if (typeof callId !== "string" || callId.length === 0) return null;
3216
3223
  return {
3217
3224
  callId,
@@ -3306,6 +3313,11 @@ function createTurnHistorySink(deps) {
3306
3313
  return new TurnHistorySink(deps);
3307
3314
  }
3308
3315
 
3316
+ // src/activities/agent-turn/provider-dispatch-barrier.ts
3317
+ async function checkpointHistoryBeforeProviderDispatch(historySink) {
3318
+ await historySink.reconcileConversationTruth({ requireDurable: true });
3319
+ }
3320
+
3309
3321
  // src/activities/agent-turn/run.ts
3310
3322
  import { sandboxRunAs as sandboxRunAs7 } from "@opengeni/runtime";
3311
3323
  import { randomUUID as randomUUID6 } from "crypto";
@@ -6479,9 +6491,39 @@ async function settleTurnFailure(deps) {
6479
6491
  control.activityError = error;
6480
6492
  throw escaped;
6481
6493
  }
6494
+ const postClaimRecovery = recoveryResult.status === "recovering" ? postClaimDatabaseRecoveryFailure({
6495
+ error: recoveryError,
6496
+ turnId: attempt.turnId,
6497
+ triggerEventId: attempt.triggerEventId,
6498
+ executionGeneration: attempt.executionGeneration,
6499
+ providerRecovery: {
6500
+ failureCode: failure.code ?? "provider_unavailable",
6501
+ providerRecoveryCount: nextProviderRecoveryCount
6502
+ }
6503
+ }) : null;
6504
+ if (postClaimRecovery) {
6505
+ control.activityStatus = "recovering";
6506
+ control.turnMetricOutcome = "recovering";
6507
+ control.activityError = error;
6508
+ throw postClaimRecovery;
6509
+ }
6482
6510
  throw recoveryError;
6483
6511
  }
6484
6512
  }
6513
+ if (attempt.turnId && attempt.triggerEventId && attempt.executionGeneration > 0 && !eventing.turnStartedPublished && !attempt.modelRequestStarted) {
6514
+ const recoveryFailure = postClaimDatabaseRecoveryFailure({
6515
+ error,
6516
+ turnId: attempt.turnId,
6517
+ triggerEventId: attempt.triggerEventId,
6518
+ executionGeneration: attempt.executionGeneration
6519
+ });
6520
+ if (recoveryFailure) {
6521
+ control.activityStatus = "recovering";
6522
+ control.turnMetricOutcome = "recovering";
6523
+ control.activityError = error;
6524
+ throw recoveryFailure;
6525
+ }
6526
+ }
6485
6527
  control.activityStatus = "failed";
6486
6528
  control.activityError = error;
6487
6529
  if (!attempt.turnId) {
@@ -7841,10 +7883,11 @@ async function runTurnStreamAttempt(deps) {
7841
7883
  };
7842
7884
  const modelResponseState = createModelResponseEventState(claimedModelUsageSourceKeys);
7843
7885
  let workerPreparationTotalRecorded = false;
7844
- const runStreamAttempt = async () => {
7886
+ const runStreamAttempt = async (options) => {
7845
7887
  if (!runInput) {
7846
7888
  throw new Error("Run input was not prepared");
7847
7889
  }
7890
+ const responseCountBeforeStream = modelResponseState.responseCount;
7848
7891
  eventing.stream = void 0;
7849
7892
  eventing.batcher = null;
7850
7893
  let currentToolBatchCallIds = /* @__PURE__ */ new Set();
@@ -7856,6 +7899,7 @@ async function runTurnStreamAttempt(deps) {
7856
7899
  const providerPublishesNativeRequestEvents = resolvedModel?.provider.kind === "codex-subscription" || resolvedModel?.provider.kind === "xai-subscription";
7857
7900
  let fallbackProviderRequestStartedAt = null;
7858
7901
  const recordFallbackProviderDispatchAtWire = async () => {
7902
+ await checkpointHistoryBeforeProviderDispatch(historySink);
7859
7903
  if (providerPublishesNativeRequestEvents || eventing.firstModelRequestPreparationRecorded || eventing.firstModelRequestPreparationStartedAt === null) {
7860
7904
  return;
7861
7905
  }
@@ -8460,6 +8504,11 @@ async function runTurnStreamAttempt(deps) {
8460
8504
  eventing.stream.completed.catch(() => void 0),
8461
8505
  cancellationSignal
8462
8506
  );
8507
+ if (options.requireTerminalModelResponse && eventing.stream.interruptions.length === 0 && modelResponseState.responseCount === responseCountBeforeStream) {
8508
+ throwIfWorkerShuttingDown();
8509
+ throwIfTurnCancelled();
8510
+ throw new PostCompactionContinuationEmptyError();
8511
+ }
8463
8512
  if (!streamSawPerResponseUsage) {
8464
8513
  const aggregateUsage = eventing.stream.state.usage;
8465
8514
  const normalizedAggregateUsage = normalizeModelCallUsage2(aggregateUsage);
@@ -8734,7 +8783,9 @@ async function runTurnStreamAttempt(deps) {
8734
8783
  let retriedAfterCompaction = false;
8735
8784
  while (true) {
8736
8785
  try {
8737
- const result = await runStreamAttempt();
8786
+ const result = await runStreamAttempt({
8787
+ requireTerminalModelResponse: retriedAfterCompaction
8788
+ });
8738
8789
  if (retriedAfterCompaction) {
8739
8790
  observability.info("context compaction recovery succeeded after in-activity retry", {
8740
8791
  sessionId: input.sessionId,
@@ -9179,9 +9230,13 @@ async function claimTurnAttempt(deps) {
9179
9230
  }
9180
9231
  const turn = claim.turn;
9181
9232
  attempt.turnId = turn.id;
9182
- const session = await requireSession(db, input.workspaceId, input.sessionId);
9183
9233
  attempt.executionGeneration = turn.executionGeneration;
9184
9234
  attempt.providerRecoveryCount = providerRecoveryCountFromMetadata(turn.metadata);
9235
+ attempt.triggerEventId = turn.triggerEventId;
9236
+ attempt.redispatchesAtDispatch = Number(
9237
+ turn.metadata?.workerDeathRedispatches ?? 0
9238
+ );
9239
+ const session = await requireSession(db, input.workspaceId, input.sessionId);
9185
9240
  let installedApiIntegrations = [];
9186
9241
  const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
9187
9242
  const fileAuthoritySubjectId = turn.initiatingHumanSubjectId ?? null;
@@ -9246,7 +9301,6 @@ async function claimTurnAttempt(deps) {
9246
9301
  billingState.isExternallyBilledTurn = billingIdentity.externallyBilled;
9247
9302
  billingState.isCodexTurn = billingIdentity.codexSubscription;
9248
9303
  billingState.isXaiTurn = billingIdentity.xaiSubscription;
9249
- attempt.triggerEventId = turn.triggerEventId;
9250
9304
  const trigger = await getSessionEvent3(db, input.workspaceId, attempt.triggerEventId);
9251
9305
  if (!trigger) {
9252
9306
  throw new Error(`Trigger event not found: ${attempt.triggerEventId}`);
@@ -9264,9 +9318,6 @@ async function claimTurnAttempt(deps) {
9264
9318
  trigger
9265
9319
  );
9266
9320
  attempt.triggerType = trigger.type;
9267
- attempt.redispatchesAtDispatch = Number(
9268
- turn.metadata?.workerDeathRedispatches ?? 0
9269
- );
9270
9321
  turnLifecycleMetricsFor(observability).start(attempt.turnId);
9271
9322
  await waitForTurnOperation(
9272
9323
  ensureRunAllowed(
@@ -10604,6 +10655,18 @@ function modelAttachmentInputPolicyForTurn(resolvedModel) {
10604
10655
  }
10605
10656
 
10606
10657
  // src/activities/agent-turn/governance-model.ts
10658
+ function preferenceSnapshotHumanSubjectId(turn) {
10659
+ return turn.initiatingHumanSubjectId ?? (turn.initiator.kind === "subject" ? turn.initiator.subjectId : null);
10660
+ }
10661
+ async function preferenceSnapshotForTurn(turn, load) {
10662
+ if (!preferenceSnapshotHumanSubjectId(turn)) return null;
10663
+ try {
10664
+ return await load();
10665
+ } catch (error) {
10666
+ if (error instanceof PreferenceRegistryInitiatorError) return null;
10667
+ throw error;
10668
+ }
10669
+ }
10607
10670
  async function prepareGovernanceAndModel(deps) {
10608
10671
  const {
10609
10672
  input,
@@ -10658,10 +10721,10 @@ async function prepareGovernanceAndModel(deps) {
10658
10721
  getWorkspace2(db, input.workspaceId),
10659
10722
  getOrCreateCompanyProfileSnapshot(db, governanceClaims),
10660
10723
  getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
10661
- getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
10662
- if (error instanceof PreferenceRegistryInitiatorError) return null;
10663
- throw error;
10664
- })
10724
+ preferenceSnapshotForTurn(
10725
+ turn,
10726
+ async () => await getOrCreatePreferenceRegistrySnapshot(db, governanceClaims)
10727
+ )
10665
10728
  ]),
10666
10729
  getWorkspaceModelPolicy(db, input.workspaceId)
10667
10730
  ]);
@@ -18437,19 +18500,54 @@ async function executeCodexImageGeneration(input) {
18437
18500
  import { getGeneratedImageArtifact as getGeneratedImageArtifact2, requireFileForSubject as requireFileForSubject2 } from "@opengeni/db";
18438
18501
  var IMAGE_GENERATION_REFERENCE_MAX_BYTES = 30 * 1024 * 1024;
18439
18502
  var IMAGE_GENERATION_REFERENCES_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
18503
+ var ImageGenerationReferenceError = class extends Error {
18504
+ constructor(code, message, options) {
18505
+ super(message, options);
18506
+ this.code = code;
18507
+ this.name = "ImageGenerationReferenceError";
18508
+ }
18509
+ };
18510
+ async function resolveImageGenerationReferencesForTool(input) {
18511
+ try {
18512
+ return {
18513
+ status: "resolved",
18514
+ references: await resolveImageGenerationReferences(input)
18515
+ };
18516
+ } catch (error) {
18517
+ if (!(error instanceof ImageGenerationReferenceError)) throw error;
18518
+ const text = `Image generation was not started: ${error.message}`;
18519
+ return {
18520
+ status: "rejected",
18521
+ result: {
18522
+ isError: true,
18523
+ status: "rejected",
18524
+ code: error.code,
18525
+ message: error.message,
18526
+ operationCreated: false,
18527
+ content: [{ type: "text", text }]
18528
+ }
18529
+ };
18530
+ }
18531
+ }
18440
18532
  async function resolveImageGenerationReferences(input) {
18441
18533
  const resolved = [];
18442
18534
  let totalBytes = 0;
18443
18535
  for (const reference2 of input.references) {
18444
18536
  const source = await referenceBytes(input, reference2);
18445
18537
  if (source.bytes.byteLength > IMAGE_GENERATION_REFERENCE_MAX_BYTES) {
18446
- throw new Error("Image reference exceeds the per-image byte limit");
18538
+ throw new ImageGenerationReferenceError(
18539
+ "reference_too_large",
18540
+ "An image reference exceeds the per-image byte limit."
18541
+ );
18447
18542
  }
18448
18543
  totalBytes += source.bytes.byteLength;
18449
18544
  if (totalBytes > IMAGE_GENERATION_REFERENCES_MAX_TOTAL_BYTES) {
18450
- throw new Error("Image references exceed the combined byte limit");
18545
+ throw new ImageGenerationReferenceError(
18546
+ "reference_too_large",
18547
+ "The image references exceed the combined byte limit."
18548
+ );
18451
18549
  }
18452
- const image = validateGeneratedImage({ bytes: source.bytes });
18550
+ const image = validateReferenceImage(source.bytes);
18453
18551
  if (source.file) assertStoredFileMatches(source.file, image);
18454
18552
  resolved.push({
18455
18553
  mediaType: image.mediaType,
@@ -18463,23 +18561,58 @@ async function resolveImageGenerationReferences(input) {
18463
18561
  async function referenceBytes(input, reference2) {
18464
18562
  if (reference2.kind === "sandbox_path") {
18465
18563
  if (!input.readSandboxFile) {
18466
- throw new Error("Sandbox image references require an active sandbox");
18564
+ throw new ImageGenerationReferenceError(
18565
+ "reference_unavailable",
18566
+ "Sandbox image references require an available workspace sandbox."
18567
+ );
18568
+ }
18569
+ try {
18570
+ return {
18571
+ bytes: await input.readSandboxFile(
18572
+ reference2.path,
18573
+ IMAGE_GENERATION_REFERENCE_MAX_BYTES + 1
18574
+ )
18575
+ };
18576
+ } catch (error) {
18577
+ if (error instanceof ImageGenerationReferenceError) throw error;
18578
+ throw new ImageGenerationReferenceError(
18579
+ "reference_unavailable",
18580
+ "The sandbox image reference could not be read. Verify that the file still exists in the current workspace.",
18581
+ { cause: error }
18582
+ );
18467
18583
  }
18468
- return {
18469
- bytes: await input.readSandboxFile(reference2.path, IMAGE_GENERATION_REFERENCE_MAX_BYTES + 1)
18470
- };
18471
18584
  }
18472
- const file = reference2.kind === "file" ? await requireFileForSubject2(input.db, {
18473
- accountId: input.accountId,
18474
- workspaceId: input.workspaceId,
18475
- subjectId: input.subjectId,
18476
- fileId: reference2.fileId
18477
- }) : await artifactFile(input.db, input.workspaceId, reference2.artifactId);
18585
+ let file;
18586
+ if (reference2.kind === "file") {
18587
+ try {
18588
+ file = await requireFileForSubject2(input.db, {
18589
+ accountId: input.accountId,
18590
+ workspaceId: input.workspaceId,
18591
+ subjectId: input.subjectId,
18592
+ fileId: reference2.fileId
18593
+ });
18594
+ } catch (error) {
18595
+ if (!(error instanceof Error) || !error.message.startsWith("File not found:")) throw error;
18596
+ throw new ImageGenerationReferenceError(
18597
+ "reference_unavailable",
18598
+ "The workspace File image reference is unavailable.",
18599
+ { cause: error }
18600
+ );
18601
+ }
18602
+ } else {
18603
+ file = await artifactFile(input.db, input.workspaceId, reference2.artifactId);
18604
+ }
18478
18605
  if (file.status !== "ready" || !file.sha256) {
18479
- throw new Error("Image reference file is not durably ready");
18606
+ throw new ImageGenerationReferenceError(
18607
+ "reference_not_ready",
18608
+ "The image reference is not durably ready yet."
18609
+ );
18480
18610
  }
18481
18611
  if (file.sizeBytes <= 0 || file.sizeBytes > IMAGE_GENERATION_REFERENCE_MAX_BYTES) {
18482
- throw new Error("Image reference file exceeds the supported size");
18612
+ throw new ImageGenerationReferenceError(
18613
+ "reference_too_large",
18614
+ "The image reference file exceeds the supported size."
18615
+ );
18483
18616
  }
18484
18617
  return {
18485
18618
  bytes: await readStoredFile(input.objectStorage, file),
@@ -18491,19 +18624,56 @@ async function readStoredFile(objectStorage, file) {
18491
18624
  start: 0,
18492
18625
  end: file.sizeBytes - 1
18493
18626
  });
18494
- if (!bytes) throw new Error("Image reference file bytes are unavailable");
18627
+ if (!bytes) {
18628
+ throw new ImageGenerationReferenceError(
18629
+ "reference_unavailable",
18630
+ "The durable image reference bytes are unavailable."
18631
+ );
18632
+ }
18495
18633
  return bytes;
18496
18634
  }
18497
18635
  async function artifactFile(db, workspaceId, artifactId) {
18498
18636
  const artifact = await getGeneratedImageArtifact2(db, workspaceId, artifactId);
18499
18637
  if (!artifact || artifact.status !== "ready") {
18500
- throw new Error(`Generated image artifact not found or unavailable: ${artifactId}`);
18638
+ throw new ImageGenerationReferenceError(
18639
+ "reference_unavailable",
18640
+ "The generated-image artifact reference is unavailable."
18641
+ );
18501
18642
  }
18502
18643
  return artifact.file;
18503
18644
  }
18504
18645
  function assertStoredFileMatches(file, image) {
18505
18646
  if (file.sizeBytes !== image.sizeBytes || file.sha256 !== image.sha256) {
18506
- throw new Error("Image reference bytes do not match their durable file metadata");
18647
+ throw new ImageGenerationReferenceError(
18648
+ "reference_integrity_mismatch",
18649
+ "The image reference bytes do not match their durable file metadata."
18650
+ );
18651
+ }
18652
+ }
18653
+ function validateReferenceImage(bytes) {
18654
+ try {
18655
+ return validateGeneratedImage({ bytes });
18656
+ } catch (error) {
18657
+ if (!(error instanceof GeneratedImageValidationError)) throw error;
18658
+ if (error.reason === "unsupported") {
18659
+ throw new ImageGenerationReferenceError(
18660
+ "unsupported_reference_media",
18661
+ "Image references must be PNG, JPEG, or WebP. Convert SVG or other formats before calling generate_image.",
18662
+ { cause: error }
18663
+ );
18664
+ }
18665
+ if (error.reason === "oversized") {
18666
+ throw new ImageGenerationReferenceError(
18667
+ "reference_too_large",
18668
+ "The image reference exceeds the supported dimensions or byte size.",
18669
+ { cause: error }
18670
+ );
18671
+ }
18672
+ throw new ImageGenerationReferenceError(
18673
+ "invalid_reference",
18674
+ "The image reference is not a valid PNG, JPEG, or WebP image.",
18675
+ { cause: error }
18676
+ );
18507
18677
  }
18508
18678
  }
18509
18679
 
@@ -18511,6 +18681,24 @@ function assertStoredFileMatches(file, image) {
18511
18681
  import { SandboxChannelAService as SandboxChannelAService4 } from "@opengeni/runtime/sandbox";
18512
18682
  import { sandboxRunAs as sandboxRunAs6 } from "@opengeni/runtime";
18513
18683
  import { VideoGenerationRejectedResult } from "@opengeni/contracts";
18684
+
18685
+ // src/activities/agent-turn/image-reference-sandbox.ts
18686
+ async function resolveImageReferenceSandboxSession(sandboxState, sdkOwnedSandboxSession) {
18687
+ let sandbox = sandboxState.resolvedSandbox;
18688
+ if (!sandbox && sandboxState.turnSandboxProvisioner) {
18689
+ sandbox = await sandboxState.turnSandboxProvisioner.get();
18690
+ }
18691
+ const session = sandboxState.lazyOwnedSandbox?.session ?? sandbox?.established.session ?? sdkOwnedSandboxSession;
18692
+ if (!session) {
18693
+ throw new Error("Sandbox image reference is unavailable");
18694
+ }
18695
+ return {
18696
+ session,
18697
+ leaseEpoch: sandbox?.leaseEpoch ?? 0
18698
+ };
18699
+ }
18700
+
18701
+ // src/activities/agent-turn/agent-build.ts
18514
18702
  async function buildTurnAgent(deps) {
18515
18703
  const {
18516
18704
  input,
@@ -18582,7 +18770,7 @@ async function buildTurnAgent(deps) {
18582
18770
  }
18583
18771
  } : {};
18584
18772
  const hostedWebSearch = hostedWebSearchForTurn(resolvedModel, runSettings.webSearchEnabled);
18585
- const resolveImageReferences = async (references) => await resolveImageGenerationReferences({
18773
+ const resolveImageReferences = async (references) => await resolveImageGenerationReferencesForTool({
18586
18774
  db,
18587
18775
  objectStorage,
18588
18776
  accountId: input.accountId,
@@ -18590,16 +18778,16 @@ async function buildTurnAgent(deps) {
18590
18778
  subjectId: fileAuthoritySubjectId,
18591
18779
  references,
18592
18780
  readSandboxFile: async (path, maxBytes) => {
18593
- const imageReferenceSession = sandboxState.setupBoxSession ?? media.sdkOwnedSandboxSession;
18594
- if (!imageReferenceSession) {
18595
- throw new Error("Sandbox image reference is unavailable");
18596
- }
18781
+ const imageReferenceSandbox = await resolveImageReferenceSandboxSession(
18782
+ sandboxState,
18783
+ media.sdkOwnedSandboxSession
18784
+ );
18597
18785
  const relativePath = path.slice("/workspace/".length);
18598
18786
  const referenceRunAs = sandboxRunAs6(eventing.modelRunSettings);
18599
18787
  const channel = new SandboxChannelAService4({
18600
- session: imageReferenceSession,
18788
+ session: imageReferenceSandbox.session,
18601
18789
  workspaceRoot: "/workspace",
18602
- leaseEpoch: sandboxState.resolvedSandbox?.leaseEpoch ?? 0,
18790
+ leaseEpoch: imageReferenceSandbox.leaseEpoch,
18603
18791
  ...referenceRunAs ? { runAs: referenceRunAs } : {}
18604
18792
  });
18605
18793
  const read = await channel.fsRead({
@@ -18607,7 +18795,12 @@ async function buildTurnAgent(deps) {
18607
18795
  encoding: "base64",
18608
18796
  maxBytes
18609
18797
  });
18610
- if (read.truncated) throw new Error("Sandbox image reference exceeds the byte limit");
18798
+ if (read.truncated) {
18799
+ throw new ImageGenerationReferenceError(
18800
+ "reference_too_large",
18801
+ "The sandbox image reference exceeds the per-image byte limit."
18802
+ );
18803
+ }
18611
18804
  return Uint8Array.from(Buffer.from(read.content, "base64"));
18612
18805
  }
18613
18806
  });
@@ -18632,7 +18825,8 @@ async function buildTurnAgent(deps) {
18632
18825
  imageGeneration: {
18633
18826
  kind: "provider_adapter",
18634
18827
  execute: async ({ prompt, references }, { toolCallId }) => {
18635
- const resolvedReferences = await resolveImageReferences(references);
18828
+ const referenceResolution = await resolveImageReferences(references);
18829
+ if (referenceResolution.status === "rejected") return referenceResolution.result;
18636
18830
  const receipt = await executeCodexImageGeneration({
18637
18831
  db,
18638
18832
  objectStorage,
@@ -18643,7 +18837,7 @@ async function buildTurnAgent(deps) {
18643
18837
  attemptId: input.attemptId,
18644
18838
  toolCallId,
18645
18839
  prompt,
18646
- references: resolvedReferences,
18840
+ references: referenceResolution.references,
18647
18841
  credentialId: imageAuthority.credentialId,
18648
18842
  codexContext: imageAuthority.credentialContext,
18649
18843
  ...runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}
@@ -18665,7 +18859,8 @@ async function buildTurnAgent(deps) {
18665
18859
  imageGeneration: {
18666
18860
  kind: "provider_adapter",
18667
18861
  execute: async ({ prompt, references }, { toolCallId }) => {
18668
- const resolvedReferences = await resolveImageReferences(references);
18862
+ const referenceResolution = await resolveImageReferences(references);
18863
+ if (referenceResolution.status === "rejected") return referenceResolution.result;
18669
18864
  const receipt = await executeXaiSubscriptionImageGeneration({
18670
18865
  db,
18671
18866
  objectStorage,
@@ -18676,7 +18871,7 @@ async function buildTurnAgent(deps) {
18676
18871
  attemptId: input.attemptId,
18677
18872
  toolCallId,
18678
18873
  prompt,
18679
- references: resolvedReferences,
18874
+ references: referenceResolution.references,
18680
18875
  credentialId: imageAuthority.credentialId,
18681
18876
  xaiContext: imageAuthority.credentialContext,
18682
18877
  ...runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}
@@ -18699,7 +18894,8 @@ async function buildTurnAgent(deps) {
18699
18894
  imageGeneration: {
18700
18895
  kind: "provider_adapter",
18701
18896
  execute: async ({ prompt, references }, { toolCallId }) => {
18702
- const resolvedReferences = await resolveImageReferences(references);
18897
+ const referenceResolution = await resolveImageReferences(references);
18898
+ if (referenceResolution.status === "rejected") return referenceResolution.result;
18703
18899
  const receipt = await executeGatewayImageGeneration({
18704
18900
  db,
18705
18901
  objectStorage,
@@ -18711,7 +18907,7 @@ async function buildTurnAgent(deps) {
18711
18907
  apiKey: gatewayApiKey,
18712
18908
  modelId: capabilitySettings.imageGenerationModel,
18713
18909
  prompt,
18714
- references: resolvedReferences,
18910
+ references: referenceResolution.references,
18715
18911
  toolCallId,
18716
18912
  ...runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}
18717
18913
  });
@@ -19455,6 +19651,9 @@ function createRunAgentTurnActivity(services) {
19455
19651
  if (!eventing.publish || !attempt.turnId) {
19456
19652
  throw new Error("Codex model request started before the turn event producer");
19457
19653
  }
19654
+ if (event.phase === "started") {
19655
+ await checkpointHistoryBeforeProviderDispatch(historySink);
19656
+ }
19458
19657
  const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
19459
19658
  if (shouldRecordStartedAudit) {
19460
19659
  firstModelRequestAuditRecorded = true;
@@ -19573,6 +19772,9 @@ function createRunAgentTurnActivity(services) {
19573
19772
  if (!eventing.publish || !attempt.turnId) {
19574
19773
  throw new Error("SuperGrok model request started before the turn event producer");
19575
19774
  }
19775
+ if (event.phase === "started") {
19776
+ await checkpointHistoryBeforeProviderDispatch(historySink);
19777
+ }
19576
19778
  const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
19577
19779
  if (shouldRecordStartedAudit) firstModelRequestAuditRecorded = true;
19578
19780
  const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
@@ -21443,4 +21645,4 @@ export {
21443
21645
  createTurnActivities,
21444
21646
  createTurnActivitiesFromServices
21445
21647
  };
21446
- //# sourceMappingURL=activities-turn-J3CR27E6.js.map
21648
+ //# sourceMappingURL=activities-turn-5RGQDBFV.js.map