@opengeni/worker-bundle 0.23.4 → 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 (84) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/claim.d.ts +1 -0
  3. package/dist/activities/agent-turn/codex.d.ts +2 -2
  4. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  5. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  6. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  7. package/dist/activities/agent-turn/run.d.ts +9 -0
  8. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  9. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  10. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  11. package/dist/activities/agent-turn/tool-environment.d.ts +2 -0
  12. package/dist/activities/agent-turn/tool-policy.d.ts +36 -5
  13. package/dist/activities/agent-turn/turn-context.d.ts +4 -1
  14. package/dist/activities/agent-turn.d.ts +1 -1
  15. package/dist/activities/automations.d.ts +1 -0
  16. package/dist/activities/capabilities.d.ts +3 -1
  17. package/dist/activities/context-compaction.d.ts +2 -0
  18. package/dist/activities/goals.d.ts +11 -0
  19. package/dist/activities/image-generation-references.d.ts +2 -1
  20. package/dist/activities/scheduled-tasks.d.ts +1 -0
  21. package/dist/activities/types.d.ts +8 -0
  22. package/dist/{activities-control-52W5IDWM.js → activities-control-ZWH7J4KZ.js} +297 -180
  23. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  24. package/dist/activities-control.d.ts +2 -0
  25. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-I4OPLQLS.js} +898 -333
  26. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  27. package/dist/activities.d.ts +2 -0
  28. package/dist/{chunk-GFNXVZVQ.js → chunk-65JEWP2Q.js} +107 -7
  29. package/dist/chunk-65JEWP2Q.js.map +1 -0
  30. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  31. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  32. package/dist/{chunk-IPVMCAW5.js → chunk-RQNH6CGR.js} +111 -62
  33. package/dist/chunk-RQNH6CGR.js.map +1 -0
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.js +43 -7
  36. package/dist/index.js.map +1 -1
  37. package/dist/observability-metrics.d.ts +34 -7
  38. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  39. package/dist/sandbox-resume.d.ts +6 -1
  40. package/dist/workflow-bundle.js +26 -3
  41. package/dist/workflows/activities.d.ts +5 -0
  42. package/dist/workflows/automations.d.ts +5 -0
  43. package/package.json +17 -17
  44. package/src/activities/agent-run-admission.ts +7 -1
  45. package/src/activities/agent-turn/admission.ts +47 -6
  46. package/src/activities/agent-turn/claim.ts +33 -5
  47. package/src/activities/agent-turn/codex.ts +2 -2
  48. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  49. package/src/activities/agent-turn/finalization.ts +31 -17
  50. package/src/activities/agent-turn/list-models.ts +101 -0
  51. package/src/activities/agent-turn/model-usage.ts +120 -45
  52. package/src/activities/agent-turn/quiescence.ts +11 -2
  53. package/src/activities/agent-turn/run.ts +80 -4
  54. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  55. package/src/activities/agent-turn/session-title.ts +55 -2
  56. package/src/activities/agent-turn/stream-attempt.ts +266 -135
  57. package/src/activities/agent-turn/tool-environment.ts +102 -7
  58. package/src/activities/agent-turn/tool-policy.ts +58 -5
  59. package/src/activities/agent-turn/turn-context.ts +8 -2
  60. package/src/activities/agent-turn.ts +5 -1
  61. package/src/activities/api-integrations.ts +11 -1
  62. package/src/activities/automations.ts +122 -67
  63. package/src/activities/capabilities.ts +129 -2
  64. package/src/activities/context-compaction.ts +3 -0
  65. package/src/activities/goals.ts +108 -31
  66. package/src/activities/image-generation-references.ts +9 -9
  67. package/src/activities/mcp-credentials.ts +53 -0
  68. package/src/activities/rig-verification.ts +5 -3
  69. package/src/activities/scheduled-tasks.ts +217 -110
  70. package/src/activities/site-auth-maintenance.ts +12 -12
  71. package/src/activities/types.ts +6 -0
  72. package/src/activities.ts +1 -0
  73. package/src/activity-services.ts +4 -1
  74. package/src/github-rest-mcp.ts +2 -1
  75. package/src/index.ts +34 -4
  76. package/src/observability-metrics.ts +154 -14
  77. package/src/sandbox-reaper-timeout.ts +10 -4
  78. package/src/sandbox-resume.ts +58 -8
  79. package/src/workflows/activities.ts +15 -0
  80. package/src/workflows/automations.ts +21 -2
  81. package/dist/activities-control-52W5IDWM.js.map +0 -1
  82. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  83. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  84. package/dist/chunk-IPVMCAW5.js.map +0 -1
@@ -17,12 +17,7 @@ import {
17
17
  } from "@opengeni/config";
18
18
  import { createRuntimeBatcher, currentActivityContext } from "../streaming";
19
19
  import type { TurnActivityServices as ActivityServices, RunAgentTurnInput } from "../types";
20
- import {
21
- maybePersistWarmWorkspaceSnapshot,
22
- persistSandboxDeadlineRotationCheckpoint,
23
- waitForWarmSnapshot,
24
- type ResumedTurnSandbox,
25
- } from "../../sandbox-resume";
20
+ import { maybePersistWarmWorkspaceSnapshot, type ResumedTurnSandbox } from "../../sandbox-resume";
26
21
  import { recordCreditMicros } from "../../observability-metrics";
27
22
  import { ChannelAPartialMutationError } from "@opengeni/runtime/sandbox";
28
23
 
@@ -51,6 +46,27 @@ export type SandboxTurnRuntimeDeps = {
51
46
 
52
47
  export type SandboxTurnRuntime = ReturnType<typeof createSandboxTurnRuntime>;
53
48
 
49
+ /**
50
+ * Provider-deadline rotation is a preemption boundary, not a cooperative
51
+ * snapshot boundary. Snapshotting while this attempt still owns active tools
52
+ * can deadlock on its own unsettled mutation admission or on an earlier native
53
+ * capture. Abort first; the ordinary attempt finalizer then drains every writer
54
+ * and releases the holder, after which the zero-holder reaper owns the exact
55
+ * generation capture and same-request takeover protocol.
56
+ */
57
+ export function preemptSandboxTurnForDeadlineRotation(input: {
58
+ controller: AbortController;
59
+ sandboxState: Pick<SandboxRuntimeState, "deadlineRotationRequested">;
60
+ sandboxGroupId: string;
61
+ leaseEpoch: number;
62
+ cancellationSignal?: AbortSignal;
63
+ }): boolean {
64
+ if (input.controller.signal.aborted || input.cancellationSignal?.aborted) return false;
65
+ input.sandboxState.deadlineRotationRequested = true;
66
+ input.controller.abort(new SandboxDeadlineRotationError(input.sandboxGroupId, input.leaseEpoch));
67
+ return true;
68
+ }
69
+
54
70
  export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
55
71
  const {
56
72
  input,
@@ -235,11 +251,12 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
235
251
  sandboxState.sandboxGroupId
236
252
  ) {
237
253
  // The lease still matches this turn's exact epoch/instance but a
238
- // rotation was requested. Start the same orderly checkpoint the
239
- // heartbeat would start on its next tick, so the model is not fed
240
- // retry errors until then. Best-effort and single-flight; the
241
- // admission failure itself is still surfaced to the caller.
242
- void beginRotationCheckpoint(
254
+ // rotation was requested. Start the same immediate preemption the
255
+ // heartbeat would start on its next tick, so this attempt cannot keep
256
+ // the provider generation pinned behind its own blocked mutation.
257
+ // Best-effort and single-flight; the admission failure itself is still
258
+ // surfaced to the caller.
259
+ void beginRotationPreemption(
243
260
  sandbox,
244
261
  sandbox.leaseEpoch,
245
262
  sandboxState.sandboxGroupId,
@@ -387,85 +404,60 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
387
404
  });
388
405
  };
389
406
  /**
390
- * Start (single-flight) the orderly provider-deadline rotation checkpoint for
391
- * the established box. Entered from the lease heartbeat once the lease is
392
- * rotation-fenced, and from a workspace-mutation admission that observed
393
- * `rotation_in_progress` at the same epoch/instance, so the model is not fed
394
- * retry errors for a whole heartbeat interval before the checkpoint runs.
407
+ * Start (single-flight) provider-deadline preemption for the established box.
408
+ * Entered from the lease heartbeat once the lease is rotation-fenced, and
409
+ * from a workspace-mutation admission that observed `rotation_in_progress` at
410
+ * the same epoch/instance. The active attempt is aborted immediately instead
411
+ * of waiting behind a turn-side snapshot: finalization first drains every
412
+ * writer and then releases the holder, licensing the zero-holder reaper's
413
+ * authoritative capture/takeover/termination protocol.
414
+ *
395
415
  * `not_rotating` means the exact epoch/instance no longer carries a requested
396
416
  * rotation (holder reaped, attempt closed, epoch superseded, or draining).
397
417
  */
398
- const beginRotationCheckpoint = async (
418
+ const beginRotationPreemption = async (
399
419
  sandbox: ResumedTurnSandbox,
400
420
  rotationEpoch: number,
401
421
  rotationGroupId: string,
402
422
  ): Promise<"started" | "busy" | "not_rotating"> => {
403
- // A cancelled attempt never starts (or reinstates a holder for) a
404
- // checkpoint; its cancellation settlement owns the holder from here.
423
+ // A cancelled attempt never starts another preemption check; its
424
+ // cancellation settlement owns the holder from here.
405
425
  if (
406
- sandboxState.rotationInFlight ||
426
+ sandboxState.rotationPreemptionInFlight ||
407
427
  sandboxRotationController.signal.aborted ||
408
428
  cancellationSignal?.aborted
409
429
  ) {
410
430
  return "busy";
411
431
  }
412
- const rotatingLease = await readLease(db, input.workspaceId, rotationGroupId).catch(() => null);
413
- if (
414
- !rotatingLease ||
415
- rotatingLease.leaseEpoch !== rotationEpoch ||
416
- rotatingLease.instanceId !== sandbox.established.instanceId ||
417
- rotatingLease.rotationRequestedAt === null
418
- ) {
419
- return "not_rotating";
432
+ if (sandboxState.rotationPreemptionInFlight || sandboxRotationController.signal.aborted) {
433
+ return "busy";
420
434
  }
421
- if (sandboxState.rotationInFlight || sandboxRotationController.signal.aborted) return "busy";
422
- sandboxState.rotationInFlight = (async () => {
423
- // Rotation admission already fenced all new workspace mutations.
424
- // Wait for an earlier periodic capture, then produce the exact
425
- // generation-complete checkpoint that licenses aborting this run.
426
- if (sandboxState.snapshotInFlight) {
427
- await waitForWarmSnapshot(
428
- sandboxState.snapshotInFlight,
429
- settings.sandboxSnapshotTimeoutMs,
430
- cancellationSignal,
431
- );
432
- }
433
- const snapshotSession = sandboxState.setupBoxSession;
434
- const snapshotTurnId = attempt.turnId;
435
- if (!snapshotSession || !snapshotTurnId) return;
436
- // The checkpoint helper first reinstates this turn's exact holder
437
- // when it was lost at the same epoch/instance (defense in depth:
438
- // the warm capture requires it), then forces the capture and
439
- // reports whether the established epoch now carries a complete
440
- // archive under its requested rotation.
441
- const checkpoint = await persistSandboxDeadlineRotationCheckpoint(
442
- { db, settings, objectStorage, observability },
443
- {
444
- accountId: input.accountId,
445
- workspaceId: input.workspaceId,
446
- sessionId: input.sessionId,
447
- turnId: snapshotTurnId,
448
- attemptId: input.attemptId,
449
- sandboxGroupId: rotationGroupId,
450
- },
451
- snapshotSession,
452
- { leaseEpoch: rotationEpoch, instanceId: sandbox.established.instanceId },
453
- cancellationSignal,
454
- );
455
- if (checkpoint.checkpointed) {
456
- sandboxRotationController.abort(
457
- new SandboxDeadlineRotationError(rotationGroupId, rotationEpoch),
458
- );
435
+ let disposition: "started" | "not_rotating" = "started";
436
+ sandboxState.rotationPreemptionInFlight = (async () => {
437
+ const rotatingLease = await readLease(db, input.workspaceId, rotationGroupId);
438
+ if (
439
+ rotatingLease?.leaseEpoch !== rotationEpoch ||
440
+ rotatingLease.instanceId !== sandbox.established.instanceId ||
441
+ rotatingLease.rotationRequestedAt === null
442
+ ) {
443
+ disposition = "not_rotating";
459
444
  return;
460
445
  }
461
- if (checkpoint.holder === "attempt_fenced" || checkpoint.holder === "lease_fenced") {
462
- // This attempt is no longer the active writer, or the exact
463
- // epoch/instance is gone. Nothing left to checkpoint here.
464
- stopLeaseHeartbeat();
465
- }
446
+
447
+ // Close the periodic producer before marking the turn for preemption.
448
+ // A tick that already launched a capture is joined by finalization via
449
+ // the capture promise's physical-settlement handle.
450
+ stopLeaseHeartbeat();
451
+ preemptSandboxTurnForDeadlineRotation({
452
+ controller: sandboxRotationController,
453
+ sandboxState,
454
+ sandboxGroupId: rotationGroupId,
455
+ leaseEpoch: rotationEpoch,
456
+ ...(cancellationSignal ? { cancellationSignal } : {}),
457
+ });
466
458
  })()
467
459
  .catch((error) => {
468
- observability.warn("sandbox deadline rotation checkpoint failed; retrying", {
460
+ observability.warn("sandbox deadline rotation preemption check failed; retrying", {
469
461
  workspaceId: input.workspaceId,
470
462
  sessionId: input.sessionId,
471
463
  sandboxGroupId: rotationGroupId,
@@ -474,10 +466,10 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
474
466
  });
475
467
  })
476
468
  .finally(() => {
477
- sandboxState.rotationInFlight = null;
469
+ sandboxState.rotationPreemptionInFlight = null;
478
470
  });
479
- await sandboxState.rotationInFlight;
480
- return "started";
471
+ await sandboxState.rotationPreemptionInFlight;
472
+ return disposition;
481
473
  };
482
474
  const startLeaseHeartbeat = (
483
475
  sandbox: ResumedTurnSandbox,
@@ -525,7 +517,7 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
525
517
  })
526
518
  .then(async (alive) => {
527
519
  if (alive) return;
528
- const rotation = await beginRotationCheckpoint(sandbox, heartbeatEpoch, heartbeatGroupId);
520
+ const rotation = await beginRotationPreemption(sandbox, heartbeatEpoch, heartbeatGroupId);
529
521
  if (rotation === "not_rotating") {
530
522
  // The holder was reaped, the exact attempt closed, the epoch was
531
523
  // superseded, or the lease began draining. Do not leave a dead
@@ -562,9 +554,10 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
562
554
  firstProviderRequestStarted: sandboxState.firstProviderRequestStarted,
563
555
  snapshotInFlight: Boolean(sandboxState.snapshotInFlight),
564
556
  turnEndCaptureInProgress: sandboxState.turnEndCaptureInProgress,
557
+ deadlineRotationRequested: sandboxState.deadlineRotationRequested,
565
558
  })
566
559
  ) {
567
- sandboxState.snapshotInFlight = maybePersistWarmWorkspaceSnapshot(
560
+ const snapshot = maybePersistWarmWorkspaceSnapshot(
568
561
  { db, settings, objectStorage },
569
562
  {
570
563
  accountId: input.accountId,
@@ -577,7 +570,9 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
577
570
  snapshotSession,
578
571
  heartbeatEpoch,
579
572
  activityContext?.cancellationSignal,
580
- )
573
+ );
574
+ sandboxState.snapshotInFlight = snapshot.settled;
575
+ void snapshot
581
576
  .then(async (persisted) => {
582
577
  if (persisted && eventing.publish) {
583
578
  await eventing.publish([
@@ -588,10 +583,12 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
588
583
  ]);
589
584
  }
590
585
  })
591
- .catch(() => undefined)
592
- .finally(() => {
586
+ .catch(() => undefined);
587
+ void snapshot.settled.then(() => {
588
+ if (sandboxState.snapshotInFlight === snapshot.settled) {
593
589
  sandboxState.snapshotInFlight = null;
594
- });
590
+ }
591
+ });
595
592
  }
596
593
  }, 10_000);
597
594
  if (
@@ -1,5 +1,6 @@
1
1
  import { hasPermission } from "@opengeni/core";
2
2
  import type { AttemptToolDefinition } from "@opengeni/codemode";
3
+ import type { GeneratedSessionTitle } from "@opengeni/runtime";
3
4
  import {
4
5
  AUTOMATIC_SESSION_TITLE_FALLBACK,
5
6
  DEFAULT_FIRST_PARTY_MCP_PERMISSIONS,
@@ -32,23 +33,75 @@ export function sessionTitleToolPlan(input: {
32
33
  tools: readonly ToolRef[];
33
34
  selectedFirstPartyMcpTools: readonly FirstPartyMcpToolName[];
34
35
  shouldRequestTitle: boolean;
36
+ parallelGenerationAvailable: boolean;
35
37
  }): {
36
38
  promoteTitleTool: boolean;
39
+ generateTitleInParallel: boolean;
37
40
  remoteFirstPartyMcpTools: FirstPartyMcpToolName[];
38
41
  preparationIndependentToolNames: string[];
39
42
  } {
40
- const promoteTitleTool =
43
+ const titleToolAvailable =
41
44
  input.shouldRequestTitle &&
42
45
  input.tools.some((tool) => tool.kind === "mcp" && tool.id === "opengeni");
46
+ const generateTitleInParallel = titleToolAvailable && input.parallelGenerationAvailable;
47
+ const promoteTitleTool = titleToolAvailable && !generateTitleInParallel;
43
48
  return {
44
49
  promoteTitleTool,
45
- remoteFirstPartyMcpTools: promoteTitleTool
50
+ generateTitleInParallel,
51
+ remoteFirstPartyMcpTools: titleToolAvailable
46
52
  ? input.selectedFirstPartyMcpTools.filter((tool) => tool !== "set_session_title")
47
53
  : [...input.selectedFirstPartyMcpTools],
48
54
  preparationIndependentToolNames: promoteTitleTool ? [SESSION_TITLE_MODEL_TOOL_NAME] : [],
49
55
  };
50
56
  }
51
57
 
58
+ export const PARALLEL_SESSION_TITLE_TIMEOUT_MS = 15_000;
59
+
60
+ export type ParallelSessionTitleGeneration = {
61
+ finish: () => Promise<GeneratedSessionTitle | null>;
62
+ cancel: () => Promise<void>;
63
+ };
64
+
65
+ /**
66
+ * Start title inference immediately and keep it independent of the main agent
67
+ * stream. finish() waits for the already-running bounded request, so a quick
68
+ * main response does not discard a valid title merely because it completed
69
+ * first. cancel() aborts and joins exceptional/cancelled exits so no provider
70
+ * work escapes the owning runAgentTurn activity.
71
+ */
72
+ export function startParallelSessionTitleGeneration(input: {
73
+ generate: (signal: AbortSignal) => Promise<GeneratedSessionTitle>;
74
+ signal?: AbortSignal;
75
+ timeoutMs?: number;
76
+ onError?: (error: unknown) => void;
77
+ }): ParallelSessionTitleGeneration {
78
+ const cancellationController = new AbortController();
79
+ const timeoutSignal = AbortSignal.timeout(input.timeoutMs ?? PARALLEL_SESSION_TITLE_TIMEOUT_MS);
80
+ const signals = [cancellationController.signal, timeoutSignal];
81
+ if (input.signal) signals.push(input.signal);
82
+ const signal = AbortSignal.any(signals);
83
+ const generation = input
84
+ .generate(signal)
85
+ .then((result) => (signal.aborted ? null : result))
86
+ .catch((error: unknown) => {
87
+ if (!signal.aborted) input.onError?.(error);
88
+ return null;
89
+ });
90
+ let finished: Promise<GeneratedSessionTitle | null> | null = null;
91
+
92
+ return {
93
+ finish: () => {
94
+ if (finished) return finished;
95
+ finished = generation;
96
+ return finished;
97
+ },
98
+ cancel: async () => {
99
+ cancellationController.abort();
100
+ await generation;
101
+ },
102
+ };
103
+ }
104
+
52
105
  export function createSessionTitleAttemptToolDefinition(input: {
53
106
  updateTitle: (title: string) => Promise<{ updated: boolean; title: string | null }>;
54
107
  }): AttemptToolDefinition {