Mastra 推出实验生命周期钩子
Introducing Experiment Lifecycle Hooks | Mastra Blog
Mastra 为实验运行新增生命周期钩子 beforeAll、beforeEach、afterEach、afterAll,可在每个阶段配置环境,例如启动时创建 Neon 分支、每个测试用例前填充数据、结束后删除分支。此前准备测试环境需要自行编写脚本或让智能体指向生产数据,现在实验可自行创建并清理短生命周期环境。运行实验需要存储适配器持久化数据集和结果,部署到 Mastra 平台时会自动配置 Turso 数据库。
正文
当前显示原文,中文全文译文尚未就绪。
You can now configure each stage of an experiment run using lifecycle hooks: beforeAll, beforeEach, afterEach, afterAll. Seed a database, spin up a sandbox, copy a fixture — then tear it all down when the run completes.
Running experiments with mocked data is an effective way to catch regressions, but there are occasions where you'll want a production-like environment to gauge real-world behavior. Lifecycle hooks let you create and configure short-lived test environments for each experiment run.
Before lifecycle hooks, preparing a test environment for every experiment meant rolling setup scripts or pointing agents at production data. With lifecycle hooks, experiments can provision their own environment and clean them up when they're finished. An experiment runs the target against each dataset item.
Running experiments requires a storage adapter to persist datasets and experiment results.
Add Turso environment variables to your LibSQLStore config. When you deploy to the Mastra platform, a Turso database will be automatically provisioned for you:
Use hooks to configure the environment at each stage of the experiment lifecycle. This example creates a Neon branch when the experiment starts, seeds it before each test case, resets it after, and deletes the branch when the experiment ends.
For more information and full configuration options, see:
Lifecycle hooks; Hook failures; startExperiment reference