@opengeni/worker-bundle 0.23.4-canary.0 → 0.24.2-canary.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.
Files changed (56) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +7 -0
  2. package/dist/activities/agent-turn/model-usage.d.ts +5 -0
  3. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  4. package/dist/activities/agent-turn/run.d.ts +9 -0
  5. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  6. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  7. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  8. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  9. package/dist/activities/agent-turn/turn-context.d.ts +2 -1
  10. package/dist/activities/agent-turn.d.ts +1 -1
  11. package/dist/activities/capabilities.d.ts +1 -0
  12. package/dist/activities/context-compaction.d.ts +2 -0
  13. package/dist/{activities-control-SGU2WEVB.js → activities-control-ZWH7J4KZ.js} +102 -94
  14. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  15. package/dist/{activities-turn-7EAQXW6Q.js → activities-turn-I4OPLQLS.js} +613 -284
  16. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  17. package/dist/{chunk-TJIPCHMP.js → chunk-65JEWP2Q.js} +105 -6
  18. package/dist/chunk-65JEWP2Q.js.map +1 -0
  19. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  20. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  21. package/dist/{chunk-S2QVRVJW.js → chunk-RQNH6CGR.js} +28 -41
  22. package/dist/chunk-RQNH6CGR.js.map +1 -0
  23. package/dist/index.js +16 -3
  24. package/dist/index.js.map +1 -1
  25. package/dist/observability-metrics.d.ts +33 -7
  26. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  27. package/dist/sandbox-resume.d.ts +6 -1
  28. package/package.json +17 -17
  29. package/src/activities/agent-turn/admission.ts +19 -0
  30. package/src/activities/agent-turn/claim.ts +9 -1
  31. package/src/activities/agent-turn/compaction-prep.ts +6 -1
  32. package/src/activities/agent-turn/finalization.ts +31 -17
  33. package/src/activities/agent-turn/model-usage.ts +33 -4
  34. package/src/activities/agent-turn/quiescence.ts +11 -2
  35. package/src/activities/agent-turn/run.ts +74 -1
  36. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  37. package/src/activities/agent-turn/session-title.ts +55 -2
  38. package/src/activities/agent-turn/stream-attempt.ts +258 -135
  39. package/src/activities/agent-turn/tool-environment.ts +41 -7
  40. package/src/activities/agent-turn/turn-context.ts +4 -2
  41. package/src/activities/agent-turn.ts +5 -1
  42. package/src/activities/api-integrations.ts +11 -1
  43. package/src/activities/capabilities.ts +57 -0
  44. package/src/activities/context-compaction.ts +3 -0
  45. package/src/activities/goals.ts +1 -0
  46. package/src/activities/mcp-credentials.ts +53 -0
  47. package/src/activities/rig-verification.ts +5 -3
  48. package/src/activities/scheduled-tasks.ts +111 -98
  49. package/src/index.ts +15 -0
  50. package/src/observability-metrics.ts +152 -14
  51. package/src/sandbox-reaper-timeout.ts +10 -4
  52. package/src/sandbox-resume.ts +58 -8
  53. package/dist/activities-control-SGU2WEVB.js.map +0 -1
  54. package/dist/activities-turn-7EAQXW6Q.js.map +0 -1
  55. package/dist/chunk-S2QVRVJW.js.map +0 -1
  56. package/dist/chunk-TJIPCHMP.js.map +0 -1
@@ -8,6 +8,12 @@ import {
8
8
  withWorkspaceGatewayCredential,
9
9
  withWorkspaceOpenRouterCatalogProvider,
10
10
  withWorkspaceOpenRouterCredential,
11
+ withOrganizationGatewayCatalogProvider,
12
+ withOrganizationGatewayCredential,
13
+ withOrganizationOpenRouterCatalogProvider,
14
+ withOrganizationOpenRouterCredential,
15
+ ORGANIZATION_GATEWAY_MODEL_ID_PREFIX,
16
+ ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX,
11
17
  withXaiSubscriptionCatalogProvider,
12
18
  } from "@opengeni/config";
13
19
  import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
@@ -21,6 +27,9 @@ import {
21
27
  workspaceCodexSubscriptionActive,
22
28
  loadWorkspaceVercelAiGatewayApiKey,
23
29
  loadWorkspaceOpenRouterApiKey,
30
+ listOrganizationModelProviderCustomModelsForWorkspace,
31
+ getOrganizationModelProviderCustomModelForExecution,
32
+ loadOrganizationModelProviderApiKey,
24
33
  type Database,
25
34
  type SessionMcpServerForRun,
26
35
  } from "@opengeni/db";
@@ -203,3 +212,51 @@ export async function settingsWithWorkspaceOpenRouterCredential(
203
212
  ? withWorkspaceOpenRouterCredential(catalogSettings, apiKey, customModels)
204
213
  : catalogSettings;
205
214
  }
215
+
216
+ export async function settingsWithOrganizationProviderCredentials(
217
+ db: Database,
218
+ accountId: string,
219
+ workspaceId: string,
220
+ settings: Settings,
221
+ retainedProductModelId?: string | null,
222
+ ): Promise<Settings> {
223
+ const buildModels = async (providerKind: "vercel_gateway" | "openrouter", prefix: string) => {
224
+ const active = await listOrganizationModelProviderCustomModelsForWorkspace(db, {
225
+ accountId,
226
+ workspaceId,
227
+ providerKind,
228
+ });
229
+ const upstreamModelId = retainedProductModelId?.startsWith(prefix)
230
+ ? retainedProductModelId.slice(prefix.length)
231
+ : null;
232
+ const retained = upstreamModelId
233
+ ? await getOrganizationModelProviderCustomModelForExecution(db, {
234
+ accountId,
235
+ workspaceId,
236
+ providerKind,
237
+ upstreamModelId,
238
+ })
239
+ : null;
240
+ return retained && !active.some((model) => model.id === retained.id)
241
+ ? [...active, retained]
242
+ : active;
243
+ };
244
+ const gatewayModels = await buildModels("vercel_gateway", ORGANIZATION_GATEWAY_MODEL_ID_PREFIX);
245
+ const openRouterModels = await buildModels("openrouter", ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX);
246
+ const gatewayKey = await loadOrganizationModelProviderApiKey(db, settings, {
247
+ accountId,
248
+ workspaceId,
249
+ providerKind: "vercel_gateway",
250
+ });
251
+ const gatewaySettings = gatewayKey
252
+ ? withOrganizationGatewayCredential(settings, gatewayKey, gatewayModels)
253
+ : withOrganizationGatewayCatalogProvider(settings, gatewayModels);
254
+ const openRouterKey = await loadOrganizationModelProviderApiKey(db, settings, {
255
+ accountId,
256
+ workspaceId,
257
+ providerKind: "openrouter",
258
+ });
259
+ return openRouterKey
260
+ ? withOrganizationOpenRouterCredential(gatewaySettings, openRouterKey, openRouterModels)
261
+ : withOrganizationOpenRouterCatalogProvider(gatewaySettings, openRouterModels);
262
+ }
@@ -99,6 +99,8 @@ export async function maybeCompactContext(
99
99
  * append again — the event is already durable.
100
100
  */
101
101
  publishLiveEvents?: (events: SessionEvent[]) => Promise<void>;
102
+ /** Observe the durable start immediately after its attempt-fenced commit. */
103
+ onCompactionStarted?: (trigger: "auto" | "operator" | "proactive" | "overflow") => void;
102
104
  /** Materialize retained screenshot receipts only in the attempt-local model view. */
103
105
  materializeHistory?: (items: CompactionItem[]) => Promise<CompactionItem[]>;
104
106
  /** Turn-scoped attachment/modality view; canonical persisted rows stay untouched. */
@@ -208,6 +210,7 @@ export async function maybeCompactContext(
208
210
  `turn attempt was fenced while recording context compaction start: ${started.reason}`,
209
211
  );
210
212
  }
213
+ options.onCompactionStarted?.(trigger);
211
214
  await options.publishLiveEvents?.(started.events);
212
215
 
213
216
  if (useRemoteV2) {
@@ -261,6 +261,7 @@ export function goalContinuationPrompt(
261
261
  "Waiting on child sessions or external events:",
262
262
  "- When the next progress depends on child sessions you spawned or on an external event, do not sleep, loop, or poll sessions_list/session_get/session_events to wait for it.",
263
263
  "- Re-check sessions_list or session_get once; if the work is still in flight, call opengeni__goal_wait with a concrete reason and a deadline (untilSeconds), then end your turn immediately. You will be woken by a child result, a message, a human prompt, or at the deadline, and this goal stays active.",
264
+ "- If the immediately preceding user-facing update already reported this same unchanged wait, do not restate it or produce another equivalent final answer. Call opengeni__goal_wait and end the turn. Report only material new state or a newly discovered blocker.",
264
265
  "- A hold is for child/external progress only. If you are blocked on a human decision, use opengeni__goal_pause under the blocked audit below instead.",
265
266
  "",
266
267
  ]
@@ -12,6 +12,9 @@ import {
12
12
  } from "@opengeni/db";
13
13
  import { recordTenancyCompatibilityLaneUse, type Observability } from "@opengeni/observability";
14
14
 
15
+ const OPENGENI_CONNECTION_ID_PATTERN =
16
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
17
+
15
18
  export function connectionTokenResolverForTurn(input: {
16
19
  db: Database;
17
20
  settings: Settings;
@@ -44,6 +47,56 @@ export function connectionTokenResolverForTurn(input: {
44
47
  })
45
48
  : buildConnectionTokenResolver(input.db, input.settings);
46
49
  return async (request) => {
50
+ // Host-owned refs carry explicit provenance because their opaque ids may
51
+ // themselves be valid UUIDs. They never enter OpenGeni's native connection
52
+ // authority or PostgreSQL UUID lookup; the bound host authorizes the exact
53
+ // immutable turn context on every credential request.
54
+ if (request.connectionRef.authoritySource === "host") {
55
+ if (hostResolver) return await baseResolver(request);
56
+ return {
57
+ status: "auth_needed",
58
+ reason: "unsupported_auth",
59
+ providerDomain: request.connectionRef.providerDomain,
60
+ authoritySource: "host",
61
+ ...(request.connectionRef.provider ? { provider: request.connectionRef.provider } : {}),
62
+ ...(request.connectionRef.connectionId
63
+ ? { connectionId: request.connectionRef.connectionId }
64
+ : {}),
65
+ ...(request.connectionRef.scopes ? { scopes: request.connectionRef.scopes } : {}),
66
+ ...(request.connectionRef.resource ? { resource: request.connectionRef.resource } : {}),
67
+ ...(request.connectionRef.selectedResources
68
+ ? { selectedResources: request.connectionRef.selectedResources }
69
+ : {}),
70
+ };
71
+ }
72
+ // Before explicit provenance existed, the public embedding contract let a
73
+ // bound host use any non-UUID opaque id. Preserve those frozen/stored refs
74
+ // during upgrades while keeping every UUID-shaped omission on OpenGeni's
75
+ // native accepted-use authority. New host refs must carry authoritySource
76
+ // so UUID-shaped host ids are unambiguous.
77
+ if (
78
+ hostResolver &&
79
+ request.connectionRef.connectionId &&
80
+ !OPENGENI_CONNECTION_ID_PATTERN.test(request.connectionRef.connectionId)
81
+ ) {
82
+ try {
83
+ return await baseResolver(request);
84
+ } catch {
85
+ return {
86
+ status: "auth_needed",
87
+ reason: "refresh_failed",
88
+ providerDomain: request.connectionRef.providerDomain,
89
+ authoritySource: "host",
90
+ ...(request.connectionRef.provider ? { provider: request.connectionRef.provider } : {}),
91
+ connectionId: request.connectionRef.connectionId,
92
+ ...(request.connectionRef.scopes ? { scopes: request.connectionRef.scopes } : {}),
93
+ ...(request.connectionRef.resource ? { resource: request.connectionRef.resource } : {}),
94
+ ...(request.connectionRef.selectedResources
95
+ ? { selectedResources: request.connectionRef.selectedResources }
96
+ : {}),
97
+ };
98
+ }
99
+ }
47
100
  const acceptedDelegation = input.turn.personalConnectionDelegations.find(
48
101
  (delegation) =>
49
102
  (delegation.connectionType === undefined ||
@@ -115,11 +115,13 @@ export function rigPlatformChecksForSettings(
115
115
  'test -x "$__og_browser_engine"',
116
116
  '__og_browser_token="$(mktemp)"',
117
117
  "trap 'opengeni-browserd-down >/dev/null 2>&1 || true; rm -f \"$__og_browser_token\"' EXIT",
118
- "printf '%s\\n' 'rig-platform-check' > \"$__og_browser_token\"",
118
+ "__og_browser_token_value=\"$(od -An -N32 -tx1 /dev/urandom | tr -d '[:space:]')\"",
119
+ 'test "${#__og_browser_token_value}" -eq 64',
120
+ 'printf \'%s\\n\' "$__og_browser_token_value" > "$__og_browser_token"',
119
121
  'chmod 0600 "$__og_browser_token"',
120
122
  'OPENGENI_BROWSERD_ADMIN_TOKEN_FILE="$__og_browser_token" OPENGENI_BROWSERD_PORT=7682 OPENGENI_BROWSERD_ALLOWED_ORIGINS= opengeni-browserd-up',
121
123
  "curl --noproxy '*' -fsS http://127.0.0.1:7682/healthz >/dev/null",
122
- "curl --noproxy '*' -fsS -H 'Authorization: Bearer rig-platform-check' http://127.0.0.1:7682/v1/browser-sessions >/dev/null",
124
+ "curl --noproxy '*' -fsS -H \"Authorization: Bearer $__og_browser_token_value\" http://127.0.0.1:7682/v1/browser-sessions >/dev/null",
123
125
  ].join("\n"),
124
126
  },
125
127
  ];
@@ -148,7 +150,7 @@ export function rigPlatformChecksForSettings(
148
150
  "test -x /usr/local/bin/opengeni-desktop-down",
149
151
  "command -v Xvfb >/dev/null",
150
152
  "command -v x11vnc >/dev/null",
151
- "command -v websockify >/dev/null",
153
+ "test -x /opt/noVNC/utils/novnc_proxy",
152
154
  "command -v scrot >/dev/null",
153
155
  "command -v xdotool >/dev/null",
154
156
  '__og_desktop_home="$(mktemp -d)"',
@@ -21,6 +21,7 @@ import {
21
21
  resolveWorkspaceCatalogSettings,
22
22
  resolveSessionToolPolicy,
23
23
  workspaceCustomModelReference,
24
+ lockActiveCustomModelForAdmission,
24
25
  scheduledSlackBotConnectionId,
25
26
  swapActiveSandbox,
26
27
  } from "@opengeni/core";
@@ -52,8 +53,6 @@ import {
52
53
  getVariableSet,
53
54
  isCodexBilledModel,
54
55
  initializeSessionStartAtomically,
55
- lockActiveWorkspaceGatewayCustomModelForAdmission,
56
- lockActiveWorkspaceOpenRouterCustomModelForAdmission,
57
56
  materializeScheduledTaskReusableSessionFromRun,
58
57
  listEnabledMcpCapabilityServerIds,
59
58
  listInstalledApiIntegrationServerIdsForDelegations,
@@ -787,18 +786,11 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
787
786
  : null;
788
787
  const beforeFreshAgentRunCommit = acceptedCustomModel
789
788
  ? async (tx: Database): Promise<void> => {
790
- const active =
791
- acceptedCustomModel.providerKind === "openrouter"
792
- ? await lockActiveWorkspaceOpenRouterCustomModelForAdmission(tx, {
793
- accountId: task.accountId,
794
- workspaceId: task.workspaceId,
795
- upstreamModelId: acceptedCustomModel.upstreamModelId,
796
- })
797
- : await lockActiveWorkspaceGatewayCustomModelForAdmission(tx, {
798
- accountId: task.accountId,
799
- workspaceId: task.workspaceId,
800
- upstreamModelId: acceptedCustomModel.upstreamModelId,
801
- });
789
+ const active = await lockActiveCustomModelForAdmission(tx, {
790
+ accountId: task.accountId,
791
+ workspaceId: task.workspaceId,
792
+ reference: acceptedCustomModel,
793
+ });
802
794
  if (!active) {
803
795
  throw new ScheduledRunTerminalAuthorityError(
804
796
  "scheduled_model_unavailable",
@@ -1398,15 +1390,16 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1398
1390
  xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
1399
1391
  scheduledTaskRunId: run.id,
1400
1392
  },
1401
- async (tx, wakeEventId) => {
1393
+ async (tx, wakeEventId, _updateId, rejectionReason) => {
1402
1394
  if (!wakeEventId) {
1395
+ const error = scheduledSessionRejectionError(rejectionReason);
1403
1396
  await settleScheduledTaskRunInTransaction(tx, {
1404
1397
  workspaceId: task.workspaceId,
1405
1398
  runId: scheduledRun.id,
1406
1399
  sessionId: session.id,
1407
1400
  triggerEventId: null,
1408
1401
  status: "skipped",
1409
- error: "session_cancelled",
1402
+ error,
1410
1403
  });
1411
1404
  return;
1412
1405
  }
@@ -1418,26 +1411,33 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1418
1411
  status: "dispatched",
1419
1412
  });
1420
1413
  },
1421
- incidentPreflightRequired
1422
- ? {
1423
- prepareSource: async (tx, sessionId) =>
1424
- await prepareIncidentTelemetrySource({
1425
- tx,
1426
- settings,
1427
- taskId: task.id,
1428
- workspaceId: task.workspaceId,
1429
- sessionId,
1430
- alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1431
- acceptedExecution,
1432
- }),
1433
- }
1434
- : undefined,
1414
+ {
1415
+ requireIdleSession: task.overlapPolicy === "skip" && !sessionCreated,
1416
+ ...(incidentPreflightRequired
1417
+ ? {
1418
+ prepareSource: async (tx, sessionId) =>
1419
+ await prepareIncidentTelemetrySource({
1420
+ tx,
1421
+ settings,
1422
+ taskId: task.id,
1423
+ workspaceId: task.workspaceId,
1424
+ sessionId,
1425
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1426
+ acceptedExecution,
1427
+ }),
1428
+ }
1429
+ : {}),
1430
+ },
1435
1431
  );
1436
- if (scheduledUpdate.reason === "session_cancelled") {
1432
+ if (
1433
+ scheduledUpdate.reason === "session_cancelled" ||
1434
+ scheduledUpdate.reason === "session_not_idle"
1435
+ ) {
1436
+ const error = scheduledSessionRejectionError(scheduledUpdate.reason);
1437
1437
  return {
1438
1438
  kind: "blocked" as const,
1439
1439
  result: { action: "blocked" as const, reason: "scheduled_run_terminal" as const },
1440
- run: { ...run, status: "skipped" as const, error: "session_cancelled" },
1440
+ run: { ...run, status: "skipped" as const, error },
1441
1441
  };
1442
1442
  }
1443
1443
  if (scheduledUpdate.added && scheduledUpdate.events.length > 0) {
@@ -1475,39 +1475,14 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1475
1475
  sessionId: session.id,
1476
1476
  });
1477
1477
  // A user-cancelled (terminal) reusable session must not be revived and
1478
- // re-billed on the next fire. Early check avoids the pre-lock goal
1479
- // upsert side-effect; the locked-callback check below is the
1480
- // authoritative atomic guard. Mirrors apps/api/src/domain/sessions.ts.
1478
+ // re-billed on the next fire. This cheap check gives an early answer;
1479
+ // the locked admission check below remains authoritative. Mirrors
1480
+ // apps/api/src/domain/sessions.ts.
1481
1481
  assertReusableSessionRevivable(session.status);
1482
1482
  // Defensive backstop for the API-level 409: a reusable session keeps
1483
1483
  // its creation-time attachment, so a diverged task attachment must
1484
1484
  // fail the run instead of silently running with the wrong secrets.
1485
1485
  assertReusableSessionBindingMatches(session, task);
1486
- // A recurring "maintain X" task re-establishes its objective on every
1487
- // fire: replace the goal text, reactivate it, and reset the counters.
1488
- const goalEvents =
1489
- task.runMode === "reusable_session" && goalSpec && run.status === "queued"
1490
- ? await upsertScheduledSessionGoalForRun(dispatchDb, {
1491
- accountId: task.accountId,
1492
- workspaceId: task.workspaceId,
1493
- sessionId: session.id,
1494
- runId: run.id,
1495
- text: goalSpec.text,
1496
- successCriteria: goalSpec.successCriteria ?? null,
1497
- maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
1498
- ...(goalSpec.mutationPolicy ? { mutationPolicy: goalSpec.mutationPolicy } : {}),
1499
- })
1500
- : [];
1501
- if (goalEvents.length > 0) {
1502
- if (deferPublications) {
1503
- deferredEvents.push({
1504
- sessionId: session.id,
1505
- events: goalEvents,
1506
- });
1507
- } else {
1508
- await publishDurableSessionEvents(bus, task.workspaceId, session.id, goalEvents);
1509
- }
1510
- }
1511
1486
  const bundled = await addSessionSystemUpdateWithSourceMutation(
1512
1487
  dispatchDb,
1513
1488
  {
@@ -1542,15 +1517,16 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1542
1517
  xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
1543
1518
  scheduledTaskRunId: run.id,
1544
1519
  },
1545
- async (tx, wakeEventId) => {
1520
+ async (tx, wakeEventId, _updateId, rejectionReason) => {
1546
1521
  if (!wakeEventId) {
1522
+ const error = scheduledSessionRejectionError(rejectionReason);
1547
1523
  await settleScheduledTaskRunInTransaction(tx, {
1548
1524
  workspaceId: task.workspaceId,
1549
1525
  runId: scheduledRun.id,
1550
1526
  sessionId: session.id,
1551
1527
  triggerEventId: null,
1552
1528
  status: "skipped",
1553
- error: "session_cancelled",
1529
+ error,
1554
1530
  });
1555
1531
  return;
1556
1532
  }
@@ -1562,10 +1538,26 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1562
1538
  status: "dispatched",
1563
1539
  });
1564
1540
  },
1565
- incidentPreflightRequired
1566
- ? {
1567
- prepareSource: async (tx, sessionId) =>
1568
- await prepareIncidentTelemetrySource({
1541
+ {
1542
+ requireIdleSession: task.overlapPolicy === "skip",
1543
+ prepareSource: async (tx, sessionId) => {
1544
+ const goalEvents =
1545
+ task.runMode === "reusable_session" && goalSpec && run.status === "queued"
1546
+ ? await upsertScheduledSessionGoalForRun(tx, {
1547
+ accountId: task.accountId,
1548
+ workspaceId: task.workspaceId,
1549
+ sessionId,
1550
+ runId: run.id,
1551
+ text: goalSpec.text,
1552
+ successCriteria: goalSpec.successCriteria ?? null,
1553
+ maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
1554
+ ...(goalSpec.mutationPolicy
1555
+ ? { mutationPolicy: goalSpec.mutationPolicy }
1556
+ : {}),
1557
+ })
1558
+ : [];
1559
+ const incidentSource = incidentPreflightRequired
1560
+ ? await prepareIncidentTelemetrySource({
1569
1561
  tx,
1570
1562
  settings,
1571
1563
  taskId: task.id,
@@ -1573,15 +1565,21 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
1573
1565
  sessionId,
1574
1566
  alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1575
1567
  acceptedExecution,
1576
- }),
1577
- }
1578
- : undefined,
1568
+ })
1569
+ : undefined;
1570
+ return {
1571
+ ...(incidentSource ?? {}),
1572
+ ...(goalEvents.length > 0 ? { events: goalEvents } : {}),
1573
+ };
1574
+ },
1575
+ },
1579
1576
  );
1580
- if (bundled.reason === "session_cancelled") {
1577
+ if (bundled.reason === "session_cancelled" || bundled.reason === "session_not_idle") {
1578
+ const error = scheduledSessionRejectionError(bundled.reason);
1581
1579
  return {
1582
1580
  kind: "blocked" as const,
1583
1581
  result: { action: "blocked" as const, reason: "scheduled_run_terminal" as const },
1584
- run: { ...run, status: "skipped" as const, error: "session_cancelled" },
1582
+ run: { ...run, status: "skipped" as const, error },
1585
1583
  };
1586
1584
  }
1587
1585
  if (bundled.added && bundled.events.length > 0) {
@@ -1805,6 +1803,14 @@ function scheduledRunTerminalResult(
1805
1803
  return { action: "blocked", reason: "scheduled_run_terminal" };
1806
1804
  }
1807
1805
 
1806
+ function scheduledSessionRejectionError(
1807
+ rejectionReason: "session_cancelled" | "session_not_idle" | null,
1808
+ ): "session_cancelled" | "scheduled_session_not_idle" {
1809
+ return rejectionReason === "session_not_idle"
1810
+ ? "scheduled_session_not_idle"
1811
+ : "session_cancelled";
1812
+ }
1813
+
1808
1814
  async function prepareIncidentTelemetrySource(input: {
1809
1815
  tx: Database;
1810
1816
  settings: ControlActivityServices["settings"];
@@ -2312,23 +2318,6 @@ async function recoverBoundScheduledTaskDispatch(input: {
2312
2318
  await publishDurableSessionEvents(input.bus, task.workspaceId, session.id, titleEvents);
2313
2319
  }
2314
2320
  }
2315
- if (!generatedSession && task.runMode === "reusable_session" && task.agentConfig.goal) {
2316
- const goalEvents = await upsertScheduledSessionGoalForRun(input.db, {
2317
- accountId: task.accountId,
2318
- workspaceId: task.workspaceId,
2319
- sessionId: session.id,
2320
- runId: input.run.id,
2321
- text: task.agentConfig.goal.text,
2322
- successCriteria: task.agentConfig.goal.successCriteria ?? null,
2323
- maxAutoContinuations: task.agentConfig.goal.maxAutoContinuations ?? null,
2324
- ...(task.agentConfig.goal.mutationPolicy
2325
- ? { mutationPolicy: task.agentConfig.goal.mutationPolicy }
2326
- : {}),
2327
- });
2328
- if (goalEvents.length > 0) {
2329
- await publishDurableSessionEvents(input.bus, task.workspaceId, session.id, goalEvents);
2330
- }
2331
- }
2332
2321
  const scheduledUpdate = await addSessionSystemUpdateWithSourceMutation(
2333
2322
  input.db,
2334
2323
  {
@@ -2367,15 +2356,16 @@ async function recoverBoundScheduledTaskDispatch(input: {
2367
2356
  input.acceptedExecution.xaiProviderAccountAuthoritySnapshot,
2368
2357
  scheduledTaskRunId: input.run.id,
2369
2358
  },
2370
- async (tx, wakeEventId) => {
2359
+ async (tx, wakeEventId, _updateId, rejectionReason) => {
2371
2360
  if (!wakeEventId) {
2361
+ const error = scheduledSessionRejectionError(rejectionReason);
2372
2362
  await settleScheduledTaskRunInTransaction(tx, {
2373
2363
  workspaceId: task.workspaceId,
2374
2364
  runId: input.run.id,
2375
2365
  sessionId: session.id,
2376
2366
  triggerEventId: null,
2377
2367
  status: "skipped",
2378
- error: "session_cancelled",
2368
+ error,
2379
2369
  });
2380
2370
  return;
2381
2371
  }
@@ -2387,10 +2377,25 @@ async function recoverBoundScheduledTaskDispatch(input: {
2387
2377
  status: "dispatched",
2388
2378
  });
2389
2379
  },
2390
- input.acceptedExecution.incidentPreflightRequired
2391
- ? {
2392
- prepareSource: async (tx, sessionId) =>
2393
- await prepareIncidentTelemetrySource({
2380
+ {
2381
+ requireIdleSession: task.overlapPolicy === "skip" && !generatedSession,
2382
+ prepareSource: async (tx, sessionId) => {
2383
+ const goal = task.agentConfig.goal;
2384
+ const goalEvents =
2385
+ !generatedSession && task.runMode === "reusable_session" && goal
2386
+ ? await upsertScheduledSessionGoalForRun(tx, {
2387
+ accountId: task.accountId,
2388
+ workspaceId: task.workspaceId,
2389
+ sessionId,
2390
+ runId: input.run.id,
2391
+ text: goal.text,
2392
+ successCriteria: goal.successCriteria ?? null,
2393
+ maxAutoContinuations: goal.maxAutoContinuations ?? null,
2394
+ ...(goal.mutationPolicy ? { mutationPolicy: goal.mutationPolicy } : {}),
2395
+ })
2396
+ : [];
2397
+ const incidentSource = input.acceptedExecution.incidentPreflightRequired
2398
+ ? await prepareIncidentTelemetrySource({
2394
2399
  tx,
2395
2400
  settings: input.settings,
2396
2401
  taskId: task.id,
@@ -2398,11 +2403,19 @@ async function recoverBoundScheduledTaskDispatch(input: {
2398
2403
  sessionId,
2399
2404
  alertOccurrenceLabels: input.acceptedExecution.alertOccurrenceLabels,
2400
2405
  acceptedExecution: input.acceptedExecution,
2401
- }),
2402
- }
2403
- : undefined,
2406
+ })
2407
+ : undefined;
2408
+ return {
2409
+ ...(incidentSource ?? {}),
2410
+ ...(goalEvents.length > 0 ? { events: goalEvents } : {}),
2411
+ };
2412
+ },
2413
+ },
2404
2414
  );
2405
- if (scheduledUpdate.reason === "session_cancelled") {
2415
+ if (
2416
+ scheduledUpdate.reason === "session_cancelled" ||
2417
+ scheduledUpdate.reason === "session_not_idle"
2418
+ ) {
2406
2419
  return { action: "blocked", reason: "scheduled_run_terminal" };
2407
2420
  }
2408
2421
  if (scheduledUpdate.added && scheduledUpdate.events.length > 0) {
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ import {
11
11
  assertRuntimeDatabasePosture,
12
12
  countSessionRecoveryBacklog,
13
13
  createDb,
14
+ getContextCompactionPendingSummary,
14
15
  markSessionWorkflowWakeDelivered,
15
16
  type Database,
16
17
  type RuntimeDatabasePostureOptions,
@@ -56,9 +57,11 @@ import {
56
57
  type WorkerLifecycleState,
57
58
  } from "./http";
58
59
  import {
60
+ initializeContextCompactionMetrics,
59
61
  initializeWorkerOutcomeMetrics,
60
62
  normalizeTurnTaskQueueStats,
61
63
  observabilityEventLogger,
64
+ startContextCompactionPendingMonitor,
62
65
  startSessionRecoveryMonitor,
63
66
  startTurnCapacityMonitor,
64
67
  type TurnTaskQueueStats,
@@ -225,6 +228,9 @@ export async function createOpenGeniWorker(options: WorkerOptions): Promise<{
225
228
  options.activityDependencies?.observability ??
226
229
  createObservability(settings, { component: `worker-${options.role}` });
227
230
  initializeWorkerOutcomeMetrics(observability);
231
+ if (options.role === "turn") {
232
+ initializeContextCompactionMetrics(observability);
233
+ }
228
234
  if (options.role === "turn" && options.workflowBundle) {
229
235
  throw new Error("workflowBundle is valid only for the control worker role");
230
236
  }
@@ -800,6 +806,9 @@ export async function createOpenGeniWorkerService(
800
806
  let workerBundle: Awaited<ReturnType<typeof createOpenGeniWorker>> | undefined;
801
807
  let turnCapacityMonitor: ReturnType<typeof startTurnCapacityMonitor> | undefined;
802
808
  let sessionRecoveryMonitor: ReturnType<typeof startSessionRecoveryMonitor> | undefined;
809
+ let contextCompactionPendingMonitor:
810
+ | ReturnType<typeof startContextCompactionPendingMonitor>
811
+ | undefined;
803
812
  const schedules: Array<{ close: () => Promise<void> }> = [];
804
813
  let httpServer: ReturnType<typeof startWorkerHttpServer> | undefined;
805
814
  let memoryPressureGuard: TurnWorkerMemoryPressureGuard | undefined;
@@ -888,6 +897,10 @@ export async function createOpenGeniWorkerService(
888
897
  observability,
889
898
  read: async () => await countSessionRecoveryBacklog(options.activityDependencies.db),
890
899
  });
900
+ contextCompactionPendingMonitor = startContextCompactionPendingMonitor({
901
+ observability,
902
+ read: async () => await getContextCompactionPendingSummary(options.activityDependencies.db),
903
+ });
891
904
  }
892
905
 
893
906
  if (workerOwnsInternalSchedules(options.role, options.internalSchedules)) {
@@ -946,6 +959,7 @@ export async function createOpenGeniWorkerService(
946
959
  await Promise.allSettled([
947
960
  turnCapacityMonitor?.close(),
948
961
  sessionRecoveryMonitor?.close(),
962
+ contextCompactionPendingMonitor?.close(),
949
963
  workerBundle?.connection.close(),
950
964
  signaler?.close(),
951
965
  ...schedules.map((schedule) => schedule.close()),
@@ -969,6 +983,7 @@ export async function createOpenGeniWorkerService(
969
983
  await Promise.allSettled([
970
984
  turnCapacityMonitor?.close(),
971
985
  sessionRecoveryMonitor?.close(),
986
+ contextCompactionPendingMonitor?.close(),
972
987
  activeWorkerBundle.connection.close(),
973
988
  activeSignaler?.close(),
974
989
  ...schedules.map((schedule) => schedule.close()),