@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.cjs CHANGED
@@ -5206,9 +5206,16 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5206
5206
  await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "targetType", "TEXT");
5207
5207
  await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "targetIds", "JSONB");
5208
5208
  await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "scorerIds", "JSONB");
5209
+ await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "organizationId", "TEXT");
5210
+ await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "projectId", "TEXT");
5211
+ await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "candidateKey", "TEXT");
5212
+ await this.#addColumnIfNotExists(storage.TABLE_DATASETS, "candidateId", "TEXT");
5209
5213
  await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "requestContext", "JSONB");
5210
5214
  await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "source", "JSONB");
5211
5215
  await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "expectedTrajectory", "JSONB");
5216
+ await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "organizationId", "TEXT");
5217
+ await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "projectId", "TEXT");
5218
+ await this.#addColumnIfNotExists(storage.TABLE_DATASET_ITEMS, "toolMocks", "JSONB");
5212
5219
  await this.createDefaultIndexes();
5213
5220
  await this.createCustomIndexes();
5214
5221
  }
@@ -5242,6 +5249,22 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5242
5249
  table: storage.TABLE_DATASET_VERSIONS,
5243
5250
  columns: ["datasetId", "version"],
5244
5251
  unique: true
5252
+ },
5253
+ // Tenancy: leading-tenant indexes for multi-tenant scans (parity with observability storage).
5254
+ {
5255
+ name: "idx_datasets_org_project",
5256
+ table: storage.TABLE_DATASETS,
5257
+ columns: ["organizationId", "projectId"]
5258
+ },
5259
+ {
5260
+ name: "idx_datasets_candidate",
5261
+ table: storage.TABLE_DATASETS,
5262
+ columns: ["candidateKey", "candidateId"]
5263
+ },
5264
+ {
5265
+ name: "idx_dataset_items_org_project",
5266
+ table: storage.TABLE_DATASET_ITEMS,
5267
+ columns: ["organizationId", "projectId"]
5245
5268
  }
5246
5269
  ];
5247
5270
  }
@@ -5279,6 +5302,10 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5279
5302
  targetType: row.targetType || null,
5280
5303
  targetIds: row.targetIds || null,
5281
5304
  scorerIds: row.scorerIds || null,
5305
+ organizationId: row.organizationId ?? null,
5306
+ projectId: row.projectId ?? null,
5307
+ candidateKey: row.candidateKey ?? null,
5308
+ candidateId: row.candidateId ?? null,
5282
5309
  version: row.version,
5283
5310
  createdAt: storage.ensureDate(row.createdAtZ || row.createdAt),
5284
5311
  updatedAt: storage.ensureDate(row.updatedAtZ || row.updatedAt)
@@ -5289,9 +5316,12 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5289
5316
  id: row.id,
5290
5317
  datasetId: row.datasetId,
5291
5318
  datasetVersion: row.datasetVersion,
5319
+ organizationId: row.organizationId ?? null,
5320
+ projectId: row.projectId ?? null,
5292
5321
  input: storage.safelyParseJSON(row.input),
5293
5322
  groundTruth: row.groundTruth ? storage.safelyParseJSON(row.groundTruth) : void 0,
5294
5323
  expectedTrajectory: row.expectedTrajectory ? storage.safelyParseJSON(row.expectedTrajectory) : void 0,
5324
+ toolMocks: row.toolMocks ? storage.safelyParseJSON(row.toolMocks) : void 0,
5295
5325
  requestContext: row.requestContext ? storage.safelyParseJSON(row.requestContext) : void 0,
5296
5326
  metadata: row.metadata ? storage.safelyParseJSON(row.metadata) : void 0,
5297
5327
  source: row.source ? storage.safelyParseJSON(row.source) : void 0,
@@ -5304,11 +5334,14 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5304
5334
  id: row.id,
5305
5335
  datasetId: row.datasetId,
5306
5336
  datasetVersion: row.datasetVersion,
5337
+ organizationId: row.organizationId ?? null,
5338
+ projectId: row.projectId ?? null,
5307
5339
  validTo: row.validTo,
5308
5340
  isDeleted: Boolean(row.isDeleted),
5309
5341
  input: storage.safelyParseJSON(row.input),
5310
5342
  groundTruth: row.groundTruth ? storage.safelyParseJSON(row.groundTruth) : void 0,
5311
5343
  expectedTrajectory: row.expectedTrajectory ? storage.safelyParseJSON(row.expectedTrajectory) : void 0,
5344
+ toolMocks: row.toolMocks ? storage.safelyParseJSON(row.toolMocks) : void 0,
5312
5345
  requestContext: row.requestContext ? storage.safelyParseJSON(row.requestContext) : void 0,
5313
5346
  metadata: row.metadata ? storage.safelyParseJSON(row.metadata) : void 0,
5314
5347
  source: row.source ? storage.safelyParseJSON(row.source) : void 0,
@@ -5343,6 +5376,10 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5343
5376
  targetType: input.targetType ?? null,
5344
5377
  targetIds: input.targetIds !== void 0 ? JSON.stringify(input.targetIds) : null,
5345
5378
  scorerIds: input.scorerIds ? JSON.stringify(input.scorerIds) : null,
5379
+ organizationId: input.organizationId ?? null,
5380
+ projectId: input.projectId ?? null,
5381
+ candidateKey: input.candidateKey ?? null,
5382
+ candidateId: input.candidateId ?? null,
5346
5383
  version: 0,
5347
5384
  createdAt: nowIso,
5348
5385
  updatedAt: nowIso
@@ -5359,6 +5396,10 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5359
5396
  targetType: input.targetType ?? null,
5360
5397
  targetIds: input.targetIds ?? null,
5361
5398
  scorerIds: input.scorerIds ?? null,
5399
+ organizationId: input.organizationId ?? null,
5400
+ projectId: input.projectId ?? null,
5401
+ candidateKey: input.candidateKey ?? null,
5402
+ candidateId: input.candidateId ?? null,
5362
5403
  version: 0,
5363
5404
  createdAt: now,
5364
5405
  updatedAt: now
@@ -5463,6 +5504,10 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5463
5504
  targetType: (args.targetType !== void 0 ? args.targetType : existing.targetType) ?? null,
5464
5505
  targetIds: (args.targetIds !== void 0 ? args.targetIds : existing.targetIds) ?? null,
5465
5506
  scorerIds: (args.scorerIds !== void 0 ? args.scorerIds : existing.scorerIds) ?? null,
5507
+ organizationId: existing.organizationId ?? null,
5508
+ projectId: existing.projectId ?? null,
5509
+ candidateKey: existing.candidateKey ?? null,
5510
+ candidateId: existing.candidateId ?? null,
5466
5511
  updatedAt: new Date(now)
5467
5512
  };
5468
5513
  } catch (error$1) {
@@ -5525,7 +5570,33 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5525
5570
  try {
5526
5571
  const { page, perPage: perPageInput } = args.pagination;
5527
5572
  const tableName = getTableName2({ indexName: storage.TABLE_DATASETS, schemaName: getSchemaName2(this.#schema) });
5528
- const countResult = await this.#db.client.one(`SELECT COUNT(*) as count FROM ${tableName}`);
5573
+ const conditions = [];
5574
+ const queryParams = [];
5575
+ let paramIndex = 1;
5576
+ if (args.filters) {
5577
+ const { organizationId, projectId, candidateKey, candidateId } = args.filters;
5578
+ if (organizationId !== void 0) {
5579
+ conditions.push(`"organizationId" = $${paramIndex++}`);
5580
+ queryParams.push(organizationId);
5581
+ }
5582
+ if (projectId !== void 0) {
5583
+ conditions.push(`"projectId" = $${paramIndex++}`);
5584
+ queryParams.push(projectId);
5585
+ }
5586
+ if (candidateKey !== void 0) {
5587
+ conditions.push(`"candidateKey" = $${paramIndex++}`);
5588
+ queryParams.push(candidateKey);
5589
+ }
5590
+ if (candidateId !== void 0) {
5591
+ conditions.push(`"candidateId" = $${paramIndex++}`);
5592
+ queryParams.push(candidateId);
5593
+ }
5594
+ }
5595
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
5596
+ const countResult = await this.#db.client.one(
5597
+ `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
5598
+ queryParams
5599
+ );
5529
5600
  const total = parseInt(countResult.count, 10);
5530
5601
  if (total === 0) {
5531
5602
  return { datasets: [], pagination: { total: 0, page, perPage: perPageInput, hasMore: false } };
@@ -5534,8 +5605,8 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5534
5605
  const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
5535
5606
  const limitValue = perPageInput === false ? total : perPage;
5536
5607
  const rows = await this.#db.client.manyOrNone(
5537
- `SELECT * FROM ${tableName} ORDER BY "createdAt" DESC, "id" ASC LIMIT $1 OFFSET $2`,
5538
- [limitValue, offset]
5608
+ `SELECT * FROM ${tableName} ${whereClause} ORDER BY "createdAt" DESC, "id" ASC LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`,
5609
+ [...queryParams, limitValue, offset]
5539
5610
  );
5540
5611
  return {
5541
5612
  datasets: (rows || []).map((row) => this.transformDatasetRow(row)),
@@ -5571,21 +5642,28 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5571
5642
  const now = /* @__PURE__ */ new Date();
5572
5643
  const nowIso = now.toISOString();
5573
5644
  let newVersion;
5645
+ let parentOrganizationId = null;
5646
+ let parentProjectId = null;
5574
5647
  await this.#db.client.tx(async (t) => {
5575
5648
  const row = await t.one(
5576
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5649
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5577
5650
  [args.datasetId]
5578
5651
  );
5579
5652
  newVersion = row.version;
5653
+ parentOrganizationId = row.organizationId ?? null;
5654
+ parentProjectId = row.projectId ?? null;
5580
5655
  await t.none(
5581
- `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)`,
5656
+ `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)`,
5582
5657
  [
5583
5658
  id,
5584
5659
  args.datasetId,
5585
5660
  newVersion,
5661
+ parentOrganizationId,
5662
+ parentProjectId,
5586
5663
  JSON.stringify(args.input),
5587
5664
  jsonbArg(args.groundTruth),
5588
5665
  jsonbArg(args.expectedTrajectory),
5666
+ jsonbArg(args.toolMocks),
5589
5667
  jsonbArg(args.requestContext),
5590
5668
  jsonbArg(args.metadata),
5591
5669
  jsonbArg(args.source),
@@ -5604,9 +5682,12 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5604
5682
  id,
5605
5683
  datasetId: args.datasetId,
5606
5684
  datasetVersion: newVersion,
5685
+ organizationId: parentOrganizationId,
5686
+ projectId: parentProjectId,
5607
5687
  input: args.input,
5608
5688
  groundTruth: args.groundTruth,
5609
5689
  expectedTrajectory: args.expectedTrajectory,
5690
+ toolMocks: args.toolMocks,
5610
5691
  requestContext: args.requestContext,
5611
5692
  metadata: args.metadata,
5612
5693
  source: args.source,
@@ -5656,29 +5737,37 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5656
5737
  const mergedInput = args.input !== void 0 ? args.input : existing.input;
5657
5738
  const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
5658
5739
  const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
5740
+ const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
5659
5741
  const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
5660
5742
  const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
5661
5743
  const mergedSource = args.source !== void 0 ? args.source : existing.source;
5662
5744
  let newVersion;
5745
+ let parentOrganizationId = null;
5746
+ let parentProjectId = null;
5663
5747
  await this.#db.client.tx(async (t) => {
5664
5748
  const row = await t.one(
5665
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5749
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5666
5750
  [args.datasetId]
5667
5751
  );
5668
5752
  newVersion = row.version;
5753
+ parentOrganizationId = row.organizationId ?? null;
5754
+ parentProjectId = row.projectId ?? null;
5669
5755
  await t.none(
5670
5756
  `UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`,
5671
5757
  [newVersion, args.id]
5672
5758
  );
5673
5759
  await t.none(
5674
- `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)`,
5760
+ `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)`,
5675
5761
  [
5676
5762
  args.id,
5677
5763
  args.datasetId,
5678
5764
  newVersion,
5765
+ parentOrganizationId,
5766
+ parentProjectId,
5679
5767
  JSON.stringify(mergedInput),
5680
5768
  jsonbArg(mergedGroundTruth),
5681
5769
  jsonbArg(mergedExpectedTrajectory),
5770
+ jsonbArg(mergedToolMocks),
5682
5771
  jsonbArg(mergedRequestContext),
5683
5772
  jsonbArg(mergedMetadata),
5684
5773
  jsonbArg(mergedSource),
@@ -5696,9 +5785,12 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5696
5785
  return {
5697
5786
  ...existing,
5698
5787
  datasetVersion: newVersion,
5788
+ organizationId: parentOrganizationId,
5789
+ projectId: parentProjectId,
5699
5790
  input: mergedInput,
5700
5791
  groundTruth: mergedGroundTruth,
5701
5792
  expectedTrajectory: mergedExpectedTrajectory,
5793
+ toolMocks: mergedToolMocks,
5702
5794
  requestContext: mergedRequestContext,
5703
5795
  metadata: mergedMetadata,
5704
5796
  source: mergedSource,
@@ -5738,23 +5830,28 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5738
5830
  const nowIso = (/* @__PURE__ */ new Date()).toISOString();
5739
5831
  await this.#db.client.tx(async (t) => {
5740
5832
  const row = await t.one(
5741
- `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
5833
+ `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`,
5742
5834
  [datasetId]
5743
5835
  );
5744
5836
  const newVersion = row.version;
5837
+ const parentOrganizationId = row.organizationId ?? null;
5838
+ const parentProjectId = row.projectId ?? null;
5745
5839
  await t.none(
5746
5840
  `UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`,
5747
5841
  [newVersion, id]
5748
5842
  );
5749
5843
  await t.none(
5750
- `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)`,
5844
+ `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)`,
5751
5845
  [
5752
5846
  id,
5753
5847
  datasetId,
5754
5848
  newVersion,
5849
+ parentOrganizationId,
5850
+ parentProjectId,
5755
5851
  JSON.stringify(existing.input),
5756
5852
  jsonbArg(existing.groundTruth),
5757
5853
  jsonbArg(existing.expectedTrajectory),
5854
+ jsonbArg(existing.toolMocks),
5758
5855
  jsonbArg(existing.requestContext),
5759
5856
  jsonbArg(existing.metadata),
5760
5857
  jsonbArg(existing.source),
@@ -5802,6 +5899,8 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5802
5899
  const nowIso = now.toISOString();
5803
5900
  const versionId = crypto.randomUUID();
5804
5901
  const itemsWithIds = input.items.map((itemInput) => ({ id: crypto.randomUUID(), input: itemInput }));
5902
+ const parentOrganizationId = dataset.organizationId ?? null;
5903
+ const parentProjectId = dataset.projectId ?? null;
5805
5904
  let newVersion;
5806
5905
  await this.#db.client.tx(async (t) => {
5807
5906
  const row = await t.one(
@@ -5811,14 +5910,17 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5811
5910
  newVersion = row.version;
5812
5911
  for (const { id, input: itemInput } of itemsWithIds) {
5813
5912
  await t.none(
5814
- `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)`,
5913
+ `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)`,
5815
5914
  [
5816
5915
  id,
5817
5916
  input.datasetId,
5818
5917
  newVersion,
5918
+ parentOrganizationId,
5919
+ parentProjectId,
5819
5920
  JSON.stringify(itemInput.input),
5820
5921
  jsonbArg(itemInput.groundTruth),
5821
5922
  jsonbArg(itemInput.expectedTrajectory),
5923
+ jsonbArg(itemInput.toolMocks),
5822
5924
  jsonbArg(itemInput.requestContext),
5823
5925
  jsonbArg(itemInput.metadata),
5824
5926
  jsonbArg(itemInput.source),
@@ -5838,9 +5940,12 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5838
5940
  id,
5839
5941
  datasetId: input.datasetId,
5840
5942
  datasetVersion: newVersion,
5943
+ organizationId: parentOrganizationId,
5944
+ projectId: parentProjectId,
5841
5945
  input: itemInput.input,
5842
5946
  groundTruth: itemInput.groundTruth,
5843
5947
  expectedTrajectory: itemInput.expectedTrajectory,
5948
+ toolMocks: itemInput.toolMocks,
5844
5949
  requestContext: itemInput.requestContext,
5845
5950
  metadata: itemInput.metadata,
5846
5951
  source: itemInput.source,
@@ -5886,6 +5991,8 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5886
5991
  });
5887
5992
  const nowIso = (/* @__PURE__ */ new Date()).toISOString();
5888
5993
  const versionId = crypto.randomUUID();
5994
+ const parentOrganizationId = dataset.organizationId ?? null;
5995
+ const parentProjectId = dataset.projectId ?? null;
5889
5996
  await this.#db.client.tx(async (t) => {
5890
5997
  const row = await t.one(
5891
5998
  `UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`,
@@ -5898,14 +6005,17 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
5898
6005
  [newVersion, item.id]
5899
6006
  );
5900
6007
  await t.none(
5901
- `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)`,
6008
+ `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)`,
5902
6009
  [
5903
6010
  item.id,
5904
6011
  input.datasetId,
5905
6012
  newVersion,
6013
+ parentOrganizationId,
6014
+ parentProjectId,
5906
6015
  JSON.stringify(item.input),
5907
6016
  jsonbArg(item.groundTruth),
5908
6017
  jsonbArg(item.expectedTrajectory),
6018
+ jsonbArg(item.toolMocks),
5909
6019
  jsonbArg(item.requestContext),
5910
6020
  jsonbArg(item.metadata),
5911
6021
  jsonbArg(item.source),
@@ -6023,6 +6133,17 @@ var DatasetsPG = class _DatasetsPG extends storage.DatasetsStorage {
6023
6133
  queryParams.push(`%${args.search}%`);
6024
6134
  paramIndex++;
6025
6135
  }
6136
+ if (args.filters) {
6137
+ const { organizationId, projectId } = args.filters;
6138
+ if (organizationId !== void 0) {
6139
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6140
+ queryParams.push(organizationId);
6141
+ }
6142
+ if (projectId !== void 0) {
6143
+ conditions.push(`"projectId" = $${paramIndex++}`);
6144
+ queryParams.push(projectId);
6145
+ }
6146
+ }
6026
6147
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
6027
6148
  const countResult = await this.#db.client.one(
6028
6149
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -6161,12 +6282,12 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6161
6282
  await this.#db.alterTable({
6162
6283
  tableName: storage.TABLE_EXPERIMENTS,
6163
6284
  schema: storage.EXPERIMENTS_SCHEMA,
6164
- ifNotExists: ["agentVersion"]
6285
+ ifNotExists: ["agentVersion", "organizationId", "projectId"]
6165
6286
  });
6166
6287
  await this.#db.alterTable({
6167
6288
  tableName: storage.TABLE_EXPERIMENT_RESULTS,
6168
6289
  schema: storage.EXPERIMENT_RESULTS_SCHEMA,
6169
- ifNotExists: ["status", "tags"]
6290
+ ifNotExists: ["status", "tags", "toolMockReport", "organizationId", "projectId"]
6170
6291
  });
6171
6292
  await this.createDefaultIndexes();
6172
6293
  await this.createCustomIndexes();
@@ -6180,6 +6301,17 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6180
6301
  table: storage.TABLE_EXPERIMENT_RESULTS,
6181
6302
  columns: ["experimentId", "itemId"],
6182
6303
  unique: true
6304
+ },
6305
+ // Tenancy: leading-tenant indexes for multi-tenant scans (parity with datasets domain).
6306
+ {
6307
+ name: "idx_experiments_org_project",
6308
+ table: storage.TABLE_EXPERIMENTS,
6309
+ columns: ["organizationId", "projectId"]
6310
+ },
6311
+ {
6312
+ name: "idx_experiment_results_org_project",
6313
+ table: storage.TABLE_EXPERIMENT_RESULTS,
6314
+ columns: ["organizationId", "projectId"]
6183
6315
  }
6184
6316
  ];
6185
6317
  }
@@ -6213,6 +6345,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6213
6345
  datasetId: row.datasetId ?? null,
6214
6346
  datasetVersion: row.datasetVersion != null ? row.datasetVersion : null,
6215
6347
  agentVersion: row.agentVersion ?? null,
6348
+ organizationId: row.organizationId ?? null,
6349
+ projectId: row.projectId ?? null,
6216
6350
  targetType: row.targetType,
6217
6351
  targetId: row.targetId,
6218
6352
  status: row.status,
@@ -6232,6 +6366,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6232
6366
  experimentId: row.experimentId,
6233
6367
  itemId: row.itemId,
6234
6368
  itemDatasetVersion: row.itemDatasetVersion != null ? row.itemDatasetVersion : null,
6369
+ organizationId: row.organizationId ?? null,
6370
+ projectId: row.projectId ?? null,
6235
6371
  input: storage.safelyParseJSON(row.input),
6236
6372
  output: row.output ? storage.safelyParseJSON(row.output) : null,
6237
6373
  groundTruth: row.groundTruth ? storage.safelyParseJSON(row.groundTruth) : null,
@@ -6242,6 +6378,7 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6242
6378
  traceId: row.traceId ?? null,
6243
6379
  status: row.status ?? null,
6244
6380
  tags: row.tags ? storage.safelyParseJSON(row.tags) : null,
6381
+ toolMockReport: row.toolMockReport ? storage.safelyParseJSON(row.toolMockReport) : null,
6245
6382
  createdAt: storage.ensureDate(row.createdAtZ || row.createdAt)
6246
6383
  };
6247
6384
  }
@@ -6261,6 +6398,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6261
6398
  datasetId: input.datasetId ?? null,
6262
6399
  datasetVersion: input.datasetVersion ?? null,
6263
6400
  agentVersion: input.agentVersion ?? null,
6401
+ organizationId: input.organizationId ?? null,
6402
+ projectId: input.projectId ?? null,
6264
6403
  targetType: input.targetType,
6265
6404
  targetId: input.targetId,
6266
6405
  status: "pending",
@@ -6282,6 +6421,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6282
6421
  datasetId: input.datasetId ?? null,
6283
6422
  datasetVersion: input.datasetVersion ?? null,
6284
6423
  agentVersion: input.agentVersion ?? null,
6424
+ organizationId: input.organizationId ?? null,
6425
+ projectId: input.projectId ?? null,
6285
6426
  targetType: input.targetType,
6286
6427
  targetId: input.targetId,
6287
6428
  status: "pending",
@@ -6423,6 +6564,17 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6423
6564
  conditions.push(`"status" = $${paramIndex++}`);
6424
6565
  queryParams.push(args.status);
6425
6566
  }
6567
+ if (args.filters) {
6568
+ const { organizationId, projectId } = args.filters;
6569
+ if (organizationId !== void 0) {
6570
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6571
+ queryParams.push(organizationId);
6572
+ }
6573
+ if (projectId !== void 0) {
6574
+ conditions.push(`"projectId" = $${paramIndex++}`);
6575
+ queryParams.push(projectId);
6576
+ }
6577
+ }
6426
6578
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
6427
6579
  const countResult = await this.#db.client.one(
6428
6580
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -6492,6 +6644,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6492
6644
  experimentId: input.experimentId,
6493
6645
  itemId: input.itemId,
6494
6646
  itemDatasetVersion: input.itemDatasetVersion ?? null,
6647
+ organizationId: input.organizationId ?? null,
6648
+ projectId: input.projectId ?? null,
6495
6649
  input: input.input,
6496
6650
  output: input.output ?? null,
6497
6651
  groundTruth: input.groundTruth ?? null,
@@ -6502,6 +6656,7 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6502
6656
  traceId: input.traceId ?? null,
6503
6657
  status: input.status ?? null,
6504
6658
  tags: input.tags ?? null,
6659
+ toolMockReport: input.toolMockReport ?? null,
6505
6660
  createdAt: nowIso
6506
6661
  }
6507
6662
  });
@@ -6510,6 +6665,8 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6510
6665
  experimentId: input.experimentId,
6511
6666
  itemId: input.itemId,
6512
6667
  itemDatasetVersion: input.itemDatasetVersion ?? null,
6668
+ organizationId: input.organizationId ?? null,
6669
+ projectId: input.projectId ?? null,
6513
6670
  input: input.input,
6514
6671
  output: input.output ?? null,
6515
6672
  groundTruth: input.groundTruth ?? null,
@@ -6520,6 +6677,7 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6520
6677
  traceId: input.traceId ?? null,
6521
6678
  status: input.status ?? null,
6522
6679
  tags: input.tags ?? null,
6680
+ toolMockReport: input.toolMockReport ?? null,
6523
6681
  createdAt: now
6524
6682
  };
6525
6683
  } catch (error$1) {
@@ -6622,6 +6780,17 @@ var ExperimentsPG = class _ExperimentsPG extends storage.ExperimentsStorage {
6622
6780
  conditions.push(`"status" = $${paramIndex++}`);
6623
6781
  queryParams.push(args.status);
6624
6782
  }
6783
+ if (args.filters) {
6784
+ const { organizationId, projectId } = args.filters;
6785
+ if (organizationId !== void 0) {
6786
+ conditions.push(`"organizationId" = $${paramIndex++}`);
6787
+ queryParams.push(organizationId);
6788
+ }
6789
+ if (projectId !== void 0) {
6790
+ conditions.push(`"projectId" = $${paramIndex++}`);
6791
+ queryParams.push(projectId);
6792
+ }
6793
+ }
6625
6794
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
6626
6795
  const countResult = await this.#db.client.one(
6627
6796
  `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`,
@@ -8309,12 +8478,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
8309
8478
  await this.#db.createTable({ tableName: storage.TABLE_THREADS, schema: storage.TABLE_SCHEMAS[storage.TABLE_THREADS] });
8310
8479
  await this.#db.createTable({ tableName: storage.TABLE_MESSAGES, schema: storage.TABLE_SCHEMAS[storage.TABLE_MESSAGES] });
8311
8480
  await this.#db.createTable({ tableName: storage.TABLE_RESOURCES, schema: storage.TABLE_SCHEMAS[storage.TABLE_RESOURCES] });
8312
- let omSchema;
8313
- try {
8314
- const { OBSERVATIONAL_MEMORY_TABLE_SCHEMA } = await import('@mastra/core/storage');
8315
- omSchema = OBSERVATIONAL_MEMORY_TABLE_SCHEMA?.[OM_TABLE];
8316
- } catch {
8317
- }
8481
+ const omSchema = _omTableSchema?.[OM_TABLE];
8318
8482
  if (omSchema) {
8319
8483
  await this.#db.createTable({
8320
8484
  tableName: OM_TABLE,
@@ -19172,6 +19336,9 @@ var PostgresStore = class extends storage.MastraCompositeStore {
19172
19336
  #poolClosed = false;
19173
19337
  schema;
19174
19338
  isInitialized = false;
19339
+ // Caches the in-flight init() so concurrent callers share one initialization
19340
+ // instead of each acquiring + pinning a client. See init() / issue #18282.
19341
+ #initPromise = null;
19175
19342
  stores;
19176
19343
  constructor(config) {
19177
19344
  try {
@@ -19238,16 +19405,25 @@ var PostgresStore = class extends storage.MastraCompositeStore {
19238
19405
  throw new Error("PostgresStore: invalid config");
19239
19406
  }
19240
19407
  async init() {
19408
+ if (this.disableInit || process.env.MASTRA_DISABLE_STORAGE_INIT === "true") {
19409
+ return;
19410
+ }
19241
19411
  if (this.isInitialized) {
19242
19412
  return;
19243
19413
  }
19244
- const pinnedClient = await this.#pool.connect();
19245
- const pinned = new PinnedClientAdapter(this.#pool, pinnedClient);
19414
+ this.#initPromise ??= this.#runPinnedInit();
19415
+ await this.#initPromise;
19416
+ }
19417
+ async #runPinnedInit() {
19418
+ let pinnedClient;
19246
19419
  try {
19420
+ pinnedClient = await this.#pool.connect();
19421
+ const pinned = new PinnedClientAdapter(this.#pool, pinnedClient);
19247
19422
  this.#db.pin(pinned);
19248
19423
  await super.init();
19249
19424
  this.isInitialized = true;
19250
19425
  } catch (error$1) {
19426
+ this.#initPromise = null;
19251
19427
  if (error$1 instanceof error.MastraError) {
19252
19428
  throw error$1;
19253
19429
  }
@@ -19260,8 +19436,10 @@ var PostgresStore = class extends storage.MastraCompositeStore {
19260
19436
  error$1
19261
19437
  );
19262
19438
  } finally {
19263
- this.#db.unpin();
19264
- pinnedClient.release();
19439
+ if (pinnedClient) {
19440
+ this.#db.unpin();
19441
+ pinnedClient.release();
19442
+ }
19265
19443
  }
19266
19444
  }
19267
19445
  /**