@opengeni/worker-bundle 0.17.2 → 0.20.1

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 (78) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +49 -1
  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/run-input.d.ts +8 -0
  9. package/dist/activities/session-state.d.ts +7 -3
  10. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  11. package/dist/activities/types.d.ts +43 -0
  12. package/dist/activities/video-generation-admission.d.ts +9 -4
  13. package/dist/activities/video-generation-credential.d.ts +17 -0
  14. package/dist/activities/video-reference-staging.d.ts +7 -1
  15. package/dist/activities/xai-auth.d.ts +23 -0
  16. package/dist/activities/xai-image-generation.d.ts +26 -0
  17. package/dist/activities/xai-video-generation.d.ts +18 -0
  18. package/dist/{activities-control-44KBWLSM.js → activities-control-SMKF2OBE.js} +438 -55
  19. package/dist/activities-control-SMKF2OBE.js.map +1 -0
  20. package/dist/activities-control.d.ts +10 -2
  21. package/dist/{activities-turn-K4CEDEGJ.js → activities-turn-IDS3WEHA.js} +6704 -4847
  22. package/dist/activities-turn-IDS3WEHA.js.map +1 -0
  23. package/dist/activities.d.ts +11 -3
  24. package/dist/{chunk-E3RI3I33.js → chunk-N5TJIFCE.js} +156 -10
  25. package/dist/chunk-N5TJIFCE.js.map +1 -0
  26. package/dist/{chunk-KKCRGAGZ.js → chunk-P6Z2SLAA.js} +99 -98
  27. package/dist/chunk-P6Z2SLAA.js.map +1 -0
  28. package/dist/index.d.ts +14 -1
  29. package/dist/index.js +49 -2
  30. package/dist/index.js.map +1 -1
  31. package/dist/model-checkpoint-memory-collector.d.ts +1 -0
  32. package/dist/model-context-contributions.d.ts +40 -0
  33. package/dist/observability-metrics.d.ts +57 -1
  34. package/dist/sandbox-routing.d.ts +4 -0
  35. package/dist/workflow-bundle.js +232 -18
  36. package/dist/workflows/activities.d.ts +1 -1
  37. package/dist/workflows/session.d.ts +17 -1
  38. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  39. package/dist/workflows.d.ts +1 -0
  40. package/package.json +16 -15
  41. package/src/activities/agent-run-admission.ts +76 -0
  42. package/src/activities/agent-turn.ts +2777 -1241
  43. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  44. package/src/activities/capabilities.ts +10 -0
  45. package/src/activities/codex-capacity.ts +48 -1
  46. package/src/activities/environment.ts +14 -8
  47. package/src/activities/goals.ts +2 -1
  48. package/src/activities/packs.ts +45 -122
  49. package/src/activities/parent-wake.ts +13 -1
  50. package/src/activities/run-input.ts +137 -45
  51. package/src/activities/scheduled-tasks.ts +30 -73
  52. package/src/activities/session-state.ts +87 -5
  53. package/src/activities/site-auth-maintenance.ts +269 -0
  54. package/src/activities/types.ts +44 -0
  55. package/src/activities/video-generation-admission.ts +37 -4
  56. package/src/activities/video-generation-credential.ts +84 -6
  57. package/src/activities/video-generation-reconciliation.ts +212 -19
  58. package/src/activities/video-media-validation.ts +8 -2
  59. package/src/activities/video-reference-staging.ts +121 -34
  60. package/src/activities/xai-auth.ts +122 -0
  61. package/src/activities/xai-image-generation.ts +86 -0
  62. package/src/activities/xai-video-generation.ts +69 -0
  63. package/src/activities-control.ts +4 -0
  64. package/src/activities.ts +7 -0
  65. package/src/index.ts +51 -0
  66. package/src/model-checkpoint-memory-collector.ts +8 -1
  67. package/src/model-context-contributions.ts +159 -0
  68. package/src/observability-metrics.ts +282 -12
  69. package/src/sandbox-routing.ts +13 -0
  70. package/src/workflows/activities.ts +1 -0
  71. package/src/workflows/file-upload-reaper.ts +8 -2
  72. package/src/workflows/session.ts +219 -14
  73. package/src/workflows/site-auth-maintenance.ts +14 -0
  74. package/src/workflows.ts +1 -0
  75. package/dist/activities-control-44KBWLSM.js.map +0 -1
  76. package/dist/activities-turn-K4CEDEGJ.js.map +0 -1
  77. package/dist/chunk-E3RI3I33.js.map +0 -1
  78. package/dist/chunk-KKCRGAGZ.js.map +0 -1
@@ -0,0 +1,122 @@
1
+ import { environmentsEncryptionKeyBytes, type Settings } from "@opengeni/config";
2
+ import {
3
+ materializeXaiCredentialForRun,
4
+ refreshXaiSubscriptionCredentialSerialized,
5
+ type Database,
6
+ } from "@opengeni/db";
7
+ import type { XaiProviderAccountAuthoritySnapshotV1 } from "@opengeni/contracts";
8
+ import {
9
+ refreshXaiToken,
10
+ XAI_CLIENT_VERSION,
11
+ XAI_SUBSCRIPTION_MODEL_ID_PREFIX,
12
+ XaiSubscriptionReloginRequired,
13
+ xaiAccessTokenExpiry,
14
+ type XaiFetch,
15
+ type XaiSubscriptionRequestContext,
16
+ type XaiSubscriptionTokenSnapshot,
17
+ } from "@opengeni/xai-subscription";
18
+
19
+ export type XaiTurnRequestAuthorization = {
20
+ context: XaiSubscriptionRequestContext;
21
+ credentialId: string;
22
+ };
23
+
24
+ export async function buildXaiTurnRequestAuthorization(input: {
25
+ db: Database;
26
+ settings: Settings;
27
+ accountId: string;
28
+ workspaceId: string;
29
+ subjectId: string;
30
+ sessionId: string;
31
+ turnId: string;
32
+ credentialId: string;
33
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
34
+ fetch?: XaiFetch;
35
+ hostedSearch?: XaiSubscriptionRequestContext["hostedSearch"];
36
+ onFinalContextUsage?: XaiSubscriptionRequestContext["onFinalContextUsage"];
37
+ nextRequestId?: XaiSubscriptionRequestContext["nextRequestId"];
38
+ }): Promise<XaiTurnRequestAuthorization> {
39
+ const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
40
+ if (!encryptionKey) {
41
+ throw new Error("SuperGrok subscriptions require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
42
+ }
43
+ let credential = await materializeXaiCredentialForRun(input.db, {
44
+ workspaceId: input.workspaceId,
45
+ subjectId: input.subjectId,
46
+ credentialId: input.credentialId,
47
+ authoritySnapshot: input.authoritySnapshot,
48
+ encryptionKey,
49
+ });
50
+
51
+ const tokenSnapshot = (): XaiSubscriptionTokenSnapshot => {
52
+ const accessToken = credential.secret.accessToken;
53
+ const userId = credential.providerAccountId;
54
+ if (!accessToken || !userId) {
55
+ throw new XaiSubscriptionReloginRequired(
56
+ "The SuperGrok connection is missing a verified token identity. Reconnect the account.",
57
+ );
58
+ }
59
+ return { accessToken, userId };
60
+ };
61
+
62
+ const refresh = async (): Promise<XaiSubscriptionTokenSnapshot> => {
63
+ const observedAccessToken = credential.secret.accessToken;
64
+ const observedRefreshToken = credential.secret.refreshToken;
65
+ if (!observedRefreshToken) {
66
+ throw new XaiSubscriptionReloginRequired(
67
+ "The SuperGrok connection cannot be refreshed. Reconnect the account.",
68
+ );
69
+ }
70
+ const result = await refreshXaiSubscriptionCredentialSerialized(input.db, {
71
+ accountId: input.accountId,
72
+ workspaceId: input.workspaceId,
73
+ subjectId: input.subjectId,
74
+ credentialId: input.credentialId,
75
+ authoritySnapshot: input.authoritySnapshot,
76
+ encryptionKey,
77
+ observedAccessToken,
78
+ observedRefreshToken,
79
+ refresh: async (current) => {
80
+ const refreshToken = current.secret.refreshToken;
81
+ if (!refreshToken) {
82
+ throw new XaiSubscriptionReloginRequired(
83
+ "The SuperGrok connection cannot be refreshed. Reconnect the account.",
84
+ );
85
+ }
86
+ const tokens = await refreshXaiToken(refreshToken, {
87
+ ...(input.fetch ? { fetch: input.fetch } : {}),
88
+ });
89
+ return {
90
+ secret: {
91
+ version: 1,
92
+ accessToken: tokens.accessToken,
93
+ refreshToken: tokens.refreshToken,
94
+ },
95
+ expiresAt:
96
+ xaiAccessTokenExpiry(tokens.accessToken) ??
97
+ new Date(Date.now() + tokens.expiresInSeconds * 1_000),
98
+ };
99
+ },
100
+ });
101
+ credential = result.credential;
102
+ return tokenSnapshot();
103
+ };
104
+
105
+ return {
106
+ credentialId: input.credentialId,
107
+ context: {
108
+ clientVersion: XAI_CLIENT_VERSION,
109
+ sessionId: input.sessionId,
110
+ turnId: input.turnId,
111
+ getToken: async () => tokenSnapshot(),
112
+ refresh,
113
+ resolveModel: (model) =>
114
+ model.startsWith(XAI_SUBSCRIPTION_MODEL_ID_PREFIX)
115
+ ? model.slice(XAI_SUBSCRIPTION_MODEL_ID_PREFIX.length)
116
+ : model,
117
+ ...(input.hostedSearch ? { hostedSearch: input.hostedSearch } : {}),
118
+ ...(input.onFinalContextUsage ? { onFinalContextUsage: input.onFinalContextUsage } : {}),
119
+ ...(input.nextRequestId ? { nextRequestId: input.nextRequestId } : {}),
120
+ },
121
+ };
122
+ }
@@ -0,0 +1,86 @@
1
+ import {
2
+ generateXaiSubscriptionImage,
3
+ XAI_IMAGE_MODEL,
4
+ XAI_SUBSCRIPTION_PROVIDER_ID,
5
+ type XaiSubscriptionRequestContext,
6
+ } from "@opengeni/xai-subscription";
7
+ import type { Database } from "@opengeni/db";
8
+ import type { ObjectStorage } from "@opengeni/storage";
9
+ import type { GeneratedImageReceipt } from "./generated-images";
10
+ import {
11
+ executeImageGenerationOperation,
12
+ imageProviderBindingHash,
13
+ } from "./image-generation-operation";
14
+ import type { ResolvedImageGenerationReference } from "./image-generation-references";
15
+
16
+ export type XaiImageGenerationPorts = {
17
+ execute: typeof executeImageGenerationOperation;
18
+ generate: typeof generateXaiSubscriptionImage;
19
+ };
20
+
21
+ const xaiImageGenerationPorts: XaiImageGenerationPorts = {
22
+ execute: executeImageGenerationOperation,
23
+ generate: generateXaiSubscriptionImage,
24
+ };
25
+
26
+ /** Execute xAI's standalone subscription image path under the durable paid-operation fence. */
27
+ export async function executeXaiSubscriptionImageGeneration(
28
+ input: {
29
+ db: Database;
30
+ objectStorage: ObjectStorage | null;
31
+ accountId: string;
32
+ workspaceId: string;
33
+ sessionId: string;
34
+ turnId: string;
35
+ attemptId: string;
36
+ toolCallId: string;
37
+ prompt: string;
38
+ references?: readonly ResolvedImageGenerationReference[];
39
+ credentialId: string;
40
+ xaiContext: Pick<XaiSubscriptionRequestContext, "getToken" | "refresh">;
41
+ abortSignal?: AbortSignal;
42
+ },
43
+ ports: XaiImageGenerationPorts = xaiImageGenerationPorts,
44
+ ): Promise<GeneratedImageReceipt> {
45
+ const providerBindingHash = imageProviderBindingHash(
46
+ XAI_SUBSCRIPTION_PROVIDER_ID,
47
+ input.credentialId,
48
+ );
49
+ return await ports.execute({
50
+ db: input.db,
51
+ objectStorage: input.objectStorage,
52
+ accountId: input.accountId,
53
+ workspaceId: input.workspaceId,
54
+ sessionId: input.sessionId,
55
+ turnId: input.turnId,
56
+ attemptId: input.attemptId,
57
+ toolCallId: input.toolCallId,
58
+ prompt: input.prompt,
59
+ providerId: XAI_SUBSCRIPTION_PROVIDER_ID,
60
+ providerBindingHash,
61
+ modelId: XAI_IMAGE_MODEL,
62
+ generate: async () => {
63
+ const generated = await ports.generate({
64
+ prompt: input.prompt,
65
+ sessionId: input.sessionId,
66
+ ...(input.references?.length
67
+ ? {
68
+ references: input.references.map((reference) => ({
69
+ mediaType: reference.mediaType,
70
+ bytes: reference.bytes,
71
+ })),
72
+ }
73
+ : {}),
74
+ getToken: input.xaiContext.getToken,
75
+ refresh: input.xaiContext.refresh,
76
+ ...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
77
+ });
78
+ return {
79
+ toolCallId: input.toolCallId,
80
+ providerItemId: null,
81
+ bytes: generated.bytes,
82
+ declaredMediaType: generated.declaredMediaType,
83
+ };
84
+ },
85
+ });
86
+ }
@@ -0,0 +1,69 @@
1
+ import { GROK_IMAGINE_VIDEO_1_5_MODEL_ID } from "@opengeni/contracts";
2
+ import type { CanonicalVideoGenerationRequest } from "@opengeni/core";
3
+ import {
4
+ getXaiSubscriptionVideoStatus,
5
+ startXaiSubscriptionVideoWithBody,
6
+ XAI_VIDEO_MODEL,
7
+ type XaiFetch,
8
+ type XaiSubscriptionRequestContext,
9
+ type XaiVideoStatus,
10
+ } from "@opengeni/xai-subscription";
11
+ import type { GatewayVideoReferenceGrant } from "./gateway-video-generation";
12
+
13
+ export function buildXaiVideoStartBody(
14
+ request: CanonicalVideoGenerationRequest,
15
+ referenceGrants: readonly GatewayVideoReferenceGrant[],
16
+ ): Record<string, unknown> {
17
+ if (request.modelId !== GROK_IMAGINE_VIDEO_1_5_MODEL_ID) {
18
+ throw new Error("The selected video model is not a SuperGrok video model");
19
+ }
20
+ if (request.sourceMode === "first_and_last_frames" || request.sourceMode === "video_reference") {
21
+ throw new Error(`Grok Imagine Video does not support ${request.sourceMode}`);
22
+ }
23
+ if (referenceGrants.length !== request.references.length) {
24
+ throw new Error("Video reference grants do not match the sealed request");
25
+ }
26
+ const reference = referenceGrants[0];
27
+ return {
28
+ model: XAI_VIDEO_MODEL,
29
+ prompt: request.prompt,
30
+ duration: request.durationSeconds,
31
+ aspect_ratio: request.aspectRatio,
32
+ resolution: request.resolution,
33
+ ...(request.sourceMode === "first_frame" && reference
34
+ ? { image: { url: reference.url } }
35
+ : request.sourceMode === "image_reference" && reference
36
+ ? { reference_images: [{ url: reference.url }] }
37
+ : {}),
38
+ };
39
+ }
40
+
41
+ export async function startXaiVideoGeneration(input: {
42
+ body: Record<string, unknown>;
43
+ sessionId: string | null;
44
+ auth: Pick<XaiSubscriptionRequestContext, "getToken" | "refresh">;
45
+ fetch?: XaiFetch;
46
+ }): Promise<{ providerJobId: string }> {
47
+ return await startXaiSubscriptionVideoWithBody({
48
+ body: input.body,
49
+ getToken: input.auth.getToken,
50
+ refresh: input.auth.refresh,
51
+ ...(input.sessionId ? { sessionId: input.sessionId } : {}),
52
+ ...(input.fetch ? { fetch: input.fetch } : {}),
53
+ });
54
+ }
55
+
56
+ export async function getXaiVideoGenerationStatus(input: {
57
+ providerJobId: string;
58
+ sessionId: string | null;
59
+ auth: Pick<XaiSubscriptionRequestContext, "getToken" | "refresh">;
60
+ fetch?: XaiFetch;
61
+ }): Promise<XaiVideoStatus> {
62
+ return await getXaiSubscriptionVideoStatus({
63
+ providerJobId: input.providerJobId,
64
+ getToken: input.auth.getToken,
65
+ refresh: input.auth.refresh,
66
+ ...(input.sessionId ? { sessionId: input.sessionId } : {}),
67
+ ...(input.fetch ? { fetch: input.fetch } : {}),
68
+ });
69
+ }
@@ -1,5 +1,6 @@
1
1
  import { createSharedActivityServices } from "./activity-services";
2
2
  import { createCodexCapacityActivities } from "./activities/codex-capacity";
3
+ import { createBrowserStateArtifactMaintenanceActivities } from "./activities/browser-state-artifact-reaper";
3
4
  import { createDocumentActivities } from "./activities/documents";
4
5
  import { createFileUploadReaperActivities } from "./activities/file-upload-reaper";
5
6
  import { createGoalActivities } from "./activities/goals";
@@ -8,6 +9,7 @@ import { createRetainedScreenshotMaintenanceActivities } from "./activities/reta
8
9
  import { createRigVerificationActivities } from "./activities/rig-verification";
9
10
  import { createSandboxLeaseActivities } from "./activities/sandbox-lease";
10
11
  import { createScheduledTaskActivities } from "./activities/scheduled-tasks";
12
+ import { createSiteAuthMaintenanceActivities } from "./activities/site-auth-maintenance";
11
13
  import { createSessionStateActivities } from "./activities/session-state";
12
14
  import type { ActivityDependencies, ControlActivityServices } from "./activities/types";
13
15
  import { createWorkflowWakeActivities } from "./activities/workflow-wake";
@@ -28,9 +30,11 @@ export function createControlActivitiesFromServices(
28
30
  ...createKnowledgeSourceSyncActivities(services, resolveDocumentServices),
29
31
  ...createSessionStateActivities(services),
30
32
  ...createScheduledTaskActivities(services),
33
+ ...createSiteAuthMaintenanceActivities(services),
31
34
  ...createGoalActivities(services),
32
35
  ...createCodexCapacityActivities(services),
33
36
  ...createRigVerificationActivities(services),
37
+ ...createBrowserStateArtifactMaintenanceActivities(services),
34
38
  ...createFileUploadReaperActivities(services),
35
39
  ...createRetainedScreenshotMaintenanceActivities(services),
36
40
  ...createWorkflowWakeActivities(services),
package/src/activities.ts CHANGED
@@ -27,6 +27,7 @@ export type {
27
27
  MaybeContinueGoalInput,
28
28
  MaybeContinueGoalResult,
29
29
  CodexCapacityWaitRef,
30
+ XaiCapacityWaitRef,
30
31
  GetCodexCapacityWaitInput,
31
32
  ReconcileCodexCapacityWaitInput,
32
33
  ReconcileCodexCapacityWaitResult,
@@ -37,6 +38,8 @@ export type {
37
38
  PersistSessionAttemptQuiescenceInput,
38
39
  ReconcileSessionAttemptQuiescenceInput,
39
40
  ReconcileSessionAttemptQuiescenceResult,
41
+ FailSessionAttemptInput,
42
+ FailSessionAttemptResult,
40
43
  RunKnowledgeSourceSyncBatchInput,
41
44
  RunKnowledgeSourceSyncBatchResult,
42
45
  RunAgentTurnInput,
@@ -97,6 +100,8 @@ export const recoverDispatch = defaultControlActivities.recoverDispatch;
97
100
  export const recoverEscapedMcpTimeout = defaultControlActivities.recoverEscapedMcpTimeout;
98
101
  export const peekSessionWork = defaultControlActivities.peekSessionWork;
99
102
  export const expireSessionHumanInput = defaultControlActivities.expireSessionHumanInput;
103
+ export const expireSessionInteractionIntervention =
104
+ defaultControlActivities.expireSessionInteractionIntervention;
100
105
  export const markSessionIdle = defaultControlActivities.markSessionIdle;
101
106
  export const dispatchScheduledTaskRun = defaultControlActivities.dispatchScheduledTaskRun;
102
107
  export const runKnowledgeSourceSyncBatch = defaultControlActivities.runKnowledgeSourceSyncBatch;
@@ -112,6 +117,8 @@ export const reapExpiredFileUploads = defaultControlActivities.reapExpiredFileUp
112
117
  export const recoverVideoGenerationWorkflows =
113
118
  defaultControlActivities.recoverVideoGenerationWorkflows;
114
119
  export const maintainRetainedScreenshots = defaultControlActivities.maintainRetainedScreenshots;
120
+ export const maintainBrowserStateArtifacts = defaultControlActivities.maintainBrowserStateArtifacts;
121
+ export const maintainSiteAuthConnections = defaultControlActivities.maintainSiteAuthConnections;
115
122
  export const dispatchSessionWorkflowWakes = defaultControlActivities.dispatchSessionWorkflowWakes;
116
123
  export const verifyRigChange = defaultControlActivities.verifyRigChange;
117
124
  export const verifyRigVersion = defaultControlActivities.verifyRigVersion;
package/src/index.ts CHANGED
@@ -108,6 +108,8 @@ export {
108
108
  const SANDBOX_REAPER_SCHEDULE_ID = "opengeni-sandbox-lease-reaper";
109
109
  export const FILE_UPLOAD_REAPER_SCHEDULE_ID = "opengeni-file-upload-reaper";
110
110
  export const FILE_UPLOAD_REAPER_PERIOD_MS = 15 * 60 * 1_000;
111
+ export const SITE_AUTH_MAINTENANCE_SCHEDULE_ID = "opengeni-site-auth-maintenance";
112
+ export const SITE_AUTH_MAINTENANCE_PERIOD_MS = 60 * 1_000;
111
113
  export type OpenGeniWorkerRole = "control" | "turn";
112
114
 
113
115
  export type WorkerOptions = {
@@ -640,6 +642,48 @@ export async function registerFileUploadReaperSchedule(
640
642
  }
641
643
  }
642
644
 
645
+ /** Register the deployment-wide maintained-auth dispatcher. Its one-minute
646
+ * cadence matches the minimum public policy interval; DB claims own overlap,
647
+ * crash recovery, and exact session idempotency. */
648
+ export async function registerSiteAuthMaintenanceSchedule(
649
+ settings: Settings,
650
+ observability: Observability,
651
+ ): Promise<{ registered: boolean; close: () => Promise<void> }> {
652
+ const connection = await Connection.connect(temporalConnectionOptions(settings));
653
+ const temporal = new TemporalClient({ connection, namespace: settings.temporalNamespace });
654
+ try {
655
+ await temporal.schedule.create({
656
+ scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,
657
+ spec: { intervals: [{ every: SITE_AUTH_MAINTENANCE_PERIOD_MS }] },
658
+ action: {
659
+ type: "startWorkflow",
660
+ workflowType: "siteAuthMaintenanceWorkflow",
661
+ taskQueue: settings.temporalTaskQueue,
662
+ args: [],
663
+ },
664
+ policies: {
665
+ overlap: ScheduleOverlapPolicy.SKIP,
666
+ catchupWindow: "1m",
667
+ pauseOnFailure: false,
668
+ },
669
+ });
670
+ observability.info("Registered the global site-auth maintenance Schedule", {
671
+ scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,
672
+ maintenancePeriodMs: SITE_AUTH_MAINTENANCE_PERIOD_MS,
673
+ });
674
+ return { registered: true, close: async () => connection.close() };
675
+ } catch (error) {
676
+ if (error instanceof ScheduleAlreadyRunning) {
677
+ observability.info("Global site-auth maintenance Schedule already registered", {
678
+ scheduleId: SITE_AUTH_MAINTENANCE_SCHEDULE_ID,
679
+ });
680
+ return { registered: false, close: async () => connection.close() };
681
+ }
682
+ await connection.close().catch(() => undefined);
683
+ throw error;
684
+ }
685
+ }
686
+
643
687
  /**
644
688
  * Register the one repair cadence for committed workflow-wake revisions. The
645
689
  * activity only reads the transactional outbox and sends revision-scoped
@@ -829,6 +873,13 @@ export async function createOpenGeniWorkerService(
829
873
  { ...retryOptions, onRetry },
830
874
  ),
831
875
  );
876
+ schedules.push(
877
+ await retryStartupDependency(
878
+ "Temporal schedule (site auth maintenance)",
879
+ () => registerSiteAuthMaintenanceSchedule(settings, observability),
880
+ { ...retryOptions, onRetry },
881
+ ),
882
+ );
832
883
  schedules.push(
833
884
  await retryStartupDependency(
834
885
  "Temporal schedule (session-workflow wake dispatcher)",
@@ -16,6 +16,13 @@ export type ModelCheckpointMemoryCollector = {
16
16
  schedule(observability: Observability): void;
17
17
  };
18
18
 
19
+ export function modelCheckpointPressureBytes(memory: ProcessMemory): number {
20
+ // `external` includes ArrayBuffer storage in Bun/Node's memoryUsage contract.
21
+ // Heap and external allocations are independent, so max(heap, external)
22
+ // undercounts the actual reclaimable pressure whenever both are large.
23
+ return memory.heapUsed + memory.external;
24
+ }
25
+
19
26
  /**
20
27
  * Reclaim request serialization and stream buffers only after one model
21
28
  * response has crossed the durable conversation checkpoint. The callback runs
@@ -51,7 +58,7 @@ export function createModelCheckpointMemoryCollector(
51
58
  return;
52
59
  }
53
60
 
54
- const pressureBytes = Math.max(memory.heapUsed, memory.external);
61
+ const pressureBytes = modelCheckpointPressureBytes(memory);
55
62
  const observedAt = now();
56
63
  if (
57
64
  !Number.isFinite(pressureBytes) ||
@@ -0,0 +1,159 @@
1
+ import type {
2
+ PreferenceRegistrySnapshot,
3
+ ResolvedCompanyProfileSnapshot,
4
+ ResolvedWorkspaceInstructionPolicySnapshot,
5
+ WorkspaceMemoryPromptMode,
6
+ } from "@opengeni/contracts";
7
+ import { composeRuntimeSkills, type RuntimeSkillActivation } from "@opengeni/runtime";
8
+
9
+ export type CompanyBrainContributionCategory =
10
+ | "mandatory_rule"
11
+ | "skill_guide_descriptor"
12
+ | "retrieved_knowledge"
13
+ | "task_note";
14
+
15
+ export type CompanyBrainContribution = Readonly<{
16
+ category: CompanyBrainContributionCategory;
17
+ source:
18
+ | "workspace_instruction_policy"
19
+ | "legacy_workspace_instructions"
20
+ | "preference_registry_descriptor"
21
+ | "company_profile"
22
+ | "legacy_memory_v1"
23
+ | "runtime_skill_catalog";
24
+ inclusionReason:
25
+ | "active_instruction_policy"
26
+ | "legacy_instruction_fallback"
27
+ | "active_preference_descriptor"
28
+ | "active_company_profile_root"
29
+ | "legacy_standing_working_set"
30
+ | "authorized_skill_descriptor";
31
+ authorityScope: "organization" | "workspace" | "user" | "session";
32
+ utf8Bytes: number;
33
+ estimatedTokens: number;
34
+ }>;
35
+
36
+ export type CompanyBrainContributionReceipt = Readonly<{
37
+ attemptId: string;
38
+ turnId: string;
39
+ sessionRole: "root" | "child";
40
+ memoryPromptMode: WorkspaceMemoryPromptMode;
41
+ instructionPolicySnapshotId: string;
42
+ preferenceSnapshotId: string | null;
43
+ companyProfileSnapshotId: string;
44
+ contributions: readonly CompanyBrainContribution[];
45
+ }>;
46
+
47
+ /**
48
+ * Build a content-free exact-attempt receipt for the Company Brain material
49
+ * selected before inference. Token counts are deliberately estimates (UTF-8
50
+ * bytes / 4); provider-accounted total input tokens remain the separate source
51
+ * of truth for billing and context pressure.
52
+ */
53
+ export function buildCompanyBrainContributionReceipt(input: {
54
+ attemptId: string;
55
+ turnId: string;
56
+ nestedAgentDepth: number;
57
+ memoryPromptMode: WorkspaceMemoryPromptMode;
58
+ instructionPolicy: ResolvedWorkspaceInstructionPolicySnapshot;
59
+ workspaceAgentInstructions: string | null;
60
+ preferences: PreferenceRegistrySnapshot | null;
61
+ companyProfile: ResolvedCompanyProfileSnapshot;
62
+ companyProfileIncluded: boolean;
63
+ workspaceMemory: string | null;
64
+ skillActivations: readonly RuntimeSkillActivation[];
65
+ }): CompanyBrainContributionReceipt {
66
+ const contributions: CompanyBrainContribution[] = [];
67
+ for (const entry of input.instructionPolicy.entries) {
68
+ contributions.push(
69
+ contribution({
70
+ category: "mandatory_rule",
71
+ source: "workspace_instruction_policy",
72
+ inclusionReason: "active_instruction_policy",
73
+ authorityScope: "workspace",
74
+ text: entry.content,
75
+ }),
76
+ );
77
+ }
78
+ if (input.instructionPolicy.entries.length === 0 && input.workspaceAgentInstructions) {
79
+ contributions.push(
80
+ contribution({
81
+ category: "mandatory_rule",
82
+ source: "legacy_workspace_instructions",
83
+ inclusionReason: "legacy_instruction_fallback",
84
+ authorityScope: "workspace",
85
+ text: input.workspaceAgentInstructions,
86
+ }),
87
+ );
88
+ }
89
+ for (const descriptor of input.preferences?.descriptors ?? []) {
90
+ contributions.push(
91
+ contribution({
92
+ category: "skill_guide_descriptor",
93
+ source: "preference_registry_descriptor",
94
+ inclusionReason: "active_preference_descriptor",
95
+ authorityScope: descriptor.scope,
96
+ text: JSON.stringify(descriptor),
97
+ }),
98
+ );
99
+ }
100
+ if (input.companyProfileIncluded && input.companyProfile.profile) {
101
+ contributions.push(
102
+ contribution({
103
+ category: "retrieved_knowledge",
104
+ source: "company_profile",
105
+ inclusionReason: "active_company_profile_root",
106
+ authorityScope: "organization",
107
+ text: JSON.stringify(input.companyProfile.profile.profile),
108
+ }),
109
+ );
110
+ }
111
+ if (input.workspaceMemory?.trim()) {
112
+ contributions.push(
113
+ contribution({
114
+ category: "retrieved_knowledge",
115
+ source: "legacy_memory_v1",
116
+ inclusionReason: "legacy_standing_working_set",
117
+ authorityScope: "workspace",
118
+ text: input.workspaceMemory,
119
+ }),
120
+ );
121
+ }
122
+ for (const descriptor of composeRuntimeSkills(input.skillActivations).configuredDescriptors) {
123
+ contributions.push(
124
+ contribution({
125
+ category: "skill_guide_descriptor",
126
+ source: "runtime_skill_catalog",
127
+ inclusionReason: "authorized_skill_descriptor",
128
+ authorityScope: descriptor.source === "session" ? "session" : "workspace",
129
+ text: descriptor.description,
130
+ }),
131
+ );
132
+ }
133
+ return Object.freeze({
134
+ attemptId: input.attemptId,
135
+ turnId: input.turnId,
136
+ sessionRole: input.nestedAgentDepth === 0 ? "root" : "child",
137
+ memoryPromptMode: input.memoryPromptMode,
138
+ instructionPolicySnapshotId: input.instructionPolicy.id,
139
+ preferenceSnapshotId: input.preferences?.id ?? null,
140
+ companyProfileSnapshotId: input.companyProfile.id,
141
+ contributions: Object.freeze(contributions),
142
+ });
143
+ }
144
+
145
+ function contribution(
146
+ input: Omit<CompanyBrainContribution, "utf8Bytes" | "estimatedTokens"> & {
147
+ text: string;
148
+ },
149
+ ): CompanyBrainContribution {
150
+ const utf8Bytes = Buffer.byteLength(input.text, "utf8");
151
+ return Object.freeze({
152
+ category: input.category,
153
+ source: input.source,
154
+ inclusionReason: input.inclusionReason,
155
+ authorityScope: input.authorityScope,
156
+ utf8Bytes,
157
+ estimatedTokens: Math.ceil(utf8Bytes / 4),
158
+ });
159
+ }