@opengeni/worker-bundle 0.7.2 → 0.7.3
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/dist/index.d.ts +5 -4
- package/dist/index.js +63 -68
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/activities/agent-turn.ts +63 -84
- package/src/activities/common.ts +8 -2
- package/src/activities/goals.ts +1 -1
- package/src/activities/parent-wake.ts +5 -18
- package/src/activities/scheduled-tasks.ts +2 -2
- package/src/activities/session-state.ts +10 -13
- package/src/activities/types.ts +6 -6
- package/src/activities.ts +1 -1
- package/src/index.ts +4 -4
- package/src/workflows/activities.ts +8 -2
- package/src/workflows/session.ts +78 -97
- package/src/workflows/workflow-wake.ts +2 -1
- package/src/workflow-wake-contract.ts +0 -5
package/src/workflows/session.ts
CHANGED
|
@@ -51,10 +51,14 @@ const ROTATION_IDLE_FLOOR_MS = 60_000; // 60s
|
|
|
51
51
|
* Pure + exported so the boundedness contract is unit-testable without a workflow env.
|
|
52
52
|
*/
|
|
53
53
|
export function continuationHoldMs(
|
|
54
|
-
result: {
|
|
54
|
+
result: {
|
|
55
|
+
status: string;
|
|
56
|
+
continueDelayMs?: number;
|
|
57
|
+
idleUntilReset?: boolean;
|
|
58
|
+
},
|
|
55
59
|
floorMs: number,
|
|
56
60
|
): number {
|
|
57
|
-
if (result.status !== "idle") {
|
|
61
|
+
if (result.status !== "idle" && result.status !== "recovering") {
|
|
58
62
|
return 0;
|
|
59
63
|
}
|
|
60
64
|
const delay = result.continueDelayMs ?? 0;
|
|
@@ -97,7 +101,7 @@ function workerDeathFailure(
|
|
|
97
101
|
export const userMessage = defineSignal<[string]>("userMessage");
|
|
98
102
|
export const queueChanged = defineSignal("queueChanged");
|
|
99
103
|
export const approvalDecision = defineSignal<[string]>("approvalDecision");
|
|
100
|
-
export const sessionControl = defineSignal
|
|
104
|
+
export const sessionControl = defineSignal("sessionControl");
|
|
101
105
|
export const codexCapacityChanged = defineSignal<[number]>("codexCapacityChanged");
|
|
102
106
|
|
|
103
107
|
export type SessionWorkflowInput = {
|
|
@@ -118,7 +122,7 @@ export type SessionWorkflowInput = {
|
|
|
118
122
|
export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void> {
|
|
119
123
|
const turnActivity = turnActivityForTaskQueue(workflowInfo().taskQueue);
|
|
120
124
|
let approvalWakeups = 0;
|
|
121
|
-
let
|
|
125
|
+
let interruptionWakeups = 0;
|
|
122
126
|
let wakeups = 0;
|
|
123
127
|
let capacityWakeups = 0;
|
|
124
128
|
let signalVersion = 0;
|
|
@@ -144,9 +148,9 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
144
148
|
nonControlSignalVersion += 1;
|
|
145
149
|
approvalWakeups += 1;
|
|
146
150
|
});
|
|
147
|
-
setHandler(sessionControl, (
|
|
151
|
+
setHandler(sessionControl, () => {
|
|
148
152
|
signalVersion += 1;
|
|
149
|
-
|
|
153
|
+
interruptionWakeups += 1;
|
|
150
154
|
});
|
|
151
155
|
setHandler(codexCapacityChanged, () => {
|
|
152
156
|
signalVersion += 1;
|
|
@@ -161,14 +165,12 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
161
165
|
let current = initial;
|
|
162
166
|
let firstEntryBaseline = entryBaseline;
|
|
163
167
|
for (;;) {
|
|
164
|
-
if (controlEventId !== null) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
168
|
// Signals can land after the waiter commit but before runAgentTurn returns.
|
|
168
169
|
// Compare the first wait against pre-dispatch counters so they cannot be
|
|
169
170
|
// baselined away; later iterations use their normal local snapshot.
|
|
170
171
|
const seenWakeups = firstEntryBaseline?.wakeups ?? wakeups;
|
|
171
172
|
const seenCapacityWakeups = firstEntryBaseline?.capacityWakeups ?? capacityWakeups;
|
|
173
|
+
const seenInterruptionWakeups = interruptionWakeups;
|
|
172
174
|
firstEntryBaseline = undefined;
|
|
173
175
|
const parsedDeadline = Date.parse(current.nextCheckAt);
|
|
174
176
|
const timerMs = Number.isFinite(parsedDeadline)
|
|
@@ -182,12 +184,12 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
182
184
|
} else if (timerMs > 0) {
|
|
183
185
|
await condition(
|
|
184
186
|
() =>
|
|
185
|
-
|
|
187
|
+
interruptionWakeups !== seenInterruptionWakeups ||
|
|
186
188
|
wakeups !== seenWakeups ||
|
|
187
189
|
capacityWakeups !== seenCapacityWakeups,
|
|
188
190
|
timerMs,
|
|
189
191
|
);
|
|
190
|
-
if (
|
|
192
|
+
if (interruptionWakeups !== seenInterruptionWakeups) {
|
|
191
193
|
return;
|
|
192
194
|
}
|
|
193
195
|
cause =
|
|
@@ -211,10 +213,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
211
213
|
capacityChecksThisRun += 1;
|
|
212
214
|
const capacityCheckBackstop =
|
|
213
215
|
input.maxCapacityChecksPerRun ?? CODEX_CAPACITY_CHECKS_PER_RUN_BACKSTOP;
|
|
214
|
-
if (
|
|
215
|
-
controlEventId === null &&
|
|
216
|
-
(workflowInfo().continueAsNewSuggested || capacityChecksThisRun >= capacityCheckBackstop)
|
|
217
|
-
) {
|
|
216
|
+
if (workflowInfo().continueAsNewSuggested || capacityChecksThisRun >= capacityCheckBackstop) {
|
|
218
217
|
// The waiter/outbox is durable in Postgres. A fresh workflow run reads
|
|
219
218
|
// it before goal continuation, reconstructs its timer, and turns any
|
|
220
219
|
// unobserved wake revision into an immediate evaluation.
|
|
@@ -235,10 +234,10 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
235
234
|
while (true) {
|
|
236
235
|
// History-overflow guard. The top of the loop is the only safe
|
|
237
236
|
// continueAsNew boundary: no turn is mid-flight (every path that reaches a
|
|
238
|
-
// new iteration settled or recovered its turn first)
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
//
|
|
237
|
+
// new iteration settled or recovered its turn first). Every interruption
|
|
238
|
+
// is already durable in Postgres, so all Temporal signals are replaceable
|
|
239
|
+
// wake hints and none must be carried into the next workflow run. A
|
|
240
|
+
// buffered userMessage/queueChanged signal only
|
|
242
241
|
// bumps `wakeups`, and its turn was written to Postgres BEFORE the signal
|
|
243
242
|
// was sent, so the fresh run observes it on its first durable work peek
|
|
244
243
|
// — losing the counter strands nothing. The queue living in Postgres is the
|
|
@@ -255,7 +254,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
255
254
|
const info = workflowInfo();
|
|
256
255
|
const maxTurnsPerRun = input.maxTurnsPerRun ?? TURNS_PER_RUN_BACKSTOP;
|
|
257
256
|
const shouldContinue = info.continueAsNewSuggested || turnsThisRun >= maxTurnsPerRun;
|
|
258
|
-
if (shouldContinue
|
|
257
|
+
if (shouldContinue) {
|
|
259
258
|
await continueAsNew<typeof sessionWorkflow>({
|
|
260
259
|
accountId: input.accountId,
|
|
261
260
|
workspaceId: input.workspaceId,
|
|
@@ -277,42 +276,16 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
277
276
|
workspaceId: input.workspaceId,
|
|
278
277
|
sessionId: input.sessionId,
|
|
279
278
|
});
|
|
280
|
-
if (peek.kind === "
|
|
281
|
-
const settlement = await activity.
|
|
279
|
+
if (peek.kind === "interruption-pending") {
|
|
280
|
+
const settlement = await activity.settleSessionInterruptions({
|
|
282
281
|
accountId: input.accountId,
|
|
283
282
|
workspaceId: input.workspaceId,
|
|
284
283
|
sessionId: input.sessionId,
|
|
285
|
-
|
|
284
|
+
attemptId: peek.attemptId,
|
|
286
285
|
workflowId,
|
|
287
286
|
});
|
|
288
|
-
if (controlEventId === peek.controlEventId) controlEventId = null;
|
|
289
287
|
if (settlement.action === "paused") {
|
|
290
|
-
|
|
291
|
-
// settlement. It must not manufacture a second loop; only later work
|
|
292
|
-
// or a different control keeps the workflow alive.
|
|
293
|
-
if (nonControlSignalVersion !== closeNonControlSignalVersion || controlEventId !== null) {
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
// A duplicate/repaired control signal may arrive after its durable fence
|
|
301
|
-
// already settled. Explicitly classify it through the same settlement
|
|
302
|
-
// activity and clear the in-memory signal so it cannot pin this workflow
|
|
303
|
-
// run forever or block continue-as-new.
|
|
304
|
-
if (controlEventId !== null) {
|
|
305
|
-
const signaledControlEventId = controlEventId;
|
|
306
|
-
const settlement = await activity.settleSessionControl({
|
|
307
|
-
accountId: input.accountId,
|
|
308
|
-
workspaceId: input.workspaceId,
|
|
309
|
-
sessionId: input.sessionId,
|
|
310
|
-
triggerEventId: signaledControlEventId,
|
|
311
|
-
workflowId,
|
|
312
|
-
});
|
|
313
|
-
if (controlEventId === signaledControlEventId) controlEventId = null;
|
|
314
|
-
if (settlement.action === "paused") {
|
|
315
|
-
if (nonControlSignalVersion !== closeNonControlSignalVersion || controlEventId !== null) {
|
|
288
|
+
if (nonControlSignalVersion !== closeNonControlSignalVersion) {
|
|
316
289
|
continue;
|
|
317
290
|
}
|
|
318
291
|
return;
|
|
@@ -326,40 +299,40 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
326
299
|
if (peek.kind === "approval-wait") {
|
|
327
300
|
const seenApprovalWakeups = approvalWakeups;
|
|
328
301
|
const seenWakeups = wakeups;
|
|
302
|
+
const seenInterruptionWakeups = interruptionWakeups;
|
|
329
303
|
await condition(
|
|
330
304
|
() =>
|
|
331
|
-
|
|
305
|
+
interruptionWakeups !== seenInterruptionWakeups ||
|
|
332
306
|
approvalWakeups !== seenApprovalWakeups ||
|
|
333
307
|
wakeups !== seenWakeups,
|
|
334
308
|
);
|
|
335
309
|
continue;
|
|
336
310
|
}
|
|
337
311
|
if (peek.kind === "idle") {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
if (continuation.action === "continue" || continuation.action === "queue") continue;
|
|
312
|
+
let continuation: activities.MaybeContinueGoalResult = { action: "none" };
|
|
313
|
+
try {
|
|
314
|
+
continuation = await goalActivity.maybeContinueGoal({
|
|
315
|
+
accountId: input.accountId,
|
|
316
|
+
workspaceId: input.workspaceId,
|
|
317
|
+
sessionId: input.sessionId,
|
|
318
|
+
workflowId,
|
|
319
|
+
});
|
|
320
|
+
} catch (error) {
|
|
321
|
+
if (isCancellation(error)) throw error;
|
|
322
|
+
await activity.enqueueGoalRetryWake({
|
|
323
|
+
accountId: input.accountId,
|
|
324
|
+
workspaceId: input.workspaceId,
|
|
325
|
+
sessionId: input.sessionId,
|
|
326
|
+
workflowId,
|
|
327
|
+
});
|
|
357
328
|
}
|
|
329
|
+
if (continuation.action === "continue" || continuation.action === "queue") continue;
|
|
358
330
|
const seenWakeups = wakeups;
|
|
359
331
|
const seenApprovalWakeups = approvalWakeups;
|
|
332
|
+
const seenInterruptionWakeups = interruptionWakeups;
|
|
360
333
|
const woke = await condition(
|
|
361
334
|
() =>
|
|
362
|
-
|
|
335
|
+
interruptionWakeups !== seenInterruptionWakeups ||
|
|
363
336
|
wakeups !== seenWakeups ||
|
|
364
337
|
approvalWakeups !== seenApprovalWakeups,
|
|
365
338
|
"5s",
|
|
@@ -396,23 +369,13 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
396
369
|
sessionId: string,
|
|
397
370
|
trigger: activities.RunAgentTurnInput["trigger"],
|
|
398
371
|
): Promise<boolean> {
|
|
399
|
-
if (controlEventId) {
|
|
400
|
-
const settlement = await activity.settleSessionControl({
|
|
401
|
-
accountId,
|
|
402
|
-
workspaceId,
|
|
403
|
-
sessionId,
|
|
404
|
-
triggerEventId: controlEventId,
|
|
405
|
-
workflowId: workflowInfo().workflowId,
|
|
406
|
-
});
|
|
407
|
-
controlEventId = null;
|
|
408
|
-
return settlement.action !== "paused";
|
|
409
|
-
}
|
|
410
|
-
|
|
411
372
|
const capacityWaitEntryBaseline = { wakeups, capacityWakeups };
|
|
412
373
|
const attemptId = uuid4();
|
|
374
|
+
const interruptionBaseline = interruptionWakeups;
|
|
413
375
|
|
|
414
376
|
const scope = new CancellationScope();
|
|
415
|
-
const
|
|
377
|
+
const workflowExecution = workflowInfo();
|
|
378
|
+
const workflowId = workflowExecution.workflowId;
|
|
416
379
|
// The stateless resume-by-id model (lease acquire + non-owned injection +
|
|
417
380
|
// release) lives entirely in the runAgentTurn activity.
|
|
418
381
|
const turn: Promise<activities.RunAgentTurnResult> = scope.run(() =>
|
|
@@ -421,6 +384,7 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
421
384
|
workspaceId,
|
|
422
385
|
sessionId,
|
|
423
386
|
workflowId,
|
|
387
|
+
workflowRunId: workflowExecution.runId,
|
|
424
388
|
attemptId,
|
|
425
389
|
trigger,
|
|
426
390
|
}),
|
|
@@ -430,22 +394,36 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
430
394
|
| { kind: "control" }
|
|
431
395
|
| { kind: "failure"; error: unknown } = await Promise.race([
|
|
432
396
|
turn.then(
|
|
433
|
-
(result: activities.RunAgentTurnResult) => ({
|
|
397
|
+
(result: activities.RunAgentTurnResult) => ({
|
|
398
|
+
kind: "result" as const,
|
|
399
|
+
result,
|
|
400
|
+
}),
|
|
434
401
|
(error: unknown) => ({ kind: "failure" as const, error }),
|
|
435
402
|
),
|
|
436
|
-
condition(() =>
|
|
403
|
+
condition(() => interruptionWakeups !== interruptionBaseline).then(() => ({
|
|
404
|
+
kind: "control" as const,
|
|
405
|
+
})),
|
|
437
406
|
]);
|
|
438
407
|
|
|
439
408
|
if (outcome.kind === "control") {
|
|
440
409
|
scope.cancel();
|
|
441
|
-
const settlement = await activity.
|
|
410
|
+
const settlement = await activity.settleSessionInterruptions({
|
|
442
411
|
accountId,
|
|
443
412
|
workspaceId,
|
|
444
413
|
sessionId,
|
|
445
|
-
|
|
414
|
+
attemptId,
|
|
446
415
|
workflowId: workflowInfo().workflowId,
|
|
447
416
|
});
|
|
448
|
-
|
|
417
|
+
// Keep the workflow alive until the cancelled activity has actually
|
|
418
|
+
// stopped. Temporal delivers activity cancellation through a heartbeat;
|
|
419
|
+
// returning while the activity promise is still detached lets the
|
|
420
|
+
// workflow complete first, after which the worker can keep streaming for
|
|
421
|
+
// the activity's entire start-to-close window. Postgres settlement above
|
|
422
|
+
// is intentionally first: it fences every late write immediately while
|
|
423
|
+
// the physical activity winds down. The control outcome is authoritative,
|
|
424
|
+
// so a concurrent activity completion or cancellation failure is ignored
|
|
425
|
+
// only after Temporal has durably observed that terminal activity state.
|
|
426
|
+
await turn.catch(() => undefined);
|
|
449
427
|
return settlement.action !== "paused";
|
|
450
428
|
}
|
|
451
429
|
|
|
@@ -454,7 +432,10 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
454
432
|
// transport/worker failed. Recover that durable boundary before generic
|
|
455
433
|
// failSession can overwrite the capacity-idle session.
|
|
456
434
|
{
|
|
457
|
-
const capacityWait = await activity.getCodexCapacityWait({
|
|
435
|
+
const capacityWait = await activity.getCodexCapacityWait({
|
|
436
|
+
workspaceId,
|
|
437
|
+
sessionId,
|
|
438
|
+
});
|
|
458
439
|
if (capacityWait) {
|
|
459
440
|
await waitForCodexCapacity(capacityWait, capacityWaitEntryBaseline);
|
|
460
441
|
return true;
|
|
@@ -503,10 +484,6 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
503
484
|
return outcome.result.status === "cancelled";
|
|
504
485
|
}
|
|
505
486
|
|
|
506
|
-
if (outcome.result.status === "recovering") {
|
|
507
|
-
return true;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
487
|
if (outcome.result.capacityWait) {
|
|
511
488
|
await waitForCodexCapacity(outcome.result.capacityWait, capacityWaitEntryBaseline);
|
|
512
489
|
return true;
|
|
@@ -520,13 +497,17 @@ export async function sessionWorkflow(input: SessionWorkflowInput): Promise<void
|
|
|
520
497
|
|
|
521
498
|
const holdMs = continuationHoldMs(outcome.result, ROTATION_IDLE_FLOOR_MS);
|
|
522
499
|
if (holdMs > 0) {
|
|
523
|
-
// Provider
|
|
524
|
-
// goal
|
|
500
|
+
// Provider recovery / rotation all-capped idle: hold the loop so the same
|
|
501
|
+
// turn or an active goal does not immediately re-enter the same rate-limit window.
|
|
525
502
|
// A rotation all-capped idle is a MANDATORY hold (idleUntilReset) — a 0/elapsed
|
|
526
503
|
// delay can never skip it (invariant 4: NO THRASH). A control or user signal
|
|
527
504
|
// ends the wait early and is handled by the main loop.
|
|
528
505
|
const seenWakeups = wakeups;
|
|
529
|
-
|
|
506
|
+
const seenInterruptionWakeups = interruptionWakeups;
|
|
507
|
+
await condition(
|
|
508
|
+
() => interruptionWakeups !== seenInterruptionWakeups || wakeups !== seenWakeups,
|
|
509
|
+
holdMs,
|
|
510
|
+
);
|
|
530
511
|
}
|
|
531
512
|
return true;
|
|
532
513
|
}
|
|
@@ -6,7 +6,8 @@ const wakeActivity = proxyActivities<Pick<typeof activities, "dispatchSessionWor
|
|
|
6
6
|
retry: { maximumAttempts: 1 },
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
/** One bounded delivery sweep;
|
|
9
|
+
/** One bounded delivery sweep; recursive controls trigger it immediately and
|
|
10
|
+
* the global Schedule repeats it every ten seconds for repair. */
|
|
10
11
|
export async function sessionWorkflowWakeDispatcherWorkflow() {
|
|
11
12
|
return await wakeActivity.dispatchSessionWorkflowWakes();
|
|
12
13
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export const SESSION_WORKFLOW_WAKE_DISPATCHER_SCHEDULE_ID =
|
|
2
|
-
"opengeni-session-workflow-wake-dispatcher";
|
|
3
|
-
export const SESSION_WORKFLOW_WAKE_DISPATCHER_WORKFLOW_TYPE =
|
|
4
|
-
"sessionWorkflowWakeDispatcherWorkflow";
|
|
5
|
-
export const SESSION_WORKFLOW_WAKE_DISPATCHER_PERIOD_MS = 10_000;
|