@opengeni/worker-bundle 0.15.1 → 0.16.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/activities/agent-turn.d.ts +27 -10
- package/dist/activities/context-compaction.d.ts +2 -0
- package/dist/activities/documents.d.ts +3 -0
- package/dist/activities/run-input.d.ts +13 -7
- package/dist/activities/types.d.ts +13 -2
- package/dist/activities.d.ts +9 -0
- package/dist/index.js +356 -117
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +1 -1
- package/dist/workflows/document-index.d.ts +5 -5
- package/package.json +12 -12
- package/src/activities/agent-turn.ts +186 -64
- package/src/activities/context-compaction.ts +32 -8
- package/src/activities/documents.ts +51 -1
- package/src/activities/run-input.ts +218 -76
- package/src/activities/types.ts +17 -1
- package/src/workflows/document-index.ts +2 -5
package/dist/index.js
CHANGED
|
@@ -114,6 +114,9 @@ import {
|
|
|
114
114
|
getEnrollment,
|
|
115
115
|
abandonRecordingForTurnAttempt,
|
|
116
116
|
markSessionAttemptQuiesced,
|
|
117
|
+
getOrCreatePreferenceRegistrySnapshot,
|
|
118
|
+
getOrCreateWorkspaceInstructionPolicySnapshot,
|
|
119
|
+
PreferenceRegistryInitiatorError,
|
|
117
120
|
beginConnectorActionExecution,
|
|
118
121
|
completeConnectorActionExecution,
|
|
119
122
|
prepareConnectorActionApproval
|
|
@@ -128,10 +131,14 @@ import {
|
|
|
128
131
|
normalizeModelCallUsage,
|
|
129
132
|
normalizeSdkEvent,
|
|
130
133
|
sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
|
|
134
|
+
projectModelInputForCapabilities,
|
|
131
135
|
appendPersistentSessionSettings,
|
|
132
136
|
appendSessionInstructions,
|
|
137
|
+
appendWorkspaceGovernance,
|
|
133
138
|
appendWorkspaceMemory,
|
|
134
139
|
composeAgentInstructions,
|
|
140
|
+
hasActiveWorkspaceInstructionPolicy,
|
|
141
|
+
renderWorkspaceGovernanceContext,
|
|
135
142
|
summarizeForCompaction as summarizeForCompaction2,
|
|
136
143
|
requestRemoteCompactionV2,
|
|
137
144
|
serializedToolsForRemoteCompaction,
|
|
@@ -187,6 +194,7 @@ function connectionTokenResolverForTurn(input) {
|
|
|
187
194
|
// src/activities/agent-turn.ts
|
|
188
195
|
import {
|
|
189
196
|
assertTurnExecutionPolicyMatchesConfigV1,
|
|
197
|
+
calculateGatewayReportedCostMicros,
|
|
190
198
|
calculateModelUsageCostMicros,
|
|
191
199
|
configuredModelPricing,
|
|
192
200
|
configuredStaticUsageLimits as configuredStaticUsageLimits2,
|
|
@@ -195,7 +203,9 @@ import {
|
|
|
195
203
|
sandboxWarmRateMicrosPerSecond,
|
|
196
204
|
serviceTierForLatencyMode,
|
|
197
205
|
settingsWithResolvedModelContext,
|
|
198
|
-
resolveTurnExecutionPolicyV1
|
|
206
|
+
resolveTurnExecutionPolicyV1,
|
|
207
|
+
OPENGENI_GATEWAY_MODELS,
|
|
208
|
+
OPENGENI_GATEWAY_PROVIDER_ID
|
|
199
209
|
} from "@opengeni/config";
|
|
200
210
|
import { ApplicationFailure, CancelledFailure as CancelledFailure2 } from "@temporalio/activity";
|
|
201
211
|
|
|
@@ -1008,7 +1018,8 @@ import {
|
|
|
1008
1018
|
classifyCodexUsageLimitError,
|
|
1009
1019
|
codexRequestStorage,
|
|
1010
1020
|
isCodexTransportError,
|
|
1011
|
-
withCodexRequestOverrides
|
|
1021
|
+
withCodexRequestOverrides,
|
|
1022
|
+
selectCodexCredentialId
|
|
1012
1023
|
} from "@opengeni/codex";
|
|
1013
1024
|
|
|
1014
1025
|
// src/activities/common.ts
|
|
@@ -1069,9 +1080,15 @@ import {
|
|
|
1069
1080
|
import { contextInputBudgetTokens } from "@opengeni/config";
|
|
1070
1081
|
|
|
1071
1082
|
// src/activities/run-input.ts
|
|
1083
|
+
import {
|
|
1084
|
+
MODEL_ATTACHMENT_REFS_FIELD,
|
|
1085
|
+
FileResourceRef,
|
|
1086
|
+
resourceMountPath
|
|
1087
|
+
} from "@opengeni/contracts";
|
|
1072
1088
|
import { createHash } from "crypto";
|
|
1073
1089
|
import {
|
|
1074
1090
|
getActiveSessionHistoryItems,
|
|
1091
|
+
getFiles,
|
|
1075
1092
|
getLatestRunState,
|
|
1076
1093
|
getHumanInputResumeForEvent,
|
|
1077
1094
|
getSandboxSessionEnvelope,
|
|
@@ -1144,7 +1161,7 @@ function modelAttachmentDescriptor(contentType) {
|
|
|
1144
1161
|
return null;
|
|
1145
1162
|
}
|
|
1146
1163
|
async function modelAttachmentContentForFiles(files, readFileBytes) {
|
|
1147
|
-
const
|
|
1164
|
+
const selected = [];
|
|
1148
1165
|
let remainingBytes = MAX_INLINE_MODEL_ATTACHMENT_BYTES;
|
|
1149
1166
|
for (const file of files) {
|
|
1150
1167
|
const descriptor = modelAttachmentDescriptor(file.contentType);
|
|
@@ -1152,65 +1169,157 @@ async function modelAttachmentContentForFiles(files, readFileBytes) {
|
|
|
1152
1169
|
if (file.status !== "ready" || !descriptor || file.sizeBytes > remainingBytes || !/^[a-f0-9]{64}$/.test(checksum)) {
|
|
1153
1170
|
continue;
|
|
1154
1171
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1172
|
+
selected.push({ file, descriptor, checksum });
|
|
1173
|
+
remainingBytes -= file.sizeBytes;
|
|
1174
|
+
}
|
|
1175
|
+
const attachments = new Array(selected.length);
|
|
1176
|
+
let cursor = 0;
|
|
1177
|
+
const worker = async () => {
|
|
1178
|
+
while (cursor < selected.length) {
|
|
1179
|
+
const index = cursor++;
|
|
1180
|
+
const { file, descriptor, checksum } = selected[index];
|
|
1181
|
+
try {
|
|
1182
|
+
const bytes = await readFileBytes(file);
|
|
1183
|
+
if (bytes.byteLength !== file.sizeBytes) {
|
|
1184
|
+
console.error("model attachment bytes did not match finalized metadata", {
|
|
1185
|
+
fileId: file.id,
|
|
1186
|
+
expectedSizeBytes: file.sizeBytes,
|
|
1187
|
+
actualSizeBytes: bytes.byteLength
|
|
1188
|
+
});
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
|
|
1192
|
+
console.error("model attachment checksum did not match finalized metadata", {
|
|
1193
|
+
fileId: file.id
|
|
1194
|
+
});
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
attachments[index] = {
|
|
1198
|
+
kind: descriptor.kind,
|
|
1199
|
+
fileId: file.id,
|
|
1200
|
+
filename: file.safeFilename,
|
|
1201
|
+
contentType: descriptor.contentType,
|
|
1202
|
+
dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`
|
|
1203
|
+
};
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
console.error("model attachment content read failed; retaining sandbox path fallback", {
|
|
1159
1206
|
fileId: file.id,
|
|
1160
|
-
|
|
1161
|
-
actualSizeBytes: bytes.byteLength
|
|
1207
|
+
errorType: safeErrorType(error)
|
|
1162
1208
|
});
|
|
1163
|
-
continue;
|
|
1164
1209
|
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1210
|
+
}
|
|
1211
|
+
};
|
|
1212
|
+
await Promise.all(
|
|
1213
|
+
Array.from({ length: Math.min(8, selected.length) }, async () => await worker())
|
|
1214
|
+
);
|
|
1215
|
+
return attachments.filter(
|
|
1216
|
+
(attachment) => attachment !== void 0
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
function modelAcceptsFileMediaType(policy, contentType) {
|
|
1220
|
+
const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
|
|
1221
|
+
return policy.inputFileMediaTypes.some(
|
|
1222
|
+
(accepted) => accepted === normalized || accepted.endsWith("/*") && normalized.startsWith(accepted.slice(0, -1))
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
function attachmentRefsFromItem(item) {
|
|
1226
|
+
const raw = item[MODEL_ATTACHMENT_REFS_FIELD];
|
|
1227
|
+
if (!Array.isArray(raw)) return [];
|
|
1228
|
+
const refs = [];
|
|
1229
|
+
for (const candidate of raw) {
|
|
1230
|
+
const parsed = FileResourceRef.safeParse(candidate);
|
|
1231
|
+
if (parsed.success) refs.push(parsed.data);
|
|
1232
|
+
}
|
|
1233
|
+
return refs;
|
|
1234
|
+
}
|
|
1235
|
+
function attachmentUnavailableText(ref, file) {
|
|
1236
|
+
const filename = file?.safeFilename ?? ref.fileId;
|
|
1237
|
+
const mediaType = file?.contentType ?? "unknown type";
|
|
1238
|
+
const path = file ? sandboxFilePath(ref, file) : `/workspace/${resourceMountPath(ref)}`;
|
|
1239
|
+
return `[Attachment not included directly because the selected model does not accept this input or it exceeded the safe inline limit: ${filename} (${mediaType}). It remains available to tools in the sandbox at ${path}.]`;
|
|
1240
|
+
}
|
|
1241
|
+
function createModelHistoryAttachmentProjector(db, workspaceId, policy, readFileBytes) {
|
|
1242
|
+
const fileById = /* @__PURE__ */ new Map();
|
|
1243
|
+
const missingFileIds = /* @__PURE__ */ new Set();
|
|
1244
|
+
const contentById = /* @__PURE__ */ new Map();
|
|
1245
|
+
const attemptedContentIds = /* @__PURE__ */ new Set();
|
|
1246
|
+
return async (items) => {
|
|
1247
|
+
const refsByIndex = /* @__PURE__ */ new Map();
|
|
1248
|
+
const orderedFileIds = [];
|
|
1249
|
+
const seenFileIds = /* @__PURE__ */ new Set();
|
|
1250
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
1251
|
+
const refs = attachmentRefsFromItem(items[index]);
|
|
1252
|
+
if (refs.length === 0) continue;
|
|
1253
|
+
refsByIndex.set(index, refs);
|
|
1254
|
+
for (const ref of refs) {
|
|
1255
|
+
if (seenFileIds.has(ref.fileId)) continue;
|
|
1256
|
+
seenFileIds.add(ref.fileId);
|
|
1257
|
+
orderedFileIds.push(ref.fileId);
|
|
1170
1258
|
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1259
|
+
}
|
|
1260
|
+
if (refsByIndex.size === 0) return items;
|
|
1261
|
+
const unknownIds = orderedFileIds.filter((id) => !fileById.has(id) && !missingFileIds.has(id));
|
|
1262
|
+
if (unknownIds.length > 0) {
|
|
1263
|
+
const files = await getFiles(db, workspaceId, unknownIds);
|
|
1264
|
+
for (const file of files) fileById.set(file.id, file);
|
|
1265
|
+
for (const id of unknownIds) {
|
|
1266
|
+
if (!fileById.has(id)) missingFileIds.add(id);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
if (readFileBytes) {
|
|
1270
|
+
const readable = [...orderedFileIds].reverse().map((id) => fileById.get(id)).filter((file) => {
|
|
1271
|
+
if (!file || attemptedContentIds.has(file.id)) return false;
|
|
1272
|
+
const descriptor = modelAttachmentDescriptor(file.contentType);
|
|
1273
|
+
return Boolean(
|
|
1274
|
+
descriptor && (descriptor.kind === "image" && policy.supportsImageInput || descriptor.kind === "file" && modelAcceptsFileMediaType(policy, file.contentType))
|
|
1275
|
+
);
|
|
1177
1276
|
});
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1277
|
+
for (const file of readable) attemptedContentIds.add(file.id);
|
|
1278
|
+
const content = await modelAttachmentContentForFiles(readable, readFileBytes);
|
|
1279
|
+
for (const attachment of content) contentById.set(attachment.fileId, attachment);
|
|
1280
|
+
}
|
|
1281
|
+
const projected = [...items];
|
|
1282
|
+
for (const [index, refs] of refsByIndex) {
|
|
1283
|
+
const original = items[index];
|
|
1284
|
+
const existingContent = Array.isArray(original.content) ? [...original.content] : [{ type: "input_text", text: String(original.content ?? "") }];
|
|
1285
|
+
const attachmentParts = refs.map((ref) => {
|
|
1286
|
+
const attachment = contentById.get(ref.fileId);
|
|
1287
|
+
if (!attachment) {
|
|
1288
|
+
return {
|
|
1289
|
+
type: "input_text",
|
|
1290
|
+
text: attachmentUnavailableText(ref, fileById.get(ref.fileId))
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
return attachment.kind === "image" ? { type: "input_image", image: attachment.dataUrl } : {
|
|
1294
|
+
type: "input_file",
|
|
1295
|
+
file: attachment.dataUrl,
|
|
1296
|
+
filename: attachment.filename
|
|
1297
|
+
};
|
|
1183
1298
|
});
|
|
1299
|
+
const clone = {
|
|
1300
|
+
...original,
|
|
1301
|
+
content: [...existingContent, ...attachmentParts]
|
|
1302
|
+
};
|
|
1303
|
+
delete clone[MODEL_ATTACHMENT_REFS_FIELD];
|
|
1304
|
+
projected[index] = clone;
|
|
1184
1305
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1306
|
+
return projected;
|
|
1307
|
+
};
|
|
1187
1308
|
}
|
|
1188
|
-
function
|
|
1189
|
-
if (
|
|
1190
|
-
let currentUserIndex = -1;
|
|
1309
|
+
function withCurrentUserAttachmentRefs(historyItems, refs) {
|
|
1310
|
+
if (refs.length === 0) return historyItems;
|
|
1191
1311
|
for (let index = historyItems.length - 1; index >= 0; index -= 1) {
|
|
1192
1312
|
const item = historyItems[index];
|
|
1193
|
-
if (item
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1313
|
+
if (item.type !== "message" || item.role !== "user") continue;
|
|
1314
|
+
const existing = attachmentRefsFromItem(item);
|
|
1315
|
+
const existingIds = new Set(existing.map((ref) => ref.fileId));
|
|
1316
|
+
const additions = refs.filter((ref) => !existingIds.has(ref.fileId));
|
|
1317
|
+
if (additions.length === 0) return historyItems;
|
|
1318
|
+
const projected = [...historyItems];
|
|
1319
|
+
projected[index] = { ...item, [MODEL_ATTACHMENT_REFS_FIELD]: [...existing, ...additions] };
|
|
1320
|
+
return projected;
|
|
1197
1321
|
}
|
|
1198
|
-
|
|
1199
|
-
const currentUser = historyItems[currentUserIndex];
|
|
1200
|
-
const existingContent = Array.isArray(currentUser.content) ? [...currentUser.content] : [{ type: "input_text", text: String(currentUser.content ?? "") }];
|
|
1201
|
-
const attachmentContent = attachments.map(
|
|
1202
|
-
(attachment) => attachment.kind === "image" ? { type: "input_image", image: attachment.dataUrl } : {
|
|
1203
|
-
type: "input_file",
|
|
1204
|
-
file: attachment.dataUrl,
|
|
1205
|
-
filename: attachment.filename
|
|
1206
|
-
}
|
|
1207
|
-
);
|
|
1208
|
-
const projected = [...historyItems];
|
|
1209
|
-
projected[currentUserIndex] = {
|
|
1210
|
-
...currentUser,
|
|
1211
|
-
content: [...existingContent, ...attachmentContent]
|
|
1212
|
-
};
|
|
1213
|
-
return projected;
|
|
1322
|
+
return historyItems;
|
|
1214
1323
|
}
|
|
1215
1324
|
async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN, options) {
|
|
1216
1325
|
if (!trigger) {
|
|
@@ -1250,10 +1359,6 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
|
|
|
1250
1359
|
resources
|
|
1251
1360
|
);
|
|
1252
1361
|
const attachmentContext = userMessageAttachmentsContext(fileAttachments);
|
|
1253
|
-
const modelAttachments = options.readFileBytesForModel ? await modelAttachmentContentForFiles(
|
|
1254
|
-
fileAttachments.map((attachment) => attachment.file),
|
|
1255
|
-
options.readFileBytesForModel
|
|
1256
|
-
) : [];
|
|
1257
1362
|
return await messageInput(
|
|
1258
1363
|
db,
|
|
1259
1364
|
runtime,
|
|
@@ -1262,7 +1367,8 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
|
|
|
1262
1367
|
void 0,
|
|
1263
1368
|
joinInternalContext(internalContext, attachmentContext),
|
|
1264
1369
|
current,
|
|
1265
|
-
|
|
1370
|
+
fileAttachments.map((attachment) => attachment.resource),
|
|
1371
|
+
options.projectModelHistory
|
|
1266
1372
|
);
|
|
1267
1373
|
}
|
|
1268
1374
|
if (trigger.type === "system.update.delivered") {
|
|
@@ -1321,20 +1427,22 @@ function joinInternalContext(...parts) {
|
|
|
1321
1427
|
const content = parts.map((part) => part?.trim()).filter((part) => Boolean(part));
|
|
1322
1428
|
return content.length > 0 ? content.join("\n\n") : void 0;
|
|
1323
1429
|
}
|
|
1324
|
-
async function messageInput(db, runtime, agent, trigger, text, internalContext, current = NON_CODEX_TURN,
|
|
1430
|
+
async function messageInput(db, runtime, agent, trigger, text, internalContext, current = NON_CODEX_TURN, currentAttachmentRefs = [], projectModelHistory) {
|
|
1325
1431
|
const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
|
|
1326
1432
|
const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
|
|
1327
|
-
const
|
|
1433
|
+
const referencedHistory = withCurrentUserAttachmentRefs(
|
|
1328
1434
|
applyCodexHistoryStrip(stored, current),
|
|
1329
|
-
|
|
1435
|
+
currentAttachmentRefs
|
|
1330
1436
|
);
|
|
1437
|
+
const historyItems = projectModelHistory ? await projectModelHistory(referencedHistory) : referencedHistory;
|
|
1331
1438
|
return {
|
|
1332
1439
|
input: await runtime.prepareInput(agent, {
|
|
1333
1440
|
kind: "message",
|
|
1334
1441
|
...text ? { text } : {},
|
|
1335
1442
|
...internalContext ? { internalContext } : {},
|
|
1336
1443
|
historyItems,
|
|
1337
|
-
sandboxEnvelope: envelope
|
|
1444
|
+
sandboxEnvelope: envelope,
|
|
1445
|
+
...projectModelHistory ? { modelInputAlreadyProjected: true } : {}
|
|
1338
1446
|
}),
|
|
1339
1447
|
modelHistoryFromItems: true
|
|
1340
1448
|
};
|
|
@@ -1358,7 +1466,7 @@ function userMessageAttachmentsContext(attachments) {
|
|
|
1358
1466
|
return ["Attached files are available in the sandbox:", ...attachedFiles].join("\n");
|
|
1359
1467
|
}
|
|
1360
1468
|
function sandboxFilePath(resource, file) {
|
|
1361
|
-
return `/workspace/${resource
|
|
1469
|
+
return `/workspace/${resourceMountPath(resource)}/${file.safeFilename}`;
|
|
1362
1470
|
}
|
|
1363
1471
|
|
|
1364
1472
|
// src/activities/context-compaction.ts
|
|
@@ -1401,9 +1509,15 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
|
|
|
1401
1509
|
requestConsumed
|
|
1402
1510
|
};
|
|
1403
1511
|
}
|
|
1404
|
-
const
|
|
1405
|
-
|
|
1512
|
+
const canonicalItems = applyCodexHistoryStrip(
|
|
1513
|
+
active,
|
|
1514
|
+
options.codexAccount ?? { currentCodexCredentialId: null }
|
|
1515
|
+
);
|
|
1516
|
+
const projectForWire = async (input) => sanitizeHistoryItemsForModel(
|
|
1517
|
+
options.projectModelInput ? await options.projectModelInput(input) : input,
|
|
1518
|
+
settings.modelToolOutputTruncationTokens
|
|
1406
1519
|
);
|
|
1520
|
+
const items = await projectForWire(canonicalItems);
|
|
1407
1521
|
const decision = decideCompaction({
|
|
1408
1522
|
items,
|
|
1409
1523
|
lastInputTokens,
|
|
@@ -1438,17 +1552,28 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
|
|
|
1438
1552
|
}
|
|
1439
1553
|
await options.publishLiveEvents?.(started.events);
|
|
1440
1554
|
if (useRemoteV2) {
|
|
1441
|
-
const outcome2 = await compactContextRemoteV2(
|
|
1555
|
+
const outcome2 = await compactContextRemoteV2(
|
|
1556
|
+
db,
|
|
1557
|
+
settings,
|
|
1558
|
+
scope,
|
|
1559
|
+
canonicalItems,
|
|
1560
|
+
items,
|
|
1561
|
+
decision,
|
|
1562
|
+
options,
|
|
1563
|
+
projectForWire
|
|
1564
|
+
);
|
|
1442
1565
|
return prependCompactionEvents(started.events, outcome2);
|
|
1443
1566
|
}
|
|
1444
1567
|
const outcome = await compactContextPortable(
|
|
1445
1568
|
db,
|
|
1446
1569
|
settings,
|
|
1447
1570
|
scope,
|
|
1571
|
+
canonicalItems,
|
|
1448
1572
|
items,
|
|
1449
1573
|
decision,
|
|
1450
1574
|
summarize,
|
|
1451
|
-
options
|
|
1575
|
+
options,
|
|
1576
|
+
projectForWire
|
|
1452
1577
|
);
|
|
1453
1578
|
return prependCompactionEvents(started.events, outcome);
|
|
1454
1579
|
}
|
|
@@ -1485,7 +1610,7 @@ async function settleSkippedAfterStart(db, scope, options, reason) {
|
|
|
1485
1610
|
requestConsumed: clearRequestedCompaction
|
|
1486
1611
|
};
|
|
1487
1612
|
}
|
|
1488
|
-
async function compactContextRemoteV2(db, settings, scope, items, decision, options) {
|
|
1613
|
+
async function compactContextRemoteV2(db, settings, scope, canonicalItems, items, decision, options, projectForWire) {
|
|
1489
1614
|
if (!options.requestRemoteCompactionV2) {
|
|
1490
1615
|
throw new EmptyCompactionSummaryError({
|
|
1491
1616
|
stage: "remote_v2_requester",
|
|
@@ -1494,8 +1619,8 @@ async function compactContextRemoteV2(db, settings, scope, items, decision, opti
|
|
|
1494
1619
|
}
|
|
1495
1620
|
const estimatedTokensBefore = estimateTokens(items);
|
|
1496
1621
|
const compactionItem = await options.requestRemoteCompactionV2(settings, items);
|
|
1497
|
-
const replacementHistory = buildRemoteV2ReplacementHistory(
|
|
1498
|
-
const estimatedTokensAfter = estimateTokens(replacementHistory);
|
|
1622
|
+
const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
|
|
1623
|
+
const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
|
|
1499
1624
|
const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
|
|
1500
1625
|
const tailItem = replacementHistory.at(-1);
|
|
1501
1626
|
if (!tailItem) {
|
|
@@ -1540,14 +1665,14 @@ async function compactContextRemoteV2(db, settings, scope, items, decision, opti
|
|
|
1540
1665
|
events: applied.events
|
|
1541
1666
|
};
|
|
1542
1667
|
}
|
|
1543
|
-
async function compactContextPortable(db, settings, scope, items, decision, summarize, options) {
|
|
1668
|
+
async function compactContextPortable(db, settings, scope, canonicalItems, items, decision, summarize, options, projectForWire) {
|
|
1544
1669
|
const estimatedTokensBefore = estimateTokens(items);
|
|
1545
1670
|
const summarized = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
|
|
1546
1671
|
const summaryBody = summarized.summaryBody;
|
|
1547
|
-
const replacementHistory = buildCompactionReplacementHistory(
|
|
1548
|
-
const estimatedTokensAfter = estimateTokens(replacementHistory);
|
|
1672
|
+
const replacementHistory = buildCompactionReplacementHistory(canonicalItems, summaryBody);
|
|
1673
|
+
const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
|
|
1549
1674
|
const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
|
|
1550
|
-
const previousReplacementFingerprint = latestCompactionReplacementFingerprint(
|
|
1675
|
+
const previousReplacementFingerprint = latestCompactionReplacementFingerprint(canonicalItems);
|
|
1551
1676
|
const summaryItem = replacementHistory.at(-1);
|
|
1552
1677
|
if (!summaryItem) {
|
|
1553
1678
|
return await settleSkippedAfterStart(db, scope, options, "summarization_failed");
|
|
@@ -6313,7 +6438,8 @@ import {
|
|
|
6313
6438
|
CAPABILITY_DESCRIPTORS,
|
|
6314
6439
|
DEFAULT_FIRST_PARTY_MCP_TOOLS,
|
|
6315
6440
|
evaluateWorkspaceModelPolicy as evaluateWorkspaceModelPolicy2,
|
|
6316
|
-
readTurnExecutionPolicyV1
|
|
6441
|
+
readTurnExecutionPolicyV1,
|
|
6442
|
+
resourceMountPath as resourceMountPath2
|
|
6317
6443
|
} from "@opengeni/contracts";
|
|
6318
6444
|
import { createHash as createHash4, randomUUID as randomUUID2 } from "crypto";
|
|
6319
6445
|
var PROVIDER_BACKPRESSURE_DELAY_MS = 6e4;
|
|
@@ -6718,6 +6844,8 @@ async function processModelResponseUsageEvent(input) {
|
|
|
6718
6844
|
externallyBilled: input.externallyBilled,
|
|
6719
6845
|
usage: responseUsage.usage,
|
|
6720
6846
|
normalizedUsage,
|
|
6847
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
6848
|
+
gatewayBilling: responseUsage.gatewayBilling,
|
|
6721
6849
|
sourceKey,
|
|
6722
6850
|
...input.latencyMode ? { latencyMode: input.latencyMode } : {},
|
|
6723
6851
|
observability: input.observability
|
|
@@ -6809,6 +6937,8 @@ async function processCompactionModelUsageEvent(input) {
|
|
|
6809
6937
|
externallyBilled: input.externallyBilled,
|
|
6810
6938
|
usage: input.usage.usage,
|
|
6811
6939
|
normalizedUsage,
|
|
6940
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
6941
|
+
gatewayBilling: input.usage.gatewayBilling,
|
|
6812
6942
|
sourceKey,
|
|
6813
6943
|
observability: input.observability
|
|
6814
6944
|
});
|
|
@@ -7073,6 +7203,9 @@ function computerToolModeForTurn(resolvedModel) {
|
|
|
7073
7203
|
if (!resolvedModel) {
|
|
7074
7204
|
return "hosted";
|
|
7075
7205
|
}
|
|
7206
|
+
if (!modelSupportsImageInputForTurn(resolvedModel)) {
|
|
7207
|
+
return "disabled";
|
|
7208
|
+
}
|
|
7076
7209
|
if (resolvedModel.provider.kind === "codex-subscription") {
|
|
7077
7210
|
return "function-image";
|
|
7078
7211
|
}
|
|
@@ -7093,8 +7226,22 @@ function structuredToolTransportForTurn(resolvedModel) {
|
|
|
7093
7226
|
function hostedWebSearchForTurn(resolvedModel, deploymentWebSearchEnabled) {
|
|
7094
7227
|
return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
|
|
7095
7228
|
}
|
|
7096
|
-
function
|
|
7097
|
-
return resolvedModel === null || resolvedModel.
|
|
7229
|
+
function modelSupportsImageInputForTurn(resolvedModel) {
|
|
7230
|
+
return resolvedModel === null || resolvedModel.configured.capabilities?.inputModalities.includes("image") !== false;
|
|
7231
|
+
}
|
|
7232
|
+
var LEGACY_INPUT_FILE_MEDIA_TYPES = [
|
|
7233
|
+
"application/json",
|
|
7234
|
+
"application/pdf",
|
|
7235
|
+
"application/x-yaml",
|
|
7236
|
+
"application/yaml",
|
|
7237
|
+
"text/*"
|
|
7238
|
+
];
|
|
7239
|
+
function modelAttachmentInputPolicyForTurn(resolvedModel) {
|
|
7240
|
+
const typedTransport = resolvedModel === null || resolvedModel.provider.api === "responses";
|
|
7241
|
+
return {
|
|
7242
|
+
supportsImageInput: typedTransport && modelSupportsImageInputForTurn(resolvedModel),
|
|
7243
|
+
inputFileMediaTypes: typedTransport ? resolvedModel?.configured.capabilities?.inputFileMediaTypes ?? LEGACY_INPUT_FILE_MEDIA_TYPES : []
|
|
7244
|
+
};
|
|
7098
7245
|
}
|
|
7099
7246
|
var TurnOperationCancelledError = class extends Error {
|
|
7100
7247
|
constructor(reason) {
|
|
@@ -8645,10 +8792,27 @@ function createRunAgentTurnActivity(services) {
|
|
|
8645
8792
|
const rigName = rigVersion && session.rigId ? await getRigName(db, input.workspaceId, session.rigId) ?? "rig" : null;
|
|
8646
8793
|
rigId = session.rigId ?? "";
|
|
8647
8794
|
rigVersionId = session.rigVersionId ?? "";
|
|
8648
|
-
const
|
|
8649
|
-
|
|
8650
|
-
input.workspaceId
|
|
8651
|
-
|
|
8795
|
+
const governanceClaims = {
|
|
8796
|
+
accountId: input.accountId,
|
|
8797
|
+
workspaceId: input.workspaceId,
|
|
8798
|
+
sessionId: input.sessionId,
|
|
8799
|
+
turnId: turn.id,
|
|
8800
|
+
attemptId: input.attemptId,
|
|
8801
|
+
executionGeneration: turn.executionGeneration
|
|
8802
|
+
};
|
|
8803
|
+
const [workspaceAgentInstructions, instructionPolicySnapshot, preferenceSnapshot] = await Promise.all([
|
|
8804
|
+
resolveWorkspaceAgentInstructions(db, input.workspaceId),
|
|
8805
|
+
getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
|
|
8806
|
+
getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
|
|
8807
|
+
if (error instanceof PreferenceRegistryInitiatorError) return null;
|
|
8808
|
+
throw error;
|
|
8809
|
+
})
|
|
8810
|
+
]);
|
|
8811
|
+
const workspaceGovernance = renderWorkspaceGovernanceContext({
|
|
8812
|
+
instructionPolicy: instructionPolicySnapshot,
|
|
8813
|
+
preferences: preferenceSnapshot
|
|
8814
|
+
});
|
|
8815
|
+
const structuredWorkspacePolicyActive = hasActiveWorkspaceInstructionPolicy(instructionPolicySnapshot);
|
|
8652
8816
|
const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
|
|
8653
8817
|
const baseRunSettings = {
|
|
8654
8818
|
// IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
|
|
@@ -8696,6 +8860,15 @@ function createRunAgentTurnActivity(services) {
|
|
|
8696
8860
|
capabilitySettings,
|
|
8697
8861
|
turnExecutionPolicy.productModelId
|
|
8698
8862
|
);
|
|
8863
|
+
const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
|
|
8864
|
+
const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
|
|
8865
|
+
const attachmentProjector = createModelHistoryAttachmentProjector(
|
|
8866
|
+
db,
|
|
8867
|
+
input.workspaceId,
|
|
8868
|
+
modelInputPolicy,
|
|
8869
|
+
objectStorage ? (file) => objectStorage.getFileBytes(file) : void 0
|
|
8870
|
+
);
|
|
8871
|
+
const modelHistoryProjector = async (items) => projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
|
|
8699
8872
|
const selectedProvider = resolvedModel?.provider;
|
|
8700
8873
|
const publicProviderHeaders = new Set(
|
|
8701
8874
|
selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? []
|
|
@@ -8925,19 +9098,22 @@ function createRunAgentTurnActivity(services) {
|
|
|
8925
9098
|
const persistentSessionSettings = {
|
|
8926
9099
|
titleIsSet: Boolean(session.title?.trim())
|
|
8927
9100
|
};
|
|
8928
|
-
const compactionInstructions =
|
|
8929
|
-
|
|
8930
|
-
|
|
8931
|
-
|
|
8932
|
-
|
|
8933
|
-
|
|
8934
|
-
|
|
9101
|
+
const compactionInstructions = appendWorkspaceMemory(
|
|
9102
|
+
appendPersistentSessionSettings(
|
|
9103
|
+
appendSessionInstructions(
|
|
9104
|
+
appendWorkspaceGovernance(
|
|
9105
|
+
composeAgentInstructions(
|
|
9106
|
+
structuredWorkspacePolicyActive ? modelRunSettings.agentInstructionsTemplate : workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate,
|
|
9107
|
+
void 0,
|
|
9108
|
+
rigVersion && rigName ? { name: rigName, version: rigVersion.version } : void 0
|
|
9109
|
+
),
|
|
9110
|
+
workspaceGovernance ?? void 0
|
|
8935
9111
|
),
|
|
8936
|
-
|
|
9112
|
+
session.instructions ?? void 0
|
|
8937
9113
|
),
|
|
8938
|
-
|
|
9114
|
+
persistentSessionSettings
|
|
8939
9115
|
),
|
|
8940
|
-
|
|
9116
|
+
workspaceMemory ?? void 0
|
|
8941
9117
|
);
|
|
8942
9118
|
const requested = await isSessionCompactionRequested(
|
|
8943
9119
|
db,
|
|
@@ -8968,6 +9144,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
8968
9144
|
codexAccount: {
|
|
8969
9145
|
currentCodexCredentialId: effectiveCodexCredentialId
|
|
8970
9146
|
},
|
|
9147
|
+
projectModelInput: modelHistoryProjector,
|
|
8971
9148
|
...compactionModeOptions
|
|
8972
9149
|
}
|
|
8973
9150
|
),
|
|
@@ -9697,7 +9874,8 @@ function createRunAgentTurnActivity(services) {
|
|
|
9697
9874
|
db,
|
|
9698
9875
|
objectStorage,
|
|
9699
9876
|
input.workspaceId,
|
|
9700
|
-
turnResources
|
|
9877
|
+
turnResources,
|
|
9878
|
+
activeSandboxBackend ?? groupBoxBackend
|
|
9701
9879
|
),
|
|
9702
9880
|
cancellationSignal,
|
|
9703
9881
|
void 0
|
|
@@ -9867,6 +10045,8 @@ function createRunAgentTurnActivity(services) {
|
|
|
9867
10045
|
// session's MCP allow-list; the effective context window drives the
|
|
9868
10046
|
// compaction threshold.
|
|
9869
10047
|
hostedWebSearch,
|
|
10048
|
+
supportsImageInput,
|
|
10049
|
+
inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
|
|
9870
10050
|
...resolvedModel ? {
|
|
9871
10051
|
encryptedReasoning: resolvedModel.provider.api === "responses" && runSettings.openaiReasoningEncryptedContent,
|
|
9872
10052
|
contextWindowTokens: resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
|
|
@@ -9912,7 +10092,8 @@ function createRunAgentTurnActivity(services) {
|
|
|
9912
10092
|
skillLibrarySkills: skillLibraryRuntime.skillLibrarySkills,
|
|
9913
10093
|
skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections
|
|
9914
10094
|
} : {},
|
|
9915
|
-
|
|
10095
|
+
...!structuredWorkspacePolicyActive && workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
|
|
10096
|
+
...workspaceGovernance ? { workspaceGovernance } : {},
|
|
9916
10097
|
...workspaceMemory ? { workspaceMemory } : {},
|
|
9917
10098
|
// Per-session persona tier (session > workspace > deployment default).
|
|
9918
10099
|
// Composed system-level AFTER the workspace persona so it refines it for
|
|
@@ -10162,6 +10343,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10162
10343
|
codexAccount: {
|
|
10163
10344
|
currentCodexCredentialId: effectiveCodexCredentialId
|
|
10164
10345
|
},
|
|
10346
|
+
projectModelInput: modelHistoryProjector,
|
|
10165
10347
|
...compactionModeOptions
|
|
10166
10348
|
}
|
|
10167
10349
|
),
|
|
@@ -10275,12 +10457,14 @@ function createRunAgentTurnActivity(services) {
|
|
|
10275
10457
|
codexAccount: {
|
|
10276
10458
|
currentCodexCredentialId: effectiveCodexCredentialId
|
|
10277
10459
|
},
|
|
10460
|
+
projectModelInput: modelHistoryProjector,
|
|
10278
10461
|
...compactionModeOptions
|
|
10279
10462
|
} : {
|
|
10280
10463
|
trigger: "auto",
|
|
10281
10464
|
codexAccount: {
|
|
10282
10465
|
currentCodexCredentialId: effectiveCodexCredentialId
|
|
10283
10466
|
},
|
|
10467
|
+
projectModelInput: modelHistoryProjector,
|
|
10284
10468
|
...compactionModeOptions
|
|
10285
10469
|
}
|
|
10286
10470
|
),
|
|
@@ -10348,15 +10532,16 @@ function createRunAgentTurnActivity(services) {
|
|
|
10348
10532
|
}
|
|
10349
10533
|
let fileMaterializationFailures = [];
|
|
10350
10534
|
let fileDownloadsMaterializedForRun = false;
|
|
10351
|
-
if (resolvedSandbox && setupBoxSession &&
|
|
10535
|
+
if (resolvedSandbox && setupBoxSession && fileResourceDownloads.length > 0) {
|
|
10352
10536
|
const boxInstanceId = resolvedSandbox.established.instanceId;
|
|
10353
|
-
const
|
|
10537
|
+
const cacheMaterialization = resolvedSandbox.established.backendId !== "selfhosted";
|
|
10538
|
+
const alreadyMaterialized = cacheMaterialization ? await getMaterializedSandboxFileResources(db, {
|
|
10354
10539
|
accountId: input.accountId,
|
|
10355
10540
|
workspaceId: input.workspaceId,
|
|
10356
10541
|
sandboxGroupId: session.sandboxGroupId,
|
|
10357
10542
|
expectedEpoch: resolvedSandbox.leaseEpoch,
|
|
10358
10543
|
instanceId: boxInstanceId
|
|
10359
|
-
});
|
|
10544
|
+
}) : /* @__PURE__ */ new Set();
|
|
10360
10545
|
const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
|
|
10361
10546
|
fileResourceDownloads,
|
|
10362
10547
|
alreadyMaterialized
|
|
@@ -10385,7 +10570,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10385
10570
|
fileMaterializationFailures = materialized.failures;
|
|
10386
10571
|
const failedFileIds = new Set(materialized.failures.map((failure) => failure.fileId));
|
|
10387
10572
|
const succeededFileIds = downloadsToMaterialize.map((download) => download.fileId).filter((fileId) => !failedFileIds.has(fileId));
|
|
10388
|
-
if (succeededFileIds.length > 0) {
|
|
10573
|
+
if (cacheMaterialization && succeededFileIds.length > 0) {
|
|
10389
10574
|
await markSandboxFileResourcesMaterialized(db, {
|
|
10390
10575
|
accountId: input.accountId,
|
|
10391
10576
|
workspaceId: input.workspaceId,
|
|
@@ -10418,9 +10603,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10418
10603
|
recovering: turn.executionGeneration > 1,
|
|
10419
10604
|
...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
|
|
10420
10605
|
...runCredentialsNote ? { runCredentialsNote } : {},
|
|
10421
|
-
|
|
10422
|
-
readFileBytesForModel: (file) => objectStorage.getFileBytes(file)
|
|
10423
|
-
} : {}
|
|
10606
|
+
projectModelHistory: modelHistoryProjector
|
|
10424
10607
|
}
|
|
10425
10608
|
);
|
|
10426
10609
|
runInput = prepared.input;
|
|
@@ -10465,6 +10648,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10465
10648
|
codexAccount: {
|
|
10466
10649
|
currentCodexCredentialId: effectiveCodexCredentialId
|
|
10467
10650
|
},
|
|
10651
|
+
projectModelInput: modelHistoryProjector,
|
|
10468
10652
|
...compactionModeOptions
|
|
10469
10653
|
}
|
|
10470
10654
|
),
|
|
@@ -12564,6 +12748,17 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
|
|
|
12564
12748
|
const inputTokens = sanitizedUsage.inputTokens ?? 0;
|
|
12565
12749
|
const outputTokens = sanitizedUsage.outputTokens ?? 0;
|
|
12566
12750
|
const totalTokens = sanitizedUsage.totalTokens ?? 0;
|
|
12751
|
+
const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : void 0;
|
|
12752
|
+
if (gatewayBilling) {
|
|
12753
|
+
const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
|
|
12754
|
+
(candidate) => candidate.productId === input.model
|
|
12755
|
+
);
|
|
12756
|
+
if (!gatewayModel || !gatewayModel.providers.includes(gatewayBilling.finalProvider)) {
|
|
12757
|
+
throw new Error(
|
|
12758
|
+
`AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`
|
|
12759
|
+
);
|
|
12760
|
+
}
|
|
12761
|
+
}
|
|
12567
12762
|
if (input.externallyBilled) {
|
|
12568
12763
|
await recordUsageEvent(db, {
|
|
12569
12764
|
accountId: input.accountId,
|
|
@@ -12600,17 +12795,20 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
|
|
|
12600
12795
|
});
|
|
12601
12796
|
}
|
|
12602
12797
|
const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
|
|
12603
|
-
if (!shouldDebit || totalTokens === 0) {
|
|
12798
|
+
if (!shouldDebit || totalTokens === 0 && !gatewayBilling) {
|
|
12604
12799
|
return {
|
|
12605
12800
|
billingPath: "opengeni_credits",
|
|
12606
12801
|
pricedCostMicros: 0,
|
|
12607
|
-
normalizedUsage
|
|
12802
|
+
normalizedUsage,
|
|
12803
|
+
...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
|
|
12608
12804
|
};
|
|
12609
12805
|
}
|
|
12610
12806
|
if (!configuredModelPricing(settings)[input.model]) {
|
|
12611
12807
|
throw new Error(`Missing model pricing for ${input.model}`);
|
|
12612
12808
|
}
|
|
12613
|
-
const costMicros =
|
|
12809
|
+
const costMicros = gatewayBilling ? calculateGatewayReportedCostMicros(settings, input.model, gatewayBilling.inferenceCostUsd, {
|
|
12810
|
+
inputTokens
|
|
12811
|
+
}) : calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
|
|
12614
12812
|
latencyMode: input.latencyMode ?? "standard"
|
|
12615
12813
|
});
|
|
12616
12814
|
await recordUsageEvent(db, {
|
|
@@ -12647,7 +12845,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
|
|
|
12647
12845
|
// Additive: the prompt-cache slice of this call's input tokens, so the
|
|
12648
12846
|
// per-call debit record carries cache efficiency alongside the token
|
|
12649
12847
|
// counts. 0 when the provider did not report cached tokens.
|
|
12650
|
-
cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0
|
|
12848
|
+
cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
|
|
12849
|
+
...gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}
|
|
12651
12850
|
}
|
|
12652
12851
|
});
|
|
12653
12852
|
recordCreditMicros(input.observability, "usage", result.debitedMicros);
|
|
@@ -12655,7 +12854,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
|
|
|
12655
12854
|
return {
|
|
12656
12855
|
billingPath: "opengeni_credits",
|
|
12657
12856
|
pricedCostMicros: costMicros,
|
|
12658
|
-
normalizedUsage
|
|
12857
|
+
normalizedUsage,
|
|
12858
|
+
...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
|
|
12659
12859
|
};
|
|
12660
12860
|
}
|
|
12661
12861
|
async function recordAuthoritativeModelCallFact(input) {
|
|
@@ -12668,7 +12868,7 @@ async function recordAuthoritativeModelCallFact(input) {
|
|
|
12668
12868
|
turnId: input.turnId,
|
|
12669
12869
|
turnAttemptId: input.turnAttemptId,
|
|
12670
12870
|
sourceKey: input.sourceKey,
|
|
12671
|
-
provider: input.provider,
|
|
12871
|
+
provider: input.billing.upstreamProvider ?? input.provider,
|
|
12672
12872
|
providerApi: input.providerApi,
|
|
12673
12873
|
model: input.model,
|
|
12674
12874
|
billingPath: input.billing.billingPath,
|
|
@@ -12707,8 +12907,8 @@ function startOfUtcMonth2() {
|
|
|
12707
12907
|
const now = /* @__PURE__ */ new Date();
|
|
12708
12908
|
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
|
12709
12909
|
}
|
|
12710
|
-
async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspaceId, resources) {
|
|
12711
|
-
if (
|
|
12910
|
+
async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspaceId, resources, activeSandboxBackend = settings.sandboxBackend) {
|
|
12911
|
+
if (!requiresSignedFileResourceDownloads(settings, activeSandboxBackend)) {
|
|
12712
12912
|
return [];
|
|
12713
12913
|
}
|
|
12714
12914
|
const fileResources = resources.filter(
|
|
@@ -12729,21 +12929,25 @@ async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspace
|
|
|
12729
12929
|
const url = await downloadStorage.createGetUrl({ key: file.objectKey });
|
|
12730
12930
|
downloads.push({
|
|
12731
12931
|
fileId: file.id,
|
|
12732
|
-
mountPath: resource
|
|
12932
|
+
mountPath: resourceMountPath2(resource),
|
|
12733
12933
|
filename: file.safeFilename,
|
|
12734
12934
|
url: url.url,
|
|
12735
12935
|
expiresAt: url.expiresAt,
|
|
12736
|
-
sizeBytes: file.sizeBytes
|
|
12936
|
+
sizeBytes: file.sizeBytes,
|
|
12937
|
+
...file.sha256 ? { sha256: file.sha256 } : {}
|
|
12737
12938
|
});
|
|
12738
12939
|
}
|
|
12739
12940
|
return downloads;
|
|
12740
12941
|
}
|
|
12741
|
-
function requiresSignedFileResourceDownloads(settings) {
|
|
12742
|
-
if (
|
|
12942
|
+
function requiresSignedFileResourceDownloads(settings, activeSandboxBackend = settings.sandboxBackend) {
|
|
12943
|
+
if (activeSandboxBackend === "none") {
|
|
12944
|
+
return false;
|
|
12945
|
+
}
|
|
12946
|
+
if (activeSandboxBackend === "selfhosted") {
|
|
12743
12947
|
return true;
|
|
12744
12948
|
}
|
|
12745
|
-
const nativeBucketMount = CAPABILITY_DESCRIPTORS[
|
|
12746
|
-
return
|
|
12949
|
+
const nativeBucketMount = CAPABILITY_DESCRIPTORS[activeSandboxBackend].nativeBucketMount;
|
|
12950
|
+
return activeSandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible" || settings.objectStorageBackend === "aws-s3" || settings.objectStorageBackend === "gcs" || nativeBucketMount && settings.objectStorageBackend === "azure-blob";
|
|
12747
12951
|
}
|
|
12748
12952
|
function objectStorageForSandboxDownloads(settings, objectStorage) {
|
|
12749
12953
|
if (settings.objectStorageBackend !== "s3-compatible" || !settings.objectStorageSandboxEndpoint) {
|
|
@@ -12756,11 +12960,13 @@ function objectStorageForSandboxDownloads(settings, objectStorage) {
|
|
|
12756
12960
|
}
|
|
12757
12961
|
|
|
12758
12962
|
// src/activities/documents.ts
|
|
12759
|
-
import { indexDocumentNow } from "@opengeni/documents";
|
|
12963
|
+
import { getDocument, indexDocumentNow } from "@opengeni/documents";
|
|
12760
12964
|
import { configuredStaticUsageLimits as configuredStaticUsageLimits3 } from "@opengeni/config";
|
|
12761
12965
|
import {
|
|
12762
12966
|
getBillingBalance as getBillingBalance3,
|
|
12763
12967
|
recordUsageEvent as recordUsageEvent2,
|
|
12968
|
+
resolveDocumentIndexAuthority,
|
|
12969
|
+
rlsContextForWorkspace,
|
|
12764
12970
|
sumUsageQuantity as sumUsageQuantity3,
|
|
12765
12971
|
withWorkspaceUsageLock
|
|
12766
12972
|
} from "@opengeni/db";
|
|
@@ -12771,7 +12977,36 @@ function createDocumentActivities(services) {
|
|
|
12771
12977
|
if (!objectStorage) {
|
|
12772
12978
|
throw new Error("object storage is not configured");
|
|
12773
12979
|
}
|
|
12980
|
+
const context = await rlsContextForWorkspace(db, input.workspaceId);
|
|
12981
|
+
if (context.accountId !== input.accountId) {
|
|
12982
|
+
throw new Error("document account/workspace authority mismatch");
|
|
12983
|
+
}
|
|
12774
12984
|
return await withWorkspaceUsageLock(db, input.workspaceId, async (lockedDb) => {
|
|
12985
|
+
const storedAuthority = await resolveDocumentIndexAuthority(lockedDb, input);
|
|
12986
|
+
if (!storedAuthority) {
|
|
12987
|
+
throw new Error("document authority was not found before indexing");
|
|
12988
|
+
}
|
|
12989
|
+
const suppliedAuthorityFields = [
|
|
12990
|
+
"authorityKind",
|
|
12991
|
+
"authorityWorkspaceId",
|
|
12992
|
+
"authoritySubjectId"
|
|
12993
|
+
];
|
|
12994
|
+
const suppliedCount = suppliedAuthorityFields.filter(
|
|
12995
|
+
(field) => Object.prototype.hasOwnProperty.call(input, field)
|
|
12996
|
+
).length;
|
|
12997
|
+
const suppliedMismatch = Object.prototype.hasOwnProperty.call(input, "authorityKind") && input.authorityKind !== storedAuthority.authorityKind || Object.prototype.hasOwnProperty.call(input, "authorityWorkspaceId") && input.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || Object.prototype.hasOwnProperty.call(input, "authoritySubjectId") && input.authoritySubjectId !== storedAuthority.authoritySubjectId;
|
|
12998
|
+
if (suppliedMismatch) {
|
|
12999
|
+
throw new Error("document authority changed before indexing");
|
|
13000
|
+
}
|
|
13001
|
+
if (suppliedCount !== 0 && suppliedCount !== suppliedAuthorityFields.length) {
|
|
13002
|
+
throw new Error("document authority tuple is partial");
|
|
13003
|
+
}
|
|
13004
|
+
const claimedDocument = await getDocument(lockedDb, input.workspaceId, input.documentId, {
|
|
13005
|
+
viewerSubjectId: storedAuthority.authoritySubjectId
|
|
13006
|
+
});
|
|
13007
|
+
if (!claimedDocument || claimedDocument.authorityKind !== storedAuthority.authorityKind || claimedDocument.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || claimedDocument.authoritySubjectId !== storedAuthority.authoritySubjectId) {
|
|
13008
|
+
throw new Error("document authority changed before indexing");
|
|
13009
|
+
}
|
|
12775
13010
|
const document = await indexDocumentNow(
|
|
12776
13011
|
lockedDb,
|
|
12777
13012
|
objectStorage,
|
|
@@ -12802,8 +13037,12 @@ function createDocumentActivities(services) {
|
|
|
12802
13037
|
throw new Error(`monthly document indexing limit reached (${limit} chunks)`);
|
|
12803
13038
|
}
|
|
12804
13039
|
}
|
|
12805
|
-
}
|
|
13040
|
+
},
|
|
13041
|
+
{ viewerSubjectId: storedAuthority.authoritySubjectId }
|
|
12806
13042
|
);
|
|
13043
|
+
if (document.authorityKind !== storedAuthority.authorityKind || document.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || document.authoritySubjectId !== storedAuthority.authoritySubjectId) {
|
|
13044
|
+
throw new Error("document authority changed before indexing");
|
|
13045
|
+
}
|
|
12807
13046
|
if (document.status === "ready") {
|
|
12808
13047
|
await recordUsageEvent2(lockedDb, {
|
|
12809
13048
|
accountId: input.accountId,
|
|
@@ -12916,7 +13155,7 @@ import {
|
|
|
12916
13155
|
retainedProcessReconciliationProof,
|
|
12917
13156
|
retainedProcessSettlementIdentity as retainedProcessSettlementIdentity2,
|
|
12918
13157
|
settleSandboxCheckpointArtifactGc,
|
|
12919
|
-
rlsContextForWorkspace,
|
|
13158
|
+
rlsContextForWorkspace as rlsContextForWorkspace2,
|
|
12920
13159
|
settleRetainedProcess as settleRetainedProcess2
|
|
12921
13160
|
} from "@opengeni/db";
|
|
12922
13161
|
import { sandboxArchiveCaptureTimeoutMs as sandboxArchiveCaptureTimeoutMs4, sandboxWarmRateMicrosPerSecond as sandboxWarmRateMicrosPerSecond2 } from "@opengeni/config";
|
|
@@ -13767,7 +14006,7 @@ async function forceDrainOverLimitWorkspaces(db, settings, workspaceIds, observa
|
|
|
13767
14006
|
let forceDrained = 0;
|
|
13768
14007
|
for (const workspaceId of workspaceIds) {
|
|
13769
14008
|
try {
|
|
13770
|
-
const { accountId } = await
|
|
14009
|
+
const { accountId } = await rlsContextForWorkspace2(db, workspaceId);
|
|
13771
14010
|
const balance = enforceBalance ? await getBillingBalance4(db, accountId) : { balanceMicros: 1 };
|
|
13772
14011
|
const result = await forceDrainOverLimitViewerOnlyBoxes(db, {
|
|
13773
14012
|
workspaceId,
|
|
@@ -13895,7 +14134,7 @@ async function probeDrainableProviderReadiness(settings, lease) {
|
|
|
13895
14134
|
}
|
|
13896
14135
|
}
|
|
13897
14136
|
async function terminateDrainableBox(db, settings, row, observability, terminateBox, probeDrainableProvider) {
|
|
13898
|
-
const { accountId } = await
|
|
14137
|
+
const { accountId } = await rlsContextForWorkspace2(db, row.workspaceId);
|
|
13899
14138
|
const lease = await readLease4(db, row.workspaceId, row.sandboxGroupId);
|
|
13900
14139
|
if (!lease) {
|
|
13901
14140
|
return false;
|