@opengeni/worker-bundle 0.4.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "OpenGeni worker entry: runOpenGeniWorker/createOpenGeniWorker(deps) + the un-bundled Temporal workflow source tree. An engine-distribution surface — its runtime closure includes engine-internal packages and the workflow bundle ships as on-disk TS source.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -34,27 +34,23 @@
34
34
  "dev:watch": "bun --watch src/index.ts",
35
35
  "start": "bun src/index.ts",
36
36
  "typecheck": "tsc --noEmit",
37
- "build": "tsup"
37
+ "build": "tsup",
38
+ "prepublishOnly": "bash ../../scripts/prepublish-guard"
38
39
  },
39
40
  "dependencies": {
40
41
  "@opengeni/codex": "^0.2.1",
41
- "@opengeni/config": "^0.2.5",
42
- "@opengeni/contracts": "^0.7.0",
43
- "@opengeni/db": "^0.4.1",
44
- "@opengeni/documents": "^0.2.5",
45
- "@opengeni/events": "^0.2.5",
42
+ "@opengeni/config": "^0.3.0",
43
+ "@opengeni/contracts": "^0.9.0",
44
+ "@opengeni/core": "^0.4.4",
45
+ "@opengeni/db": "^0.6.0",
46
+ "@opengeni/documents": "^0.2.7",
47
+ "@opengeni/events": "^0.2.7",
46
48
  "@opengeni/observability": "^0.2.1",
47
- "@opengeni/runtime": "^0.3.1",
48
- "@opengeni/storage": "^0.2.5",
49
+ "@opengeni/runtime": "^0.5.0",
50
+ "@opengeni/storage": "^0.2.7",
49
51
  "@temporalio/activity": "^1.17.0",
50
52
  "@temporalio/client": "^1.17.0",
51
53
  "@temporalio/worker": "^1.17.0",
52
54
  "@temporalio/workflow": "^1.17.0"
53
- },
54
- "devDependencies": {
55
- "@opengeni/testing": "workspace:*",
56
- "postgres": "^3.4.9",
57
- "typescript": "^6.0.3",
58
- "tsup": "^8.5.0"
59
55
  }
60
56
  }
@@ -38,6 +38,7 @@ import {
38
38
  heartbeatLeaseHolder,
39
39
  accrueWarmSeconds,
40
40
  SandboxLeaseSupersededError,
41
+ buildConnectionTokenResolver,
41
42
  type AppendEventInput,
42
43
  } from "@opengeni/db";
43
44
  import { appendAndPublishEvents } from "@opengeni/events";
@@ -171,6 +172,13 @@ export function isWorkerShutdownCancellation(error: unknown): boolean {
171
172
 
172
173
  export const CONTEXT_WINDOW_OVERFLOW_RECOVERY_MESSAGE =
173
174
  "The conversation reached the model context compaction threshold; it has been compacted. Send a message to continue.";
175
+ const MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS = 3;
176
+
177
+ function compactionFailureReason(reason: string): string {
178
+ return reason.startsWith("compaction summarization failed:")
179
+ ? reason
180
+ : `compaction summarization failed: ${reason}`;
181
+ }
174
182
 
175
183
  export function classifyContextWindowOverflowError(error: unknown): { message: string; code?: string; detail?: string } | null {
176
184
  const fields = collectErrorStrings(error);
@@ -1009,7 +1017,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1009
1017
  // minted, the host `gitCredentials` provider may supply it; unset → self-mint
1010
1018
  // from settings. gitToken is undefined on the selfhosted skip path (the machine
1011
1019
  // uses its own git creds).
1012
- const { environment: sandboxEnvironment, gitToken: sandboxGitToken } = await sandboxEnvironmentForRun(
1020
+ const {
1021
+ environment: sandboxEnvironment,
1022
+ gitToken: sandboxGitToken,
1023
+ toolspaceToken: sandboxToolspaceToken,
1024
+ } = await sandboxEnvironmentForRun(
1013
1025
  runSettings,
1014
1026
  turnResources,
1015
1027
  workspaceEnvironment?.values ?? {},
@@ -1017,6 +1029,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1017
1029
  skipGitHubToken: activeSandboxBackend === "selfhosted",
1018
1030
  scope: connectionScope,
1019
1031
  gitCredentials: connectionCredentials?.gitCredentials,
1032
+ sessionId: input.sessionId,
1033
+ runId: turnId,
1020
1034
  },
1021
1035
  );
1022
1036
 
@@ -1251,12 +1265,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1251
1265
  // (initialize + tools/list) can resolve the per-workspace bearer from
1252
1266
  // codexRequestStorage (runtime/codexAppsMcpRequestInit). withCodex is the
1253
1267
  // identity on every non-codex turn, so this is a no-op for existing paths.
1268
+ const resolveCredential = buildConnectionTokenResolver(db, runSettings);
1254
1269
  preparedTools = await withCodex(() => runtime.prepareTools(runSettings, turnTools, {
1255
1270
  accountId: input.accountId,
1256
1271
  workspaceId: input.workspaceId,
1257
1272
  sessionId: input.sessionId,
1258
1273
  subjectId: "worker:first-party-mcp",
1259
1274
  subjectLabel: "OpenGeni worker",
1275
+ resolveCredential,
1276
+ onAuthNeeded: async (payload) => {
1277
+ await publish!([{ type: "tool.auth_needed", payload }], true);
1278
+ },
1260
1279
  // Manager-style sessions carry a creation-validated permission set
1261
1280
  // for their first-party MCP token; null keeps the fixed default.
1262
1281
  ...(session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {}),
@@ -1292,6 +1311,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1292
1311
  // seeds it to the box's token file before the repository-clone runs; it never
1293
1312
  // touches the box/agent manifest env.
1294
1313
  ...(activeSandboxBackend !== "selfhosted" && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {}),
1314
+ // Toolspace is delivered on EVERY backend including selfhosted. The git-
1315
+ // token skip does NOT transfer: that token is inert on a connected
1316
+ // machine (it uses its own git creds), but the toolspace token is the
1317
+ // machine's ONLY path to programmatic tool calling and grants no more
1318
+ // than toolspace:call for its own session (own-session-bound, turn TTL,
1319
+ // budgeted, approval-tools excluded). The runtime seeds it to the box's
1320
+ // token file over the same exec channel, off-manifest, on every backend.
1321
+ ...(sandboxToolspaceToken ? { toolspaceTokenSeed: sandboxToolspaceToken } : {}),
1295
1322
  ...(activeSandboxBackend ? { activeSandboxBackend } : {}),
1296
1323
  fileResourceDownloads,
1297
1324
  mcpServers: preparedTools.mcpServers,
@@ -1356,18 +1383,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1356
1383
  : {}),
1357
1384
  });
1358
1385
  const compactSummarizer = resolvedModel
1359
- ? (s: Settings, m: Array<Record<string, unknown>>) => withCodex(() => summarizeForCompaction(s, m, {
1386
+ ? (s: Settings, m: Array<Record<string, unknown>>, o?: { maxTranscriptTokens?: number }) => withCodex(() => summarizeForCompaction(s, m, {
1360
1387
  client: resolvedModel.client,
1361
1388
  api: resolvedModel.provider.api,
1362
1389
  model: resolvedModel.configured.id,
1363
1390
  maxOutputTokens: SUMMARY_BUFFER_TOKENS,
1391
+ ...(o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}),
1364
1392
  }))
1365
1393
  : undefined;
1366
1394
  // Pre-turn client-side context compaction (Azure path). When the
1367
1395
  // resolved mode is "client" and the single Codex-parity threshold is
1368
1396
  // crossed, this summarizes the current active history and rebuilds active
1369
1397
  // history as [user messages..., summary] BEFORE the model input is read.
1370
- // Best-effort: a no-op or failure leaves the un-compacted history intact.
1398
+ // Summarizer failures fall through to hard-trim retry and deterministic
1399
+ // fallback; only structural no-ops leave the history unchanged.
1371
1400
  // Skipped on approval/preempt resumes (no fresh user turn; the frozen
1372
1401
  // RunState or in-flight tail must replay verbatim).
1373
1402
  if (triggerType === "user.message" || triggerType === "goal.continuation") {
@@ -1463,7 +1492,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1463
1492
  { accountId: input.accountId, workspaceId: input.workspaceId, sessionId: input.sessionId, turnId: activeTurnId },
1464
1493
  session.lastInputTokens,
1465
1494
  compactSummarizer,
1466
- { force: true },
1495
+ { force: true, requireShrink: true },
1467
1496
  );
1468
1497
  if (outcome.compacted) {
1469
1498
  await publish!([{ type: "session.context.compacted", payload: { summaryPosition: outcome.summaryPosition, trigger: triggerLabel } }]);
@@ -1665,7 +1694,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1665
1694
  };
1666
1695
 
1667
1696
  await prepareRunAttemptInput();
1668
- let compactionRecoveryAttempted = false;
1697
+ let compactionRecoveryAttempts = 0;
1669
1698
  let retriedAfterCompaction = false;
1670
1699
  while (true) {
1671
1700
  try {
@@ -1681,10 +1710,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1681
1710
  const overflow = classifyContextWindowOverflowError(attemptError);
1682
1711
  const compactionNeeded = findCompactionNeededError(attemptError);
1683
1712
  const recoveryKind = compactionNeeded ? "proactive" : overflow ? "overflow" : null;
1684
- if (!recoveryKind || !publish || !turnStartedPublished || compactionRecoveryAttempted) {
1713
+ if (recoveryKind && compactionRecoveryAttempts >= MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS) {
1714
+ throw new Error(
1715
+ `compaction summarization failed: context compaction recovery attempt cap reached (${MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS})`,
1716
+ { cause: attemptError },
1717
+ );
1718
+ }
1719
+ if (!recoveryKind || !publish || !turnStartedPublished) {
1685
1720
  throw attemptError;
1686
1721
  }
1687
- compactionRecoveryAttempted = true;
1722
+ compactionRecoveryAttempts += 1;
1688
1723
  await flushRuntimeBatcher();
1689
1724
  await reconcileConversationTruth({ skipInputOnlyRows: true });
1690
1725
  const progressPersisted = modelOrToolProgressPersisted;
@@ -1699,16 +1734,46 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1699
1734
  thresholdTokens: compactionNeeded?.thresholdTokens,
1700
1735
  });
1701
1736
  let compacted = false;
1737
+ let compactionFailureMessage: string | null = null;
1702
1738
  try {
1703
1739
  const outcome = await forceContextCompaction(recoveryKind);
1704
1740
  compacted = outcome.compacted;
1741
+ if (!outcome.compacted) {
1742
+ compactionFailureMessage = compactionFailureReason(outcome.reason);
1743
+ }
1705
1744
  } catch (compactError) {
1745
+ compactionFailureMessage = compactionFailureReason(compactError instanceof Error ? compactError.message : String(compactError));
1706
1746
  observability.warn("context compaction recovery compaction failed", {
1707
1747
  sessionId: input.sessionId,
1708
1748
  turnId: activeTurnId,
1709
- error: compactError instanceof Error ? compactError.message : String(compactError),
1749
+ error: compactionFailureMessage,
1710
1750
  });
1711
1751
  }
1752
+ if (!compacted) {
1753
+ const errorMessage = compactionFailureMessage ?? "compaction summarization failed: compaction produced no replacement history";
1754
+ if (!progressPersisted) {
1755
+ throw new Error(errorMessage, { cause: attemptError });
1756
+ }
1757
+ await publish!([
1758
+ {
1759
+ type: "turn.failed",
1760
+ payload: {
1761
+ error: errorMessage,
1762
+ code: "context_compaction_failed",
1763
+ retryable: false,
1764
+ recovery: "user_message",
1765
+ compacted: false,
1766
+ },
1767
+ },
1768
+ { type: "session.status.changed", payload: { status: "idle" } },
1769
+ ], true);
1770
+ await finishTurn(db, input.workspaceId, activeTurnId, "failed");
1771
+ turnMetricOutcome = "failed";
1772
+ await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
1773
+ activityStatus = "idle";
1774
+ activityError = attemptError;
1775
+ return { status: "idle" };
1776
+ }
1712
1777
  if (progressPersisted) {
1713
1778
  // The user's intent was live mid-turn — after a successful
1714
1779
  // compaction the turn AUTO-CONTINUES via the same synthesized
@@ -1,4 +1,5 @@
1
1
  import { environmentsEncryptionKeyBytes, parseModelProvidersJson, type RegistryProvider, type Settings } from "@opengeni/config";
2
+ import { settingsWithMcpCapabilityServers } from "@opengeni/core";
2
3
  import {
3
4
  CODEX_APPS_MCP_SERVER_ID,
4
5
  CODEX_APPS_MCP_SERVER_NAME,
@@ -10,13 +11,11 @@ import {
10
11
  CODEX_PROVIDER_ID,
11
12
  } from "@opengeni/codex";
12
13
  import {
13
- decryptedCapabilityHeaders,
14
14
  listEnabledMcpCapabilityServers,
15
15
  listSessionMcpServerMetadata,
16
16
  listSessionMcpServersForRun,
17
17
  workspaceCodexSubscriptionActive,
18
18
  type Database,
19
- type EnabledMcpCapabilityServer,
20
19
  type SessionMcpServerForRun,
21
20
  } from "@opengeni/db";
22
21
 
@@ -58,6 +57,7 @@ export function settingsWithSessionMcpServers(settings: Settings, servers: Sessi
58
57
  ...(server.allowedTools ? { allowedTools: server.allowedTools } : {}),
59
58
  ...(server.timeoutMs ? { timeoutMs: server.timeoutMs } : {}),
60
59
  cacheToolsList: server.cacheToolsList ?? false,
60
+ ...(server.requireApproval !== undefined ? { requireApproval: server.requireApproval } : {}),
61
61
  headers: server.headers,
62
62
  })),
63
63
  ],
@@ -143,31 +143,3 @@ export function withCodexProvider(settings: Settings): Settings {
143
143
  };
144
144
  return { ...settings, modelProvidersJson: JSON.stringify([...providers, codexProvider]) };
145
145
  }
146
-
147
- function settingsWithMcpCapabilityServers(settings: Settings, enabled: EnabledMcpCapabilityServer[]): Settings {
148
- if (enabled.length === 0) {
149
- return settings;
150
- }
151
- const encryptionKey = environmentsEncryptionKeyBytes(settings);
152
- const existingIds = new Set(settings.mcpServers.map((server) => server.id));
153
- const dynamicServers = enabled
154
- .filter((server) => !existingIds.has(server.id))
155
- .flatMap((server) => {
156
- const headers = decryptedCapabilityHeaders(server, encryptionKey);
157
- if (headers === "unavailable") {
158
- // Without its credential headers this server can only fail auth at
159
- // connect time and break agent turns; leave it out of the run.
160
- return [];
161
- }
162
- return [{
163
- id: server.id,
164
- name: server.name,
165
- url: server.url,
166
- ...(server.allowedTools ? { allowedTools: server.allowedTools } : {}),
167
- ...(server.timeoutMs ? { timeoutMs: server.timeoutMs } : {}),
168
- cacheToolsList: server.cacheToolsList ?? false,
169
- ...(headers ? { headers } : {}),
170
- }];
171
- });
172
- return dynamicServers.length ? { ...settings, mcpServers: [...settings.mcpServers, ...dynamicServers] } : settings;
173
- }
@@ -7,8 +7,10 @@ import {
7
7
  import {
8
8
  SUMMARY_BUFFER_TOKENS,
9
9
  buildCompactionPromptInput,
10
+ buildDeterministicFallbackCompactionHistory,
10
11
  buildCompactionReplacementHistory,
11
12
  decideClientCompaction,
13
+ estimateTokens,
12
14
  sanitizeHistoryItemsForModel,
13
15
  summarizeForCompaction,
14
16
  type CompactionItem,
@@ -23,6 +25,9 @@ export type MaybeCompactResult =
23
25
  summaryPosition: number;
24
26
  signalTokens: number;
25
27
  thresholdTokens: number;
28
+ method: "summary" | "fallback";
29
+ estimatedTokensBefore: number;
30
+ estimatedTokensAfter: number;
26
31
  };
27
32
 
28
33
  /**
@@ -35,16 +40,23 @@ export type MaybeCompactResult =
35
40
  * supersedes the old active rows and inserts replacement active rows:
36
41
  * all real user messages plus one summary.
37
42
  *
38
- * Best-effort by design: any failure (planner says no, summarize fails, DB
39
- * hiccup) returns without compacting and never throws — the turn proceeds with
40
- * the un-compacted history. The read path's existing sanitizer keeps that safe.
43
+ * Required compactions are fail-closed on the summarizer: full rendered
44
+ * transcript, hard-trim rendered retry, then deterministic non-LLM fallback.
45
+ * DB errors still throw to the caller; "compacted:false" is reserved for
46
+ * structural no-ops or impossible shrink, not summarizer failures.
41
47
  *
42
48
  * There is no kept assistant/tool tail in client mode now. Assistant messages,
43
49
  * tool calls/results, reasoning, and images stay only in inactive audit rows.
44
50
  */
51
+ export type CompactionSummarizerOptions = {
52
+ maxTranscriptTokens?: number;
53
+ attempt: "full" | "hard_trim";
54
+ };
55
+
45
56
  export type CompactionSummarizer = (
46
57
  settings: Settings,
47
58
  input: CompactionItem[],
59
+ options?: CompactionSummarizerOptions,
48
60
  ) => Promise<string | null>;
49
61
 
50
62
  export async function maybeCompactContext(
@@ -53,10 +65,13 @@ export async function maybeCompactContext(
53
65
  scope: { accountId: string; workspaceId: string; sessionId: string; turnId?: string | null },
54
66
  lastInputTokens: number | null,
55
67
  // Injectable for tests; defaults to the real provider-aware model call.
56
- summarize: CompactionSummarizer = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: SUMMARY_BUFFER_TOKENS }),
68
+ summarize: CompactionSummarizer = (s, m, o) => summarizeForCompaction(s, m, {
69
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS,
70
+ ...(o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}),
71
+ }),
57
72
  // Operator-forced (the /compact command): bypass the budget trigger and
58
73
  // compact now if there is anything to summarize. Structural guards still hold.
59
- options: { force?: boolean } = {},
74
+ options: { force?: boolean; requireShrink?: boolean } = {},
60
75
  ): Promise<MaybeCompactResult> {
61
76
  if (resolveContextCompactionMode(settings) !== "client") {
62
77
  return { compacted: false, reason: "mode_not_client" };
@@ -73,6 +88,7 @@ export async function maybeCompactContext(
73
88
  lastInputTokens,
74
89
  contextWindowTokens: settings.contextWindowTokens,
75
90
  contextReservedOutputTokens: settings.contextReservedOutputTokens,
91
+ contextCompactionThresholdRatio: settings.contextCompactionThresholdRatio,
76
92
  ...(options.force ? { force: true } : {}),
77
93
  });
78
94
  if (!decision.shouldCompact) {
@@ -80,15 +96,65 @@ export async function maybeCompactContext(
80
96
  }
81
97
 
82
98
  const promptInput = buildCompactionPromptInput(items);
83
- const summaryBody = await summarize(settings, promptInput);
84
- if (!summaryBody) {
85
- return { compacted: false, reason: "summarize_failed" };
99
+ const estimatedTokensBefore = estimateTokens(items);
100
+ const shrinkBaselineTokens = Math.max(estimatedTokensBefore, decision.signalTokens);
101
+ const requireShrink = options.requireShrink || decision.reason === "above_threshold";
102
+ let summarizerFailure = "summarizer returned no summary";
103
+ let summaryBody: string | null = null;
104
+
105
+ const fullAttempt = await runSummarizerAttempt(summarize, settings, promptInput, { attempt: "full" });
106
+ if (fullAttempt.summary) {
107
+ summaryBody = fullAttempt.summary;
108
+ } else {
109
+ summarizerFailure = fullAttempt.failure;
110
+ const hardTrimBudget = Math.max(1, Math.floor(Math.max(1, decision.thresholdTokens) * 0.5));
111
+ const retryAttempt = await runSummarizerAttempt(summarize, settings, promptInput, {
112
+ attempt: "hard_trim",
113
+ maxTranscriptTokens: hardTrimBudget,
114
+ });
115
+ if (retryAttempt.summary) {
116
+ summaryBody = retryAttempt.summary;
117
+ } else {
118
+ summarizerFailure = retryAttempt.failure || summarizerFailure;
119
+ }
120
+ }
121
+
122
+ let replacementHistory: CompactionItem[] | null = null;
123
+ let method: "summary" | "fallback" = "summary";
124
+ if (summaryBody) {
125
+ const summaryReplacement = buildCompactionReplacementHistory(items, summaryBody);
126
+ const summaryEstimate = estimateTokens(summaryReplacement);
127
+ if (!requireShrink || summaryEstimate < shrinkBaselineTokens) {
128
+ replacementHistory = summaryReplacement;
129
+ } else {
130
+ summarizerFailure = `summary replacement did not reduce context signal (${summaryEstimate} >= ${shrinkBaselineTokens})`;
131
+ }
132
+ }
133
+
134
+ if (!replacementHistory) {
135
+ method = "fallback";
136
+ const fallbackTargetTokens = Math.max(
137
+ 1,
138
+ Math.min(
139
+ Math.max(1, Math.floor(shrinkBaselineTokens * 0.5)),
140
+ Math.max(1, Math.floor(Math.max(1, decision.thresholdTokens) * 0.5)),
141
+ ),
142
+ );
143
+ replacementHistory = buildDeterministicFallbackCompactionHistory({
144
+ items,
145
+ cause: summarizerFailure,
146
+ targetTokens: fallbackTargetTokens,
147
+ });
148
+ }
149
+
150
+ const estimatedTokensAfter = estimateTokens(replacementHistory);
151
+ if (requireShrink && estimatedTokensAfter >= shrinkBaselineTokens) {
152
+ return { compacted: false, reason: `compaction summarization failed: fallback did not reduce context signal (${estimatedTokensAfter} >= ${shrinkBaselineTokens})` };
86
153
  }
87
154
 
88
- const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
89
155
  const summaryItem = replacementHistory.at(-1);
90
156
  if (!summaryItem) {
91
- return { compacted: false, reason: "empty_replacement_history" };
157
+ return { compacted: false, reason: `compaction summarization failed: ${summarizerFailure}` };
92
158
  }
93
159
  const nextPosition = await nextSessionHistoryPosition(db, scope.workspaceId, scope.sessionId);
94
160
  const replacementItems = replacementHistory.slice(0, -1).map((item, index) => ({
@@ -114,5 +180,25 @@ export async function maybeCompactContext(
114
180
  summaryPosition,
115
181
  signalTokens: decision.signalTokens,
116
182
  thresholdTokens: decision.thresholdTokens,
183
+ method,
184
+ estimatedTokensBefore,
185
+ estimatedTokensAfter,
117
186
  };
118
187
  }
188
+
189
+ async function runSummarizerAttempt(
190
+ summarize: CompactionSummarizer,
191
+ settings: Settings,
192
+ promptInput: CompactionItem[],
193
+ options: CompactionSummarizerOptions,
194
+ ): Promise<{ summary: string | null; failure: string }> {
195
+ try {
196
+ const summary = await summarize(settings, promptInput, options);
197
+ if (summary && summary.trim().length > 0) {
198
+ return { summary, failure: "" };
199
+ }
200
+ return { summary: null, failure: "summarizer returned no summary" };
201
+ } catch (error) {
202
+ return { summary: null, failure: error instanceof Error ? error.message : String(error) };
203
+ }
204
+ }
@@ -1,9 +1,11 @@
1
1
  import {
2
2
  applyGitAuthPointerEnvironment,
3
+ firstPartyMcpWorkspaceUrl,
3
4
  stableSandboxEnvironmentForRun,
4
5
  type Settings,
5
6
  } from "@opengeni/config";
6
7
  import {
8
+ signDelegatedAccessToken,
7
9
  type ConnectionCredentialsPort,
8
10
  type GitCredentials,
9
11
  type ResourceRef,
@@ -104,8 +106,10 @@ export async function sandboxEnvironmentForRun(
104
106
  skipGitHubToken?: boolean;
105
107
  scope?: ConnectionScope;
106
108
  gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
109
+ sessionId?: string;
110
+ runId?: string;
107
111
  } = {},
108
- ): Promise<{ environment: Record<string, string>; gitToken?: string }> {
112
+ ): Promise<{ environment: Record<string, string>; gitToken?: string; toolspaceToken?: string }> {
109
113
  // Precedence: deployment allowlist < git identity < workspace environment
110
114
  // < backend-aware HOME (the STABLE base, shared with the API-direct attach
111
115
  // paths via stableSandboxEnvironmentForRun) < platform run-scoped GitHub auth
@@ -122,7 +126,35 @@ export async function sandboxEnvironmentForRun(
122
126
  // OPENGENI_GIT_TOKEN_FILE), so the token VALUE never rides the manifest and the
123
127
  // SDK's per-turn provided-session env delta stays empty even though the token
124
128
  // rotates. The agent can refresh the token mid-turn via the `github_token` MCP tool.
125
- const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment);
129
+ const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
130
+ const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
131
+ // TOOLSPACE (selfhosted parity): the toolspace token is minted for EVERY
132
+ // backend, including a connected machine. Unlike the platform GitHub token
133
+ // (inert on selfhosted → skipped above), the toolspace token is the machine's
134
+ // only path to programmatic tool calling, and it grants no more than the
135
+ // machine owner's own authority (toolspace:call, own-session-bound, turn TTL,
136
+ // budgeted, approval-tools excluded). Delivery mirrors the docker path: the
137
+ // caller threads it OFF-MANIFEST as the seed the runtime writes to
138
+ // $OPENGENI_TOOLSPACE_TOKEN_FILE over the box's exec channel.
139
+ let toolspaceToken: string | undefined;
140
+ if (
141
+ settings.toolspaceEnabled
142
+ && settings.delegationSecret
143
+ && options.scope
144
+ && options.sessionId
145
+ && options.runId
146
+ ) {
147
+ toolspaceToken = await signDelegatedAccessToken(settings.delegationSecret, {
148
+ accountId: options.scope.accountId,
149
+ workspaceId: options.scope.workspaceId,
150
+ subjectId: `sandbox:${options.runId}`,
151
+ subjectLabel: "sandbox toolspace",
152
+ permissions: ["toolspace:call"],
153
+ sessionId: options.sessionId,
154
+ exp: Math.floor(Date.now() / 1000) + 60 * 60,
155
+ });
156
+ environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(settings, options.scope.workspaceId);
157
+ }
126
158
  const selection = githubRepositorySelection(resources);
127
159
  // NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
128
160
  // a connected machine (selfhosted), the platform GitHub App installation token is
@@ -134,7 +166,7 @@ export async function sandboxEnvironmentForRun(
134
166
  // (validateNoEnvironmentDelta). The API-direct viewer attach path already drops the
135
167
  // token under this exact contract — proof a box runs fine without it.
136
168
  if (!selection || options.skipGitHubToken) {
137
- return { environment };
169
+ return { environment, ...(toolspaceToken ? { toolspaceToken } : {}) };
138
170
  }
139
171
  // Run-scoped sandbox preparation for GitHub App repository resources. When a
140
172
  // host binds `gitCredentials`, the HOST mints the installation token (BYO
@@ -173,7 +205,7 @@ export async function sandboxEnvironmentForRun(
173
205
  // cold-creates the box for a repo-attached session — an attach-warmed box
174
206
  // missing these keys kills the next repo turn on the SDK's manifest-env guard.
175
207
  applyGitAuthPointerEnvironment(environment, identity);
176
- return { environment, gitToken: token };
208
+ return { environment, gitToken: token, ...(toolspaceToken ? { toolspaceToken } : {}) };
177
209
  }
178
210
 
179
211
  function githubRepositorySelection(resources: ResourceRef[]): { installationId: number; repositoryIds: number[] } | null {
@@ -37,6 +37,7 @@ import {
37
37
  countSandboxLeasesByLiveness,
38
38
  forceDrainOverLimitViewerOnlyBoxes,
39
39
  getBillingBalance,
40
+ listCreditBalancesByAccount,
40
41
  listLiveModalSandboxLeaseAttributions,
41
42
  listMeterableWarmLeases,
42
43
  persistDrainSnapshot,
@@ -63,6 +64,7 @@ import {
63
64
  } from "@opengeni/runtime";
64
65
  import type { ActivityServices } from "./types";
65
66
  import {
67
+ recordCreditBalanceGauges,
66
68
  recordCreditMicros,
67
69
  recordSandboxLeaseGauges,
68
70
  recordSandboxOrphansTerminated,
@@ -174,7 +176,7 @@ export function createSandboxLeaseActivities(
174
176
  if (!settings.sandboxOwnershipEnabled) {
175
177
  return { examined: 0, terminated: 0, skipped: 0, metered: 0, forceDrained: 0, modalOrphansTerminated: 0 };
176
178
  }
177
- await refreshQueueAndLeaseGauges(db, observability);
179
+ await refreshQueueLeaseAndCreditGauges(db, observability);
178
180
 
179
181
  // (0) Warm-meter tick (P2.1) — accrue warm-seconds for every WARM viewer-only
180
182
  // box (turn-held boxes meter on the turn heartbeat, so the list fn excludes
@@ -229,7 +231,7 @@ export function createSandboxLeaseActivities(
229
231
  });
230
232
  }
231
233
 
232
- await refreshQueueAndLeaseGauges(db, observability);
234
+ await refreshQueueLeaseAndCreditGauges(db, observability);
233
235
 
234
236
  if (drainable.length > 0 || metered.accrued > 0 || forceDrained > 0 || modalOrphansTerminated > 0) {
235
237
  observability.info("sandbox reaper swept", {
@@ -299,7 +301,7 @@ async function accrueWarmTick(
299
301
  return { accrued, workspaceIds };
300
302
  }
301
303
 
302
- async function refreshQueueAndLeaseGauges(
304
+ async function refreshQueueLeaseAndCreditGauges(
303
305
  db: ActivityServices["db"],
304
306
  observability: ActivityServices["observability"],
305
307
  ): Promise<void> {
@@ -317,6 +319,13 @@ async function refreshQueueAndLeaseGauges(
317
319
  error: error instanceof Error ? error.message : String(error),
318
320
  });
319
321
  }
322
+ try {
323
+ recordCreditBalanceGauges(observability, await listCreditBalancesByAccount(db));
324
+ } catch (error) {
325
+ observability.warn("sandbox reaper: credit-balance gauge refresh failed", {
326
+ error: error instanceof Error ? error.message : String(error),
327
+ });
328
+ }
320
329
  }
321
330
 
322
331
  /**
@@ -15,6 +15,7 @@ export function createRuntimeBatcher(flushEvents: (events: AppendEventInput[]) =
15
15
  "agent.toolCall.created",
16
16
  "agent.toolCall.output",
17
17
  "agent.message.completed",
18
+ "tool.auth_needed",
18
19
  "session.requiresAction",
19
20
  "turn.completed",
20
21
  "turn.failed",
@@ -5,8 +5,10 @@ import type { RuntimeMetricsHooks } from "@opengeni/runtime";
5
5
  export type TurnOutcome = "completed" | "failed" | "cancelled" | "preempted";
6
6
  export type CreditMicrosKind = "usage" | "grant" | "topup" | "refund";
7
7
  export type SandboxLeaseLiveness = "cold" | "warming" | "warm" | "draining";
8
+ export type CreditBalanceGauge = { accountId: string; balanceMicros: number };
8
9
 
9
10
  const turnTrackers = new WeakMap<Observability, TurnLifecycleMetrics>();
11
+ const creditBalanceGaugeAccounts = new WeakMap<Observability, Set<string>>();
10
12
 
11
13
  export function observabilityEventLogger(observability: Observability): EventLogger {
12
14
  return {
@@ -194,6 +196,31 @@ export function recordSandboxLeaseGauges(observability: Observability, counts: P
194
196
  }
195
197
  }
196
198
 
199
+ export function recordCreditBalanceGauges(observability: Observability, balances: CreditBalanceGauge[]): void {
200
+ const previous = creditBalanceGaugeAccounts.get(observability) ?? new Set<string>();
201
+ const current = new Set<string>();
202
+ for (const balance of balances) {
203
+ current.add(balance.accountId);
204
+ observability.setGauge({
205
+ name: "opengeni_credit_balance_micros",
206
+ help: "Current credit balance in micros by account.",
207
+ labels: { account_id: balance.accountId },
208
+ value: balance.balanceMicros,
209
+ });
210
+ }
211
+ for (const accountId of previous) {
212
+ if (!current.has(accountId)) {
213
+ observability.setGauge({
214
+ name: "opengeni_credit_balance_micros",
215
+ help: "Current credit balance in micros by account.",
216
+ labels: { account_id: accountId },
217
+ value: 0,
218
+ });
219
+ }
220
+ }
221
+ creditBalanceGaugeAccounts.set(observability, current);
222
+ }
223
+
197
224
  export function recordSandboxOrphansTerminated(observability: Observability, count: number): void {
198
225
  if (count <= 0) {
199
226
  return;