@opengeni/worker-bundle 0.11.8 → 0.12.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.11.8",
3
+ "version": "0.12.1",
4
4
  "description": "OpenGeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -41,16 +41,16 @@
41
41
  "dependencies": {
42
42
  "@opengeni/agent-proto": "^0.3.0",
43
43
  "@opengeni/codex": "^0.2.7",
44
- "@opengeni/config": "^0.7.6",
45
- "@opengeni/contracts": "^0.19.4",
46
- "@opengeni/core": "^0.11.8",
47
- "@opengeni/db": "^0.12.6",
48
- "@opengeni/documents": "^0.2.36",
49
- "@opengeni/events": "^0.3.27",
50
- "@opengeni/github": "^0.3.18",
44
+ "@opengeni/config": "^0.7.8",
45
+ "@opengeni/contracts": "^0.20.1",
46
+ "@opengeni/core": "^0.12.1",
47
+ "@opengeni/db": "^0.13.1",
48
+ "@opengeni/documents": "^0.2.38",
49
+ "@opengeni/events": "^0.3.29",
50
+ "@opengeni/github": "^0.3.20",
51
51
  "@opengeni/observability": "^0.3.0",
52
- "@opengeni/runtime": "^0.13.10",
53
- "@opengeni/storage": "^0.2.30",
52
+ "@opengeni/runtime": "^0.13.12",
53
+ "@opengeni/storage": "^0.2.32",
54
54
  "@temporalio/activity": "^1.17.0",
55
55
  "@temporalio/client": "^1.17.0",
56
56
  "@temporalio/worker": "^1.17.0",
@@ -1,3 +1,13 @@
1
+ import {
2
+ OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
3
+ OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
4
+ OPENGENI_SLACK_BOT_SESSION_METADATA_KEY,
5
+ } from "@opengeni/contracts";
6
+ import {
7
+ openGeniSlackBotMetadata,
8
+ requireOpenGeniSlackBotConnection,
9
+ scheduledSlackBotConnectionId,
10
+ } from "@opengeni/core";
1
11
  import {
2
12
  appendSessionEventsWithLockedSessionUpdate,
3
13
  addSessionSystemUpdateWithSourceMutation,
@@ -41,6 +51,23 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
41
51
  ): Promise<DispatchScheduledTaskRunResult> => {
42
52
  const { settings, db, bus, wakeSessionWorkflow } = await services();
43
53
  const task = await requireScheduledTask(db, input.workspaceId, input.taskId);
54
+ // A scheduled bot selection was authorized when the task was written,
55
+ // but connection status and tenant/role binding are mutable. Revalidate
56
+ // before any session/model cost and never fall back to a personal Slack
57
+ // OAuth grant when the selected bot has been revoked or changed.
58
+ const slackBotConnection = task.agentConfig.slackBotConnectionId
59
+ ? await requireOpenGeniSlackBotConnection(
60
+ db,
61
+ task.workspaceId,
62
+ task.agentConfig.slackBotConnectionId,
63
+ )
64
+ : null;
65
+ const slackBotMetadata = slackBotConnection
66
+ ? openGeniSlackBotMetadata(slackBotConnection.metadata)
67
+ : null;
68
+ if (slackBotConnection && !slackBotMetadata) {
69
+ throw new Error("OpenGeni Slack bot connection metadata is invalid");
70
+ }
44
71
  // The scheduled task's model can be codex/<slug>; resolve it here so the
45
72
  // admission gate can skip the credit/cost gates for a codex-billed run
46
73
  // (paid by the user's ChatGPT/Codex plan). This file uses BASE settings (no
@@ -160,6 +187,8 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
160
187
  frozenRigVersionId = rig.activeVersion.id;
161
188
  }
162
189
  let session: Awaited<ReturnType<typeof createSession>>;
190
+ const taskMetadata = { ...task.agentConfig.metadata };
191
+ delete taskMetadata[OPENGENI_SLACK_BOT_SESSION_METADATA_KEY];
163
192
  try {
164
193
  session = await createSession(db, {
165
194
  accountId: task.accountId,
@@ -168,11 +197,16 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
168
197
  resources: task.agentConfig.resources,
169
198
  tools: taskTools,
170
199
  metadata: {
171
- ...task.agentConfig.metadata,
200
+ ...taskMetadata,
172
201
  model,
173
202
  reasoningEffort,
174
203
  scheduledTaskId: task.id,
175
204
  scheduledTaskRunId: run.id,
205
+ ...(slackBotConnection
206
+ ? {
207
+ [OPENGENI_SLACK_BOT_SESSION_METADATA_KEY]: slackBotConnection.id,
208
+ }
209
+ : {}),
176
210
  },
177
211
  createdBy: {
178
212
  kind: "service",
@@ -231,6 +265,16 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
231
265
  ...(variableSet
232
266
  ? { variableSetId: variableSet.id, variableSetName: variableSet.name }
233
267
  : {}),
268
+ ...(slackBotConnection && slackBotMetadata
269
+ ? {
270
+ slackBotConnection: {
271
+ credentialRole: OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
272
+ credentialLabel: OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
273
+ connectionId: slackBotConnection.id,
274
+ slackTeamId: slackBotMetadata.slackTeamId,
275
+ },
276
+ }
277
+ : {}),
234
278
  },
235
279
  },
236
280
  ...(goal
@@ -316,6 +360,14 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
316
360
  "scheduled task variableSet attachment does not match its reusable session",
317
361
  );
318
362
  }
363
+ if (
364
+ scheduledSlackBotConnectionId(session.metadata) !==
365
+ (task.agentConfig.slackBotConnectionId ?? null)
366
+ ) {
367
+ throw new Error(
368
+ "scheduled task OpenGeni Slack bot binding does not match its reusable session",
369
+ );
370
+ }
319
371
  // A recurring "maintain X" task re-establishes its objective on every
320
372
  // fire: replace the goal text, reactivate it, and reset the counters.
321
373
  const reusableGoal =
@@ -203,6 +203,49 @@ export type CaptureWorkspaceRevisionInput = {
203
203
  keepLatest?: number;
204
204
  };
205
205
 
206
+ type CaptureRepositoryReadResult =
207
+ | {
208
+ complete: true;
209
+ status: Awaited<ReturnType<SandboxChannelAService["gitStatus"]>>;
210
+ diff: Awaited<ReturnType<SandboxChannelAService["gitDiff"]>>;
211
+ }
212
+ | { complete: false; degradedReason: "repository_read_unavailable" };
213
+
214
+ /** One discovered repository is all-or-nothing capture authority. A provider
215
+ * transport failure must never be represented as an authoritative empty diff. */
216
+ export async function readCaptureRepository(
217
+ svc: Pick<SandboxChannelAService, "gitStatus" | "gitDiff">,
218
+ root: string,
219
+ ): Promise<CaptureRepositoryReadResult> {
220
+ let status: Awaited<ReturnType<SandboxChannelAService["gitStatus"]>>;
221
+ try {
222
+ status = await svc.gitStatus({ path: root });
223
+ } catch (error) {
224
+ const boxExiting = classifyCaptureEntryError(error);
225
+ if (boxExiting) throw boxExiting;
226
+ return { complete: false, degradedReason: "repository_read_unavailable" };
227
+ }
228
+ if (!status.isRepo) {
229
+ return { complete: false, degradedReason: "repository_read_unavailable" };
230
+ }
231
+ try {
232
+ const diff = await svc.gitDiff({
233
+ path: root,
234
+ staged: false,
235
+ includeUntracked: true,
236
+ fromRef: "HEAD",
237
+ pathspec: [],
238
+ contextLines: 3,
239
+ maxBytesPerFile: PER_FILE_DIFF_GUARD_BYTES,
240
+ });
241
+ return { complete: true, status, diff };
242
+ } catch (error) {
243
+ const boxExiting = classifyCaptureEntryError(error);
244
+ if (boxExiting) throw boxExiting;
245
+ return { complete: false, degradedReason: "repository_read_unavailable" };
246
+ }
247
+ }
248
+
206
249
  let loggedStorageNullOnce = false;
207
250
 
208
251
  /**
@@ -338,46 +381,15 @@ async function runCapture(
338
381
  );
339
382
  }
340
383
  if (!discovery.complete) {
341
- const capturedAt = new Date();
342
384
  const reason = captureDegradedReason(discovery.degradedReason);
343
- const stats = {
344
- degradedReason: reason,
345
- discoveredRepoCount: discovery.repos.length,
346
- durationMs: Date.now() - startedAt,
347
- };
348
- const inserted = await insertFailedWorkspaceCapture(input.db, {
349
- accountId: input.accountId,
350
- workspaceId: input.workspaceId,
351
- sessionId: input.sessionId,
352
- turnId: input.turnId,
353
- attemptId: input.attemptId,
354
- sandboxGroupId: input.sandboxGroupId,
355
- expectedEpoch: input.leaseEpoch,
385
+ await persistDegradedCapture(
386
+ input,
387
+ startedAt,
388
+ signal,
356
389
  revision,
357
- stats,
358
- capturedAt,
359
- });
360
- throwIfCaptureAborted(signal);
361
- if (!inserted) {
362
- observability.incrementCounter({
363
- name: "opengeni_workspace_capture_total",
364
- labels: { result: "superseded" },
365
- });
366
- return;
367
- }
368
- observability.warn("workspace capture degraded — repository discovery incomplete", {
369
- "opengeni.session_id": input.sessionId,
370
- "opengeni.turn_id": input.turnId ?? "",
371
- "workspace_capture.degraded_reason": reason,
372
- "workspace_capture.discovered_repo_count": discovery.repos.length,
373
- });
374
- observability.incrementCounter({
375
- name: "opengeni_workspace_capture_total",
376
- labels: { result: "degraded_repository_discovery" },
377
- });
378
- if (input.publish) {
379
- await input.publish(inserted.events).catch(() => undefined);
380
- }
390
+ reason,
391
+ discovery.repos.length,
392
+ );
381
393
  return;
382
394
  }
383
395
  const repoRoots = discovery.repos;
@@ -389,41 +401,24 @@ async function runCapture(
389
401
 
390
402
  for (const root of repoRoots) {
391
403
  throwIfCaptureAborted(signal);
392
- // Per-repo resilience: a box death aborts the whole capture (BoxExitingError
393
- // propagates); any other single-repo failure (a repo that vanished, a
394
- // transient git error) SKIPS that repo and the capture continues with the
395
- // rest. One flaky repo must never kill the capture.
396
- let status: Awaited<ReturnType<typeof svc.gitStatus>>;
397
- try {
398
- status = await svc.gitStatus({ path: root });
399
- throwIfCaptureAborted(signal);
400
- } catch (error) {
401
- const boxExiting = classifyCaptureEntryError(error);
402
- if (boxExiting) throw boxExiting;
403
- continue;
404
- }
405
- if (!status.isRepo) continue;
406
- // `git diff HEAD`: combined staged+unstaged tracked changes vs HEAD (the
407
- // review diff). Untracked files are absent here but present in status.files
408
- // and captured as after-images. A repo with no commits yields an empty diff
409
- // (git diff HEAD errors → empty numstat) and everything reads as untracked.
410
- let diff: Awaited<ReturnType<typeof svc.gitDiff>>;
411
- try {
412
- diff = await svc.gitDiff({
413
- path: root,
414
- staged: false,
415
- includeUntracked: true,
416
- fromRef: "HEAD",
417
- pathspec: [],
418
- contextLines: 3,
419
- maxBytesPerFile: PER_FILE_DIFF_GUARD_BYTES,
420
- });
421
- throwIfCaptureAborted(signal);
422
- } catch (error) {
423
- const boxExiting = classifyCaptureEntryError(error);
424
- if (boxExiting) throw boxExiting;
425
- diff = { files: [], revision: 0 };
404
+ // Repository discovery defines the authoritative set. Once a root is in
405
+ // that set, status/diff failure must degrade the entire revision rather than
406
+ // publish a partial or false-empty change surface. Box death still aborts
407
+ // through the outer best-effort boundary.
408
+ const repository = await readCaptureRepository(svc, root);
409
+ throwIfCaptureAborted(signal);
410
+ if (!repository.complete) {
411
+ await persistDegradedCapture(
412
+ input,
413
+ startedAt,
414
+ signal,
415
+ revision,
416
+ repository.degradedReason,
417
+ repoRoots.length,
418
+ );
419
+ return;
426
420
  }
421
+ const { status, diff } = repository;
427
422
  // Drop residue churn from the review diff too. On a desktop box the seed's
428
423
  // `git add -A` in $HOME commits ~/.config/xfce4/* into HEAD, so `git diff HEAD`
429
424
  // lists the continuously-rewritten desktop dotfiles as changed — noise the
@@ -851,6 +846,63 @@ function captureDegradedReason(
851
846
  }
852
847
  }
853
848
 
849
+ async function persistDegradedCapture(
850
+ input: CaptureWorkspaceRevisionInput,
851
+ startedAt: number,
852
+ signal: AbortSignal,
853
+ revision: number,
854
+ reason: WorkspaceCaptureDegradedReason,
855
+ discoveredRepoCount: number,
856
+ ): Promise<void> {
857
+ const capturedAt = new Date();
858
+ const stats = {
859
+ degradedReason: reason,
860
+ discoveredRepoCount,
861
+ durationMs: Date.now() - startedAt,
862
+ };
863
+ const inserted = await insertFailedWorkspaceCapture(input.db, {
864
+ accountId: input.accountId,
865
+ workspaceId: input.workspaceId,
866
+ sessionId: input.sessionId,
867
+ turnId: input.turnId,
868
+ attemptId: input.attemptId,
869
+ sandboxGroupId: input.sandboxGroupId,
870
+ expectedEpoch: input.leaseEpoch,
871
+ revision,
872
+ stats,
873
+ capturedAt,
874
+ });
875
+ throwIfCaptureAborted(signal);
876
+ if (!inserted) {
877
+ input.observability.incrementCounter({
878
+ name: "opengeni_workspace_capture_total",
879
+ labels: { result: "superseded" },
880
+ });
881
+ return;
882
+ }
883
+ const discoveryFailure = reason.startsWith("repository_discovery_");
884
+ input.observability.warn(
885
+ discoveryFailure
886
+ ? "workspace capture degraded — repository discovery incomplete"
887
+ : "workspace capture degraded — repository read incomplete",
888
+ {
889
+ "opengeni.session_id": input.sessionId,
890
+ "opengeni.turn_id": input.turnId ?? "",
891
+ "workspace_capture.degraded_reason": reason,
892
+ "workspace_capture.discovered_repo_count": discoveredRepoCount,
893
+ },
894
+ );
895
+ input.observability.incrementCounter({
896
+ name: "opengeni_workspace_capture_total",
897
+ labels: {
898
+ result: discoveryFailure ? "degraded_repository_discovery" : "degraded_repository_read",
899
+ },
900
+ });
901
+ if (input.publish) {
902
+ await input.publish(inserted.events).catch(() => undefined);
903
+ }
904
+ }
905
+
854
906
  function throwIfCaptureAborted(signal: AbortSignal): void {
855
907
  if (signal.aborted) {
856
908
  throw new Error("workspace capture cancelled after its deadline");
@@ -1,11 +1,63 @@
1
+ import type { Settings } from "@opengeni/config";
2
+ import type { WorkerOptions } from "@temporalio/worker";
3
+
4
+ export type TurnWorkerConcurrencySettings = Pick<
5
+ Settings,
6
+ | "turnWorkerConcurrencyMode"
7
+ | "turnWorkerMaxConcurrentTurns"
8
+ | "turnWorkerTargetCpuUsage"
9
+ | "turnWorkerTargetMemoryUsage"
10
+ >;
11
+
12
+ export type TurnWorkerConcurrencyOptions = Pick<
13
+ WorkerOptions,
14
+ "maxConcurrentActivityTaskExecutions" | "tuner"
15
+ >;
16
+
1
17
  /**
2
- * Process-local Temporal execution ceilings.
3
- *
4
- * Turn workers host only runAgentTurn, so this is the exact number of agent
5
- * turns one worker process may own concurrently. Keep the value centralized so
6
- * production and the real Temporal integration harness exercise one topology.
18
+ * Ordinary multi-worker deployments keep a fixed per-process ceiling so HPA or
19
+ * an operator owns horizontal capacity. The single-machine profile selects the
20
+ * resource tuner instead: Temporal admits activity slots while whole-system CPU
21
+ * and memory remain below the configured targets, then stops polling more work.
7
22
  */
8
- export const TURN_WORKER_MAX_CONCURRENT_TURNS = 16;
23
+ export function turnWorkerConcurrencyOptions(
24
+ settings: TurnWorkerConcurrencySettings,
25
+ ): TurnWorkerConcurrencyOptions {
26
+ if (settings.turnWorkerConcurrencyMode === "fixed") {
27
+ return {
28
+ maxConcurrentActivityTaskExecutions: settings.turnWorkerMaxConcurrentTurns,
29
+ };
30
+ }
31
+
32
+ return {
33
+ tuner: {
34
+ tunerOptions: {
35
+ targetCpuUsage: settings.turnWorkerTargetCpuUsage,
36
+ targetMemoryUsage: settings.turnWorkerTargetMemoryUsage,
37
+ },
38
+ activityTaskSlotOptions: {
39
+ minimumSlots: 1,
40
+ maximumSlots: settings.turnWorkerMaxConcurrentTurns,
41
+ rampThrottle: "50ms",
42
+ },
43
+ },
44
+ };
45
+ }
46
+
47
+ export function turnWorkerConcurrencyLogFields(settings: TurnWorkerConcurrencySettings): {
48
+ concurrencyMode: "fixed" | "resource-based";
49
+ maxConcurrentTurns: number;
50
+ targetCpuUsage: number | null;
51
+ targetMemoryUsage: number | null;
52
+ } {
53
+ const resourceBased = settings.turnWorkerConcurrencyMode === "resource-based";
54
+ return {
55
+ concurrencyMode: settings.turnWorkerConcurrencyMode,
56
+ maxConcurrentTurns: settings.turnWorkerMaxConcurrentTurns,
57
+ targetCpuUsage: resourceBased ? settings.turnWorkerTargetCpuUsage : null,
58
+ targetMemoryUsage: resourceBased ? settings.turnWorkerTargetMemoryUsage : null,
59
+ };
60
+ }
9
61
 
10
62
  export const CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES = 32;
11
63
  export const CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS = 40;
package/src/index.ts CHANGED
@@ -57,7 +57,8 @@ import {
57
57
  import {
58
58
  CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,
59
59
  CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,
60
- TURN_WORKER_MAX_CONCURRENT_TURNS,
60
+ turnWorkerConcurrencyLogFields,
61
+ turnWorkerConcurrencyOptions,
61
62
  } from "./concurrency";
62
63
  import {
63
64
  constructWithOwnedConnection,
@@ -179,13 +180,10 @@ export async function createOpenGeniWorker(options: WorkerOptions): Promise<{
179
180
  ? { workflowBundle: options.workflowBundle }
180
181
  : resolveOpenGeniWorkflowDefinition()),
181
182
  maxConcurrentWorkflowTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS,
183
+ maxConcurrentActivityTaskExecutions: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,
182
184
  }
183
- : {}),
185
+ : turnWorkerConcurrencyOptions(settings)),
184
186
  activities,
185
- maxConcurrentActivityTaskExecutions:
186
- options.role === "turn"
187
- ? TURN_WORKER_MAX_CONCURRENT_TURNS
188
- : CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,
189
187
  // Cancellation is delivered through an activity heartbeat. The SDK would
190
188
  // otherwise throttle a two-minute heartbeat timeout to its 60-second cap,
191
189
  // making Pause/Steer take roughly a minute even though runAgentTurn emits a
@@ -642,10 +640,14 @@ export async function createOpenGeniWorkerService(
642
640
  options.role === "control"
643
641
  ? settings.temporalTaskQueue
644
642
  : turnTaskQueue(settings.temporalTaskQueue),
645
- maxConcurrentActivityTaskExecutions:
646
- options.role === "turn"
647
- ? TURN_WORKER_MAX_CONCURRENT_TURNS
648
- : CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,
643
+ ...(options.role === "turn"
644
+ ? turnWorkerConcurrencyLogFields(settings)
645
+ : {
646
+ concurrencyMode: "fixed",
647
+ maxConcurrentTurns: CONTROL_WORKER_MAX_CONCURRENT_ACTIVITIES,
648
+ targetCpuUsage: null,
649
+ targetMemoryUsage: null,
650
+ }),
649
651
  maxConcurrentWorkflowTaskExecutions:
650
652
  options.role === "control" ? CONTROL_WORKER_MAX_CONCURRENT_WORKFLOW_TASKS : 0,
651
653
  httpPort: options.http === false ? null : settings.workerHttpPort,