@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/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Cloudgeni-ai
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/dist/index.d.ts CHANGED
@@ -178,6 +178,15 @@ declare function createActivities(dependencies?: ActivityDependencies): {
178
178
  parser: string;
179
179
  chunkCount: number;
180
180
  error: string | null;
181
+ sourceKind: "email" | "repository" | "manual_upload" | "meeting_transcript" | "chat" | "document" | "web" | "other";
182
+ sourceUri: string | null;
183
+ sourceExternalId: string | null;
184
+ sourceTitle: string | null;
185
+ sourceAuthor: string | null;
186
+ sourceCreatedAt: string | null;
187
+ sourceUpdatedAt: string | null;
188
+ sourceVersion: string | null;
189
+ aclTags: string[];
181
190
  createdAt: string;
182
191
  updatedAt: string;
183
192
  }>;
package/dist/index.js CHANGED
@@ -55,7 +55,8 @@ import {
55
55
  sumUsageQuantity as sumUsageQuantity2,
56
56
  heartbeatLeaseHolder,
57
57
  accrueWarmSeconds,
58
- SandboxLeaseSupersededError as SandboxLeaseSupersededError2
58
+ SandboxLeaseSupersededError as SandboxLeaseSupersededError2,
59
+ buildConnectionTokenResolver
59
60
  } from "@opengeni/db";
60
61
  import { appendAndPublishEvents as appendAndPublishEvents2 } from "@opengeni/events";
61
62
  import {
@@ -74,6 +75,7 @@ import { CancelledFailure } from "@temporalio/activity";
74
75
 
75
76
  // src/activities/capabilities.ts
76
77
  import { environmentsEncryptionKeyBytes, parseModelProvidersJson } from "@opengeni/config";
78
+ import { settingsWithMcpCapabilityServers } from "@opengeni/core";
77
79
  import {
78
80
  CODEX_APPS_MCP_SERVER_ID,
79
81
  CODEX_APPS_MCP_SERVER_NAME,
@@ -85,7 +87,6 @@ import {
85
87
  CODEX_PROVIDER_ID
86
88
  } from "@opengeni/codex";
87
89
  import {
88
- decryptedCapabilityHeaders,
89
90
  listEnabledMcpCapabilityServers,
90
91
  listSessionMcpServerMetadata,
91
92
  listSessionMcpServersForRun,
@@ -122,6 +123,7 @@ function settingsWithSessionMcpServers(settings, servers) {
122
123
  ...server.allowedTools ? { allowedTools: server.allowedTools } : {},
123
124
  ...server.timeoutMs ? { timeoutMs: server.timeoutMs } : {},
124
125
  cacheToolsList: server.cacheToolsList ?? false,
126
+ ...server.requireApproval !== void 0 ? { requireApproval: server.requireApproval } : {},
125
127
  headers: server.headers
126
128
  }))
127
129
  ]
@@ -171,29 +173,6 @@ function withCodexProvider(settings) {
171
173
  };
172
174
  return { ...settings, modelProvidersJson: JSON.stringify([...providers, codexProvider]) };
173
175
  }
174
- function settingsWithMcpCapabilityServers(settings, enabled) {
175
- if (enabled.length === 0) {
176
- return settings;
177
- }
178
- const encryptionKey = environmentsEncryptionKeyBytes(settings);
179
- const existingIds = new Set(settings.mcpServers.map((server) => server.id));
180
- const dynamicServers = enabled.filter((server) => !existingIds.has(server.id)).flatMap((server) => {
181
- const headers = decryptedCapabilityHeaders(server, encryptionKey);
182
- if (headers === "unavailable") {
183
- return [];
184
- }
185
- return [{
186
- id: server.id,
187
- name: server.name,
188
- url: server.url,
189
- ...server.allowedTools ? { allowedTools: server.allowedTools } : {},
190
- ...server.timeoutMs ? { timeoutMs: server.timeoutMs } : {},
191
- cacheToolsList: server.cacheToolsList ?? false,
192
- ...headers ? { headers } : {}
193
- }];
194
- });
195
- return dynamicServers.length ? { ...settings, mcpServers: [...settings.mcpServers, ...dynamicServers] } : settings;
196
- }
197
176
 
198
177
  // src/activities/codex-rotation.ts
199
178
  var MIN_IDLE_MS = 6e4;
@@ -368,13 +347,18 @@ import {
368
347
  import {
369
348
  SUMMARY_BUFFER_TOKENS,
370
349
  buildCompactionPromptInput,
350
+ buildDeterministicFallbackCompactionHistory,
371
351
  buildCompactionReplacementHistory,
372
352
  decideClientCompaction,
353
+ estimateTokens,
373
354
  sanitizeHistoryItemsForModel,
374
355
  summarizeForCompaction
375
356
  } from "@opengeni/runtime";
376
357
  import { resolveContextCompactionMode } from "@opengeni/config";
377
- async function maybeCompactContext(db, settings, scope, lastInputTokens, summarize = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: SUMMARY_BUFFER_TOKENS }), options = {}) {
358
+ async function maybeCompactContext(db, settings, scope, lastInputTokens, summarize = (s, m, o) => summarizeForCompaction(s, m, {
359
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS,
360
+ ...o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}
361
+ }), options = {}) {
378
362
  if (resolveContextCompactionMode(settings) !== "client") {
379
363
  return { compacted: false, reason: "mode_not_client" };
380
364
  }
@@ -388,20 +372,67 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
388
372
  lastInputTokens,
389
373
  contextWindowTokens: settings.contextWindowTokens,
390
374
  contextReservedOutputTokens: settings.contextReservedOutputTokens,
375
+ contextCompactionThresholdRatio: settings.contextCompactionThresholdRatio,
391
376
  ...options.force ? { force: true } : {}
392
377
  });
393
378
  if (!decision.shouldCompact) {
394
379
  return { compacted: false, reason: decision.reason };
395
380
  }
396
381
  const promptInput = buildCompactionPromptInput(items);
397
- const summaryBody = await summarize(settings, promptInput);
398
- if (!summaryBody) {
399
- return { compacted: false, reason: "summarize_failed" };
382
+ const estimatedTokensBefore = estimateTokens(items);
383
+ const shrinkBaselineTokens = Math.max(estimatedTokensBefore, decision.signalTokens);
384
+ const requireShrink = options.requireShrink || decision.reason === "above_threshold";
385
+ let summarizerFailure = "summarizer returned no summary";
386
+ let summaryBody = null;
387
+ const fullAttempt = await runSummarizerAttempt(summarize, settings, promptInput, { attempt: "full" });
388
+ if (fullAttempt.summary) {
389
+ summaryBody = fullAttempt.summary;
390
+ } else {
391
+ summarizerFailure = fullAttempt.failure;
392
+ const hardTrimBudget = Math.max(1, Math.floor(Math.max(1, decision.thresholdTokens) * 0.5));
393
+ const retryAttempt = await runSummarizerAttempt(summarize, settings, promptInput, {
394
+ attempt: "hard_trim",
395
+ maxTranscriptTokens: hardTrimBudget
396
+ });
397
+ if (retryAttempt.summary) {
398
+ summaryBody = retryAttempt.summary;
399
+ } else {
400
+ summarizerFailure = retryAttempt.failure || summarizerFailure;
401
+ }
402
+ }
403
+ let replacementHistory = null;
404
+ let method = "summary";
405
+ if (summaryBody) {
406
+ const summaryReplacement = buildCompactionReplacementHistory(items, summaryBody);
407
+ const summaryEstimate = estimateTokens(summaryReplacement);
408
+ if (!requireShrink || summaryEstimate < shrinkBaselineTokens) {
409
+ replacementHistory = summaryReplacement;
410
+ } else {
411
+ summarizerFailure = `summary replacement did not reduce context signal (${summaryEstimate} >= ${shrinkBaselineTokens})`;
412
+ }
413
+ }
414
+ if (!replacementHistory) {
415
+ method = "fallback";
416
+ const fallbackTargetTokens = Math.max(
417
+ 1,
418
+ Math.min(
419
+ Math.max(1, Math.floor(shrinkBaselineTokens * 0.5)),
420
+ Math.max(1, Math.floor(Math.max(1, decision.thresholdTokens) * 0.5))
421
+ )
422
+ );
423
+ replacementHistory = buildDeterministicFallbackCompactionHistory({
424
+ items,
425
+ cause: summarizerFailure,
426
+ targetTokens: fallbackTargetTokens
427
+ });
428
+ }
429
+ const estimatedTokensAfter = estimateTokens(replacementHistory);
430
+ if (requireShrink && estimatedTokensAfter >= shrinkBaselineTokens) {
431
+ return { compacted: false, reason: `compaction summarization failed: fallback did not reduce context signal (${estimatedTokensAfter} >= ${shrinkBaselineTokens})` };
400
432
  }
401
- const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
402
433
  const summaryItem = replacementHistory.at(-1);
403
434
  if (!summaryItem) {
404
- return { compacted: false, reason: "empty_replacement_history" };
435
+ return { compacted: false, reason: `compaction summarization failed: ${summarizerFailure}` };
405
436
  }
406
437
  const nextPosition = await nextSessionHistoryPosition(db, scope.workspaceId, scope.sessionId);
407
438
  const replacementItems = replacementHistory.slice(0, -1).map((item, index) => ({
@@ -424,15 +455,33 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
424
455
  supersededFrom: nextPosition,
425
456
  summaryPosition,
426
457
  signalTokens: decision.signalTokens,
427
- thresholdTokens: decision.thresholdTokens
458
+ thresholdTokens: decision.thresholdTokens,
459
+ method,
460
+ estimatedTokensBefore,
461
+ estimatedTokensAfter
428
462
  };
429
463
  }
464
+ async function runSummarizerAttempt(summarize, settings, promptInput, options) {
465
+ try {
466
+ const summary = await summarize(settings, promptInput, options);
467
+ if (summary && summary.trim().length > 0) {
468
+ return { summary, failure: "" };
469
+ }
470
+ return { summary: null, failure: "summarizer returned no summary" };
471
+ } catch (error) {
472
+ return { summary: null, failure: error instanceof Error ? error.message : String(error) };
473
+ }
474
+ }
430
475
 
431
476
  // src/activities/environment.ts
432
477
  import {
433
478
  applyGitAuthPointerEnvironment,
479
+ firstPartyMcpWorkspaceUrl,
434
480
  stableSandboxEnvironmentForRun
435
481
  } from "@opengeni/config";
482
+ import {
483
+ signDelegatedAccessToken
484
+ } from "@opengeni/contracts";
436
485
  import {
437
486
  loadWorkspaceEnvironmentForRun as loadWorkspaceEnvironmentForRunFromDb
438
487
  } from "@opengeni/db";
@@ -468,10 +517,24 @@ function assertWorkspaceEcho(kind, scope, echoedWorkspaceId) {
468
517
  }
469
518
  }
470
519
  async function sandboxEnvironmentForRun(settings, resources, workspaceEnvironment = {}, options = {}) {
471
- const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment);
520
+ const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
521
+ const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
522
+ let toolspaceToken;
523
+ if (settings.toolspaceEnabled && !options.skipToolspaceToken && settings.delegationSecret && options.scope && options.sessionId && options.runId) {
524
+ toolspaceToken = await signDelegatedAccessToken(settings.delegationSecret, {
525
+ accountId: options.scope.accountId,
526
+ workspaceId: options.scope.workspaceId,
527
+ subjectId: `sandbox:${options.runId}`,
528
+ subjectLabel: "sandbox toolspace",
529
+ permissions: ["toolspace:call"],
530
+ sessionId: options.sessionId,
531
+ exp: Math.floor(Date.now() / 1e3) + 60 * 60
532
+ });
533
+ environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(settings, options.scope.workspaceId);
534
+ }
472
535
  const selection = githubRepositorySelection(resources);
473
536
  if (!selection || options.skipGitHubToken) {
474
- return { environment };
537
+ return { environment, ...toolspaceToken ? { toolspaceToken } : {} };
475
538
  }
476
539
  let token;
477
540
  let identity;
@@ -493,7 +556,7 @@ async function sandboxEnvironmentForRun(settings, resources, workspaceEnvironmen
493
556
  identity = githubAppBotIdentity(settings);
494
557
  }
495
558
  applyGitAuthPointerEnvironment(environment, identity);
496
- return { environment, gitToken: token };
559
+ return { environment, gitToken: token, ...toolspaceToken ? { toolspaceToken } : {} };
497
560
  }
498
561
  function githubRepositorySelection(resources) {
499
562
  const selected = resources.flatMap((resource) => {
@@ -1111,6 +1174,7 @@ function createRuntimeBatcher(flushEvents) {
1111
1174
  "agent.toolCall.created",
1112
1175
  "agent.toolCall.output",
1113
1176
  "agent.message.completed",
1177
+ "tool.auth_needed",
1114
1178
  "session.requiresAction",
1115
1179
  "turn.completed",
1116
1180
  "turn.failed",
@@ -2006,6 +2070,10 @@ function isWorkerShutdownCancellation(error) {
2006
2070
  return error instanceof CancelledFailure && error.message === "WORKER_SHUTDOWN";
2007
2071
  }
2008
2072
  var CONTEXT_WINDOW_OVERFLOW_RECOVERY_MESSAGE = "The conversation reached the model context compaction threshold; it has been compacted. Send a message to continue.";
2073
+ var MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS = 3;
2074
+ function compactionFailureReason(reason) {
2075
+ return reason.startsWith("compaction summarization failed:") ? reason : `compaction summarization failed: ${reason}`;
2076
+ }
2009
2077
  function classifyContextWindowOverflowError(error) {
2010
2078
  const fields = collectErrorStrings(error);
2011
2079
  const matched = fields.find(
@@ -2414,14 +2482,21 @@ function createRunAgentTurnActivity(services) {
2414
2482
  async () => activeSandboxRecord?.kind ?? null
2415
2483
  );
2416
2484
  const machinePrimary = activeSandboxBackend === "selfhosted" && Boolean(activeSandboxPointer?.activeSandboxId) && Boolean(activeSandboxRecord?.enrollmentId);
2417
- const { environment: sandboxEnvironment, gitToken: sandboxGitToken } = await sandboxEnvironmentForRun(
2485
+ const {
2486
+ environment: sandboxEnvironment,
2487
+ gitToken: sandboxGitToken,
2488
+ toolspaceToken: sandboxToolspaceToken
2489
+ } = await sandboxEnvironmentForRun(
2418
2490
  runSettings,
2419
2491
  turnResources,
2420
2492
  workspaceEnvironment?.values ?? {},
2421
2493
  {
2422
2494
  skipGitHubToken: activeSandboxBackend === "selfhosted",
2495
+ skipToolspaceToken: activeSandboxBackend === "selfhosted",
2423
2496
  scope: connectionScope,
2424
- gitCredentials: connectionCredentials?.gitCredentials
2497
+ gitCredentials: connectionCredentials?.gitCredentials,
2498
+ sessionId: input.sessionId,
2499
+ runId: turnId
2425
2500
  }
2426
2501
  );
2427
2502
  if (settings.sandboxOwnershipEnabled && turn.sandboxBackend !== "none") {
@@ -2575,12 +2650,17 @@ function createRunAgentTurnActivity(services) {
2575
2650
  }
2576
2651
  const fileResourceDownloads = await sandboxFileDownloadsForRun(runSettings, db, objectStorage, input.workspaceId, turnResources);
2577
2652
  throwIfWorkerShuttingDown();
2653
+ const resolveCredential = buildConnectionTokenResolver(db, runSettings);
2578
2654
  preparedTools = await withCodex(() => runtime.prepareTools(runSettings, turnTools, {
2579
2655
  accountId: input.accountId,
2580
2656
  workspaceId: input.workspaceId,
2581
2657
  sessionId: input.sessionId,
2582
2658
  subjectId: "worker:first-party-mcp",
2583
2659
  subjectLabel: "OpenGeni worker",
2660
+ resolveCredential,
2661
+ onAuthNeeded: async (payload) => {
2662
+ await publish([{ type: "tool.auth_needed", payload }], true);
2663
+ },
2584
2664
  // Manager-style sessions carry a creation-validated permission set
2585
2665
  // for their first-party MCP token; null keeps the fixed default.
2586
2666
  ...session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {}
@@ -2597,6 +2677,7 @@ function createRunAgentTurnActivity(services) {
2597
2677
  // seeds it to the box's token file before the repository-clone runs; it never
2598
2678
  // touches the box/agent manifest env.
2599
2679
  ...activeSandboxBackend !== "selfhosted" && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {},
2680
+ ...activeSandboxBackend !== "selfhosted" && sandboxToolspaceToken ? { toolspaceTokenSeed: sandboxToolspaceToken } : {},
2600
2681
  ...activeSandboxBackend ? { activeSandboxBackend } : {},
2601
2682
  fileResourceDownloads,
2602
2683
  mcpServers: preparedTools.mcpServers,
@@ -2652,11 +2733,12 @@ function createRunAgentTurnActivity(services) {
2652
2733
  }
2653
2734
  } : {}
2654
2735
  });
2655
- const compactSummarizer = resolvedModel ? (s, m) => withCodex(() => summarizeForCompaction2(s, m, {
2736
+ const compactSummarizer = resolvedModel ? (s, m, o) => withCodex(() => summarizeForCompaction2(s, m, {
2656
2737
  client: resolvedModel.client,
2657
2738
  api: resolvedModel.provider.api,
2658
2739
  model: resolvedModel.configured.id,
2659
- maxOutputTokens: SUMMARY_BUFFER_TOKENS2
2740
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS2,
2741
+ ...o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}
2660
2742
  })) : void 0;
2661
2743
  if (triggerType === "user.message" || triggerType === "goal.continuation") {
2662
2744
  try {
@@ -2713,7 +2795,7 @@ function createRunAgentTurnActivity(services) {
2713
2795
  { accountId: input.accountId, workspaceId: input.workspaceId, sessionId: input.sessionId, turnId: activeTurnId },
2714
2796
  session.lastInputTokens,
2715
2797
  compactSummarizer,
2716
- { force: true }
2798
+ { force: true, requireShrink: true }
2717
2799
  );
2718
2800
  if (outcome.compacted) {
2719
2801
  await publish([{ type: "session.context.compacted", payload: { summaryPosition: outcome.summaryPosition, trigger: triggerLabel } }]);
@@ -2894,7 +2976,7 @@ function createRunAgentTurnActivity(services) {
2894
2976
  return { status: "idle" };
2895
2977
  };
2896
2978
  await prepareRunAttemptInput();
2897
- let compactionRecoveryAttempted = false;
2979
+ let compactionRecoveryAttempts = 0;
2898
2980
  let retriedAfterCompaction = false;
2899
2981
  while (true) {
2900
2982
  try {
@@ -2910,10 +2992,16 @@ function createRunAgentTurnActivity(services) {
2910
2992
  const overflow = classifyContextWindowOverflowError(attemptError);
2911
2993
  const compactionNeeded = findCompactionNeededError(attemptError);
2912
2994
  const recoveryKind = compactionNeeded ? "proactive" : overflow ? "overflow" : null;
2913
- if (!recoveryKind || !publish || !turnStartedPublished || compactionRecoveryAttempted) {
2995
+ if (recoveryKind && compactionRecoveryAttempts >= MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS) {
2996
+ throw new Error(
2997
+ `compaction summarization failed: context compaction recovery attempt cap reached (${MAX_CONTEXT_COMPACTION_RECOVERY_ATTEMPTS})`,
2998
+ { cause: attemptError }
2999
+ );
3000
+ }
3001
+ if (!recoveryKind || !publish || !turnStartedPublished) {
2914
3002
  throw attemptError;
2915
3003
  }
2916
- compactionRecoveryAttempted = true;
3004
+ compactionRecoveryAttempts += 1;
2917
3005
  await flushRuntimeBatcher();
2918
3006
  await reconcileConversationTruth({ skipInputOnlyRows: true });
2919
3007
  const progressPersisted = modelOrToolProgressPersisted;
@@ -2928,16 +3016,46 @@ function createRunAgentTurnActivity(services) {
2928
3016
  thresholdTokens: compactionNeeded?.thresholdTokens
2929
3017
  });
2930
3018
  let compacted = false;
3019
+ let compactionFailureMessage = null;
2931
3020
  try {
2932
3021
  const outcome = await forceContextCompaction(recoveryKind);
2933
3022
  compacted = outcome.compacted;
3023
+ if (!outcome.compacted) {
3024
+ compactionFailureMessage = compactionFailureReason(outcome.reason);
3025
+ }
2934
3026
  } catch (compactError) {
3027
+ compactionFailureMessage = compactionFailureReason(compactError instanceof Error ? compactError.message : String(compactError));
2935
3028
  observability.warn("context compaction recovery compaction failed", {
2936
3029
  sessionId: input.sessionId,
2937
3030
  turnId: activeTurnId,
2938
- error: compactError instanceof Error ? compactError.message : String(compactError)
3031
+ error: compactionFailureMessage
2939
3032
  });
2940
3033
  }
3034
+ if (!compacted) {
3035
+ const errorMessage = compactionFailureMessage ?? "compaction summarization failed: compaction produced no replacement history";
3036
+ if (!progressPersisted) {
3037
+ throw new Error(errorMessage, { cause: attemptError });
3038
+ }
3039
+ await publish([
3040
+ {
3041
+ type: "turn.failed",
3042
+ payload: {
3043
+ error: errorMessage,
3044
+ code: "context_compaction_failed",
3045
+ retryable: false,
3046
+ recovery: "user_message",
3047
+ compacted: false
3048
+ }
3049
+ },
3050
+ { type: "session.status.changed", payload: { status: "idle" } }
3051
+ ], true);
3052
+ await finishTurn(db, input.workspaceId, activeTurnId, "failed");
3053
+ turnMetricOutcome = "failed";
3054
+ await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
3055
+ activityStatus = "idle";
3056
+ activityError = attemptError;
3057
+ return { status: "idle" };
3058
+ }
2941
3059
  if (progressPersisted) {
2942
3060
  const triggerPayload = trigger.payload && typeof trigger.payload === "object" && !Array.isArray(trigger.payload) ? trigger.payload : {};
2943
3061
  const compactionResumedTurn = triggerType === "turn.preempted" && (triggerPayload.reason === "context_compacted" || triggerPayload.reason === "context_overflow_compacted");