@opengeni/worker-bundle 0.12.5 → 0.12.10

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/src/activities.ts CHANGED
@@ -37,6 +37,8 @@ export type {
37
37
  RecoverDispatchInput,
38
38
  RecoverDispatchResult,
39
39
  PersistSessionAttemptQuiescenceInput,
40
+ ReconcileSessionAttemptQuiescenceInput,
41
+ ReconcileSessionAttemptQuiescenceResult,
40
42
  RunAgentTurnInput,
41
43
  RunAgentTurnResult,
42
44
  SessionAttemptQuiescenceProof,
@@ -102,6 +104,7 @@ function createActivityServices(
102
104
  observability,
103
105
  wakeSessionWorkflow: dependencies.wakeSessionWorkflow ?? null,
104
106
  signalSessionAttemptQuiesced: dependencies.signalSessionAttemptQuiesced ?? null,
107
+ inspectSessionAttemptActivity: dependencies.inspectSessionAttemptActivity ?? null,
105
108
  signalCodexCapacityWorkflow: dependencies.signalCodexCapacityWorkflow ?? null,
106
109
  // §7.5 P3 — host-entitlements port. No constructed default: standalone
107
110
  // has no host meter, so unset → null → `ensureRunAllowed` reads the
@@ -161,6 +164,8 @@ export const failSessionAttempt = defaultControlActivities.failSessionAttempt;
161
164
  export const settleSessionInterruptions = defaultControlActivities.settleSessionInterruptions;
162
165
  export const persistSessionAttemptQuiescence =
163
166
  defaultControlActivities.persistSessionAttemptQuiescence;
167
+ export const reconcileSessionAttemptQuiescence =
168
+ defaultControlActivities.reconcileSessionAttemptQuiescence;
164
169
  export const recoverDispatch = defaultControlActivities.recoverDispatch;
165
170
  export const peekSessionWork = defaultControlActivities.peekSessionWork;
166
171
  export const expireSessionHumanInput = defaultControlActivities.expireSessionHumanInput;
package/src/index.ts CHANGED
@@ -36,6 +36,7 @@ import {
36
36
  type ActivityDependencies,
37
37
  } from "./activities";
38
38
  import type {
39
+ InspectSessionAttemptActivity,
39
40
  SignalCodexCapacityWorkflow,
40
41
  SignalSessionAttemptQuiesced,
41
42
  WakeSessionWorkflowSignal,
@@ -98,6 +99,34 @@ export type WorkerOptions = {
98
99
  workflowBundle?: WorkflowBundleOption;
99
100
  };
100
101
 
102
+ type TemporalActivityLease = {
103
+ lastHeartbeatTime?: { seconds?: unknown; nanos?: number | null } | null;
104
+ lastStartedTime?: { seconds?: unknown; nanos?: number | null } | null;
105
+ activityOptions?: {
106
+ heartbeatTimeout?: { seconds?: unknown; nanos?: number | null } | null;
107
+ } | null;
108
+ };
109
+
110
+ function temporalTimeMs(
111
+ value: { seconds?: unknown; nanos?: number | null } | null | undefined,
112
+ ): number | null {
113
+ if (value?.seconds === undefined || value.seconds === null) return null;
114
+ return Number(String(value.seconds)) * 1_000 + Math.floor((value.nanos ?? 0) / 1_000_000);
115
+ }
116
+
117
+ export function temporalActivityLeaseSettled(
118
+ pending: TemporalActivityLease | null | undefined,
119
+ nowMs = Date.now(),
120
+ ): boolean {
121
+ if (!pending) return true;
122
+ const heartbeatAt =
123
+ temporalTimeMs(pending.lastHeartbeatTime) ?? temporalTimeMs(pending.lastStartedTime);
124
+ const heartbeatTimeoutMs = temporalTimeMs(pending.activityOptions?.heartbeatTimeout);
125
+ return (
126
+ heartbeatAt !== null && heartbeatTimeoutMs !== null && nowMs >= heartbeatAt + heartbeatTimeoutMs
127
+ );
128
+ }
129
+
101
130
  type WorkerWorkflowDefinition =
102
131
  | { workflowBundle: WorkflowBundleOption }
103
132
  | { workflowsPath: string };
@@ -220,6 +249,7 @@ export async function createWorkerWorkflowSignaler(
220
249
  ): Promise<{
221
250
  wakeSessionWorkflow: WakeSessionWorkflowSignal;
222
251
  signalSessionAttemptQuiesced: SignalSessionAttemptQuiesced;
252
+ inspectSessionAttemptActivity: InspectSessionAttemptActivity;
223
253
  signalCodexCapacityWorkflow: SignalCodexCapacityWorkflow;
224
254
  check: () => Promise<void>;
225
255
  close: () => Promise<void>;
@@ -280,6 +310,16 @@ export async function createWorkerWorkflowSignaler(
280
310
  // The signalled workflow's DB-only control activity owns committing the
281
311
  // receipt and its exact wake revision atomically.
282
312
  },
313
+ inspectSessionAttemptActivity: async ({ workflowId, workflowRunId, activityId }) => {
314
+ const description = await connection.workflowService.describeWorkflowExecution({
315
+ namespace: settings.temporalNamespace,
316
+ execution: { workflowId, runId: workflowRunId },
317
+ });
318
+ const pending = description.pendingActivities?.find(
319
+ (activity) => activity.activityId === activityId,
320
+ );
321
+ return temporalActivityLeaseSettled(pending) ? "settled" : "pending";
322
+ },
283
323
  signalCodexCapacityWorkflow: async ({
284
324
  accountId,
285
325
  workspaceId,
@@ -534,6 +574,7 @@ export async function createOpenGeniWorkerService(
534
574
  const needsSignaler =
535
575
  !options.activityDependencies.wakeSessionWorkflow ||
536
576
  !options.activityDependencies.signalSessionAttemptQuiesced ||
577
+ !options.activityDependencies.inspectSessionAttemptActivity ||
537
578
  !options.activityDependencies.signalCodexCapacityWorkflow;
538
579
  if (needsSignaler) {
539
580
  signaler = await retryStartupDependency(
@@ -547,10 +588,18 @@ export async function createOpenGeniWorkerService(
547
588
  const signalSessionAttemptQuiesced =
548
589
  options.activityDependencies.signalSessionAttemptQuiesced ??
549
590
  signaler?.signalSessionAttemptQuiesced;
591
+ const inspectSessionAttemptActivity =
592
+ options.activityDependencies.inspectSessionAttemptActivity ??
593
+ signaler?.inspectSessionAttemptActivity;
550
594
  const signalCodexCapacityWorkflow =
551
595
  options.activityDependencies.signalCodexCapacityWorkflow ??
552
596
  signaler?.signalCodexCapacityWorkflow;
553
- if (!wakeSessionWorkflow || !signalSessionAttemptQuiesced || !signalCodexCapacityWorkflow) {
597
+ if (
598
+ !wakeSessionWorkflow ||
599
+ !signalSessionAttemptQuiesced ||
600
+ !inspectSessionAttemptActivity ||
601
+ !signalCodexCapacityWorkflow
602
+ ) {
554
603
  throw new Error("OpenGeni worker lifecycle could not resolve its workflow signalers");
555
604
  }
556
605
  workerBundle = await createOpenGeniWorker({
@@ -564,6 +613,7 @@ export async function createOpenGeniWorkerService(
564
613
  observability,
565
614
  wakeSessionWorkflow,
566
615
  signalSessionAttemptQuiesced,
616
+ inspectSessionAttemptActivity,
567
617
  signalCodexCapacityWorkflow,
568
618
  },
569
619
  });
@@ -3,7 +3,6 @@ import type * as activities from "../activities";
3
3
 
4
4
  type WorkflowControlActivities = Pick<
5
5
  typeof activities,
6
- | "dispatchScheduledTaskRun"
7
6
  | "enqueueGoalRetryWake"
8
7
  | "expireSessionHumanInput"
9
8
  | "failSessionAttempt"
@@ -11,6 +10,7 @@ type WorkflowControlActivities = Pick<
11
10
  | "markSessionIdle"
12
11
  | "peekSessionWork"
13
12
  | "persistSessionAttemptQuiescence"
13
+ | "reconcileSessionAttemptQuiescence"
14
14
  | "reconcileCodexCapacityWait"
15
15
  | "recoverDispatch"
16
16
  | "settleSessionInterruptions"
@@ -34,6 +34,22 @@ export const activity = proxyActivities<WorkflowControlActivities>({
34
34
  },
35
35
  });
36
36
 
37
+ /** One schedule occurrence is bounded. Transient control-plane failures get a
38
+ * short retry window, but a revoked binding or other permanent task error must
39
+ * not leave a Temporal activity retrying forever; the next scheduled occurrence
40
+ * is an independent workflow and will re-evaluate current state. */
41
+ export const scheduledTaskActivity = proxyActivities<
42
+ Pick<typeof activities, "dispatchScheduledTaskRun">
43
+ >({
44
+ startToCloseTimeout: "2 minutes",
45
+ retry: {
46
+ initialInterval: "1 second",
47
+ backoffCoefficient: 2,
48
+ maximumInterval: "30 seconds",
49
+ maximumAttempts: 5,
50
+ },
51
+ });
52
+
37
53
  /** Goal continuation evaluates a durable Postgres obligation at an idle
38
54
  * boundary. A transient failure gets a short retry window, then records an
39
55
  * explicit delayed outbox wake instead of relying on an unrelated mutation or
@@ -1,24 +1,53 @@
1
- import { workflowInfo } from "@temporalio/workflow";
2
- import { activity } from "./activities";
1
+ import { patched, workflowInfo } from "@temporalio/workflow";
2
+ import type { TurnInitiator } from "@opengeni/contracts";
3
+ import { scheduledTaskActivity } from "./activities";
3
4
 
4
- export type ScheduledTaskFireWorkflowInput = {
5
+ type ScheduledTaskFireWorkflowBase = {
5
6
  accountId: string;
6
7
  workspaceId: string;
7
8
  taskId: string;
8
- triggerType: "scheduled" | "manual";
9
- agentRunUsageIdempotencyKey?: string;
10
9
  };
11
10
 
11
+ export type ScheduledTaskFireWorkflowInput = ScheduledTaskFireWorkflowBase &
12
+ (
13
+ | {
14
+ triggerType: "scheduled";
15
+ agentRunUsageIdempotencyKey?: never;
16
+ initiator?: never;
17
+ }
18
+ | {
19
+ triggerType: "manual";
20
+ agentRunUsageIdempotencyKey: string;
21
+ initiator: TurnInitiator;
22
+ }
23
+ );
24
+
12
25
  export async function scheduledTaskFireWorkflow(
13
26
  input: ScheduledTaskFireWorkflowInput,
14
27
  ): Promise<void> {
15
- await activity.dispatchScheduledTaskRun({
28
+ // Old malformed manual histories must terminate rather than retrying a usage
29
+ // conflict forever. All current producers are statically required to provide
30
+ // the exact charging identity.
31
+ if (
32
+ patched("scheduled-task-manual-initiator-v1") &&
33
+ input.triggerType === "manual" &&
34
+ (!input.agentRunUsageIdempotencyKey || !input.initiator)
35
+ ) {
36
+ return;
37
+ }
38
+ const base = {
16
39
  workspaceId: input.workspaceId,
17
40
  taskId: input.taskId,
18
- triggerType: input.triggerType,
19
41
  producerKey: workflowInfo().workflowId,
20
- ...(input.agentRunUsageIdempotencyKey
21
- ? { agentRunUsageIdempotencyKey: input.agentRunUsageIdempotencyKey }
22
- : {}),
23
- });
42
+ };
43
+ await scheduledTaskActivity.dispatchScheduledTaskRun(
44
+ input.triggerType === "manual"
45
+ ? {
46
+ ...base,
47
+ triggerType: "manual",
48
+ agentRunUsageIdempotencyKey: input.agentRunUsageIdempotencyKey,
49
+ initiator: input.initiator,
50
+ }
51
+ : { ...base, triggerType: "scheduled" },
52
+ );
24
53
  }
@@ -168,6 +168,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
168
168
  // order and workflow-side idempotent fallback. Keep that exact replay path;
169
169
  // every new run records v2 and uses the activity-owned receipt contract.
170
170
  const receiptGatedCancellation = patched("session-attempt-quiescence-v2");
171
+ const writerSetQuiescenceRecovery = patched("session-attempt-writer-set-quiescence-v1");
171
172
  const turnActivity = turnActivityForTaskQueue(workflowInfo().taskQueue, receiptGatedCancellation);
172
173
  let approvalWakeups = 0;
173
174
  let interruptionWakeups = 0;
@@ -393,6 +394,16 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
393
394
  continue;
394
395
  }
395
396
  if (peek.kind === "cancellation-wait") {
397
+ if (writerSetQuiescenceRecovery) {
398
+ const reconciliation = await activity.reconcileSessionAttemptQuiescence({
399
+ accountId: input.accountId,
400
+ workspaceId: input.workspaceId,
401
+ sessionId: input.sessionId,
402
+ attemptId: peek.attemptId,
403
+ workflowId,
404
+ });
405
+ if (reconciliation.action === "quiesced") continue;
406
+ }
396
407
  // Logical settlement is complete, but the exact predecessor activity has
397
408
  // not yet durably proved sandbox/tool quiescence. Wait only briefly for
398
409
  // its transactional queueChanged wake. If provider/tool cancellation is