@adcp/sdk 17.0.0-beta.29 → 17.0.0-beta.30

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 (34) hide show
  1. package/dist/lib/reporting/ledger/handler.d.mts +4 -4
  2. package/dist/lib/reporting/ledger/handler.d.ts +4 -4
  3. package/dist/lib/reporting/ledger/handler.js +22 -5
  4. package/dist/lib/reporting/ledger/handler.mjs +22 -5
  5. package/dist/lib/reporting/ledger/producer.js +85 -10
  6. package/dist/lib/reporting/ledger/producer.mjs +85 -10
  7. package/dist/lib/reporting/ledger/types.d.mts +12 -1
  8. package/dist/lib/reporting/ledger/types.d.ts +12 -1
  9. package/dist/lib/reporting/service/index.d.mts +2 -1
  10. package/dist/lib/reporting/service/index.d.ts +2 -1
  11. package/dist/lib/reporting/service/index.js +37 -5
  12. package/dist/lib/reporting/service/index.mjs +37 -5
  13. package/dist/lib/reporting/source/fixtures.d.mts +4 -0
  14. package/dist/lib/reporting/source/fixtures.d.ts +4 -0
  15. package/dist/lib/reporting/source/inline.d.mts +5 -0
  16. package/dist/lib/reporting/source/inline.d.ts +5 -0
  17. package/dist/lib/reporting/source/inline.js +93 -13
  18. package/dist/lib/reporting/source/inline.mjs +93 -13
  19. package/dist/lib/reporting/source/schemas/reporting-source-slice-request-v1.json +24 -0
  20. package/dist/lib/reporting/source/source.d.mts +8 -0
  21. package/dist/lib/reporting/source/source.d.ts +8 -0
  22. package/dist/lib/reporting/source/source.js +18 -0
  23. package/dist/lib/reporting/source/source.mjs +18 -0
  24. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  25. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  26. package/dist/lib/version.d.mts +3 -3
  27. package/dist/lib/version.d.ts +3 -3
  28. package/dist/lib/version.js +3 -3
  29. package/dist/lib/version.mjs +3 -3
  30. package/docs/TYPE-SUMMARY.md +5 -3
  31. package/docs/guides/REPORTING-SOURCE-EXECUTOR.md +9 -1
  32. package/docs/llms.txt +1 -1
  33. package/docs/migration-14.x-rc-worksheet.md +4 -4
  34. package/package.json +1 -1
@@ -57,6 +57,7 @@ export interface ReliableReportingAdapterV1 {
57
57
  */
58
58
  export interface ReliableReportingCoverageV1 {
59
59
  constituents: ReportingLedgerConfigurationV1['constituents'];
60
+ packageBindings?: ReportingLedgerConfigurationV1['packageBindings'];
60
61
  }
61
62
  export interface ReliableReportingSourceRouteV1 {
62
63
  /** Key in `adapters`; selected by trusted host code, never by buyer input. */
@@ -66,7 +67,7 @@ export interface ReliableReportingSourceRouteV1 {
66
67
  /** Trusted upstream reporting clock for this account/scope. */
67
68
  sourceTimezone: string;
68
69
  }
69
- type LedgerInstallInput = Omit<ReportingLedgerConfigurationV1, 'configurationId' | 'installedAt' | 'semanticFingerprint' | 'calendarRules' | 'historyStart' | 'historyStartOrdinal' | 'subscription' | 'owedSet' | 'account' | 'sourceScope' | 'sourceTimezone' | 'sourceSettings' | 'contract' | 'constituents' | 'mediaBuyIds'>;
70
+ type LedgerInstallInput = Omit<ReportingLedgerConfigurationV1, 'configurationId' | 'installedAt' | 'semanticFingerprint' | 'calendarRules' | 'historyStart' | 'historyStartOrdinal' | 'subscription' | 'owedSet' | 'account' | 'sourceScope' | 'sourceTimezone' | 'sourceSettings' | 'contract' | 'constituents' | 'mediaBuyIds' | 'packageBindings'>;
70
71
  /**
71
72
  * Seller-resolved configuration facts. `currency`, account identity, source
72
73
  * scope, timezone, and source contract are deliberately absent.
@@ -99,6 +99,7 @@ function createReliableReportingService(input) {
99
99
  if (adapterEntries.length === 0) throw new TypeError("Reliable reporting requires at least one adapter");
100
100
  positiveInteger(options.automatedRecoveryWindowSeconds, "automatedRecoveryWindowSeconds");
101
101
  const sources = /* @__PURE__ */ new Map();
102
+ const inlineAdapterIds = /* @__PURE__ */ new Set();
102
103
  const adapterOfferings = /* @__PURE__ */ new Map();
103
104
  const offerings = /* @__PURE__ */ new Map();
104
105
  const deliveryOfferings = [];
@@ -112,6 +113,7 @@ function createReliableReportingService(input) {
112
113
  if (adapter.fetchSlice === void 0 === (adapter.executor === void 0)) {
113
114
  throw new TypeError("Each reporting adapter requires exactly one of fetchSlice or executor");
114
115
  }
116
+ if (adapter.fetchSlice !== void 0) inlineAdapterIds.add(adapterId);
115
117
  const source = adapter.executor ?? (0, import_source.createInlineReportingSourceExecutor)(adapter.fetchSlice, sourceOffering, {
116
118
  ...adapter.inlineReplayRetention ? { replayRetention: adapter.inlineReplayRetention } : {},
117
119
  // Under a simulated clock (the test kit, a sandbox advance_time), acquisitions are stamped at
@@ -378,7 +380,10 @@ function createReliableReportingService(input) {
378
380
  if (ownership === "account") {
379
381
  assertSupportedScheduleSemantics(frozenInput.schedule, deliveryOffering, normalizedTimezone, offering);
380
382
  }
381
- const { constituents, mediaBuyIds } = trustedCoverage(coverage, frozenInput.requestedMetrics);
383
+ const { constituents, mediaBuyIds, packageBindings } = trustedCoverage(coverage, frozenInput.requestedMetrics);
384
+ if (inlineAdapterIds.has(route.adapterId) && packageBindings.length > 0 && !frozenInput.requestedDimensions.includes("package_id")) {
385
+ throw new TypeError("Inline package reporting requires package_id in requestedDimensions");
386
+ }
382
387
  if (configuration.expectedMediaBuyIds !== void 0 && !sameIdMembers(configuration.expectedMediaBuyIds, mediaBuyIds)) {
383
388
  throw new TypeError("Trusted reporting coverage conflicts with the configuration media-buy assertion");
384
389
  }
@@ -422,7 +427,8 @@ function createReliableReportingService(input) {
422
427
  sourceSettings: { ...sourceSettings, currency: normalizedCurrency },
423
428
  contract: structuredClone(offering.contract),
424
429
  constituents,
425
- mediaBuyIds
430
+ mediaBuyIds,
431
+ ...packageBindings.length ? { packageBindings } : {}
426
432
  },
427
433
  requestedMetrics: frozenInput.requestedMetrics
428
434
  };
@@ -485,7 +491,11 @@ function createReliableReportingService(input) {
485
491
  assertWholeSourceLocalDays(owed, generation.input.sourceTimezone);
486
492
  }
487
493
  const owedBuys = new Set(owed.media_buy_ids);
488
- const constituents = generation.pool.filter((value) => value.mediaBuyId && owedBuys.has(value.mediaBuyId));
494
+ const packageBindings = generation.packageBindings.filter((value) => owedBuys.has(value.mediaBuyId));
495
+ const packageConstituentIds = new Set(packageBindings.map((value) => value.constituentId));
496
+ const constituents = generation.pool.filter(
497
+ (value) => value.constituentKind === "package_item" ? packageConstituentIds.has(value.constituentId) : Boolean(value.mediaBuyId && owedBuys.has(value.mediaBuyId))
498
+ );
489
499
  const covered = new Set(constituents.map((value) => value.mediaBuyId));
490
500
  if (owed.media_buy_ids.some((mediaBuyId) => !covered.has(mediaBuyId))) {
491
501
  throw new TypeError("resolveCoverage did not authorize every media buy the subscription owes");
@@ -499,6 +509,7 @@ function createReliableReportingService(input) {
499
509
  period: { start: owed.period_start, end: owed.period_end },
500
510
  mediaBuyIds: [...owed.media_buy_ids],
501
511
  constituents: structuredClone(constituents),
512
+ ...packageBindings.length ? { packageBindings: structuredClone(packageBindings) } : {},
502
513
  ...owed.stub === true ? { stub: true } : {}
503
514
  });
504
515
  }
@@ -583,7 +594,7 @@ function createReliableReportingService(input) {
583
594
  sourceSettings: structuredClone(policy.sourceSettings)
584
595
  };
585
596
  const { input: input2 } = await prepareGeneration(configuration, account, "owed_set");
586
- const { constituents, mediaBuyIds: _mediaBuyIds, ...generation } = input2;
597
+ const { constituents, mediaBuyIds: _mediaBuyIds, packageBindings, ...generation } = input2;
587
598
  void _mediaBuyIds;
588
599
  return {
589
600
  input: {
@@ -597,6 +608,7 @@ function createReliableReportingService(input) {
597
608
  }
598
609
  },
599
610
  pool: constituents,
611
+ packageBindings: packageBindings ?? [],
600
612
  ...managed2 ? { managedOffering: structuredClone(deliveryOffering) } : {},
601
613
  version
602
614
  };
@@ -1066,7 +1078,26 @@ function trustedCoverage(coverage, requestedMetrics) {
1066
1078
  const mediaBuyIds = [
1067
1079
  ...new Set(constituents.map((value) => value.mediaBuyId).filter((value) => Boolean(value)))
1068
1080
  ].sort();
1069
- return { constituents, mediaBuyIds };
1081
+ const rawBindings = coverage.packageBindings ?? [];
1082
+ if (!Array.isArray(rawBindings)) throw new TypeError("resolveCoverage packageBindings must be an array");
1083
+ const packageConstituents = constituents.filter((value) => value.constituentKind === "package_item");
1084
+ if (rawBindings.length !== packageConstituents.length) {
1085
+ throw new TypeError("resolveCoverage must bind every package_item constituent to one media buy");
1086
+ }
1087
+ const packageConstituentIds = new Set(packageConstituents.map((value) => value.constituentId));
1088
+ const packageBindings = rawBindings.map((binding) => {
1089
+ if (!binding || typeof binding.constituentId !== "string" || typeof binding.mediaBuyId !== "string" || !packageConstituentIds.has(binding.constituentId) || !mediaBuyIds.includes(binding.mediaBuyId)) {
1090
+ throw new TypeError("resolveCoverage package binding is outside its authorized constituents");
1091
+ }
1092
+ return { constituentId: binding.constituentId, mediaBuyId: binding.mediaBuyId };
1093
+ });
1094
+ if (new Set(packageBindings.map((value) => value.constituentId)).size !== packageBindings.length) {
1095
+ throw new TypeError("resolveCoverage package bindings must name unique constituents");
1096
+ }
1097
+ packageBindings.sort(
1098
+ (left, right) => left.constituentId < right.constituentId ? -1 : left.constituentId > right.constituentId ? 1 : 0
1099
+ );
1100
+ return { constituents, mediaBuyIds, packageBindings };
1070
1101
  }
1071
1102
  function sameIdMembers(left, right) {
1072
1103
  if (!Array.isArray(left)) throw new TypeError("expectedMediaBuyIds must be an array of media buy IDs");
@@ -1227,6 +1258,7 @@ function assertNoUntrustedLineageFields(configuration) {
1227
1258
  "ctx_metadata",
1228
1259
  "constituents",
1229
1260
  "mediaBuyIds",
1261
+ "packageBindings",
1230
1262
  "applicability",
1231
1263
  "products",
1232
1264
  "productIds",
@@ -96,6 +96,7 @@ function createReliableReportingService(input) {
96
96
  if (adapterEntries.length === 0) throw new TypeError("Reliable reporting requires at least one adapter");
97
97
  positiveInteger(options.automatedRecoveryWindowSeconds, "automatedRecoveryWindowSeconds");
98
98
  const sources = /* @__PURE__ */ new Map();
99
+ const inlineAdapterIds = /* @__PURE__ */ new Set();
99
100
  const adapterOfferings = /* @__PURE__ */ new Map();
100
101
  const offerings = /* @__PURE__ */ new Map();
101
102
  const deliveryOfferings = [];
@@ -109,6 +110,7 @@ function createReliableReportingService(input) {
109
110
  if (adapter.fetchSlice === void 0 === (adapter.executor === void 0)) {
110
111
  throw new TypeError("Each reporting adapter requires exactly one of fetchSlice or executor");
111
112
  }
113
+ if (adapter.fetchSlice !== void 0) inlineAdapterIds.add(adapterId);
112
114
  const source = adapter.executor ?? createInlineReportingSourceExecutor(adapter.fetchSlice, sourceOffering, {
113
115
  ...adapter.inlineReplayRetention ? { replayRetention: adapter.inlineReplayRetention } : {},
114
116
  // Under a simulated clock (the test kit, a sandbox advance_time), acquisitions are stamped at
@@ -375,7 +377,10 @@ function createReliableReportingService(input) {
375
377
  if (ownership === "account") {
376
378
  assertSupportedScheduleSemantics(frozenInput.schedule, deliveryOffering, normalizedTimezone, offering);
377
379
  }
378
- const { constituents, mediaBuyIds } = trustedCoverage(coverage, frozenInput.requestedMetrics);
380
+ const { constituents, mediaBuyIds, packageBindings } = trustedCoverage(coverage, frozenInput.requestedMetrics);
381
+ if (inlineAdapterIds.has(route.adapterId) && packageBindings.length > 0 && !frozenInput.requestedDimensions.includes("package_id")) {
382
+ throw new TypeError("Inline package reporting requires package_id in requestedDimensions");
383
+ }
379
384
  if (configuration.expectedMediaBuyIds !== void 0 && !sameIdMembers(configuration.expectedMediaBuyIds, mediaBuyIds)) {
380
385
  throw new TypeError("Trusted reporting coverage conflicts with the configuration media-buy assertion");
381
386
  }
@@ -419,7 +424,8 @@ function createReliableReportingService(input) {
419
424
  sourceSettings: { ...sourceSettings, currency: normalizedCurrency },
420
425
  contract: structuredClone(offering.contract),
421
426
  constituents,
422
- mediaBuyIds
427
+ mediaBuyIds,
428
+ ...packageBindings.length ? { packageBindings } : {}
423
429
  },
424
430
  requestedMetrics: frozenInput.requestedMetrics
425
431
  };
@@ -482,7 +488,11 @@ function createReliableReportingService(input) {
482
488
  assertWholeSourceLocalDays(owed, generation.input.sourceTimezone);
483
489
  }
484
490
  const owedBuys = new Set(owed.media_buy_ids);
485
- const constituents = generation.pool.filter((value) => value.mediaBuyId && owedBuys.has(value.mediaBuyId));
491
+ const packageBindings = generation.packageBindings.filter((value) => owedBuys.has(value.mediaBuyId));
492
+ const packageConstituentIds = new Set(packageBindings.map((value) => value.constituentId));
493
+ const constituents = generation.pool.filter(
494
+ (value) => value.constituentKind === "package_item" ? packageConstituentIds.has(value.constituentId) : Boolean(value.mediaBuyId && owedBuys.has(value.mediaBuyId))
495
+ );
486
496
  const covered = new Set(constituents.map((value) => value.mediaBuyId));
487
497
  if (owed.media_buy_ids.some((mediaBuyId) => !covered.has(mediaBuyId))) {
488
498
  throw new TypeError("resolveCoverage did not authorize every media buy the subscription owes");
@@ -496,6 +506,7 @@ function createReliableReportingService(input) {
496
506
  period: { start: owed.period_start, end: owed.period_end },
497
507
  mediaBuyIds: [...owed.media_buy_ids],
498
508
  constituents: structuredClone(constituents),
509
+ ...packageBindings.length ? { packageBindings: structuredClone(packageBindings) } : {},
499
510
  ...owed.stub === true ? { stub: true } : {}
500
511
  });
501
512
  }
@@ -580,7 +591,7 @@ function createReliableReportingService(input) {
580
591
  sourceSettings: structuredClone(policy.sourceSettings)
581
592
  };
582
593
  const { input: input2 } = await prepareGeneration(configuration, account, "owed_set");
583
- const { constituents, mediaBuyIds: _mediaBuyIds, ...generation } = input2;
594
+ const { constituents, mediaBuyIds: _mediaBuyIds, packageBindings, ...generation } = input2;
584
595
  void _mediaBuyIds;
585
596
  return {
586
597
  input: {
@@ -594,6 +605,7 @@ function createReliableReportingService(input) {
594
605
  }
595
606
  },
596
607
  pool: constituents,
608
+ packageBindings: packageBindings ?? [],
597
609
  ...managed2 ? { managedOffering: structuredClone(deliveryOffering) } : {},
598
610
  version
599
611
  };
@@ -1063,7 +1075,26 @@ function trustedCoverage(coverage, requestedMetrics) {
1063
1075
  const mediaBuyIds = [
1064
1076
  ...new Set(constituents.map((value) => value.mediaBuyId).filter((value) => Boolean(value)))
1065
1077
  ].sort();
1066
- return { constituents, mediaBuyIds };
1078
+ const rawBindings = coverage.packageBindings ?? [];
1079
+ if (!Array.isArray(rawBindings)) throw new TypeError("resolveCoverage packageBindings must be an array");
1080
+ const packageConstituents = constituents.filter((value) => value.constituentKind === "package_item");
1081
+ if (rawBindings.length !== packageConstituents.length) {
1082
+ throw new TypeError("resolveCoverage must bind every package_item constituent to one media buy");
1083
+ }
1084
+ const packageConstituentIds = new Set(packageConstituents.map((value) => value.constituentId));
1085
+ const packageBindings = rawBindings.map((binding) => {
1086
+ if (!binding || typeof binding.constituentId !== "string" || typeof binding.mediaBuyId !== "string" || !packageConstituentIds.has(binding.constituentId) || !mediaBuyIds.includes(binding.mediaBuyId)) {
1087
+ throw new TypeError("resolveCoverage package binding is outside its authorized constituents");
1088
+ }
1089
+ return { constituentId: binding.constituentId, mediaBuyId: binding.mediaBuyId };
1090
+ });
1091
+ if (new Set(packageBindings.map((value) => value.constituentId)).size !== packageBindings.length) {
1092
+ throw new TypeError("resolveCoverage package bindings must name unique constituents");
1093
+ }
1094
+ packageBindings.sort(
1095
+ (left, right) => left.constituentId < right.constituentId ? -1 : left.constituentId > right.constituentId ? 1 : 0
1096
+ );
1097
+ return { constituents, mediaBuyIds, packageBindings };
1067
1098
  }
1068
1099
  function sameIdMembers(left, right) {
1069
1100
  if (!Array.isArray(left)) throw new TypeError("expectedMediaBuyIds must be an array of media buy IDs");
@@ -1224,6 +1255,7 @@ function assertNoUntrustedLineageFields(configuration) {
1224
1255
  "ctx_metadata",
1225
1256
  "constituents",
1226
1257
  "mediaBuyIds",
1258
+ "packageBindings",
1227
1259
  "applicability",
1228
1260
  "products",
1229
1261
  "productIds",
@@ -357,6 +357,10 @@ export declare function redactedReportingSourceResultV1(level?: 'basic' | 'evide
357
357
  mediaBuyIds: string[];
358
358
  packageIds: string[];
359
359
  denominatorFingerprint: string;
360
+ packageBindings?: {
361
+ constituentId: string;
362
+ mediaBuyId: string;
363
+ }[] | undefined;
360
364
  };
361
365
  requestedMetrics: string[];
362
366
  requestedDimensions: string[];
@@ -357,6 +357,10 @@ export declare function redactedReportingSourceResultV1(level?: 'basic' | 'evide
357
357
  mediaBuyIds: string[];
358
358
  packageIds: string[];
359
359
  denominatorFingerprint: string;
360
+ packageBindings?: {
361
+ constituentId: string;
362
+ mediaBuyId: string;
363
+ }[] | undefined;
360
364
  };
361
365
  requestedMetrics: string[];
362
366
  requestedDimensions: string[];
@@ -8,6 +8,11 @@ export interface InlineReportingDeliveryRequestV1 {
8
8
  constituents: Array<Readonly<{
9
9
  constituent_id: string;
10
10
  media_buy_id: string;
11
+ package_id?: never;
12
+ }> | Readonly<{
13
+ constituent_id: string;
14
+ media_buy_id: string;
15
+ package_id: string;
11
16
  }>>;
12
17
  start_date: string;
13
18
  end_date: string;
@@ -8,6 +8,11 @@ export interface InlineReportingDeliveryRequestV1 {
8
8
  constituents: Array<Readonly<{
9
9
  constituent_id: string;
10
10
  media_buy_id: string;
11
+ package_id?: never;
12
+ }> | Readonly<{
13
+ constituent_id: string;
14
+ media_buy_id: string;
15
+ package_id: string;
11
16
  }>>;
12
17
  start_date: string;
13
18
  end_date: string;
@@ -161,7 +161,12 @@ function createInlineExecutorWithCapacities(deliveryFetch, offeringInput, execut
161
161
  }
162
162
  const offering = import_source.ReportingSourceOfferingV1Schema.parse({
163
163
  ...parsedOffering,
164
- applicability: { ...parsedOffering.applicability, constituentKinds: ["media_buy"] },
164
+ applicability: {
165
+ ...parsedOffering.applicability,
166
+ constituentKinds: parsedOffering.applicability.constituentKinds.filter(
167
+ (kind) => kind === "media_buy" || kind === "package_item"
168
+ )
169
+ },
165
170
  formats: [format],
166
171
  sourceExecution: {
167
172
  ...parsedOffering.sourceExecution,
@@ -204,6 +209,13 @@ function createInlineExecutorWithCapacities(deliveryFetch, offeringInput, execut
204
209
  if (request.sourceRequest.groupIds.length > 0) {
205
210
  return failure("UNSUPPORTED_OFFERING", "terminal", "Inline reporting does not support source group reads");
206
211
  }
212
+ if (request.coverage.constituents.some((constituent) => constituent.constituentKind === "package_item") && (request.coverage.packageBindings === void 0 || !request.requestedDimensions.includes("package_id"))) {
213
+ return failure(
214
+ "UNSUPPORTED_OFFERING",
215
+ "terminal",
216
+ "Inline package reporting requires bound package items and package_id"
217
+ );
218
+ }
207
219
  let deliveryDates;
208
220
  try {
209
221
  deliveryDates = inlineDeliveryDates(request);
@@ -326,16 +338,22 @@ function createInlineExecutorWithCapacities(deliveryFetch, offeringInput, execut
326
338
  return executor;
327
339
  }
328
340
  async function executeAndSeal(deliveryFetch, offering, format, request, deliveryDates, deadlineAt, signal, storage, executionNamespace, scopeKey, capacities, reclaimer, observer, clock) {
341
+ const packageMediaBuyByConstituentId = new Map(
342
+ (request.coverage.packageBindings ?? []).map((binding) => [binding.constituentId, binding.mediaBuyId])
343
+ );
329
344
  let fetched;
330
345
  try {
331
346
  fetched = await deliveryFetch(
332
347
  {
333
348
  account: structuredClone(request.account),
334
349
  media_buy_ids: [...request.coverage.mediaBuyIds],
335
- constituents: request.coverage.constituents.map((constituent) => ({
336
- constituent_id: constituent.constituentId,
337
- media_buy_id: constituent.mediaBuyId
338
- })),
350
+ constituents: request.coverage.constituents.map(
351
+ (constituent) => constituent.constituentKind === "package_item" ? {
352
+ constituent_id: constituent.constituentId,
353
+ media_buy_id: packageMediaBuyByConstituentId.get(constituent.constituentId),
354
+ package_id: constituent.packageId
355
+ } : { constituent_id: constituent.constituentId, media_buy_id: constituent.mediaBuyId }
356
+ ),
339
357
  start_date: deliveryDates.start,
340
358
  end_date: deliveryDates.end,
341
359
  source_read_cutoff_at: request.period.sourceReadCutoffAt,
@@ -488,6 +506,13 @@ async function executeAndSeal(deliveryFetch, offering, format, request, delivery
488
506
  return failure("INTEGRITY_FAILED", "terminal", "Inline delivery availability evidence is invalid");
489
507
  }
490
508
  }
509
+ if (request.coverage.packageIds.length > 0 && availabilityEvidence === void 0) {
510
+ return failure(
511
+ "UNSUPPORTED_OFFERING",
512
+ "terminal",
513
+ "Package reporting requires an object response with availability_evidence"
514
+ );
515
+ }
491
516
  const captureLayout = captureLayoutFor(request);
492
517
  const claimBearingRowCount = sourceRowInputs.length + (availabilityEvidenceSlot.kind === "data" ? auxiliaryRowInputs.length : 0);
493
518
  if (workUnitsExceedCap(
@@ -560,13 +585,26 @@ async function executeAndSeal(deliveryFetch, offering, format, request, delivery
560
585
  );
561
586
  const sourceSnapshotsByMediaBuyId = groupSnapshotsByMediaBuyId(sourceSnapshots, admittedMediaBuyIds);
562
587
  const auxiliarySnapshotsByMediaBuyId = groupSnapshotsByMediaBuyId(auxiliarySnapshots, admittedMediaBuyIds);
588
+ const admittedPackagePairs = new Set(
589
+ request.coverage.constituents.filter((constituent) => constituent.constituentKind === "package_item").map((constituent) => packagePairForConstituent(constituent, packageMediaBuyByConstituentId))
590
+ );
591
+ const sourceSnapshotsByPackagePair = groupSnapshotsByPackagePair(sourceSnapshots, admittedPackagePairs);
592
+ const auxiliarySnapshotsByPackagePair = groupSnapshotsByPackagePair(auxiliarySnapshots, admittedPackagePairs);
563
593
  if (sourceSnapshots.length > 0) {
564
594
  if (allSnapshots.some((snapshot) => !admittedMediaBuyIds.has(snapshot?.mediaBuyId ?? ""))) {
565
595
  return failure("INTEGRITY_FAILED", "terminal", "Inline delivery fetch returned an out-of-scope row");
566
596
  }
597
+ if (captureLayout.indexOf.has("package_id") && allSnapshots.some((snapshot) => {
598
+ if (!snapshot) return false;
599
+ const packageId = snapshotFieldValue(snapshot, "package_id");
600
+ return packageId !== void 0 && (typeof packageId !== "string" || snapshot.mediaBuyId === void 0 || !admittedPackagePairs.has(packagePairKey(snapshot.mediaBuyId, packageId)));
601
+ })) {
602
+ return failure("INTEGRITY_FAILED", "terminal", "Inline delivery fetch returned an out-of-scope package row");
603
+ }
567
604
  if (request.coverage.constituents.some((constituent) => {
568
- if (constituent.constituentKind !== "media_buy" || !constituent.mediaBuyId) return true;
569
- const constituentRows = sourceSnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS;
605
+ const constituentRows = constituent.constituentKind === "package_item" ? sourceSnapshotsByPackagePair.get(
606
+ packagePairForConstituent(constituent, packageMediaBuyByConstituentId)
607
+ ) ?? NO_SNAPSHOTS : constituent.mediaBuyId ? sourceSnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS : NO_SNAPSHOTS;
570
608
  return constituentRows.some(
571
609
  (snapshot) => request.requestedDimensions.some((dimension) => !snapshotHasDimension(snapshot, dimension))
572
610
  ) || availabilityEvidence === void 0 && (constituentRows.length === 0 || constituentRows.some(
@@ -584,7 +622,10 @@ async function executeAndSeal(deliveryFetch, offering, format, request, delivery
584
622
  const rowEvidenceFailure = validateSnapshotsAgainstAvailabilityEvidence(
585
623
  sourceSnapshotsByMediaBuyId,
586
624
  auxiliarySnapshotsByMediaBuyId,
625
+ sourceSnapshotsByPackagePair,
626
+ auxiliarySnapshotsByPackagePair,
587
627
  request,
628
+ packageMediaBuyByConstituentId,
588
629
  availabilityEvidence.cells
589
630
  );
590
631
  if (rowEvidenceFailure === "partial") {
@@ -624,7 +665,9 @@ async function executeAndSeal(deliveryFetch, offering, format, request, delivery
624
665
  }
625
666
  const declaredMetrics = new Map(offering.metrics.map((metric) => [metric.name, metric]));
626
667
  const constituentIdsWithRows = new Set(
627
- request.coverage.constituents.filter((constituent) => constituent.mediaBuyId && sourceSnapshotsByMediaBuyId.has(constituent.mediaBuyId)).map((constituent) => constituent.constituentId)
668
+ request.coverage.constituents.filter(
669
+ (constituent) => constituent.constituentKind === "package_item" ? sourceSnapshotsByPackagePair.has(packagePairForConstituent(constituent, packageMediaBuyByConstituentId)) : constituent.mediaBuyId && sourceSnapshotsByMediaBuyId.has(constituent.mediaBuyId)
670
+ ).map((constituent) => constituent.constituentId)
628
671
  );
629
672
  let projectedAvailability;
630
673
  if (availabilityEvidence) {
@@ -1022,10 +1065,14 @@ function validationWorkExceedsCap(sourceSnapshots, auxiliarySnapshots, request,
1022
1065
  const perConstituentCap = Math.floor(cap / unitsPerVisit);
1023
1066
  const sourceCounts = countSnapshotsByMediaBuyId(sourceSnapshots);
1024
1067
  const auxiliaryCounts = includeAuxiliary ? countSnapshotsByMediaBuyId(auxiliarySnapshots) : void 0;
1068
+ const sourcePackageCounts = countSnapshotsByPackagePair(sourceSnapshots);
1069
+ const auxiliaryPackageCounts = includeAuxiliary ? countSnapshotsByPackagePair(auxiliarySnapshots) : void 0;
1070
+ const packageMediaBuyByConstituentId = new Map(
1071
+ (request.coverage.packageBindings ?? []).map((binding) => [binding.constituentId, binding.mediaBuyId])
1072
+ );
1025
1073
  let work = 0;
1026
1074
  for (const constituent of request.coverage.constituents) {
1027
- if (!constituent.mediaBuyId) continue;
1028
- const visits = (sourceCounts.get(constituent.mediaBuyId) ?? 0) + (auxiliaryCounts?.get(constituent.mediaBuyId) ?? 0);
1075
+ const visits = constituent.constituentKind === "package_item" ? (sourcePackageCounts.get(packagePairForConstituent(constituent, packageMediaBuyByConstituentId)) ?? 0) + (auxiliaryPackageCounts?.get(packagePairForConstituent(constituent, packageMediaBuyByConstituentId)) ?? 0) : constituent.mediaBuyId ? (sourceCounts.get(constituent.mediaBuyId) ?? 0) + (auxiliaryCounts?.get(constituent.mediaBuyId) ?? 0) : 0;
1029
1076
  if (visits > perConstituentCap) return true;
1030
1077
  work += visits * unitsPerVisit;
1031
1078
  if (work > cap) return true;
@@ -1040,6 +1087,37 @@ function countSnapshotsByMediaBuyId(snapshots) {
1040
1087
  }
1041
1088
  return counts;
1042
1089
  }
1090
+ function packagePairKey(mediaBuyId, packageId) {
1091
+ return JSON.stringify([mediaBuyId, packageId]);
1092
+ }
1093
+ function packagePairForConstituent(constituent, mediaBuyByConstituentId) {
1094
+ return packagePairKey(mediaBuyByConstituentId.get(constituent.constituentId), constituent.packageId);
1095
+ }
1096
+ function groupSnapshotsByPackagePair(snapshots, admittedPackagePairs) {
1097
+ const grouped = /* @__PURE__ */ new Map();
1098
+ for (const snapshot of snapshots) {
1099
+ if (!snapshot) continue;
1100
+ const packageId = snapshotFieldValue(snapshot, "package_id");
1101
+ if (typeof packageId !== "string" || snapshot.mediaBuyId === void 0) continue;
1102
+ const pair = packagePairKey(snapshot.mediaBuyId, packageId);
1103
+ if (!admittedPackagePairs.has(pair)) continue;
1104
+ const rows = grouped.get(pair);
1105
+ if (rows) rows.push(snapshot);
1106
+ else grouped.set(pair, [snapshot]);
1107
+ }
1108
+ return grouped;
1109
+ }
1110
+ function countSnapshotsByPackagePair(snapshots) {
1111
+ const counts = /* @__PURE__ */ new Map();
1112
+ for (const snapshot of snapshots) {
1113
+ if (!snapshot) continue;
1114
+ const packageId = snapshotFieldValue(snapshot, "package_id");
1115
+ if (typeof packageId !== "string" || snapshot.mediaBuyId === void 0) continue;
1116
+ const pair = packagePairKey(snapshot.mediaBuyId, packageId);
1117
+ counts.set(pair, (counts.get(pair) ?? 0) + 1);
1118
+ }
1119
+ return counts;
1120
+ }
1043
1121
  function groupSnapshotsByMediaBuyId(snapshots, admittedMediaBuyIds) {
1044
1122
  const grouped = /* @__PURE__ */ new Map();
1045
1123
  for (const snapshot of snapshots) {
@@ -1055,14 +1133,16 @@ function groupSnapshotsByMediaBuyId(snapshots, admittedMediaBuyIds) {
1055
1133
  function snapshotHasDimension(snapshot, dimension) {
1056
1134
  return dimension === "media_buy_id" ? snapshot.mediaBuyId !== void 0 : snapshotFieldValue(snapshot, dimension) !== void 0;
1057
1135
  }
1058
- function validateSnapshotsAgainstAvailabilityEvidence(sourceSnapshotsByMediaBuyId, auxiliarySnapshotsByMediaBuyId, request, cells) {
1136
+ function validateSnapshotsAgainstAvailabilityEvidence(sourceSnapshotsByMediaBuyId, auxiliarySnapshotsByMediaBuyId, sourceSnapshotsByPackagePair, auxiliarySnapshotsByPackagePair, request, packageMediaBuyByConstituentId, cells) {
1059
1137
  const cellsByConstituent = /* @__PURE__ */ new Map();
1060
1138
  for (const constituent of request.coverage.constituents) cellsByConstituent.set(constituent.constituentId, []);
1061
1139
  for (const cell of cells) cellsByConstituent.get(cell.constituent_id)?.push(cell);
1062
1140
  let integrity = false;
1063
1141
  for (const constituent of request.coverage.constituents) {
1064
- const sourceRows = constituent.mediaBuyId ? sourceSnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS : NO_SNAPSHOTS;
1065
- const auxiliaryRows = constituent.mediaBuyId ? auxiliarySnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS : NO_SNAPSHOTS;
1142
+ const sourceRows = constituent.constituentKind === "package_item" ? sourceSnapshotsByPackagePair.get(packagePairForConstituent(constituent, packageMediaBuyByConstituentId)) ?? NO_SNAPSHOTS : constituent.mediaBuyId ? sourceSnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS : NO_SNAPSHOTS;
1143
+ const auxiliaryRows = constituent.constituentKind === "package_item" ? auxiliarySnapshotsByPackagePair.get(
1144
+ packagePairForConstituent(constituent, packageMediaBuyByConstituentId)
1145
+ ) ?? NO_SNAPSHOTS : constituent.mediaBuyId ? auxiliarySnapshotsByMediaBuyId.get(constituent.mediaBuyId) ?? NO_SNAPSHOTS : NO_SNAPSHOTS;
1066
1146
  for (const cell of cellsByConstituent.get(constituent.constituentId) ?? []) {
1067
1147
  if (cell.status === "present" && sourceRows.length === 0) return "partial";
1068
1148
  for (const snapshot of sourceRows) {