@opengeni/worker-bundle 0.7.5 → 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.
- package/dist/chunk-BJUW5LCM.js +325 -0
- package/dist/chunk-BJUW5LCM.js.map +1 -0
- package/dist/host-export.d.ts +70 -0
- package/dist/host-export.js +7 -0
- package/dist/host-export.js.map +1 -0
- package/dist/index.d.ts +98 -6
- package/dist/index.js +2812 -813
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +12358 -0
- package/package.json +18 -12
- package/src/activities/agent-turn.ts +2045 -545
- package/src/activities/capabilities.ts +9 -2
- package/src/activities/codex-capacity.ts +6 -11
- package/src/activities/codex-rotation.ts +75 -86
- package/src/activities/context-compaction.ts +104 -24
- package/src/activities/environment.ts +367 -92
- package/src/activities/expiring-material-renewal.ts +143 -0
- package/src/activities/goals.ts +4 -0
- package/src/activities/mcp-credentials.ts +38 -0
- package/src/activities/packs.ts +115 -2
- package/src/activities/run-credential-renewal.ts +70 -0
- package/src/activities/run-credentials.ts +132 -0
- package/src/activities/run-input.ts +23 -0
- package/src/activities/sandbox-lease.ts +1 -1
- package/src/activities/scheduled-tasks.ts +21 -0
- package/src/activities/session-state.ts +72 -0
- package/src/activities/streaming.ts +1 -0
- package/src/activities/toolspace-token-renewal.ts +57 -0
- package/src/activities/types.ts +44 -6
- package/src/activities/workspace-capture.ts +113 -93
- package/src/activities.ts +10 -2
- package/src/host-export-pump.ts +416 -0
- package/src/host-export.ts +22 -0
- package/src/http.ts +77 -28
- package/src/index.ts +360 -156
- package/src/sandbox-resume.ts +47 -1
- package/src/sandbox-routing.ts +1 -1
- package/src/worker-service-lifecycle.ts +104 -0
- package/src/workflows/activities.ts +13 -7
- package/src/workflows/session.ts +219 -23
- 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
|
|
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
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* fallback
|
|
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
|
|
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<
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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 =
|
|
231
|
-
|
|
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
|
}
|