@opengeni/worker-bundle 0.23.4 → 0.26.0-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 +17 -2
- package/dist/activities/agent-turn/agent-build.d.ts +3 -10
- package/dist/activities/agent-turn/claim.d.ts +8 -0
- package/dist/activities/agent-turn/codex.d.ts +2 -2
- package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
- package/dist/activities/agent-turn/errors.d.ts +1 -1
- package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
- package/dist/activities/agent-turn/list-models.d.ts +16 -0
- package/dist/activities/agent-turn/model-usage.d.ts +15 -1
- package/dist/activities/agent-turn/quiescence.d.ts +11 -6
- 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 +4 -13
- package/dist/activities/agent-turn/tool-policy.d.ts +40 -5
- package/dist/activities/agent-turn/turn-context.d.ts +13 -2
- package/dist/activities/agent-turn.d.ts +1 -1
- package/dist/activities/automations.d.ts +1 -0
- package/dist/activities/capabilities.d.ts +3 -1
- package/dist/activities/codemode-dispatcher.d.ts +10 -1
- package/dist/activities/codex-capacity.d.ts +32 -3
- package/dist/activities/codex-fleet-shadow.d.ts +1 -1
- package/dist/activities/codex-image-generation.d.ts +1 -1
- package/dist/activities/codex-rotation.d.ts +8 -67
- package/dist/activities/context-compaction.d.ts +2 -0
- package/dist/activities/goals.d.ts +12 -1
- package/dist/activities/google-drive-publication.d.ts +2 -13
- package/dist/activities/image-generation-operation.d.ts +13 -4
- package/dist/activities/image-generation-references.d.ts +2 -1
- package/dist/activities/scheduled-tasks.d.ts +1 -0
- package/dist/activities/session-state.d.ts +4 -2
- package/dist/activities/types.d.ts +19 -1
- package/dist/{activities-control-52W5IDWM.js → activities-control-CTEMLTHG.js} +339 -195
- package/dist/activities-control-CTEMLTHG.js.map +1 -0
- package/dist/activities-control.d.ts +4 -0
- package/dist/{activities-turn-A2G2KLSY.js → activities-turn-S5IU347A.js} +1846 -1136
- package/dist/activities-turn-S5IU347A.js.map +1 -0
- package/dist/activities.d.ts +4 -0
- package/dist/{chunk-GFNXVZVQ.js → chunk-D4ZEFOYH.js} +107 -7
- package/dist/chunk-D4ZEFOYH.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-IPVMCAW5.js → chunk-TAUE3GYT.js} +225 -154
- package/dist/chunk-TAUE3GYT.js.map +1 -0
- package/dist/github-rest-mcp.d.ts +0 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -7
- package/dist/index.js.map +1 -1
- package/dist/observability-metrics.d.ts +35 -8
- package/dist/sandbox-reaper-timeout.d.ts +2 -2
- package/dist/sandbox-resume.d.ts +6 -1
- package/dist/workflow-bundle.js +96 -10
- package/dist/workflows/activities.d.ts +6 -1
- package/dist/workflows/automations.d.ts +5 -0
- package/dist/workflows/session.d.ts +3 -0
- package/package.json +17 -17
- package/src/activities/agent-run-admission.ts +7 -1
- package/src/activities/agent-turn/admission.ts +47 -6
- package/src/activities/agent-turn/agent-build.ts +20 -26
- package/src/activities/agent-turn/claim.ts +59 -6
- package/src/activities/agent-turn/codex-capacity.ts +311 -234
- package/src/activities/agent-turn/codex.ts +2 -2
- package/src/activities/agent-turn/compaction-prep.ts +8 -1
- package/src/activities/agent-turn/credential-leases.ts +36 -0
- package/src/activities/agent-turn/errors.ts +29 -1
- package/src/activities/agent-turn/failure-settlement.ts +451 -323
- package/src/activities/agent-turn/finalization.ts +31 -17
- package/src/activities/agent-turn/list-models.ts +101 -0
- package/src/activities/agent-turn/model-usage.ts +120 -45
- package/src/activities/agent-turn/quiescence.ts +32 -17
- package/src/activities/agent-turn/run.ts +98 -9
- 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 +281 -136
- package/src/activities/agent-turn/tool-environment.ts +132 -39
- package/src/activities/agent-turn/tool-policy.ts +71 -12
- package/src/activities/agent-turn/turn-context.ts +21 -2
- package/src/activities/agent-turn.ts +5 -1
- package/src/activities/automations.ts +122 -67
- package/src/activities/capabilities.ts +129 -2
- package/src/activities/codemode-dispatcher.ts +235 -59
- package/src/activities/codex-capacity.ts +89 -10
- package/src/activities/codex-fleet-shadow.ts +8 -1
- package/src/activities/codex-image-generation.ts +19 -2
- package/src/activities/codex-rotation.ts +45 -142
- package/src/activities/context-compaction.ts +3 -0
- package/src/activities/goals.ts +116 -39
- package/src/activities/google-drive-publication.ts +4 -81
- package/src/activities/image-generation-operation.ts +28 -3
- package/src/activities/image-generation-references.ts +9 -9
- package/src/activities/mcp-credentials.ts +53 -0
- package/src/activities/rig-verification.ts +5 -3
- package/src/activities/sandbox-lease.ts +29 -12
- package/src/activities/scheduled-tasks.ts +217 -110
- package/src/activities/session-state.ts +17 -0
- package/src/activities/site-auth-maintenance.ts +12 -12
- package/src/activities/types.ts +22 -8
- package/src/activities.ts +2 -0
- package/src/activity-services.ts +4 -1
- package/src/github-rest-mcp.ts +2 -83
- package/src/index.ts +34 -4
- package/src/observability-metrics.ts +158 -14
- package/src/sandbox-reaper-timeout.ts +10 -4
- package/src/sandbox-resume.ts +58 -8
- package/src/sandbox-routing.ts +71 -16
- package/src/workflows/activities.ts +16 -0
- package/src/workflows/automations.ts +21 -2
- package/src/workflows/session.ts +85 -7
- package/dist/activities/api-integrations.d.ts +0 -21
- package/dist/activities-control-52W5IDWM.js.map +0 -1
- package/dist/activities-turn-A2G2KLSY.js.map +0 -1
- package/dist/chunk-GFNXVZVQ.js.map +0 -1
- package/dist/chunk-IPVMCAW5.js.map +0 -1
- package/src/activities/api-integrations.ts +0 -141
|
@@ -152,6 +152,23 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
152
152
|
cancellationSignal,
|
|
153
153
|
control.activityStatus,
|
|
154
154
|
);
|
|
155
|
+
const drainInFlightWarmSnapshot = async (): Promise<void> => {
|
|
156
|
+
const snapshot = sandboxState.snapshotInFlight;
|
|
157
|
+
if (!snapshot) return;
|
|
158
|
+
// This is physical quiescence, not optional turn-end persistence. The
|
|
159
|
+
// provider capture SDK is not cancellable, so an aborted finalizer signal
|
|
160
|
+
// must not detach this wait and release the holder while the continuation
|
|
161
|
+
// is still reading or publishing the exact workspace generation.
|
|
162
|
+
await snapshot.catch((error) => {
|
|
163
|
+
console.error(
|
|
164
|
+
"in-flight workspace snapshot settlement failed (turn outcome unaffected)",
|
|
165
|
+
safeErrorDiagnostic(error),
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
if (sandboxState.snapshotInFlight === snapshot) {
|
|
169
|
+
sandboxState.snapshotInFlight = null;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
155
172
|
try {
|
|
156
173
|
const toolCancellationFence = eventing.toolCancellationFenceRef.current;
|
|
157
174
|
// Every renewal controller is an attempt-owned sandbox writer. Capture
|
|
@@ -468,6 +485,7 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
468
485
|
const runTurnEndPersistence = shouldRunTurnEndWorkspacePersistence({
|
|
469
486
|
activityStatus: control.activityStatus,
|
|
470
487
|
cancellationRequested: finalizerSignal?.aborted === true,
|
|
488
|
+
deadlineRotationRequested: sandboxState.deadlineRotationRequested,
|
|
471
489
|
});
|
|
472
490
|
if (
|
|
473
491
|
runTurnEndPersistence &&
|
|
@@ -565,11 +583,13 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
565
583
|
if (sandboxState.leaseHeartbeatTimer) {
|
|
566
584
|
stopLeaseHeartbeat();
|
|
567
585
|
}
|
|
568
|
-
if (sandboxState.
|
|
569
|
-
await
|
|
570
|
-
() => undefined,
|
|
571
|
-
);
|
|
586
|
+
if (sandboxState.rotationPreemptionInFlight) {
|
|
587
|
+
await sandboxState.rotationPreemptionInFlight.catch(() => undefined);
|
|
572
588
|
}
|
|
589
|
+
// No heartbeat can register another capture after this point. Join the
|
|
590
|
+
// actual provider continuation before either starting a turn-end capture
|
|
591
|
+
// or entering the proof-bearing holder release.
|
|
592
|
+
await drainInFlightWarmSnapshot();
|
|
573
593
|
if (sandboxState.resolvedSandbox) {
|
|
574
594
|
// TURN-END mid-session snapshot (sandbox-file-persistence): fold the
|
|
575
595
|
// turn's finished /workspace onto the lease before releasing the holder,
|
|
@@ -585,19 +605,7 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
585
605
|
sandboxState.sandboxGroupId &&
|
|
586
606
|
settledTurnId
|
|
587
607
|
) {
|
|
588
|
-
|
|
589
|
-
// above, but a capture it launched may still be in flight — and that
|
|
590
|
-
// capture predates the turn's final writes. Wait for it, but only up
|
|
591
|
-
// to the snapshot timeout: release must never depend on an unbounded
|
|
592
|
-
// provider capture.
|
|
593
|
-
if (sandboxState.snapshotInFlight) {
|
|
594
|
-
await waitForWarmSnapshot(
|
|
595
|
-
sandboxState.snapshotInFlight,
|
|
596
|
-
settings.sandboxSnapshotTimeoutMs,
|
|
597
|
-
finalizerSignal,
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
const persisted = await maybePersistWarmWorkspaceSnapshot(
|
|
608
|
+
const snapshot = maybePersistWarmWorkspaceSnapshot(
|
|
601
609
|
{ db, settings, objectStorage },
|
|
602
610
|
{
|
|
603
611
|
accountId: input.accountId,
|
|
@@ -611,6 +619,8 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
611
619
|
sandboxState.resolvedSandbox.leaseEpoch,
|
|
612
620
|
finalizerSignal,
|
|
613
621
|
);
|
|
622
|
+
sandboxState.snapshotInFlight = snapshot.settled;
|
|
623
|
+
const persisted = await snapshot;
|
|
614
624
|
if (persisted && eventing.publish) {
|
|
615
625
|
await eventing
|
|
616
626
|
.publish([
|
|
@@ -641,6 +651,10 @@ export async function finalizeTurnAttempt(deps: TurnFinalizationDeps): Promise<v
|
|
|
641
651
|
// Temporal cancellation: the eager listener may already have dropped
|
|
642
652
|
// the holder, and this idempotent proof-bearing pass still must run.
|
|
643
653
|
stopLeaseHeartbeat();
|
|
654
|
+
if (sandboxState.rotationPreemptionInFlight) {
|
|
655
|
+
await sandboxState.rotationPreemptionInFlight.catch(() => undefined);
|
|
656
|
+
}
|
|
657
|
+
await drainInFlightWarmSnapshot();
|
|
644
658
|
const sandboxReleaseTargets = new Set(sandboxState.lateSandboxesAwaitingWriterDrain);
|
|
645
659
|
sandboxState.lateSandboxesAwaitingWriterDrain.clear();
|
|
646
660
|
if (sandboxState.resolvedSandbox) {
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { AttemptToolDefinition } from "@opengeni/codemode";
|
|
2
|
+
import {
|
|
3
|
+
compareModelPickerOrder,
|
|
4
|
+
modelPickerBillingClassFor,
|
|
5
|
+
} from "@opengeni/contracts/model-picker-order";
|
|
6
|
+
import type { WorkspaceModelSelection } from "@opengeni/core";
|
|
7
|
+
|
|
8
|
+
export const LIST_MODELS_TOOL_NAME = "list_models";
|
|
9
|
+
|
|
10
|
+
export const LIST_MODELS_TOOL_DESCRIPTION =
|
|
11
|
+
"List the models this workspace can select right now, in picker order, with deployment-defined cost and optional guidance. This does not switch the current session model; use an ID with session_create or the human model picker.";
|
|
12
|
+
|
|
13
|
+
function safeLineField(value: string): string {
|
|
14
|
+
return value
|
|
15
|
+
.replace(/[\r\n|]/gu, " ")
|
|
16
|
+
.replace(/\s+/gu, " ")
|
|
17
|
+
.trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function renderListModels(input: {
|
|
21
|
+
currentModelId: string | null;
|
|
22
|
+
selections: readonly WorkspaceModelSelection[];
|
|
23
|
+
modelNotes: Readonly<Record<string, string>>;
|
|
24
|
+
}): string {
|
|
25
|
+
const lines: string[] = [];
|
|
26
|
+
if (input.currentModelId) {
|
|
27
|
+
lines.push(`Current: ${safeLineField(input.currentModelId)}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const selectable = input.selections
|
|
31
|
+
.filter((entry) => entry.availability.selectable)
|
|
32
|
+
.map((selection) => ({
|
|
33
|
+
selection,
|
|
34
|
+
billingClass: modelPickerBillingClassFor(selection.model),
|
|
35
|
+
selectable: selection.availability.selectable,
|
|
36
|
+
label: selection.model.label,
|
|
37
|
+
}))
|
|
38
|
+
.sort(compareModelPickerOrder);
|
|
39
|
+
if (selectable.length === 0) {
|
|
40
|
+
lines.push("No models are available in this workspace.");
|
|
41
|
+
return lines.join("\n");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
for (const { selection } of selectable) {
|
|
45
|
+
const { model } = selection;
|
|
46
|
+
const fields = [safeLineField(model.id), safeLineField(model.label), model.cost];
|
|
47
|
+
const note = input.modelNotes[model.id];
|
|
48
|
+
if (note) fields.push(safeLineField(note));
|
|
49
|
+
lines.push(`- ${fields.join(" | ")}`);
|
|
50
|
+
}
|
|
51
|
+
return lines.join("\n");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function createListModelsAttemptToolDefinition(input: {
|
|
55
|
+
currentModelId: string | null;
|
|
56
|
+
load: () => Promise<{
|
|
57
|
+
selections: readonly WorkspaceModelSelection[];
|
|
58
|
+
modelNotes: Readonly<Record<string, string>>;
|
|
59
|
+
}>;
|
|
60
|
+
}): AttemptToolDefinition {
|
|
61
|
+
return {
|
|
62
|
+
identity: { serverId: "opengeni", toolName: LIST_MODELS_TOOL_NAME },
|
|
63
|
+
modelName: LIST_MODELS_TOOL_NAME,
|
|
64
|
+
codemodePath: ["opengeni", LIST_MODELS_TOOL_NAME],
|
|
65
|
+
title: "List selectable models",
|
|
66
|
+
description: LIST_MODELS_TOOL_DESCRIPTION,
|
|
67
|
+
inputSchema: {
|
|
68
|
+
type: "object",
|
|
69
|
+
properties: {},
|
|
70
|
+
additionalProperties: false,
|
|
71
|
+
},
|
|
72
|
+
annotations: {
|
|
73
|
+
title: "List selectable models",
|
|
74
|
+
readOnlyHint: true,
|
|
75
|
+
destructiveHint: false,
|
|
76
|
+
idempotentHint: true,
|
|
77
|
+
openWorldHint: false,
|
|
78
|
+
},
|
|
79
|
+
source: "opengeni",
|
|
80
|
+
approval: "none",
|
|
81
|
+
execute: async (args) => {
|
|
82
|
+
if (Object.keys(args).length !== 0) {
|
|
83
|
+
throw new Error("list_models accepts no arguments");
|
|
84
|
+
}
|
|
85
|
+
const catalog = await input.load();
|
|
86
|
+
return {
|
|
87
|
+
isError: false,
|
|
88
|
+
content: [
|
|
89
|
+
{
|
|
90
|
+
type: "text",
|
|
91
|
+
text: renderListModels({
|
|
92
|
+
currentModelId: input.currentModelId,
|
|
93
|
+
selections: catalog.selections,
|
|
94
|
+
modelNotes: catalog.modelNotes,
|
|
95
|
+
}),
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -15,11 +15,13 @@ import {
|
|
|
15
15
|
} from "@opengeni/runtime";
|
|
16
16
|
import {
|
|
17
17
|
calculateGatewayReportedCostBreakdown,
|
|
18
|
+
calculateGatewayReportedProviderCostMicros,
|
|
18
19
|
calculateModelUsageCostBreakdown,
|
|
19
20
|
configuredModelPricingSchedules,
|
|
21
|
+
resolveModelProvider,
|
|
20
22
|
responseSatisfiesLatencyMode,
|
|
21
|
-
OPENGENI_GATEWAY_MODELS,
|
|
22
23
|
OPENGENI_GATEWAY_PROVIDER_ID,
|
|
24
|
+
WORKSPACE_GATEWAY_PROVIDER_ID,
|
|
23
25
|
type ModelUsageInput,
|
|
24
26
|
type ModelProviderApi,
|
|
25
27
|
type Settings,
|
|
@@ -123,6 +125,8 @@ export function createCompactionModelUsageEventState(
|
|
|
123
125
|
return { usageCount: 0, claimedSourceKeys };
|
|
124
126
|
}
|
|
125
127
|
|
|
128
|
+
export const createSessionTitleModelUsageEventState = createCompactionModelUsageEventState;
|
|
129
|
+
|
|
126
130
|
export function modelResponseContextSignal(
|
|
127
131
|
state: ModelResponseEventState,
|
|
128
132
|
): { revision: number; totalTokens: number } | null {
|
|
@@ -191,6 +195,8 @@ export async function processModelResponseTerminalEvent(input: {
|
|
|
191
195
|
latencyMode?: LatencyMode;
|
|
192
196
|
metricProvider: string;
|
|
193
197
|
externallyBilled: boolean;
|
|
198
|
+
chargesOpenGeniCredits?: boolean;
|
|
199
|
+
countsTowardTokenCap?: boolean;
|
|
194
200
|
servingCredentialId: string | null;
|
|
195
201
|
priorSessionCredentialId: string | null;
|
|
196
202
|
emittedSourceKeys: Set<string>;
|
|
@@ -251,9 +257,14 @@ export async function processModelResponseTerminalEvent(input: {
|
|
|
251
257
|
turnAttemptId: input.turnAttemptId,
|
|
252
258
|
model: input.model,
|
|
253
259
|
externallyBilled: input.externallyBilled,
|
|
260
|
+
...(input.chargesOpenGeniCredits !== undefined
|
|
261
|
+
? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
|
|
262
|
+
: {}),
|
|
263
|
+
...(input.countsTowardTokenCap !== undefined
|
|
264
|
+
? { countsTowardTokenCap: input.countsTowardTokenCap }
|
|
265
|
+
: {}),
|
|
254
266
|
usage: responseUsage.usage,
|
|
255
267
|
normalizedUsage,
|
|
256
|
-
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
257
268
|
gatewayBilling: responseUsage.gatewayBilling,
|
|
258
269
|
sourceKey,
|
|
259
270
|
...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
|
|
@@ -272,6 +283,8 @@ export async function processModelResponseTerminalEvent(input: {
|
|
|
272
283
|
sourceKey,
|
|
273
284
|
usage: responseUsage,
|
|
274
285
|
normalizedUsage,
|
|
286
|
+
...(billing ? { billingPath: billing.billingPath } : {}),
|
|
287
|
+
...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
|
|
275
288
|
servingAccountHash: accountContext.servingAccountHash,
|
|
276
289
|
accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
|
|
277
290
|
emittedSourceKeys: input.emittedSourceKeys,
|
|
@@ -331,6 +344,7 @@ export async function processModelResponseTerminalEvent(input: {
|
|
|
331
344
|
export async function processCompactionModelUsageEvent(input: {
|
|
332
345
|
usage: ModelResponseUsage;
|
|
333
346
|
state: CompactionModelUsageEventState;
|
|
347
|
+
sourceKind?: "compaction" | "session-title";
|
|
334
348
|
dispatchId: string | null;
|
|
335
349
|
settings: Settings;
|
|
336
350
|
db: ActivityServices["db"];
|
|
@@ -345,6 +359,8 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
345
359
|
providerApi: ModelProviderApi;
|
|
346
360
|
model: string;
|
|
347
361
|
externallyBilled: boolean;
|
|
362
|
+
chargesOpenGeniCredits?: boolean;
|
|
363
|
+
countsTowardTokenCap?: boolean;
|
|
348
364
|
servingCredentialId: string | null;
|
|
349
365
|
priorSessionCredentialId: string | null;
|
|
350
366
|
emittedSourceKeys: Set<string>;
|
|
@@ -360,7 +376,7 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
360
376
|
const sourceKey = modelUsageSourceKey({
|
|
361
377
|
responseId: input.usage.responseId,
|
|
362
378
|
dispatchId: input.dispatchId,
|
|
363
|
-
positionalKey:
|
|
379
|
+
positionalKey: `${input.sourceKind ?? "compaction"}-${usageOrdinal}`,
|
|
364
380
|
});
|
|
365
381
|
if (input.state.claimedSourceKeys.has(sourceKey)) {
|
|
366
382
|
return { status: "duplicate", sourceKey };
|
|
@@ -388,9 +404,14 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
388
404
|
turnAttemptId: input.turnAttemptId,
|
|
389
405
|
model: input.model,
|
|
390
406
|
externallyBilled: input.externallyBilled,
|
|
407
|
+
...(input.chargesOpenGeniCredits !== undefined
|
|
408
|
+
? { chargesOpenGeniCredits: input.chargesOpenGeniCredits }
|
|
409
|
+
: {}),
|
|
410
|
+
...(input.countsTowardTokenCap !== undefined
|
|
411
|
+
? { countsTowardTokenCap: input.countsTowardTokenCap }
|
|
412
|
+
: {}),
|
|
391
413
|
usage: input.usage.usage,
|
|
392
414
|
normalizedUsage,
|
|
393
|
-
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
394
415
|
gatewayBilling: input.usage.gatewayBilling,
|
|
395
416
|
sourceKey,
|
|
396
417
|
observability: input.observability,
|
|
@@ -408,6 +429,8 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
408
429
|
sourceKey,
|
|
409
430
|
usage: input.usage,
|
|
410
431
|
normalizedUsage,
|
|
432
|
+
...(billing ? { billingPath: billing.billingPath } : {}),
|
|
433
|
+
...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
|
|
411
434
|
servingAccountHash: accountContext.servingAccountHash,
|
|
412
435
|
accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
|
|
413
436
|
emittedSourceKeys: input.emittedSourceKeys,
|
|
@@ -436,6 +459,15 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
436
459
|
return { status: "processed", sourceKey, authoritative };
|
|
437
460
|
}
|
|
438
461
|
|
|
462
|
+
export async function processSessionTitleModelUsageEvent(
|
|
463
|
+
input: Omit<Parameters<typeof processCompactionModelUsageEvent>[0], "sourceKind">,
|
|
464
|
+
): ReturnType<typeof processCompactionModelUsageEvent> {
|
|
465
|
+
return await processCompactionModelUsageEvent({
|
|
466
|
+
...input,
|
|
467
|
+
sourceKind: "session-title",
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
439
471
|
export async function emitModelCallUsage(input: {
|
|
440
472
|
observability: ActivityServices["observability"];
|
|
441
473
|
publish: TurnEventPublisher | null;
|
|
@@ -449,6 +481,10 @@ export async function emitModelCallUsage(input: {
|
|
|
449
481
|
sourceKey: string;
|
|
450
482
|
usage: ModelResponseUsage | { usage?: unknown | null } | null;
|
|
451
483
|
normalizedUsage?: ModelCallUsageNormalization;
|
|
484
|
+
/** Accepted billing authority persisted for Insights repair after a soft fact-write failure. */
|
|
485
|
+
billingPath?: ModelUsageBillingRecord["billingPath"];
|
|
486
|
+
/** Validated Gateway endpoint provider persisted for exact Insights repair. */
|
|
487
|
+
upstreamProvider?: string;
|
|
452
488
|
// Prompt-cache research dimensions (log-only; NEVER on a metric label or a
|
|
453
489
|
// durable event). The opaque serving-account tag and whether it changed since
|
|
454
490
|
// the session's previous call — the account-switch hypothesis for cache misses.
|
|
@@ -480,6 +516,8 @@ export async function emitModelCallUsage(input: {
|
|
|
480
516
|
providerApi: input.providerApi,
|
|
481
517
|
model: input.model,
|
|
482
518
|
sourceKey: input.sourceKey,
|
|
519
|
+
...(input.billingPath ? { billingPath: input.billingPath } : {}),
|
|
520
|
+
...(input.upstreamProvider ? { upstreamProvider: input.upstreamProvider } : {}),
|
|
483
521
|
...telemetry,
|
|
484
522
|
},
|
|
485
523
|
},
|
|
@@ -555,6 +593,8 @@ export type ModelUsageBillingRecord = {
|
|
|
555
593
|
pricedCostMicros: number;
|
|
556
594
|
/** Hypothetical provider-rate USD micros; never an OpenGeni charge. */
|
|
557
595
|
estimatedProviderCostMicros: number | null;
|
|
596
|
+
/** Hypothetical OpenGeni credit price at the captured rate; never a debit. */
|
|
597
|
+
equivalentCreditCostMicros: number | null;
|
|
558
598
|
pricingSource: "configured_list_price" | "gateway_reported" | null;
|
|
559
599
|
normalizedUsage: ModelCallUsageNormalization;
|
|
560
600
|
upstreamProvider?: string;
|
|
@@ -572,7 +612,8 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
572
612
|
turnAttemptId: string;
|
|
573
613
|
model: string;
|
|
574
614
|
externallyBilled: boolean;
|
|
575
|
-
|
|
615
|
+
chargesOpenGeniCredits?: boolean;
|
|
616
|
+
countsTowardTokenCap?: boolean;
|
|
576
617
|
gatewayBilling?: ModelResponseUsage["gatewayBilling"];
|
|
577
618
|
usage?: ModelUsageInput | ModelCallUsageInput | null;
|
|
578
619
|
normalizedUsage?: ModelCallUsageNormalization;
|
|
@@ -589,19 +630,35 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
589
630
|
const inputTokens = sanitizedUsage.inputTokens ?? 0;
|
|
590
631
|
const outputTokens = sanitizedUsage.outputTokens ?? 0;
|
|
591
632
|
const totalTokens = sanitizedUsage.totalTokens ?? 0;
|
|
592
|
-
const
|
|
633
|
+
const chargesOpenGeniCredits = input.chargesOpenGeniCredits ?? !input.externallyBilled;
|
|
634
|
+
const countsTowardTokenCap = input.countsTowardTokenCap ?? !input.externallyBilled;
|
|
635
|
+
const resolvedGatewayModel = input.gatewayBilling
|
|
636
|
+
? resolveModelProvider(settings, input.model)
|
|
637
|
+
: undefined;
|
|
638
|
+
const gatewayProviderId = resolvedGatewayModel?.provider.id;
|
|
639
|
+
const gatewayBilling =
|
|
640
|
+
gatewayProviderId === OPENGENI_GATEWAY_PROVIDER_ID ||
|
|
641
|
+
gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID
|
|
642
|
+
? input.gatewayBilling
|
|
643
|
+
: undefined;
|
|
644
|
+
const allowedProviders = resolvedGatewayModel?.model.requestPolicy?.gateway.only;
|
|
645
|
+
const unpinnedWorkspaceGatewayModel =
|
|
646
|
+
gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID && allowedProviders === undefined;
|
|
593
647
|
if (gatewayBilling) {
|
|
594
|
-
const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
|
|
595
|
-
(candidate) => candidate.productId === input.model,
|
|
596
|
-
);
|
|
597
648
|
if (
|
|
598
|
-
!
|
|
599
|
-
!
|
|
649
|
+
!unpinnedWorkspaceGatewayModel &&
|
|
650
|
+
(!allowedProviders ||
|
|
651
|
+
!(allowedProviders as readonly string[]).includes(gatewayBilling.finalProvider))
|
|
600
652
|
) {
|
|
601
653
|
throw new Error(
|
|
602
654
|
`AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`,
|
|
603
655
|
);
|
|
604
656
|
}
|
|
657
|
+
if (unpinnedWorkspaceGatewayModel && chargesOpenGeniCredits) {
|
|
658
|
+
throw new Error(
|
|
659
|
+
`Workspace Gateway custom model ${input.model} cannot charge OpenGeni credits without pinned pricing`,
|
|
660
|
+
);
|
|
661
|
+
}
|
|
605
662
|
}
|
|
606
663
|
const pricingSchedules = configuredModelPricingSchedules(settings);
|
|
607
664
|
const configuredPricingModel = pricingSchedules[input.model]
|
|
@@ -610,12 +667,19 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
610
667
|
? input.model.slice("codex/".length)
|
|
611
668
|
: null;
|
|
612
669
|
const pricingBreakdown = gatewayBilling
|
|
613
|
-
?
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
670
|
+
? unpinnedWorkspaceGatewayModel
|
|
671
|
+
? {
|
|
672
|
+
providerCostMicros: calculateGatewayReportedProviderCostMicros(
|
|
673
|
+
gatewayBilling.inferenceCostUsd,
|
|
674
|
+
),
|
|
675
|
+
creditCostMicros: 0,
|
|
676
|
+
}
|
|
677
|
+
: calculateGatewayReportedCostBreakdown(
|
|
678
|
+
settings,
|
|
679
|
+
configuredPricingModel ?? input.model,
|
|
680
|
+
gatewayBilling.inferenceCostUsd,
|
|
681
|
+
{ inputTokens },
|
|
682
|
+
)
|
|
619
683
|
: configuredPricingModel
|
|
620
684
|
? calculateModelUsageCostBreakdown(settings, configuredPricingModel, sanitizedUsage, {
|
|
621
685
|
latencyMode: input.latencyMode ?? "standard",
|
|
@@ -629,22 +693,38 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
629
693
|
: hasCompleteCoreTokenTelemetry
|
|
630
694
|
? (pricingBreakdown?.providerCostMicros ?? null)
|
|
631
695
|
: null;
|
|
696
|
+
const equivalentCreditCostMicros =
|
|
697
|
+
pricingBreakdown && !unpinnedWorkspaceGatewayModel
|
|
698
|
+
? gatewayBilling || hasCompleteCoreTokenTelemetry
|
|
699
|
+
? pricingBreakdown.creditCostMicros
|
|
700
|
+
: null
|
|
701
|
+
: null;
|
|
632
702
|
const pricingSource = gatewayBilling
|
|
633
703
|
? ("gateway_reported" as const)
|
|
634
704
|
: estimatedProviderCostMicros !== null
|
|
635
705
|
? ("configured_list_price" as const)
|
|
636
706
|
: null;
|
|
637
|
-
//
|
|
638
|
-
//
|
|
639
|
-
//
|
|
640
|
-
//
|
|
641
|
-
//
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
707
|
+
// Provider settlement and workspace-facing cost are separate. Externally
|
|
708
|
+
// metered subscription/workspace turns remain exempt from the OpenGeni token
|
|
709
|
+
// cap, while a deployment-funded free model still records model.tokens. Every
|
|
710
|
+
// non-credit path records a zero-cost marker and never consults pricing for a
|
|
711
|
+
// debit.
|
|
712
|
+
if (countsTowardTokenCap && totalTokens > 0) {
|
|
713
|
+
await recordUsageEvent(db, {
|
|
714
|
+
accountId: input.accountId,
|
|
715
|
+
workspaceId: input.workspaceId,
|
|
716
|
+
eventType: "model.tokens",
|
|
717
|
+
quantity: totalTokens,
|
|
718
|
+
unit: "tokens",
|
|
719
|
+
sourceResourceType: "model_response",
|
|
720
|
+
sourceResourceId: `${input.turnId}:${input.sourceKey}`,
|
|
721
|
+
sessionId: input.sessionId,
|
|
722
|
+
turnId: input.turnId,
|
|
723
|
+
turnAttemptId: input.turnAttemptId,
|
|
724
|
+
idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
if (!chargesOpenGeniCredits) {
|
|
648
728
|
await recordUsageEvent(db, {
|
|
649
729
|
accountId: input.accountId,
|
|
650
730
|
workspaceId: input.workspaceId,
|
|
@@ -662,31 +742,19 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
662
742
|
billingPath: "external",
|
|
663
743
|
pricedCostMicros: 0,
|
|
664
744
|
estimatedProviderCostMicros,
|
|
745
|
+
equivalentCreditCostMicros,
|
|
665
746
|
pricingSource,
|
|
666
747
|
normalizedUsage,
|
|
748
|
+
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
667
749
|
};
|
|
668
750
|
}
|
|
669
|
-
if (totalTokens > 0) {
|
|
670
|
-
await recordUsageEvent(db, {
|
|
671
|
-
accountId: input.accountId,
|
|
672
|
-
workspaceId: input.workspaceId,
|
|
673
|
-
eventType: "model.tokens",
|
|
674
|
-
quantity: totalTokens,
|
|
675
|
-
unit: "tokens",
|
|
676
|
-
sourceResourceType: "model_response",
|
|
677
|
-
sourceResourceId: `${input.turnId}:${input.sourceKey}`,
|
|
678
|
-
sessionId: input.sessionId,
|
|
679
|
-
turnId: input.turnId,
|
|
680
|
-
turnAttemptId: input.turnAttemptId,
|
|
681
|
-
idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`,
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
751
|
const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
|
|
685
752
|
if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
|
|
686
753
|
return {
|
|
687
754
|
billingPath: "opengeni_credits",
|
|
688
755
|
pricedCostMicros: 0,
|
|
689
756
|
estimatedProviderCostMicros,
|
|
757
|
+
equivalentCreditCostMicros,
|
|
690
758
|
pricingSource,
|
|
691
759
|
normalizedUsage,
|
|
692
760
|
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
@@ -740,6 +808,7 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
740
808
|
billingPath: "opengeni_credits",
|
|
741
809
|
pricedCostMicros: costMicros,
|
|
742
810
|
estimatedProviderCostMicros,
|
|
811
|
+
equivalentCreditCostMicros,
|
|
743
812
|
pricingSource,
|
|
744
813
|
normalizedUsage,
|
|
745
814
|
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
@@ -777,6 +846,7 @@ export async function recordAuthoritativeModelCallFact(input: {
|
|
|
777
846
|
billingPath: input.billing.billingPath,
|
|
778
847
|
pricedCostMicros: input.billing.pricedCostMicros,
|
|
779
848
|
estimatedProviderCostMicros: input.billing.estimatedProviderCostMicros,
|
|
849
|
+
equivalentCreditCostMicros: input.billing.equivalentCreditCostMicros,
|
|
780
850
|
pricingSource: input.billing.pricingSource,
|
|
781
851
|
inputTokens: telemetry.inputTokens,
|
|
782
852
|
outputTokens: telemetry.outputTokens,
|
|
@@ -804,10 +874,15 @@ export function sanitizedModelUsageInput(normalized: ModelCallUsageNormalization
|
|
|
804
874
|
? { outputTokens: normalized.telemetry.outputTokens }
|
|
805
875
|
: {}),
|
|
806
876
|
...(normalized.totalTokens !== null ? { totalTokens: normalized.totalTokens } : {}),
|
|
807
|
-
...(normalized.telemetry.cachedTokens !== null
|
|
877
|
+
...(normalized.telemetry.cachedTokens !== null || normalized.telemetry.cacheWriteTokens !== null
|
|
808
878
|
? {
|
|
809
879
|
inputTokensDetails: {
|
|
810
|
-
|
|
880
|
+
...(normalized.telemetry.cachedTokens === null
|
|
881
|
+
? {}
|
|
882
|
+
: { cached_tokens: normalized.telemetry.cachedTokens }),
|
|
883
|
+
...(normalized.telemetry.cacheWriteTokens === null
|
|
884
|
+
? {}
|
|
885
|
+
: { cache_write_tokens: normalized.telemetry.cacheWriteTokens }),
|
|
811
886
|
},
|
|
812
887
|
}
|
|
813
888
|
: {}),
|
|
@@ -15,8 +15,13 @@ import { sleep } from "./sandbox-provision";
|
|
|
15
15
|
export function shouldRunTurnEndWorkspacePersistence(input: {
|
|
16
16
|
activityStatus: RunAgentTurnResult["status"] | "unknown";
|
|
17
17
|
cancellationRequested: boolean;
|
|
18
|
+
deadlineRotationRequested?: boolean;
|
|
18
19
|
}): boolean {
|
|
19
|
-
return
|
|
20
|
+
return (
|
|
21
|
+
input.activityStatus !== "cancelled" &&
|
|
22
|
+
!input.cancellationRequested &&
|
|
23
|
+
input.deadlineRotationRequested !== true
|
|
24
|
+
);
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/**
|
|
@@ -29,9 +34,13 @@ export function shouldStartPeriodicWorkspaceSnapshot(input: {
|
|
|
29
34
|
firstProviderRequestStarted: boolean;
|
|
30
35
|
snapshotInFlight: boolean;
|
|
31
36
|
turnEndCaptureInProgress: boolean;
|
|
37
|
+
deadlineRotationRequested: boolean;
|
|
32
38
|
}): boolean {
|
|
33
39
|
return (
|
|
34
|
-
input.firstProviderRequestStarted &&
|
|
40
|
+
input.firstProviderRequestStarted &&
|
|
41
|
+
!input.snapshotInFlight &&
|
|
42
|
+
!input.turnEndCaptureInProgress &&
|
|
43
|
+
!input.deadlineRotationRequested
|
|
35
44
|
);
|
|
36
45
|
}
|
|
37
46
|
|
|
@@ -80,6 +89,7 @@ export function assertSessionAttemptQuiescenceRecoveryDurable(input: {
|
|
|
80
89
|
export const QUIESCENCE_PROOF_SIGNAL_INITIAL_RETRY_MS = 250;
|
|
81
90
|
export const QUIESCENCE_PROOF_SIGNAL_MAX_RETRY_MS = 5_000;
|
|
82
91
|
export const TURN_QUIESCENCE_WATCHDOG_MS = 5 * 60_000;
|
|
92
|
+
export const TURN_FINALIZER_STEP_TIMEOUT_MS = 30_000;
|
|
83
93
|
|
|
84
94
|
/** A fenced activity must never heartbeat forever while its physical writer
|
|
85
95
|
* drain is stuck. The last-resort worker exit stops every in-process writer;
|
|
@@ -232,35 +242,40 @@ export async function releaseTurnSandboxAfterWriterDrain(
|
|
|
232
242
|
}
|
|
233
243
|
|
|
234
244
|
/**
|
|
235
|
-
* Await
|
|
236
|
-
* execution window.
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
245
|
+
* Await bounded, idempotent finalizer housekeeping only while this Temporal
|
|
246
|
+
* activity still owns its execution window. A completed turn must never keep
|
|
247
|
+
* heartbeating forever because a provider/tool close promise did not settle:
|
|
248
|
+
* that strands every later durable prompt behind an already-terminal attempt.
|
|
249
|
+
* Cancellation or the hard timeout detaches the operation with a rejection
|
|
250
|
+
* handler; the operation may still finish its attempt-scoped cleanup, but it
|
|
251
|
+
* cannot pin activity terminalization.
|
|
240
252
|
*/
|
|
241
253
|
export async function waitForTurnFinalizerStep<T>(
|
|
242
254
|
operation: Promise<T>,
|
|
243
255
|
signal: AbortSignal | undefined,
|
|
256
|
+
timeoutMs = TURN_FINALIZER_STEP_TIMEOUT_MS,
|
|
244
257
|
): Promise<T | undefined> {
|
|
245
|
-
if (
|
|
246
|
-
if (signal.aborted) {
|
|
258
|
+
if (signal?.aborted) {
|
|
247
259
|
void operation.catch(() => undefined);
|
|
248
260
|
return undefined;
|
|
249
261
|
}
|
|
250
262
|
|
|
251
|
-
let
|
|
252
|
-
const
|
|
253
|
-
|
|
263
|
+
let resolveDetached: (() => void) | undefined;
|
|
264
|
+
const detached = new Promise<undefined>((resolve) => {
|
|
265
|
+
resolveDetached = () => resolve(undefined);
|
|
254
266
|
});
|
|
255
|
-
const
|
|
267
|
+
const detach = (): void => {
|
|
256
268
|
void operation.catch(() => undefined);
|
|
257
|
-
|
|
269
|
+
resolveDetached?.();
|
|
258
270
|
};
|
|
259
|
-
signal
|
|
271
|
+
signal?.addEventListener("abort", detach, { once: true });
|
|
272
|
+
const timeout = setTimeout(detach, Math.max(1, timeoutMs));
|
|
273
|
+
timeout.unref?.();
|
|
260
274
|
try {
|
|
261
|
-
return await Promise.race([operation,
|
|
275
|
+
return await Promise.race([operation, detached]);
|
|
262
276
|
} finally {
|
|
263
|
-
|
|
277
|
+
clearTimeout(timeout);
|
|
278
|
+
signal?.removeEventListener("abort", detach);
|
|
264
279
|
}
|
|
265
280
|
}
|
|
266
281
|
|