@opengeni/worker-bundle 0.29.0-canary.1 → 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.
Files changed (47) hide show
  1. package/dist/activities/agent-turn/knowledge-source-tools.d.ts +9 -0
  2. package/dist/activities/agent-turn/knowledge-sources.d.ts +20 -0
  3. package/dist/activities/agent-turn/stream-attempt.d.ts +1 -0
  4. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  5. package/dist/activities/knowledge-indexing.d.ts +10 -0
  6. package/dist/activities/run-input.d.ts +1 -0
  7. package/dist/activities/types.d.ts +10 -3
  8. package/dist/{activities-control-MKGEOBXT.js → activities-control-HMFGSKC5.js} +331 -3381
  9. package/dist/activities-control-HMFGSKC5.js.map +1 -0
  10. package/dist/activities-control.d.ts +12 -0
  11. package/dist/{activities-turn-MYD63DTN.js → activities-turn-AP67JADS.js} +1293 -1020
  12. package/dist/activities-turn-AP67JADS.js.map +1 -0
  13. package/dist/activities.d.ts +12 -0
  14. package/dist/{chunk-OA5CSY5N.js → chunk-CYITU5UH.js} +3348 -174
  15. package/dist/chunk-CYITU5UH.js.map +1 -0
  16. package/dist/index.d.ts +13 -0
  17. package/dist/index.js +48 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/workflow-bundle.js +65 -35
  20. package/dist/workflows/knowledge-indexing.d.ts +2 -0
  21. package/dist/workflows.d.ts +1 -0
  22. package/package.json +19 -18
  23. package/src/activities/agent-turn/knowledge-source-tools.ts +98 -0
  24. package/src/activities/agent-turn/knowledge-sources.ts +86 -0
  25. package/src/activities/agent-turn/run.ts +1244 -1112
  26. package/src/activities/agent-turn/skill-install.ts +1 -1
  27. package/src/activities/agent-turn/skill-save.ts +1 -1
  28. package/src/activities/agent-turn/skill-tools.ts +5 -12
  29. package/src/activities/agent-turn/stream-attempt.ts +3 -0
  30. package/src/activities/agent-turn/tool-environment.ts +24 -0
  31. package/src/activities/documents.ts +17 -52
  32. package/src/activities/knowledge-indexing.ts +130 -0
  33. package/src/activities/knowledge-source-sync.ts +120 -16
  34. package/src/activities/mcp-credentials.ts +2 -0
  35. package/src/activities/run-input.ts +2 -0
  36. package/src/activities/scheduled-tasks.ts +25 -62
  37. package/src/activities/types.ts +8 -2
  38. package/src/activities/video-generation-reconciliation.ts +8 -1
  39. package/src/activities-control.ts +2 -0
  40. package/src/activities.ts +1 -0
  41. package/src/index.ts +49 -0
  42. package/src/pr-review-credentials.ts +2 -2
  43. package/src/workflows/knowledge-indexing.ts +12 -0
  44. package/src/workflows.ts +1 -0
  45. package/dist/activities-control-MKGEOBXT.js.map +0 -1
  46. package/dist/activities-turn-MYD63DTN.js.map +0 -1
  47. package/dist/chunk-OA5CSY5N.js.map +0 -1
@@ -23,6 +23,7 @@ import {
23
23
  compactionFailureReason,
24
24
  compactionFailureReasonFromError,
25
25
  computeIdleDelayMs,
26
+ createKnowledgeSourceSyncActivities,
26
27
  createModelHistoryAttachmentProjector,
27
28
  createRuntimeBatcher,
28
29
  createSharedActivityServices,
@@ -86,7 +87,7 @@ import {
86
87
  withFirstPartyTools,
87
88
  wrapLazyTurnBoxWithRouting,
88
89
  wrapTurnBoxWithRouting
89
- } from "./chunk-OA5CSY5N.js";
90
+ } from "./chunk-CYITU5UH.js";
90
91
  import {
91
92
  StreamTimingMetrics,
92
93
  makeMachineOpObserver,
@@ -120,8 +121,70 @@ import {
120
121
  // src/activities-turn.ts
121
122
  import { createProductionAgentRuntime, summarizeForCompaction as summarizeForCompaction2 } from "@opengeni/runtime";
122
123
 
124
+ // src/activities/agent-turn/knowledge-sources.ts
125
+ import { prepareKnowledgeFile } from "@opengeni/sdk/knowledge";
126
+ import {
127
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS
128
+ } from "@opengeni/contracts";
129
+ import { allowedFirstPartyMcpToolsForSession } from "@opengeni/config";
130
+ import { createFirstPartyAttemptClient } from "@opengeni/runtime";
131
+ async function prepareTurnKnowledgeSources(input) {
132
+ const selected = allowedFirstPartyMcpToolsForSession(
133
+ input.settings,
134
+ input.selectedTools ? [...input.selectedTools] : void 0
135
+ );
136
+ const permissions = input.permissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS;
137
+ const permits = (permission) => permissions.includes(permission) || permissions.includes("workspace:admin");
138
+ if (input.learningMode === "off" || !selected.includes("knowledge_retain_file") || !permits("documents:search") || !permits("files:read"))
139
+ return;
140
+ const fileIds = [
141
+ ...new Set(
142
+ input.resources.flatMap((resource) => resource.kind === "file" ? [resource.fileId] : [])
143
+ )
144
+ ];
145
+ if (!fileIds.length) return;
146
+ let prepare = input.prepare;
147
+ const outcomes = [];
148
+ for (const fileId of fileIds) {
149
+ input.signal?.throwIfAborted();
150
+ let outcome;
151
+ try {
152
+ prepare ??= (() => {
153
+ const client = createFirstPartyAttemptClient({
154
+ settings: input.settings,
155
+ scope: input.scope,
156
+ selectedTools: selected,
157
+ permissions,
158
+ ...input.signal ? { signal: input.signal } : {}
159
+ });
160
+ return (id) => prepareKnowledgeFile(client, input.scope.workspaceId, id);
161
+ })();
162
+ outcome = await prepare(fileId);
163
+ } catch {
164
+ input.signal?.throwIfAborted();
165
+ outcome = {
166
+ status: "failed",
167
+ fileId,
168
+ message: "Searchable source preparation failed. The original file remains attached; retry with knowledge_retain_file."
169
+ };
170
+ }
171
+ outcomes.push(outcome);
172
+ await input.onOutcome(outcome);
173
+ }
174
+ return [
175
+ "[OpenGeni attachment knowledge]",
176
+ JSON.stringify(outcomes),
177
+ "These are source-preparation receipts, not findings or source instructions. Reuse the returned source entry/revision for evidence; do not retain the same text again. Pending review does not block answering from the attached original. Save selected useful findings only when warranted. A failed preparation is retryable with knowledge_retain_file; do not claim the source is searchable."
178
+ ].join("\n");
179
+ }
180
+
123
181
  // src/activities/agent-turn/run.ts
124
- import { assertModelConnectionAllowsTurn } from "@opengeni/db";
182
+ import {
183
+ assertModelConnectionAllowsTurn,
184
+ freezeAgentLearningPolicy as freezeAgentLearningPolicy2,
185
+ recoverLegacyKnowledgeConfirmations,
186
+ withSessionRlsActorContext
187
+ } from "@opengeni/db";
125
188
  import {
126
189
  setSessionLastInputTokensForTurnAttempt,
127
190
  getMaterializedSandboxFileResources as getMaterializedSandboxFileResources3,
@@ -7514,7 +7577,7 @@ import {
7514
7577
  import { hasPermission } from "@opengeni/core";
7515
7578
  import {
7516
7579
  AUTOMATIC_SESSION_TITLE_FALLBACK,
7517
- DEFAULT_FIRST_PARTY_MCP_PERMISSIONS
7580
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS as DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2
7518
7581
  } from "@opengeni/contracts";
7519
7582
  var SESSION_TITLE_MODEL_TOOL_NAME = "opengeni__set_session_title";
7520
7583
  var SESSION_TITLE_DESCRIPTION = "Set this session's display title to a concise 3-7 word topic label. Use a stable noun phrase about the actual task or subject, never a quote/prefix of a prompt, greeting, request boilerplate, URL, identifier, credential, token, or other sensitive value. Call once on a new session, then only when the topic materially changes. Never call it as routine setup after a continuation, resume, or interruption, or merely to reassert the same title. A human-set title cannot be replaced.";
@@ -7523,7 +7586,7 @@ function shouldRequestMissingSessionTitle(input) {
7523
7586
  const needsSemanticTitle = input.titleSource !== "user" && (!title || title === AUTOMATIC_SESSION_TITLE_FALLBACK);
7524
7587
  if (!needsSemanticTitle) return false;
7525
7588
  if (!input.firstPartyMcpTools.includes("set_session_title")) return false;
7526
- const permissions = input.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS;
7589
+ const permissions = input.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2;
7527
7590
  return hasPermission([...permissions], "sessions:control");
7528
7591
  }
7529
7592
  function sessionTitleToolPlan(input) {
@@ -7825,6 +7888,7 @@ async function runTurnStreamAttempt(deps) {
7825
7888
  unavailableSandboxFilesNote,
7826
7889
  runCredentialsNote,
7827
7890
  mcpAvailabilityNote,
7891
+ knowledgeSourcePreparationNote,
7828
7892
  fileAuthoritySubjectId,
7829
7893
  activeSandboxBackend,
7830
7894
  groupBoxBackend,
@@ -7938,6 +8002,7 @@ async function runTurnStreamAttempt(deps) {
7938
8002
  ...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
7939
8003
  ...runCredentialsNote ? { runCredentialsNote } : {},
7940
8004
  ...mcpAvailabilityNote ? { mcpAvailabilityNote } : {},
8005
+ ...knowledgeSourcePreparationNote ? { knowledgeSourcePreparationNote } : {},
7941
8006
  providerApi,
7942
8007
  projectCanonicalHistory: generatedImageHistoryProjector,
7943
8008
  materializeModelHistory: media.materializeScreenshotHistory,
@@ -14292,6 +14357,86 @@ async function prepareRunCredentials(deps) {
14292
14357
  };
14293
14358
  }
14294
14359
 
14360
+ // src/activities/agent-turn/knowledge-source-tools.ts
14361
+ import {
14362
+ freezeAgentLearningPolicy,
14363
+ getScheduledTaskRunAcceptedExecution,
14364
+ readScheduledKnowledgeSource
14365
+ } from "@opengeni/db";
14366
+ async function createKnowledgeSourceAttemptTools(input) {
14367
+ if (input.context.actor.kind !== "agent") return [];
14368
+ const policy = await freezeAgentLearningPolicy(input.db, input.context);
14369
+ if (typeof policy.scheduledTaskRunId !== "string") return [];
14370
+ const runId = policy.scheduledTaskRunId;
14371
+ const accepted = await getScheduledTaskRunAcceptedExecution(input.db, {
14372
+ workspaceId: input.context.workspaceId,
14373
+ runId
14374
+ });
14375
+ const source = accepted?.task.agentConfig.knowledgeSource;
14376
+ if (!accepted || !source || accepted.task.action.kind !== "agent_turn") return [];
14377
+ const actor = input.context.actor;
14378
+ const authorize = async () => {
14379
+ const current = await freezeAgentLearningPolicy(input.db, input.context);
14380
+ if (current.scheduledTaskRunId !== runId) throw new Error("Source run authority changed");
14381
+ };
14382
+ const definition = (name, title, description, inputSchema, execute, readOnly) => ({
14383
+ identity: { serverId: "opengeni", toolName: name },
14384
+ modelName: name,
14385
+ codemodePath: ["opengeni", name],
14386
+ title,
14387
+ description,
14388
+ inputSchema,
14389
+ annotations: {
14390
+ title,
14391
+ readOnlyHint: readOnly,
14392
+ destructiveHint: false,
14393
+ idempotentHint: true,
14394
+ openWorldHint: !readOnly
14395
+ },
14396
+ source: "opengeni",
14397
+ approval: "none",
14398
+ execute: async (args) => {
14399
+ await authorize();
14400
+ const output = await execute(args);
14401
+ return { isError: false, content: [{ type: "text", text: JSON.stringify(output) }] };
14402
+ }
14403
+ });
14404
+ return [
14405
+ definition(
14406
+ "knowledge_source_fetch",
14407
+ "Fetch selected source",
14408
+ "Fetch a checkpointed batch from this scheduled task's selected source. Retains original files and searchable source content under the run's Knowledge policy. It does not create findings. Use knowledge_source_read to inspect content, then knowledge_save for useful findings with evidence. Call again if action is continue. Review first stages content without interrupting this task; Off keeps Knowledge disabled.",
14409
+ { type: "object", properties: {}, additionalProperties: false },
14410
+ async () => (await freezeAgentLearningPolicy(input.db, input.context)).effective.knowledge === "off" ? { action: "disabled", reason: "Knowledge is Off for this run" } : input.fetch({
14411
+ accountId: input.context.accountId,
14412
+ workspaceId: input.context.workspaceId,
14413
+ taskId: accepted.task.id,
14414
+ scheduledTaskRunId: runId,
14415
+ sourceId: source.sourceId,
14416
+ overlapPolicy: accepted.task.overlapPolicy === "skip" ? "skip" : "buffer_one",
14417
+ agent: actor
14418
+ }),
14419
+ false
14420
+ ),
14421
+ definition(
14422
+ "knowledge_source_read",
14423
+ "Read selected source content",
14424
+ "List source entries retained or updated in this run. Pass entryId to read up to 16,000 characters, following nextOffset for more. Follow nextCursor with afterId for subsequent list pages. Includes this run's pending source content so you can prepare findings in the same review batch; pending knowledge stays out of ordinary search. Use entryId and revisionId as evidence when saving findings.",
14425
+ {
14426
+ type: "object",
14427
+ properties: {
14428
+ entryId: { type: "string", format: "uuid" },
14429
+ afterId: { type: "string", format: "uuid" },
14430
+ offset: { type: "integer", minimum: 0, maximum: 1e8 }
14431
+ },
14432
+ additionalProperties: false
14433
+ },
14434
+ async (args) => readScheduledKnowledgeSource(input.db, input.context, args),
14435
+ true
14436
+ )
14437
+ ];
14438
+ }
14439
+
14295
14440
  // src/activities/agent-turn/tool-environment.ts
14296
14441
  import { getWorkspaceConnectionModelRestrictions } from "@opengeni/db";
14297
14442
  import {
@@ -14935,6 +15080,7 @@ import {
14935
15080
  buildHostConnectionTokenResolver,
14936
15081
  getSessionTurnForAttempt,
14937
15082
  authorizeDirectHostMcpUse,
15083
+ resolveAcceptedHostMcpBinding,
14938
15084
  resolveAcceptedConnectionUse,
14939
15085
  sessionTenancyProductActivated
14940
15086
  } from "@opengeni/db";
@@ -15021,6 +15167,7 @@ function connectionTokenResolverForTurn(input) {
15021
15167
  initiator: input.turn.initiator,
15022
15168
  initiatorContext: input.turn.initiatorContext,
15023
15169
  surface: "model",
15170
+ resolveAcceptedBinding: (request) => resolveAcceptedHostMcpBinding(hostDb, request),
15024
15171
  // Only immutable captured direct-turn authority is currently supported.
15025
15172
  // Direct, scheduled and exact causal/child snapshots are validated;
15026
15173
  // no authority follows merely from a binding or creator identity.
@@ -30539,7 +30686,7 @@ async function materializeConnectorAttachmentsInChannel(channel, request, option
30539
30686
  }
30540
30687
 
30541
30688
  // src/activities/agent-turn/tool-environment.ts
30542
- import { allowedFirstPartyMcpToolsForSession } from "@opengeni/config";
30689
+ import { allowedFirstPartyMcpToolsForSession as allowedFirstPartyMcpToolsForSession2 } from "@opengeni/config";
30543
30690
 
30544
30691
  // src/activities/codemode-dispatcher.ts
30545
30692
  import { randomUUID as randomUUID3 } from "crypto";
@@ -30946,7 +31093,7 @@ import {
30946
31093
  import { SandboxChannelAService as SandboxChannelAService3 } from "@opengeni/runtime/sandbox";
30947
31094
  import { sandboxRunAs as sandboxRunAs4 } from "@opengeni/runtime";
30948
31095
  import {
30949
- DEFAULT_FIRST_PARTY_MCP_PERMISSIONS as DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2
31096
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS as DEFAULT_FIRST_PARTY_MCP_PERMISSIONS3
30950
31097
  } from "@opengeni/contracts";
30951
31098
 
30952
31099
  // src/activities/agent-turn/turn-sandbox-access.ts
@@ -31043,9 +31190,6 @@ function createListModelsAttemptToolDefinition(input) {
31043
31190
 
31044
31191
  // src/activities/agent-turn/skill-tools.ts
31045
31192
  import { createHash as createHash12 } from "crypto";
31046
- import {
31047
- skillReviewHumanInput
31048
- } from "@opengeni/contracts";
31049
31193
  import {
31050
31194
  assertSkillReadAttempt,
31051
31195
  skillReviewResolution,
@@ -31225,7 +31369,7 @@ function createSkillSaveAttemptToolDefinition(input) {
31225
31369
  modelName: "skill_save",
31226
31370
  codemodePath: ["opengeni", "skill_save"],
31227
31371
  title: "Save Skill text",
31228
- description: "Create or edit a workspace Skill without a sandbox. Supply only changed UTF-8 text files and explicit deletions; omitted files are preserved. Use the revision and scope version returned by Skill discovery for stale-write protection. For creation choose a new UUID, set expectedRevisionId to null and expectedScopeVersion to 1. Learning mode determines whether the change is live, pending approval, or refused. If humanInput is returned, call request_human_input verbatim, Save activates the exact reviewed revision as the answer is accepted. Do not ask for a second review or call a separate activation tool.",
31372
+ description: "Create or edit a workspace Skill without a sandbox. Supply only changed UTF-8 text files and explicit deletions; omitted files are preserved. Use the revision and scope version returned by Skill discovery for stale-write protection. For creation choose a new UUID, set expectedRevisionId to null and expectedScopeVersion to 1. The effective Skills setting governs publication: Automatic publishes, Review first saves a pending revision in Knowledge > Needs review while the task continues, and Off prevents agent authoring. Report the actual receipt without asking an approval question. Private chats save personal Skills; shared chats save workspace Skills.",
31229
31373
  inputSchema: {
31230
31374
  type: "object",
31231
31375
  properties: {
@@ -31311,7 +31455,7 @@ function createSkillInstallAttemptToolDefinition(input) {
31311
31455
  modelName: "skill_install",
31312
31456
  codemodePath: ["opengeni", "skill_install"],
31313
31457
  title: "Install a Skill",
31314
- description: "Install a Skill from a public skills.sh or GitHub URL, or library:<id> returned by skill_search. The server resolves and pins the source; no prior preview or invented hash is required. Learning mode controls activation. Replacing an existing installation requires its current installation version, and preserves workspace customizations.",
31458
+ description: "Install a Skill from a public skills.sh or GitHub URL, or library:<id> returned by skill_search. The server resolves and pins the source; no prior preview or invented hash is required. The effective Skills setting governs agent installation: Automatic publishes, Review first leaves a pending change in Knowledge > Needs review while the task continues, and Off prevents agent installation. Human installation from the UI remains available. Do not ask an approval question for a pending receipt. Replacing an existing installation requires its current installation version, and preserves workspace customizations.",
31315
31459
  inputSchema: {
31316
31460
  type: "object",
31317
31461
  properties: {
@@ -31603,18 +31747,17 @@ function createWorkspaceSkillTools(input) {
31603
31747
  files: record6.files
31604
31748
  };
31605
31749
  };
31606
- const withConfirmation = async (receipt) => {
31750
+ const withReviewState = async (receipt) => {
31607
31751
  const reviewResolution = receipt.skillReview ? await skillReviewResolution(input.db, context, receipt.skillReview) : void 0;
31608
31752
  return {
31609
31753
  ...receipt,
31610
- ...reviewResolution ? { reviewResolution } : {},
31611
- ...receipt.skillReview && reviewResolution === "pending" ? { humanInput: skillReviewHumanInput(receipt.skillReview) } : {}
31754
+ ...reviewResolution ? { reviewResolution } : {}
31612
31755
  };
31613
31756
  };
31614
31757
  const save = async (request) => {
31615
31758
  const artifact = buildPortableSkillArtifact3(request.files);
31616
31759
  const base = request.expectedRevisionId ? await readSkill(input.db, context, request.skillId, request.expectedRevisionId) : null;
31617
- return withConfirmation(
31760
+ return withReviewState(
31618
31761
  await saveSkill(input.db, {
31619
31762
  ...context,
31620
31763
  ...request,
@@ -31671,7 +31814,7 @@ function createWorkspaceSkillTools(input) {
31671
31814
  operationId: request.operationId,
31672
31815
  requestIdentity
31673
31816
  });
31674
- if (replay) return withConfirmation(replay.skillReceipt);
31817
+ if (replay) return withReviewState(replay.skillReceipt);
31675
31818
  let source;
31676
31819
  if (request.source.startsWith("library:")) {
31677
31820
  const loaded = loadSkillLibrarySkill(request.source.slice("library:".length));
@@ -31730,7 +31873,7 @@ function createWorkspaceSkillTools(input) {
31730
31873
  expectedInstallationVersion: request.expectedInstallationVersion
31731
31874
  } : {}
31732
31875
  });
31733
- return withConfirmation(installed.skillReceipt);
31876
+ return withReviewState(installed.skillReceipt);
31734
31877
  }
31735
31878
  }),
31736
31879
  createSkillCheckoutAttemptToolDefinition({
@@ -32087,10 +32230,10 @@ async function prepareTurnToolRuntime(deps) {
32087
32230
  );
32088
32231
  if (linkedAuthority && !linkedAuthority.authorized)
32089
32232
  throw new Error("Native identity link was revoked");
32090
- const effectiveFirstPartyPermissions = linkedAuthority ? (session.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2).filter(
32233
+ const effectiveFirstPartyPermissions = linkedAuthority ? (session.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS3).filter(
32091
32234
  (permission) => hasPermission2(linkedAuthority.permissions, permission)
32092
32235
  ) : session.firstPartyMcpPermissions;
32093
- const selectedFirstPartyMcpTools = allowedFirstPartyMcpToolsForSession(
32236
+ const selectedFirstPartyMcpTools = allowedFirstPartyMcpToolsForSession2(
32094
32237
  runSettings,
32095
32238
  session.firstPartyMcpTools
32096
32239
  );
@@ -32106,10 +32249,10 @@ async function prepareTurnToolRuntime(deps) {
32106
32249
  parallelGenerationAvailable: typeof runtime.generateSessionTitle === "function"
32107
32250
  });
32108
32251
  const googleDrivePublicationAllowed = selectedFirstPartyMcpTools.includes("editable_artifact_export") && selectedFirstPartyMcpTools.includes("editable_artifact_export_status") && hasPermission2(
32109
- [...effectiveFirstPartyPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2],
32252
+ [...effectiveFirstPartyPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS3],
32110
32253
  "artifacts:read"
32111
32254
  ) && hasPermission2(
32112
- [...effectiveFirstPartyPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS2],
32255
+ [...effectiveFirstPartyPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS3],
32113
32256
  "artifacts:publish"
32114
32257
  );
32115
32258
  const googleDriveConnectorBindings = googleDrivePublicationTool && googleDrivePublicationTarget && googleDrivePublicationAllowed ? [
@@ -32212,6 +32355,21 @@ async function prepareTurnToolRuntime(deps) {
32212
32355
  });
32213
32356
  }
32214
32357
  });
32358
+ const sourceTools = deps.fetchKnowledgeSource ? await createKnowledgeSourceAttemptTools({
32359
+ db,
32360
+ context: {
32361
+ accountId: input.accountId,
32362
+ workspaceId: input.workspaceId,
32363
+ actor: {
32364
+ kind: "agent",
32365
+ sessionId: input.sessionId,
32366
+ turnId: turn.id,
32367
+ attemptId: input.attemptId,
32368
+ executionGeneration: attempt.executionGeneration
32369
+ }
32370
+ },
32371
+ fetch: deps.fetchKnowledgeSource
32372
+ }) : [];
32215
32373
  const operationRecoveryEnabled = githubRestMcp.settings.mcpServers.some(
32216
32374
  (server) => server.operationRecovery && Object.keys(server.operationRecovery).length > 0 && githubRestMcp.tools.some((tool) => tool.kind === "mcp" && tool.id === server.id)
32217
32375
  );
@@ -32259,6 +32417,7 @@ async function prepareTurnToolRuntime(deps) {
32259
32417
  })
32260
32418
  ] : [],
32261
32419
  ...skillTools,
32420
+ ...sourceTools,
32262
32421
  createListModelsAttemptToolDefinition({
32263
32422
  currentModelId: turnExecutionPolicy.productModelId,
32264
32423
  load: async () => {
@@ -34519,6 +34678,13 @@ function sessionTitleXaiRequestContext(context, nextRequestId) {
34519
34678
  };
34520
34679
  }
34521
34680
  function createRunAgentTurnActivity(services) {
34681
+ let documents;
34682
+ const sourceActivities = createKnowledgeSourceSyncActivities(services, async () => {
34683
+ documents ??= import("@opengeni/documents").then(
34684
+ async ({ createDocumentServices }) => createDocumentServices((await services()).settings)
34685
+ );
34686
+ return documents;
34687
+ });
34522
34688
  const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
34523
34689
  const turnCompletionMemoryCollector = createModelCheckpointMemoryCollector();
34524
34690
  return async function runAgentTurn(input) {
@@ -34741,1042 +34907,1142 @@ function createRunAgentTurnActivity(services) {
34741
34907
  if (!attempt.turnId) {
34742
34908
  throw new Error("Turn id was not initialized");
34743
34909
  }
34744
- if (!leases.codex.holderId) {
34745
- throw new Error("Codex lease holder was not initialized");
34746
- }
34747
- const workerPreparationStartedAt = performance.now();
34748
- {
34749
- const capacityDeps = {
34750
- input,
34751
- settings,
34752
- db,
34753
- bus,
34754
- observability,
34755
- wakeSessionWorkflow,
34756
- signalCodexCapacityWorkflow,
34757
- cancellationSignal,
34758
- dispatchId,
34759
- control,
34760
- attempt,
34761
- billingState,
34762
- eventing: wiredEventing,
34763
- providerTurn,
34764
- leases,
34765
- claimedResult,
34766
- acknowledgeLostAttemptOwnership,
34767
- acknowledgeRecoveryQuiescence,
34768
- setLastInputTokensFenced,
34769
- turn,
34770
- session,
34771
- turnExecutionPolicy,
34772
- trigger,
34773
- codexWorkspaceKey
34774
- };
34775
- const codexCapacity = await selectCodexTurnCapacity(capacityDeps);
34776
- if ("exit" in codexCapacity) return codexCapacity.exit;
34777
- const xaiCapacity = await selectXaiTurnCapacity(capacityDeps);
34778
- if ("exit" in xaiCapacity) return xaiCapacity.exit;
34779
- }
34780
- const governance = await prepareGovernanceAndModel({
34781
- input,
34782
- db,
34783
- runtime,
34784
- objectStorage,
34785
- eventing,
34786
- workspaceRefs,
34787
- media,
34788
- turn,
34789
- session,
34790
- capabilitySettings,
34791
- fileAuthoritySubjectId,
34792
- humanInputResume,
34793
- turnExecutionPolicy,
34794
- requiredGeneratedVideoFiles
34795
- });
34796
- if ("exit" in governance) return governance.exit;
34797
- await assertModelConnectionAllowsTurn(db, {
34910
+ const learning = await freezeAgentLearningPolicy2(db, {
34911
+ accountId: input.accountId,
34798
34912
  workspaceId: input.workspaceId,
34799
- subjectId: turn.initiatingHumanSubjectId ?? "worker:model-access",
34800
- modelId: turnExecutionPolicy.productModelId,
34801
- codexCredentialId: providerTurn.effectiveCodexCredentialId,
34802
- xaiCredentialId: providerTurn.effectiveXaiCredentialId
34803
- });
34804
- const {
34805
- runtimePreparationStartedAt,
34806
- packRuntime,
34807
- rigVersion,
34808
- rigName,
34809
- agentHumanInputEnabled,
34810
- workspaceAgentInstructions,
34811
- workspaceGovernance,
34812
- structuredWorkspacePolicyActive,
34813
- workspaceMemory,
34814
- buildCompanyBrainContributionReceiptFor,
34815
- logicalSandboxSettings,
34816
- verifiedRigProviderImageId,
34817
- runSettings,
34818
- resolvedModel,
34819
- providerApi,
34820
- nativeImageProviderBinding,
34821
- lazyToolTransport,
34822
- modelInputPolicy,
34823
- supportsImageInput,
34824
- modelHistoryProjector,
34825
- generatedImageHistoryProjector,
34826
- compactionModelHistoryProjector
34827
- } = governance.ok;
34828
- let codexModelRequestSequence = 0;
34829
- let firstModelRequestAuditRecorded = false;
34830
- const firstModelRequestCheckpoints = /* @__PURE__ */ new Set();
34831
- const codexContext = resolvedModel?.provider.kind === "codex-subscription" ? (() => {
34832
- const resolver = buildCodexTokenResolver(
34833
- db,
34834
- runSettings,
34835
- input.workspaceId,
34836
- providerTurn.effectiveCodexCredentialId ?? ""
34837
- );
34838
- const resolveTrackedToken = async (resolve) => {
34839
- const token = await resolve();
34840
- providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
34841
- return token;
34842
- };
34843
- return {
34844
- clientVersion: CODEX_CLIENT_VERSION2,
34845
- // Backend sticky cache-routing key — the SAME id as the body's
34846
- // prompt_cache_key (set from input.sessionId for codex turns),
34847
- // so routing and cache key agree. Without this header on the
34848
- // wire, byte-identical resends hit the prompt cache ~50%
34849
- // (per-request shard lottery = prod's measured 48.6% on sol);
34850
- // with it, resends pin to the warm shard (Codex CLI parity).
34913
+ actor: {
34914
+ kind: "agent",
34851
34915
  sessionId: input.sessionId,
34852
- getToken: () => resolveTrackedToken(resolver.getToken),
34853
- refresh: () => resolveTrackedToken(resolver.refresh),
34854
- resolveModel: buildModelResolver(
34855
- CODEX_FALLBACK_MODEL_SLUGS,
34856
- CODEX_FALLBACK_MODEL_SLUGS[0]
34857
- ),
34858
- onUsageHeaders: (snapshot) => {
34859
- providerTurn.latestCodexUsage = snapshot;
34860
- },
34861
- // latest wins; flushed once in finally
34862
- beforeProviderDispatch: () => {
34863
- leases.codex.assertUsable();
34864
- },
34865
- onRequestPreparationDiagnostic: (phase) => {
34866
- if (eventing.firstModelRequestCheckpointAt === null || firstModelRequestCheckpoints.has(phase)) {
34867
- return;
34868
- }
34869
- const now = performance.now();
34870
- const metricPhase = phase === "transport_entry" ? "model_sdk_serialization" : phase === "credential_ready" ? "model_credential_resolution" : "model_wire_normalization";
34871
- firstModelRequestCheckpoints.add(phase);
34872
- recordTurnStartupPhase(observability, {
34873
- phase: metricPhase,
34874
- provider: turnExecutionPolicy.providerId,
34875
- backend: activeSandboxBackend ?? groupBoxBackend,
34876
- outcome: "completed",
34877
- durationSeconds: (now - eventing.firstModelRequestCheckpointAt) / 1e3,
34878
- count: turnTools.length
34879
- });
34880
- eventing.firstModelRequestCheckpointAt = now;
34881
- },
34882
- onRequestOpaqueArtifacts: ({ fingerprints }) => {
34883
- providerTurn.lastCodexRequestOpaqueArtifacts = fingerprints;
34884
- },
34885
- onModelRequestDiagnostic: (event) => {
34886
- if (event.phase === "started") sandboxState.firstProviderRequestStarted = true;
34887
- if (event.phase === "started" && eventing.firstModelRequestPreparationStartedAt !== null && !eventing.firstModelRequestPreparationRecorded) {
34888
- eventing.firstModelRequestPreparationRecorded = true;
34889
- recordTurnStartupPhase(observability, {
34890
- phase: "model_request_preparation",
34891
- provider: turnExecutionPolicy.providerId,
34892
- backend: activeSandboxBackend ?? groupBoxBackend,
34893
- outcome: "completed",
34894
- durationSeconds: (performance.now() - eventing.firstModelRequestPreparationStartedAt) / 1e3,
34895
- count: turnTools.length
34896
- });
34897
- }
34898
- const phase = event.phase === "headers" ? "headers" : event.phase === "first_byte" ? "first_byte" : event.phase === "completed" || event.phase === "failed" || event.phase === "timed_out" ? "terminal" : null;
34899
- if (!phase) return;
34900
- recordModelRequestPhase(observability, {
34901
- provider: "codex-subscription",
34902
- phase,
34903
- ...event.phase === "completed" ? { outcome: "completed" } : event.phase === "failed" ? { outcome: "failed" } : event.phase === "timed_out" ? { outcome: "timed_out" } : {},
34904
- durationSeconds: event.durationMs / 1e3
34905
- });
34906
- },
34907
- nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
34908
- onModelRequestEvent: async (event) => {
34909
- if (!eventing.publish || !attempt.turnId) {
34910
- throw new Error("Codex model request started before the turn event producer");
34911
- }
34912
- if (event.phase === "started") {
34913
- await checkpointHistoryBeforeProviderDispatch(historySink);
34914
- }
34915
- const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
34916
- if (shouldRecordStartedAudit) {
34917
- firstModelRequestAuditRecorded = true;
34918
- }
34919
- const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
34920
- let auditOutcome = "completed";
34921
- try {
34922
- await eventing.publish([
34923
- ...shouldRecordStartedAudit && eventing.firstModelRequestPreparationStartedAt !== null ? [
34924
- {
34925
- type: "turn.startup.phase.completed",
34926
- payload: {
34927
- phase: "model_preparation",
34928
- durationMs: Math.max(
34929
- 0,
34930
- Math.round(
34931
- performance.now() - eventing.firstModelRequestPreparationStartedAt
34932
- )
34933
- )
34934
- }
34935
- }
34936
- ] : [],
34937
- {
34938
- type: "agent.model.request",
34939
- payload: {
34940
- ...event,
34941
- provider: "codex-subscription",
34942
- turnId: attempt.turnId,
34943
- attemptId: input.attemptId,
34944
- dispatchId,
34945
- executionGeneration: attempt.executionGeneration
34946
- }
34916
+ turnId: attempt.turnId,
34917
+ attemptId: input.attemptId,
34918
+ executionGeneration: attempt.executionGeneration
34919
+ }
34920
+ });
34921
+ return await withSessionRlsActorContext(
34922
+ {
34923
+ subjectId: "service:agent-turn",
34924
+ initiatingHumanSubjectId: fileAuthoritySubjectId,
34925
+ privateFileOwnerSubjectId: learning.defaultScope === "personal" ? learning.subjectId : null
34926
+ },
34927
+ async () => {
34928
+ if (!leases.codex.holderId) {
34929
+ throw new Error("Codex lease holder was not initialized");
34930
+ }
34931
+ const workerPreparationStartedAt = performance.now();
34932
+ {
34933
+ const capacityDeps = {
34934
+ input,
34935
+ settings,
34936
+ db,
34937
+ bus,
34938
+ observability,
34939
+ wakeSessionWorkflow,
34940
+ signalCodexCapacityWorkflow,
34941
+ cancellationSignal,
34942
+ dispatchId,
34943
+ control,
34944
+ attempt,
34945
+ billingState,
34946
+ eventing: wiredEventing,
34947
+ providerTurn,
34948
+ leases,
34949
+ claimedResult,
34950
+ acknowledgeLostAttemptOwnership,
34951
+ acknowledgeRecoveryQuiescence,
34952
+ setLastInputTokensFenced,
34953
+ turn,
34954
+ session,
34955
+ turnExecutionPolicy,
34956
+ trigger,
34957
+ codexWorkspaceKey
34958
+ };
34959
+ const codexCapacity = await selectCodexTurnCapacity(capacityDeps);
34960
+ if ("exit" in codexCapacity) return codexCapacity.exit;
34961
+ const xaiCapacity = await selectXaiTurnCapacity(capacityDeps);
34962
+ if ("exit" in xaiCapacity) return xaiCapacity.exit;
34963
+ }
34964
+ const governance = await prepareGovernanceAndModel({
34965
+ input,
34966
+ db,
34967
+ runtime,
34968
+ objectStorage,
34969
+ eventing,
34970
+ workspaceRefs,
34971
+ media,
34972
+ turn,
34973
+ session,
34974
+ capabilitySettings,
34975
+ fileAuthoritySubjectId,
34976
+ humanInputResume,
34977
+ turnExecutionPolicy,
34978
+ requiredGeneratedVideoFiles
34979
+ });
34980
+ if ("exit" in governance) return governance.exit;
34981
+ await assertModelConnectionAllowsTurn(db, {
34982
+ workspaceId: input.workspaceId,
34983
+ subjectId: turn.initiatingHumanSubjectId ?? "worker:model-access",
34984
+ modelId: turnExecutionPolicy.productModelId,
34985
+ codexCredentialId: providerTurn.effectiveCodexCredentialId,
34986
+ xaiCredentialId: providerTurn.effectiveXaiCredentialId
34987
+ });
34988
+ const {
34989
+ runtimePreparationStartedAt,
34990
+ packRuntime,
34991
+ rigVersion,
34992
+ rigName,
34993
+ agentHumanInputEnabled,
34994
+ workspaceAgentInstructions,
34995
+ workspaceGovernance,
34996
+ structuredWorkspacePolicyActive,
34997
+ workspaceMemory,
34998
+ buildCompanyBrainContributionReceiptFor,
34999
+ logicalSandboxSettings,
35000
+ verifiedRigProviderImageId,
35001
+ runSettings,
35002
+ resolvedModel,
35003
+ providerApi,
35004
+ nativeImageProviderBinding,
35005
+ lazyToolTransport,
35006
+ modelInputPolicy,
35007
+ supportsImageInput,
35008
+ modelHistoryProjector,
35009
+ generatedImageHistoryProjector,
35010
+ compactionModelHistoryProjector
35011
+ } = governance.ok;
35012
+ let codexModelRequestSequence = 0;
35013
+ let firstModelRequestAuditRecorded = false;
35014
+ const firstModelRequestCheckpoints = /* @__PURE__ */ new Set();
35015
+ const codexContext = resolvedModel?.provider.kind === "codex-subscription" ? (() => {
35016
+ const resolver = buildCodexTokenResolver(
35017
+ db,
35018
+ runSettings,
35019
+ input.workspaceId,
35020
+ providerTurn.effectiveCodexCredentialId ?? ""
35021
+ );
35022
+ const resolveTrackedToken = async (resolve) => {
35023
+ const token = await resolve();
35024
+ providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
35025
+ return token;
35026
+ };
35027
+ return {
35028
+ clientVersion: CODEX_CLIENT_VERSION2,
35029
+ // Backend sticky cache-routing key — the SAME id as the body's
35030
+ // prompt_cache_key (set from input.sessionId for codex turns),
35031
+ // so routing and cache key agree. Without this header on the
35032
+ // wire, byte-identical resends hit the prompt cache ~50%
35033
+ // (per-request shard lottery = prod's measured 48.6% on sol);
35034
+ // with it, resends pin to the warm shard (Codex CLI parity).
35035
+ sessionId: input.sessionId,
35036
+ getToken: () => resolveTrackedToken(resolver.getToken),
35037
+ refresh: () => resolveTrackedToken(resolver.refresh),
35038
+ resolveModel: buildModelResolver(
35039
+ CODEX_FALLBACK_MODEL_SLUGS,
35040
+ CODEX_FALLBACK_MODEL_SLUGS[0]
35041
+ ),
35042
+ onUsageHeaders: (snapshot) => {
35043
+ providerTurn.latestCodexUsage = snapshot;
35044
+ },
35045
+ // latest wins; flushed once in finally
35046
+ beforeProviderDispatch: () => {
35047
+ leases.codex.assertUsable();
35048
+ },
35049
+ onRequestPreparationDiagnostic: (phase) => {
35050
+ if (eventing.firstModelRequestCheckpointAt === null || firstModelRequestCheckpoints.has(phase)) {
35051
+ return;
34947
35052
  }
34948
- ]);
34949
- attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
34950
- attempt.providerRecoveryCount,
34951
- event.phase
34952
- );
34953
- } catch (error51) {
34954
- auditOutcome = "failed";
34955
- throw error51;
34956
- } finally {
34957
- if (auditStartedAt !== null) {
35053
+ const now = performance.now();
35054
+ const metricPhase = phase === "transport_entry" ? "model_sdk_serialization" : phase === "credential_ready" ? "model_credential_resolution" : "model_wire_normalization";
35055
+ firstModelRequestCheckpoints.add(phase);
34958
35056
  recordTurnStartupPhase(observability, {
34959
- phase: "model_request_audit",
35057
+ phase: metricPhase,
34960
35058
  provider: turnExecutionPolicy.providerId,
34961
35059
  backend: activeSandboxBackend ?? groupBoxBackend,
34962
- outcome: auditOutcome,
34963
- durationSeconds: (performance.now() - auditStartedAt) / 1e3,
35060
+ outcome: "completed",
35061
+ durationSeconds: (now - eventing.firstModelRequestCheckpointAt) / 1e3,
34964
35062
  count: turnTools.length
34965
35063
  });
35064
+ eventing.firstModelRequestCheckpointAt = now;
35065
+ },
35066
+ onRequestOpaqueArtifacts: ({ fingerprints }) => {
35067
+ providerTurn.lastCodexRequestOpaqueArtifacts = fingerprints;
35068
+ },
35069
+ onModelRequestDiagnostic: (event) => {
35070
+ if (event.phase === "started")
35071
+ sandboxState.firstProviderRequestStarted = true;
35072
+ if (event.phase === "started" && eventing.firstModelRequestPreparationStartedAt !== null && !eventing.firstModelRequestPreparationRecorded) {
35073
+ eventing.firstModelRequestPreparationRecorded = true;
35074
+ recordTurnStartupPhase(observability, {
35075
+ phase: "model_request_preparation",
35076
+ provider: turnExecutionPolicy.providerId,
35077
+ backend: activeSandboxBackend ?? groupBoxBackend,
35078
+ outcome: "completed",
35079
+ durationSeconds: (performance.now() - eventing.firstModelRequestPreparationStartedAt) / 1e3,
35080
+ count: turnTools.length
35081
+ });
35082
+ }
35083
+ const phase = event.phase === "headers" ? "headers" : event.phase === "first_byte" ? "first_byte" : event.phase === "completed" || event.phase === "failed" || event.phase === "timed_out" ? "terminal" : null;
35084
+ if (!phase) return;
35085
+ recordModelRequestPhase(observability, {
35086
+ provider: "codex-subscription",
35087
+ phase,
35088
+ ...event.phase === "completed" ? { outcome: "completed" } : event.phase === "failed" ? { outcome: "failed" } : event.phase === "timed_out" ? { outcome: "timed_out" } : {},
35089
+ durationSeconds: event.durationMs / 1e3
35090
+ });
35091
+ },
35092
+ nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
35093
+ onModelRequestEvent: async (event) => {
35094
+ if (!eventing.publish || !attempt.turnId) {
35095
+ throw new Error(
35096
+ "Codex model request started before the turn event producer"
35097
+ );
35098
+ }
35099
+ if (event.phase === "started") {
35100
+ await checkpointHistoryBeforeProviderDispatch(historySink);
35101
+ }
35102
+ const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
35103
+ if (shouldRecordStartedAudit) {
35104
+ firstModelRequestAuditRecorded = true;
35105
+ }
35106
+ const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
35107
+ let auditOutcome = "completed";
35108
+ try {
35109
+ await eventing.publish([
35110
+ ...shouldRecordStartedAudit && eventing.firstModelRequestPreparationStartedAt !== null ? [
35111
+ {
35112
+ type: "turn.startup.phase.completed",
35113
+ payload: {
35114
+ phase: "model_preparation",
35115
+ durationMs: Math.max(
35116
+ 0,
35117
+ Math.round(
35118
+ performance.now() - eventing.firstModelRequestPreparationStartedAt
35119
+ )
35120
+ )
35121
+ }
35122
+ }
35123
+ ] : [],
35124
+ {
35125
+ type: "agent.model.request",
35126
+ payload: {
35127
+ ...event,
35128
+ provider: "codex-subscription",
35129
+ turnId: attempt.turnId,
35130
+ attemptId: input.attemptId,
35131
+ dispatchId,
35132
+ executionGeneration: attempt.executionGeneration
35133
+ }
35134
+ }
35135
+ ]);
35136
+ attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
35137
+ attempt.providerRecoveryCount,
35138
+ event.phase
35139
+ );
35140
+ } catch (error51) {
35141
+ auditOutcome = "failed";
35142
+ throw error51;
35143
+ } finally {
35144
+ if (auditStartedAt !== null) {
35145
+ recordTurnStartupPhase(observability, {
35146
+ phase: "model_request_audit",
35147
+ provider: turnExecutionPolicy.providerId,
35148
+ backend: activeSandboxBackend ?? groupBoxBackend,
35149
+ outcome: auditOutcome,
35150
+ durationSeconds: (performance.now() - auditStartedAt) / 1e3,
35151
+ count: turnTools.length
35152
+ });
35153
+ }
35154
+ }
34966
35155
  }
35156
+ };
35157
+ })() : null;
35158
+ if (resolvedModel?.provider.kind === "xai-subscription") {
35159
+ if (!providerTurn.effectiveXaiCredentialId || !leases.xai.subjectId) {
35160
+ throw new Error("SuperGrok subscription execution has no leased credential");
34967
35161
  }
34968
- }
34969
- };
34970
- })() : null;
34971
- if (resolvedModel?.provider.kind === "xai-subscription") {
34972
- if (!providerTurn.effectiveXaiCredentialId || !leases.xai.subjectId) {
34973
- throw new Error("SuperGrok subscription execution has no leased credential");
34974
- }
34975
- let xaiModelRequestSequence = 0;
34976
- const authorization = await buildXaiTurnRequestAuthorization({
34977
- db,
34978
- settings: runSettings,
34979
- accountId: input.accountId,
34980
- workspaceId: input.workspaceId,
34981
- subjectId: leases.xai.subjectId,
34982
- sessionId: input.sessionId,
34983
- turnId: turn.id,
34984
- credentialId: providerTurn.effectiveXaiCredentialId,
34985
- authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot,
34986
- hostedSearch: {
34987
- webSearch: runSettings.webSearchEnabled,
34988
- xSearch: runSettings.webSearchEnabled
34989
- },
34990
- streamIdleTimeoutMs: runSettings.supergrokResponseStreamIdleTimeoutMs,
34991
- nextRequestId: () => `${dispatchId}:xai:${++xaiModelRequestSequence}`,
34992
- onModelRequestDiagnostic: (event) => {
34993
- const requestKey = `${event.requestId}:${event.transportAttempt}`;
34994
- if (event.phase === "started") {
34995
- sandboxState.firstProviderRequestStarted = true;
34996
- modelRequestLifecycleMetricsFor(observability).start(
34997
- requestKey,
34998
- "supergrok-subscription"
34999
- );
35000
- if (eventing.firstModelRequestPreparationStartedAt !== null && !eventing.firstModelRequestPreparationRecorded) {
35001
- eventing.firstModelRequestPreparationRecorded = true;
35002
- recordTurnStartupPhase(observability, {
35003
- phase: "model_request_preparation",
35004
- provider: turnExecutionPolicy.providerId,
35005
- backend: activeSandboxBackend ?? groupBoxBackend,
35006
- outcome: "completed",
35007
- durationSeconds: (performance.now() - eventing.firstModelRequestPreparationStartedAt) / 1e3,
35008
- count: turnTools.length
35162
+ let xaiModelRequestSequence = 0;
35163
+ const authorization = await buildXaiTurnRequestAuthorization({
35164
+ db,
35165
+ settings: runSettings,
35166
+ accountId: input.accountId,
35167
+ workspaceId: input.workspaceId,
35168
+ subjectId: leases.xai.subjectId,
35169
+ sessionId: input.sessionId,
35170
+ turnId: turn.id,
35171
+ credentialId: providerTurn.effectiveXaiCredentialId,
35172
+ authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot,
35173
+ hostedSearch: {
35174
+ webSearch: runSettings.webSearchEnabled,
35175
+ xSearch: runSettings.webSearchEnabled
35176
+ },
35177
+ streamIdleTimeoutMs: runSettings.supergrokResponseStreamIdleTimeoutMs,
35178
+ nextRequestId: () => `${dispatchId}:xai:${++xaiModelRequestSequence}`,
35179
+ onModelRequestDiagnostic: (event) => {
35180
+ const requestKey = `${event.requestId}:${event.transportAttempt}`;
35181
+ if (event.phase === "started") {
35182
+ sandboxState.firstProviderRequestStarted = true;
35183
+ modelRequestLifecycleMetricsFor(observability).start(
35184
+ requestKey,
35185
+ "supergrok-subscription"
35186
+ );
35187
+ if (eventing.firstModelRequestPreparationStartedAt !== null && !eventing.firstModelRequestPreparationRecorded) {
35188
+ eventing.firstModelRequestPreparationRecorded = true;
35189
+ recordTurnStartupPhase(observability, {
35190
+ phase: "model_request_preparation",
35191
+ provider: turnExecutionPolicy.providerId,
35192
+ backend: activeSandboxBackend ?? groupBoxBackend,
35193
+ outcome: "completed",
35194
+ durationSeconds: (performance.now() - eventing.firstModelRequestPreparationStartedAt) / 1e3,
35195
+ count: turnTools.length
35196
+ });
35197
+ }
35198
+ }
35199
+ if (event.phase === "first_event" || event.phase === "progress") {
35200
+ modelRequestLifecycleMetricsFor(observability).event(
35201
+ requestKey,
35202
+ event.phase === "progress" ? event.interEventGapMs : void 0
35203
+ );
35204
+ }
35205
+ const terminal = event.phase === "completed" || event.phase === "failed" || event.phase === "timed_out";
35206
+ if (terminal) {
35207
+ modelRequestLifecycleMetricsFor(observability).finish(requestKey);
35208
+ }
35209
+ const phase = event.phase === "headers" ? "headers" : event.phase === "first_event" ? "first_byte" : terminal ? "terminal" : null;
35210
+ if (!phase) return;
35211
+ recordModelRequestPhase(observability, {
35212
+ provider: "supergrok-subscription",
35213
+ phase,
35214
+ ...terminal ? {
35215
+ outcome: event.phase === "completed" ? "completed" : event.phase === "timed_out" ? "timed_out" : "failed"
35216
+ } : {},
35217
+ durationSeconds: event.durationMs / 1e3
35009
35218
  });
35219
+ },
35220
+ onModelRequestEvent: async (event) => {
35221
+ if (!eventing.publish || !attempt.turnId) {
35222
+ throw new Error("SuperGrok model request started before the turn event producer");
35223
+ }
35224
+ if (event.phase === "started") {
35225
+ await checkpointHistoryBeforeProviderDispatch(historySink);
35226
+ }
35227
+ const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
35228
+ if (shouldRecordStartedAudit) firstModelRequestAuditRecorded = true;
35229
+ const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
35230
+ let auditOutcome = "completed";
35231
+ try {
35232
+ await eventing.publish([
35233
+ ...shouldRecordStartedAudit && eventing.firstModelRequestPreparationStartedAt !== null ? [
35234
+ {
35235
+ type: "turn.startup.phase.completed",
35236
+ payload: {
35237
+ phase: "model_preparation",
35238
+ durationMs: Math.max(
35239
+ 0,
35240
+ Math.round(
35241
+ performance.now() - eventing.firstModelRequestPreparationStartedAt
35242
+ )
35243
+ )
35244
+ }
35245
+ }
35246
+ ] : [],
35247
+ {
35248
+ type: "agent.model.request",
35249
+ payload: {
35250
+ ...event,
35251
+ provider: "supergrok-subscription",
35252
+ turnId: attempt.turnId,
35253
+ attemptId: input.attemptId,
35254
+ dispatchId,
35255
+ executionGeneration: attempt.executionGeneration
35256
+ }
35257
+ }
35258
+ ]);
35259
+ attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
35260
+ attempt.providerRecoveryCount,
35261
+ event.phase
35262
+ );
35263
+ } catch (error51) {
35264
+ auditOutcome = "failed";
35265
+ throw error51;
35266
+ } finally {
35267
+ if (auditStartedAt !== null) {
35268
+ recordTurnStartupPhase(observability, {
35269
+ phase: "model_request_audit",
35270
+ provider: turnExecutionPolicy.providerId,
35271
+ backend: activeSandboxBackend ?? groupBoxBackend,
35272
+ outcome: auditOutcome,
35273
+ durationSeconds: (performance.now() - auditStartedAt) / 1e3,
35274
+ count: turnTools.length
35275
+ });
35276
+ }
35277
+ }
35010
35278
  }
35011
- }
35012
- if (event.phase === "first_event" || event.phase === "progress") {
35013
- modelRequestLifecycleMetricsFor(observability).event(
35014
- requestKey,
35015
- event.phase === "progress" ? event.interEventGapMs : void 0
35016
- );
35017
- }
35018
- const terminal = event.phase === "completed" || event.phase === "failed" || event.phase === "timed_out";
35019
- if (terminal) {
35020
- modelRequestLifecycleMetricsFor(observability).finish(requestKey);
35021
- }
35022
- const phase = event.phase === "headers" ? "headers" : event.phase === "first_event" ? "first_byte" : terminal ? "terminal" : null;
35023
- if (!phase) return;
35024
- recordModelRequestPhase(observability, {
35025
- provider: "supergrok-subscription",
35026
- phase,
35027
- ...terminal ? {
35028
- outcome: event.phase === "completed" ? "completed" : event.phase === "timed_out" ? "timed_out" : "failed"
35029
- } : {},
35030
- durationSeconds: event.durationMs / 1e3
35031
35279
  });
35032
- },
35033
- onModelRequestEvent: async (event) => {
35034
- if (!eventing.publish || !attempt.turnId) {
35035
- throw new Error("SuperGrok model request started before the turn event producer");
35036
- }
35037
- if (event.phase === "started") {
35038
- await checkpointHistoryBeforeProviderDispatch(historySink);
35280
+ providerTurn.xaiRequestContext = authorization.context;
35281
+ }
35282
+ const withCodex = (fn) => codexContext ? codexRequestStorage.run(codexContext, fn) : fn();
35283
+ const withProviderRequestContext = (fn) => providerTurn.xaiRequestContext ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn) : withCodex(fn);
35284
+ let codexSessionTitleRequestSequence = 0;
35285
+ let xaiSessionTitleRequestSequence = 0;
35286
+ const codexSessionTitleContext = codexContext ? sessionTitleCodexRequestContext(
35287
+ codexContext,
35288
+ () => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`
35289
+ ) : null;
35290
+ const xaiSessionTitleContext = providerTurn.xaiRequestContext ? sessionTitleXaiRequestContext(
35291
+ providerTurn.xaiRequestContext,
35292
+ () => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`
35293
+ ) : null;
35294
+ const withSessionTitleProviderRequestContext = (fn) => xaiSessionTitleContext ? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn) : codexSessionTitleContext ? codexRequestStorage.run(codexSessionTitleContext, fn) : fn();
35295
+ const withCodexRemoteCompaction = (fn) => withCodex(
35296
+ () => withCodexRequestOverrides(
35297
+ {
35298
+ betaFeatures: [REMOTE_COMPACTION_V2_BETA_FEATURE],
35299
+ turnMetadata: {
35300
+ request_kind: "compaction",
35301
+ compaction: {
35302
+ implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION2,
35303
+ strategy: "memento"
35304
+ }
35305
+ }
35306
+ },
35307
+ fn
35308
+ )
35309
+ );
35310
+ const compactionPrep = await prepareCompaction({
35311
+ input,
35312
+ settings: capabilitySettings,
35313
+ db,
35314
+ bus,
35315
+ observability,
35316
+ cancellationSignal,
35317
+ control,
35318
+ attempt,
35319
+ billingState,
35320
+ eventing: wiredEventing,
35321
+ providerTurn,
35322
+ leases,
35323
+ media,
35324
+ claimedResult,
35325
+ claimedModelUsageSourceKeys,
35326
+ emittedModelUsageSourceKeys,
35327
+ modelUsageDispatchId,
35328
+ turn,
35329
+ session,
35330
+ turnExecutionPolicy,
35331
+ resolvedModel,
35332
+ workspaceAgentInstructions,
35333
+ workspaceGovernance,
35334
+ structuredWorkspacePolicyActive,
35335
+ workspaceMemory,
35336
+ rigVersion,
35337
+ rigName,
35338
+ compactionModelHistoryProjector,
35339
+ summarizeContextForCompaction,
35340
+ withProviderRequestContext,
35341
+ withCodexRemoteCompaction
35342
+ });
35343
+ if ("exit" in compactionPrep) return compactionPrep.exit;
35344
+ const {
35345
+ promptCacheKey,
35346
+ remotePrefix,
35347
+ remoteCompactionRequester,
35348
+ publishCompactionLiveEvents,
35349
+ publishCompactionOutcomeEvents,
35350
+ compactionModeOptions,
35351
+ compactionOnlyTurn,
35352
+ compactionSummarizerFor,
35353
+ settleDeferredSteerAfterCompaction
35354
+ } = compactionPrep.ok;
35355
+ const toolPolicy = await prepareTurnToolPolicy({
35356
+ input,
35357
+ db,
35358
+ cancellationSignal,
35359
+ connectionCredentials,
35360
+ turn,
35361
+ session,
35362
+ fileAuthoritySubjectId,
35363
+ capabilitySettings,
35364
+ runSettings,
35365
+ rigVersion,
35366
+ workspaceRefs
35367
+ });
35368
+ const {
35369
+ turnResources: claimedTurnResources,
35370
+ runtimeResources: claimedRuntimeResources,
35371
+ mcpAvailabilityNote,
35372
+ turnTools,
35373
+ connectionScope,
35374
+ workspaceVariableSet,
35375
+ sandboxWorkspaceEnvironmentValues
35376
+ } = toolPolicy;
35377
+ let knowledgeRecoveryNote;
35378
+ try {
35379
+ const recovered = await recoverLegacyKnowledgeConfirmations(db, {
35380
+ accountId: input.accountId,
35381
+ workspaceId: input.workspaceId,
35382
+ actor: {
35383
+ kind: "agent",
35384
+ sessionId: input.sessionId,
35385
+ turnId: turn.id,
35386
+ attemptId: input.attemptId,
35387
+ executionGeneration: attempt.executionGeneration
35388
+ }
35389
+ });
35390
+ if (recovered.receipts.length || recovered.instructionReceipts.length || recovered.unavailable) {
35391
+ knowledgeRecoveryNote = `[OpenGeni legacy confirmation recovery]
35392
+ ${JSON.stringify(recovered)}
35393
+ These 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.`;
35394
+ for (const receipt of recovered.instructionReceipts) {
35395
+ await eventing.publish(
35396
+ [{ type: "instruction.confirmation.recovered", payload: { receipt } }],
35397
+ true
35398
+ );
35399
+ }
35400
+ for (const receipt of recovered.receipts) {
35401
+ await eventing.publish(
35402
+ [{ type: "knowledge.confirmation.recovered", payload: { receipt } }],
35403
+ true
35404
+ );
35405
+ }
35039
35406
  }
35040
- const shouldRecordStartedAudit = event.phase === "started" && !firstModelRequestAuditRecorded;
35041
- if (shouldRecordStartedAudit) firstModelRequestAuditRecorded = true;
35042
- const auditStartedAt = shouldRecordStartedAudit ? performance.now() : null;
35043
- let auditOutcome = "completed";
35044
- try {
35045
- await eventing.publish([
35046
- ...shouldRecordStartedAudit && eventing.firstModelRequestPreparationStartedAt !== null ? [
35407
+ } catch {
35408
+ cancellationSignal?.throwIfAborted();
35409
+ knowledgeRecoveryNote = "[OpenGeni confirmation recovery unavailable] Do not assume an earlier remember confirmation was published. Inspect the current Knowledge or instruction review.";
35410
+ }
35411
+ const attachmentPreparationNote = await prepareTurnKnowledgeSources({
35412
+ settings: runSettings,
35413
+ scope: {
35414
+ accountId: input.accountId,
35415
+ workspaceId: input.workspaceId,
35416
+ sessionId: input.sessionId,
35417
+ turnId: turn.id,
35418
+ attemptId: input.attemptId,
35419
+ executionGeneration: attempt.executionGeneration
35420
+ },
35421
+ // Only attachments on this accepted turn are new source inputs.
35422
+ // Session-level file receipts remain historical context and must
35423
+ // never trigger metadata reads, downloads, or re-preparation.
35424
+ resources: turn.resources,
35425
+ selectedTools: session.firstPartyMcpTools,
35426
+ permissions: session.firstPartyMcpPermissions,
35427
+ learningMode: learning.effective.knowledge,
35428
+ signal: cancellationSignal,
35429
+ onOutcome: async (outcome) => {
35430
+ if (outcome.status === "disabled") return;
35431
+ await eventing.publish(
35432
+ [
35047
35433
  {
35048
- type: "turn.startup.phase.completed",
35049
- payload: {
35050
- phase: "model_preparation",
35051
- durationMs: Math.max(
35052
- 0,
35053
- Math.round(
35054
- performance.now() - eventing.firstModelRequestPreparationStartedAt
35055
- )
35056
- )
35057
- }
35058
- }
35059
- ] : [],
35060
- {
35061
- type: "agent.model.request",
35062
- payload: {
35063
- ...event,
35064
- provider: "supergrok-subscription",
35065
- turnId: attempt.turnId,
35066
- attemptId: input.attemptId,
35067
- dispatchId,
35068
- executionGeneration: attempt.executionGeneration
35434
+ type: outcome.status === "failed" ? "knowledge.source.failed" : "knowledge.source.prepared",
35435
+ payload: outcome
35069
35436
  }
35070
- }
35071
- ]);
35072
- attempt.providerRecoveryCount = providerRecoveryCountAfterModelRequestPhase(
35073
- attempt.providerRecoveryCount,
35074
- event.phase
35437
+ ],
35438
+ true
35439
+ );
35440
+ }
35441
+ });
35442
+ const knowledgeSourcePreparationNote = [knowledgeRecoveryNote, attachmentPreparationNote].filter(Boolean).join("\n\n") || void 0;
35443
+ const sandboxRoute = await resolveSandboxRoute({
35444
+ input,
35445
+ settings,
35446
+ db,
35447
+ eventing,
35448
+ sandboxState,
35449
+ media,
35450
+ fileAuthoritySubjectId,
35451
+ runSettings,
35452
+ logicalSandboxSettings
35453
+ });
35454
+ const {
35455
+ routingOn,
35456
+ activeSandboxBackend,
35457
+ machinePrimary,
35458
+ groupBoxBackend,
35459
+ sandboxCreationBackend,
35460
+ effectiveRunCredentialBackend
35461
+ } = sandboxRoute;
35462
+ const { turnResources, runtimeResources } = await waitForTurnOperation(
35463
+ applyTurnGitHubRepositoryBindings({
35464
+ db,
35465
+ settings: runSettings,
35466
+ workspaceId: input.workspaceId,
35467
+ sessionId: input.sessionId,
35468
+ activeSandboxBackend,
35469
+ claimedTurnResources,
35470
+ claimedRuntimeResources,
35471
+ publish: async (events) => {
35472
+ await eventing.publish(events, true);
35473
+ },
35474
+ warn: (message, fields) => observability.warn(message, fields)
35475
+ }),
35476
+ cancellationSignal,
35477
+ void 0
35478
+ );
35479
+ const runCredentials = await prepareRunCredentials({
35480
+ input,
35481
+ settings,
35482
+ db,
35483
+ observability,
35484
+ cancellationSignal,
35485
+ connectionCredentials,
35486
+ personalGitHubCredentials,
35487
+ eventing,
35488
+ attempt,
35489
+ renewals,
35490
+ sandboxState,
35491
+ sandboxRuntime,
35492
+ turn,
35493
+ session,
35494
+ fileAuthoritySubjectId,
35495
+ runSettings,
35496
+ workspaceVariableSet,
35497
+ turnResources,
35498
+ requiredGeneratedVideoFiles,
35499
+ machinePrimary,
35500
+ activeSandboxBackend,
35501
+ groupBoxBackend,
35502
+ sandboxCreationBackend,
35503
+ effectiveRunCredentialBackend,
35504
+ sandboxWorkspaceEnvironmentValues,
35505
+ connectionScope,
35506
+ runWorkspaceMutationForSandbox
35507
+ });
35508
+ const {
35509
+ runCredentialResolver,
35510
+ establishPolicy,
35511
+ initialRunCredentialMaterial,
35512
+ runCredentialsNote,
35513
+ hostCredentialRootSessionId,
35514
+ codemodeAuthority,
35515
+ sandboxArtifactRuntime,
35516
+ sandboxEnvironment,
35517
+ sandboxGitToken,
35518
+ sandboxGitTokens,
35519
+ sandboxGitCredentialBindings,
35520
+ sandboxCodemodeToken,
35521
+ sandboxCodemodeTokenExpiresAt,
35522
+ transientCodemodeEnvironment,
35523
+ initialGitCredentials,
35524
+ startRunGitCredentialsMint,
35525
+ attachGitCredentialRenewal,
35526
+ attachCodemodeTokenRenewal,
35527
+ attachRunCredentialRenewal
35528
+ } = runCredentials;
35529
+ await establishTurnSandbox({
35530
+ ...sandboxRoute,
35531
+ input,
35532
+ settings,
35533
+ db,
35534
+ bus,
35535
+ objectStorage,
35536
+ observability,
35537
+ cancellationSignal,
35538
+ runtimeCancellationSignal,
35539
+ sandboxResumeSignal,
35540
+ activityContext,
35541
+ opJournal,
35542
+ sandboxState,
35543
+ eventing,
35544
+ attempt,
35545
+ turn,
35546
+ session,
35547
+ fileAuthoritySubjectId,
35548
+ capabilitySettings,
35549
+ turnExecutionPolicy,
35550
+ runSettings,
35551
+ logicalSandboxSettings,
35552
+ verifiedRigProviderImageId,
35553
+ runtimePreparationStartedAt,
35554
+ establishPolicy,
35555
+ sandboxEnvironment,
35556
+ startRunGitCredentialsMint,
35557
+ machineOpObserver,
35558
+ sandboxOperationObserver,
35559
+ sandboxRuntime,
35560
+ transientCodemodeEnvironment,
35561
+ rigVersion,
35562
+ turnResources
35563
+ });
35564
+ const ordinaryFileResourceDownloads = await (async () => {
35565
+ const fileResolutionStartedAt = performance.now();
35566
+ let fileResolutionOutcome = "completed";
35567
+ try {
35568
+ return await waitForTurnOperation(
35569
+ sandboxFileDownloadsForRun(
35570
+ runSettings,
35571
+ db,
35572
+ objectStorage,
35573
+ input.accountId,
35574
+ input.workspaceId,
35575
+ fileAuthoritySubjectId,
35576
+ turn.resources,
35577
+ activeSandboxBackend ?? groupBoxBackend
35578
+ ),
35579
+ cancellationSignal,
35580
+ void 0
35075
35581
  );
35076
35582
  } catch (error51) {
35077
- auditOutcome = "failed";
35583
+ fileResolutionOutcome = "failed";
35078
35584
  throw error51;
35079
35585
  } finally {
35080
- if (auditStartedAt !== null) {
35081
- recordTurnStartupPhase(observability, {
35082
- phase: "model_request_audit",
35083
- provider: turnExecutionPolicy.providerId,
35084
- backend: activeSandboxBackend ?? groupBoxBackend,
35085
- outcome: auditOutcome,
35086
- durationSeconds: (performance.now() - auditStartedAt) / 1e3,
35087
- count: turnTools.length
35088
- });
35089
- }
35586
+ recordTurnStartupPhase(observability, {
35587
+ phase: "file_resolution",
35588
+ provider: turnExecutionPolicy.providerId,
35589
+ backend: activeSandboxBackend ?? groupBoxBackend,
35590
+ outcome: fileResolutionOutcome,
35591
+ durationSeconds: (performance.now() - fileResolutionStartedAt) / 1e3,
35592
+ count: turn.resources.length
35593
+ });
35090
35594
  }
35595
+ })();
35596
+ if (requiredGeneratedVideoFiles.length > 0 && !objectStorage) {
35597
+ throw new Error("Generated video materialization requires object storage");
35091
35598
  }
35092
- });
35093
- providerTurn.xaiRequestContext = authorization.context;
35094
- }
35095
- const withCodex = (fn) => codexContext ? codexRequestStorage.run(codexContext, fn) : fn();
35096
- const withProviderRequestContext = (fn) => providerTurn.xaiRequestContext ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn) : withCodex(fn);
35097
- let codexSessionTitleRequestSequence = 0;
35098
- let xaiSessionTitleRequestSequence = 0;
35099
- const codexSessionTitleContext = codexContext ? sessionTitleCodexRequestContext(
35100
- codexContext,
35101
- () => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`
35102
- ) : null;
35103
- const xaiSessionTitleContext = providerTurn.xaiRequestContext ? sessionTitleXaiRequestContext(
35104
- providerTurn.xaiRequestContext,
35105
- () => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`
35106
- ) : null;
35107
- const withSessionTitleProviderRequestContext = (fn) => xaiSessionTitleContext ? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn) : codexSessionTitleContext ? codexRequestStorage.run(codexSessionTitleContext, fn) : fn();
35108
- const withCodexRemoteCompaction = (fn) => withCodex(
35109
- () => withCodexRequestOverrides(
35110
- {
35111
- betaFeatures: [REMOTE_COMPACTION_V2_BETA_FEATURE],
35112
- turnMetadata: {
35113
- request_kind: "compaction",
35114
- compaction: {
35115
- implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION2,
35116
- strategy: "memento"
35117
- }
35118
- }
35119
- },
35120
- fn
35121
- )
35122
- );
35123
- const compactionPrep = await prepareCompaction({
35124
- input,
35125
- settings: capabilitySettings,
35126
- db,
35127
- bus,
35128
- observability,
35129
- cancellationSignal,
35130
- control,
35131
- attempt,
35132
- billingState,
35133
- eventing: wiredEventing,
35134
- providerTurn,
35135
- leases,
35136
- media,
35137
- claimedResult,
35138
- claimedModelUsageSourceKeys,
35139
- emittedModelUsageSourceKeys,
35140
- modelUsageDispatchId,
35141
- turn,
35142
- session,
35143
- turnExecutionPolicy,
35144
- resolvedModel,
35145
- workspaceAgentInstructions,
35146
- workspaceGovernance,
35147
- structuredWorkspacePolicyActive,
35148
- workspaceMemory,
35149
- rigVersion,
35150
- rigName,
35151
- compactionModelHistoryProjector,
35152
- summarizeContextForCompaction,
35153
- withProviderRequestContext,
35154
- withCodexRemoteCompaction
35155
- });
35156
- if ("exit" in compactionPrep) return compactionPrep.exit;
35157
- const {
35158
- promptCacheKey,
35159
- remotePrefix,
35160
- remoteCompactionRequester,
35161
- publishCompactionLiveEvents,
35162
- publishCompactionOutcomeEvents,
35163
- compactionModeOptions,
35164
- compactionOnlyTurn,
35165
- compactionSummarizerFor,
35166
- settleDeferredSteerAfterCompaction
35167
- } = compactionPrep.ok;
35168
- const toolPolicy = await prepareTurnToolPolicy({
35169
- input,
35170
- db,
35171
- cancellationSignal,
35172
- connectionCredentials,
35173
- turn,
35174
- session,
35175
- fileAuthoritySubjectId,
35176
- capabilitySettings,
35177
- runSettings,
35178
- rigVersion,
35179
- workspaceRefs
35180
- });
35181
- const {
35182
- turnResources: claimedTurnResources,
35183
- runtimeResources: claimedRuntimeResources,
35184
- mcpAvailabilityNote,
35185
- turnTools,
35186
- connectionScope,
35187
- workspaceVariableSet,
35188
- sandboxWorkspaceEnvironmentValues
35189
- } = toolPolicy;
35190
- const sandboxRoute = await resolveSandboxRoute({
35191
- input,
35192
- settings,
35193
- db,
35194
- eventing,
35195
- sandboxState,
35196
- media,
35197
- fileAuthoritySubjectId,
35198
- runSettings,
35199
- logicalSandboxSettings
35200
- });
35201
- const {
35202
- routingOn,
35203
- activeSandboxBackend,
35204
- machinePrimary,
35205
- groupBoxBackend,
35206
- sandboxCreationBackend,
35207
- effectiveRunCredentialBackend
35208
- } = sandboxRoute;
35209
- const { turnResources, runtimeResources } = await waitForTurnOperation(
35210
- applyTurnGitHubRepositoryBindings({
35211
- db,
35212
- settings: runSettings,
35213
- workspaceId: input.workspaceId,
35214
- sessionId: input.sessionId,
35215
- activeSandboxBackend,
35216
- claimedTurnResources,
35217
- claimedRuntimeResources,
35218
- publish: async (events) => {
35219
- await eventing.publish(events, true);
35220
- },
35221
- warn: (message, fields) => observability.warn(message, fields)
35222
- }),
35223
- cancellationSignal,
35224
- void 0
35225
- );
35226
- const runCredentials = await prepareRunCredentials({
35227
- input,
35228
- settings,
35229
- db,
35230
- observability,
35231
- cancellationSignal,
35232
- connectionCredentials,
35233
- personalGitHubCredentials,
35234
- eventing,
35235
- attempt,
35236
- renewals,
35237
- sandboxState,
35238
- sandboxRuntime,
35239
- turn,
35240
- session,
35241
- fileAuthoritySubjectId,
35242
- runSettings,
35243
- workspaceVariableSet,
35244
- turnResources,
35245
- requiredGeneratedVideoFiles,
35246
- machinePrimary,
35247
- activeSandboxBackend,
35248
- groupBoxBackend,
35249
- sandboxCreationBackend,
35250
- effectiveRunCredentialBackend,
35251
- sandboxWorkspaceEnvironmentValues,
35252
- connectionScope,
35253
- runWorkspaceMutationForSandbox
35254
- });
35255
- const {
35256
- runCredentialResolver,
35257
- establishPolicy,
35258
- initialRunCredentialMaterial,
35259
- runCredentialsNote,
35260
- hostCredentialRootSessionId,
35261
- codemodeAuthority,
35262
- sandboxArtifactRuntime,
35263
- sandboxEnvironment,
35264
- sandboxGitToken,
35265
- sandboxGitTokens,
35266
- sandboxGitCredentialBindings,
35267
- sandboxCodemodeToken,
35268
- sandboxCodemodeTokenExpiresAt,
35269
- transientCodemodeEnvironment,
35270
- initialGitCredentials,
35271
- startRunGitCredentialsMint,
35272
- attachGitCredentialRenewal,
35273
- attachCodemodeTokenRenewal,
35274
- attachRunCredentialRenewal
35275
- } = runCredentials;
35276
- await establishTurnSandbox({
35277
- ...sandboxRoute,
35278
- input,
35279
- settings,
35280
- db,
35281
- bus,
35282
- objectStorage,
35283
- observability,
35284
- cancellationSignal,
35285
- runtimeCancellationSignal,
35286
- sandboxResumeSignal,
35287
- activityContext,
35288
- opJournal,
35289
- sandboxState,
35290
- eventing,
35291
- attempt,
35292
- turn,
35293
- session,
35294
- fileAuthoritySubjectId,
35295
- capabilitySettings,
35296
- turnExecutionPolicy,
35297
- runSettings,
35298
- logicalSandboxSettings,
35299
- verifiedRigProviderImageId,
35300
- runtimePreparationStartedAt,
35301
- establishPolicy,
35302
- sandboxEnvironment,
35303
- startRunGitCredentialsMint,
35304
- machineOpObserver,
35305
- sandboxOperationObserver,
35306
- sandboxRuntime,
35307
- transientCodemodeEnvironment,
35308
- rigVersion,
35309
- turnResources
35310
- });
35311
- const ordinaryFileResourceDownloads = await (async () => {
35312
- const fileResolutionStartedAt = performance.now();
35313
- let fileResolutionOutcome = "completed";
35314
- try {
35315
- return await waitForTurnOperation(
35316
- sandboxFileDownloadsForRun(
35317
- runSettings,
35318
- db,
35599
+ const generatedVideoDownloads = [];
35600
+ if (objectStorage && requiredGeneratedVideoFiles.length > 0) {
35601
+ const downloadStorage = objectStorageForSandboxDownloads(
35602
+ eventing.modelRunSettings,
35319
35603
  objectStorage,
35320
- input.accountId,
35321
- input.workspaceId,
35322
- fileAuthoritySubjectId,
35323
- turn.resources,
35324
- activeSandboxBackend ?? groupBoxBackend
35325
- ),
35604
+ media.sandboxFileDownloadBackend
35605
+ );
35606
+ for (const file2 of requiredGeneratedVideoFiles) {
35607
+ const signed = await downloadStorage.createGetUrl({
35608
+ key: file2.objectKey
35609
+ });
35610
+ generatedVideoDownloads.push({
35611
+ fileId: file2.fileId,
35612
+ mountPath: "generated-videos",
35613
+ filename: file2.filename,
35614
+ url: signed.url,
35615
+ expiresAt: signed.expiresAt,
35616
+ sizeBytes: file2.sizeBytes,
35617
+ sha256: file2.sha256
35618
+ });
35619
+ }
35620
+ }
35621
+ const fileResourceDownloads = [
35622
+ ...new Map(
35623
+ [...ordinaryFileResourceDownloads, ...generatedVideoDownloads].map((download) => [
35624
+ download.fileId,
35625
+ download
35626
+ ])
35627
+ ).values()
35628
+ ];
35629
+ const toolRuntime = await prepareTurnToolRuntime({
35630
+ fetchKnowledgeSource: sourceActivities.runKnowledgeSourceSyncBatch,
35631
+ selectedSkillActivations: packRuntime.skillActivations,
35632
+ input,
35633
+ catalogSourceSettings,
35634
+ db,
35635
+ bus,
35636
+ runtime,
35637
+ objectStorage,
35638
+ observability,
35326
35639
  cancellationSignal,
35327
- void 0
35328
- );
35329
- } catch (error51) {
35330
- fileResolutionOutcome = "failed";
35331
- throw error51;
35332
- } finally {
35333
- recordTurnStartupPhase(observability, {
35334
- phase: "file_resolution",
35335
- provider: turnExecutionPolicy.providerId,
35336
- backend: activeSandboxBackend ?? groupBoxBackend,
35337
- outcome: fileResolutionOutcome,
35338
- durationSeconds: (performance.now() - fileResolutionStartedAt) / 1e3,
35339
- count: turn.resources.length
35640
+ connectionCredentials,
35641
+ eventing,
35642
+ attempt,
35643
+ sandboxState,
35644
+ media,
35645
+ toolResultSpill,
35646
+ turn,
35647
+ session,
35648
+ fileAuthoritySubjectId,
35649
+ capabilitySettings,
35650
+ installedApiIntegrations,
35651
+ codexAppsCredentialId,
35652
+ turnExecutionPolicy,
35653
+ trigger,
35654
+ runSettings,
35655
+ lazyToolTransport,
35656
+ turnTools,
35657
+ connectionScope,
35658
+ hostCredentialRootSessionId,
35659
+ sandboxArtifactRuntime,
35660
+ activeSandboxBackend,
35661
+ groupBoxBackend,
35662
+ routingOn,
35663
+ runtimeCancellationSignal,
35664
+ credentialSubjectId,
35665
+ interactionInterventionResume,
35666
+ runWorkspaceMutationForSandbox,
35667
+ throwIfWorkerShuttingDown,
35668
+ throwIfTurnCancelled
35340
35669
  });
35341
- }
35342
- })();
35343
- if (requiredGeneratedVideoFiles.length > 0 && !objectStorage) {
35344
- throw new Error("Generated video materialization requires object storage");
35345
- }
35346
- const generatedVideoDownloads = [];
35347
- if (objectStorage && requiredGeneratedVideoFiles.length > 0) {
35348
- const downloadStorage = objectStorageForSandboxDownloads(
35349
- eventing.modelRunSettings,
35350
- objectStorage,
35351
- media.sandboxFileDownloadBackend
35352
- );
35353
- for (const file2 of requiredGeneratedVideoFiles) {
35354
- const signed = await downloadStorage.createGetUrl({
35355
- key: file2.objectKey
35670
+ const {
35671
+ attemptConnectorActionBindings,
35672
+ connectorActionPolicy,
35673
+ generateSessionTitleInParallel,
35674
+ postToolPreparationStartedAt,
35675
+ preparationIndependentToolNames
35676
+ } = toolRuntime;
35677
+ const builtAgent = await buildTurnAgent({
35678
+ skillCatalog: toolRuntime.skillCatalog,
35679
+ input,
35680
+ db,
35681
+ runtime,
35682
+ objectStorage,
35683
+ observability,
35684
+ cancellationSignal,
35685
+ runtimeCancellationSignal,
35686
+ eventing,
35687
+ attempt,
35688
+ sandboxState,
35689
+ providerTurn,
35690
+ media,
35691
+ leases,
35692
+ turn,
35693
+ session,
35694
+ fileAuthoritySubjectId,
35695
+ capabilitySettings,
35696
+ humanInputResume,
35697
+ turnExecutionPolicy,
35698
+ runSettings,
35699
+ logicalSandboxSettings,
35700
+ verifiedRigProviderImageId,
35701
+ resolvedModel,
35702
+ nativeImageProviderBinding,
35703
+ lazyToolTransport,
35704
+ modelInputPolicy,
35705
+ supportsImageInput,
35706
+ agentHumanInputEnabled,
35707
+ workspaceAgentInstructions,
35708
+ workspaceGovernance,
35709
+ structuredWorkspacePolicyActive,
35710
+ workspaceMemory,
35711
+ rigVersion,
35712
+ rigName,
35713
+ buildCompanyBrainContributionReceiptFor,
35714
+ promptCacheKey,
35715
+ workspaceVariableSet,
35716
+ runtimeResources,
35717
+ sandboxEnvironment,
35718
+ sandboxArtifactRuntime,
35719
+ sandboxGitToken,
35720
+ sandboxGitTokens,
35721
+ sandboxGitCredentialBindings,
35722
+ sandboxCodemodeToken,
35723
+ fileResourceDownloads,
35724
+ attemptConnectorActionBindings,
35725
+ connectorActionPolicy,
35726
+ trigger,
35727
+ preparationIndependentToolNames,
35728
+ videoGenerationAcceptancesByCallId,
35729
+ activeSandboxBackend,
35730
+ groupBoxBackend,
35731
+ postToolPreparationStartedAt,
35732
+ codexContext
35356
35733
  });
35357
- generatedVideoDownloads.push({
35358
- fileId: file2.fileId,
35359
- mountPath: "generated-videos",
35360
- filename: file2.filename,
35361
- url: signed.url,
35362
- expiresAt: signed.expiresAt,
35363
- sizeBytes: file2.sizeBytes,
35364
- sha256: file2.sha256
35734
+ const { agent, modelVisibleSkillCatalogText, postAgentPreparationStartedAt } = builtAgent;
35735
+ await bindLazySandboxProvisioner({
35736
+ ...sandboxRoute,
35737
+ input,
35738
+ settings,
35739
+ db,
35740
+ bus,
35741
+ objectStorage,
35742
+ observability,
35743
+ cancellationSignal,
35744
+ runtimeCancellationSignal,
35745
+ sandboxResumeSignal,
35746
+ activityContext,
35747
+ opJournal,
35748
+ sandboxState,
35749
+ eventing,
35750
+ attempt,
35751
+ turn,
35752
+ session,
35753
+ fileAuthoritySubjectId,
35754
+ capabilitySettings,
35755
+ turnExecutionPolicy,
35756
+ runSettings,
35757
+ logicalSandboxSettings,
35758
+ verifiedRigProviderImageId,
35759
+ runtimePreparationStartedAt,
35760
+ establishPolicy,
35761
+ sandboxEnvironment,
35762
+ startRunGitCredentialsMint,
35763
+ machineOpObserver,
35764
+ sandboxOperationObserver,
35765
+ sandboxRuntime,
35766
+ transientCodemodeEnvironment,
35767
+ agent,
35768
+ attachRunCredentialRenewal,
35769
+ attachGitCredentialRenewal,
35770
+ attachCodemodeTokenRenewal,
35771
+ throwIfWorkerShuttingDown,
35772
+ throwIfTurnCancelled,
35773
+ initialRunCredentialMaterial,
35774
+ sandboxCodemodeToken,
35775
+ media,
35776
+ toolResultSpill,
35777
+ connectionScope,
35778
+ codemodeAuthority,
35779
+ rigVersion,
35780
+ turnResources
35365
35781
  });
35366
- }
35367
- }
35368
- const fileResourceDownloads = [
35369
- ...new Map(
35370
- [...ordinaryFileResourceDownloads, ...generatedVideoDownloads].map((download) => [
35371
- download.fileId,
35372
- download
35373
- ])
35374
- ).values()
35375
- ];
35376
- const toolRuntime = await prepareTurnToolRuntime({
35377
- selectedSkillActivations: packRuntime.skillActivations,
35378
- input,
35379
- catalogSourceSettings,
35380
- db,
35381
- bus,
35382
- runtime,
35383
- objectStorage,
35384
- observability,
35385
- cancellationSignal,
35386
- connectionCredentials,
35387
- eventing,
35388
- attempt,
35389
- sandboxState,
35390
- media,
35391
- toolResultSpill,
35392
- turn,
35393
- session,
35394
- fileAuthoritySubjectId,
35395
- capabilitySettings,
35396
- installedApiIntegrations,
35397
- codexAppsCredentialId,
35398
- turnExecutionPolicy,
35399
- trigger,
35400
- runSettings,
35401
- lazyToolTransport,
35402
- turnTools,
35403
- connectionScope,
35404
- hostCredentialRootSessionId,
35405
- sandboxArtifactRuntime,
35406
- activeSandboxBackend,
35407
- groupBoxBackend,
35408
- routingOn,
35409
- runtimeCancellationSignal,
35410
- credentialSubjectId,
35411
- interactionInterventionResume,
35412
- runWorkspaceMutationForSandbox,
35413
- throwIfWorkerShuttingDown,
35414
- throwIfTurnCancelled
35415
- });
35416
- const {
35417
- attemptConnectorActionBindings,
35418
- connectorActionPolicy,
35419
- generateSessionTitleInParallel,
35420
- postToolPreparationStartedAt,
35421
- preparationIndependentToolNames
35422
- } = toolRuntime;
35423
- const builtAgent = await buildTurnAgent({
35424
- skillCatalog: toolRuntime.skillCatalog,
35425
- input,
35426
- db,
35427
- runtime,
35428
- objectStorage,
35429
- observability,
35430
- cancellationSignal,
35431
- runtimeCancellationSignal,
35432
- eventing,
35433
- attempt,
35434
- sandboxState,
35435
- providerTurn,
35436
- media,
35437
- leases,
35438
- turn,
35439
- session,
35440
- fileAuthoritySubjectId,
35441
- capabilitySettings,
35442
- humanInputResume,
35443
- turnExecutionPolicy,
35444
- runSettings,
35445
- logicalSandboxSettings,
35446
- verifiedRigProviderImageId,
35447
- resolvedModel,
35448
- nativeImageProviderBinding,
35449
- lazyToolTransport,
35450
- modelInputPolicy,
35451
- supportsImageInput,
35452
- agentHumanInputEnabled,
35453
- workspaceAgentInstructions,
35454
- workspaceGovernance,
35455
- structuredWorkspacePolicyActive,
35456
- workspaceMemory,
35457
- rigVersion,
35458
- rigName,
35459
- buildCompanyBrainContributionReceiptFor,
35460
- promptCacheKey,
35461
- workspaceVariableSet,
35462
- runtimeResources,
35463
- sandboxEnvironment,
35464
- sandboxArtifactRuntime,
35465
- sandboxGitToken,
35466
- sandboxGitTokens,
35467
- sandboxGitCredentialBindings,
35468
- sandboxCodemodeToken,
35469
- fileResourceDownloads,
35470
- attemptConnectorActionBindings,
35471
- connectorActionPolicy,
35472
- trigger,
35473
- preparationIndependentToolNames,
35474
- videoGenerationAcceptancesByCallId,
35475
- activeSandboxBackend,
35476
- groupBoxBackend,
35477
- postToolPreparationStartedAt,
35478
- codexContext
35479
- });
35480
- const { agent, modelVisibleSkillCatalogText, postAgentPreparationStartedAt } = builtAgent;
35481
- await bindLazySandboxProvisioner({
35482
- ...sandboxRoute,
35483
- input,
35484
- settings,
35485
- db,
35486
- bus,
35487
- objectStorage,
35488
- observability,
35489
- cancellationSignal,
35490
- runtimeCancellationSignal,
35491
- sandboxResumeSignal,
35492
- activityContext,
35493
- opJournal,
35494
- sandboxState,
35495
- eventing,
35496
- attempt,
35497
- turn,
35498
- session,
35499
- fileAuthoritySubjectId,
35500
- capabilitySettings,
35501
- turnExecutionPolicy,
35502
- runSettings,
35503
- logicalSandboxSettings,
35504
- verifiedRigProviderImageId,
35505
- runtimePreparationStartedAt,
35506
- establishPolicy,
35507
- sandboxEnvironment,
35508
- startRunGitCredentialsMint,
35509
- machineOpObserver,
35510
- sandboxOperationObserver,
35511
- sandboxRuntime,
35512
- transientCodemodeEnvironment,
35513
- agent,
35514
- attachRunCredentialRenewal,
35515
- attachGitCredentialRenewal,
35516
- attachCodemodeTokenRenewal,
35517
- throwIfWorkerShuttingDown,
35518
- throwIfTurnCancelled,
35519
- initialRunCredentialMaterial,
35520
- sandboxCodemodeToken,
35521
- media,
35522
- toolResultSpill,
35523
- connectionScope,
35524
- codemodeAuthority,
35525
- rigVersion,
35526
- turnResources
35527
- });
35528
- let companyBrainContributionReceiptRecorded = false;
35529
- const recordCompanyBrainContributionReceiptOnce = () => {
35530
- if (companyBrainContributionReceiptRecorded) return;
35531
- companyBrainContributionReceiptRecorded = true;
35532
- try {
35533
- const companyBrainContributionReceipt = buildCompanyBrainContributionReceiptFor(
35534
- modelVisibleSkillCatalogText
35535
- );
35536
- eventing.companyBrainContextContributions = summarizeCompanyBrainContributions(
35537
- companyBrainContributionReceipt
35538
- );
35539
- recordCompanyBrainContributions(observability, companyBrainContributionReceipt);
35540
- observability.info("model context contribution receipt", {
35541
- attemptId: companyBrainContributionReceipt.attemptId,
35542
- turnId: companyBrainContributionReceipt.turnId,
35543
- contextSelectionReceiptId: companyBrainContributionReceipt.contextSelectionReceiptId,
35544
- sessionRole: companyBrainContributionReceipt.sessionRole,
35545
- memoryPromptMode: companyBrainContributionReceipt.memoryPromptMode,
35546
- instructionPolicySnapshotId: companyBrainContributionReceipt.instructionPolicySnapshotId,
35547
- preferenceSnapshotId: companyBrainContributionReceipt.preferenceSnapshotId,
35548
- companyProfileSnapshotId: companyBrainContributionReceipt.companyProfileSnapshotId,
35549
- contributions: JSON.stringify(companyBrainContributionReceipt.contributions)
35782
+ let companyBrainContributionReceiptRecorded = false;
35783
+ const recordCompanyBrainContributionReceiptOnce = () => {
35784
+ if (companyBrainContributionReceiptRecorded) return;
35785
+ companyBrainContributionReceiptRecorded = true;
35786
+ try {
35787
+ const companyBrainContributionReceipt = buildCompanyBrainContributionReceiptFor(
35788
+ modelVisibleSkillCatalogText
35789
+ );
35790
+ eventing.companyBrainContextContributions = summarizeCompanyBrainContributions(
35791
+ companyBrainContributionReceipt
35792
+ );
35793
+ recordCompanyBrainContributions(observability, companyBrainContributionReceipt);
35794
+ observability.info("model context contribution receipt", {
35795
+ attemptId: companyBrainContributionReceipt.attemptId,
35796
+ turnId: companyBrainContributionReceipt.turnId,
35797
+ contextSelectionReceiptId: companyBrainContributionReceipt.contextSelectionReceiptId,
35798
+ sessionRole: companyBrainContributionReceipt.sessionRole,
35799
+ memoryPromptMode: companyBrainContributionReceipt.memoryPromptMode,
35800
+ instructionPolicySnapshotId: companyBrainContributionReceipt.instructionPolicySnapshotId,
35801
+ preferenceSnapshotId: companyBrainContributionReceipt.preferenceSnapshotId,
35802
+ companyProfileSnapshotId: companyBrainContributionReceipt.companyProfileSnapshotId,
35803
+ contributions: JSON.stringify(companyBrainContributionReceipt.contributions)
35804
+ });
35805
+ } catch {
35806
+ }
35807
+ };
35808
+ const postAgentCompaction = await runPostAgentCompaction({
35809
+ input,
35810
+ settings: capabilitySettings,
35811
+ db,
35812
+ bus,
35813
+ observability,
35814
+ cancellationSignal,
35815
+ control,
35816
+ attempt,
35817
+ billingState,
35818
+ eventing: wiredEventing,
35819
+ providerTurn,
35820
+ leases,
35821
+ media,
35822
+ claimedResult,
35823
+ claimedModelUsageSourceKeys,
35824
+ emittedModelUsageSourceKeys,
35825
+ modelUsageDispatchId,
35826
+ turn,
35827
+ session,
35828
+ turnExecutionPolicy,
35829
+ resolvedModel,
35830
+ workspaceAgentInstructions,
35831
+ workspaceGovernance,
35832
+ structuredWorkspacePolicyActive,
35833
+ workspaceMemory,
35834
+ rigVersion,
35835
+ rigName,
35836
+ compactionModelHistoryProjector,
35837
+ summarizeContextForCompaction,
35838
+ withProviderRequestContext,
35839
+ withCodexRemoteCompaction,
35840
+ remotePrefix,
35841
+ remoteCompactionRequester,
35842
+ publishCompactionLiveEvents,
35843
+ publishCompactionOutcomeEvents,
35844
+ compactionModeOptions,
35845
+ compactionOnlyTurn,
35846
+ compactionSummarizerFor,
35847
+ settleDeferredSteerAfterCompaction,
35848
+ agent
35550
35849
  });
35551
- } catch {
35552
- }
35553
- };
35554
- const postAgentCompaction = await runPostAgentCompaction({
35555
- input,
35556
- settings: capabilitySettings,
35557
- db,
35558
- bus,
35559
- observability,
35560
- cancellationSignal,
35561
- control,
35562
- attempt,
35563
- billingState,
35564
- eventing: wiredEventing,
35565
- providerTurn,
35566
- leases,
35567
- media,
35568
- claimedResult,
35569
- claimedModelUsageSourceKeys,
35570
- emittedModelUsageSourceKeys,
35571
- modelUsageDispatchId,
35572
- turn,
35573
- session,
35574
- turnExecutionPolicy,
35575
- resolvedModel,
35576
- workspaceAgentInstructions,
35577
- workspaceGovernance,
35578
- structuredWorkspacePolicyActive,
35579
- workspaceMemory,
35580
- rigVersion,
35581
- rigName,
35582
- compactionModelHistoryProjector,
35583
- summarizeContextForCompaction,
35584
- withProviderRequestContext,
35585
- withCodexRemoteCompaction,
35586
- remotePrefix,
35587
- remoteCompactionRequester,
35588
- publishCompactionLiveEvents,
35589
- publishCompactionOutcomeEvents,
35590
- compactionModeOptions,
35591
- compactionOnlyTurn,
35592
- compactionSummarizerFor,
35593
- settleDeferredSteerAfterCompaction,
35594
- agent
35595
- });
35596
- if ("exit" in postAgentCompaction) return postAgentCompaction.exit;
35597
- const { compactSummarizer } = postAgentCompaction.ok;
35598
- recordTurnStartupPhase(observability, {
35599
- phase: "post_agent_preparation",
35600
- provider: turnExecutionPolicy.providerId,
35601
- backend: activeSandboxBackend ?? groupBoxBackend,
35602
- outcome: "completed",
35603
- durationSeconds: (performance.now() - postAgentPreparationStartedAt) / 1e3
35604
- });
35605
- let fileMaterializationFailures = [];
35606
- let fileDownloadsMaterializedForRun = false;
35607
- if (sandboxState.resolvedSandbox && sandboxState.setupBoxSession && fileResourceDownloads.length > 0) {
35608
- const fileMaterializationStartedAt = performance.now();
35609
- let fileMaterializationOutcome = "completed";
35610
- let fileMaterializationCache = "miss";
35611
- try {
35612
- const boxInstanceId = sandboxState.resolvedSandbox.established.instanceId;
35613
- const alreadyMaterialized = await getMaterializedSandboxFileResources3(db, {
35614
- accountId: input.accountId,
35615
- workspaceId: input.workspaceId,
35616
- sandboxGroupId: session.sandboxGroupId,
35617
- expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
35618
- instanceId: boxInstanceId
35850
+ if ("exit" in postAgentCompaction) return postAgentCompaction.exit;
35851
+ const { compactSummarizer } = postAgentCompaction.ok;
35852
+ recordTurnStartupPhase(observability, {
35853
+ phase: "post_agent_preparation",
35854
+ provider: turnExecutionPolicy.providerId,
35855
+ backend: activeSandboxBackend ?? groupBoxBackend,
35856
+ outcome: "completed",
35857
+ durationSeconds: (performance.now() - postAgentPreparationStartedAt) / 1e3
35619
35858
  });
35620
- const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
35621
- fileResourceDownloads,
35622
- alreadyMaterialized
35623
- );
35624
- if (downloadsToMaterialize.length === 0) {
35625
- fileMaterializationCache = "hit";
35626
- }
35627
- const runAs = sandboxRunAs6(runSettings);
35628
- if (downloadsToMaterialize.length > 0) {
35629
- const materialized = await runWorkspaceMutationForSandbox(
35630
- sandboxState.resolvedSandbox,
35631
- "fileMaterialization",
35632
- async () => await materializeSandboxFileDownloads3(
35633
- sandboxState.setupBoxSession,
35634
- downloadsToMaterialize,
35635
- {
35636
- onRuntimeEvent: async (event) => {
35637
- await eventing.publish([{ type: event.type, payload: event.payload }], true);
35638
- },
35639
- ...runAs ? { runAs } : {},
35640
- ...eventing.toolCancellationFenceRef.current ? {
35641
- commandRunner: eventing.toolCancellationFenceRef.current.runSandboxCommand.bind(
35642
- eventing.toolCancellationFenceRef.current
35643
- )
35644
- } : {}
35645
- }
35646
- )
35647
- );
35648
- fileMaterializationFailures = materialized.failures;
35649
- fileMaterializationOutcome = sandboxFileMaterializationOutcome(materialized.failures);
35650
- const failedFileIds = new Set(materialized.failures.map((failure) => failure.fileId));
35651
- const succeededFileIds = downloadsToMaterialize.map((download) => download.fileId).filter((fileId) => !failedFileIds.has(fileId));
35652
- if (succeededFileIds.length > 0) {
35653
- await markSandboxFileResourcesMaterialized3(db, {
35859
+ let fileMaterializationFailures = [];
35860
+ let fileDownloadsMaterializedForRun = false;
35861
+ if (sandboxState.resolvedSandbox && sandboxState.setupBoxSession && fileResourceDownloads.length > 0) {
35862
+ const fileMaterializationStartedAt = performance.now();
35863
+ let fileMaterializationOutcome = "completed";
35864
+ let fileMaterializationCache = "miss";
35865
+ try {
35866
+ const boxInstanceId = sandboxState.resolvedSandbox.established.instanceId;
35867
+ const alreadyMaterialized = await getMaterializedSandboxFileResources3(db, {
35654
35868
  accountId: input.accountId,
35655
35869
  workspaceId: input.workspaceId,
35656
35870
  sandboxGroupId: session.sandboxGroupId,
35657
35871
  expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
35658
- instanceId: boxInstanceId,
35659
- fileIds: succeededFileIds
35872
+ instanceId: boxInstanceId
35873
+ });
35874
+ const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
35875
+ fileResourceDownloads,
35876
+ alreadyMaterialized
35877
+ );
35878
+ if (downloadsToMaterialize.length === 0) {
35879
+ fileMaterializationCache = "hit";
35880
+ }
35881
+ const runAs = sandboxRunAs6(runSettings);
35882
+ if (downloadsToMaterialize.length > 0) {
35883
+ const materialized = await runWorkspaceMutationForSandbox(
35884
+ sandboxState.resolvedSandbox,
35885
+ "fileMaterialization",
35886
+ async () => await materializeSandboxFileDownloads3(
35887
+ sandboxState.setupBoxSession,
35888
+ downloadsToMaterialize,
35889
+ {
35890
+ onRuntimeEvent: async (event) => {
35891
+ await eventing.publish(
35892
+ [{ type: event.type, payload: event.payload }],
35893
+ true
35894
+ );
35895
+ },
35896
+ ...runAs ? { runAs } : {},
35897
+ ...eventing.toolCancellationFenceRef.current ? {
35898
+ commandRunner: eventing.toolCancellationFenceRef.current.runSandboxCommand.bind(
35899
+ eventing.toolCancellationFenceRef.current
35900
+ )
35901
+ } : {}
35902
+ }
35903
+ )
35904
+ );
35905
+ fileMaterializationFailures = materialized.failures;
35906
+ fileMaterializationOutcome = sandboxFileMaterializationOutcome(
35907
+ materialized.failures
35908
+ );
35909
+ const failedFileIds = new Set(
35910
+ materialized.failures.map((failure) => failure.fileId)
35911
+ );
35912
+ const succeededFileIds = downloadsToMaterialize.map((download) => download.fileId).filter((fileId) => !failedFileIds.has(fileId));
35913
+ if (succeededFileIds.length > 0) {
35914
+ await markSandboxFileResourcesMaterialized3(db, {
35915
+ accountId: input.accountId,
35916
+ workspaceId: input.workspaceId,
35917
+ sandboxGroupId: session.sandboxGroupId,
35918
+ expectedEpoch: sandboxState.resolvedSandbox.leaseEpoch,
35919
+ instanceId: boxInstanceId,
35920
+ fileIds: succeededFileIds
35921
+ });
35922
+ }
35923
+ }
35924
+ fileDownloadsMaterializedForRun = true;
35925
+ } catch (error51) {
35926
+ fileMaterializationOutcome = "failed";
35927
+ throw error51;
35928
+ } finally {
35929
+ recordTurnStartupPhase(observability, {
35930
+ phase: "file_materialization",
35931
+ provider: turnExecutionPolicy.providerId,
35932
+ backend: activeSandboxBackend ?? groupBoxBackend,
35933
+ outcome: fileMaterializationOutcome,
35934
+ durationSeconds: (performance.now() - fileMaterializationStartedAt) / 1e3,
35935
+ count: fileResourceDownloads.length,
35936
+ cache: fileMaterializationCache
35660
35937
  });
35661
35938
  }
35939
+ } else {
35940
+ recordTurnStartupPhase(observability, {
35941
+ phase: "file_materialization",
35942
+ provider: turnExecutionPolicy.providerId,
35943
+ backend: activeSandboxBackend ?? groupBoxBackend,
35944
+ outcome: "completed",
35945
+ durationSeconds: 0,
35946
+ count: 0
35947
+ });
35662
35948
  }
35663
- fileDownloadsMaterializedForRun = true;
35664
- } catch (error51) {
35665
- fileMaterializationOutcome = "failed";
35666
- throw error51;
35667
- } finally {
35668
- recordTurnStartupPhase(observability, {
35669
- phase: "file_materialization",
35670
- provider: turnExecutionPolicy.providerId,
35671
- backend: activeSandboxBackend ?? groupBoxBackend,
35672
- outcome: fileMaterializationOutcome,
35673
- durationSeconds: (performance.now() - fileMaterializationStartedAt) / 1e3,
35674
- count: fileResourceDownloads.length,
35675
- cache: fileMaterializationCache
35949
+ const requiredVideoFileIds = new Set(
35950
+ requiredGeneratedVideoFiles.map((file2) => file2.fileId)
35951
+ );
35952
+ const failedRequiredVideo = fileMaterializationFailures.find(
35953
+ (failure) => requiredVideoFileIds.has(failure.fileId)
35954
+ );
35955
+ if (failedRequiredVideo) {
35956
+ observability.warn("Generated video sandbox materialization deferred", {
35957
+ errorClass: "SandboxFileDownloadFailure",
35958
+ errorCode: "generated_video_materialization_deferred",
35959
+ origin: "worker"
35960
+ });
35961
+ }
35962
+ const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
35963
+ fileMaterializationFailures
35964
+ );
35965
+ return await runTurnStreamAttempt({
35966
+ input,
35967
+ settings: capabilitySettings,
35968
+ db,
35969
+ bus,
35970
+ runtime,
35971
+ objectStorage,
35972
+ observability,
35973
+ entitlements,
35974
+ startVideoGenerationWorkflow,
35975
+ cancellationSignal,
35976
+ runtimeCancellationSignal,
35977
+ activityContext,
35978
+ dispatchId,
35979
+ control,
35980
+ attempt,
35981
+ billingState,
35982
+ sandboxState,
35983
+ eventing: wiredEventing,
35984
+ providerTurn,
35985
+ leases,
35986
+ historySink,
35987
+ media,
35988
+ toolResultSpill,
35989
+ claimedResult,
35990
+ flushRuntimeBatcher,
35991
+ finalizeTurnOpStreamOps,
35992
+ runWorkspaceMutationForSandbox,
35993
+ throwIfWorkerShuttingDown,
35994
+ throwIfTurnCancelled,
35995
+ setLastInputTokensFenced,
35996
+ attachGitCredentialRenewal,
35997
+ attachCodemodeTokenRenewal,
35998
+ attachRunCredentialRenewal,
35999
+ withProviderRequestContext,
36000
+ withSessionTitleProviderRequestContext,
36001
+ publishCompactionLiveEvents,
36002
+ publishCompactionOutcomeEvents,
36003
+ recordCompanyBrainContributionReceiptOnce,
36004
+ modelCheckpointMemoryCollector,
36005
+ claimedModelUsageSourceKeys,
36006
+ emittedModelUsageSourceKeys,
36007
+ modelUsageDispatchId,
36008
+ workerPreparationStartedAt,
36009
+ fileDownloadsMaterializedForRun,
36010
+ unavailableSandboxFilesNote,
36011
+ runCredentialsNote,
36012
+ mcpAvailabilityNote,
36013
+ knowledgeSourcePreparationNote,
36014
+ fileAuthoritySubjectId,
36015
+ activeSandboxBackend,
36016
+ groupBoxBackend,
36017
+ turnExecutionPolicy,
36018
+ turn,
36019
+ trigger,
36020
+ humanInputResume,
36021
+ attachPendingUpdatesAfterOpenSuffix,
36022
+ agent,
36023
+ resolvedModel,
36024
+ providerApi,
36025
+ runSettings,
36026
+ turnTools,
36027
+ generateSessionTitleInParallel,
36028
+ sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
36029
+ compactSummarizer,
36030
+ settleDeferredSteerAfterCompaction,
36031
+ compactionModelHistoryProjector,
36032
+ generatedImageHistoryProjector,
36033
+ modelHistoryProjector,
36034
+ compactionModeOptions,
36035
+ initialRunCredentialMaterial,
36036
+ initialGitCredentials,
36037
+ sandboxEnvironment,
36038
+ sandboxCodemodeToken,
36039
+ sandboxCodemodeTokenExpiresAt,
36040
+ fileResourceDownloads,
36041
+ runCredentialResolver,
36042
+ videoGenerationAcceptancesByCallId
35676
36043
  });
35677
36044
  }
35678
- } else {
35679
- recordTurnStartupPhase(observability, {
35680
- phase: "file_materialization",
35681
- provider: turnExecutionPolicy.providerId,
35682
- backend: activeSandboxBackend ?? groupBoxBackend,
35683
- outcome: "completed",
35684
- durationSeconds: 0,
35685
- count: 0
35686
- });
35687
- }
35688
- const requiredVideoFileIds = new Set(requiredGeneratedVideoFiles.map((file2) => file2.fileId));
35689
- const failedRequiredVideo = fileMaterializationFailures.find(
35690
- (failure) => requiredVideoFileIds.has(failure.fileId)
35691
36045
  );
35692
- if (failedRequiredVideo) {
35693
- observability.warn("Generated video sandbox materialization deferred", {
35694
- errorClass: "SandboxFileDownloadFailure",
35695
- errorCode: "generated_video_materialization_deferred",
35696
- origin: "worker"
35697
- });
35698
- }
35699
- const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
35700
- fileMaterializationFailures
35701
- );
35702
- return await runTurnStreamAttempt({
35703
- input,
35704
- settings: capabilitySettings,
35705
- db,
35706
- bus,
35707
- runtime,
35708
- objectStorage,
35709
- observability,
35710
- entitlements,
35711
- startVideoGenerationWorkflow,
35712
- cancellationSignal,
35713
- runtimeCancellationSignal,
35714
- activityContext,
35715
- dispatchId,
35716
- control,
35717
- attempt,
35718
- billingState,
35719
- sandboxState,
35720
- eventing: wiredEventing,
35721
- providerTurn,
35722
- leases,
35723
- historySink,
35724
- media,
35725
- toolResultSpill,
35726
- claimedResult,
35727
- flushRuntimeBatcher,
35728
- finalizeTurnOpStreamOps,
35729
- runWorkspaceMutationForSandbox,
35730
- throwIfWorkerShuttingDown,
35731
- throwIfTurnCancelled,
35732
- setLastInputTokensFenced,
35733
- attachGitCredentialRenewal,
35734
- attachCodemodeTokenRenewal,
35735
- attachRunCredentialRenewal,
35736
- withProviderRequestContext,
35737
- withSessionTitleProviderRequestContext,
35738
- publishCompactionLiveEvents,
35739
- publishCompactionOutcomeEvents,
35740
- recordCompanyBrainContributionReceiptOnce,
35741
- modelCheckpointMemoryCollector,
35742
- claimedModelUsageSourceKeys,
35743
- emittedModelUsageSourceKeys,
35744
- modelUsageDispatchId,
35745
- workerPreparationStartedAt,
35746
- fileDownloadsMaterializedForRun,
35747
- unavailableSandboxFilesNote,
35748
- runCredentialsNote,
35749
- mcpAvailabilityNote,
35750
- fileAuthoritySubjectId,
35751
- activeSandboxBackend,
35752
- groupBoxBackend,
35753
- turnExecutionPolicy,
35754
- turn,
35755
- trigger,
35756
- humanInputResume,
35757
- attachPendingUpdatesAfterOpenSuffix,
35758
- agent,
35759
- resolvedModel,
35760
- providerApi,
35761
- runSettings,
35762
- turnTools,
35763
- generateSessionTitleInParallel,
35764
- sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
35765
- compactSummarizer,
35766
- settleDeferredSteerAfterCompaction,
35767
- compactionModelHistoryProjector,
35768
- generatedImageHistoryProjector,
35769
- modelHistoryProjector,
35770
- compactionModeOptions,
35771
- initialRunCredentialMaterial,
35772
- initialGitCredentials,
35773
- sandboxEnvironment,
35774
- sandboxCodemodeToken,
35775
- sandboxCodemodeTokenExpiresAt,
35776
- fileResourceDownloads,
35777
- runCredentialResolver,
35778
- videoGenerationAcceptancesByCallId
35779
- });
35780
36046
  } catch (error51) {
35781
36047
  eventing.preparedTools?.inputWaitYield?.closeAdmission();
35782
36048
  return await settleTurnFailure({
@@ -35858,6 +36124,7 @@ import {
35858
36124
  markVideoGenerationTerminalUpdate,
35859
36125
  markVideoGenerationTerminalUpdateInTransaction,
35860
36126
  mediaGenerationResultForStoredOperation,
36127
+ withSessionRlsActorContext as withSessionRlsActorContext2,
35861
36128
  rescheduleVideoGenerationOperation,
35862
36129
  settleVideoGenerationFailure,
35863
36130
  settleVideoGenerationReady
@@ -36709,7 +36976,13 @@ async function cleanupTerminalReferences(service, operation) {
36709
36976
  }
36710
36977
  }
36711
36978
  async function terminalPayload(service, operation) {
36712
- return await mediaGenerationResultForStoredOperation(service.db, operation);
36979
+ return withSessionRlsActorContext2(
36980
+ {
36981
+ subjectId: "service:video-generation-reconciliation",
36982
+ privateFileOwnerSubjectId: operation.privateFileOwnerSubjectId ?? null
36983
+ },
36984
+ () => mediaGenerationResultForStoredOperation(service.db, operation)
36985
+ );
36713
36986
  }
36714
36987
  async function requireOperation(service, input) {
36715
36988
  const operation = await getVideoGenerationOperation2(
@@ -36929,4 +37202,4 @@ export {
36929
37202
  createTurnActivities,
36930
37203
  createTurnActivitiesFromServices
36931
37204
  };
36932
- //# sourceMappingURL=activities-turn-MYD63DTN.js.map
37205
+ //# sourceMappingURL=activities-turn-AP67JADS.js.map