@opengeni/worker-bundle 0.29.0-canary.0 → 0.29.0-canary.2
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/knowledge-source-tools.d.ts +9 -0
- package/dist/activities/agent-turn/knowledge-sources.d.ts +20 -0
- package/dist/activities/agent-turn/stream-attempt.d.ts +1 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
- package/dist/activities/knowledge-indexing.d.ts +10 -0
- package/dist/activities/run-input.d.ts +1 -0
- package/dist/activities/types.d.ts +10 -3
- package/dist/{activities-control-MKGEOBXT.js → activities-control-HMFGSKC5.js} +331 -3381
- package/dist/activities-control-HMFGSKC5.js.map +1 -0
- package/dist/activities-control.d.ts +12 -0
- package/dist/{activities-turn-5MZZ6HQA.js → activities-turn-AP67JADS.js} +1296 -1021
- package/dist/activities-turn-AP67JADS.js.map +1 -0
- package/dist/activities.d.ts +12 -0
- package/dist/{chunk-OA5CSY5N.js → chunk-CYITU5UH.js} +3348 -174
- package/dist/chunk-CYITU5UH.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +48 -1
- package/dist/index.js.map +1 -1
- package/dist/workflow-bundle.js +65 -35
- package/dist/workflows/knowledge-indexing.d.ts +2 -0
- package/dist/workflows.d.ts +1 -0
- package/package.json +19 -18
- package/src/activities/agent-turn/knowledge-source-tools.ts +98 -0
- package/src/activities/agent-turn/knowledge-sources.ts +86 -0
- package/src/activities/agent-turn/run.ts +1244 -1112
- package/src/activities/agent-turn/skill-install.ts +1 -1
- package/src/activities/agent-turn/skill-save.ts +1 -1
- package/src/activities/agent-turn/skill-selection.ts +2 -0
- package/src/activities/agent-turn/skill-tools.ts +5 -12
- package/src/activities/agent-turn/stream-attempt.ts +3 -0
- package/src/activities/agent-turn/tool-environment.ts +24 -0
- package/src/activities/codex-image-generation.ts +1 -2
- package/src/activities/documents.ts +17 -52
- package/src/activities/knowledge-indexing.ts +130 -0
- package/src/activities/knowledge-source-sync.ts +120 -16
- package/src/activities/mcp-credentials.ts +2 -0
- package/src/activities/run-input.ts +2 -0
- package/src/activities/scheduled-tasks.ts +25 -62
- package/src/activities/types.ts +8 -2
- package/src/activities/video-generation-reconciliation.ts +8 -1
- package/src/activities-control.ts +2 -0
- package/src/activities.ts +1 -0
- package/src/index.ts +49 -0
- package/src/pr-review-credentials.ts +2 -2
- package/src/workflows/knowledge-indexing.ts +12 -0
- package/src/workflows.ts +1 -0
- package/dist/activities-control-MKGEOBXT.js.map +0 -1
- package/dist/activities-turn-5MZZ6HQA.js.map +0 -1
- package/dist/chunk-OA5CSY5N.js.map +0 -1
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createKnowledgeSourceSyncActivities } from "../knowledge-source-sync";
|
|
2
|
+
import { prepareTurnKnowledgeSources } from "./knowledge-sources";
|
|
3
|
+
import {
|
|
4
|
+
assertModelConnectionAllowsTurn,
|
|
5
|
+
freezeAgentLearningPolicy,
|
|
6
|
+
recoverLegacyKnowledgeConfirmations,
|
|
7
|
+
withSessionRlsActorContext,
|
|
8
|
+
} from "@opengeni/db";
|
|
2
9
|
import {
|
|
3
10
|
setSessionLastInputTokensForTurnAttempt,
|
|
4
11
|
getMaterializedSandboxFileResources,
|
|
@@ -133,6 +140,13 @@ export function sessionTitleXaiRequestContext(
|
|
|
133
140
|
|
|
134
141
|
/** Lifecycle orchestrator: claim → capacity → governance → sandbox → tools → stream. */
|
|
135
142
|
export function createRunAgentTurnActivity(services: () => Promise<ActivityServices>) {
|
|
143
|
+
let documents: Promise<import("@opengeni/documents").DocumentServices> | undefined;
|
|
144
|
+
const sourceActivities = createKnowledgeSourceSyncActivities(services, async () => {
|
|
145
|
+
documents ??= import("@opengeni/documents").then(async ({ createDocumentServices }) =>
|
|
146
|
+
createDocumentServices((await services()).settings),
|
|
147
|
+
);
|
|
148
|
+
return documents;
|
|
149
|
+
});
|
|
136
150
|
const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
|
|
137
151
|
// Keep a distinct cooldown for terminal collection. A collection at the
|
|
138
152
|
// final model checkpoint happens while the activity still owns its complete
|
|
@@ -409,181 +423,342 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
409
423
|
if (!attempt.turnId) {
|
|
410
424
|
throw new Error("Turn id was not initialized");
|
|
411
425
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
}
|
|
415
|
-
const workerPreparationStartedAt = performance.now();
|
|
416
|
-
|
|
417
|
-
// Multi-account (P1): resolve the effective Codex account for this turn
|
|
418
|
-
// (session-pin > workspace active) and stamp it on the session so the
|
|
419
|
-
// in-session "Running on:" indicator reflects reality. Emit a switch event
|
|
420
|
-
// when it changed from the prior run's account so the pill flips live.
|
|
421
|
-
// Gated on the codex-billed predicate — non-codex turns never touch this.
|
|
422
|
-
{
|
|
423
|
-
const capacityDeps: CapacityPhaseDeps = {
|
|
424
|
-
input,
|
|
425
|
-
settings,
|
|
426
|
-
db,
|
|
427
|
-
bus,
|
|
428
|
-
observability,
|
|
429
|
-
wakeSessionWorkflow,
|
|
430
|
-
signalCodexCapacityWorkflow,
|
|
431
|
-
cancellationSignal,
|
|
432
|
-
dispatchId,
|
|
433
|
-
control,
|
|
434
|
-
attempt,
|
|
435
|
-
billingState,
|
|
436
|
-
eventing: wiredEventing,
|
|
437
|
-
providerTurn,
|
|
438
|
-
leases,
|
|
439
|
-
claimedResult,
|
|
440
|
-
acknowledgeLostAttemptOwnership,
|
|
441
|
-
acknowledgeRecoveryQuiescence,
|
|
442
|
-
setLastInputTokensFenced,
|
|
443
|
-
turn,
|
|
444
|
-
session,
|
|
445
|
-
turnExecutionPolicy,
|
|
446
|
-
trigger,
|
|
447
|
-
codexWorkspaceKey,
|
|
448
|
-
};
|
|
449
|
-
const codexCapacity = await selectCodexTurnCapacity(capacityDeps);
|
|
450
|
-
if ("exit" in codexCapacity) return codexCapacity.exit;
|
|
451
|
-
const xaiCapacity = await selectXaiTurnCapacity(capacityDeps);
|
|
452
|
-
if ("exit" in xaiCapacity) return xaiCapacity.exit;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
const governance = await prepareGovernanceAndModel({
|
|
456
|
-
input,
|
|
457
|
-
db,
|
|
458
|
-
runtime,
|
|
459
|
-
objectStorage,
|
|
460
|
-
eventing,
|
|
461
|
-
workspaceRefs,
|
|
462
|
-
media,
|
|
463
|
-
turn,
|
|
464
|
-
session,
|
|
465
|
-
capabilitySettings,
|
|
466
|
-
fileAuthoritySubjectId,
|
|
467
|
-
humanInputResume,
|
|
468
|
-
turnExecutionPolicy,
|
|
469
|
-
requiredGeneratedVideoFiles,
|
|
470
|
-
});
|
|
471
|
-
if ("exit" in governance) return governance.exit;
|
|
472
|
-
await assertModelConnectionAllowsTurn(db, {
|
|
426
|
+
const learning = await freezeAgentLearningPolicy(db, {
|
|
427
|
+
accountId: input.accountId,
|
|
473
428
|
workspaceId: input.workspaceId,
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
429
|
+
actor: {
|
|
430
|
+
kind: "agent",
|
|
431
|
+
sessionId: input.sessionId,
|
|
432
|
+
turnId: attempt.turnId,
|
|
433
|
+
attemptId: input.attemptId,
|
|
434
|
+
executionGeneration: attempt.executionGeneration,
|
|
435
|
+
},
|
|
478
436
|
});
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
verifiedRigProviderImageId,
|
|
492
|
-
runSettings,
|
|
493
|
-
resolvedModel,
|
|
494
|
-
providerApi,
|
|
495
|
-
nativeImageProviderBinding,
|
|
496
|
-
lazyToolTransport,
|
|
497
|
-
modelInputPolicy,
|
|
498
|
-
supportsImageInput,
|
|
499
|
-
modelHistoryProjector,
|
|
500
|
-
generatedImageHistoryProjector,
|
|
501
|
-
compactionModelHistoryProjector,
|
|
502
|
-
} = governance.ok;
|
|
437
|
+
return await withSessionRlsActorContext(
|
|
438
|
+
{
|
|
439
|
+
subjectId: "service:agent-turn",
|
|
440
|
+
initiatingHumanSubjectId: fileAuthoritySubjectId,
|
|
441
|
+
privateFileOwnerSubjectId:
|
|
442
|
+
learning.defaultScope === "personal" ? learning.subjectId : null,
|
|
443
|
+
},
|
|
444
|
+
async () => {
|
|
445
|
+
if (!leases.codex.holderId) {
|
|
446
|
+
throw new Error("Codex lease holder was not initialized");
|
|
447
|
+
}
|
|
448
|
+
const workerPreparationStartedAt = performance.now();
|
|
503
449
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
450
|
+
// Multi-account (P1): resolve the effective Codex account for this turn
|
|
451
|
+
// (session-pin > workspace active) and stamp it on the session so the
|
|
452
|
+
// in-session "Running on:" indicator reflects reality. Emit a switch event
|
|
453
|
+
// when it changed from the prior run's account so the pill flips live.
|
|
454
|
+
// Gated on the codex-billed predicate — non-codex turns never touch this.
|
|
455
|
+
{
|
|
456
|
+
const capacityDeps: CapacityPhaseDeps = {
|
|
457
|
+
input,
|
|
458
|
+
settings,
|
|
459
|
+
db,
|
|
460
|
+
bus,
|
|
461
|
+
observability,
|
|
462
|
+
wakeSessionWorkflow,
|
|
463
|
+
signalCodexCapacityWorkflow,
|
|
464
|
+
cancellationSignal,
|
|
465
|
+
dispatchId,
|
|
466
|
+
control,
|
|
467
|
+
attempt,
|
|
468
|
+
billingState,
|
|
469
|
+
eventing: wiredEventing,
|
|
470
|
+
providerTurn,
|
|
471
|
+
leases,
|
|
472
|
+
claimedResult,
|
|
473
|
+
acknowledgeLostAttemptOwnership,
|
|
474
|
+
acknowledgeRecoveryQuiescence,
|
|
475
|
+
setLastInputTokensFenced,
|
|
476
|
+
turn,
|
|
477
|
+
session,
|
|
478
|
+
turnExecutionPolicy,
|
|
479
|
+
trigger,
|
|
480
|
+
codexWorkspaceKey,
|
|
481
|
+
};
|
|
482
|
+
const codexCapacity = await selectCodexTurnCapacity(capacityDeps);
|
|
483
|
+
if ("exit" in codexCapacity) return codexCapacity.exit;
|
|
484
|
+
const xaiCapacity = await selectXaiTurnCapacity(capacityDeps);
|
|
485
|
+
if ("exit" in xaiCapacity) return xaiCapacity.exit;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const governance = await prepareGovernanceAndModel({
|
|
489
|
+
input,
|
|
490
|
+
db,
|
|
491
|
+
runtime,
|
|
492
|
+
objectStorage,
|
|
493
|
+
eventing,
|
|
494
|
+
workspaceRefs,
|
|
495
|
+
media,
|
|
496
|
+
turn,
|
|
497
|
+
session,
|
|
498
|
+
capabilitySettings,
|
|
499
|
+
fileAuthoritySubjectId,
|
|
500
|
+
humanInputResume,
|
|
501
|
+
turnExecutionPolicy,
|
|
502
|
+
requiredGeneratedVideoFiles,
|
|
503
|
+
});
|
|
504
|
+
if ("exit" in governance) return governance.exit;
|
|
505
|
+
await assertModelConnectionAllowsTurn(db, {
|
|
506
|
+
workspaceId: input.workspaceId,
|
|
507
|
+
subjectId: turn.initiatingHumanSubjectId ?? "worker:model-access",
|
|
508
|
+
modelId: turnExecutionPolicy.productModelId,
|
|
509
|
+
codexCredentialId: providerTurn.effectiveCodexCredentialId,
|
|
510
|
+
xaiCredentialId: providerTurn.effectiveXaiCredentialId,
|
|
511
|
+
});
|
|
512
|
+
const {
|
|
513
|
+
runtimePreparationStartedAt,
|
|
514
|
+
packRuntime,
|
|
515
|
+
rigVersion,
|
|
516
|
+
rigName,
|
|
517
|
+
agentHumanInputEnabled,
|
|
518
|
+
workspaceAgentInstructions,
|
|
519
|
+
workspaceGovernance,
|
|
520
|
+
structuredWorkspacePolicyActive,
|
|
521
|
+
workspaceMemory,
|
|
522
|
+
buildCompanyBrainContributionReceiptFor,
|
|
523
|
+
logicalSandboxSettings,
|
|
524
|
+
verifiedRigProviderImageId,
|
|
525
|
+
runSettings,
|
|
526
|
+
resolvedModel,
|
|
527
|
+
providerApi,
|
|
528
|
+
nativeImageProviderBinding,
|
|
529
|
+
lazyToolTransport,
|
|
530
|
+
modelInputPolicy,
|
|
531
|
+
supportsImageInput,
|
|
532
|
+
modelHistoryProjector,
|
|
533
|
+
generatedImageHistoryProjector,
|
|
534
|
+
compactionModelHistoryProjector,
|
|
535
|
+
} = governance.ok;
|
|
536
|
+
|
|
537
|
+
// A codex-subscription turn resolves the bearer for THIS turn's effective
|
|
538
|
+
// codex account (effectiveCodexCredentialId; pin > workspace-active) at
|
|
539
|
+
// model-call time — multi-account P1 means a workspace can hold N accounts,
|
|
540
|
+
// so the bearer is per-account, not per-workspace. codexSubscriptionFetch
|
|
541
|
+
// (on the provider's OpenAI client) reads this AsyncLocalStorage context.
|
|
542
|
+
// Build it once and wrap BOTH the compaction summarizer (a separate model
|
|
543
|
+
// call on the same codex client) and the main run; otherwise the summarizer
|
|
544
|
+
// would hit the codex backend unauthenticated.
|
|
545
|
+
let codexModelRequestSequence = 0;
|
|
546
|
+
let firstModelRequestAuditRecorded = false;
|
|
547
|
+
const firstModelRequestCheckpoints = new Set<string>();
|
|
548
|
+
const codexContext: CodexRequestContext | null =
|
|
549
|
+
resolvedModel?.provider.kind === "codex-subscription"
|
|
550
|
+
? ((): CodexRequestContext => {
|
|
551
|
+
// The empty-string fallback yields no row → null credential → the
|
|
552
|
+
// existing CodexReloginRequired path (a codex turn with no usable
|
|
553
|
+
// account fails closed, exactly as before multi-account).
|
|
554
|
+
const resolver = buildCodexTokenResolver(
|
|
555
|
+
db,
|
|
556
|
+
runSettings,
|
|
557
|
+
input.workspaceId,
|
|
558
|
+
providerTurn.effectiveCodexCredentialId ?? "",
|
|
559
|
+
);
|
|
560
|
+
const resolveTrackedToken = async (
|
|
561
|
+
resolve: () => ReturnType<typeof resolver.getToken>,
|
|
562
|
+
) => {
|
|
563
|
+
const token = await resolve();
|
|
564
|
+
providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
|
|
565
|
+
return token;
|
|
566
|
+
};
|
|
567
|
+
return {
|
|
568
|
+
clientVersion: CODEX_CLIENT_VERSION,
|
|
569
|
+
// Backend sticky cache-routing key — the SAME id as the body's
|
|
570
|
+
// prompt_cache_key (set from input.sessionId for codex turns),
|
|
571
|
+
// so routing and cache key agree. Without this header on the
|
|
572
|
+
// wire, byte-identical resends hit the prompt cache ~50%
|
|
573
|
+
// (per-request shard lottery = prod's measured 48.6% on sol);
|
|
574
|
+
// with it, resends pin to the warm shard (Codex CLI parity).
|
|
575
|
+
sessionId: input.sessionId,
|
|
576
|
+
getToken: () => resolveTrackedToken(resolver.getToken),
|
|
577
|
+
refresh: () => resolveTrackedToken(resolver.refresh),
|
|
578
|
+
resolveModel: buildModelResolver(
|
|
579
|
+
CODEX_FALLBACK_MODEL_SLUGS,
|
|
580
|
+
CODEX_FALLBACK_MODEL_SLUGS[0],
|
|
581
|
+
),
|
|
582
|
+
onUsageHeaders: (snapshot) => {
|
|
583
|
+
providerTurn.latestCodexUsage = snapshot;
|
|
584
|
+
}, // latest wins; flushed once in finally
|
|
585
|
+
beforeProviderDispatch: () => {
|
|
586
|
+
leases.codex.assertUsable();
|
|
587
|
+
},
|
|
588
|
+
onRequestPreparationDiagnostic: (phase) => {
|
|
589
|
+
if (
|
|
590
|
+
eventing.firstModelRequestCheckpointAt === null ||
|
|
591
|
+
firstModelRequestCheckpoints.has(phase)
|
|
592
|
+
) {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
const now = performance.now();
|
|
596
|
+
const metricPhase =
|
|
597
|
+
phase === "transport_entry"
|
|
598
|
+
? "model_sdk_serialization"
|
|
599
|
+
: phase === "credential_ready"
|
|
600
|
+
? "model_credential_resolution"
|
|
601
|
+
: "model_wire_normalization";
|
|
602
|
+
firstModelRequestCheckpoints.add(phase);
|
|
603
|
+
recordTurnStartupPhase(observability, {
|
|
604
|
+
phase: metricPhase,
|
|
605
|
+
provider: turnExecutionPolicy.providerId,
|
|
606
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
607
|
+
outcome: "completed",
|
|
608
|
+
durationSeconds: (now - eventing.firstModelRequestCheckpointAt) / 1_000,
|
|
609
|
+
count: turnTools.length,
|
|
610
|
+
});
|
|
611
|
+
eventing.firstModelRequestCheckpointAt = now;
|
|
612
|
+
},
|
|
613
|
+
onRequestOpaqueArtifacts: ({ fingerprints }) => {
|
|
614
|
+
providerTurn.lastCodexRequestOpaqueArtifacts = fingerprints;
|
|
615
|
+
},
|
|
616
|
+
onModelRequestDiagnostic: (event) => {
|
|
617
|
+
if (event.phase === "started")
|
|
618
|
+
sandboxState.firstProviderRequestStarted = true;
|
|
619
|
+
if (
|
|
620
|
+
event.phase === "started" &&
|
|
621
|
+
eventing.firstModelRequestPreparationStartedAt !== null &&
|
|
622
|
+
!eventing.firstModelRequestPreparationRecorded
|
|
623
|
+
) {
|
|
624
|
+
eventing.firstModelRequestPreparationRecorded = true;
|
|
625
|
+
recordTurnStartupPhase(observability, {
|
|
626
|
+
phase: "model_request_preparation",
|
|
627
|
+
provider: turnExecutionPolicy.providerId,
|
|
628
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
629
|
+
outcome: "completed",
|
|
630
|
+
durationSeconds:
|
|
631
|
+
(performance.now() - eventing.firstModelRequestPreparationStartedAt) /
|
|
632
|
+
1_000,
|
|
633
|
+
count: turnTools.length,
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
const phase =
|
|
637
|
+
event.phase === "headers"
|
|
638
|
+
? "headers"
|
|
639
|
+
: event.phase === "first_byte"
|
|
640
|
+
? "first_byte"
|
|
641
|
+
: event.phase === "completed" ||
|
|
642
|
+
event.phase === "failed" ||
|
|
643
|
+
event.phase === "timed_out"
|
|
644
|
+
? "terminal"
|
|
645
|
+
: null;
|
|
646
|
+
if (!phase) return;
|
|
647
|
+
recordModelRequestPhase(observability, {
|
|
648
|
+
provider: "codex-subscription",
|
|
649
|
+
phase,
|
|
650
|
+
...(event.phase === "completed"
|
|
651
|
+
? { outcome: "completed" as const }
|
|
652
|
+
: event.phase === "failed"
|
|
653
|
+
? { outcome: "failed" as const }
|
|
654
|
+
: event.phase === "timed_out"
|
|
655
|
+
? { outcome: "timed_out" as const }
|
|
656
|
+
: {}),
|
|
657
|
+
durationSeconds: event.durationMs / 1000,
|
|
658
|
+
});
|
|
659
|
+
},
|
|
660
|
+
nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
|
|
661
|
+
onModelRequestEvent: async (event) => {
|
|
662
|
+
if (!eventing.publish || !attempt.turnId) {
|
|
663
|
+
throw new Error(
|
|
664
|
+
"Codex model request started before the turn event producer",
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
if (event.phase === "started") {
|
|
668
|
+
await checkpointHistoryBeforeProviderDispatch(historySink);
|
|
669
|
+
}
|
|
670
|
+
const shouldRecordStartedAudit =
|
|
671
|
+
event.phase === "started" && !firstModelRequestAuditRecorded;
|
|
672
|
+
if (shouldRecordStartedAudit) {
|
|
673
|
+
firstModelRequestAuditRecorded = true;
|
|
674
|
+
}
|
|
675
|
+
const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
|
|
676
|
+
let auditOutcome: "completed" | "failed" = "completed";
|
|
677
|
+
try {
|
|
678
|
+
await eventing.publish([
|
|
679
|
+
...(shouldRecordStartedAudit &&
|
|
680
|
+
eventing.firstModelRequestPreparationStartedAt !== null
|
|
681
|
+
? [
|
|
682
|
+
{
|
|
683
|
+
type: "turn.startup.phase.completed" as const,
|
|
684
|
+
payload: {
|
|
685
|
+
phase: "model_preparation",
|
|
686
|
+
durationMs: Math.max(
|
|
687
|
+
0,
|
|
688
|
+
Math.round(
|
|
689
|
+
performance.now() -
|
|
690
|
+
eventing.firstModelRequestPreparationStartedAt,
|
|
691
|
+
),
|
|
692
|
+
),
|
|
693
|
+
},
|
|
694
|
+
},
|
|
695
|
+
]
|
|
696
|
+
: []),
|
|
697
|
+
{
|
|
698
|
+
type: "agent.model.request",
|
|
699
|
+
payload: {
|
|
700
|
+
...event,
|
|
701
|
+
provider: "codex-subscription",
|
|
702
|
+
turnId: attempt.turnId,
|
|
703
|
+
attemptId: input.attemptId,
|
|
704
|
+
dispatchId,
|
|
705
|
+
executionGeneration: attempt.executionGeneration,
|
|
706
|
+
},
|
|
707
|
+
},
|
|
708
|
+
]);
|
|
709
|
+
attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
|
|
710
|
+
attempt.providerRecoveryCount,
|
|
711
|
+
event.phase,
|
|
712
|
+
);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
auditOutcome = "failed";
|
|
715
|
+
throw error;
|
|
716
|
+
} finally {
|
|
717
|
+
if (auditStartedAt !== null) {
|
|
718
|
+
recordTurnStartupPhase(observability, {
|
|
719
|
+
phase: "model_request_audit",
|
|
720
|
+
provider: turnExecutionPolicy.providerId,
|
|
721
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
722
|
+
outcome: auditOutcome,
|
|
723
|
+
durationSeconds: (performance.now() - auditStartedAt) / 1_000,
|
|
724
|
+
count: turnTools.length,
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
},
|
|
729
|
+
};
|
|
730
|
+
})()
|
|
731
|
+
: null;
|
|
732
|
+
if (resolvedModel?.provider.kind === "xai-subscription") {
|
|
733
|
+
if (!providerTurn.effectiveXaiCredentialId || !leases.xai.subjectId) {
|
|
734
|
+
throw new Error("SuperGrok subscription execution has no leased credential");
|
|
735
|
+
}
|
|
736
|
+
let xaiModelRequestSequence = 0;
|
|
737
|
+
const authorization = await buildXaiTurnRequestAuthorization({
|
|
738
|
+
db,
|
|
739
|
+
settings: runSettings,
|
|
740
|
+
accountId: input.accountId,
|
|
741
|
+
workspaceId: input.workspaceId,
|
|
742
|
+
subjectId: leases.xai.subjectId,
|
|
743
|
+
sessionId: input.sessionId,
|
|
744
|
+
turnId: turn.id,
|
|
745
|
+
credentialId: providerTurn.effectiveXaiCredentialId,
|
|
746
|
+
authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot,
|
|
747
|
+
hostedSearch: {
|
|
748
|
+
webSearch: runSettings.webSearchEnabled,
|
|
749
|
+
xSearch: runSettings.webSearchEnabled,
|
|
750
|
+
},
|
|
751
|
+
streamIdleTimeoutMs: runSettings.supergrokResponseStreamIdleTimeoutMs,
|
|
752
|
+
nextRequestId: () => `${dispatchId}:xai:${++xaiModelRequestSequence}`,
|
|
753
|
+
onModelRequestDiagnostic: (event) => {
|
|
754
|
+
const requestKey = `${event.requestId}:${event.transportAttempt}`;
|
|
755
|
+
if (event.phase === "started") {
|
|
756
|
+
sandboxState.firstProviderRequestStarted = true;
|
|
757
|
+
modelRequestLifecycleMetricsFor(observability).start(
|
|
758
|
+
requestKey,
|
|
759
|
+
"supergrok-subscription",
|
|
760
|
+
);
|
|
585
761
|
if (
|
|
586
|
-
event.phase === "started" &&
|
|
587
762
|
eventing.firstModelRequestPreparationStartedAt !== null &&
|
|
588
763
|
!eventing.firstModelRequestPreparationRecorded
|
|
589
764
|
) {
|
|
@@ -599,983 +774,940 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
599
774
|
count: turnTools.length,
|
|
600
775
|
});
|
|
601
776
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
777
|
+
}
|
|
778
|
+
if (event.phase === "first_event" || event.phase === "progress") {
|
|
779
|
+
modelRequestLifecycleMetricsFor(observability).event(
|
|
780
|
+
requestKey,
|
|
781
|
+
event.phase === "progress" ? event.interEventGapMs : undefined,
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
const terminal =
|
|
785
|
+
event.phase === "completed" ||
|
|
786
|
+
event.phase === "failed" ||
|
|
787
|
+
event.phase === "timed_out";
|
|
788
|
+
if (terminal) {
|
|
789
|
+
modelRequestLifecycleMetricsFor(observability).finish(requestKey);
|
|
790
|
+
}
|
|
791
|
+
const phase =
|
|
792
|
+
event.phase === "headers"
|
|
793
|
+
? "headers"
|
|
794
|
+
: event.phase === "first_event"
|
|
795
|
+
? "first_byte"
|
|
796
|
+
: terminal
|
|
797
|
+
? "terminal"
|
|
798
|
+
: null;
|
|
799
|
+
if (!phase) return;
|
|
800
|
+
recordModelRequestPhase(observability, {
|
|
801
|
+
provider: "supergrok-subscription",
|
|
802
|
+
phase,
|
|
803
|
+
...(terminal
|
|
804
|
+
? {
|
|
805
|
+
outcome:
|
|
806
|
+
event.phase === "completed"
|
|
807
|
+
? ("completed" as const)
|
|
808
|
+
: event.phase === "timed_out"
|
|
809
|
+
? ("timed_out" as const)
|
|
810
|
+
: ("failed" as const),
|
|
811
|
+
}
|
|
812
|
+
: {}),
|
|
813
|
+
durationSeconds: event.durationMs / 1_000,
|
|
814
|
+
});
|
|
815
|
+
},
|
|
816
|
+
onModelRequestEvent: async (event) => {
|
|
817
|
+
if (!eventing.publish || !attempt.turnId) {
|
|
818
|
+
throw new Error("SuperGrok model request started before the turn event producer");
|
|
819
|
+
}
|
|
820
|
+
if (event.phase === "started") {
|
|
821
|
+
await checkpointHistoryBeforeProviderDispatch(historySink);
|
|
822
|
+
}
|
|
823
|
+
const shouldRecordStartedAudit =
|
|
824
|
+
event.phase === "started" && !firstModelRequestAuditRecorded;
|
|
825
|
+
if (shouldRecordStartedAudit) firstModelRequestAuditRecorded = true;
|
|
826
|
+
const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
|
|
827
|
+
let auditOutcome: "completed" | "failed" = "completed";
|
|
828
|
+
try {
|
|
829
|
+
await eventing.publish([
|
|
830
|
+
...(shouldRecordStartedAudit &&
|
|
831
|
+
eventing.firstModelRequestPreparationStartedAt !== null
|
|
832
|
+
? [
|
|
833
|
+
{
|
|
834
|
+
type: "turn.startup.phase.completed" as const,
|
|
835
|
+
payload: {
|
|
836
|
+
phase: "model_preparation",
|
|
837
|
+
durationMs: Math.max(
|
|
838
|
+
0,
|
|
839
|
+
Math.round(
|
|
840
|
+
performance.now() -
|
|
841
|
+
eventing.firstModelRequestPreparationStartedAt,
|
|
656
842
|
),
|
|
657
|
-
|
|
843
|
+
),
|
|
658
844
|
},
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
845
|
+
},
|
|
846
|
+
]
|
|
847
|
+
: []),
|
|
848
|
+
{
|
|
849
|
+
type: "agent.model.request",
|
|
850
|
+
payload: {
|
|
851
|
+
...event,
|
|
852
|
+
provider: "supergrok-subscription",
|
|
853
|
+
turnId: attempt.turnId,
|
|
854
|
+
attemptId: input.attemptId,
|
|
855
|
+
dispatchId,
|
|
856
|
+
executionGeneration: attempt.executionGeneration,
|
|
671
857
|
},
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
},
|
|
693
|
-
};
|
|
694
|
-
})()
|
|
695
|
-
: null;
|
|
696
|
-
if (resolvedModel?.provider.kind === "xai-subscription") {
|
|
697
|
-
if (!providerTurn.effectiveXaiCredentialId || !leases.xai.subjectId) {
|
|
698
|
-
throw new Error("SuperGrok subscription execution has no leased credential");
|
|
699
|
-
}
|
|
700
|
-
let xaiModelRequestSequence = 0;
|
|
701
|
-
const authorization = await buildXaiTurnRequestAuthorization({
|
|
702
|
-
db,
|
|
703
|
-
settings: runSettings,
|
|
704
|
-
accountId: input.accountId,
|
|
705
|
-
workspaceId: input.workspaceId,
|
|
706
|
-
subjectId: leases.xai.subjectId,
|
|
707
|
-
sessionId: input.sessionId,
|
|
708
|
-
turnId: turn.id,
|
|
709
|
-
credentialId: providerTurn.effectiveXaiCredentialId,
|
|
710
|
-
authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot,
|
|
711
|
-
hostedSearch: {
|
|
712
|
-
webSearch: runSettings.webSearchEnabled,
|
|
713
|
-
xSearch: runSettings.webSearchEnabled,
|
|
714
|
-
},
|
|
715
|
-
streamIdleTimeoutMs: runSettings.supergrokResponseStreamIdleTimeoutMs,
|
|
716
|
-
nextRequestId: () => `${dispatchId}:xai:${++xaiModelRequestSequence}`,
|
|
717
|
-
onModelRequestDiagnostic: (event) => {
|
|
718
|
-
const requestKey = `${event.requestId}:${event.transportAttempt}`;
|
|
719
|
-
if (event.phase === "started") {
|
|
720
|
-
sandboxState.firstProviderRequestStarted = true;
|
|
721
|
-
modelRequestLifecycleMetricsFor(observability).start(
|
|
722
|
-
requestKey,
|
|
723
|
-
"supergrok-subscription",
|
|
724
|
-
);
|
|
725
|
-
if (
|
|
726
|
-
eventing.firstModelRequestPreparationStartedAt !== null &&
|
|
727
|
-
!eventing.firstModelRequestPreparationRecorded
|
|
728
|
-
) {
|
|
729
|
-
eventing.firstModelRequestPreparationRecorded = true;
|
|
730
|
-
recordTurnStartupPhase(observability, {
|
|
731
|
-
phase: "model_request_preparation",
|
|
732
|
-
provider: turnExecutionPolicy.providerId,
|
|
733
|
-
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
734
|
-
outcome: "completed",
|
|
735
|
-
durationSeconds:
|
|
736
|
-
(performance.now() - eventing.firstModelRequestPreparationStartedAt) / 1_000,
|
|
737
|
-
count: turnTools.length,
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
if (event.phase === "first_event" || event.phase === "progress") {
|
|
742
|
-
modelRequestLifecycleMetricsFor(observability).event(
|
|
743
|
-
requestKey,
|
|
744
|
-
event.phase === "progress" ? event.interEventGapMs : undefined,
|
|
745
|
-
);
|
|
746
|
-
}
|
|
747
|
-
const terminal =
|
|
748
|
-
event.phase === "completed" ||
|
|
749
|
-
event.phase === "failed" ||
|
|
750
|
-
event.phase === "timed_out";
|
|
751
|
-
if (terminal) {
|
|
752
|
-
modelRequestLifecycleMetricsFor(observability).finish(requestKey);
|
|
753
|
-
}
|
|
754
|
-
const phase =
|
|
755
|
-
event.phase === "headers"
|
|
756
|
-
? "headers"
|
|
757
|
-
: event.phase === "first_event"
|
|
758
|
-
? "first_byte"
|
|
759
|
-
: terminal
|
|
760
|
-
? "terminal"
|
|
761
|
-
: null;
|
|
762
|
-
if (!phase) return;
|
|
763
|
-
recordModelRequestPhase(observability, {
|
|
764
|
-
provider: "supergrok-subscription",
|
|
765
|
-
phase,
|
|
766
|
-
...(terminal
|
|
767
|
-
? {
|
|
768
|
-
outcome:
|
|
769
|
-
event.phase === "completed"
|
|
770
|
-
? ("completed" as const)
|
|
771
|
-
: event.phase === "timed_out"
|
|
772
|
-
? ("timed_out" as const)
|
|
773
|
-
: ("failed" as const),
|
|
858
|
+
},
|
|
859
|
+
]);
|
|
860
|
+
attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
|
|
861
|
+
attempt.providerRecoveryCount,
|
|
862
|
+
event.phase,
|
|
863
|
+
);
|
|
864
|
+
} catch (error) {
|
|
865
|
+
auditOutcome = "failed";
|
|
866
|
+
throw error;
|
|
867
|
+
} finally {
|
|
868
|
+
if (auditStartedAt !== null) {
|
|
869
|
+
recordTurnStartupPhase(observability, {
|
|
870
|
+
phase: "model_request_audit",
|
|
871
|
+
provider: turnExecutionPolicy.providerId,
|
|
872
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
873
|
+
outcome: auditOutcome,
|
|
874
|
+
durationSeconds: (performance.now() - auditStartedAt) / 1_000,
|
|
875
|
+
count: turnTools.length,
|
|
876
|
+
});
|
|
774
877
|
}
|
|
775
|
-
|
|
776
|
-
|
|
878
|
+
}
|
|
879
|
+
},
|
|
777
880
|
});
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
: []),
|
|
881
|
+
providerTurn.xaiRequestContext = authorization.context;
|
|
882
|
+
}
|
|
883
|
+
const withCodex = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
884
|
+
codexContext ? codexRequestStorage.run(codexContext, fn) : fn();
|
|
885
|
+
const withProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
886
|
+
providerTurn.xaiRequestContext
|
|
887
|
+
? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn)
|
|
888
|
+
: withCodex(fn);
|
|
889
|
+
let codexSessionTitleRequestSequence = 0;
|
|
890
|
+
let xaiSessionTitleRequestSequence = 0;
|
|
891
|
+
const codexSessionTitleContext = codexContext
|
|
892
|
+
? sessionTitleCodexRequestContext(
|
|
893
|
+
codexContext,
|
|
894
|
+
() => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`,
|
|
895
|
+
)
|
|
896
|
+
: null;
|
|
897
|
+
const xaiSessionTitleContext = providerTurn.xaiRequestContext
|
|
898
|
+
? sessionTitleXaiRequestContext(
|
|
899
|
+
providerTurn.xaiRequestContext,
|
|
900
|
+
() => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`,
|
|
901
|
+
)
|
|
902
|
+
: null;
|
|
903
|
+
const withSessionTitleProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
904
|
+
xaiSessionTitleContext
|
|
905
|
+
? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn)
|
|
906
|
+
: codexSessionTitleContext
|
|
907
|
+
? codexRequestStorage.run(codexSessionTitleContext, fn)
|
|
908
|
+
: fn();
|
|
909
|
+
const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
910
|
+
withCodex(() =>
|
|
911
|
+
withCodexRequestOverrides(
|
|
810
912
|
{
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
executionGeneration: attempt.executionGeneration,
|
|
913
|
+
betaFeatures: [REMOTE_COMPACTION_V2_BETA_FEATURE],
|
|
914
|
+
turnMetadata: {
|
|
915
|
+
request_kind: "compaction",
|
|
916
|
+
compaction: {
|
|
917
|
+
implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
|
|
918
|
+
strategy: "memento",
|
|
919
|
+
},
|
|
819
920
|
},
|
|
820
921
|
},
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
922
|
+
fn,
|
|
923
|
+
),
|
|
924
|
+
);
|
|
925
|
+
const compactionPrep = await prepareCompaction({
|
|
926
|
+
input,
|
|
927
|
+
settings: capabilitySettings,
|
|
928
|
+
db,
|
|
929
|
+
bus,
|
|
930
|
+
observability,
|
|
931
|
+
cancellationSignal,
|
|
932
|
+
control,
|
|
933
|
+
attempt,
|
|
934
|
+
billingState,
|
|
935
|
+
eventing: wiredEventing,
|
|
936
|
+
providerTurn,
|
|
937
|
+
leases,
|
|
938
|
+
media,
|
|
939
|
+
claimedResult,
|
|
940
|
+
claimedModelUsageSourceKeys,
|
|
941
|
+
emittedModelUsageSourceKeys,
|
|
942
|
+
modelUsageDispatchId,
|
|
943
|
+
turn,
|
|
944
|
+
session,
|
|
945
|
+
turnExecutionPolicy,
|
|
946
|
+
resolvedModel,
|
|
947
|
+
workspaceAgentInstructions,
|
|
948
|
+
workspaceGovernance,
|
|
949
|
+
structuredWorkspacePolicyActive,
|
|
950
|
+
workspaceMemory,
|
|
951
|
+
rigVersion,
|
|
952
|
+
rigName,
|
|
953
|
+
compactionModelHistoryProjector,
|
|
954
|
+
summarizeContextForCompaction,
|
|
955
|
+
withProviderRequestContext,
|
|
956
|
+
withCodexRemoteCompaction,
|
|
957
|
+
});
|
|
958
|
+
if ("exit" in compactionPrep) return compactionPrep.exit;
|
|
959
|
+
const {
|
|
960
|
+
promptCacheKey,
|
|
961
|
+
remotePrefix,
|
|
962
|
+
remoteCompactionRequester,
|
|
963
|
+
publishCompactionLiveEvents,
|
|
964
|
+
publishCompactionOutcomeEvents,
|
|
965
|
+
compactionModeOptions,
|
|
966
|
+
compactionOnlyTurn,
|
|
967
|
+
compactionSummarizerFor,
|
|
968
|
+
settleDeferredSteerAfterCompaction,
|
|
969
|
+
} = compactionPrep.ok;
|
|
970
|
+
|
|
971
|
+
const toolPolicy = await prepareTurnToolPolicy({
|
|
972
|
+
input,
|
|
973
|
+
db,
|
|
974
|
+
cancellationSignal,
|
|
975
|
+
connectionCredentials,
|
|
976
|
+
turn,
|
|
977
|
+
session,
|
|
978
|
+
fileAuthoritySubjectId,
|
|
979
|
+
capabilitySettings,
|
|
980
|
+
runSettings,
|
|
981
|
+
rigVersion,
|
|
982
|
+
workspaceRefs,
|
|
983
|
+
});
|
|
984
|
+
const {
|
|
985
|
+
turnResources: claimedTurnResources,
|
|
986
|
+
runtimeResources: claimedRuntimeResources,
|
|
987
|
+
mcpAvailabilityNote,
|
|
988
|
+
turnTools,
|
|
989
|
+
connectionScope,
|
|
990
|
+
workspaceVariableSet,
|
|
991
|
+
sandboxWorkspaceEnvironmentValues,
|
|
992
|
+
} = toolPolicy;
|
|
993
|
+
|
|
994
|
+
let knowledgeRecoveryNote: string | undefined;
|
|
995
|
+
try {
|
|
996
|
+
// An already-answered pre-cutover confirmation is human-authorized
|
|
997
|
+
// protocol recovery, independent of new tool selection or learning mode.
|
|
998
|
+
const recovered = await recoverLegacyKnowledgeConfirmations(db, {
|
|
999
|
+
accountId: input.accountId,
|
|
1000
|
+
workspaceId: input.workspaceId,
|
|
1001
|
+
actor: {
|
|
1002
|
+
kind: "agent",
|
|
1003
|
+
sessionId: input.sessionId,
|
|
1004
|
+
turnId: turn.id,
|
|
1005
|
+
attemptId: input.attemptId,
|
|
1006
|
+
executionGeneration: attempt.executionGeneration,
|
|
1007
|
+
},
|
|
1008
|
+
});
|
|
1009
|
+
if (
|
|
1010
|
+
recovered.receipts.length ||
|
|
1011
|
+
recovered.instructionReceipts.length ||
|
|
1012
|
+
recovered.unavailable
|
|
1013
|
+
) {
|
|
1014
|
+
knowledgeRecoveryNote = `[OpenGeni legacy confirmation recovery]\n${JSON.stringify(recovered)}\nThese are publication receipts for the human's earlier answer. Do not call the retired remember tools or duplicate these entries. Unavailable confirmations were not published. Inspect the current Knowledge or instruction review before taking further action.`;
|
|
1015
|
+
for (const receipt of recovered.instructionReceipts) {
|
|
1016
|
+
await eventing.publish!(
|
|
1017
|
+
[{ type: "instruction.confirmation.recovered", payload: { receipt } }],
|
|
1018
|
+
true,
|
|
1019
|
+
);
|
|
1020
|
+
}
|
|
1021
|
+
for (const receipt of recovered.receipts) {
|
|
1022
|
+
await eventing.publish!(
|
|
1023
|
+
[{ type: "knowledge.confirmation.recovered", payload: { receipt } }],
|
|
1024
|
+
true,
|
|
1025
|
+
);
|
|
839
1026
|
}
|
|
840
1027
|
}
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
codexContext,
|
|
856
|
-
() => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`,
|
|
857
|
-
)
|
|
858
|
-
: null;
|
|
859
|
-
const xaiSessionTitleContext = providerTurn.xaiRequestContext
|
|
860
|
-
? sessionTitleXaiRequestContext(
|
|
861
|
-
providerTurn.xaiRequestContext,
|
|
862
|
-
() => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`,
|
|
863
|
-
)
|
|
864
|
-
: null;
|
|
865
|
-
const withSessionTitleProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
866
|
-
xaiSessionTitleContext
|
|
867
|
-
? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn)
|
|
868
|
-
: codexSessionTitleContext
|
|
869
|
-
? codexRequestStorage.run(codexSessionTitleContext, fn)
|
|
870
|
-
: fn();
|
|
871
|
-
const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
|
|
872
|
-
withCodex(() =>
|
|
873
|
-
withCodexRequestOverrides(
|
|
874
|
-
{
|
|
875
|
-
betaFeatures: [REMOTE_COMPACTION_V2_BETA_FEATURE],
|
|
876
|
-
turnMetadata: {
|
|
877
|
-
request_kind: "compaction",
|
|
878
|
-
compaction: {
|
|
879
|
-
implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
|
|
880
|
-
strategy: "memento",
|
|
881
|
-
},
|
|
882
|
-
},
|
|
1028
|
+
} catch {
|
|
1029
|
+
cancellationSignal?.throwIfAborted();
|
|
1030
|
+
knowledgeRecoveryNote =
|
|
1031
|
+
"[OpenGeni confirmation recovery unavailable] Do not assume an earlier remember confirmation was published. Inspect the current Knowledge or instruction review.";
|
|
1032
|
+
}
|
|
1033
|
+
const attachmentPreparationNote = await prepareTurnKnowledgeSources({
|
|
1034
|
+
settings: runSettings,
|
|
1035
|
+
scope: {
|
|
1036
|
+
accountId: input.accountId,
|
|
1037
|
+
workspaceId: input.workspaceId,
|
|
1038
|
+
sessionId: input.sessionId,
|
|
1039
|
+
turnId: turn.id,
|
|
1040
|
+
attemptId: input.attemptId,
|
|
1041
|
+
executionGeneration: attempt.executionGeneration,
|
|
883
1042
|
},
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
resolvedModel,
|
|
909
|
-
workspaceAgentInstructions,
|
|
910
|
-
workspaceGovernance,
|
|
911
|
-
structuredWorkspacePolicyActive,
|
|
912
|
-
workspaceMemory,
|
|
913
|
-
rigVersion,
|
|
914
|
-
rigName,
|
|
915
|
-
compactionModelHistoryProjector,
|
|
916
|
-
summarizeContextForCompaction,
|
|
917
|
-
withProviderRequestContext,
|
|
918
|
-
withCodexRemoteCompaction,
|
|
919
|
-
});
|
|
920
|
-
if ("exit" in compactionPrep) return compactionPrep.exit;
|
|
921
|
-
const {
|
|
922
|
-
promptCacheKey,
|
|
923
|
-
remotePrefix,
|
|
924
|
-
remoteCompactionRequester,
|
|
925
|
-
publishCompactionLiveEvents,
|
|
926
|
-
publishCompactionOutcomeEvents,
|
|
927
|
-
compactionModeOptions,
|
|
928
|
-
compactionOnlyTurn,
|
|
929
|
-
compactionSummarizerFor,
|
|
930
|
-
settleDeferredSteerAfterCompaction,
|
|
931
|
-
} = compactionPrep.ok;
|
|
932
|
-
|
|
933
|
-
const toolPolicy = await prepareTurnToolPolicy({
|
|
934
|
-
input,
|
|
935
|
-
db,
|
|
936
|
-
cancellationSignal,
|
|
937
|
-
connectionCredentials,
|
|
938
|
-
turn,
|
|
939
|
-
session,
|
|
940
|
-
fileAuthoritySubjectId,
|
|
941
|
-
capabilitySettings,
|
|
942
|
-
runSettings,
|
|
943
|
-
rigVersion,
|
|
944
|
-
workspaceRefs,
|
|
945
|
-
});
|
|
946
|
-
const {
|
|
947
|
-
turnResources: claimedTurnResources,
|
|
948
|
-
runtimeResources: claimedRuntimeResources,
|
|
949
|
-
mcpAvailabilityNote,
|
|
950
|
-
turnTools,
|
|
951
|
-
connectionScope,
|
|
952
|
-
workspaceVariableSet,
|
|
953
|
-
sandboxWorkspaceEnvironmentValues,
|
|
954
|
-
} = toolPolicy;
|
|
955
|
-
|
|
956
|
-
const sandboxRoute = await resolveSandboxRoute({
|
|
957
|
-
input,
|
|
958
|
-
settings,
|
|
959
|
-
db,
|
|
960
|
-
eventing,
|
|
961
|
-
sandboxState,
|
|
962
|
-
media,
|
|
963
|
-
fileAuthoritySubjectId,
|
|
964
|
-
runSettings,
|
|
965
|
-
logicalSandboxSettings,
|
|
966
|
-
});
|
|
967
|
-
const {
|
|
968
|
-
routingOn,
|
|
969
|
-
activeSandboxBackend,
|
|
970
|
-
machinePrimary,
|
|
971
|
-
groupBoxBackend,
|
|
972
|
-
sandboxCreationBackend,
|
|
973
|
-
effectiveRunCredentialBackend,
|
|
974
|
-
} = sandboxRoute;
|
|
1043
|
+
// Only attachments on this accepted turn are new source inputs.
|
|
1044
|
+
// Session-level file receipts remain historical context and must
|
|
1045
|
+
// never trigger metadata reads, downloads, or re-preparation.
|
|
1046
|
+
resources: turn.resources,
|
|
1047
|
+
selectedTools: session.firstPartyMcpTools,
|
|
1048
|
+
permissions: session.firstPartyMcpPermissions,
|
|
1049
|
+
learningMode: learning.effective.knowledge,
|
|
1050
|
+
signal: cancellationSignal,
|
|
1051
|
+
onOutcome: async (outcome) => {
|
|
1052
|
+
if (outcome.status === "disabled") return;
|
|
1053
|
+
await eventing.publish!(
|
|
1054
|
+
[
|
|
1055
|
+
{
|
|
1056
|
+
type:
|
|
1057
|
+
outcome.status === "failed"
|
|
1058
|
+
? "knowledge.source.failed"
|
|
1059
|
+
: "knowledge.source.prepared",
|
|
1060
|
+
payload: outcome,
|
|
1061
|
+
},
|
|
1062
|
+
],
|
|
1063
|
+
true,
|
|
1064
|
+
);
|
|
1065
|
+
},
|
|
1066
|
+
});
|
|
975
1067
|
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
// and the runtime clone plan derive binding ids from the same resource
|
|
980
|
-
// set. A Connected Machine receives no platform Git credential, so its
|
|
981
|
-
// resources stay exactly as stored. Resolution never fails the turn;
|
|
982
|
-
// an unusable bound repository stays bare and is reported visibly.
|
|
983
|
-
const { turnResources, runtimeResources } = await waitForTurnOperation(
|
|
984
|
-
applyTurnGitHubRepositoryBindings({
|
|
985
|
-
db,
|
|
986
|
-
settings: runSettings,
|
|
987
|
-
workspaceId: input.workspaceId,
|
|
988
|
-
sessionId: input.sessionId,
|
|
989
|
-
activeSandboxBackend,
|
|
990
|
-
claimedTurnResources,
|
|
991
|
-
claimedRuntimeResources,
|
|
992
|
-
publish: async (events) => {
|
|
993
|
-
await eventing.publish!(events, true);
|
|
994
|
-
},
|
|
995
|
-
warn: (message, fields) => observability.warn(message, fields),
|
|
996
|
-
}),
|
|
997
|
-
cancellationSignal,
|
|
998
|
-
undefined,
|
|
999
|
-
);
|
|
1068
|
+
const knowledgeSourcePreparationNote =
|
|
1069
|
+
[knowledgeRecoveryNote, attachmentPreparationNote].filter(Boolean).join("\n\n") ||
|
|
1070
|
+
undefined;
|
|
1000
1071
|
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
requiredGeneratedVideoFiles,
|
|
1021
|
-
machinePrimary,
|
|
1022
|
-
activeSandboxBackend,
|
|
1023
|
-
groupBoxBackend,
|
|
1024
|
-
sandboxCreationBackend,
|
|
1025
|
-
effectiveRunCredentialBackend,
|
|
1026
|
-
sandboxWorkspaceEnvironmentValues,
|
|
1027
|
-
connectionScope,
|
|
1028
|
-
runWorkspaceMutationForSandbox,
|
|
1029
|
-
});
|
|
1030
|
-
const {
|
|
1031
|
-
runCredentialResolver,
|
|
1032
|
-
establishPolicy,
|
|
1033
|
-
initialRunCredentialMaterial,
|
|
1034
|
-
runCredentialsNote,
|
|
1035
|
-
hostCredentialRootSessionId,
|
|
1036
|
-
codemodeAuthority,
|
|
1037
|
-
sandboxArtifactRuntime,
|
|
1038
|
-
sandboxEnvironment,
|
|
1039
|
-
sandboxGitToken,
|
|
1040
|
-
sandboxGitTokens,
|
|
1041
|
-
sandboxGitCredentialBindings,
|
|
1042
|
-
sandboxCodemodeToken,
|
|
1043
|
-
sandboxCodemodeTokenExpiresAt,
|
|
1044
|
-
transientCodemodeEnvironment,
|
|
1045
|
-
initialGitCredentials,
|
|
1046
|
-
startRunGitCredentialsMint,
|
|
1047
|
-
attachGitCredentialRenewal,
|
|
1048
|
-
attachCodemodeTokenRenewal,
|
|
1049
|
-
attachRunCredentialRenewal,
|
|
1050
|
-
} = runCredentials;
|
|
1051
|
-
|
|
1052
|
-
await establishTurnSandbox({
|
|
1053
|
-
...sandboxRoute,
|
|
1054
|
-
input,
|
|
1055
|
-
settings,
|
|
1056
|
-
db,
|
|
1057
|
-
bus,
|
|
1058
|
-
objectStorage,
|
|
1059
|
-
observability,
|
|
1060
|
-
cancellationSignal,
|
|
1061
|
-
runtimeCancellationSignal,
|
|
1062
|
-
sandboxResumeSignal,
|
|
1063
|
-
activityContext,
|
|
1064
|
-
opJournal,
|
|
1065
|
-
sandboxState,
|
|
1066
|
-
eventing,
|
|
1067
|
-
attempt,
|
|
1068
|
-
turn,
|
|
1069
|
-
session,
|
|
1070
|
-
fileAuthoritySubjectId,
|
|
1071
|
-
capabilitySettings,
|
|
1072
|
-
turnExecutionPolicy,
|
|
1073
|
-
runSettings,
|
|
1074
|
-
logicalSandboxSettings,
|
|
1075
|
-
verifiedRigProviderImageId,
|
|
1076
|
-
runtimePreparationStartedAt,
|
|
1077
|
-
establishPolicy,
|
|
1078
|
-
sandboxEnvironment,
|
|
1079
|
-
startRunGitCredentialsMint,
|
|
1080
|
-
machineOpObserver,
|
|
1081
|
-
sandboxOperationObserver,
|
|
1082
|
-
sandboxRuntime,
|
|
1083
|
-
transientCodemodeEnvironment,
|
|
1084
|
-
rigVersion,
|
|
1085
|
-
turnResources,
|
|
1086
|
-
});
|
|
1072
|
+
const sandboxRoute = await resolveSandboxRoute({
|
|
1073
|
+
input,
|
|
1074
|
+
settings,
|
|
1075
|
+
db,
|
|
1076
|
+
eventing,
|
|
1077
|
+
sandboxState,
|
|
1078
|
+
media,
|
|
1079
|
+
fileAuthoritySubjectId,
|
|
1080
|
+
runSettings,
|
|
1081
|
+
logicalSandboxSettings,
|
|
1082
|
+
});
|
|
1083
|
+
const {
|
|
1084
|
+
routingOn,
|
|
1085
|
+
activeSandboxBackend,
|
|
1086
|
+
machinePrimary,
|
|
1087
|
+
groupBoxBackend,
|
|
1088
|
+
sandboxCreationBackend,
|
|
1089
|
+
effectiveRunCredentialBackend,
|
|
1090
|
+
} = sandboxRoute;
|
|
1087
1091
|
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1092
|
+
// A bare github.com repository URI (API caller, older session, or an
|
|
1093
|
+
// agent-spawned child inheriting its parent's resources) resolves to
|
|
1094
|
+
// the workspace's GitHub App binding here, before credential minting
|
|
1095
|
+
// and the runtime clone plan derive binding ids from the same resource
|
|
1096
|
+
// set. A Connected Machine receives no platform Git credential, so its
|
|
1097
|
+
// resources stay exactly as stored. Resolution never fails the turn;
|
|
1098
|
+
// an unusable bound repository stays bare and is reported visibly.
|
|
1099
|
+
const { turnResources, runtimeResources } = await waitForTurnOperation(
|
|
1100
|
+
applyTurnGitHubRepositoryBindings({
|
|
1095
1101
|
db,
|
|
1096
|
-
|
|
1097
|
-
input.
|
|
1098
|
-
input.
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1102
|
+
settings: runSettings,
|
|
1103
|
+
workspaceId: input.workspaceId,
|
|
1104
|
+
sessionId: input.sessionId,
|
|
1105
|
+
activeSandboxBackend,
|
|
1106
|
+
claimedTurnResources,
|
|
1107
|
+
claimedRuntimeResources,
|
|
1108
|
+
publish: async (events) => {
|
|
1109
|
+
await eventing.publish!(events, true);
|
|
1110
|
+
},
|
|
1111
|
+
warn: (message, fields) => observability.warn(message, fields),
|
|
1112
|
+
}),
|
|
1103
1113
|
cancellationSignal,
|
|
1104
1114
|
undefined,
|
|
1105
1115
|
);
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1116
|
+
|
|
1117
|
+
const runCredentials = await prepareRunCredentials({
|
|
1118
|
+
input,
|
|
1119
|
+
settings,
|
|
1120
|
+
db,
|
|
1121
|
+
observability,
|
|
1122
|
+
cancellationSignal,
|
|
1123
|
+
connectionCredentials,
|
|
1124
|
+
personalGitHubCredentials,
|
|
1125
|
+
eventing,
|
|
1126
|
+
attempt,
|
|
1127
|
+
renewals,
|
|
1128
|
+
sandboxState,
|
|
1129
|
+
sandboxRuntime,
|
|
1130
|
+
turn,
|
|
1131
|
+
session,
|
|
1132
|
+
fileAuthoritySubjectId,
|
|
1133
|
+
runSettings,
|
|
1134
|
+
workspaceVariableSet,
|
|
1135
|
+
turnResources,
|
|
1136
|
+
requiredGeneratedVideoFiles,
|
|
1137
|
+
machinePrimary,
|
|
1138
|
+
activeSandboxBackend,
|
|
1139
|
+
groupBoxBackend,
|
|
1140
|
+
sandboxCreationBackend,
|
|
1141
|
+
effectiveRunCredentialBackend,
|
|
1142
|
+
sandboxWorkspaceEnvironmentValues,
|
|
1143
|
+
connectionScope,
|
|
1144
|
+
runWorkspaceMutationForSandbox,
|
|
1117
1145
|
});
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1146
|
+
const {
|
|
1147
|
+
runCredentialResolver,
|
|
1148
|
+
establishPolicy,
|
|
1149
|
+
initialRunCredentialMaterial,
|
|
1150
|
+
runCredentialsNote,
|
|
1151
|
+
hostCredentialRootSessionId,
|
|
1152
|
+
codemodeAuthority,
|
|
1153
|
+
sandboxArtifactRuntime,
|
|
1154
|
+
sandboxEnvironment,
|
|
1155
|
+
sandboxGitToken,
|
|
1156
|
+
sandboxGitTokens,
|
|
1157
|
+
sandboxGitCredentialBindings,
|
|
1158
|
+
sandboxCodemodeToken,
|
|
1159
|
+
sandboxCodemodeTokenExpiresAt,
|
|
1160
|
+
transientCodemodeEnvironment,
|
|
1161
|
+
initialGitCredentials,
|
|
1162
|
+
startRunGitCredentialsMint,
|
|
1163
|
+
attachGitCredentialRenewal,
|
|
1164
|
+
attachCodemodeTokenRenewal,
|
|
1165
|
+
attachRunCredentialRenewal,
|
|
1166
|
+
} = runCredentials;
|
|
1167
|
+
|
|
1168
|
+
await establishTurnSandbox({
|
|
1169
|
+
...sandboxRoute,
|
|
1170
|
+
input,
|
|
1171
|
+
settings,
|
|
1172
|
+
db,
|
|
1173
|
+
bus,
|
|
1174
|
+
objectStorage,
|
|
1175
|
+
observability,
|
|
1176
|
+
cancellationSignal,
|
|
1177
|
+
runtimeCancellationSignal,
|
|
1178
|
+
sandboxResumeSignal,
|
|
1179
|
+
activityContext,
|
|
1180
|
+
opJournal,
|
|
1181
|
+
sandboxState,
|
|
1182
|
+
eventing,
|
|
1183
|
+
attempt,
|
|
1184
|
+
turn,
|
|
1185
|
+
session,
|
|
1186
|
+
fileAuthoritySubjectId,
|
|
1187
|
+
capabilitySettings,
|
|
1188
|
+
turnExecutionPolicy,
|
|
1189
|
+
runSettings,
|
|
1190
|
+
logicalSandboxSettings,
|
|
1191
|
+
verifiedRigProviderImageId,
|
|
1192
|
+
runtimePreparationStartedAt,
|
|
1193
|
+
establishPolicy,
|
|
1194
|
+
sandboxEnvironment,
|
|
1195
|
+
startRunGitCredentialsMint,
|
|
1196
|
+
machineOpObserver,
|
|
1197
|
+
sandboxOperationObserver,
|
|
1198
|
+
sandboxRuntime,
|
|
1199
|
+
transientCodemodeEnvironment,
|
|
1200
|
+
rigVersion,
|
|
1201
|
+
turnResources,
|
|
1133
1202
|
});
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1203
|
+
|
|
1204
|
+
const ordinaryFileResourceDownloads = await (async () => {
|
|
1205
|
+
const fileResolutionStartedAt = performance.now();
|
|
1206
|
+
let fileResolutionOutcome: "completed" | "failed" = "completed";
|
|
1207
|
+
try {
|
|
1208
|
+
return await waitForTurnOperation(
|
|
1209
|
+
sandboxFileDownloadsForRun(
|
|
1210
|
+
runSettings,
|
|
1211
|
+
db,
|
|
1212
|
+
objectStorage,
|
|
1213
|
+
input.accountId,
|
|
1214
|
+
input.workspaceId,
|
|
1215
|
+
fileAuthoritySubjectId,
|
|
1216
|
+
turn.resources,
|
|
1217
|
+
activeSandboxBackend ?? groupBoxBackend,
|
|
1218
|
+
),
|
|
1219
|
+
cancellationSignal,
|
|
1220
|
+
undefined,
|
|
1221
|
+
);
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
fileResolutionOutcome = "failed";
|
|
1224
|
+
throw error;
|
|
1225
|
+
} finally {
|
|
1226
|
+
recordTurnStartupPhase(observability, {
|
|
1227
|
+
phase: "file_resolution",
|
|
1228
|
+
provider: turnExecutionPolicy.providerId,
|
|
1229
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
1230
|
+
outcome: fileResolutionOutcome,
|
|
1231
|
+
durationSeconds: (performance.now() - fileResolutionStartedAt) / 1_000,
|
|
1232
|
+
count: turn.resources.length,
|
|
1233
|
+
});
|
|
1234
|
+
}
|
|
1235
|
+
})();
|
|
1236
|
+
if (requiredGeneratedVideoFiles.length > 0 && !objectStorage) {
|
|
1237
|
+
throw new Error("Generated video materialization requires object storage");
|
|
1238
|
+
}
|
|
1239
|
+
const generatedVideoDownloads: SandboxFileDownload[] = [];
|
|
1240
|
+
if (objectStorage && requiredGeneratedVideoFiles.length > 0) {
|
|
1241
|
+
const downloadStorage = objectStorageForSandboxDownloads(
|
|
1242
|
+
eventing.modelRunSettings,
|
|
1243
|
+
objectStorage,
|
|
1244
|
+
media.sandboxFileDownloadBackend,
|
|
1245
|
+
);
|
|
1246
|
+
for (const file of requiredGeneratedVideoFiles) {
|
|
1247
|
+
const signed = await downloadStorage.createGetUrl({
|
|
1248
|
+
key: file.objectKey,
|
|
1249
|
+
});
|
|
1250
|
+
generatedVideoDownloads.push({
|
|
1251
|
+
fileId: file.fileId,
|
|
1252
|
+
mountPath: "generated-videos",
|
|
1253
|
+
filename: file.filename,
|
|
1254
|
+
url: signed.url,
|
|
1255
|
+
expiresAt: signed.expiresAt,
|
|
1256
|
+
sizeBytes: file.sizeBytes,
|
|
1257
|
+
sha256: file.sha256,
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
const fileResourceDownloads = [
|
|
1262
|
+
...new Map(
|
|
1263
|
+
[...ordinaryFileResourceDownloads, ...generatedVideoDownloads].map((download) => [
|
|
1264
|
+
download.fileId,
|
|
1265
|
+
download,
|
|
1266
|
+
]),
|
|
1267
|
+
).values(),
|
|
1268
|
+
];
|
|
1269
|
+
const toolRuntime = await prepareTurnToolRuntime({
|
|
1270
|
+
fetchKnowledgeSource: sourceActivities.runKnowledgeSourceSyncBatch,
|
|
1271
|
+
selectedSkillActivations: packRuntime.skillActivations,
|
|
1272
|
+
input,
|
|
1273
|
+
catalogSourceSettings,
|
|
1274
|
+
db,
|
|
1275
|
+
bus,
|
|
1276
|
+
runtime,
|
|
1277
|
+
objectStorage,
|
|
1278
|
+
observability,
|
|
1279
|
+
cancellationSignal,
|
|
1280
|
+
connectionCredentials,
|
|
1281
|
+
eventing,
|
|
1282
|
+
attempt,
|
|
1283
|
+
sandboxState,
|
|
1284
|
+
media,
|
|
1285
|
+
toolResultSpill,
|
|
1286
|
+
turn,
|
|
1287
|
+
session,
|
|
1288
|
+
fileAuthoritySubjectId,
|
|
1289
|
+
capabilitySettings,
|
|
1290
|
+
installedApiIntegrations,
|
|
1291
|
+
codexAppsCredentialId,
|
|
1292
|
+
turnExecutionPolicy,
|
|
1293
|
+
trigger,
|
|
1294
|
+
runSettings,
|
|
1295
|
+
lazyToolTransport,
|
|
1296
|
+
turnTools,
|
|
1297
|
+
connectionScope,
|
|
1298
|
+
hostCredentialRootSessionId,
|
|
1299
|
+
sandboxArtifactRuntime,
|
|
1300
|
+
activeSandboxBackend,
|
|
1301
|
+
groupBoxBackend,
|
|
1302
|
+
routingOn,
|
|
1303
|
+
runtimeCancellationSignal,
|
|
1304
|
+
credentialSubjectId,
|
|
1305
|
+
interactionInterventionResume,
|
|
1306
|
+
runWorkspaceMutationForSandbox,
|
|
1307
|
+
throwIfWorkerShuttingDown,
|
|
1308
|
+
throwIfTurnCancelled,
|
|
1142
1309
|
});
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
]),
|
|
1151
|
-
).values(),
|
|
1152
|
-
];
|
|
1153
|
-
const toolRuntime = await prepareTurnToolRuntime({
|
|
1154
|
-
selectedSkillActivations: packRuntime.skillActivations,
|
|
1155
|
-
input,
|
|
1156
|
-
catalogSourceSettings,
|
|
1157
|
-
db,
|
|
1158
|
-
bus,
|
|
1159
|
-
runtime,
|
|
1160
|
-
objectStorage,
|
|
1161
|
-
observability,
|
|
1162
|
-
cancellationSignal,
|
|
1163
|
-
connectionCredentials,
|
|
1164
|
-
eventing,
|
|
1165
|
-
attempt,
|
|
1166
|
-
sandboxState,
|
|
1167
|
-
media,
|
|
1168
|
-
toolResultSpill,
|
|
1169
|
-
turn,
|
|
1170
|
-
session,
|
|
1171
|
-
fileAuthoritySubjectId,
|
|
1172
|
-
capabilitySettings,
|
|
1173
|
-
installedApiIntegrations,
|
|
1174
|
-
codexAppsCredentialId,
|
|
1175
|
-
turnExecutionPolicy,
|
|
1176
|
-
trigger,
|
|
1177
|
-
runSettings,
|
|
1178
|
-
lazyToolTransport,
|
|
1179
|
-
turnTools,
|
|
1180
|
-
connectionScope,
|
|
1181
|
-
hostCredentialRootSessionId,
|
|
1182
|
-
sandboxArtifactRuntime,
|
|
1183
|
-
activeSandboxBackend,
|
|
1184
|
-
groupBoxBackend,
|
|
1185
|
-
routingOn,
|
|
1186
|
-
runtimeCancellationSignal,
|
|
1187
|
-
credentialSubjectId,
|
|
1188
|
-
interactionInterventionResume,
|
|
1189
|
-
runWorkspaceMutationForSandbox,
|
|
1190
|
-
throwIfWorkerShuttingDown,
|
|
1191
|
-
throwIfTurnCancelled,
|
|
1192
|
-
});
|
|
1193
|
-
const {
|
|
1194
|
-
attemptConnectorActionBindings,
|
|
1195
|
-
connectorActionPolicy,
|
|
1196
|
-
generateSessionTitleInParallel,
|
|
1197
|
-
postToolPreparationStartedAt,
|
|
1198
|
-
preparationIndependentToolNames,
|
|
1199
|
-
} = toolRuntime;
|
|
1310
|
+
const {
|
|
1311
|
+
attemptConnectorActionBindings,
|
|
1312
|
+
connectorActionPolicy,
|
|
1313
|
+
generateSessionTitleInParallel,
|
|
1314
|
+
postToolPreparationStartedAt,
|
|
1315
|
+
preparationIndependentToolNames,
|
|
1316
|
+
} = toolRuntime;
|
|
1200
1317
|
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1318
|
+
const builtAgent = await buildTurnAgent({
|
|
1319
|
+
skillCatalog: toolRuntime.skillCatalog,
|
|
1320
|
+
input,
|
|
1321
|
+
db,
|
|
1322
|
+
runtime,
|
|
1323
|
+
objectStorage,
|
|
1324
|
+
observability,
|
|
1325
|
+
cancellationSignal,
|
|
1326
|
+
runtimeCancellationSignal,
|
|
1327
|
+
eventing,
|
|
1328
|
+
attempt,
|
|
1329
|
+
sandboxState,
|
|
1330
|
+
providerTurn,
|
|
1331
|
+
media,
|
|
1332
|
+
leases,
|
|
1333
|
+
turn,
|
|
1334
|
+
session,
|
|
1335
|
+
fileAuthoritySubjectId,
|
|
1336
|
+
capabilitySettings,
|
|
1337
|
+
humanInputResume,
|
|
1338
|
+
turnExecutionPolicy,
|
|
1339
|
+
runSettings,
|
|
1340
|
+
logicalSandboxSettings,
|
|
1341
|
+
verifiedRigProviderImageId,
|
|
1342
|
+
resolvedModel,
|
|
1343
|
+
nativeImageProviderBinding,
|
|
1344
|
+
lazyToolTransport,
|
|
1345
|
+
modelInputPolicy,
|
|
1346
|
+
supportsImageInput,
|
|
1347
|
+
agentHumanInputEnabled,
|
|
1348
|
+
workspaceAgentInstructions,
|
|
1349
|
+
workspaceGovernance,
|
|
1350
|
+
structuredWorkspacePolicyActive,
|
|
1351
|
+
workspaceMemory,
|
|
1352
|
+
rigVersion,
|
|
1353
|
+
rigName,
|
|
1354
|
+
buildCompanyBrainContributionReceiptFor,
|
|
1355
|
+
promptCacheKey,
|
|
1356
|
+
workspaceVariableSet,
|
|
1357
|
+
runtimeResources,
|
|
1358
|
+
sandboxEnvironment,
|
|
1359
|
+
sandboxArtifactRuntime,
|
|
1360
|
+
sandboxGitToken,
|
|
1361
|
+
sandboxGitTokens,
|
|
1362
|
+
sandboxGitCredentialBindings,
|
|
1363
|
+
sandboxCodemodeToken,
|
|
1364
|
+
fileResourceDownloads,
|
|
1365
|
+
attemptConnectorActionBindings,
|
|
1366
|
+
connectorActionPolicy,
|
|
1367
|
+
trigger,
|
|
1368
|
+
preparationIndependentToolNames,
|
|
1369
|
+
videoGenerationAcceptancesByCallId,
|
|
1370
|
+
activeSandboxBackend,
|
|
1371
|
+
groupBoxBackend,
|
|
1372
|
+
postToolPreparationStartedAt,
|
|
1373
|
+
codexContext,
|
|
1374
|
+
});
|
|
1375
|
+
const { agent, modelVisibleSkillCatalogText, postAgentPreparationStartedAt } = builtAgent;
|
|
1259
1376
|
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1377
|
+
await bindLazySandboxProvisioner({
|
|
1378
|
+
...sandboxRoute,
|
|
1379
|
+
input,
|
|
1380
|
+
settings,
|
|
1381
|
+
db,
|
|
1382
|
+
bus,
|
|
1383
|
+
objectStorage,
|
|
1384
|
+
observability,
|
|
1385
|
+
cancellationSignal,
|
|
1386
|
+
runtimeCancellationSignal,
|
|
1387
|
+
sandboxResumeSignal,
|
|
1388
|
+
activityContext,
|
|
1389
|
+
opJournal,
|
|
1390
|
+
sandboxState,
|
|
1391
|
+
eventing,
|
|
1392
|
+
attempt,
|
|
1393
|
+
turn,
|
|
1394
|
+
session,
|
|
1395
|
+
fileAuthoritySubjectId,
|
|
1396
|
+
capabilitySettings,
|
|
1397
|
+
turnExecutionPolicy,
|
|
1398
|
+
runSettings,
|
|
1399
|
+
logicalSandboxSettings,
|
|
1400
|
+
verifiedRigProviderImageId,
|
|
1401
|
+
runtimePreparationStartedAt,
|
|
1402
|
+
establishPolicy,
|
|
1403
|
+
sandboxEnvironment,
|
|
1404
|
+
startRunGitCredentialsMint,
|
|
1405
|
+
machineOpObserver,
|
|
1406
|
+
sandboxOperationObserver,
|
|
1407
|
+
sandboxRuntime,
|
|
1408
|
+
transientCodemodeEnvironment,
|
|
1409
|
+
agent,
|
|
1410
|
+
attachRunCredentialRenewal,
|
|
1411
|
+
attachGitCredentialRenewal,
|
|
1412
|
+
attachCodemodeTokenRenewal,
|
|
1413
|
+
throwIfWorkerShuttingDown,
|
|
1414
|
+
throwIfTurnCancelled,
|
|
1415
|
+
initialRunCredentialMaterial,
|
|
1416
|
+
sandboxCodemodeToken,
|
|
1417
|
+
media,
|
|
1418
|
+
toolResultSpill,
|
|
1419
|
+
connectionScope,
|
|
1420
|
+
codemodeAuthority,
|
|
1421
|
+
rigVersion,
|
|
1422
|
+
turnResources,
|
|
1423
|
+
});
|
|
1307
1424
|
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1425
|
+
let companyBrainContributionReceiptRecorded = false;
|
|
1426
|
+
const recordCompanyBrainContributionReceiptOnce = (): void => {
|
|
1427
|
+
if (companyBrainContributionReceiptRecorded) return;
|
|
1428
|
+
companyBrainContributionReceiptRecorded = true;
|
|
1429
|
+
try {
|
|
1430
|
+
const companyBrainContributionReceipt = buildCompanyBrainContributionReceiptFor(
|
|
1431
|
+
modelVisibleSkillCatalogText,
|
|
1432
|
+
);
|
|
1433
|
+
eventing.companyBrainContextContributions = summarizeCompanyBrainContributions(
|
|
1434
|
+
companyBrainContributionReceipt,
|
|
1435
|
+
);
|
|
1436
|
+
recordCompanyBrainContributions(observability, companyBrainContributionReceipt);
|
|
1437
|
+
observability.info("model context contribution receipt", {
|
|
1438
|
+
attemptId: companyBrainContributionReceipt.attemptId,
|
|
1439
|
+
turnId: companyBrainContributionReceipt.turnId,
|
|
1440
|
+
contextSelectionReceiptId:
|
|
1441
|
+
companyBrainContributionReceipt.contextSelectionReceiptId,
|
|
1442
|
+
sessionRole: companyBrainContributionReceipt.sessionRole,
|
|
1443
|
+
memoryPromptMode: companyBrainContributionReceipt.memoryPromptMode,
|
|
1444
|
+
instructionPolicySnapshotId:
|
|
1445
|
+
companyBrainContributionReceipt.instructionPolicySnapshotId,
|
|
1446
|
+
preferenceSnapshotId: companyBrainContributionReceipt.preferenceSnapshotId,
|
|
1447
|
+
companyProfileSnapshotId: companyBrainContributionReceipt.companyProfileSnapshotId,
|
|
1448
|
+
contributions: JSON.stringify(companyBrainContributionReceipt.contributions),
|
|
1449
|
+
});
|
|
1450
|
+
} catch {
|
|
1451
|
+
// Observability must never change model execution semantics.
|
|
1452
|
+
}
|
|
1453
|
+
};
|
|
1454
|
+
const postAgentCompaction = await runPostAgentCompaction({
|
|
1455
|
+
input,
|
|
1456
|
+
settings: capabilitySettings,
|
|
1457
|
+
db,
|
|
1458
|
+
bus,
|
|
1459
|
+
observability,
|
|
1460
|
+
cancellationSignal,
|
|
1461
|
+
control,
|
|
1462
|
+
attempt,
|
|
1463
|
+
billingState,
|
|
1464
|
+
eventing: wiredEventing,
|
|
1465
|
+
providerTurn,
|
|
1466
|
+
leases,
|
|
1467
|
+
media,
|
|
1468
|
+
claimedResult,
|
|
1469
|
+
claimedModelUsageSourceKeys,
|
|
1470
|
+
emittedModelUsageSourceKeys,
|
|
1471
|
+
modelUsageDispatchId,
|
|
1472
|
+
turn,
|
|
1473
|
+
session,
|
|
1474
|
+
turnExecutionPolicy,
|
|
1475
|
+
resolvedModel,
|
|
1476
|
+
workspaceAgentInstructions,
|
|
1477
|
+
workspaceGovernance,
|
|
1478
|
+
structuredWorkspacePolicyActive,
|
|
1479
|
+
workspaceMemory,
|
|
1480
|
+
rigVersion,
|
|
1481
|
+
rigName,
|
|
1482
|
+
compactionModelHistoryProjector,
|
|
1483
|
+
summarizeContextForCompaction,
|
|
1484
|
+
withProviderRequestContext,
|
|
1485
|
+
withCodexRemoteCompaction,
|
|
1486
|
+
remotePrefix,
|
|
1487
|
+
remoteCompactionRequester,
|
|
1488
|
+
publishCompactionLiveEvents,
|
|
1489
|
+
publishCompactionOutcomeEvents,
|
|
1490
|
+
compactionModeOptions,
|
|
1491
|
+
compactionOnlyTurn,
|
|
1492
|
+
compactionSummarizerFor,
|
|
1493
|
+
settleDeferredSteerAfterCompaction,
|
|
1494
|
+
agent,
|
|
1331
1495
|
});
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
}
|
|
1335
|
-
};
|
|
1336
|
-
const postAgentCompaction = await runPostAgentCompaction({
|
|
1337
|
-
input,
|
|
1338
|
-
settings: capabilitySettings,
|
|
1339
|
-
db,
|
|
1340
|
-
bus,
|
|
1341
|
-
observability,
|
|
1342
|
-
cancellationSignal,
|
|
1343
|
-
control,
|
|
1344
|
-
attempt,
|
|
1345
|
-
billingState,
|
|
1346
|
-
eventing: wiredEventing,
|
|
1347
|
-
providerTurn,
|
|
1348
|
-
leases,
|
|
1349
|
-
media,
|
|
1350
|
-
claimedResult,
|
|
1351
|
-
claimedModelUsageSourceKeys,
|
|
1352
|
-
emittedModelUsageSourceKeys,
|
|
1353
|
-
modelUsageDispatchId,
|
|
1354
|
-
turn,
|
|
1355
|
-
session,
|
|
1356
|
-
turnExecutionPolicy,
|
|
1357
|
-
resolvedModel,
|
|
1358
|
-
workspaceAgentInstructions,
|
|
1359
|
-
workspaceGovernance,
|
|
1360
|
-
structuredWorkspacePolicyActive,
|
|
1361
|
-
workspaceMemory,
|
|
1362
|
-
rigVersion,
|
|
1363
|
-
rigName,
|
|
1364
|
-
compactionModelHistoryProjector,
|
|
1365
|
-
summarizeContextForCompaction,
|
|
1366
|
-
withProviderRequestContext,
|
|
1367
|
-
withCodexRemoteCompaction,
|
|
1368
|
-
remotePrefix,
|
|
1369
|
-
remoteCompactionRequester,
|
|
1370
|
-
publishCompactionLiveEvents,
|
|
1371
|
-
publishCompactionOutcomeEvents,
|
|
1372
|
-
compactionModeOptions,
|
|
1373
|
-
compactionOnlyTurn,
|
|
1374
|
-
compactionSummarizerFor,
|
|
1375
|
-
settleDeferredSteerAfterCompaction,
|
|
1376
|
-
agent,
|
|
1377
|
-
});
|
|
1378
|
-
if ("exit" in postAgentCompaction) return postAgentCompaction.exit;
|
|
1379
|
-
const { compactSummarizer } = postAgentCompaction.ok;
|
|
1496
|
+
if ("exit" in postAgentCompaction) return postAgentCompaction.exit;
|
|
1497
|
+
const { compactSummarizer } = postAgentCompaction.ok;
|
|
1380
1498
|
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
});
|
|
1388
|
-
let fileMaterializationFailures: SandboxFileDownloadFailure[] = [];
|
|
1389
|
-
let fileDownloadsMaterializedForRun = false;
|
|
1390
|
-
if (
|
|
1391
|
-
sandboxState.resolvedSandbox &&
|
|
1392
|
-
sandboxState.setupBoxSession &&
|
|
1393
|
-
fileResourceDownloads.length > 0
|
|
1394
|
-
) {
|
|
1395
|
-
const fileMaterializationStartedAt = performance.now();
|
|
1396
|
-
let fileMaterializationOutcome: "completed" | "failed" = "completed";
|
|
1397
|
-
let fileMaterializationCache: "hit" | "miss" = "miss";
|
|
1398
|
-
try {
|
|
1399
|
-
const boxInstanceId = sandboxState.resolvedSandbox.established.instanceId;
|
|
1400
|
-
// A successful transfer is durable for this exact filesystem instance.
|
|
1401
|
-
// Do not turn later model startup into an integrity scan. If an owner or
|
|
1402
|
-
// agent removes the file, it can be restored explicitly through the
|
|
1403
|
-
// existing Files MCP using the durable file id carried in model history.
|
|
1404
|
-
const alreadyMaterialized = await getMaterializedSandboxFileResources(db, {
|
|
1405
|
-
accountId: input.accountId,
|
|
1406
|
-
workspaceId: input.workspaceId,
|
|
1407
|
-
sandboxGroupId: session.sandboxGroupId,
|
|
1408
|
-
expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
|
|
1409
|
-
instanceId: boxInstanceId,
|
|
1499
|
+
recordTurnStartupPhase(observability, {
|
|
1500
|
+
phase: "post_agent_preparation",
|
|
1501
|
+
provider: turnExecutionPolicy.providerId,
|
|
1502
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
1503
|
+
outcome: "completed",
|
|
1504
|
+
durationSeconds: (performance.now() - postAgentPreparationStartedAt) / 1_000,
|
|
1410
1505
|
});
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
onRuntimeEvent: async (event) => {
|
|
1429
|
-
await eventing.publish!([{ type: event.type, payload: event.payload }], true);
|
|
1430
|
-
},
|
|
1431
|
-
...(runAs ? { runAs } : {}),
|
|
1432
|
-
...(eventing.toolCancellationFenceRef.current
|
|
1433
|
-
? {
|
|
1434
|
-
commandRunner:
|
|
1435
|
-
eventing.toolCancellationFenceRef.current.runSandboxCommand.bind(
|
|
1436
|
-
eventing.toolCancellationFenceRef.current,
|
|
1437
|
-
),
|
|
1438
|
-
}
|
|
1439
|
-
: {}),
|
|
1440
|
-
},
|
|
1441
|
-
),
|
|
1442
|
-
);
|
|
1443
|
-
fileMaterializationFailures = materialized.failures;
|
|
1444
|
-
fileMaterializationOutcome = sandboxFileMaterializationOutcome(materialized.failures);
|
|
1445
|
-
const failedFileIds = new Set(materialized.failures.map((failure) => failure.fileId));
|
|
1446
|
-
const succeededFileIds = downloadsToMaterialize
|
|
1447
|
-
.map((download) => download.fileId)
|
|
1448
|
-
.filter((fileId) => !failedFileIds.has(fileId));
|
|
1449
|
-
if (succeededFileIds.length > 0) {
|
|
1450
|
-
await markSandboxFileResourcesMaterialized(db, {
|
|
1506
|
+
let fileMaterializationFailures: SandboxFileDownloadFailure[] = [];
|
|
1507
|
+
let fileDownloadsMaterializedForRun = false;
|
|
1508
|
+
if (
|
|
1509
|
+
sandboxState.resolvedSandbox &&
|
|
1510
|
+
sandboxState.setupBoxSession &&
|
|
1511
|
+
fileResourceDownloads.length > 0
|
|
1512
|
+
) {
|
|
1513
|
+
const fileMaterializationStartedAt = performance.now();
|
|
1514
|
+
let fileMaterializationOutcome: "completed" | "failed" = "completed";
|
|
1515
|
+
let fileMaterializationCache: "hit" | "miss" = "miss";
|
|
1516
|
+
try {
|
|
1517
|
+
const boxInstanceId = sandboxState.resolvedSandbox.established.instanceId;
|
|
1518
|
+
// A successful transfer is durable for this exact filesystem instance.
|
|
1519
|
+
// Do not turn later model startup into an integrity scan. If an owner or
|
|
1520
|
+
// agent removes the file, it can be restored explicitly through the
|
|
1521
|
+
// existing Files MCP using the durable file id carried in model history.
|
|
1522
|
+
const alreadyMaterialized = await getMaterializedSandboxFileResources(db, {
|
|
1451
1523
|
accountId: input.accountId,
|
|
1452
1524
|
workspaceId: input.workspaceId,
|
|
1453
1525
|
sandboxGroupId: session.sandboxGroupId,
|
|
1454
1526
|
expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
|
|
1455
1527
|
instanceId: boxInstanceId,
|
|
1456
|
-
|
|
1528
|
+
});
|
|
1529
|
+
const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
|
|
1530
|
+
fileResourceDownloads,
|
|
1531
|
+
alreadyMaterialized,
|
|
1532
|
+
);
|
|
1533
|
+
if (downloadsToMaterialize.length === 0) {
|
|
1534
|
+
fileMaterializationCache = "hit";
|
|
1535
|
+
}
|
|
1536
|
+
const runAs = sandboxRunAs(runSettings);
|
|
1537
|
+
if (downloadsToMaterialize.length > 0) {
|
|
1538
|
+
const materialized = await runWorkspaceMutationForSandbox(
|
|
1539
|
+
sandboxState.resolvedSandbox,
|
|
1540
|
+
"fileMaterialization",
|
|
1541
|
+
async () =>
|
|
1542
|
+
await materializeSandboxFileDownloads(
|
|
1543
|
+
sandboxState.setupBoxSession as CodemodeTokenWriterSession,
|
|
1544
|
+
downloadsToMaterialize,
|
|
1545
|
+
{
|
|
1546
|
+
onRuntimeEvent: async (event) => {
|
|
1547
|
+
await eventing.publish!(
|
|
1548
|
+
[{ type: event.type, payload: event.payload }],
|
|
1549
|
+
true,
|
|
1550
|
+
);
|
|
1551
|
+
},
|
|
1552
|
+
...(runAs ? { runAs } : {}),
|
|
1553
|
+
...(eventing.toolCancellationFenceRef.current
|
|
1554
|
+
? {
|
|
1555
|
+
commandRunner:
|
|
1556
|
+
eventing.toolCancellationFenceRef.current.runSandboxCommand.bind(
|
|
1557
|
+
eventing.toolCancellationFenceRef.current,
|
|
1558
|
+
),
|
|
1559
|
+
}
|
|
1560
|
+
: {}),
|
|
1561
|
+
},
|
|
1562
|
+
),
|
|
1563
|
+
);
|
|
1564
|
+
fileMaterializationFailures = materialized.failures;
|
|
1565
|
+
fileMaterializationOutcome = sandboxFileMaterializationOutcome(
|
|
1566
|
+
materialized.failures,
|
|
1567
|
+
);
|
|
1568
|
+
const failedFileIds = new Set(
|
|
1569
|
+
materialized.failures.map((failure) => failure.fileId),
|
|
1570
|
+
);
|
|
1571
|
+
const succeededFileIds = downloadsToMaterialize
|
|
1572
|
+
.map((download) => download.fileId)
|
|
1573
|
+
.filter((fileId) => !failedFileIds.has(fileId));
|
|
1574
|
+
if (succeededFileIds.length > 0) {
|
|
1575
|
+
await markSandboxFileResourcesMaterialized(db, {
|
|
1576
|
+
accountId: input.accountId,
|
|
1577
|
+
workspaceId: input.workspaceId,
|
|
1578
|
+
sandboxGroupId: session.sandboxGroupId,
|
|
1579
|
+
expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
|
|
1580
|
+
instanceId: boxInstanceId,
|
|
1581
|
+
fileIds: succeededFileIds,
|
|
1582
|
+
});
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
fileDownloadsMaterializedForRun = true;
|
|
1586
|
+
} catch (error) {
|
|
1587
|
+
fileMaterializationOutcome = "failed";
|
|
1588
|
+
throw error;
|
|
1589
|
+
} finally {
|
|
1590
|
+
recordTurnStartupPhase(observability, {
|
|
1591
|
+
phase: "file_materialization",
|
|
1592
|
+
provider: turnExecutionPolicy.providerId,
|
|
1593
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
1594
|
+
outcome: fileMaterializationOutcome,
|
|
1595
|
+
durationSeconds: (performance.now() - fileMaterializationStartedAt) / 1_000,
|
|
1596
|
+
count: fileResourceDownloads.length,
|
|
1597
|
+
cache: fileMaterializationCache,
|
|
1457
1598
|
});
|
|
1458
1599
|
}
|
|
1600
|
+
} else {
|
|
1601
|
+
recordTurnStartupPhase(observability, {
|
|
1602
|
+
phase: "file_materialization",
|
|
1603
|
+
provider: turnExecutionPolicy.providerId,
|
|
1604
|
+
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
1605
|
+
outcome: "completed",
|
|
1606
|
+
durationSeconds: 0,
|
|
1607
|
+
count: 0,
|
|
1608
|
+
});
|
|
1459
1609
|
}
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1610
|
+
const requiredVideoFileIds = new Set(
|
|
1611
|
+
requiredGeneratedVideoFiles.map((file) => file.fileId),
|
|
1612
|
+
);
|
|
1613
|
+
const failedRequiredVideo = fileMaterializationFailures.find((failure) =>
|
|
1614
|
+
requiredVideoFileIds.has(failure.fileId),
|
|
1615
|
+
);
|
|
1616
|
+
if (failedRequiredVideo) {
|
|
1617
|
+
// Retention is already durable. A sandbox copy miss must not fail the
|
|
1618
|
+
// turn or replay generation; the File remains retrievable via Files MCP.
|
|
1619
|
+
observability.warn("Generated video sandbox materialization deferred", {
|
|
1620
|
+
errorClass: "SandboxFileDownloadFailure",
|
|
1621
|
+
errorCode: "generated_video_materialization_deferred",
|
|
1622
|
+
origin: "worker",
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
|
|
1626
|
+
fileMaterializationFailures,
|
|
1627
|
+
);
|
|
1628
|
+
return await runTurnStreamAttempt({
|
|
1629
|
+
input,
|
|
1630
|
+
settings: capabilitySettings,
|
|
1631
|
+
db,
|
|
1632
|
+
bus,
|
|
1633
|
+
runtime,
|
|
1634
|
+
objectStorage,
|
|
1635
|
+
observability,
|
|
1636
|
+
entitlements,
|
|
1637
|
+
startVideoGenerationWorkflow,
|
|
1638
|
+
cancellationSignal,
|
|
1639
|
+
runtimeCancellationSignal,
|
|
1640
|
+
activityContext,
|
|
1641
|
+
dispatchId,
|
|
1642
|
+
control,
|
|
1643
|
+
attempt,
|
|
1644
|
+
billingState,
|
|
1645
|
+
sandboxState,
|
|
1646
|
+
eventing: wiredEventing,
|
|
1647
|
+
providerTurn,
|
|
1648
|
+
leases,
|
|
1649
|
+
historySink,
|
|
1650
|
+
media,
|
|
1651
|
+
toolResultSpill,
|
|
1652
|
+
claimedResult,
|
|
1653
|
+
flushRuntimeBatcher,
|
|
1654
|
+
finalizeTurnOpStreamOps,
|
|
1655
|
+
runWorkspaceMutationForSandbox,
|
|
1656
|
+
throwIfWorkerShuttingDown,
|
|
1657
|
+
throwIfTurnCancelled,
|
|
1658
|
+
setLastInputTokensFenced,
|
|
1659
|
+
attachGitCredentialRenewal,
|
|
1660
|
+
attachCodemodeTokenRenewal,
|
|
1661
|
+
attachRunCredentialRenewal,
|
|
1662
|
+
withProviderRequestContext,
|
|
1663
|
+
withSessionTitleProviderRequestContext,
|
|
1664
|
+
publishCompactionLiveEvents,
|
|
1665
|
+
publishCompactionOutcomeEvents,
|
|
1666
|
+
recordCompanyBrainContributionReceiptOnce,
|
|
1667
|
+
modelCheckpointMemoryCollector,
|
|
1668
|
+
claimedModelUsageSourceKeys,
|
|
1669
|
+
emittedModelUsageSourceKeys,
|
|
1670
|
+
modelUsageDispatchId,
|
|
1671
|
+
workerPreparationStartedAt,
|
|
1672
|
+
fileDownloadsMaterializedForRun,
|
|
1673
|
+
unavailableSandboxFilesNote,
|
|
1674
|
+
runCredentialsNote,
|
|
1675
|
+
mcpAvailabilityNote,
|
|
1676
|
+
knowledgeSourcePreparationNote,
|
|
1677
|
+
fileAuthoritySubjectId,
|
|
1678
|
+
activeSandboxBackend,
|
|
1679
|
+
groupBoxBackend,
|
|
1680
|
+
turnExecutionPolicy,
|
|
1681
|
+
turn,
|
|
1682
|
+
trigger,
|
|
1683
|
+
humanInputResume,
|
|
1684
|
+
attachPendingUpdatesAfterOpenSuffix,
|
|
1685
|
+
agent,
|
|
1686
|
+
resolvedModel,
|
|
1687
|
+
providerApi,
|
|
1688
|
+
runSettings,
|
|
1689
|
+
turnTools,
|
|
1690
|
+
generateSessionTitleInParallel,
|
|
1691
|
+
sessionTitlePrompt: session.initialMessage.trim()
|
|
1692
|
+
? session.initialMessage
|
|
1693
|
+
: turn.prompt,
|
|
1694
|
+
compactSummarizer,
|
|
1695
|
+
settleDeferredSteerAfterCompaction,
|
|
1696
|
+
compactionModelHistoryProjector,
|
|
1697
|
+
generatedImageHistoryProjector,
|
|
1698
|
+
modelHistoryProjector,
|
|
1699
|
+
compactionModeOptions,
|
|
1700
|
+
initialRunCredentialMaterial,
|
|
1701
|
+
initialGitCredentials,
|
|
1702
|
+
sandboxEnvironment,
|
|
1703
|
+
sandboxCodemodeToken,
|
|
1704
|
+
sandboxCodemodeTokenExpiresAt,
|
|
1705
|
+
fileResourceDownloads,
|
|
1706
|
+
runCredentialResolver,
|
|
1707
|
+
videoGenerationAcceptancesByCallId,
|
|
1473
1708
|
});
|
|
1474
|
-
}
|
|
1475
|
-
} else {
|
|
1476
|
-
recordTurnStartupPhase(observability, {
|
|
1477
|
-
phase: "file_materialization",
|
|
1478
|
-
provider: turnExecutionPolicy.providerId,
|
|
1479
|
-
backend: activeSandboxBackend ?? groupBoxBackend,
|
|
1480
|
-
outcome: "completed",
|
|
1481
|
-
durationSeconds: 0,
|
|
1482
|
-
count: 0,
|
|
1483
|
-
});
|
|
1484
|
-
}
|
|
1485
|
-
const requiredVideoFileIds = new Set(requiredGeneratedVideoFiles.map((file) => file.fileId));
|
|
1486
|
-
const failedRequiredVideo = fileMaterializationFailures.find((failure) =>
|
|
1487
|
-
requiredVideoFileIds.has(failure.fileId),
|
|
1709
|
+
},
|
|
1488
1710
|
);
|
|
1489
|
-
if (failedRequiredVideo) {
|
|
1490
|
-
// Retention is already durable. A sandbox copy miss must not fail the
|
|
1491
|
-
// turn or replay generation; the File remains retrievable via Files MCP.
|
|
1492
|
-
observability.warn("Generated video sandbox materialization deferred", {
|
|
1493
|
-
errorClass: "SandboxFileDownloadFailure",
|
|
1494
|
-
errorCode: "generated_video_materialization_deferred",
|
|
1495
|
-
origin: "worker",
|
|
1496
|
-
});
|
|
1497
|
-
}
|
|
1498
|
-
const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
|
|
1499
|
-
fileMaterializationFailures,
|
|
1500
|
-
);
|
|
1501
|
-
return await runTurnStreamAttempt({
|
|
1502
|
-
input,
|
|
1503
|
-
settings: capabilitySettings,
|
|
1504
|
-
db,
|
|
1505
|
-
bus,
|
|
1506
|
-
runtime,
|
|
1507
|
-
objectStorage,
|
|
1508
|
-
observability,
|
|
1509
|
-
entitlements,
|
|
1510
|
-
startVideoGenerationWorkflow,
|
|
1511
|
-
cancellationSignal,
|
|
1512
|
-
runtimeCancellationSignal,
|
|
1513
|
-
activityContext,
|
|
1514
|
-
dispatchId,
|
|
1515
|
-
control,
|
|
1516
|
-
attempt,
|
|
1517
|
-
billingState,
|
|
1518
|
-
sandboxState,
|
|
1519
|
-
eventing: wiredEventing,
|
|
1520
|
-
providerTurn,
|
|
1521
|
-
leases,
|
|
1522
|
-
historySink,
|
|
1523
|
-
media,
|
|
1524
|
-
toolResultSpill,
|
|
1525
|
-
claimedResult,
|
|
1526
|
-
flushRuntimeBatcher,
|
|
1527
|
-
finalizeTurnOpStreamOps,
|
|
1528
|
-
runWorkspaceMutationForSandbox,
|
|
1529
|
-
throwIfWorkerShuttingDown,
|
|
1530
|
-
throwIfTurnCancelled,
|
|
1531
|
-
setLastInputTokensFenced,
|
|
1532
|
-
attachGitCredentialRenewal,
|
|
1533
|
-
attachCodemodeTokenRenewal,
|
|
1534
|
-
attachRunCredentialRenewal,
|
|
1535
|
-
withProviderRequestContext,
|
|
1536
|
-
withSessionTitleProviderRequestContext,
|
|
1537
|
-
publishCompactionLiveEvents,
|
|
1538
|
-
publishCompactionOutcomeEvents,
|
|
1539
|
-
recordCompanyBrainContributionReceiptOnce,
|
|
1540
|
-
modelCheckpointMemoryCollector,
|
|
1541
|
-
claimedModelUsageSourceKeys,
|
|
1542
|
-
emittedModelUsageSourceKeys,
|
|
1543
|
-
modelUsageDispatchId,
|
|
1544
|
-
workerPreparationStartedAt,
|
|
1545
|
-
fileDownloadsMaterializedForRun,
|
|
1546
|
-
unavailableSandboxFilesNote,
|
|
1547
|
-
runCredentialsNote,
|
|
1548
|
-
mcpAvailabilityNote,
|
|
1549
|
-
fileAuthoritySubjectId,
|
|
1550
|
-
activeSandboxBackend,
|
|
1551
|
-
groupBoxBackend,
|
|
1552
|
-
turnExecutionPolicy,
|
|
1553
|
-
turn,
|
|
1554
|
-
trigger,
|
|
1555
|
-
humanInputResume,
|
|
1556
|
-
attachPendingUpdatesAfterOpenSuffix,
|
|
1557
|
-
agent,
|
|
1558
|
-
resolvedModel,
|
|
1559
|
-
providerApi,
|
|
1560
|
-
runSettings,
|
|
1561
|
-
turnTools,
|
|
1562
|
-
generateSessionTitleInParallel,
|
|
1563
|
-
sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
|
|
1564
|
-
compactSummarizer,
|
|
1565
|
-
settleDeferredSteerAfterCompaction,
|
|
1566
|
-
compactionModelHistoryProjector,
|
|
1567
|
-
generatedImageHistoryProjector,
|
|
1568
|
-
modelHistoryProjector,
|
|
1569
|
-
compactionModeOptions,
|
|
1570
|
-
initialRunCredentialMaterial,
|
|
1571
|
-
initialGitCredentials,
|
|
1572
|
-
sandboxEnvironment,
|
|
1573
|
-
sandboxCodemodeToken,
|
|
1574
|
-
sandboxCodemodeTokenExpiresAt,
|
|
1575
|
-
fileResourceDownloads,
|
|
1576
|
-
runCredentialResolver,
|
|
1577
|
-
videoGenerationAcceptancesByCallId,
|
|
1578
|
-
});
|
|
1579
1711
|
} catch (error) {
|
|
1580
1712
|
// Stop new external wait mutations before asynchronous failure
|
|
1581
1713
|
// persistence. Do not drain here: the original failure/cancellation and
|