@opengeni/worker-bundle 0.4.1 → 0.5.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.5.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.3",
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.4.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,14 +1017,21 @@ 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 ?? {},
1016
1028
  {
1017
1029
  skipGitHubToken: activeSandboxBackend === "selfhosted",
1030
+ skipToolspaceToken: activeSandboxBackend === "selfhosted",
1018
1031
  scope: connectionScope,
1019
1032
  gitCredentials: connectionCredentials?.gitCredentials,
1033
+ sessionId: input.sessionId,
1034
+ runId: turnId,
1020
1035
  },
1021
1036
  );
1022
1037
 
@@ -1251,12 +1266,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1251
1266
  // (initialize + tools/list) can resolve the per-workspace bearer from
1252
1267
  // codexRequestStorage (runtime/codexAppsMcpRequestInit). withCodex is the
1253
1268
  // identity on every non-codex turn, so this is a no-op for existing paths.
1269
+ const resolveCredential = buildConnectionTokenResolver(db, runSettings);
1254
1270
  preparedTools = await withCodex(() => runtime.prepareTools(runSettings, turnTools, {
1255
1271
  accountId: input.accountId,
1256
1272
  workspaceId: input.workspaceId,
1257
1273
  sessionId: input.sessionId,
1258
1274
  subjectId: "worker:first-party-mcp",
1259
1275
  subjectLabel: "OpenGeni worker",
1276
+ resolveCredential,
1277
+ onAuthNeeded: async (payload) => {
1278
+ await publish!([{ type: "tool.auth_needed", payload }], true);
1279
+ },
1260
1280
  // Manager-style sessions carry a creation-validated permission set
1261
1281
  // for their first-party MCP token; null keeps the fixed default.
1262
1282
  ...(session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {}),
@@ -1292,6 +1312,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1292
1312
  // seeds it to the box's token file before the repository-clone runs; it never
1293
1313
  // touches the box/agent manifest env.
1294
1314
  ...(activeSandboxBackend !== "selfhosted" && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {}),
1315
+ ...(activeSandboxBackend !== "selfhosted" && sandboxToolspaceToken ? { toolspaceTokenSeed: sandboxToolspaceToken } : {}),
1295
1316
  ...(activeSandboxBackend ? { activeSandboxBackend } : {}),
1296
1317
  fileResourceDownloads,
1297
1318
  mcpServers: preparedTools.mcpServers,
@@ -1356,18 +1377,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1356
1377
  : {}),
1357
1378
  });
1358
1379
  const compactSummarizer = resolvedModel
1359
- ? (s: Settings, m: Array<Record<string, unknown>>) => withCodex(() => summarizeForCompaction(s, m, {
1380
+ ? (s: Settings, m: Array<Record<string, unknown>>, o?: { maxTranscriptTokens?: number }) => withCodex(() => summarizeForCompaction(s, m, {
1360
1381
  client: resolvedModel.client,
1361
1382
  api: resolvedModel.provider.api,
1362
1383
  model: resolvedModel.configured.id,
1363
1384
  maxOutputTokens: SUMMARY_BUFFER_TOKENS,
1385
+ ...(o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}),
1364
1386
  }))
1365
1387
  : undefined;
1366
1388
  // Pre-turn client-side context compaction (Azure path). When the
1367
1389
  // resolved mode is "client" and the single Codex-parity threshold is
1368
1390
  // crossed, this summarizes the current active history and rebuilds active
1369
1391
  // 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.
1392
+ // Summarizer failures fall through to hard-trim retry and deterministic
1393
+ // fallback; only structural no-ops leave the history unchanged.
1371
1394
  // Skipped on approval/preempt resumes (no fresh user turn; the frozen
1372
1395
  // RunState or in-flight tail must replay verbatim).
1373
1396
  if (triggerType === "user.message" || triggerType === "goal.continuation") {
@@ -1463,7 +1486,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1463
1486
  { accountId: input.accountId, workspaceId: input.workspaceId, sessionId: input.sessionId, turnId: activeTurnId },
1464
1487
  session.lastInputTokens,
1465
1488
  compactSummarizer,
1466
- { force: true },
1489
+ { force: true, requireShrink: true },
1467
1490
  );
1468
1491
  if (outcome.compacted) {
1469
1492
  await publish!([{ type: "session.context.compacted", payload: { summaryPosition: outcome.summaryPosition, trigger: triggerLabel } }]);
@@ -1665,7 +1688,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1665
1688
  };
1666
1689
 
1667
1690
  await prepareRunAttemptInput();
1668
- let compactionRecoveryAttempted = false;
1691
+ let compactionRecoveryAttempts = 0;
1669
1692
  let retriedAfterCompaction = false;
1670
1693
  while (true) {
1671
1694
  try {
@@ -1681,10 +1704,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1681
1704
  const overflow = classifyContextWindowOverflowError(attemptError);
1682
1705
  const compactionNeeded = findCompactionNeededError(attemptError);
1683
1706
  const recoveryKind = compactionNeeded ? "proactive" : overflow ? "overflow" : null;
1684
- if (!recoveryKind || !publish || !turnStartedPublished || compactionRecoveryAttempted) {
1707
+ if (recoveryKind && compactionRecoveryAttempts >= MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS) {
1708
+ throw new Error(
1709
+ `compaction summarization failed: context compaction recovery attempt cap reached (${MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS})`,
1710
+ { cause: attemptError },
1711
+ );
1712
+ }
1713
+ if (!recoveryKind || !publish || !turnStartedPublished) {
1685
1714
  throw attemptError;
1686
1715
  }
1687
- compactionRecoveryAttempted = true;
1716
+ compactionRecoveryAttempts += 1;
1688
1717
  await flushRuntimeBatcher();
1689
1718
  await reconcileConversationTruth({ skipInputOnlyRows: true });
1690
1719
  const progressPersisted = modelOrToolProgressPersisted;
@@ -1699,16 +1728,46 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1699
1728
  thresholdTokens: compactionNeeded?.thresholdTokens,
1700
1729
  });
1701
1730
  let compacted = false;
1731
+ let compactionFailureMessage: string | null = null;
1702
1732
  try {
1703
1733
  const outcome = await forceContextCompaction(recoveryKind);
1704
1734
  compacted = outcome.compacted;
1735
+ if (!outcome.compacted) {
1736
+ compactionFailureMessage = compactionFailureReason(outcome.reason);
1737
+ }
1705
1738
  } catch (compactError) {
1739
+ compactionFailureMessage = compactionFailureReason(compactError instanceof Error ? compactError.message : String(compactError));
1706
1740
  observability.warn("context compaction recovery compaction failed", {
1707
1741
  sessionId: input.sessionId,
1708
1742
  turnId: activeTurnId,
1709
- error: compactError instanceof Error ? compactError.message : String(compactError),
1743
+ error: compactionFailureMessage,
1710
1744
  });
1711
1745
  }
1746
+ if (!compacted) {
1747
+ const errorMessage = compactionFailureMessage ?? "compaction summarization failed: compaction produced no replacement history";
1748
+ if (!progressPersisted) {
1749
+ throw new Error(errorMessage, { cause: attemptError });
1750
+ }
1751
+ await publish!([
1752
+ {
1753
+ type: "turn.failed",
1754
+ payload: {
1755
+ error: errorMessage,
1756
+ code: "context_compaction_failed",
1757
+ retryable: false,
1758
+ recovery: "user_message",
1759
+ compacted: false,
1760
+ },
1761
+ },
1762
+ { type: "session.status.changed", payload: { status: "idle" } },
1763
+ ], true);
1764
+ await finishTurn(db, input.workspaceId, activeTurnId, "failed");
1765
+ turnMetricOutcome = "failed";
1766
+ await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
1767
+ activityStatus = "idle";
1768
+ activityError = attemptError;
1769
+ return { status: "idle" };
1770
+ }
1712
1771
  if (progressPersisted) {
1713
1772
  // The user's intent was live mid-turn — after a successful
1714
1773
  // 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,11 @@ export async function sandboxEnvironmentForRun(
104
106
  skipGitHubToken?: boolean;
105
107
  scope?: ConnectionScope;
106
108
  gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
109
+ sessionId?: string;
110
+ runId?: string;
111
+ skipToolspaceToken?: boolean;
107
112
  } = {},
108
- ): Promise<{ environment: Record<string, string>; gitToken?: string }> {
113
+ ): Promise<{ environment: Record<string, string>; gitToken?: string; toolspaceToken?: string }> {
109
114
  // Precedence: deployment allowlist < git identity < workspace environment
110
115
  // < backend-aware HOME (the STABLE base, shared with the API-direct attach
111
116
  // paths via stableSandboxEnvironmentForRun) < platform run-scoped GitHub auth
@@ -122,7 +127,28 @@ export async function sandboxEnvironmentForRun(
122
127
  // OPENGENI_GIT_TOKEN_FILE), so the token VALUE never rides the manifest and the
123
128
  // SDK's per-turn provided-session env delta stays empty even though the token
124
129
  // rotates. The agent can refresh the token mid-turn via the `github_token` MCP tool.
125
- const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment);
130
+ const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
131
+ const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
132
+ let toolspaceToken: string | undefined;
133
+ if (
134
+ settings.toolspaceEnabled
135
+ && !options.skipToolspaceToken
136
+ && settings.delegationSecret
137
+ && options.scope
138
+ && options.sessionId
139
+ && options.runId
140
+ ) {
141
+ toolspaceToken = await signDelegatedAccessToken(settings.delegationSecret, {
142
+ accountId: options.scope.accountId,
143
+ workspaceId: options.scope.workspaceId,
144
+ subjectId: `sandbox:${options.runId}`,
145
+ subjectLabel: "sandbox toolspace",
146
+ permissions: ["toolspace:call"],
147
+ sessionId: options.sessionId,
148
+ exp: Math.floor(Date.now() / 1000) + 60 * 60,
149
+ });
150
+ environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(settings, options.scope.workspaceId);
151
+ }
126
152
  const selection = githubRepositorySelection(resources);
127
153
  // NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
128
154
  // a connected machine (selfhosted), the platform GitHub App installation token is
@@ -134,7 +160,7 @@ export async function sandboxEnvironmentForRun(
134
160
  // (validateNoEnvironmentDelta). The API-direct viewer attach path already drops the
135
161
  // token under this exact contract — proof a box runs fine without it.
136
162
  if (!selection || options.skipGitHubToken) {
137
- return { environment };
163
+ return { environment, ...(toolspaceToken ? { toolspaceToken } : {}) };
138
164
  }
139
165
  // Run-scoped sandbox preparation for GitHub App repository resources. When a
140
166
  // host binds `gitCredentials`, the HOST mints the installation token (BYO
@@ -173,7 +199,7 @@ export async function sandboxEnvironmentForRun(
173
199
  // cold-creates the box for a repo-attached session — an attach-warmed box
174
200
  // missing these keys kills the next repo turn on the SDK's manifest-env guard.
175
201
  applyGitAuthPointerEnvironment(environment, identity);
176
- return { environment, gitToken: token };
202
+ return { environment, gitToken: token, ...(toolspaceToken ? { toolspaceToken } : {}) };
177
203
  }
178
204
 
179
205
  function githubRepositorySelection(resources: ResourceRef[]): { installationId: number; repositoryIds: number[] } | null {
@@ -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",