@opengeni/worker-bundle 0.2.1 → 0.3.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.
@@ -12,9 +12,12 @@ import {
12
12
  import {
13
13
  decryptedCapabilityHeaders,
14
14
  listEnabledMcpCapabilityServers,
15
+ listSessionMcpServerMetadata,
16
+ listSessionMcpServersForRun,
15
17
  workspaceCodexSubscriptionActive,
16
18
  type Database,
17
19
  type EnabledMcpCapabilityServer,
20
+ type SessionMcpServerForRun,
18
21
  } from "@opengeni/db";
19
22
 
20
23
  export async function settingsWithEnabledCapabilityMcpServers(db: Database, workspaceId: string, settings: Settings): Promise<Settings> {
@@ -22,6 +25,45 @@ export async function settingsWithEnabledCapabilityMcpServers(db: Database, work
22
25
  return settingsWithMcpCapabilityServers(settings, enabled);
23
26
  }
24
27
 
28
+ export async function settingsWithSessionMcpServersForRun(
29
+ db: Database,
30
+ workspaceId: string,
31
+ sessionId: string,
32
+ settings: Settings,
33
+ ): Promise<Settings> {
34
+ const encryptionKey = environmentsEncryptionKeyBytes(settings);
35
+ if (!encryptionKey) {
36
+ const metadata = await listSessionMcpServerMetadata(db, workspaceId, sessionId);
37
+ if (metadata.length === 0) {
38
+ return settings;
39
+ }
40
+ throw new Error("session MCP server credentials require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
41
+ }
42
+ return settingsWithSessionMcpServers(settings, await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey));
43
+ }
44
+
45
+ export function settingsWithSessionMcpServers(settings: Settings, servers: SessionMcpServerForRun[]): Settings {
46
+ if (servers.length === 0) {
47
+ return settings;
48
+ }
49
+ const sessionIds = new Set(servers.map((server) => server.id));
50
+ return {
51
+ ...settings,
52
+ mcpServers: [
53
+ ...settings.mcpServers.filter((server) => !sessionIds.has(server.id)),
54
+ ...servers.map((server) => ({
55
+ id: server.id,
56
+ ...(server.name ? { name: server.name } : {}),
57
+ url: server.url,
58
+ ...(server.allowedTools ? { allowedTools: server.allowedTools } : {}),
59
+ ...(server.timeoutMs ? { timeoutMs: server.timeoutMs } : {}),
60
+ cacheToolsList: server.cacheToolsList ?? false,
61
+ headers: server.headers,
62
+ })),
63
+ ],
64
+ };
65
+ }
66
+
25
67
  /**
26
68
  * When the workspace has an active Codex subscription connected and the feature
27
69
  * is enabled, inject a synthetic "codex-subscription" registry provider so a
@@ -1,48 +1,50 @@
1
1
  import {
2
2
  applyContextCompaction,
3
3
  getActiveSessionHistoryItems,
4
+ nextSessionHistoryPosition,
4
5
  type Database,
5
6
  } from "@opengeni/db";
6
7
  import {
7
- buildCompactionMessages,
8
- buildSummaryItem,
9
- planCompaction,
8
+ SUMMARY_BUFFER_TOKENS,
9
+ buildCompactionPromptInput,
10
+ buildCompactionReplacementHistory,
11
+ decideClientCompaction,
12
+ sanitizeHistoryItemsForModel,
10
13
  summarizeForCompaction,
11
14
  type CompactionItem,
12
15
  } from "@opengeni/runtime";
13
- import {
14
- contextInputBudgetTokens,
15
- resolveContextCompactionMode,
16
- type Settings,
17
- } from "@opengeni/config";
16
+ import { resolveContextCompactionMode, type Settings } from "@opengeni/config";
18
17
 
19
18
  export type MaybeCompactResult =
20
19
  | { compacted: false; reason: string }
21
- | { compacted: true; supersededFrom: number; summaryPosition: number; hardForced: boolean };
20
+ | {
21
+ compacted: true;
22
+ supersededFrom: number;
23
+ summaryPosition: number;
24
+ signalTokens: number;
25
+ thresholdTokens: number;
26
+ };
22
27
 
23
28
  /**
24
29
  * Pre-turn client-side context compaction (the Azure path).
25
30
  *
26
31
  * Runs BEFORE the model call when the resolved compaction mode is "client".
27
- * Reads the active history rows + the last turn's actual input tokens, asks the
28
- * pure planner whether/where to compact, and — when it should — summarizes the
29
- * orphan-safe prefix into one plain user message via a model call and writes it
30
- * with applyContextCompaction (supersede prefix rows, insert the summary).
32
+ * Reads the active history rows + the most recent provider-reported input
33
+ * tokens, applies the single Codex-parity threshold, and - when it should -
34
+ * summarizes the active history with the Codex checkpoint prompt. The write
35
+ * supersedes the old active rows and inserts replacement active rows:
36
+ * all real user messages plus one summary.
31
37
  *
32
38
  * Best-effort by design: any failure (planner says no, summarize fails, DB
33
39
  * hiccup) returns without compacting and never throws — the turn proceeds with
34
40
  * the un-compacted history. The read path's existing sanitizer keeps that safe.
35
41
  *
36
- * The boundary the planner picks is the start of a kept user-message turn, so
37
- * no tool-call pair straddles the cut. The summary row is inserted at a
38
- * fractional position a half-step before the kept tail (boundaryPosition - 0.5),
39
- * which sorts ahead of the tail without overwriting any real prefix row, so the
40
- * active read path returns [summary, ...recent tail] in order and the full
41
- * superseded prefix survives untouched as an audit trail.
42
+ * There is no kept assistant/tool tail in client mode now. Assistant messages,
43
+ * tool calls/results, reasoning, and images stay only in inactive audit rows.
42
44
  */
43
45
  export type CompactionSummarizer = (
44
46
  settings: Settings,
45
- messages: { system: string; user: string },
47
+ input: CompactionItem[],
46
48
  ) => Promise<string | null>;
47
49
 
48
50
  export async function maybeCompactContext(
@@ -51,7 +53,7 @@ export async function maybeCompactContext(
51
53
  scope: { accountId: string; workspaceId: string; sessionId: string; turnId?: string | null },
52
54
  lastInputTokens: number | null,
53
55
  // Injectable for tests; defaults to the real provider-aware model call.
54
- summarize: CompactionSummarizer = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: s.contextSummaryMaxTokens }),
56
+ summarize: CompactionSummarizer = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: SUMMARY_BUFFER_TOKENS }),
55
57
  // Operator-forced (the /compact command): bypass the budget trigger and
56
58
  // compact now if there is anything to summarize. Structural guards still hold.
57
59
  options: { force?: boolean } = {},
@@ -65,61 +67,52 @@ export async function maybeCompactContext(
65
67
  return { compacted: false, reason: "no_history" };
66
68
  }
67
69
 
68
- const items = active.map((row) => row.item) as CompactionItem[];
69
- const plan = planCompaction({
70
+ const items = sanitizeHistoryItemsForModel(active.map((row) => row.item) as CompactionItem[]);
71
+ const decision = decideClientCompaction({
70
72
  items,
71
73
  lastInputTokens,
72
- inputBudgetTokens: contextInputBudgetTokens(settings),
73
- softFraction: settings.contextCompactSoftFraction,
74
- hardFraction: settings.contextCompactHardFraction,
75
- keepRecentTokens: settings.contextKeepRecentTokens,
74
+ contextWindowTokens: settings.contextWindowTokens,
75
+ contextReservedOutputTokens: settings.contextReservedOutputTokens,
76
76
  ...(options.force ? { force: true } : {}),
77
77
  });
78
- if (!plan.shouldCompact) {
79
- return { compacted: false, reason: plan.reason };
80
- }
81
- if (plan.hardForced) {
82
- // At/over the hard ceiling (hardFraction*B): the session is past the soft
83
- // trigger by a wide margin (often because last_input_tokens was stale-low
84
- // and the actual history overflowed). Logged distinctly so the hard-force
85
- // backstop firing is observable in production.
86
- console.warn(
87
- `context compaction HARD-FORCED for session ${scope.sessionId}: signal ${plan.signalTokens} tokens at/over hard ceiling`,
88
- );
78
+ if (!decision.shouldCompact) {
79
+ return { compacted: false, reason: decision.reason };
89
80
  }
90
81
 
91
- const messages = buildCompactionMessages(plan);
92
- const summaryBody = await summarize(settings, messages);
82
+ const promptInput = buildCompactionPromptInput(items);
83
+ const summaryBody = await summarize(settings, promptInput);
93
84
  if (!summaryBody) {
94
85
  return { compacted: false, reason: "summarize_failed" };
95
86
  }
96
87
 
97
- // Boundary is an index into the active rows; map to absolute positions. The
98
- // kept tail starts at the boundary row's position; the summary takes a
99
- // FRACTIONAL position a half-step before it. Positions are whole numbers, so
100
- // boundaryPosition - 0.5 sorts immediately ahead of the kept tail and behind
101
- // the last superseded prefix row while colliding with NO real row — the
102
- // earlier `boundaryPosition - 1` always landed on (and overwrote) the real
103
- // prefix row there.
104
- const boundaryRow = active[plan.boundaryIndex];
105
- if (!boundaryRow) {
106
- return { compacted: false, reason: "boundary_out_of_range" };
88
+ const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
89
+ const summaryItem = replacementHistory.at(-1);
90
+ if (!summaryItem) {
91
+ return { compacted: false, reason: "empty_replacement_history" };
107
92
  }
108
- const boundaryPosition = boundaryRow.position;
109
- if (boundaryPosition <= 0) {
110
- return { compacted: false, reason: "boundary_at_origin" };
111
- }
112
- const summaryPosition = boundaryPosition - 0.5;
93
+ const nextPosition = await nextSessionHistoryPosition(db, scope.workspaceId, scope.sessionId);
94
+ const replacementItems = replacementHistory.slice(0, -1).map((item, index) => ({
95
+ position: nextPosition + index,
96
+ item,
97
+ }));
98
+ const summaryPosition = nextPosition + replacementItems.length;
113
99
 
114
100
  await applyContextCompaction(db, {
115
101
  accountId: scope.accountId,
116
102
  workspaceId: scope.workspaceId,
117
103
  sessionId: scope.sessionId,
118
104
  turnId: scope.turnId ?? null,
119
- boundaryPosition,
105
+ boundaryPosition: nextPosition,
106
+ replacementItems,
120
107
  summaryPosition,
121
- summaryItem: buildSummaryItem(summaryBody),
108
+ summaryItem: summaryItem as Record<string, unknown>,
122
109
  });
123
110
 
124
- return { compacted: true, supersededFrom: boundaryPosition, summaryPosition, hardForced: plan.hardForced };
111
+ return {
112
+ compacted: true,
113
+ supersededFrom: nextPosition,
114
+ summaryPosition,
115
+ signalTokens: decision.signalTokens,
116
+ thresholdTokens: decision.thresholdTokens,
117
+ };
125
118
  }
@@ -234,8 +234,8 @@ async function messageInput(
234
234
  const inputBudgetTokens = readPathBudgetTokens(settings);
235
235
  if (settings?.sessionHistorySource === "items") {
236
236
  // Active rows only: after a client-side context compaction this is
237
- // [active summary, ...active recent tail]; superseded (summarized-away)
238
- // prefix rows stay in the table as an audit trail but never reach the model.
237
+ // [retained user messages..., active summary]; superseded rows stay in the
238
+ // table as an audit trail but never reach the model.
239
239
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
240
240
  if (stored.length > 0) {
241
241
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
package/src/index.ts CHANGED
@@ -16,6 +16,11 @@ export type WorkerOptions = {
16
16
  settings?: Settings;
17
17
  activities?: ReturnType<typeof createActivities>;
18
18
  activityDependencies?: ActivityDependencies;
19
+ // Embedded hosts install @opengeni/worker-bundle under node_modules, where
20
+ // Temporal's workflow webpack refuses to transpile TS. They relocate
21
+ // src/workflows.ts to a host-owned path and point the worker at it here.
22
+ // Unset (standalone) keeps today's in-package path byte-for-byte.
23
+ workflowsPath?: string;
19
24
  };
20
25
 
21
26
  export async function createOpenGeniWorker(options: WorkerOptions = {}): Promise<{
@@ -41,7 +46,7 @@ export async function createOpenGeniWorker(options: WorkerOptions = {}): Promise
41
46
  connection,
42
47
  namespace: settings.temporalNamespace,
43
48
  taskQueue: settings.temporalTaskQueue,
44
- workflowsPath: new URL("../src/workflows.ts", import.meta.url).pathname,
49
+ workflowsPath: options.workflowsPath ?? new URL("../src/workflows.ts", import.meta.url).pathname,
45
50
  activities,
46
51
  });
47
52
  return { worker, connection };