@opengeni/worker-bundle 0.23.4-canary.0 → 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.
- package/dist/activities/agent-turn/admission.d.ts +7 -0
- package/dist/activities/agent-turn/model-usage.d.ts +5 -0
- package/dist/activities/agent-turn/quiescence.d.ts +2 -0
- package/dist/activities/agent-turn/run.d.ts +9 -0
- package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
- package/dist/activities/agent-turn/session-title.d.ts +21 -0
- package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
- package/dist/activities/agent-turn/turn-context.d.ts +2 -1
- package/dist/activities/agent-turn.d.ts +1 -1
- package/dist/activities/capabilities.d.ts +1 -0
- package/dist/activities/context-compaction.d.ts +2 -0
- package/dist/{activities-control-SGU2WEVB.js → activities-control-ZWH7J4KZ.js} +102 -94
- package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
- package/dist/{activities-turn-7EAQXW6Q.js → activities-turn-I4OPLQLS.js} +613 -284
- package/dist/activities-turn-I4OPLQLS.js.map +1 -0
- package/dist/{chunk-TJIPCHMP.js → chunk-65JEWP2Q.js} +105 -6
- package/dist/chunk-65JEWP2Q.js.map +1 -0
- package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
- package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
- package/dist/{chunk-S2QVRVJW.js → chunk-RQNH6CGR.js} +28 -41
- package/dist/chunk-RQNH6CGR.js.map +1 -0
- package/dist/index.js +16 -3
- package/dist/index.js.map +1 -1
- package/dist/observability-metrics.d.ts +33 -7
- package/dist/sandbox-reaper-timeout.d.ts +2 -2
- package/dist/sandbox-resume.d.ts +6 -1
- package/package.json +17 -17
- package/src/activities/agent-turn/admission.ts +19 -0
- package/src/activities/agent-turn/claim.ts +9 -1
- package/src/activities/agent-turn/compaction-prep.ts +6 -1
- package/src/activities/agent-turn/finalization.ts +31 -17
- package/src/activities/agent-turn/model-usage.ts +33 -4
- package/src/activities/agent-turn/quiescence.ts +11 -2
- package/src/activities/agent-turn/run.ts +74 -1
- package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
- package/src/activities/agent-turn/session-title.ts +55 -2
- package/src/activities/agent-turn/stream-attempt.ts +258 -135
- package/src/activities/agent-turn/tool-environment.ts +41 -7
- package/src/activities/agent-turn/turn-context.ts +4 -2
- package/src/activities/agent-turn.ts +5 -1
- package/src/activities/api-integrations.ts +11 -1
- package/src/activities/capabilities.ts +57 -0
- package/src/activities/context-compaction.ts +3 -0
- package/src/activities/goals.ts +1 -0
- package/src/activities/mcp-credentials.ts +53 -0
- package/src/activities/rig-verification.ts +5 -3
- package/src/activities/scheduled-tasks.ts +111 -98
- package/src/index.ts +15 -0
- package/src/observability-metrics.ts +152 -14
- package/src/sandbox-reaper-timeout.ts +10 -4
- package/src/sandbox-resume.ts +58 -8
- package/dist/activities-control-SGU2WEVB.js.map +0 -1
- package/dist/activities-turn-7EAQXW6Q.js.map +0 -1
- package/dist/chunk-S2QVRVJW.js.map +0 -1
- package/dist/chunk-TJIPCHMP.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
|
|
239
|
-
// heartbeat would start on its next tick, so
|
|
240
|
-
//
|
|
241
|
-
// admission failure itself is still
|
|
242
|
-
|
|
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)
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
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
|
|
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
|
|
404
|
-
//
|
|
423
|
+
// A cancelled attempt never starts another preemption check; its
|
|
424
|
+
// cancellation settlement owns the holder from here.
|
|
405
425
|
if (
|
|
406
|
-
sandboxState.
|
|
426
|
+
sandboxState.rotationPreemptionInFlight ||
|
|
407
427
|
sandboxRotationController.signal.aborted ||
|
|
408
428
|
cancellationSignal?.aborted
|
|
409
429
|
) {
|
|
410
430
|
return "busy";
|
|
411
431
|
}
|
|
412
|
-
|
|
413
|
-
|
|
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
|
-
|
|
422
|
-
sandboxState.
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
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
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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
|
|
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.
|
|
469
|
+
sandboxState.rotationPreemptionInFlight = null;
|
|
478
470
|
});
|
|
479
|
-
await sandboxState.
|
|
480
|
-
return
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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 {
|