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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +3 -10
  3. package/dist/activities/agent-turn/claim.d.ts +8 -0
  4. package/dist/activities/agent-turn/codex.d.ts +2 -2
  5. package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
  6. package/dist/activities/agent-turn/errors.d.ts +1 -1
  7. package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +11 -6
  11. package/dist/activities/agent-turn/run.d.ts +9 -0
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  13. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +4 -13
  16. package/dist/activities/agent-turn/tool-policy.d.ts +40 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +13 -2
  18. package/dist/activities/agent-turn.d.ts +1 -1
  19. package/dist/activities/automations.d.ts +1 -0
  20. package/dist/activities/capabilities.d.ts +3 -1
  21. package/dist/activities/codemode-dispatcher.d.ts +10 -1
  22. package/dist/activities/codex-capacity.d.ts +32 -3
  23. package/dist/activities/codex-fleet-shadow.d.ts +1 -1
  24. package/dist/activities/codex-image-generation.d.ts +1 -1
  25. package/dist/activities/codex-rotation.d.ts +8 -67
  26. package/dist/activities/context-compaction.d.ts +2 -0
  27. package/dist/activities/goals.d.ts +12 -1
  28. package/dist/activities/google-drive-publication.d.ts +2 -13
  29. package/dist/activities/image-generation-operation.d.ts +13 -4
  30. package/dist/activities/image-generation-references.d.ts +2 -1
  31. package/dist/activities/scheduled-tasks.d.ts +1 -0
  32. package/dist/activities/session-state.d.ts +4 -2
  33. package/dist/activities/types.d.ts +19 -1
  34. package/dist/{activities-control-52W5IDWM.js → activities-control-CTEMLTHG.js} +339 -195
  35. package/dist/activities-control-CTEMLTHG.js.map +1 -0
  36. package/dist/activities-control.d.ts +4 -0
  37. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-S5IU347A.js} +1846 -1136
  38. package/dist/activities-turn-S5IU347A.js.map +1 -0
  39. package/dist/activities.d.ts +4 -0
  40. package/dist/{chunk-GFNXVZVQ.js → chunk-D4ZEFOYH.js} +107 -7
  41. package/dist/chunk-D4ZEFOYH.js.map +1 -0
  42. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  43. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  44. package/dist/{chunk-IPVMCAW5.js → chunk-TAUE3GYT.js} +225 -154
  45. package/dist/chunk-TAUE3GYT.js.map +1 -0
  46. package/dist/github-rest-mcp.d.ts +0 -2
  47. package/dist/index.d.ts +2 -0
  48. package/dist/index.js +43 -7
  49. package/dist/index.js.map +1 -1
  50. package/dist/observability-metrics.d.ts +35 -8
  51. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  52. package/dist/sandbox-resume.d.ts +6 -1
  53. package/dist/workflow-bundle.js +96 -10
  54. package/dist/workflows/activities.d.ts +6 -1
  55. package/dist/workflows/automations.d.ts +5 -0
  56. package/dist/workflows/session.d.ts +3 -0
  57. package/package.json +17 -17
  58. package/src/activities/agent-run-admission.ts +7 -1
  59. package/src/activities/agent-turn/admission.ts +47 -6
  60. package/src/activities/agent-turn/agent-build.ts +20 -26
  61. package/src/activities/agent-turn/claim.ts +59 -6
  62. package/src/activities/agent-turn/codex-capacity.ts +311 -234
  63. package/src/activities/agent-turn/codex.ts +2 -2
  64. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  65. package/src/activities/agent-turn/credential-leases.ts +36 -0
  66. package/src/activities/agent-turn/errors.ts +29 -1
  67. package/src/activities/agent-turn/failure-settlement.ts +451 -323
  68. package/src/activities/agent-turn/finalization.ts +31 -17
  69. package/src/activities/agent-turn/list-models.ts +101 -0
  70. package/src/activities/agent-turn/model-usage.ts +120 -45
  71. package/src/activities/agent-turn/quiescence.ts +32 -17
  72. package/src/activities/agent-turn/run.ts +98 -9
  73. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  74. package/src/activities/agent-turn/session-title.ts +55 -2
  75. package/src/activities/agent-turn/stream-attempt.ts +281 -136
  76. package/src/activities/agent-turn/tool-environment.ts +132 -39
  77. package/src/activities/agent-turn/tool-policy.ts +71 -12
  78. package/src/activities/agent-turn/turn-context.ts +21 -2
  79. package/src/activities/agent-turn.ts +5 -1
  80. package/src/activities/automations.ts +122 -67
  81. package/src/activities/capabilities.ts +129 -2
  82. package/src/activities/codemode-dispatcher.ts +235 -59
  83. package/src/activities/codex-capacity.ts +89 -10
  84. package/src/activities/codex-fleet-shadow.ts +8 -1
  85. package/src/activities/codex-image-generation.ts +19 -2
  86. package/src/activities/codex-rotation.ts +45 -142
  87. package/src/activities/context-compaction.ts +3 -0
  88. package/src/activities/goals.ts +116 -39
  89. package/src/activities/google-drive-publication.ts +4 -81
  90. package/src/activities/image-generation-operation.ts +28 -3
  91. package/src/activities/image-generation-references.ts +9 -9
  92. package/src/activities/mcp-credentials.ts +53 -0
  93. package/src/activities/rig-verification.ts +5 -3
  94. package/src/activities/sandbox-lease.ts +29 -12
  95. package/src/activities/scheduled-tasks.ts +217 -110
  96. package/src/activities/session-state.ts +17 -0
  97. package/src/activities/site-auth-maintenance.ts +12 -12
  98. package/src/activities/types.ts +22 -8
  99. package/src/activities.ts +2 -0
  100. package/src/activity-services.ts +4 -1
  101. package/src/github-rest-mcp.ts +2 -83
  102. package/src/index.ts +34 -4
  103. package/src/observability-metrics.ts +158 -14
  104. package/src/sandbox-reaper-timeout.ts +10 -4
  105. package/src/sandbox-resume.ts +58 -8
  106. package/src/sandbox-routing.ts +71 -16
  107. package/src/workflows/activities.ts +16 -0
  108. package/src/workflows/automations.ts +21 -2
  109. package/src/workflows/session.ts +85 -7
  110. package/dist/activities/api-integrations.d.ts +0 -21
  111. package/dist/activities-control-52W5IDWM.js.map +0 -1
  112. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  113. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  114. package/dist/chunk-IPVMCAW5.js.map +0 -1
  115. package/src/activities/api-integrations.ts +0 -141
@@ -1,12 +1,13 @@
1
1
  import {
2
2
  observabilityEventLogger
3
- } from "./chunk-GFNXVZVQ.js";
3
+ } from "./chunk-D4ZEFOYH.js";
4
4
 
5
5
  // src/activity-services.ts
6
6
  import { dbSearchPath, getSettings, resolveNatsControlPlaneAuth } from "@opengeni/config";
7
7
  import { configureChildLifecycleNotices, createDb } from "@opengeni/db";
8
8
  import { createNatsEventBus } from "@opengeni/events";
9
9
  import { createObservability } from "@opengeni/observability";
10
+ import { resolveCatalogSettings } from "@opengeni/core";
10
11
  import { createObjectStorage } from "@opengeni/storage";
11
12
 
12
13
  // src/personal-github-git-credentials.ts
@@ -400,9 +401,11 @@ function createSharedActivityServices(dependencies) {
400
401
  });
401
402
  const controlPlaneAuth = resolveNatsControlPlaneAuth(settings);
402
403
  const db = dependencies.db ?? dbClient.db;
404
+ const resolvedSettings = (await resolveCatalogSettings(db, settings)).settings;
403
405
  configureChildLifecycleNotices({ enabled: settings.childLifecycleNoticesEnabled });
404
406
  return {
405
- settings,
407
+ settings: resolvedSettings,
408
+ catalogSourceSettings: settings,
406
409
  db,
407
410
  bus: dependencies.bus ?? await createNatsEventBus(
408
411
  settings.natsUrl,
@@ -428,6 +431,7 @@ function createSharedActivityServices(dependencies) {
428
431
 
429
432
  // src/activities/codex-capacity.ts
430
433
  import {
434
+ armCodexCapacityWait,
431
435
  fetchCodexUsageForAccount,
432
436
  getCodexCapacityWaitForSession,
433
437
  getXaiCapacityWaitForSession,
@@ -436,6 +440,7 @@ import {
436
440
  reconcileCodexCapacityWait as reconcileCodexCapacityWaitDb,
437
441
  reconcileXaiCapacityWait as reconcileXaiCapacityWaitDb
438
442
  } from "@opengeni/db";
443
+ import { publishDurableSessionEvents } from "@opengeni/events";
439
444
 
440
445
  // src/activities/codex-rotation.ts
441
446
  function codexAccountNeedsLiveCapacityRefresh(account, now) {
@@ -502,9 +507,6 @@ function shardCredentialForSession(args) {
502
507
  const index = fnv1a32(sessionId) % eligibles.length;
503
508
  return eligibles[index].id;
504
509
  }
505
- function earliestCodexReset(accounts, now) {
506
- return earliestReset(accounts, now);
507
- }
508
510
  function chooseShardedHome(args) {
509
511
  const { sessionId, currentPolicyPin, accounts, now } = args;
510
512
  const pinRow = currentPolicyPin ? accounts.find((acct) => acct.id === currentPolicyPin) ?? null : null;
@@ -567,20 +569,6 @@ function computeIdleDelayMs(earliestResetAt, now, maxMs) {
567
569
  const delta = earliestResetAt.getTime() - now.getTime();
568
570
  return Math.min(Math.max(delta, MIN_IDLE_MS), maxMs);
569
571
  }
570
- var REACTIVE_PERSISTENCE_FAULT_FLOOR_MS = 5e3;
571
- var REACTIVE_ROTATION_MARGIN = 2;
572
- var REACTIVE_CIRCUIT_BREAKER_IDLE_MS = MIN_IDLE_MS;
573
- function computeReactiveRotationResume(args) {
574
- const streak = args.priorConsecutiveRotations + 1;
575
- const bound = args.connectedAccountCount + REACTIVE_ROTATION_MARGIN;
576
- if (streak > bound) {
577
- return { continueDelayMs: REACTIVE_CIRCUIT_BREAKER_IDLE_MS, idleUntilReset: true };
578
- }
579
- if (!args.cooldownPersisted) {
580
- return { continueDelayMs: REACTIVE_PERSISTENCE_FAULT_FLOOR_MS, idleUntilReset: false };
581
- }
582
- return { continueDelayMs: 0, idleUntilReset: false };
583
- }
584
572
  function chooseRotationActive(args) {
585
573
  const { activeCredentialId, priorCredentialId, accounts, now } = args;
586
574
  const rotationStrategy = effectiveRotationStrategy(args.rotationStrategy);
@@ -644,36 +632,12 @@ function chooseRotationActive(args) {
644
632
  }, void 0);
645
633
  return decide(chosen);
646
634
  }
647
- function chooseLegacyRotationActive(args) {
648
- if (args.rotationStrategy === "most_remaining") {
649
- const active = args.activeCredentialId ? args.accounts.find((account) => account.id === args.activeCredentialId) : void 0;
650
- if (active && isCodexCredentialEligible(active, args.now)) {
651
- return { kind: "active", credentialId: active.id, moved: false };
652
- }
653
- }
654
- return chooseRotationActive({
655
- ...args,
656
- accounts: args.accounts.map((account) => ({
657
- ...account,
658
- activeLeaseCount: 0,
659
- selectionCount: 0,
660
- lastSelectedAt: null
661
- }))
662
- });
663
- }
664
635
  function selectCodexCredentialLeaseForTurn(args) {
665
- const {
666
- accounts,
667
- activeCredentialId,
668
- rotationEnabled,
669
- leaseRotationEnabled,
670
- rotationStrategy,
671
- existingCredentialId
672
- } = args.context;
673
- const leasingEnabled = args.leasingEnabled && leaseRotationEnabled;
636
+ const { accounts, activeCredentialId, rotationEnabled, rotationStrategy, existingCredentialId } = args.context;
637
+ const failedCredentialIds = new Set(args.context.failedCredentialIds ?? []);
674
638
  const strategy = effectiveRotationStrategy(rotationStrategy);
675
639
  const existing = existingCredentialId ? accounts.find((account) => account.id === existingCredentialId) : void 0;
676
- if (existing && isCodexCredentialHealthy(existing, args.now)) {
640
+ if (existing && !failedCredentialIds.has(existing.id) && isCodexCredentialHealthy(existing, args.now)) {
677
641
  return {
678
642
  credentialId: existing.id,
679
643
  advanceActivePointer: false,
@@ -684,9 +648,6 @@ function selectCodexCredentialLeaseForTurn(args) {
684
648
  }
685
649
  };
686
650
  }
687
- const connectedIds = new Set(
688
- accounts.filter((account) => account.allocatorEnabled).map((account) => account.id)
689
- );
690
651
  const pinDisposition = classifyCodexPin({
691
652
  pinnedCredentialId: args.sessionPinnedCredentialId,
692
653
  pinSource: args.sessionPinSource,
@@ -695,9 +656,16 @@ function selectCodexCredentialLeaseForTurn(args) {
695
656
  });
696
657
  if (pinDisposition === "manual" && args.sessionPinnedCredentialId) {
697
658
  const pinned = accounts.find((account) => account.id === args.sessionPinnedCredentialId);
698
- if (!pinned?.allocatorEnabled) {
659
+ if (!pinned) {
699
660
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
700
661
  }
662
+ if (!pinned.allocatorEnabled) {
663
+ return {
664
+ credentialId: null,
665
+ decision: { kind: "allocatorDisabled", credentialId: pinned.id },
666
+ advanceActivePointer: false
667
+ };
668
+ }
701
669
  if (isCodexCredentialHealthy(pinned, args.now)) {
702
670
  return {
703
671
  credentialId: pinned.id,
@@ -715,13 +683,14 @@ function selectCodexCredentialLeaseForTurn(args) {
715
683
  };
716
684
  }
717
685
  if (pinDisposition === "sharded") {
718
- if (accounts.length === 0) {
686
+ const unattemptedAccounts2 = accounts.filter((account) => !failedCredentialIds.has(account.id));
687
+ if (unattemptedAccounts2.length === 0) {
719
688
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
720
689
  }
721
690
  const shard = chooseShardedHome({
722
691
  sessionId: args.sessionId,
723
692
  currentPolicyPin: args.sessionPinSource === "policy" ? args.sessionPinnedCredentialId : null,
724
- accounts,
693
+ accounts: unattemptedAccounts2,
725
694
  now: args.now
726
695
  });
727
696
  if (shard.kind === "allCapped") {
@@ -743,9 +712,16 @@ function selectCodexCredentialLeaseForTurn(args) {
743
712
  }
744
713
  if (!rotationEnabled) {
745
714
  const active = activeCredentialId ? accounts.find((account) => account.id === activeCredentialId) : void 0;
746
- if (!active?.allocatorEnabled) {
715
+ if (!active) {
747
716
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
748
717
  }
718
+ if (!active.allocatorEnabled) {
719
+ return {
720
+ credentialId: null,
721
+ decision: { kind: "allocatorDisabled", credentialId: active.id },
722
+ advanceActivePointer: false
723
+ };
724
+ }
749
725
  if (!isCodexCredentialHealthy(active, args.now)) {
750
726
  return {
751
727
  credentialId: null,
@@ -759,22 +735,15 @@ function selectCodexCredentialLeaseForTurn(args) {
759
735
  advanceActivePointer: false
760
736
  };
761
737
  }
762
- if (!leasingEnabled && pinDisposition !== "clearStale" && args.sessionPinnedCredentialId) {
763
- const credentialId = connectedIds.has(args.sessionPinnedCredentialId) ? args.sessionPinnedCredentialId : activeCredentialId && connectedIds.has(activeCredentialId) ? activeCredentialId : null;
764
- return {
765
- credentialId,
766
- decision: credentialId ? { kind: "active", credentialId, moved: false } : { kind: "none" }
767
- };
768
- }
769
738
  const priorId = args.sessionLastCredentialId ?? activeCredentialId;
770
- const choose = leasingEnabled ? chooseRotationActive : chooseLegacyRotationActive;
771
- const decision = choose({
739
+ const unattemptedAccounts = accounts.filter((account) => !failedCredentialIds.has(account.id));
740
+ const decision = chooseRotationActive({
772
741
  rotationStrategy: strategy,
773
742
  activeCredentialId,
774
743
  // Per-session continuity is the round-robin/drain cursor. Falling back to
775
744
  // the workspace pointer is only correct when this session has never run.
776
745
  priorCredentialId: priorId,
777
- accounts,
746
+ accounts: unattemptedAccounts,
778
747
  now: args.now
779
748
  });
780
749
  return {
@@ -813,11 +782,10 @@ async function signalPendingCodexCapacityWakeTargets(services, workspaceId) {
813
782
  );
814
783
  await signalCodexCapacityWakeTargets(services, targets);
815
784
  }
816
- function codexCapacityDecision(context, settings) {
785
+ function codexCapacityDecision(context) {
817
786
  const now = /* @__PURE__ */ new Date();
818
787
  const selected = selectCodexCredentialLeaseForTurn({
819
788
  context,
820
- leasingEnabled: settings.codexCredentialLeasingEnabled,
821
789
  sessionId: context.sessionId,
822
790
  sessionPinnedCredentialId: context.sessionPinnedCredentialId,
823
791
  sessionPinSource: context.sessionPinSource,
@@ -836,21 +804,54 @@ function codexCapacityDecision(context, settings) {
836
804
  }
837
805
  };
838
806
  }
839
- const authoritativeReset = authoritativeCodexCapacityResetAt(context.accounts, now);
840
- const hasReconcilableQuotaCooldown = context.accounts.some(
807
+ const policyCredentialId = context.sessionPinSource === "manual" && context.sessionPinnedCredentialId ? context.sessionPinnedCredentialId : !context.rotationEnabled ? context.activeCredentialId : null;
808
+ const capacityAccounts = policyCredentialId ? context.accounts.filter((account) => account.id === policyCredentialId) : context.accounts;
809
+ const authoritativeReset = authoritativeCodexCapacityResetAt(capacityAccounts, now);
810
+ const hasReconcilableQuotaCooldown = capacityAccounts.some(
841
811
  (account) => account.status === "active" && account.allocatorEnabled && account.exhaustedKind === "quota" && account.exhaustedUntil !== null && account.exhaustedUntil > now
842
812
  );
813
+ const policyAccount = capacityAccounts[0] ?? null;
814
+ const mutationOnlyStatusBlock = policyAccount != null && (!policyAccount.allocatorEnabled || policyAccount.status !== "active") || authoritativeReset === null && capacityAccounts.length > 0 && capacityAccounts.every(
815
+ (account) => !account.allocatorEnabled || account.status !== "active"
816
+ );
817
+ const noneReason = selected.decision.kind === "none" ? context.sessionPinSource === "manual" && context.sessionPinnedCredentialId !== null ? "manual_pin_missing" : !context.rotationEnabled && context.activeCredentialId === null ? "rotation_off_active_pointer_missing" : context.policyScope !== null && context.accounts.length === 0 ? "policy_filtered_pool_empty" : context.accounts.length === 0 ? "no_connected_credentials" : "no_eligible_credential" : null;
843
818
  return {
844
819
  kind: "unavailable",
845
820
  earliestResetAt: authoritativeReset,
846
- resetKind: authoritativeReset && !hasReconcilableQuotaCooldown ? "authoritative" : "bounded_refresh",
821
+ resetKind: selected.decision.kind === "none" || selected.decision.kind === "allocatorDisabled" || mutationOnlyStatusBlock ? "mutation_only" : authoritativeReset && !hasReconcilableQuotaCooldown ? "authoritative" : "bounded_refresh",
847
822
  diagnostic: {
848
823
  connectedCount: context.accounts.length,
849
824
  allocatorEnabledCount: context.accounts.filter((account) => account.allocatorEnabled).length,
850
- policyHash: context.policyHash
825
+ policyHash: context.policyHash,
826
+ ...noneReason ? { reason: noneReason } : {}
851
827
  }
852
828
  };
853
829
  }
830
+ async function armAndReconcileCodexCapacityWait(services, input, options = {}) {
831
+ const armed = await armCodexCapacityWait(services.db, input);
832
+ if (armed.action !== "waiting") return armed;
833
+ options.onArmed?.();
834
+ await publishDurableSessionEvents(services.bus, input.workspaceId, input.sessionId, armed.events);
835
+ const evaluated = await reconcileCodexCapacityWaitDb(
836
+ services.db,
837
+ {
838
+ accountId: input.accountId,
839
+ workspaceId: input.workspaceId,
840
+ sessionId: input.sessionId,
841
+ waiterId: armed.waiter.id,
842
+ generation: armed.waiter.generation,
843
+ ...input.now ? { now: input.now } : {}
844
+ },
845
+ (context) => codexCapacityDecision(context)
846
+ );
847
+ await publishDurableSessionEvents(
848
+ services.bus,
849
+ input.workspaceId,
850
+ input.sessionId,
851
+ evaluated.events
852
+ );
853
+ return evaluated;
854
+ }
854
855
  async function refreshCapacityMetadata(services, workspaceId) {
855
856
  const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
856
857
  const now = /* @__PURE__ */ new Date();
@@ -931,7 +932,8 @@ function createCodexCapacityActivities(services) {
931
932
  if (!current || current.id !== input.waiterId || current.generation !== input.generation) {
932
933
  return { action: "stale" };
933
934
  }
934
- if (input.cause === "timer" && current.nextCheckAt.getTime() <= Date.now()) {
935
+ const boundedRefreshAttempted = current.resetKind === "bounded_refresh" && input.cause === "timer" && current.nextCheckAt.getTime() <= Date.now();
936
+ if (boundedRefreshAttempted) {
935
937
  await refreshCapacityMetadata(resolved, input.workspaceId);
936
938
  }
937
939
  const result = await reconcileCodexCapacityWaitDb(
@@ -941,9 +943,10 @@ function createCodexCapacityActivities(services) {
941
943
  workspaceId: input.workspaceId,
942
944
  sessionId: input.sessionId,
943
945
  waiterId: input.waiterId,
944
- generation: input.generation
946
+ generation: input.generation,
947
+ boundedRefreshAttempted
945
948
  },
946
- (context) => codexCapacityDecision(context, resolved.settings)
949
+ (context) => codexCapacityDecision(context)
947
950
  );
948
951
  if (result.events.length > 0) {
949
952
  try {
@@ -972,7 +975,13 @@ function createCodexCapacityActivities(services) {
972
975
  import {
973
976
  allowedFirstPartyMcpToolsForSession,
974
977
  configuredStaticUsageLimits,
975
- policyProviderIdForModel
978
+ policyProviderIdForModel,
979
+ resolveModelProvider,
980
+ resolveTurnExecutionPolicyV1,
981
+ withCodexCatalogProvider,
982
+ withXaiSubscriptionCatalogProvider,
983
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
984
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX
976
985
  } from "@opengeni/config";
977
986
  import {
978
987
  evaluateWorkspaceModelPolicy,
@@ -990,6 +999,11 @@ import {
990
999
  requireSession,
991
1000
  sumUsageQuantity
992
1001
  } from "@opengeni/db";
1002
+ import {
1003
+ modelFundingForAdmission,
1004
+ resolveCatalogSettings as resolveCatalogSettings2,
1005
+ resolveWorkspaceCatalogSettings
1006
+ } from "@opengeni/core";
993
1007
  function createGoalActivities(services) {
994
1008
  async function enqueueGoalRetryWake(input) {
995
1009
  const { db } = await services();
@@ -1005,35 +1019,40 @@ function createGoalActivities(services) {
1005
1019
  });
1006
1020
  }
1007
1021
  async function maybeContinueGoal(input) {
1008
- const { settings, db, bus } = await services();
1022
+ const service = await services();
1023
+ const { db, bus } = service;
1024
+ const catalogSourceSettings = service.catalogSourceSettings ?? service.settings;
1009
1025
  const existingGoal = await getSessionGoal(db, input.workspaceId, input.sessionId);
1010
1026
  if (!existingGoal || existingGoal.status !== "active") {
1011
1027
  return { action: "none" };
1012
1028
  }
1029
+ let settings = (await resolveCatalogSettings2(db, catalogSourceSettings)).settings;
1013
1030
  const session = await requireSession(db, input.workspaceId, input.sessionId);
1014
1031
  const latestStartedTurn = await getLatestStartedSessionTurn(
1015
1032
  db,
1016
1033
  input.workspaceId,
1017
1034
  input.sessionId
1018
1035
  );
1019
- let continuationModel = latestStartedTurn?.model ?? session.model;
1036
+ const inheritedContinuationModel = latestStartedTurn?.model ?? session.model;
1037
+ let continuationModel = inheritedContinuationModel;
1020
1038
  const continuationReasoningEffort = latestStartedTurn?.reasoningEffort ?? session.reasoningEffort;
1021
1039
  const continuationLatencyMode = latestStartedTurn?.latencyMode ?? session.latencyMode;
1022
- let modelPolicyBlocked = null;
1023
1040
  const workspaceModelPolicy = await getWorkspaceModelPolicy(db, input.workspaceId);
1024
- if (workspaceModelPolicy) {
1025
- const policyBlocks = (modelId) => !evaluateWorkspaceModelPolicy(workspaceModelPolicy, {
1026
- providerId: policyProviderIdForModel(settings, modelId),
1027
- modelId
1028
- }).allowed;
1029
- if (policyBlocks(continuationModel)) {
1030
- if (continuationModel !== session.model && !policyBlocks(session.model)) {
1031
- continuationModel = session.model;
1032
- } else {
1033
- modelPolicyBlocked = `workspace model policy blocks model "${continuationModel}"; pick an allowed model or change the workspace model policy`;
1034
- }
1035
- }
1041
+ if (inheritedContinuationModel.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) || inheritedContinuationModel.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX) || session.model.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) || session.model.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX)) {
1042
+ settings = (await resolveWorkspaceCatalogSettings(db, catalogSourceSettings, {
1043
+ accountId: input.accountId,
1044
+ workspaceId: input.workspaceId,
1045
+ retainedProductModelIds: [inheritedContinuationModel, session.model]
1046
+ })).settings;
1036
1047
  }
1048
+ const modelDecision = goalContinuationModelDecision({
1049
+ settings,
1050
+ workspaceModelPolicy,
1051
+ inheritedModel: inheritedContinuationModel,
1052
+ sessionModel: session.model
1053
+ });
1054
+ continuationModel = modelDecision.model;
1055
+ let modelPolicyBlocked = modelDecision.blocked;
1037
1056
  if (!modelPolicyBlocked && session.codexCompactionMode === "remote_v2" && !isCodexBilledModel(continuationModel)) {
1038
1057
  modelPolicyBlocked = `session is locked to Codex remote compaction v2; model "${continuationModel}" is not a Codex subscription model`;
1039
1058
  }
@@ -1043,13 +1062,27 @@ function createGoalActivities(services) {
1043
1062
  workspaceId: input.workspaceId,
1044
1063
  model: continuationModel
1045
1064
  });
1065
+ const fundedWithoutCredits = goalContinuationFundedWithoutCredits(
1066
+ settings,
1067
+ continuationModel,
1068
+ isCodexRun
1069
+ );
1046
1070
  const budgetBlocked = await goalRunBudgetBlocked(
1047
1071
  settings,
1048
1072
  db,
1049
1073
  input.accountId,
1050
1074
  input.workspaceId,
1051
- isCodexRun
1075
+ fundedWithoutCredits
1052
1076
  );
1077
+ const turnExecutionPolicy = resolveTurnExecutionPolicyV1(settings, {
1078
+ modelId: continuationModel,
1079
+ requestedModelId: null,
1080
+ modelSource: "continuation",
1081
+ reasoningEffort: continuationReasoningEffort,
1082
+ reasoningSource: "continuation",
1083
+ latencyMode: continuationLatencyMode,
1084
+ latencyModeSource: "continuation"
1085
+ });
1053
1086
  const decision = await materializeGoalContinuation(db, {
1054
1087
  accountId: input.accountId,
1055
1088
  workspaceId: input.workspaceId,
@@ -1070,10 +1103,11 @@ function createGoalActivities(services) {
1070
1103
  model: continuationModel,
1071
1104
  reasoningEffort: continuationReasoningEffort,
1072
1105
  latencyMode: continuationLatencyMode,
1106
+ turnExecutionPolicy,
1073
1107
  tools: withFirstPartyTools(settings, session.tools),
1074
1108
  sandboxBackend: session.sandboxBackend
1075
1109
  },
1076
- // The hold guidance is only given when `goal_wait` is actually in this
1110
+ // Long-wait guidance is only given when `wait_for_input` is actually in this
1077
1111
  // session's effective first-party selection (the same source the worker
1078
1112
  // signs into the delegated token and the API uses to register tools), so
1079
1113
  // a pre-existing narrowed selection is never told to call a missing tool.
@@ -1083,7 +1117,7 @@ function createGoalActivities(services) {
1083
1117
  session.firstPartyMcpTools
1084
1118
  );
1085
1119
  return goalContinuationPrompt(goal, autoContinuation, cap, {
1086
- goalWaitAvailable: effectiveFirstPartyTools.includes("goal_wait"),
1120
+ inputWaitAvailable: effectiveFirstPartyTools.includes("wait_for_input"),
1087
1121
  humanInputRespondAvailable: effectiveFirstPartyTools.includes(
1088
1122
  "session_human_input_respond"
1089
1123
  )
@@ -1100,12 +1134,41 @@ function createGoalActivities(services) {
1100
1134
  maybeContinueGoal
1101
1135
  };
1102
1136
  }
1137
+ function goalContinuationModelDecision(input) {
1138
+ const catalogSettings = input.settings.supergrokSubscriptionEnabled ? withXaiSubscriptionCatalogProvider(
1139
+ input.settings.codexSubscriptionEnabled ? withCodexCatalogProvider(input.settings) : input.settings
1140
+ ) : input.settings.codexSubscriptionEnabled ? withCodexCatalogProvider(input.settings) : input.settings;
1141
+ const policyBlocks = (modelId) => input.workspaceModelPolicy !== null && !evaluateWorkspaceModelPolicy(input.workspaceModelPolicy, {
1142
+ providerId: policyProviderIdForModel(catalogSettings, modelId),
1143
+ modelId
1144
+ }).allowed;
1145
+ const candidates = [.../* @__PURE__ */ new Set([input.inheritedModel, input.sessionModel])];
1146
+ for (const model of candidates) {
1147
+ if (resolveModelProvider(catalogSettings, model) && !policyBlocks(model)) {
1148
+ return { model, blocked: null };
1149
+ }
1150
+ }
1151
+ if (!resolveModelProvider(catalogSettings, input.inheritedModel)) {
1152
+ return {
1153
+ model: input.inheritedModel,
1154
+ blocked: `model "${input.inheritedModel}" is no longer in the deployment or workspace catalog; choose an available model before resuming the goal`
1155
+ };
1156
+ }
1157
+ return {
1158
+ model: input.inheritedModel,
1159
+ blocked: `workspace model policy blocks model "${input.inheritedModel}"; pick an allowed model or change the workspace model policy`
1160
+ };
1161
+ }
1162
+ function goalContinuationFundedWithoutCredits(settings, model, codexBilled) {
1163
+ return modelFundingForAdmission(settings, model, codexBilled).fundedWithoutCredits;
1164
+ }
1103
1165
  function goalContinuationPrompt(_goal, _autoContinuation, _cap, options = {}) {
1104
- const waitingGuidance = options.goalWaitAvailable ? [
1105
- "Waiting on child sessions or external events:",
1106
- "- When the next progress depends on child sessions you spawned or on an external event, do not sleep, loop, or poll sessions_list/session_get/session_events to wait for it.",
1107
- "- Re-check sessions_list or session_get once; if the work is still in flight, call opengeni__goal_wait with a concrete reason and a deadline (untilSeconds), then end your turn immediately. You will be woken by a child result, a message, a human prompt, or at the deadline, and this goal stays active.",
1108
- "- A hold is for child/external progress only. If you are blocked on a human decision, use opengeni__goal_pause under the blocked audit below instead.",
1166
+ const waitingGuidance = options.inputWaitAvailable ? [
1167
+ "Waiting on child sessions, background commands, or external events:",
1168
+ "- When further progress depends on work already in flight, do not sleep, loop, or poll session or command state repeatedly.",
1169
+ "- Re-check once; if the work is still in flight and the wait is long or uncertain, call opengeni__wait_for_input with a concrete reason and timeoutSeconds, then end your turn immediately. Relevant session input or the safety deadline will start a new turn, and this goal stays active.",
1170
+ "- If the immediately preceding user-facing update already reported this same unchanged wait, do not restate it or produce another equivalent final answer. Call opengeni__wait_for_input and end the turn. Report only material new state or a newly discovered blocker.",
1171
+ "- If you are blocked on a human decision, use opengeni__goal_pause under the blocked audit below instead.",
1109
1172
  ""
1110
1173
  ] : [];
1111
1174
  const childNoticeGuidance = [
@@ -1166,8 +1229,8 @@ function withFirstPartyTools(settings, tools) {
1166
1229
  }
1167
1230
  return mergeToolRefs(tools, [{ kind: "mcp", id: "opengeni" }]);
1168
1231
  }
1169
- async function goalRunBudgetBlocked(settings, db, accountId, workspaceId, isCodexRun) {
1170
- if (!isCodexRun && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
1232
+ async function goalRunBudgetBlocked(settings, db, accountId, workspaceId, fundedWithoutCredits) {
1233
+ if (!fundedWithoutCredits && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
1171
1234
  const balance = await getBillingBalance(db, accountId);
1172
1235
  if (balance.balanceMicros <= 0) {
1173
1236
  return "insufficient OpenGeni credits";
@@ -1175,7 +1238,7 @@ async function goalRunBudgetBlocked(settings, db, accountId, workspaceId, isCode
1175
1238
  }
1176
1239
  if (settings.usageLimitsMode === "static" || settings.usageLimitsMode === "managed") {
1177
1240
  const limits = configuredStaticUsageLimits(settings);
1178
- if (!isCodexRun && limits.maxMonthlyCostMicrosPerAccount) {
1241
+ if (!fundedWithoutCredits && limits.maxMonthlyCostMicrosPerAccount) {
1179
1242
  const used = await sumUsageQuantity(db, {
1180
1243
  accountId,
1181
1244
  eventType: "model.cost",
@@ -2662,49 +2725,35 @@ function legacyNativeArchiveFromEnvelope(envelope) {
2662
2725
  existing?.version === 1 ? Date.parse(existing.capturedAt) : Date.now()
2663
2726
  );
2664
2727
  }
2665
- async function persistSandboxDeadlineRotationCheckpoint(services, ids, session, expected, signal) {
2666
- const { db, settings } = services;
2667
- if (signal?.aborted) {
2668
- return { checkpointed: false, holder: "attempt_fenced", lease: null };
2669
- }
2670
- const reinstated = await reinstateTurnLeaseHolder(db, {
2671
- accountId: ids.accountId,
2672
- workspaceId: ids.workspaceId,
2673
- sandboxGroupId: ids.sandboxGroupId,
2674
- sessionId: ids.sessionId,
2675
- attemptId: ids.attemptId,
2676
- expectedEpoch: expected.leaseEpoch,
2677
- expectedInstanceId: expected.instanceId,
2678
- leaseTtlMs: settings.sandboxLeaseTtlMs
2728
+ function maybePersistWarmWorkspaceSnapshot(services, ids, session, leaseEpoch, signal, force = false) {
2729
+ let continuationRegistered = false;
2730
+ let resolvePhysicalSettlement = () => void 0;
2731
+ const physicalSettlement = new Promise((resolve) => {
2732
+ resolvePhysicalSettlement = resolve;
2679
2733
  });
2680
- if (reinstated.status === "reinstated") {
2681
- const message = "sandbox rotation checkpoint reinstated the live turn holder";
2682
- const fields = {
2683
- workspaceId: ids.workspaceId,
2684
- sessionId: ids.sessionId,
2685
- sandboxGroupId: ids.sandboxGroupId,
2686
- leaseEpoch: expected.leaseEpoch,
2687
- liveness: reinstated.lease.liveness
2688
- };
2689
- if (services.observability) services.observability.warn(message, fields);
2690
- else console.warn(message, fields);
2691
- }
2692
- if (reinstated.status !== "present" && reinstated.status !== "reinstated") {
2693
- return { checkpointed: false, holder: reinstated.status, lease: null };
2694
- }
2695
- await maybePersistWarmWorkspaceSnapshot(
2734
+ const result = persistWarmWorkspaceSnapshot(
2696
2735
  services,
2697
2736
  ids,
2698
2737
  session,
2699
- expected.leaseEpoch,
2738
+ leaseEpoch,
2700
2739
  signal,
2701
- true
2740
+ force,
2741
+ (continuation) => {
2742
+ continuationRegistered = true;
2743
+ void continuation.then(resolvePhysicalSettlement, resolvePhysicalSettlement);
2744
+ }
2702
2745
  );
2703
- const lease = await readLease(db, ids.workspaceId, ids.sandboxGroupId);
2704
- const checkpointed = lease?.leaseEpoch === expected.leaseEpoch && lease.instanceId === expected.instanceId && lease.rotationRequestedAt !== null && lease.archiveComplete && signal?.aborted !== true;
2705
- return { checkpointed, holder: reinstated.status, lease };
2746
+ void result.then(
2747
+ () => {
2748
+ if (!continuationRegistered) resolvePhysicalSettlement();
2749
+ },
2750
+ () => {
2751
+ if (!continuationRegistered) resolvePhysicalSettlement();
2752
+ }
2753
+ );
2754
+ return Object.assign(result, { settled: physicalSettlement });
2706
2755
  }
2707
- async function maybePersistWarmWorkspaceSnapshot(services, ids, session, leaseEpoch, signal, force = false) {
2756
+ async function persistWarmWorkspaceSnapshot(services, ids, session, leaseEpoch, signal, force, registerOwnedContinuation) {
2708
2757
  const { db, settings } = services;
2709
2758
  const intervalMs = settings.sandboxSnapshotIntervalMs;
2710
2759
  if (intervalMs <= 0 && !force) {
@@ -2916,6 +2965,7 @@ async function maybePersistWarmWorkspaceSnapshot(services, ids, session, leaseEp
2916
2965
  return { kind: "settled", persisted: false };
2917
2966
  }
2918
2967
  );
2968
+ registerOwnedContinuation(settled.then(() => void 0));
2919
2969
  let cancelListener;
2920
2970
  const outcome = await Promise.race([
2921
2971
  settled,
@@ -3931,10 +3981,10 @@ function createTurnSandboxProvisioner(establish, options = {}) {
3931
3981
 
3932
3982
  // src/activities/agent-turn/quiescence.ts
3933
3983
  function shouldRunTurnEndWorkspacePersistence(input) {
3934
- return input.activityStatus !== "cancelled" && !input.cancellationRequested;
3984
+ return input.activityStatus !== "cancelled" && !input.cancellationRequested && input.deadlineRotationRequested !== true;
3935
3985
  }
3936
3986
  function shouldStartPeriodicWorkspaceSnapshot(input) {
3937
- return input.firstProviderRequestStarted && !input.snapshotInFlight && !input.turnEndCaptureInProgress;
3987
+ return input.firstProviderRequestStarted && !input.snapshotInFlight && !input.turnEndCaptureInProgress && !input.deadlineRotationRequested;
3938
3988
  }
3939
3989
  function assertPhysicalToolQuiescenceForCancellation(input) {
3940
3990
  if (!input.acknowledgeQuiescence || input.physicalToolQuiescenceConfirmed) return;
@@ -3955,6 +4005,7 @@ function assertSessionAttemptQuiescenceRecoveryDurable(input) {
3955
4005
  var QUIESCENCE_PROOF_SIGNAL_INITIAL_RETRY_MS = 250;
3956
4006
  var QUIESCENCE_PROOF_SIGNAL_MAX_RETRY_MS = 5e3;
3957
4007
  var TURN_QUIESCENCE_WATCHDOG_MS = 5 * 6e4;
4008
+ var TURN_FINALIZER_STEP_TIMEOUT_MS = 3e4;
3958
4009
  function armTurnQuiescenceWatchdog(input) {
3959
4010
  if (!input.enabled) return () => void 0;
3960
4011
  let armed = true;
@@ -4043,25 +4094,27 @@ async function releaseTurnSandboxAfterWriterDrain(sandbox, options = {}) {
4043
4094
  }
4044
4095
  throw lastError;
4045
4096
  }
4046
- async function waitForTurnFinalizerStep(operation, signal) {
4047
- if (!signal) return await operation;
4048
- if (signal.aborted) {
4097
+ async function waitForTurnFinalizerStep(operation, signal, timeoutMs = TURN_FINALIZER_STEP_TIMEOUT_MS) {
4098
+ if (signal?.aborted) {
4049
4099
  void operation.catch(() => void 0);
4050
4100
  return void 0;
4051
4101
  }
4052
- let resolveCancellation;
4053
- const cancelled = new Promise((resolve) => {
4054
- resolveCancellation = () => resolve(void 0);
4102
+ let resolveDetached;
4103
+ const detached = new Promise((resolve) => {
4104
+ resolveDetached = () => resolve(void 0);
4055
4105
  });
4056
- const cancel = () => {
4106
+ const detach = () => {
4057
4107
  void operation.catch(() => void 0);
4058
- resolveCancellation?.();
4108
+ resolveDetached?.();
4059
4109
  };
4060
- signal.addEventListener("abort", cancel, { once: true });
4110
+ signal?.addEventListener("abort", detach, { once: true });
4111
+ const timeout = setTimeout(detach, Math.max(1, timeoutMs));
4112
+ timeout.unref?.();
4061
4113
  try {
4062
- return await Promise.race([operation, cancelled]);
4114
+ return await Promise.race([operation, detached]);
4063
4115
  } finally {
4064
- signal.removeEventListener("abort", cancel);
4116
+ clearTimeout(timeout);
4117
+ signal?.removeEventListener("abort", detach);
4065
4118
  }
4066
4119
  }
4067
4120
  async function waitForTurnStreamCleanup(batcherFlush, providerCompleted, signal) {
@@ -4518,7 +4571,18 @@ var STATUSLESS_UPSTREAM_CONNECTIVITY_MESSAGE = "unable to connect. is the comput
4518
4571
  function isExactStatuslessUpstreamConnectivityMessage(message) {
4519
4572
  return message.trim().toLowerCase() === STATUSLESS_UPSTREAM_CONNECTIVITY_MESSAGE;
4520
4573
  }
4574
+ function providerSafetyRefusalDiagnostic(error) {
4575
+ return collectErrorStrings(error).find(
4576
+ (value) => /^(?:content_policy_violation|content_filter|safety_violation|bio_policy|cyber_policy)$/.test(
4577
+ value
4578
+ ) || /\bthis request was blocked by our safety systems\b/i.test(value)
4579
+ );
4580
+ }
4581
+ function isProviderSafetyRefusal(error) {
4582
+ return providerSafetyRefusalDiagnostic(error) !== void 0;
4583
+ }
4521
4584
  function isTransientProviderError(error) {
4585
+ if (isProviderSafetyRefusal(error)) return false;
4522
4586
  const status = typeof error === "object" && error !== null ? Number(
4523
4587
  error.status ?? error.statusCode
4524
4588
  ) : void 0;
@@ -4538,6 +4602,7 @@ function isTransientProviderError(error) {
4538
4602
  );
4539
4603
  }
4540
4604
  function classifyXaiCredentialFailure(error) {
4605
+ if (isProviderSafetyRefusal(error)) return null;
4541
4606
  let relogin = error;
4542
4607
  for (let depth = 0; depth < 6 && relogin && typeof relogin === "object"; depth += 1) {
4543
4608
  if (relogin instanceof XaiSubscriptionReloginRequired) {
@@ -4583,6 +4648,15 @@ function agentRunFailurePayload(error, options = {}) {
4583
4648
  historyPersistenceStage: error.stage
4584
4649
  };
4585
4650
  }
4651
+ const safetyRefusalDiagnostic = providerSafetyRefusalDiagnostic(error);
4652
+ if (safetyRefusalDiagnostic !== void 0) {
4653
+ return {
4654
+ error: "The model provider blocked this request through its safety systems. Automatic retries stopped.",
4655
+ code: "provider_safety_refusal",
4656
+ retryable: false,
4657
+ detail: safetyRefusalDiagnostic
4658
+ };
4659
+ }
4586
4660
  const message = error instanceof Error ? error.message : String(error);
4587
4661
  const status = typeof error === "object" && error !== null ? Number(
4588
4662
  error.status ?? error.statusCode
@@ -4759,6 +4833,7 @@ function codexCredentialCooldownUntil(failure, account, now) {
4759
4833
  );
4760
4834
  }
4761
4835
  function classifyCodexCredentialFailure(error) {
4836
+ if (isProviderSafetyRefusal(error)) return null;
4762
4837
  let refreshError = error;
4763
4838
  for (let depth = 0; depth < 6 && refreshError && typeof refreshError === "object"; depth += 1) {
4764
4839
  if (refreshError instanceof CodexReloginRequired) {
@@ -4834,16 +4909,13 @@ export {
4834
4909
  codexAccountNeedsLiveCapacityRefresh,
4835
4910
  classifyCodexPin,
4836
4911
  isCodexCredentialEligible,
4837
- shardCredentialForSession,
4838
- earliestCodexReset,
4839
4912
  authoritativeCodexCapacityResetAt,
4840
4913
  computeIdleDelayMs,
4841
- computeReactiveRotationResume,
4842
- chooseRotationActive,
4843
4914
  selectCodexCredentialLeaseForTurn,
4844
4915
  refreshCodexUsageAndRepairCapacityWaiters,
4845
4916
  signalCodexCapacityWakeTargets,
4846
4917
  signalPendingCodexCapacityWakeTargets,
4918
+ armAndReconcileCodexCapacityWait,
4847
4919
  createCodexCapacityActivities,
4848
4920
  createGoalActivities,
4849
4921
  withFirstPartyTools,
@@ -4877,7 +4949,6 @@ export {
4877
4949
  validateIncidentTelemetrySystemUpdateAuthority,
4878
4950
  sandboxLeaseHolderIdForAttempt,
4879
4951
  waitForWarmSnapshot,
4880
- persistSandboxDeadlineRotationCheckpoint,
4881
4952
  maybePersistWarmWorkspaceSnapshot,
4882
4953
  resumeBoxForTurn,
4883
4954
  SandboxDeadlineRotationError,
@@ -4932,4 +5003,4 @@ export {
4932
5003
  codexUsageLimitFailurePayload,
4933
5004
  CODEX_USAGE_LIMIT_MAX_RESUME_MS
4934
5005
  };
4935
- //# sourceMappingURL=chunk-IPVMCAW5.js.map
5006
+ //# sourceMappingURL=chunk-TAUE3GYT.js.map