@opengeni/worker-bundle 0.20.0 → 0.20.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 +25 -2
- package/dist/activities/google-drive-publication.d.ts +53 -0
- package/dist/activities/image-generation-references.d.ts +2 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +21 -0
- package/dist/activities/knowledge-source-sync.d.ts +29 -1
- package/dist/activities/run-input.d.ts +20 -2
- package/dist/activities/session-state.d.ts +5 -3
- package/dist/activities/types.d.ts +27 -0
- package/dist/{activities-control-XURE4VKN.js → activities-control-BJSDRSIP.js} +369 -20
- package/dist/activities-control-BJSDRSIP.js.map +1 -0
- package/dist/activities-control.d.ts +2 -2
- package/dist/{activities-turn-6EBXGGLS.js → activities-turn-J5YO7XXX.js} +2281 -1074
- package/dist/activities-turn-J5YO7XXX.js.map +1 -0
- package/dist/activities.d.ts +3 -3
- package/dist/{chunk-TYNPCG4U.js → chunk-N5TJIFCE.js} +69 -1
- package/dist/chunk-N5TJIFCE.js.map +1 -0
- package/dist/{chunk-5GRJIP5H.js → chunk-P6Z2SLAA.js} +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/observability-metrics.d.ts +37 -0
- package/dist/workflow-bundle.js +174 -4
- package/dist/workflows/session.d.ts +17 -1
- package/package.json +13 -13
- package/src/activities/agent-turn.ts +1801 -1175
- package/src/activities/documents.ts +6 -4
- package/src/activities/google-drive-publication.ts +840 -0
- package/src/activities/image-generation-references.ts +11 -2
- package/src/activities/knowledge-source-sync-driver.ts +23 -0
- package/src/activities/knowledge-source-sync.ts +384 -9
- package/src/activities/run-input.ts +175 -51
- package/src/activities/session-state.ts +69 -5
- package/src/activities/types.ts +27 -0
- package/src/activities.ts +2 -0
- package/src/observability-metrics.ts +184 -3
- package/src/workflows/session.ts +199 -8
- package/dist/activities-control-XURE4VKN.js.map +0 -1
- package/dist/activities-turn-6EBXGGLS.js.map +0 -1
- package/dist/chunk-TYNPCG4U.js.map +0 -1
- /package/dist/{chunk-5GRJIP5H.js.map → chunk-P6Z2SLAA.js.map} +0 -0
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
withFirstPartyTools,
|
|
38
38
|
wrapLazyTurnBoxWithRouting,
|
|
39
39
|
wrapTurnBoxWithRouting
|
|
40
|
-
} from "./chunk-
|
|
40
|
+
} from "./chunk-P6Z2SLAA.js";
|
|
41
41
|
import {
|
|
42
42
|
StreamTimingMetrics,
|
|
43
43
|
makeMachineOpObserver,
|
|
@@ -51,9 +51,12 @@ import {
|
|
|
51
51
|
recordModelRequestPhase,
|
|
52
52
|
recordSessionEventAppendLatency,
|
|
53
53
|
recordSessionEventPublishLatency,
|
|
54
|
+
recordTurnSandboxEstablishPolicy,
|
|
55
|
+
recordTurnStartupPhase,
|
|
56
|
+
recordTurnWorkerPreparationTotal,
|
|
54
57
|
runtimeMetricsHooksForObservability,
|
|
55
58
|
turnLifecycleMetricsFor
|
|
56
|
-
} from "./chunk-
|
|
59
|
+
} from "./chunk-N5TJIFCE.js";
|
|
57
60
|
|
|
58
61
|
// src/activities-turn.ts
|
|
59
62
|
import { createProductionAgentRuntime, summarizeForCompaction as summarizeForCompaction2 } from "@opengeni/runtime";
|
|
@@ -70,9 +73,11 @@ import {
|
|
|
70
73
|
getRigName,
|
|
71
74
|
getRigVersion,
|
|
72
75
|
getSandbox,
|
|
76
|
+
getFilesForSubject as getFilesForSubject2,
|
|
73
77
|
readActiveSandbox,
|
|
78
|
+
requireFileForSubject as requireFileForSubject3,
|
|
74
79
|
setActiveSandbox,
|
|
75
|
-
requireFile as
|
|
80
|
+
requireFile as requireFile2,
|
|
76
81
|
getSessionEvent as getSessionEvent2,
|
|
77
82
|
getSessionRootId as getSessionRootId2,
|
|
78
83
|
getSessionGoal,
|
|
@@ -102,7 +107,7 @@ import {
|
|
|
102
107
|
CODEX_CREDENTIAL_LEASE_TTL_MS,
|
|
103
108
|
getCodexRotationSettings,
|
|
104
109
|
getWorkspaceModelPolicy,
|
|
105
|
-
getWorkspaceGrant,
|
|
110
|
+
getWorkspaceGrant as getWorkspaceGrant2,
|
|
106
111
|
getWorkspace,
|
|
107
112
|
listCodexAccountStatuses,
|
|
108
113
|
fetchCodexUsageForAccount,
|
|
@@ -148,6 +153,7 @@ import {
|
|
|
148
153
|
SandboxImageConflictError,
|
|
149
154
|
ActiveSessionHistoryLimitExceededError,
|
|
150
155
|
ApprovalRunStateLimitExceededError,
|
|
156
|
+
isRetryableDatabaseTransportFailure,
|
|
151
157
|
isSessionEventPersistenceError,
|
|
152
158
|
getEnrollment,
|
|
153
159
|
abandonRecordingForTurnAttempt,
|
|
@@ -156,9 +162,9 @@ import {
|
|
|
156
162
|
getOrCreatePreferenceRegistrySnapshot,
|
|
157
163
|
getOrCreateWorkspaceInstructionPolicySnapshot,
|
|
158
164
|
PreferenceRegistryInitiatorError,
|
|
159
|
-
beginConnectorActionExecution,
|
|
160
|
-
completeConnectorActionExecution,
|
|
161
|
-
prepareConnectorActionApproval,
|
|
165
|
+
beginConnectorActionExecution as beginConnectorActionExecution2,
|
|
166
|
+
completeConnectorActionExecution as completeConnectorActionExecution2,
|
|
167
|
+
prepareConnectorActionApproval as prepareConnectorActionApproval2,
|
|
162
168
|
withCodexAppsRequestAuthorization
|
|
163
169
|
} from "@opengeni/db";
|
|
164
170
|
import { appendAndPublishTurnEventsFenced as appendAndPublishTurnEventsFenced2, publishDurableSessionEvents as publishDurableSessionEvents2 } from "@opengeni/events";
|
|
@@ -215,6 +221,604 @@ import {
|
|
|
215
221
|
stopRecording as stopRecordingOnBox2
|
|
216
222
|
} from "@opengeni/runtime";
|
|
217
223
|
|
|
224
|
+
// src/activities/google-drive-publication.ts
|
|
225
|
+
import { createHash } from "crypto";
|
|
226
|
+
import { AttemptToolApprovalRequiredError } from "@opengeni/codemode";
|
|
227
|
+
import {
|
|
228
|
+
GOOGLE_DRIVE_FILE_SCOPE,
|
|
229
|
+
GOOGLE_DRIVE_FULL_SCOPE,
|
|
230
|
+
GOOGLE_DRIVE_PUBLICATION_CREATE_ACTION,
|
|
231
|
+
GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
232
|
+
GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
233
|
+
GOOGLE_DRIVE_PROVIDER_DOMAIN,
|
|
234
|
+
GoogleDriveConnectionMetadata,
|
|
235
|
+
GoogleDrivePublicationReceipt,
|
|
236
|
+
GoogleDrivePublicationToolInput,
|
|
237
|
+
googleDriveScopesAllowCapability
|
|
238
|
+
} from "@opengeni/contracts/google-drive";
|
|
239
|
+
import {
|
|
240
|
+
beginConnectorActionExecution,
|
|
241
|
+
completeConnectorActionExecution,
|
|
242
|
+
getConnectionMetadata,
|
|
243
|
+
getWorkspaceGrant,
|
|
244
|
+
prepareConnectorActionApproval,
|
|
245
|
+
requireFile
|
|
246
|
+
} from "@opengeni/db";
|
|
247
|
+
import {
|
|
248
|
+
readAuthorizedEditableArtifactMaterialization
|
|
249
|
+
} from "@opengeni/db/editable-artifact-durable-export";
|
|
250
|
+
import { readResponseJsonBounded, undiciFetch } from "@opengeni/network";
|
|
251
|
+
var DRIVE_API = "https://www.googleapis.com/drive/v3";
|
|
252
|
+
var DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3";
|
|
253
|
+
var DRIVE_FOLDER_MIME = "application/vnd.google-apps.folder";
|
|
254
|
+
var RESPONSE_MAX_BYTES = 256 * 1024;
|
|
255
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
256
|
+
var FIRST_PARTY_MCP_SUBJECT_ID = "worker:first-party-mcp";
|
|
257
|
+
var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
|
|
258
|
+
var defaultPorts = {
|
|
259
|
+
getConnection: getConnectionMetadata,
|
|
260
|
+
getMembership: getWorkspaceGrant,
|
|
261
|
+
readMaterialization: readAuthorizedEditableArtifactMaterialization,
|
|
262
|
+
requireFile,
|
|
263
|
+
prepare: prepareConnectorActionApproval,
|
|
264
|
+
begin: beginConnectorActionExecution,
|
|
265
|
+
complete: completeConnectorActionExecution,
|
|
266
|
+
fetch: undiciFetch
|
|
267
|
+
};
|
|
268
|
+
async function resolveGoogleDrivePublicationTarget(db, workspaceId, delegations, ports = defaultPorts) {
|
|
269
|
+
const frozen = delegations.filter(
|
|
270
|
+
(delegation2) => delegation2.serverId === GOOGLE_DRIVE_PUBLICATION_SERVER_ID && delegation2.providerDomain.toLowerCase() === GOOGLE_DRIVE_PROVIDER_DOMAIN && delegation2.kind === "oauth2"
|
|
271
|
+
);
|
|
272
|
+
if (frozen.length !== 1) return null;
|
|
273
|
+
const delegation = frozen[0];
|
|
274
|
+
if (!await ports.getMembership(db, delegation.ownerSubjectId, workspaceId)) return null;
|
|
275
|
+
const connection = await ports.getConnection(
|
|
276
|
+
db,
|
|
277
|
+
workspaceId,
|
|
278
|
+
delegation.connectionId,
|
|
279
|
+
delegation.ownerSubjectId
|
|
280
|
+
);
|
|
281
|
+
if (!connection || connection.id !== delegation.connectionId || connection.subjectId !== delegation.ownerSubjectId || connection.providerDomain.toLowerCase() !== GOOGLE_DRIVE_PROVIDER_DOMAIN || connection.kind !== "oauth2" || connection.status !== "active" || !googleDriveScopesAllowCapability(connection.grantedScopes, "publish_file")) {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
const metadata = GoogleDriveConnectionMetadata.safeParse(connection.metadata);
|
|
285
|
+
if (!metadata.success || !metadata.data.outputDestination || metadata.data.lifecycle?.state === "paused" || metadata.data.lifecycle && metadata.data.lifecycle.state !== "active") {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
return Object.freeze({
|
|
289
|
+
ownerSubjectId: delegation.ownerSubjectId,
|
|
290
|
+
connectionId: connection.id,
|
|
291
|
+
destination: metadata.data.outputDestination,
|
|
292
|
+
credentialScope: connection.grantedScopes.includes(GOOGLE_DRIVE_FILE_SCOPE) ? GOOGLE_DRIVE_FILE_SCOPE : GOOGLE_DRIVE_FULL_SCOPE
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function googleDrivePublicationConnectorCall(target, rawRequest, approvalId) {
|
|
296
|
+
const request = GoogleDrivePublicationToolInput.parse(rawRequest);
|
|
297
|
+
return {
|
|
298
|
+
approvalId,
|
|
299
|
+
connectionId: target.connectionId,
|
|
300
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
301
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
302
|
+
arguments: {
|
|
303
|
+
action: GOOGLE_DRIVE_PUBLICATION_CREATE_ACTION,
|
|
304
|
+
destination: target.destination,
|
|
305
|
+
artifact: {
|
|
306
|
+
id: request.file.artifactId,
|
|
307
|
+
versionId: request.file.versionId,
|
|
308
|
+
title: request.title,
|
|
309
|
+
modality: request.modality,
|
|
310
|
+
sourceSha256: request.file.sha256
|
|
311
|
+
},
|
|
312
|
+
canonicalReceipt: { ...request.file },
|
|
313
|
+
idempotencyKeyDigest: createHash("sha256").update(request.idempotencyKey, "utf8").digest("hex")
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function createGoogleDrivePublicationAttemptTool(input) {
|
|
318
|
+
const ports = input.ports ?? defaultPorts;
|
|
319
|
+
return {
|
|
320
|
+
identity: {
|
|
321
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
322
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME
|
|
323
|
+
},
|
|
324
|
+
modelName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
325
|
+
codemodePath: ["google_drive", "publish_file"],
|
|
326
|
+
title: "Publish artifact to Google Drive",
|
|
327
|
+
description: "Publish a completed editable-artifact Office export into the configured Google Drive output folder. First call editable_artifact_export and poll editable_artifact_export_status until it returns a file receipt, then pass that exact receipt here.",
|
|
328
|
+
inputSchema: {
|
|
329
|
+
type: "object",
|
|
330
|
+
properties: {
|
|
331
|
+
title: { type: "string", minLength: 1, maxLength: 512 },
|
|
332
|
+
modality: {
|
|
333
|
+
type: "string",
|
|
334
|
+
enum: ["document", "spreadsheet", "presentation"]
|
|
335
|
+
},
|
|
336
|
+
file: {
|
|
337
|
+
type: "object",
|
|
338
|
+
properties: {
|
|
339
|
+
fileId: { type: "string", format: "uuid" },
|
|
340
|
+
filename: { type: "string", minLength: 1, maxLength: 512 },
|
|
341
|
+
contentType: { type: "string", minLength: 1, maxLength: 256 },
|
|
342
|
+
sizeBytes: { type: "integer", minimum: 1 },
|
|
343
|
+
sha256: { type: "string", pattern: "^[0-9a-f]{64}$" },
|
|
344
|
+
artifactId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
345
|
+
versionId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
346
|
+
materializationJobId: { type: "string", pattern: "^[0-9a-f]{32}$" },
|
|
347
|
+
sourceHeadSequence: { type: "integer", minimum: 0 },
|
|
348
|
+
sourceStateHash: {
|
|
349
|
+
type: "string",
|
|
350
|
+
pattern: "^sha256:[0-9a-f]{64}$"
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
required: [
|
|
354
|
+
"fileId",
|
|
355
|
+
"filename",
|
|
356
|
+
"contentType",
|
|
357
|
+
"sizeBytes",
|
|
358
|
+
"sha256",
|
|
359
|
+
"artifactId",
|
|
360
|
+
"versionId",
|
|
361
|
+
"materializationJobId",
|
|
362
|
+
"sourceHeadSequence",
|
|
363
|
+
"sourceStateHash"
|
|
364
|
+
],
|
|
365
|
+
additionalProperties: false
|
|
366
|
+
},
|
|
367
|
+
idempotencyKey: { type: "string", minLength: 1, maxLength: 200 }
|
|
368
|
+
},
|
|
369
|
+
required: ["title", "modality", "file", "idempotencyKey"],
|
|
370
|
+
additionalProperties: false
|
|
371
|
+
},
|
|
372
|
+
outputSchema: {
|
|
373
|
+
type: "object",
|
|
374
|
+
properties: {
|
|
375
|
+
connectionId: { type: "string", format: "uuid" },
|
|
376
|
+
providerFileId: { type: "string" },
|
|
377
|
+
webViewLink: { type: "string", format: "uri" },
|
|
378
|
+
mimeType: {
|
|
379
|
+
type: "string",
|
|
380
|
+
enum: [
|
|
381
|
+
"application/vnd.google-apps.document",
|
|
382
|
+
"application/vnd.google-apps.spreadsheet",
|
|
383
|
+
"application/vnd.google-apps.presentation"
|
|
384
|
+
]
|
|
385
|
+
},
|
|
386
|
+
destination: {
|
|
387
|
+
type: "object",
|
|
388
|
+
properties: {
|
|
389
|
+
folderId: { type: "string" },
|
|
390
|
+
folderName: { type: "string" },
|
|
391
|
+
driveId: { type: ["string", "null"] },
|
|
392
|
+
location: { type: "string", enum: ["my_drive", "shared_drive"] },
|
|
393
|
+
selectedAt: { type: "string" }
|
|
394
|
+
},
|
|
395
|
+
required: ["folderId", "folderName", "driveId", "location", "selectedAt"],
|
|
396
|
+
additionalProperties: false
|
|
397
|
+
},
|
|
398
|
+
replayed: { type: "boolean" }
|
|
399
|
+
},
|
|
400
|
+
required: [
|
|
401
|
+
"connectionId",
|
|
402
|
+
"providerFileId",
|
|
403
|
+
"webViewLink",
|
|
404
|
+
"mimeType",
|
|
405
|
+
"destination",
|
|
406
|
+
"replayed"
|
|
407
|
+
],
|
|
408
|
+
additionalProperties: false
|
|
409
|
+
},
|
|
410
|
+
annotations: {
|
|
411
|
+
title: "Publish artifact to Google Drive",
|
|
412
|
+
readOnlyHint: false,
|
|
413
|
+
destructiveHint: false,
|
|
414
|
+
idempotentHint: true,
|
|
415
|
+
openWorldHint: true
|
|
416
|
+
},
|
|
417
|
+
source: "mcp",
|
|
418
|
+
approval: "policy",
|
|
419
|
+
execute: async (raw, context) => {
|
|
420
|
+
const request = GoogleDrivePublicationToolInput.parse(raw);
|
|
421
|
+
const connectorCall = googleDrivePublicationConnectorCall(
|
|
422
|
+
input.target,
|
|
423
|
+
request,
|
|
424
|
+
context.operationId
|
|
425
|
+
);
|
|
426
|
+
let requestId = null;
|
|
427
|
+
if (context.caller.kind === "codemode") {
|
|
428
|
+
const admission = await ports.begin(input.db, input.identity, connectorCall);
|
|
429
|
+
if (!admission.allowed) {
|
|
430
|
+
throw new Error(`Google Drive publication was not executed: ${admission.reason}`);
|
|
431
|
+
}
|
|
432
|
+
if (!admission.managed) {
|
|
433
|
+
throw new Error("Google Drive publication has no explicit connector action policy");
|
|
434
|
+
}
|
|
435
|
+
requestId = admission.requestId;
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
const receipt = await executeGoogleDrivePublication(
|
|
439
|
+
{
|
|
440
|
+
db: input.db,
|
|
441
|
+
objectStorage: input.objectStorage,
|
|
442
|
+
identity: input.identity,
|
|
443
|
+
subjectId: input.subjectId,
|
|
444
|
+
target: input.target,
|
|
445
|
+
request,
|
|
446
|
+
resolveCredential: input.resolveCredential,
|
|
447
|
+
...context.signal ?? input.signal ? { signal: context.signal ?? input.signal } : {}
|
|
448
|
+
},
|
|
449
|
+
ports
|
|
450
|
+
);
|
|
451
|
+
if (requestId) {
|
|
452
|
+
await ports.complete(input.db, {
|
|
453
|
+
accountId: input.identity.accountId,
|
|
454
|
+
workspaceId: input.identity.workspaceId,
|
|
455
|
+
requestId,
|
|
456
|
+
attemptId: input.identity.attemptId,
|
|
457
|
+
outcome: "completed"
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
content: [{ type: "text", text: JSON.stringify(receipt) }],
|
|
462
|
+
structuredContent: receipt
|
|
463
|
+
};
|
|
464
|
+
} catch (error) {
|
|
465
|
+
if (requestId) {
|
|
466
|
+
await ports.complete(input.db, {
|
|
467
|
+
accountId: input.identity.accountId,
|
|
468
|
+
workspaceId: input.identity.workspaceId,
|
|
469
|
+
requestId,
|
|
470
|
+
attemptId: input.identity.attemptId,
|
|
471
|
+
outcome: "uncertain"
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
async function authorizeGoogleDrivePublicationAttempt(input) {
|
|
480
|
+
const request = GoogleDrivePublicationToolInput.parse(input.arguments);
|
|
481
|
+
const preparation = await (input.ports ?? defaultPorts).prepare(
|
|
482
|
+
input.db,
|
|
483
|
+
input.identity,
|
|
484
|
+
googleDrivePublicationConnectorCall(input.target, request, input.approvalId)
|
|
485
|
+
);
|
|
486
|
+
if (!preparation.managed) {
|
|
487
|
+
throw new Error("Google Drive publication has no explicit connector action policy");
|
|
488
|
+
}
|
|
489
|
+
if (preparation.decision === "ask") throw new AttemptToolApprovalRequiredError();
|
|
490
|
+
if (preparation.decision === "block") {
|
|
491
|
+
throw new Error("Google Drive publication is blocked by connector action policy");
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async function executeGoogleDrivePublication(input, ports = defaultPorts) {
|
|
495
|
+
const request = GoogleDrivePublicationToolInput.parse(input.request);
|
|
496
|
+
const connection = await ports.getConnection(
|
|
497
|
+
input.db,
|
|
498
|
+
input.identity.workspaceId,
|
|
499
|
+
input.target.connectionId,
|
|
500
|
+
input.subjectId
|
|
501
|
+
);
|
|
502
|
+
if (!connection || connection.subjectId !== input.subjectId) {
|
|
503
|
+
throw new Error("Google Drive publication connection is unavailable");
|
|
504
|
+
}
|
|
505
|
+
assertPublicationConnection(input, connection);
|
|
506
|
+
const materialization = await ports.readMaterialization(input.db, {
|
|
507
|
+
scope: {
|
|
508
|
+
accountId: input.identity.accountId,
|
|
509
|
+
workspaceId: input.identity.workspaceId
|
|
510
|
+
},
|
|
511
|
+
artifactId: request.file.artifactId,
|
|
512
|
+
jobId: request.file.materializationJobId,
|
|
513
|
+
actor: {
|
|
514
|
+
kind: "agent",
|
|
515
|
+
subjectId: FIRST_PARTY_MCP_SUBJECT_ID,
|
|
516
|
+
replicaId: publicationActorReplicaId(input.identity),
|
|
517
|
+
sessionId: input.identity.sessionId,
|
|
518
|
+
turnId: input.identity.turnId,
|
|
519
|
+
attemptId: input.identity.attemptId,
|
|
520
|
+
generation: input.identity.executionGeneration
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
assertCanonicalMaterialization(input.identity, request, materialization);
|
|
524
|
+
const expected = expectedExportWorkspaceFile(input.identity.workspaceId, request);
|
|
525
|
+
if (request.file.fileId !== expected.fileId) {
|
|
526
|
+
throw new Error("Google Drive publication requires a canonical editable-artifact export file");
|
|
527
|
+
}
|
|
528
|
+
const sourceFile = await ports.requireFile(
|
|
529
|
+
input.db,
|
|
530
|
+
input.identity.workspaceId,
|
|
531
|
+
request.file.fileId
|
|
532
|
+
);
|
|
533
|
+
if (sourceFile.status !== "ready" || sourceFile.id !== expected.fileId || sourceFile.safeFilename !== expected.safeFilename || sourceFile.objectKey !== expected.objectKey || sourceFile.filename !== request.file.filename || sourceFile.contentType !== request.file.contentType || sourceFile.sizeBytes !== request.file.sizeBytes || sourceFile.sha256 !== request.file.sha256) {
|
|
534
|
+
throw new Error("Editable artifact export file differs from its canonical receipt");
|
|
535
|
+
}
|
|
536
|
+
const bytes = await input.objectStorage.getFileBytes(sourceFile);
|
|
537
|
+
if (bytes.byteLength !== request.file.sizeBytes || createHash("sha256").update(bytes).digest("hex") !== request.file.sha256) {
|
|
538
|
+
throw new Error("Editable artifact export bytes failed verification");
|
|
539
|
+
}
|
|
540
|
+
const credential = await input.resolveCredential({
|
|
541
|
+
workspaceId: input.identity.workspaceId,
|
|
542
|
+
subjectId: input.subjectId,
|
|
543
|
+
serverId: GOOGLE_DRIVE_PUBLICATION_SERVER_ID,
|
|
544
|
+
toolName: GOOGLE_DRIVE_PUBLICATION_TOOL_NAME,
|
|
545
|
+
connectionRef: {
|
|
546
|
+
providerDomain: GOOGLE_DRIVE_PROVIDER_DOMAIN,
|
|
547
|
+
connectionId: input.target.connectionId,
|
|
548
|
+
kind: "oauth2",
|
|
549
|
+
subjectScope: "subject",
|
|
550
|
+
scopes: [input.target.credentialScope]
|
|
551
|
+
},
|
|
552
|
+
destinationUrl: `${DRIVE_UPLOAD_API}/files`,
|
|
553
|
+
forceRefresh: false
|
|
554
|
+
});
|
|
555
|
+
if (credential.status !== "ok" || credential.connectionId !== input.target.connectionId) {
|
|
556
|
+
throw new Error("Google Drive publication credential is unavailable");
|
|
557
|
+
}
|
|
558
|
+
const resolvedConnection = await ports.getConnection(
|
|
559
|
+
input.db,
|
|
560
|
+
input.identity.workspaceId,
|
|
561
|
+
input.target.connectionId,
|
|
562
|
+
input.subjectId
|
|
563
|
+
);
|
|
564
|
+
if (!resolvedConnection) {
|
|
565
|
+
throw new Error("Google Drive publication authority changed; retry on a new turn");
|
|
566
|
+
}
|
|
567
|
+
assertPublicationConnection(input, resolvedConnection);
|
|
568
|
+
return await publishToGoogleDrive({
|
|
569
|
+
workspaceId: input.identity.workspaceId,
|
|
570
|
+
connectionId: input.target.connectionId,
|
|
571
|
+
destination: input.target.destination,
|
|
572
|
+
request,
|
|
573
|
+
bytes,
|
|
574
|
+
headers: credential.headers,
|
|
575
|
+
fetchImpl: ports.fetch,
|
|
576
|
+
...input.signal ? { signal: input.signal } : {}
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
function assertPublicationConnection(input, connection) {
|
|
580
|
+
const metadata = GoogleDriveConnectionMetadata.parse(connection.metadata);
|
|
581
|
+
const credentialScope = connection.grantedScopes.includes(GOOGLE_DRIVE_FILE_SCOPE) ? GOOGLE_DRIVE_FILE_SCOPE : GOOGLE_DRIVE_FULL_SCOPE;
|
|
582
|
+
if (input.subjectId !== input.target.ownerSubjectId || connection.id !== input.target.connectionId || connection.subjectId !== input.target.ownerSubjectId || connection.providerDomain.toLowerCase() !== GOOGLE_DRIVE_PROVIDER_DOMAIN || connection.kind !== "oauth2" || connection.status !== "active" || metadata.lifecycle?.state === "paused" || metadata.lifecycle && metadata.lifecycle.state !== "active" || !googleDriveScopesAllowCapability(connection.grantedScopes, "publish_file") || credentialScope !== input.target.credentialScope || !metadata.outputDestination || !sameDestination(metadata.outputDestination, input.target.destination)) {
|
|
583
|
+
throw new Error("Google Drive publication authority changed; retry on a new turn");
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function publicationActorReplicaId(identity) {
|
|
587
|
+
const value = createHash("sha256").update(
|
|
588
|
+
`google-drive-publication:${identity.sessionId}:${identity.turnId}:${identity.attemptId}:${identity.executionGeneration}`,
|
|
589
|
+
"utf8"
|
|
590
|
+
).digest("hex").slice(0, 16);
|
|
591
|
+
return /^0+$/u.test(value) ? `1${value.slice(1)}` : value;
|
|
592
|
+
}
|
|
593
|
+
function assertCanonicalMaterialization(identity, request, job) {
|
|
594
|
+
const format = officeFormat(request.modality, request.file.contentType);
|
|
595
|
+
if (!job || job.scope.accountId !== identity.accountId || job.scope.workspaceId !== identity.workspaceId || job.artifactId !== request.file.artifactId || job.id !== request.file.materializationJobId || job.versionId !== request.file.versionId || job.targetHeadSequence !== request.file.sourceHeadSequence || job.stateHash !== request.file.sourceStateHash || job.format !== format || job.state !== "succeeded" || !job.result || job.result.byteSize !== request.file.sizeBytes || job.result.mimeType !== request.file.contentType || job.result.contentHash !== `sha256:${request.file.sha256}`) {
|
|
596
|
+
throw new Error("Google Drive publication receipt differs from its canonical materialization");
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function expectedExportWorkspaceFile(workspaceId, request) {
|
|
600
|
+
const format = officeFormat(request.modality, request.file.contentType);
|
|
601
|
+
const fileId = deterministicUuid(
|
|
602
|
+
`editable-artifact-export:file:${workspaceId}:${request.file.artifactId}:${request.file.versionId}:${request.file.materializationJobId}`
|
|
603
|
+
);
|
|
604
|
+
const safeFilename = `artifact-${request.file.artifactId}-${request.file.materializationJobId}.${format}`;
|
|
605
|
+
return {
|
|
606
|
+
fileId,
|
|
607
|
+
safeFilename,
|
|
608
|
+
objectKey: `workspaces/${workspaceId}/files/${fileId}/artifact-exports/${safeFilename}`
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function officeFormat(modality, contentType) {
|
|
612
|
+
const expected = {
|
|
613
|
+
document: {
|
|
614
|
+
format: "docx",
|
|
615
|
+
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
616
|
+
},
|
|
617
|
+
spreadsheet: {
|
|
618
|
+
format: "xlsx",
|
|
619
|
+
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
620
|
+
},
|
|
621
|
+
presentation: {
|
|
622
|
+
format: "pptx",
|
|
623
|
+
contentType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
624
|
+
}
|
|
625
|
+
}[modality];
|
|
626
|
+
if (contentType !== expected.contentType) {
|
|
627
|
+
throw new Error("Google Drive native publication requires the matching Office export format");
|
|
628
|
+
}
|
|
629
|
+
return expected.format;
|
|
630
|
+
}
|
|
631
|
+
function deterministicUuid(seed) {
|
|
632
|
+
const bytes = createHash("sha256").update(seed, "utf8").digest().subarray(0, 16);
|
|
633
|
+
bytes[6] = (bytes[6] ?? 0) & 15 | 80;
|
|
634
|
+
bytes[8] = (bytes[8] ?? 0) & 63 | 128;
|
|
635
|
+
const hex = bytes.toString("hex");
|
|
636
|
+
const value = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
637
|
+
if (!UUID_PATTERN.test(value)) throw new Error("Generated workspace file identity is invalid");
|
|
638
|
+
return value;
|
|
639
|
+
}
|
|
640
|
+
async function publishToGoogleDrive(input) {
|
|
641
|
+
await verifyDestinationAtWriteTime(input);
|
|
642
|
+
const operationKey = publicationOperationKey(input);
|
|
643
|
+
const existing = await findExistingPublication(input, operationKey);
|
|
644
|
+
if (existing) return driveReceipt(input, existing, true);
|
|
645
|
+
const mimeType = googleNativeMime(input.request.modality);
|
|
646
|
+
const boundary = `opengeni_${operationKey.slice(0, 32)}`;
|
|
647
|
+
const metadata = JSON.stringify({
|
|
648
|
+
name: input.request.title,
|
|
649
|
+
mimeType,
|
|
650
|
+
parents: [input.destination.folderId],
|
|
651
|
+
appProperties: {
|
|
652
|
+
opengeniPublicationKey: operationKey,
|
|
653
|
+
opengeniArtifactId: input.request.file.artifactId,
|
|
654
|
+
opengeniArtifactVersionId: input.request.file.versionId,
|
|
655
|
+
opengeniSourceSha256: input.request.file.sha256
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
const prefix = new TextEncoder().encode(
|
|
659
|
+
`--${boundary}\r
|
|
660
|
+
Content-Type: application/json; charset=UTF-8\r
|
|
661
|
+
\r
|
|
662
|
+
${metadata}\r
|
|
663
|
+
--${boundary}\r
|
|
664
|
+
Content-Type: ${input.request.file.contentType}\r
|
|
665
|
+
\r
|
|
666
|
+
`
|
|
667
|
+
);
|
|
668
|
+
const suffix = new TextEncoder().encode(`\r
|
|
669
|
+
--${boundary}--\r
|
|
670
|
+
`);
|
|
671
|
+
const body = new Uint8Array(prefix.byteLength + input.bytes.byteLength + suffix.byteLength);
|
|
672
|
+
body.set(prefix, 0);
|
|
673
|
+
body.set(input.bytes, prefix.byteLength);
|
|
674
|
+
body.set(suffix, prefix.byteLength + input.bytes.byteLength);
|
|
675
|
+
const url = new URL(`${DRIVE_UPLOAD_API}/files`);
|
|
676
|
+
url.searchParams.set("uploadType", "multipart");
|
|
677
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
678
|
+
url.searchParams.set(
|
|
679
|
+
"fields",
|
|
680
|
+
"id,name,mimeType,parents,driveId,webViewLink,appProperties,trashed"
|
|
681
|
+
);
|
|
682
|
+
const created = parseDriveFile(
|
|
683
|
+
await requestJson(input.fetchImpl, url, {
|
|
684
|
+
method: "POST",
|
|
685
|
+
headers: {
|
|
686
|
+
...input.headers,
|
|
687
|
+
accept: "application/json",
|
|
688
|
+
"content-type": `multipart/related; boundary=${boundary}`,
|
|
689
|
+
"content-length": String(body.byteLength)
|
|
690
|
+
},
|
|
691
|
+
body,
|
|
692
|
+
...input.signal ? { signal: input.signal } : {}
|
|
693
|
+
})
|
|
694
|
+
);
|
|
695
|
+
assertPublicationMatch(input, created, operationKey);
|
|
696
|
+
return driveReceipt(input, created, false);
|
|
697
|
+
}
|
|
698
|
+
async function verifyDestinationAtWriteTime(input) {
|
|
699
|
+
const url = new URL(`${DRIVE_API}/files/${encodeURIComponent(input.destination.folderId)}`);
|
|
700
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
701
|
+
url.searchParams.set("fields", "id,name,mimeType,driveId,trashed,capabilities(canAddChildren)");
|
|
702
|
+
const folder = record(await requestJson(input.fetchImpl, url, { headers: input.headers }));
|
|
703
|
+
const capabilities = record(folder.capabilities);
|
|
704
|
+
if (folder.id !== input.destination.folderId || folder.name !== input.destination.folderName || folder.mimeType !== DRIVE_FOLDER_MIME || folder.trashed === true || capabilities.canAddChildren !== true || (folder.driveId ?? null) !== input.destination.driveId) {
|
|
705
|
+
throw new Error("Google Drive output destination changed or is no longer writable");
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
async function findExistingPublication(input, operationKey) {
|
|
709
|
+
const url = new URL(`${DRIVE_API}/files`);
|
|
710
|
+
url.searchParams.set(
|
|
711
|
+
"q",
|
|
712
|
+
`trashed = false and '${driveQuery(input.destination.folderId)}' in parents and appProperties has { key='opengeniPublicationKey' and value='${operationKey}' }`
|
|
713
|
+
);
|
|
714
|
+
url.searchParams.set("spaces", "drive");
|
|
715
|
+
url.searchParams.set("supportsAllDrives", "true");
|
|
716
|
+
url.searchParams.set("includeItemsFromAllDrives", "true");
|
|
717
|
+
if (input.destination.driveId) {
|
|
718
|
+
url.searchParams.set("corpora", "drive");
|
|
719
|
+
url.searchParams.set("driveId", input.destination.driveId);
|
|
720
|
+
} else {
|
|
721
|
+
url.searchParams.set("corpora", "user");
|
|
722
|
+
}
|
|
723
|
+
url.searchParams.set(
|
|
724
|
+
"fields",
|
|
725
|
+
"files(id,name,mimeType,parents,driveId,webViewLink,appProperties,trashed)"
|
|
726
|
+
);
|
|
727
|
+
const payload = record(await requestJson(input.fetchImpl, url, { headers: input.headers }));
|
|
728
|
+
const files = Array.isArray(payload.files) ? payload.files.map(parseDriveFile) : [];
|
|
729
|
+
if (files.length > 1) throw new Error("Google Drive publication idempotency marker is ambiguous");
|
|
730
|
+
const existing = files[0] ?? null;
|
|
731
|
+
if (existing) assertPublicationMatch(input, existing, operationKey);
|
|
732
|
+
return existing;
|
|
733
|
+
}
|
|
734
|
+
function parseDriveFile(value) {
|
|
735
|
+
const item = record(value);
|
|
736
|
+
const appProperties = record(item.appProperties);
|
|
737
|
+
if (typeof item.id !== "string" || typeof item.name !== "string" || typeof item.mimeType !== "string" || !Array.isArray(item.parents) || !item.parents.every((entry) => typeof entry === "string") || item.driveId !== void 0 && item.driveId !== null && typeof item.driveId !== "string" || typeof item.webViewLink !== "string" || typeof item.trashed !== "boolean") {
|
|
738
|
+
throw new Error("Google Drive returned an invalid publication receipt");
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
id: item.id,
|
|
742
|
+
name: item.name,
|
|
743
|
+
mimeType: item.mimeType,
|
|
744
|
+
parents: item.parents,
|
|
745
|
+
driveId: item.driveId ?? null,
|
|
746
|
+
webViewLink: item.webViewLink,
|
|
747
|
+
appProperties: Object.fromEntries(
|
|
748
|
+
Object.entries(appProperties).filter(
|
|
749
|
+
(entry) => typeof entry[1] === "string"
|
|
750
|
+
)
|
|
751
|
+
),
|
|
752
|
+
trashed: item.trashed
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function assertPublicationMatch(input, file, operationKey) {
|
|
756
|
+
if (file.trashed || file.name !== input.request.title || file.mimeType !== googleNativeMime(input.request.modality) || file.parents.length !== 1 || file.parents[0] !== input.destination.folderId || file.driveId !== input.destination.driveId || file.appProperties.opengeniPublicationKey !== operationKey || file.appProperties.opengeniArtifactId !== input.request.file.artifactId || file.appProperties.opengeniArtifactVersionId !== input.request.file.versionId || file.appProperties.opengeniSourceSha256 !== input.request.file.sha256) {
|
|
757
|
+
throw new Error("Google Drive publication idempotency key conflicts with another file");
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
function driveReceipt(input, file, replayed) {
|
|
761
|
+
return GoogleDrivePublicationReceipt.parse({
|
|
762
|
+
connectionId: input.connectionId,
|
|
763
|
+
providerFileId: file.id,
|
|
764
|
+
webViewLink: file.webViewLink,
|
|
765
|
+
mimeType: googleNativeMime(input.request.modality),
|
|
766
|
+
destination: input.destination,
|
|
767
|
+
replayed
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
function publicationOperationKey(input) {
|
|
771
|
+
return createHash("sha256").update(
|
|
772
|
+
JSON.stringify({
|
|
773
|
+
workspaceId: input.workspaceId,
|
|
774
|
+
connectionId: input.connectionId,
|
|
775
|
+
destination: input.destination,
|
|
776
|
+
artifactId: input.request.file.artifactId,
|
|
777
|
+
versionId: input.request.file.versionId,
|
|
778
|
+
materializationJobId: input.request.file.materializationJobId,
|
|
779
|
+
sourceSha256: input.request.file.sha256,
|
|
780
|
+
idempotencyKey: input.request.idempotencyKey
|
|
781
|
+
}),
|
|
782
|
+
"utf8"
|
|
783
|
+
).digest("hex");
|
|
784
|
+
}
|
|
785
|
+
function googleNativeMime(modality) {
|
|
786
|
+
switch (modality) {
|
|
787
|
+
case "document":
|
|
788
|
+
return "application/vnd.google-apps.document";
|
|
789
|
+
case "spreadsheet":
|
|
790
|
+
return "application/vnd.google-apps.spreadsheet";
|
|
791
|
+
case "presentation":
|
|
792
|
+
return "application/vnd.google-apps.presentation";
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function sameDestination(left, right) {
|
|
796
|
+
return left.folderId === right.folderId && left.folderName === right.folderName && left.driveId === right.driveId && left.location === right.location && left.selectedAt === right.selectedAt;
|
|
797
|
+
}
|
|
798
|
+
async function requestJson(fetchImpl, url, init) {
|
|
799
|
+
let response;
|
|
800
|
+
try {
|
|
801
|
+
response = await fetchImpl(url, {
|
|
802
|
+
...init,
|
|
803
|
+
redirect: "error",
|
|
804
|
+
signal: init.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
805
|
+
});
|
|
806
|
+
} catch {
|
|
807
|
+
throw new Error("Google Drive publication transport failed");
|
|
808
|
+
}
|
|
809
|
+
if (!response.ok) {
|
|
810
|
+
await response.body?.cancel().catch(() => void 0);
|
|
811
|
+
throw new Error(`Google Drive publication request failed with status ${response.status}`);
|
|
812
|
+
}
|
|
813
|
+
return await readResponseJsonBounded(response, RESPONSE_MAX_BYTES, "Google Drive publication");
|
|
814
|
+
}
|
|
815
|
+
function record(value) {
|
|
816
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
817
|
+
}
|
|
818
|
+
function driveQuery(value) {
|
|
819
|
+
return value.replace(/\\/gu, "\\\\").replace(/'/gu, "\\'");
|
|
820
|
+
}
|
|
821
|
+
|
|
218
822
|
// src/activities/mcp-credentials.ts
|
|
219
823
|
import {
|
|
220
824
|
buildConnectionTokenResolver,
|
|
@@ -634,7 +1238,7 @@ function safeErrorName(error) {
|
|
|
634
1238
|
// src/activities/codemode-dispatcher.ts
|
|
635
1239
|
import { randomUUID } from "crypto";
|
|
636
1240
|
import {
|
|
637
|
-
AttemptToolApprovalRequiredError,
|
|
1241
|
+
AttemptToolApprovalRequiredError as AttemptToolApprovalRequiredError2,
|
|
638
1242
|
AttemptToolCatalogStaleError,
|
|
639
1243
|
AttemptToolNotFoundError,
|
|
640
1244
|
codemodeDispatchSubject,
|
|
@@ -845,7 +1449,7 @@ var CodemodeAttemptDispatcher = class {
|
|
|
845
1449
|
);
|
|
846
1450
|
await this.settleWithOutput(operation, claimId, { state: "completed", result });
|
|
847
1451
|
} catch (error) {
|
|
848
|
-
const knownPreExecution = error instanceof
|
|
1452
|
+
const knownPreExecution = error instanceof AttemptToolApprovalRequiredError2 || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolNotFoundError;
|
|
849
1453
|
await this.settleWithOutput(operation, claimId, {
|
|
850
1454
|
state: knownPreExecution ? "failed" : "outcome_unknown",
|
|
851
1455
|
errorCode: errorCode(error, signal),
|
|
@@ -896,7 +1500,7 @@ function errorCode(error, signal) {
|
|
|
896
1500
|
return "tool_outcome_unknown";
|
|
897
1501
|
}
|
|
898
1502
|
function safeErrorMessage(error) {
|
|
899
|
-
if (error instanceof
|
|
1503
|
+
if (error instanceof AttemptToolApprovalRequiredError2 || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolNotFoundError) {
|
|
900
1504
|
return error.message;
|
|
901
1505
|
}
|
|
902
1506
|
return "Codemode call failed before tool execution";
|
|
@@ -1035,7 +1639,7 @@ import {
|
|
|
1035
1639
|
markImageGenerationOperationRetentionFailed,
|
|
1036
1640
|
prepareImageGenerationOperation
|
|
1037
1641
|
} from "@opengeni/db";
|
|
1038
|
-
import { createHash as
|
|
1642
|
+
import { createHash as createHash4 } from "crypto";
|
|
1039
1643
|
|
|
1040
1644
|
// src/activities/generated-images.ts
|
|
1041
1645
|
import {
|
|
@@ -1051,7 +1655,7 @@ import {
|
|
|
1051
1655
|
recordGeneratedImageArtifactError,
|
|
1052
1656
|
settleGeneratedImageArtifactReady
|
|
1053
1657
|
} from "@opengeni/db";
|
|
1054
|
-
import { createHash as
|
|
1658
|
+
import { createHash as createHash3 } from "crypto";
|
|
1055
1659
|
|
|
1056
1660
|
// src/activities/retained-screenshots.ts
|
|
1057
1661
|
import {
|
|
@@ -1070,7 +1674,7 @@ import {
|
|
|
1070
1674
|
recordRetainedScreenshotArtifactError,
|
|
1071
1675
|
settleRetainedScreenshotArtifactReady
|
|
1072
1676
|
} from "@opengeni/db";
|
|
1073
|
-
import { createHash } from "crypto";
|
|
1677
|
+
import { createHash as createHash2 } from "crypto";
|
|
1074
1678
|
var PNG_SIGNATURE = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10);
|
|
1075
1679
|
var PNG_ALLOWED_CRITICAL_CHUNKS = /* @__PURE__ */ new Set(["IHDR", "PLTE", "IDAT", "IEND"]);
|
|
1076
1680
|
var PNG_MAX_CHUNKS = 16384;
|
|
@@ -1130,10 +1734,10 @@ function imageBytesFromSdkToolOutput(output) {
|
|
|
1130
1734
|
if (Array.isArray(output)) {
|
|
1131
1735
|
for (const entry of output) {
|
|
1132
1736
|
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
|
|
1133
|
-
const
|
|
1134
|
-
if (
|
|
1737
|
+
const record5 = entry;
|
|
1738
|
+
if (record5.type !== "input_image") continue;
|
|
1135
1739
|
const decoded = imageBytesFromSdkImageSource(
|
|
1136
|
-
|
|
1740
|
+
record5.image ?? record5.image_url ?? record5.imageUrl
|
|
1137
1741
|
);
|
|
1138
1742
|
if (decoded) return decoded;
|
|
1139
1743
|
}
|
|
@@ -1166,13 +1770,13 @@ function toolOutputContainsInlineImage(output) {
|
|
|
1166
1770
|
if (Array.isArray(output)) {
|
|
1167
1771
|
return output.some((entry) => {
|
|
1168
1772
|
if (!entry || typeof entry !== "object" || Array.isArray(entry)) return false;
|
|
1169
|
-
const
|
|
1170
|
-
return
|
|
1773
|
+
const record6 = entry;
|
|
1774
|
+
return record6.type === "input_image" && sdkImageSourceContainsInlineImage(record6.image ?? record6.image_url ?? record6.imageUrl);
|
|
1171
1775
|
});
|
|
1172
1776
|
}
|
|
1173
1777
|
if (!output || typeof output !== "object") return false;
|
|
1174
|
-
const
|
|
1175
|
-
return
|
|
1778
|
+
const record5 = output;
|
|
1779
|
+
return record5.type === "image" && sdkImageSourceContainsInlineImage(record5.image);
|
|
1176
1780
|
}
|
|
1177
1781
|
function decodeInlineImageDataUrl(value) {
|
|
1178
1782
|
if (typeof value !== "string") return null;
|
|
@@ -1339,7 +1943,7 @@ function validateComputerScreenshot(input, limits = {}) {
|
|
|
1339
1943
|
bytes: input.bytes,
|
|
1340
1944
|
mediaType: "image/png",
|
|
1341
1945
|
sizeBytes: input.bytes.byteLength,
|
|
1342
|
-
sha256:
|
|
1946
|
+
sha256: createHash2("sha256").update(input.bytes).digest("hex"),
|
|
1343
1947
|
width,
|
|
1344
1948
|
height
|
|
1345
1949
|
};
|
|
@@ -1395,11 +1999,11 @@ function validateRetainableSessionImage(input, limits = {}) {
|
|
|
1395
1999
|
bytes: input.bytes,
|
|
1396
2000
|
...identity,
|
|
1397
2001
|
sizeBytes: input.bytes.byteLength,
|
|
1398
|
-
sha256:
|
|
2002
|
+
sha256: createHash2("sha256").update(input.bytes).digest("hex")
|
|
1399
2003
|
};
|
|
1400
2004
|
}
|
|
1401
2005
|
function retainedScreenshotIdentity(input) {
|
|
1402
|
-
const settlementKey =
|
|
2006
|
+
const settlementKey = createHash2("sha256").update("opengeni:computer-screenshot:v1\0").update(input.sessionId).update("\0").update(input.turnId).update("\0").update(input.attemptId).update("\0").update(input.toolCallId).update("\0").update(input.toolOutputId).digest("hex");
|
|
1403
2007
|
const uuidBytes = Uint8Array.from(Buffer.from(settlementKey.slice(0, 32), "hex"));
|
|
1404
2008
|
uuidBytes[6] = uuidBytes[6] & 15 | 128;
|
|
1405
2009
|
uuidBytes[8] = uuidBytes[8] & 63 | 128;
|
|
@@ -1700,12 +2304,12 @@ function visitSerializedRunStateItemArrays(root, visitor) {
|
|
|
1700
2304
|
if (Array.isArray(root.generatedItems)) {
|
|
1701
2305
|
for (const wrapper of root.generatedItems) {
|
|
1702
2306
|
if (!wrapper || typeof wrapper !== "object" || Array.isArray(wrapper)) continue;
|
|
1703
|
-
const
|
|
1704
|
-
const rawItem =
|
|
2307
|
+
const record5 = wrapper;
|
|
2308
|
+
const rawItem = record5.rawItem;
|
|
1705
2309
|
if (!rawItem || typeof rawItem !== "object" || Array.isArray(rawItem)) continue;
|
|
1706
2310
|
const items = [rawItem];
|
|
1707
2311
|
visitor(items);
|
|
1708
|
-
if (items[0] !== rawItem)
|
|
2312
|
+
if (items[0] !== rawItem) record5.rawItem = items[0];
|
|
1709
2313
|
}
|
|
1710
2314
|
}
|
|
1711
2315
|
if (Array.isArray(root.modelResponses)) {
|
|
@@ -1729,12 +2333,12 @@ async function visitSerializedRunStateItemArraysAsync(root, visitor) {
|
|
|
1729
2333
|
if (Array.isArray(root.generatedItems)) {
|
|
1730
2334
|
for (const wrapper of root.generatedItems) {
|
|
1731
2335
|
if (!wrapper || typeof wrapper !== "object" || Array.isArray(wrapper)) continue;
|
|
1732
|
-
const
|
|
1733
|
-
const rawItem =
|
|
2336
|
+
const record5 = wrapper;
|
|
2337
|
+
const rawItem = record5.rawItem;
|
|
1734
2338
|
if (!rawItem || typeof rawItem !== "object" || Array.isArray(rawItem)) continue;
|
|
1735
2339
|
const items = [rawItem];
|
|
1736
2340
|
await visitor(items);
|
|
1737
|
-
if (items[0] !== rawItem)
|
|
2341
|
+
if (items[0] !== rawItem) record5.rawItem = items[0];
|
|
1738
2342
|
}
|
|
1739
2343
|
}
|
|
1740
2344
|
if (Array.isArray(root.modelResponses)) {
|
|
@@ -1832,8 +2436,8 @@ function retainedReceipt(value) {
|
|
|
1832
2436
|
}
|
|
1833
2437
|
function isInlineImageContent(entry) {
|
|
1834
2438
|
if (!entry || typeof entry !== "object" || Array.isArray(entry)) return false;
|
|
1835
|
-
const
|
|
1836
|
-
return
|
|
2439
|
+
const record5 = entry;
|
|
2440
|
+
return record5.type === "input_image" && typeof record5.image === "string" && record5.image.startsWith("data:image/");
|
|
1837
2441
|
}
|
|
1838
2442
|
function historyCallId(item) {
|
|
1839
2443
|
const value = item.callId ?? item.call_id;
|
|
@@ -2135,7 +2739,7 @@ function validateGeneratedImage(input) {
|
|
|
2135
2739
|
}
|
|
2136
2740
|
}
|
|
2137
2741
|
function generatedImageIdentity(input) {
|
|
2138
|
-
const hash =
|
|
2742
|
+
const hash = createHash3("sha256").update("opengeni:generated-image:v2\0").update(input.workspaceId).update("\0").update(input.sourceStrategy).update("\0").update(input.providerId).update("\0").update(input.providerBindingHash).update("\0");
|
|
2139
2743
|
if (input.sourceStrategy === "native_hosted") {
|
|
2140
2744
|
hash.update(input.providerItemId);
|
|
2141
2745
|
} else {
|
|
@@ -2796,7 +3400,7 @@ async function recoverCompletedImage(input, identity) {
|
|
|
2796
3400
|
return retainedGeneratedImageFromArtifact(recovered).receipt;
|
|
2797
3401
|
}
|
|
2798
3402
|
function digest(prefix, ...values) {
|
|
2799
|
-
const hash =
|
|
3403
|
+
const hash = createHash4("sha256").update(prefix);
|
|
2800
3404
|
for (const value of values) hash.update(value).update("\0");
|
|
2801
3405
|
return hash.digest("hex");
|
|
2802
3406
|
}
|
|
@@ -2897,15 +3501,15 @@ import {
|
|
|
2897
3501
|
FileResourceRef,
|
|
2898
3502
|
resourceMountPath
|
|
2899
3503
|
} from "@opengeni/contracts";
|
|
2900
|
-
import { createHash as
|
|
3504
|
+
import { createHash as createHash5 } from "crypto";
|
|
2901
3505
|
import {
|
|
2902
3506
|
getActiveSessionHistoryItemsPaged,
|
|
2903
|
-
|
|
3507
|
+
getFilesForSubject,
|
|
2904
3508
|
getLatestRunState,
|
|
2905
3509
|
getHumanInputResumeForEvent,
|
|
2906
3510
|
getSandboxSessionEnvelope,
|
|
2907
3511
|
listSessionSystemUpdatesForTurn,
|
|
2908
|
-
|
|
3512
|
+
requireFileForSubject
|
|
2909
3513
|
} from "@opengeni/db";
|
|
2910
3514
|
import {
|
|
2911
3515
|
projectHistoryForProvider,
|
|
@@ -2938,6 +3542,25 @@ function resumeRunState(state) {
|
|
|
2938
3542
|
}
|
|
2939
3543
|
return projectRejectedProviderArtifactsFromSerializedRunState(state.serializedRunState);
|
|
2940
3544
|
}
|
|
3545
|
+
async function measureHistoryPreparationPhase(options, phase, operation) {
|
|
3546
|
+
const startedAt = performance.now();
|
|
3547
|
+
let outcome = "completed";
|
|
3548
|
+
try {
|
|
3549
|
+
return await operation();
|
|
3550
|
+
} catch (error) {
|
|
3551
|
+
outcome = "failed";
|
|
3552
|
+
throw error;
|
|
3553
|
+
} finally {
|
|
3554
|
+
try {
|
|
3555
|
+
options.onPreparationPhase?.({
|
|
3556
|
+
phase,
|
|
3557
|
+
outcome,
|
|
3558
|
+
durationSeconds: (performance.now() - startedAt) / 1e3
|
|
3559
|
+
});
|
|
3560
|
+
} catch {
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
2941
3564
|
var MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
|
|
2942
3565
|
var MODEL_IMAGE_CONTENT_TYPES = /* @__PURE__ */ new Set(["image/gif", "image/jpeg", "image/png", "image/webp"]);
|
|
2943
3566
|
var MODEL_FILE_CONTENT_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -2995,7 +3618,7 @@ async function modelAttachmentContentForFiles(files, readFileBytes) {
|
|
|
2995
3618
|
});
|
|
2996
3619
|
continue;
|
|
2997
3620
|
}
|
|
2998
|
-
if (
|
|
3621
|
+
if (createHash5("sha256").update(bytes).digest("hex") !== checksum) {
|
|
2999
3622
|
console.error("model attachment checksum did not match finalized metadata", {
|
|
3000
3623
|
fileId: file.id
|
|
3001
3624
|
});
|
|
@@ -3040,12 +3663,15 @@ function attachmentRefsFromItem(item) {
|
|
|
3040
3663
|
return refs;
|
|
3041
3664
|
}
|
|
3042
3665
|
function attachmentUnavailableText(ref, file) {
|
|
3666
|
+
if (!file) {
|
|
3667
|
+
return `[Attachment unavailable or no longer authorized: ${ref.fileId}.]`;
|
|
3668
|
+
}
|
|
3043
3669
|
const filename = file?.safeFilename ?? ref.fileId;
|
|
3044
3670
|
const mediaType = file?.contentType ?? "unknown type";
|
|
3045
3671
|
const path = file ? sandboxFilePath(ref, file) : `/workspace/${resourceMountPath(ref)}`;
|
|
3046
3672
|
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}.]`;
|
|
3047
3673
|
}
|
|
3048
|
-
function createModelHistoryAttachmentProjector(db,
|
|
3674
|
+
function createModelHistoryAttachmentProjector(db, authority, policy, readFileBytes) {
|
|
3049
3675
|
const fileById = /* @__PURE__ */ new Map();
|
|
3050
3676
|
const missingFileIds = /* @__PURE__ */ new Set();
|
|
3051
3677
|
const contentById = /* @__PURE__ */ new Map();
|
|
@@ -3067,7 +3693,10 @@ function createModelHistoryAttachmentProjector(db, workspaceId, policy, readFile
|
|
|
3067
3693
|
if (refsByIndex.size === 0) return items;
|
|
3068
3694
|
const unknownIds = orderedFileIds.filter((id) => !fileById.has(id) && !missingFileIds.has(id));
|
|
3069
3695
|
if (unknownIds.length > 0) {
|
|
3070
|
-
const files = await
|
|
3696
|
+
const files = await getFilesForSubject(db, {
|
|
3697
|
+
...authority,
|
|
3698
|
+
fileIds: unknownIds
|
|
3699
|
+
});
|
|
3071
3700
|
for (const file of files) fileById.set(file.id, file);
|
|
3072
3701
|
for (const id of unknownIds) {
|
|
3073
3702
|
if (!fileById.has(id)) missingFileIds.add(id);
|
|
@@ -3132,11 +3761,15 @@ async function turnInput(db, runtime, agent, trigger, options) {
|
|
|
3132
3761
|
if (!trigger) {
|
|
3133
3762
|
throw new Error("Missing trigger event");
|
|
3134
3763
|
}
|
|
3135
|
-
const updates = await
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3764
|
+
const updates = await measureHistoryPreparationPhase(
|
|
3765
|
+
options,
|
|
3766
|
+
"system_update_load",
|
|
3767
|
+
async () => await listSessionSystemUpdatesForTurn(
|
|
3768
|
+
db,
|
|
3769
|
+
trigger.workspaceId,
|
|
3770
|
+
trigger.sessionId,
|
|
3771
|
+
options.turnId
|
|
3772
|
+
)
|
|
3140
3773
|
);
|
|
3141
3774
|
if (updates.length > 0) {
|
|
3142
3775
|
const historyItemIds = new Set(
|
|
@@ -3152,7 +3785,8 @@ async function turnInput(db, runtime, agent, trigger, options) {
|
|
|
3152
3785
|
"Continue the same inference from durable conversation and sandbox state. A previous execution stopped before it could finish. Do not repeat completed side effects; inspect actual state when uncertain."
|
|
3153
3786
|
].join("\n") : void 0,
|
|
3154
3787
|
options.unavailableSandboxFilesNote,
|
|
3155
|
-
options.runCredentialsNote
|
|
3788
|
+
options.runCredentialsNote,
|
|
3789
|
+
options.mcpAvailabilityNote
|
|
3156
3790
|
);
|
|
3157
3791
|
if (trigger.type === "user.message") {
|
|
3158
3792
|
const payload = trigger.payload;
|
|
@@ -3161,10 +3795,16 @@ async function turnInput(db, runtime, agent, trigger, options) {
|
|
|
3161
3795
|
throw new Error("user.message payload is missing text and annotations");
|
|
3162
3796
|
}
|
|
3163
3797
|
const resources = Array.isArray(payload.resources) ? payload.resources : [];
|
|
3164
|
-
const fileAttachments = await
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3798
|
+
const fileAttachments = await measureHistoryPreparationPhase(
|
|
3799
|
+
options,
|
|
3800
|
+
"current_attachment_resolution",
|
|
3801
|
+
async () => await resolveUserMessageFileAttachments(
|
|
3802
|
+
db,
|
|
3803
|
+
options.fileAuthority.accountId,
|
|
3804
|
+
trigger.workspaceId,
|
|
3805
|
+
options.fileAuthority.subjectId,
|
|
3806
|
+
resources
|
|
3807
|
+
)
|
|
3168
3808
|
);
|
|
3169
3809
|
const attachmentContext = userMessageAttachmentsContext(fileAttachments);
|
|
3170
3810
|
return await messageInput(
|
|
@@ -3179,7 +3819,8 @@ async function turnInput(db, runtime, agent, trigger, options) {
|
|
|
3179
3819
|
options.projectCanonicalHistory,
|
|
3180
3820
|
options.materializeModelHistory,
|
|
3181
3821
|
options.projectModelHistory,
|
|
3182
|
-
options.loadActiveHistory
|
|
3822
|
+
options.loadActiveHistory,
|
|
3823
|
+
options
|
|
3183
3824
|
);
|
|
3184
3825
|
}
|
|
3185
3826
|
if (trigger.type === "system.update.delivered") {
|
|
@@ -3198,7 +3839,8 @@ async function turnInput(db, runtime, agent, trigger, options) {
|
|
|
3198
3839
|
options.projectCanonicalHistory,
|
|
3199
3840
|
options.materializeModelHistory,
|
|
3200
3841
|
options.projectModelHistory,
|
|
3201
|
-
options.loadActiveHistory
|
|
3842
|
+
options.loadActiveHistory,
|
|
3843
|
+
options
|
|
3202
3844
|
);
|
|
3203
3845
|
}
|
|
3204
3846
|
if (trigger.type === "user.approvalDecision") {
|
|
@@ -3258,40 +3900,88 @@ function joinInternalContext(...parts) {
|
|
|
3258
3900
|
const content = parts.map((part) => part?.trim()).filter((part) => Boolean(part));
|
|
3259
3901
|
return content.length > 0 ? content.join("\n\n") : void 0;
|
|
3260
3902
|
}
|
|
3261
|
-
async function messageInput(db, runtime, agent, trigger, text, internalContext, currentAttachmentRefs = [], providerApi = "responses", projectCanonicalHistory, materializeModelHistory, projectModelHistory, loadActiveHistory = getActiveSessionHistoryItemsPaged) {
|
|
3262
|
-
const stored = await
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3903
|
+
async function messageInput(db, runtime, agent, trigger, text, internalContext, currentAttachmentRefs = [], providerApi = "responses", projectCanonicalHistory, materializeModelHistory, projectModelHistory, loadActiveHistory = getActiveSessionHistoryItemsPaged, preparationOptions = {}) {
|
|
3904
|
+
const [stored, envelope] = await Promise.all([
|
|
3905
|
+
measureHistoryPreparationPhase(
|
|
3906
|
+
preparationOptions,
|
|
3907
|
+
"durable_history_load",
|
|
3908
|
+
async () => loadActiveHistory(db, trigger.workspaceId, trigger.sessionId)
|
|
3909
|
+
),
|
|
3910
|
+
measureHistoryPreparationPhase(
|
|
3911
|
+
preparationOptions,
|
|
3912
|
+
"sandbox_envelope_load",
|
|
3913
|
+
async () => getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId)
|
|
3914
|
+
)
|
|
3915
|
+
]);
|
|
3916
|
+
const canonicalView = await measureHistoryPreparationPhase(
|
|
3917
|
+
preparationOptions,
|
|
3918
|
+
"canonical_projection",
|
|
3919
|
+
async () => {
|
|
3920
|
+
const active = projectRejectedProviderArtifacts(stored);
|
|
3921
|
+
return projectCanonicalHistory ? await projectCanonicalHistory(active) : active;
|
|
3922
|
+
}
|
|
3923
|
+
);
|
|
3924
|
+
const providerView = await measureHistoryPreparationPhase(
|
|
3925
|
+
preparationOptions,
|
|
3926
|
+
"provider_projection",
|
|
3927
|
+
() => projectHistoryForProvider(canonicalView, providerApi)
|
|
3928
|
+
);
|
|
3929
|
+
const referencedHistory = await measureHistoryPreparationPhase(
|
|
3930
|
+
preparationOptions,
|
|
3931
|
+
"attachment_ref_projection",
|
|
3932
|
+
() => withCurrentUserAttachmentRefs(providerView, currentAttachmentRefs)
|
|
3933
|
+
);
|
|
3934
|
+
const materializedHistory = await measureHistoryPreparationPhase(
|
|
3935
|
+
preparationOptions,
|
|
3936
|
+
"screenshot_materialization",
|
|
3937
|
+
async () => materializeModelHistory ? await materializeModelHistory(referencedHistory) : referencedHistory
|
|
3938
|
+
);
|
|
3939
|
+
const historyItems = await measureHistoryPreparationPhase(
|
|
3940
|
+
preparationOptions,
|
|
3941
|
+
"model_attachment_projection",
|
|
3942
|
+
async () => projectModelHistory ? await projectModelHistory(materializedHistory) : materializedHistory
|
|
3943
|
+
);
|
|
3944
|
+
const prepared = await measureHistoryPreparationPhase(
|
|
3945
|
+
preparationOptions,
|
|
3946
|
+
"runtime_input_assembly",
|
|
3947
|
+
async () => await runtime.prepareInput(agent, {
|
|
3948
|
+
kind: "message",
|
|
3949
|
+
...text ? { text } : {},
|
|
3950
|
+
...internalContext ? { internalContext } : {},
|
|
3951
|
+
historyItems,
|
|
3952
|
+
sandboxEnvelope: envelope,
|
|
3953
|
+
...projectModelHistory ? { modelInputAlreadyProjected: true } : {}
|
|
3954
|
+
})
|
|
3955
|
+
);
|
|
3956
|
+
const providerArtifactCandidates = await measureHistoryPreparationPhase(
|
|
3957
|
+
preparationOptions,
|
|
3958
|
+
"artifact_candidate_scan",
|
|
3959
|
+
() => {
|
|
3960
|
+
const preparedItems = Array.isArray(prepared.input) ? new Set(prepared.input) : /* @__PURE__ */ new Set();
|
|
3961
|
+
return {
|
|
3962
|
+
knownHistoryItemIds: stored.map((row) => row.id),
|
|
3963
|
+
historyItemIds: stored.filter(
|
|
3964
|
+
(row) => row.providerArtifactInvalidatedAt === null && hasOpaqueProviderArtifact(row.item) && preparedItems.has(row.item)
|
|
3965
|
+
).map((row) => row.id)
|
|
3966
|
+
};
|
|
3967
|
+
}
|
|
3968
|
+
);
|
|
3279
3969
|
return {
|
|
3280
3970
|
input: prepared,
|
|
3281
3971
|
persistedHistoryCount: prepared.persistedHistoryCount,
|
|
3282
|
-
providerArtifactCandidates
|
|
3283
|
-
knownHistoryItemIds: stored.map((row) => row.id),
|
|
3284
|
-
historyItemIds: stored.filter(
|
|
3285
|
-
(row) => row.providerArtifactInvalidatedAt === null && hasOpaqueProviderArtifact(row.item) && preparedItems.has(row.item)
|
|
3286
|
-
).map((row) => row.id)
|
|
3287
|
-
}
|
|
3972
|
+
providerArtifactCandidates
|
|
3288
3973
|
};
|
|
3289
3974
|
}
|
|
3290
|
-
async function resolveUserMessageFileAttachments(db, workspaceId, resources) {
|
|
3975
|
+
async function resolveUserMessageFileAttachments(db, accountId, workspaceId, subjectId, resources) {
|
|
3291
3976
|
const attachments = [];
|
|
3292
3977
|
for (const resource of resources) {
|
|
3293
3978
|
if (resource.kind !== "file") continue;
|
|
3294
|
-
const file = await
|
|
3979
|
+
const file = await requireFileForSubject(db, {
|
|
3980
|
+
accountId,
|
|
3981
|
+
workspaceId,
|
|
3982
|
+
subjectId,
|
|
3983
|
+
fileId: resource.fileId
|
|
3984
|
+
});
|
|
3295
3985
|
attachments.push({ resource, file });
|
|
3296
3986
|
}
|
|
3297
3987
|
return attachments;
|
|
@@ -3604,21 +4294,21 @@ function isContextWindowExceeded(error, seen = /* @__PURE__ */ new WeakSet()) {
|
|
|
3604
4294
|
if (!error || typeof error !== "object") return false;
|
|
3605
4295
|
if (seen.has(error)) return false;
|
|
3606
4296
|
seen.add(error);
|
|
3607
|
-
const
|
|
3608
|
-
const code = typeof
|
|
3609
|
-
const message = typeof
|
|
4297
|
+
const record5 = error;
|
|
4298
|
+
const code = typeof record5.code === "string" ? record5.code.toLowerCase() : "";
|
|
4299
|
+
const message = typeof record5.message === "string" ? record5.message.toLowerCase() : error instanceof Error ? error.message.toLowerCase() : "";
|
|
3610
4300
|
const direct = code === "context_length_exceeded" || code === "context_window_exceeded" || message.includes("context window") || message.includes("maximum context length") || message.includes("too many tokens");
|
|
3611
|
-
return direct || isContextWindowExceeded(
|
|
4301
|
+
return direct || isContextWindowExceeded(record5.cause, seen) || isContextWindowExceeded(record5.error, seen) || isContextWindowExceeded(record5.diagnostics, seen);
|
|
3612
4302
|
}
|
|
3613
4303
|
function isExactContextLengthExceeded(error, seen = /* @__PURE__ */ new WeakSet()) {
|
|
3614
4304
|
if (!error || typeof error !== "object") return false;
|
|
3615
4305
|
if (seen.has(error)) return false;
|
|
3616
4306
|
seen.add(error);
|
|
3617
|
-
const
|
|
3618
|
-
if (
|
|
4307
|
+
const record5 = error;
|
|
4308
|
+
if (record5.code === "context_length_exceeded") {
|
|
3619
4309
|
return true;
|
|
3620
4310
|
}
|
|
3621
|
-
return isExactContextLengthExceeded(
|
|
4311
|
+
return isExactContextLengthExceeded(record5.cause, seen) || isExactContextLengthExceeded(record5.error, seen) || isExactContextLengthExceeded(record5.diagnostics, seen);
|
|
3622
4312
|
}
|
|
3623
4313
|
|
|
3624
4314
|
// src/activities/video-generation-admission.ts
|
|
@@ -3642,7 +4332,7 @@ import {
|
|
|
3642
4332
|
calculateVideoGenerationCreditCostMicros,
|
|
3643
4333
|
environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes3
|
|
3644
4334
|
} from "@opengeni/config";
|
|
3645
|
-
import { createHash as
|
|
4335
|
+
import { createHash as createHash7, randomUUID as randomUUID3 } from "crypto";
|
|
3646
4336
|
|
|
3647
4337
|
// src/activities/video-reference-staging.ts
|
|
3648
4338
|
import {
|
|
@@ -3650,7 +4340,7 @@ import {
|
|
|
3650
4340
|
} from "@opengeni/contracts";
|
|
3651
4341
|
|
|
3652
4342
|
// src/activities/video-media-validation.ts
|
|
3653
|
-
import { createHash as
|
|
4343
|
+
import { createHash as createHash6, randomUUID as randomUUID2 } from "crypto";
|
|
3654
4344
|
import { open, mkdir, rm, stat } from "fs/promises";
|
|
3655
4345
|
import { join } from "path";
|
|
3656
4346
|
import { spawn } from "child_process";
|
|
@@ -3678,7 +4368,7 @@ async function copyVersionedObjectToVerifiedTemp(input) {
|
|
|
3678
4368
|
const path = join(directory, "payload");
|
|
3679
4369
|
await mkdir(directory, { recursive: false, mode: 448 });
|
|
3680
4370
|
const handle = await open(path, "wx", 384);
|
|
3681
|
-
const hash =
|
|
4371
|
+
const hash = createHash6("sha256");
|
|
3682
4372
|
let offset = 0;
|
|
3683
4373
|
try {
|
|
3684
4374
|
while (offset < input.expectedSizeBytes) {
|
|
@@ -3766,9 +4456,9 @@ async function probeBrowserCompatibleMp4(path, ffprobePath) {
|
|
|
3766
4456
|
} catch {
|
|
3767
4457
|
throw new Error("Media probe returned malformed metadata");
|
|
3768
4458
|
}
|
|
3769
|
-
const root =
|
|
3770
|
-
const format =
|
|
3771
|
-
const streams = Array.isArray(root?.streams) ? root.streams.map(
|
|
4459
|
+
const root = record2(decoded);
|
|
4460
|
+
const format = record2(root?.format);
|
|
4461
|
+
const streams = Array.isArray(root?.streams) ? root.streams.map(record2).filter(Boolean) : [];
|
|
3772
4462
|
const video = streams.filter(
|
|
3773
4463
|
(stream) => stream?.codec_type === "video" && !isAttachedPicture(stream)
|
|
3774
4464
|
);
|
|
@@ -3791,7 +4481,7 @@ async function probeBrowserCompatibleMp4(path, ffprobePath) {
|
|
|
3791
4481
|
};
|
|
3792
4482
|
}
|
|
3793
4483
|
function isAttachedPicture(stream) {
|
|
3794
|
-
return Number(
|
|
4484
|
+
return Number(record2(stream.disposition)?.attached_pic) === 1;
|
|
3795
4485
|
}
|
|
3796
4486
|
async function spawnBounded(command, args) {
|
|
3797
4487
|
return await new Promise((resolve, reject) => {
|
|
@@ -3868,7 +4558,7 @@ function finitePositive(value) {
|
|
|
3868
4558
|
function stringValue(value) {
|
|
3869
4559
|
return typeof value === "string" && value.trim() ? value : null;
|
|
3870
4560
|
}
|
|
3871
|
-
function
|
|
4561
|
+
function record2(value) {
|
|
3872
4562
|
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
3873
4563
|
}
|
|
3874
4564
|
function assertSha256(value) {
|
|
@@ -4297,7 +4987,7 @@ async function admitVideoGenerationRequest(input) {
|
|
|
4297
4987
|
toolCallId: input.toolCallId,
|
|
4298
4988
|
admissionKey,
|
|
4299
4989
|
requestDigest,
|
|
4300
|
-
promptDigest:
|
|
4990
|
+
promptDigest: createHash7("sha256").update(canonical.prompt).digest("hex"),
|
|
4301
4991
|
requestEncrypted: encryptEnvironmentValue2(encryptionKey, JSON.stringify(canonical)),
|
|
4302
4992
|
modelId: canonical.modelId,
|
|
4303
4993
|
sourceMode: canonical.sourceMode,
|
|
@@ -5241,6 +5931,8 @@ async function bindRunCredentialResolver(input) {
|
|
|
5241
5931
|
}
|
|
5242
5932
|
|
|
5243
5933
|
// src/activities/types.ts
|
|
5934
|
+
var PRE_CLAIM_FAILURE_TYPE = "OpenGeniPreClaimFailure";
|
|
5935
|
+
var PRE_CLAIM_FAILURE_MESSAGE = "Agent turn admission failed before attempt claim.";
|
|
5244
5936
|
var ESCAPED_MCP_TIMEOUT_RECOVERY_FAILURE_TYPE = "EscapedMcpTimeoutRecoveryFailure";
|
|
5245
5937
|
var ESCAPED_MCP_TIMEOUT_RECOVERY_FAILURE_MESSAGE = "MCP request timeout recovery checkpoint failed before model request";
|
|
5246
5938
|
|
|
@@ -5643,7 +6335,7 @@ async function executeCodexImageGeneration(input) {
|
|
|
5643
6335
|
}
|
|
5644
6336
|
|
|
5645
6337
|
// src/activities/image-generation-references.ts
|
|
5646
|
-
import { getGeneratedImageArtifact as getGeneratedImageArtifact2,
|
|
6338
|
+
import { getGeneratedImageArtifact as getGeneratedImageArtifact2, requireFileForSubject as requireFileForSubject2 } from "@opengeni/db";
|
|
5647
6339
|
var IMAGE_GENERATION_REFERENCE_MAX_BYTES = 30 * 1024 * 1024;
|
|
5648
6340
|
var IMAGE_GENERATION_REFERENCES_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
|
|
5649
6341
|
async function resolveImageGenerationReferences(input) {
|
|
@@ -5678,7 +6370,12 @@ async function referenceBytes(input, reference2) {
|
|
|
5678
6370
|
bytes: await input.readSandboxFile(reference2.path, IMAGE_GENERATION_REFERENCE_MAX_BYTES + 1)
|
|
5679
6371
|
};
|
|
5680
6372
|
}
|
|
5681
|
-
const file = reference2.kind === "file" ? await
|
|
6373
|
+
const file = reference2.kind === "file" ? await requireFileForSubject2(input.db, {
|
|
6374
|
+
accountId: input.accountId,
|
|
6375
|
+
workspaceId: input.workspaceId,
|
|
6376
|
+
subjectId: input.subjectId,
|
|
6377
|
+
fileId: reference2.fileId
|
|
6378
|
+
}) : await artifactFile(input.db, input.workspaceId, reference2.artifactId);
|
|
5682
6379
|
if (file.status !== "ready" || !file.sha256) {
|
|
5683
6380
|
throw new Error("Image reference file is not durably ready");
|
|
5684
6381
|
}
|
|
@@ -5712,7 +6409,7 @@ function assertStoredFileMatches(file, image) {
|
|
|
5712
6409
|
}
|
|
5713
6410
|
|
|
5714
6411
|
// src/activities/workspace-capture.ts
|
|
5715
|
-
import { createHash as
|
|
6412
|
+
import { createHash as createHash8 } from "crypto";
|
|
5716
6413
|
import {
|
|
5717
6414
|
deleteWorkspaceCaptureRows,
|
|
5718
6415
|
insertFailedWorkspaceCapture,
|
|
@@ -6466,7 +7163,7 @@ function joinRepoPath(repoRoot, repoRelPath) {
|
|
|
6466
7163
|
return `${repoRoot.replace(/\/+$/, "")}/${repoRelPath}`;
|
|
6467
7164
|
}
|
|
6468
7165
|
function sha256(bytes) {
|
|
6469
|
-
return
|
|
7166
|
+
return createHash8("sha256").update(bytes).digest("hex");
|
|
6470
7167
|
}
|
|
6471
7168
|
function utf8(s) {
|
|
6472
7169
|
return new TextEncoder().encode(s);
|
|
@@ -6551,7 +7248,7 @@ import {
|
|
|
6551
7248
|
resourceMountPath as resourceMountPath2,
|
|
6552
7249
|
VideoGenerationRejectedResult
|
|
6553
7250
|
} from "@opengeni/contracts";
|
|
6554
|
-
import { createHash as
|
|
7251
|
+
import { createHash as createHash9, randomUUID as randomUUID4 } from "crypto";
|
|
6555
7252
|
|
|
6556
7253
|
// src/model-checkpoint-memory-collector.ts
|
|
6557
7254
|
var MODEL_CHECKPOINT_GC_PRESSURE_BYTES = 512 * 1024 * 1024;
|
|
@@ -6736,7 +7433,7 @@ function providerRetryAfterMs(error, nowMs = Date.now()) {
|
|
|
6736
7433
|
}
|
|
6737
7434
|
return null;
|
|
6738
7435
|
}
|
|
6739
|
-
function
|
|
7436
|
+
function unavailableMcpOperationalContext(input) {
|
|
6740
7437
|
if (input.droppedCount <= 0) {
|
|
6741
7438
|
return void 0;
|
|
6742
7439
|
}
|
|
@@ -6756,17 +7453,38 @@ function escapedMcpTimeoutRecoveryFailure(input) {
|
|
|
6756
7453
|
details: [input.detail]
|
|
6757
7454
|
});
|
|
6758
7455
|
}
|
|
7456
|
+
function preClaimAdmissionFailure(error) {
|
|
7457
|
+
const persistenceFailure = isSessionEventPersistenceError(error) ? error : null;
|
|
7458
|
+
const sqlState = persistenceFailure?.details.sqlState ?? null;
|
|
7459
|
+
const rawTransportFailure = sqlState === null && isRetryableDatabaseTransportFailure(error);
|
|
7460
|
+
const retryable = Boolean(
|
|
7461
|
+
rawTransportFailure || persistenceFailure && (sqlState === null || sqlState.startsWith("08") || sqlState.startsWith("40") || sqlState.startsWith("53") || sqlState === "55P03" || sqlState === "57014" || sqlState === "57P01" || sqlState === "57P02" || sqlState === "57P03" || sqlState.startsWith("58"))
|
|
7462
|
+
);
|
|
7463
|
+
const detail = {
|
|
7464
|
+
disposition: retryable ? "retryable" : "permanent",
|
|
7465
|
+
code: persistenceFailure?.details.code ?? (rawTransportFailure ? "db_failure" : "claim_invariant")
|
|
7466
|
+
};
|
|
7467
|
+
return ApplicationFailure.create({
|
|
7468
|
+
message: PRE_CLAIM_FAILURE_MESSAGE,
|
|
7469
|
+
type: PRE_CLAIM_FAILURE_TYPE,
|
|
7470
|
+
nonRetryable: true,
|
|
7471
|
+
details: [detail]
|
|
7472
|
+
});
|
|
7473
|
+
}
|
|
6759
7474
|
function filterUnmaterializedSandboxFileDownloads(downloads, materializedFileIds) {
|
|
6760
7475
|
if (downloads.length === 0 || materializedFileIds.size === 0) {
|
|
6761
7476
|
return downloads;
|
|
6762
7477
|
}
|
|
6763
7478
|
return downloads.filter((download) => !materializedFileIds.has(download.fileId));
|
|
6764
7479
|
}
|
|
7480
|
+
function sandboxFileMaterializationOutcome(failures) {
|
|
7481
|
+
return failures.length === 0 ? "completed" : "failed";
|
|
7482
|
+
}
|
|
6765
7483
|
function codexWorkspaceMetricKey(workspaceId) {
|
|
6766
|
-
return
|
|
7484
|
+
return createHash9("sha256").update(workspaceId).digest("hex").slice(0, 12);
|
|
6767
7485
|
}
|
|
6768
7486
|
function stableHumanInputRequestId(sessionId, turnId, toolCallId) {
|
|
6769
|
-
const hex =
|
|
7487
|
+
const hex = createHash9("sha256").update("opengeni-human-input-v1\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
|
|
6770
7488
|
hex[12] = "5";
|
|
6771
7489
|
hex[16] = ["8", "9", "a", "b"][Number.parseInt(hex[16] ?? "0", 16) % 4] ?? "8";
|
|
6772
7490
|
const value = hex.join("");
|
|
@@ -6789,7 +7507,7 @@ function stableInteractionInterventionOperationId(sessionId, turnId, toolCallId)
|
|
|
6789
7507
|
);
|
|
6790
7508
|
}
|
|
6791
7509
|
function stableInteractionInterventionUuid(namespace, sessionId, turnId, toolCallId) {
|
|
6792
|
-
const hex =
|
|
7510
|
+
const hex = createHash9("sha256").update(namespace).update("\0").update(sessionId).update("\0").update(turnId).update("\0").update(toolCallId).digest("hex").slice(0, 32).split("");
|
|
6793
7511
|
hex[12] = "5";
|
|
6794
7512
|
hex[16] = ["8", "9", "a", "b"][Number.parseInt(hex[16] ?? "0", 16) % 4] ?? "8";
|
|
6795
7513
|
const value = hex.join("");
|
|
@@ -7046,15 +7764,15 @@ function collectErrorStrings(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
|
7046
7764
|
}
|
|
7047
7765
|
seen.add(value);
|
|
7048
7766
|
const out = [];
|
|
7049
|
-
const
|
|
7767
|
+
const record5 = value;
|
|
7050
7768
|
for (const key of ["message", "code", "type", "name", "param"]) {
|
|
7051
|
-
const field =
|
|
7769
|
+
const field = record5[key];
|
|
7052
7770
|
if (typeof field === "string" && field.length > 0) {
|
|
7053
7771
|
out.push(field);
|
|
7054
7772
|
}
|
|
7055
7773
|
}
|
|
7056
7774
|
for (const key of ["error", "cause", "response", "data"]) {
|
|
7057
|
-
out.push(...collectErrorStrings(
|
|
7775
|
+
out.push(...collectErrorStrings(record5[key], seen));
|
|
7058
7776
|
}
|
|
7059
7777
|
return out;
|
|
7060
7778
|
}
|
|
@@ -7432,18 +8150,33 @@ function managedSandboxOwnershipForTurn(machinePrimary, attemptId, sandboxGroupI
|
|
|
7432
8150
|
function shouldEstablishSandboxForTurn(sandboxOwnershipEnabled, homeBackend, machinePrimary) {
|
|
7433
8151
|
return sandboxOwnershipEnabled && (homeBackend !== "none" || machinePrimary);
|
|
7434
8152
|
}
|
|
7435
|
-
function
|
|
7436
|
-
if (!
|
|
8153
|
+
function sandboxEstablishPolicyDecision(input) {
|
|
8154
|
+
if (!input.lazyEnabled) return { policy: "eager", reason: "lazy_disabled" };
|
|
8155
|
+
if (input.machinePrimary) return { policy: "eager", reason: "machine_primary" };
|
|
8156
|
+
if (input.sandboxBackend === "none") return { policy: "eager", reason: "backend_none" };
|
|
8157
|
+
if (input.hasInitialRunCredentialMaterial) {
|
|
8158
|
+
return { policy: "eager", reason: "initial_run_credentials" };
|
|
8159
|
+
}
|
|
8160
|
+
if (input.generatedVideoFileCount > 0) {
|
|
8161
|
+
return { policy: "eager", reason: "generated_video_files" };
|
|
8162
|
+
}
|
|
8163
|
+
if (input.hasSignedFileResources) {
|
|
8164
|
+
return { policy: "eager", reason: "signed_file_resources" };
|
|
8165
|
+
}
|
|
8166
|
+
return { policy: "on-demand", reason: "eligible" };
|
|
8167
|
+
}
|
|
8168
|
+
function pointerReconcileReason(record5) {
|
|
8169
|
+
if (!record5) {
|
|
7437
8170
|
return "stale_pointer";
|
|
7438
8171
|
}
|
|
7439
8172
|
const establishable = swapTargetEstablishability({
|
|
7440
|
-
kind:
|
|
8173
|
+
kind: record5.kind,
|
|
7441
8174
|
isSessionGroup: false
|
|
7442
8175
|
});
|
|
7443
8176
|
if (!establishable.ok) {
|
|
7444
8177
|
return establishable.code;
|
|
7445
8178
|
}
|
|
7446
|
-
if (
|
|
8179
|
+
if (record5.kind === "selfhosted" && !record5.enrollmentId) {
|
|
7447
8180
|
return "offline_enrollment";
|
|
7448
8181
|
}
|
|
7449
8182
|
return null;
|
|
@@ -7452,15 +8185,15 @@ async function reconcileActiveSandboxPointer(db, ids, pointer, loadRecord, publi
|
|
|
7452
8185
|
if (!pointer?.activeSandboxId) {
|
|
7453
8186
|
return { pointer, record: null };
|
|
7454
8187
|
}
|
|
7455
|
-
let
|
|
8188
|
+
let record5;
|
|
7456
8189
|
try {
|
|
7457
|
-
|
|
8190
|
+
record5 = await loadRecord(pointer.activeSandboxId);
|
|
7458
8191
|
} catch {
|
|
7459
8192
|
return { pointer, record: null };
|
|
7460
8193
|
}
|
|
7461
|
-
const reason = pointerReconcileReason(
|
|
8194
|
+
const reason = pointerReconcileReason(record5);
|
|
7462
8195
|
if (!reason) {
|
|
7463
|
-
return { pointer, record:
|
|
8196
|
+
return { pointer, record: record5 };
|
|
7464
8197
|
}
|
|
7465
8198
|
const fromEpoch = pointer.activeEpoch;
|
|
7466
8199
|
const reset = await setActiveSandbox(db, {
|
|
@@ -8434,7 +9167,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
8434
9167
|
if (!objectStorage) {
|
|
8435
9168
|
throw new Error("Generated image sandbox materialization requires object storage");
|
|
8436
9169
|
}
|
|
8437
|
-
const file = await
|
|
9170
|
+
const file = await requireFile2(db, input.workspaceId, receipt.artifact.artifactId);
|
|
8438
9171
|
const downloadStorage = objectStorageForSandboxDownloads(modelRunSettings, objectStorage);
|
|
8439
9172
|
const signed = await downloadStorage.createGetUrl({
|
|
8440
9173
|
key: file.objectKey
|
|
@@ -8760,7 +9493,6 @@ function createRunAgentTurnActivity(services) {
|
|
|
8760
9493
|
let lastCodexRequestOpaqueArtifacts = [];
|
|
8761
9494
|
let triggerType = null;
|
|
8762
9495
|
try {
|
|
8763
|
-
const session = await requireSession(db, input.workspaceId, input.sessionId);
|
|
8764
9496
|
const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
|
|
8765
9497
|
sessionId: input.sessionId,
|
|
8766
9498
|
workflowId: input.workflowId,
|
|
@@ -8775,10 +9507,12 @@ function createRunAgentTurnActivity(services) {
|
|
|
8775
9507
|
}
|
|
8776
9508
|
const turn = claim.turn;
|
|
8777
9509
|
turnId = turn.id;
|
|
9510
|
+
const session = await requireSession(db, input.workspaceId, input.sessionId);
|
|
8778
9511
|
executionGeneration = turn.executionGeneration;
|
|
8779
9512
|
providerRecoveryCount = providerRecoveryCountFromMetadata(turn.metadata);
|
|
8780
9513
|
let installedApiIntegrations = [];
|
|
8781
9514
|
const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
|
|
9515
|
+
const fileAuthoritySubjectId = turn.initiatingHumanSubjectId ?? null;
|
|
8782
9516
|
const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
|
|
8783
9517
|
db,
|
|
8784
9518
|
input.workspaceId,
|
|
@@ -9031,6 +9765,14 @@ function createRunAgentTurnActivity(services) {
|
|
|
9031
9765
|
});
|
|
9032
9766
|
throwIfWorkerShuttingDown();
|
|
9033
9767
|
throwIfTurnCancelled();
|
|
9768
|
+
recordTurnStartupPhase(observability, {
|
|
9769
|
+
phase: "claim_and_policy",
|
|
9770
|
+
provider: turnExecutionPolicy.providerId,
|
|
9771
|
+
backend: turn.sandboxBackend,
|
|
9772
|
+
outcome: "completed",
|
|
9773
|
+
durationSeconds: (performance.now() - activityStarted) / 1e3
|
|
9774
|
+
});
|
|
9775
|
+
const turnStartSettlementStartedAt = performance.now();
|
|
9034
9776
|
if (!await settle({
|
|
9035
9777
|
events: [
|
|
9036
9778
|
{ type: "session.status.changed", payload: { status: "running" } },
|
|
@@ -9045,76 +9787,34 @@ function createRunAgentTurnActivity(services) {
|
|
|
9045
9787
|
})) {
|
|
9046
9788
|
return claimedResult({ status: "cancelled" });
|
|
9047
9789
|
}
|
|
9790
|
+
recordTurnStartupPhase(observability, {
|
|
9791
|
+
phase: "turn_start_settlement",
|
|
9792
|
+
provider: turnExecutionPolicy.providerId,
|
|
9793
|
+
backend: turn.sandboxBackend,
|
|
9794
|
+
outcome: "completed",
|
|
9795
|
+
durationSeconds: (performance.now() - turnStartSettlementStartedAt) / 1e3,
|
|
9796
|
+
count: 2
|
|
9797
|
+
});
|
|
9048
9798
|
turnStartedPublished = true;
|
|
9799
|
+
const workerPreparationStartedAt = performance.now();
|
|
9049
9800
|
if (isCodexTurn) {
|
|
9050
|
-
const
|
|
9051
|
-
|
|
9052
|
-
|
|
9053
|
-
|
|
9054
|
-
|
|
9055
|
-
|
|
9056
|
-
|
|
9057
|
-
|
|
9058
|
-
|
|
9059
|
-
|
|
9060
|
-
|
|
9061
|
-
|
|
9062
|
-
|
|
9063
|
-
|
|
9064
|
-
if (settings.codexCredentialLeasingEnabled) {
|
|
9065
|
-
leaseAcquisitionStartedAtMs = performance.now();
|
|
9066
|
-
leased = await acquireCodexCredentialLease(
|
|
9067
|
-
db,
|
|
9068
|
-
{
|
|
9069
|
-
accountId: input.accountId,
|
|
9070
|
-
workspaceId: input.workspaceId,
|
|
9071
|
-
turnId,
|
|
9072
|
-
holderId: codexLeaseHolderId,
|
|
9073
|
-
advanceActivePointer: sessionPin === null
|
|
9074
|
-
},
|
|
9075
|
-
selectForTurn
|
|
9076
|
-
);
|
|
9077
|
-
} else {
|
|
9078
|
-
const [rotation, accounts] = await Promise.all([
|
|
9079
|
-
getCodexRotationSettings(db, input.workspaceId),
|
|
9080
|
-
listCodexAccountStatuses(db, input.workspaceId)
|
|
9081
|
-
]);
|
|
9082
|
-
const leaseAccounts = accounts.map((account) => ({
|
|
9083
|
-
...account,
|
|
9084
|
-
activeLeaseCount: 0,
|
|
9085
|
-
selectionCount: 0,
|
|
9086
|
-
lastSelectedAt: null
|
|
9087
|
-
}));
|
|
9088
|
-
const activeCredentialId = rotation?.activeCredentialId ?? null;
|
|
9089
|
-
const selected = selectForTurn({
|
|
9090
|
-
accounts: leaseAccounts,
|
|
9091
|
-
activeCredentialId,
|
|
9092
|
-
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9093
|
-
leaseRotationEnabled: false,
|
|
9094
|
-
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
9095
|
-
existingCredentialId: null,
|
|
9096
|
-
policyScope: null,
|
|
9097
|
-
unavailableDiagnostics: []
|
|
9098
|
-
});
|
|
9099
|
-
leased = {
|
|
9100
|
-
...selected,
|
|
9101
|
-
accounts: leaseAccounts,
|
|
9102
|
-
activeCredentialId,
|
|
9103
|
-
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9104
|
-
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
9105
|
-
reused: false,
|
|
9106
|
-
holderId: null,
|
|
9107
|
-
generation: null,
|
|
9108
|
-
leasedUntil: null,
|
|
9109
|
-
unavailableDiagnostics: [],
|
|
9110
|
-
advanceActivePointer: selected.advanceActivePointer !== false
|
|
9111
|
-
};
|
|
9112
|
-
}
|
|
9113
|
-
if (leased.decision.kind === "allCapped") {
|
|
9114
|
-
await refreshCappedCodexUsageRows(db, settings, input.workspaceId, leased.accounts, {
|
|
9115
|
-
signalCodexCapacityWorkflow,
|
|
9116
|
-
wakeSessionWorkflow
|
|
9801
|
+
const credentialSelectionStartedAt = performance.now();
|
|
9802
|
+
let credentialSelectionOutcome = "completed";
|
|
9803
|
+
try {
|
|
9804
|
+
const sessionCodex = await getSessionCodexState(db, input.workspaceId, input.sessionId);
|
|
9805
|
+
const sessionPin = sessionCodex?.pinnedCredentialId ?? null;
|
|
9806
|
+
const sessionPinSource = sessionCodex?.pinSource ?? null;
|
|
9807
|
+
const selectForTurn = (context) => selectCodexCredentialLeaseForTurn({
|
|
9808
|
+
context,
|
|
9809
|
+
leasingEnabled: settings.codexCredentialLeasingEnabled,
|
|
9810
|
+
sessionId: input.sessionId,
|
|
9811
|
+
sessionPinnedCredentialId: sessionPin,
|
|
9812
|
+
sessionPinSource,
|
|
9813
|
+
sessionLastCredentialId: sessionCodex?.lastCredentialId ?? null,
|
|
9814
|
+
now: /* @__PURE__ */ new Date()
|
|
9117
9815
|
});
|
|
9816
|
+
let leased;
|
|
9817
|
+
let leaseAcquisitionStartedAtMs = null;
|
|
9118
9818
|
if (settings.codexCredentialLeasingEnabled) {
|
|
9119
9819
|
leaseAcquisitionStartedAtMs = performance.now();
|
|
9120
9820
|
leased = await acquireCodexCredentialLease(
|
|
@@ -9139,9 +9839,10 @@ function createRunAgentTurnActivity(services) {
|
|
|
9139
9839
|
selectionCount: 0,
|
|
9140
9840
|
lastSelectedAt: null
|
|
9141
9841
|
}));
|
|
9842
|
+
const activeCredentialId = rotation?.activeCredentialId ?? null;
|
|
9142
9843
|
const selected = selectForTurn({
|
|
9143
9844
|
accounts: leaseAccounts,
|
|
9144
|
-
activeCredentialId
|
|
9845
|
+
activeCredentialId,
|
|
9145
9846
|
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9146
9847
|
leaseRotationEnabled: false,
|
|
9147
9848
|
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
@@ -9152,7 +9853,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
9152
9853
|
leased = {
|
|
9153
9854
|
...selected,
|
|
9154
9855
|
accounts: leaseAccounts,
|
|
9155
|
-
activeCredentialId
|
|
9856
|
+
activeCredentialId,
|
|
9156
9857
|
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9157
9858
|
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
9158
9859
|
reused: false,
|
|
@@ -9163,163 +9864,282 @@ function createRunAgentTurnActivity(services) {
|
|
|
9163
9864
|
advanceActivePointer: selected.advanceActivePointer !== false
|
|
9164
9865
|
};
|
|
9165
9866
|
}
|
|
9166
|
-
|
|
9167
|
-
|
|
9168
|
-
|
|
9169
|
-
|
|
9170
|
-
|
|
9171
|
-
|
|
9172
|
-
|
|
9173
|
-
|
|
9174
|
-
|
|
9175
|
-
const pinMutation = await withSessionCodexCapacityMutation(
|
|
9176
|
-
db,
|
|
9177
|
-
{
|
|
9178
|
-
workspaceId: input.workspaceId,
|
|
9179
|
-
reason: "codex_policy_pin_changed"
|
|
9180
|
-
},
|
|
9181
|
-
async (tx) => {
|
|
9182
|
-
const changed = await setSessionCodexPinInTransaction(
|
|
9183
|
-
tx,
|
|
9184
|
-
input.workspaceId,
|
|
9185
|
-
input.sessionId,
|
|
9186
|
-
leased.credentialId,
|
|
9187
|
-
"policy",
|
|
9867
|
+
if (leased.decision.kind === "allCapped") {
|
|
9868
|
+
await refreshCappedCodexUsageRows(db, settings, input.workspaceId, leased.accounts, {
|
|
9869
|
+
signalCodexCapacityWorkflow,
|
|
9870
|
+
wakeSessionWorkflow
|
|
9871
|
+
});
|
|
9872
|
+
if (settings.codexCredentialLeasingEnabled) {
|
|
9873
|
+
leaseAcquisitionStartedAtMs = performance.now();
|
|
9874
|
+
leased = await acquireCodexCredentialLease(
|
|
9875
|
+
db,
|
|
9188
9876
|
{
|
|
9189
|
-
|
|
9190
|
-
|
|
9191
|
-
|
|
9192
|
-
|
|
9193
|
-
|
|
9194
|
-
|
|
9195
|
-
|
|
9196
|
-
}
|
|
9197
|
-
);
|
|
9198
|
-
await signalCodexCapacityWakeTargets(
|
|
9199
|
-
{ signalCodexCapacityWorkflow, wakeSessionWorkflow },
|
|
9200
|
-
pinMutation.wakeTargets
|
|
9201
|
-
);
|
|
9202
|
-
} else if (selectedPinDisposition === "clearStale") {
|
|
9203
|
-
const pinMutation = await withSessionCodexCapacityMutation(
|
|
9204
|
-
db,
|
|
9205
|
-
{
|
|
9206
|
-
workspaceId: input.workspaceId,
|
|
9207
|
-
reason: "codex_stale_policy_pin_cleared"
|
|
9208
|
-
},
|
|
9209
|
-
async (tx) => {
|
|
9210
|
-
const changed = await setSessionCodexPinInTransaction(
|
|
9211
|
-
tx,
|
|
9212
|
-
input.workspaceId,
|
|
9213
|
-
input.sessionId,
|
|
9214
|
-
null,
|
|
9215
|
-
"policy",
|
|
9216
|
-
{
|
|
9217
|
-
expected: {
|
|
9218
|
-
pinnedCredentialId: sessionPin,
|
|
9219
|
-
pinSource: sessionPinSource
|
|
9220
|
-
}
|
|
9221
|
-
}
|
|
9877
|
+
accountId: input.accountId,
|
|
9878
|
+
workspaceId: input.workspaceId,
|
|
9879
|
+
turnId,
|
|
9880
|
+
holderId: codexLeaseHolderId,
|
|
9881
|
+
advanceActivePointer: sessionPin === null
|
|
9882
|
+
},
|
|
9883
|
+
selectForTurn
|
|
9222
9884
|
);
|
|
9223
|
-
|
|
9885
|
+
} else {
|
|
9886
|
+
const [rotation, accounts] = await Promise.all([
|
|
9887
|
+
getCodexRotationSettings(db, input.workspaceId),
|
|
9888
|
+
listCodexAccountStatuses(db, input.workspaceId)
|
|
9889
|
+
]);
|
|
9890
|
+
const leaseAccounts = accounts.map((account) => ({
|
|
9891
|
+
...account,
|
|
9892
|
+
activeLeaseCount: 0,
|
|
9893
|
+
selectionCount: 0,
|
|
9894
|
+
lastSelectedAt: null
|
|
9895
|
+
}));
|
|
9896
|
+
const selected = selectForTurn({
|
|
9897
|
+
accounts: leaseAccounts,
|
|
9898
|
+
activeCredentialId: rotation?.activeCredentialId ?? null,
|
|
9899
|
+
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9900
|
+
leaseRotationEnabled: false,
|
|
9901
|
+
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
9902
|
+
existingCredentialId: null,
|
|
9903
|
+
policyScope: null,
|
|
9904
|
+
unavailableDiagnostics: []
|
|
9905
|
+
});
|
|
9906
|
+
leased = {
|
|
9907
|
+
...selected,
|
|
9908
|
+
accounts: leaseAccounts,
|
|
9909
|
+
activeCredentialId: rotation?.activeCredentialId ?? null,
|
|
9910
|
+
rotationEnabled: rotation?.rotationEnabled ?? false,
|
|
9911
|
+
rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
|
|
9912
|
+
reused: false,
|
|
9913
|
+
holderId: null,
|
|
9914
|
+
generation: null,
|
|
9915
|
+
leasedUntil: null,
|
|
9916
|
+
unavailableDiagnostics: [],
|
|
9917
|
+
advanceActivePointer: selected.advanceActivePointer !== false
|
|
9918
|
+
};
|
|
9224
9919
|
}
|
|
9225
|
-
|
|
9226
|
-
|
|
9227
|
-
|
|
9228
|
-
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
9232
|
-
await setActiveCodexCredential(db, input.workspaceId, rotationDecision.credentialId);
|
|
9233
|
-
}
|
|
9234
|
-
effectiveCodexCredentialId = leased.credentialId;
|
|
9235
|
-
codexLeaseGeneration = leased.generation;
|
|
9236
|
-
codexLeaseConfirmedUntilMs = leased.leasedUntil && leaseAcquisitionStartedAtMs !== null ? leaseAcquisitionStartedAtMs + CODEX_CREDENTIAL_LEASE_TTL_MS : null;
|
|
9237
|
-
codexLeaseHeld = effectiveCodexCredentialId !== null && leased.holderId !== null && leased.generation !== null && codexLeaseConfirmedUntilMs !== null;
|
|
9238
|
-
if (codexLeaseHeld) startCodexLeaseHeartbeat();
|
|
9239
|
-
const actualOutcome = effectiveCodexCredentialId ? "selected" : rotationDecision.kind === "allCapped" ? "waiting" : "none";
|
|
9240
|
-
const actualReason = effectiveCodexCredentialId ? leased.reused ? "lease_reused" : sessionPin === effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active" : rotationDecision.kind === "allCapped" ? "all_capped" : "none";
|
|
9241
|
-
const fencedInFlight = leased.reused;
|
|
9242
|
-
const shadowResult = await publishCodexFleetShadowDecisionV1({
|
|
9243
|
-
enabled: settings.codexFleetPolicyShadowEnabled,
|
|
9244
|
-
decision: {
|
|
9245
|
-
accounts: leased.accounts,
|
|
9246
|
-
actualCredentialId: effectiveCodexCredentialId,
|
|
9247
|
-
actualOutcome,
|
|
9248
|
-
actualReason,
|
|
9249
|
-
affinityCredentialId: fencedInFlight ? effectiveCodexCredentialId : sessionPin ?? sessionCodex?.lastCredentialId ?? null,
|
|
9250
|
-
fencedInFlight,
|
|
9251
|
-
nearExhaustionPct: settings.codexRotationNearExhaustionPct,
|
|
9252
|
-
now: /* @__PURE__ */ new Date(),
|
|
9253
|
-
aliasSeed: randomUUID4()
|
|
9254
|
-
},
|
|
9255
|
-
publish
|
|
9256
|
-
});
|
|
9257
|
-
if (shadowResult.outcome === "published") {
|
|
9258
|
-
const shadowPayload = shadowResult.payload;
|
|
9259
|
-
observability.incrementCounter({
|
|
9260
|
-
name: "opengeni_codex_fleet_shadow_decisions_total",
|
|
9261
|
-
help: "Shadow decisions by bounded actual/shadow outcome and comparison.",
|
|
9262
|
-
labels: codexFleetShadowDecisionMetricLabelsV1(shadowPayload)
|
|
9263
|
-
});
|
|
9264
|
-
observability.info("Codex adaptive fleet shadow decision", {
|
|
9265
|
-
workspaceId: input.workspaceId,
|
|
9266
|
-
policyVersion: shadowPayload.replay.policyVersion,
|
|
9267
|
-
inputFingerprint: shadowPayload.replay.inputFingerprint,
|
|
9268
|
-
decisionFingerprint: shadowPayload.replay.decisionFingerprint,
|
|
9269
|
-
actualOutcome: shadowPayload.actual.outcome,
|
|
9270
|
-
shadowOutcome: shadowPayload.replay.decision.outcome,
|
|
9271
|
-
comparison: shadowPayload.comparison,
|
|
9272
|
-
candidateCount: shadowPayload.replay.input.candidates.length,
|
|
9273
|
-
truncatedCandidateCount: shadowPayload.replay.truncatedCandidateCount,
|
|
9274
|
-
payloadBytes: shadowResult.payloadBytes
|
|
9275
|
-
});
|
|
9276
|
-
} else if (shadowResult.outcome === "failed") {
|
|
9277
|
-
observability.incrementCounter({
|
|
9278
|
-
name: "opengeni_codex_fleet_shadow_errors_total",
|
|
9279
|
-
help: "Shadow decision build/publication failures.",
|
|
9280
|
-
labels: codexFleetShadowErrorMetricLabelsV1(shadowResult)
|
|
9920
|
+
}
|
|
9921
|
+
const rotationDecision = leased.decision;
|
|
9922
|
+
const selectedPinDisposition = classifyCodexPin({
|
|
9923
|
+
pinnedCredentialId: sessionPin,
|
|
9924
|
+
pinSource: sessionPinSource,
|
|
9925
|
+
strategy: leased.rotationStrategy,
|
|
9926
|
+
rotationEnabled: leased.rotationEnabled
|
|
9281
9927
|
});
|
|
9282
|
-
|
|
9283
|
-
|
|
9284
|
-
|
|
9285
|
-
|
|
9286
|
-
|
|
9287
|
-
|
|
9288
|
-
|
|
9928
|
+
if (selectedPinDisposition === "sharded" && leased.credentialId !== null && (sessionPinSource !== "policy" || sessionPin !== leased.credentialId)) {
|
|
9929
|
+
const pinMutation = await withSessionCodexCapacityMutation(
|
|
9930
|
+
db,
|
|
9931
|
+
{
|
|
9932
|
+
workspaceId: input.workspaceId,
|
|
9933
|
+
reason: "codex_policy_pin_changed"
|
|
9934
|
+
},
|
|
9935
|
+
async (tx) => {
|
|
9936
|
+
const changed = await setSessionCodexPinInTransaction(
|
|
9937
|
+
tx,
|
|
9938
|
+
input.workspaceId,
|
|
9939
|
+
input.sessionId,
|
|
9940
|
+
leased.credentialId,
|
|
9941
|
+
"policy",
|
|
9942
|
+
{
|
|
9943
|
+
expected: {
|
|
9944
|
+
pinnedCredentialId: sessionPin,
|
|
9945
|
+
pinSource: sessionPinSource
|
|
9946
|
+
}
|
|
9947
|
+
}
|
|
9948
|
+
);
|
|
9949
|
+
return { result: changed, changed };
|
|
9950
|
+
}
|
|
9951
|
+
);
|
|
9952
|
+
await signalCodexCapacityWakeTargets(
|
|
9953
|
+
{ signalCodexCapacityWorkflow, wakeSessionWorkflow },
|
|
9954
|
+
pinMutation.wakeTargets
|
|
9955
|
+
);
|
|
9956
|
+
} else if (selectedPinDisposition === "clearStale") {
|
|
9957
|
+
const pinMutation = await withSessionCodexCapacityMutation(
|
|
9958
|
+
db,
|
|
9959
|
+
{
|
|
9960
|
+
workspaceId: input.workspaceId,
|
|
9961
|
+
reason: "codex_stale_policy_pin_cleared"
|
|
9962
|
+
},
|
|
9963
|
+
async (tx) => {
|
|
9964
|
+
const changed = await setSessionCodexPinInTransaction(
|
|
9965
|
+
tx,
|
|
9966
|
+
input.workspaceId,
|
|
9967
|
+
input.sessionId,
|
|
9968
|
+
null,
|
|
9969
|
+
"policy",
|
|
9970
|
+
{
|
|
9971
|
+
expected: {
|
|
9972
|
+
pinnedCredentialId: sessionPin,
|
|
9973
|
+
pinSource: sessionPinSource
|
|
9974
|
+
}
|
|
9975
|
+
}
|
|
9976
|
+
);
|
|
9977
|
+
return { result: changed, changed };
|
|
9978
|
+
}
|
|
9979
|
+
);
|
|
9980
|
+
await signalCodexCapacityWakeTargets(
|
|
9981
|
+
{ signalCodexCapacityWorkflow, wakeSessionWorkflow },
|
|
9982
|
+
pinMutation.wakeTargets
|
|
9983
|
+
);
|
|
9984
|
+
}
|
|
9985
|
+
if (!settings.codexCredentialLeasingEnabled && leased.advanceActivePointer && sessionPin === null && rotationDecision.kind === "active" && rotationDecision.moved) {
|
|
9986
|
+
await setActiveCodexCredential(db, input.workspaceId, rotationDecision.credentialId);
|
|
9987
|
+
}
|
|
9988
|
+
effectiveCodexCredentialId = leased.credentialId;
|
|
9989
|
+
codexLeaseGeneration = leased.generation;
|
|
9990
|
+
codexLeaseConfirmedUntilMs = leased.leasedUntil && leaseAcquisitionStartedAtMs !== null ? leaseAcquisitionStartedAtMs + CODEX_CREDENTIAL_LEASE_TTL_MS : null;
|
|
9991
|
+
codexLeaseHeld = effectiveCodexCredentialId !== null && leased.holderId !== null && leased.generation !== null && codexLeaseConfirmedUntilMs !== null;
|
|
9992
|
+
if (codexLeaseHeld) startCodexLeaseHeartbeat();
|
|
9993
|
+
const actualOutcome = effectiveCodexCredentialId ? "selected" : rotationDecision.kind === "allCapped" ? "waiting" : "none";
|
|
9994
|
+
const actualReason = effectiveCodexCredentialId ? leased.reused ? "lease_reused" : sessionPin === effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active" : rotationDecision.kind === "allCapped" ? "all_capped" : "none";
|
|
9995
|
+
const fencedInFlight = leased.reused;
|
|
9996
|
+
const shadowResult = await publishCodexFleetShadowDecisionV1({
|
|
9997
|
+
enabled: settings.codexFleetPolicyShadowEnabled,
|
|
9998
|
+
decision: {
|
|
9999
|
+
accounts: leased.accounts,
|
|
10000
|
+
actualCredentialId: effectiveCodexCredentialId,
|
|
10001
|
+
actualOutcome,
|
|
10002
|
+
actualReason,
|
|
10003
|
+
affinityCredentialId: fencedInFlight ? effectiveCodexCredentialId : sessionPin ?? sessionCodex?.lastCredentialId ?? null,
|
|
10004
|
+
fencedInFlight,
|
|
10005
|
+
nearExhaustionPct: settings.codexRotationNearExhaustionPct,
|
|
10006
|
+
now: /* @__PURE__ */ new Date(),
|
|
10007
|
+
aliasSeed: randomUUID4()
|
|
10008
|
+
},
|
|
10009
|
+
publish
|
|
9289
10010
|
});
|
|
9290
|
-
|
|
9291
|
-
|
|
9292
|
-
|
|
9293
|
-
|
|
9294
|
-
|
|
9295
|
-
|
|
9296
|
-
|
|
9297
|
-
|
|
9298
|
-
|
|
9299
|
-
|
|
9300
|
-
|
|
10011
|
+
if (shadowResult.outcome === "published") {
|
|
10012
|
+
const shadowPayload = shadowResult.payload;
|
|
10013
|
+
observability.incrementCounter({
|
|
10014
|
+
name: "opengeni_codex_fleet_shadow_decisions_total",
|
|
10015
|
+
help: "Shadow decisions by bounded actual/shadow outcome and comparison.",
|
|
10016
|
+
labels: codexFleetShadowDecisionMetricLabelsV1(shadowPayload)
|
|
10017
|
+
});
|
|
10018
|
+
observability.info("Codex adaptive fleet shadow decision", {
|
|
10019
|
+
workspaceId: input.workspaceId,
|
|
10020
|
+
policyVersion: shadowPayload.replay.policyVersion,
|
|
10021
|
+
inputFingerprint: shadowPayload.replay.inputFingerprint,
|
|
10022
|
+
decisionFingerprint: shadowPayload.replay.decisionFingerprint,
|
|
10023
|
+
actualOutcome: shadowPayload.actual.outcome,
|
|
10024
|
+
shadowOutcome: shadowPayload.replay.decision.outcome,
|
|
10025
|
+
comparison: shadowPayload.comparison,
|
|
10026
|
+
candidateCount: shadowPayload.replay.input.candidates.length,
|
|
10027
|
+
truncatedCandidateCount: shadowPayload.replay.truncatedCandidateCount,
|
|
10028
|
+
payloadBytes: shadowResult.payloadBytes
|
|
10029
|
+
});
|
|
10030
|
+
} else if (shadowResult.outcome === "failed") {
|
|
10031
|
+
observability.incrementCounter({
|
|
10032
|
+
name: "opengeni_codex_fleet_shadow_errors_total",
|
|
10033
|
+
help: "Shadow decision build/publication failures.",
|
|
10034
|
+
labels: codexFleetShadowErrorMetricLabelsV1(shadowResult)
|
|
10035
|
+
});
|
|
10036
|
+
observability.warn("Codex adaptive fleet shadow decision failed open", {
|
|
10037
|
+
stage: shadowResult.stage,
|
|
10038
|
+
reason: shadowResult.reason,
|
|
10039
|
+
errorClass: "CodexFleetShadowOperationError",
|
|
10040
|
+
errorCode: "codex_fleet_shadow_failed",
|
|
10041
|
+
origin: "worker",
|
|
10042
|
+
payloadBytes: shadowResult.payloadBytes
|
|
10043
|
+
});
|
|
10044
|
+
}
|
|
10045
|
+
const eligibleCount = leased.accounts.filter(
|
|
10046
|
+
(account) => isCodexCredentialEligible(account, /* @__PURE__ */ new Date())
|
|
10047
|
+
).length;
|
|
10048
|
+
const poolDepth = eligibleCount === 0 ? "zero" : eligibleCount === 1 ? "one" : "many";
|
|
9301
10049
|
observability.incrementCounter({
|
|
9302
|
-
name: "
|
|
9303
|
-
help: "
|
|
10050
|
+
name: "opengeni_codex_pool_observations_total",
|
|
10051
|
+
help: "Observed eligible Codex pool depth buckets at turn selection.",
|
|
9304
10052
|
labels: { workspace_key: codexWorkspaceKey, depth: poolDepth }
|
|
9305
10053
|
});
|
|
9306
|
-
|
|
9307
|
-
|
|
9308
|
-
|
|
9309
|
-
|
|
9310
|
-
|
|
9311
|
-
|
|
9312
|
-
|
|
9313
|
-
|
|
9314
|
-
|
|
10054
|
+
if (eligibleCount <= 1) {
|
|
10055
|
+
observability.incrementCounter({
|
|
10056
|
+
name: "opengeni_codex_pool_low_total",
|
|
10057
|
+
help: "Alert signal emitted when the eligible Codex pool is zero or one.",
|
|
10058
|
+
labels: { workspace_key: codexWorkspaceKey, depth: poolDepth }
|
|
10059
|
+
});
|
|
10060
|
+
observability.warn("Codex eligible credential pool is low", {
|
|
10061
|
+
workspaceId: input.workspaceId,
|
|
10062
|
+
eligibleCount,
|
|
10063
|
+
connectedCount: leased.accounts.length,
|
|
10064
|
+
depth: poolDepth
|
|
10065
|
+
});
|
|
10066
|
+
}
|
|
10067
|
+
if (effectiveCodexCredentialId === null && leased.accounts.length > 0 && leased.accounts.every((account) => !account.allocatorEnabled) && turnId) {
|
|
10068
|
+
if (turn.source === "compaction") {
|
|
10069
|
+
if (!await settle({
|
|
10070
|
+
events: [
|
|
10071
|
+
{
|
|
10072
|
+
type: "turn.cancelled",
|
|
10073
|
+
payload: {
|
|
10074
|
+
maintenance: "context_compaction",
|
|
10075
|
+
reason: "codex_allocator_disabled",
|
|
10076
|
+
requestPreserved: true
|
|
10077
|
+
}
|
|
10078
|
+
},
|
|
10079
|
+
{
|
|
10080
|
+
type: "session.status.changed",
|
|
10081
|
+
payload: { status: "idle" }
|
|
10082
|
+
}
|
|
10083
|
+
],
|
|
10084
|
+
turnStatus: "cancelled",
|
|
10085
|
+
sessionStatus: "idle",
|
|
10086
|
+
activeTurnId: null
|
|
10087
|
+
})) {
|
|
10088
|
+
return claimedResult({ status: "cancelled" });
|
|
10089
|
+
}
|
|
10090
|
+
turnMetricOutcome = "cancelled";
|
|
10091
|
+
activityStatus = "idle";
|
|
10092
|
+
return claimedResult({ status: "idle", deferredUntilWake: true });
|
|
10093
|
+
}
|
|
10094
|
+
const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(
|
|
10095
|
+
() => null
|
|
10096
|
+
);
|
|
10097
|
+
const activeGoal = goal?.status === "active" ? goal : null;
|
|
10098
|
+
const armed = await armCodexCapacityWait(db, {
|
|
10099
|
+
accountId: input.accountId,
|
|
10100
|
+
workspaceId: input.workspaceId,
|
|
10101
|
+
sessionId: input.sessionId,
|
|
10102
|
+
turnId,
|
|
10103
|
+
attemptId: input.attemptId,
|
|
10104
|
+
workflowId: input.workflowId,
|
|
10105
|
+
goalId: activeGoal?.id ?? null,
|
|
10106
|
+
goalVersion: activeGoal?.version ?? null,
|
|
10107
|
+
earliestResetAt: null,
|
|
10108
|
+
resetKind: "bounded_refresh",
|
|
10109
|
+
failurePayload: {
|
|
10110
|
+
error: "All connected Codex subscriptions are disabled for new allocations.",
|
|
10111
|
+
code: "codex_allocator_disabled",
|
|
10112
|
+
detail: "waiting for a credential to be re-enabled, reconnected, or added"
|
|
10113
|
+
}
|
|
10114
|
+
});
|
|
10115
|
+
if (armed.action === "waiting") {
|
|
10116
|
+
await publishDurableSessionEvents2(
|
|
10117
|
+
bus,
|
|
10118
|
+
input.workspaceId,
|
|
10119
|
+
input.sessionId,
|
|
10120
|
+
armed.events
|
|
10121
|
+
);
|
|
10122
|
+
turnMetricOutcome = "recovering";
|
|
10123
|
+
activityStatus = "waiting_capacity";
|
|
10124
|
+
return claimedResult({
|
|
10125
|
+
status: "waiting_capacity",
|
|
10126
|
+
capacityWait: {
|
|
10127
|
+
waiterId: armed.waiter.id,
|
|
10128
|
+
generation: armed.waiter.generation,
|
|
10129
|
+
nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
|
|
10130
|
+
wakeRevision: armed.waiter.wakeRevision
|
|
10131
|
+
}
|
|
10132
|
+
});
|
|
10133
|
+
}
|
|
9315
10134
|
if (!await settle({
|
|
9316
10135
|
events: [
|
|
9317
10136
|
{
|
|
9318
|
-
type: "turn.
|
|
10137
|
+
type: "turn.failed",
|
|
9319
10138
|
payload: {
|
|
9320
|
-
|
|
9321
|
-
|
|
9322
|
-
|
|
10139
|
+
error: "All connected Codex subscriptions are disabled for new allocations.",
|
|
10140
|
+
code: "codex_allocator_disabled",
|
|
10141
|
+
retryable: false,
|
|
10142
|
+
recovery: "user_message"
|
|
9323
10143
|
}
|
|
9324
10144
|
},
|
|
9325
10145
|
{
|
|
@@ -9327,89 +10147,106 @@ function createRunAgentTurnActivity(services) {
|
|
|
9327
10147
|
payload: { status: "idle" }
|
|
9328
10148
|
}
|
|
9329
10149
|
],
|
|
9330
|
-
turnStatus: "
|
|
10150
|
+
turnStatus: "failed",
|
|
9331
10151
|
sessionStatus: "idle",
|
|
9332
10152
|
activeTurnId: null
|
|
9333
10153
|
})) {
|
|
9334
10154
|
return claimedResult({ status: "cancelled" });
|
|
9335
10155
|
}
|
|
9336
|
-
turnMetricOutcome = "
|
|
10156
|
+
turnMetricOutcome = "failed";
|
|
9337
10157
|
activityStatus = "idle";
|
|
9338
|
-
return claimedResult({ status: "idle"
|
|
10158
|
+
return claimedResult({ status: "idle" });
|
|
9339
10159
|
}
|
|
9340
|
-
|
|
9341
|
-
()
|
|
9342
|
-
|
|
9343
|
-
|
|
9344
|
-
|
|
9345
|
-
|
|
9346
|
-
|
|
9347
|
-
|
|
9348
|
-
|
|
9349
|
-
|
|
9350
|
-
|
|
9351
|
-
|
|
9352
|
-
|
|
9353
|
-
|
|
9354
|
-
|
|
9355
|
-
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
10160
|
+
if (rotationDecision.kind === "allCapped" && turnId) {
|
|
10161
|
+
if (turn.source === "compaction") {
|
|
10162
|
+
if (!await settle({
|
|
10163
|
+
events: [
|
|
10164
|
+
{
|
|
10165
|
+
type: "turn.cancelled",
|
|
10166
|
+
payload: {
|
|
10167
|
+
maintenance: "context_compaction",
|
|
10168
|
+
reason: "codex_capacity_unavailable",
|
|
10169
|
+
requestPreserved: true
|
|
10170
|
+
}
|
|
10171
|
+
},
|
|
10172
|
+
{
|
|
10173
|
+
type: "session.status.changed",
|
|
10174
|
+
payload: { status: "idle" }
|
|
10175
|
+
}
|
|
10176
|
+
],
|
|
10177
|
+
turnStatus: "cancelled",
|
|
10178
|
+
sessionStatus: "idle",
|
|
10179
|
+
activeTurnId: null
|
|
10180
|
+
})) {
|
|
10181
|
+
return claimedResult({ status: "cancelled" });
|
|
10182
|
+
}
|
|
10183
|
+
turnMetricOutcome = "cancelled";
|
|
10184
|
+
activityStatus = "idle";
|
|
10185
|
+
return claimedResult({ status: "idle", deferredUntilWake: true });
|
|
9359
10186
|
}
|
|
9360
|
-
|
|
9361
|
-
|
|
9362
|
-
await publishDurableSessionEvents2(
|
|
9363
|
-
bus,
|
|
9364
|
-
input.workspaceId,
|
|
9365
|
-
input.sessionId,
|
|
9366
|
-
armed.events
|
|
10187
|
+
const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(
|
|
10188
|
+
() => null
|
|
9367
10189
|
);
|
|
9368
|
-
|
|
9369
|
-
|
|
9370
|
-
|
|
9371
|
-
|
|
9372
|
-
|
|
9373
|
-
|
|
9374
|
-
|
|
9375
|
-
|
|
9376
|
-
|
|
9377
|
-
}
|
|
10190
|
+
const goalActive = Boolean(goal && goal.status === "active");
|
|
10191
|
+
const resumeMs = computeIdleDelayMs(
|
|
10192
|
+
rotationDecision.earliestResetAt,
|
|
10193
|
+
/* @__PURE__ */ new Date(),
|
|
10194
|
+
CODEX_USAGE_LIMIT_MAX_RESUME_MS
|
|
10195
|
+
);
|
|
10196
|
+
const failurePayload = codexUsageLimitFailurePayload(
|
|
10197
|
+
{ resetsInSeconds: Math.ceil(resumeMs / 1e3) },
|
|
10198
|
+
"all connected Codex subscriptions are rate-limited",
|
|
10199
|
+
{ allAccounts: true }
|
|
10200
|
+
);
|
|
10201
|
+
const authoritativeResetAt = authoritativeCodexCapacityResetAt(
|
|
10202
|
+
leased.accounts,
|
|
10203
|
+
/* @__PURE__ */ new Date()
|
|
10204
|
+
);
|
|
10205
|
+
const armed = await armCodexCapacityWait(db, {
|
|
10206
|
+
accountId: input.accountId,
|
|
10207
|
+
workspaceId: input.workspaceId,
|
|
10208
|
+
sessionId: input.sessionId,
|
|
10209
|
+
turnId,
|
|
10210
|
+
attemptId: input.attemptId,
|
|
10211
|
+
workflowId: input.workflowId,
|
|
10212
|
+
goalId: goalActive && goal ? goal.id : null,
|
|
10213
|
+
goalVersion: goalActive && goal ? goal.version : null,
|
|
10214
|
+
earliestResetAt: authoritativeResetAt,
|
|
10215
|
+
resetKind: authoritativeResetAt ? "authoritative" : "bounded_refresh",
|
|
10216
|
+
failurePayload
|
|
9378
10217
|
});
|
|
9379
|
-
|
|
9380
|
-
|
|
9381
|
-
|
|
9382
|
-
|
|
9383
|
-
|
|
9384
|
-
|
|
9385
|
-
|
|
9386
|
-
|
|
9387
|
-
|
|
9388
|
-
|
|
10218
|
+
if (armed.action === "waiting") {
|
|
10219
|
+
await publishDurableSessionEvents2(
|
|
10220
|
+
bus,
|
|
10221
|
+
input.workspaceId,
|
|
10222
|
+
input.sessionId,
|
|
10223
|
+
armed.events
|
|
10224
|
+
);
|
|
10225
|
+
turnMetricOutcome = "recovering";
|
|
10226
|
+
activityStatus = "waiting_capacity";
|
|
10227
|
+
return claimedResult({
|
|
10228
|
+
status: "waiting_capacity",
|
|
10229
|
+
capacityWait: {
|
|
10230
|
+
waiterId: armed.waiter.id,
|
|
10231
|
+
generation: armed.waiter.generation,
|
|
10232
|
+
nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
|
|
10233
|
+
wakeRevision: armed.waiter.wakeRevision
|
|
9389
10234
|
}
|
|
9390
|
-
}
|
|
9391
|
-
|
|
9392
|
-
],
|
|
9393
|
-
turnStatus: "failed",
|
|
9394
|
-
sessionStatus: "idle",
|
|
9395
|
-
activeTurnId: null
|
|
9396
|
-
})) {
|
|
9397
|
-
return claimedResult({ status: "cancelled" });
|
|
9398
|
-
}
|
|
9399
|
-
turnMetricOutcome = "failed";
|
|
9400
|
-
activityStatus = "idle";
|
|
9401
|
-
return claimedResult({ status: "idle" });
|
|
9402
|
-
}
|
|
9403
|
-
if (rotationDecision.kind === "allCapped" && turnId) {
|
|
9404
|
-
if (turn.source === "compaction") {
|
|
10235
|
+
});
|
|
10236
|
+
}
|
|
9405
10237
|
if (!await settle({
|
|
9406
10238
|
events: [
|
|
10239
|
+
// `rotated:true` (Finding 2): the proactive all-capped wait is the SAME
|
|
10240
|
+
// rotation-wait state as the reactive all-capped path, so it must freeze
|
|
10241
|
+
// autoContinuations identically (evaluateGoalContinuation reads this marker)
|
|
10242
|
+
// — a goal waiting out a long reset must not burn its continuation budget on
|
|
10243
|
+
// the proactive path while the reactive path spares it.
|
|
9407
10244
|
{
|
|
9408
|
-
type: "turn.
|
|
10245
|
+
type: "turn.failed",
|
|
9409
10246
|
payload: {
|
|
9410
|
-
|
|
9411
|
-
|
|
9412
|
-
|
|
10247
|
+
...failurePayload,
|
|
10248
|
+
recovery: goalActive ? "goal_continuation" : "user_message",
|
|
10249
|
+
rotated: true
|
|
9413
10250
|
}
|
|
9414
10251
|
},
|
|
9415
10252
|
{
|
|
@@ -9417,143 +10254,78 @@ function createRunAgentTurnActivity(services) {
|
|
|
9417
10254
|
payload: { status: "idle" }
|
|
9418
10255
|
}
|
|
9419
10256
|
],
|
|
9420
|
-
turnStatus: "
|
|
10257
|
+
turnStatus: "failed",
|
|
9421
10258
|
sessionStatus: "idle",
|
|
9422
10259
|
activeTurnId: null
|
|
9423
10260
|
})) {
|
|
9424
10261
|
return claimedResult({ status: "cancelled" });
|
|
9425
10262
|
}
|
|
9426
|
-
turnMetricOutcome = "
|
|
10263
|
+
turnMetricOutcome = "failed";
|
|
9427
10264
|
activityStatus = "idle";
|
|
9428
|
-
return claimedResult(
|
|
10265
|
+
return claimedResult(
|
|
10266
|
+
goalActive ? {
|
|
10267
|
+
status: "idle",
|
|
10268
|
+
continueDelayMs: resumeMs,
|
|
10269
|
+
idleUntilReset: true
|
|
10270
|
+
} : { status: "idle" }
|
|
10271
|
+
);
|
|
9429
10272
|
}
|
|
9430
|
-
|
|
9431
|
-
|
|
9432
|
-
|
|
9433
|
-
|
|
9434
|
-
const resumeMs = computeIdleDelayMs(
|
|
9435
|
-
rotationDecision.earliestResetAt,
|
|
9436
|
-
/* @__PURE__ */ new Date(),
|
|
9437
|
-
CODEX_USAGE_LIMIT_MAX_RESUME_MS
|
|
9438
|
-
);
|
|
9439
|
-
const failurePayload = codexUsageLimitFailurePayload(
|
|
9440
|
-
{ resetsInSeconds: Math.ceil(resumeMs / 1e3) },
|
|
9441
|
-
"all connected Codex subscriptions are rate-limited",
|
|
9442
|
-
{ allAccounts: true }
|
|
9443
|
-
);
|
|
9444
|
-
const authoritativeResetAt = authoritativeCodexCapacityResetAt(
|
|
9445
|
-
leased.accounts,
|
|
9446
|
-
/* @__PURE__ */ new Date()
|
|
9447
|
-
);
|
|
9448
|
-
const armed = await armCodexCapacityWait(db, {
|
|
9449
|
-
accountId: input.accountId,
|
|
9450
|
-
workspaceId: input.workspaceId,
|
|
9451
|
-
sessionId: input.sessionId,
|
|
9452
|
-
turnId,
|
|
9453
|
-
attemptId: input.attemptId,
|
|
9454
|
-
workflowId: input.workflowId,
|
|
9455
|
-
goalId: goalActive && goal ? goal.id : null,
|
|
9456
|
-
goalVersion: goalActive && goal ? goal.version : null,
|
|
9457
|
-
earliestResetAt: authoritativeResetAt,
|
|
9458
|
-
resetKind: authoritativeResetAt ? "authoritative" : "bounded_refresh",
|
|
9459
|
-
failurePayload
|
|
9460
|
-
});
|
|
9461
|
-
if (armed.action === "waiting") {
|
|
9462
|
-
await publishDurableSessionEvents2(
|
|
9463
|
-
bus,
|
|
10273
|
+
if (effectiveCodexCredentialId) {
|
|
10274
|
+
const priorAccountId = sessionCodex?.lastCredentialId ?? null;
|
|
10275
|
+
await recordSessionActiveCodexCredential(
|
|
10276
|
+
db,
|
|
9464
10277
|
input.workspaceId,
|
|
9465
10278
|
input.sessionId,
|
|
9466
|
-
|
|
10279
|
+
effectiveCodexCredentialId
|
|
9467
10280
|
);
|
|
9468
|
-
|
|
9469
|
-
|
|
9470
|
-
|
|
9471
|
-
|
|
9472
|
-
|
|
9473
|
-
|
|
9474
|
-
|
|
9475
|
-
|
|
9476
|
-
|
|
10281
|
+
if (priorAccountId !== effectiveCodexCredentialId) {
|
|
10282
|
+
const rotated = rotationDecision.kind === "active" && rotationDecision.moved;
|
|
10283
|
+
await publish([
|
|
10284
|
+
{
|
|
10285
|
+
type: "codex.account.switched",
|
|
10286
|
+
payload: {
|
|
10287
|
+
fromAccountId: priorAccountId,
|
|
10288
|
+
toAccountId: effectiveCodexCredentialId,
|
|
10289
|
+
reason: rotated ? "rotation" : "manual"
|
|
10290
|
+
}
|
|
10291
|
+
}
|
|
10292
|
+
]);
|
|
10293
|
+
}
|
|
10294
|
+
const selectionReason = leased.reused ? "lease_reused" : sessionPin === effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active";
|
|
10295
|
+
observability.incrementCounter({
|
|
10296
|
+
name: "opengeni_codex_credential_selections_total",
|
|
10297
|
+
help: "Codex credential selections by strategy and reason.",
|
|
10298
|
+
labels: {
|
|
10299
|
+
workspace_key: codexWorkspaceKey,
|
|
10300
|
+
strategy: leased.rotationStrategy,
|
|
10301
|
+
reason: selectionReason
|
|
9477
10302
|
}
|
|
9478
10303
|
});
|
|
9479
|
-
}
|
|
9480
|
-
if (!await settle({
|
|
9481
|
-
events: [
|
|
9482
|
-
// `rotated:true` (Finding 2): the proactive all-capped wait is the SAME
|
|
9483
|
-
// rotation-wait state as the reactive all-capped path, so it must freeze
|
|
9484
|
-
// autoContinuations identically (evaluateGoalContinuation reads this marker)
|
|
9485
|
-
// — a goal waiting out a long reset must not burn its continuation budget on
|
|
9486
|
-
// the proactive path while the reactive path spares it.
|
|
9487
|
-
{
|
|
9488
|
-
type: "turn.failed",
|
|
9489
|
-
payload: {
|
|
9490
|
-
...failurePayload,
|
|
9491
|
-
recovery: goalActive ? "goal_continuation" : "user_message",
|
|
9492
|
-
rotated: true
|
|
9493
|
-
}
|
|
9494
|
-
},
|
|
9495
|
-
{ type: "session.status.changed", payload: { status: "idle" } }
|
|
9496
|
-
],
|
|
9497
|
-
turnStatus: "failed",
|
|
9498
|
-
sessionStatus: "idle",
|
|
9499
|
-
activeTurnId: null
|
|
9500
|
-
})) {
|
|
9501
|
-
return claimedResult({ status: "cancelled" });
|
|
9502
|
-
}
|
|
9503
|
-
turnMetricOutcome = "failed";
|
|
9504
|
-
activityStatus = "idle";
|
|
9505
|
-
return claimedResult(
|
|
9506
|
-
goalActive ? {
|
|
9507
|
-
status: "idle",
|
|
9508
|
-
continueDelayMs: resumeMs,
|
|
9509
|
-
idleUntilReset: true
|
|
9510
|
-
} : { status: "idle" }
|
|
9511
|
-
);
|
|
9512
|
-
}
|
|
9513
|
-
if (effectiveCodexCredentialId) {
|
|
9514
|
-
const priorAccountId = sessionCodex?.lastCredentialId ?? null;
|
|
9515
|
-
await recordSessionActiveCodexCredential(
|
|
9516
|
-
db,
|
|
9517
|
-
input.workspaceId,
|
|
9518
|
-
input.sessionId,
|
|
9519
|
-
effectiveCodexCredentialId
|
|
9520
|
-
);
|
|
9521
|
-
if (priorAccountId !== effectiveCodexCredentialId) {
|
|
9522
|
-
const rotated = rotationDecision.kind === "active" && rotationDecision.moved;
|
|
9523
10304
|
await publish([
|
|
9524
10305
|
{
|
|
9525
|
-
type: "codex.
|
|
10306
|
+
type: "codex.credential.selected",
|
|
9526
10307
|
payload: {
|
|
9527
|
-
|
|
9528
|
-
|
|
9529
|
-
reason:
|
|
10308
|
+
credentialId: effectiveCodexCredentialId,
|
|
10309
|
+
strategy: leased.rotationStrategy,
|
|
10310
|
+
reason: selectionReason,
|
|
10311
|
+
eligibleCount,
|
|
10312
|
+
connectedCount: leased.accounts.length,
|
|
10313
|
+
reused: leased.reused
|
|
9530
10314
|
}
|
|
9531
10315
|
}
|
|
9532
10316
|
]);
|
|
9533
10317
|
}
|
|
9534
|
-
|
|
9535
|
-
|
|
9536
|
-
|
|
9537
|
-
|
|
9538
|
-
|
|
9539
|
-
|
|
9540
|
-
|
|
9541
|
-
|
|
9542
|
-
|
|
10318
|
+
} catch (error) {
|
|
10319
|
+
credentialSelectionOutcome = "failed";
|
|
10320
|
+
throw error;
|
|
10321
|
+
} finally {
|
|
10322
|
+
recordTurnStartupPhase(observability, {
|
|
10323
|
+
phase: "credential_selection",
|
|
10324
|
+
provider: "codex-subscription",
|
|
10325
|
+
backend: turn.sandboxBackend,
|
|
10326
|
+
outcome: credentialSelectionOutcome,
|
|
10327
|
+
durationSeconds: (performance.now() - credentialSelectionStartedAt) / 1e3
|
|
9543
10328
|
});
|
|
9544
|
-
await publish([
|
|
9545
|
-
{
|
|
9546
|
-
type: "codex.credential.selected",
|
|
9547
|
-
payload: {
|
|
9548
|
-
credentialId: effectiveCodexCredentialId,
|
|
9549
|
-
strategy: leased.rotationStrategy,
|
|
9550
|
-
reason: selectionReason,
|
|
9551
|
-
eligibleCount,
|
|
9552
|
-
connectedCount: leased.accounts.length,
|
|
9553
|
-
reused: leased.reused
|
|
9554
|
-
}
|
|
9555
|
-
}
|
|
9556
|
-
]);
|
|
9557
10329
|
}
|
|
9558
10330
|
}
|
|
9559
10331
|
if (isXaiTurn) {
|
|
@@ -9733,6 +10505,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
9733
10505
|
credentialId: effectiveXaiCredentialId
|
|
9734
10506
|
});
|
|
9735
10507
|
}
|
|
10508
|
+
const runtimePreparationStartedAt = performance.now();
|
|
9736
10509
|
const [packRuntime, installedSkillRuntime] = await Promise.all([
|
|
9737
10510
|
resolveWorkspacePackRuntime(db, input.workspaceId),
|
|
9738
10511
|
resolveWorkspaceInstalledSkillRuntime(db, input.workspaceId)
|
|
@@ -9820,9 +10593,21 @@ function createRunAgentTurnActivity(services) {
|
|
|
9820
10593
|
modelCanReceiveRetainedSessionImages = supportsImageInput;
|
|
9821
10594
|
const attachmentProjector = createModelHistoryAttachmentProjector(
|
|
9822
10595
|
db,
|
|
9823
|
-
|
|
10596
|
+
{
|
|
10597
|
+
accountId: input.accountId,
|
|
10598
|
+
workspaceId: input.workspaceId,
|
|
10599
|
+
subjectId: fileAuthoritySubjectId
|
|
10600
|
+
},
|
|
9824
10601
|
modelInputPolicy,
|
|
9825
|
-
objectStorage ? (file) =>
|
|
10602
|
+
objectStorage ? async (file) => {
|
|
10603
|
+
await requireFileForSubject3(db, {
|
|
10604
|
+
accountId: input.accountId,
|
|
10605
|
+
workspaceId: input.workspaceId,
|
|
10606
|
+
subjectId: fileAuthoritySubjectId,
|
|
10607
|
+
fileId: file.id
|
|
10608
|
+
});
|
|
10609
|
+
return await objectStorage.getFileBytes(file);
|
|
10610
|
+
} : void 0
|
|
9826
10611
|
);
|
|
9827
10612
|
const modelHistoryProjector = async (items) => projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
|
|
9828
10613
|
const generatedImageHistoryProjector = async (items) => {
|
|
@@ -9880,6 +10665,11 @@ function createRunAgentTurnActivity(services) {
|
|
|
9880
10665
|
});
|
|
9881
10666
|
}
|
|
9882
10667
|
let codexModelRequestSequence = 0;
|
|
10668
|
+
let firstModelRequestPreparationStartedAt = null;
|
|
10669
|
+
let firstModelRequestPreparationRecorded = false;
|
|
10670
|
+
let firstModelRequestAuditRecorded = false;
|
|
10671
|
+
let firstModelRequestCheckpointAt = null;
|
|
10672
|
+
const firstModelRequestCheckpoints = /* @__PURE__ */ new Set();
|
|
9883
10673
|
const codexContext = resolvedModel?.provider.kind === "codex-subscription" ? (() => {
|
|
9884
10674
|
const resolver = buildCodexTokenResolver(
|
|
9885
10675
|
db,
|
|
@@ -9906,10 +10696,38 @@ function createRunAgentTurnActivity(services) {
|
|
|
9906
10696
|
latestCodexUsage = snapshot;
|
|
9907
10697
|
},
|
|
9908
10698
|
// latest wins; flushed once in finally
|
|
10699
|
+
onRequestPreparationDiagnostic: (phase) => {
|
|
10700
|
+
if (firstModelRequestCheckpointAt === null || firstModelRequestCheckpoints.has(phase)) {
|
|
10701
|
+
return;
|
|
10702
|
+
}
|
|
10703
|
+
const now = performance.now();
|
|
10704
|
+
const metricPhase = phase === "transport_entry" ? "model_sdk_serialization" : phase === "credential_ready" ? "model_credential_resolution" : "model_wire_normalization";
|
|
10705
|
+
firstModelRequestCheckpoints.add(phase);
|
|
10706
|
+
recordTurnStartupPhase(observability, {
|
|
10707
|
+
phase: metricPhase,
|
|
10708
|
+
provider: turnExecutionPolicy.providerId,
|
|
10709
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
10710
|
+
outcome: "completed",
|
|
10711
|
+
durationSeconds: (now - firstModelRequestCheckpointAt) / 1e3,
|
|
10712
|
+
count: turnTools.length
|
|
10713
|
+
});
|
|
10714
|
+
firstModelRequestCheckpointAt = now;
|
|
10715
|
+
},
|
|
9909
10716
|
onRequestOpaqueArtifacts: ({ fingerprints }) => {
|
|
9910
10717
|
lastCodexRequestOpaqueArtifacts = fingerprints;
|
|
9911
10718
|
},
|
|
9912
10719
|
onModelRequestDiagnostic: (event) => {
|
|
10720
|
+
if (event.phase === "started" && firstModelRequestPreparationStartedAt !== null && !firstModelRequestPreparationRecorded) {
|
|
10721
|
+
firstModelRequestPreparationRecorded = true;
|
|
10722
|
+
recordTurnStartupPhase(observability, {
|
|
10723
|
+
phase: "model_request_preparation",
|
|
10724
|
+
provider: turnExecutionPolicy.providerId,
|
|
10725
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
10726
|
+
outcome: "completed",
|
|
10727
|
+
durationSeconds: (performance.now() - firstModelRequestPreparationStartedAt) / 1e3,
|
|
10728
|
+
count: turnTools.length
|
|
10729
|
+
});
|
|
10730
|
+
}
|
|
9913
10731
|
const phase = event.phase === "headers" ? "headers" : event.phase === "first_byte" ? "first_byte" : event.phase === "completed" || event.phase === "failed" || event.phase === "timed_out" ? "terminal" : null;
|
|
9914
10732
|
if (!phase) return;
|
|
9915
10733
|
recordModelRequestPhase(observability, {
|
|
@@ -9924,19 +10742,41 @@ function createRunAgentTurnActivity(services) {
|
|
|
9924
10742
|
if (!publish || !turnId) {
|
|
9925
10743
|
throw new Error("Codex model request started before the turn event producer");
|
|
9926
10744
|
}
|
|
9927
|
-
|
|
9928
|
-
|
|
9929
|
-
|
|
9930
|
-
|
|
9931
|
-
|
|
9932
|
-
|
|
9933
|
-
|
|
9934
|
-
|
|
9935
|
-
|
|
9936
|
-
|
|
10745
|
+
const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
|
|
10746
|
+
if (shouldRecordStartedAudit) {
|
|
10747
|
+
firstModelRequestAuditRecorded = true;
|
|
10748
|
+
}
|
|
10749
|
+
const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
|
|
10750
|
+
let auditOutcome = "completed";
|
|
10751
|
+
try {
|
|
10752
|
+
await publish([
|
|
10753
|
+
{
|
|
10754
|
+
type: "agent.model.request",
|
|
10755
|
+
payload: {
|
|
10756
|
+
...event,
|
|
10757
|
+
provider: "codex-subscription",
|
|
10758
|
+
turnId,
|
|
10759
|
+
attemptId: input.attemptId,
|
|
10760
|
+
dispatchId,
|
|
10761
|
+
executionGeneration
|
|
10762
|
+
}
|
|
9937
10763
|
}
|
|
10764
|
+
]);
|
|
10765
|
+
} catch (error) {
|
|
10766
|
+
auditOutcome = "failed";
|
|
10767
|
+
throw error;
|
|
10768
|
+
} finally {
|
|
10769
|
+
if (auditStartedAt !== null) {
|
|
10770
|
+
recordTurnStartupPhase(observability, {
|
|
10771
|
+
phase: "model_request_audit",
|
|
10772
|
+
provider: turnExecutionPolicy.providerId,
|
|
10773
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
10774
|
+
outcome: auditOutcome,
|
|
10775
|
+
durationSeconds: (performance.now() - auditStartedAt) / 1e3,
|
|
10776
|
+
count: turnTools.length
|
|
10777
|
+
});
|
|
9938
10778
|
}
|
|
9939
|
-
|
|
10779
|
+
}
|
|
9940
10780
|
}
|
|
9941
10781
|
};
|
|
9942
10782
|
})() : null;
|
|
@@ -10228,7 +11068,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10228
11068
|
availableMcpServerIds: runSettings.mcpServers.map((server) => server.id),
|
|
10229
11069
|
defaultMcpServerIds: defaultSessionMcpServerIds(capabilitySettings.mcpServers)
|
|
10230
11070
|
});
|
|
10231
|
-
const
|
|
11071
|
+
const mcpAvailabilityNote = unavailableMcpOperationalContext({
|
|
10232
11072
|
droppedIds: resolvedToolPolicy.effectivePolicy.droppedIds,
|
|
10233
11073
|
droppedCount: resolvedToolPolicy.effectivePolicy.counts.dropped
|
|
10234
11074
|
});
|
|
@@ -10343,13 +11183,36 @@ function createRunAgentTurnActivity(services) {
|
|
|
10343
11183
|
cancellationSignal,
|
|
10344
11184
|
void 0
|
|
10345
11185
|
);
|
|
10346
|
-
const
|
|
10347
|
-
|
|
10348
|
-
|
|
10349
|
-
|
|
11186
|
+
const establishDecision = sandboxEstablishPolicyDecision({
|
|
11187
|
+
lazyEnabled: lazyProvisionEnabled(settings),
|
|
11188
|
+
machinePrimary,
|
|
11189
|
+
sandboxBackend: runSettings.sandboxBackend,
|
|
11190
|
+
// Resolved run credentials must be written to one exact leased sandbox
|
|
11191
|
+
// before agent execution. A warm active pointer can bypass the lazy
|
|
11192
|
+
// provisioner entirely, which previously skipped materialization.
|
|
11193
|
+
hasInitialRunCredentialMaterial: initialRunCredentialMaterial !== null,
|
|
11194
|
+
generatedVideoFileCount: requiredGeneratedVideoFiles.length,
|
|
11195
|
+
hasSignedFileResources: requiresSignedFileResourceDownloads(
|
|
11196
|
+
runSettings,
|
|
11197
|
+
activeSandboxBackend ?? groupBoxBackend
|
|
11198
|
+
) && turnResources.some((resource) => resource.kind === "file")
|
|
11199
|
+
});
|
|
11200
|
+
const establishPolicy = establishDecision.policy;
|
|
11201
|
+
recordTurnSandboxEstablishPolicy(observability, {
|
|
11202
|
+
policy: establishDecision.policy,
|
|
11203
|
+
reason: establishDecision.reason,
|
|
11204
|
+
backend: machinePrimary ? "selfhosted" : groupBoxBackend
|
|
11205
|
+
});
|
|
10350
11206
|
if (activeSandboxBackend !== "selfhosted") {
|
|
10351
11207
|
await assertGitHubResourcesRemainAuthorized(db, input.workspaceId, turnResources);
|
|
10352
11208
|
}
|
|
11209
|
+
await assertFileResourcesRemainAuthorized(
|
|
11210
|
+
db,
|
|
11211
|
+
input.accountId,
|
|
11212
|
+
input.workspaceId,
|
|
11213
|
+
fileAuthoritySubjectId,
|
|
11214
|
+
turnResources
|
|
11215
|
+
);
|
|
10353
11216
|
const authorizeGitHubTokenMint = async (selection) => {
|
|
10354
11217
|
await assertGitHubTokenMintSelectionAuthorized(
|
|
10355
11218
|
db,
|
|
@@ -10702,195 +11565,237 @@ function createRunAgentTurnActivity(services) {
|
|
|
10702
11565
|
}
|
|
10703
11566
|
runCredentialRenewal = controller;
|
|
10704
11567
|
};
|
|
11568
|
+
recordTurnStartupPhase(observability, {
|
|
11569
|
+
phase: "runtime_preparation",
|
|
11570
|
+
provider: turnExecutionPolicy.providerId,
|
|
11571
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
11572
|
+
outcome: "completed",
|
|
11573
|
+
durationSeconds: (performance.now() - runtimePreparationStartedAt) / 1e3
|
|
11574
|
+
});
|
|
10705
11575
|
if (shouldEstablishSandboxForTurn(
|
|
10706
11576
|
settings.sandboxOwnershipEnabled,
|
|
10707
11577
|
turn.sandboxBackend,
|
|
10708
11578
|
machinePrimary
|
|
10709
11579
|
)) {
|
|
10710
|
-
const
|
|
10711
|
-
|
|
10712
|
-
|
|
10713
|
-
|
|
10714
|
-
|
|
10715
|
-
|
|
10716
|
-
|
|
10717
|
-
if (machinePrimary) {
|
|
10718
|
-
const machineOpStream = settings.agentOpStreamEnabled === true ? (await getEnrollment(db, input.workspaceId, activeSandboxRecord.enrollmentId))?.opStream === true : false;
|
|
10719
|
-
const established = await establishSelfhostedTurnSession(
|
|
10720
|
-
{
|
|
10721
|
-
db,
|
|
10722
|
-
settings,
|
|
10723
|
-
bus,
|
|
10724
|
-
onOp: machineOpObserver.observer,
|
|
10725
|
-
onSandboxOperation: sandboxOperationObserver,
|
|
10726
|
-
opJournal
|
|
10727
|
-
},
|
|
10728
|
-
{
|
|
10729
|
-
workspaceId: input.workspaceId,
|
|
10730
|
-
agentId: activeSandboxRecord.enrollmentId,
|
|
10731
|
-
opStream: machineOpStream,
|
|
10732
|
-
epoch: activeSandboxPointer.activeEpoch,
|
|
10733
|
-
environment: sandboxEnvironment,
|
|
10734
|
-
...transientCodemodeEnvironment ? { transientExecEnvironment: transientCodemodeEnvironment } : {},
|
|
10735
|
-
workingDir: activeSandboxPointer.workingDir
|
|
10736
|
-
}
|
|
11580
|
+
const sandboxEstablishStartedAt = performance.now();
|
|
11581
|
+
let sandboxEstablishOutcome = "completed";
|
|
11582
|
+
try {
|
|
11583
|
+
const managedOwnership = managedSandboxOwnershipForTurn(
|
|
11584
|
+
machinePrimary,
|
|
11585
|
+
input.attemptId,
|
|
11586
|
+
session.sandboxGroupId
|
|
10737
11587
|
);
|
|
10738
|
-
|
|
10739
|
-
|
|
10740
|
-
|
|
10741
|
-
|
|
10742
|
-
|
|
10743
|
-
// SelfhostedSession for the machine pointer so the turn-start manifest
|
|
10744
|
-
// write (via the proxy's `state` getter) and the per-op reads hit ONE
|
|
10745
|
-
// instance — no two-instance manifest divergence.
|
|
10746
|
-
established: wrapTurnBoxWithRouting(
|
|
11588
|
+
sandboxHolderId = managedOwnership?.holderId ?? null;
|
|
11589
|
+
sandboxGroupId = managedOwnership?.sandboxGroupId ?? null;
|
|
11590
|
+
if (machinePrimary) {
|
|
11591
|
+
const machineOpStream = settings.agentOpStreamEnabled === true ? (await getEnrollment(db, input.workspaceId, activeSandboxRecord.enrollmentId))?.opStream === true : false;
|
|
11592
|
+
const established = await establishSelfhostedTurnSession(
|
|
10747
11593
|
{
|
|
10748
11594
|
db,
|
|
10749
11595
|
settings,
|
|
10750
11596
|
bus,
|
|
10751
|
-
opJournal,
|
|
10752
11597
|
onOp: machineOpObserver.observer,
|
|
10753
11598
|
onSandboxOperation: sandboxOperationObserver,
|
|
10754
|
-
|
|
10755
|
-
...runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}
|
|
11599
|
+
opJournal
|
|
10756
11600
|
},
|
|
10757
11601
|
{
|
|
10758
11602
|
workspaceId: input.workspaceId,
|
|
10759
|
-
|
|
11603
|
+
agentId: activeSandboxRecord.enrollmentId,
|
|
11604
|
+
opStream: machineOpStream,
|
|
11605
|
+
epoch: activeSandboxPointer.activeEpoch,
|
|
10760
11606
|
environment: sandboxEnvironment,
|
|
10761
11607
|
...transientCodemodeEnvironment ? { transientExecEnvironment: transientCodemodeEnvironment } : {},
|
|
10762
|
-
|
|
10763
|
-
|
|
10764
|
-
|
|
10765
|
-
|
|
10766
|
-
|
|
11608
|
+
workingDir: activeSandboxPointer.workingDir
|
|
11609
|
+
}
|
|
11610
|
+
);
|
|
11611
|
+
machinePrimarySession = established.session;
|
|
11612
|
+
setupBoxSession = established.session;
|
|
11613
|
+
resolvedSandbox = {
|
|
11614
|
+
// Wrap in the SAME routing proxy so a mid-turn swap (to another machine
|
|
11615
|
+
// or back to the group box) still re-routes per op. PIN this established
|
|
11616
|
+
// SelfhostedSession for the machine pointer so the turn-start manifest
|
|
11617
|
+
// write (via the proxy's `state` getter) and the per-op reads hit ONE
|
|
11618
|
+
// instance — no two-instance manifest divergence.
|
|
11619
|
+
established: wrapTurnBoxWithRouting(
|
|
11620
|
+
{
|
|
11621
|
+
db,
|
|
11622
|
+
settings,
|
|
11623
|
+
bus,
|
|
11624
|
+
opJournal,
|
|
11625
|
+
onOp: machineOpObserver.observer,
|
|
11626
|
+
onSandboxOperation: sandboxOperationObserver,
|
|
11627
|
+
onHomeSandboxRebound,
|
|
11628
|
+
...runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}
|
|
10767
11629
|
},
|
|
10768
|
-
|
|
10769
|
-
|
|
10770
|
-
|
|
11630
|
+
{
|
|
11631
|
+
workspaceId: input.workspaceId,
|
|
11632
|
+
sessionId: input.sessionId,
|
|
11633
|
+
environment: sandboxEnvironment,
|
|
11634
|
+
...transientCodemodeEnvironment ? { transientExecEnvironment: transientCodemodeEnvironment } : {},
|
|
11635
|
+
workspaceMutationFence: {
|
|
11636
|
+
accountId: input.accountId,
|
|
11637
|
+
turnId: turn.id,
|
|
11638
|
+
executionGeneration,
|
|
11639
|
+
attemptId: input.attemptId
|
|
11640
|
+
},
|
|
11641
|
+
pinnedSelfhosted: {
|
|
11642
|
+
sandboxId: activeSandboxPointer.activeSandboxId,
|
|
11643
|
+
epoch: activeSandboxPointer.activeEpoch
|
|
11644
|
+
},
|
|
11645
|
+
// HOME semantics for a mid-turn clear-to-null: only a genuine
|
|
11646
|
+
// machine-HOME session (its home IS this machine, session.sandboxBackend
|
|
11647
|
+
// === "selfhosted") resolves null back to the pinned machine. A Modal-HOME
|
|
11648
|
+
// session merely PINNED to a machine this turn never established its group
|
|
11649
|
+
// box, so defaultIsHome:false makes a clear-to-null fail typed
|
|
11650
|
+
// (`home_unavailable_this_turn`) rather than silently serving the machine;
|
|
11651
|
+
// the detach takes effect next turn. (Lazy home-box establishment on such a
|
|
11652
|
+
// clear is a deferred follow-up; issue #341.)
|
|
11653
|
+
defaultIsHome: session.sandboxBackend === "selfhosted"
|
|
10771
11654
|
},
|
|
10772
|
-
|
|
10773
|
-
|
|
10774
|
-
|
|
10775
|
-
|
|
10776
|
-
|
|
10777
|
-
|
|
10778
|
-
|
|
10779
|
-
|
|
10780
|
-
|
|
10781
|
-
|
|
10782
|
-
|
|
10783
|
-
|
|
10784
|
-
|
|
10785
|
-
|
|
10786
|
-
|
|
10787
|
-
|
|
10788
|
-
|
|
10789
|
-
|
|
10790
|
-
|
|
10791
|
-
} else {
|
|
10792
|
-
resolvedSandbox = await waitForTurnOperation(
|
|
10793
|
-
resumeBoxForTurn(
|
|
10794
|
-
{
|
|
10795
|
-
db,
|
|
10796
|
-
settings: runSettings,
|
|
10797
|
-
logicalFallbackSettings: logicalSandboxSettings,
|
|
10798
|
-
cancellationSignal: sandboxResumeSignal,
|
|
10799
|
-
sandboxMetrics: runtimeMetricsHooksForObservability(observability),
|
|
10800
|
-
onSandboxLost: publishSandboxLost
|
|
10801
|
-
},
|
|
10802
|
-
{
|
|
10803
|
-
accountId: input.accountId,
|
|
10804
|
-
workspaceId: input.workspaceId,
|
|
10805
|
-
sandboxGroupId: session.sandboxGroupId,
|
|
10806
|
-
sessionId: input.sessionId,
|
|
10807
|
-
// groupBoxBackend, not turn.sandboxBackend: a machine-home turn that
|
|
10808
|
-
// is not machine-primary resumes a real cloud group box (the
|
|
10809
|
-
// deployment default), never a "selfhosted" box (which would throw
|
|
10810
|
-
// for lack of a bound agentId).
|
|
10811
|
-
backend: groupBoxBackend,
|
|
10812
|
-
os: session.sandboxOs,
|
|
10813
|
-
environment: sandboxEnvironment,
|
|
10814
|
-
// IMAGE IS SHARED STATE (B3, Modal warm-box path only): the container image
|
|
10815
|
-
// this run resolves. The lease stamps it + conflicts on a live shared box
|
|
10816
|
-
// running a DIFFERENT image (solo → durable rotation; N-holders →
|
|
10817
|
-
// SandboxImageConflictError surfaced as an actionable turn error). Prefer
|
|
10818
|
-
// the explicit Modal image ref, else the docker image. The selfhosted branch
|
|
10819
|
-
// (establishSelfhostedTurnSession) NEVER passes
|
|
10820
|
-
// an image — B3 lives only on this Modal else-branch.
|
|
10821
|
-
...runSettings.modalImageRef ?? runSettings.dockerImage ? {
|
|
10822
|
-
image: runSettings.modalImageRef ?? runSettings.dockerImage
|
|
10823
|
-
} : {},
|
|
10824
|
-
// RIG IS SHARED STATE (M3): stamp the frozen rig version so the lease
|
|
10825
|
-
// conflicts on a live shared box set up under a different rig (solo
|
|
10826
|
-
// durable rotation / N-holders SandboxRigConflictError). Omitted for a
|
|
10827
|
-
// rig-less turn -> never stamped or enforced (shares exactly as today).
|
|
10828
|
-
...rigVersion ? { rigVersionId: rigVersion.id } : {}
|
|
10829
|
-
},
|
|
10830
|
-
"turn",
|
|
10831
|
-
managedOwnership.holderId
|
|
10832
|
-
),
|
|
10833
|
-
sandboxResumeSignal,
|
|
10834
|
-
releaseLateSandbox
|
|
10835
|
-
);
|
|
10836
|
-
setupBoxSession = resolvedSandbox.established.session;
|
|
10837
|
-
await publishSandboxLifecycleEvents(resolvedSandbox);
|
|
10838
|
-
resolvedSandbox = {
|
|
10839
|
-
...resolvedSandbox,
|
|
10840
|
-
established: wrapTurnBoxWithRouting(
|
|
10841
|
-
{
|
|
10842
|
-
db,
|
|
10843
|
-
settings,
|
|
10844
|
-
bus,
|
|
10845
|
-
opJournal,
|
|
10846
|
-
onSandboxOperation: sandboxOperationObserver,
|
|
10847
|
-
onHomeSandboxLost: publishSandboxLost,
|
|
10848
|
-
onHomeSandboxRebound,
|
|
10849
|
-
...runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}
|
|
10850
|
-
},
|
|
10851
|
-
// Thread the SAME declared environment the group box was created with
|
|
10852
|
-
// (resumeBoxForTurn, above) so a selfhosted swap target's manifest
|
|
10853
|
-
// carries it too — the SDK's per-turn manifest-env delta stays empty
|
|
10854
|
-
// (no "cannot change manifest environment variables" throw).
|
|
10855
|
-
{
|
|
10856
|
-
workspaceId: input.workspaceId,
|
|
10857
|
-
sessionId: input.sessionId,
|
|
10858
|
-
environment: sandboxEnvironment,
|
|
10859
|
-
...transientCodemodeEnvironment ? { transientExecEnvironment: transientCodemodeEnvironment } : {},
|
|
10860
|
-
workspaceMutationFence: {
|
|
10861
|
-
accountId: input.accountId,
|
|
10862
|
-
turnId: turn.id,
|
|
10863
|
-
executionGeneration,
|
|
10864
|
-
attemptId: input.attemptId
|
|
11655
|
+
established
|
|
11656
|
+
),
|
|
11657
|
+
// For a machine route this is the active-pointer epoch, not a cloud
|
|
11658
|
+
// lease epoch. Cloud-only heartbeat/snapshot/capture paths require
|
|
11659
|
+
// sandboxGroupId and remain disabled for this branch.
|
|
11660
|
+
leaseEpoch: activeSandboxPointer.activeEpoch,
|
|
11661
|
+
release: async () => void 0
|
|
11662
|
+
};
|
|
11663
|
+
} else if (establishPolicy === "on-demand") {
|
|
11664
|
+
} else {
|
|
11665
|
+
resolvedSandbox = await waitForTurnOperation(
|
|
11666
|
+
resumeBoxForTurn(
|
|
11667
|
+
{
|
|
11668
|
+
db,
|
|
11669
|
+
settings: runSettings,
|
|
11670
|
+
logicalFallbackSettings: logicalSandboxSettings,
|
|
11671
|
+
cancellationSignal: sandboxResumeSignal,
|
|
11672
|
+
sandboxMetrics: runtimeMetricsHooksForObservability(observability),
|
|
11673
|
+
onSandboxLost: publishSandboxLost
|
|
10865
11674
|
},
|
|
10866
|
-
|
|
11675
|
+
{
|
|
10867
11676
|
accountId: input.accountId,
|
|
11677
|
+
workspaceId: input.workspaceId,
|
|
10868
11678
|
sandboxGroupId: session.sandboxGroupId,
|
|
10869
|
-
|
|
10870
|
-
|
|
10871
|
-
|
|
10872
|
-
|
|
10873
|
-
|
|
10874
|
-
|
|
10875
|
-
|
|
10876
|
-
|
|
10877
|
-
|
|
10878
|
-
|
|
10879
|
-
|
|
11679
|
+
sessionId: input.sessionId,
|
|
11680
|
+
// groupBoxBackend, not turn.sandboxBackend: a machine-home turn that
|
|
11681
|
+
// is not machine-primary resumes a real cloud group box (the
|
|
11682
|
+
// deployment default), never a "selfhosted" box (which would throw
|
|
11683
|
+
// for lack of a bound agentId).
|
|
11684
|
+
backend: groupBoxBackend,
|
|
11685
|
+
os: session.sandboxOs,
|
|
11686
|
+
environment: sandboxEnvironment,
|
|
11687
|
+
// IMAGE IS SHARED STATE (B3, Modal warm-box path only): the container image
|
|
11688
|
+
// this run resolves. The lease stamps it + conflicts on a live shared box
|
|
11689
|
+
// running a DIFFERENT image (solo → durable rotation; N-holders →
|
|
11690
|
+
// SandboxImageConflictError surfaced as an actionable turn error). Prefer
|
|
11691
|
+
// the explicit Modal image ref, else the docker image. The selfhosted branch
|
|
11692
|
+
// (establishSelfhostedTurnSession) NEVER passes
|
|
11693
|
+
// an image — B3 lives only on this Modal else-branch.
|
|
11694
|
+
...runSettings.modalImageRef ?? runSettings.dockerImage ? {
|
|
11695
|
+
image: runSettings.modalImageRef ?? runSettings.dockerImage
|
|
11696
|
+
} : {},
|
|
11697
|
+
// RIG IS SHARED STATE (M3): stamp the frozen rig version so the lease
|
|
11698
|
+
// conflicts on a live shared box set up under a different rig (solo
|
|
11699
|
+
// durable rotation / N-holders SandboxRigConflictError). Omitted for a
|
|
11700
|
+
// rig-less turn -> never stamped or enforced (shares exactly as today).
|
|
11701
|
+
...rigVersion ? { rigVersionId: rigVersion.id } : {}
|
|
11702
|
+
},
|
|
11703
|
+
"turn",
|
|
11704
|
+
managedOwnership.holderId
|
|
11705
|
+
),
|
|
11706
|
+
sandboxResumeSignal,
|
|
11707
|
+
releaseLateSandbox
|
|
11708
|
+
);
|
|
11709
|
+
setupBoxSession = resolvedSandbox.established.session;
|
|
11710
|
+
await publishSandboxLifecycleEvents(resolvedSandbox);
|
|
11711
|
+
resolvedSandbox = {
|
|
11712
|
+
...resolvedSandbox,
|
|
11713
|
+
established: wrapTurnBoxWithRouting(
|
|
11714
|
+
{
|
|
11715
|
+
db,
|
|
11716
|
+
settings,
|
|
11717
|
+
bus,
|
|
11718
|
+
opJournal,
|
|
11719
|
+
onSandboxOperation: sandboxOperationObserver,
|
|
11720
|
+
onHomeSandboxLost: publishSandboxLost,
|
|
11721
|
+
onHomeSandboxRebound,
|
|
11722
|
+
...runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}
|
|
11723
|
+
},
|
|
11724
|
+
// Thread the SAME declared environment the group box was created with
|
|
11725
|
+
// (resumeBoxForTurn, above) so a selfhosted swap target's manifest
|
|
11726
|
+
// carries it too — the SDK's per-turn manifest-env delta stays empty
|
|
11727
|
+
// (no "cannot change manifest environment variables" throw).
|
|
11728
|
+
{
|
|
11729
|
+
workspaceId: input.workspaceId,
|
|
11730
|
+
sessionId: input.sessionId,
|
|
11731
|
+
environment: sandboxEnvironment,
|
|
11732
|
+
...transientCodemodeEnvironment ? { transientExecEnvironment: transientCodemodeEnvironment } : {},
|
|
11733
|
+
workspaceMutationFence: {
|
|
11734
|
+
accountId: input.accountId,
|
|
11735
|
+
turnId: turn.id,
|
|
11736
|
+
executionGeneration,
|
|
11737
|
+
attemptId: input.attemptId
|
|
11738
|
+
},
|
|
11739
|
+
homeLease: {
|
|
11740
|
+
accountId: input.accountId,
|
|
11741
|
+
sandboxGroupId: session.sandboxGroupId,
|
|
11742
|
+
leaseEpoch: resolvedSandbox.leaseEpoch,
|
|
11743
|
+
instanceId: resolvedSandbox.established.instanceId,
|
|
11744
|
+
backend: groupBoxBackend
|
|
11745
|
+
}
|
|
11746
|
+
},
|
|
11747
|
+
resolvedSandbox.established
|
|
11748
|
+
)
|
|
11749
|
+
};
|
|
11750
|
+
}
|
|
11751
|
+
if (resolvedSandbox) {
|
|
11752
|
+
startLeaseHeartbeat(resolvedSandbox, activeSandboxBackend ?? groupBoxBackend);
|
|
11753
|
+
}
|
|
11754
|
+
} catch (error) {
|
|
11755
|
+
sandboxEstablishOutcome = "failed";
|
|
11756
|
+
throw error;
|
|
11757
|
+
} finally {
|
|
11758
|
+
recordTurnStartupPhase(observability, {
|
|
11759
|
+
phase: "sandbox_establish",
|
|
11760
|
+
provider: turnExecutionPolicy.providerId,
|
|
11761
|
+
backend: machinePrimary ? "selfhosted" : groupBoxBackend,
|
|
11762
|
+
outcome: sandboxEstablishOutcome,
|
|
11763
|
+
durationSeconds: (performance.now() - sandboxEstablishStartedAt) / 1e3
|
|
11764
|
+
});
|
|
10880
11765
|
}
|
|
10881
11766
|
}
|
|
10882
|
-
const ordinaryFileResourceDownloads = await
|
|
10883
|
-
|
|
10884
|
-
|
|
10885
|
-
|
|
10886
|
-
|
|
10887
|
-
|
|
10888
|
-
|
|
10889
|
-
|
|
10890
|
-
|
|
10891
|
-
|
|
10892
|
-
|
|
10893
|
-
|
|
11767
|
+
const ordinaryFileResourceDownloads = await (async () => {
|
|
11768
|
+
const fileResolutionStartedAt = performance.now();
|
|
11769
|
+
let fileResolutionOutcome = "completed";
|
|
11770
|
+
try {
|
|
11771
|
+
return await waitForTurnOperation(
|
|
11772
|
+
sandboxFileDownloadsForRun(
|
|
11773
|
+
runSettings,
|
|
11774
|
+
db,
|
|
11775
|
+
objectStorage,
|
|
11776
|
+
input.accountId,
|
|
11777
|
+
input.workspaceId,
|
|
11778
|
+
fileAuthoritySubjectId,
|
|
11779
|
+
turnResources,
|
|
11780
|
+
activeSandboxBackend ?? groupBoxBackend
|
|
11781
|
+
),
|
|
11782
|
+
cancellationSignal,
|
|
11783
|
+
void 0
|
|
11784
|
+
);
|
|
11785
|
+
} catch (error) {
|
|
11786
|
+
fileResolutionOutcome = "failed";
|
|
11787
|
+
throw error;
|
|
11788
|
+
} finally {
|
|
11789
|
+
recordTurnStartupPhase(observability, {
|
|
11790
|
+
phase: "file_resolution",
|
|
11791
|
+
provider: turnExecutionPolicy.providerId,
|
|
11792
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
11793
|
+
outcome: fileResolutionOutcome,
|
|
11794
|
+
durationSeconds: (performance.now() - fileResolutionStartedAt) / 1e3,
|
|
11795
|
+
count: turnResources.length
|
|
11796
|
+
});
|
|
11797
|
+
}
|
|
11798
|
+
})();
|
|
10894
11799
|
if (requiredGeneratedVideoFiles.length > 0 && !objectStorage) {
|
|
10895
11800
|
throw new Error("Generated video materialization requires object storage");
|
|
10896
11801
|
}
|
|
@@ -10920,6 +11825,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10920
11825
|
])
|
|
10921
11826
|
).values()
|
|
10922
11827
|
];
|
|
11828
|
+
const toolContextPreparationStartedAt = performance.now();
|
|
10923
11829
|
throwIfWorkerShuttingDown();
|
|
10924
11830
|
throwIfTurnCancelled();
|
|
10925
11831
|
const mcpCredentialRootSessionId = connectionCredentials?.mcpCredentials && hostCredentialRootSessionId ? hostCredentialRootSessionId : input.sessionId;
|
|
@@ -10939,7 +11845,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
10939
11845
|
const delegatedOwnerHasMembership = async (subjectId) => {
|
|
10940
11846
|
const existing = delegatedMembershipChecks.get(subjectId);
|
|
10941
11847
|
if (existing) return await existing;
|
|
10942
|
-
const check =
|
|
11848
|
+
const check = getWorkspaceGrant2(db, subjectId, input.workspaceId).then(Boolean);
|
|
10943
11849
|
delegatedMembershipChecks.set(subjectId, check);
|
|
10944
11850
|
return await check;
|
|
10945
11851
|
};
|
|
@@ -10955,6 +11861,32 @@ function createRunAgentTurnActivity(services) {
|
|
|
10955
11861
|
}
|
|
10956
11862
|
return result;
|
|
10957
11863
|
};
|
|
11864
|
+
const connectorActionIdentity = {
|
|
11865
|
+
accountId: input.accountId,
|
|
11866
|
+
workspaceId: input.workspaceId,
|
|
11867
|
+
sessionId: input.sessionId,
|
|
11868
|
+
turnId: turn.id,
|
|
11869
|
+
attemptId: input.attemptId,
|
|
11870
|
+
executionGeneration,
|
|
11871
|
+
initiator: {
|
|
11872
|
+
kind: turn.initiator.kind,
|
|
11873
|
+
subjectId: turn.initiator.subjectId
|
|
11874
|
+
}
|
|
11875
|
+
};
|
|
11876
|
+
const googleDrivePublicationTarget = objectStorage ? await resolveGoogleDrivePublicationTarget(
|
|
11877
|
+
db,
|
|
11878
|
+
input.workspaceId,
|
|
11879
|
+
personalConnectionDelegations
|
|
11880
|
+
) : null;
|
|
11881
|
+
const googleDrivePublicationTool = objectStorage && googleDrivePublicationTarget ? createGoogleDrivePublicationAttemptTool({
|
|
11882
|
+
db,
|
|
11883
|
+
objectStorage,
|
|
11884
|
+
identity: connectorActionIdentity,
|
|
11885
|
+
subjectId: googleDrivePublicationTarget.ownerSubjectId,
|
|
11886
|
+
target: googleDrivePublicationTarget,
|
|
11887
|
+
resolveCredential,
|
|
11888
|
+
...runtimeCancellationSignal ? { signal: runtimeCancellationSignal } : {}
|
|
11889
|
+
}) : null;
|
|
10958
11890
|
const publishToolAuthNeeded = async (payload) => {
|
|
10959
11891
|
if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
|
|
10960
11892
|
return;
|
|
@@ -11005,53 +11937,117 @@ function createRunAgentTurnActivity(services) {
|
|
|
11005
11937
|
runSettings,
|
|
11006
11938
|
session.firstPartyMcpTools
|
|
11007
11939
|
);
|
|
11008
|
-
|
|
11009
|
-
|
|
11010
|
-
|
|
11011
|
-
|
|
11012
|
-
|
|
11013
|
-
|
|
11014
|
-
|
|
11015
|
-
|
|
11016
|
-
|
|
11017
|
-
|
|
11018
|
-
|
|
11940
|
+
const googleDrivePublicationAllowed = selectedFirstPartyMcpTools.includes("editable_artifact_export") && selectedFirstPartyMcpTools.includes("editable_artifact_export_status") && (!session.firstPartyMcpPermissions?.length || session.firstPartyMcpPermissions.includes("artifacts:read") && session.firstPartyMcpPermissions.includes("artifacts:publish"));
|
|
11941
|
+
const googleDriveConnectorBindings = googleDrivePublicationTool && googleDrivePublicationTarget && googleDrivePublicationAllowed ? [
|
|
11942
|
+
{
|
|
11943
|
+
modelName: googleDrivePublicationTool.modelName,
|
|
11944
|
+
call: (approvalId, arguments_) => googleDrivePublicationConnectorCall(
|
|
11945
|
+
googleDrivePublicationTarget,
|
|
11946
|
+
arguments_,
|
|
11947
|
+
approvalId
|
|
11948
|
+
)
|
|
11949
|
+
}
|
|
11950
|
+
] : [];
|
|
11951
|
+
const attemptToolDefinitions = [
|
|
11952
|
+
...createFirstPartyInteractionAttemptToolDefinitions({
|
|
11953
|
+
settings: runSettings,
|
|
11954
|
+
scope: {
|
|
11955
|
+
accountId: input.accountId,
|
|
11956
|
+
workspaceId: input.workspaceId,
|
|
11957
|
+
sessionId: input.sessionId,
|
|
11958
|
+
turnId: turn.id,
|
|
11959
|
+
attemptId: input.attemptId,
|
|
11960
|
+
executionGeneration
|
|
11961
|
+
},
|
|
11962
|
+
...session.firstPartyMcpPermissions?.length ? { permissions: session.firstPartyMcpPermissions } : {},
|
|
11963
|
+
selectedTools: selectedFirstPartyMcpTools,
|
|
11019
11964
|
subjectId: "worker:first-party-mcp",
|
|
11020
11965
|
subjectLabel: "OpenGeni worker",
|
|
11021
|
-
...
|
|
11022
|
-
|
|
11023
|
-
|
|
11024
|
-
|
|
11025
|
-
|
|
11026
|
-
|
|
11027
|
-
|
|
11028
|
-
|
|
11029
|
-
|
|
11030
|
-
|
|
11031
|
-
|
|
11032
|
-
|
|
11033
|
-
|
|
11034
|
-
|
|
11035
|
-
|
|
11036
|
-
|
|
11037
|
-
|
|
11038
|
-
|
|
11039
|
-
|
|
11040
|
-
|
|
11041
|
-
|
|
11042
|
-
|
|
11043
|
-
|
|
11044
|
-
},
|
|
11045
|
-
|
|
11046
|
-
|
|
11966
|
+
...interactionInterventionResume ? { interventionResume: interactionInterventionResume } : {}
|
|
11967
|
+
}),
|
|
11968
|
+
...googleDrivePublicationTool && googleDrivePublicationAllowed ? [googleDrivePublicationTool] : []
|
|
11969
|
+
];
|
|
11970
|
+
recordTurnStartupPhase(observability, {
|
|
11971
|
+
phase: "tool_context_preparation",
|
|
11972
|
+
provider: turnExecutionPolicy.providerId,
|
|
11973
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
11974
|
+
outcome: "completed",
|
|
11975
|
+
durationSeconds: (performance.now() - toolContextPreparationStartedAt) / 1e3,
|
|
11976
|
+
count: turnTools.length
|
|
11977
|
+
});
|
|
11978
|
+
const toolPreparationStartedAt = performance.now();
|
|
11979
|
+
let toolPreparationOutcome = "completed";
|
|
11980
|
+
try {
|
|
11981
|
+
preparedTools = await waitForTurnOperation(
|
|
11982
|
+
runtime.prepareTools(runSettings, turnTools, {
|
|
11983
|
+
accountId: input.accountId,
|
|
11984
|
+
workspaceId: input.workspaceId,
|
|
11985
|
+
sessionId: input.sessionId,
|
|
11986
|
+
// Sign the calling turn into the first-party token so tools classify
|
|
11987
|
+
// the caller by its own identity (sacred-pause guard), not the racy
|
|
11988
|
+
// live active pointer.
|
|
11989
|
+
...turnId ? { turnId } : {},
|
|
11990
|
+
attemptId: input.attemptId,
|
|
11991
|
+
executionGeneration,
|
|
11047
11992
|
subjectId: "worker:first-party-mcp",
|
|
11048
11993
|
subjectLabel: "OpenGeni worker",
|
|
11049
|
-
...
|
|
11050
|
-
|
|
11051
|
-
|
|
11052
|
-
|
|
11053
|
-
|
|
11054
|
-
|
|
11994
|
+
...credentialSubjectId ? { credentialSubjectId } : {},
|
|
11995
|
+
...codexAppsAuth ? { codexAppsAuth } : {},
|
|
11996
|
+
resolveCredential,
|
|
11997
|
+
onAuthNeeded: publishToolAuthNeeded,
|
|
11998
|
+
localMcpServers,
|
|
11999
|
+
onPreparationPhase: (measurement) => {
|
|
12000
|
+
recordTurnStartupPhase(observability, {
|
|
12001
|
+
phase: `tool_${measurement.phase}`,
|
|
12002
|
+
provider: turnExecutionPolicy.providerId,
|
|
12003
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
12004
|
+
outcome: measurement.outcome,
|
|
12005
|
+
durationSeconds: measurement.durationSeconds,
|
|
12006
|
+
count: turnTools.length
|
|
12007
|
+
});
|
|
12008
|
+
},
|
|
12009
|
+
onAttemptToolCatalog: async (catalog) => {
|
|
12010
|
+
await persistAttemptToolCatalog(db, catalog);
|
|
12011
|
+
},
|
|
12012
|
+
// Manager-style sessions carry a creation-validated permission set
|
|
12013
|
+
// for their first-party MCP token; null keeps the fixed default.
|
|
12014
|
+
...session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {},
|
|
12015
|
+
firstPartyTools: selectedFirstPartyMcpTools,
|
|
12016
|
+
nestedAgentDepth: session.nestedAgentDepth,
|
|
12017
|
+
effectiveMaxNestedAgentDepth: session.effectiveMaxNestedAgentDepth,
|
|
12018
|
+
attemptToolDefinitions,
|
|
12019
|
+
...googleDrivePublicationTarget && googleDrivePublicationAllowed ? {
|
|
12020
|
+
attemptToolAuthorize: async ({ call }) => {
|
|
12021
|
+
if (call.caller.kind !== "codemode" || call.identity.serverId !== "google-drive-publishing" || call.identity.toolName !== "google_drive_publish_file") {
|
|
12022
|
+
return;
|
|
12023
|
+
}
|
|
12024
|
+
await authorizeGoogleDrivePublicationAttempt({
|
|
12025
|
+
db,
|
|
12026
|
+
identity: connectorActionIdentity,
|
|
12027
|
+
target: googleDrivePublicationTarget,
|
|
12028
|
+
approvalId: call.operationId,
|
|
12029
|
+
arguments: call.arguments
|
|
12030
|
+
});
|
|
12031
|
+
}
|
|
12032
|
+
} : {}
|
|
12033
|
+
}),
|
|
12034
|
+
cancellationSignal,
|
|
12035
|
+
async (latePreparedTools) => await latePreparedTools.close().catch(() => void 0)
|
|
12036
|
+
);
|
|
12037
|
+
} catch (error) {
|
|
12038
|
+
toolPreparationOutcome = "failed";
|
|
12039
|
+
throw error;
|
|
12040
|
+
} finally {
|
|
12041
|
+
recordTurnStartupPhase(observability, {
|
|
12042
|
+
phase: "tool_preparation",
|
|
12043
|
+
provider: turnExecutionPolicy.providerId,
|
|
12044
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
12045
|
+
outcome: toolPreparationOutcome,
|
|
12046
|
+
durationSeconds: (performance.now() - toolPreparationStartedAt) / 1e3,
|
|
12047
|
+
count: turnTools.length
|
|
12048
|
+
});
|
|
12049
|
+
}
|
|
12050
|
+
const postToolPreparationStartedAt = performance.now();
|
|
11055
12051
|
if (turnId && preparedTools.attemptToolEnvironment) {
|
|
11056
12052
|
codemodeDispatcher = new CodemodeAttemptDispatcher(
|
|
11057
12053
|
db,
|
|
@@ -11081,7 +12077,9 @@ function createRunAgentTurnActivity(services) {
|
|
|
11081
12077
|
const resolveImageReferences = async (references) => await resolveImageGenerationReferences({
|
|
11082
12078
|
db,
|
|
11083
12079
|
objectStorage,
|
|
12080
|
+
accountId: input.accountId,
|
|
11084
12081
|
workspaceId: input.workspaceId,
|
|
12082
|
+
subjectId: fileAuthoritySubjectId,
|
|
11085
12083
|
references,
|
|
11086
12084
|
readSandboxFile: async (path, maxBytes) => {
|
|
11087
12085
|
const imageReferenceSession = setupBoxSession ?? sdkOwnedSandboxSession;
|
|
@@ -11359,22 +12357,10 @@ function createRunAgentTurnActivity(services) {
|
|
|
11359
12357
|
turnExecutionPolicy.providerId,
|
|
11360
12358
|
turnExecutionPolicy.latencyMode
|
|
11361
12359
|
);
|
|
11362
|
-
const connectorActionIdentity = {
|
|
11363
|
-
accountId: input.accountId,
|
|
11364
|
-
workspaceId: input.workspaceId,
|
|
11365
|
-
sessionId: input.sessionId,
|
|
11366
|
-
turnId,
|
|
11367
|
-
attemptId: input.attemptId,
|
|
11368
|
-
executionGeneration,
|
|
11369
|
-
initiator: {
|
|
11370
|
-
kind: turn.initiator.kind,
|
|
11371
|
-
subjectId: turn.initiator.subjectId
|
|
11372
|
-
}
|
|
11373
|
-
};
|
|
11374
12360
|
const connectorActionPolicy = {
|
|
11375
|
-
prepare: async (call) => await
|
|
11376
|
-
begin: async (call) => await
|
|
11377
|
-
complete: async ({ requestId, outcome }) => await
|
|
12361
|
+
prepare: async (call) => await prepareConnectorActionApproval2(db, connectorActionIdentity, call),
|
|
12362
|
+
begin: async (call) => await beginConnectorActionExecution2(db, connectorActionIdentity, call),
|
|
12363
|
+
complete: async ({ requestId, outcome }) => await completeConnectorActionExecution2(db, {
|
|
11378
12364
|
accountId: input.accountId,
|
|
11379
12365
|
workspaceId: input.workspaceId,
|
|
11380
12366
|
requestId,
|
|
@@ -11399,146 +12385,167 @@ function createRunAgentTurnActivity(services) {
|
|
|
11399
12385
|
reason: "attached to session"
|
|
11400
12386
|
}))
|
|
11401
12387
|
];
|
|
11402
|
-
|
|
11403
|
-
|
|
11404
|
-
|
|
11405
|
-
|
|
11406
|
-
|
|
11407
|
-
|
|
11408
|
-
genesisTitleHint: isGenesisTurn,
|
|
11409
|
-
persistentSessionSettings: {
|
|
11410
|
-
titleIsSet: Boolean(session.title?.trim())
|
|
11411
|
-
},
|
|
11412
|
-
sandboxEnvironment,
|
|
11413
|
-
...preparedTools.attemptToolCatalog ? { attemptToolCatalog: preparedTools.attemptToolCatalog } : {},
|
|
11414
|
-
...sandboxArtifactRuntime.available ? { artifactRuntimeAvailable: true } : {},
|
|
11415
|
-
...cancellationSignal ? { turnCancellationSignal: cancellationSignal } : {},
|
|
11416
|
-
onToolCancellationFence: (fence) => {
|
|
11417
|
-
toolCancellationFenceRef.current = fence;
|
|
11418
|
-
},
|
|
11419
|
-
// TOKEN-BROKER (B1): forward the per-turn git token OFF-MANIFEST as the clone
|
|
11420
|
-
// seed. ONLY when the effective backend is NOT selfhosted (the connected
|
|
11421
|
-
// machine uses its own git creds — mirrors the skipGitHubToken gate above)
|
|
11422
|
-
// AND the mint actually produced a token (repo resources present). The runtime
|
|
11423
|
-
// seeds it to the box's token file before the repository-clone runs; it never
|
|
11424
|
-
// touches the box/agent manifest env.
|
|
11425
|
-
...activeSandboxBackend !== "selfhosted" && sandboxGitTokens ? { gitTokenSeeds: sandboxGitTokens } : {},
|
|
11426
|
-
...activeSandboxBackend !== "selfhosted" && sandboxGitCredentialBindings ? { gitCredentialBindings: sandboxGitCredentialBindings } : {},
|
|
11427
|
-
...activeSandboxBackend !== "selfhosted" && !sandboxGitTokens && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {},
|
|
11428
|
-
...sandboxCodemodeToken ? { codemodeAvailable: true } : {},
|
|
11429
|
-
// Managed boxes receive the bearer through their protected per-session
|
|
11430
|
-
// token file. Connected Machines use transient per-exec delivery above,
|
|
11431
|
-
// so they must not run the file-seeding lifecycle hook.
|
|
11432
|
-
...activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken ? {
|
|
11433
|
-
codemodeTokenSeed: sandboxCodemodeToken,
|
|
11434
|
-
codemodeTokenSessionId: input.sessionId
|
|
11435
|
-
} : {},
|
|
11436
|
-
...activeSandboxBackend ? { activeSandboxBackend } : {},
|
|
11437
|
-
fileResourceDownloads,
|
|
11438
|
-
mcpServers: preparedTools.mcpServers,
|
|
11439
|
-
resolvedMcpConnectionIds: preparedTools.resolvedMcpConnectionIds,
|
|
11440
|
-
connectorActionPolicy,
|
|
11441
|
-
// LIVE by-reference connector namespaces (fills during this turn's
|
|
11442
|
-
// codex_apps tools/list): the codex tool_search description reads it per
|
|
11443
|
-
// model call so the model sees the account's real connected sources.
|
|
11444
|
-
codexConnectorNamespaces: preparedTools.codexConnectorNamespaces,
|
|
11445
|
-
// Resolved-model routing + gating (legacy defaults when null). The model
|
|
11446
|
-
// is passed as the model *string* (agent.model = runSettings.openaiModel),
|
|
11447
|
-
// NOT a Model instance: an instance only survives the in-process
|
|
11448
|
-
// ("none") run, whereas the SandboxAgent/Modal path drops it and
|
|
11449
|
-
// re-resolves the model *name* through the global MultiProviderModelProvider
|
|
11450
|
-
// configureOpenAI installed — so registry models (Fireworks GLM) route to
|
|
11451
|
-
// their own client instead of 404ing against the built-in Azure/OpenAI
|
|
11452
|
-
// client. The gating still comes from the resolved provider: server-side
|
|
11453
|
-
// store/compaction follow the provider's compaction mode (registry
|
|
11454
|
-
// providers resolve to "client"); encrypted reasoning is only
|
|
11455
|
-
// round-tripped on the Responses wire API; hosted web search is attached
|
|
11456
|
-
// whenever the provider declares it runnable and is independent of the
|
|
11457
|
-
// session's MCP allow-list; the effective context window drives the
|
|
11458
|
-
// compaction threshold.
|
|
11459
|
-
hostedWebSearch,
|
|
11460
|
-
...imageGenerationOption,
|
|
11461
|
-
...videoGenerationOption,
|
|
11462
|
-
lazyToolTransport,
|
|
11463
|
-
supportsImageInput,
|
|
11464
|
-
inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
|
|
11465
|
-
...resolvedModel ? {
|
|
11466
|
-
encryptedReasoning: resolvedModel.provider.api === "responses" && runSettings.openaiReasoningEncryptedContent,
|
|
11467
|
-
contextWindowTokens: resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
|
|
11468
|
-
// The ChatGPT/Codex backend rejects the SDK's HOSTED apply_patch
|
|
11469
|
-
// tool. Gateway Responses routes likewise expose ordinary function
|
|
11470
|
-
// tools, not OpenAI-hosted sandbox tools. Tell buildAgent to use
|
|
11471
|
-
// function apply_patch and wrap successful view_image results as
|
|
11472
|
-
// typed input_image content. Chat wires have no proven typed image
|
|
11473
|
-
// result transport and therefore receive no view_image tool.
|
|
11474
|
-
structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
|
|
11475
|
-
// EXPLICIT computer-use tool transport, derived from the resolved provider's
|
|
11476
|
-
// authoritative wire identity (codex → function-image, chat → disabled,
|
|
11477
|
-
// responses → hosted) so the runtime never string-sniffs the model instance's
|
|
11478
|
-
// constructor name. See {@link computerToolModeForTurn}.
|
|
11479
|
-
computerToolMode: computerToolModeForTurn(resolvedModel),
|
|
11480
|
-
...promptCacheKey ? { promptCacheKey } : {}
|
|
11481
|
-
} : (
|
|
11482
|
-
// LEGACY global-client fallback (resolveTurnModel returned null → the model
|
|
11483
|
-
// is not in the registry, served by the built-in OpenAI/Azure Responses
|
|
11484
|
-
// client). That backend has real hosted support, so pin computerToolMode to
|
|
11485
|
-
// "hosted" EXPLICITLY rather than leaving the runtime to sniff the instance.
|
|
11486
|
-
{
|
|
11487
|
-
computerToolMode: computerToolModeForTurn(null),
|
|
11488
|
-
promptCacheKey: input.sessionId
|
|
11489
|
-
}
|
|
11490
|
-
),
|
|
11491
|
-
// Lazy computer-use seam: runtime first brings up :0 only after the model
|
|
11492
|
-
// selects a computer tool, then this hook begins the optional proof
|
|
11493
|
-
// recording. Shell/filesystem turns never invoke either operation.
|
|
11494
|
-
onComputerUseReady: async () => {
|
|
11495
|
-
if (!resolvedSandbox) {
|
|
11496
|
-
throw new Error("Computer-use display became ready without a resolved sandbox");
|
|
11497
|
-
}
|
|
11498
|
-
didComputerUse = true;
|
|
11499
|
-
await maybeStartOnTurnRecording(resolvedSandbox, activeSandboxBackend);
|
|
11500
|
-
},
|
|
11501
|
-
onRetainableSessionImageOutput: retainSessionImageAtToolBoundary,
|
|
11502
|
-
...runtimeSkillActivations.length > 0 ? { skillActivations: runtimeSkillActivations } : {},
|
|
11503
|
-
...!structuredWorkspacePolicyActive && workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
|
|
11504
|
-
...workspaceGovernance ? { workspaceGovernance } : {},
|
|
11505
|
-
...workspaceMemory ? { workspaceMemory } : {},
|
|
11506
|
-
goalSnapshot: turn.goalSnapshot,
|
|
11507
|
-
// Per-session persona tier (session > workspace > deployment default).
|
|
11508
|
-
// Composed system-level AFTER the workspace persona so it refines it for
|
|
11509
|
-
// this one session; absent ⇒ byte-identical to today's composition.
|
|
11510
|
-
...session.instructions ? { sessionInstructions: session.instructions } : {},
|
|
11511
|
-
// Exact host context captured when this turn was accepted. It is
|
|
11512
|
-
// system-level and disappears with the turn rather than entering chat.
|
|
11513
|
-
...turn.turnInstructions || mcpAvailabilityInstructions ? {
|
|
11514
|
-
turnInstructions: [turn.turnInstructions, mcpAvailabilityInstructions].filter((value) => Boolean(value)).join(" ")
|
|
11515
|
-
} : {},
|
|
11516
|
-
...workspaceEnvironmentOption,
|
|
11517
|
-
// RIG RUNTIME (M3): the doctrine block, the setup-script hook (only when
|
|
11518
|
-
// the frozen version carries a non-empty script), and the rig credential
|
|
11519
|
-
// hooks. All absent for a rig-less turn (byte-for-byte today).
|
|
11520
|
-
...rigVersion && rigName ? {
|
|
11521
|
-
rig: { name: rigName, version: rigVersion.version },
|
|
11522
|
-
...rigVersion.setupScript && rigVersion.setupScript.trim().length > 0 ? {
|
|
11523
|
-
rigSetup: {
|
|
11524
|
-
rigId: session.rigId,
|
|
11525
|
-
versionId: rigVersion.id,
|
|
11526
|
-
rigName,
|
|
11527
|
-
script: rigVersion.setupScript,
|
|
11528
|
-
timeoutMs: runSettings.rigSetupTimeoutMs,
|
|
11529
|
-
contentHash: rigProviderImageContentHash({
|
|
11530
|
-
backend: turn.sandboxBackend,
|
|
11531
|
-
sourceImage: rigProviderImageSourceImage(
|
|
11532
|
-
logicalSandboxSettings,
|
|
11533
|
-
turn.sandboxBackend
|
|
11534
|
-
),
|
|
11535
|
-
definition: rigVersion
|
|
11536
|
-
})
|
|
11537
|
-
}
|
|
11538
|
-
} : {},
|
|
11539
|
-
...rigVersion.credentialHooks.length > 0 ? { rigCredentialHookIds: rigVersion.credentialHooks } : {}
|
|
11540
|
-
} : {}
|
|
12388
|
+
recordTurnStartupPhase(observability, {
|
|
12389
|
+
phase: "post_tool_preparation",
|
|
12390
|
+
provider: turnExecutionPolicy.providerId,
|
|
12391
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
12392
|
+
outcome: "completed",
|
|
12393
|
+
durationSeconds: (performance.now() - postToolPreparationStartedAt) / 1e3
|
|
11541
12394
|
});
|
|
12395
|
+
const agent = (() => {
|
|
12396
|
+
const agentConstructionStartedAt = performance.now();
|
|
12397
|
+
let agentConstructionOutcome = "completed";
|
|
12398
|
+
try {
|
|
12399
|
+
return runtime.buildAgent(modelRunSettings, turnResources, {
|
|
12400
|
+
reasoningEffort: turn.reasoningEffort,
|
|
12401
|
+
latencyMode: turnExecutionPolicy.latencyMode,
|
|
12402
|
+
...serviceTier ? { serviceTier } : {},
|
|
12403
|
+
...humanInputResume ? { humanInputResponse: humanInputResume } : {},
|
|
12404
|
+
humanInputEnabled: agentHumanInputEnabled,
|
|
12405
|
+
genesisTitleHint: isGenesisTurn,
|
|
12406
|
+
persistentSessionSettings: {
|
|
12407
|
+
titleIsSet: Boolean(session.title?.trim())
|
|
12408
|
+
},
|
|
12409
|
+
sandboxEnvironment,
|
|
12410
|
+
...preparedTools.attemptToolCatalog ? { attemptToolCatalog: preparedTools.attemptToolCatalog } : {},
|
|
12411
|
+
...sandboxArtifactRuntime.available ? { artifactRuntimeAvailable: true } : {},
|
|
12412
|
+
...cancellationSignal ? { turnCancellationSignal: cancellationSignal } : {},
|
|
12413
|
+
onToolCancellationFence: (fence) => {
|
|
12414
|
+
toolCancellationFenceRef.current = fence;
|
|
12415
|
+
},
|
|
12416
|
+
// TOKEN-BROKER (B1): forward the per-turn git token OFF-MANIFEST as the clone
|
|
12417
|
+
// seed. ONLY when the effective backend is NOT selfhosted (the connected
|
|
12418
|
+
// machine uses its own git creds — mirrors the skipGitHubToken gate above)
|
|
12419
|
+
// AND the mint actually produced a token (repo resources present). The runtime
|
|
12420
|
+
// seeds it to the box's token file before the repository-clone runs; it never
|
|
12421
|
+
// touches the box/agent manifest env.
|
|
12422
|
+
...activeSandboxBackend !== "selfhosted" && sandboxGitTokens ? { gitTokenSeeds: sandboxGitTokens } : {},
|
|
12423
|
+
...activeSandboxBackend !== "selfhosted" && sandboxGitCredentialBindings ? { gitCredentialBindings: sandboxGitCredentialBindings } : {},
|
|
12424
|
+
...activeSandboxBackend !== "selfhosted" && !sandboxGitTokens && sandboxGitToken ? { gitTokenSeed: sandboxGitToken } : {},
|
|
12425
|
+
...sandboxCodemodeToken ? { codemodeAvailable: true } : {},
|
|
12426
|
+
// Managed boxes receive the bearer through their protected per-session
|
|
12427
|
+
// token file. Connected Machines use transient per-exec delivery above,
|
|
12428
|
+
// so they must not run the file-seeding lifecycle hook.
|
|
12429
|
+
...activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken ? {
|
|
12430
|
+
codemodeTokenSeed: sandboxCodemodeToken,
|
|
12431
|
+
codemodeTokenSessionId: input.sessionId
|
|
12432
|
+
} : {},
|
|
12433
|
+
...activeSandboxBackend ? { activeSandboxBackend } : {},
|
|
12434
|
+
fileResourceDownloads,
|
|
12435
|
+
mcpServers: preparedTools.mcpServers,
|
|
12436
|
+
resolvedMcpConnectionIds: preparedTools.resolvedMcpConnectionIds,
|
|
12437
|
+
connectorActionPolicy,
|
|
12438
|
+
attemptConnectorActionBindings: googleDriveConnectorBindings,
|
|
12439
|
+
// LIVE by-reference connector namespaces (fills during this turn's
|
|
12440
|
+
// codex_apps tools/list): the codex tool_search description reads it per
|
|
12441
|
+
// model call so the model sees the account's real connected sources.
|
|
12442
|
+
codexConnectorNamespaces: preparedTools.codexConnectorNamespaces,
|
|
12443
|
+
// Resolved-model routing + gating (legacy defaults when null). The model
|
|
12444
|
+
// is passed as the model *string* (agent.model = runSettings.openaiModel),
|
|
12445
|
+
// NOT a Model instance: an instance only survives the in-process
|
|
12446
|
+
// ("none") run, whereas the SandboxAgent/Modal path drops it and
|
|
12447
|
+
// re-resolves the model *name* through the global MultiProviderModelProvider
|
|
12448
|
+
// configureOpenAI installed — so registry models (Fireworks GLM) route to
|
|
12449
|
+
// their own client instead of 404ing against the built-in Azure/OpenAI
|
|
12450
|
+
// client. The gating still comes from the resolved provider: server-side
|
|
12451
|
+
// store/compaction follow the provider's compaction mode (registry
|
|
12452
|
+
// providers resolve to "client"); encrypted reasoning is only
|
|
12453
|
+
// round-tripped on the Responses wire API; hosted web search is attached
|
|
12454
|
+
// whenever the provider declares it runnable and is independent of the
|
|
12455
|
+
// session's MCP allow-list; the effective context window drives the
|
|
12456
|
+
// compaction threshold.
|
|
12457
|
+
hostedWebSearch,
|
|
12458
|
+
...imageGenerationOption,
|
|
12459
|
+
...videoGenerationOption,
|
|
12460
|
+
lazyToolTransport,
|
|
12461
|
+
supportsImageInput,
|
|
12462
|
+
inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
|
|
12463
|
+
...resolvedModel ? {
|
|
12464
|
+
encryptedReasoning: resolvedModel.provider.api === "responses" && runSettings.openaiReasoningEncryptedContent,
|
|
12465
|
+
contextWindowTokens: resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
|
|
12466
|
+
// The ChatGPT/Codex backend rejects the SDK's HOSTED apply_patch
|
|
12467
|
+
// tool. Gateway Responses routes likewise expose ordinary function
|
|
12468
|
+
// tools, not OpenAI-hosted sandbox tools. Tell buildAgent to use
|
|
12469
|
+
// function apply_patch and wrap successful view_image results as
|
|
12470
|
+
// typed input_image content. Chat wires have no proven typed image
|
|
12471
|
+
// result transport and therefore receive no view_image tool.
|
|
12472
|
+
structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
|
|
12473
|
+
// EXPLICIT computer-use tool transport, derived from the resolved provider's
|
|
12474
|
+
// authoritative wire identity (codex → function-image, chat → disabled,
|
|
12475
|
+
// responses → hosted) so the runtime never string-sniffs the model instance's
|
|
12476
|
+
// constructor name. See {@link computerToolModeForTurn}.
|
|
12477
|
+
computerToolMode: computerToolModeForTurn(resolvedModel),
|
|
12478
|
+
...promptCacheKey ? { promptCacheKey } : {}
|
|
12479
|
+
} : (
|
|
12480
|
+
// LEGACY global-client fallback (resolveTurnModel returned null → the model
|
|
12481
|
+
// is not in the registry, served by the built-in OpenAI/Azure Responses
|
|
12482
|
+
// client). That backend has real hosted support, so pin computerToolMode to
|
|
12483
|
+
// "hosted" EXPLICITLY rather than leaving the runtime to sniff the instance.
|
|
12484
|
+
{
|
|
12485
|
+
computerToolMode: computerToolModeForTurn(null),
|
|
12486
|
+
promptCacheKey: input.sessionId
|
|
12487
|
+
}
|
|
12488
|
+
),
|
|
12489
|
+
// Lazy computer-use seam: runtime first brings up :0 only after the model
|
|
12490
|
+
// selects a computer tool, then this hook begins the optional proof
|
|
12491
|
+
// recording. Shell/filesystem turns never invoke either operation.
|
|
12492
|
+
onComputerUseReady: async () => {
|
|
12493
|
+
if (!resolvedSandbox) {
|
|
12494
|
+
throw new Error("Computer-use display became ready without a resolved sandbox");
|
|
12495
|
+
}
|
|
12496
|
+
didComputerUse = true;
|
|
12497
|
+
await maybeStartOnTurnRecording(resolvedSandbox, activeSandboxBackend);
|
|
12498
|
+
},
|
|
12499
|
+
onRetainableSessionImageOutput: retainSessionImageAtToolBoundary,
|
|
12500
|
+
...runtimeSkillActivations.length > 0 ? { skillActivations: runtimeSkillActivations } : {},
|
|
12501
|
+
...!structuredWorkspacePolicyActive && workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
|
|
12502
|
+
...workspaceGovernance ? { workspaceGovernance } : {},
|
|
12503
|
+
...workspaceMemory ? { workspaceMemory } : {},
|
|
12504
|
+
goalSnapshot: turn.goalSnapshot,
|
|
12505
|
+
// Per-session persona tier (session > workspace > deployment default).
|
|
12506
|
+
// Composed system-level AFTER the workspace persona so it refines it for
|
|
12507
|
+
// this one session; absent ⇒ byte-identical to today's composition.
|
|
12508
|
+
...session.instructions ? { sessionInstructions: session.instructions } : {},
|
|
12509
|
+
...workspaceEnvironmentOption,
|
|
12510
|
+
// RIG RUNTIME (M3): the doctrine block, the setup-script hook (only when
|
|
12511
|
+
// the frozen version carries a non-empty script), and the rig credential
|
|
12512
|
+
// hooks. All absent for a rig-less turn (byte-for-byte today).
|
|
12513
|
+
...rigVersion && rigName ? {
|
|
12514
|
+
rig: { name: rigName, version: rigVersion.version },
|
|
12515
|
+
...rigVersion.setupScript && rigVersion.setupScript.trim().length > 0 ? {
|
|
12516
|
+
rigSetup: {
|
|
12517
|
+
rigId: session.rigId,
|
|
12518
|
+
versionId: rigVersion.id,
|
|
12519
|
+
rigName,
|
|
12520
|
+
script: rigVersion.setupScript,
|
|
12521
|
+
timeoutMs: runSettings.rigSetupTimeoutMs,
|
|
12522
|
+
contentHash: rigProviderImageContentHash({
|
|
12523
|
+
backend: turn.sandboxBackend,
|
|
12524
|
+
sourceImage: rigProviderImageSourceImage(
|
|
12525
|
+
logicalSandboxSettings,
|
|
12526
|
+
turn.sandboxBackend
|
|
12527
|
+
),
|
|
12528
|
+
definition: rigVersion
|
|
12529
|
+
})
|
|
12530
|
+
}
|
|
12531
|
+
} : {},
|
|
12532
|
+
...rigVersion.credentialHooks.length > 0 ? { rigCredentialHookIds: rigVersion.credentialHooks } : {}
|
|
12533
|
+
} : {}
|
|
12534
|
+
});
|
|
12535
|
+
} catch (error) {
|
|
12536
|
+
agentConstructionOutcome = "failed";
|
|
12537
|
+
throw error;
|
|
12538
|
+
} finally {
|
|
12539
|
+
recordTurnStartupPhase(observability, {
|
|
12540
|
+
phase: "agent_construction",
|
|
12541
|
+
provider: turnExecutionPolicy.providerId,
|
|
12542
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
12543
|
+
outcome: agentConstructionOutcome,
|
|
12544
|
+
durationSeconds: (performance.now() - agentConstructionStartedAt) / 1e3
|
|
12545
|
+
});
|
|
12546
|
+
}
|
|
12547
|
+
})();
|
|
12548
|
+
const postAgentPreparationStartedAt = performance.now();
|
|
11542
12549
|
if (modelRunSettings.sandboxBackend !== "none" && toolCancellationFenceRef.current === null) {
|
|
11543
12550
|
throw new Error(
|
|
11544
12551
|
"Sandbox agent construction did not install the mandatory turn tool cancellation fence"
|
|
@@ -11585,7 +12592,11 @@ function createRunAgentTurnActivity(services) {
|
|
|
11585
12592
|
environment: sandboxEnvironment,
|
|
11586
12593
|
...runSettings.modalImageRef ?? runSettings.dockerImage ? {
|
|
11587
12594
|
image: runSettings.modalImageRef ?? runSettings.dockerImage
|
|
11588
|
-
} : {}
|
|
12595
|
+
} : {},
|
|
12596
|
+
// The lazy acquire must enforce the same frozen rig authority
|
|
12597
|
+
// as the eager acquire; otherwise a warm box for another rig
|
|
12598
|
+
// can bypass the shared-state conflict/rotation fence.
|
|
12599
|
+
...rigVersion ? { rigVersionId: rigVersion.id } : {}
|
|
11589
12600
|
},
|
|
11590
12601
|
"turn",
|
|
11591
12602
|
lazyHolderId
|
|
@@ -11983,58 +12994,96 @@ function createRunAgentTurnActivity(services) {
|
|
|
11983
12994
|
return claimedResult({ status: "idle" });
|
|
11984
12995
|
}
|
|
11985
12996
|
}
|
|
12997
|
+
recordTurnStartupPhase(observability, {
|
|
12998
|
+
phase: "post_agent_preparation",
|
|
12999
|
+
provider: turnExecutionPolicy.providerId,
|
|
13000
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13001
|
+
outcome: "completed",
|
|
13002
|
+
durationSeconds: (performance.now() - postAgentPreparationStartedAt) / 1e3
|
|
13003
|
+
});
|
|
11986
13004
|
let fileMaterializationFailures = [];
|
|
11987
13005
|
let fileDownloadsMaterializedForRun = false;
|
|
11988
13006
|
if (resolvedSandbox && setupBoxSession && fileResourceDownloads.length > 0) {
|
|
11989
|
-
const
|
|
13007
|
+
const fileMaterializationStartedAt = performance.now();
|
|
13008
|
+
let fileMaterializationOutcome = "completed";
|
|
11990
13009
|
const cacheMaterialization = resolvedSandbox.established.backendId !== "selfhosted";
|
|
11991
|
-
|
|
11992
|
-
|
|
11993
|
-
|
|
11994
|
-
|
|
11995
|
-
|
|
11996
|
-
|
|
11997
|
-
|
|
11998
|
-
|
|
11999
|
-
|
|
12000
|
-
|
|
12001
|
-
|
|
12002
|
-
|
|
12003
|
-
|
|
12004
|
-
const materialized = await runWorkspaceMutationForSandbox(
|
|
12005
|
-
resolvedSandbox,
|
|
12006
|
-
"fileMaterialization",
|
|
12007
|
-
async () => await materializeSandboxFileDownloads(
|
|
12008
|
-
setupBoxSession,
|
|
12009
|
-
downloadsToMaterialize,
|
|
12010
|
-
{
|
|
12011
|
-
onRuntimeEvent: async (event) => {
|
|
12012
|
-
await publish([{ type: event.type, payload: event.payload }], true);
|
|
12013
|
-
},
|
|
12014
|
-
...runAs ? { runAs } : {},
|
|
12015
|
-
...toolCancellationFenceRef.current ? {
|
|
12016
|
-
commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
|
|
12017
|
-
toolCancellationFenceRef.current
|
|
12018
|
-
)
|
|
12019
|
-
} : {}
|
|
12020
|
-
}
|
|
12021
|
-
)
|
|
13010
|
+
let fileMaterializationCache = cacheMaterialization ? "miss" : "disabled";
|
|
13011
|
+
try {
|
|
13012
|
+
const boxInstanceId = resolvedSandbox.established.instanceId;
|
|
13013
|
+
const alreadyMaterialized = cacheMaterialization ? await getMaterializedSandboxFileResources(db, {
|
|
13014
|
+
accountId: input.accountId,
|
|
13015
|
+
workspaceId: input.workspaceId,
|
|
13016
|
+
sandboxGroupId: session.sandboxGroupId,
|
|
13017
|
+
expectedEpoch: resolvedSandbox.leaseEpoch,
|
|
13018
|
+
instanceId: boxInstanceId
|
|
13019
|
+
}) : /* @__PURE__ */ new Set();
|
|
13020
|
+
const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
|
|
13021
|
+
fileResourceDownloads,
|
|
13022
|
+
alreadyMaterialized
|
|
12022
13023
|
);
|
|
12023
|
-
|
|
12024
|
-
|
|
12025
|
-
|
|
12026
|
-
|
|
12027
|
-
|
|
12028
|
-
|
|
12029
|
-
|
|
12030
|
-
|
|
12031
|
-
|
|
12032
|
-
|
|
12033
|
-
|
|
12034
|
-
|
|
13024
|
+
if (cacheMaterialization && downloadsToMaterialize.length === 0) {
|
|
13025
|
+
fileMaterializationCache = "hit";
|
|
13026
|
+
}
|
|
13027
|
+
const runAs = sandboxRunAs(runSettings);
|
|
13028
|
+
if (downloadsToMaterialize.length > 0) {
|
|
13029
|
+
const materialized = await runWorkspaceMutationForSandbox(
|
|
13030
|
+
resolvedSandbox,
|
|
13031
|
+
"fileMaterialization",
|
|
13032
|
+
async () => await materializeSandboxFileDownloads(
|
|
13033
|
+
setupBoxSession,
|
|
13034
|
+
downloadsToMaterialize,
|
|
13035
|
+
{
|
|
13036
|
+
onRuntimeEvent: async (event) => {
|
|
13037
|
+
await publish([{ type: event.type, payload: event.payload }], true);
|
|
13038
|
+
},
|
|
13039
|
+
...runAs ? { runAs } : {},
|
|
13040
|
+
...toolCancellationFenceRef.current ? {
|
|
13041
|
+
commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
|
|
13042
|
+
toolCancellationFenceRef.current
|
|
13043
|
+
)
|
|
13044
|
+
} : {}
|
|
13045
|
+
}
|
|
13046
|
+
)
|
|
13047
|
+
);
|
|
13048
|
+
fileMaterializationFailures = materialized.failures;
|
|
13049
|
+
fileMaterializationOutcome = sandboxFileMaterializationOutcome(materialized.failures);
|
|
13050
|
+
const failedFileIds = new Set(materialized.failures.map((failure) => failure.fileId));
|
|
13051
|
+
const succeededFileIds = downloadsToMaterialize.map((download) => download.fileId).filter((fileId) => !failedFileIds.has(fileId));
|
|
13052
|
+
if (cacheMaterialization && succeededFileIds.length > 0) {
|
|
13053
|
+
await markSandboxFileResourcesMaterialized(db, {
|
|
13054
|
+
accountId: input.accountId,
|
|
13055
|
+
workspaceId: input.workspaceId,
|
|
13056
|
+
sandboxGroupId: session.sandboxGroupId,
|
|
13057
|
+
expectedEpoch: resolvedSandbox.leaseEpoch,
|
|
13058
|
+
instanceId: boxInstanceId,
|
|
13059
|
+
fileIds: succeededFileIds
|
|
13060
|
+
});
|
|
13061
|
+
}
|
|
12035
13062
|
}
|
|
13063
|
+
fileDownloadsMaterializedForRun = true;
|
|
13064
|
+
} catch (error) {
|
|
13065
|
+
fileMaterializationOutcome = "failed";
|
|
13066
|
+
throw error;
|
|
13067
|
+
} finally {
|
|
13068
|
+
recordTurnStartupPhase(observability, {
|
|
13069
|
+
phase: "file_materialization",
|
|
13070
|
+
provider: turnExecutionPolicy.providerId,
|
|
13071
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13072
|
+
outcome: fileMaterializationOutcome,
|
|
13073
|
+
durationSeconds: (performance.now() - fileMaterializationStartedAt) / 1e3,
|
|
13074
|
+
count: fileResourceDownloads.length,
|
|
13075
|
+
cache: fileMaterializationCache
|
|
13076
|
+
});
|
|
12036
13077
|
}
|
|
12037
|
-
|
|
13078
|
+
} else {
|
|
13079
|
+
recordTurnStartupPhase(observability, {
|
|
13080
|
+
phase: "file_materialization",
|
|
13081
|
+
provider: turnExecutionPolicy.providerId,
|
|
13082
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13083
|
+
outcome: "completed",
|
|
13084
|
+
durationSeconds: 0,
|
|
13085
|
+
count: 0
|
|
13086
|
+
});
|
|
12038
13087
|
}
|
|
12039
13088
|
const requiredVideoFileIds = new Set(requiredGeneratedVideoFiles.map((file) => file.fileId));
|
|
12040
13089
|
const failedRequiredVideo = fileMaterializationFailures.find(
|
|
@@ -12076,28 +13125,91 @@ function createRunAgentTurnActivity(services) {
|
|
|
12076
13125
|
}
|
|
12077
13126
|
let runInput = null;
|
|
12078
13127
|
const prepareRunAttemptInput = async () => {
|
|
12079
|
-
const
|
|
12080
|
-
|
|
12081
|
-
|
|
12082
|
-
|
|
12083
|
-
|
|
12084
|
-
|
|
12085
|
-
|
|
12086
|
-
|
|
12087
|
-
|
|
12088
|
-
|
|
12089
|
-
|
|
12090
|
-
|
|
12091
|
-
|
|
13128
|
+
const historyPreparationStartedAt = performance.now();
|
|
13129
|
+
let historyPreparationOutcome = "completed";
|
|
13130
|
+
let preparedHistoryCount = null;
|
|
13131
|
+
try {
|
|
13132
|
+
const prepared = await turnInput(db, runtime, agent, trigger, {
|
|
13133
|
+
turnId: activeTurnId,
|
|
13134
|
+
fileAuthority: {
|
|
13135
|
+
accountId: input.accountId,
|
|
13136
|
+
subjectId: fileAuthoritySubjectId
|
|
13137
|
+
},
|
|
13138
|
+
recovering: turn.executionGeneration > 1,
|
|
13139
|
+
...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
|
|
13140
|
+
...runCredentialsNote ? { runCredentialsNote } : {},
|
|
13141
|
+
...mcpAvailabilityNote ? { mcpAvailabilityNote } : {},
|
|
13142
|
+
providerApi,
|
|
13143
|
+
projectCanonicalHistory: generatedImageHistoryProjector,
|
|
13144
|
+
materializeModelHistory: materializeScreenshotHistory,
|
|
13145
|
+
materializeSerializedRunState: materializeScreenshotRunState,
|
|
13146
|
+
projectModelHistory: modelHistoryProjector,
|
|
13147
|
+
onPreparationPhase: (measurement) => {
|
|
13148
|
+
recordTurnStartupPhase(observability, {
|
|
13149
|
+
phase: `history_${measurement.phase}`,
|
|
13150
|
+
provider: turnExecutionPolicy.providerId,
|
|
13151
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13152
|
+
outcome: measurement.outcome,
|
|
13153
|
+
durationSeconds: measurement.durationSeconds
|
|
13154
|
+
});
|
|
13155
|
+
}
|
|
13156
|
+
});
|
|
13157
|
+
const generatedImageMaterializationStartedAt = performance.now();
|
|
13158
|
+
let generatedImageMaterializationOutcome = "completed";
|
|
13159
|
+
try {
|
|
13160
|
+
for (const receipt of generatedImageReceiptsByArtifactId.values()) {
|
|
13161
|
+
await materializeGeneratedImage(receipt);
|
|
13162
|
+
}
|
|
13163
|
+
} catch (error) {
|
|
13164
|
+
generatedImageMaterializationOutcome = "failed";
|
|
13165
|
+
throw error;
|
|
13166
|
+
} finally {
|
|
13167
|
+
recordTurnStartupPhase(observability, {
|
|
13168
|
+
phase: "history_generated_image_materialization",
|
|
13169
|
+
provider: turnExecutionPolicy.providerId,
|
|
13170
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13171
|
+
outcome: generatedImageMaterializationOutcome,
|
|
13172
|
+
durationSeconds: (performance.now() - generatedImageMaterializationStartedAt) / 1e3,
|
|
13173
|
+
count: generatedImageReceiptsByArtifactId.size
|
|
13174
|
+
});
|
|
13175
|
+
}
|
|
13176
|
+
runInput = prepared.input;
|
|
13177
|
+
providerArtifactCandidates = prepared.providerArtifactCandidates;
|
|
13178
|
+
persistedHistoryCount = prepared.persistedHistoryCount;
|
|
13179
|
+
preparedHistoryCount = prepared.persistedHistoryCount;
|
|
13180
|
+
const historyPositionStartedAt = performance.now();
|
|
13181
|
+
let historyPositionOutcome = "completed";
|
|
13182
|
+
try {
|
|
13183
|
+
nextHistoryPosition = await nextSessionHistoryPosition(
|
|
13184
|
+
db,
|
|
13185
|
+
input.workspaceId,
|
|
13186
|
+
input.sessionId
|
|
13187
|
+
);
|
|
13188
|
+
} catch (error) {
|
|
13189
|
+
historyPositionOutcome = "failed";
|
|
13190
|
+
throw error;
|
|
13191
|
+
} finally {
|
|
13192
|
+
recordTurnStartupPhase(observability, {
|
|
13193
|
+
phase: "history_position_load",
|
|
13194
|
+
provider: turnExecutionPolicy.providerId,
|
|
13195
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13196
|
+
outcome: historyPositionOutcome,
|
|
13197
|
+
durationSeconds: (performance.now() - historyPositionStartedAt) / 1e3
|
|
13198
|
+
});
|
|
13199
|
+
}
|
|
13200
|
+
} catch (error) {
|
|
13201
|
+
historyPreparationOutcome = "failed";
|
|
13202
|
+
throw error;
|
|
13203
|
+
} finally {
|
|
13204
|
+
recordTurnStartupPhase(observability, {
|
|
13205
|
+
phase: "history_preparation",
|
|
13206
|
+
provider: turnExecutionPolicy.providerId,
|
|
13207
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13208
|
+
outcome: historyPreparationOutcome,
|
|
13209
|
+
durationSeconds: (performance.now() - historyPreparationStartedAt) / 1e3,
|
|
13210
|
+
count: preparedHistoryCount
|
|
13211
|
+
});
|
|
12092
13212
|
}
|
|
12093
|
-
runInput = prepared.input;
|
|
12094
|
-
providerArtifactCandidates = prepared.providerArtifactCandidates;
|
|
12095
|
-
persistedHistoryCount = prepared.persistedHistoryCount;
|
|
12096
|
-
nextHistoryPosition = await nextSessionHistoryPosition(
|
|
12097
|
-
db,
|
|
12098
|
-
input.workspaceId,
|
|
12099
|
-
input.sessionId
|
|
12100
|
-
);
|
|
12101
13213
|
};
|
|
12102
13214
|
const forceContextCompaction = async (triggerLabel, recoverySignalTokens) => {
|
|
12103
13215
|
const outcome = await waitForTurnOperation(
|
|
@@ -12140,6 +13252,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
12140
13252
|
return outcome;
|
|
12141
13253
|
};
|
|
12142
13254
|
const modelResponseState = createModelResponseEventState(claimedModelUsageSourceKeys);
|
|
13255
|
+
let workerPreparationTotalRecorded = false;
|
|
12143
13256
|
const runStreamAttempt = async () => {
|
|
12144
13257
|
if (!runInput) {
|
|
12145
13258
|
throw new Error("Run input was not prepared");
|
|
@@ -12154,102 +13267,154 @@ function createRunAgentTurnActivity(services) {
|
|
|
12154
13267
|
const ownedEstablished = resolvedSandbox?.established ?? lazyOwnedSandbox;
|
|
12155
13268
|
const runStreamOnce = async () => {
|
|
12156
13269
|
if (resolvedSandbox && !lazyOwnedSandbox && ownedEstablished) {
|
|
12157
|
-
const
|
|
12158
|
-
|
|
12159
|
-
|
|
12160
|
-
|
|
12161
|
-
|
|
12162
|
-
|
|
12163
|
-
|
|
12164
|
-
|
|
12165
|
-
|
|
12166
|
-
|
|
12167
|
-
|
|
12168
|
-
|
|
12169
|
-
|
|
12170
|
-
|
|
12171
|
-
|
|
12172
|
-
|
|
12173
|
-
|
|
12174
|
-
|
|
12175
|
-
|
|
12176
|
-
|
|
12177
|
-
|
|
12178
|
-
|
|
12179
|
-
|
|
12180
|
-
|
|
12181
|
-
|
|
12182
|
-
|
|
12183
|
-
|
|
12184
|
-
|
|
12185
|
-
|
|
12186
|
-
|
|
12187
|
-
|
|
12188
|
-
|
|
12189
|
-
|
|
12190
|
-
|
|
12191
|
-
|
|
12192
|
-
|
|
12193
|
-
|
|
13270
|
+
const ownedSandboxSetupStartedAt = performance.now();
|
|
13271
|
+
let ownedSandboxSetupOutcome = "completed";
|
|
13272
|
+
try {
|
|
13273
|
+
const eagerSetupSession = setupBoxSession ?? ownedEstablished.session;
|
|
13274
|
+
await attachRunCredentialRenewal(
|
|
13275
|
+
eagerSetupSession,
|
|
13276
|
+
resolvedSandbox
|
|
13277
|
+
);
|
|
13278
|
+
const eagerCredentialSetupSession = initialRunCredentialMaterial ? withRunCredentialsSession(eagerSetupSession, input.sessionId) : eagerSetupSession;
|
|
13279
|
+
await runWorkspaceMutationForSandbox(
|
|
13280
|
+
resolvedSandbox,
|
|
13281
|
+
"eagerOwnedSandboxSetup",
|
|
13282
|
+
async () => await runOwnedSandboxSetup(
|
|
13283
|
+
agent,
|
|
13284
|
+
ownedEstablished.session,
|
|
13285
|
+
eagerCredentialSetupSession,
|
|
13286
|
+
{
|
|
13287
|
+
settings: runSettings,
|
|
13288
|
+
environment: sandboxEnvironment,
|
|
13289
|
+
preparedInput: runInput,
|
|
13290
|
+
...fileDownloadsMaterializedForRun ? { fileDownloadsMaterialized: true } : {},
|
|
13291
|
+
onRuntimeEvent: async (event) => {
|
|
13292
|
+
await renewServingCredentialLease("runtime_event");
|
|
13293
|
+
if (servingCredentialLeaseLost()) {
|
|
13294
|
+
throw new Error("Provider credential lease expired during sandbox setup");
|
|
13295
|
+
}
|
|
13296
|
+
await publish([{ type: event.type, payload: event.payload }], true);
|
|
13297
|
+
},
|
|
13298
|
+
...toolCancellationFenceRef.current ? {
|
|
13299
|
+
commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
|
|
13300
|
+
toolCancellationFenceRef.current
|
|
13301
|
+
)
|
|
13302
|
+
} : {}
|
|
13303
|
+
}
|
|
13304
|
+
)
|
|
13305
|
+
);
|
|
13306
|
+
await attachGitCredentialRenewal(
|
|
13307
|
+
eagerSetupSession,
|
|
13308
|
+
initialGitCredentials
|
|
13309
|
+
);
|
|
13310
|
+
} catch (error) {
|
|
13311
|
+
ownedSandboxSetupOutcome = "failed";
|
|
13312
|
+
throw error;
|
|
13313
|
+
} finally {
|
|
13314
|
+
recordTurnStartupPhase(observability, {
|
|
13315
|
+
phase: "owned_sandbox_setup",
|
|
13316
|
+
provider: turnExecutionPolicy.providerId,
|
|
13317
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13318
|
+
outcome: ownedSandboxSetupOutcome,
|
|
13319
|
+
durationSeconds: (performance.now() - ownedSandboxSetupStartedAt) / 1e3,
|
|
13320
|
+
count: fileResourceDownloads.length
|
|
13321
|
+
});
|
|
13322
|
+
}
|
|
12194
13323
|
}
|
|
12195
13324
|
recordCompanyBrainContributionReceiptOnce();
|
|
12196
|
-
|
|
12197
|
-
|
|
12198
|
-
|
|
12199
|
-
|
|
12200
|
-
|
|
12201
|
-
|
|
12202
|
-
|
|
12203
|
-
|
|
12204
|
-
|
|
12205
|
-
|
|
12206
|
-
|
|
12207
|
-
|
|
12208
|
-
|
|
12209
|
-
|
|
12210
|
-
|
|
12211
|
-
|
|
12212
|
-
|
|
12213
|
-
|
|
12214
|
-
|
|
12215
|
-
|
|
12216
|
-
// Pin platform setup (hooks + file materialization) to the un-proxied
|
|
12217
|
-
// established box — never through the routing proxy, which would
|
|
12218
|
-
// re-route those execs onto a machine swapped in mid-turn.
|
|
12219
|
-
...setupBoxSession ? { setupSession: setupBoxSession } : {},
|
|
12220
|
-
...fileDownloadsMaterializedForRun ? { fileDownloadsMaterialized: true } : {},
|
|
12221
|
-
// Both owned paths execute setup outside runStream: eager just
|
|
12222
|
-
// above under its exact admission, lazy in the provisioner.
|
|
12223
|
-
deferredSetup: true
|
|
12224
|
-
}
|
|
12225
|
-
} : {},
|
|
12226
|
-
...activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken && sandboxCodemodeTokenExpiresAt && !lazyOwnedSandbox ? {
|
|
12227
|
-
onCodemodeTokenSessionReady: async (tokenSession) => {
|
|
12228
|
-
const renewalSession = setupBoxSession ?? tokenSession;
|
|
12229
|
-
await attachCodemodeTokenRenewal(renewalSession);
|
|
12230
|
-
}
|
|
12231
|
-
} : {},
|
|
12232
|
-
...runCredentialResolver ? {
|
|
12233
|
-
runCredentialSessionId: input.sessionId,
|
|
12234
|
-
...!lazyOwnedSandbox ? {
|
|
12235
|
-
onRunCredentialSessionReady: async (credentialSession) => {
|
|
12236
|
-
const pinnedCredentialSession = setupBoxSession ? setupBoxSession : credentialSession;
|
|
12237
|
-
await attachRunCredentialRenewal(pinnedCredentialSession);
|
|
13325
|
+
const providerDispatchStartedAt = performance.now();
|
|
13326
|
+
let providerDispatchOutcome = "completed";
|
|
13327
|
+
try {
|
|
13328
|
+
if (!workerPreparationTotalRecorded) {
|
|
13329
|
+
workerPreparationTotalRecorded = true;
|
|
13330
|
+
recordTurnWorkerPreparationTotal(observability, {
|
|
13331
|
+
provider: turnExecutionPolicy.providerId,
|
|
13332
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13333
|
+
outcome: "completed",
|
|
13334
|
+
durationSeconds: (performance.now() - workerPreparationStartedAt) / 1e3
|
|
13335
|
+
});
|
|
13336
|
+
}
|
|
13337
|
+
modelRequestStarted = true;
|
|
13338
|
+
return await runtime.runStream(agent, runInput, modelRunSettings, {
|
|
13339
|
+
signal: runtimeCancellationSignal,
|
|
13340
|
+
sandboxEnvironment,
|
|
13341
|
+
onRuntimeEvent: async (event) => {
|
|
13342
|
+
await renewServingCredentialLease("runtime_event");
|
|
13343
|
+
if (servingCredentialLeaseLost()) {
|
|
13344
|
+
throw new Error("Provider credential lease expired during the active turn");
|
|
12238
13345
|
}
|
|
12239
|
-
|
|
12240
|
-
|
|
12241
|
-
|
|
12242
|
-
|
|
12243
|
-
|
|
12244
|
-
|
|
12245
|
-
|
|
13346
|
+
await publish([{ type: event.type, payload: event.payload }], true);
|
|
13347
|
+
},
|
|
13348
|
+
// P1.2: inject the resumed box NON-OWNED (the SDK never reaps it — the
|
|
13349
|
+
// keystone). Absent when the flag is off -> legacy build-and-discard.
|
|
13350
|
+
...ownedEstablished ? {
|
|
13351
|
+
ownedSandbox: {
|
|
13352
|
+
client: ownedEstablished.client,
|
|
13353
|
+
session: ownedEstablished.session,
|
|
13354
|
+
...resolvedSandbox?.established.sessionState ? {
|
|
13355
|
+
sessionState: resolvedSandbox.established.sessionState
|
|
13356
|
+
} : {},
|
|
13357
|
+
// Pin platform setup (hooks + file materialization) to the un-proxied
|
|
13358
|
+
// established box — never through the routing proxy, which would
|
|
13359
|
+
// re-route those execs onto a machine swapped in mid-turn.
|
|
13360
|
+
...setupBoxSession ? { setupSession: setupBoxSession } : {},
|
|
13361
|
+
...fileDownloadsMaterializedForRun ? { fileDownloadsMaterialized: true } : {},
|
|
13362
|
+
// Both owned paths execute setup outside runStream: eager just
|
|
13363
|
+
// above under its exact admission, lazy in the provisioner.
|
|
13364
|
+
deferredSetup: true
|
|
12246
13365
|
}
|
|
12247
|
-
}
|
|
12248
|
-
|
|
12249
|
-
|
|
12250
|
-
|
|
12251
|
-
|
|
12252
|
-
|
|
13366
|
+
} : {},
|
|
13367
|
+
...activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken && sandboxCodemodeTokenExpiresAt && !lazyOwnedSandbox ? {
|
|
13368
|
+
onCodemodeTokenSessionReady: async (tokenSession) => {
|
|
13369
|
+
const renewalSession = setupBoxSession ?? tokenSession;
|
|
13370
|
+
await attachCodemodeTokenRenewal(renewalSession);
|
|
13371
|
+
}
|
|
13372
|
+
} : {},
|
|
13373
|
+
...runCredentialResolver ? {
|
|
13374
|
+
runCredentialSessionId: input.sessionId,
|
|
13375
|
+
...!lazyOwnedSandbox ? {
|
|
13376
|
+
onRunCredentialSessionReady: async (credentialSession) => {
|
|
13377
|
+
const pinnedCredentialSession = setupBoxSession ? setupBoxSession : credentialSession;
|
|
13378
|
+
await attachRunCredentialRenewal(pinnedCredentialSession);
|
|
13379
|
+
}
|
|
13380
|
+
} : {}
|
|
13381
|
+
} : {},
|
|
13382
|
+
...modelRunSettings.sandboxBackend !== "none" ? {
|
|
13383
|
+
onSandboxSessionReady: async (sandboxSession) => {
|
|
13384
|
+
sdkOwnedSandboxSession = sandboxSession;
|
|
13385
|
+
for (const receipt of generatedImageReceiptsByArtifactId.values()) {
|
|
13386
|
+
await materializeGeneratedImageInOwnedSdkSession(receipt, sandboxSession);
|
|
13387
|
+
}
|
|
13388
|
+
}
|
|
13389
|
+
} : {},
|
|
13390
|
+
contextCompactionSignal: () => modelResponseContextSignal(modelResponseState),
|
|
13391
|
+
contextCompactionRequested: () => isSessionCompactionRequested(db, input.workspaceId, input.sessionId),
|
|
13392
|
+
onModelPreparationPhase: (measurement) => {
|
|
13393
|
+
recordTurnStartupPhase(observability, {
|
|
13394
|
+
phase: `model_prepare_${measurement.phase}`,
|
|
13395
|
+
provider: turnExecutionPolicy.providerId,
|
|
13396
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13397
|
+
outcome: measurement.outcome,
|
|
13398
|
+
durationSeconds: measurement.durationSeconds,
|
|
13399
|
+
...measurement.count === void 0 ? {} : { count: measurement.count }
|
|
13400
|
+
});
|
|
13401
|
+
},
|
|
13402
|
+
...toolCancellationFenceRef.current ? {
|
|
13403
|
+
turnToolCancellationFence: toolCancellationFenceRef.current
|
|
13404
|
+
} : {}
|
|
13405
|
+
});
|
|
13406
|
+
} catch (error) {
|
|
13407
|
+
providerDispatchOutcome = "failed";
|
|
13408
|
+
throw error;
|
|
13409
|
+
} finally {
|
|
13410
|
+
recordTurnStartupPhase(observability, {
|
|
13411
|
+
phase: "provider_dispatch",
|
|
13412
|
+
provider: turnExecutionPolicy.providerId,
|
|
13413
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13414
|
+
outcome: providerDispatchOutcome,
|
|
13415
|
+
durationSeconds: (performance.now() - providerDispatchStartedAt) / 1e3
|
|
13416
|
+
});
|
|
13417
|
+
}
|
|
12253
13418
|
};
|
|
12254
13419
|
if (codexLeaseLost) {
|
|
12255
13420
|
throw new Error("Codex credential lease expired before the model run");
|
|
@@ -12270,11 +13435,30 @@ function createRunAgentTurnActivity(services) {
|
|
|
12270
13435
|
onFlush: ({ events, durationSeconds }) => recordBatchFlush(observability, { events, durationSeconds })
|
|
12271
13436
|
}
|
|
12272
13437
|
);
|
|
13438
|
+
const streamBootstrapStartedAt = performance.now();
|
|
13439
|
+
let streamBootstrapRecorded = false;
|
|
13440
|
+
const recordStreamBootstrap = () => {
|
|
13441
|
+
if (streamBootstrapRecorded) return;
|
|
13442
|
+
streamBootstrapRecorded = true;
|
|
13443
|
+
recordTurnStartupPhase(observability, {
|
|
13444
|
+
phase: "stream_bootstrap",
|
|
13445
|
+
provider: turnExecutionPolicy.providerId,
|
|
13446
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
13447
|
+
outcome: "completed",
|
|
13448
|
+
durationSeconds: (performance.now() - streamBootstrapStartedAt) / 1e3,
|
|
13449
|
+
count: turnTools.length
|
|
13450
|
+
});
|
|
13451
|
+
};
|
|
13452
|
+
if (!firstModelRequestPreparationRecorded) {
|
|
13453
|
+
firstModelRequestPreparationStartedAt = performance.now();
|
|
13454
|
+
firstModelRequestCheckpointAt = firstModelRequestPreparationStartedAt;
|
|
13455
|
+
}
|
|
12273
13456
|
const iterator = stream.toStream()[Symbol.asyncIterator]();
|
|
12274
13457
|
let streamDone = false;
|
|
12275
13458
|
try {
|
|
12276
13459
|
while (true) {
|
|
12277
13460
|
const next = await nextStreamEvent(iterator, activityContext);
|
|
13461
|
+
recordStreamBootstrap();
|
|
12278
13462
|
if (next.done) {
|
|
12279
13463
|
streamDone = true;
|
|
12280
13464
|
break;
|
|
@@ -13830,7 +15014,10 @@ function createRunAgentTurnActivity(services) {
|
|
|
13830
15014
|
}
|
|
13831
15015
|
activityStatus = "failed";
|
|
13832
15016
|
activityError = error;
|
|
13833
|
-
if (!
|
|
15017
|
+
if (!turnId) {
|
|
15018
|
+
throw preClaimAdmissionFailure(error);
|
|
15019
|
+
}
|
|
15020
|
+
if (!publish || !turnStartedPublished) {
|
|
13834
15021
|
throw error;
|
|
13835
15022
|
}
|
|
13836
15023
|
await flushRuntimeBatcher();
|
|
@@ -14321,6 +15508,21 @@ async function assertGitHubResourcesRemainAuthorized(db, workspaceId, resources)
|
|
|
14321
15508
|
);
|
|
14322
15509
|
}
|
|
14323
15510
|
}
|
|
15511
|
+
async function assertFileResourcesRemainAuthorized(db, accountId, workspaceId, subjectId, resources) {
|
|
15512
|
+
const fileIds = resources.flatMap(
|
|
15513
|
+
(resource) => resource.kind === "file" ? [resource.fileId] : []
|
|
15514
|
+
);
|
|
15515
|
+
if (fileIds.length === 0) return;
|
|
15516
|
+
const authorized = await getFilesForSubject2(db, {
|
|
15517
|
+
accountId,
|
|
15518
|
+
workspaceId,
|
|
15519
|
+
subjectId,
|
|
15520
|
+
fileIds
|
|
15521
|
+
});
|
|
15522
|
+
if (authorized.length !== new Set(fileIds).size) {
|
|
15523
|
+
throw new Error("One or more file resources are unavailable or no longer authorized");
|
|
15524
|
+
}
|
|
15525
|
+
}
|
|
14324
15526
|
async function assertGitHubTokenMintSelectionAuthorized(db, workspaceId, installationId, repositoryIds) {
|
|
14325
15527
|
if (!await areGitHubRepositoriesAllowedForWorkspace(
|
|
14326
15528
|
db,
|
|
@@ -14858,7 +16060,7 @@ function startOfUtcMonth() {
|
|
|
14858
16060
|
const now = /* @__PURE__ */ new Date();
|
|
14859
16061
|
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
|
14860
16062
|
}
|
|
14861
|
-
async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspaceId, resources, activeSandboxBackend = settings.sandboxBackend) {
|
|
16063
|
+
async function sandboxFileDownloadsForRun(settings, db, objectStorage, accountId, workspaceId, subjectId, resources, activeSandboxBackend = settings.sandboxBackend) {
|
|
14862
16064
|
if (!requiresSignedFileResourceDownloads(settings, activeSandboxBackend)) {
|
|
14863
16065
|
return [];
|
|
14864
16066
|
}
|
|
@@ -14876,7 +16078,12 @@ async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspace
|
|
|
14876
16078
|
const downloadStorage = objectStorageForSandboxDownloads(settings, objectStorage);
|
|
14877
16079
|
const downloads = [];
|
|
14878
16080
|
for (const resource of fileResources) {
|
|
14879
|
-
const file = await
|
|
16081
|
+
const file = await requireFileForSubject3(db, {
|
|
16082
|
+
accountId,
|
|
16083
|
+
workspaceId,
|
|
16084
|
+
subjectId,
|
|
16085
|
+
fileId: resource.fileId
|
|
16086
|
+
});
|
|
14880
16087
|
const url = await downloadStorage.createGetUrl({ key: file.objectKey });
|
|
14881
16088
|
downloads.push({
|
|
14882
16089
|
fileId: file.id,
|
|
@@ -14941,7 +16148,7 @@ var GATEWAY_VIDEO_START_URL = "https://ai-gateway.vercel.sh/v4/ai/video-model/st
|
|
|
14941
16148
|
var GATEWAY_VIDEO_STATUS_URL = "https://ai-gateway.vercel.sh/v4/ai/video-model/status";
|
|
14942
16149
|
var PROTOCOL_VERSION = "0.0.1";
|
|
14943
16150
|
var VIDEO_SPECIFICATION_VERSION = "4";
|
|
14944
|
-
var
|
|
16151
|
+
var RESPONSE_MAX_BYTES2 = 1024 * 1024;
|
|
14945
16152
|
var ERROR_MAX_BYTES = 64 * 1024;
|
|
14946
16153
|
var GatewayVideoApiError = class extends Error {
|
|
14947
16154
|
constructor(status, retryable, message) {
|
|
@@ -14964,7 +16171,7 @@ async function startGatewayVideoGenerationWithBody(input) {
|
|
|
14964
16171
|
...input.fetch ? { fetch: input.fetch } : {}
|
|
14965
16172
|
});
|
|
14966
16173
|
const parsed = await readGatewayJson(response, "AI Gateway video start", input.signal);
|
|
14967
|
-
const operation =
|
|
16174
|
+
const operation = record3(parsed.operation);
|
|
14968
16175
|
const providerJobId = stringValue2(operation?.gatewayJobId);
|
|
14969
16176
|
if (!providerJobId || providerJobId.length > 1024) {
|
|
14970
16177
|
throw new GatewayVideoApiError(null, true, "AI Gateway video start response is malformed");
|
|
@@ -14999,7 +16206,7 @@ async function getGatewayVideoGenerationStatus(input) {
|
|
|
14999
16206
|
if (status !== "completed" || !Array.isArray(parsed.videos) || parsed.videos.length !== 1) {
|
|
15000
16207
|
throw new GatewayVideoApiError(null, true, "AI Gateway video status response is malformed");
|
|
15001
16208
|
}
|
|
15002
|
-
const video =
|
|
16209
|
+
const video = record3(parsed.videos[0]);
|
|
15003
16210
|
const outputUrl = video?.type === "url" ? stringValue2(video.url) : null;
|
|
15004
16211
|
const mediaType = stringValue2(video?.mediaType);
|
|
15005
16212
|
if (!outputUrl || !mediaType || outputUrl.length > 8192 || mediaType.length > 128) {
|
|
@@ -15084,7 +16291,7 @@ async function gatewayFetch(input) {
|
|
|
15084
16291
|
);
|
|
15085
16292
|
}
|
|
15086
16293
|
async function readGatewayJson(response, label, signal) {
|
|
15087
|
-
const text = await readResponseTextBounded2(response,
|
|
16294
|
+
const text = await readResponseTextBounded2(response, RESPONSE_MAX_BYTES2, label, {
|
|
15088
16295
|
...signal ? { signal } : {}
|
|
15089
16296
|
});
|
|
15090
16297
|
let parsed;
|
|
@@ -15093,7 +16300,7 @@ async function readGatewayJson(response, label, signal) {
|
|
|
15093
16300
|
} catch {
|
|
15094
16301
|
throw new GatewayVideoApiError(null, true, `${label} response is not JSON`);
|
|
15095
16302
|
}
|
|
15096
|
-
const value =
|
|
16303
|
+
const value = record3(parsed);
|
|
15097
16304
|
if (!value) throw new GatewayVideoApiError(null, true, `${label} response is malformed`);
|
|
15098
16305
|
return value;
|
|
15099
16306
|
}
|
|
@@ -15112,7 +16319,7 @@ function requireHttps(value) {
|
|
|
15112
16319
|
}
|
|
15113
16320
|
return url.toString();
|
|
15114
16321
|
}
|
|
15115
|
-
function
|
|
16322
|
+
function record3(value) {
|
|
15116
16323
|
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
15117
16324
|
}
|
|
15118
16325
|
function stringValue2(value) {
|
|
@@ -15121,8 +16328,8 @@ function stringValue2(value) {
|
|
|
15121
16328
|
function boundedError(value) {
|
|
15122
16329
|
let message = value;
|
|
15123
16330
|
try {
|
|
15124
|
-
const parsed =
|
|
15125
|
-
const error =
|
|
16331
|
+
const parsed = record3(JSON.parse(value));
|
|
16332
|
+
const error = record3(parsed?.error);
|
|
15126
16333
|
message = stringValue2(error?.message) ?? stringValue2(parsed?.message) ?? value;
|
|
15127
16334
|
} catch {
|
|
15128
16335
|
}
|
|
@@ -15130,14 +16337,14 @@ function boundedError(value) {
|
|
|
15130
16337
|
}
|
|
15131
16338
|
function publicProviderReason(error, cancelled) {
|
|
15132
16339
|
if (cancelled) return "The video provider cancelled the generation.";
|
|
15133
|
-
const message = typeof error === "string" ? error : stringValue2(
|
|
16340
|
+
const message = typeof error === "string" ? error : stringValue2(record3(error)?.message) ?? "The video provider could not complete the generation.";
|
|
15134
16341
|
return message.replace(/\s+/gu, " ").trim().slice(0, 1e3);
|
|
15135
16342
|
}
|
|
15136
16343
|
|
|
15137
16344
|
// src/activities/video-output-retention.ts
|
|
15138
16345
|
import { GENERATED_VIDEO_MAX_BYTES as GENERATED_VIDEO_MAX_BYTES2 } from "@opengeni/contracts";
|
|
15139
16346
|
import { pinnedFetch } from "@opengeni/network";
|
|
15140
|
-
import { createHash as
|
|
16347
|
+
import { createHash as createHash10, randomUUID as randomUUID5 } from "crypto";
|
|
15141
16348
|
import { createReadStream } from "fs";
|
|
15142
16349
|
import { mkdir as mkdir2, open as open2, rm as rm2 } from "fs/promises";
|
|
15143
16350
|
import { join as join2 } from "path";
|
|
@@ -15175,7 +16382,7 @@ async function downloadGeneratedVideoToVerifiedTemp(input) {
|
|
|
15175
16382
|
await mkdir2(directory, { recursive: false, mode: 448 });
|
|
15176
16383
|
const handle = await open2(path, "wx", 384);
|
|
15177
16384
|
const reader = response.body.getReader();
|
|
15178
|
-
const digest2 =
|
|
16385
|
+
const digest2 = createHash10("sha256");
|
|
15179
16386
|
let sizeBytes = 0;
|
|
15180
16387
|
try {
|
|
15181
16388
|
for (; ; ) {
|
|
@@ -15301,7 +16508,7 @@ async function verifyStoredObject(input) {
|
|
|
15301
16508
|
if (!head || head.ContentLength !== input.expectedSizeBytes || head.ContentType !== "video/mp4" || head.Metadata?.sha256 !== input.expectedSha256 || !head.VersionToken) {
|
|
15302
16509
|
throw new Error("Retained video object metadata is inconsistent");
|
|
15303
16510
|
}
|
|
15304
|
-
const hash =
|
|
16511
|
+
const hash = createHash10("sha256");
|
|
15305
16512
|
let offset = 0;
|
|
15306
16513
|
while (offset < input.expectedSizeBytes) {
|
|
15307
16514
|
throwIfAborted3(input.signal);
|
|
@@ -15834,8 +17041,8 @@ function decryptCanonicalRequest(operation, key) {
|
|
|
15834
17041
|
return request;
|
|
15835
17042
|
}
|
|
15836
17043
|
function validateCanonicalRequest(value) {
|
|
15837
|
-
const row =
|
|
15838
|
-
const references = Array.isArray(row?.references) ? row.references.map(
|
|
17044
|
+
const row = record4(value);
|
|
17045
|
+
const references = Array.isArray(row?.references) ? row.references.map(record4) : null;
|
|
15839
17046
|
if (row?.schemaVersion !== 1 || typeof row.modelId !== "string" || typeof row.prompt !== "string" || ![
|
|
15840
17047
|
"text",
|
|
15841
17048
|
"first_frame",
|
|
@@ -15853,7 +17060,7 @@ function validateCanonicalRequest(value) {
|
|
|
15853
17060
|
}
|
|
15854
17061
|
function decryptProviderBody(operation, key) {
|
|
15855
17062
|
if (!operation.providerRequestEncrypted) throw new Error("Frozen provider request is missing");
|
|
15856
|
-
const body =
|
|
17063
|
+
const body = record4(JSON.parse(decryptEnvironmentValue2(key, operation.providerRequestEncrypted)));
|
|
15857
17064
|
if (!body || Buffer.byteLength(JSON.stringify(body)) > 256 * 1024) {
|
|
15858
17065
|
throw new Error("Frozen provider request is malformed");
|
|
15859
17066
|
}
|
|
@@ -15982,7 +17189,7 @@ function isPermanentProviderFailure(error) {
|
|
|
15982
17189
|
function privateError(error) {
|
|
15983
17190
|
return (error instanceof Error ? `${error.name}: ${error.message}` : String(error)).replace(/https?:\/\/\S+/gu, "[url omitted]").slice(0, RECONCILE_ERROR_MAX);
|
|
15984
17191
|
}
|
|
15985
|
-
function
|
|
17192
|
+
function record4(value) {
|
|
15986
17193
|
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
15987
17194
|
}
|
|
15988
17195
|
function isTerminal(status) {
|
|
@@ -16033,4 +17240,4 @@ export {
|
|
|
16033
17240
|
createTurnActivities,
|
|
16034
17241
|
createTurnActivitiesFromServices
|
|
16035
17242
|
};
|
|
16036
|
-
//# sourceMappingURL=activities-turn-
|
|
17243
|
+
//# sourceMappingURL=activities-turn-J5YO7XXX.js.map
|