@opengeni/worker-bundle 0.16.0 → 0.16.4

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.
@@ -1,8 +1,5 @@
1
- export type DocumentIndexWorkflowInput = {
2
- accountId: string;
3
- workspaceId: string;
4
- documentId: string;
5
- };
1
+ import type { IndexDocumentInput } from "../activities/types";
2
+ export type DocumentIndexWorkflowInput = IndexDocumentInput;
6
3
  export declare function documentIndexWorkflow(input: DocumentIndexWorkflowInput): Promise<{
7
4
  id: string;
8
5
  workspaceId: string;
@@ -22,6 +19,9 @@ export declare function documentIndexWorkflow(input: DocumentIndexWorkflowInput)
22
19
  sourceUpdatedAt: string | null;
23
20
  sourceVersion: string | null;
24
21
  aclTags: string[];
22
+ authorityKind: "organization" | "personal" | "workspace";
23
+ authorityWorkspaceId: string | null;
24
+ authoritySubjectId: string | null;
25
25
  visibility: "private" | "workspace";
26
26
  createdBy: string | null;
27
27
  agentAccess: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeni/worker-bundle",
3
- "version": "0.16.0",
3
+ "version": "0.16.4",
4
4
  "description": "OpenGeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -41,16 +41,16 @@
41
41
  "dependencies": {
42
42
  "@opengeni/agent-proto": "^0.3.0",
43
43
  "@opengeni/codex": "^0.2.10",
44
- "@opengeni/config": "^0.10.3",
45
- "@opengeni/contracts": "^0.32.0",
46
- "@opengeni/core": "^0.19.0",
47
- "@opengeni/db": "^0.23.0",
48
- "@opengeni/documents": "^0.2.72",
49
- "@opengeni/events": "^0.3.62",
50
- "@opengeni/github": "^0.4.19",
51
- "@opengeni/observability": "^0.4.6",
52
- "@opengeni/runtime": "^0.18.0",
53
- "@opengeni/storage": "^0.2.56",
44
+ "@opengeni/config": "^0.10.7",
45
+ "@opengeni/contracts": "^0.36.0",
46
+ "@opengeni/core": "^0.20.3",
47
+ "@opengeni/db": "^0.27.0",
48
+ "@opengeni/documents": "^0.3.3",
49
+ "@opengeni/events": "^0.3.66",
50
+ "@opengeni/github": "^0.4.23",
51
+ "@opengeni/observability": "^0.4.10",
52
+ "@opengeni/runtime": "^0.18.4",
53
+ "@opengeni/storage": "^0.2.60",
54
54
  "@temporalio/activity": "^1.17.0",
55
55
  "@temporalio/client": "^1.17.0",
56
56
  "@temporalio/worker": "^1.17.0",
@@ -94,6 +94,7 @@ import {
94
94
  normalizeModelCallUsage,
95
95
  normalizeSdkEvent,
96
96
  sanitizeHistoryItemsForModel,
97
+ projectModelInputForCapabilities,
97
98
  appendPersistentSessionSettings,
98
99
  appendSessionInstructions,
99
100
  appendWorkspaceGovernance,
@@ -271,7 +272,13 @@ import {
271
272
  redactSensitiveText,
272
273
  type SecretForRedaction,
273
274
  } from "./redaction";
274
- import { applyCodexHistoryStrip, turnInput, type TurnCodexAccount } from "./run-input";
275
+ import {
276
+ applyCodexHistoryStrip,
277
+ createModelHistoryAttachmentProjector,
278
+ turnInput,
279
+ type ModelAttachmentInputPolicy,
280
+ type TurnCodexAccount,
281
+ } from "./run-input";
275
282
  import {
276
283
  createRuntimeBatcher,
277
284
  currentActivityContext,
@@ -338,6 +345,7 @@ import {
338
345
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
339
346
  evaluateWorkspaceModelPolicy,
340
347
  readTurnExecutionPolicyV1,
348
+ resourceMountPath,
341
349
  type LatencyMode,
342
350
  type ResourceRef,
343
351
  type SessionEvent,
@@ -1870,11 +1878,15 @@ export function shouldStartOnTurnRecording(params: {
1870
1878
  export function computerToolModeForTurn(
1871
1879
  resolvedModel: {
1872
1880
  provider: { kind: RegistryProviderKind; api: ModelProviderApi };
1881
+ configured: { capabilities: { inputModalities: string[] } };
1873
1882
  } | null,
1874
1883
  ): ComputerToolMode {
1875
1884
  if (!resolvedModel) {
1876
1885
  return "hosted"; // legacy built-in Responses client — real hosted support
1877
1886
  }
1887
+ if (!modelSupportsImageInputForTurn(resolvedModel)) {
1888
+ return "disabled";
1889
+ }
1878
1890
  if (resolvedModel.provider.kind === "codex-subscription") {
1879
1891
  return "function-image";
1880
1892
  }
@@ -1914,17 +1926,43 @@ export function hostedWebSearchForTurn(
1914
1926
  return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
1915
1927
  }
1916
1928
 
1917
- /**
1918
- * Decide whether this turn's provider accepts typed file/image content in a
1919
- * user message. The legacy built-in client and registry Responses clients use
1920
- * the Agents SDK Responses converter, which supports `input_image` and
1921
- * `input_file`. Chat-completions providers retain the sandbox-path projection
1922
- * until that transport declares and proves an equivalent capability.
1923
- */
1924
- export function modelAcceptsTypedAttachmentContentForTurn(
1925
- resolvedModel: { provider: { api: ModelProviderApi } } | null,
1929
+ /** Exact model-catalog modality gate; null preserves the legacy built-in path. */
1930
+ export function modelSupportsImageInputForTurn(
1931
+ resolvedModel: {
1932
+ configured: { capabilities?: { inputModalities: string[] } };
1933
+ } | null,
1926
1934
  ): boolean {
1927
- return resolvedModel === null || resolvedModel.provider.api === "responses";
1935
+ return (
1936
+ resolvedModel === null ||
1937
+ resolvedModel.configured.capabilities?.inputModalities.includes("image") !== false
1938
+ );
1939
+ }
1940
+
1941
+ const LEGACY_INPUT_FILE_MEDIA_TYPES = [
1942
+ "application/json",
1943
+ "application/pdf",
1944
+ "application/x-yaml",
1945
+ "application/yaml",
1946
+ "text/*",
1947
+ ] as const;
1948
+
1949
+ /** Image and file input are independent catalogue capabilities. */
1950
+ export function modelAttachmentInputPolicyForTurn(
1951
+ resolvedModel: {
1952
+ provider: { api: ModelProviderApi };
1953
+ configured: {
1954
+ capabilities?: { inputModalities: string[]; inputFileMediaTypes?: string[] };
1955
+ };
1956
+ } | null,
1957
+ ): ModelAttachmentInputPolicy {
1958
+ const typedTransport = resolvedModel === null || resolvedModel.provider.api === "responses";
1959
+ return {
1960
+ supportsImageInput: typedTransport && modelSupportsImageInputForTurn(resolvedModel),
1961
+ inputFileMediaTypes: typedTransport
1962
+ ? (resolvedModel?.configured.capabilities?.inputFileMediaTypes ??
1963
+ LEGACY_INPUT_FILE_MEDIA_TYPES)
1964
+ : [],
1965
+ };
1928
1966
  }
1929
1967
 
1930
1968
  export type TurnSandboxProvisioner<T> = {
@@ -4145,6 +4183,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4145
4183
  capabilitySettings,
4146
4184
  turnExecutionPolicy.productModelId,
4147
4185
  );
4186
+ const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
4187
+ const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
4188
+ const attachmentProjector = createModelHistoryAttachmentProjector(
4189
+ db,
4190
+ input.workspaceId,
4191
+ modelInputPolicy,
4192
+ objectStorage ? (file) => objectStorage.getFileBytes(file) : undefined,
4193
+ );
4194
+ const modelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4195
+ projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
4148
4196
  const selectedProvider = resolvedModel?.provider;
4149
4197
  const publicProviderHeaders = new Set(
4150
4198
  selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? [],
@@ -4507,6 +4555,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4507
4555
  codexAccount: {
4508
4556
  currentCodexCredentialId: effectiveCodexCredentialId,
4509
4557
  },
4558
+ projectModelInput: modelHistoryProjector,
4510
4559
  ...compactionModeOptions,
4511
4560
  },
4512
4561
  ),
@@ -5470,6 +5519,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5470
5519
  objectStorage,
5471
5520
  input.workspaceId,
5472
5521
  turnResources,
5522
+ activeSandboxBackend ?? groupBoxBackend,
5473
5523
  ),
5474
5524
  cancellationSignal,
5475
5525
  undefined,
@@ -5688,6 +5738,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5688
5738
  // session's MCP allow-list; the effective context window drives the
5689
5739
  // compaction threshold.
5690
5740
  hostedWebSearch,
5741
+ supportsImageInput,
5742
+ inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
5691
5743
  ...(resolvedModel
5692
5744
  ? {
5693
5745
  encryptedReasoning:
@@ -6034,6 +6086,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6034
6086
  codexAccount: {
6035
6087
  currentCodexCredentialId: effectiveCodexCredentialId,
6036
6088
  },
6089
+ projectModelInput: modelHistoryProjector,
6037
6090
  ...compactionModeOptions,
6038
6091
  },
6039
6092
  ),
@@ -6168,6 +6221,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6168
6221
  codexAccount: {
6169
6222
  currentCodexCredentialId: effectiveCodexCredentialId,
6170
6223
  },
6224
+ projectModelInput: modelHistoryProjector,
6171
6225
  ...compactionModeOptions,
6172
6226
  }
6173
6227
  : {
@@ -6175,6 +6229,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6175
6229
  codexAccount: {
6176
6230
  currentCodexCredentialId: effectiveCodexCredentialId,
6177
6231
  },
6232
+ projectModelInput: modelHistoryProjector,
6178
6233
  ...compactionModeOptions,
6179
6234
  },
6180
6235
  ),
@@ -6246,20 +6301,22 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6246
6301
  }
6247
6302
  let fileMaterializationFailures: SandboxFileDownloadFailure[] = [];
6248
6303
  let fileDownloadsMaterializedForRun = false;
6249
- if (
6250
- resolvedSandbox &&
6251
- setupBoxSession &&
6252
- activeSandboxBackend !== "selfhosted" &&
6253
- fileResourceDownloads.length > 0
6254
- ) {
6304
+ if (resolvedSandbox && setupBoxSession && fileResourceDownloads.length > 0) {
6255
6305
  const boxInstanceId = resolvedSandbox.established.instanceId;
6256
- const alreadyMaterialized = await getMaterializedSandboxFileResources(db, {
6257
- accountId: input.accountId,
6258
- workspaceId: input.workspaceId,
6259
- sandboxGroupId: session.sandboxGroupId,
6260
- expectedEpoch: resolvedSandbox.leaseEpoch,
6261
- instanceId: boxInstanceId,
6262
- });
6306
+ // Managed boxes are immutable platform state, so their durable lease can
6307
+ // memoize successful downloads. A connected machine is user-owned: files
6308
+ // can be changed or removed between turns, so verify/materialize every
6309
+ // attached file each turn instead of trusting the managed-box cache.
6310
+ const cacheMaterialization = resolvedSandbox.established.backendId !== "selfhosted";
6311
+ const alreadyMaterialized = cacheMaterialization
6312
+ ? await getMaterializedSandboxFileResources(db, {
6313
+ accountId: input.accountId,
6314
+ workspaceId: input.workspaceId,
6315
+ sandboxGroupId: session.sandboxGroupId,
6316
+ expectedEpoch: resolvedSandbox.leaseEpoch,
6317
+ instanceId: boxInstanceId,
6318
+ })
6319
+ : new Set<string>();
6263
6320
  const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
6264
6321
  fileResourceDownloads,
6265
6322
  alreadyMaterialized,
@@ -6293,7 +6350,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6293
6350
  const succeededFileIds = downloadsToMaterialize
6294
6351
  .map((download) => download.fileId)
6295
6352
  .filter((fileId) => !failedFileIds.has(fileId));
6296
- if (succeededFileIds.length > 0) {
6353
+ if (cacheMaterialization && succeededFileIds.length > 0) {
6297
6354
  await markSandboxFileResourcesMaterialized(db, {
6298
6355
  accountId: input.accountId,
6299
6356
  workspaceId: input.workspaceId,
@@ -6334,11 +6391,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6334
6391
  recovering: turn.executionGeneration > 1,
6335
6392
  ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
6336
6393
  ...(runCredentialsNote ? { runCredentialsNote } : {}),
6337
- ...(modelAcceptsTypedAttachmentContentForTurn(resolvedModel) && objectStorage
6338
- ? {
6339
- readFileBytesForModel: (file) => objectStorage.getFileBytes(file),
6340
- }
6341
- : {}),
6394
+ projectModelHistory: modelHistoryProjector,
6342
6395
  },
6343
6396
  );
6344
6397
  runInput = prepared.input;
@@ -6412,6 +6465,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6412
6465
  codexAccount: {
6413
6466
  currentCodexCredentialId: effectiveCodexCredentialId,
6414
6467
  },
6468
+ projectModelInput: modelHistoryProjector,
6415
6469
  ...compactionModeOptions,
6416
6470
  },
6417
6471
  ),
@@ -9482,8 +9536,9 @@ async function sandboxFileDownloadsForRun(
9482
9536
  objectStorage: ObjectStorage | null,
9483
9537
  workspaceId: string,
9484
9538
  resources: ResourceRef[],
9539
+ activeSandboxBackend: Settings["sandboxBackend"] = settings.sandboxBackend,
9485
9540
  ): Promise<SandboxFileDownload[]> {
9486
- if (settings.sandboxBackend === "none" || !requiresSignedFileResourceDownloads(settings)) {
9541
+ if (!requiresSignedFileResourceDownloads(settings, activeSandboxBackend)) {
9487
9542
  return [];
9488
9543
  }
9489
9544
  const fileResources = resources.filter(
@@ -9504,17 +9559,24 @@ async function sandboxFileDownloadsForRun(
9504
9559
  const url = await downloadStorage.createGetUrl({ key: file.objectKey });
9505
9560
  downloads.push({
9506
9561
  fileId: file.id,
9507
- mountPath: resource.mountPath ?? `files/${file.id}`,
9562
+ mountPath: resourceMountPath(resource),
9508
9563
  filename: file.safeFilename,
9509
9564
  url: url.url,
9510
9565
  expiresAt: url.expiresAt,
9511
9566
  sizeBytes: file.sizeBytes,
9567
+ ...(file.sha256 ? { sha256: file.sha256 } : {}),
9512
9568
  });
9513
9569
  }
9514
9570
  return downloads;
9515
9571
  }
9516
9572
 
9517
- function requiresSignedFileResourceDownloads(settings: Settings): boolean {
9573
+ export function requiresSignedFileResourceDownloads(
9574
+ settings: Settings,
9575
+ activeSandboxBackend: Settings["sandboxBackend"] = settings.sandboxBackend,
9576
+ ): boolean {
9577
+ if (activeSandboxBackend === "none") {
9578
+ return false;
9579
+ }
9518
9580
  // A selfhosted machine (bring-your-own-compute) can NEVER mount ANY object store
9519
9581
  // — it is a remote user machine reached only over NATS, so file resources are
9520
9582
  // ALWAYS delivered by exec-curling a pre-signed URL onto it. Without this a
@@ -9522,15 +9584,15 @@ function requiresSignedFileResourceDownloads(settings: Settings): boolean {
9522
9584
  // resources on an azure-blob / s3-compatible store (nativeBucketMount=false),
9523
9585
  // a regression from the pre-honest-label path where the same turn ran home=modal
9524
9586
  // and modal's descriptor forced signed downloads.
9525
- if (settings.sandboxBackend === "selfhosted") {
9587
+ if (activeSandboxBackend === "selfhosted") {
9526
9588
  return true;
9527
9589
  }
9528
9590
  // A nativeBucketMount backend (modal) cannot mount Azure Blob entries, so it
9529
9591
  // needs pre-signed downloads for that store. Keying on the descriptor (not the
9530
9592
  // "modal" literal) keeps this correct as bucket-mount backends are added.
9531
- const nativeBucketMount = CAPABILITY_DESCRIPTORS[settings.sandboxBackend].nativeBucketMount;
9593
+ const nativeBucketMount = CAPABILITY_DESCRIPTORS[activeSandboxBackend].nativeBucketMount;
9532
9594
  return (
9533
- (settings.sandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible") ||
9595
+ (activeSandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible") ||
9534
9596
  settings.objectStorageBackend === "aws-s3" ||
9535
9597
  settings.objectStorageBackend === "gcs" ||
9536
9598
  (nativeBucketMount && settings.objectStorageBackend === "azure-blob")
@@ -100,6 +100,8 @@ export async function maybeCompactContext(
100
100
  * append again — the event is already durable.
101
101
  */
102
102
  publishLiveEvents?: (events: SessionEvent[]) => Promise<void>;
103
+ /** Turn-scoped attachment/modality view; canonical persisted rows stay untouched. */
104
+ projectModelInput?: (items: CompactionItem[]) => Promise<CompactionItem[]>;
103
105
  } = {},
104
106
  ): Promise<MaybeCompactResult> {
105
107
  if (options.codexCompactionMode === "remote_v2" && options.isCodexSubscriptionTurn !== true) {
@@ -142,9 +144,16 @@ export async function maybeCompactContext(
142
144
  };
143
145
  }
144
146
 
145
- const items = sanitizeHistoryItemsForModel(
146
- applyCodexHistoryStrip(active, options.codexAccount ?? { currentCodexCredentialId: null }),
147
+ const canonicalItems = applyCodexHistoryStrip(
148
+ active,
149
+ options.codexAccount ?? { currentCodexCredentialId: null },
147
150
  ) as CompactionItem[];
151
+ const projectForWire = async (input: CompactionItem[]): Promise<CompactionItem[]> =>
152
+ sanitizeHistoryItemsForModel(
153
+ options.projectModelInput ? await options.projectModelInput(input) : input,
154
+ settings.modelToolOutputTruncationTokens,
155
+ ) as CompactionItem[];
156
+ const items = await projectForWire(canonicalItems);
148
157
  const decision = decideCompaction({
149
158
  items,
150
159
  lastInputTokens,
@@ -181,7 +190,16 @@ export async function maybeCompactContext(
181
190
  await options.publishLiveEvents?.(started.events);
182
191
 
183
192
  if (useRemoteV2) {
184
- const outcome = await compactContextRemoteV2(db, settings, scope, items, decision, options);
193
+ const outcome = await compactContextRemoteV2(
194
+ db,
195
+ settings,
196
+ scope,
197
+ canonicalItems,
198
+ items,
199
+ decision,
200
+ options,
201
+ projectForWire,
202
+ );
185
203
  return prependCompactionEvents(started.events, outcome);
186
204
  }
187
205
 
@@ -189,10 +207,12 @@ export async function maybeCompactContext(
189
207
  db,
190
208
  settings,
191
209
  scope,
210
+ canonicalItems,
192
211
  items,
193
212
  decision,
194
213
  summarize,
195
214
  options,
215
+ projectForWire,
196
216
  );
197
217
  return prependCompactionEvents(started.events, outcome);
198
218
  }
@@ -285,6 +305,7 @@ async function compactContextRemoteV2(
285
305
  executionGeneration: number;
286
306
  attemptId: string;
287
307
  },
308
+ canonicalItems: CompactionItem[],
288
309
  items: CompactionItem[],
289
310
  decision: { signalTokens: number; thresholdTokens: number },
290
311
  options: {
@@ -293,6 +314,7 @@ async function compactContextRemoteV2(
293
314
  requestRemoteCompactionV2?: RemoteCompactionV2Requester;
294
315
  producerCodexCredentialId?: string | null;
295
316
  },
317
+ projectForWire: (items: CompactionItem[]) => Promise<CompactionItem[]>,
296
318
  ): Promise<MaybeCompactResult> {
297
319
  if (!options.requestRemoteCompactionV2) {
298
320
  throw new EmptyCompactionSummaryError({
@@ -305,8 +327,8 @@ async function compactContextRemoteV2(
305
327
  // No local "must shrink / must differ" gate — that is portable-only.
306
328
  // Fail closed on provider/extract failure — no portable fallback.
307
329
  const compactionItem = await options.requestRemoteCompactionV2(settings, items);
308
- const replacementHistory = buildRemoteV2ReplacementHistory(items, compactionItem);
309
- const estimatedTokensAfter = estimateTokens(replacementHistory);
330
+ const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
331
+ const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
310
332
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
311
333
  const tailItem = replacementHistory.at(-1);
312
334
  if (!tailItem) {
@@ -365,6 +387,7 @@ async function compactContextPortable(
365
387
  executionGeneration: number;
366
388
  attemptId: string;
367
389
  },
390
+ canonicalItems: CompactionItem[],
368
391
  items: CompactionItem[],
369
392
  decision: { signalTokens: number; thresholdTokens: number },
370
393
  summarize: CompactionSummarizer,
@@ -372,14 +395,15 @@ async function compactContextPortable(
372
395
  clearRequestedCompaction?: boolean;
373
396
  trigger?: "auto" | "operator" | "proactive" | "overflow";
374
397
  },
398
+ projectForWire: (items: CompactionItem[]) => Promise<CompactionItem[]>,
375
399
  ): Promise<MaybeCompactResult> {
376
400
  const estimatedTokensBefore = estimateTokens(items);
377
401
  const summarized = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
378
402
  const summaryBody = summarized.summaryBody;
379
- const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
380
- const estimatedTokensAfter = estimateTokens(replacementHistory);
403
+ const replacementHistory = buildCompactionReplacementHistory(canonicalItems, summaryBody);
404
+ const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
381
405
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
382
- const previousReplacementFingerprint = latestCompactionReplacementFingerprint(items);
406
+ const previousReplacementFingerprint = latestCompactionReplacementFingerprint(canonicalItems);
383
407
  const summaryItem = replacementHistory.at(-1);
384
408
  if (!summaryItem) {
385
409
  // Started already fanout; settle visibly so the landmark cannot stick on
@@ -1,8 +1,10 @@
1
- import { indexDocumentNow } from "@opengeni/documents";
1
+ import { getDocument, indexDocumentNow } from "@opengeni/documents";
2
2
  import { configuredStaticUsageLimits } from "@opengeni/config";
3
3
  import {
4
4
  getBillingBalance,
5
5
  recordUsageEvent,
6
+ resolveDocumentIndexAuthority,
7
+ rlsContextForWorkspace,
6
8
  sumUsageQuantity,
7
9
  withWorkspaceUsageLock,
8
10
  } from "@opengeni/db";
@@ -15,7 +17,47 @@ export function createDocumentActivities(services: () => Promise<ActivityService
15
17
  if (!objectStorage) {
16
18
  throw new Error("object storage is not configured");
17
19
  }
20
+ const context = await rlsContextForWorkspace(db, input.workspaceId);
21
+ if (context.accountId !== input.accountId) {
22
+ throw new Error("document account/workspace authority mismatch");
23
+ }
18
24
  return await withWorkspaceUsageLock(db, input.workspaceId, async (lockedDb) => {
25
+ const storedAuthority = await resolveDocumentIndexAuthority(lockedDb, input);
26
+ if (!storedAuthority) {
27
+ throw new Error("document authority was not found before indexing");
28
+ }
29
+ const suppliedAuthorityFields = [
30
+ "authorityKind",
31
+ "authorityWorkspaceId",
32
+ "authoritySubjectId",
33
+ ] as const;
34
+ const suppliedCount = suppliedAuthorityFields.filter((field) =>
35
+ Object.prototype.hasOwnProperty.call(input, field),
36
+ ).length;
37
+ const suppliedMismatch =
38
+ (Object.prototype.hasOwnProperty.call(input, "authorityKind") &&
39
+ input.authorityKind !== storedAuthority.authorityKind) ||
40
+ (Object.prototype.hasOwnProperty.call(input, "authorityWorkspaceId") &&
41
+ input.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId) ||
42
+ (Object.prototype.hasOwnProperty.call(input, "authoritySubjectId") &&
43
+ input.authoritySubjectId !== storedAuthority.authoritySubjectId);
44
+ if (suppliedMismatch) {
45
+ throw new Error("document authority changed before indexing");
46
+ }
47
+ if (suppliedCount !== 0 && suppliedCount !== suppliedAuthorityFields.length) {
48
+ throw new Error("document authority tuple is partial");
49
+ }
50
+ const claimedDocument = await getDocument(lockedDb, input.workspaceId, input.documentId, {
51
+ viewerSubjectId: storedAuthority.authoritySubjectId,
52
+ });
53
+ if (
54
+ !claimedDocument ||
55
+ claimedDocument.authorityKind !== storedAuthority.authorityKind ||
56
+ claimedDocument.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId ||
57
+ claimedDocument.authoritySubjectId !== storedAuthority.authoritySubjectId
58
+ ) {
59
+ throw new Error("document authority changed before indexing");
60
+ }
19
61
  const document = await indexDocumentNow(
20
62
  lockedDb,
21
63
  objectStorage,
@@ -48,7 +90,15 @@ export function createDocumentActivities(services: () => Promise<ActivityService
48
90
  }
49
91
  },
50
92
  },
93
+ { viewerSubjectId: storedAuthority.authoritySubjectId },
51
94
  );
95
+ if (
96
+ document.authorityKind !== storedAuthority.authorityKind ||
97
+ document.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId ||
98
+ document.authoritySubjectId !== storedAuthority.authoritySubjectId
99
+ ) {
100
+ throw new Error("document authority changed before indexing");
101
+ }
52
102
  if (document.status === "ready") {
53
103
  await recordUsageEvent(lockedDb, {
54
104
  accountId: input.accountId,