@opengeni/worker-bundle 0.2.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.
@@ -0,0 +1,222 @@
1
+ import { Settings } from '@opengeni/config';
2
+ import { Observability } from '@opengeni/observability';
3
+ import { Worker, NativeConnection } from '@temporalio/worker';
4
+ import { EntitlementsPort, ConnectionCredentialsPort, ScheduledTaskTriggerType } from '@opengeni/contracts';
5
+ import { Database } from '@opengeni/db';
6
+ import { DocumentServices } from '@opengeni/documents';
7
+ import { EventBus } from '@opengeni/events';
8
+ import { OpenGeniRuntime } from '@opengeni/runtime';
9
+ import { ObjectStorage } from '@opengeni/storage';
10
+
11
+ type WakeSessionWorkflowSignal = (input: {
12
+ accountId: string;
13
+ workspaceId: string;
14
+ sessionId: string;
15
+ workflowId: string;
16
+ }) => Promise<void>;
17
+ type ActivityServices = {
18
+ settings: Settings;
19
+ db: Database;
20
+ bus: EventBus;
21
+ runtime: OpenGeniRuntime;
22
+ objectStorage: ObjectStorage | null;
23
+ documentServices: DocumentServices;
24
+ observability: Observability;
25
+ wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
26
+ entitlements?: EntitlementsPort | null;
27
+ connectionCredentials?: ConnectionCredentialsPort | null;
28
+ };
29
+ type ActivityDependencies = Partial<ActivityServices>;
30
+ type RunAgentTurnInput = {
31
+ accountId: string;
32
+ workspaceId: string;
33
+ sessionId: string;
34
+ triggerEventId: string;
35
+ workflowId: string;
36
+ turnId?: string;
37
+ };
38
+ type RequeueTurnAfterWorkerDeathInput = {
39
+ accountId: string;
40
+ workspaceId: string;
41
+ sessionId: string;
42
+ triggerEventId: string;
43
+ workflowId: string;
44
+ turnId: string;
45
+ };
46
+ type RequeueTurnAfterWorkerDeathResult = {
47
+ action: "requeued";
48
+ redispatches: number;
49
+ } | {
50
+ action: "stale";
51
+ } | {
52
+ action: "exceeded";
53
+ redispatches: number;
54
+ };
55
+ type ClaimNextQueuedTurnInput = {
56
+ workspaceId: string;
57
+ sessionId: string;
58
+ workflowId: string;
59
+ };
60
+ type MarkSessionIdleInput = {
61
+ workspaceId: string;
62
+ sessionId: string;
63
+ };
64
+ type MaybeContinueGoalInput = {
65
+ accountId: string;
66
+ workspaceId: string;
67
+ sessionId: string;
68
+ workflowId: string;
69
+ };
70
+ type MaybeContinueGoalResult = {
71
+ action: "none" | "queue" | "continue" | "paused";
72
+ };
73
+ type PauseGoalForInterruptInput = {
74
+ workspaceId: string;
75
+ sessionId: string;
76
+ triggerEventId?: string;
77
+ };
78
+ type DispatchScheduledTaskRunInput = {
79
+ workspaceId: string;
80
+ taskId: string;
81
+ triggerType: ScheduledTaskTriggerType;
82
+ agentRunUsageIdempotencyKey?: string;
83
+ };
84
+ type DispatchScheduledTaskRunResult = {
85
+ action: "start" | "signal";
86
+ accountId: string;
87
+ workspaceId: string;
88
+ sessionId: string;
89
+ triggerEventId: string;
90
+ workflowId: string;
91
+ };
92
+ type IndexDocumentInput = {
93
+ accountId: string;
94
+ workspaceId: string;
95
+ documentId: string;
96
+ };
97
+ type RunAgentTurnResult = {
98
+ status: "idle" | "requires_action" | "failed" | "cancelled" | "preempted";
99
+ continueDelayMs?: number;
100
+ idleUntilReset?: boolean;
101
+ };
102
+
103
+ type ReapSandboxLeasesResult = {
104
+ /** Stale viewer holders + warming-death rows the sweep touched is folded into
105
+ * the DB pass; here we report the terminate outcomes. */
106
+ examined: number;
107
+ /** Boxes whose provider terminate fired AND whose lease went cold. */
108
+ terminated: number;
109
+ /** Drainable rows skipped because the CAS no longer held (re-armed / newer
110
+ * epoch / already drained by a concurrent sweep) — provider stop() did NOT
111
+ * fire. */
112
+ skipped: number;
113
+ /** Warm viewer-only leases that accrued warm-seconds this tick (P2.1). */
114
+ metered: number;
115
+ /** Viewer-only boxes force-drained because their workspace is over a limit
116
+ * (0 balance / over the warm cap) — turn-held boxes are never drained (P2.1). */
117
+ forceDrained: number;
118
+ };
119
+
120
+ declare function createActivities(dependencies?: ActivityDependencies): {
121
+ reapSandboxLeases: () => Promise<ReapSandboxLeasesResult>;
122
+ maybeContinueGoal: (input: MaybeContinueGoalInput) => Promise<MaybeContinueGoalResult>;
123
+ pauseGoalForInterrupt: (input: PauseGoalForInterruptInput) => Promise<void>;
124
+ dispatchScheduledTaskRun: (input: DispatchScheduledTaskRunInput) => Promise<DispatchScheduledTaskRunResult>;
125
+ failSession: (input: RunAgentTurnInput & {
126
+ error?: string;
127
+ }) => Promise<void>;
128
+ interruptActiveTurn: (input: RunAgentTurnInput) => Promise<void>;
129
+ requeueTurnAfterWorkerDeath: (input: RequeueTurnAfterWorkerDeathInput) => Promise<RequeueTurnAfterWorkerDeathResult>;
130
+ claimNextQueuedTurn: (input: ClaimNextQueuedTurnInput) => Promise<{
131
+ id: string;
132
+ workspaceId: string;
133
+ sessionId: string;
134
+ triggerEventId: string;
135
+ temporalWorkflowId: string;
136
+ status: "queued" | "running" | "requires_action" | "failed" | "cancelled" | "completed";
137
+ source: "user" | "scheduled_task" | "api" | "goal";
138
+ position: number;
139
+ prompt: string;
140
+ resources: ({
141
+ kind: "repository";
142
+ uri: string;
143
+ ref: string;
144
+ mountPath?: string | undefined;
145
+ subpath?: string | undefined;
146
+ githubInstallationId?: number | undefined;
147
+ githubRepositoryId?: number | undefined;
148
+ } | {
149
+ kind: "file";
150
+ fileId: string;
151
+ mountPath?: string | undefined;
152
+ })[];
153
+ tools: {
154
+ kind: "mcp";
155
+ id: string;
156
+ optional?: boolean | undefined;
157
+ }[];
158
+ model: string;
159
+ reasoningEffort: "none" | "minimal" | "low" | "medium" | "high" | "xhigh";
160
+ sandboxBackend: "local" | "none" | "docker" | "modal" | "daytona" | "runloop" | "e2b" | "blaxel" | "cloudflare" | "vercel" | "selfhosted";
161
+ sandboxOs: "linux" | "macos" | "windows" | null;
162
+ metadata: Record<string, unknown>;
163
+ startedAt: string | null;
164
+ finishedAt: string | null;
165
+ createdAt: string;
166
+ updatedAt: string;
167
+ } | null>;
168
+ markSessionIdle: (input: MarkSessionIdleInput) => Promise<void>;
169
+ indexDocument: (input: IndexDocumentInput) => Promise<{
170
+ id: string;
171
+ workspaceId: string;
172
+ baseId: string;
173
+ fileId: string;
174
+ status: "queued" | "failed" | "ready" | "indexing";
175
+ title: string;
176
+ parser: string;
177
+ chunkCount: number;
178
+ error: string | null;
179
+ createdAt: string;
180
+ updatedAt: string;
181
+ }>;
182
+ runAgentTurn: (input: RunAgentTurnInput) => Promise<RunAgentTurnResult>;
183
+ runAgentSegment: (input: RunAgentTurnInput) => Promise<RunAgentTurnResult>;
184
+ };
185
+
186
+ type WorkerOptions = {
187
+ settings?: Settings;
188
+ activities?: ReturnType<typeof createActivities>;
189
+ activityDependencies?: ActivityDependencies;
190
+ };
191
+ declare function createOpenGeniWorker(options?: WorkerOptions): Promise<{
192
+ worker: Worker;
193
+ connection: NativeConnection;
194
+ }>;
195
+ declare function createWorkerWorkflowSignaler(settings: Settings): Promise<{
196
+ wakeSessionWorkflow: WakeSessionWorkflowSignal;
197
+ close: () => Promise<void>;
198
+ }>;
199
+ /**
200
+ * Register the ONE global reaper Temporal Schedule (the sole liveness/GC/cost-stop
201
+ * driver — P1.3 / OD-3). Gated on sandboxOwnershipEnabled: with the flag off the
202
+ * Schedule is never created, so the lease feature is fully dark (no sweeps, no
203
+ * terminates).
204
+ *
205
+ * The Schedule fires sandboxReaperWorkflow on the worker's global task queue
206
+ * every settings.sandboxLeaseReaperPeriodMs (the SAME cadence the boot invariant
207
+ * `reaperPeriod < viewerHolderTTL` and `reaperPeriod + idleGrace < providerLifetime`
208
+ * validates in packages/config — wiring the schedule period to it). SKIP overlap means a slow
209
+ * sweep never overlaps itself. Idempotent: a duplicate scheduleId across the
210
+ * worker pool collides on ScheduleAlreadyRunning and no-ops, so the Schedule is
211
+ * registered exactly once per deployment.
212
+ *
213
+ * Returns a `close()` for the dedicated client connection (separate from the
214
+ * worker's NativeConnection — the Schedule client is a @temporalio/client).
215
+ */
216
+ declare function registerSandboxReaperSchedule(settings: Settings, observability: Observability): Promise<{
217
+ registered: boolean;
218
+ close: () => Promise<void>;
219
+ }>;
220
+ declare function startWorker(): Promise<void>;
221
+
222
+ export { type WorkerOptions, createOpenGeniWorker, createWorkerWorkflowSignaler, registerSandboxReaperSchedule, startWorker };