@opengeni/worker-bundle 0.7.1 → 0.7.2

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 (45) hide show
  1. package/dist/index.d.ts +182 -67
  2. package/dist/index.js +7113 -1652
  3. package/dist/index.js.map +1 -1
  4. package/package.json +18 -18
  5. package/src/activities/agent-turn.ts +4337 -1052
  6. package/src/activities/capabilities.ts +43 -8
  7. package/src/activities/codex-capacity.ts +223 -0
  8. package/src/activities/codex-rotation.ts +550 -54
  9. package/src/activities/common.ts +7 -1
  10. package/src/activities/context-compaction.ts +152 -118
  11. package/src/activities/documents.ts +32 -27
  12. package/src/activities/environment.ts +318 -75
  13. package/src/activities/file-upload-reaper.ts +79 -0
  14. package/src/activities/git-credential-renewal.ts +183 -0
  15. package/src/activities/goals.ts +142 -123
  16. package/src/activities/packs.ts +55 -5
  17. package/src/activities/parent-wake.ts +228 -41
  18. package/src/activities/recording.ts +154 -122
  19. package/src/activities/redaction.ts +3 -1
  20. package/src/activities/rig-verification.ts +441 -0
  21. package/src/activities/run-input.ts +110 -122
  22. package/src/activities/sandbox-lease.ts +186 -51
  23. package/src/activities/scheduled-tasks.ts +287 -171
  24. package/src/activities/session-state.ts +168 -157
  25. package/src/activities/streaming.ts +143 -19
  26. package/src/activities/turn-attempt-fenced.ts +14 -0
  27. package/src/activities/types.ts +102 -32
  28. package/src/activities/workflow-wake.ts +44 -0
  29. package/src/activities/workspace-capture.ts +964 -0
  30. package/src/activities.ts +75 -35
  31. package/src/concurrency.ts +11 -0
  32. package/src/http.ts +16 -6
  33. package/src/index.ts +322 -49
  34. package/src/observability-metrics.ts +441 -8
  35. package/src/op-journal.ts +46 -0
  36. package/src/sandbox-resume.ts +431 -201
  37. package/src/sandbox-routing.ts +209 -22
  38. package/src/workflow-wake-contract.ts +5 -0
  39. package/src/workflows/activities.ts +58 -7
  40. package/src/workflows/file-upload-reaper.ts +12 -0
  41. package/src/workflows/rig-verification.ts +28 -0
  42. package/src/workflows/scheduled-tasks.ts +12 -26
  43. package/src/workflows/session.ts +330 -173
  44. package/src/workflows/workflow-wake.ts +12 -0
  45. package/src/workflows.ts +9 -7
@@ -22,7 +22,13 @@ export function assertReusableSessionRevivable(status: SessionStatus): void {
22
22
  }
23
23
  }
24
24
 
25
- export function scheduledUserMessagePayload(prompt: string, resources: ResourceRef[], tools: ToolRef[], taskId: string, runId: string): Record<string, unknown> {
25
+ export function scheduledUserMessagePayload(
26
+ prompt: string,
27
+ resources: ResourceRef[],
28
+ tools: ToolRef[],
29
+ taskId: string,
30
+ runId: string,
31
+ ): Record<string, unknown> {
26
32
  return {
27
33
  text: prompt,
28
34
  scheduledTaskId: taskId,
@@ -1,204 +1,238 @@
1
1
  import {
2
2
  applyContextCompaction,
3
3
  getActiveSessionHistoryItems,
4
- nextSessionHistoryPosition,
4
+ recordSkippedContextCompaction,
5
5
  type Database,
6
6
  } from "@opengeni/db";
7
7
  import {
8
8
  SUMMARY_BUFFER_TOKENS,
9
9
  buildCompactionPromptInput,
10
- buildDeterministicFallbackCompactionHistory,
11
10
  buildCompactionReplacementHistory,
12
- decideClientCompaction,
11
+ decideCompaction,
13
12
  estimateTokens,
14
13
  sanitizeHistoryItemsForModel,
15
14
  summarizeForCompaction,
16
15
  type CompactionItem,
17
16
  } from "@opengeni/runtime";
18
- import { resolveContextCompactionMode, type Settings } from "@opengeni/config";
17
+ import type { Settings } from "@opengeni/config";
18
+ import type { SessionEvent } from "@opengeni/contracts";
19
+ import { TurnAttemptFencedError } from "./turn-attempt-fenced";
19
20
 
20
21
  export type MaybeCompactResult =
21
- | { compacted: false; reason: string }
22
+ | {
23
+ compacted: false;
24
+ reason: string;
25
+ events: SessionEvent[];
26
+ requestConsumed: boolean;
27
+ }
22
28
  | {
23
29
  compacted: true;
24
30
  supersededFrom: number;
25
31
  summaryPosition: number;
26
32
  signalTokens: number;
27
33
  thresholdTokens: number;
28
- method: "summary" | "fallback";
29
34
  estimatedTokensBefore: number;
30
35
  estimatedTokensAfter: number;
36
+ events: SessionEvent[];
31
37
  };
32
38
 
33
39
  /**
34
- * Pre-turn client-side context compaction (the Azure path).
40
+ * Durable portable context compaction, following Codex CLI's local path.
35
41
  *
36
- * Runs BEFORE the model call when the resolved compaction mode is "client".
42
+ * Runs before a fresh model call and on a same-turn compaction recovery.
37
43
  * Reads the active history rows + the most recent provider-reported input
38
44
  * tokens, applies the single Codex-parity threshold, and - when it should -
39
45
  * summarizes the active history with the Codex checkpoint prompt. The write
40
46
  * supersedes the old active rows and inserts replacement active rows:
41
47
  * all real user messages plus one summary.
42
48
  *
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.
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.
47
53
  *
48
- * There is no kept assistant/tool tail in client mode now. Assistant messages,
54
+ * There is no kept assistant/tool tail. Assistant messages,
49
55
  * tool calls/results, reasoning, and images stay only in inactive audit rows.
50
56
  */
51
- export type CompactionSummarizerOptions = {
52
- maxTranscriptTokens?: number;
53
- attempt: "full" | "hard_trim";
54
- };
55
-
56
- export type CompactionSummarizer = (
57
- settings: Settings,
58
- input: CompactionItem[],
59
- options?: CompactionSummarizerOptions,
60
- ) => Promise<string | null>;
57
+ export type CompactionSummarizer = (settings: Settings, input: CompactionItem[]) => Promise<string>;
61
58
 
62
59
  export async function maybeCompactContext(
63
60
  db: Database,
64
61
  settings: Settings,
65
- scope: { accountId: string; workspaceId: string; sessionId: string; turnId?: string | null },
62
+ scope: {
63
+ accountId: string;
64
+ workspaceId: string;
65
+ sessionId: string;
66
+ turnId: string;
67
+ executionGeneration: number;
68
+ attemptId: string;
69
+ },
66
70
  lastInputTokens: number | null,
67
71
  // Injectable for tests; defaults to the real provider-aware model call.
68
- summarize: CompactionSummarizer = (s, m, o) => summarizeForCompaction(s, m, {
69
- maxOutputTokens: SUMMARY_BUFFER_TOKENS,
70
- ...(o?.maxTranscriptTokens ? { maxTranscriptTokens: o.maxTranscriptTokens } : {}),
71
- }),
72
+ summarize: CompactionSummarizer = (s, m) =>
73
+ summarizeForCompaction(s, m, {
74
+ maxOutputTokens: SUMMARY_BUFFER_TOKENS,
75
+ }),
72
76
  // Operator-forced (the /compact command): bypass the budget trigger and
73
77
  // compact now if there is anything to summarize. Structural guards still hold.
74
- options: { force?: boolean; requireShrink?: boolean } = {},
78
+ options: {
79
+ force?: boolean;
80
+ clearRequestedCompaction?: boolean;
81
+ trigger?: "auto" | "operator" | "proactive" | "overflow";
82
+ } = {},
75
83
  ): Promise<MaybeCompactResult> {
76
- if (resolveContextCompactionMode(settings) !== "client") {
77
- return { compacted: false, reason: "mode_not_client" };
78
- }
79
-
80
84
  const active = await getActiveSessionHistoryItems(db, scope.workspaceId, scope.sessionId);
81
85
  if (active.length === 0) {
82
- return { compacted: false, reason: "no_history" };
86
+ let requestConsumed = false;
87
+ if (options.clearRequestedCompaction) {
88
+ const skipped = await recordSkippedContextCompaction(db, {
89
+ ...scope,
90
+ expectedExecutionGeneration: scope.executionGeneration,
91
+ expectedAttemptId: scope.attemptId,
92
+ reason: "no_history",
93
+ });
94
+ if (!skipped.recorded) {
95
+ throw new TurnAttemptFencedError(
96
+ "turn attempt was fenced while consuming an empty context compaction request",
97
+ );
98
+ }
99
+ requestConsumed = true;
100
+ return {
101
+ compacted: false,
102
+ reason: "no_history",
103
+ events: skipped.events,
104
+ requestConsumed,
105
+ };
106
+ }
107
+ return { compacted: false, reason: "no_history", events: [], requestConsumed };
83
108
  }
84
109
 
85
110
  const items = sanitizeHistoryItemsForModel(active.map((row) => row.item) as CompactionItem[]);
86
- const decision = decideClientCompaction({
111
+ const decision = decideCompaction({
87
112
  items,
88
113
  lastInputTokens,
89
114
  contextWindowTokens: settings.contextWindowTokens,
90
115
  contextReservedOutputTokens: settings.contextReservedOutputTokens,
116
+ contextAutoCompactThresholdTokens: settings.contextAutoCompactThresholdTokens,
91
117
  contextCompactionThresholdRatio: settings.contextCompactionThresholdRatio,
92
118
  ...(options.force ? { force: true } : {}),
93
119
  });
94
120
  if (!decision.shouldCompact) {
95
- return { compacted: false, reason: decision.reason };
121
+ return {
122
+ compacted: false,
123
+ reason: decision.reason,
124
+ events: [],
125
+ requestConsumed: false,
126
+ };
96
127
  }
97
128
 
98
- const promptInput = buildCompactionPromptInput(items);
99
129
  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
-
130
+ const summaryBody = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
131
+ const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
150
132
  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})` };
153
- }
154
-
155
133
  const summaryItem = replacementHistory.at(-1);
156
134
  if (!summaryItem) {
157
- return { compacted: false, reason: `compaction summarization failed: ${summarizerFailure}` };
135
+ return {
136
+ compacted: false,
137
+ reason: "compaction produced no replacement history",
138
+ events: [],
139
+ requestConsumed: false,
140
+ };
158
141
  }
159
- const nextPosition = await nextSessionHistoryPosition(db, scope.workspaceId, scope.sessionId);
160
- const replacementItems = replacementHistory.slice(0, -1).map((item, index) => ({
161
- position: nextPosition + index,
162
- item,
163
- }));
164
- const summaryPosition = nextPosition + replacementItems.length;
165
-
166
- await applyContextCompaction(db, {
142
+ if (estimatedTokensAfter >= estimatedTokensBefore) {
143
+ let requestConsumed = false;
144
+ if (options.clearRequestedCompaction) {
145
+ const skipped = await recordSkippedContextCompaction(db, {
146
+ ...scope,
147
+ expectedExecutionGeneration: scope.executionGeneration,
148
+ expectedAttemptId: scope.attemptId,
149
+ reason: "replacement_not_smaller",
150
+ });
151
+ if (!skipped.recorded) {
152
+ throw new TurnAttemptFencedError(
153
+ "turn attempt was fenced while consuming a non-shrinking context compaction request",
154
+ );
155
+ }
156
+ requestConsumed = true;
157
+ return {
158
+ compacted: false,
159
+ reason: "replacement_not_smaller",
160
+ events: skipped.events,
161
+ requestConsumed,
162
+ };
163
+ }
164
+ return {
165
+ compacted: false,
166
+ reason: "replacement_not_smaller",
167
+ events: [],
168
+ requestConsumed,
169
+ };
170
+ }
171
+ const applied = await applyContextCompaction(db, {
167
172
  accountId: scope.accountId,
168
173
  workspaceId: scope.workspaceId,
169
174
  sessionId: scope.sessionId,
170
- turnId: scope.turnId ?? null,
171
- boundaryPosition: nextPosition,
172
- replacementItems,
173
- summaryPosition,
175
+ turnId: scope.turnId,
176
+ expectedExecutionGeneration: scope.executionGeneration,
177
+ expectedAttemptId: scope.attemptId,
178
+ replacementItems: replacementHistory.slice(0, -1),
174
179
  summaryItem: summaryItem as Record<string, unknown>,
180
+ replacementInputTokens: estimatedTokensAfter,
181
+ ...(options.clearRequestedCompaction ? { clearRequestedCompaction: true } : {}),
182
+ eventPayload: {
183
+ trigger: options.trigger ?? "auto",
184
+ estimatedTokensBefore,
185
+ estimatedTokensAfter,
186
+ },
175
187
  });
188
+ if (!applied.applied) {
189
+ throw new TurnAttemptFencedError(
190
+ `turn attempt was fenced during context compaction: ${applied.reason}`,
191
+ );
192
+ }
176
193
 
177
194
  return {
178
195
  compacted: true,
179
- supersededFrom: nextPosition,
180
- summaryPosition,
196
+ supersededFrom: applied.supersededFrom,
197
+ summaryPosition: applied.summaryPosition,
181
198
  signalTokens: decision.signalTokens,
182
199
  thresholdTokens: decision.thresholdTokens,
183
- method,
184
200
  estimatedTokensBefore,
185
201
  estimatedTokensAfter,
202
+ events: applied.events,
186
203
  };
187
204
  }
188
205
 
189
- async function runSummarizerAttempt(
206
+ async function summarizeWithCodexOverflowTrimming(
190
207
  summarize: CompactionSummarizer,
191
208
  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: "" };
209
+ 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();
199
222
  }
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
223
  }
204
224
  }
225
+
226
+ function isContextWindowExceeded(error: unknown): boolean {
227
+ if (!error || typeof error !== "object") return false;
228
+ const record = error as Record<string, unknown>;
229
+ const code = typeof record.code === "string" ? record.code.toLowerCase() : "";
230
+ const message = error instanceof Error ? error.message.toLowerCase() : "";
231
+ return (
232
+ code === "context_length_exceeded" ||
233
+ code === "context_window_exceeded" ||
234
+ message.includes("context window") ||
235
+ message.includes("maximum context length") ||
236
+ message.includes("too many tokens")
237
+ );
238
+ }
@@ -6,10 +6,7 @@ import {
6
6
  sumUsageQuantity,
7
7
  withWorkspaceUsageLock,
8
8
  } from "@opengeni/db";
9
- import type {
10
- ActivityServices,
11
- IndexDocumentInput,
12
- } from "./types";
9
+ import type { ActivityServices, IndexDocumentInput } from "./types";
13
10
 
14
11
  export function createDocumentActivities(services: () => Promise<ActivityServices>) {
15
12
  return {
@@ -19,31 +16,39 @@ export function createDocumentActivities(services: () => Promise<ActivityService
19
16
  throw new Error("object storage is not configured");
20
17
  }
21
18
  return await withWorkspaceUsageLock(db, input.workspaceId, async (lockedDb) => {
22
- const document = await indexDocumentNow(lockedDb, objectStorage, input.workspaceId, input.documentId, documentServices, {
23
- beforeEmbed: async ({ chunkCount }) => {
24
- if (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed") {
25
- const balance = await getBillingBalance(lockedDb, input.accountId);
26
- if (balance.balanceMicros <= 0) {
27
- throw new Error("insufficient OpenGeni credits");
19
+ const document = await indexDocumentNow(
20
+ lockedDb,
21
+ objectStorage,
22
+ input.workspaceId,
23
+ input.documentId,
24
+ documentServices,
25
+ {
26
+ beforeEmbed: async ({ chunkCount }) => {
27
+ if (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed") {
28
+ const balance = await getBillingBalance(lockedDb, input.accountId);
29
+ if (balance.balanceMicros <= 0) {
30
+ throw new Error("insufficient OpenGeni credits");
31
+ }
28
32
  }
29
- }
30
- if (settings.usageLimitsMode !== "static" && settings.usageLimitsMode !== "managed") {
31
- return;
32
- }
33
- const limit = configuredStaticUsageLimits(settings).maxDocumentIndexedChunksPerWorkspace;
34
- if (!limit) {
35
- return;
36
- }
37
- const used = await sumUsageQuantity(lockedDb, {
38
- workspaceId: input.workspaceId,
39
- eventType: "document.indexed",
40
- since: startOfUtcMonth(),
41
- });
42
- if (used + chunkCount > limit) {
43
- throw new Error(`monthly document indexing limit reached (${limit} chunks)`);
44
- }
33
+ if (settings.usageLimitsMode !== "static" && settings.usageLimitsMode !== "managed") {
34
+ return;
35
+ }
36
+ const limit =
37
+ configuredStaticUsageLimits(settings).maxDocumentIndexedChunksPerWorkspace;
38
+ if (!limit) {
39
+ return;
40
+ }
41
+ const used = await sumUsageQuantity(lockedDb, {
42
+ workspaceId: input.workspaceId,
43
+ eventType: "document.indexed",
44
+ since: startOfUtcMonth(),
45
+ });
46
+ if (used + chunkCount > limit) {
47
+ throw new Error(`monthly document indexing limit reached (${limit} chunks)`);
48
+ }
49
+ },
45
50
  },
46
- });
51
+ );
47
52
  if (document.status === "ready") {
48
53
  await recordUsageEvent(lockedDb, {
49
54
  accountId: input.accountId,