@opengeni/worker-bundle 2.0.3-canary.36079201993001 → 2.0.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.
@@ -1,5 +1,5 @@
1
1
  import { type Database } from "@opengeni/db";
2
- import { prepareCompactionPromptInput, type CompactionItem, type CompactionProviderRejection } from "@opengeni/runtime";
2
+ import { type CompactionItem, type CompactionProviderRejection } from "@opengeni/runtime";
3
3
  import { type Settings } from "@opengeni/config";
4
4
  import type { SessionEvent } from "@opengeni/contracts";
5
5
  export type MaybeCompactResult = {
@@ -28,10 +28,7 @@ export type MaybeCompactResult = {
28
28
  * Codex, call Codex `/codex/responses` with `compaction_trigger` and persist the
29
29
  * opaque compaction item. Fail closed — never silently fall back to portable.
30
30
  */
31
- export type CompactionSummarizer = ((settings: Settings, input: CompactionItem[]) => Promise<string>) & {
32
- /** Model-visible instructions and tool schemas outside the history estimate. */
33
- estimatePrefixTokens?: () => number;
34
- };
31
+ export type CompactionSummarizer = (settings: Settings, input: CompactionItem[]) => Promise<string>;
35
32
  /** Returns the opaque Codex remote compaction v2 item. */
36
33
  export type RemoteCompactionV2Requester = (settings: Settings, input: CompactionItem[]) => Promise<CompactionItem>;
37
34
  export declare function maybeCompactContext(db: Database, settings: Settings, scope: {
@@ -89,10 +86,5 @@ export declare function settleFailedContextCompactionLandmark(db: Database, scop
89
86
  }): Promise<Extract<MaybeCompactResult, {
90
87
  compacted: false;
91
88
  }>>;
92
- export declare function summarizeWithCodexOverflowTrimming(summarize: CompactionSummarizer, settings: Settings, activeHistory: CompactionItem[]): Promise<{
93
- summaryBody: string;
94
- preparation: ReturnType<typeof prepareCompactionPromptInput>;
95
- providerCalls: number;
96
- }>;
97
89
  export declare function isContextWindowExceeded(error: unknown, seen?: WeakSet<object>): boolean;
98
90
  export declare function isExactContextLengthExceeded(error: unknown, seen?: WeakSet<object>): boolean;
@@ -7069,8 +7069,7 @@ import {
7069
7069
  import {
7070
7070
  EmptyCompactionSummaryError,
7071
7071
  REMOTE_COMPACTION_V2_IMPLEMENTATION,
7072
- compactionSummaryOutputTokens,
7073
- buildSummaryItem,
7072
+ SUMMARY_BUFFER_TOKENS,
7074
7073
  buildCompactionReplacementHistory,
7075
7074
  buildRemoteV2ReplacementHistory,
7076
7075
  compactionThresholdTokens,
@@ -7085,7 +7084,7 @@ import {
7085
7084
  } from "@opengeni/runtime";
7086
7085
  import { contextInputBudgetTokens } from "@opengeni/config";
7087
7086
  async function maybeCompactContext(db, settings, scope, lastInputTokens, summarize = (s, m) => summarizeForCompaction(s, m, {
7088
- maxOutputTokens: compactionSummaryOutputTokens(s.contextWindowTokens)
7087
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS
7089
7088
  }), options = {}) {
7090
7089
  if (options.codexCompactionMode === "remote_v2" && options.isCodexSubscriptionTurn !== true) {
7091
7090
  throw new Error(
@@ -7316,23 +7315,12 @@ async function compactContextPortable(db, settings, scope, canonicalItems, items
7316
7315
  const summarized = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
7317
7316
  const summaryBody = summarized.summaryBody;
7318
7317
  const retainedTokens = await retentionTokenCounts(canonicalItems, projectForWire);
7319
- const outputReserve = compactionSummaryOutputTokens(settings.contextWindowTokens);
7320
- const structuralBudget = Math.min(
7321
- contextInputBudgetTokens(settings) || settings.contextWindowTokens - outputReserve,
7322
- settings.contextWindowTokens - outputReserve
7323
- );
7324
- const prefixTokens = Math.max(0, Math.ceil(summarize.estimatePrefixTokens?.() ?? 0));
7325
- const summaryTokens = estimateTokens([buildSummaryItem(summaryBody)]);
7326
7318
  const replacementHistory = buildCompactionReplacementHistory(
7327
7319
  canonicalItems,
7328
7320
  summaryBody,
7329
- (item) => retainedTokens.get(item) ?? estimateTokens([item]),
7330
- Math.min(outputReserve, Math.max(0, structuralBudget - prefixTokens - summaryTokens))
7321
+ (item) => retainedTokens.get(item) ?? estimateTokens([item])
7331
7322
  );
7332
7323
  const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
7333
- if (estimatedTokensAfter + prefixTokens > structuralBudget) {
7334
- return await settleSkippedAfterStart(db, scope, options, "replacement_exceeds_model_budget");
7335
- }
7336
7324
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
7337
7325
  const previousReplacementFingerprint = latestCompactionReplacementFingerprint(canonicalItems);
7338
7326
  const summaryIndex = replacementHistory.length - (readReasoningConfiguration(replacementHistory.at(-1) ?? {}) ? 2 : 1);
@@ -7385,27 +7373,14 @@ async function compactContextPortable(db, settings, scope, canonicalItems, items
7385
7373
  };
7386
7374
  }
7387
7375
  async function summarizeWithCodexOverflowTrimming(summarize, settings, activeHistory) {
7388
- const summaryAwareBudget = Math.max(
7389
- 0,
7390
- settings.contextWindowTokens - compactionSummaryOutputTokens(settings.contextWindowTokens)
7391
- );
7376
+ const summaryAwareBudget = Math.max(0, settings.contextWindowTokens - SUMMARY_BUFFER_TOKENS);
7392
7377
  const configuredInputBudget = contextInputBudgetTokens(settings);
7393
7378
  const structuralBudget = Math.min(
7394
7379
  configuredInputBudget > 0 ? configuredInputBudget : summaryAwareBudget,
7395
7380
  summaryAwareBudget
7396
7381
  );
7397
- const prefixTokens = Math.max(0, Math.ceil(summarize.estimatePrefixTokens?.() ?? 0));
7398
- const initialBudget = Math.max(0, structuralBudget - prefixTokens);
7382
+ const initialBudget = Math.floor(structuralBudget * 0.85);
7399
7383
  let preparation = prepareCompactionPromptInput(activeHistory, initialBudget);
7400
- const requireHistory = () => {
7401
- if (activeHistory.length > 0 && preparation.input.length === 1) {
7402
- throw new EmptyCompactionSummaryError({
7403
- stage: "portable_input_budget",
7404
- reason: "no_history_fit"
7405
- });
7406
- }
7407
- };
7408
- requireHistory();
7409
7384
  try {
7410
7385
  return {
7411
7386
  summaryBody: await summarize(settings, preparation.input),
@@ -7415,10 +7390,9 @@ async function summarizeWithCodexOverflowTrimming(summarize, settings, activeHis
7415
7390
  } catch (error51) {
7416
7391
  if (!isContextWindowExceeded(error51)) throw error51;
7417
7392
  const retryBudget = Math.floor(
7418
- Math.min(initialBudget * 0.4, preparation.estimatedInputTokens * 0.4)
7393
+ Math.min(initialBudget * 0.5, preparation.estimatedInputTokens * 0.5)
7419
7394
  );
7420
7395
  preparation = prepareCompactionPromptInput(activeHistory, retryBudget);
7421
- requireHistory();
7422
7396
  return {
7423
7397
  summaryBody: await summarize(settings, preparation.input),
7424
7398
  preparation,
@@ -11544,8 +11518,7 @@ import {
11544
11518
  compactionThresholdTokens as compactionThresholdTokens2,
11545
11519
  CompactionNeededError,
11546
11520
  compactionProviderRejection as compactionProviderRejection2,
11547
- estimateSerializedValueTokens,
11548
- compactionSummaryOutputTokens as compactionSummaryOutputTokens2
11521
+ SUMMARY_BUFFER_TOKENS as SUMMARY_BUFFER_TOKENS2
11549
11522
  } from "@opengeni/runtime";
11550
11523
  async function prepareCompaction(deps) {
11551
11524
  const {
@@ -11584,11 +11557,6 @@ async function prepareCompaction(deps) {
11584
11557
  const remotePrefix = {
11585
11558
  agent: null
11586
11559
  };
11587
- const portableResponsesNeedsAgentPrefix = resolvedModel?.provider.api === "responses" && !(billingState.isCodexTurn && session.codexCompactionMode === "remote_v2");
11588
- const preparedPortableRequest = () => {
11589
- if (!remotePrefix.agent) throw new Error("Compaction agent is unavailable");
11590
- return preparedCompactionRequest(remotePrefix.agent);
11591
- };
11592
11560
  const promptCacheKey = acceptsPromptCacheKeyForTurn(resolvedModel) ? input.sessionId : void 0;
11593
11561
  const compactionUsageState = createCompactionModelUsageEventState(claimedModelUsageSourceKeys);
11594
11562
  const recordCompactionUsage = async (usage) => {
@@ -11620,37 +11588,24 @@ async function prepareCompaction(deps) {
11620
11588
  contextContributions: eventing.companyBrainContextContributions
11621
11589
  });
11622
11590
  };
11623
- const compactionSummarizerFor = (systemInstructions) => {
11624
- const summarize = resolvedModel ? (s, m) => withProviderRequestContext(
11625
- () => summarizeContextForCompaction(s, m, {
11626
- client: resolvedModel.client,
11627
- provider: resolvedModel.provider,
11628
- api: resolvedModel.provider.api,
11629
- model: turnExecutionPolicy.upstreamModelId,
11630
- maxOutputTokens: compactionSummaryOutputTokens2(s.contextWindowTokens),
11631
- ...cancellationSignal ? { signal: cancellationSignal } : {},
11632
- onUsage: recordCompactionUsage,
11633
- ...systemInstructions ? { systemInstructions } : {},
11634
- ...promptCacheKey ? { promptCacheKey } : {},
11635
- ...portableResponsesNeedsAgentPrefix ? { preparedRequest: preparedPortableRequest() } : {}
11636
- })
11637
- ) : (s, m) => summarizeContextForCompaction(s, m, {
11591
+ const compactionSummarizerFor = (systemInstructions) => resolvedModel ? (s, m) => withProviderRequestContext(
11592
+ () => summarizeContextForCompaction(s, m, {
11593
+ client: resolvedModel.client,
11594
+ provider: resolvedModel.provider,
11595
+ api: resolvedModel.provider.api,
11638
11596
  model: turnExecutionPolicy.upstreamModelId,
11639
- maxOutputTokens: compactionSummaryOutputTokens2(s.contextWindowTokens),
11640
- ...cancellationSignal ? { signal: cancellationSignal } : {},
11597
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS2,
11641
11598
  onUsage: recordCompactionUsage,
11642
11599
  ...systemInstructions ? { systemInstructions } : {},
11643
11600
  ...promptCacheKey ? { promptCacheKey } : {}
11644
- });
11645
- summarize.estimatePrefixTokens = () => {
11646
- if (resolvedModel?.provider.api === "chat") {
11647
- return estimateSerializedValueTokens(systemInstructions ?? "");
11648
- }
11649
- const prepared = portableResponsesNeedsAgentPrefix ? preparedPortableRequest() : null;
11650
- return estimateSerializedValueTokens(prepared?.systemInstructions ?? systemInstructions ?? "") + (prepared ? estimateSerializedValueTokens(prepared.tools) : 0);
11651
- };
11652
- return summarize;
11653
- };
11601
+ })
11602
+ ) : (s, m) => summarizeContextForCompaction(s, m, {
11603
+ model: turnExecutionPolicy.upstreamModelId,
11604
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS2,
11605
+ onUsage: recordCompactionUsage,
11606
+ ...systemInstructions ? { systemInstructions } : {},
11607
+ ...promptCacheKey ? { promptCacheKey } : {}
11608
+ });
11654
11609
  const remoteCompactionRequester = resolvedModel && billingState.isCodexTurn ? (s, m) => withCodexRemoteCompaction(async () => {
11655
11610
  if (!remotePrefix.agent) throw new Error("Compaction agent is unavailable");
11656
11611
  const preparedRequest = preparedCompactionRequest(remotePrefix.agent);
@@ -11713,7 +11668,7 @@ async function prepareCompaction(deps) {
11713
11668
  control.activityStatus = "idle";
11714
11669
  return claimedResult({ status: "idle" });
11715
11670
  };
11716
- if (compactionOnlyTurn && !remoteV2CompactionNeedsAgentPrefix && !portableResponsesNeedsAgentPrefix) {
11671
+ if (compactionOnlyTurn && !remoteV2CompactionNeedsAgentPrefix) {
11717
11672
  const compactionInstructions = appendWorkspaceMemory(
11718
11673
  appendSessionInstructions(
11719
11674
  appendWorkspaceGovernance(
@@ -11855,7 +11810,6 @@ async function runPostAgentCompaction(deps) {
11855
11810
  claimedResult,
11856
11811
  turn,
11857
11812
  session,
11858
- resolvedModel,
11859
11813
  remotePrefix,
11860
11814
  remoteCompactionRequester,
11861
11815
  publishCompactionLiveEvents,
@@ -11869,17 +11823,16 @@ async function runPostAgentCompaction(deps) {
11869
11823
  agent
11870
11824
  } = deps;
11871
11825
  const agentInstructions = typeof agent.instructions === "string" ? agent.instructions : "";
11872
- const preparedPortable = resolvedModel?.provider.api === "responses" && !(remoteCompactionRequester && session.codexCompactionMode === "remote_v2");
11873
11826
  const compactSummarizer = compactionSummarizerFor(
11874
11827
  agentInstructions.trim() ? agentInstructions : void 0
11875
11828
  );
11876
- if (remoteCompactionRequester || preparedPortable) {
11829
+ if (remoteCompactionRequester) {
11877
11830
  remotePrefix.agent = agent;
11878
11831
  }
11879
11832
  if (compactionOnlyTurn) {
11880
11833
  const requested = await isSessionCompactionRequested2(db, input.workspaceId, input.sessionId);
11881
11834
  let outcome = null;
11882
- if (requested && (preparedPortable || remoteCompactionRequester && session.codexCompactionMode === "remote_v2")) {
11835
+ if (requested && remoteCompactionRequester && session.codexCompactionMode === "remote_v2") {
11883
11836
  queuePreparedCompaction(
11884
11837
  agent,
11885
11838
  new CompactionNeededError({
@@ -11989,7 +11942,7 @@ async function runPostAgentCompaction(deps) {
11989
11942
  control.activityStatus = "idle";
11990
11943
  return { exit: claimedResult({ status: "idle" }) };
11991
11944
  }
11992
- if (preparedPortable || remoteCompactionRequester && session.codexCompactionMode === "remote_v2") {
11945
+ if (remoteCompactionRequester && session.codexCompactionMode === "remote_v2") {
11993
11946
  const forced = await isSessionCompactionRequested2(db, input.workspaceId, input.sessionId);
11994
11947
  const thresholdTokens = compactionThresholdTokens2(eventing.modelRunSettings);
11995
11948
  if (forced || (attempt.triggerType === "user.message" || attempt.triggerType === "system.update.delivered") && (session.lastInputTokens ?? 0) >= thresholdTokens) {
@@ -37655,4 +37608,4 @@ export {
37655
37608
  createTurnActivities,
37656
37609
  createTurnActivitiesFromServices
37657
37610
  };
37658
- //# sourceMappingURL=activities-turn-AJHDYNZU.js.map
37611
+ //# sourceMappingURL=activities-turn-EZEDZTEZ.js.map