@mastra/mysql 0.12.1 → 0.13.0-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.
@@ -3,7 +3,7 @@ name: mastra-mysql
3
3
  description: Documentation for @mastra/mysql. Use when working with @mastra/mysql APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/mysql"
6
- version: "0.12.1"
6
+ version: "0.13.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.12.1",
2
+ "version": "0.13.0-alpha.1",
3
3
  "package": "@mastra/mysql",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -4175,6 +4175,13 @@ async function assertKnowledgeDescriptionWithinBoundCompat(description) {
4175
4175
  }
4176
4176
  assertWithinBound(description);
4177
4177
  }
4178
+ let assertRecordTextWithinBound;
4179
+ async function assertKnowledgeRecordTextWithinBoundCompat(text) {
4180
+ if (!assertRecordTextWithinBound) assertRecordTextWithinBound = (await import("@mastra/core/storage")).assertKnowledgeRecordTextWithinBound ?? ((value) => {
4181
+ if (value.length > 1e3) throw new Error("Knowledge record text exceeds the 1000 UTF-16 code unit limit; split it into separate facts or summarize it");
4182
+ });
4183
+ assertRecordTextWithinBound(text);
4184
+ }
4178
4185
  function mysqlSql(sql) {
4179
4186
  return sql.replaceAll("jsonb(?)", "CAST(? AS JSON)").replaceAll("INSERT OR IGNORE", "INSERT IGNORE").replaceAll("\"", "`");
4180
4187
  }
@@ -4193,7 +4200,21 @@ function createExecutor(client) {
4193
4200
  };
4194
4201
  } };
4195
4202
  }
4196
- const visibleSql = `(scopeKey = ? OR LEFT(?, CHAR_LENGTH(scopeKey) + 1) = CONCAT(scopeKey, char(31)))`;
4203
+ /**
4204
+ * Scope keys visible to a caller holding `scope`: a row is visible when its scope is a subset of the
4205
+ * caller's (companion entries such as `resource:x:uncurated` make this more than a prefix match).
4206
+ * Matching rows by `scopeKey` keeps visibility filters on an index; a caller scope has only a few
4207
+ * entries, so the list stays small.
4208
+ */
4209
+ function visibleScopeKeys(scope) {
4210
+ const canonical = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(scope);
4211
+ const keys = /* @__PURE__ */ new Set();
4212
+ for (let mask = 1; mask < 1 << canonical.length; mask++) try {
4213
+ keys.add((0, _mastra_core_storage.knowledgeScopeKey)(canonical.filter((_, index) => mask & 1 << index)));
4214
+ } catch {}
4215
+ return [...keys];
4216
+ }
4217
+ const visibleSql = (keys, scopeKeyColumn = "scopeKey") => `${scopeKeyColumn} IN (${keys.map(() => "?").join(",")})`;
4197
4218
  function parseJson$1(value) {
4198
4219
  if (typeof value === "string") return JSON.parse(value);
4199
4220
  if (value instanceof Uint8Array) return JSON.parse(new TextDecoder().decode(value));
@@ -4243,7 +4264,6 @@ function parseKnowledge(row) {
4243
4264
  sourceThreadId: String(row.sourceThreadId),
4244
4265
  capturedAt: toDate(row.capturedAt),
4245
4266
  when: optionalDate(row.when),
4246
- maxScope: row.maxScope == null ? void 0 : String(row.maxScope),
4247
4267
  metadata: row.metadata == null ? void 0 : parseJson$1(row.metadata),
4248
4268
  deletedAt: optionalDate(row.deletedAt),
4249
4269
  deletedBy: row.deletedBy == null ? void 0 : String(row.deletedBy)
@@ -4269,13 +4289,17 @@ function parseOutbox(row) {
4269
4289
  const KNOWLEDGE_INDEX_DDL = [
4270
4290
  `CREATE UNIQUE INDEX idx_knowledge_nodes_identity ON "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (type(32), scopeKey(255), canonicalName(255))`,
4271
4291
  `CREATE INDEX idx_knowledge_nodes_scope ON "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (scopeKey(255), type(32))`,
4292
+ `CREATE INDEX idx_knowledge_nodes_name ON "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" (type(32), canonicalName(255))`,
4272
4293
  `CREATE INDEX idx_knowledge_records_node_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (node(191), id(26) DESC)`,
4273
4294
  `CREATE INDEX idx_knowledge_records_thread_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (sourceThreadId(191), id(26) DESC)`,
4274
4295
  `CREATE INDEX idx_knowledge_mentions_record ON "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" (recordId(191), sourceType(32), sourceId(191))`,
4275
4296
  `CREATE INDEX idx_knowledge_activity_latest ON "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" (id(26) DESC)`,
4297
+ `CREATE INDEX idx_knowledge_records_scope ON "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (scopeKey(255), id(26))`,
4298
+ `CREATE INDEX idx_knowledge_activity_scope ON "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" (scopeKey(255), id(26) DESC)`,
4276
4299
  `CREATE UNIQUE INDEX idx_knowledge_outbox_idempotency ON "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (idempotencyKey(255))`,
4277
4300
  `CREATE INDEX idx_knowledge_outbox_claim ON "${_mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX}" (status(32), availableAt, createdAt)`
4278
4301
  ];
4302
+ const KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE = "Knowledge curation cursors were removed: observation-time curate is the only Knowledge writer and needs no cursor.";
4279
4303
  var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4280
4304
  static getExportDDL() {
4281
4305
  return [
@@ -4296,11 +4320,6 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4296
4320
  "recordId"
4297
4321
  ]
4298
4322
  }),
4299
- generateTableSQL({
4300
- tableName: _mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
4301
- schema: _mastra_core_storage.KNOWLEDGE_CURSORS_SCHEMA,
4302
- compositePrimaryKey: ["sourceThreadId", "agent"]
4303
- }),
4304
4323
  generateTableSQL({
4305
4324
  tableName: _mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
4306
4325
  schema: _mastra_core_storage.KNOWLEDGE_ACTIVITY_SCHEMA
@@ -4339,10 +4358,6 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4339
4358
  tableName: _mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS,
4340
4359
  schema: _mastra_core_storage.KNOWLEDGE_MENTIONS_SCHEMA
4341
4360
  });
4342
- await this.#operations.createTable({
4343
- tableName: _mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
4344
- schema: _mastra_core_storage.KNOWLEDGE_CURSORS_SCHEMA
4345
- });
4346
4361
  await this.#operations.createTable({
4347
4362
  tableName: _mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
4348
4363
  schema: _mastra_core_storage.KNOWLEDGE_ACTIVITY_SCHEMA
@@ -4363,7 +4378,6 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4363
4378
  _mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS,
4364
4379
  _mastra_core_storage.TABLE_KNOWLEDGE_RECORDS,
4365
4380
  _mastra_core_storage.TABLE_KNOWLEDGE_NODES,
4366
- _mastra_core_storage.TABLE_KNOWLEDGE_CURSORS,
4367
4381
  _mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY,
4368
4382
  _mastra_core_storage.TABLE_KNOWLEDGE_SEMANTIC_OUTBOX
4369
4383
  ]) await tx.execute(`DELETE FROM "${table}"`);
@@ -4371,6 +4385,11 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4371
4385
  }
4372
4386
  async createNode(input) {
4373
4387
  await assertKnowledgeDescriptionWithinBoundCompat(input.description);
4388
+ if (input.scopeAddresses?.length) {
4389
+ const error = /* @__PURE__ */ new Error("This Knowledge storage adapter does not expose structural scope placement.");
4390
+ error.name = "KnowledgeUnsupportedCapabilityError";
4391
+ throw error;
4392
+ }
4374
4393
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
4375
4394
  return this.#transaction(async (tx) => {
4376
4395
  const existing = await this.#getNodeByName(tx, input.name, scope);
@@ -4425,13 +4444,13 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4425
4444
  return this.#resolveNode(this.#client, input.name, (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4426
4445
  }
4427
4446
  async listNodes(input) {
4428
- const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4447
+ const keys = visibleScopeKeys((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4429
4448
  const clauses = [
4430
4449
  `type = 'node'`,
4431
4450
  "mergedInto IS NULL",
4432
- visibleSql
4451
+ visibleSql(keys)
4433
4452
  ];
4434
- const args = [key, key];
4453
+ const args = [...keys];
4435
4454
  if (input.namePrefix) {
4436
4455
  clauses.push("canonicalName LIKE ? ESCAPE '='");
4437
4456
  args.push(`${escapeLikePattern(canonicalName(input.namePrefix))}%`);
@@ -4578,10 +4597,10 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4578
4597
  });
4579
4598
  }
4580
4599
  async appendKnowledge(input) {
4600
+ await assertKnowledgeRecordTextWithinBoundCompat(input.text);
4581
4601
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
4582
4602
  const resolutionScope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.resolutionScope);
4583
4603
  const defaultScope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.defaultScope);
4584
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, input.maxScope);
4585
4604
  return this.#transaction(async (tx) => {
4586
4605
  const parent = await this.#resolveTerminalNode(tx, nodeReferenceId(input.node));
4587
4606
  if (!parent) throw new _mastra_core_storage.KnowledgeNotFoundError("node", nodeReferenceId(input.node));
@@ -4593,11 +4612,10 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4593
4612
  sourceThreadId: input.sourceThreadId,
4594
4613
  capturedAt: /* @__PURE__ */ new Date(),
4595
4614
  when: input.when ? new Date(input.when) : void 0,
4596
- maxScope: input.maxScope,
4597
4615
  metadata: input.metadata
4598
4616
  };
4599
4617
  await tx.execute({
4600
- sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (id,node,text,scope,scopeKey,sourceThreadId,capturedAt,"when",maxScope,metadata,deletedAt,deletedBy) VALUES (?,?,?,jsonb(?),?,?,?,?,?,jsonb(?),NULL,NULL)`,
4618
+ sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" (id,node,text,scope,scopeKey,sourceThreadId,capturedAt,"when",metadata,deletedAt,deletedBy) VALUES (?,?,?,jsonb(?),?,?,?,?,jsonb(?),NULL,NULL)`,
4601
4619
  args: [
4602
4620
  record.id,
4603
4621
  record.node,
@@ -4607,7 +4625,6 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4607
4625
  record.sourceThreadId,
4608
4626
  record.capturedAt.toISOString(),
4609
4627
  record.when?.toISOString() ?? null,
4610
- record.maxScope ?? null,
4611
4628
  record.metadata ? JSON.stringify(record.metadata) : null
4612
4629
  ]
4613
4630
  });
@@ -4634,17 +4651,13 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4634
4651
  return this.#queryKnowledge(input, "related");
4635
4652
  }
4636
4653
  async knowledgeBySource(input) {
4637
- const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4638
- const args = [
4639
- input.sourceThreadId,
4640
- key,
4641
- key
4642
- ];
4654
+ const keys = visibleScopeKeys((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4655
+ const args = [input.sourceThreadId, ...keys];
4643
4656
  if (input.after) args.push(input.after);
4644
4657
  const limit = input.limit ?? 100;
4645
4658
  args.push(limit + 1);
4646
4659
  const records = (await this.#client.execute({
4647
- sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE sourceThreadId=? AND ${visibleSql}${input.includeDeleted ? "" : " AND deletedAt IS NULL"}${input.after ? " AND id > ?" : ""} ORDER BY id ASC LIMIT ?`,
4660
+ sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" WHERE sourceThreadId=? AND ${visibleSql(keys)}${input.includeDeleted ? "" : " AND deletedAt IS NULL"}${input.after ? " AND id > ?" : ""} ORDER BY id ASC LIMIT ?`,
4648
4661
  args
4649
4662
  })).rows.map(parseKnowledge);
4650
4663
  return {
@@ -4698,7 +4711,6 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4698
4711
  return this.#transaction(async (tx) => {
4699
4712
  const record = await this.#getKnowledge(tx, input.id, true);
4700
4713
  if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
4701
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(scope, record.maxScope);
4702
4714
  await tx.execute({
4703
4715
  sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET scope=jsonb(?),scopeKey=? WHERE id=?`,
4704
4716
  args: [
@@ -4716,33 +4728,16 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4716
4728
  };
4717
4729
  });
4718
4730
  }
4719
- async raiseKnowledgeCeiling(input) {
4720
- return this.#transaction(async (tx) => {
4721
- const record = await this.#getKnowledge(tx, input.id, true);
4722
- if (!record) throw new _mastra_core_storage.KnowledgeNotFoundError("record", input.id);
4723
- (0, _mastra_core_storage.assertKnowledgeScopeWithinCeiling)(record.scope, input.maxScope);
4724
- (0, _mastra_core_storage.assertKnowledgeCeilingRaised)(record.maxScope, input.maxScope);
4725
- await tx.execute({
4726
- sql: `UPDATE "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" SET maxScope=? WHERE id=?`,
4727
- args: [input.maxScope ?? null, input.id]
4728
- });
4729
- return {
4730
- ...record,
4731
- maxScope: input.maxScope
4732
- };
4733
- });
4734
- }
4735
4731
  async search(input) {
4736
4732
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
4737
- const key = (0, _mastra_core_storage.knowledgeScopeKey)(scope);
4733
+ const keys = visibleScopeKeys(scope);
4738
4734
  const normalizedQuery = input.query.trim().toLocaleLowerCase();
4739
4735
  if (!normalizedQuery) return [];
4740
4736
  const query = `%${escapeLikePattern(normalizedQuery)}%`;
4741
4737
  const results = (await this.#client.execute({
4742
- sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE mergedInto IS NULL AND ${visibleSql} AND (canonicalName LIKE ? ESCAPE '=' OR lower(COALESCE(kind,'')) LIKE ? ESCAPE '=' OR lower(COALESCE(content,'')) LIKE ? ESCAPE '=' OR lower(COALESCE(description,'')) LIKE ? ESCAPE '=') ORDER BY updatedAt DESC LIMIT ?`,
4738
+ sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE mergedInto IS NULL AND ${visibleSql(keys)} AND (canonicalName LIKE ? ESCAPE '=' OR lower(COALESCE(kind,'')) LIKE ? ESCAPE '=' OR lower(COALESCE(content,'')) LIKE ? ESCAPE '=' OR lower(COALESCE(description,'')) LIKE ? ESCAPE '=') ORDER BY updatedAt DESC LIMIT ?`,
4743
4739
  args: [
4744
- key,
4745
- key,
4740
+ ...keys,
4746
4741
  query,
4747
4742
  query,
4748
4743
  query,
@@ -4763,10 +4758,9 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4763
4758
  }));
4764
4759
  if (results.length < (input.limit ?? 20)) {
4765
4760
  const records = await this.#client.execute({
4766
- sql: `SELECT f.*,f.scope AS scopeJson,r.name,r.scope AS parentScopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" f JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" r ON r.id=f.node AND r.type='node' AND r.mergedInto IS NULL WHERE f.deletedAt IS NULL AND ${visibleSql.replaceAll("scopeKey", "f.scopeKey")} AND lower(f.text) LIKE ? ESCAPE '=' ORDER BY f.id DESC LIMIT ?`,
4761
+ sql: `SELECT f.*,f.scope AS scopeJson,r.name,r.scope AS parentScopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" f JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" r ON r.id=f.node AND r.type='node' AND r.mergedInto IS NULL WHERE f.deletedAt IS NULL AND ${visibleSql(keys, "f.scopeKey")} AND lower(f.text) LIKE ? ESCAPE '=' ORDER BY f.id DESC LIMIT ?`,
4767
4762
  args: [
4768
- key,
4769
- key,
4763
+ ...keys,
4770
4764
  query,
4771
4765
  (input.limit ?? 20) - results.length
4772
4766
  ]
@@ -4776,7 +4770,7 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4776
4770
  return {
4777
4771
  type: "record",
4778
4772
  id: String(row.id),
4779
- recordId: String(row.node),
4773
+ recordId: parentVisible ? String(row.node) : String(row.id),
4780
4774
  name: parentVisible ? String(row.name) : "(private node)",
4781
4775
  text: String(row.text),
4782
4776
  scope: parseJson$1(row.scopeJson)
@@ -4785,41 +4779,26 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4785
4779
  }
4786
4780
  return results;
4787
4781
  }
4788
- async getCurationCursor(input) {
4789
- const row = (await this.#client.execute({
4790
- sql: `SELECT * FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS}" WHERE sourceThreadId=? AND agent=?`,
4791
- args: [input.sourceThreadId, input.agent]
4792
- })).rows[0];
4793
- return row ? {
4794
- sourceThreadId: String(row.sourceThreadId),
4795
- agent: String(row.agent),
4796
- lastKnowledgeId: String(row.lastKnowledgeId),
4797
- updatedAt: toDate(row.updatedAt)
4798
- } : null;
4799
- }
4800
- async advanceCurationCursor(input) {
4801
- const updatedAt = /* @__PURE__ */ new Date();
4802
- if ((await this.#client.execute({
4803
- sql: `INSERT INTO "${_mastra_core_storage.TABLE_KNOWLEDGE_CURSORS}" (sourceThreadId,agent,lastKnowledgeId,updatedAt) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE lastKnowledgeId=IF(VALUES(lastKnowledgeId) >= lastKnowledgeId, VALUES(lastKnowledgeId), lastKnowledgeId),updatedAt=IF(VALUES(lastKnowledgeId) >= lastKnowledgeId, VALUES(updatedAt), updatedAt)`,
4804
- args: [
4805
- input.sourceThreadId,
4806
- input.agent,
4807
- input.lastKnowledgeId,
4808
- updatedAt.toISOString()
4809
- ]
4810
- })).rowsAffected === 0) throw new Error("Knowledge curation cursor cannot move backwards");
4811
- return {
4812
- ...input,
4813
- updatedAt
4814
- };
4782
+ /**
4783
+ * @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
4784
+ * cursor. Always throws.
4785
+ */
4786
+ async getCurationCursor(_input) {
4787
+ throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
4788
+ }
4789
+ /**
4790
+ * @deprecated Curation cursors were removed. Observation-time curate is the only Knowledge writer and needs no
4791
+ * cursor. Always throws.
4792
+ */
4793
+ async advanceCurationCursor(_input) {
4794
+ throw new Error(KNOWLEDGE_CURATION_CURSOR_REMOVED_MESSAGE);
4815
4795
  }
4816
4796
  async listActivity(input) {
4817
- const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4797
+ const keys = visibleScopeKeys((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4818
4798
  return (await this.#client.execute({
4819
- sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" WHERE ${visibleSql}${input.after ? " AND id < ?" : ""} ORDER BY id DESC LIMIT ?`,
4799
+ sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_ACTIVITY}" WHERE ${visibleSql(keys)}${input.after ? " AND id < ?" : ""} ORDER BY id DESC LIMIT ?`,
4820
4800
  args: [
4821
- key,
4822
- key,
4801
+ ...keys,
4823
4802
  ...input.after ? [input.after] : [],
4824
4803
  input.limit ?? 100
4825
4804
  ]
@@ -4841,9 +4820,9 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4841
4820
  args.push(input.status);
4842
4821
  }
4843
4822
  if (input.scope) {
4844
- const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4845
- clauses.push(visibleSql);
4846
- args.push(key, key);
4823
+ const keys = visibleScopeKeys(input.scope);
4824
+ clauses.push(visibleSql(keys));
4825
+ args.push(...keys);
4847
4826
  }
4848
4827
  args.push(input.limit ?? 100);
4849
4828
  return (await this.#client.execute({
@@ -4862,9 +4841,9 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4862
4841
  ];
4863
4842
  const args = [now.toISOString(), stale.toISOString()];
4864
4843
  if (input.scope) {
4865
- const key = (0, _mastra_core_storage.knowledgeScopeKey)((0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope));
4866
- clauses.push(visibleSql);
4867
- args.push(key, key);
4844
+ const keys = visibleScopeKeys(input.scope);
4845
+ clauses.push(visibleSql(keys));
4846
+ args.push(...keys);
4868
4847
  }
4869
4848
  args.push(input.limit ?? 100);
4870
4849
  const ids = (await tx.execute({
@@ -4942,12 +4921,14 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4942
4921
  return result.rows[0] ? parseNode(result.rows[0]) : null;
4943
4922
  }
4944
4923
  async #resolveNode(executor, name, scope) {
4945
- for (let length = scope.length; length > 0; length--) {
4946
- const node = await this.#getNodeByName(executor, name, scope.slice(0, length));
4947
- if (node) {
4948
- const terminal = await this.#resolveTerminalNode(executor, node.id);
4949
- if (terminal && (0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) return terminal;
4950
- }
4924
+ const keys = visibleScopeKeys(scope);
4925
+ const candidates = (await executor.execute({
4926
+ sql: `SELECT *,scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_NODES}" WHERE type='node' AND canonicalName=? AND (scopeKey IN (${keys.map(() => "?").join(",")}) OR mergedInto IS NOT NULL)`,
4927
+ args: [canonicalName(name), ...keys]
4928
+ })).rows.map(parseNode).sort((left, right) => right.scope.length - left.scope.length);
4929
+ for (const candidate of candidates) {
4930
+ const terminal = await this.#resolveTerminalNode(executor, candidate.id);
4931
+ if (terminal && (0, _mastra_core_storage.isKnowledgeScopeVisible)(terminal.scope, scope)) return terminal;
4951
4932
  }
4952
4933
  return null;
4953
4934
  }
@@ -4972,17 +4953,16 @@ var KnowledgeMySQL = class extends _mastra_core_storage.KnowledgeStorage {
4972
4953
  const scope = (0, _mastra_core_storage.canonicalizeKnowledgeScope)(input.scope);
4973
4954
  const node = await this.#resolveTerminalNode(this.#client, nodeReferenceId(input.node));
4974
4955
  if (!node) return { records: [] };
4975
- const key = (0, _mastra_core_storage.knowledgeScopeKey)(scope);
4956
+ const keys = visibleScopeKeys(scope);
4976
4957
  const args = [
4977
4958
  node.id,
4978
4959
  ...relationship === "related" ? [node.id] : [],
4979
- key,
4980
- key
4960
+ ...keys
4981
4961
  ];
4982
4962
  if (input.after) args.push(input.after);
4983
4963
  args.push((input.limit ?? 100) + 1);
4984
4964
  const records = (await this.#client.execute({
4985
- sql: `SELECT DISTINCT f.*,f.scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" f${relationship === "about" ? "" : ` LEFT JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" m ON m.sourceType='record' AND m.sourceId=f.id`} WHERE ${relationship === "about" ? "f.node=?" : relationship === "mentioning" ? "m.recordId=?" : "(f.node=? OR m.recordId=?)"} AND ${visibleSql.replaceAll("scopeKey", "f.scopeKey")}${input.includeDeleted ? "" : " AND f.deletedAt IS NULL"}${input.after ? " AND f.id < ?" : ""} ORDER BY f.id DESC LIMIT ?`,
4965
+ sql: `SELECT DISTINCT f.*,f.scope AS scopeJson FROM "${_mastra_core_storage.TABLE_KNOWLEDGE_RECORDS}" f${relationship === "about" ? "" : ` LEFT JOIN "${_mastra_core_storage.TABLE_KNOWLEDGE_MENTIONS}" m ON m.sourceType='record' AND m.sourceId=f.id`} WHERE ${relationship === "about" ? "f.node=?" : relationship === "mentioning" ? "m.recordId=?" : "(f.node=? OR m.recordId=?)"} AND ${visibleSql(keys, "f.scopeKey")}${input.includeDeleted ? "" : " AND f.deletedAt IS NULL"}${input.after ? " AND f.id < ?" : ""} ORDER BY f.id DESC LIMIT ?`,
4986
4966
  args
4987
4967
  })).rows.map(parseKnowledge);
4988
4968
  const limit = input.limit ?? 100;
@@ -10982,7 +10962,7 @@ var WorkflowsMySQL = class WorkflowsMySQL extends _mastra_core_storage.Workflows
10982
10962
  }, error);
10983
10963
  }
10984
10964
  }
10985
- async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext }) {
10965
+ async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext, state }) {
10986
10966
  const connection = await this.pool.getConnection();
10987
10967
  try {
10988
10968
  await connection.beginTransaction();
@@ -11002,6 +10982,7 @@ var WorkflowsMySQL = class WorkflowsMySQL extends _mastra_core_storage.Workflows
11002
10982
  const currentSnapshot = parseSnapshot(rows[0].snapshot);
11003
10983
  const context = { ...currentSnapshot.context ?? {} };
11004
10984
  context[stepId] = result;
10985
+ if (state !== void 0) context.__state = state;
11005
10986
  const updatedSnapshot = {
11006
10987
  ...currentSnapshot,
11007
10988
  context,