@opengeni/worker-bundle 0.7.4 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/chunk-BJUW5LCM.js +325 -0
  2. package/dist/chunk-BJUW5LCM.js.map +1 -0
  3. package/dist/host-export.d.ts +70 -0
  4. package/dist/host-export.js +7 -0
  5. package/dist/host-export.js.map +1 -0
  6. package/dist/index.d.ts +98 -6
  7. package/dist/index.js +2853 -834
  8. package/dist/index.js.map +1 -1
  9. package/dist/workflow-bundle.js +12358 -0
  10. package/package.json +18 -12
  11. package/src/activities/agent-turn.ts +2052 -554
  12. package/src/activities/capabilities.ts +9 -2
  13. package/src/activities/codex-capacity.ts +6 -11
  14. package/src/activities/codex-rotation.ts +75 -86
  15. package/src/activities/context-compaction.ts +104 -24
  16. package/src/activities/environment.ts +367 -92
  17. package/src/activities/expiring-material-renewal.ts +143 -0
  18. package/src/activities/goals.ts +4 -0
  19. package/src/activities/mcp-credentials.ts +38 -0
  20. package/src/activities/packs.ts +115 -2
  21. package/src/activities/parent-wake.ts +48 -38
  22. package/src/activities/run-credential-renewal.ts +70 -0
  23. package/src/activities/run-credentials.ts +132 -0
  24. package/src/activities/run-input.ts +23 -0
  25. package/src/activities/sandbox-lease.ts +1 -1
  26. package/src/activities/scheduled-tasks.ts +21 -0
  27. package/src/activities/session-state.ts +83 -12
  28. package/src/activities/streaming.ts +1 -0
  29. package/src/activities/toolspace-token-renewal.ts +57 -0
  30. package/src/activities/types.ts +44 -6
  31. package/src/activities/workspace-capture.ts +113 -93
  32. package/src/activities.ts +10 -2
  33. package/src/host-export-pump.ts +416 -0
  34. package/src/host-export.ts +22 -0
  35. package/src/http.ts +77 -28
  36. package/src/index.ts +360 -156
  37. package/src/sandbox-resume.ts +47 -1
  38. package/src/sandbox-routing.ts +1 -1
  39. package/src/worker-service-lifecycle.ts +104 -0
  40. package/src/workflows/activities.ts +13 -7
  41. package/src/workflows/session.ts +219 -23
  42. package/src/workflows.ts +1 -0
@@ -6,15 +6,17 @@ import {
6
6
  } from "@opengeni/db";
7
7
  import {
8
8
  SUMMARY_BUFFER_TOKENS,
9
- buildCompactionPromptInput,
10
9
  buildCompactionReplacementHistory,
10
+ compactionReplacementFingerprint,
11
11
  decideCompaction,
12
12
  estimateTokens,
13
+ latestCompactionReplacementFingerprint,
14
+ prepareCompactionPromptInput,
13
15
  sanitizeHistoryItemsForModel,
14
16
  summarizeForCompaction,
15
17
  type CompactionItem,
16
18
  } from "@opengeni/runtime";
17
- import type { Settings } from "@opengeni/config";
19
+ import { contextInputBudgetTokens, type Settings } from "@opengeni/config";
18
20
  import type { SessionEvent } from "@opengeni/contracts";
19
21
  import { TurnAttemptFencedError } from "./turn-attempt-fenced";
20
22
 
@@ -33,6 +35,7 @@ export type MaybeCompactResult =
33
35
  thresholdTokens: number;
34
36
  estimatedTokensBefore: number;
35
37
  estimatedTokensAfter: number;
38
+ replacementFingerprint: string;
36
39
  events: SessionEvent[];
37
40
  };
38
41
 
@@ -46,10 +49,10 @@ export type MaybeCompactResult =
46
49
  * supersedes the old active rows and inserts replacement active rows:
47
50
  * all real user messages plus one summary.
48
51
  *
49
- * If the summarizer itself exceeds the context window, the oldest prompt item
50
- * is removed and the same compaction call is retried, exactly like Codex local
51
- * compaction. Other provider failures propagate. There is no non-model
52
- * fallback and no silent request-local history trimming.
52
+ * Before sampling, a temporary copy is fitted by minimizing old tool outputs
53
+ * and then removing whole oldest work units if necessary. One authoritative
54
+ * provider overflow permits one smaller refit. Other failures propagate. There
55
+ * is no non-model fallback or mutation of canonical history before success.
53
56
  *
54
57
  * There is no kept assistant/tool tail. Assistant messages,
55
58
  * tool calls/results, reasoning, and images stay only in inactive audit rows.
@@ -127,9 +130,12 @@ export async function maybeCompactContext(
127
130
  }
128
131
 
129
132
  const estimatedTokensBefore = estimateTokens(items);
130
- const summaryBody = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
133
+ const summarized = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
134
+ const summaryBody = summarized.summaryBody;
131
135
  const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
132
136
  const estimatedTokensAfter = estimateTokens(replacementHistory);
137
+ const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
138
+ const previousReplacementFingerprint = latestCompactionReplacementFingerprint(items);
133
139
  const summaryItem = replacementHistory.at(-1);
134
140
  if (!summaryItem) {
135
141
  return {
@@ -139,6 +145,35 @@ export async function maybeCompactContext(
139
145
  requestConsumed: false,
140
146
  };
141
147
  }
148
+ if (previousReplacementFingerprint === replacementFingerprint) {
149
+ let requestConsumed = false;
150
+ if (options.clearRequestedCompaction) {
151
+ const skipped = await recordSkippedContextCompaction(db, {
152
+ ...scope,
153
+ expectedExecutionGeneration: scope.executionGeneration,
154
+ expectedAttemptId: scope.attemptId,
155
+ reason: "replacement_unchanged",
156
+ });
157
+ if (!skipped.recorded) {
158
+ throw new TurnAttemptFencedError(
159
+ "turn attempt was fenced while consuming an unchanged context compaction request",
160
+ );
161
+ }
162
+ requestConsumed = true;
163
+ return {
164
+ compacted: false,
165
+ reason: "replacement_unchanged",
166
+ events: skipped.events,
167
+ requestConsumed,
168
+ };
169
+ }
170
+ return {
171
+ compacted: false,
172
+ reason: "replacement_unchanged",
173
+ events: [],
174
+ requestConsumed,
175
+ };
176
+ }
142
177
  if (estimatedTokensAfter >= estimatedTokensBefore) {
143
178
  let requestConsumed = false;
144
179
  if (options.clearRequestedCompaction) {
@@ -183,6 +218,10 @@ export async function maybeCompactContext(
183
218
  trigger: options.trigger ?? "auto",
184
219
  estimatedTokensBefore,
185
220
  estimatedTokensAfter,
221
+ compactionInputEstimatedTokens: summarized.preparation.estimatedInputTokens,
222
+ compactionInputToolOutputsRewritten: summarized.preparation.rewrittenToolOutputs,
223
+ compactionInputHistoryItemsDropped: summarized.preparation.droppedHistoryItems,
224
+ compactionInputProviderCalls: summarized.providerCalls,
186
225
  },
187
226
  });
188
227
  if (!applied.applied) {
@@ -199,6 +238,7 @@ export async function maybeCompactContext(
199
238
  thresholdTokens: decision.thresholdTokens,
200
239
  estimatedTokensBefore,
201
240
  estimatedTokensAfter,
241
+ replacementFingerprint,
202
242
  events: applied.events,
203
243
  };
204
244
  }
@@ -207,32 +247,72 @@ async function summarizeWithCodexOverflowTrimming(
207
247
  summarize: CompactionSummarizer,
208
248
  settings: Settings,
209
249
  activeHistory: CompactionItem[],
210
- ): Promise<string> {
211
- const compactionHistory = activeHistory.slice();
212
- while (true) {
213
- try {
214
- return await summarize(settings, buildCompactionPromptInput(compactionHistory));
215
- } catch (error) {
216
- if (!isContextWindowExceeded(error) || compactionHistory.length === 0) {
217
- throw error;
218
- }
219
- // Codex removes one oldest history item, keeps the synthesized checkpoint
220
- // prompt, resets its stream retry count, and samples the summary again.
221
- compactionHistory.shift();
222
- }
250
+ ): Promise<{
251
+ summaryBody: string;
252
+ preparation: ReturnType<typeof prepareCompactionPromptInput>;
253
+ providerCalls: number;
254
+ }> {
255
+ // Codex's estimator is intentionally coarse. Keep the explicit checkpoint
256
+ // request below both the effective input window and the raw window minus the
257
+ // requested summary, then leave 15% estimator headroom. This changes only the
258
+ // temporary summarizer input; durable active history remains untouched until
259
+ // applyContextCompaction succeeds under the attempt fence.
260
+ const summaryAwareBudget = Math.max(0, settings.contextWindowTokens - SUMMARY_BUFFER_TOKENS);
261
+ const configuredInputBudget = contextInputBudgetTokens(settings);
262
+ const structuralBudget = Math.min(
263
+ configuredInputBudget > 0 ? configuredInputBudget : summaryAwareBudget,
264
+ summaryAwareBudget,
265
+ );
266
+ const initialBudget = Math.floor(structuralBudget * 0.85);
267
+ let preparation = prepareCompactionPromptInput(activeHistory, initialBudget);
268
+ try {
269
+ return {
270
+ summaryBody: await summarize(settings, preparation.input),
271
+ preparation,
272
+ providerCalls: 1,
273
+ };
274
+ } catch (error) {
275
+ if (!isContextWindowExceeded(error)) throw error;
276
+ // The provider is more authoritative than the byte/4 estimate. Refit once
277
+ // to 70% of both the configured target and the actual prepared estimate;
278
+ // then fail terminally with prior history intact. Never issue one failing
279
+ // request per oldest item. The production incident proved that the
280
+ // provider can count slightly more than twice the byte/4 estimate, so a
281
+ // half-size retry is the smallest honest bound for that observed skew.
282
+ const retryBudget = Math.floor(
283
+ Math.min(initialBudget * 0.5, preparation.estimatedInputTokens * 0.5),
284
+ );
285
+ preparation = prepareCompactionPromptInput(activeHistory, retryBudget);
286
+ return {
287
+ summaryBody: await summarize(settings, preparation.input),
288
+ preparation,
289
+ providerCalls: 2,
290
+ };
223
291
  }
224
292
  }
225
293
 
226
- function isContextWindowExceeded(error: unknown): boolean {
294
+ export function isContextWindowExceeded(error: unknown, seen = new WeakSet<object>()): boolean {
227
295
  if (!error || typeof error !== "object") return false;
296
+ if (seen.has(error)) return false;
297
+ seen.add(error);
228
298
  const record = error as Record<string, unknown>;
229
299
  const code = typeof record.code === "string" ? record.code.toLowerCase() : "";
230
- const message = error instanceof Error ? error.message.toLowerCase() : "";
231
- return (
300
+ const message =
301
+ typeof record.message === "string"
302
+ ? record.message.toLowerCase()
303
+ : error instanceof Error
304
+ ? error.message.toLowerCase()
305
+ : "";
306
+ const direct =
232
307
  code === "context_length_exceeded" ||
233
308
  code === "context_window_exceeded" ||
234
309
  message.includes("context window") ||
235
310
  message.includes("maximum context length") ||
236
- message.includes("too many tokens")
311
+ message.includes("too many tokens");
312
+ return (
313
+ direct ||
314
+ isContextWindowExceeded(record.cause, seen) ||
315
+ isContextWindowExceeded(record.error, seen) ||
316
+ isContextWindowExceeded(record.diagnostics, seen)
237
317
  );
238
318
  }