@mastra/core 1.87.0-alpha.12 → 1.87.0-alpha.14

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 (584) hide show
  1. package/dist/agent/active-run-signal-queues.d.ts +49 -0
  2. package/dist/agent/agent.d.ts +17 -1
  3. package/dist/agent/durable/abort-transport.d.ts +1 -1
  4. package/dist/agent/durable/durable-agent.d.ts +1 -0
  5. package/dist/agent/durable/execution-engine.d.ts +12 -0
  6. package/dist/agent/durable/index.cjs +1 -1
  7. package/dist/agent/durable/index.js +1 -1
  8. package/dist/agent/durable/preparation.d.ts +17 -0
  9. package/dist/agent/durable/stored-signal-drain.d.ts +100 -0
  10. package/dist/agent/durable/types.d.ts +37 -0
  11. package/dist/agent/durable/utils/resolve-runtime.d.ts +11 -0
  12. package/dist/agent/durable/utils/serialize-state.d.ts +1 -0
  13. package/dist/agent/durable/workflows/create-durable-agentic-workflow.d.ts +1 -0
  14. package/dist/agent/durable/workflows/durable-loop-builder.d.ts +5 -0
  15. package/dist/agent/durable/workflows/finalize-run.d.ts +6 -0
  16. package/dist/agent/durable/workflows/shared/message-list-state.d.ts +6 -2
  17. package/dist/agent/durable/workflows/shared/schemas.d.ts +2 -0
  18. package/dist/agent/durable/workflows/steps/llm-execution.d.ts +1 -0
  19. package/dist/agent/index.cjs +2 -2
  20. package/dist/agent/index.js +2 -2
  21. package/dist/agent/signals.d.ts +7 -3
  22. package/dist/agent/stored-run-signals.d.ts +93 -0
  23. package/dist/agent/stored-signals.d.ts +40 -0
  24. package/dist/agent/streams/abort-transport.d.ts +10 -1
  25. package/dist/agent/streams/constants.d.ts +9 -0
  26. package/dist/agent/streams/publisher.d.ts +3 -1
  27. package/dist/agent/thread-stream-runtime.d.ts +46 -4
  28. package/dist/agent/tool-name.d.ts +21 -0
  29. package/dist/agent-builder/ee/index.cjs +1 -1
  30. package/dist/agent-builder/ee/index.js +1 -1
  31. package/dist/agent-controller/index.cjs +1 -1
  32. package/dist/agent-controller/index.js +1 -1
  33. package/dist/agent-controller/types.d.ts +6 -0
  34. package/dist/{agent-controller-DfeTRuBc.js → agent-controller-FSjSZ_iD.js} +11 -9
  35. package/dist/{agent-controller-BcN7r3yI.cjs → agent-controller-k4B293tu.cjs} +11 -9
  36. package/dist/background-tasks/index.cjs +1 -1
  37. package/dist/background-tasks/index.js +1 -1
  38. package/dist/background-tasks/manager.d.ts +3 -1
  39. package/dist/background-tasks/workflow-id.d.ts +42 -0
  40. package/dist/{background-tasks-BcU--rXL.js → background-tasks-ClI6oF60.js} +239 -28
  41. package/dist/{background-tasks-DRUHnQOk.cjs → background-tasks-D40Y_iLc.cjs} +262 -27
  42. package/dist/browser/index.cjs +2 -2
  43. package/dist/browser/index.js +2 -2
  44. package/dist/{builder-Bufqrcz3.cjs → builder-C_I6l0US.cjs} +2 -2
  45. package/dist/{builder-TuIc5l7X.js → builder-D00XMm41.js} +2 -2
  46. package/dist/capabilities/302ai.json +197 -197
  47. package/dist/capabilities/abliteratedllm.json +33 -33
  48. package/dist/capabilities/abliteration-ai.json +10 -10
  49. package/dist/capabilities/above.json +22 -22
  50. package/dist/capabilities/adaption.json +7 -7
  51. package/dist/capabilities/adzuki.json +31 -31
  52. package/dist/capabilities/agentrouter.json +4 -4
  53. package/dist/capabilities/agnes.json +4 -4
  54. package/dist/capabilities/ai-router.json +6 -6
  55. package/dist/capabilities/aiand.json +22 -22
  56. package/dist/capabilities/aigrow.json +11 -11
  57. package/dist/capabilities/aimlapi.json +146 -146
  58. package/dist/capabilities/aipg.json +2 -2
  59. package/dist/capabilities/aixy.json +20 -20
  60. package/dist/capabilities/aki-io.json +13 -13
  61. package/dist/capabilities/alibaba-cn.json +126 -90
  62. package/dist/capabilities/alibaba-coding-plan-cn.json +6 -11
  63. package/dist/capabilities/alibaba-coding-plan.json +6 -11
  64. package/dist/capabilities/alibaba-token-plan-cn.json +49 -49
  65. package/dist/capabilities/alibaba-token-plan.json +54 -54
  66. package/dist/capabilities/alibaba.json +87 -61
  67. package/dist/capabilities/ambient.json +14 -14
  68. package/dist/capabilities/amd.json +8 -8
  69. package/dist/capabilities/anthropic.json +42 -42
  70. package/dist/capabilities/anyapi.json +16 -16
  71. package/dist/capabilities/apiko.json +5 -5
  72. package/dist/capabilities/arcee.json +11 -11
  73. package/dist/capabilities/auriko.json +15 -15
  74. package/dist/capabilities/bailing.json +5 -5
  75. package/dist/capabilities/baseten.json +35 -35
  76. package/dist/capabilities/bee.json +3 -3
  77. package/dist/capabilities/berget.json +8 -8
  78. package/dist/capabilities/bothub.json +16 -16
  79. package/dist/capabilities/boundless.json +34 -34
  80. package/dist/capabilities/bumo.json +37 -37
  81. package/dist/capabilities/byteplus.json +29 -29
  82. package/dist/capabilities/cerebras.json +4 -4
  83. package/dist/capabilities/cheapaimodel.json +10 -10
  84. package/dist/capabilities/cheapest-token.json +29 -29
  85. package/dist/capabilities/chutes.json +58 -10
  86. package/dist/capabilities/cline-pass.json +38 -38
  87. package/dist/capabilities/cloudcode.json +6 -6
  88. package/dist/capabilities/cloudferro-sherlock.json +4 -4
  89. package/dist/capabilities/cloudflare-workers-ai.json +16 -16
  90. package/dist/capabilities/commandcode.json +147 -147
  91. package/dist/capabilities/concentrate.json +277 -277
  92. package/dist/capabilities/cortecs.json +102 -102
  93. package/dist/capabilities/crossmodel.json +183 -183
  94. package/dist/capabilities/crusoe.json +17 -17
  95. package/dist/capabilities/daoxe.json +8 -8
  96. package/dist/capabilities/databricks.json +124 -124
  97. package/dist/capabilities/deepinfra.json +101 -101
  98. package/dist/capabilities/deepseek.json +3 -3
  99. package/dist/capabilities/digitalocean.json +116 -116
  100. package/dist/capabilities/dothoosh.json +32 -32
  101. package/dist/capabilities/doubleword.json +30 -30
  102. package/dist/capabilities/edenai.json +290 -290
  103. package/dist/capabilities/empiriolabs.json +162 -162
  104. package/dist/capabilities/engy.json +19 -19
  105. package/dist/capabilities/entrim.json +8 -8
  106. package/dist/capabilities/evroc.json +13 -13
  107. package/dist/capabilities/ezkielyna.json +12 -12
  108. package/dist/capabilities/fastrouter.json +86 -86
  109. package/dist/capabilities/fireworks-ai.json +99 -99
  110. package/dist/capabilities/fortytwo.json +4 -4
  111. package/dist/capabilities/freemodel.json +14 -14
  112. package/dist/capabilities/friendli.json +12 -12
  113. package/dist/capabilities/frogbot.json +91 -91
  114. package/dist/capabilities/gisoo.json +18 -18
  115. package/dist/capabilities/gmicloud.json +19 -19
  116. package/dist/capabilities/google.json +52 -52
  117. package/dist/capabilities/graphn.json +12 -12
  118. package/dist/capabilities/greenpt.json +34 -34
  119. package/dist/capabilities/groq.json +6 -6
  120. package/dist/capabilities/helicone.json +87 -87
  121. package/dist/capabilities/hetzner.json +4 -4
  122. package/dist/capabilities/hopper.json +2 -2
  123. package/dist/capabilities/hostinger-router.json +13 -20
  124. package/dist/capabilities/hpc-ai.json +10 -10
  125. package/dist/capabilities/huawei-maas.json +11 -11
  126. package/dist/capabilities/hubris.json +28 -28
  127. package/dist/capabilities/huggingface.json +92 -85
  128. package/dist/capabilities/hyper.json +18 -18
  129. package/dist/capabilities/impossibl.json +146 -146
  130. package/dist/capabilities/inception.json +2 -2
  131. package/dist/capabilities/inceptron.json +8 -8
  132. package/dist/capabilities/inco.json +4 -4
  133. package/dist/capabilities/infer.json +14 -14
  134. package/dist/capabilities/inferall.json +6 -6
  135. package/dist/capabilities/inference-coop.json +12 -12
  136. package/dist/capabilities/inferenceapis.json +26 -26
  137. package/dist/capabilities/inferris.json +4 -4
  138. package/dist/capabilities/infersia.json +10 -10
  139. package/dist/capabilities/inferx.json +3 -3
  140. package/dist/capabilities/infomaniak.json +3 -3
  141. package/dist/capabilities/io-net.json +4 -4
  142. package/dist/capabilities/isoquant.json +4 -4
  143. package/dist/capabilities/iteracompute.json +55 -55
  144. package/dist/capabilities/jad-ai.json +2 -2
  145. package/dist/capabilities/jalapeno.json +10 -10
  146. package/dist/capabilities/jiekou.json +58 -58
  147. package/dist/capabilities/joingonka.json +4 -4
  148. package/dist/capabilities/jzstoken.json +51 -51
  149. package/dist/capabilities/keln.json +22 -22
  150. package/dist/capabilities/kenari.json +141 -116
  151. package/dist/capabilities/kilo.json +629 -645
  152. package/dist/capabilities/kimi-code-plan-cn.json +6 -6
  153. package/dist/capabilities/kimi-code-plan-global.json +6 -6
  154. package/dist/capabilities/kitani.json +6 -6
  155. package/dist/capabilities/klavo.json +26 -26
  156. package/dist/capabilities/korouter.json +27 -27
  157. package/dist/capabilities/kunavo.json +9 -9
  158. package/dist/capabilities/liquid.json +12 -12
  159. package/dist/capabilities/lithosai.json +3 -3
  160. package/dist/capabilities/llmgateway-providers.json +920 -928
  161. package/dist/capabilities/llmgateway.json +357 -349
  162. package/dist/capabilities/llmtr.json +32 -32
  163. package/dist/capabilities/logits.json +2 -2
  164. package/dist/capabilities/lowrouter.json +48 -48
  165. package/dist/capabilities/lyceum.json +14 -14
  166. package/dist/capabilities/melious.json +9 -9
  167. package/dist/capabilities/merge-gateway.json +276 -276
  168. package/dist/capabilities/meta.json +10 -10
  169. package/dist/capabilities/minimax-cn-coding-plan.json +5 -5
  170. package/dist/capabilities/minimax-cn.json +5 -5
  171. package/dist/capabilities/minimax-coding-plan.json +5 -5
  172. package/dist/capabilities/minimax.json +5 -5
  173. package/dist/capabilities/mistral.json +19 -19
  174. package/dist/capabilities/mixlayer.json +4 -4
  175. package/dist/capabilities/modal.json +14 -14
  176. package/dist/capabilities/model-oracle-ai.json +26 -26
  177. package/dist/capabilities/modelis.json +17 -17
  178. package/dist/capabilities/modular.json +13 -13
  179. package/dist/capabilities/moneo.json +16 -16
  180. package/dist/capabilities/moonshotai-cn.json +6 -6
  181. package/dist/capabilities/moonshotai.json +6 -6
  182. package/dist/capabilities/morph.json +17 -17
  183. package/dist/capabilities/munito-eu.json +6 -6
  184. package/dist/capabilities/munito.json +10 -10
  185. package/dist/capabilities/nan.json +30 -30
  186. package/dist/capabilities/nano-gpt.json +595 -596
  187. package/dist/capabilities/nearai.json +99 -99
  188. package/dist/capabilities/nebius.json +31 -31
  189. package/dist/capabilities/neon.json +126 -126
  190. package/dist/capabilities/neuralwatt.json +43 -43
  191. package/dist/capabilities/nokeep.json +4 -4
  192. package/dist/capabilities/novita-ai.json +123 -123
  193. package/dist/capabilities/nvidia.json +33 -33
  194. package/dist/capabilities/oathbreaker.json +4 -4
  195. package/dist/capabilities/obscurify.json +29 -29
  196. package/dist/capabilities/oci.json +9 -9
  197. package/dist/capabilities/ofox.json +243 -607
  198. package/dist/capabilities/ollama-cloud.json +55 -55
  199. package/dist/capabilities/openai.json +55 -55
  200. package/dist/capabilities/opencode-go.json +96 -94
  201. package/dist/capabilities/openreason.json +4 -4
  202. package/dist/capabilities/openrouter.json +529 -543
  203. package/dist/capabilities/opper.json +29 -29
  204. package/dist/capabilities/orcarouter.json +436 -178
  205. package/dist/capabilities/outerlabs.json +28 -28
  206. package/dist/capabilities/ovhcloud.json +21 -22
  207. package/dist/capabilities/pareto.json +6 -6
  208. package/dist/capabilities/perplexity-agent.json +63 -63
  209. package/dist/capabilities/phala.json +30 -30
  210. package/dist/capabilities/pioneer.json +23 -23
  211. package/dist/capabilities/poe.json +140 -140
  212. package/dist/capabilities/poolside.json +3 -3
  213. package/dist/capabilities/powerspring.json +14 -14
  214. package/dist/capabilities/primalabs.json +7 -7
  215. package/dist/capabilities/prime.json +119 -119
  216. package/dist/capabilities/privatemode-ai.json +10 -10
  217. package/dist/capabilities/proxystacks.json +7 -7
  218. package/dist/capabilities/qiniu-ai.json +15 -15
  219. package/dist/capabilities/refuseless.json +5 -5
  220. package/dist/capabilities/regolo-ai.json +8 -8
  221. package/dist/capabilities/requesty.json +204 -204
  222. package/dist/capabilities/ruapi.json +24 -24
  223. package/dist/capabilities/runcrate.json +15 -15
  224. package/dist/capabilities/runinfra.json +16 -16
  225. package/dist/capabilities/sakana.json +5 -5
  226. package/dist/capabilities/sambanova.json +7 -7
  227. package/dist/capabilities/sandbase.json +21 -21
  228. package/dist/capabilities/sarvam.json +11 -11
  229. package/dist/capabilities/scaleway.json +15 -15
  230. package/dist/capabilities/scnet-token-plan.json +14 -14
  231. package/dist/capabilities/scx-ai.json +26 -26
  232. package/dist/capabilities/sensenova.json +12 -12
  233. package/dist/capabilities/siliconflow-cn.json +54 -54
  234. package/dist/capabilities/siliconflow.json +66 -66
  235. package/dist/capabilities/snowflake-cortex.json +22 -22
  236. package/dist/capabilities/sokkan.json +7 -7
  237. package/dist/capabilities/sparrowcodes.json +23 -23
  238. package/dist/capabilities/standardcompute.json +96 -96
  239. package/dist/capabilities/steadyrouter.json +38 -38
  240. package/dist/capabilities/stepfun-ai-step-plan.json +15 -9
  241. package/dist/capabilities/stepfun-ai.json +22 -8
  242. package/dist/capabilities/stepfun-step-plan.json +15 -8
  243. package/dist/capabilities/stepfun.json +22 -8
  244. package/dist/capabilities/stepping-stone.json +25 -25
  245. package/dist/capabilities/synthetic.json +10 -10
  246. package/dist/capabilities/synthorai.json +24 -24
  247. package/dist/capabilities/teamorouter.json +18 -18
  248. package/dist/capabilities/teapot-router.json +12 -12
  249. package/dist/capabilities/tencent-token-plan-intl.json +20 -20
  250. package/dist/capabilities/tencent-token-plan.json +38 -38
  251. package/dist/capabilities/tencent-tokenhub.json +46 -46
  252. package/dist/capabilities/tensorx.json +52 -52
  253. package/dist/capabilities/tenstorrent.json +8 -8
  254. package/dist/capabilities/the-grid-ai.json +35 -35
  255. package/dist/capabilities/thinkingmachines.json +78 -78
  256. package/dist/capabilities/tinfoil.json +5 -5
  257. package/dist/capabilities/toapis.json +2 -2
  258. package/dist/capabilities/togetherai.json +53 -61
  259. package/dist/capabilities/tokengo.json +23 -23
  260. package/dist/capabilities/tokenify.json +9 -9
  261. package/dist/capabilities/trustedrouter.json +5 -5
  262. package/dist/capabilities/trustedtokens.json +19 -19
  263. package/dist/capabilities/umans-ai-coding-plan.json +15 -15
  264. package/dist/capabilities/umans-ai.json +11 -11
  265. package/dist/capabilities/unrestricted.json +8 -8
  266. package/dist/capabilities/uplinkswitch.json +22 -22
  267. package/dist/capabilities/upstage.json +4 -4
  268. package/dist/capabilities/vancine.json +19 -19
  269. package/dist/capabilities/vercel.json +520 -513
  270. package/dist/capabilities/verda.json +8 -1
  271. package/dist/capabilities/vivgrid.json +81 -81
  272. package/dist/capabilities/volcengine-coding-plan.json +29 -29
  273. package/dist/capabilities/volcengine.json +21 -21
  274. package/dist/capabilities/vultr.json +26 -26
  275. package/dist/capabilities/wafer.ai.json +13 -13
  276. package/dist/capabilities/wallaby.json +4 -4
  277. package/dist/capabilities/wally.json +13 -13
  278. package/dist/capabilities/wandb.json +17 -17
  279. package/dist/capabilities/watt.json +24 -24
  280. package/dist/capabilities/xai.json +4 -4
  281. package/dist/capabilities/xiaomi-token-plan-ams.json +2 -2
  282. package/dist/capabilities/xiaomi-token-plan-cn.json +2 -2
  283. package/dist/capabilities/xiaomi-token-plan-sgp.json +2 -2
  284. package/dist/capabilities/xiaomi.json +2 -2
  285. package/dist/capabilities/xpersona.json +10 -10
  286. package/dist/capabilities/xxapi.json +28 -28
  287. package/dist/capabilities/zai-coding-plan.json +13 -13
  288. package/dist/capabilities/zai.json +28 -28
  289. package/dist/capabilities/zenifra.json +29 -29
  290. package/dist/capabilities/zenmux.json +178 -178
  291. package/dist/capabilities/zerogpu.json +15 -15
  292. package/dist/capabilities/zhipuai-coding-plan.json +6 -6
  293. package/dist/capabilities/zhipuai.json +25 -25
  294. package/dist/capabilities/zro.json +5 -5
  295. package/dist/channels/index.cjs +2 -2
  296. package/dist/channels/index.js +2 -2
  297. package/dist/{channels-MsOnUvU6.cjs → channels-BU5_kAUO.cjs} +1 -1
  298. package/dist/{channels-Cdsqdajh.js → channels-DMSae7xD.js} +1 -1
  299. package/dist/coding-agent/index.cjs +3 -3
  300. package/dist/coding-agent/index.js +3 -3
  301. package/dist/{constants-Bou0hEgY.cjs → constants-CFO8w780.cjs} +8 -0
  302. package/dist/{constants-CFWgIaqA.js → constants-CUv0mU2y.js} +8 -0
  303. package/dist/datasets/index.cjs +2 -2
  304. package/dist/datasets/index.js +2 -2
  305. package/dist/docs/SKILL.md +2 -1
  306. package/dist/docs/assets/SOURCE_MAP.json +1044 -1009
  307. package/dist/docs/references/docs-agents-code-mode.md +2 -2
  308. package/dist/docs/references/docs-agents-human-in-the-loop.md +2 -2
  309. package/dist/docs/references/docs-agents-networks.md +1 -1
  310. package/dist/docs/references/docs-agents-overview.md +6 -6
  311. package/dist/docs/references/docs-agents-processors.md +9 -9
  312. package/dist/docs/references/docs-agents-resumable-streams.md +2 -2
  313. package/dist/docs/references/docs-agents-structured-output.md +6 -6
  314. package/dist/docs/references/docs-agents-tools.md +9 -9
  315. package/dist/docs/references/docs-browser.md +1 -1
  316. package/dist/docs/references/docs-channels.md +5 -5
  317. package/dist/docs/references/docs-classifier.md +1 -1
  318. package/dist/docs/references/docs-connections-a2a.md +1 -1
  319. package/dist/docs/references/docs-connections-acp.md +2 -2
  320. package/dist/docs/references/docs-connections-overview.md +1 -1
  321. package/dist/docs/references/docs-connections-sdk-agents.md +6 -6
  322. package/dist/docs/references/docs-deployment-workers.md +1 -1
  323. package/dist/docs/references/docs-develop.md +1 -1
  324. package/dist/docs/references/docs-evals-custom-scorers.md +1 -1
  325. package/dist/docs/references/docs-evals-multi-turn.md +2 -2
  326. package/dist/docs/references/docs-guides-build-an-eval-loop.md +2 -2
  327. package/dist/docs/references/docs-guides-context-engineering.md +3 -3
  328. package/dist/docs/references/docs-guides-streaming.md +1 -1
  329. package/dist/docs/references/docs-harness-agent-controller.md +6 -3
  330. package/dist/docs/references/docs-harness-background-tasks.md +2 -2
  331. package/dist/docs/references/docs-harness-durable-agents.md +5 -5
  332. package/dist/docs/references/docs-harness-goals.md +2 -2
  333. package/dist/docs/references/docs-harness-schedules.md +35 -1
  334. package/dist/docs/references/docs-knowledge-agents-and-tools.md +2 -2
  335. package/dist/docs/references/docs-knowledge-importers.md +22 -1
  336. package/dist/docs/references/docs-knowledge-overview.md +1 -1
  337. package/dist/docs/references/docs-knowledge-scopes-and-access.md +1 -1
  338. package/dist/docs/references/docs-memory-memory-processors.md +8 -8
  339. package/dist/docs/references/docs-memory-message-history.md +3 -3
  340. package/dist/docs/references/docs-memory-observational-memory.md +1 -1
  341. package/dist/docs/references/docs-memory-semantic-recall.md +2 -2
  342. package/dist/docs/references/docs-memory-working-memory.md +3 -2
  343. package/dist/docs/references/docs-sandbox-overview.md +2 -2
  344. package/dist/docs/references/docs-sandbox-skills.md +1 -1
  345. package/dist/docs/references/docs-server-mastra-client.md +1 -1
  346. package/dist/docs/references/docs-server-mcp.md +23 -0
  347. package/dist/docs/references/docs-server-pubsub.md +21 -1
  348. package/dist/docs/references/docs-server-request-context.md +4 -4
  349. package/dist/docs/references/docs-signals-notifications.md +2 -2
  350. package/dist/docs/references/docs-signals-overview.md +1 -1
  351. package/dist/docs/references/docs-signals-running-across-processes.md +3 -0
  352. package/dist/docs/references/docs-signals-sending-signals.md +7 -7
  353. package/dist/docs/references/docs-signals-signal-providers.md +18 -2
  354. package/dist/docs/references/docs-signals-state-signals.md +1 -0
  355. package/dist/docs/references/docs-signals-thread-streams.md +12 -0
  356. package/dist/docs/references/docs-skills.md +4 -4
  357. package/dist/docs/references/docs-storage.md +12 -12
  358. package/dist/docs/references/docs-studio-editor.md +2 -2
  359. package/dist/docs/references/docs-subagents.md +26 -3
  360. package/dist/docs/references/integrations-agentic-ui-copilotkit.md +1 -1
  361. package/dist/docs/references/integrations-agentic-ui-openui.md +1 -1
  362. package/dist/docs/references/integrations-channels-discord.md +1 -1
  363. package/dist/docs/references/integrations-channels-github.md +1 -1
  364. package/dist/docs/references/integrations-channels-imessage.md +2 -2
  365. package/dist/docs/references/integrations-channels-ring.md +2 -2
  366. package/dist/docs/references/integrations-channels-slack.md +1 -1
  367. package/dist/docs/references/integrations-channels-teams.md +1 -1
  368. package/dist/docs/references/integrations-channels-telegram.md +1 -1
  369. package/dist/docs/references/integrations-channels-whatsapp.md +1 -1
  370. package/dist/docs/references/integrations-tools-firecrawl.md +1 -1
  371. package/dist/docs/references/reference-acp-acp-agent.md +1 -1
  372. package/dist/docs/references/reference-acp-create-acp-tool.md +1 -1
  373. package/dist/docs/references/reference-agent-controller-agent-controller-class.md +3 -1
  374. package/dist/docs/references/reference-agent-controller-session.md +5 -5
  375. package/dist/docs/references/reference-agents-agent.md +40 -14
  376. package/dist/docs/references/reference-agents-channels.md +11 -11
  377. package/dist/docs/references/reference-agents-durable-agent.md +2 -2
  378. package/dist/docs/references/reference-agents-getLLM.md +2 -2
  379. package/dist/docs/references/reference-agents-getMetadata.md +2 -2
  380. package/dist/docs/references/reference-agents-network.md +2 -2
  381. package/dist/docs/references/reference-browser-mastra-browser.md +1 -1
  382. package/dist/docs/references/reference-channels-thread-link.md +1 -1
  383. package/dist/docs/references/reference-classifier-classifier.md +2 -0
  384. package/dist/docs/references/reference-coding-agent-build-base-prompt.md +1 -1
  385. package/dist/docs/references/reference-coding-agent-create-coding-agent.md +7 -7
  386. package/dist/docs/references/reference-configuration.md +15 -14
  387. package/dist/docs/references/reference-core-getAgentById.md +1 -1
  388. package/dist/docs/references/reference-core-mastra-model-gateway.md +1 -1
  389. package/dist/docs/references/reference-datasets-startExperiment.md +1 -1
  390. package/dist/docs/references/reference-editor-mastra-editor.md +2 -2
  391. package/dist/docs/references/reference-file-based-agents-config.md +3 -3
  392. package/dist/docs/references/reference-file-based-agents-schedules.md +1 -1
  393. package/dist/docs/references/reference-file-based-agents-subagents.md +3 -3
  394. package/dist/docs/references/reference-file-based-agents-workspace.md +1 -1
  395. package/dist/docs/references/reference-knowledge-knowledge-class.md +12 -0
  396. package/dist/docs/references/reference-manual-install.md +1 -1
  397. package/dist/docs/references/reference-memory-memory-class.md +3 -3
  398. package/dist/docs/references/reference-memory-observational-memory.md +3 -3
  399. package/dist/docs/references/reference-observability-tracing-bridges-datadog.md +12 -11
  400. package/dist/docs/references/reference-observability-tracing-configuration.md +2 -2
  401. package/dist/docs/references/reference-observability-tracing-exporters-datadog.md +14 -12
  402. package/dist/docs/references/reference-processors-batch-parts-processor.md +1 -1
  403. package/dist/docs/references/reference-processors-classifier-processor.md +2 -2
  404. package/dist/docs/references/reference-processors-cyber-refusal-handler.md +1 -1
  405. package/dist/docs/references/reference-processors-language-detector.md +1 -1
  406. package/dist/docs/references/reference-processors-message-history-processor.md +1 -1
  407. package/dist/docs/references/reference-processors-model-selection-processor.md +3 -3
  408. package/dist/docs/references/reference-processors-moderation-processor.md +4 -4
  409. package/dist/docs/references/reference-processors-pii-detector.md +2 -2
  410. package/dist/docs/references/reference-processors-prefill-error-handler.md +2 -2
  411. package/dist/docs/references/reference-processors-processor-interface.md +19 -8
  412. package/dist/docs/references/reference-processors-prompt-injection-detector.md +1 -1
  413. package/dist/docs/references/reference-processors-provider-history-compat.md +1 -1
  414. package/dist/docs/references/reference-processors-response-cache.md +1 -1
  415. package/dist/docs/references/reference-processors-semantic-recall-processor.md +1 -1
  416. package/dist/docs/references/reference-processors-skill-search-processor.md +1 -1
  417. package/dist/docs/references/reference-processors-skills-processor.md +1 -1
  418. package/dist/docs/references/reference-processors-stream-error-retry-processor.md +3 -3
  419. package/dist/docs/references/reference-processors-system-prompt-scrubber.md +1 -1
  420. package/dist/docs/references/reference-processors-token-limiter-processor.md +3 -3
  421. package/dist/docs/references/reference-processors-tool-call-filter.md +2 -2
  422. package/dist/docs/references/reference-processors-tool-search-processor.md +3 -3
  423. package/dist/docs/references/reference-processors-unicode-normalizer.md +1 -1
  424. package/dist/docs/references/reference-processors-working-memory-processor.md +1 -1
  425. package/dist/docs/references/reference-rag-chunking-and-embedding.md +1 -1
  426. package/dist/docs/references/reference-rag-graph-rag-guide.md +2 -2
  427. package/dist/docs/references/reference-rag-retrieval.md +13 -13
  428. package/dist/docs/references/reference-signals-create-notification-inbox-tool.md +1 -1
  429. package/dist/docs/references/reference-signals-time-signal-provider.md +102 -0
  430. package/dist/docs/references/reference-storage-composite.md +4 -0
  431. package/dist/docs/references/reference-storage-overview.md +29 -29
  432. package/dist/docs/references/reference-tools-create-code-mode.md +1 -1
  433. package/dist/docs/references/reference-tools-vector-query-tool.md +3 -3
  434. package/dist/docs/references/reference-voice-overview.md +28 -28
  435. package/dist/docs/references/reference-voice-speech-to-speech.md +10 -10
  436. package/dist/docs/references/reference-voice-speech-to-text.md +1 -1
  437. package/dist/docs/references/reference-voice-text-to-speech.md +1 -1
  438. package/dist/docs/references/reference-voice-voice.addInstructions.md +1 -1
  439. package/dist/docs/references/reference-voice-voice.listen.md +1 -1
  440. package/dist/docs/references/reference-workers-overview.md +3 -1
  441. package/dist/docs/references/reference-workflows-step.md +1 -1
  442. package/dist/docs/references/reference-workflows-workflow-methods-map.md +1 -1
  443. package/dist/docs/references/reference-workflows-workflow.md +1 -1
  444. package/dist/docs/references/reference-workspace-factory-sandbox.md +2 -0
  445. package/dist/docs/references/reference-workspace-local-filesystem.md +1 -1
  446. package/dist/docs/references/reference-workspace-local-sandbox.md +1 -1
  447. package/dist/docs/references/reference.md +1 -0
  448. package/dist/evals/index.cjs +2 -2
  449. package/dist/evals/index.js +2 -2
  450. package/dist/evals/scoreTraces/index.cjs +2 -2
  451. package/dist/evals/scoreTraces/index.js +2 -2
  452. package/dist/harness/index.cjs +1 -1
  453. package/dist/harness/index.js +1 -1
  454. package/dist/index.cjs +1 -1
  455. package/dist/index.js +1 -1
  456. package/dist/knowledge/importers/overlay.d.ts +32 -0
  457. package/dist/knowledge/importers/sink.d.ts +11 -1
  458. package/dist/knowledge/importers/types.d.ts +9 -0
  459. package/dist/knowledge/index.cjs +551 -87
  460. package/dist/knowledge/index.d.ts +6 -0
  461. package/dist/knowledge/index.js +551 -87
  462. package/dist/knowledge/page-tools.d.ts +2 -0
  463. package/dist/llm/index.cjs +3 -3
  464. package/dist/llm/index.js +3 -3
  465. package/dist/llm/model/provider-types.generated.d.ts +7865 -7877
  466. package/dist/{llm-CS6YY_IV.cjs → llm-C1aiWB6O.cjs} +2 -2
  467. package/dist/{llm-zjtMQn7M.js → llm-D5rGQSnS.js} +2 -2
  468. package/dist/loop/index.cjs +2 -2
  469. package/dist/loop/index.js +2 -2
  470. package/dist/loop/loop-runtime.d.ts +1 -1
  471. package/dist/loop/shared/signal-outcome.d.ts +1 -1
  472. package/dist/loop/shared/steps/signal-drain-core.d.ts +9 -3
  473. package/dist/{manager-BU_lY6kl.cjs → manager-B2z5f39G.cjs} +3 -3
  474. package/dist/{manager-Do0MENhH.js → manager-DxwPLhYJ.js} +3 -3
  475. package/dist/mastra/index.cjs +1 -1
  476. package/dist/mastra/index.js +1 -1
  477. package/dist/{mastra-C-1zIyS5.js → mastra-CHd7UnbI.js} +17 -17
  478. package/dist/{mastra-DYVjsXTO.cjs → mastra-WMuI7TZD.cjs} +17 -17
  479. package/dist/memory/index.cjs +2 -2
  480. package/dist/memory/index.js +2 -2
  481. package/dist/{model-DIzIjDej.js → model-B8DlLHex.js} +1 -1
  482. package/dist/{model-DGFc1hw3.cjs → model-C2CTzjMk.cjs} +1 -1
  483. package/dist/{netlify-CyL6FNsw.cjs → netlify-Dhs0UJul.cjs} +1 -1
  484. package/dist/{netlify-B9mLgxJs.js → netlify-xQFmVbr4.js} +1 -1
  485. package/dist/notifications/index.cjs +1 -1
  486. package/dist/notifications/index.js +1 -1
  487. package/dist/processor-provider/index.cjs +1 -1
  488. package/dist/processor-provider/index.js +1 -1
  489. package/dist/processors/index.cjs +2 -2
  490. package/dist/processors/index.js +2 -2
  491. package/dist/{provider-registry-g9m_fyV8.cjs → provider-registry-CSQPAFzz.cjs} +22244 -22307
  492. package/dist/{provider-registry-DdQoO_jd.js → provider-registry-FxVHWmht.js} +22244 -22307
  493. package/dist/provider-registry.json +22283 -22350
  494. package/dist/{registry-generator-CX6Ev697.js → registry-generator-BzsrHxrr.js} +1 -1
  495. package/dist/{registry-generator-At8J4ZOR.cjs → registry-generator-Dsh7xcz9.cjs} +1 -1
  496. package/dist/{rehydrate-BzD6UX8f.cjs → rehydrate-8q7KX4rD.cjs} +2 -2
  497. package/dist/{rehydrate-DEDxsr6E.js → rehydrate-DV1iO9eZ.js} +2 -2
  498. package/dist/relevance/index.cjs +2 -2
  499. package/dist/relevance/index.js +2 -2
  500. package/dist/{run-registry-PiQTSx2Y.cjs → run-registry-DOlZccsr.cjs} +4703 -2233
  501. package/dist/{run-registry-D22IF5SK.js → run-registry-sTm40Ecv.js} +4704 -2234
  502. package/dist/{scheduler-B-VZUvv_.js → scheduler-0ShtMM1I.js} +71 -21
  503. package/dist/{scheduler-D00X03jQ.cjs → scheduler-CMjK0LoQ.cjs} +82 -20
  504. package/dist/server/types.d.ts +6 -0
  505. package/dist/signals/index.cjs +2 -1
  506. package/dist/signals/index.d.ts +1 -0
  507. package/dist/signals/index.js +2 -2
  508. package/dist/signals/time-signal-provider.d.ts +62 -0
  509. package/dist/signals-BTfT-6DE.js +280 -0
  510. package/dist/signals-CIwzgkvN.cjs +291 -0
  511. package/dist/{source-BVOi8Xhp.cjs → source-BC8Et5qZ.cjs} +7 -0
  512. package/dist/{source-fNa6u1kH.js → source-DYZMfjv3.js} +7 -0
  513. package/dist/storage/constants.cjs +1 -1
  514. package/dist/storage/constants.js +1 -1
  515. package/dist/storage/domains/agents/index.cjs +1 -1
  516. package/dist/storage/domains/agents/index.js +1 -1
  517. package/dist/storage/domains/inmemory-db.d.ts +6 -0
  518. package/dist/storage/domains/knowledge/base.d.ts +22 -4
  519. package/dist/storage/domains/knowledge/inmemory.d.ts +2 -0
  520. package/dist/storage/domains/knowledge/pages.d.ts +8 -0
  521. package/dist/storage/domains/memory/base.d.ts +107 -0
  522. package/dist/storage/domains/memory/index.d.ts +1 -0
  523. package/dist/storage/domains/memory/inmemory.d.ts +36 -0
  524. package/dist/storage/domains/memory/queue-thread.d.ts +36 -0
  525. package/dist/storage/domains/schedules/base.d.ts +31 -2
  526. package/dist/storage/domains/schedules/inmemory.d.ts +9 -2
  527. package/dist/storage/domains/workflows/base.d.ts +14 -0
  528. package/dist/storage/domains/workflows/inmemory.d.ts +4 -1
  529. package/dist/storage/index.cjs +9 -4
  530. package/dist/storage/index.js +5 -5
  531. package/dist/stream/index.cjs +2 -2
  532. package/dist/stream/index.js +2 -2
  533. package/dist/{stream-BbPZavop.cjs → stream-aGuW9AL8.cjs} +88 -2
  534. package/dist/{stream-CoQTdzLm.js → stream-k-Q1NLUs.js} +59 -3
  535. package/dist/test-utils/llm-mock.cjs +1 -1
  536. package/dist/test-utils/llm-mock.js +1 -1
  537. package/dist/tool-loop-agent/index.cjs +1 -1
  538. package/dist/tool-loop-agent/index.js +1 -1
  539. package/dist/{tool-loop-agent-DhQkNDaR.js → tool-loop-agent-CJrysPOR.js} +3 -3
  540. package/dist/{tool-loop-agent-8lyD6O4o.cjs → tool-loop-agent-WcRtGJHn.cjs} +3 -3
  541. package/dist/{utils-DtmyBuBT.cjs → utils-DEzQLxWP.cjs} +1 -1
  542. package/dist/{utils-B-3lET9S.js → utils-DamF4CfD.js} +1 -1
  543. package/dist/utils.cjs +1 -1
  544. package/dist/utils.js +1 -1
  545. package/dist/{validate-vucjWXcz.cjs → validate-CDlONK0o.cjs} +1 -1
  546. package/dist/{validate-CO4rWYyH.js → validate-rum3NMFA.js} +1 -1
  547. package/dist/vector/index.cjs +1 -1
  548. package/dist/vector/index.js +1 -1
  549. package/dist/worker/index.cjs +1 -1
  550. package/dist/worker/index.js +1 -1
  551. package/dist/{worker-BgifMkOw.cjs → worker-6MiKMAqB.cjs} +3 -3
  552. package/dist/{worker-B6CpapAF.js → worker-BnXipQth.js} +2 -2
  553. package/dist/{worker-eUT7GgQ9.cjs → worker-rwal1zI8.cjs} +2 -2
  554. package/dist/{worker-CCpSrXBY.js → worker-st16x0my.js} +3 -3
  555. package/dist/{workflow-B4Tn58DD.js → workflow-BzhYKgc_.js} +25 -33
  556. package/dist/{workflow-BJSrGEEL.cjs → workflow-D2-gn0U0.cjs} +25 -33
  557. package/dist/workflows/builder/index.cjs +1 -1
  558. package/dist/workflows/builder/index.js +1 -1
  559. package/dist/workflows/evented/execution-engine.d.ts +3 -1
  560. package/dist/workflows/evented/index.cjs +1 -1
  561. package/dist/workflows/evented/index.js +1 -1
  562. package/dist/workflows/evented/setup-warning.d.ts +11 -0
  563. package/dist/workflows/evented/workflow-event-processor/index.d.ts +10 -2
  564. package/dist/workflows/evented/workflow-event-processor/loop.d.ts +1 -1
  565. package/dist/workflows/evented/workflow-event-processor/parallel.d.ts +2 -2
  566. package/dist/workflows/evented/workflow-event-processor/sleep.d.ts +2 -2
  567. package/dist/workflows/evented/workflow.d.ts +6 -2
  568. package/dist/workflows/handlers/entry.d.ts +8 -0
  569. package/dist/workflows/index.cjs +7 -5
  570. package/dist/workflows/index.js +6 -6
  571. package/dist/workflows/run-storage.d.ts +11 -2
  572. package/dist/workflows/scheduler/claim.d.ts +51 -0
  573. package/dist/workflows/scheduler/index.d.ts +2 -0
  574. package/dist/workflows/types.d.ts +6 -0
  575. package/dist/workflows/workflow.d.ts +85 -10
  576. package/dist/workspace/filesystem/local-filesystem.d.ts +13 -0
  577. package/dist/workspace/index.cjs +1 -1
  578. package/dist/workspace/index.js +1 -1
  579. package/dist/{workspace-CnmmMou8.js → workspace-Ct2kAiDv.js} +50 -11
  580. package/dist/{workspace-C5lXteRU.cjs → workspace-CzzrnE6Q.cjs} +50 -11
  581. package/package.json +7 -7
  582. package/src/llm/model/provider-types.generated.d.ts +7865 -7877
  583. package/dist/signals-B1JSj-Ew.cjs +0 -58
  584. package/dist/signals-De9VnwL2.js +0 -53
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_base = require("../base-BIzR1D3_.cjs");
3
- const require_run_registry = require("../run-registry-PiQTSx2Y.cjs");
3
+ const require_run_registry = require("../run-registry-DOlZccsr.cjs");
4
4
  const require_tool = require("../tool-CYlT2UA-.cjs");
5
5
  let crypto = require("crypto");
6
6
  let _mastra_schema_compat_schema = require("@mastra/schema-compat/schema");
@@ -325,7 +325,8 @@ const DECLARATION_FIELDS = {
325
325
  required: true
326
326
  },
327
327
  parent: { kind: "nullableString" },
328
- fallbackParent: { kind: "nonEmpty" }
328
+ fallbackParent: { kind: "nonEmpty" },
329
+ path: { kind: "segments" }
329
330
  };
330
331
  const ITEM_FIELDS = {
331
332
  ...DECLARATION_FIELDS,
@@ -371,6 +372,9 @@ function checkFields(value, fields, where) {
371
372
  case "aliases":
372
373
  out[key] = checkDenseArray(field, at).map((alias, index) => checkString(alias, `${at}[${index}]`, true));
373
374
  break;
375
+ case "segments":
376
+ out[key] = checkDenseArray(field, at).map((segment, index) => checkString(segment, `${at}[${index}]`));
377
+ break;
374
378
  case "links": out[key] = checkDenseArray(field, at).map((link, index) => checkFields(link, {
375
379
  sourceId: {
376
380
  kind: "nonEmpty",
@@ -1094,6 +1098,16 @@ var KnowledgeImportNotGatheredError = class extends Error {
1094
1098
  }
1095
1099
  };
1096
1100
  /** The flush needs a call the overlay doesn't model: it falls back to the per-op path. */
1101
+ /**
1102
+ * The gather read the rows once, its plan writing pages only in place, and the decide pass went to
1103
+ * create, move or delete a page anyway: one more pass reads them again under the lock.
1104
+ */
1105
+ var KnowledgeImportRereadRequiredError = class extends Error {
1106
+ constructor(what) {
1107
+ super(`The batched import flush needs its rows read again under the lock: ${what}`);
1108
+ this.name = "KnowledgeImportRereadRequiredError";
1109
+ }
1110
+ };
1097
1111
  var KnowledgeImportBatchUnsupportedError = class extends Error {
1098
1112
  call;
1099
1113
  constructor(call) {
@@ -1103,7 +1117,31 @@ var KnowledgeImportBatchUnsupportedError = class extends Error {
1103
1117
  }
1104
1118
  };
1105
1119
  const nodeKey = (bindingId, sourceId) => `${bindingId}\u0000${sourceId}`;
1106
- const pathKey = (scopeId, path) => JSON.stringify([scopeId, ...path]);
1120
+ /** The key {@link ImportOverlayRows.paths} holds a scope path under. */
1121
+ const importPathKey = (scopeId, path) => JSON.stringify([scopeId, ...path]);
1122
+ const pathKey = importPathKey;
1123
+ /**
1124
+ * The key path of each live page whose whole chain to its scope is among `pages`, to that page: a
1125
+ * path a read chain already answers, with no `getPagesByPaths`. One key path has one live page.
1126
+ */
1127
+ function importChainPaths(pages) {
1128
+ const out = /* @__PURE__ */ new Map();
1129
+ for (const page of pages.values()) {
1130
+ if (!page || page.isScope) continue;
1131
+ const path = [];
1132
+ let at = page;
1133
+ while (at && !at.isScope) {
1134
+ if (at.key === void 0 || at.deletedAt || at.mergedInto || path.length >= 10) {
1135
+ at = void 0;
1136
+ break;
1137
+ }
1138
+ path.unshift(at.key);
1139
+ at = at.parentId ? pages.get(at.parentId) : null;
1140
+ }
1141
+ if (at) out.set(pathKey(at.id, path), page.id);
1142
+ }
1143
+ return out;
1144
+ }
1107
1145
  const childKey = (parentId, key) => `${parentId}\u0000${key}`;
1108
1146
  const byPosition = (a, b) => a.position < b.position ? -1 : a.position > b.position ? 1 : a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
1109
1147
  /** UTF-8 byte order, as the stores sort source IDs. */
@@ -1147,7 +1185,9 @@ const copyRows = (rows) => ({
1147
1185
  tooLarge: new Set(rows.tooLarge),
1148
1186
  linkTargets: new Set(rows.linkTargets),
1149
1187
  tooManyLinkers: new Set(rows.tooManyLinkers),
1150
- ...rows.bindings ? { bindings: rows.bindings } : {}
1188
+ ...rows.bindings ? { bindings: rows.bindings } : {},
1189
+ ...rows.inPlace ? { inPlace: true } : {},
1190
+ ...rows.readOnce ? { readOnce: true } : {}
1151
1191
  });
1152
1192
  const copyState = (state) => ({
1153
1193
  rows: copyRows(state.rows),
@@ -1268,6 +1308,17 @@ var ImportOverlay = class {
1268
1308
  #setPage(id, page) {
1269
1309
  this.#state.rows.pages.set(id, page);
1270
1310
  this.#childIndex = void 0;
1311
+ this.#chainPaths = void 0;
1312
+ }
1313
+ /** {@link importChainPaths} of the working copy, kept like the child index. */
1314
+ #chainPaths;
1315
+ #pathFromChains(key) {
1316
+ const { pages } = this.#state.rows;
1317
+ if (this.#chainPaths?.pages !== pages) this.#chainPaths = {
1318
+ pages,
1319
+ byPath: importChainPaths(pages)
1320
+ };
1321
+ return this.#chainPaths.byPath.get(key);
1271
1322
  }
1272
1323
  /** Live ordinary children by position, once gathered. */
1273
1324
  #children(parentId) {
@@ -1370,6 +1421,8 @@ var ImportOverlay = class {
1370
1421
  if (path.length === 0) return null;
1371
1422
  const key = pathKey(scopeId, path);
1372
1423
  if (!rows().paths.has(key)) {
1424
+ const id = this.#pathFromChains(key);
1425
+ if (id) return this.#page(id);
1373
1426
  this.missing.paths.set(key, {
1374
1427
  scopeId,
1375
1428
  path
@@ -1385,6 +1438,7 @@ var ImportOverlay = class {
1385
1438
  for (const id of unlocked) this.missing.locks.add(id);
1386
1439
  throw new KnowledgeImportNotGatheredError(`locks ${unlocked.join(", ")}`);
1387
1440
  }
1441
+ return [];
1388
1442
  },
1389
1443
  listNotes: async ({ pageId }) => {
1390
1444
  if (!rows().notes.has(pageId)) {
@@ -1394,6 +1448,7 @@ var ImportOverlay = class {
1394
1448
  return { notes: rows().notes.get(pageId).filter((note) => !note.deletedAt) };
1395
1449
  },
1396
1450
  createPage: async (input) => {
1451
+ if (rows().readOnce) throw new KnowledgeImportRereadRequiredError("createPage");
1397
1452
  if (input.id !== void 0 || input.position !== void 0) throw new KnowledgeImportBatchUnsupportedError("createPage with id or position");
1398
1453
  if (!input.name.trim()) throw new Error("Knowledge page name must not be empty");
1399
1454
  require_run_registry.assertKnowledgePageDescriptionWithinBound(input.description || void 0);
@@ -1524,6 +1579,7 @@ var ImportOverlay = class {
1524
1579
  return ids;
1525
1580
  },
1526
1581
  movePage: async (input) => {
1582
+ if (rows().readOnce) throw new KnowledgeImportRereadRequiredError("movePage");
1527
1583
  if (input.position !== void 0) throw new KnowledgeImportBatchUnsupportedError("movePage with a position");
1528
1584
  const page = this.#livePage(input.id);
1529
1585
  if (page.isScope) throw new Error(`Knowledge scope page ${page.id} can't be moved`);
@@ -1544,7 +1600,12 @@ var ImportOverlay = class {
1544
1600
  if (parentDepth + 1 + Math.max(...subtree.map((entry) => entry.depth)) > 10) throw new require_run_registry.KnowledgePageDepthError(page.id);
1545
1601
  const scopeId = parent.isScope ? parent.id : parent.scopeId;
1546
1602
  if (scopeId !== page.scopeId) throw new KnowledgeImportBatchUnsupportedError("movePage across scopes");
1547
- if (page.key !== void 0) {
1603
+ const key = input.key ?? page.key;
1604
+ if (input.key !== void 0) {
1605
+ const problem = require_run_registry.knowledgePageKeyProblem(input.key);
1606
+ if (problem) throw new Error(problem);
1607
+ }
1608
+ if (key !== void 0) {
1548
1609
  const parentKeys = parent.isScope ? {
1549
1610
  scopeId: parent.id,
1550
1611
  path: []
@@ -1552,10 +1613,10 @@ var ImportOverlay = class {
1552
1613
  if (!parentKeys) throw new KnowledgeImportBatchUnsupportedError("movePage under a broken chain");
1553
1614
  const clash = await impl.getPageByPath({
1554
1615
  scopeId,
1555
- path: [...parentKeys.path, page.key],
1616
+ path: [...parentKeys.path, key],
1556
1617
  read: "all"
1557
1618
  });
1558
- if (clash && clash.id !== page.id) throw new require_run_registry.KnowledgePageKeyConflictError(page.key, parent.id);
1619
+ if (clash && clash.id !== page.id) throw new require_run_registry.KnowledgePageKeyConflictError(key, parent.id);
1559
1620
  }
1560
1621
  if (this.#state.moved.has(page.id)) throw new KnowledgeImportBatchUnsupportedError("a second move of a page");
1561
1622
  const position = this.#nextPosition(parent.id);
@@ -1563,6 +1624,7 @@ var ImportOverlay = class {
1563
1624
  const at = now();
1564
1625
  const moved = {
1565
1626
  ...page,
1627
+ ...key !== void 0 ? { key } : {},
1566
1628
  parentId: parent.id,
1567
1629
  position,
1568
1630
  version: page.version + 1,
@@ -1582,6 +1644,7 @@ var ImportOverlay = class {
1582
1644
  fromScopeId: page.scopeId,
1583
1645
  expectedVersion: input.expectedVersion,
1584
1646
  parentId: parent.id,
1647
+ ...input.key !== void 0 ? { key: input.key } : {},
1585
1648
  scopeId,
1586
1649
  depth: parentDepth + 1,
1587
1650
  position,
@@ -1590,6 +1653,7 @@ var ImportOverlay = class {
1590
1653
  return structuredClone(moved);
1591
1654
  },
1592
1655
  deletePage: async (input) => {
1656
+ if (rows().readOnce) throw new KnowledgeImportRereadRequiredError("deletePage");
1593
1657
  const page = this.#livePage(input.id);
1594
1658
  if (page.isScope) throw new Error(`Knowledge scope page ${page.id} can't be deleted`);
1595
1659
  if (page.version !== input.expectedVersion) throw new require_run_registry.KnowledgeConflictError(page.id);
@@ -1712,6 +1776,17 @@ var ImportOverlay = class {
1712
1776
  const key = rows().nodesByPage.get(pageId);
1713
1777
  return key ? structuredClone(this.#node(key)) : null;
1714
1778
  },
1779
+ getNodesByPages: async (pageIds) => {
1780
+ const unread = [...new Set(pageIds)].filter((pageId) => !rows().nodesByPage.has(pageId));
1781
+ if (unread.length) {
1782
+ for (const pageId of unread) this.missing.nodesByPage.add(pageId);
1783
+ throw new KnowledgeImportNotGatheredError(`nodes of pages ${unread.join(", ")}`);
1784
+ }
1785
+ return [...new Set(pageIds)].flatMap((pageId) => {
1786
+ const node = rows().nodesByPage.get(pageId) ? this.#node(rows().nodesByPage.get(pageId)) : null;
1787
+ return node ? [structuredClone(node)] : [];
1788
+ });
1789
+ },
1715
1790
  saveNode: async (node) => {
1716
1791
  const key = nodeKey(node.bindingId, node.sourceId);
1717
1792
  const mapped = rows().nodesByPage.get(node.pageId);
@@ -1818,36 +1893,84 @@ async function readImportRows(input) {
1818
1893
  ...missing?.pages ?? [],
1819
1894
  ...missing?.locks ?? []
1820
1895
  ]));
1821
- const plan = input.plan(rows);
1822
- const roots = [.../* @__PURE__ */ new Set([...plan.subtrees, ...missing?.subtrees ?? []])].filter((id) => !rows.complete.has(id));
1896
+ let plan = input.plan(rows);
1823
1897
  const carried = [];
1824
- if (roots.length) {
1825
- const subtrees = await storage.getPageSubtrees({
1826
- rootIds: roots,
1827
- limit: 499
1828
- });
1829
- for (const root of roots) {
1830
- const pages = subtrees[root];
1831
- if (!pages) {
1832
- rows.tooLarge.add(root);
1833
- continue;
1898
+ let held = [];
1899
+ let settled = false;
1900
+ for (let stage = 0; stage < GATHER_PLAN_STAGES; stage++) {
1901
+ if (stage) plan = input.plan(rows);
1902
+ const before = rows.pages.size + rows.paths.size + rows.complete.size;
1903
+ const roots = [.../* @__PURE__ */ new Set([...plan.subtrees, ...stage ? [] : missing?.subtrees ?? []])].filter((id) => !rows.complete.has(id) && !rows.tooLarge.has(id));
1904
+ if (roots.length) {
1905
+ const subtrees = await storage.getPageSubtrees({
1906
+ rootIds: roots,
1907
+ limit: 499
1908
+ });
1909
+ for (const root of roots) {
1910
+ const pages = subtrees[root];
1911
+ if (!pages) {
1912
+ rows.tooLarge.add(root);
1913
+ continue;
1914
+ }
1915
+ rows.complete.add(root);
1916
+ for (const page of pages) {
1917
+ rows.pages.set(page.id, page);
1918
+ rows.complete.add(page.id);
1919
+ carried.push(page.id);
1920
+ }
1834
1921
  }
1835
- rows.complete.add(root);
1836
- for (const page of pages) {
1837
- rows.pages.set(page.id, page);
1838
- rows.complete.add(page.id);
1839
- carried.push(page.id);
1922
+ const unmapped = carried.filter((id) => !rows.nodesByPage.has(id));
1923
+ if (unmapped.length) {
1924
+ for (const id of unmapped) rows.nodesByPage.set(id, null);
1925
+ for (const node of await importers.getNodesByPages(unmapped)) {
1926
+ const key = nodeKey(node.bindingId, node.sourceId);
1927
+ rows.nodes.set(key, node);
1928
+ rows.nodesByPage.set(node.pageId, key);
1929
+ }
1840
1930
  }
1841
1931
  }
1842
- const unmapped = carried.filter((id) => !rows.nodesByPage.has(id));
1843
- if (unmapped.length) {
1844
- for (const id of unmapped) rows.nodesByPage.set(id, null);
1845
- for (const node of await importers.getNodesByPages(unmapped)) {
1932
+ const paths = new Map(plan.paths.map((each) => [pathKey(each.scopeId, each.path), each]));
1933
+ if (!stage) for (const [key, each] of missing?.paths ?? []) paths.set(key, each);
1934
+ for (const key of [...paths.keys()]) if (rows.paths.has(key)) paths.delete(key);
1935
+ const byScope = /* @__PURE__ */ new Map();
1936
+ for (const [key, { scopeId, path }] of paths) {
1937
+ if (path.length === 0) continue;
1938
+ byScope.set(scopeId, [...byScope.get(scopeId) ?? [], {
1939
+ key,
1940
+ path
1941
+ }]);
1942
+ }
1943
+ for (const [scopeId, entries] of byScope) {
1944
+ const found = await storage.getPagesByPaths({
1945
+ scopeId,
1946
+ paths: entries.map((each) => each.path),
1947
+ read: "all"
1948
+ });
1949
+ entries.forEach((each, i) => {
1950
+ const page = found[i] ?? null;
1951
+ rows.paths.set(each.key, page?.id ?? null);
1952
+ if (page && !rows.pages.has(page.id)) rows.pages.set(page.id, page);
1953
+ });
1954
+ }
1955
+ held = [...rows.paths.values()].filter((id) => !!id);
1956
+ await addChains(new Set(held.map((id) => rows.pages.get(id)?.parentId).filter((id) => !!id)));
1957
+ const byPage = [...new Set([
1958
+ ...held,
1959
+ ...plan.holders ?? [],
1960
+ ...stage ? [] : missing?.nodesByPage ?? []
1961
+ ].filter((id) => !rows.nodesByPage.has(id)))];
1962
+ if (byPage.length) {
1963
+ for (const id of byPage) rows.nodesByPage.set(id, null);
1964
+ for (const node of await importers.getNodesByPages(byPage)) {
1846
1965
  const key = nodeKey(node.bindingId, node.sourceId);
1847
1966
  rows.nodes.set(key, node);
1848
1967
  rows.nodesByPage.set(node.pageId, key);
1849
1968
  }
1850
1969
  }
1970
+ if (rows.pages.size + rows.paths.size + rows.complete.size === before) {
1971
+ settled = true;
1972
+ break;
1973
+ }
1851
1974
  }
1852
1975
  const targets = new Set(plan.linkTargets);
1853
1976
  for (const id of carried) {
@@ -1879,39 +2002,6 @@ async function readImportRows(input) {
1879
2002
  await addChains(new Set([...linkers, ...reached].map((node) => node.pageId)));
1880
2003
  }
1881
2004
  if (plan.bindings || missing?.bindings.size) rows.bindings ??= await importers.listBindings();
1882
- const paths = new Map(plan.paths.map((each) => [pathKey(each.scopeId, each.path), each]));
1883
- for (const [key, each] of missing?.paths ?? []) paths.set(key, each);
1884
- const byScope = /* @__PURE__ */ new Map();
1885
- for (const [key, { scopeId, path }] of paths) {
1886
- if (path.length === 0) continue;
1887
- byScope.set(scopeId, [...byScope.get(scopeId) ?? [], {
1888
- key,
1889
- path
1890
- }]);
1891
- }
1892
- for (const [scopeId, entries] of byScope) {
1893
- const found = await storage.getPagesByPaths({
1894
- scopeId,
1895
- paths: entries.map((each) => each.path),
1896
- read: "all"
1897
- });
1898
- entries.forEach((each, i) => {
1899
- const page = found[i] ?? null;
1900
- rows.paths.set(each.key, page?.id ?? null);
1901
- if (page && !rows.pages.has(page.id)) rows.pages.set(page.id, page);
1902
- });
1903
- }
1904
- const held = [...rows.paths.values()].filter((id) => !!id);
1905
- await addChains(new Set(held.map((id) => rows.pages.get(id)?.parentId).filter((id) => !!id)));
1906
- const byPage = [...new Set([...held, ...missing?.nodesByPage ?? []].filter((id) => !rows.nodesByPage.has(id)))];
1907
- if (byPage.length) {
1908
- for (const id of byPage) rows.nodesByPage.set(id, null);
1909
- for (const node of await importers.getNodesByPages(byPage)) {
1910
- const key = nodeKey(node.bindingId, node.sourceId);
1911
- rows.nodes.set(key, node);
1912
- rows.nodesByPage.set(node.pageId, key);
1913
- }
1914
- }
1915
2005
  const notes = [.../* @__PURE__ */ new Set([
1916
2006
  ...plan.notes,
1917
2007
  ...held,
@@ -1929,8 +2019,11 @@ async function readImportRows(input) {
1929
2019
  const last = await storage.getLastChildPositions({ parentIds: positions });
1930
2020
  for (const id of positions) rows.positions.set(id, last[id] ?? null);
1931
2021
  }
2022
+ rows.inPlace = settled && !!plan.inPlace;
1932
2023
  return rows;
1933
2024
  }
2025
+ /** Times one gather plans again from what it read (path folders, then the page at the path, then its children). */
2026
+ const GATHER_PLAN_STAGES = 4;
1934
2027
  /** Most linking nodes one gather reads; past it, a flush that lists them is saved op by op. */
1935
2028
  const LINKING_NODES_READ = 500;
1936
2029
  /** Live, non-scope pages among the rows: the ones a decide pass may lock. */
@@ -1944,14 +2037,27 @@ const lockable = (rows) => [...rows.pages.values()].filter((page) => !!page && !
1944
2037
  async function gatherImportRows(input) {
1945
2038
  const locked = new Set(input.locked ?? []);
1946
2039
  let rows = await readImportRows(input);
2040
+ if (input.storage.getCapabilities().serializedWriteTransactions && rows.notes.size === 0) {
2041
+ for (const id of lockable(rows)) locked.add(id);
2042
+ rows.locked = locked;
2043
+ return rows;
2044
+ }
1947
2045
  for (let round = 0; round < (input.rounds ?? 3); round++) {
1948
2046
  const unlocked = lockable(rows).filter((id) => !locked.has(id));
1949
2047
  if (!unlocked.length) {
1950
2048
  rows.locked = locked;
1951
2049
  return rows;
1952
2050
  }
1953
- await input.storage.lockPages(unlocked.sort());
2051
+ const versions = await input.storage.lockPages(unlocked.sort());
1954
2052
  for (const id of unlocked) locked.add(id);
2053
+ if (round === 0 && !input.reread && rows.inPlace && rows.notes.size === 0 && Array.isArray(versions) && versions.length === unlocked.length) {
2054
+ const read = new Map(versions.map((row) => [row.id, row.version]));
2055
+ if (unlocked.every((id) => read.get(id) === rows.pages.get(id)?.version)) {
2056
+ rows.locked = locked;
2057
+ rows.readOnce = true;
2058
+ return rows;
2059
+ }
2060
+ }
1955
2061
  rows = await readImportRows(input);
1956
2062
  }
1957
2063
  return null;
@@ -2052,6 +2158,24 @@ function knowledgeImportKey(sourceId) {
2052
2158
  return sourceId.replace(/[/#%:~]/g, (char) => `~${char.charCodeAt(0).toString(16).toUpperCase()}`);
2053
2159
  }
2054
2160
  const live = (page) => !!page && !page.deletedAt && !page.mergedInto;
2161
+ /** Source IDs the host keeps for the folders it makes on an item's path; importers can't send them. */
2162
+ const KNOWLEDGE_IMPORT_PATH_STUB_PREFIX = "~path:";
2163
+ /**
2164
+ * Why an item can never land, whatever the store holds: a reserved `sourceId`, or a `path` the host
2165
+ * can't place (with `parent` or `fallbackParent`, empty, an empty or blank segment, a page key too long to
2166
+ * link to, or deeper than a page can sit under a target `targetDepth` keys deep). Null when it can.
2167
+ */
2168
+ function knowledgeImportItemProblem(item, targetDepth) {
2169
+ if (item.sourceId.startsWith("~path:")) return `sourceId must not start with "${KNOWLEDGE_IMPORT_PATH_STUB_PREFIX}"`;
2170
+ const { path } = item;
2171
+ if (path === void 0) return null;
2172
+ if (item.parent != null || item.fallbackParent !== void 0) return "path is not allowed with parent or fallbackParent";
2173
+ if (path.length === 0) return "path must have at least one segment";
2174
+ if (path.some((segment) => !segment.trim())) return "path segments must not be empty or blank";
2175
+ if (path.some((segment) => knowledgeImportKey(segment).length > 512)) return `path segment keys must be at most 512 characters`;
2176
+ if (targetDepth + path.length > 10) return `path would sit more than 10 levels below the scope`;
2177
+ return null;
2178
+ }
2055
2179
  /** Core errors that mean "this op can't land here", by name: an adapter may carry its own core copy. */
2056
2180
  function refusalFor(error) {
2057
2181
  if (error instanceof KnowledgeImportRefusalError) return error.reason;
@@ -2087,11 +2211,17 @@ function order(ops) {
2087
2211
  const id = op.op === "remove" ? op.sourceId : op.item.sourceId;
2088
2212
  const before = last.get(id);
2089
2213
  if (op.op === "declare" && before?.op === "upsert") {
2214
+ const { parent, fallbackParent, path, ...rest } = before.item;
2215
+ const kept = op.item.path !== void 0 ? rest : op.item.parent !== void 0 || op.item.fallbackParent !== void 0 ? {
2216
+ ...rest,
2217
+ ...parent !== void 0 ? { parent } : {},
2218
+ ...fallbackParent !== void 0 ? { fallbackParent } : {}
2219
+ } : before.item;
2090
2220
  last.delete(id);
2091
2221
  last.set(id, {
2092
2222
  op: "upsert",
2093
2223
  item: {
2094
- ...before.item,
2224
+ ...kept,
2095
2225
  ...op.item
2096
2226
  }
2097
2227
  });
@@ -2155,7 +2285,19 @@ async function applyKnowledgeImportFlush(input) {
2155
2285
  embeds: [],
2156
2286
  applied: false
2157
2287
  };
2158
- const ops = order(input.ops);
2288
+ const rekey = !!storage.getCapabilities().supportsPageRekey;
2289
+ let ops = order(input.ops).filter((op) => {
2290
+ const sourceId = op.op === "remove" ? op.sourceId : op.item.sourceId;
2291
+ const problem = op.op === "remove" ? sourceId.startsWith(KNOWLEDGE_IMPORT_PATH_STUB_PREFIX) : !!knowledgeImportItemProblem(op.item, 1);
2292
+ const unplaceable = op.op !== "remove" && op.item.path !== void 0 && !rekey;
2293
+ if (!problem && !unplaceable) return true;
2294
+ if (!problem) result.pathsUnsupported = true;
2295
+ result.refused.push({
2296
+ sourceId,
2297
+ reason: "invalid"
2298
+ });
2299
+ return false;
2300
+ });
2159
2301
  if (ops.length === 0 && !(input.settle && binding.anchorPageId)) {
2160
2302
  if (input.settle) result.settle = {
2161
2303
  pending: false,
@@ -2168,9 +2310,11 @@ async function applyKnowledgeImportFlush(input) {
2168
2310
  const keyPath = async (page) => {
2169
2311
  const keys = [];
2170
2312
  const ids = [];
2313
+ const versions = [];
2171
2314
  if (page.isScope) return page.id === scope.id ? {
2172
2315
  keys,
2173
- ids
2316
+ ids,
2317
+ versions
2174
2318
  } : null;
2175
2319
  const parents = page.parentId ? await storage.getPageParents({
2176
2320
  parentId: page.parentId,
@@ -2179,11 +2323,13 @@ async function applyKnowledgeImportFlush(input) {
2179
2323
  for (const at of [page, ...parents]) {
2180
2324
  if (at.isScope) return at.id === scope.id ? {
2181
2325
  keys,
2182
- ids
2326
+ ids,
2327
+ versions
2183
2328
  } : null;
2184
2329
  if (!live(at) || at.key === void 0) return null;
2185
2330
  keys.unshift(at.key);
2186
2331
  ids.unshift(at.id);
2332
+ versions.unshift(at.version);
2187
2333
  }
2188
2334
  return null;
2189
2335
  };
@@ -2216,8 +2362,17 @@ async function applyKnowledgeImportFlush(input) {
2216
2362
  read: "all"
2217
2363
  });
2218
2364
  const path = live(page) ? await keyPath(page) : null;
2365
+ if (page && path && storage.getCapabilities().serializedWriteTransactions) return {
2366
+ page,
2367
+ keys: path.keys
2368
+ };
2219
2369
  if (page && path) {
2220
- await storage.lockPages(path.ids);
2370
+ const read = new Map(path.ids.map((id, i) => [id, path.versions[i]]));
2371
+ const locked = await storage.lockPages(path.ids);
2372
+ if (Array.isArray(locked) && locked.length === read.size && locked.every((row) => read.get(row.id) === row.version)) return {
2373
+ page,
2374
+ keys: path.keys
2375
+ };
2221
2376
  const fresh = await storage.getPage({
2222
2377
  id: page.id,
2223
2378
  read: "all"
@@ -2298,6 +2453,15 @@ async function applyKnowledgeImportFlush(input) {
2298
2453
  return result;
2299
2454
  }
2300
2455
  const anchorPath = anchor?.keys ?? [];
2456
+ ops = ops.filter((op) => {
2457
+ if (op.op === "remove" || !op.item.path) return true;
2458
+ if (anchorPath.length + op.item.path.length <= 10) return true;
2459
+ result.refused.push({
2460
+ sourceId: op.item.sourceId,
2461
+ reason: "invalid"
2462
+ });
2463
+ return false;
2464
+ });
2301
2465
  /**
2302
2466
  * A page this binding owns, checked after locking: live, in the target's scope, mapped back to the
2303
2467
  * node. A person may have moved it out of the anchor, under anyone's pages: it is still the
@@ -2397,6 +2561,90 @@ async function applyKnowledgeImportFlush(input) {
2397
2561
  return node && await stateOf(node) === "live" ? node : null;
2398
2562
  };
2399
2563
  const ref = (keys) => `${target.scope}#${keys.join("/")}`;
2564
+ /** The folders an op's path made, counted as created once the op lands. */
2565
+ let madeFolders = [];
2566
+ /**
2567
+ * The page a path item goes under: the anchor, then each segment but the last. A live page at a
2568
+ * segment is used as it is, whoever wrote it; a missing one (a merged page reads as missing, as a
2569
+ * merged parent does for a new child) becomes an empty folder this binding owns, keyed
2570
+ * `~path:<keys>`, that the item sending that path takes over. Returns the node the item is under,
2571
+ * when it is this binding's.
2572
+ */
2573
+ const pathFolders = async (path) => {
2574
+ let at = anchor;
2575
+ let parentSourceId = null;
2576
+ let holderOf = null;
2577
+ const readHolder = async () => {
2578
+ if (!holderOf) return;
2579
+ const holder = await importers.getNodeByPage(holderOf.id);
2580
+ parentSourceId = holder?.bindingId === binding.id ? holder.sourceId : null;
2581
+ holderOf = null;
2582
+ };
2583
+ const relative = [];
2584
+ for (const segment of path.slice(0, -1)) {
2585
+ const key = knowledgeImportKey(segment);
2586
+ relative.push(key);
2587
+ const keys = [...at.keys, key];
2588
+ const found = await storage.getPageByPath({
2589
+ scopeId: scope.id,
2590
+ path: keys,
2591
+ read: "all"
2592
+ });
2593
+ if (found) {
2594
+ await storage.lockPages([found.id]);
2595
+ const fresh = await storage.getPage({
2596
+ id: found.id,
2597
+ read: "all"
2598
+ });
2599
+ if (!live(fresh) || fresh.parentId !== at.page.id || fresh.key !== key) throw new KnowledgeImportRefusalError("conflict");
2600
+ holderOf = fresh;
2601
+ at = {
2602
+ page: fresh,
2603
+ keys
2604
+ };
2605
+ continue;
2606
+ }
2607
+ await readHolder();
2608
+ const sourceId = KNOWLEDGE_IMPORT_PATH_STUB_PREFIX + relative.join("/");
2609
+ const page = await storage.createPage({
2610
+ parentId: at.page.id,
2611
+ key,
2612
+ name: segment,
2613
+ body: "",
2614
+ provenance
2615
+ });
2616
+ await importers.saveNode({
2617
+ bindingId: binding.id,
2618
+ sourceId,
2619
+ pageId: page.id,
2620
+ kind: null,
2621
+ parentSourceId,
2622
+ aliases: [],
2623
+ unresolvedLinks: [],
2624
+ links: [],
2625
+ linkSection: null,
2626
+ stub: false,
2627
+ adopted: false,
2628
+ unsettled: false
2629
+ });
2630
+ await links(page, { pages: [page] });
2631
+ madeFolders.push({
2632
+ sourceId,
2633
+ page,
2634
+ keys
2635
+ });
2636
+ parentSourceId = sourceId;
2637
+ at = {
2638
+ page,
2639
+ keys
2640
+ };
2641
+ }
2642
+ await readHolder();
2643
+ return {
2644
+ destination: at,
2645
+ parentSourceId
2646
+ };
2647
+ };
2400
2648
  const linkLine = async (link, node) => {
2401
2649
  const page = node ? await storage.getPage({
2402
2650
  id: node.pageId,
@@ -2502,7 +2750,7 @@ async function applyKnowledgeImportFlush(input) {
2502
2750
  * and the pages it rewrote; the caller counts them once its op lands.
2503
2751
  */
2504
2752
  let deferredLinkers = false;
2505
- const moveOwned = async (self, page, parent) => {
2753
+ const moveOwned = async (self, page, parent, key) => {
2506
2754
  const targets = [];
2507
2755
  const othersTargets = /* @__PURE__ */ new Map();
2508
2756
  await storage.lockSubtree(page.id);
@@ -2516,6 +2764,7 @@ async function applyKnowledgeImportFlush(input) {
2516
2764
  id: page.id,
2517
2765
  expectedVersion: page.version,
2518
2766
  parentId: parent.page.id,
2767
+ ...key !== void 0 && key !== page.key ? { key } : {},
2519
2768
  provenance
2520
2769
  });
2521
2770
  const keys = [...parent.keys, moved.key];
@@ -2571,6 +2820,97 @@ async function applyKnowledgeImportFlush(input) {
2571
2820
  });
2572
2821
  }
2573
2822
  };
2823
+ /**
2824
+ * Frees `key` under `destination` for a path item's own page to move onto. A folder of this binding's
2825
+ * that holds the path is absorbed: its children move under the item's page in order, their nodes
2826
+ * point at the item, a body a person wrote in it is kept in a note on the item's page, and the empty
2827
+ * folder goes. A folder someone left notes on, or whose body would replace a body the item's page
2828
+ * already keeps, is not absorbed: the item is refused `occupied`, as for any other page at the path.
2829
+ * An item inside the folder, sent at the folder's path, leaves it first under a key no item uses, so
2830
+ * it never moves under itself; the move after this one gives it the folder's key. Returns whether it
2831
+ * kept a body (the item then holds a person's text, so its removal gives that back) and the item's
2832
+ * page as it is now.
2833
+ */
2834
+ const absorbAt = async (self, destination, key, page) => {
2835
+ const held = await storage.getPageByPath({
2836
+ scopeId: scope.id,
2837
+ path: [...destination.keys, key],
2838
+ read: "all"
2839
+ });
2840
+ if (!held || held.id === page.id) return {
2841
+ kept: false,
2842
+ page
2843
+ };
2844
+ await storage.lockPages([held.id]);
2845
+ const stub = await storage.getPage({
2846
+ id: held.id,
2847
+ read: "all"
2848
+ });
2849
+ if (!live(stub) || stub.parentId !== destination.page.id || stub.key !== key) throw new KnowledgeImportRefusalError("occupied");
2850
+ const holder = await importers.getNodeByPage(stub.id);
2851
+ if (holder?.bindingId !== binding.id || !holder.sourceId.startsWith("~path:")) throw new KnowledgeImportRefusalError("occupied");
2852
+ if ((await storage.listNotes({
2853
+ pageId: stub.id,
2854
+ read: "all",
2855
+ limit: 1
2856
+ })).notes.length > 0) throw new KnowledgeImportRefusalError("occupied");
2857
+ if (stub.body && (await keptBodyNotes(page.id)).length > 0) throw new KnowledgeImportRefusalError("occupied");
2858
+ const children = [];
2859
+ let cursor;
2860
+ do {
2861
+ const out = await storage.listChildPages({
2862
+ parentId: stub.id,
2863
+ read: "all",
2864
+ ...cursor ? { cursor } : {}
2865
+ });
2866
+ children.push(...out.pages);
2867
+ cursor = out.nextCursor;
2868
+ } while (cursor);
2869
+ let inside = page.parentId;
2870
+ while (inside && inside !== stub.id) inside = (await storage.getPage({
2871
+ id: inside,
2872
+ read: "all"
2873
+ }))?.parentId ?? null;
2874
+ if (inside) {
2875
+ await storage.lockSubtree(page.id);
2876
+ page = await storage.movePage({
2877
+ id: page.id,
2878
+ expectedVersion: page.version,
2879
+ parentId: destination.page.id,
2880
+ key: `~absorbing-${(0, crypto.randomUUID)()}`,
2881
+ provenance
2882
+ });
2883
+ }
2884
+ for (const child of children.filter((child) => child.id !== page.id)) {
2885
+ await storage.lockSubtree(child.id);
2886
+ await storage.movePage({
2887
+ id: child.id,
2888
+ expectedVersion: child.version,
2889
+ parentId: page.id,
2890
+ provenance
2891
+ });
2892
+ const node = await importers.getNodeByPage(child.id);
2893
+ if (node?.bindingId === binding.id && node.parentSourceId === holder.sourceId) await importers.saveNode({
2894
+ ...node,
2895
+ parentSourceId: self
2896
+ });
2897
+ }
2898
+ if (stub.body) await keepBody(page.id, stub.body);
2899
+ const emptied = await storage.getPage({
2900
+ id: stub.id,
2901
+ read: "all"
2902
+ });
2903
+ await storage.deletePage({
2904
+ id: stub.id,
2905
+ expectedVersion: emptied.version,
2906
+ deletedBy: provenance.author
2907
+ });
2908
+ await importers.deleteNode(binding.id, holder.sourceId);
2909
+ return {
2910
+ kept: !!stub.body,
2911
+ page
2912
+ };
2913
+ };
2574
2914
  const write = async (item, hasBody) => {
2575
2915
  let existing = await importers.getNode(binding.id, item.sourceId);
2576
2916
  const state = existing ? await stateOf(existing) : null;
@@ -2585,7 +2925,19 @@ async function applyKnowledgeImportFlush(input) {
2585
2925
  const parentMerged = parentState === "merged";
2586
2926
  const misplaced = !!item.parent && !sourceParent && !parentMerged;
2587
2927
  const parentNode = sourceParent ?? await liveNodeOf(item.fallbackParent);
2588
- const destination = parentNode ? await owned(parentNode.pageId, parentNode.sourceId) : anchor;
2928
+ let destination = parentNode ? await owned(parentNode.pageId, parentNode.sourceId) : anchor;
2929
+ let key = knowledgeImportKey(item.sourceId);
2930
+ let parentSourceId = item.parent ?? null;
2931
+ if (item.path) {
2932
+ const current = existing ? await owned(existing.pageId, item.sourceId) : null;
2933
+ if (current?.outside) {
2934
+ key = current.page.key;
2935
+ parentSourceId = existing.parentSourceId;
2936
+ } else {
2937
+ ({destination, parentSourceId} = await pathFolders(item.path));
2938
+ key = knowledgeImportKey(item.path.at(-1));
2939
+ }
2940
+ }
2589
2941
  if (existing && destination.page.id === existing.pageId) throw new KnowledgeImportRefusalError("invalid");
2590
2942
  let rendered = hasBody ? await renderBody(item) : void 0;
2591
2943
  let fields = {
@@ -2593,7 +2945,6 @@ async function applyKnowledgeImportFlush(input) {
2593
2945
  ...item.description !== void 0 ? { description: item.description } : {},
2594
2946
  ...rendered ? { body: rendered.body } : {}
2595
2947
  };
2596
- const key = knowledgeImportKey(item.sourceId);
2597
2948
  let page;
2598
2949
  let change;
2599
2950
  let moved = false;
@@ -2613,9 +2964,34 @@ async function applyKnowledgeImportFlush(input) {
2613
2964
  read: "all"
2614
2965
  });
2615
2966
  if (!live(fresh) || fresh.parentId !== destination.page.id || fresh.key !== key) throw new KnowledgeImportRefusalError("occupied");
2616
- const imported = await release(fresh.id);
2967
+ const holder = await importers.getNodeByPage(fresh.id);
2617
2968
  renamed = fields.name !== fresh.name;
2618
- if (!imported) await keepBody(fresh.id, fresh.body ?? "");
2969
+ if (holder?.bindingId === binding.id) {
2970
+ if (!holder.sourceId.startsWith("~path:")) throw new KnowledgeImportRefusalError("occupied");
2971
+ const childIds = [];
2972
+ let cursor;
2973
+ do {
2974
+ const out = await storage.listChildPages({
2975
+ parentId: fresh.id,
2976
+ read: "all",
2977
+ ...cursor ? { cursor } : {}
2978
+ });
2979
+ childIds.push(...out.pages.map((child) => child.id));
2980
+ cursor = out.nextCursor;
2981
+ } while (cursor);
2982
+ const childNodes = importers.getNodesByPages ? await importers.getNodesByPages(childIds) : (await Promise.all(childIds.map((id) => importers.getNodeByPage(id)))).filter((n) => !!n);
2983
+ await importers.deleteNode(binding.id, holder.sourceId);
2984
+ for (const child of childNodes) if (child.bindingId === binding.id && child.parentSourceId === holder.sourceId) await importers.saveNode({
2985
+ ...child,
2986
+ parentSourceId: item.sourceId
2987
+ });
2988
+ if (fresh.body) await keepBody(fresh.id, fresh.body);
2989
+ adopted = !!fresh.body;
2990
+ } else {
2991
+ await release(fresh.id);
2992
+ if (!holder) await keepBody(fresh.id, fresh.body ?? "");
2993
+ adopted = true;
2994
+ }
2619
2995
  page = await storage.updatePage({
2620
2996
  id: fresh.id,
2621
2997
  expectedVersion: fresh.version,
@@ -2623,7 +2999,6 @@ async function applyKnowledgeImportFlush(input) {
2623
2999
  provenance
2624
3000
  });
2625
3001
  change = "updated";
2626
- adopted = true;
2627
3002
  } else if (!existing) {
2628
3003
  page = await storage.createPage({
2629
3004
  parentId: destination.page.id,
@@ -2637,9 +3012,15 @@ async function applyKnowledgeImportFlush(input) {
2637
3012
  const current = await owned(existing.pageId, item.sourceId);
2638
3013
  page = current.page;
2639
3014
  if (existing.stub && existing.adopted && !REMOVED_BODIES.has(page.body ?? "")) await keepBody(page.id, page.body ?? "");
2640
- if (!current.outside && !parentMerged && page.parentId !== destination.page.id) {
3015
+ const rekey = !!item.path && !current.outside && page.key !== key;
3016
+ if (!current.outside && !parentMerged && (page.parentId !== destination.page.id || rekey)) {
2641
3017
  if (destination.keys.length > current.keys.length && sameKeys(destination.keys.slice(0, current.keys.length), current.keys)) throw new KnowledgeImportRefusalError("invalid");
2642
- ({page, relinked} = await moveOwned(item.sourceId, page, destination));
3018
+ if (item.path) {
3019
+ const absorbed = await absorbAt(item.sourceId, destination, key, page);
3020
+ page = absorbed.page;
3021
+ if (absorbed.kept) adopted = true;
3022
+ }
3023
+ ({page, relinked} = await moveOwned(item.sourceId, page, destination, item.path ? key : void 0));
2643
3024
  moved = true;
2644
3025
  if (hasBody) {
2645
3026
  rendered = await renderBody(item);
@@ -2676,7 +3057,7 @@ async function applyKnowledgeImportFlush(input) {
2676
3057
  sourceId: item.sourceId,
2677
3058
  pageId: page.id,
2678
3059
  kind: item.kind ?? existing?.kind ?? null,
2679
- parentSourceId: item.parent ?? null,
3060
+ parentSourceId,
2680
3061
  aliases: item.aliases ?? existing?.aliases ?? [],
2681
3062
  unresolvedLinks: rendered ? rendered.unresolved : existing?.unresolvedLinks ?? [],
2682
3063
  links: rendered ? rendered.links : existing?.links ?? [],
@@ -2713,6 +3094,7 @@ async function applyKnowledgeImportFlush(input) {
2713
3094
  for (const op of [...writes, ...removes]) {
2714
3095
  const sourceId = op.op === "remove" ? op.sourceId : op.item.sourceId;
2715
3096
  const embeds = [];
3097
+ madeFolders = [];
2716
3098
  try {
2717
3099
  await storage.transaction(async () => {
2718
3100
  if (op.op === "remove") {
@@ -2790,6 +3172,21 @@ async function applyKnowledgeImportFlush(input) {
2790
3172
  return;
2791
3173
  }
2792
3174
  const written = await write(op.item, op.op === "upsert");
3175
+ for (const folder of madeFolders) {
3176
+ result.applied = true;
3177
+ result.counts.created++;
3178
+ embeds.push({
3179
+ kind: "index",
3180
+ pageId: folder.page.id
3181
+ });
3182
+ result.changes.push({
3183
+ sourceId: folder.sourceId,
3184
+ pageId: folder.page.id,
3185
+ change: "created",
3186
+ title: folder.page.name,
3187
+ path: ref(folder.keys)
3188
+ });
3189
+ }
2793
3190
  if (!written) {
2794
3191
  result.counts.merged++;
2795
3192
  return;
@@ -2873,6 +3270,11 @@ async function applyKnowledgeImportFlush(input) {
2873
3270
  }
2874
3271
  return keys;
2875
3272
  };
3273
+ const chains = importChainPaths(rows.pages);
3274
+ const pageAt = (keys) => {
3275
+ const key = importPathKey(scope.id, keys);
3276
+ return rows.paths.has(key) ? rows.paths.get(key) : chains.get(key);
3277
+ };
2876
3278
  const at = (sourceId, seen) => {
2877
3279
  if (!sourceId) return null;
2878
3280
  const node = rows.nodes.get(`${binding.id}\u0000${sourceId}`);
@@ -2888,35 +3290,79 @@ async function applyKnowledgeImportFlush(input) {
2888
3290
  if (!op || seen.has(sourceId)) return null;
2889
3291
  return {
2890
3292
  id: null,
2891
- keys: [...destinationOf(op, /* @__PURE__ */ new Set([...seen, sourceId])).keys, knowledgeImportKey(sourceId)]
3293
+ keys: [...destinationOf(op, /* @__PURE__ */ new Set([...seen, sourceId])).keys, leafOf(op)]
2892
3294
  };
2893
3295
  };
2894
- const destinationOf = (op, seen) => at(op.item.parent, seen) ?? at(op.item.fallbackParent, seen) ?? {
2895
- id: folder.id,
2896
- keys: anchorPath
3296
+ const leafOf = (op) => knowledgeImportKey(op.item.path?.at(-1) ?? op.item.sourceId);
3297
+ const destinationOf = (op, seen) => {
3298
+ if (op.item.path) {
3299
+ const keys = [...anchorPath, ...op.item.path.slice(0, -1).map(knowledgeImportKey)];
3300
+ return {
3301
+ id: op.item.path.length === 1 ? folder.id : pageAt(keys),
3302
+ keys
3303
+ };
3304
+ }
3305
+ return at(op.item.parent, seen) ?? at(op.item.fallbackParent, seen) ?? {
3306
+ id: folder.id,
3307
+ keys: anchorPath
3308
+ };
2897
3309
  };
2898
3310
  const out = {
2899
3311
  paths: [],
2900
3312
  positions: [],
2901
3313
  notes: [],
3314
+ holders: [],
2902
3315
  subtrees: [],
2903
3316
  linkTargets: removed,
2904
- bindings: removed.length > 0
3317
+ bindings: removed.length > 0,
3318
+ inPlace: removed.length === 0
2905
3319
  };
2906
3320
  const nodeAt = (sourceId) => rows.nodes.get(`${binding.id}\u0000${sourceId}`);
2907
3321
  const left = /* @__PURE__ */ new Set();
2908
3322
  for (const op of writeOps.values()) {
2909
3323
  const destination = destinationOf(op, /* @__PURE__ */ new Set([op.item.sourceId]));
3324
+ const leaf = [...destination.keys, leafOf(op)];
3325
+ const page = rows.pages.get(nodeAt(op.item.sourceId)?.pageId ?? "");
3326
+ if (destination.id && live(page ?? null) && page.parentId === destination.id && (!op.item.path || page.key === leafOf(op))) {
3327
+ if (op.item.path && op.item.path.length > 1) out.holders.push(destination.id);
3328
+ continue;
3329
+ }
3330
+ out.inPlace = false;
2910
3331
  out.paths.push({
2911
3332
  scopeId: scope.id,
2912
- path: [...destination.keys, knowledgeImportKey(op.item.sourceId)]
3333
+ path: leaf
2913
3334
  });
2914
3335
  if (destination.id) out.positions.push(destination.id);
2915
- const page = rows.pages.get(nodeAt(op.item.sourceId)?.pageId ?? "");
2916
- if (live(page ?? null) && page.parentId !== destination.id) {
3336
+ if (destination.id !== void 0 && live(page ?? null) && (page.parentId !== destination.id || op.item.path && page.key !== leafOf(op))) {
2917
3337
  out.subtrees.push(page.id);
2918
3338
  if (page.parentId) left.add(page.parentId);
2919
3339
  }
3340
+ if (!op.item.path) continue;
3341
+ out.positions.push(folder.id);
3342
+ for (let i = anchorPath.length + 1; i < leaf.length; i++) {
3343
+ out.paths.push({
3344
+ scopeId: scope.id,
3345
+ path: leaf.slice(0, i)
3346
+ });
3347
+ const prefix = pageAt(leaf.slice(0, i));
3348
+ if (prefix) out.positions.push(prefix);
3349
+ }
3350
+ const held = pageAt(leaf);
3351
+ if (held && held !== page?.id) {
3352
+ out.subtrees.push(held);
3353
+ out.notes.push(held);
3354
+ if (page) {
3355
+ out.notes.push(page.id);
3356
+ out.positions.push(page.id);
3357
+ const keys = keysOf(page);
3358
+ if (keys) {
3359
+ for (const child of rows.pages.values()) if (child?.parentId === held && live(child) && child.key !== void 0) out.paths.push({
3360
+ scopeId: scope.id,
3361
+ path: [...keys, child.key]
3362
+ });
3363
+ }
3364
+ }
3365
+ }
2920
3366
  }
2921
3367
  for (const id of out.positions) if (left.has(id)) out.subtrees.push(id);
2922
3368
  for (const sourceId of removed) {
@@ -2949,6 +3395,7 @@ async function applyKnowledgeImportFlush(input) {
2949
3395
  };
2950
3396
  const missing = emptyImportOverlayMissing();
2951
3397
  let locked = /* @__PURE__ */ new Set();
3398
+ let reread = false;
2952
3399
  try {
2953
3400
  return await real.storage.transaction(async () => {
2954
3401
  for (let pass = 0; pass < BATCH_DECIDE_PASSES; pass++) {
@@ -2960,7 +3407,8 @@ async function applyKnowledgeImportFlush(input) {
2960
3407
  pageIds: folder ? [folder.id] : [],
2961
3408
  plan,
2962
3409
  missing,
2963
- locked
3410
+ locked,
3411
+ reread
2964
3412
  });
2965
3413
  if (!rows) throw new BatchFallback("lock-churn");
2966
3414
  locked = rows.locked;
@@ -2980,6 +3428,10 @@ async function applyKnowledgeImportFlush(input) {
2980
3428
  }
2981
3429
  continue;
2982
3430
  }
3431
+ if (error instanceof KnowledgeImportRereadRequiredError) {
3432
+ reread = true;
3433
+ continue;
3434
+ }
2983
3435
  if (error instanceof KnowledgeImportBatchUnsupportedError) throw new BatchFallback(error.call);
2984
3436
  throw error;
2985
3437
  } finally {
@@ -3722,6 +4174,7 @@ function createLocalKnowledgeImporterHost(writer, options) {
3722
4174
  /** Store time (epoch ms) at which a running run stops being live under `leaseMs`. */
3723
4175
  const liveUntil = (run) => Math.min((run.heartbeatAt ?? run.startedAt).getTime() + (leaseMs ?? Infinity), run.deadlineAt?.getTime() ?? Infinity);
3724
4176
  const listBindings = async () => typeof options.bindings === "function" ? await options.bindings() : options.bindings;
4177
+ const pathWarned = /* @__PURE__ */ new Set();
3725
4178
  const storageAndImporters = async () => {
3726
4179
  const storage = await writer.getStorage();
3727
4180
  const importers = storage.importers;
@@ -3775,6 +4228,7 @@ function createLocalKnowledgeImporterHost(writer, options) {
3775
4228
  finish,
3776
4229
  status: finish.outcome === "canceled" ? "canceled" : "failed"
3777
4230
  });
4231
+ pathWarned.delete(run.id);
3778
4232
  return true;
3779
4233
  };
3780
4234
  const reclaimStaleRuns = async (importers, bindingId) => {
@@ -3932,9 +4386,11 @@ function createLocalKnowledgeImporterHost(writer, options) {
3932
4386
  let embeds = [];
3933
4387
  let settling = false;
3934
4388
  let fallbackWarning;
4389
+ let pathsUnsupported = false;
3935
4390
  try {
3936
4391
  const write = (deadlineAt) => storage.transaction(async (tx) => {
3937
4392
  fallbackWarning = void 0;
4393
+ pathsUnsupported = false;
3938
4394
  const importers = tx.importers;
3939
4395
  const guard = await importers.lockWriterGuard(peek.firstWriteAt ? "share" : "update");
3940
4396
  const run = await importers.lockRun(flush.runId);
@@ -3980,6 +4436,7 @@ function createLocalKnowledgeImporterHost(writer, options) {
3980
4436
  } } : {}
3981
4437
  });
3982
4438
  if (applied.fallback) fallbackWarning = `Knowledge importer: flush ${flush.seq} of run ${run.id} saved op by op instead of batched (${applied.fallback})`;
4439
+ pathsUnsupported = !!applied.pathsUnsupported;
3983
4440
  const settled = applied.settle;
3984
4441
  const hostUpdates = {};
3985
4442
  if (settled && (settled.cursor ?? "") !== (hostState[SETTLE_CURSOR_KEY] ?? "")) hostUpdates[SETTLE_CURSOR_KEY] = settled.cursor ?? "";
@@ -4029,6 +4486,10 @@ function createLocalKnowledgeImporterHost(writer, options) {
4029
4486
  deadlineAt = Math.min(budgetAt, error.cutoff - LEASE_CUTOFF_MARGIN_MS);
4030
4487
  }
4031
4488
  if (fallbackWarning) writer.warn(fallbackWarning);
4489
+ if (pathsUnsupported && !pathWarned.has(flush.runId)) {
4490
+ pathWarned.add(flush.runId);
4491
+ writer.warn(`Knowledge importer: run ${flush.runId} refused items placed by path: path placement needs a store that supports page re-key; ${Object.getPrototypeOf(storage).constructor.name} does not`);
4492
+ }
4032
4493
  await runEmbeds(storage, embeds);
4033
4494
  return receipt;
4034
4495
  } catch (error) {
@@ -4120,7 +4581,7 @@ function createLocalKnowledgeImporterHost(writer, options) {
4120
4581
  }
4121
4582
  const { storage } = await storageAndImporters();
4122
4583
  try {
4123
- return await storage.transaction(async (tx) => {
4584
+ const receipt = await storage.transaction(async (tx) => {
4124
4585
  const importers = tx.importers;
4125
4586
  const guard = await importers.lockWriterGuard("share");
4126
4587
  const run = await importers.lockRun(request.runId);
@@ -4164,6 +4625,8 @@ function createLocalKnowledgeImporterHost(writer, options) {
4164
4625
  });
4165
4626
  return runReceipt(finished);
4166
4627
  });
4628
+ pathWarned.delete(request.runId);
4629
+ return receipt;
4167
4630
  } catch (error) {
4168
4631
  throw hostError(error);
4169
4632
  }
@@ -5541,7 +6004,8 @@ var Knowledge = class extends require_base.MastraBase {
5541
6004
  warn: (message) => this.logger.warn(message),
5542
6005
  maxTokens: this.#tools.maxTokens,
5543
6006
  ...principals ? { principals } : {},
5544
- ...options?.author ? { author: options.author } : {}
6007
+ ...options?.author ? { author: options.author } : {},
6008
+ ...options?.requireExpectedVersion ? { requireExpectedVersion: true } : {}
5545
6009
  }, this.#groups(options), (context) => context?.requestContext?.delete?.(require_run_registry.PINNED_READ_MEMO_KEY));
5546
6010
  }
5547
6011
  /**