@opengeni/worker-bundle 0.17.2 → 0.20.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 (75) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +25 -0
  3. package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
  4. package/dist/activities/capabilities.d.ts +6 -0
  5. package/dist/activities/codex-capacity.d.ts +1 -0
  6. package/dist/activities/environment.d.ts +7 -1
  7. package/dist/activities/packs.d.ts +8 -10
  8. package/dist/activities/session-state.d.ts +4 -2
  9. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  10. package/dist/activities/types.d.ts +16 -0
  11. package/dist/activities/video-generation-admission.d.ts +9 -4
  12. package/dist/activities/video-generation-credential.d.ts +17 -0
  13. package/dist/activities/video-reference-staging.d.ts +7 -1
  14. package/dist/activities/xai-auth.d.ts +23 -0
  15. package/dist/activities/xai-image-generation.d.ts +26 -0
  16. package/dist/activities/xai-video-generation.d.ts +18 -0
  17. package/dist/{activities-control-44KBWLSM.js → activities-control-XURE4VKN.js} +387 -51
  18. package/dist/activities-control-XURE4VKN.js.map +1 -0
  19. package/dist/activities-control.d.ts +8 -0
  20. package/dist/{activities-turn-K4CEDEGJ.js → activities-turn-6EBXGGLS.js} +5312 -3951
  21. package/dist/activities-turn-6EBXGGLS.js.map +1 -0
  22. package/dist/activities.d.ts +9 -1
  23. package/dist/{chunk-KKCRGAGZ.js → chunk-5GRJIP5H.js} +99 -98
  24. package/dist/chunk-5GRJIP5H.js.map +1 -0
  25. package/dist/{chunk-E3RI3I33.js → chunk-TYNPCG4U.js} +88 -10
  26. package/dist/chunk-TYNPCG4U.js.map +1 -0
  27. package/dist/index.d.ts +13 -0
  28. package/dist/index.js +49 -2
  29. package/dist/index.js.map +1 -1
  30. package/dist/model-checkpoint-memory-collector.d.ts +1 -0
  31. package/dist/model-context-contributions.d.ts +40 -0
  32. package/dist/observability-metrics.d.ts +20 -1
  33. package/dist/sandbox-routing.d.ts +4 -0
  34. package/dist/workflow-bundle.js +59 -15
  35. package/dist/workflows/activities.d.ts +1 -1
  36. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  37. package/dist/workflows.d.ts +1 -0
  38. package/package.json +16 -15
  39. package/src/activities/agent-run-admission.ts +76 -0
  40. package/src/activities/agent-turn.ts +1178 -142
  41. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  42. package/src/activities/capabilities.ts +10 -0
  43. package/src/activities/codex-capacity.ts +48 -1
  44. package/src/activities/environment.ts +14 -8
  45. package/src/activities/goals.ts +2 -1
  46. package/src/activities/packs.ts +45 -122
  47. package/src/activities/parent-wake.ts +13 -1
  48. package/src/activities/scheduled-tasks.ts +30 -73
  49. package/src/activities/session-state.ts +18 -0
  50. package/src/activities/site-auth-maintenance.ts +269 -0
  51. package/src/activities/types.ts +17 -0
  52. package/src/activities/video-generation-admission.ts +37 -4
  53. package/src/activities/video-generation-credential.ts +84 -6
  54. package/src/activities/video-generation-reconciliation.ts +212 -19
  55. package/src/activities/video-media-validation.ts +8 -2
  56. package/src/activities/video-reference-staging.ts +121 -34
  57. package/src/activities/xai-auth.ts +122 -0
  58. package/src/activities/xai-image-generation.ts +86 -0
  59. package/src/activities/xai-video-generation.ts +69 -0
  60. package/src/activities-control.ts +4 -0
  61. package/src/activities.ts +5 -0
  62. package/src/index.ts +51 -0
  63. package/src/model-checkpoint-memory-collector.ts +8 -1
  64. package/src/model-context-contributions.ts +159 -0
  65. package/src/observability-metrics.ts +98 -9
  66. package/src/sandbox-routing.ts +13 -0
  67. package/src/workflows/activities.ts +1 -0
  68. package/src/workflows/file-upload-reaper.ts +8 -2
  69. package/src/workflows/session.ts +20 -6
  70. package/src/workflows/site-auth-maintenance.ts +14 -0
  71. package/src/workflows.ts +1 -0
  72. package/dist/activities-control-44KBWLSM.js.map +0 -1
  73. package/dist/activities-turn-K4CEDEGJ.js.map +0 -1
  74. package/dist/chunk-E3RI3I33.js.map +0 -1
  75. package/dist/chunk-KKCRGAGZ.js.map +0 -1
@@ -3,6 +3,7 @@ import { errorCodeToJSON } from "@opengeni/agent-proto";
3
3
  import { SandboxBackend, type SessionEventType } from "@opengeni/contracts";
4
4
  import type { EventLogger } from "@opengeni/events";
5
5
  import type { Attributes, AttributeValue, Observability } from "@opengeni/observability";
6
+ import type { CompanyBrainContributionReceipt } from "./model-context-contributions";
6
7
  import {
7
8
  SELFHOSTED_INFRASTRUCTURE_FAULT_CLASSES,
8
9
  modelUsageTokenCountOrNull,
@@ -248,7 +249,7 @@ export function turnLifecycleMetricsFor(observability: Observability): TurnLifec
248
249
  }
249
250
 
250
251
  export class TurnLifecycleMetrics {
251
- private readonly startedTurns = new Map<string, number>();
252
+ private readonly turns = new Map<string, { startedAt: number; lastProgressAt: number }>();
252
253
  private timer: ReturnType<typeof setInterval> | null = null;
253
254
 
254
255
  constructor(
@@ -257,15 +258,22 @@ export class TurnLifecycleMetrics {
257
258
  ) {}
258
259
 
259
260
  start(turnId: string): void {
260
- this.startedTurns.set(turnId, this.now());
261
+ const now = this.now();
262
+ this.turns.set(turnId, { startedAt: now, lastProgressAt: now });
261
263
  this.ensureTimer();
262
264
  this.refreshGauges();
263
265
  }
264
266
 
267
+ progress(turnId: string): void {
268
+ const turn = this.turns.get(turnId);
269
+ if (!turn) return;
270
+ turn.lastProgressAt = this.now();
271
+ }
272
+
265
273
  finish(turnId: string, outcome: TurnOutcome | null, durationSeconds?: number): void {
266
- const startedAt = this.startedTurns.get(turnId);
274
+ const startedAt = this.turns.get(turnId)?.startedAt;
267
275
  if (startedAt !== undefined) {
268
- this.startedTurns.delete(turnId);
276
+ this.turns.delete(turnId);
269
277
  }
270
278
  if (outcome) {
271
279
  const observedDuration =
@@ -284,7 +292,7 @@ export class TurnLifecycleMetrics {
284
292
  });
285
293
  }
286
294
  this.refreshGauges();
287
- if (this.startedTurns.size === 0) {
295
+ if (this.turns.size === 0) {
288
296
  this.stopTimer();
289
297
  }
290
298
  }
@@ -293,32 +301,46 @@ export class TurnLifecycleMetrics {
293
301
  this.observability.setGauge({
294
302
  name: "opengeni_turns_inflight",
295
303
  help: "Current number of in-flight agent turns in this worker process.",
296
- value: this.startedTurns.size,
304
+ value: this.turns.size,
297
305
  });
298
306
  this.observability.setGauge({
299
307
  name: "opengeni_turn_oldest_inflight_age_seconds",
300
308
  help: "Age in seconds of the oldest in-flight agent turn in this worker process.",
301
309
  value: this.oldestInflightAgeSeconds(),
302
310
  });
311
+ this.observability.setGauge({
312
+ name: "opengeni_turn_oldest_no_progress_age_seconds",
313
+ help: "Seconds since durable progress for the least recently progressing in-flight turn.",
314
+ value: this.oldestNoProgressAgeSeconds(),
315
+ });
303
316
  }
304
317
 
305
318
  stop(): void {
306
- this.startedTurns.clear();
319
+ this.turns.clear();
307
320
  this.refreshGauges();
308
321
  this.stopTimer();
309
322
  }
310
323
 
311
324
  private oldestInflightAgeSeconds(): number {
312
- if (this.startedTurns.size === 0) {
325
+ if (this.turns.size === 0) {
313
326
  return 0;
314
327
  }
315
328
  let oldest = Number.POSITIVE_INFINITY;
316
- for (const startedAt of this.startedTurns.values()) {
329
+ for (const { startedAt } of this.turns.values()) {
317
330
  oldest = Math.min(oldest, startedAt);
318
331
  }
319
332
  return Math.max(0, (this.now() - oldest) / 1000);
320
333
  }
321
334
 
335
+ private oldestNoProgressAgeSeconds(): number {
336
+ if (this.turns.size === 0) return 0;
337
+ let leastRecentProgress = Number.POSITIVE_INFINITY;
338
+ for (const { lastProgressAt } of this.turns.values()) {
339
+ leastRecentProgress = Math.min(leastRecentProgress, lastProgressAt);
340
+ }
341
+ return Math.max(0, (this.now() - leastRecentProgress) / 1000);
342
+ }
343
+
322
344
  private ensureTimer(): void {
323
345
  if (this.timer) {
324
346
  return;
@@ -806,6 +828,39 @@ export function recordCreditMicros(
806
828
  });
807
829
  }
808
830
 
831
+ const MODEL_REQUEST_PHASE_BUCKETS = [
832
+ 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.35, 0.5, 1, 2, 5, 10, 30, 60, 300,
833
+ ];
834
+
835
+ /**
836
+ * Record provider lifecycle diagnostics from the synchronous transport observer.
837
+ * Provider ids come from the resolved provider registry; phase/outcome are closed
838
+ * enums. This function never performs I/O outside the in-memory metrics registry.
839
+ */
840
+ export function recordModelRequestPhase(
841
+ observability: Observability,
842
+ input: {
843
+ provider: string;
844
+ phase: "headers" | "first_byte" | "terminal";
845
+ outcome?: "completed" | "failed" | "timed_out";
846
+ durationSeconds: number;
847
+ },
848
+ ): void {
849
+ const outcome = input.outcome ?? "";
850
+ observability.incrementCounter({
851
+ name: "opengeni_model_request_phases_total",
852
+ help: "Provider model-request lifecycle phases by bounded phase and outcome.",
853
+ labels: { provider: input.provider, phase: input.phase, outcome },
854
+ });
855
+ observability.observeHistogram({
856
+ name: "opengeni_model_request_phase_duration_seconds",
857
+ help: "Monotonic seconds from provider dispatch to a model-request lifecycle phase.",
858
+ buckets: MODEL_REQUEST_PHASE_BUCKETS,
859
+ labels: { provider: input.provider, phase: input.phase },
860
+ value: Math.max(0, input.durationSeconds),
861
+ });
862
+ }
863
+
809
864
  // ── Streaming SLIs ────────────────────────────────────────────────────────────
810
865
  // Instruments the token-streaming pipeline so "streaming is sluggish" is a number,
811
866
  // not a vibe. Split across the three attributable stages so an operator can tell
@@ -992,6 +1047,40 @@ export function recordContextCompaction(observability: Observability, trigger: s
992
1047
  });
993
1048
  }
994
1049
 
1050
+ const MODEL_CONTEXT_CONTRIBUTION_TOKEN_BUCKETS = [1, 8, 32, 128, 512, 2_048, 8_192];
1051
+
1052
+ /**
1053
+ * Metadata-only Company Brain exposure telemetry. Labels are closed enums and
1054
+ * never include tenant ids, record ids, titles, descriptions, or content.
1055
+ */
1056
+ export function recordCompanyBrainContributions(
1057
+ observability: Observability,
1058
+ receipt: CompanyBrainContributionReceipt,
1059
+ ): void {
1060
+ for (const contribution of receipt.contributions) {
1061
+ const labels = {
1062
+ category: contribution.category,
1063
+ source: contribution.source,
1064
+ inclusion_reason: contribution.inclusionReason,
1065
+ authority_scope: contribution.authorityScope,
1066
+ session_role: receipt.sessionRole,
1067
+ memory_prompt_mode: receipt.memoryPromptMode,
1068
+ };
1069
+ observability.incrementCounter({
1070
+ name: "opengeni_model_context_contributions_total",
1071
+ help: "Company Brain contributions exposed to a model, by bounded reason and authority.",
1072
+ labels,
1073
+ });
1074
+ observability.observeHistogram({
1075
+ name: "opengeni_model_context_contribution_estimated_tokens",
1076
+ help: "Estimated tokens per model-visible Company Brain contribution.",
1077
+ buckets: MODEL_CONTEXT_CONTRIBUTION_TOKEN_BUCKETS,
1078
+ labels,
1079
+ value: contribution.estimatedTokens,
1080
+ });
1081
+ }
1082
+ }
1083
+
995
1084
  // ── Prompt-cache efficiency ─────────────────────────────────────────────────
996
1085
  // Per model-call prompt-cache signal, provider-labelled only (bounded
997
1086
  // cardinality — never a session id or account). `cached_tokens` is the slice of
@@ -141,6 +141,8 @@ export type RoutingWiringIds = {
141
141
  * manifest environment variables." Optional → the resolver defaults to `{}`.
142
142
  */
143
143
  environment?: Record<string, string>;
144
+ /** Attempt-local values projected only onto Connected Machine child execs. */
145
+ transientExecEnvironment?: () => Readonly<Record<string, string>>;
144
146
  /**
145
147
  * Stage D machine-primary: PIN the already-established turn SelfhostedSession
146
148
  * (the `established` arg's session) for THIS machine pointer `(sandboxId, epoch)`
@@ -721,6 +723,9 @@ export function wrapTurnBoxWithRouting(
721
723
  // environment variables" throw when the turn pins to a vm). Mirrors the group
722
724
  // box, which is created WITH this same environment (resumeBoxForTurn).
723
725
  ...(ids.environment !== undefined ? { environment: ids.environment } : {}),
726
+ ...(ids.transientExecEnvironment !== undefined
727
+ ? { transientExecEnvironment: ids.transientExecEnvironment }
728
+ : {}),
724
729
  // Stage D machine-primary: pin THIS established SelfhostedSession for the machine
725
730
  // pointer so the resolver returns the SAME instance (no two-instance manifest
726
731
  // divergence). `established.session` is the SelfhostedSession the establish branch
@@ -907,6 +912,9 @@ export function wrapLazyTurnBoxWithRouting(
907
912
  ...selfhostedResolverTimeouts(settings),
908
913
  ...(onOp !== undefined ? { selfhostedOnOp: onOp } : {}),
909
914
  ...(ids.environment !== undefined ? { environment: ids.environment } : {}),
915
+ ...(ids.transientExecEnvironment !== undefined
916
+ ? { transientExecEnvironment: ids.transientExecEnvironment }
917
+ : {}),
910
918
  });
911
919
 
912
920
  const proxy = new RoutingSandboxSession({
@@ -1022,6 +1030,8 @@ export type SelfhostedTurnSessionArgs = {
1022
1030
  * the manifest), threaded so the SDK's per-turn provided-session env delta is
1023
1031
  * empty. */
1024
1032
  environment: Record<string, string>;
1033
+ /** Attempt-local values projected only onto this machine's child execs. */
1034
+ transientExecEnvironment?: () => Readonly<Record<string, string>>;
1025
1035
  /** The session working directory (per-session pointer). Null ⇒ workspace_root. */
1026
1036
  workingDir: string | null;
1027
1037
  };
@@ -1078,6 +1088,9 @@ export async function establishSelfhostedTurnSession(
1078
1088
  controlRpcFactory: controlRpcFactory(bus),
1079
1089
  epoch: args.epoch,
1080
1090
  environment: args.environment,
1091
+ ...(args.transientExecEnvironment !== undefined
1092
+ ? { transientExecEnvironment: args.transientExecEnvironment }
1093
+ : {}),
1081
1094
  workingDir: args.workingDir,
1082
1095
  // Give this turn's exec ops the long deadline (control ops stay short) so a real
1083
1096
  // command is not killed at the control wall.
@@ -5,6 +5,7 @@ type WorkflowControlActivities = Pick<
5
5
  typeof activities,
6
6
  | "enqueueGoalRetryWake"
7
7
  | "expireSessionHumanInput"
8
+ | "expireSessionInteractionIntervention"
8
9
  | "failSessionAttempt"
9
10
  | "getCodexCapacityWait"
10
11
  | "markSessionIdle"
@@ -2,8 +2,13 @@ import { proxyActivities } from "@temporalio/workflow";
2
2
  import type * as activities from "../activities";
3
3
 
4
4
  const reaperActivity = proxyActivities<
5
- Pick<typeof activities, "reapExpiredFileUploads" | "maintainRetainedScreenshots"> &
6
- Pick<typeof activities, "recoverVideoGenerationWorkflows">
5
+ Pick<
6
+ typeof activities,
7
+ | "reapExpiredFileUploads"
8
+ | "maintainRetainedScreenshots"
9
+ | "maintainBrowserStateArtifacts"
10
+ | "recoverVideoGenerationWorkflows"
11
+ >
7
12
  >({
8
13
  startToCloseTimeout: "5 minutes",
9
14
  retry: { maximumAttempts: 1 },
@@ -13,5 +18,6 @@ const reaperActivity = proxyActivities<
13
18
  export async function fileUploadReaperWorkflow(): Promise<void> {
14
19
  await reaperActivity.reapExpiredFileUploads();
15
20
  await reaperActivity.maintainRetainedScreenshots();
21
+ await reaperActivity.maintainBrowserStateArtifacts();
16
22
  await reaperActivity.recoverVideoGenerationWorkflows();
17
23
  }
@@ -288,7 +288,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
288
288
  }
289
289
  }
290
290
 
291
- async function waitForCodexCapacity(
291
+ async function waitForProviderCapacity(
292
292
  initial: activities.CodexCapacityWaitRef,
293
293
  entryBaseline?: { wakeups: number; capacityWakeups: number },
294
294
  ): Promise<void> {
@@ -329,13 +329,17 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
329
329
  ? "signal"
330
330
  : "timer";
331
331
  }
332
- const result = await activity.reconcileCodexCapacityWait({
332
+ const reconcileInput = {
333
333
  accountId: input.accountId,
334
334
  workspaceId: input.workspaceId,
335
335
  sessionId: input.sessionId,
336
336
  waiterId: current.waiterId,
337
337
  generation: current.generation,
338
338
  cause,
339
+ };
340
+ const result = await activity.reconcileCodexCapacityWait({
341
+ ...reconcileInput,
342
+ ...(current.provider ? { provider: current.provider } : {}),
339
343
  });
340
344
  if (result.action !== "waiting") {
341
345
  return;
@@ -467,7 +471,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
467
471
  return;
468
472
  }
469
473
  if (peek.kind === "capacity-wait") {
470
- await waitForCodexCapacity(peek.ref);
474
+ await waitForProviderCapacity(peek.ref);
471
475
  continue;
472
476
  }
473
477
  if (peek.kind === "approval-wait") {
@@ -475,7 +479,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
475
479
  const seenWakeups = wakeups;
476
480
  const seenInterruptionWakeups = interruptionWakeups;
477
481
  const timeoutMs =
478
- peek.humanInputRequestId && peek.expiresAt
482
+ (peek.humanInputRequestId || peek.interactionInterventionId) && peek.expiresAt
479
483
  ? humanInputDeadlineWaitMs(peek.expiresAt)
480
484
  : undefined;
481
485
  const wakeCondition = () =>
@@ -500,6 +504,16 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
500
504
  if (expiry.action === "stale") {
501
505
  await condition(wakeCondition, HUMAN_INPUT_EXPIRY_STALE_RETRY_MS);
502
506
  }
507
+ } else if (!woke && peek.interactionInterventionId) {
508
+ const expiry = await activity.expireSessionInteractionIntervention({
509
+ accountId: input.accountId,
510
+ workspaceId: input.workspaceId,
511
+ sessionId: input.sessionId,
512
+ interventionId: peek.interactionInterventionId,
513
+ });
514
+ if (expiry.action === "stale") {
515
+ await condition(wakeCondition, HUMAN_INPUT_EXPIRY_STALE_RETRY_MS);
516
+ }
503
517
  }
504
518
  continue;
505
519
  }
@@ -696,7 +710,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
696
710
  sessionId,
697
711
  });
698
712
  if (capacityWait) {
699
- await waitForCodexCapacity(capacityWait, capacityWaitEntryBaseline);
713
+ await waitForProviderCapacity(capacityWait, capacityWaitEntryBaseline);
700
714
  return true;
701
715
  }
702
716
  }
@@ -768,7 +782,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
768
782
  }
769
783
 
770
784
  if (outcome.result.capacityWait) {
771
- await waitForCodexCapacity(outcome.result.capacityWait, capacityWaitEntryBaseline);
785
+ await waitForProviderCapacity(outcome.result.capacityWait, capacityWaitEntryBaseline);
772
786
  return true;
773
787
  }
774
788
 
@@ -0,0 +1,14 @@
1
+ import { proxyActivities } from "@temporalio/workflow";
2
+ import type * as activities from "../activities";
3
+
4
+ const maintenanceActivity = proxyActivities<Pick<typeof activities, "maintainSiteAuthConnections">>(
5
+ {
6
+ startToCloseTimeout: "5 minutes",
7
+ retry: { maximumAttempts: 1 },
8
+ },
9
+ );
10
+
11
+ /** One bounded durable claim/dispatch sweep; the Temporal Schedule owns cadence. */
12
+ export async function siteAuthMaintenanceWorkflow(): Promise<void> {
13
+ await maintenanceActivity.maintainSiteAuthConnections();
14
+ }
package/src/workflows.ts CHANGED
@@ -26,6 +26,7 @@ export {
26
26
  sandboxReaperWorkflowV2,
27
27
  } from "./workflows/sandbox-reaper";
28
28
  export { fileUploadReaperWorkflow } from "./workflows/file-upload-reaper";
29
+ export { siteAuthMaintenanceWorkflow } from "./workflows/site-auth-maintenance";
29
30
  export {
30
31
  videoGenerationWorkflow,
31
32
  videoGenerationWorkflowId,