@mastra/pg 1.14.0 → 1.14.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5181,9 +5181,16 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5181
5181
  await this.#addColumnIfNotExists(TABLE_DATASETS, "targetType", "TEXT");
5182
5182
  await this.#addColumnIfNotExists(TABLE_DATASETS, "targetIds", "JSONB");
5183
5183
  await this.#addColumnIfNotExists(TABLE_DATASETS, "scorerIds", "JSONB");
5184
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "organizationId", "TEXT");
5185
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "projectId", "TEXT");
5186
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "candidateKey", "TEXT");
5187
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "candidateId", "TEXT");
5184
5188
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "requestContext", "JSONB");
5185
5189
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "source", "JSONB");
5186
5190
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "expectedTrajectory", "JSONB");
5191
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "organizationId", "TEXT");
5192
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "projectId", "TEXT");
5193
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "toolMocks", "JSONB");
5187
5194
  await this.createDefaultIndexes();
5188
5195
  await this.createCustomIndexes();
5189
5196
  }
@@ -5217,6 +5224,22 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5217
5224
  table: TABLE_DATASET_VERSIONS,
5218
5225
  columns: ["datasetId", "version"],
5219
5226
  unique: true
5227
+ },
5228
+ // Tenancy: leading-tenant indexes for multi-tenant scans (parity with observability storage).
5229
+ {
5230
+ name: "idx_datasets_org_project",
5231
+ table: TABLE_DATASETS,
5232
+ columns: ["organizationId", "projectId"]
5233
+ },
5234
+ {
5235
+ name: "idx_datasets_candidate",
5236
+ table: TABLE_DATASETS,
5237
+ columns: ["candidateKey", "candidateId"]
5238
+ },
5239
+ {
5240
+ name: "idx_dataset_items_org_project",
5241
+ table: TABLE_DATASET_ITEMS,
5242
+ columns: ["organizationId", "projectId"]
5220
5243
  }
5221
5244
  ];
5222
5245
  }
@@ -5254,6 +5277,10 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5254
5277
  targetType: row.targetType || null,
5255
5278
  targetIds: row.targetIds || null,
5256
5279
  scorerIds: row.scorerIds || null,
5280
+ organizationId: row.organizationId ?? null,
5281
+ projectId: row.projectId ?? null,
5282
+ candidateKey: row.candidateKey ?? null,
5283
+ candidateId: row.candidateId ?? null,
5257
5284
  version: row.version,
5258
5285
  createdAt: ensureDate(row.createdAtZ || row.createdAt),
5259
5286
  updatedAt: ensureDate(row.updatedAtZ || row.updatedAt)
@@ -5264,9 +5291,12 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5264
5291
  id: row.id,
5265
5292
  datasetId: row.datasetId,
5266
5293
  datasetVersion: row.datasetVersion,
5294
+ organizationId: row.organizationId ?? null,
5295
+ projectId: row.projectId ?? null,
5267
5296
  input: safelyParseJSON(row.input),
5268
5297
  groundTruth: row.groundTruth ? safelyParseJSON(row.groundTruth) : void 0,
5269
5298
  expectedTrajectory: row.expectedTrajectory ? safelyParseJSON(row.expectedTrajectory) : void 0,
5299
+ toolMocks: row.toolMocks ? safelyParseJSON(row.toolMocks) : void 0,
5270
5300
  requestContext: row.requestContext ? safelyParseJSON(row.requestContext) : void 0,
5271
5301
  metadata: row.metadata ? safelyParseJSON(row.metadata) : void 0,
5272
5302
  source: row.source ? safelyParseJSON(row.source) : void 0,
@@ -5279,11 +5309,14 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5279
5309
  id: row.id,
5280
5310
  datasetId: row.datasetId,
5281
5311
  datasetVersion: row.datasetVersion,
5312
+ organizationId: row.organizationId ?? null,
5313
+ projectId: row.projectId ?? null,
5282
5314
  validTo: row.validTo,
5283
5315
  isDeleted: Boolean(row.isDeleted),
5284
5316
  input: safelyParseJSON(row.input),
5285
5317
  groundTruth: row.groundTruth ? safelyParseJSON(row.groundTruth) : void 0,
5286
5318
  expectedTrajectory: row.expectedTrajectory ? safelyParseJSON(row.expectedTrajectory) : void 0,
5319
+ toolMocks: row.toolMocks ? safelyParseJSON(row.toolMocks) : void 0,
5287
5320
  requestContext: row.requestContext ? safelyParseJSON(row.requestContext) : void 0,
5288
5321
  metadata: row.metadata ? safelyParseJSON(row.metadata) : void 0,
5289
5322
  source: row.source ? safelyParseJSON(row.source) : void 0,
@@ -5318,6 +5351,10 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5318
5351
  targetType: input.targetType ?? null,
5319
5352
  targetIds: input.targetIds !== void 0 ? JSON.stringify(input.targetIds) : null,
5320
5353
  scorerIds: input.scorerIds ? JSON.stringify(input.scorerIds) : null,
5354
+ organizationId: input.organizationId ?? null,
5355
+ projectId: input.projectId ?? null,
5356
+ candidateKey: input.candidateKey ?? null,
5357
+ candidateId: input.candidateId ?? null,
5321
5358
  version: 0,
5322
5359
  createdAt: nowIso,
5323
5360
  updatedAt: nowIso
@@ -5334,6 +5371,10 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5334
5371
  targetType: input.targetType ?? null,
5335
5372
  targetIds: input.targetIds ?? null,
5336
5373
  scorerIds: input.scorerIds ?? null,
5374
+ organizationId: input.organizationId ?? null,
5375
+ projectId: input.projectId ?? null,
5376
+ candidateKey: input.candidateKey ?? null,
5377
+ candidateId: input.candidateId ?? null,
5337
5378
  version: 0,
5338
5379
  createdAt: now,
5339
5380
  updatedAt: now
@@ -5438,6 +5479,10 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5438
5479
  targetType: (args.targetType !== void 0 ? args.targetType : existing.targetType) ?? null,
5439
5480
  targetIds: (args.targetIds !== void 0 ? args.targetIds : existing.targetIds) ?? null,
5440
5481
  scorerIds: (args.scorerIds !== void 0 ? args.scorerIds : existing.scorerIds) ?? null,
5482
+ organizationId: existing.organizationId ?? null,
5483
+ projectId: existing.projectId ?? null,
5484
+ candidateKey: existing.candidateKey ?? null,
5485
+ candidateId: existing.candidateId ?? null,
5441
5486
  updatedAt: new Date(now)
5442
5487
  };
5443
5488
  } catch (error) {
@@ -5500,7 +5545,33 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5500
5545
  try {
5501
5546
  const { page, perPage: perPageInput } = args.pagination;
5502
5547
  const tableName = getTableName2({ indexName: TABLE_DATASETS, schemaName: getSchemaName2(this.#schema) });
5503
- const countResult = await this.#db.client.one(`SELECT COUNT(*) as count FROM ${tableName}`);
5548
+ const conditions = [];
5549
+ const queryParams = [];
5550
+ let paramIndex = 1;
5551
+ if (args.filters) {
5552
+ const { organizationId, projectId, candidateKey, candidateId } = args.filters;
5553
+ if (organizationId !== void 0) {
5554
+ conditions.push(`"organizationId" = $${paramIndex++}`);
5555
+ queryParams.push(organizationId);
5556
+ }
5557
+ if (projectId !== void 0) {
5558
+ conditions.push(`"projectId" = $${paramIndex++}`);
5559
+ queryParams.push(projectId);
5560
+ }
5561
+ if (candidateKey !== void 0) {
5562
+ conditions.push(`"candidateKey" = $${paramIndex++}`);
5563
+ queryParams.push(candidateKey);
5564
+ }
5565
+ if (candidateId !== void 0) {
5566
+ conditions.push(`"candidateId" = $${paramIndex++}`);
5567
+ queryParams.push(candidateId);
5568
+ }
5569
+ }
5570
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
5571
+ const countResult = await this.#db.client.one(
5572
+ `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
5573
+ queryParams
5574
+ );
5504
5575
  const total = parseInt(countResult.count, 10);
5505
5576
  if (total === 0) {
5506
5577
  return { datasets: [], pagination: { total: 0, page, perPage: perPageInput, hasMore: false } };
@@ -5509,8 +5580,8 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5509
5580
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
5510
5581
  const limitValue = perPageInput === false ? total : perPage;
5511
5582
  const rows = await this.#db.client.manyOrNone(
5512
- `SELECT * FROM ${tableName} ORDER BY "createdAt" DESC, "id" ASC LIMIT $1 OFFSET $2`,
5513
- [limitValue, offset]
5583
+ `SELECT * FROM ${tableName} ${whereClause} ORDER BY "createdAt" DESC, "id" ASC LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`,
5584
+ [...queryParams, limitValue, offset]
5514
5585
  );
5515
5586
  return {
5516
5587
  datasets: (rows || []).map((row) => this.transformDatasetRow(row)),
@@ -5546,21 +5617,28 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5546
5617
  const now = /* @__PURE__ */ new Date();
5547
5618
  const nowIso = now.toISOString();
5548
5619
  let newVersion;
5620
+ let parentOrganizationId = null;
5621
+ let parentProjectId = null;
5549
5622
  await this.#db.client.tx(async (t) => {
5550
5623
  const row = await t.one(
5551
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5624
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5552
5625
  [args.datasetId]
5553
5626
  );
5554
5627
  newVersion = row.version;
5628
+ parentOrganizationId = row.organizationId ?? null;
5629
+ parentProjectId = row.projectId ?? null;
5555
5630
  await t.none(
5556
- `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","validTo","isDeleted","input","groundTruth","expectedTrajectory","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,NULL,false,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)`,
5631
+ `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,NULL,false,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)`,
5557
5632
  [
5558
5633
  id,
5559
5634
  args.datasetId,
5560
5635
  newVersion,
5636
+ parentOrganizationId,
5637
+ parentProjectId,
5561
5638
  JSON.stringify(args.input),
5562
5639
  jsonbArg(args.groundTruth),
5563
5640
  jsonbArg(args.expectedTrajectory),
5641
+ jsonbArg(args.toolMocks),
5564
5642
  jsonbArg(args.requestContext),
5565
5643
  jsonbArg(args.metadata),
5566
5644
  jsonbArg(args.source),
@@ -5579,9 +5657,12 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5579
5657
  id,
5580
5658
  datasetId: args.datasetId,
5581
5659
  datasetVersion: newVersion,
5660
+ organizationId: parentOrganizationId,
5661
+ projectId: parentProjectId,
5582
5662
  input: args.input,
5583
5663
  groundTruth: args.groundTruth,
5584
5664
  expectedTrajectory: args.expectedTrajectory,
5665
+ toolMocks: args.toolMocks,
5585
5666
  requestContext: args.requestContext,
5586
5667
  metadata: args.metadata,
5587
5668
  source: args.source,
@@ -5631,29 +5712,37 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5631
5712
  const mergedInput = args.input !== void 0 ? args.input : existing.input;
5632
5713
  const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
5633
5714
  const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
5715
+ const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
5634
5716
  const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
5635
5717
  const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
5636
5718
  const mergedSource = args.source !== void 0 ? args.source : existing.source;
5637
5719
  let newVersion;
5720
+ let parentOrganizationId = null;
5721
+ let parentProjectId = null;
5638
5722
  await this.#db.client.tx(async (t) => {
5639
5723
  const row = await t.one(
5640
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5724
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5641
5725
  [args.datasetId]
5642
5726
  );
5643
5727
  newVersion = row.version;
5728
+ parentOrganizationId = row.organizationId ?? null;
5729
+ parentProjectId = row.projectId ?? null;
5644
5730
  await t.none(
5645
5731
  `UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`,
5646
5732
  [newVersion, args.id]
5647
5733
  );
5648
5734
  await t.none(
5649
- `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","validTo","isDeleted","input","groundTruth","expectedTrajectory","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,NULL,false,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)`,
5735
+ `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,NULL,false,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)`,
5650
5736
  [
5651
5737
  args.id,
5652
5738
  args.datasetId,
5653
5739
  newVersion,
5740
+ parentOrganizationId,
5741
+ parentProjectId,
5654
5742
  JSON.stringify(mergedInput),
5655
5743
  jsonbArg(mergedGroundTruth),
5656
5744
  jsonbArg(mergedExpectedTrajectory),
5745
+ jsonbArg(mergedToolMocks),
5657
5746
  jsonbArg(mergedRequestContext),
5658
5747
  jsonbArg(mergedMetadata),
5659
5748
  jsonbArg(mergedSource),
@@ -5671,9 +5760,12 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5671
5760
  return {
5672
5761
  ...existing,
5673
5762
  datasetVersion: newVersion,
5763
+ organizationId: parentOrganizationId,
5764
+ projectId: parentProjectId,
5674
5765
  input: mergedInput,
5675
5766
  groundTruth: mergedGroundTruth,
5676
5767
  expectedTrajectory: mergedExpectedTrajectory,
5768
+ toolMocks: mergedToolMocks,
5677
5769
  requestContext: mergedRequestContext,
5678
5770
  metadata: mergedMetadata,
5679
5771
  source: mergedSource,
@@ -5713,23 +5805,28 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5713
5805
  const nowIso = (/* @__PURE__ */ new Date()).toISOString();
5714
5806
  await this.#db.client.tx(async (t) => {
5715
5807
  const row = await t.one(
5716
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5808
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5717
5809
  [datasetId]
5718
5810
  );
5719
5811
  const newVersion = row.version;
5812
+ const parentOrganizationId = row.organizationId ?? null;
5813
+ const parentProjectId = row.projectId ?? null;
5720
5814
  await t.none(
5721
5815
  `UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`,
5722
5816
  [newVersion, id]
5723
5817
  );
5724
5818
  await t.none(
5725
- `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","validTo","isDeleted","input","groundTruth","expectedTrajectory","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,NULL,true,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)`,
5819
+ `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,NULL,true,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)`,
5726
5820
  [
5727
5821
  id,
5728
5822
  datasetId,
5729
5823
  newVersion,
5824
+ parentOrganizationId,
5825
+ parentProjectId,
5730
5826
  JSON.stringify(existing.input),
5731
5827
  jsonbArg(existing.groundTruth),
5732
5828
  jsonbArg(existing.expectedTrajectory),
5829
+ jsonbArg(existing.toolMocks),
5733
5830
  jsonbArg(existing.requestContext),
5734
5831
  jsonbArg(existing.metadata),
5735
5832
  jsonbArg(existing.source),
@@ -5777,6 +5874,8 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5777
5874
  const nowIso = now.toISOString();
5778
5875
  const versionId = crypto.randomUUID();
5779
5876
  const itemsWithIds = input.items.map((itemInput) => ({ id: crypto.randomUUID(), input: itemInput }));
5877
+ const parentOrganizationId = dataset.organizationId ?? null;
5878
+ const parentProjectId = dataset.projectId ?? null;
5780
5879
  let newVersion;
5781
5880
  await this.#db.client.tx(async (t) => {
5782
5881
  const row = await t.one(
@@ -5786,14 +5885,17 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5786
5885
  newVersion = row.version;
5787
5886
  for (const { id, input: itemInput } of itemsWithIds) {
5788
5887
  await t.none(
5789
- `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","validTo","isDeleted","input","groundTruth","expectedTrajectory","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,NULL,false,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)`,
5888
+ `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,NULL,false,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)`,
5790
5889
  [
5791
5890
  id,
5792
5891
  input.datasetId,
5793
5892
  newVersion,
5893
+ parentOrganizationId,
5894
+ parentProjectId,
5794
5895
  JSON.stringify(itemInput.input),
5795
5896
  jsonbArg(itemInput.groundTruth),
5796
5897
  jsonbArg(itemInput.expectedTrajectory),
5898
+ jsonbArg(itemInput.toolMocks),
5797
5899
  jsonbArg(itemInput.requestContext),
5798
5900
  jsonbArg(itemInput.metadata),
5799
5901
  jsonbArg(itemInput.source),
@@ -5813,9 +5915,12 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5813
5915
  id,
5814
5916
  datasetId: input.datasetId,
5815
5917
  datasetVersion: newVersion,
5918
+ organizationId: parentOrganizationId,
5919
+ projectId: parentProjectId,
5816
5920
  input: itemInput.input,
5817
5921
  groundTruth: itemInput.groundTruth,
5818
5922
  expectedTrajectory: itemInput.expectedTrajectory,
5923
+ toolMocks: itemInput.toolMocks,
5819
5924
  requestContext: itemInput.requestContext,
5820
5925
  metadata: itemInput.metadata,
5821
5926
  source: itemInput.source,
@@ -5861,6 +5966,8 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5861
5966
  });
5862
5967
  const nowIso = (/* @__PURE__ */ new Date()).toISOString();
5863
5968
  const versionId = crypto.randomUUID();
5969
+ const parentOrganizationId = dataset.organizationId ?? null;
5970
+ const parentProjectId = dataset.projectId ?? null;
5864
5971
  await this.#db.client.tx(async (t) => {
5865
5972
  const row = await t.one(
5866
5973
  `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
@@ -5873,14 +5980,17 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5873
5980
  [newVersion, item.id]
5874
5981
  );
5875
5982
  await t.none(
5876
- `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","validTo","isDeleted","input","groundTruth","expectedTrajectory","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,NULL,true,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)`,
5983
+ `INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,NULL,true,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)`,
5877
5984
  [
5878
5985
  item.id,
5879
5986
  input.datasetId,
5880
5987
  newVersion,
5988
+ parentOrganizationId,
5989
+ parentProjectId,
5881
5990
  JSON.stringify(item.input),
5882
5991
  jsonbArg(item.groundTruth),
5883
5992
  jsonbArg(item.expectedTrajectory),
5993
+ jsonbArg(item.toolMocks),
5884
5994
  jsonbArg(item.requestContext),
5885
5995
  jsonbArg(item.metadata),
5886
5996
  jsonbArg(item.source),
@@ -5998,6 +6108,17 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5998
6108
  queryParams.push(`%${args.search}%`);
5999
6109
  paramIndex++;
6000
6110
  }
6111
+ if (args.filters) {
6112
+ const { organizationId, projectId } = args.filters;
6113
+ if (organizationId !== void 0) {
6114
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6115
+ queryParams.push(organizationId);
6116
+ }
6117
+ if (projectId !== void 0) {
6118
+ conditions.push(`"projectId" = $${paramIndex++}`);
6119
+ queryParams.push(projectId);
6120
+ }
6121
+ }
6001
6122
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
6002
6123
  const countResult = await this.#db.client.one(
6003
6124
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -6136,12 +6257,12 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6136
6257
  await this.#db.alterTable({
6137
6258
  tableName: TABLE_EXPERIMENTS,
6138
6259
  schema: EXPERIMENTS_SCHEMA,
6139
- ifNotExists: ["agentVersion"]
6260
+ ifNotExists: ["agentVersion", "organizationId", "projectId"]
6140
6261
  });
6141
6262
  await this.#db.alterTable({
6142
6263
  tableName: TABLE_EXPERIMENT_RESULTS,
6143
6264
  schema: EXPERIMENT_RESULTS_SCHEMA,
6144
- ifNotExists: ["status", "tags"]
6265
+ ifNotExists: ["status", "tags", "toolMockReport", "organizationId", "projectId"]
6145
6266
  });
6146
6267
  await this.createDefaultIndexes();
6147
6268
  await this.createCustomIndexes();
@@ -6155,6 +6276,17 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6155
6276
  table: TABLE_EXPERIMENT_RESULTS,
6156
6277
  columns: ["experimentId", "itemId"],
6157
6278
  unique: true
6279
+ },
6280
+ // Tenancy: leading-tenant indexes for multi-tenant scans (parity with datasets domain).
6281
+ {
6282
+ name: "idx_experiments_org_project",
6283
+ table: TABLE_EXPERIMENTS,
6284
+ columns: ["organizationId", "projectId"]
6285
+ },
6286
+ {
6287
+ name: "idx_experiment_results_org_project",
6288
+ table: TABLE_EXPERIMENT_RESULTS,
6289
+ columns: ["organizationId", "projectId"]
6158
6290
  }
6159
6291
  ];
6160
6292
  }
@@ -6188,6 +6320,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6188
6320
  datasetId: row.datasetId ?? null,
6189
6321
  datasetVersion: row.datasetVersion != null ? row.datasetVersion : null,
6190
6322
  agentVersion: row.agentVersion ?? null,
6323
+ organizationId: row.organizationId ?? null,
6324
+ projectId: row.projectId ?? null,
6191
6325
  targetType: row.targetType,
6192
6326
  targetId: row.targetId,
6193
6327
  status: row.status,
@@ -6207,6 +6341,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6207
6341
  experimentId: row.experimentId,
6208
6342
  itemId: row.itemId,
6209
6343
  itemDatasetVersion: row.itemDatasetVersion != null ? row.itemDatasetVersion : null,
6344
+ organizationId: row.organizationId ?? null,
6345
+ projectId: row.projectId ?? null,
6210
6346
  input: safelyParseJSON(row.input),
6211
6347
  output: row.output ? safelyParseJSON(row.output) : null,
6212
6348
  groundTruth: row.groundTruth ? safelyParseJSON(row.groundTruth) : null,
@@ -6217,6 +6353,7 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6217
6353
  traceId: row.traceId ?? null,
6218
6354
  status: row.status ?? null,
6219
6355
  tags: row.tags ? safelyParseJSON(row.tags) : null,
6356
+ toolMockReport: row.toolMockReport ? safelyParseJSON(row.toolMockReport) : null,
6220
6357
  createdAt: ensureDate(row.createdAtZ || row.createdAt)
6221
6358
  };
6222
6359
  }
@@ -6236,6 +6373,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6236
6373
  datasetId: input.datasetId ?? null,
6237
6374
  datasetVersion: input.datasetVersion ?? null,
6238
6375
  agentVersion: input.agentVersion ?? null,
6376
+ organizationId: input.organizationId ?? null,
6377
+ projectId: input.projectId ?? null,
6239
6378
  targetType: input.targetType,
6240
6379
  targetId: input.targetId,
6241
6380
  status: "pending",
@@ -6257,6 +6396,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6257
6396
  datasetId: input.datasetId ?? null,
6258
6397
  datasetVersion: input.datasetVersion ?? null,
6259
6398
  agentVersion: input.agentVersion ?? null,
6399
+ organizationId: input.organizationId ?? null,
6400
+ projectId: input.projectId ?? null,
6260
6401
  targetType: input.targetType,
6261
6402
  targetId: input.targetId,
6262
6403
  status: "pending",
@@ -6398,6 +6539,17 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6398
6539
  conditions.push(`"status" = $${paramIndex++}`);
6399
6540
  queryParams.push(args.status);
6400
6541
  }
6542
+ if (args.filters) {
6543
+ const { organizationId, projectId } = args.filters;
6544
+ if (organizationId !== void 0) {
6545
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6546
+ queryParams.push(organizationId);
6547
+ }
6548
+ if (projectId !== void 0) {
6549
+ conditions.push(`"projectId" = $${paramIndex++}`);
6550
+ queryParams.push(projectId);
6551
+ }
6552
+ }
6401
6553
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
6402
6554
  const countResult = await this.#db.client.one(
6403
6555
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -6467,6 +6619,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6467
6619
  experimentId: input.experimentId,
6468
6620
  itemId: input.itemId,
6469
6621
  itemDatasetVersion: input.itemDatasetVersion ?? null,
6622
+ organizationId: input.organizationId ?? null,
6623
+ projectId: input.projectId ?? null,
6470
6624
  input: input.input,
6471
6625
  output: input.output ?? null,
6472
6626
  groundTruth: input.groundTruth ?? null,
@@ -6477,6 +6631,7 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6477
6631
  traceId: input.traceId ?? null,
6478
6632
  status: input.status ?? null,
6479
6633
  tags: input.tags ?? null,
6634
+ toolMockReport: input.toolMockReport ?? null,
6480
6635
  createdAt: nowIso
6481
6636
  }
6482
6637
  });
@@ -6485,6 +6640,8 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6485
6640
  experimentId: input.experimentId,
6486
6641
  itemId: input.itemId,
6487
6642
  itemDatasetVersion: input.itemDatasetVersion ?? null,
6643
+ organizationId: input.organizationId ?? null,
6644
+ projectId: input.projectId ?? null,
6488
6645
  input: input.input,
6489
6646
  output: input.output ?? null,
6490
6647
  groundTruth: input.groundTruth ?? null,
@@ -6495,6 +6652,7 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6495
6652
  traceId: input.traceId ?? null,
6496
6653
  status: input.status ?? null,
6497
6654
  tags: input.tags ?? null,
6655
+ toolMockReport: input.toolMockReport ?? null,
6498
6656
  createdAt: now
6499
6657
  };
6500
6658
  } catch (error) {
@@ -6597,6 +6755,17 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6597
6755
  conditions.push(`"status" = $${paramIndex++}`);
6598
6756
  queryParams.push(args.status);
6599
6757
  }
6758
+ if (args.filters) {
6759
+ const { organizationId, projectId } = args.filters;
6760
+ if (organizationId !== void 0) {
6761
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6762
+ queryParams.push(organizationId);
6763
+ }
6764
+ if (projectId !== void 0) {
6765
+ conditions.push(`"projectId" = $${paramIndex++}`);
6766
+ queryParams.push(projectId);
6767
+ }
6768
+ }
6600
6769
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
6601
6770
  const countResult = await this.#db.client.one(
6602
6771
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -8284,12 +8453,7 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
8284
8453
  await this.#db.createTable({ tableName: TABLE_THREADS, schema: TABLE_SCHEMAS[TABLE_THREADS] });
8285
8454
  await this.#db.createTable({ tableName: TABLE_MESSAGES, schema: TABLE_SCHEMAS[TABLE_MESSAGES] });
8286
8455
  await this.#db.createTable({ tableName: TABLE_RESOURCES, schema: TABLE_SCHEMAS[TABLE_RESOURCES] });
8287
- let omSchema;
8288
- try {
8289
- const { OBSERVATIONAL_MEMORY_TABLE_SCHEMA } = await import('@mastra/core/storage');
8290
- omSchema = OBSERVATIONAL_MEMORY_TABLE_SCHEMA?.[OM_TABLE];
8291
- } catch {
8292
- }
8456
+ const omSchema = _omTableSchema?.[OM_TABLE];
8293
8457
  if (omSchema) {
8294
8458
  await this.#db.createTable({
8295
8459
  tableName: OM_TABLE,
@@ -19147,6 +19311,9 @@ var PostgresStore = class extends MastraCompositeStore {
19147
19311
  #poolClosed = false;
19148
19312
  schema;
19149
19313
  isInitialized = false;
19314
+ // Caches the in-flight init() so concurrent callers share one initialization
19315
+ // instead of each acquiring + pinning a client. See init() / issue #18282.
19316
+ #initPromise = null;
19150
19317
  stores;
19151
19318
  constructor(config) {
19152
19319
  try {
@@ -19213,16 +19380,25 @@ var PostgresStore = class extends MastraCompositeStore {
19213
19380
  throw new Error("PostgresStore: invalid config");
19214
19381
  }
19215
19382
  async init() {
19383
+ if (this.disableInit || process.env.MASTRA_DISABLE_STORAGE_INIT === "true") {
19384
+ return;
19385
+ }
19216
19386
  if (this.isInitialized) {
19217
19387
  return;
19218
19388
  }
19219
- const pinnedClient = await this.#pool.connect();
19220
- const pinned = new PinnedClientAdapter(this.#pool, pinnedClient);
19389
+ this.#initPromise ??= this.#runPinnedInit();
19390
+ await this.#initPromise;
19391
+ }
19392
+ async #runPinnedInit() {
19393
+ let pinnedClient;
19221
19394
  try {
19395
+ pinnedClient = await this.#pool.connect();
19396
+ const pinned = new PinnedClientAdapter(this.#pool, pinnedClient);
19222
19397
  this.#db.pin(pinned);
19223
19398
  await super.init();
19224
19399
  this.isInitialized = true;
19225
19400
  } catch (error) {
19401
+ this.#initPromise = null;
19226
19402
  if (error instanceof MastraError) {
19227
19403
  throw error;
19228
19404
  }
@@ -19235,8 +19411,10 @@ var PostgresStore = class extends MastraCompositeStore {
19235
19411
  error
19236
19412
  );
19237
19413
  } finally {
19238
- this.#db.unpin();
19239
- pinnedClient.release();
19414
+ if (pinnedClient) {
19415
+ this.#db.unpin();
19416
+ pinnedClient.release();
19417
+ }
19240
19418
  }
19241
19419
  }
19242
19420
  /**