@opengeni/worker-bundle 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -520,7 +520,7 @@ async function sandboxEnvironmentForRun(settings, resources, workspaceEnvironmen
520
520
  const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
521
521
  const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
522
522
  let toolspaceToken;
523
- if (settings.toolspaceEnabled && !options.skipToolspaceToken && settings.delegationSecret && options.scope && options.sessionId && options.runId) {
523
+ if (settings.toolspaceEnabled && settings.delegationSecret && options.scope && options.sessionId && options.runId) {
524
524
  toolspaceToken = await signDelegatedAccessToken(settings.delegationSecret, {
525
525
  accountId: options.scope.accountId,
526
526
  workspaceId: options.scope.workspaceId,
@@ -1710,6 +1710,7 @@ function routingEnabled(settings) {
1710
1710
 
1711
1711
  // src/observability-metrics.ts
1712
1712
  var turnTrackers = /* @__PURE__ */ new WeakMap();
1713
+ var creditBalanceGaugeAccounts = /* @__PURE__ */ new WeakMap();
1713
1714
  function observabilityEventLogger(observability) {
1714
1715
  return {
1715
1716
  debug: (message, attributes) => observability.debug(message, eventAttributes(attributes)),
@@ -1881,6 +1882,30 @@ function recordSandboxLeaseGauges(observability, counts) {
1881
1882
  });
1882
1883
  }
1883
1884
  }
1885
+ function recordCreditBalanceGauges(observability, balances) {
1886
+ const previous = creditBalanceGaugeAccounts.get(observability) ?? /* @__PURE__ */ new Set();
1887
+ const current = /* @__PURE__ */ new Set();
1888
+ for (const balance of balances) {
1889
+ current.add(balance.accountId);
1890
+ observability.setGauge({
1891
+ name: "opengeni_credit_balance_micros",
1892
+ help: "Current credit balance in micros by account.",
1893
+ labels: { account_id: balance.accountId },
1894
+ value: balance.balanceMicros
1895
+ });
1896
+ }
1897
+ for (const accountId of previous) {
1898
+ if (!current.has(accountId)) {
1899
+ observability.setGauge({
1900
+ name: "opengeni_credit_balance_micros",
1901
+ help: "Current credit balance in micros by account.",
1902
+ labels: { account_id: accountId },
1903
+ value: 0
1904
+ });
1905
+ }
1906
+ }
1907
+ creditBalanceGaugeAccounts.set(observability, current);
1908
+ }
1884
1909
  function recordSandboxOrphansTerminated(observability, count) {
1885
1910
  if (count <= 0) {
1886
1911
  return;
@@ -2492,7 +2517,6 @@ function createRunAgentTurnActivity(services) {
2492
2517
  workspaceEnvironment?.values ?? {},
2493
2518
  {
2494
2519
  skipGitHubToken: activeSandboxBackend === "selfhosted",
2495
- skipToolspaceToken: activeSandboxBackend === "selfhosted",
2496
2520
  scope: connectionScope,
2497
2521
  gitCredentials: connectionCredentials?.gitCredentials,
2498
2522
  sessionId: input.sessionId,
@@ -2677,7 +2701,14 @@ function createRunAgentTurnActivity(services) {
2677
2701
  // seeds it to the box's token file before the repository-clone runs; it never
2678
2702
  // touches the box/agent manifest env.
2679
2703
  ...activeSandboxBackend !== "selfhosted" && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {},
2680
- ...activeSandboxBackend !== "selfhosted" && sandboxToolspaceToken ? { toolspaceTokenSeed: sandboxToolspaceToken } : {},
2704
+ // Toolspace is delivered on EVERY backend including selfhosted. The git-
2705
+ // token skip does NOT transfer: that token is inert on a connected
2706
+ // machine (it uses its own git creds), but the toolspace token is the
2707
+ // machine's ONLY path to programmatic tool calling and grants no more
2708
+ // than toolspace:call for its own session (own-session-bound, turn TTL,
2709
+ // budgeted, approval-tools excluded). The runtime seeds it to the box's
2710
+ // token file over the same exec channel, off-manifest, on every backend.
2711
+ ...sandboxToolspaceToken ? { toolspaceTokenSeed: sandboxToolspaceToken } : {},
2681
2712
  ...activeSandboxBackend ? { activeSandboxBackend } : {},
2682
2713
  fileResourceDownloads,
2683
2714
  mcpServers: preparedTools.mcpServers,
@@ -3771,6 +3802,7 @@ import {
3771
3802
  countSandboxLeasesByLiveness,
3772
3803
  forceDrainOverLimitViewerOnlyBoxes,
3773
3804
  getBillingBalance as getBillingBalance4,
3805
+ listCreditBalancesByAccount,
3774
3806
  listLiveModalSandboxLeaseAttributions,
3775
3807
  listMeterableWarmLeases,
3776
3808
  persistDrainSnapshot,
@@ -3795,7 +3827,7 @@ function createSandboxLeaseActivities(services, options = {}) {
3795
3827
  if (!settings.sandboxOwnershipEnabled) {
3796
3828
  return { examined: 0, terminated: 0, skipped: 0, metered: 0, forceDrained: 0, modalOrphansTerminated: 0 };
3797
3829
  }
3798
- await refreshQueueAndLeaseGauges(db, observability);
3830
+ await refreshQueueLeaseAndCreditGauges(db, observability);
3799
3831
  const metered = await accrueWarmTick(db, settings, observability);
3800
3832
  const forceDrained = await forceDrainOverLimitWorkspaces(db, settings, metered.workspaceIds, observability);
3801
3833
  const drainable = await reapStaleLeaseHoldersGlobal(db, {
@@ -3831,7 +3863,7 @@ function createSandboxLeaseActivities(services, options = {}) {
3831
3863
  error: error instanceof Error ? error.message : String(error)
3832
3864
  });
3833
3865
  }
3834
- await refreshQueueAndLeaseGauges(db, observability);
3866
+ await refreshQueueLeaseAndCreditGauges(db, observability);
3835
3867
  if (drainable.length > 0 || metered.accrued > 0 || forceDrained > 0 || modalOrphansTerminated > 0) {
3836
3868
  observability.info("sandbox reaper swept", {
3837
3869
  drainable: drainable.length,
@@ -3884,7 +3916,7 @@ async function accrueWarmTick(db, settings, observability) {
3884
3916
  }
3885
3917
  return { accrued, workspaceIds };
3886
3918
  }
3887
- async function refreshQueueAndLeaseGauges(db, observability) {
3919
+ async function refreshQueueLeaseAndCreditGauges(db, observability) {
3888
3920
  try {
3889
3921
  recordTurnsQueuedGauge(observability, await countQueuedTurns(db));
3890
3922
  } catch (error) {
@@ -3899,6 +3931,13 @@ async function refreshQueueAndLeaseGauges(db, observability) {
3899
3931
  error: error instanceof Error ? error.message : String(error)
3900
3932
  });
3901
3933
  }
3934
+ try {
3935
+ recordCreditBalanceGauges(observability, await listCreditBalancesByAccount(db));
3936
+ } catch (error) {
3937
+ observability.warn("sandbox reaper: credit-balance gauge refresh failed", {
3938
+ error: error instanceof Error ? error.message : String(error)
3939
+ });
3940
+ }
3902
3941
  }
3903
3942
  async function forceDrainOverLimitWorkspaces(db, settings, workspaceIds, observability) {
3904
3943
  const enforceBalance = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";